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_mark_home                PORTABLE C      */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    Kenneth Maxwell, Microsoft Corporation                              */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This service moves the end mark position to the start of the input  */
47 /*    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 /*    _gx_single_line_text_input_text_rectangle_get                       */
70 /*                                          Retrieve text rectangle       */
71 /*                                                                        */
72 /*  CALLED BY                                                             */
73 /*                                                                        */
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_mark_home(GX_SINGLE_LINE_TEXT_INPUT * text_input)85 UINT _gx_single_line_text_input_mark_home(GX_SINGLE_LINE_TEXT_INPUT *text_input)
86 {
87 GX_TEXT_INPUT_CURSOR *cursor_ptr;
88 GX_VALUE              border_width;
89 GX_VALUE              text_width;
90 GX_RECTANGLE          client;
91 GX_FONT              *gx_font;
92 GX_VALUE              new_xoffset;
93 GX_VALUE              new_cursor_pos;
94 UINT                  start_mark = text_input -> gx_single_line_text_input_start_mark;
95 UINT                  end_mark = text_input -> gx_single_line_text_input_end_mark;
96 GX_STRING             string;
97 
98     cursor_ptr = &text_input -> gx_single_line_text_input_cursor_instance;
99 
100     /* Check if cursor already in the home position. */
101     if (text_input -> gx_single_line_text_input_insert_pos == 0)
102     {
103         return GX_SUCCESS;
104     }
105 
106     if (start_mark == end_mark)
107     {
108         /* Set start mark and end mark to current cursor position. */
109         start_mark = text_input -> gx_single_line_text_input_insert_pos;
110         end_mark = text_input -> gx_single_line_text_input_insert_pos;
111 
112         text_input -> gx_single_line_text_input_start_mark = start_mark;
113         text_input -> gx_single_line_text_input_end_mark = end_mark;
114     }
115 
116     _gx_widget_border_width_get((GX_WIDGET *)text_input, &border_width);
117     _gx_widget_client_get((GX_WIDGET *)text_input, border_width, &client);
118 
119     switch (text_input -> gx_widget_style & GX_STYLE_TEXT_ALIGNMENT_MASK)
120     {
121     case GX_STYLE_TEXT_RIGHT:
122         /* Calculate new x offset.  */
123         _gx_widget_font_get((GX_WIDGET *)text_input, text_input -> gx_prompt_font_id, &gx_font);
124         string.gx_string_ptr = text_input -> gx_single_line_text_input_buffer;
125         string.gx_string_length = text_input -> gx_single_line_text_input_string_size;
126         _gx_system_string_width_get_ext(gx_font, &string, &text_width);
127         new_xoffset = (GX_VALUE)(client.gx_rectangle_right - client.gx_rectangle_left + 1);
128 
129         new_xoffset = (GX_VALUE)(new_xoffset - 3);
130 
131         if (text_width < new_xoffset)
132         {
133             new_xoffset = text_width;
134         }
135 
136         /* Calculate new cursor position.  */
137         new_cursor_pos = (GX_VALUE)(client.gx_rectangle_right - 1 - new_xoffset);
138         break;
139 
140     case GX_STYLE_TEXT_CENTER:
141         /* Calculate new cursor position.  */
142         new_xoffset = 0;
143         new_cursor_pos = (GX_VALUE)(client.gx_rectangle_left + 1 + ((client.gx_rectangle_right - client.gx_rectangle_left + 1) >> 1));
144         new_cursor_pos = (GX_VALUE)(new_cursor_pos - text_input -> gx_single_line_text_input_xoffset);
145         break;
146 
147 
148     case GX_STYLE_TEXT_LEFT:
149     default:
150         new_xoffset = 0;
151         new_cursor_pos = (GX_VALUE)(client.gx_rectangle_left + 1);
152         break;
153     }
154 
155     if (text_input -> gx_single_line_text_input_xoffset != new_xoffset)
156     {
157         /* We need to update x offset, mark whole text input area dirty.  */
158         _gx_system_dirty_mark((GX_WIDGET *)text_input);
159     }
160     else
161     {
162         if (start_mark == end_mark)
163         {
164             /* Mark old curosr rectangle as dirty. */
165             _gx_text_input_cursor_dirty_rectangle_get(cursor_ptr, &client);
166             _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
167         }
168 
169         /* Get bounding rectangle of highlight text. */
170         _gx_single_line_text_input_text_rectangle_get(text_input, (INT)(0 - end_mark), &client);
171 
172         /* Mark highlight area as dirty. */
173         _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
174     }
175 
176     text_input -> gx_single_line_text_input_end_mark = 0;
177 
178     /* Update cursor position. */
179     cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = new_cursor_pos;
180 
181     /* Update character insert position.  */
182     text_input -> gx_single_line_text_input_insert_pos = 0;
183 
184     /* Update text input x offset.  */
185     text_input -> gx_single_line_text_input_xoffset = new_xoffset;
186 
187     return GX_SUCCESS;
188 }
189 
190