Ignore:
File:
1 edited

Legend:

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

    re0cf963 r3c22438a  
    11/*
    2  * Copyright (c) 2021 Jiri Svoboda
     2 * Copyright (c) 2026 Jiri Svoboda
    33 * Copyright (c) 2012 Martin Sucha
    44 * All rights reserved.
     
    6060#include <ui/menu.h>
    6161#include <ui/menubar.h>
     62#include <ui/menudd.h>
    6263#include <ui/menuentry.h>
    6364#include <ui/promptdialog.h>
     
    176177static void pos_handle(pos_event_t *ev);
    177178
     179static errno_t file_new(void);
     180static void file_open(void);
     181static errno_t file_open_file(const char *fname);
    178182static errno_t file_save(char const *fname);
    179183static void file_save_as(void);
    180 static errno_t file_insert(char *fname);
     184static errno_t file_insert(const char *fname);
    181185static errno_t file_save_range(char const *fname, spt_t const *spos,
    182186    spt_t const *epos);
     
    236240static void edit_ui_destroy(edit_t *);
    237241
     242static void edit_wnd_resize(ui_window_t *, void *);
    238243static void edit_wnd_close(ui_window_t *, void *);
     244static void edit_wnd_focus(ui_window_t *, void *, unsigned);
    239245static void edit_wnd_kbd_event(ui_window_t *, void *, kbd_event_t *);
     246static void edit_wnd_unfocus(ui_window_t *, void *, unsigned);
    240247
    241248static ui_window_cb_t edit_window_cb = {
     249        .resize = edit_wnd_resize,
    242250        .close = edit_wnd_close,
    243         .kbd = edit_wnd_kbd_event
     251        .focus = edit_wnd_focus,
     252        .kbd = edit_wnd_kbd_event,
     253        .unfocus = edit_wnd_unfocus
    244254};
    245255
     256static void edit_menubar_activate(ui_menu_bar_t *, void *);
     257static void edit_menubar_deactivate(ui_menu_bar_t *, void *);
     258
     259static ui_menu_bar_cb_t edit_menubar_cb = {
     260        .activate = edit_menubar_activate,
     261        .deactivate = edit_menubar_deactivate
     262};
     263
     264static void edit_file_new(ui_menu_entry_t *, void *);
     265static void edit_file_open(ui_menu_entry_t *, void *);
    246266static void edit_file_save(ui_menu_entry_t *, void *);
    247267static void edit_file_save_as(ui_menu_entry_t *, void *);
     
    268288};
    269289
     290static void open_dialog_bok(ui_file_dialog_t *, void *, const char *);
     291static void open_dialog_bcancel(ui_file_dialog_t *, void *);
     292static void open_dialog_close(ui_file_dialog_t *, void *);
     293
     294static ui_file_dialog_cb_t open_dialog_cb = {
     295        .bok = open_dialog_bok,
     296        .bcancel = open_dialog_bcancel,
     297        .close = open_dialog_close
     298};
     299
    270300static void save_as_dialog_bok(ui_file_dialog_t *, void *, const char *);
    271301static void save_as_dialog_bcancel(ui_file_dialog_t *, void *);
     
    300330int main(int argc, char *argv[])
    301331{
    302         bool new_file;
    303332        errno_t rc;
    304333
     
    306335        pane.sh_column = 1;
    307336
    308         /* Start with an empty sheet. */
    309         rc = sheet_create(&doc.sh);
    310         if (rc != EOK) {
    311                 printf("Out of memory.\n");
    312                 return -1;
    313         }
    314 
    315         /* Place caret at the beginning of file. */
    316         spt_t sof;
    317         pt_get_sof(&sof);
    318         sheet_place_tag(doc.sh, &sof, &pane.caret_pos);
    319         pane.ideal_column = 1;
     337        /* Create UI */
     338        rc = edit_ui_create(&edit);
     339        if (rc != EOK)
     340                return 1;
    320341
    321342        if (argc == 2) {
    322343                doc.file_name = str_dup(argv[1]);
     344                rc = file_open_file(argv[1]);
     345                if (rc != EOK) {
     346                        status_display("File not found. Starting empty file.");
     347                        rc = file_new();
     348                }
    323349        } else if (argc > 1) {
    324350                printf("Invalid arguments.\n");
    325351                return -2;
    326352        } else {
    327                 doc.file_name = NULL;
    328         }
    329 
    330         new_file = false;
    331 
    332         if (doc.file_name == NULL || file_insert(doc.file_name) != EOK)
    333                 new_file = true;
    334 
    335         /* Place selection start tag. */
    336         sheet_place_tag(doc.sh, &sof, &pane.sel_start);
    337 
    338         /* Move to beginning of file. */
    339         pt_get_sof(&sof);
    340 
    341         /* Create UI */
    342         rc = edit_ui_create(&edit);
    343         if (rc != EOK)
    344                 return 1;
    345 
    346         caret_move(sof, true, true);
     353                rc = file_new();
     354        }
    347355
    348356        /* Initial display */
     
    353361        }
    354362
    355         pane_status_display(&pane);
    356         if (new_file && doc.file_name != NULL)
    357                 status_display("File not found. Starting empty file.");
    358         pane_caret_display(&pane);
    359         cursor_setvis(true);
    360 
    361363        ui_run(edit.ui);
    362364
    363365        edit_ui_destroy(&edit);
    364366        return 0;
     367}
     368
     369/** Set menu bar rectangle.
     370 *
     371 * Set the menu bar rectangle based on editor window dimensions.
     372 * Used when window is created or resized.
     373 *
     374 * @param edit Editor
     375 */
     376static void edit_menubar_set_rect(edit_t *edit)
     377{
     378        gfx_rect_t arect;
     379        gfx_rect_t rect;
     380
     381        ui_window_get_app_rect(edit->window, &arect);
     382
     383        rect.p0 = arect.p0;
     384        rect.p1.x = arect.p1.x;
     385        rect.p1.y = arect.p0.y + 1;
     386        ui_menu_bar_set_rect(edit->menubar, &rect);
     387}
     388
     389/** Set status bar rectangle.
     390 *
     391 * Set the status bar rectangle based on editor window dimensions.
     392 * Used when window is created or resized.
     393 *
     394 * @param edit Editor
     395 */
     396static void edit_status_set_rect(edit_t *edit)
     397{
     398        gfx_rect_t arect;
     399        gfx_rect_t rect;
     400
     401        ui_window_get_app_rect(edit->window, &arect);
     402        rect.p0.x = arect.p0.x;
     403        rect.p0.y = arect.p1.y - 1;
     404        rect.p1 = arect.p1;
     405        ui_label_set_rect(edit->status, &rect);
    365406}
    366407
     
    377418        ui_menu_t *mfile = NULL;
    378419        ui_menu_t *medit = NULL;
     420        ui_menu_entry_t *mnew = NULL;
     421        ui_menu_entry_t *mopen = NULL;
    379422        ui_menu_entry_t *msave = NULL;
    380423        ui_menu_entry_t *msaveas = NULL;
     
    393436        ui_menu_entry_t *mssep = NULL;
    394437        ui_menu_entry_t *mgoto = NULL;
    395         gfx_rect_t arect;
    396         gfx_rect_t rect;
    397438
    398439        rc = ui_create(UI_CONSOLE_DEFAULT, &edit->ui);
     
    430471        }
    431472
    432         rc = ui_menu_create(edit->menubar, "File", &mfile);
     473        ui_menu_bar_set_cb(edit->menubar, &edit_menubar_cb, (void *) edit);
     474
     475        rc = ui_menu_dd_create(edit->menubar, "~F~ile", NULL, &mfile);
    433476        if (rc != EOK) {
    434477                printf("Error creating menu.\n");
     
    436479        }
    437480
    438         rc = ui_menu_entry_create(mfile, "Save", "Ctrl-S", &msave);
     481        rc = ui_menu_entry_create(mfile, "~N~ew", "Ctrl-N", &mnew);
    439482        if (rc != EOK) {
    440483                printf("Error creating menu.\n");
     
    442485        }
    443486
    444         ui_menu_entry_set_cb(msave, edit_file_save, (void *) edit);
    445 
    446         rc = ui_menu_entry_create(mfile, "Save As", "Ctrl-E", &msaveas);
     487        ui_menu_entry_set_cb(mnew, edit_file_new, (void *) edit);
     488
     489        rc = ui_menu_entry_create(mfile, "~O~pen", "Ctrl-O", &mopen);
    447490        if (rc != EOK) {
    448491                printf("Error creating menu.\n");
     
    450493        }
    451494
    452         ui_menu_entry_set_cb(msaveas, edit_file_save_as, (void *) edit);
    453 
    454         rc = ui_menu_entry_sep_create(mfile, &mfsep);
     495        ui_menu_entry_set_cb(mopen, edit_file_open, (void *) edit);
     496
     497        rc = ui_menu_entry_create(mfile, "~S~ave", "Ctrl-S", &msave);
    455498        if (rc != EOK) {
    456499                printf("Error creating menu.\n");
     
    458501        }
    459502
    460         rc = ui_menu_entry_create(mfile, "Exit", "Ctrl-Q", &mexit);
     503        ui_menu_entry_set_cb(msave, edit_file_save, (void *) edit);
     504
     505        rc = ui_menu_entry_create(mfile, "Save ~A~s", "Ctrl-E", &msaveas);
    461506        if (rc != EOK) {
    462507                printf("Error creating menu.\n");
     
    464509        }
    465510
    466         ui_menu_entry_set_cb(mexit, edit_file_exit, (void *) edit);
    467 
    468         rc = ui_menu_create(edit->menubar, "Edit", &medit);
     511        ui_menu_entry_set_cb(msaveas, edit_file_save_as, (void *) edit);
     512
     513        rc = ui_menu_entry_sep_create(mfile, &mfsep);
    469514        if (rc != EOK) {
    470515                printf("Error creating menu.\n");
     
    472517        }
    473518
    474         rc = ui_menu_entry_create(medit, "Cut", "Ctrl-X", &mcut);
     519        rc = ui_menu_entry_create(mfile, "E~x~it", "Ctrl-Q", &mexit);
    475520        if (rc != EOK) {
    476521                printf("Error creating menu.\n");
     
    478523        }
    479524
    480         ui_menu_entry_set_cb(mcut, edit_edit_cut, (void *) edit);
    481 
    482         rc = ui_menu_entry_create(medit, "Copy", "Ctrl-C", &mcopy);
     525        ui_menu_entry_set_cb(mexit, edit_file_exit, (void *) edit);
     526
     527        rc = ui_menu_dd_create(edit->menubar, "~E~dit", NULL, &medit);
    483528        if (rc != EOK) {
    484529                printf("Error creating menu.\n");
     
    486531        }
    487532
    488         ui_menu_entry_set_cb(mcopy, edit_edit_copy, (void *) edit);
    489 
    490         rc = ui_menu_entry_create(medit, "Paste", "Ctrl-V", &mpaste);
     533        rc = ui_menu_entry_create(medit, "Cu~t~", "Ctrl-X", &mcut);
    491534        if (rc != EOK) {
    492535                printf("Error creating menu.\n");
     
    494537        }
    495538
    496         ui_menu_entry_set_cb(mpaste, edit_edit_paste, (void *) edit);
    497 
    498         rc = ui_menu_entry_create(medit, "Delete", "Del", &mdelete);
     539        ui_menu_entry_set_cb(mcut, edit_edit_cut, (void *) edit);
     540
     541        rc = ui_menu_entry_create(medit, "~C~opy", "Ctrl-C", &mcopy);
    499542        if (rc != EOK) {
    500543                printf("Error creating menu.\n");
     
    502545        }
    503546
    504         ui_menu_entry_set_cb(mdelete, edit_edit_delete, (void *) edit);
    505 
    506         rc = ui_menu_entry_sep_create(medit, &mesep);
     547        ui_menu_entry_set_cb(mcopy, edit_edit_copy, (void *) edit);
     548
     549        rc = ui_menu_entry_create(medit, "~P~aste", "Ctrl-V", &mpaste);
    507550        if (rc != EOK) {
    508551                printf("Error creating menu.\n");
     
    510553        }
    511554
    512         rc = ui_menu_entry_create(medit, "Select All", "Ctrl-A", &mselall);
     555        ui_menu_entry_set_cb(mpaste, edit_edit_paste, (void *) edit);
     556
     557        rc = ui_menu_entry_create(medit, "~D~elete", "Del", &mdelete);
    513558        if (rc != EOK) {
    514559                printf("Error creating menu.\n");
     
    516561        }
    517562
    518         ui_menu_entry_set_cb(mselall, edit_edit_select_all, (void *) edit);
    519 
    520         rc = ui_menu_create(edit->menubar, "Search", &msearch);
     563        ui_menu_entry_set_cb(mdelete, edit_edit_delete, (void *) edit);
     564
     565        rc = ui_menu_entry_sep_create(medit, &mesep);
    521566        if (rc != EOK) {
    522567                printf("Error creating menu.\n");
     
    524569        }
    525570
    526         rc = ui_menu_entry_create(msearch, "Find", "Ctrl-F", &mfind);
     571        rc = ui_menu_entry_create(medit, "Select ~A~ll", "Ctrl-A", &mselall);
    527572        if (rc != EOK) {
    528573                printf("Error creating menu.\n");
     
    530575        }
    531576
    532         ui_menu_entry_set_cb(mfind, edit_search_find, (void *) edit);
    533 
    534         rc = ui_menu_entry_create(msearch, "Reverse Find", "Ctrl-Shift-F", &mfindr);
     577        ui_menu_entry_set_cb(mselall, edit_edit_select_all, (void *) edit);
     578
     579        rc = ui_menu_dd_create(edit->menubar, "~S~earch", NULL, &msearch);
    535580        if (rc != EOK) {
    536581                printf("Error creating menu.\n");
     
    538583        }
    539584
    540         ui_menu_entry_set_cb(mfindr, edit_search_reverse_find, (void *) edit);
    541 
    542         rc = ui_menu_entry_create(msearch, "Find Next", "Ctrl-N", &mfindn);
     585        rc = ui_menu_entry_create(msearch, "~F~ind", "Ctrl-F", &mfind);
    543586        if (rc != EOK) {
    544587                printf("Error creating menu.\n");
     
    546589        }
    547590
    548         ui_menu_entry_set_cb(mfindn, edit_search_find_next, (void *) edit);
    549 
    550         rc = ui_menu_entry_sep_create(msearch, &mssep);
     591        ui_menu_entry_set_cb(mfind, edit_search_find, (void *) edit);
     592
     593        rc = ui_menu_entry_create(msearch, "~R~everse Find", "Ctrl-Shift-F", &mfindr);
    551594        if (rc != EOK) {
    552595                printf("Error creating menu.\n");
     
    554597        }
    555598
    556         rc = ui_menu_entry_create(msearch, "Go To Line", "Ctrl-L", &mgoto);
     599        ui_menu_entry_set_cb(mfindr, edit_search_reverse_find, (void *) edit);
     600
     601        rc = ui_menu_entry_create(msearch, "Find ~N~ext", "Ctrl-R", &mfindn);
    557602        if (rc != EOK) {
    558603                printf("Error creating menu.\n");
     
    560605        }
    561606
     607        ui_menu_entry_set_cb(mfindn, edit_search_find_next, (void *) edit);
     608
     609        rc = ui_menu_entry_sep_create(msearch, &mssep);
     610        if (rc != EOK) {
     611                printf("Error creating menu.\n");
     612                return rc;
     613        }
     614
     615        rc = ui_menu_entry_create(msearch, "Go To ~L~ine", "Ctrl-L", &mgoto);
     616        if (rc != EOK) {
     617                printf("Error creating menu.\n");
     618                return rc;
     619        }
     620
    562621        ui_menu_entry_set_cb(mgoto, edit_search_go_to_line, (void *) edit);
    563622
    564         ui_window_get_app_rect(edit->window, &arect);
    565 
    566         rect.p0 = arect.p0;
    567         rect.p1.x = arect.p1.x;
    568         rect.p1.y = arect.p0.y + 1;
    569         ui_menu_bar_set_rect(edit->menubar, &rect);
     623        edit_menubar_set_rect(edit);
    570624
    571625        rc = ui_fixed_add(fixed, ui_menu_bar_ctl(edit->menubar));
     
    593647        }
    594648
    595         rect.p0.x = arect.p0.x;
    596         rect.p0.y = arect.p1.y - 1;
    597         rect.p1 = arect.p1;
    598         ui_label_set_rect(edit->status, &rect);
     649        edit_status_set_rect(edit);
    599650
    600651        rc = ui_fixed_add(fixed, ui_label_ctl(edit->status));
     
    726777                ui_quit(edit.ui);
    727778                break;
     779        case KC_N:
     780                file_new();
     781                break;
     782        case KC_O:
     783                file_open();
     784                break;
    728785        case KC_S:
    729786                if (doc.file_name != NULL)
     
    759816                search_prompt(false);
    760817                break;
    761         case KC_N:
     818        case KC_R:
    762819                search_repeat();
    763820                break;
     
    898955}
    899956
    900 /** Save the document. */
    901 static errno_t file_save(char const *fname)
    902 {
    903         spt_t sp, ep;
     957/** Create new document. */
     958static errno_t file_new(void)
     959{
    904960        errno_t rc;
    905 
    906         status_display("Saving...");
    907         pt_get_sof(&sp);
    908         pt_get_eof(&ep);
    909 
    910         rc = file_save_range(fname, &sp, &ep);
    911 
    912         switch (rc) {
    913         case EINVAL:
    914                 status_display("Error opening file!");
    915                 break;
    916         case EIO:
    917                 status_display("Error writing data!");
    918                 break;
    919         default:
    920                 status_display("File saved.");
    921                 break;
    922         }
    923 
    924         return rc;
    925 }
    926 
    927 /** Open Save As dialog. */
    928 static void file_save_as(void)
     961        sheet_t *sh;
     962
     963        /* Create empty sheet. */
     964        rc = sheet_create(&sh);
     965        if (rc != EOK) {
     966                printf("Out of memory.\n");
     967                return ENOMEM;
     968        }
     969
     970        if (doc.sh != NULL)
     971                sheet_destroy(doc.sh);
     972
     973        doc.sh = sh;
     974
     975        /* Place caret at the beginning of file. */
     976        spt_t sof;
     977        pt_get_sof(&sof);
     978        sheet_place_tag(doc.sh, &sof, &pane.caret_pos);
     979        pane.ideal_column = 1;
     980
     981        doc.file_name = NULL;
     982
     983        /* Place selection start tag. */
     984        sheet_place_tag(doc.sh, &sof, &pane.sel_start);
     985
     986        /* Move to beginning of file. */
     987        pt_get_sof(&sof);
     988
     989        caret_move(sof, true, true);
     990
     991        pane_status_display(&pane);
     992        pane_caret_display(&pane);
     993        pane_text_display(&pane);
     994        cursor_setvis(true);
     995
     996        return EOK;
     997}
     998
     999/** Open Open File dialog. */
     1000static void file_open(void)
    9291001{
    9301002        const char *old_fname = (doc.file_name != NULL) ? doc.file_name : "";
     
    9341006
    9351007        ui_file_dialog_params_init(&fdparams);
     1008        fdparams.caption = "Open File";
     1009        fdparams.ifname = old_fname;
     1010
     1011        rc = ui_file_dialog_create(edit.ui, &fdparams, &dialog);
     1012        if (rc != EOK) {
     1013                printf("Error creating message dialog.\n");
     1014                return;
     1015        }
     1016
     1017        ui_file_dialog_set_cb(dialog, &open_dialog_cb, &edit);
     1018}
     1019
     1020/** Open exising document. */
     1021static errno_t file_open_file(const char *fname)
     1022{
     1023        errno_t rc;
     1024        sheet_t *sh;
     1025        char *fn;
     1026
     1027        /* Create empty sheet. */
     1028        rc = sheet_create(&sh);
     1029        if (rc != EOK) {
     1030                printf("Out of memory.\n");
     1031                return ENOMEM;
     1032        }
     1033
     1034        fn = str_dup(fname);
     1035        if (fn == NULL) {
     1036                sheet_destroy(sh);
     1037                return ENOMEM;
     1038        }
     1039
     1040        if (doc.sh != NULL)
     1041                sheet_destroy(doc.sh);
     1042
     1043        doc.sh = sh;
     1044
     1045        /* Place caret at the beginning of file. */
     1046        spt_t sof;
     1047        pt_get_sof(&sof);
     1048        sheet_place_tag(doc.sh, &sof, &pane.caret_pos);
     1049        pane.ideal_column = 1;
     1050
     1051        rc = file_insert(fname);
     1052        if (rc != EOK)
     1053                return rc;
     1054
     1055        doc.file_name = fn;
     1056
     1057        /* Place selection start tag. */
     1058        sheet_place_tag(doc.sh, &sof, &pane.sel_start);
     1059
     1060        /* Move to beginning of file. */
     1061        pt_get_sof(&sof);
     1062
     1063        caret_move(sof, true, true);
     1064
     1065        pane_status_display(&pane);
     1066        pane_caret_display(&pane);
     1067        pane_text_display(&pane);
     1068        cursor_setvis(true);
     1069
     1070        return EOK;
     1071}
     1072
     1073/** Save the document. */
     1074static errno_t file_save(char const *fname)
     1075{
     1076        spt_t sp, ep;
     1077        errno_t rc;
     1078
     1079        status_display("Saving...");
     1080        pt_get_sof(&sp);
     1081        pt_get_eof(&ep);
     1082
     1083        rc = file_save_range(fname, &sp, &ep);
     1084
     1085        switch (rc) {
     1086        case EINVAL:
     1087                status_display("Error opening file!");
     1088                break;
     1089        case EIO:
     1090                status_display("Error writing data!");
     1091                break;
     1092        default:
     1093                status_display("File saved.");
     1094                break;
     1095        }
     1096
     1097        return rc;
     1098}
     1099
     1100/** Open Save As dialog. */
     1101static void file_save_as(void)
     1102{
     1103        const char *old_fname = (doc.file_name != NULL) ? doc.file_name : "";
     1104        ui_file_dialog_params_t fdparams;
     1105        ui_file_dialog_t *dialog;
     1106        errno_t rc;
     1107
     1108        ui_file_dialog_params_init(&fdparams);
    9361109        fdparams.caption = "Save As";
    9371110        fdparams.ifname = old_fname;
     
    9511124 * of the caret.
    9521125 */
    953 static errno_t file_insert(char *fname)
     1126static errno_t file_insert(const char *fname)
    9541127{
    9551128        FILE *f;
     
    9781151
    9791152                bcnt -= off;
    980                 memcpy(buf, buf + off, bcnt);
     1153                memmove(buf, buf + off, bcnt);
    9811154
    9821155                insert_char(c);
     
    10601233}
    10611234
    1062 /** Initialize pane.
    1063  *
    1064  * TODO: Replace with pane_create() that allocates the pane.
    1065  *
    1066  * @param window Editor window
    1067  * @param pane Pane
    1068  * @return EOK on success or an error code
    1069  */
    1070 static errno_t pane_init(ui_window_t *window, pane_t *pane)
    1071 {
    1072         errno_t rc;
     1235static void pane_set_rect(pane_t *pane)
     1236{
    10731237        gfx_rect_t arect;
    10741238
    1075         pane->control = NULL;
    1076         pane->color = NULL;
    1077         pane->sel_color = NULL;
    1078 
    1079         rc = ui_control_new(&pane_ctl_ops, (void *) pane, &pane->control);
    1080         if (rc != EOK)
    1081                 goto error;
    1082 
    1083         rc = gfx_color_new_ega(0x07, &pane->color);
    1084         if (rc != EOK)
    1085                 goto error;
    1086 
    1087         rc = gfx_color_new_ega(0x1e, &pane->sel_color);
    1088         if (rc != EOK)
    1089                 goto error;
    1090 
    1091         pane->res = ui_window_get_res(window);
    1092         pane->window = window;
    1093 
    1094         ui_window_get_app_rect(window, &arect);
     1239        ui_window_get_app_rect(pane->window, &arect);
    10951240        pane->rect.p0.x = arect.p0.x;
    10961241        pane->rect.p0.y = arect.p0.y + 1;
     
    11001245        pane->columns = pane->rect.p1.x - pane->rect.p0.x;
    11011246        pane->rows = pane->rect.p1.y - pane->rect.p0.y;
    1102 
     1247}
     1248
     1249/** Initialize pane.
     1250 *
     1251 * TODO: Replace with pane_create() that allocates the pane.
     1252 *
     1253 * @param window Editor window
     1254 * @param pane Pane
     1255 * @return EOK on success or an error code
     1256 */
     1257static errno_t pane_init(ui_window_t *window, pane_t *pane)
     1258{
     1259        errno_t rc;
     1260
     1261        pane->control = NULL;
     1262        pane->color = NULL;
     1263        pane->sel_color = NULL;
     1264
     1265        rc = ui_control_new(&pane_ctl_ops, (void *) pane, &pane->control);
     1266        if (rc != EOK)
     1267                goto error;
     1268
     1269        rc = gfx_color_new_ega(0x07, &pane->color);
     1270        if (rc != EOK)
     1271                goto error;
     1272
     1273        rc = gfx_color_new_ega(0x1e, &pane->sel_color);
     1274        if (rc != EOK)
     1275                goto error;
     1276
     1277        pane->res = ui_window_get_res(window);
     1278        pane->window = window;
     1279
     1280        pane_set_rect(pane);
    11031281        return EOK;
    11041282error:
     
    11141292
    11151293        return rc;
     1294}
     1295
     1296static void pane_resize(pane_t *pane)
     1297{
     1298        pane_set_rect(pane);
     1299
     1300        /* Scroll in case cursor is now outside of the editor pane. */
     1301        caret_move_relative(0, 0, dir_before, true);
     1302
     1303        pane_caret_display(pane);
    11161304}
    11171305
     
    12591447
    12601448        gfx_text_fmt_init(&fmt);
     1449        fmt.font = font;
    12611450        fmt.color = pane->color;
    12621451
     
    13181507                                        return rc;
    13191508
    1320                                 rc = gfx_puttext(font, &tpos, &fmt, cbuf);
     1509                                rc = gfx_puttext(&tpos, &fmt, cbuf);
    13211510                                if (rc != EOK)
    13221511                                        return rc;
     
    14081597         */
    14091598        while (true) {
    1410                 int rc = asprintf(&text, "%d, %d (%d): File '%s'. Ctrl-Q Quit  Ctrl-S Save  "
    1411                     "Ctrl-E Save As", coord.row, coord.column, last_row, fname);
     1599                int rc = asprintf(&text, "%d, %d (%d): File '%s'. Ctrl-Q Quit  "
     1600                    "F10 Menu", coord.row, coord.column, last_row, fname);
    14121601                if (rc < 0) {
    14131602                        n = 0;
     
    22072396}
    22082397
    2209 /** Window close request
     2398/** Window was resized.
    22102399 *
    22112400 * @param window Window
    22122401 * @param arg Argument (edit_t *)
    22132402 */
     2403static void edit_wnd_resize(ui_window_t *window, void *arg)
     2404{
     2405        edit_t *edit = (edit_t *) arg;
     2406
     2407        edit_menubar_set_rect(edit);
     2408        edit_status_set_rect(edit);
     2409        pane_resize(&pane);
     2410}
     2411
     2412/** Window close request
     2413 *
     2414 * @param window Window
     2415 * @param arg Argument (edit_t *)
     2416 */
    22142417static void edit_wnd_close(ui_window_t *window, void *arg)
    22152418{
     
    22172420
    22182421        ui_quit(edit->ui);
     2422}
     2423
     2424/** Window focus event
     2425 *
     2426 * @param window Window
     2427 * @param arg Argument (edit_t *)
     2428 * @param focus Focus number
     2429 */
     2430static void edit_wnd_focus(ui_window_t *window, void *arg, unsigned focus)
     2431{
     2432        edit_t *edit = (edit_t *)arg;
     2433
     2434        (void)edit;
     2435        pane_caret_display(&pane);
     2436        cursor_setvis(true);
    22192437}
    22202438
     
    22292447{
    22302448        pane.keymod = event->mods;
     2449
     2450        if (ui_window_def_kbd(window, event) == ui_claimed)
     2451                return;
    22312452
    22322453        if (event->type == KEY_PRESS) {
     
    22372458}
    22382459
     2460/** Window unfocus event
     2461 *
     2462 * @param window Window
     2463 * @param arg Argument (edit_t *)
     2464 * @param focus Focus number
     2465 */
     2466static void edit_wnd_unfocus(ui_window_t *window, void *arg, unsigned focus)
     2467{
     2468        edit_t *edit = (edit_t *) arg;
     2469
     2470        (void)edit;
     2471        cursor_setvis(false);
     2472}
     2473
     2474/** Menu bar activate event
     2475 *
     2476 * @param mbar Menu bar
     2477 * @param arg Argument (edit_t *)
     2478 */
     2479static void edit_menubar_activate(ui_menu_bar_t *mbar, void *arg)
     2480{
     2481        edit_t *edit = (edit_t *)arg;
     2482
     2483        (void)edit;
     2484        cursor_setvis(false);
     2485}
     2486
     2487/** Menu bar deactivate event
     2488 *
     2489 * @param mbar Menu bar
     2490 * @param arg Argument (edit_t *)
     2491 */
     2492static void edit_menubar_deactivate(ui_menu_bar_t *mbar, void *arg)
     2493{
     2494        edit_t *edit = (edit_t *)arg;
     2495
     2496        (void)edit;
     2497        pane_caret_display(&pane);
     2498        cursor_setvis(true);
     2499}
     2500
     2501/** File / New menu entry selected.
     2502 *
     2503 * @param mentry Menu entry
     2504 * @param arg Argument (edit_t *)
     2505 */
     2506static void edit_file_new(ui_menu_entry_t *mentry, void *arg)
     2507{
     2508        edit_t *edit = (edit_t *) arg;
     2509
     2510        (void)edit;
     2511        file_new();
     2512        (void) gfx_update(ui_window_get_gc(edit->window));
     2513}
     2514
     2515/** File / Open menu entry selected.
     2516 *
     2517 * @param mentry Menu entry
     2518 * @param arg Argument (edit_t *)
     2519 */
     2520static void edit_file_open(ui_menu_entry_t *mentry, void *arg)
     2521{
     2522        edit_t *edit = (edit_t *) arg;
     2523
     2524        (void)edit;
     2525        file_open();
     2526}
     2527
    22392528/** File / Save menu entry selected.
    22402529 *
     
    23922681}
    23932682
    2394 /** Save As dialog OK button press.
    2395  *
    2396  * @param dialog Save As dialog
     2683/** Open File dialog OK button press.
     2684 *
     2685 * @param dialog Open File dialog
    23972686 * @param arg Argument (ui_demo_t *)
    23982687 * @param fname File name
    23992688 */
    2400 static void save_as_dialog_bok(ui_file_dialog_t *dialog, void *arg,
     2689static void open_dialog_bok(ui_file_dialog_t *dialog, void *arg,
    24012690    const char *fname)
    24022691{
    24032692        edit_t *edit = (edit_t *)arg;
    2404         gfx_context_t *gc = ui_window_get_gc(edit->window);
    24052693        char *cname;
    24062694        errno_t rc;
    24072695
     2696        (void)edit;
    24082697        ui_file_dialog_destroy(dialog);
    2409         // TODO Smarter cursor management
    2410         pane.rflags |= REDRAW_CARET;
    2411         (void) pane_update(&pane);
    2412         gfx_cursor_set_visible(gc, true);
    24132698
    24142699        cname = str_dup(fname);
     
    24182703        }
    24192704
     2705        rc = file_open_file(fname);
     2706        if (rc != EOK)
     2707                return;
     2708
     2709        if (doc.file_name != NULL)
     2710                free(doc.file_name);
     2711        doc.file_name = cname;
     2712
     2713        (void) gfx_update(ui_window_get_gc(edit->window));
     2714}
     2715
     2716/** Open File dialog cancel button press.
     2717 *
     2718 * @param dialog File dialog
     2719 * @param arg Argument (ui_demo_t *)
     2720 */
     2721static void open_dialog_bcancel(ui_file_dialog_t *dialog, void *arg)
     2722{
     2723        edit_t *edit = (edit_t *)arg;
     2724
     2725        (void)edit;
     2726        ui_file_dialog_destroy(dialog);
     2727}
     2728
     2729/** Open File dialog close request.
     2730 *
     2731 * @param dialog File dialog
     2732 * @param arg Argument (ui_demo_t *)
     2733 */
     2734static void open_dialog_close(ui_file_dialog_t *dialog, void *arg)
     2735{
     2736        edit_t *edit = (edit_t *)arg;
     2737
     2738        (void)edit;
     2739        ui_file_dialog_destroy(dialog);
     2740}
     2741
     2742/** Save As dialog OK button press.
     2743 *
     2744 * @param dialog Save As dialog
     2745 * @param arg Argument (ui_demo_t *)
     2746 * @param fname File name
     2747 */
     2748static void save_as_dialog_bok(ui_file_dialog_t *dialog, void *arg,
     2749    const char *fname)
     2750{
     2751        edit_t *edit = (edit_t *)arg;
     2752        char *cname;
     2753        errno_t rc;
     2754
     2755        (void)edit;
     2756        ui_file_dialog_destroy(dialog);
     2757
     2758        cname = str_dup(fname);
     2759        if (cname == NULL) {
     2760                printf("Out of memory.\n");
     2761                return;
     2762        }
     2763
    24202764        rc = file_save(fname);
    24212765        if (rc != EOK)
     
    24362780{
    24372781        edit_t *edit = (edit_t *)arg;
    2438         gfx_context_t *gc = ui_window_get_gc(edit->window);
    2439 
     2782
     2783        (void)edit;
    24402784        ui_file_dialog_destroy(dialog);
    2441         // TODO Smarter cursor management
    2442         pane.rflags |= REDRAW_CARET;
    2443         (void) pane_update(&pane);
    2444         gfx_cursor_set_visible(gc, true);
    24452785}
    24462786
     
    24532793{
    24542794        edit_t *edit = (edit_t *)arg;
    2455         gfx_context_t *gc = ui_window_get_gc(edit->window);
    2456 
     2795
     2796        (void)edit;
    24572797        ui_file_dialog_destroy(dialog);
    2458         // TODO Smarter cursor management
    2459         pane.rflags |= REDRAW_CARET;
    2460         (void) pane_update(&pane);
    2461         gfx_cursor_set_visible(gc, true);
    24622798}
    24632799
     
    24722808{
    24732809        edit_t *edit = (edit_t *) arg;
    2474         gfx_context_t *gc = ui_window_get_gc(edit->window);
    24752810        char *endptr;
    24762811        int line;
     
    24842819
    24852820        caret_move_absolute(line, pane.ideal_column, dir_before, false);
    2486         // TODO Smarter cursor management
     2821        (void)edit;
    24872822        (void) pane_update(&pane);
    2488         gfx_cursor_set_visible(gc, true);
    2489         (void) gfx_update(gc);
    24902823}
    24912824
     
    24982831{
    24992832        edit_t *edit = (edit_t *) arg;
    2500         gfx_context_t *gc = ui_window_get_gc(edit->window);
    2501 
     2833
     2834        (void)edit;
    25022835        ui_prompt_dialog_destroy(dialog);
    2503         // TODO Smarter cursor management
    2504         pane.rflags |= REDRAW_CARET;
    2505         (void) pane_update(&pane);
    2506         gfx_cursor_set_visible(gc, true);
    25072836}
    25082837
     
    25152844{
    25162845        edit_t *edit = (edit_t *) arg;
    2517         gfx_context_t *gc = ui_window_get_gc(edit->window);
    2518 
     2846
     2847        (void)edit;
    25192848        ui_prompt_dialog_destroy(dialog);
    2520         // TODO Smarter cursor management
    2521         pane.rflags |= REDRAW_CARET;
    2522         (void) pane_update(&pane);
    2523         gfx_cursor_set_visible(gc, true);
    25242849}
    25252850
     
    25342859{
    25352860        edit_t *edit = (edit_t *) arg;
    2536         gfx_context_t *gc = ui_window_get_gc(edit->window);
    25372861        char *pattern;
    25382862        bool reverse;
    25392863
     2864        (void)edit;
    25402865        ui_prompt_dialog_destroy(dialog);
    25412866
     
    25542879        search(pattern, reverse);
    25552880
    2556         // TODO Smarter cursor management
    25572881        (void) pane_update(&pane);
    2558         gfx_cursor_set_visible(gc, true);
    2559         (void) gfx_update(gc);
    25602882}
    25612883
     
    25682890{
    25692891        edit_t *edit = (edit_t *) arg;
    2570         gfx_context_t *gc = ui_window_get_gc(edit->window);
    2571 
     2892
     2893        (void)edit;
    25722894        ui_prompt_dialog_destroy(dialog);
    2573         // TODO Smarter cursor management
    2574         pane.rflags |= REDRAW_CARET;
    2575         (void) pane_update(&pane);
    2576         gfx_cursor_set_visible(gc, true);
    25772895}
    25782896
     
    25852903{
    25862904        edit_t *edit = (edit_t *) arg;
    2587         gfx_context_t *gc = ui_window_get_gc(edit->window);
    2588 
     2905
     2906        (void)edit;
    25892907        ui_prompt_dialog_destroy(dialog);
    2590         // TODO Smarter cursor management
    2591         pane.rflags |= REDRAW_CARET;
    2592         (void) pane_update(&pane);
    2593         gfx_cursor_set_visible(gc, true);
    25942908}
    25952909
Note: See TracChangeset for help on using the changeset viewer.