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