GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: gx_single_line_text_input_position_get.c Lines: 61 61 100.0 %
Date: 2026-03-06 19:21:09 Branches: 19 19 100.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 * Copyright (c) 2026-present Eclipse ThreadX contributors
4
 *
5
 * This program and the accompanying materials are made available under the
6
 * terms of the MIT License which is available at
7
 * https://opensource.org/licenses/MIT.
8
 *
9
 * SPDX-License-Identifier: MIT
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_utility.h"
33
34
/**************************************************************************/
35
/*                                                                        */
36
/*  FUNCTION                                               RELEASE        */
37
/*                                                                        */
38
/*    _gx_single_line_text_input_position_get             PORTABLE C      */
39
/*                                                           6.1          */
40
/*  AUTHOR                                                                */
41
/*                                                                        */
42
/*    Kenneth Maxwell, Microsoft Corporation                              */
43
/*                                                                        */
44
/*  DESCRIPTION                                                           */
45
/*                                                                        */
46
/*    This service positions the text input cursor based on the requested */
47
/*    cursor position. The text input cursor index will be calculated     */
48
/*    based on the x value of the pixel pisition.                         */
49
/*                                                                        */
50
/*  INPUT                                                                 */
51
/*                                                                        */
52
/*    text_input                                Single-line text input    */
53
/*                                                widget control block    */
54
/*    pixel_position                            X value of pixel position */
55
/*                                                                        */
56
/*  OUTPUT                                                                */
57
/*                                                                        */
58
/*    Status                                    Completion status         */
59
/*                                                                        */
60
/*  CALLS                                                                 */
61
/*                                                                        */
62
/*    _gx_widget_font_get                   Get widget font               */
63
/*    _gx_widget_border_width_get           Get the widget border width   */
64
/*    _gx_widget_client_get                 Get client rectangle          */
65
/*    _gx_utility_utf8_string_character_get Parse utf8 string to          */
66
/*                                            multi-byte glyph            */
67
/*    _gx_system_string_width_get           Get the width of a string     */
68
/*    _gx_system_dirty_mark                 Mark a widget area dirty      */
69
/*    _gx_system_dirty_partial_add          Mark the partial area of a    */
70
/*                                            widget as dirty             */
71
/*                                                                        */
72
/*  CALLED BY                                                             */
73
/*                                                                        */
74
/*    Application Code                                                    */
75
/*    GUIX Internal Code                                                  */
76
/*                                                                        */
77
/**************************************************************************/
78
1866
UINT _gx_single_line_text_input_position_get(GX_SINGLE_LINE_TEXT_INPUT *text_input, INT pixel_position)
79
{
80
GX_TEXT_INPUT_CURSOR *cursor_ptr;
81
GX_CHAR              *string_buffer;
82
GX_FONT              *gx_font;
83
GX_RECTANGLE          client;
84
GX_VALUE              width;
85
GX_VALUE              cursor_pos;
86
GX_VALUE              x_pos;
87
GX_VALUE              distance;
88
UINT                  new_insert_pos;
89
UINT                  index;
90
1866
UINT                  glyph_len = 1;
91
GX_STRING             utf8_string;
92
GX_STRING             string;
93
94
1866
    string.gx_string_ptr = text_input -> gx_single_line_text_input_buffer;
95
1866
    string.gx_string_length = text_input -> gx_single_line_text_input_string_size;
96
97
1866
    cursor_ptr = &text_input -> gx_single_line_text_input_cursor_instance;
98
99
1866
    string_buffer = text_input -> gx_single_line_text_input_buffer;
100
101
1866
    _gx_widget_font_get((GX_WIDGET *)text_input, text_input -> gx_prompt_font_id, &gx_font);
102
103
1866
    _gx_widget_border_width_get((GX_WIDGET *)text_input, &width);
104
1866
    _gx_widget_client_get((GX_WIDGET *)text_input, width, &client);
105
106
1866
    switch (text_input -> gx_widget_style & GX_STYLE_TEXT_ALIGNMENT_MASK)
107
    {
108
454
    case GX_STYLE_TEXT_RIGHT:
109
454
        x_pos = (GX_VALUE)(client.gx_rectangle_right - 1);
110
454
        break;
111
112
454
    case GX_STYLE_TEXT_CENTER:
113
454
        x_pos = (GX_VALUE)(client.gx_rectangle_left + 1 + ((client.gx_rectangle_right - client.gx_rectangle_left + 1) >> 1));
114
454
        break;
115
116
958
    case GX_STYLE_TEXT_LEFT:
117
    default:
118
958
        x_pos = (GX_VALUE)(client.gx_rectangle_left + 1);
119
958
        break;
120
    }
121
122
1866
    x_pos = (GX_VALUE)(x_pos - text_input -> gx_single_line_text_input_xoffset);
123
124
    /* Reset mouse down position when it was out of the text show area. */
125
1866
    if (pixel_position < client.gx_rectangle_left + 1)
126
    {
127
608
        pixel_position = client.gx_rectangle_left + 1;
128
    }
129
1258
    else if (pixel_position > client.gx_rectangle_right + 1)
130
    {
131
605
        pixel_position = client.gx_rectangle_right - 1;
132
    }
133
134
    /*Compute the distance from the first character position to the mouse down position. */
135
1866
    if (pixel_position > x_pos)
136
    {
137
1582
        distance = (GX_VALUE)(pixel_position - x_pos);
138
    }
139
    else
140
    {
141
284
        distance = 0;
142
    }
143
144
1866
    new_insert_pos = string.gx_string_length;
145
1866
    index = 0;
146
1866
    cursor_pos = 0;
147
148
76039
    while (string.gx_string_length > 0)
149
    {
150
#ifdef GX_UTF8_SUPPORT
151
75567
        _gx_utility_utf8_string_character_get(&string, GX_NULL, &glyph_len);
152
#else
153
        string.gx_string_ptr++;
154
        string.gx_string_length--;
155
#endif
156
75567
        utf8_string.gx_string_ptr = string_buffer + index;
157
75567
        utf8_string.gx_string_length = glyph_len;
158
159
75567
        _gx_system_string_width_get_ext(gx_font, &utf8_string, &width);
160
75567
        if ((cursor_pos + width / 2) > distance)
161
        {
162
1394
            new_insert_pos = index;
163
1394
            break;
164
        }
165
74173
        cursor_pos = (GX_VALUE)(cursor_pos + width);
166
74173
        index += glyph_len;
167
    }
168
169
    /* Update insert position. */
170
1866
    text_input -> gx_single_line_text_input_insert_pos = new_insert_pos;
171
172
1866
    cursor_pos = (GX_VALUE)(x_pos + cursor_pos);
173
174
1866
    if (cursor_pos < client.gx_rectangle_left + 1)
175
    {
176
        /* Cursor is beyond widget left, adjust text offset to show cursor.  */
177
60
        cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = (GX_VALUE)(client.gx_rectangle_left + 1);
178
179
60
        distance = (GX_VALUE)(client.gx_rectangle_left + 1 - cursor_pos);
180
60
        text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)(text_input -> gx_single_line_text_input_xoffset - distance);
181
182
        /* Text offset is modified, mark whole text input area dirty. */
183
60
        _gx_system_dirty_mark((GX_WIDGET *)text_input);
184
    }
185
1806
    else if (cursor_pos > client.gx_rectangle_right - 1)
186
    {
187
        /* Cursor is beyond widget right, adjust text offset to show cursor.  */
188
64
        cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = (GX_VALUE)(client.gx_rectangle_right - 1);
189
190
64
        distance = (GX_VALUE)(cursor_pos - (client.gx_rectangle_right - 1));
191
64
        text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)(text_input -> gx_single_line_text_input_xoffset + distance);
192
193
        /* Text offset is modified, mark whole text input area dirty. */
194
64
        _gx_system_dirty_mark((GX_WIDGET *)text_input);
195
    }
196
1742
    else if (cursor_ptr -> gx_text_input_cursor_pos.gx_point_x != cursor_pos)
197
    {
198
        /* For this situation, we only need to mark old and new cursor position dirty.  */
199
750
        client.gx_rectangle_left = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_x - (cursor_ptr -> gx_text_input_cursor_width >> 1));
200
750
        client.gx_rectangle_right = (GX_VALUE)(client.gx_rectangle_left + cursor_ptr -> gx_text_input_cursor_width);
201
750
        _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
202
203
750
        cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = cursor_pos;
204
205
750
        client.gx_rectangle_left = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_x - (cursor_ptr -> gx_text_input_cursor_width >> 1));
206
750
        client.gx_rectangle_right = (GX_VALUE)(client.gx_rectangle_left + cursor_ptr -> gx_text_input_cursor_width);
207
750
        _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
208
    }
209
210
1866
    return GX_SUCCESS;
211
}
212