Changeset 994f87b in mainline
- Timestamp:
- 2024-07-19T16:13:22Z (5 months ago)
- Branches:
- master
- Children:
- f32f89a
- Parents:
- 705b65ea
- git-author:
- Jiri Svoboda <jiri@…> (2024-07-19 16:04:36)
- git-committer:
- Jiri Svoboda <jiri@…> (2024-07-19 16:13:22)
- Location:
- uspace/app/edit
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/edit/edit.c
r705b65ea r994f87b 177 177 static void pos_handle(pos_event_t *ev); 178 178 179 static errno_t file_new(void); 180 static void file_open(void); 181 static errno_t file_open_file(const char *fname); 179 182 static errno_t file_save(char const *fname); 180 183 static void file_save_as(void); 181 static errno_t file_insert(c har *fname);184 static errno_t file_insert(const char *fname); 182 185 static errno_t file_save_range(char const *fname, spt_t const *spos, 183 186 spt_t const *epos); … … 257 260 }; 258 261 262 static void edit_file_new(ui_menu_entry_t *, void *); 263 static void edit_file_open(ui_menu_entry_t *, void *); 259 264 static void edit_file_save(ui_menu_entry_t *, void *); 260 265 static void edit_file_save_as(ui_menu_entry_t *, void *); … … 281 286 }; 282 287 288 static void open_dialog_bok(ui_file_dialog_t *, void *, const char *); 289 static void open_dialog_bcancel(ui_file_dialog_t *, void *); 290 static void open_dialog_close(ui_file_dialog_t *, void *); 291 292 static ui_file_dialog_cb_t open_dialog_cb = { 293 .bok = open_dialog_bok, 294 .bcancel = open_dialog_bcancel, 295 .close = open_dialog_close 296 }; 297 283 298 static void save_as_dialog_bok(ui_file_dialog_t *, void *, const char *); 284 299 static void save_as_dialog_bcancel(ui_file_dialog_t *, void *); … … 313 328 int main(int argc, char *argv[]) 314 329 { 315 bool new_file;316 330 errno_t rc; 317 331 … … 319 333 pane.sh_column = 1; 320 334 321 /* Start with an empty sheet. */ 322 rc = sheet_create(&doc.sh); 323 if (rc != EOK) { 324 printf("Out of memory.\n"); 325 return -1; 326 } 327 328 /* Place caret at the beginning of file. */ 329 spt_t sof; 330 pt_get_sof(&sof); 331 sheet_place_tag(doc.sh, &sof, &pane.caret_pos); 332 pane.ideal_column = 1; 335 /* Create UI */ 336 rc = edit_ui_create(&edit); 337 if (rc != EOK) 338 return 1; 333 339 334 340 if (argc == 2) { 335 341 doc.file_name = str_dup(argv[1]); 342 rc = file_open_file(argv[1]); 343 if (rc != EOK) { 344 status_display("File not found. Starting empty file."); 345 rc = file_new(); 346 } 336 347 } else if (argc > 1) { 337 348 printf("Invalid arguments.\n"); 338 349 return -2; 339 350 } else { 340 doc.file_name = NULL; 341 } 342 343 new_file = false; 344 345 if (doc.file_name == NULL || file_insert(doc.file_name) != EOK) 346 new_file = true; 347 348 /* Place selection start tag. */ 349 sheet_place_tag(doc.sh, &sof, &pane.sel_start); 350 351 /* Move to beginning of file. */ 352 pt_get_sof(&sof); 353 354 /* Create UI */ 355 rc = edit_ui_create(&edit); 356 if (rc != EOK) 357 return 1; 358 359 caret_move(sof, true, true); 351 rc = file_new(); 352 } 360 353 361 354 /* Initial display */ … … 365 358 return rc; 366 359 } 367 368 pane_status_display(&pane);369 if (new_file && doc.file_name != NULL)370 status_display("File not found. Starting empty file.");371 pane_caret_display(&pane);372 cursor_setvis(true);373 360 374 361 ui_run(edit.ui); … … 390 377 ui_menu_t *mfile = NULL; 391 378 ui_menu_t *medit = NULL; 379 ui_menu_entry_t *mnew = NULL; 380 ui_menu_entry_t *mopen = NULL; 392 381 ui_menu_entry_t *msave = NULL; 393 382 ui_menu_entry_t *msaveas = NULL; … … 451 440 } 452 441 442 rc = ui_menu_entry_create(mfile, "~N~ew", "Ctrl-N", &mnew); 443 if (rc != EOK) { 444 printf("Error creating menu.\n"); 445 return rc; 446 } 447 448 ui_menu_entry_set_cb(mnew, edit_file_new, (void *) edit); 449 450 rc = ui_menu_entry_create(mfile, "~O~pen", "Ctrl-O", &mopen); 451 if (rc != EOK) { 452 printf("Error creating menu.\n"); 453 return rc; 454 } 455 456 ui_menu_entry_set_cb(mopen, edit_file_open, (void *) edit); 457 453 458 rc = ui_menu_entry_create(mfile, "~S~ave", "Ctrl-S", &msave); 454 459 if (rc != EOK) { … … 555 560 ui_menu_entry_set_cb(mfindr, edit_search_reverse_find, (void *) edit); 556 561 557 rc = ui_menu_entry_create(msearch, "Find ~N~ext", "Ctrl- N", &mfindn);562 rc = ui_menu_entry_create(msearch, "Find ~N~ext", "Ctrl-R", &mfindn); 558 563 if (rc != EOK) { 559 564 printf("Error creating menu.\n"); … … 741 746 ui_quit(edit.ui); 742 747 break; 748 case KC_N: 749 file_new(); 750 break; 751 case KC_O: 752 file_open(); 753 break; 743 754 case KC_S: 744 755 if (doc.file_name != NULL) … … 774 785 search_prompt(false); 775 786 break; 776 case KC_ N:787 case KC_R: 777 788 search_repeat(); 778 789 break; … … 913 924 } 914 925 915 /** Save the document. */ 916 static errno_t file_save(char const *fname) 917 { 918 spt_t sp, ep; 926 /** Create new document. */ 927 static errno_t file_new(void) 928 { 919 929 errno_t rc; 920 921 status_display("Saving..."); 922 pt_get_sof(&sp); 923 pt_get_eof(&ep); 924 925 rc = file_save_range(fname, &sp, &ep); 926 927 switch (rc) { 928 case EINVAL: 929 status_display("Error opening file!"); 930 break; 931 case EIO: 932 status_display("Error writing data!"); 933 break; 934 default: 935 status_display("File saved."); 936 break; 937 } 938 939 return rc; 940 } 941 942 /** Open Save As dialog. */ 943 static void file_save_as(void) 930 sheet_t *sh; 931 932 /* Create empty sheet. */ 933 rc = sheet_create(&sh); 934 if (rc != EOK) { 935 printf("Out of memory.\n"); 936 return ENOMEM; 937 } 938 939 if (doc.sh != NULL) 940 sheet_destroy(doc.sh); 941 942 doc.sh = sh; 943 944 /* Place caret at the beginning of file. */ 945 spt_t sof; 946 pt_get_sof(&sof); 947 sheet_place_tag(doc.sh, &sof, &pane.caret_pos); 948 pane.ideal_column = 1; 949 950 doc.file_name = NULL; 951 952 /* Place selection start tag. */ 953 sheet_place_tag(doc.sh, &sof, &pane.sel_start); 954 955 /* Move to beginning of file. */ 956 pt_get_sof(&sof); 957 958 caret_move(sof, true, true); 959 960 pane_status_display(&pane); 961 pane_caret_display(&pane); 962 pane_text_display(&pane); 963 cursor_setvis(true); 964 965 return EOK; 966 } 967 968 /** Open Open File dialog. */ 969 static void file_open(void) 944 970 { 945 971 const char *old_fname = (doc.file_name != NULL) ? doc.file_name : ""; … … 949 975 950 976 ui_file_dialog_params_init(&fdparams); 977 fdparams.caption = "Open File"; 978 fdparams.ifname = old_fname; 979 980 rc = ui_file_dialog_create(edit.ui, &fdparams, &dialog); 981 if (rc != EOK) { 982 printf("Error creating message dialog.\n"); 983 return; 984 } 985 986 ui_file_dialog_set_cb(dialog, &open_dialog_cb, &edit); 987 } 988 989 /** Open exising document. */ 990 static errno_t file_open_file(const char *fname) 991 { 992 errno_t rc; 993 sheet_t *sh; 994 char *fn; 995 996 /* Create empty sheet. */ 997 rc = sheet_create(&sh); 998 if (rc != EOK) { 999 printf("Out of memory.\n"); 1000 return ENOMEM; 1001 } 1002 1003 fn = str_dup(fname); 1004 if (fn == NULL) { 1005 sheet_destroy(sh); 1006 return ENOMEM; 1007 } 1008 1009 if (doc.sh != NULL) 1010 sheet_destroy(doc.sh); 1011 1012 doc.sh = sh; 1013 1014 /* Place caret at the beginning of file. */ 1015 spt_t sof; 1016 pt_get_sof(&sof); 1017 sheet_place_tag(doc.sh, &sof, &pane.caret_pos); 1018 pane.ideal_column = 1; 1019 1020 rc = file_insert(fname); 1021 if (rc != EOK) 1022 return rc; 1023 1024 doc.file_name = fn; 1025 1026 /* Place selection start tag. */ 1027 sheet_place_tag(doc.sh, &sof, &pane.sel_start); 1028 1029 /* Move to beginning of file. */ 1030 pt_get_sof(&sof); 1031 1032 caret_move(sof, true, true); 1033 1034 pane_status_display(&pane); 1035 pane_caret_display(&pane); 1036 pane_text_display(&pane); 1037 cursor_setvis(true); 1038 1039 return EOK; 1040 } 1041 1042 /** Save the document. */ 1043 static errno_t file_save(char const *fname) 1044 { 1045 spt_t sp, ep; 1046 errno_t rc; 1047 1048 status_display("Saving..."); 1049 pt_get_sof(&sp); 1050 pt_get_eof(&ep); 1051 1052 rc = file_save_range(fname, &sp, &ep); 1053 1054 switch (rc) { 1055 case EINVAL: 1056 status_display("Error opening file!"); 1057 break; 1058 case EIO: 1059 status_display("Error writing data!"); 1060 break; 1061 default: 1062 status_display("File saved."); 1063 break; 1064 } 1065 1066 return rc; 1067 } 1068 1069 /** Open Save As dialog. */ 1070 static void file_save_as(void) 1071 { 1072 const char *old_fname = (doc.file_name != NULL) ? doc.file_name : ""; 1073 ui_file_dialog_params_t fdparams; 1074 ui_file_dialog_t *dialog; 1075 errno_t rc; 1076 1077 ui_file_dialog_params_init(&fdparams); 951 1078 fdparams.caption = "Save As"; 952 1079 fdparams.ifname = old_fname; … … 966 1093 * of the caret. 967 1094 */ 968 static errno_t file_insert(c har *fname)1095 static errno_t file_insert(const char *fname) 969 1096 { 970 1097 FILE *f; … … 2312 2439 } 2313 2440 2441 /** File / New menu entry selected. 2442 * 2443 * @param mentry Menu entry 2444 * @param arg Argument (edit_t *) 2445 */ 2446 static void edit_file_new(ui_menu_entry_t *mentry, void *arg) 2447 { 2448 edit_t *edit = (edit_t *) arg; 2449 2450 (void)edit; 2451 file_new(); 2452 (void) gfx_update(ui_window_get_gc(edit->window)); 2453 } 2454 2455 /** File / Open menu entry selected. 2456 * 2457 * @param mentry Menu entry 2458 * @param arg Argument (edit_t *) 2459 */ 2460 static void edit_file_open(ui_menu_entry_t *mentry, void *arg) 2461 { 2462 edit_t *edit = (edit_t *) arg; 2463 2464 (void)edit; 2465 file_open(); 2466 } 2467 2314 2468 /** File / Save menu entry selected. 2315 2469 * … … 2465 2619 (void) arg; 2466 2620 caret_go_to_line_ask(); 2621 } 2622 2623 /** Open File dialog OK button press. 2624 * 2625 * @param dialog Open File dialog 2626 * @param arg Argument (ui_demo_t *) 2627 * @param fname File name 2628 */ 2629 static void open_dialog_bok(ui_file_dialog_t *dialog, void *arg, 2630 const char *fname) 2631 { 2632 edit_t *edit = (edit_t *)arg; 2633 char *cname; 2634 errno_t rc; 2635 2636 (void)edit; 2637 ui_file_dialog_destroy(dialog); 2638 2639 cname = str_dup(fname); 2640 if (cname == NULL) { 2641 printf("Out of memory.\n"); 2642 return; 2643 } 2644 2645 rc = file_open_file(fname); 2646 if (rc != EOK) 2647 return; 2648 2649 if (doc.file_name != NULL) 2650 free(doc.file_name); 2651 doc.file_name = cname; 2652 2653 (void) gfx_update(ui_window_get_gc(edit->window)); 2654 } 2655 2656 /** Open File dialog cancel button press. 2657 * 2658 * @param dialog File dialog 2659 * @param arg Argument (ui_demo_t *) 2660 */ 2661 static void open_dialog_bcancel(ui_file_dialog_t *dialog, void *arg) 2662 { 2663 edit_t *edit = (edit_t *)arg; 2664 2665 (void)edit; 2666 ui_file_dialog_destroy(dialog); 2667 } 2668 2669 /** Open File dialog close request. 2670 * 2671 * @param dialog File dialog 2672 * @param arg Argument (ui_demo_t *) 2673 */ 2674 static void open_dialog_close(ui_file_dialog_t *dialog, void *arg) 2675 { 2676 edit_t *edit = (edit_t *)arg; 2677 2678 (void)edit; 2679 ui_file_dialog_destroy(dialog); 2467 2680 } 2468 2681 -
uspace/app/edit/sheet.c
r705b65ea r994f87b 1 1 /* 2 * Copyright (c) 20 09Jiri Svoboda2 * Copyright (c) 2024 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 90 90 } 91 91 92 /** Destroy sheet. */ 93 void sheet_destroy(sheet_t *sh) 94 { 95 free(sh->data); 96 free(sh); 97 } 98 92 99 /** Insert text into sheet. 93 100 * -
uspace/app/edit/sheet.h
r705b65ea r994f87b 1 1 /* 2 * Copyright (c) 20 09Jiri Svoboda2 * Copyright (c) 2024 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 91 91 92 92 extern errno_t sheet_create(sheet_t **); 93 extern void sheet_destroy(sheet_t *); 93 94 extern errno_t sheet_insert(sheet_t *, spt_t *, enum dir_spec, char *); 94 95 extern errno_t sheet_delete(sheet_t *, spt_t *, spt_t *);
Note:
See TracChangeset
for help on using the changeset viewer.