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