Changeset 68632b4 in mainline
- Timestamp:
- 2021-09-20T18:36:24Z (3 years ago)
- Branches:
- master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 7572eb6
- Parents:
- 6df564c
- Location:
- uspace
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/edit/edit.c
r6df564c r68632b4 1 1 /* 2 * Copyright (c) 20 09Jiri Svoboda2 * Copyright (c) 2021 Jiri Svoboda 3 3 * Copyright (c) 2012 Martin Sucha 4 4 * All rights reserved. … … 36 36 */ 37 37 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> 38 46 #include <stdio.h> 39 47 #include <stdlib.h> 40 48 #include <stddef.h> 41 49 #include <stdbool.h> 50 #include <types/common.h> 51 #include <ui/ui.h> 52 #include <ui/window.h> 42 53 #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>51 54 52 55 #include "sheet.h" … … 94 97 } pane_t; 95 98 99 /** Text editor */ 100 typedef struct { 101 /** User interface */ 102 ui_t *ui; 103 /** Editor window */ 104 ui_window_t *window; 105 } edit_t; 106 96 107 /** Document 97 108 * … … 103 114 } doc_t; 104 115 105 static console_ctrl_t *con;116 static edit_t edit; 106 117 static doc_t doc; 107 118 static bool done; … … 185 196 186 197 static void status_display(char const *str); 198 static errno_t edit_ui_create(edit_t *); 199 static void edit_ui_destroy(edit_t *); 187 200 188 201 int main(int argc, char *argv[]) 189 202 { 190 cons_event_t ev;203 // cons_event_t ev; 191 204 bool new_file; 192 205 errno_t rc; 193 206 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; 198 217 199 218 pane.rows = scr_rows - 1; … … 236 255 caret_move(sof, true, true); 237 256 257 /* Create UI */ 258 rc = edit_ui_create(&edit); 259 if (rc != EOK) 260 return 1; 261 238 262 /* Initial display */ 239 263 cursor_visible = true; 264 rc = ui_window_paint(edit.window); 265 if (rc != EOK) { 266 printf("Error painting window.\n"); 267 return rc; 268 } 240 269 241 270 cursor_hide(); 242 console_clear(con);271 // console_clear(con); 243 272 pane_text_display(); 244 273 pane_status_display(); … … 247 276 pane_caret_display(); 248 277 cursor_show(); 249 278 /* 250 279 done = false; 251 280 … … 268 297 } 269 298 270 / * Redraw as necessary. */299 / Redraw as necessary. / 271 300 272 301 cursor_hide(); … … 285 314 286 315 console_clear(con); 287 316 */ 317 318 ui_run(edit.ui); 319 320 edit_ui_destroy(&edit); 288 321 return 0; 322 } 323 324 /** Create text editor UI. 325 * 326 * @param edit Editor 327 * @return EOK on success or an error code 328 */ 329 static 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(¶ms); 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, ¶ms, &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; 417 error: 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 */ 429 static void edit_ui_destroy(edit_t *edit) 430 { 431 ui_window_destroy(edit->window); 432 ui_destroy(edit->ui); 289 433 } 290 434 … … 322 466 { 323 467 if (cursor_visible != visible) { 324 console_cursor_visibility(con, visible);468 // console_cursor_visibility(con, visible); 325 469 cursor_visible = visible; 326 470 } … … 630 774 static char *prompt(char const *prompt, char const *init_value) 631 775 { 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"); 694 777 } 695 778 … … 817 900 /* Draw rows from the sheet. */ 818 901 819 console_set_pos(con, 0, 0);902 // console_set_pos(con, 0, 0); 820 903 pane_row_range_display(0, rows); 821 904 … … 825 908 sysarg_t j; 826 909 for (i = rows; i < pane.rows; ++i) { 827 console_set_pos(con, 0, i);910 // console_set_pos(con, 0, i); 828 911 for (j = 0; j < scr_columns; ++j) 829 912 putchar(' '); 830 console_flush(con);913 // console_flush(con); 831 914 } 832 915 … … 877 960 /* Draw rows from the sheet. */ 878 961 879 console_set_pos(con, 0, 0);962 // console_set_pos(con, 0, 0); 880 963 for (i = r0; i < r1; ++i) { 881 964 /* Starting point for row display */ … … 896 979 if (coord_cmp(&csel_start, &rbc) <= 0 && 897 980 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); 901 984 } 902 985 903 console_set_pos(con, 0, i);986 // console_set_pos(con, 0, i); 904 987 size = str_size(row_buf); 905 988 pos = 0; … … 907 990 while (pos < size) { 908 991 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); 912 995 } 913 996 914 997 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); 918 1001 } 919 1002 … … 933 1016 934 1017 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); 938 1021 } 939 1022 … … 947 1030 for (j = 0; j < fill; ++j) 948 1031 putchar(' '); 949 console_flush(con);950 console_set_style(con, STYLE_NORMAL);1032 // console_flush(con); 1033 // console_set_style(con, STYLE_NORMAL); 951 1034 } 952 1035 … … 987 1070 return; 988 1071 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); 991 1074 992 1075 /* … … 1041 1124 pos = scr_columns - 1 - n; 1042 1125 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); 1045 1128 1046 1129 pane.rflags |= REDRAW_CARET; … … 1056 1139 1057 1140 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); 1060 1143 } 1061 1144 … … 1719 1802 static void status_display(char const *str) 1720 1803 { 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); 1723 1806 1724 1807 int pos = -(scr_columns - 3); 1725 1808 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); 1728 1811 1729 1812 pane.rflags |= REDRAW_CARET; -
uspace/app/edit/meson.build
r6df564c r68632b4 1 1 # 2 # Copyright (c) 2021 Jiri Svoboda 2 3 # Copyright (c) 2005 Martin Decky 3 4 # Copyright (c) 2007 Jakub Jermar … … 28 29 # 29 30 31 deps = [ 'ui' ] 30 32 src = files( 31 33 'edit.c', -
uspace/lib/ui/include/types/ui/ui.h
r6df564c r68632b4 42 42 typedef struct ui ui_t; 43 43 44 /** Use the default display service (argument to ui_create() */44 /** Use the default display service (argument to ui_create()) */ 45 45 #define UI_DISPLAY_DEFAULT NULL 46 /** Use the default console service (argument to ui_create()) */ 47 #define UI_CONSOLE_DEFAULT "cons@" 46 48 47 49 /** Window system */
Note:
See TracChangeset
for help on using the changeset viewer.