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_display.h"
29 #include "gx_context.h"
30 #include "gx_widget.h"
31 #include "gx_single_line_text_input.h"
32 #include "gx_text_input_cursor.h"
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _gx_single_line_text_input_home                     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 position to the start of   */
47 /*    the input string.                                                   */
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 font by specified ID      */
61 /*    _gx_widget_border_width_get           Get the widget border width   */
62 /*    _gx_widget_client_get                 Retrieves client area of the  */
63 /*                                            widget                      */
64 /*    _gx_system_string_width_get           Get the width of a string     */
65 /*    _gx_system_dirty_mark                 Mart the area of the widget   */
66 /*                                            dirty                       */
67 /*    _gx_system_dirty_partial_add          Mark the partial area of a    */
68 /*                                            widget as dirty             */
69 /*                                                                        */
70 /*  CALLED BY                                                             */
71 /*                                                                        */
72 /*    Application Code                                                    */
73 /*    GUIX Internal Code                                                  */
74 /*                                                                        */
75 /*  RELEASE HISTORY                                                       */
76 /*                                                                        */
77 /*    DATE              NAME                      DESCRIPTION             */
78 /*                                                                        */
79 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
80 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
81 /*                                            resulting in version 6.1    */
82 /*                                                                        */
83 /**************************************************************************/
_gx_single_line_text_input_home(GX_SINGLE_LINE_TEXT_INPUT * text_input)84 UINT _gx_single_line_text_input_home(GX_SINGLE_LINE_TEXT_INPUT *text_input)
85 {
86 GX_TEXT_INPUT_CURSOR *cursor_ptr;
87 GX_VALUE              border_width;
88 GX_VALUE              text_width;
89 GX_RECTANGLE          client;
90 GX_FONT              *gx_font;
91 GX_VALUE              new_xoffset;
92 GX_VALUE              new_cursor_pos;
93 UINT                  start_mark = text_input -> gx_single_line_text_input_start_mark;
94 UINT                  end_mark = text_input -> gx_single_line_text_input_end_mark;
95 GX_BOOL               mark_new_cursor_dirty = GX_FALSE;
96 GX_STRING             string;
97 
98     if (start_mark != end_mark)
99     {
100         text_input -> gx_single_line_text_input_start_mark = 0;
101         text_input -> gx_single_line_text_input_end_mark = 0;
102     }
103 
104     cursor_ptr = &text_input -> gx_single_line_text_input_cursor_instance;
105 
106     /* Check if cursor already in the home position. */
107     if (text_input -> gx_single_line_text_input_insert_pos == 0)
108     {
109         return GX_SUCCESS;
110     }
111 
112     _gx_widget_border_width_get((GX_WIDGET *)text_input, &border_width);
113     _gx_widget_client_get((GX_WIDGET *)text_input, border_width, &client);
114 
115     switch (text_input -> gx_widget_style & GX_STYLE_TEXT_ALIGNMENT_MASK)
116     {
117     case GX_STYLE_TEXT_RIGHT:
118         /* Calculate new x offset.  */
119         _gx_widget_font_get((GX_WIDGET *)text_input, text_input -> gx_prompt_font_id, &gx_font);
120 
121         string.gx_string_ptr = text_input -> gx_single_line_text_input_buffer;
122         string.gx_string_length = text_input -> gx_single_line_text_input_string_size;
123         _gx_system_string_width_get_ext(gx_font, &string, &text_width);
124         new_xoffset = (GX_VALUE)(client.gx_rectangle_right - client.gx_rectangle_left + 1);
125 
126         new_xoffset = (GX_VALUE)(new_xoffset - 3);
127 
128         if (text_width < new_xoffset)
129         {
130             new_xoffset = text_width;
131         }
132 
133         /* Calculate new cursor position.  */
134         new_cursor_pos = (GX_VALUE)(client.gx_rectangle_right - 1 - new_xoffset);
135         break;
136 
137     case GX_STYLE_TEXT_CENTER:
138         /* Calculate new cursor position.  */
139         new_xoffset = 0;
140         new_cursor_pos = (GX_VALUE)(client.gx_rectangle_left + 1 + ((client.gx_rectangle_right - client.gx_rectangle_left + 1) >> 1));
141         new_cursor_pos = (GX_VALUE)(new_cursor_pos - text_input -> gx_single_line_text_input_xoffset);
142         break;
143 
144 
145     case GX_STYLE_TEXT_LEFT:
146     default:
147         new_xoffset = 0;
148         new_cursor_pos = (GX_VALUE)(client.gx_rectangle_left + 1);
149         break;
150     }
151 
152     if (text_input -> gx_single_line_text_input_xoffset != new_xoffset)
153     {
154         /* We need to update x offset, mark whole text input area dirty.  */
155         _gx_system_dirty_mark((GX_WIDGET *)text_input);
156     }
157     else
158     {
159         if (start_mark != end_mark)
160         {
161             /* Get bounding rectangle of highlight text. */
162             _gx_single_line_text_input_text_rectangle_get(text_input, (INT)(start_mark - end_mark), &client);
163 
164             /* Mark highlight area as dirty. */
165             _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
166 
167             mark_new_cursor_dirty = GX_TRUE;
168         }
169         else
170         {
171             /* No need to update x offset, mark old and new cursor position area dirty.  */
172 
173             /* Mark old curosr rectangle as dirty. */
174             _gx_text_input_cursor_dirty_rectangle_get(cursor_ptr, &client);
175             _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
176 
177             mark_new_cursor_dirty = GX_TRUE;
178         }
179     }
180 
181     /* Update cursor position. */
182     cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = new_cursor_pos;
183 
184     /* Update character insert position.  */
185     text_input -> gx_single_line_text_input_insert_pos = 0;
186 
187     /* Update text input x offset.  */
188     text_input -> gx_single_line_text_input_xoffset = new_xoffset;
189 
190     if (mark_new_cursor_dirty)
191     {
192         /* Mark new cursor rectangle as dirty. */
193         _gx_text_input_cursor_dirty_rectangle_get(cursor_ptr, &client);
194         _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
195     }
196 
197     return GX_SUCCESS;
198 }
199 
200