Ignore:
File:
1 edited

Legend:

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

    r3c22438a re0cf963  
    11/*
    2  * Copyright (c) 2026 Jiri Svoboda
     2 * Copyright (c) 2021 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>
    6362#include <ui/menuentry.h>
    6463#include <ui/promptdialog.h>
     
    177176static void pos_handle(pos_event_t *ev);
    178177
    179 static errno_t file_new(void);
    180 static void file_open(void);
    181 static errno_t file_open_file(const char *fname);
    182178static errno_t file_save(char const *fname);
    183179static void file_save_as(void);
    184 static errno_t file_insert(const char *fname);
     180static errno_t file_insert(char *fname);
    185181static errno_t file_save_range(char const *fname, spt_t const *spos,
    186182    spt_t const *epos);
     
    240236static void edit_ui_destroy(edit_t *);
    241237
    242 static void edit_wnd_resize(ui_window_t *, void *);
    243238static void edit_wnd_close(ui_window_t *, void *);
    244 static void edit_wnd_focus(ui_window_t *, void *, unsigned);
    245239static void edit_wnd_kbd_event(ui_window_t *, void *, kbd_event_t *);
    246 static void edit_wnd_unfocus(ui_window_t *, void *, unsigned);
    247240
    248241static ui_window_cb_t edit_window_cb = {
    249         .resize = edit_wnd_resize,
    250242        .close = edit_wnd_close,
    251         .focus = edit_wnd_focus,
    252         .kbd = edit_wnd_kbd_event,
    253         .unfocus = edit_wnd_unfocus
     243        .kbd = edit_wnd_kbd_event
    254244};
    255245
    256 static void edit_menubar_activate(ui_menu_bar_t *, void *);
    257 static void edit_menubar_deactivate(ui_menu_bar_t *, void *);
    258 
    259 static ui_menu_bar_cb_t edit_menubar_cb = {
    260         .activate = edit_menubar_activate,
    261         .deactivate = edit_menubar_deactivate
    262 };
    263 
    264 static void edit_file_new(ui_menu_entry_t *, void *);
    265 static void edit_file_open(ui_menu_entry_t *, void *);
    266246static void edit_file_save(ui_menu_entry_t *, void *);
    267247static void edit_file_save_as(ui_menu_entry_t *, void *);
     
    288268};
    289269
    290 static void open_dialog_bok(ui_file_dialog_t *, void *, const char *);
    291 static void open_dialog_bcancel(ui_file_dialog_t *, void *);
    292 static void open_dialog_close(ui_file_dialog_t *, void *);
    293 
    294 static 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 
    300270static void save_as_dialog_bok(ui_file_dialog_t *, void *, const char *);
    301271static void save_as_dialog_bcancel(ui_file_dialog_t *, void *);
     
    330300int main(int argc, char *argv[])
    331301{
     302        bool new_file;
    332303        errno_t rc;
    333304
    334305        pane.sh_row = 1;
    335306        pane.sh_column = 1;
     307
     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;
     320
     321        if (argc == 2) {
     322                doc.file_name = str_dup(argv[1]);
     323        } else if (argc > 1) {
     324                printf("Invalid arguments.\n");
     325                return -2;
     326        } 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);
    336340
    337341        /* Create UI */
     
    340344                return 1;
    341345
    342         if (argc == 2) {
    343                 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                 }
    349         } else if (argc > 1) {
    350                 printf("Invalid arguments.\n");
    351                 return -2;
    352         } else {
    353                 rc = file_new();
    354         }
     346        caret_move(sof, true, true);
    355347
    356348        /* Initial display */
     
    361353        }
    362354
     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
    363361        ui_run(edit.ui);
    364362
    365363        edit_ui_destroy(&edit);
    366364        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  */
    376 static 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  */
    396 static 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);
    406365}
    407366
     
    418377        ui_menu_t *mfile = NULL;
    419378        ui_menu_t *medit = NULL;
    420         ui_menu_entry_t *mnew = NULL;
    421         ui_menu_entry_t *mopen = NULL;
    422379        ui_menu_entry_t *msave = NULL;
    423380        ui_menu_entry_t *msaveas = NULL;
     
    436393        ui_menu_entry_t *mssep = NULL;
    437394        ui_menu_entry_t *mgoto = NULL;
     395        gfx_rect_t arect;
     396        gfx_rect_t rect;
    438397
    439398        rc = ui_create(UI_CONSOLE_DEFAULT, &edit->ui);
     
    471430        }
    472431
    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);
     432        rc = ui_menu_create(edit->menubar, "File", &mfile);
    476433        if (rc != EOK) {
    477434                printf("Error creating menu.\n");
     
    479436        }
    480437
    481         rc = ui_menu_entry_create(mfile, "~N~ew", "Ctrl-N", &mnew);
     438        rc = ui_menu_entry_create(mfile, "Save", "Ctrl-S", &msave);
    482439        if (rc != EOK) {
    483440                printf("Error creating menu.\n");
     
    485442        }
    486443
    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);
     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);
    490447        if (rc != EOK) {
    491448                printf("Error creating menu.\n");
     
    493450        }
    494451
    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);
     452        ui_menu_entry_set_cb(msaveas, edit_file_save_as, (void *) edit);
     453
     454        rc = ui_menu_entry_sep_create(mfile, &mfsep);
    498455        if (rc != EOK) {
    499456                printf("Error creating menu.\n");
     
    501458        }
    502459
    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);
     460        rc = ui_menu_entry_create(mfile, "Exit", "Ctrl-Q", &mexit);
    506461        if (rc != EOK) {
    507462                printf("Error creating menu.\n");
     
    509464        }
    510465
    511         ui_menu_entry_set_cb(msaveas, edit_file_save_as, (void *) edit);
    512 
    513         rc = ui_menu_entry_sep_create(mfile, &mfsep);
     466        ui_menu_entry_set_cb(mexit, edit_file_exit, (void *) edit);
     467
     468        rc = ui_menu_create(edit->menubar, "Edit", &medit);
    514469        if (rc != EOK) {
    515470                printf("Error creating menu.\n");
     
    517472        }
    518473
    519         rc = ui_menu_entry_create(mfile, "E~x~it", "Ctrl-Q", &mexit);
     474        rc = ui_menu_entry_create(medit, "Cut", "Ctrl-X", &mcut);
    520475        if (rc != EOK) {
    521476                printf("Error creating menu.\n");
     
    523478        }
    524479
    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);
     480        ui_menu_entry_set_cb(mcut, edit_edit_cut, (void *) edit);
     481
     482        rc = ui_menu_entry_create(medit, "Copy", "Ctrl-C", &mcopy);
    528483        if (rc != EOK) {
    529484                printf("Error creating menu.\n");
     
    531486        }
    532487
    533         rc = ui_menu_entry_create(medit, "Cu~t~", "Ctrl-X", &mcut);
     488        ui_menu_entry_set_cb(mcopy, edit_edit_copy, (void *) edit);
     489
     490        rc = ui_menu_entry_create(medit, "Paste", "Ctrl-V", &mpaste);
    534491        if (rc != EOK) {
    535492                printf("Error creating menu.\n");
     
    537494        }
    538495
    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);
     496        ui_menu_entry_set_cb(mpaste, edit_edit_paste, (void *) edit);
     497
     498        rc = ui_menu_entry_create(medit, "Delete", "Del", &mdelete);
    542499        if (rc != EOK) {
    543500                printf("Error creating menu.\n");
     
    545502        }
    546503
    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);
     504        ui_menu_entry_set_cb(mdelete, edit_edit_delete, (void *) edit);
     505
     506        rc = ui_menu_entry_sep_create(medit, &mesep);
    550507        if (rc != EOK) {
    551508                printf("Error creating menu.\n");
     
    553510        }
    554511
    555         ui_menu_entry_set_cb(mpaste, edit_edit_paste, (void *) edit);
    556 
    557         rc = ui_menu_entry_create(medit, "~D~elete", "Del", &mdelete);
     512        rc = ui_menu_entry_create(medit, "Select All", "Ctrl-A", &mselall);
    558513        if (rc != EOK) {
    559514                printf("Error creating menu.\n");
     
    561516        }
    562517
    563         ui_menu_entry_set_cb(mdelete, edit_edit_delete, (void *) edit);
    564 
    565         rc = ui_menu_entry_sep_create(medit, &mesep);
     518        ui_menu_entry_set_cb(mselall, edit_edit_select_all, (void *) edit);
     519
     520        rc = ui_menu_create(edit->menubar, "Search", &msearch);
    566521        if (rc != EOK) {
    567522                printf("Error creating menu.\n");
     
    569524        }
    570525
    571         rc = ui_menu_entry_create(medit, "Select ~A~ll", "Ctrl-A", &mselall);
     526        rc = ui_menu_entry_create(msearch, "Find", "Ctrl-F", &mfind);
    572527        if (rc != EOK) {
    573528                printf("Error creating menu.\n");
     
    575530        }
    576531
    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);
     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);
    580535        if (rc != EOK) {
    581536                printf("Error creating menu.\n");
     
    583538        }
    584539
    585         rc = ui_menu_entry_create(msearch, "~F~ind", "Ctrl-F", &mfind);
     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);
    586543        if (rc != EOK) {
    587544                printf("Error creating menu.\n");
     
    589546        }
    590547
    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);
     548        ui_menu_entry_set_cb(mfindn, edit_search_find_next, (void *) edit);
     549
     550        rc = ui_menu_entry_sep_create(msearch, &mssep);
    594551        if (rc != EOK) {
    595552                printf("Error creating menu.\n");
     
    597554        }
    598555
    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);
     556        rc = ui_menu_entry_create(msearch, "Go To Line", "Ctrl-L", &mgoto);
    602557        if (rc != EOK) {
    603558                printf("Error creating menu.\n");
     
    605560        }
    606561
    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 
    621562        ui_menu_entry_set_cb(mgoto, edit_search_go_to_line, (void *) edit);
    622563
    623         edit_menubar_set_rect(edit);
     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);
    624570
    625571        rc = ui_fixed_add(fixed, ui_menu_bar_ctl(edit->menubar));
     
    647593        }
    648594
    649         edit_status_set_rect(edit);
     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);
    650599
    651600        rc = ui_fixed_add(fixed, ui_label_ctl(edit->status));
     
    777726                ui_quit(edit.ui);
    778727                break;
    779         case KC_N:
    780                 file_new();
    781                 break;
    782         case KC_O:
    783                 file_open();
    784                 break;
    785728        case KC_S:
    786729                if (doc.file_name != NULL)
     
    816759                search_prompt(false);
    817760                break;
    818         case KC_R:
     761        case KC_N:
    819762                search_repeat();
    820763                break;
     
    955898}
    956899
    957 /** Create new document. */
    958 static errno_t file_new(void)
    959 {
     900/** Save the document. */
     901static errno_t file_save(char const *fname)
     902{
     903        spt_t sp, ep;
    960904        errno_t rc;
    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. */
    1000 static void file_open(void)
     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. */
     928static void file_save_as(void)
    1001929{
    1002930        const char *old_fname = (doc.file_name != NULL) ? doc.file_name : "";
     
    1006934
    1007935        ui_file_dialog_params_init(&fdparams);
    1008         fdparams.caption = "Open File";
     936        fdparams.caption = "Save As";
    1009937        fdparams.ifname = old_fname;
    1010938
     
    1015943        }
    1016944
    1017         ui_file_dialog_set_cb(dialog, &open_dialog_cb, &edit);
    1018 }
    1019 
    1020 /** Open exising document. */
    1021 static 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. */
    1074 static 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. */
    1101 static 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);
    1109         fdparams.caption = "Save As";
    1110         fdparams.ifname = old_fname;
    1111 
    1112         rc = ui_file_dialog_create(edit.ui, &fdparams, &dialog);
    1113         if (rc != EOK) {
    1114                 printf("Error creating message dialog.\n");
    1115                 return;
    1116         }
    1117 
    1118945        ui_file_dialog_set_cb(dialog, &save_as_dialog_cb, &edit);
    1119946}
     
    1124951 * of the caret.
    1125952 */
    1126 static errno_t file_insert(const char *fname)
     953static errno_t file_insert(char *fname)
    1127954{
    1128955        FILE *f;
     
    1151978
    1152979                bcnt -= off;
    1153                 memmove(buf, buf + off, bcnt);
     980                memcpy(buf, buf + off, bcnt);
    1154981
    1155982                insert_char(c);
     
    12331060}
    12341061
    1235 static void pane_set_rect(pane_t *pane)
    1236 {
     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 */
     1070static errno_t pane_init(ui_window_t *window, pane_t *pane)
     1071{
     1072        errno_t rc;
    12371073        gfx_rect_t arect;
    12381074
    1239         ui_window_get_app_rect(pane->window, &arect);
     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);
    12401095        pane->rect.p0.x = arect.p0.x;
    12411096        pane->rect.p0.y = arect.p0.y + 1;
     
    12451100        pane->columns = pane->rect.p1.x - pane->rect.p0.x;
    12461101        pane->rows = pane->rect.p1.y - pane->rect.p0.y;
    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  */
    1257 static 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);
     1102
    12811103        return EOK;
    12821104error:
     
    12921114
    12931115        return rc;
    1294 }
    1295 
    1296 static 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);
    13041116}
    13051117
     
    14471259
    14481260        gfx_text_fmt_init(&fmt);
    1449         fmt.font = font;
    14501261        fmt.color = pane->color;
    14511262
     
    15071318                                        return rc;
    15081319
    1509                                 rc = gfx_puttext(&tpos, &fmt, cbuf);
     1320                                rc = gfx_puttext(font, &tpos, &fmt, cbuf);
    15101321                                if (rc != EOK)
    15111322                                        return rc;
     
    15971408         */
    15981409        while (true) {
    1599                 int rc = asprintf(&text, "%d, %d (%d): File '%s'. Ctrl-Q Quit  "
    1600                     "F10 Menu", coord.row, coord.column, last_row, fname);
     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);
    16011412                if (rc < 0) {
    16021413                        n = 0;
     
    23962207}
    23972208
    2398 /** Window was resized.
     2209/** Window close request
    23992210 *
    24002211 * @param window Window
    24012212 * @param arg Argument (edit_t *)
    24022213 */
    2403 static void edit_wnd_resize(ui_window_t *window, void *arg)
     2214static void edit_wnd_close(ui_window_t *window, void *arg)
    24042215{
    24052216        edit_t *edit = (edit_t *) arg;
    24062217
    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  */
    2417 static void edit_wnd_close(ui_window_t *window, void *arg)
    2418 {
    2419         edit_t *edit = (edit_t *) arg;
    2420 
    24212218        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  */
    2430 static 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);
    24372219}
    24382220
     
    24472229{
    24482230        pane.keymod = event->mods;
    2449 
    2450         if (ui_window_def_kbd(window, event) == ui_claimed)
    2451                 return;
    24522231
    24532232        if (event->type == KEY_PRESS) {
     
    24582237}
    24592238
    2460 /** Window unfocus event
    2461  *
    2462  * @param window Window
    2463  * @param arg Argument (edit_t *)
    2464  * @param focus Focus number
    2465  */
    2466 static 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  */
    2479 static 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  */
    2492 static 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  */
    2506 static 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  */
    2520 static 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 
    25282239/** File / Save menu entry selected.
    25292240 *
     
    26812392}
    26822393
    2683 /** Open File dialog OK button press.
    2684  *
    2685  * @param dialog Open File dialog
     2394/** Save As dialog OK button press.
     2395 *
     2396 * @param dialog Save As dialog
    26862397 * @param arg Argument (ui_demo_t *)
    26872398 * @param fname File name
    26882399 */
    2689 static void open_dialog_bok(ui_file_dialog_t *dialog, void *arg,
     2400static void save_as_dialog_bok(ui_file_dialog_t *dialog, void *arg,
    26902401    const char *fname)
    26912402{
    26922403        edit_t *edit = (edit_t *)arg;
     2404        gfx_context_t *gc = ui_window_get_gc(edit->window);
    26932405        char *cname;
    26942406        errno_t rc;
    26952407
    2696         (void)edit;
    26972408        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);
    26982413
    26992414        cname = str_dup(fname);
     
    27032418        }
    27042419
    2705         rc = file_open_file(fname);
     2420        rc = file_save(fname);
    27062421        if (rc != EOK)
    27072422                return;
     
    27112426        doc.file_name = cname;
    27122427
    2713         (void) gfx_update(ui_window_get_gc(edit->window));
    2714 }
    2715 
    2716 /** Open File dialog cancel button press.
     2428}
     2429
     2430/** Save As dialog cancel button press.
    27172431 *
    27182432 * @param dialog File dialog
    27192433 * @param arg Argument (ui_demo_t *)
    27202434 */
    2721 static void open_dialog_bcancel(ui_file_dialog_t *dialog, void *arg)
     2435static void save_as_dialog_bcancel(ui_file_dialog_t *dialog, void *arg)
    27222436{
    27232437        edit_t *edit = (edit_t *)arg;
    2724 
    2725         (void)edit;
     2438        gfx_context_t *gc = ui_window_get_gc(edit->window);
     2439
    27262440        ui_file_dialog_destroy(dialog);
    2727 }
    2728 
    2729 /** Open File dialog close request.
     2441        // TODO Smarter cursor management
     2442        pane.rflags |= REDRAW_CARET;
     2443        (void) pane_update(&pane);
     2444        gfx_cursor_set_visible(gc, true);
     2445}
     2446
     2447/** Save As dialog close request.
    27302448 *
    27312449 * @param dialog File dialog
    27322450 * @param arg Argument (ui_demo_t *)
    27332451 */
    2734 static void open_dialog_close(ui_file_dialog_t *dialog, void *arg)
     2452static void save_as_dialog_close(ui_file_dialog_t *dialog, void *arg)
    27352453{
    27362454        edit_t *edit = (edit_t *)arg;
    2737 
    2738         (void)edit;
     2455        gfx_context_t *gc = ui_window_get_gc(edit->window);
     2456
    27392457        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  */
    2748 static 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 
    2764         rc = file_save(fname);
    2765         if (rc != EOK)
    2766                 return;
    2767 
    2768         if (doc.file_name != NULL)
    2769                 free(doc.file_name);
    2770         doc.file_name = cname;
    2771 
    2772 }
    2773 
    2774 /** Save As dialog cancel button press.
    2775  *
    2776  * @param dialog File dialog
    2777  * @param arg Argument (ui_demo_t *)
    2778  */
    2779 static void save_as_dialog_bcancel(ui_file_dialog_t *dialog, void *arg)
    2780 {
    2781         edit_t *edit = (edit_t *)arg;
    2782 
    2783         (void)edit;
    2784         ui_file_dialog_destroy(dialog);
    2785 }
    2786 
    2787 /** Save As dialog close request.
    2788  *
    2789  * @param dialog File dialog
    2790  * @param arg Argument (ui_demo_t *)
    2791  */
    2792 static void save_as_dialog_close(ui_file_dialog_t *dialog, void *arg)
    2793 {
    2794         edit_t *edit = (edit_t *)arg;
    2795 
    2796         (void)edit;
    2797         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);
    27982462}
    27992463
     
    28082472{
    28092473        edit_t *edit = (edit_t *) arg;
     2474        gfx_context_t *gc = ui_window_get_gc(edit->window);
    28102475        char *endptr;
    28112476        int line;
     
    28192484
    28202485        caret_move_absolute(line, pane.ideal_column, dir_before, false);
    2821         (void)edit;
     2486        // TODO Smarter cursor management
    28222487        (void) pane_update(&pane);
     2488        gfx_cursor_set_visible(gc, true);
     2489        (void) gfx_update(gc);
    28232490}
    28242491
     
    28312498{
    28322499        edit_t *edit = (edit_t *) arg;
    2833 
    2834         (void)edit;
     2500        gfx_context_t *gc = ui_window_get_gc(edit->window);
     2501
    28352502        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);
    28362507}
    28372508
     
    28442515{
    28452516        edit_t *edit = (edit_t *) arg;
    2846 
    2847         (void)edit;
     2517        gfx_context_t *gc = ui_window_get_gc(edit->window);
     2518
    28482519        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);
    28492524}
    28502525
     
    28592534{
    28602535        edit_t *edit = (edit_t *) arg;
     2536        gfx_context_t *gc = ui_window_get_gc(edit->window);
    28612537        char *pattern;
    28622538        bool reverse;
    28632539
    2864         (void)edit;
    28652540        ui_prompt_dialog_destroy(dialog);
    28662541
     
    28792554        search(pattern, reverse);
    28802555
     2556        // TODO Smarter cursor management
    28812557        (void) pane_update(&pane);
     2558        gfx_cursor_set_visible(gc, true);
     2559        (void) gfx_update(gc);
    28822560}
    28832561
     
    28902568{
    28912569        edit_t *edit = (edit_t *) arg;
    2892 
    2893         (void)edit;
     2570        gfx_context_t *gc = ui_window_get_gc(edit->window);
     2571
    28942572        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);
    28952577}
    28962578
     
    29032585{
    29042586        edit_t *edit = (edit_t *) arg;
    2905 
    2906         (void)edit;
     2587        gfx_context_t *gc = ui_window_get_gc(edit->window);
     2588
    29072589        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);
    29082594}
    29092595
Note: See TracChangeset for help on using the changeset viewer.