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 #include "gx_utility.h"
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _gx_single_line_text_input_mark_previous            PORTABLE C      */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    Kenneth Maxwell, Microsoft Corporation                              */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function highlights the character before the end mark position.*/
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    text_input                            Single-line text input widget */
51 /*                                            control block               */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    None                                                                */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _gx_utility_utf8_string_character_get Parse utf8 string to          */
60 /*                                            multi-byte glyph            */
61 /*    _gx_widget_border_width_get           Get the widget border width   */
62 /*    _gx_widget_client_get                 Get widget client rectangle   */
63 /*    _gx_widget_font_get                   Get font by specified ID      */
64 /*    _gx_system_string_width_get           Get the width of a string     */
65 /*    _gx_system_dirty_partial_add          Mark the partial area of a    */
66 /*                                            widget as dirty             */
67 /*    _gx_text_input_cursor_dirty_rectangle_get                           */
68 /*                                          Get cursor 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_previous(GX_SINGLE_LINE_TEXT_INPUT * text_input)83 UINT _gx_single_line_text_input_mark_previous(GX_SINGLE_LINE_TEXT_INPUT *text_input)
84 {
85 GX_TEXT_INPUT_CURSOR *cursor_ptr = &text_input -> gx_single_line_text_input_cursor_instance;
86 GX_RECTANGLE          client;
87 GX_RECTANGLE          cursor_rect;
88 GX_VALUE              new_cursor_pos;
89 GX_VALUE              width;
90 GX_FONT              *gx_font;
91 UINT                  glyph_len = 1;
92 GX_BOOL               mark_all = GX_FALSE;
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_STRING             string;
96 
97     if (start_mark == end_mark)
98     {
99         start_mark = text_input -> gx_single_line_text_input_insert_pos;
100         end_mark = text_input -> gx_single_line_text_input_insert_pos;
101 
102         text_input -> gx_single_line_text_input_start_mark = start_mark;
103         text_input -> gx_single_line_text_input_end_mark = end_mark;
104     }
105 
106     string.gx_string_ptr = (GX_CONST GX_CHAR *)text_input -> gx_single_line_text_input_buffer;
107     string.gx_string_length = text_input -> gx_single_line_text_input_string_size;
108 
109     if (end_mark > 0)
110     {
111 #ifdef GX_UTF8_SUPPORT
112         /* Get the glyph length of the character before end mark */
113         _gx_utility_utf8_string_backward_character_length_get(&string, (INT)(end_mark - 1), &glyph_len);
114 #endif
115 
116         text_input -> gx_single_line_text_input_end_mark -= glyph_len;
117         text_input -> gx_single_line_text_input_insert_pos -= glyph_len;
118 
119         /* Pick up widget border width. */
120         _gx_widget_border_width_get((GX_WIDGET *)text_input, &width);
121 
122         /* Get widget client rectangle. */
123         _gx_widget_client_get((GX_WIDGET *)text_input, width, &client);
124 
125         /* Pick up text font. */
126         _gx_widget_font_get((GX_WIDGET *)text_input, text_input -> gx_prompt_font_id, &gx_font);
127 
128         /* Get width of the character before end mark. */
129         string.gx_string_ptr += (end_mark - glyph_len);
130         string.gx_string_length = glyph_len;
131         _gx_system_string_width_get_ext(gx_font, &string, &width);
132 
133         new_cursor_pos = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_x - width);
134 
135         switch (text_input -> gx_widget_style & GX_STYLE_TEXT_ALIGNMENT_MASK)
136         {
137         case GX_STYLE_TEXT_CENTER:
138             break;
139 
140         case GX_STYLE_TEXT_RIGHT:
141         case GX_STYLE_TEXT_LEFT:
142         default:
143             if (new_cursor_pos < client.gx_rectangle_left + 1)
144             {
145                 /* The previous marked character is out of client area, update x offset. */
146                 text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)(text_input -> gx_single_line_text_input_xoffset -
147                                                                              client.gx_rectangle_left - 1 + new_cursor_pos);
148 
149                 new_cursor_pos = (GX_VALUE)(client.gx_rectangle_left + 1);
150 
151                 /* Should mark all widget as dirty*/
152                 mark_all = GX_TRUE;
153             }
154             else if (new_cursor_pos > client.gx_rectangle_right - 1)
155             {
156                 /* Cursor position is out of client rectangle, update x offset. */
157                 text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)(text_input -> gx_single_line_text_input_xoffset +
158                                                                              new_cursor_pos - client.gx_rectangle_right + 1);
159                 new_cursor_pos = (GX_VALUE)(client.gx_rectangle_right - 1);
160 
161                 /* Should mark all widget as dirty.  */
162                 mark_all = GX_TRUE;
163             }
164             break;
165         }
166 
167 
168         if (!mark_all)
169         {
170             client.gx_rectangle_left = new_cursor_pos;
171             client.gx_rectangle_right = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_x - 1);
172 
173             if (start_mark == end_mark)
174             {
175                 /* Get old cursor rectangle. */
176                 _gx_text_input_cursor_dirty_rectangle_get(cursor_ptr, &cursor_rect);
177 
178                 /* Combine with old cursor rectangle. */
179                 _gx_utility_rectangle_combine(&client, &cursor_rect);
180             }
181         }
182 
183         cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = new_cursor_pos;
184 
185         _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
186     }
187 
188     return GX_SUCCESS;
189 }
190 
191