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_utility.h"
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _gx_single_line_text_input_position_get             PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Kenneth Maxwell, Microsoft Corporation                              */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This service positions the text input cursor based on the requested */
46 /*    cursor position. The text input cursor index will be calculated     */
47 /*    based on the x value of the pixel pisition.                         */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    text_input                                Single-line text input    */
52 /*                                                widget control block    */
53 /*    pixel_position                            X value of pixel position */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    Status                                    Completion status         */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _gx_widget_font_get                   Get widget font               */
62 /*    _gx_widget_border_width_get           Get the widget border width   */
63 /*    _gx_widget_client_get                 Get client rectangle          */
64 /*    _gx_utility_utf8_string_character_get Parse utf8 string to          */
65 /*                                            multi-byte glyph            */
66 /*    _gx_system_string_width_get           Get the width of a string     */
67 /*    _gx_system_dirty_mark                 Mark a widget area dirty      */
68 /*    _gx_system_dirty_partial_add          Mark the partial area of a    */
69 /*                                            widget as dirty             */
70 /*                                                                        */
71 /*  CALLED BY                                                             */
72 /*                                                                        */
73 /*    Application Code                                                    */
74 /*    GUIX Internal Code                                                  */
75 /*                                                                        */
76 /*  RELEASE HISTORY                                                       */
77 /*                                                                        */
78 /*    DATE              NAME                      DESCRIPTION             */
79 /*                                                                        */
80 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
81 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
82 /*                                            resulting in version 6.1    */
83 /*                                                                        */
84 /**************************************************************************/
_gx_single_line_text_input_position_get(GX_SINGLE_LINE_TEXT_INPUT * text_input,INT pixel_position)85 UINT _gx_single_line_text_input_position_get(GX_SINGLE_LINE_TEXT_INPUT *text_input, INT pixel_position)
86 {
87 GX_TEXT_INPUT_CURSOR *cursor_ptr;
88 GX_CHAR              *string_buffer;
89 GX_FONT              *gx_font;
90 GX_RECTANGLE          client;
91 GX_VALUE              width;
92 GX_VALUE              cursor_pos;
93 GX_VALUE              x_pos;
94 GX_VALUE              distance;
95 UINT                  new_insert_pos;
96 UINT                  index;
97 UINT                  glyph_len = 1;
98 GX_STRING             utf8_string;
99 GX_STRING             string;
100 
101     string.gx_string_ptr = text_input -> gx_single_line_text_input_buffer;
102     string.gx_string_length = text_input -> gx_single_line_text_input_string_size;
103 
104     cursor_ptr = &text_input -> gx_single_line_text_input_cursor_instance;
105 
106     string_buffer = text_input -> gx_single_line_text_input_buffer;
107 
108     _gx_widget_font_get((GX_WIDGET *)text_input, text_input -> gx_prompt_font_id, &gx_font);
109 
110     _gx_widget_border_width_get((GX_WIDGET *)text_input, &width);
111     _gx_widget_client_get((GX_WIDGET *)text_input, width, &client);
112 
113     switch (text_input -> gx_widget_style & GX_STYLE_TEXT_ALIGNMENT_MASK)
114     {
115     case GX_STYLE_TEXT_RIGHT:
116         x_pos = (GX_VALUE)(client.gx_rectangle_right - 1);
117         break;
118 
119     case GX_STYLE_TEXT_CENTER:
120         x_pos = (GX_VALUE)(client.gx_rectangle_left + 1 + ((client.gx_rectangle_right - client.gx_rectangle_left + 1) >> 1));
121         break;
122 
123     case GX_STYLE_TEXT_LEFT:
124     default:
125         x_pos = (GX_VALUE)(client.gx_rectangle_left + 1);
126         break;
127     }
128 
129     x_pos = (GX_VALUE)(x_pos - text_input -> gx_single_line_text_input_xoffset);
130 
131     /* Reset mouse down position when it was out of the text show area. */
132     if (pixel_position < client.gx_rectangle_left + 1)
133     {
134         pixel_position = client.gx_rectangle_left + 1;
135     }
136     else if (pixel_position > client.gx_rectangle_right + 1)
137     {
138         pixel_position = client.gx_rectangle_right - 1;
139     }
140 
141     /*Compute the distance from the first character position to the mouse down position. */
142     if (pixel_position > x_pos)
143     {
144         distance = (GX_VALUE)(pixel_position - x_pos);
145     }
146     else
147     {
148         distance = 0;
149     }
150 
151     new_insert_pos = string.gx_string_length;
152     index = 0;
153     cursor_pos = 0;
154 
155     while (string.gx_string_length > 0)
156     {
157 #ifdef GX_UTF8_SUPPORT
158         _gx_utility_utf8_string_character_get(&string, GX_NULL, &glyph_len);
159 #else
160         string.gx_string_ptr++;
161         string.gx_string_length--;
162 #endif
163         utf8_string.gx_string_ptr = string_buffer + index;
164         utf8_string.gx_string_length = glyph_len;
165 
166         _gx_system_string_width_get_ext(gx_font, &utf8_string, &width);
167         if ((cursor_pos + width / 2) > distance)
168         {
169             new_insert_pos = index;
170             break;
171         }
172         cursor_pos = (GX_VALUE)(cursor_pos + width);
173         index += glyph_len;
174     }
175 
176     /* Update insert position. */
177     text_input -> gx_single_line_text_input_insert_pos = new_insert_pos;
178 
179     cursor_pos = (GX_VALUE)(x_pos + cursor_pos);
180 
181     if (cursor_pos < client.gx_rectangle_left + 1)
182     {
183         /* Cursor is beyond widget left, adjust text offset to show cursor.  */
184         cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = (GX_VALUE)(client.gx_rectangle_left + 1);
185 
186         distance = (GX_VALUE)(client.gx_rectangle_left + 1 - cursor_pos);
187         text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)(text_input -> gx_single_line_text_input_xoffset - distance);
188 
189         /* Text offset is modified, mark whole text input area dirty. */
190         _gx_system_dirty_mark((GX_WIDGET *)text_input);
191     }
192     else if (cursor_pos > client.gx_rectangle_right - 1)
193     {
194         /* Cursor is beyond widget right, adjust text offset to show cursor.  */
195         cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = (GX_VALUE)(client.gx_rectangle_right - 1);
196 
197         distance = (GX_VALUE)(cursor_pos - (client.gx_rectangle_right - 1));
198         text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)(text_input -> gx_single_line_text_input_xoffset + distance);
199 
200         /* Text offset is modified, mark whole text input area dirty. */
201         _gx_system_dirty_mark((GX_WIDGET *)text_input);
202     }
203     else if (cursor_ptr -> gx_text_input_cursor_pos.gx_point_x != cursor_pos)
204     {
205         /* For this situation, we only need to mark old and new cursor position dirty.  */
206         client.gx_rectangle_left = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_x - (cursor_ptr -> gx_text_input_cursor_width >> 1));
207         client.gx_rectangle_right = (GX_VALUE)(client.gx_rectangle_left + cursor_ptr -> gx_text_input_cursor_width);
208         _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
209 
210         cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = cursor_pos;
211 
212         client.gx_rectangle_left = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_x - (cursor_ptr -> gx_text_input_cursor_width >> 1));
213         client.gx_rectangle_right = (GX_VALUE)(client.gx_rectangle_left + cursor_ptr -> gx_text_input_cursor_width);
214         _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
215     }
216 
217     return GX_SUCCESS;
218 }
219 
220