GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: gx_single_line_text_input_position_get.c Lines: 61 61 100.0 %
Date: 2024-12-05 08:52:37 Branches: 19 19 100.0 %

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