Changes in uspace/app/edit/edit.c [994f87b:ec50d65e] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/edit/edit.c
r994f87b rec50d65e 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);182 179 static errno_t file_save(char const *fname); 183 180 static void file_save_as(void); 184 static errno_t file_insert(c onst char *fname);181 static errno_t file_insert(char *fname); 185 182 static errno_t file_save_range(char const *fname, spt_t const *spos, 186 183 spt_t const *epos); … … 260 257 }; 261 258 262 static void edit_file_new(ui_menu_entry_t *, void *);263 static void edit_file_open(ui_menu_entry_t *, void *);264 259 static void edit_file_save(ui_menu_entry_t *, void *); 265 260 static void edit_file_save_as(ui_menu_entry_t *, void *); … … 286 281 }; 287 282 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_close296 };297 298 283 static void save_as_dialog_bok(ui_file_dialog_t *, void *, const char *); 299 284 static void save_as_dialog_bcancel(ui_file_dialog_t *, void *); … … 328 313 int main(int argc, char *argv[]) 329 314 { 315 bool new_file; 330 316 errno_t rc; 331 317 332 318 pane.sh_row = 1; 333 319 pane.sh_column = 1; 320 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; 333 334 if (argc == 2) { 335 doc.file_name = str_dup(argv[1]); 336 } else if (argc > 1) { 337 printf("Invalid arguments.\n"); 338 return -2; 339 } 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); 334 353 335 354 /* Create UI */ … … 338 357 return 1; 339 358 340 if (argc == 2) { 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 } 347 } else if (argc > 1) { 348 printf("Invalid arguments.\n"); 349 return -2; 350 } else { 351 rc = file_new(); 352 } 359 caret_move(sof, true, true); 353 360 354 361 /* Initial display */ … … 358 365 return rc; 359 366 } 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); 360 373 361 374 ui_run(edit.ui); … … 377 390 ui_menu_t *mfile = NULL; 378 391 ui_menu_t *medit = NULL; 379 ui_menu_entry_t *mnew = NULL;380 ui_menu_entry_t *mopen = NULL;381 392 ui_menu_entry_t *msave = NULL; 382 393 ui_menu_entry_t *msaveas = NULL; … … 440 451 } 441 452 442 rc = ui_menu_entry_create(mfile, "~ N~ew", "Ctrl-N", &mnew);453 rc = ui_menu_entry_create(mfile, "~S~ave", "Ctrl-S", &msave); 443 454 if (rc != EOK) { 444 455 printf("Error creating menu.\n"); … … 446 457 } 447 458 448 ui_menu_entry_set_cb(m new, edit_file_new, (void *) edit);449 450 rc = ui_menu_entry_create(mfile, " ~O~pen", "Ctrl-O", &mopen);459 ui_menu_entry_set_cb(msave, edit_file_save, (void *) edit); 460 461 rc = ui_menu_entry_create(mfile, "Save ~A~s", "Ctrl-E", &msaveas); 451 462 if (rc != EOK) { 452 463 printf("Error creating menu.\n"); … … 454 465 } 455 466 456 ui_menu_entry_set_cb(m open, edit_file_open, (void *) edit);457 458 rc = ui_menu_entry_ create(mfile, "~S~ave", "Ctrl-S", &msave);467 ui_menu_entry_set_cb(msaveas, edit_file_save_as, (void *) edit); 468 469 rc = ui_menu_entry_sep_create(mfile, &mfsep); 459 470 if (rc != EOK) { 460 471 printf("Error creating menu.\n"); … … 462 473 } 463 474 464 ui_menu_entry_set_cb(msave, edit_file_save, (void *) edit); 465 466 rc = ui_menu_entry_create(mfile, "Save ~A~s", "Ctrl-E", &msaveas); 475 rc = ui_menu_entry_create(mfile, "E~x~it", "Ctrl-Q", &mexit); 467 476 if (rc != EOK) { 468 477 printf("Error creating menu.\n"); … … 470 479 } 471 480 472 ui_menu_entry_set_cb(m saveas, edit_file_save_as, (void *) edit);473 474 rc = ui_menu_ entry_sep_create(mfile, &mfsep);481 ui_menu_entry_set_cb(mexit, edit_file_exit, (void *) edit); 482 483 rc = ui_menu_dd_create(edit->menubar, "~E~dit", NULL, &medit); 475 484 if (rc != EOK) { 476 485 printf("Error creating menu.\n"); … … 478 487 } 479 488 480 rc = ui_menu_entry_create(m file, "E~x~it", "Ctrl-Q", &mexit);489 rc = ui_menu_entry_create(medit, "Cu~t~", "Ctrl-X", &mcut); 481 490 if (rc != EOK) { 482 491 printf("Error creating menu.\n"); … … 484 493 } 485 494 486 ui_menu_entry_set_cb(m exit, edit_file_exit, (void *) edit);487 488 rc = ui_menu_ dd_create(edit->menubar, "~E~dit", NULL, &medit);495 ui_menu_entry_set_cb(mcut, edit_edit_cut, (void *) edit); 496 497 rc = ui_menu_entry_create(medit, "~C~opy", "Ctrl-C", &mcopy); 489 498 if (rc != EOK) { 490 499 printf("Error creating menu.\n"); … … 492 501 } 493 502 494 rc = ui_menu_entry_create(medit, "Cu~t~", "Ctrl-X", &mcut); 503 ui_menu_entry_set_cb(mcopy, edit_edit_copy, (void *) edit); 504 505 rc = ui_menu_entry_create(medit, "~P~aste", "Ctrl-V", &mpaste); 495 506 if (rc != EOK) { 496 507 printf("Error creating menu.\n"); … … 498 509 } 499 510 500 ui_menu_entry_set_cb(m cut, edit_edit_cut, (void *) edit);501 502 rc = ui_menu_entry_create(medit, "~ C~opy", "Ctrl-C", &mcopy);511 ui_menu_entry_set_cb(mpaste, edit_edit_paste, (void *) edit); 512 513 rc = ui_menu_entry_create(medit, "~D~elete", "Del", &mdelete); 503 514 if (rc != EOK) { 504 515 printf("Error creating menu.\n"); … … 506 517 } 507 518 508 ui_menu_entry_set_cb(m copy, edit_edit_copy, (void *) edit);509 510 rc = ui_menu_entry_ create(medit, "~P~aste", "Ctrl-V", &mpaste);519 ui_menu_entry_set_cb(mdelete, edit_edit_delete, (void *) edit); 520 521 rc = ui_menu_entry_sep_create(medit, &mesep); 511 522 if (rc != EOK) { 512 523 printf("Error creating menu.\n"); … … 514 525 } 515 526 516 ui_menu_entry_set_cb(mpaste, edit_edit_paste, (void *) edit); 517 518 rc = ui_menu_entry_create(medit, "~D~elete", "Del", &mdelete); 527 rc = ui_menu_entry_create(medit, "Select ~A~ll", "Ctrl-A", &mselall); 519 528 if (rc != EOK) { 520 529 printf("Error creating menu.\n"); … … 522 531 } 523 532 524 ui_menu_entry_set_cb(m delete, edit_edit_delete, (void *) edit);525 526 rc = ui_menu_ entry_sep_create(medit, &mesep);533 ui_menu_entry_set_cb(mselall, edit_edit_select_all, (void *) edit); 534 535 rc = ui_menu_dd_create(edit->menubar, "~S~earch", NULL, &msearch); 527 536 if (rc != EOK) { 528 537 printf("Error creating menu.\n"); … … 530 539 } 531 540 532 rc = ui_menu_entry_create(m edit, "Select ~A~ll", "Ctrl-A", &mselall);541 rc = ui_menu_entry_create(msearch, "~F~ind", "Ctrl-F", &mfind); 533 542 if (rc != EOK) { 534 543 printf("Error creating menu.\n"); … … 536 545 } 537 546 538 ui_menu_entry_set_cb(m selall, edit_edit_select_all, (void *) edit);539 540 rc = ui_menu_ dd_create(edit->menubar, "~S~earch", NULL, &msearch);547 ui_menu_entry_set_cb(mfind, edit_search_find, (void *) edit); 548 549 rc = ui_menu_entry_create(msearch, "~R~everse Find", "Ctrl-Shift-F", &mfindr); 541 550 if (rc != EOK) { 542 551 printf("Error creating menu.\n"); … … 544 553 } 545 554 546 rc = ui_menu_entry_create(msearch, "~F~ind", "Ctrl-F", &mfind);547 if (rc != EOK) {548 printf("Error creating menu.\n");549 return rc;550 }551 552 ui_menu_entry_set_cb(mfind, edit_search_find, (void *) edit);553 554 rc = ui_menu_entry_create(msearch, "~R~everse Find", "Ctrl-Shift-F", &mfindr);555 if (rc != EOK) {556 printf("Error creating menu.\n");557 return rc;558 }559 560 555 ui_menu_entry_set_cb(mfindr, edit_search_reverse_find, (void *) edit); 561 556 562 rc = ui_menu_entry_create(msearch, "Find ~N~ext", "Ctrl- R", &mfindn);557 rc = ui_menu_entry_create(msearch, "Find ~N~ext", "Ctrl-N", &mfindn); 563 558 if (rc != EOK) { 564 559 printf("Error creating menu.\n"); … … 746 741 ui_quit(edit.ui); 747 742 break; 748 case KC_N:749 file_new();750 break;751 case KC_O:752 file_open();753 break;754 743 case KC_S: 755 744 if (doc.file_name != NULL) … … 785 774 search_prompt(false); 786 775 break; 787 case KC_ R:776 case KC_N: 788 777 search_repeat(); 789 778 break; … … 924 913 } 925 914 926 /** Create new document. */ 927 static errno_t file_new(void) 928 { 915 /** Save the document. */ 916 static errno_t file_save(char const *fname) 917 { 918 spt_t sp, ep; 929 919 errno_t rc; 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) 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) 970 944 { 971 945 const char *old_fname = (doc.file_name != NULL) ? doc.file_name : ""; … … 975 949 976 950 ui_file_dialog_params_init(&fdparams); 977 fdparams.caption = " Open File";951 fdparams.caption = "Save As"; 978 952 fdparams.ifname = old_fname; 979 953 … … 984 958 } 985 959 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);1078 fdparams.caption = "Save As";1079 fdparams.ifname = old_fname;1080 1081 rc = ui_file_dialog_create(edit.ui, &fdparams, &dialog);1082 if (rc != EOK) {1083 printf("Error creating message dialog.\n");1084 return;1085 }1086 1087 960 ui_file_dialog_set_cb(dialog, &save_as_dialog_cb, &edit); 1088 961 } … … 1093 966 * of the caret. 1094 967 */ 1095 static errno_t file_insert(c onst char *fname)968 static errno_t file_insert(char *fname) 1096 969 { 1097 970 FILE *f; … … 2439 2312 } 2440 2313 2441 /** File / New menu entry selected.2442 *2443 * @param mentry Menu entry2444 * @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 entry2458 * @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 2468 2314 /** File / Save menu entry selected. 2469 2315 * … … 2619 2465 (void) arg; 2620 2466 caret_go_to_line_ask(); 2621 }2622 2623 /** Open File dialog OK button press.2624 *2625 * @param dialog Open File dialog2626 * @param arg Argument (ui_demo_t *)2627 * @param fname File name2628 */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 dialog2659 * @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 dialog2672 * @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 2467 } 2681 2468
Note:
See TracChangeset
for help on using the changeset viewer.