Changeset 994f87b in mainline


Ignore:
Timestamp:
2024-07-19T16:13:22Z (3 months ago)
Author:
Jiri Svoboda <jiri@…>
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)
Message:

Add File/New and File/Open to text editor

Change repeat search shortcut to Ctrl-R as Ctrl-N is now used for
New File.

Location:
uspace/app/edit
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/edit/edit.c

    r705b65ea r994f87b  
    177177static void pos_handle(pos_event_t *ev);
    178178
     179static errno_t file_new(void);
     180static void file_open(void);
     181static errno_t file_open_file(const char *fname);
    179182static errno_t file_save(char const *fname);
    180183static void file_save_as(void);
    181 static errno_t file_insert(char *fname);
     184static errno_t file_insert(const char *fname);
    182185static errno_t file_save_range(char const *fname, spt_t const *spos,
    183186    spt_t const *epos);
     
    257260};
    258261
     262static void edit_file_new(ui_menu_entry_t *, void *);
     263static void edit_file_open(ui_menu_entry_t *, void *);
    259264static void edit_file_save(ui_menu_entry_t *, void *);
    260265static void edit_file_save_as(ui_menu_entry_t *, void *);
     
    281286};
    282287
     288static void open_dialog_bok(ui_file_dialog_t *, void *, const char *);
     289static void open_dialog_bcancel(ui_file_dialog_t *, void *);
     290static void open_dialog_close(ui_file_dialog_t *, void *);
     291
     292static 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
    283298static void save_as_dialog_bok(ui_file_dialog_t *, void *, const char *);
    284299static void save_as_dialog_bcancel(ui_file_dialog_t *, void *);
     
    313328int main(int argc, char *argv[])
    314329{
    315         bool new_file;
    316330        errno_t rc;
    317331
     
    319333        pane.sh_column = 1;
    320334
    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;
    333339
    334340        if (argc == 2) {
    335341                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                }
    336347        } else if (argc > 1) {
    337348                printf("Invalid arguments.\n");
    338349                return -2;
    339350        } 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        }
    360353
    361354        /* Initial display */
     
    365358                return rc;
    366359        }
    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);
    373360
    374361        ui_run(edit.ui);
     
    390377        ui_menu_t *mfile = NULL;
    391378        ui_menu_t *medit = NULL;
     379        ui_menu_entry_t *mnew = NULL;
     380        ui_menu_entry_t *mopen = NULL;
    392381        ui_menu_entry_t *msave = NULL;
    393382        ui_menu_entry_t *msaveas = NULL;
     
    451440        }
    452441
     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
    453458        rc = ui_menu_entry_create(mfile, "~S~ave", "Ctrl-S", &msave);
    454459        if (rc != EOK) {
     
    555560        ui_menu_entry_set_cb(mfindr, edit_search_reverse_find, (void *) edit);
    556561
    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);
    558563        if (rc != EOK) {
    559564                printf("Error creating menu.\n");
     
    741746                ui_quit(edit.ui);
    742747                break;
     748        case KC_N:
     749                file_new();
     750                break;
     751        case KC_O:
     752                file_open();
     753                break;
    743754        case KC_S:
    744755                if (doc.file_name != NULL)
     
    774785                search_prompt(false);
    775786                break;
    776         case KC_N:
     787        case KC_R:
    777788                search_repeat();
    778789                break;
     
    913924}
    914925
    915 /** Save the document. */
    916 static errno_t file_save(char const *fname)
    917 {
    918         spt_t sp, ep;
     926/** Create new document. */
     927static errno_t file_new(void)
     928{
    919929        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. */
     969static void file_open(void)
    944970{
    945971        const char *old_fname = (doc.file_name != NULL) ? doc.file_name : "";
     
    949975
    950976        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. */
     990static 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. */
     1043static 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. */
     1070static 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);
    9511078        fdparams.caption = "Save As";
    9521079        fdparams.ifname = old_fname;
     
    9661093 * of the caret.
    9671094 */
    968 static errno_t file_insert(char *fname)
     1095static errno_t file_insert(const char *fname)
    9691096{
    9701097        FILE *f;
     
    23122439}
    23132440
     2441/** File / New menu entry selected.
     2442 *
     2443 * @param mentry Menu entry
     2444 * @param arg Argument (edit_t *)
     2445 */
     2446static 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 */
     2460static 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
    23142468/** File / Save menu entry selected.
    23152469 *
     
    24652619        (void) arg;
    24662620        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 */
     2629static 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 */
     2661static 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 */
     2674static 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);
    24672680}
    24682681
  • uspace/app/edit/sheet.c

    r705b65ea r994f87b  
    11/*
    2  * Copyright (c) 2009 Jiri Svoboda
     2 * Copyright (c) 2024 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    9090}
    9191
     92/** Destroy sheet. */
     93void sheet_destroy(sheet_t *sh)
     94{
     95        free(sh->data);
     96        free(sh);
     97}
     98
    9299/** Insert text into sheet.
    93100 *
  • uspace/app/edit/sheet.h

    r705b65ea r994f87b  
    11/*
    2  * Copyright (c) 2009 Jiri Svoboda
     2 * Copyright (c) 2024 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    9191
    9292extern errno_t sheet_create(sheet_t **);
     93extern void sheet_destroy(sheet_t *);
    9394extern errno_t sheet_insert(sheet_t *, spt_t *, enum dir_spec, char *);
    9495extern errno_t sheet_delete(sheet_t *, spt_t *, spt_t *);
Note: See TracChangeset for help on using the changeset viewer.