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_text_input_cursor.h"
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _gx_single_line_text_input_end                      PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Kenneth Maxwell, Microsoft Corporation                              */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This service positions the text input widget cursor at the end of   */
46 /*    the input string.                                                   */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    text_input                            Single line text input widget */
51 /*                                            control blcok               */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    status                                Completion status             */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _gx_widget_font_get                   Get font by specified ID      */
60 /*    _gx_widget_border_width_get           Get the widget border width   */
61 /*    _gx_widget_client_get                 Retrieves client area of the  */
62 /*                                            widget                      */
63 /*    _gx_system_string_width_get           Get the width of a string     */
64 /*    _gx_system_dirty_mark                 Mart the area of the widget   */
65 /*                                            dirty                       */
66 /*    _gx_system_dirty_partial_add          Mark the partial area of a    */
67 /*                                            widget as dirty             */
68 /*                                                                        */
69 /*  CALLED BY                                                             */
70 /*                                                                        */
71 /*    Application Code                                                    */
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_end(GX_SINGLE_LINE_TEXT_INPUT * text_input)83 UINT _gx_single_line_text_input_end(GX_SINGLE_LINE_TEXT_INPUT *text_input)
84 {
85 GX_TEXT_INPUT_CURSOR *cursor_ptr;
86 GX_VALUE              text_width;
87 GX_RECTANGLE          client;
88 GX_VALUE              new_xoffset;
89 GX_VALUE              new_cursor_pos;
90 GX_VALUE              border_width;
91 GX_FONT              *gx_font;
92 UINT                  start_mark = text_input -> gx_single_line_text_input_start_mark;
93 UINT                  end_mark = text_input -> gx_single_line_text_input_end_mark;
94 GX_BOOL               mark_new_cursor_dirty = GX_FALSE;
95 GX_STRING             string;
96 
97     if (start_mark != end_mark)
98     {
99         text_input -> gx_single_line_text_input_start_mark = 0;
100         text_input -> gx_single_line_text_input_end_mark = 0;
101     }
102 
103     cursor_ptr = &text_input -> gx_single_line_text_input_cursor_instance;
104     string.gx_string_ptr = text_input -> gx_single_line_text_input_buffer;
105     string.gx_string_length = text_input -> gx_single_line_text_input_string_size;
106 
107     /* Pick up text width. */
108     _gx_widget_font_get((GX_WIDGET *)text_input, text_input -> gx_prompt_font_id, &gx_font);
109 
110     /* Calculate the widget width for showing text. */
111     _gx_widget_border_width_get((GX_WIDGET *)text_input, &border_width);
112     _gx_widget_client_get((GX_WIDGET *)text_input, border_width, &client);
113 
114     /* Update the value of cursor position and xoffset. */
115     switch (text_input -> gx_widget_style & GX_STYLE_TEXT_ALIGNMENT_MASK)
116     {
117     case GX_STYLE_TEXT_RIGHT:
118         new_xoffset = 0;
119         new_cursor_pos = (GX_VALUE)(client.gx_rectangle_right - 1);
120         break;
121 
122     case GX_STYLE_TEXT_CENTER:
123         new_xoffset = 0;
124         new_cursor_pos = (GX_VALUE)(client.gx_rectangle_left + 1 + ((client.gx_rectangle_right - client.gx_rectangle_left + 1) >> 1));
125         new_cursor_pos = (GX_VALUE)(new_cursor_pos + text_input -> gx_single_line_text_input_xoffset);
126         break;
127 
128     case GX_STYLE_TEXT_LEFT:
129     default:
130         /* Calculate new xoffset.  */
131         _gx_system_string_width_get_ext(gx_font, &string, &text_width);
132         new_xoffset = (GX_VALUE)(client.gx_rectangle_right - client.gx_rectangle_left + 1);
133         new_xoffset = (GX_VALUE)(new_xoffset - 3);
134         new_xoffset = (GX_VALUE)(text_width - new_xoffset);
135 
136         if (new_xoffset < 0)
137         {
138             new_xoffset = 0;
139         }
140 
141         /* Calculate new cursor position. */
142         new_cursor_pos = (GX_VALUE)(client.gx_rectangle_left + 1 - new_xoffset + text_width);
143         break;
144     }
145 
146     if (text_input -> gx_single_line_text_input_xoffset != new_xoffset)
147     {
148         /* We need to update text xoffset, mark whole dirty area dirty.  */
149         _gx_system_dirty_mark((GX_WIDGET *)text_input);
150     }
151     else
152     {
153         if (start_mark != end_mark)
154         {
155             _gx_single_line_text_input_text_rectangle_get(text_input, (INT)(start_mark - end_mark), &client);
156 
157             _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
158 
159             mark_new_cursor_dirty = GX_TRUE;
160         }
161         else if (text_input -> gx_single_line_text_input_insert_pos != string.gx_string_length)
162         {
163 
164             /* No need to update text xoffset, mark old and new dirty area */
165             _gx_text_input_cursor_dirty_rectangle_get(cursor_ptr, &client);
166             _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
167 
168             mark_new_cursor_dirty = GX_TRUE;
169         }
170     }
171 
172     /* Update the value of cursor position. */
173     cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = new_cursor_pos;
174 
175     /* Update character insert position.  */
176     text_input -> gx_single_line_text_input_insert_pos = string.gx_string_length;
177 
178     /* Update text inpu x offset.  */
179     text_input -> gx_single_line_text_input_xoffset = new_xoffset;
180 
181     if (mark_new_cursor_dirty)
182     {
183         /* Mark new cursor area as dirty. */
184         _gx_text_input_cursor_dirty_rectangle_get(cursor_ptr, &client);
185         _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
186     }
187 
188     return GX_SUCCESS;
189 }
190 
191