1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** GUIX Component                                                        */
17 /**                                                                       */
18 /**   Text Input Management (Single Line Text Input)                      */
19 /**                                                                       */
20 /**************************************************************************/
21 
22 #define GX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "gx_api.h"
28 #include "gx_context.h"
29 #include "gx_system.h"
30 #include "gx_widget.h"
31 #include "gx_single_line_text_input.h"
32 #include "gx_utility.h"
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _gx_single_line_text_input_position_get             PORTABLE C      */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    Kenneth Maxwell, Microsoft Corporation                              */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This service positions the text input cursor based on the requested */
47 /*    cursor position. The text input cursor index will be calculated     */
48 /*    based on the x value of the pixel pisition.                         */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    text_input                                Single-line text input    */
53 /*                                                widget control block    */
54 /*    pixel_position                            X value of pixel position */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    Status                                    Completion status         */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    _gx_widget_font_get                   Get widget font               */
63 /*    _gx_widget_border_width_get           Get the widget border width   */
64 /*    _gx_widget_client_get                 Get client rectangle          */
65 /*    _gx_utility_utf8_string_character_get Parse utf8 string to          */
66 /*                                            multi-byte glyph            */
67 /*    _gx_system_string_width_get           Get the width of a string     */
68 /*    _gx_system_dirty_mark                 Mark a widget area dirty      */
69 /*    _gx_system_dirty_partial_add          Mark the partial area of a    */
70 /*                                            widget as dirty             */
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_position_get(GX_SINGLE_LINE_TEXT_INPUT * text_input,INT pixel_position)86 UINT _gx_single_line_text_input_position_get(GX_SINGLE_LINE_TEXT_INPUT *text_input, INT pixel_position)
87 {
88 GX_TEXT_INPUT_CURSOR *cursor_ptr;
89 GX_CHAR              *string_buffer;
90 GX_FONT              *gx_font;
91 GX_RECTANGLE          client;
92 GX_VALUE              width;
93 GX_VALUE              cursor_pos;
94 GX_VALUE              x_pos;
95 GX_VALUE              distance;
96 UINT                  new_insert_pos;
97 UINT                  index;
98 UINT                  glyph_len = 1;
99 GX_STRING             utf8_string;
100 GX_STRING             string;
101 
102     string.gx_string_ptr = text_input -> gx_single_line_text_input_buffer;
103     string.gx_string_length = text_input -> gx_single_line_text_input_string_size;
104 
105     cursor_ptr = &text_input -> gx_single_line_text_input_cursor_instance;
106 
107     string_buffer = text_input -> gx_single_line_text_input_buffer;
108 
109     _gx_widget_font_get((GX_WIDGET *)text_input, text_input -> gx_prompt_font_id, &gx_font);
110 
111     _gx_widget_border_width_get((GX_WIDGET *)text_input, &width);
112     _gx_widget_client_get((GX_WIDGET *)text_input, width, &client);
113 
114     switch (text_input -> gx_widget_style & GX_STYLE_TEXT_ALIGNMENT_MASK)
115     {
116     case GX_STYLE_TEXT_RIGHT:
117         x_pos = (GX_VALUE)(client.gx_rectangle_right - 1);
118         break;
119 
120     case GX_STYLE_TEXT_CENTER:
121         x_pos = (GX_VALUE)(client.gx_rectangle_left + 1 + ((client.gx_rectangle_right - client.gx_rectangle_left + 1) >> 1));
122         break;
123 
124     case GX_STYLE_TEXT_LEFT:
125     default:
126         x_pos = (GX_VALUE)(client.gx_rectangle_left + 1);
127         break;
128     }
129 
130     x_pos = (GX_VALUE)(x_pos - text_input -> gx_single_line_text_input_xoffset);
131 
132     /* Reset mouse down position when it was out of the text show area. */
133     if (pixel_position < client.gx_rectangle_left + 1)
134     {
135         pixel_position = client.gx_rectangle_left + 1;
136     }
137     else if (pixel_position > client.gx_rectangle_right + 1)
138     {
139         pixel_position = client.gx_rectangle_right - 1;
140     }
141 
142     /*Compute the distance from the first character position to the mouse down position. */
143     if (pixel_position > x_pos)
144     {
145         distance = (GX_VALUE)(pixel_position - x_pos);
146     }
147     else
148     {
149         distance = 0;
150     }
151 
152     new_insert_pos = string.gx_string_length;
153     index = 0;
154     cursor_pos = 0;
155 
156     while (string.gx_string_length > 0)
157     {
158 #ifdef GX_UTF8_SUPPORT
159         _gx_utility_utf8_string_character_get(&string, GX_NULL, &glyph_len);
160 #else
161         string.gx_string_ptr++;
162         string.gx_string_length--;
163 #endif
164         utf8_string.gx_string_ptr = string_buffer + index;
165         utf8_string.gx_string_length = glyph_len;
166 
167         _gx_system_string_width_get_ext(gx_font, &utf8_string, &width);
168         if ((cursor_pos + width / 2) > distance)
169         {
170             new_insert_pos = index;
171             break;
172         }
173         cursor_pos = (GX_VALUE)(cursor_pos + width);
174         index += glyph_len;
175     }
176 
177     /* Update insert position. */
178     text_input -> gx_single_line_text_input_insert_pos = new_insert_pos;
179 
180     cursor_pos = (GX_VALUE)(x_pos + cursor_pos);
181 
182     if (cursor_pos < client.gx_rectangle_left + 1)
183     {
184         /* Cursor is beyond widget left, adjust text offset to show cursor.  */
185         cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = (GX_VALUE)(client.gx_rectangle_left + 1);
186 
187         distance = (GX_VALUE)(client.gx_rectangle_left + 1 - cursor_pos);
188         text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)(text_input -> gx_single_line_text_input_xoffset - distance);
189 
190         /* Text offset is modified, mark whole text input area dirty. */
191         _gx_system_dirty_mark((GX_WIDGET *)text_input);
192     }
193     else if (cursor_pos > client.gx_rectangle_right - 1)
194     {
195         /* Cursor is beyond widget right, adjust text offset to show cursor.  */
196         cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = (GX_VALUE)(client.gx_rectangle_right - 1);
197 
198         distance = (GX_VALUE)(cursor_pos - (client.gx_rectangle_right - 1));
199         text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)(text_input -> gx_single_line_text_input_xoffset + distance);
200 
201         /* Text offset is modified, mark whole text input area dirty. */
202         _gx_system_dirty_mark((GX_WIDGET *)text_input);
203     }
204     else if (cursor_ptr -> gx_text_input_cursor_pos.gx_point_x != cursor_pos)
205     {
206         /* For this situation, we only need to mark old and new cursor position dirty.  */
207         client.gx_rectangle_left = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_x - (cursor_ptr -> gx_text_input_cursor_width >> 1));
208         client.gx_rectangle_right = (GX_VALUE)(client.gx_rectangle_left + cursor_ptr -> gx_text_input_cursor_width);
209         _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
210 
211         cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = cursor_pos;
212 
213         client.gx_rectangle_left = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_x - (cursor_ptr -> gx_text_input_cursor_width >> 1));
214         client.gx_rectangle_right = (GX_VALUE)(client.gx_rectangle_left + cursor_ptr -> gx_text_input_cursor_width);
215         _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
216     }
217 
218     return GX_SUCCESS;
219 }
220 
221