1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** GUIX Component                                                        */
16 /**                                                                       */
17 /**   Multi Line Text Input Management (Multi Line Text Input)            */
18 /**                                                                       */
19 /**************************************************************************/
20 
21 #define GX_SOURCE_CODE
22 
23 
24 /* Include necessary system files.  */
25 
26 #include "gx_api.h"
27 #include "gx_system.h"
28 #include "gx_widget.h"
29 #include "gx_window.h"
30 #include "gx_utility.h"
31 #include "gx_multi_line_text_input.h"
32 #include "gx_multi_line_text_view.h"
33 #include "gx_utility.h"
34 #include "gx_scrollbar.h"
35 
36 /**************************************************************************/
37 /*                                                                        */
38 /*  FUNCTION                                               RELEASE        */
39 /*                                                                        */
40 /*    _gx_multi_line_text_input_delete                    PORTABLE C      */
41 /*                                                           6.1          */
42 /*  AUTHOR                                                                */
43 /*                                                                        */
44 /*    Kenneth Maxwell, Microsoft Corporation                              */
45 /*                                                                        */
46 /*  DESCRIPTION                                                           */
47 /*                                                                        */
48 /*    This function deletes a character after the cursor.                 */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    text_input                        Multi line text input             */
53 /*                                        control block                   */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    None                                                                */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _gx_widget_font_get                   Retrieve font                 */
62 /*    _gx_window_client_width_get           Get the client width          */
63 /*    _gx_window_scrollbar_find             Find scrollbar for a window   */
64 /*    _gx_scrollbar_reset                   Reset scrollbar information   */
65 /*    _gx_multi_line_text_input_char_remove Remove characters from input  */
66 /*                                            string                      */
67 /*    _gx_multi_line_text_view_display_info_get                           */
68 /*                                          Get the number of characters  */
69 /*                                            that a line can display     */
70 /*    _gx_multi_line_text_view_string_total_rows_compute                  */
71 /*                                          Calculate total rows of input */
72 /*                                            string                      */
73 /*    _gx_multi_line_text_view_line_cache_update                          */
74 /*                                          Update line cache             */
75 /*    _gx_multi_line_text_input_cursor_pos_update                         */
76 /*                                          Update cursor position        */
77 /*                                            according to insert position*/
78 /*    _gx_system_dirty_partial_add          Add one dirty area to dirty   */
79 /*                                            list                        */
80 /*    _gx_utility_utf8_string_character_get Parses utf8 string to         */
81 /*                                            multi-byte glyph            */
82 /*    _gx_utility_string_length_check       Test string length            */
83 /*                                                                        */
84 /*  CALLED BY                                                             */
85 /*                                                                        */
86 /*    _gx_multi_line_text_input_keydown_process                           */
87 /*                                                                        */
88 /*  RELEASE HISTORY                                                       */
89 /*                                                                        */
90 /*    DATE              NAME                      DESCRIPTION             */
91 /*                                                                        */
92 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
93 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
94 /*                                            resulting in version 6.1    */
95 /*                                                                        */
96 /**************************************************************************/
_gx_multi_line_text_input_delete(GX_MULTI_LINE_TEXT_INPUT * text_input)97 UINT _gx_multi_line_text_input_delete(GX_MULTI_LINE_TEXT_INPUT *text_input)
98 {
99 GX_TEXT_INPUT_CURSOR    *cursor_ptr = &text_input -> gx_multi_line_text_input_cursor_instance;
100 GX_MULTI_LINE_TEXT_VIEW *view = (GX_MULTI_LINE_TEXT_VIEW *)text_input;
101 UINT                     insert_pos;
102 UINT                     string_size;
103 GX_RECTANGLE             client;
104 GX_RECTANGLE             dirty_rect;
105 GX_FONT                 *font;
106 GX_VALUE                 line_height;
107 UINT                     old_text_rows;
108 INT                      shift;
109 INT                      last_visible_line;
110 UINT                     glyph_len = 1;
111 GX_SCROLLBAR            *scroll;
112 GX_STRING                string;
113 UINT                     start_mark = text_input -> gx_multi_line_text_input_start_mark;
114 UINT                     end_mark = text_input -> gx_multi_line_text_input_end_mark;
115 
116     if (end_mark > start_mark)
117     {
118         return _gx_multi_line_text_input_backspace(text_input);
119     }
120 
121     insert_pos =  text_input -> gx_multi_line_text_input_text_insert_position;
122     string_size = text_input -> gx_multi_line_text_view_text.gx_string_length;
123     if (insert_pos < string_size)
124     {
125         /* Pickup text font.  */
126         _gx_widget_font_get((GX_WIDGET *)text_input, text_input -> gx_multi_line_text_view_font_id, &font);
127 
128         if (!font)
129         {
130             return GX_FAILURE;
131         }
132 
133         if (start_mark != end_mark)
134         {
135             glyph_len = (UINT)GX_ABS((INT)start_mark - (INT)end_mark);
136             text_input -> gx_multi_line_text_input_start_mark = 0;
137             text_input -> gx_multi_line_text_input_end_mark = 0;
138         }
139         else
140         {
141 #ifdef GX_UTF8_SUPPORT
142             string.gx_string_ptr = text_input -> gx_multi_line_text_view_text.gx_string_ptr + insert_pos;
143             string.gx_string_length = text_input -> gx_multi_line_text_view_text.gx_string_length - insert_pos;
144             _gx_utility_utf8_string_character_get(&string, GX_NULL, &glyph_len);
145 #endif
146 
147             string.gx_string_ptr = text_input -> gx_multi_line_text_view_text.gx_string_ptr + insert_pos;
148             if (string.gx_string_ptr[0] == GX_KEY_CARRIAGE_RETURN || string.gx_string_ptr[0] == GX_KEY_LINE_FEED)
149             {
150                 glyph_len = text_input -> gx_multi_line_text_input_new_line_character_size;
151             }
152         }
153 
154         /* Get scroll shift. */
155         shift = (GX_VALUE)(text_input -> gx_multi_line_text_view_text_scroll_shift);
156 
157         /* Remove character after cursor.  */
158         _gx_multi_line_text_input_char_remove(text_input, insert_pos, glyph_len);
159 
160         /* Save old text rows.  */
161         old_text_rows = text_input -> gx_multi_line_text_view_text_total_rows;
162 
163         /* Calculate new text rows.  */
164         _gx_multi_line_text_view_string_total_rows_compute((GX_MULTI_LINE_TEXT_VIEW *)text_input);
165 
166         if (old_text_rows != text_input -> gx_multi_line_text_view_text_total_rows)
167         {
168             _gx_window_scrollbar_find((GX_WINDOW *)text_input, GX_TYPE_VERTICAL_SCROLL, &scroll);
169             if (scroll)
170             {
171                 /* Reset scrollbar.  */
172                 _gx_scrollbar_reset(scroll, GX_NULL);
173             }
174             else
175             {
176 
177                 if (text_input -> gx_multi_line_text_view_text_total_rows >
178                     text_input -> gx_multi_line_text_view_cache_size)
179                 {
180                     _gx_multi_line_text_view_line_cache_update((GX_MULTI_LINE_TEXT_VIEW *)text_input);
181                 }
182             }
183         }
184 
185         /* Recalculate cursor position. */
186         _gx_multi_line_text_input_cursor_pos_update(text_input, GX_TRUE);
187 
188         if (shift == text_input -> gx_multi_line_text_view_text_scroll_shift)
189         {
190             /* Get text height. */
191             line_height = (GX_VALUE)(font -> gx_font_line_height +
192                                      text_input -> gx_multi_line_text_view_line_space);
193 
194             if (line_height)
195             {
196                 client = text_input -> gx_window_client;
197 
198                 /* Offset client area by length of whitespace.  */
199                 _gx_utility_rectangle_resize(&client, (GX_VALUE)(-text_input -> gx_multi_line_text_view_whitespace));
200 
201                 dirty_rect = client;
202 
203                 /* Calculate dirty area. */
204                 dirty_rect.gx_rectangle_top = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_y - line_height - (line_height >> 1));
205 
206                 /* Calculate last visible line. */
207                 last_visible_line = (-text_input -> gx_multi_line_text_view_text_scroll_shift) / line_height;
208                 last_visible_line = last_visible_line + (INT)view -> gx_multi_line_text_view_text_visible_rows + 1;
209 
210                 if (last_visible_line > (INT)text_input -> gx_multi_line_text_view_text_total_rows)
211                 {
212                     last_visible_line = (INT)text_input -> gx_multi_line_text_view_text_total_rows;
213                 }
214 
215                 shift = (last_visible_line + (INT)old_text_rows - (INT)text_input -> gx_multi_line_text_view_text_total_rows) * line_height + shift;
216                 shift += client.gx_rectangle_top;
217 
218                 if (shift < dirty_rect.gx_rectangle_bottom)
219                 {
220                     dirty_rect.gx_rectangle_bottom = (GX_VALUE)shift;
221                 }
222 
223                 _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &dirty_rect);
224             }
225         }
226         else
227         {
228             _gx_system_dirty_mark((GX_WIDGET *)text_input);
229         }
230     }
231 
232     return GX_SUCCESS;
233 }
234 
235