GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: gx_multi_line_text_input_backspace.c Lines: 59 59 100.0 %
Date: 2026-03-06 19:21:09 Branches: 28 28 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
/**   Multi Line Text Input Management (Multi 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_system.h"
29
#include "gx_multi_line_text_input.h"
30
#include "gx_multi_line_text_view.h"
31
#include "gx_window.h"
32
#include "gx_widget.h"
33
#include "gx_utility.h"
34
#include "gx_scrollbar.h"
35
36
/**************************************************************************/
37
/*                                                                        */
38
/*  FUNCTION                                               RELEASE        */
39
/*                                                                        */
40
/*    _gx_multi_line_text_input_backspace                 PORTABLE C      */
41
/*                                                           6.1.7        */
42
/*  AUTHOR                                                                */
43
/*                                                                        */
44
/*    Kenneth Maxwell, Microsoft Corporation                              */
45
/*                                                                        */
46
/*  DESCRIPTION                                                           */
47
/*                                                                        */
48
/*    This function deletes a character before the cursor.                */
49
/*                                                                        */
50
/*  INPUT                                                                 */
51
/*                                                                        */
52
/*    text_input                            Multi line text input widget  */
53
/*                                            control block               */
54
/*                                                                        */
55
/*  OUTPUT                                                                */
56
/*                                                                        */
57
/*    None                                                                */
58
/*                                                                        */
59
/*  CALLS                                                                 */
60
/*                                                                        */
61
/*    _gx_widget_font_get                   Retrieve font                 */
62
/*    _gx_utility_rectangle_resize          Increase/Shrink rectangle with*/
63
/*                                            specified value             */
64
/*    _gx_utility_utf8_string_character_get Parses utf8 string to         */
65
/*                                            multi-byte glyph            */
66
/*    _gx_utility_string_length_check       Test string length            */
67
/*    _gx_window_scrollbar_find             Find scrollbar for a window   */
68
/*    _gx_window_client_width_get           Get client width              */
69
/*    _gx_scrollbar_reset                   Reset scrollbar information   */
70
/*    _gx_multi_line_text_view_line_cache_update                          */
71
/*                                          Update line cache             */
72
/*    _gx_multi_line_text_view_display_info_get                           */
73
/*                                          Get the number of characters  */
74
/*                                            that a line can display     */
75
/*    _gx_multi_line_text_input_char_remove Remove a character            */
76
/*    _gx_multi_line_text_input_cursor_pos_update                         */
77
/*                                          Update cursor position        */
78
/*                                            according to insert index   */
79
/*    _gx_multi_line_text_view_string_total_rows_compute                  */
80
/*    _gx_system_string_width_get           Get string width              */
81
/*    _gx_system_dirty_partial_add          Add one dirty area to dirty   */
82
/*                                            list                        */
83
/*                                                                        */
84
/*                                                                        */
85
/*  CALLED BY                                                             */
86
/*                                                                        */
87
/*    _gx_multi_line_text_input_keydown_process                           */
88
/*                                                                        */
89
/**************************************************************************/
90
457
UINT _gx_multi_line_text_input_backspace(GX_MULTI_LINE_TEXT_INPUT *text_input)
91
{
92
457
GX_TEXT_INPUT_CURSOR    *cursor_ptr = &text_input -> gx_multi_line_text_input_cursor_instance;
93
457
GX_MULTI_LINE_TEXT_VIEW *view = (GX_MULTI_LINE_TEXT_VIEW *)text_input;
94
GX_FONT                 *font;
95
GX_RECTANGLE             client;
96
GX_RECTANGLE             dirty_rect;
97
UINT                     insert_pos;
98
INT                      shift;
99
INT                      last_visible_line;
100
GX_VALUE                 line_height;
101
GX_VALUE                 delete_width;
102
UINT                     old_text_rows;
103
457
UINT                     glyph_len = 1;
104
GX_STRING                string;
105
GX_SCROLLBAR            *scroll;
106
457
UINT                     start_mark = text_input -> gx_multi_line_text_input_start_mark;
107
457
UINT                     end_mark = text_input -> gx_multi_line_text_input_end_mark;
108
109
457
    if (end_mark < start_mark)
110
    {
111
34
        return _gx_multi_line_text_input_delete(text_input);
112
    }
113
114
423
    insert_pos = text_input -> gx_multi_line_text_input_text_insert_position;
115
116
423
    if (insert_pos > 0)
117
    {
118
341
        _gx_widget_font_get((GX_WIDGET *)text_input, text_input -> gx_multi_line_text_view_font_id, &font);
119
120
341
        if (!font)
121
        {
122
2
            return GX_FAILURE;
123
        }
124
125
339
        if (start_mark != end_mark)
126
        {
127
182
            glyph_len = (UINT)GX_ABS((INT)start_mark - (INT)end_mark);
128
182
            text_input -> gx_multi_line_text_input_start_mark = 0;
129
182
            text_input -> gx_multi_line_text_input_end_mark = 0;
130
131
182
            string.gx_string_ptr = &text_input -> gx_multi_line_text_view_text.gx_string_ptr[insert_pos - glyph_len];
132
        }
133
        else
134
        {
135
157
            _gx_multi_line_text_input_cursor_visible(text_input);
136
137
#ifdef GX_UTF8_SUPPORT
138
157
            _gx_utility_utf8_string_backward_character_length_get(&text_input -> gx_multi_line_text_view_text,
139
157
                                                                  (INT)(insert_pos - 1), &glyph_len);
140
#endif
141
142
            /* Get the character to be deleted.  */
143
157
            string.gx_string_ptr = &text_input -> gx_multi_line_text_view_text.gx_string_ptr[insert_pos - glyph_len];
144
145

157
            if (string.gx_string_ptr[0] == GX_KEY_CARRIAGE_RETURN || string.gx_string_ptr[0] == GX_KEY_LINE_FEED)
146
            {
147
64
                glyph_len = text_input -> gx_multi_line_text_input_new_line_character_size;
148
149
64
                string.gx_string_ptr = &text_input -> gx_multi_line_text_view_text.gx_string_ptr[insert_pos - glyph_len];
150
            }
151
        }
152
153
339
        string.gx_string_length = glyph_len;
154
155
        /* Get the width of the character to be deleted. */
156
339
        _gx_system_string_width_get_ext(font, &string, &delete_width);
157
158
        /* Record old shift value.  */
159
339
        shift = text_input -> gx_multi_line_text_view_text_scroll_shift;
160
161
        /* Remove character before cursor. */
162
339
        _gx_multi_line_text_input_char_remove(text_input, insert_pos - glyph_len, glyph_len);
163
164
        /* Save old text rows.  */
165
339
        old_text_rows = text_input -> gx_multi_line_text_view_text_total_rows;
166
167
        /* Calculate new text rows.  */
168
339
        _gx_multi_line_text_view_string_total_rows_compute((GX_MULTI_LINE_TEXT_VIEW *)text_input);
169
170
339
        if (old_text_rows != text_input -> gx_multi_line_text_view_text_total_rows)
171
        {
172
205
            _gx_window_scrollbar_find((GX_WINDOW *)text_input, GX_TYPE_VERTICAL_SCROLL, &scroll);
173
205
            if (scroll)
174
            {
175
                /* Reset scrollbar.  */
176
143
                _gx_scrollbar_reset(scroll, GX_NULL);
177
            }
178
            else
179
            {
180
62
                if (text_input -> gx_multi_line_text_view_text_total_rows >
181
62
                    view -> gx_multi_line_text_view_cache_size)
182
                {
183
1
                    _gx_multi_line_text_view_line_cache_update((GX_MULTI_LINE_TEXT_VIEW *)text_input);
184
                }
185
            }
186
        }
187
188
        /* Update text insert position.  */
189
339
        text_input -> gx_multi_line_text_input_text_insert_position -= glyph_len;
190
191
339
        client = text_input -> gx_window_client;
192
193
339
        if (text_input -> gx_multi_line_text_view_whitespace)
194
        {
195
            /* Offset client bounding box.  */
196
206
            _gx_utility_rectangle_resize(&client, (GX_VALUE)(-text_input -> gx_multi_line_text_view_whitespace));
197
        }
198
199
        /* Recalculate cursor position. */
200
339
        _gx_multi_line_text_input_cursor_pos_update(text_input, GX_TRUE);
201
202
339
        if (shift == text_input -> gx_multi_line_text_view_text_scroll_shift)
203
        {
204
205
271
            line_height = (GX_VALUE)(font -> gx_font_line_height + text_input -> gx_multi_line_text_view_line_space);
206
207
271
            if (line_height)
208
            {
209
                /* Calculate dirty area. */
210
270
                dirty_rect = client;
211
212
270
                dirty_rect.gx_rectangle_top = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_y - (line_height << 1));
213
214
                /* Calculate last visible line. */
215
270
                last_visible_line = (-text_input -> gx_multi_line_text_view_text_scroll_shift) / line_height;
216
270
                last_visible_line = last_visible_line + (INT)view -> gx_multi_line_text_view_text_visible_rows + 1;
217
218
270
                if (last_visible_line > (INT)text_input -> gx_multi_line_text_view_text_total_rows)
219
                {
220
147
                    last_visible_line = (INT)text_input -> gx_multi_line_text_view_text_total_rows;
221
                }
222
223
270
                shift = (last_visible_line + (INT)old_text_rows - (INT)text_input -> gx_multi_line_text_view_text_total_rows) * line_height + shift;
224
270
                shift += client.gx_rectangle_top;
225
226
270
                if (shift < dirty_rect.gx_rectangle_bottom)
227
                {
228
86
                    dirty_rect.gx_rectangle_bottom = (GX_VALUE)shift;
229
                }
230
231
270
                _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &dirty_rect);
232
            }
233
        }
234
        else
235
        {
236
68
            _gx_system_dirty_mark((GX_WIDGET *)text_input);
237
        }
238
    }
239
240
421
    return GX_SUCCESS;
241
}
242