Changeset ebb1489 in mainline for uspace/app/edit/edit.c
- Timestamp:
- 2024-10-13T08:23:40Z (8 weeks ago)
- Children:
- 0472cf17
- Parents:
- 2a0c827c (diff), b3b79981 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - git-author:
- boba-buba <120932204+boba-buba@…> (2024-10-13 08:23:40)
- git-committer:
- GitHub <noreply@…> (2024-10-13 08:23:40)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/edit/edit.c
r2a0c827c rebb1489 1 1 /* 2 * Copyright (c) 202 3Jiri Svoboda2 * Copyright (c) 2024 Jiri Svoboda 3 3 * Copyright (c) 2012 Martin Sucha 4 4 * All rights reserved. … … 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); … … 238 241 239 242 static void edit_wnd_close(ui_window_t *, void *); 243 static void edit_wnd_focus(ui_window_t *, void *, unsigned); 240 244 static void edit_wnd_kbd_event(ui_window_t *, void *, kbd_event_t *); 245 static void edit_wnd_unfocus(ui_window_t *, void *, unsigned); 241 246 242 247 static ui_window_cb_t edit_window_cb = { 243 248 .close = edit_wnd_close, 244 .kbd = edit_wnd_kbd_event 249 .focus = edit_wnd_focus, 250 .kbd = edit_wnd_kbd_event, 251 .unfocus = edit_wnd_unfocus 245 252 }; 246 253 254 static void edit_menubar_activate(ui_menu_bar_t *, void *); 255 static void edit_menubar_deactivate(ui_menu_bar_t *, void *); 256 257 static ui_menu_bar_cb_t edit_menubar_cb = { 258 .activate = edit_menubar_activate, 259 .deactivate = edit_menubar_deactivate 260 }; 261 262 static void edit_file_new(ui_menu_entry_t *, void *); 263 static void edit_file_open(ui_menu_entry_t *, void *); 247 264 static void edit_file_save(ui_menu_entry_t *, void *); 248 265 static void edit_file_save_as(ui_menu_entry_t *, void *); … … 269 286 }; 270 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 271 298 static void save_as_dialog_bok(ui_file_dialog_t *, void *, const char *); 272 299 static void save_as_dialog_bcancel(ui_file_dialog_t *, void *); … … 301 328 int main(int argc, char *argv[]) 302 329 { 303 bool new_file;304 330 errno_t rc; 305 331 … … 307 333 pane.sh_column = 1; 308 334 309 /* Start with an empty sheet. */ 310 rc = sheet_create(&doc.sh); 311 if (rc != EOK) { 312 printf("Out of memory.\n"); 313 return -1; 314 } 315 316 /* Place caret at the beginning of file. */ 317 spt_t sof; 318 pt_get_sof(&sof); 319 sheet_place_tag(doc.sh, &sof, &pane.caret_pos); 320 pane.ideal_column = 1; 335 /* Create UI */ 336 rc = edit_ui_create(&edit); 337 if (rc != EOK) 338 return 1; 321 339 322 340 if (argc == 2) { 323 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 } 324 347 } else if (argc > 1) { 325 348 printf("Invalid arguments.\n"); 326 349 return -2; 327 350 } else { 328 doc.file_name = NULL; 329 } 330 331 new_file = false; 332 333 if (doc.file_name == NULL || file_insert(doc.file_name) != EOK) 334 new_file = true; 335 336 /* Place selection start tag. */ 337 sheet_place_tag(doc.sh, &sof, &pane.sel_start); 338 339 /* Move to beginning of file. */ 340 pt_get_sof(&sof); 341 342 /* Create UI */ 343 rc = edit_ui_create(&edit); 344 if (rc != EOK) 345 return 1; 346 347 caret_move(sof, true, true); 351 rc = file_new(); 352 } 348 353 349 354 /* Initial display */ … … 353 358 return rc; 354 359 } 355 356 pane_status_display(&pane);357 if (new_file && doc.file_name != NULL)358 status_display("File not found. Starting empty file.");359 pane_caret_display(&pane);360 cursor_setvis(true);361 360 362 361 ui_run(edit.ui); … … 378 377 ui_menu_t *mfile = NULL; 379 378 ui_menu_t *medit = NULL; 379 ui_menu_entry_t *mnew = NULL; 380 ui_menu_entry_t *mopen = NULL; 380 381 ui_menu_entry_t *msave = NULL; 381 382 ui_menu_entry_t *msaveas = NULL; … … 431 432 } 432 433 434 ui_menu_bar_set_cb(edit->menubar, &edit_menubar_cb, (void *) edit); 435 433 436 rc = ui_menu_dd_create(edit->menubar, "~F~ile", NULL, &mfile); 434 437 if (rc != EOK) { … … 437 440 } 438 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 439 458 rc = ui_menu_entry_create(mfile, "~S~ave", "Ctrl-S", &msave); 440 459 if (rc != EOK) { … … 541 560 ui_menu_entry_set_cb(mfindr, edit_search_reverse_find, (void *) edit); 542 561 543 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); 544 563 if (rc != EOK) { 545 564 printf("Error creating menu.\n"); … … 727 746 ui_quit(edit.ui); 728 747 break; 748 case KC_N: 749 file_new(); 750 break; 751 case KC_O: 752 file_open(); 753 break; 729 754 case KC_S: 730 755 if (doc.file_name != NULL) … … 760 785 search_prompt(false); 761 786 break; 762 case KC_ N:787 case KC_R: 763 788 search_repeat(); 764 789 break; … … 899 924 } 900 925 901 /** Save the document. */ 902 static errno_t file_save(char const *fname) 903 { 904 spt_t sp, ep; 926 /** Create new document. */ 927 static errno_t file_new(void) 928 { 905 929 errno_t rc; 906 907 status_display("Saving..."); 908 pt_get_sof(&sp); 909 pt_get_eof(&ep); 910 911 rc = file_save_range(fname, &sp, &ep); 912 913 switch (rc) { 914 case EINVAL: 915 status_display("Error opening file!"); 916 break; 917 case EIO: 918 status_display("Error writing data!"); 919 break; 920 default: 921 status_display("File saved."); 922 break; 923 } 924 925 return rc; 926 } 927 928 /** Open Save As dialog. */ 929 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) 930 970 { 931 971 const char *old_fname = (doc.file_name != NULL) ? doc.file_name : ""; … … 935 975 936 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); 937 1078 fdparams.caption = "Save As"; 938 1079 fdparams.ifname = old_fname; … … 952 1093 * of the caret. 953 1094 */ 954 static errno_t file_insert(c har *fname)1095 static errno_t file_insert(const char *fname) 955 1096 { 956 1097 FILE *f; … … 2221 2362 } 2222 2363 2364 /** Window focus event 2365 * 2366 * @param window Window 2367 * @param arg Argument (edit_t *) 2368 * @param focus Focus number 2369 */ 2370 static void edit_wnd_focus(ui_window_t *window, void *arg, unsigned focus) 2371 { 2372 edit_t *edit = (edit_t *)arg; 2373 2374 (void)edit; 2375 pane_caret_display(&pane); 2376 cursor_setvis(true); 2377 } 2378 2223 2379 /** Window keyboard event 2224 2380 * … … 2242 2398 } 2243 2399 2400 /** Window unfocus event 2401 * 2402 * @param window Window 2403 * @param arg Argument (edit_t *) 2404 * @param focus Focus number 2405 */ 2406 static void edit_wnd_unfocus(ui_window_t *window, void *arg, unsigned focus) 2407 { 2408 edit_t *edit = (edit_t *) arg; 2409 2410 (void)edit; 2411 cursor_setvis(false); 2412 } 2413 2414 /** Menu bar activate event 2415 * 2416 * @param mbar Menu bar 2417 * @param arg Argument (edit_t *) 2418 */ 2419 static void edit_menubar_activate(ui_menu_bar_t *mbar, void *arg) 2420 { 2421 edit_t *edit = (edit_t *)arg; 2422 2423 (void)edit; 2424 cursor_setvis(false); 2425 } 2426 2427 /** Menu bar deactivate event 2428 * 2429 * @param mbar Menu bar 2430 * @param arg Argument (edit_t *) 2431 */ 2432 static void edit_menubar_deactivate(ui_menu_bar_t *mbar, void *arg) 2433 { 2434 edit_t *edit = (edit_t *)arg; 2435 2436 (void)edit; 2437 pane_caret_display(&pane); 2438 cursor_setvis(true); 2439 } 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 2244 2468 /** File / Save menu entry selected. 2245 2469 * … … 2397 2621 } 2398 2622 2399 /** Save Asdialog OK button press.2400 * 2401 * @param dialog Save Asdialog2623 /** Open File dialog OK button press. 2624 * 2625 * @param dialog Open File dialog 2402 2626 * @param arg Argument (ui_demo_t *) 2403 2627 * @param fname File name 2404 2628 */ 2405 static void save_as_dialog_bok(ui_file_dialog_t *dialog, void *arg,2629 static void open_dialog_bok(ui_file_dialog_t *dialog, void *arg, 2406 2630 const char *fname) 2407 2631 { 2408 2632 edit_t *edit = (edit_t *)arg; 2409 gfx_context_t *gc = ui_window_get_gc(edit->window);2410 2633 char *cname; 2411 2634 errno_t rc; 2412 2635 2636 (void)edit; 2413 2637 ui_file_dialog_destroy(dialog); 2414 // TODO Smarter cursor management2415 pane.rflags |= REDRAW_CARET;2416 (void) pane_update(&pane);2417 gfx_cursor_set_visible(gc, true);2418 2638 2419 2639 cname = str_dup(fname); … … 2423 2643 } 2424 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); 2680 } 2681 2682 /** Save As dialog OK button press. 2683 * 2684 * @param dialog Save As dialog 2685 * @param arg Argument (ui_demo_t *) 2686 * @param fname File name 2687 */ 2688 static void save_as_dialog_bok(ui_file_dialog_t *dialog, void *arg, 2689 const char *fname) 2690 { 2691 edit_t *edit = (edit_t *)arg; 2692 char *cname; 2693 errno_t rc; 2694 2695 (void)edit; 2696 ui_file_dialog_destroy(dialog); 2697 2698 cname = str_dup(fname); 2699 if (cname == NULL) { 2700 printf("Out of memory.\n"); 2701 return; 2702 } 2703 2425 2704 rc = file_save(fname); 2426 2705 if (rc != EOK) … … 2441 2720 { 2442 2721 edit_t *edit = (edit_t *)arg; 2443 gfx_context_t *gc = ui_window_get_gc(edit->window); 2444 2722 2723 (void)edit; 2445 2724 ui_file_dialog_destroy(dialog); 2446 // TODO Smarter cursor management2447 pane.rflags |= REDRAW_CARET;2448 (void) pane_update(&pane);2449 gfx_cursor_set_visible(gc, true);2450 2725 } 2451 2726 … … 2458 2733 { 2459 2734 edit_t *edit = (edit_t *)arg; 2460 gfx_context_t *gc = ui_window_get_gc(edit->window); 2461 2735 2736 (void)edit; 2462 2737 ui_file_dialog_destroy(dialog); 2463 // TODO Smarter cursor management2464 pane.rflags |= REDRAW_CARET;2465 (void) pane_update(&pane);2466 gfx_cursor_set_visible(gc, true);2467 2738 } 2468 2739 … … 2477 2748 { 2478 2749 edit_t *edit = (edit_t *) arg; 2479 gfx_context_t *gc = ui_window_get_gc(edit->window);2480 2750 char *endptr; 2481 2751 int line; … … 2489 2759 2490 2760 caret_move_absolute(line, pane.ideal_column, dir_before, false); 2491 // TODO Smarter cursor management2761 (void)edit; 2492 2762 (void) pane_update(&pane); 2493 gfx_cursor_set_visible(gc, true);2494 (void) gfx_update(gc);2495 2763 } 2496 2764 … … 2503 2771 { 2504 2772 edit_t *edit = (edit_t *) arg; 2505 gfx_context_t *gc = ui_window_get_gc(edit->window); 2506 2773 2774 (void)edit; 2507 2775 ui_prompt_dialog_destroy(dialog); 2508 // TODO Smarter cursor management2509 pane.rflags |= REDRAW_CARET;2510 (void) pane_update(&pane);2511 gfx_cursor_set_visible(gc, true);2512 2776 } 2513 2777 … … 2520 2784 { 2521 2785 edit_t *edit = (edit_t *) arg; 2522 gfx_context_t *gc = ui_window_get_gc(edit->window); 2523 2786 2787 (void)edit; 2524 2788 ui_prompt_dialog_destroy(dialog); 2525 // TODO Smarter cursor management2526 pane.rflags |= REDRAW_CARET;2527 (void) pane_update(&pane);2528 gfx_cursor_set_visible(gc, true);2529 2789 } 2530 2790 … … 2539 2799 { 2540 2800 edit_t *edit = (edit_t *) arg; 2541 gfx_context_t *gc = ui_window_get_gc(edit->window);2542 2801 char *pattern; 2543 2802 bool reverse; 2544 2803 2804 (void)edit; 2545 2805 ui_prompt_dialog_destroy(dialog); 2546 2806 … … 2559 2819 search(pattern, reverse); 2560 2820 2561 // TODO Smarter cursor management2562 2821 (void) pane_update(&pane); 2563 gfx_cursor_set_visible(gc, true);2564 (void) gfx_update(gc);2565 2822 } 2566 2823 … … 2573 2830 { 2574 2831 edit_t *edit = (edit_t *) arg; 2575 gfx_context_t *gc = ui_window_get_gc(edit->window); 2576 2832 2833 (void)edit; 2577 2834 ui_prompt_dialog_destroy(dialog); 2578 // TODO Smarter cursor management2579 pane.rflags |= REDRAW_CARET;2580 (void) pane_update(&pane);2581 gfx_cursor_set_visible(gc, true);2582 2835 } 2583 2836 … … 2590 2843 { 2591 2844 edit_t *edit = (edit_t *) arg; 2592 gfx_context_t *gc = ui_window_get_gc(edit->window); 2593 2845 2846 (void)edit; 2594 2847 ui_prompt_dialog_destroy(dialog); 2595 // TODO Smarter cursor management2596 pane.rflags |= REDRAW_CARET;2597 (void) pane_update(&pane);2598 gfx_cursor_set_visible(gc, true);2599 2848 } 2600 2849
Note:
See TracChangeset
for help on using the changeset viewer.