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 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _gx_single_line_text_input_draw_position_get        PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Kenneth Maxwell, Microsoft Corporation                              */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This service calculates draw start position of text input text.     */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    text_input                            Single-line text input widget */
49 /*                                            control block.              */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    None                                                                */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _gx_context_brush_get                 Get brush of current context  */
58 /*    _gx_widget_border_width_get           Get widget border width       */
59 /*    _gx_widget_client_get                 Get widget client rectangle   */
60 /*    _gx_system_string_width_get           Get the width of a string     */
61 /*    _gx_single_line_text_input_position_update                          */
62 /*                                          Update cursor position by     */
63 /*                                            insert index                */
64 /*                                                                        */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    _gx_single_line_text_input_draw                                     */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
75 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
76 /*                                            resulting in version 6.1    */
77 /*                                                                        */
78 /**************************************************************************/
_gx_single_line_text_input_draw_position_get(GX_SINGLE_LINE_TEXT_INPUT * text_input,GX_VALUE * xpos,GX_VALUE * ypos)79 UINT _gx_single_line_text_input_draw_position_get(GX_SINGLE_LINE_TEXT_INPUT *text_input, GX_VALUE *xpos, GX_VALUE *ypos)
80 {
81 GX_VALUE     border_width;
82 UINT         text_height;
83 GX_VALUE     text_width;
84 GX_VALUE     client_height;
85 GX_VALUE     client_width;
86 GX_VALUE     x_pos;
87 GX_VALUE     y_pos;
88 GX_RECTANGLE client;
89 GX_BRUSH    *brush;
90 GX_BOOL      update_cursor_pos;
91 GX_STRING    string;
92 
93     _gx_context_brush_get(&brush);
94 
95     if (brush -> gx_brush_font == GX_NULL)
96     {
97         return(GX_FAILURE);
98     }
99 
100     if (text_input -> gx_widget_style & GX_STYLE_CURSOR_ALWAYS)
101     {
102         update_cursor_pos = GX_TRUE;
103     }
104     else
105     {
106         update_cursor_pos = GX_FALSE;
107     }
108 
109     /* Pickup widget width. */
110     _gx_widget_border_width_get((GX_WIDGET *)text_input, &border_width);
111 
112     _gx_widget_client_get((GX_WIDGET *)text_input, border_width, &client);
113 
114     client_width = (GX_VALUE)(client.gx_rectangle_right - client.gx_rectangle_left + 1);
115     client_height = (GX_VALUE)(client.gx_rectangle_bottom - client.gx_rectangle_top + 1);
116 
117 
118     text_height = brush -> gx_brush_font -> gx_font_line_height;
119     string.gx_string_ptr = text_input -> gx_single_line_text_input_buffer;
120     string.gx_string_length = text_input -> gx_single_line_text_input_string_size;
121     _gx_system_string_width_get_ext(brush -> gx_brush_font, &string, &text_width);
122 
123     switch (text_input -> gx_widget_style & GX_STYLE_TEXT_ALIGNMENT_MASK)
124     {
125     case GX_STYLE_TEXT_RIGHT:
126         x_pos = (GX_VALUE)(client.gx_rectangle_right - 1);
127 
128         x_pos = (GX_VALUE)(x_pos - text_input -> gx_single_line_text_input_xoffset);
129 
130         if (text_width < (client_width - 3))
131         {
132             /* Text width is shorter than client width. */
133             if (text_input -> gx_single_line_text_input_xoffset != text_width)
134             {
135                 /* If xoffset is not equal to text width,
136                    reset xoffset and update cursor position. */
137                 text_input -> gx_single_line_text_input_xoffset = text_width;
138                 update_cursor_pos = GX_TRUE;
139             }
140         }
141         else if (x_pos > client.gx_rectangle_left + 1)
142         {
143             /* If text start/end position is inside client area,
144                reset xoffset value and update cursor position. */
145             text_input -> gx_single_line_text_input_xoffset = text_width;
146             update_cursor_pos = GX_TRUE;
147         }
148 
149         x_pos = (GX_VALUE)(client.gx_rectangle_right - 1);
150         break;
151 
152     case GX_STYLE_TEXT_CENTER:
153         x_pos = (GX_VALUE)(client.gx_rectangle_left + 1 + (client_width >> 1));
154         x_pos = (GX_VALUE)(x_pos - text_input -> gx_single_line_text_input_xoffset);
155 
156         if (text_width < (client_width - 3))
157         {
158             /* Text width is shorter than client width. */
159             if (text_input -> gx_single_line_text_input_xoffset != ((text_width + 1) >> 1))
160             {
161                 /* If xoffset is not equal to 0,
162                    reset xoffset value and update cursor position. */
163                 text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)((text_width + 1) >> 1);
164                 update_cursor_pos = GX_TRUE;
165             }
166         }
167         else if ((x_pos > client.gx_rectangle_left + 1) ||
168                  (x_pos + text_width < client.gx_rectangle_right - 1))
169         {
170             /* If text start/end position is inside client area,
171                reset xoffset value and update cursor position. */
172             text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)((text_width + 1) >> 1);
173             update_cursor_pos = GX_TRUE;
174         }
175         x_pos = (GX_VALUE)(client.gx_rectangle_left + 1 + (client_width >> 1));
176         break;
177 
178     case GX_STYLE_TEXT_LEFT:
179     default:
180         x_pos = (GX_VALUE)(client.gx_rectangle_left + 1);
181 
182         x_pos = (GX_VALUE)(x_pos - text_input -> gx_single_line_text_input_xoffset);
183 
184         if (text_width < (client_width - 3))
185         {
186             /* Text width is shorter than client width. */
187             if (text_input -> gx_single_line_text_input_xoffset != 0)
188             {
189                 /* If xoffset is not equal to 0,
190                    reset xoffset value and update cursor position. */
191                 text_input -> gx_single_line_text_input_xoffset = 0;
192                 update_cursor_pos = GX_TRUE;
193             }
194         }
195         else if (x_pos + text_width < client.gx_rectangle_right - 1)
196         {
197             /* If text start/end position is inside client area,
198                reset xoffset value and update cursor position. */
199             text_input -> gx_single_line_text_input_xoffset = 0;
200             update_cursor_pos = GX_TRUE;
201         }
202 
203         x_pos = (GX_VALUE)(client.gx_rectangle_left + 1);
204         break;
205     }
206 
207     if (update_cursor_pos)
208     {
209         _gx_single_line_text_input_position_update(text_input);
210     }
211 
212     x_pos = (GX_VALUE)(x_pos - text_input -> gx_single_line_text_input_xoffset);
213 
214     y_pos = (GX_VALUE)(text_input -> gx_widget_size.gx_rectangle_top + border_width);
215     y_pos = (GX_VALUE)(y_pos - text_input -> gx_single_line_text_input_yoffset);
216     y_pos = (GX_VALUE)(y_pos + (GX_VALUE)(((INT)client_height - (INT)text_height) >> 1));
217 
218     *xpos = x_pos;
219     *ypos = y_pos;
220 
221     return GX_SUCCESS;
222 }
223 
224