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_window.h"
29 #include "gx_scrollbar.h"
30 #include "gx_multi_line_text_input.h"
31 #include "gx_multi_line_text_view.h"
32 #include "gx_text_input_cursor.h"
33 #include "gx_utility.h"
34 #include "gx_widget.h"
35 
36 /**************************************************************************/
37 /*                                                                        */
38 /*  FUNCTION                                               RELEASE        */
39 /*                                                                        */
40 /*    _gx_multi_line_text_input_left_arrow                PORTABLE C      */
41 /*                                                           6.1          */
42 /*  AUTHOR                                                                */
43 /*                                                                        */
44 /*    Kenneth Maxwell, Microsoft Corporation                              */
45 /*                                                                        */
46 /*  DESCRIPTION                                                           */
47 /*                                                                        */
48 /*    This function move the cursor to left by one character.             */
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_text_input_cursor_dirty_rectangle_get                           */
63 /*                                          Get cursor rectangle          */
64 /*    _gx_system_string_width_get           Get the pixel width of one    */
65 /*                                            specificed string           */
66 /*    _gx_multi_line_text_input_cursor_pos_update                         */
67 /*                                          Calculate cursor position     */
68 /*                                            according to insert index   */
69 /*    _gx_system_dirty_partial_add          Add one dirty area to         */
70 /*                                            dirty list                  */
71 /*    _gx_system_dirty_mark                 Mark widget area dirty        */
72 /*    _gx_utility_utf8_string_character_get Parses utf8 string to         */
73 /*                                            multi-byte glyph            */
74 /*    _gx_utility_string_length_check       Test string length            */
75 /*                                                                        */
76 /*  CALLED BY                                                             */
77 /*                                                                        */
78 /*    _gx_multi_line_text_input_keydown_process                           */
79 /*                                                                        */
80 /*  RELEASE HISTORY                                                       */
81 /*                                                                        */
82 /*    DATE              NAME                      DESCRIPTION             */
83 /*                                                                        */
84 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
85 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
86 /*                                            resulting in version 6.1    */
87 /*                                                                        */
88 /**************************************************************************/
_gx_multi_line_text_input_left_arrow(GX_MULTI_LINE_TEXT_INPUT * text_input)89 UINT _gx_multi_line_text_input_left_arrow(GX_MULTI_LINE_TEXT_INPUT *text_input)
90 {
91 GX_TEXT_INPUT_CURSOR *cursor_ptr = &text_input -> gx_multi_line_text_input_cursor_instance;
92 GX_VALUE              char_width;
93 GX_STRING             string;
94 UINT                  insert_pos;
95 GX_POINT              cur_pos;
96 INT                   shift;
97 GX_RECTANGLE          cur_rect;
98 UINT                  glyph_len = 1;
99 GX_FONT              *font;
100 UINT                  start_mark = text_input -> gx_multi_line_text_input_start_mark;
101 UINT                  end_mark = text_input -> gx_multi_line_text_input_end_mark;
102 
103     if (start_mark != end_mark)
104     {
105         cur_pos = cursor_ptr -> gx_text_input_cursor_pos;
106 
107         if (end_mark < start_mark)
108         {
109             _gx_multi_line_text_input_cursor_visible(text_input);
110 
111             /* Get highlight area. */
112             _gx_multi_line_text_input_highlight_rectangle_get(text_input, &cur_rect);
113         }
114         else
115         {
116             text_input -> gx_multi_line_text_input_text_insert_position = start_mark;
117 
118             shift = text_input -> gx_multi_line_text_view_text_scroll_shift;
119 
120             /* Recalculate cursor position according to cursor insert position. */
121             _gx_multi_line_text_input_cursor_pos_update(text_input, GX_TRUE);
122 
123             /* Dirty mark. */
124             if (shift == text_input -> gx_multi_line_text_view_text_scroll_shift)
125             {
126                 _gx_multi_line_text_input_text_rectangle_get(text_input, cursor_ptr -> gx_text_input_cursor_pos, cur_pos, &cur_rect);
127             }
128             else
129             {
130                 cur_rect = text_input -> gx_widget_size;
131             }
132         }
133 
134         text_input -> gx_multi_line_text_input_start_mark = 0;
135         text_input -> gx_multi_line_text_input_end_mark = 0;
136 
137         /* Mark highlight area as dirty. */
138         _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &cur_rect);
139 
140         /* Mark cursor area as dirty. */
141         _gx_text_input_cursor_dirty_rectangle_get(&text_input -> gx_multi_line_text_input_cursor_instance, &cur_rect);
142         _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &cur_rect);
143     }
144     else
145     {
146         _gx_widget_font_get((GX_WIDGET *)text_input, text_input -> gx_multi_line_text_view_font_id, &font);
147 
148         if (!font)
149         {
150             return GX_FAILURE;
151         }
152 
153         insert_pos = text_input -> gx_multi_line_text_input_text_insert_position;
154         if (insert_pos > 0)
155         {
156             _gx_multi_line_text_input_cursor_visible(text_input);
157 
158 #ifdef GX_UTF8_SUPPORT
159             _gx_utility_utf8_string_backward_character_length_get(&text_input -> gx_multi_line_text_view_text,
160                                                                   (INT)(insert_pos - 1), &glyph_len);
161 #endif
162             string.gx_string_ptr = &text_input -> gx_multi_line_text_view_text.gx_string_ptr[insert_pos - glyph_len];
163 
164             if (string.gx_string_ptr[0] == GX_KEY_CARRIAGE_RETURN || string.gx_string_ptr[0] == GX_KEY_LINE_FEED)
165             {
166                 glyph_len = text_input -> gx_multi_line_text_input_new_line_character_size;
167             }
168 
169             /* Move the cursor position in the input buffer forward by one character. */
170             text_input -> gx_multi_line_text_input_text_insert_position -= glyph_len;
171             string.gx_string_ptr = &text_input -> gx_multi_line_text_view_text.gx_string_ptr[insert_pos - glyph_len];
172             string.gx_string_length = glyph_len;
173             _gx_system_string_width_get_ext(font, &string, &char_width);
174 
175             /* Record scroll shift before recalculate cursor position. */
176             _gx_text_input_cursor_dirty_rectangle_get(&text_input -> gx_multi_line_text_input_cursor_instance, &cur_rect);
177             shift = text_input -> gx_multi_line_text_view_text_scroll_shift;
178 
179             _gx_multi_line_text_input_cursor_pos_update(text_input, GX_TRUE);
180 
181             /* Dirty mark. */
182             if (shift == text_input -> gx_multi_line_text_view_text_scroll_shift)
183             {
184                 _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &cur_rect);
185                 _gx_text_input_cursor_dirty_rectangle_get(&text_input -> gx_multi_line_text_input_cursor_instance, &cur_rect);
186                 _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &cur_rect);
187             }
188             else
189             {
190                 _gx_system_dirty_mark((GX_WIDGET *)text_input);
191             }
192         }
193     }
194 
195     return GX_SUCCESS;
196 }
197 
198