Changeset 68632b4 in mainline for uspace/app/edit/edit.c


Ignore:
Timestamp:
2021-09-20T18:36:24Z (3 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
7572eb6
Parents:
6df564c
Message:

Port Text Editor to libui (WIP 1)

Remove io/console.h calls, create UI window and run UI.

File:
1 edited

Legend:

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

    r6df564c r68632b4  
    11/*
    2  * Copyright (c) 2009 Jiri Svoboda
     2 * Copyright (c) 2021 Jiri Svoboda
    33 * Copyright (c) 2012 Martin Sucha
    44 * All rights reserved.
     
    3636 */
    3737
     38#include <align.h>
     39#include <clipboard.h>
     40#include <errno.h>
     41#include <io/kbd_event.h>
     42#include <io/keycode.h>
     43#include <io/pos_event.h>
     44#include <io/style.h>
     45#include <macros.h>
    3846#include <stdio.h>
    3947#include <stdlib.h>
    4048#include <stddef.h>
    4149#include <stdbool.h>
     50#include <types/common.h>
     51#include <ui/ui.h>
     52#include <ui/window.h>
    4253#include <vfs/vfs.h>
    43 #include <io/console.h>
    44 #include <io/style.h>
    45 #include <io/keycode.h>
    46 #include <errno.h>
    47 #include <align.h>
    48 #include <macros.h>
    49 #include <clipboard.h>
    50 #include <types/common.h>
    5154
    5255#include "sheet.h"
     
    9497} pane_t;
    9598
     99/** Text editor */
     100typedef struct {
     101        /** User interface */
     102        ui_t *ui;
     103        /** Editor window */
     104        ui_window_t *window;
     105} edit_t;
     106
    96107/** Document
    97108 *
     
    103114} doc_t;
    104115
    105 static console_ctrl_t *con;
     116static edit_t edit;
    106117static doc_t doc;
    107118static bool done;
     
    185196
    186197static void status_display(char const *str);
     198static errno_t edit_ui_create(edit_t *);
     199static void edit_ui_destroy(edit_t *);
    187200
    188201int main(int argc, char *argv[])
    189202{
    190         cons_event_t ev;
     203//      cons_event_t ev;
    191204        bool new_file;
    192205        errno_t rc;
    193206
    194         con = console_init(stdin, stdout);
    195         console_clear(con);
    196 
    197         console_get_size(con, &scr_columns, &scr_rows);
     207        (void) pos_handle;
     208        (void) key_handle_press;
     209        (void) pane_row_display;
     210
     211//      con = console_init(stdin, stdout);
     212//      console_clear(con);
     213
     214//      console_get_size(con, &scr_columns, &scr_rows);
     215        scr_columns = 80;
     216        scr_rows = 25;
    198217
    199218        pane.rows = scr_rows - 1;
     
    236255        caret_move(sof, true, true);
    237256
     257        /* Create UI */
     258        rc = edit_ui_create(&edit);
     259        if (rc != EOK)
     260                return 1;
     261
    238262        /* Initial display */
    239263        cursor_visible = true;
     264        rc = ui_window_paint(edit.window);
     265        if (rc != EOK) {
     266                printf("Error painting window.\n");
     267                return rc;
     268        }
    240269
    241270        cursor_hide();
    242         console_clear(con);
     271//      console_clear(con);
    243272        pane_text_display();
    244273        pane_status_display();
     
    247276        pane_caret_display();
    248277        cursor_show();
    249 
     278/*
    250279        done = false;
    251280
     
    268297                }
    269298
    270                 /* Redraw as necessary. */
     299                / Redraw as necessary. /
    271300
    272301                cursor_hide();
     
    285314
    286315        console_clear(con);
    287 
     316*/
     317
     318        ui_run(edit.ui);
     319
     320        edit_ui_destroy(&edit);
    288321        return 0;
     322}
     323
     324/** Create text editor UI.
     325 *
     326 * @param edit Editor
     327 * @return EOK on success or an error code
     328 */
     329static errno_t edit_ui_create(edit_t *edit)
     330{
     331        errno_t rc;
     332        ui_wnd_params_t params;
     333
     334        rc = ui_create(UI_CONSOLE_DEFAULT, &edit->ui);
     335        if (rc != EOK) {
     336                printf("Error creating UI on display %s.\n",
     337                    UI_CONSOLE_DEFAULT);
     338                goto error;
     339        }
     340
     341        ui_wnd_params_init(&params);
     342        params.caption = "Text Editor";
     343        params.rect.p0.x = 0;
     344        params.rect.p0.y = 0;
     345        params.rect.p1.x = 38;
     346        params.rect.p1.y = 18;
     347
     348        rc = ui_window_create(edit->ui, &params, &edit->window);
     349        if (rc != EOK) {
     350                printf("Error creating window.\n");
     351                goto error;
     352        }
     353/*
     354        ui_window_set_cb(window, &window_cb, (void *) &calc);
     355        calc.ui = ui;
     356
     357        ui_res = ui_window_get_res(window);
     358        calc.ui_res = ui_res;
     359
     360        rc = ui_fixed_create(&fixed);
     361        if (rc != EOK) {
     362                printf("Error creating fixed layout.\n");
     363                return rc;
     364        }
     365
     366        rc = ui_menu_bar_create(ui, window, &calc.menubar);
     367        if (rc != EOK) {
     368                printf("Error creating menu bar.\n");
     369                return rc;
     370        }
     371
     372        rc = ui_menu_create(calc.menubar, "File", &mfile);
     373        if (rc != EOK) {
     374                printf("Error creating menu.\n");
     375                return rc;
     376        }
     377
     378        rc = ui_menu_entry_create(mfile, "Exit", "Alt-F4", &mexit);
     379        if (rc != EOK) {
     380                printf("Error creating menu.\n");
     381                return rc;
     382        }
     383
     384        ui_menu_entry_set_cb(mexit, calc_file_exit, (void *) &calc);
     385
     386        rc = ui_menu_create(calc.menubar, "Edit", &medit);
     387        if (rc != EOK) {
     388                printf("Error creating menu.\n");
     389                return rc;
     390        }
     391
     392        rc = ui_menu_entry_create(medit, "Copy", "Ctrl-C", &mcopy);
     393        if (rc != EOK) {
     394                printf("Error creating menu.\n");
     395                return rc;
     396        }
     397
     398        ui_menu_entry_set_cb(mcopy, calc_edit_copy, (void *) &calc);
     399
     400        rc = ui_menu_entry_create(medit, "Paste", "Ctrl-V", &mpaste);
     401        if (rc != EOK) {
     402                printf("Error creating menu.\n");
     403                return rc;
     404        }
     405
     406        ui_menu_entry_set_cb(mpaste, calc_edit_paste, (void *) &calc);
     407
     408        ui_menu_bar_set_rect(calc.menubar, &calc.geom.menubar_rect);
     409
     410        rc = ui_fixed_add(fixed, ui_menu_bar_ctl(calc.menubar));
     411        if (rc != EOK) {
     412                printf("Error adding control to layout.\n");
     413                return rc;
     414        }
     415*/
     416        return EOK;
     417error:
     418        if (edit->window != NULL)
     419                ui_window_destroy(edit->window);
     420        if (edit->ui != NULL)
     421                ui_destroy(edit->ui);
     422        return rc;
     423}
     424
     425/** Destroy text editor UI.
     426 *
     427 * @param edit Editor
     428 */
     429static void edit_ui_destroy(edit_t *edit)
     430{
     431        ui_window_destroy(edit->window);
     432        ui_destroy(edit->ui);
    289433}
    290434
     
    322466{
    323467        if (cursor_visible != visible) {
    324                 console_cursor_visibility(con, visible);
     468//              console_cursor_visibility(con, visible);
    325469                cursor_visible = visible;
    326470        }
     
    630774static char *prompt(char const *prompt, char const *init_value)
    631775{
    632         cons_event_t ev;
    633         kbd_event_t *kev;
    634         char *str;
    635         char32_t buffer[INFNAME_MAX_LEN + 1];
    636         int max_len;
    637         int nc;
    638         bool done;
    639         errno_t rc;
    640 
    641         asprintf(&str, "%s: %s", prompt, init_value);
    642         status_display(str);
    643         console_set_pos(con, 1 + str_length(str), scr_rows - 1);
    644         free(str);
    645 
    646         console_set_style(con, STYLE_INVERTED);
    647 
    648         max_len = min(INFNAME_MAX_LEN, scr_columns - 4 - str_length(prompt));
    649         str_to_wstr(buffer, max_len + 1, init_value);
    650         nc = wstr_length(buffer);
    651         done = false;
    652 
    653         while (!done) {
    654                 rc = console_get_event(con, &ev);
    655                 if (rc != EOK)
    656                         return NULL;
    657 
    658                 if (ev.type == CEV_KEY && ev.ev.key.type == KEY_PRESS) {
    659                         kev = &ev.ev.key;
    660 
    661                         /* Handle key press. */
    662                         if ((kev->mods & (KM_CTRL | KM_ALT)) == 0) {
    663                                 switch (kev->key) {
    664                                 case KC_ESCAPE:
    665                                         return NULL;
    666                                 case KC_BACKSPACE:
    667                                         if (nc > 0) {
    668                                                 putchar('\b');
    669                                                 console_flush(con);
    670                                                 --nc;
    671                                         }
    672                                         break;
    673                                 case KC_ENTER:
    674                                         done = true;
    675                                         break;
    676                                 default:
    677                                         if (kev->c >= 32 && nc < max_len) {
    678                                                 putuchar(kev->c);
    679                                                 console_flush(con);
    680                                                 buffer[nc++] = kev->c;
    681                                         }
    682                                         break;
    683                                 }
    684                         }
    685                 }
    686         }
    687 
    688         buffer[nc] = '\0';
    689         str = wstr_to_astr(buffer);
    690 
    691         console_set_style(con, STYLE_NORMAL);
    692 
    693         return str;
     776        return str_dup("42");
    694777}
    695778
     
    817900        /* Draw rows from the sheet. */
    818901
    819         console_set_pos(con, 0, 0);
     902//      console_set_pos(con, 0, 0);
    820903        pane_row_range_display(0, rows);
    821904
     
    825908        sysarg_t j;
    826909        for (i = rows; i < pane.rows; ++i) {
    827                 console_set_pos(con, 0, i);
     910//              console_set_pos(con, 0, i);
    828911                for (j = 0; j < scr_columns; ++j)
    829912                        putchar(' ');
    830                 console_flush(con);
     913//              console_flush(con);
    831914        }
    832915
     
    877960        /* Draw rows from the sheet. */
    878961
    879         console_set_pos(con, 0, 0);
     962//      console_set_pos(con, 0, 0);
    880963        for (i = r0; i < r1; ++i) {
    881964                /* Starting point for row display */
     
    896979                if (coord_cmp(&csel_start, &rbc) <= 0 &&
    897980                    coord_cmp(&rbc, &csel_end) < 0) {
    898                         console_flush(con);
    899                         console_set_style(con, STYLE_SELECTED);
    900                         console_flush(con);
     981//                      console_flush(con);
     982//                      console_set_style(con, STYLE_SELECTED);
     983//                      console_flush(con);
    901984                }
    902985
    903                 console_set_pos(con, 0, i);
     986//              console_set_pos(con, 0, i);
    904987                size = str_size(row_buf);
    905988                pos = 0;
     
    907990                while (pos < size) {
    908991                        if ((csel_start.row == rbc.row) && (csel_start.column == s_column)) {
    909                                 console_flush(con);
    910                                 console_set_style(con, STYLE_SELECTED);
    911                                 console_flush(con);
     992//                              console_flush(con);
     993//                              console_set_style(con, STYLE_SELECTED);
     994//                              console_flush(con);
    912995                        }
    913996
    914997                        if ((csel_end.row == rbc.row) && (csel_end.column == s_column)) {
    915                                 console_flush(con);
    916                                 console_set_style(con, STYLE_NORMAL);
    917                                 console_flush(con);
     998//                              console_flush(con);
     999//                              console_set_style(con, STYLE_NORMAL);
     1000//                              console_flush(con);
    9181001                        }
    9191002
     
    9331016
    9341017                if ((csel_end.row == rbc.row) && (csel_end.column == s_column)) {
    935                         console_flush(con);
    936                         console_set_style(con, STYLE_NORMAL);
    937                         console_flush(con);
     1018//                      console_flush(con);
     1019//                      console_set_style(con, STYLE_NORMAL);
     1020//                      console_flush(con);
    9381021                }
    9391022
     
    9471030                for (j = 0; j < fill; ++j)
    9481031                        putchar(' ');
    949                 console_flush(con);
    950                 console_set_style(con, STYLE_NORMAL);
     1032//              console_flush(con);
     1033//              console_set_style(con, STYLE_NORMAL);
    9511034        }
    9521035
     
    9871070                return;
    9881071
    989         console_set_pos(con, 0, scr_rows - 1);
    990         console_set_style(con, STYLE_INVERTED);
     1072//      console_set_pos(con, 0, scr_rows - 1);
     1073//      console_set_style(con, STYLE_INVERTED);
    9911074
    9921075        /*
     
    10411124        pos = scr_columns - 1 - n;
    10421125        printf("%*s", pos, "");
    1043         console_flush(con);
    1044         console_set_style(con, STYLE_NORMAL);
     1126//      console_flush(con);
     1127//      console_set_style(con, STYLE_NORMAL);
    10451128
    10461129        pane.rflags |= REDRAW_CARET;
     
    10561139
    10571140        spt_get_coord(&caret_pt, &coord);
    1058         console_set_pos(con, coord.column - pane.sh_column,
    1059             coord.row - pane.sh_row);
     1141//      console_set_pos(con, coord.column - pane.sh_column,
     1142//          coord.row - pane.sh_row);
    10601143}
    10611144
     
    17191802static void status_display(char const *str)
    17201803{
    1721         console_set_pos(con, 0, scr_rows - 1);
    1722         console_set_style(con, STYLE_INVERTED);
     1804//      console_set_pos(con, 0, scr_rows - 1);
     1805//      console_set_style(con, STYLE_INVERTED);
    17231806
    17241807        int pos = -(scr_columns - 3);
    17251808        printf(" %*s ", pos, str);
    1726         console_flush(con);
    1727         console_set_style(con, STYLE_NORMAL);
     1809//      console_flush(con);
     1810//      console_set_style(con, STYLE_NORMAL);
    17281811
    17291812        pane.rflags |= REDRAW_CARET;
Note: See TracChangeset for help on using the changeset viewer.