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 /**   Text Input Management (Single 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_context.h"
28 #include "gx_system.h"
29 #include "gx_widget.h"
30 #include "gx_single_line_text_input.h"
31 #include "gx_text_input_cursor.h"
32 #include "gx_utility.h"
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _gx_single_line_text_input_right_arrow              PORTABLE C      */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    Kenneth Maxwell, Microsoft Corporation                              */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This service moves the text input cursor one character position to  */
47 /*    the right.                                                          */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    text_input                           Single-line text input widget  */
52 /*                                           control block                */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    None                                                                */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _gx_widget_font_get                   Get the font of the text      */
61 /*    _gx_widget_border_width_get           Get the widget border width   */
62 /*    _gx_widget_client_get                 Get widget client rectangle   */
63 /*    _gx_system_string_width_get           Get the width of a string     */
64 /*    _gx_system_dirty_partial_add          Mark the specified area of    */
65 /*                                            a widget as dirty           */
66 /*    _gx_system_dirty_mark                 Mark the widget as dirty      */
67 /*    _gx_text_input_cursor_dirty_rectangle_get                           */
68 /*                                          Get cursor rectangle          */
69 /*    _gx_utility_utf8_string_character_get Parse utf8 string to          */
70 /*                                            multi-byte glyph            */
71 /*                                                                        */
72 /*  CALLED BY                                                             */
73 /*                                                                        */
74 /*    Application Code                                                    */
75 /*    GUIX Internal Code                                                  */
76 /*                                                                        */
77 /*  RELEASE HISTORY                                                       */
78 /*                                                                        */
79 /*    DATE              NAME                      DESCRIPTION             */
80 /*                                                                        */
81 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
82 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
83 /*                                            resulting in version 6.1    */
84 /*                                                                        */
85 /**************************************************************************/
_gx_single_line_text_input_right_arrow(GX_SINGLE_LINE_TEXT_INPUT * text_input)86 UINT _gx_single_line_text_input_right_arrow(GX_SINGLE_LINE_TEXT_INPUT *text_input)
87 {
88 GX_TEXT_INPUT_CURSOR *cursor_ptr = &text_input -> gx_single_line_text_input_cursor_instance;
89 GX_CHAR              *string_buffer = text_input -> gx_single_line_text_input_buffer;
90 UINT                  string_size = text_input -> gx_single_line_text_input_string_size;
91 UINT                  insert_pos;
92 GX_VALUE              new_cursor_pos;
93 GX_VALUE              old_cursor_pos;
94 GX_VALUE              width;
95 GX_FONT              *gx_font;
96 GX_RECTANGLE          client;
97 GX_RECTANGLE          cursor_rect;
98 UINT                  glyph_len = 1;
99 UINT                  start_mark = text_input -> gx_single_line_text_input_start_mark;
100 UINT                  end_mark = text_input -> gx_single_line_text_input_end_mark;
101 GX_BOOL               mark_all = GX_FALSE;
102 GX_STRING             string;
103 
104     /* Pick up widget border width. */
105     _gx_widget_border_width_get((GX_WIDGET *)text_input, &width);
106 
107     /* Get widget client rectangle. */
108     _gx_widget_client_get((GX_WIDGET *)text_input, width, &client);
109 
110     old_cursor_pos = cursor_ptr -> gx_text_input_cursor_pos.gx_point_x;
111     new_cursor_pos = cursor_ptr -> gx_text_input_cursor_pos.gx_point_x;
112 
113     if (start_mark != end_mark)
114     {
115         text_input -> gx_single_line_text_input_start_mark = 0;
116         text_input -> gx_single_line_text_input_end_mark = 0;
117 
118         if (end_mark > start_mark)
119         {
120             if (((new_cursor_pos > client.gx_rectangle_left) &&
121                  (new_cursor_pos < client.gx_rectangle_right)) ||
122                 ((text_input -> gx_widget_style & GX_STYLE_TEXT_ALIGNMENT_MASK) == GX_STYLE_TEXT_CENTER))
123             {
124                 /* No need to update cursor position, just need mark
125                    cursor and highlight area as dirty. */
126                 _gx_text_input_cursor_dirty_rectangle_get(cursor_ptr, &cursor_rect);
127 
128                 _gx_single_line_text_input_text_rectangle_get(text_input, (INT)(start_mark - end_mark), &client);
129 
130                 /* Combine cursor rect and highlight area. */
131                 _gx_utility_rectangle_combine(&client, &cursor_rect);
132 
133                 return _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
134             }
135         }
136     }
137 
138     insert_pos = text_input -> gx_single_line_text_input_insert_pos;
139 
140     /* Update cursor position and offset in x-axis.  */
141     if ((insert_pos < string_size) ||
142         (end_mark > start_mark))
143     {
144         if (start_mark >= end_mark)
145         {
146             string.gx_string_ptr = string_buffer + insert_pos;
147 
148             /* Calculate new cursor position. */
149             if (start_mark != end_mark)
150             {
151                 glyph_len = start_mark - end_mark;
152             }
153 #ifdef GX_UTF8_SUPPORT
154             else
155             {
156                 string.gx_string_length = string_size - insert_pos;
157                 _gx_utility_utf8_string_character_get(&string, GX_NULL, &glyph_len);
158             }
159 #endif
160             text_input -> gx_single_line_text_input_insert_pos += glyph_len;
161 
162             /* Calculate cursor position. */
163             _gx_widget_font_get((GX_WIDGET *)text_input, text_input -> gx_prompt_font_id, &gx_font);
164             string.gx_string_ptr = string_buffer + insert_pos;
165             string.gx_string_length = glyph_len;
166             _gx_system_string_width_get_ext(gx_font, &string, &width);
167 
168             new_cursor_pos = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_x + width);
169         }
170 
171 
172         switch (text_input -> gx_widget_style & GX_STYLE_TEXT_ALIGNMENT_MASK)
173         {
174         case GX_STYLE_TEXT_CENTER:
175             if (start_mark == end_mark)
176             {
177                 /* No need to update text input x offset, mark old and new cursor position area as dirty.  */
178                 _gx_text_input_cursor_dirty_rectangle_get(cursor_ptr, &client);
179                 _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
180             }
181 
182             cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = new_cursor_pos;
183             _gx_text_input_cursor_dirty_rectangle_get(cursor_ptr, &client);
184             _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
185             break;
186 
187         case GX_STYLE_TEXT_RIGHT:
188         case GX_STYLE_TEXT_LEFT:
189         default:
190             if (new_cursor_pos > client.gx_rectangle_right - 1)
191             {
192                 /* Cursor position is out of client rectangle, update x offset. */
193                 text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)(text_input -> gx_single_line_text_input_xoffset +
194                                                                              new_cursor_pos - client.gx_rectangle_right + 1);
195                 cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = (GX_VALUE)(client.gx_rectangle_right - 1);
196 
197                 /* Should mark all widget as dirty.  */
198                 mark_all = GX_TRUE;
199             }
200             else if (new_cursor_pos < client.gx_rectangle_left + 1)
201             {
202                 /* Update text input x offset. */
203                 text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)(text_input -> gx_single_line_text_input_xoffset -
204                                                                              client.gx_rectangle_left - 1 + new_cursor_pos);
205                 cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = (GX_VALUE)(client.gx_rectangle_left + 1);
206 
207                 mark_all = GX_TRUE;
208             }
209             else
210             {
211                 /* No need to update text input x offset, mark old and new cursor position area as dirty.  */
212                 if (start_mark == end_mark)
213                 {
214                     _gx_text_input_cursor_dirty_rectangle_get(cursor_ptr, &client);
215                     _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
216                 }
217 
218                 cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = new_cursor_pos;
219                 _gx_text_input_cursor_dirty_rectangle_get(cursor_ptr, &client);
220                 _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
221             }
222             break;
223         }
224 
225         if ((!mark_all) && (start_mark != end_mark))
226         {
227             /* Mark highlight area as dirty. */
228             client.gx_rectangle_left = old_cursor_pos;
229             client.gx_rectangle_right = (GX_VALUE)(new_cursor_pos - 1);
230         }
231 
232         _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
233     }
234 
235     return GX_SUCCESS;
236 }
237 
238