Changeset be869b0 in mainline for uspace/app/edit/edit.c


Ignore:
Timestamp:
2021-09-23T08:24:23Z (3 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
e2ca44f
Parents:
f2d6d44e
git-author:
Jiri Svoboda <jiri@…> (2021-09-22 17:24:15)
git-committer:
Jiri Svoboda <jiri@…> (2021-09-23 08:24:23)
Message:

Basic rendering of pane text

File:
1 edited

Legend:

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

    rf2d6d44e rbe869b0  
    4040#include <errno.h>
    4141#include <gfx/color.h>
     42#include <gfx/font.h>
    4243#include <gfx/render.h>
     44#include <gfx/text.h>
    4345#include <io/kbd_event.h>
    4446#include <io/keycode.h>
     
    5658#include <ui/menubar.h>
    5759#include <ui/menuentry.h>
     60#include <ui/resource.h>
    5861#include <ui/ui.h>
    5962#include <ui/window.h>
     
    7376 *
    7477 * A rectangular area of the screen used to edit a document. Different
    75  * panes can be possibly used to edit the same document.
     78 * panes can be possibly used to edit the same document. This is a custom
     79 * UI control.
    7680 */
    7781typedef struct {
     
    8791        /** Pane rectangle */
    8892        gfx_rect_t rect;
     93
     94        /** Pane color */
     95        gfx_color_t *color;
    8996
    9097        /* Pane dimensions */
     
    178185static void pane_fini(pane_t *);
    179186static ui_control_t *pane_ctl(pane_t *);
    180 static void pane_text_display(void);
     187static errno_t pane_text_display(pane_t *);
    181188static void pane_row_display(void);
    182 static void pane_row_range_display(int r0, int r1);
     189static errno_t pane_row_range_display(pane_t *, int r0, int r1);
    183190static void pane_status_display(void);
    184191static void pane_caret_display(void);
     
    315322        cursor_hide();
    316323//      console_clear(con);
    317         pane_text_display();
     324        pane_text_display(&pane);
    318325        pane_status_display();
    319326        if (new_file && doc.file_name != NULL)
     
    347354
    348355                if (pane.rflags & REDRAW_TEXT)
    349                         pane_text_display();
     356                        pane_text_display(&pane);
    350357                if (pane.rflags & REDRAW_ROW)
    351358                        pane_row_display();
     
    498505static void edit_ui_destroy(edit_t *edit)
    499506{
    500         pane_fini(&pane);
    501507        ui_window_destroy(edit->window);
    502508        ui_destroy(edit->ui);
     
    978984                return rc;
    979985
     986        rc = gfx_color_new_ega(0x07, &pane->color);
     987        if (rc != EOK) {
     988                ui_control_delete(pane->control);
     989                return rc;
     990        }
     991
    980992        pane->res = ui_window_get_res(window);
    981993        pane->window = window;
     
    987999        pane->rect.p1.y = arect.p1.y - 1;
    9881000
     1001        pane->columns = pane->rect.p1.x - pane->rect.p0.x;
     1002        pane->rows = pane->rect.p1.y - pane->rect.p0.y;
     1003
    9891004        return EOK;
    9901005}
     
    9981013static void pane_fini(pane_t *pane)
    9991014{
     1015        gfx_color_delete(pane->color);
     1016        pane->color = NULL;
    10001017        ui_control_delete(pane->control);
    10011018        pane->control = NULL;
     
    10121029}
    10131030
    1014 static void pane_text_display(void)
    1015 {
     1031/** Display pane text.
     1032 *
     1033 * @param pane Pane
     1034 * @return EOK on success or an error code
     1035 */
     1036static errno_t pane_text_display(pane_t *pane)
     1037{
     1038        gfx_rect_t rect;
     1039        gfx_context_t *gc;
     1040        errno_t rc;
    10161041        int sh_rows, rows;
    10171042
    10181043        sheet_get_num_rows(doc.sh, &sh_rows);
    1019         rows = min(sh_rows - pane.sh_row + 1, pane.rows);
     1044        rows = min(sh_rows - pane->sh_row + 1, pane->rows);
    10201045
    10211046        /* Draw rows from the sheet. */
    10221047
    1023 //      console_set_pos(con, 0, 0);
    1024         pane_row_range_display(0, rows);
     1048        rc = pane_row_range_display(pane, 0, rows);
     1049        if (rc != EOK)
     1050                return rc;
    10251051
    10261052        /* Clear the remaining rows if file is short. */
    10271053
    1028         int i;
    1029         sysarg_t j;
    1030         for (i = rows; i < pane.rows; ++i) {
    1031 //              console_set_pos(con, 0, i);
    1032                 for (j = 0; j < scr_columns; ++j)
    1033                         putchar(' ');
    1034 //              console_flush(con);
    1035         }
    1036 
    1037         pane.rflags |= (REDRAW_STATUS | REDRAW_CARET);
    1038         pane.rflags &= ~REDRAW_ROW;
     1054        gc = ui_window_get_gc(pane->window);
     1055
     1056        rc = gfx_set_color(gc, pane->color);
     1057        if (rc != EOK)
     1058                goto error;
     1059
     1060        rect.p0.x = pane->rect.p0.x;
     1061        rect.p0.y = pane->rect.p0.y + rows;
     1062        rect.p1.x = pane->rect.p1.x;
     1063        rect.p1.y = pane->rect.p1.y;
     1064
     1065        rc = gfx_fill_rect(gc, &rect);
     1066        if (rc != EOK)
     1067                goto error;
     1068
     1069        pane->rflags &= ~REDRAW_ROW;
     1070        return EOK;
     1071error:
     1072        return rc;
    10391073}
    10401074
     
    10501084
    10511085        ridx = coord.row - pane.sh_row;
    1052         pane_row_range_display(ridx, ridx + 1);
     1086        (void) pane_row_range_display(&pane, ridx, ridx + 1);
    10531087        pane.rflags |= (REDRAW_STATUS | REDRAW_CARET);
    10541088}
    10551089
    1056 static void pane_row_range_display(int r0, int r1)
    1057 {
    1058         int i, j, fill;
     1090/** Display a range of rows of text.
     1091 *
     1092 * @param r0 Start row (inclusive)
     1093 * @param r1 End row (exclusive)
     1094 * @return EOk on success or an error code
     1095 */
     1096static errno_t pane_row_range_display(pane_t *pane, int r0, int r1)
     1097{
     1098        int i, fill;
    10591099        spt_t rb, re, dep, pt;
    10601100        coord_t rbc, rec;
    10611101        char row_buf[ROW_BUF_SIZE];
     1102        char cbuf[STR_BOUNDS(1) + 1];
    10621103        char32_t c;
    10631104        size_t pos, size;
     1105        size_t cpos;
    10641106        int s_column;
    10651107        coord_t csel_start, csel_end, ctmp;
     1108        gfx_font_t *font;
     1109        gfx_context_t *gc;
     1110        gfx_text_fmt_t fmt;
     1111        gfx_coord2_t tpos;
     1112        gfx_rect_t rect;
     1113        errno_t rc;
     1114
     1115        font = ui_resource_get_font(edit.ui_res);
     1116        gc = ui_window_get_gc(edit.window);
     1117
     1118        gfx_text_fmt_init(&fmt);
     1119        fmt.color = pane->color;
    10661120
    10671121        /* Determine selection start and end. */
    10681122
    1069         tag_get_pt(&pane.sel_start, &pt);
     1123        tag_get_pt(&pane->sel_start, &pt);
    10701124        spt_get_coord(&pt, &csel_start);
    10711125
    1072         tag_get_pt(&pane.caret_pos, &pt);
     1126        tag_get_pt(&pane->caret_pos, &pt);
    10731127        spt_get_coord(&pt, &csel_end);
    10741128
     
    10831137//      console_set_pos(con, 0, 0);
    10841138        for (i = r0; i < r1; ++i) {
     1139                tpos.x = pane->rect.p0.x;
     1140                tpos.y = pane->rect.p0.y + i;
     1141
    10851142                /* Starting point for row display */
    1086                 rbc.row = pane.sh_row + i;
    1087                 rbc.column = pane.sh_column;
     1143                rbc.row = pane->sh_row + i;
     1144                rbc.column = pane->sh_column;
    10881145                sheet_get_cell_pt(doc.sh, &rbc, dir_before, &rb);
    10891146
    10901147                /* Ending point for row display */
    1091                 rec.row = pane.sh_row + i;
    1092                 rec.column = pane.sh_column + pane.columns;
     1148                rec.row = pane->sh_row + i;
     1149                rec.column = pane->sh_column + pane->columns;
    10931150                sheet_get_cell_pt(doc.sh, &rec, dir_before, &re);
    10941151
     
    11081165                size = str_size(row_buf);
    11091166                pos = 0;
    1110                 s_column = pane.sh_column;
     1167                s_column = pane->sh_column;
    11111168                while (pos < size) {
    11121169                        if ((csel_start.row == rbc.row) && (csel_start.column == s_column)) {
     
    11241181                        c = str_decode(row_buf, &pos, size);
    11251182                        if (c != '\t') {
    1126                                 printf("%lc", (wint_t) c);
     1183                                cpos = 0;
     1184                                rc = chr_encode(c, cbuf, &cpos, sizeof(cbuf));
     1185                                if (rc != EOK)
     1186                                        return rc;
     1187
     1188                                rc = gfx_puttext(font, &tpos, &fmt, cbuf);
     1189                                if (rc != EOK)
     1190                                        return rc;
     1191
    11271192                                s_column += 1;
     1193                                tpos.x++;
    11281194                        } else {
    11291195                                fill = 1 + ALIGN_UP(s_column, TAB_WIDTH) -
    11301196                                    s_column;
    11311197
    1132                                 for (j = 0; j < fill; ++j)
    1133                                         putchar(' ');
     1198                                rc = gfx_set_color(gc, fmt.color);
     1199                                if (rc != EOK)
     1200                                        return rc;
     1201
     1202                                rect.p0.x = tpos.x;
     1203                                rect.p0.y = tpos.y;
     1204                                rect.p1.x = tpos.x + fill;
     1205                                rect.p1.y = tpos.y + 1;
     1206
     1207                                rc = gfx_fill_rect(gc, &rect);
     1208                                if (rc != EOK)
     1209                                        return rc;
     1210
    11341211                                s_column += fill;
     1212                                tpos.x += fill;
    11351213                        }
    11361214                }
     
    11441222                /* Fill until the end of display area. */
    11451223
    1146                 if ((unsigned)s_column - 1 < scr_columns)
    1147                         fill = scr_columns - (s_column - 1);
    1148                 else
    1149                         fill = 0;
    1150 
    1151                 for (j = 0; j < fill; ++j)
    1152                         putchar(' ');
    1153 //              console_flush(con);
    1154 //              console_set_style(con, STYLE_NORMAL);
    1155         }
    1156 
    1157         pane.rflags |= REDRAW_CARET;
     1224                rc = gfx_set_color(gc, fmt.color);
     1225                if (rc != EOK)
     1226                        return rc;
     1227
     1228                rect.p0.x = tpos.x;
     1229                rect.p0.y = tpos.y;
     1230                rect.p1.x = pane->rect.p1.x;
     1231                rect.p1.y = tpos.y + 1;
     1232
     1233                rc = gfx_fill_rect(gc, &rect);
     1234                if (rc != EOK)
     1235                        return rc;
     1236        }
     1237
     1238        return EOK;
    11581239}
    11591240
     
    12821363{
    12831364        pane_t *pane = (pane_t *)arg;
    1284         gfx_color_t *color = NULL;
    12851365        gfx_context_t *gc;
    12861366        errno_t rc;
     
    12881368        gc = ui_window_get_gc(pane->window);
    12891369
    1290         rc = gfx_color_new_ega(0x01, &color);
    1291         if (rc != EOK)
    1292                 goto error;
    1293 
    1294         rc = gfx_set_color(gc, color);
    1295         if (rc != EOK)
    1296                 goto error;
    1297 
    1298         rc = gfx_fill_rect(gc, &pane->rect);
     1370        rc = pane_text_display(pane);
    12991371        if (rc != EOK)
    13001372                goto error;
     
    13041376                goto error;
    13051377
    1306         gfx_color_delete(color);
    1307         return EOK;
    13081378error:
    1309         if (color != NULL)
    1310                 gfx_color_delete(color);
    13111379        return rc;
    13121380}
Note: See TracChangeset for help on using the changeset viewer.