1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** GUIX Component                                                        */
17 /**                                                                       */
18 /**   Text Input Management (Single Line Text Input)                      */
19 /**                                                                       */
20 /**************************************************************************/
21 
22 #define GX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "gx_api.h"
28 #include "gx_context.h"
29 #include "gx_system.h"
30 #include "gx_widget.h"
31 #include "gx_single_line_text_input.h"
32 #include "gx_text_input_cursor.h"
33 #include "gx_utility.h"
34 
35 /**************************************************************************/
36 /*                                                                        */
37 /*  FUNCTION                                               RELEASE        */
38 /*                                                                        */
39 /*    _gx_single_line_text_input_mark_previous            PORTABLE C      */
40 /*                                                           6.1          */
41 /*  AUTHOR                                                                */
42 /*                                                                        */
43 /*    Kenneth Maxwell, Microsoft Corporation                              */
44 /*                                                                        */
45 /*  DESCRIPTION                                                           */
46 /*                                                                        */
47 /*    This function highlights the character before the end mark position.*/
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_utility_utf8_string_character_get Parse utf8 string to          */
61 /*                                            multi-byte glyph            */
62 /*    _gx_widget_border_width_get           Get the widget border width   */
63 /*    _gx_widget_client_get                 Get widget client rectangle   */
64 /*    _gx_widget_font_get                   Get font by specified ID      */
65 /*    _gx_system_string_width_get           Get the width of a string     */
66 /*    _gx_system_dirty_partial_add          Mark the partial area of a    */
67 /*                                            widget as dirty             */
68 /*    _gx_text_input_cursor_dirty_rectangle_get                           */
69 /*                                          Get cursor rectangle          */
70 /*                                                                        */
71 /*  CALLED BY                                                             */
72 /*                                                                        */
73 /*    GUIX Internal Code                                                  */
74 /*                                                                        */
75 /*  RELEASE HISTORY                                                       */
76 /*                                                                        */
77 /*    DATE              NAME                      DESCRIPTION             */
78 /*                                                                        */
79 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
80 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
81 /*                                            resulting in version 6.1    */
82 /*                                                                        */
83 /**************************************************************************/
_gx_single_line_text_input_mark_previous(GX_SINGLE_LINE_TEXT_INPUT * text_input)84 UINT _gx_single_line_text_input_mark_previous(GX_SINGLE_LINE_TEXT_INPUT *text_input)
85 {
86 GX_TEXT_INPUT_CURSOR *cursor_ptr = &text_input -> gx_single_line_text_input_cursor_instance;
87 GX_RECTANGLE          client;
88 GX_RECTANGLE          cursor_rect;
89 GX_VALUE              new_cursor_pos;
90 GX_VALUE              width;
91 GX_FONT              *gx_font;
92 UINT                  glyph_len = 1;
93 GX_BOOL               mark_all = GX_FALSE;
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     if (start_mark == end_mark)
99     {
100         start_mark = text_input -> gx_single_line_text_input_insert_pos;
101         end_mark = text_input -> gx_single_line_text_input_insert_pos;
102 
103         text_input -> gx_single_line_text_input_start_mark = start_mark;
104         text_input -> gx_single_line_text_input_end_mark = end_mark;
105     }
106 
107     string.gx_string_ptr = (GX_CONST GX_CHAR *)text_input -> gx_single_line_text_input_buffer;
108     string.gx_string_length = text_input -> gx_single_line_text_input_string_size;
109 
110     if (end_mark > 0)
111     {
112 #ifdef GX_UTF8_SUPPORT
113         /* Get the glyph length of the character before end mark */
114         _gx_utility_utf8_string_backward_character_length_get(&string, (INT)(end_mark - 1), &glyph_len);
115 #endif
116 
117         text_input -> gx_single_line_text_input_end_mark -= glyph_len;
118         text_input -> gx_single_line_text_input_insert_pos -= glyph_len;
119 
120         /* Pick up widget border width. */
121         _gx_widget_border_width_get((GX_WIDGET *)text_input, &width);
122 
123         /* Get widget client rectangle. */
124         _gx_widget_client_get((GX_WIDGET *)text_input, width, &client);
125 
126         /* Pick up text font. */
127         _gx_widget_font_get((GX_WIDGET *)text_input, text_input -> gx_prompt_font_id, &gx_font);
128 
129         /* Get width of the character before end mark. */
130         string.gx_string_ptr += (end_mark - glyph_len);
131         string.gx_string_length = glyph_len;
132         _gx_system_string_width_get_ext(gx_font, &string, &width);
133 
134         new_cursor_pos = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_x - width);
135 
136         switch (text_input -> gx_widget_style & GX_STYLE_TEXT_ALIGNMENT_MASK)
137         {
138         case GX_STYLE_TEXT_CENTER:
139             break;
140 
141         case GX_STYLE_TEXT_RIGHT:
142         case GX_STYLE_TEXT_LEFT:
143         default:
144             if (new_cursor_pos < client.gx_rectangle_left + 1)
145             {
146                 /* The previous marked character is out of client area, update x offset. */
147                 text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)(text_input -> gx_single_line_text_input_xoffset -
148                                                                              client.gx_rectangle_left - 1 + new_cursor_pos);
149 
150                 new_cursor_pos = (GX_VALUE)(client.gx_rectangle_left + 1);
151 
152                 /* Should mark all widget as dirty*/
153                 mark_all = GX_TRUE;
154             }
155             else if (new_cursor_pos > client.gx_rectangle_right - 1)
156             {
157                 /* Cursor position is out of client rectangle, update x offset. */
158                 text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)(text_input -> gx_single_line_text_input_xoffset +
159                                                                              new_cursor_pos - client.gx_rectangle_right + 1);
160                 new_cursor_pos = (GX_VALUE)(client.gx_rectangle_right - 1);
161 
162                 /* Should mark all widget as dirty.  */
163                 mark_all = GX_TRUE;
164             }
165             break;
166         }
167 
168 
169         if (!mark_all)
170         {
171             client.gx_rectangle_left = new_cursor_pos;
172             client.gx_rectangle_right = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_x - 1);
173 
174             if (start_mark == end_mark)
175             {
176                 /* Get old cursor rectangle. */
177                 _gx_text_input_cursor_dirty_rectangle_get(cursor_ptr, &cursor_rect);
178 
179                 /* Combine with old cursor rectangle. */
180                 _gx_utility_rectangle_combine(&client, &cursor_rect);
181             }
182         }
183 
184         cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = new_cursor_pos;
185 
186         _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
187     }
188 
189     return GX_SUCCESS;
190 }
191 
192