GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: gx_single_line_text_input_backspace.c Lines: 87 87 100.0 %
Date: 2024-12-05 08:52:37 Branches: 29 29 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_system.h"
28
#include "gx_context.h"
29
#include "gx_widget.h"
30
#include "gx_single_line_text_input.h"
31
#include "gx_utility.h"
32
#include "gx_text_input_cursor.h"
33
34
/**************************************************************************/
35
/*                                                                        */
36
/*  FUNCTION                                               RELEASE        */
37
/*                                                                        */
38
/*    _gx_single_line_text_input_backspace                PORTABLE C      */
39
/*                                                           6.1          */
40
/*  AUTHOR                                                                */
41
/*                                                                        */
42
/*    Kenneth Maxwell, Microsoft Corporation                              */
43
/*                                                                        */
44
/*  DESCRIPTION                                                           */
45
/*                                                                        */
46
/*    This service processes a backspace character.                       */
47
/*                                                                        */
48
/*  INPUT                                                                 */
49
/*                                                                        */
50
/*    text_input                            Single-line text input widget */
51
/*                                            control blcok               */
52
/*                                                                        */
53
/*  OUTPUT                                                                */
54
/*                                                                        */
55
/*    status                                Completion status             */
56
/*                                                                        */
57
/*  CALLS                                                                 */
58
/*                                                                        */
59
/*    memmove                               Move a block of memory        */
60
/*    _gx_widget_font_get                   Get font by specified ID      */
61
/*    _gx_widget_client_get                 Get client rectangle          */
62
/*    _gx_widget_border_width_get           Get the widget border width   */
63
/*    _gx_system_string_width_get           Get the width of a string     */
64
/*    _gx_system_dirty_partial_add          Mark the partial area of a    */
65
/*                                            widget as dirty             */
66
/*    _gx_utility_utf8_string_character_get Parse utf8 string to          */
67
/*                                            multi-byte glyph            */
68
/*    _gx_single_line_text_input_position_update                          */
69
/*                                          Update cursor position        */
70
/*                                            according to insert position*/
71
/*                                                                        */
72
/*  CALLED BY                                                             */
73
/*                                                                        */
74
/*    Application Code                                                    */
75
/*    GUIX Internal Code                                                  */
76
/*                                                                        */
77
/*  RELEASE HISTORY                                                       */
78
/*                                                                        */
79
/*    DATE              NAME                      DESCRIPTION             */
80
/*                                                                        */
81
/*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
82
/*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
83
/*                                            resulting in version 6.1    */
84
/*                                                                        */
85
/**************************************************************************/
86
575
UINT _gx_single_line_text_input_backspace(GX_SINGLE_LINE_TEXT_INPUT *text_input)
87
{
88
575
GX_TEXT_INPUT_CURSOR *cursor_ptr = &text_input -> gx_single_line_text_input_cursor_instance;
89
575
GX_WIDGET            *widget = (GX_WIDGET *)text_input;
90
UINT                  insert_pos;
91
GX_VALUE              x_pos;
92
GX_VALUE              new_x_pos;
93
GX_RECTANGLE          client;
94
GX_VALUE              border_width;
95
GX_VALUE              delete_char_width;
96
GX_VALUE              text_width;
97
GX_VALUE              leftoff;
98
GX_VALUE              rightoff;
99
GX_FONT              *gx_font;
100
GX_CHAR              *input_buffer;
101
UINT                  string_size;
102
GX_STRING             string;
103
575
UINT                  glyph_len = 1;
104
105
575
    if (text_input -> gx_single_line_text_input_start_mark > text_input -> gx_single_line_text_input_end_mark)
106
    {
107
21
        return _gx_single_line_text_input_character_delete(text_input);
108
    }
109
110
554
    insert_pos = text_input -> gx_single_line_text_input_insert_pos;
111
112
554
    if (insert_pos > 0)
113
    {
114
532
        input_buffer = text_input -> gx_single_line_text_input_buffer;
115
532
        string_size = text_input -> gx_single_line_text_input_string_size;
116
117
532
        _gx_widget_border_width_get(widget, &border_width);
118
532
        _gx_widget_client_get((GX_WIDGET *)text_input, border_width, &client);
119
120
532
        if (text_input -> gx_single_line_text_input_start_mark != text_input -> gx_single_line_text_input_end_mark)
121
        {
122
47
            glyph_len = text_input -> gx_single_line_text_input_end_mark - text_input -> gx_single_line_text_input_start_mark;
123
124
47
            if (cursor_ptr -> gx_text_input_cursor_pos.gx_point_x <= client.gx_rectangle_left ||
125
40
                cursor_ptr -> gx_text_input_cursor_pos.gx_point_x >= client.gx_rectangle_right)
126
            {
127
26
                _gx_single_line_text_input_right_arrow(text_input);
128
            }
129
130
47
            text_input -> gx_single_line_text_input_start_mark = 0;
131
47
            text_input -> gx_single_line_text_input_end_mark = 0;
132
        }
133
#ifdef GX_UTF8_SUPPORT
134
        else
135
        {
136
            /* Get the glyph length of the cursor left character. */
137
485
            string.gx_string_ptr = input_buffer;
138
485
            string.gx_string_length = string_size;
139
485
            _gx_utility_utf8_string_backward_character_length_get(&string, (INT)(insert_pos - 1), &glyph_len);
140
        }
141
#endif
142
143
        /* Pick up delete character width.  */
144
532
        _gx_widget_font_get((GX_WIDGET *)text_input, text_input -> gx_prompt_font_id, &gx_font);
145
146
532
        string.gx_string_ptr = input_buffer + insert_pos - glyph_len;
147
532
        string.gx_string_length = glyph_len;
148
532
        _gx_system_string_width_get_ext(gx_font, &string, &delete_char_width);
149
150
        /* Pick up text witth. */
151
532
        string.gx_string_ptr = input_buffer;
152
532
        string.gx_string_length = string_size;
153
532
        _gx_system_string_width_get_ext(gx_font, &string, &text_width);
154
155
        /* Delete a character from string buffer.  */
156
532
        memmove(input_buffer + insert_pos - glyph_len, input_buffer + insert_pos, string.gx_string_length - insert_pos);
157
532
        text_input -> gx_single_line_text_input_buffer[string_size - glyph_len] = '\0';
158
159
        /* Update input string size and insert position.  */
160
532
        text_input -> gx_single_line_text_input_string_size -= glyph_len;
161
532
        text_input -> gx_single_line_text_input_insert_pos -= glyph_len;
162
163
532
        switch (text_input -> gx_widget_style & GX_STYLE_TEXT_ALIGNMENT_MASK)
164
        {
165
137
        case GX_STYLE_TEXT_RIGHT:
166
137
            x_pos = (GX_VALUE)(client.gx_rectangle_right - 1);
167
137
            x_pos = (GX_VALUE)(x_pos - text_input -> gx_single_line_text_input_xoffset);
168
169
137
            leftoff = (GX_VALUE)(client.gx_rectangle_left + 1 - x_pos);
170
137
            rightoff = (GX_VALUE)(x_pos + text_width - (client.gx_rectangle_right - 1));
171
172

137
            if ((leftoff > delete_char_width) || (rightoff == 0))
173
            {
174
                /* Decrease text inpit xoffset by the width of the deleted character width.  */
175
129
                text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)(text_input -> gx_single_line_text_input_xoffset - delete_char_width);
176
177
                /* No need to update cursor position, mark the cursor left area dirty.  */
178
129
                client.gx_rectangle_right = cursor_ptr -> gx_text_input_cursor_pos.gx_point_x;
179
129
                _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
180
            }
181

8
            else if ((leftoff == 0) && (rightoff > delete_char_width))
182
            {
183
                /* Decrease cursor position by the width of the deleted character width.  */
184
1
                cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_x - delete_char_width);
185
186
                /* No need to update x offset, mark the cursor right area dirty.  */
187
1
                client.gx_rectangle_left = cursor_ptr -> gx_text_input_cursor_pos.gx_point_x;
188
1
                _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
189
            }
190
            else
191
            {
192
7
                rightoff = (GX_VALUE)(rightoff - (delete_char_width - leftoff));
193
194
7
                if (rightoff > 0)
195
                {
196
1
                    text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)(text_input -> gx_single_line_text_input_xoffset - leftoff);
197
1
                    cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_x - delete_char_width + leftoff);
198
                }
199
                else
200
                {
201
6
                    text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)(text_input -> gx_single_line_text_input_xoffset - rightoff);
202
6
                    cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_x - delete_char_width - rightoff);
203
                }
204
205
7
                _gx_system_dirty_mark((GX_WIDGET *)text_input);
206
            }
207
137
            break;
208
209
136
        case GX_STYLE_TEXT_CENTER:
210
136
            x_pos = (GX_VALUE)(client.gx_rectangle_left + 1);
211
136
            x_pos = (GX_VALUE)(x_pos + ((client.gx_rectangle_right - client.gx_rectangle_left + 1) >> 1));
212
136
            x_pos = (GX_VALUE)(x_pos - text_input -> gx_single_line_text_input_xoffset);
213
136
            if (text_width >= (client.gx_rectangle_right - client.gx_rectangle_left + 1 - border_width))
214
            {
215
                /* Dirty mark this widget. */
216
108
                _gx_system_dirty_mark((GX_WIDGET *)text_input);
217
            }
218
            else
219
            {
220
28
                client.gx_rectangle_left = (GX_VALUE)(x_pos - (cursor_ptr -> gx_text_input_cursor_width >> 1));
221
28
                client.gx_rectangle_right = (GX_VALUE)(x_pos + text_width + ((cursor_ptr -> gx_text_input_cursor_width + 1) >> 1) - 1);
222
28
                _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
223
            }
224
            /* Calculate the cursor position. */
225
136
            text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)((text_width - delete_char_width + 1) >> 1);
226
136
            cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_x +
227
136
                                                                           (((text_width + 1) >> 1) - text_input -> gx_single_line_text_input_xoffset) -
228
                                                                           delete_char_width);
229
136
            break;
230
231
259
        case GX_STYLE_TEXT_LEFT:
232
        default:
233
259
            x_pos = (GX_VALUE)(client.gx_rectangle_left + 1);
234
259
            x_pos = (GX_VALUE)(x_pos - text_input -> gx_single_line_text_input_xoffset);
235
236
259
            new_x_pos = (GX_VALUE)(x_pos + delete_char_width);
237
238
259
            if (new_x_pos < client.gx_rectangle_left + 1)
239
            {
240
                /* Decrease x offset value by the width of the deleted character. */
241
192
                text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)(text_input -> gx_single_line_text_input_xoffset - delete_char_width);
242
243
                /* No need to update cursor position, mark the cursor left area dirty. */
244
192
                client.gx_rectangle_right = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_x + ((cursor_ptr -> gx_text_input_cursor_width + 1) >> 1) - 1);
245
192
                _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
246
            }
247
67
            else if (x_pos < client.gx_rectangle_left + 1)
248
            {
249
10
                cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_x - delete_char_width +
250
10
                                                                               text_input -> gx_single_line_text_input_xoffset);
251
252
10
                text_input -> gx_single_line_text_input_xoffset = 0;
253
254
10
                _gx_system_dirty_mark((GX_WIDGET *)text_input);
255
            }
256
            else
257
            {
258
                /* Decrease cursor position by the width of the deleted character.  */
259
57
                cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_x - delete_char_width);
260
261
                /* No need to update text input x offset, mark th ecursor right area dirty. */
262
57
                client.gx_rectangle_left = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_x - (cursor_ptr -> gx_text_input_cursor_width >> 1));
263
57
                _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
264
            }
265
259
            break;
266
        }
267
268
269
        /* Mark text input be changed.  */
270
532
        text_input -> gx_single_line_text_input_was_modified = GX_TRUE;
271
    }
272
273
554
    return GX_SUCCESS;
274
}
275