GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: gx_multi_line_text_input_char_insert.c Lines: 74 74 100.0 %
Date: 2026-03-06 19:21:09 Branches: 36 36 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_utility.h"
30
#include "gx_scrollbar.h"
31
#include "gx_multi_line_text_input.h"
32
#include "gx_multi_line_text_view.h"
33
#include "gx_window.h"
34
#include "gx_widget.h"
35
36
/**************************************************************************/
37
/*                                                                        */
38
/*  FUNCTION                                               RELEASE        */
39
/*                                                                        */
40
/*    _gx_multi_line_text_input_char_insert               PORTABLE C      */
41
/*                                                           6.1          */
42
/*  AUTHOR                                                                */
43
/*                                                                        */
44
/*    Kenneth Maxwell, Microsoft Corporation                              */
45
/*                                                                        */
46
/*  DESCRIPTION (deprecated)                                              */
47
/*                                                                        */
48
/*    This function inserts a character into the input buffer.            */
49
/*                                                                        */
50
/*  INPUT                                                                 */
51
/*                                                                        */
52
/*    text_input                            Multi line text input widget  */
53
/*                                            control block               */
54
/*    str                                   Utf8 string to be inserted    */
55
/*    str_size                              Inserted string size in bytes */
56
/*                                                                        */
57
/*  OUTPUT                                                                */
58
/*                                                                        */
59
/*    None                                                                */
60
/*                                                                        */
61
/*  CALLS                                                                 */
62
/*                                                                        */
63
/*    _gx_multi_line_text_input_char_insert_ext                           */
64
/*                                                                        */
65
/*  CALLED BY                                                             */
66
/*                                                                        */
67
/*    _gx_multi_line_text_input_keydown_process                           */
68
/*                                                                        */
69
/**************************************************************************/
70
#if defined(GX_ENABLE_DEPRECATED_STRING_API)
71
1
UINT _gx_multi_line_text_input_char_insert(GX_MULTI_LINE_TEXT_INPUT *text_input, GX_UBYTE *str, UINT str_size)
72
{
73
UINT      status;
74
GX_STRING string;
75
76
1
    string.gx_string_ptr = (GX_CHAR *)str;
77
1
    string.gx_string_length = str_size;
78
1
    status = _gx_multi_line_text_input_char_insert_ext(text_input, &string);
79
80
1
    return status;
81
}
82
#endif
83
84
/**************************************************************************/
85
/*                                                                        */
86
/*  FUNCTION                                               RELEASE        */
87
/*                                                                        */
88
/*    _gx_multi_line_text_input_char_insert_ext           PORTABLE C      */
89
/*                                                           6.1          */
90
/*  AUTHOR                                                                */
91
/*                                                                        */
92
/*    Kenneth Maxwell, Microsoft Corporation                              */
93
/*                                                                        */
94
/*  DESCRIPTION                                                           */
95
/*                                                                        */
96
/*    This function inserts a character into the input buffer.            */
97
/*                                                                        */
98
/*  INPUT                                                                 */
99
/*                                                                        */
100
/*    text_input                            Multi line text input widget  */
101
/*                                            control block               */
102
/*    str                                   Utf8 string to be inserted    */
103
/*    str_size                              Inserted string size in bytes */
104
/*                                                                        */
105
/*  OUTPUT                                                                */
106
/*                                                                        */
107
/*    None                                                                */
108
/*                                                                        */
109
/*  CALLS                                                                 */
110
/*                                                                        */
111
/*    memmove                               Move block of memory          */
112
/*    _gx_widget_font_get                   Retrieve font                 */
113
/*    _gx_window_scrollbar_find             Find scrollbar for a window   */
114
/*    _gx_scrollbar_reset                   Reset scrollbar information   */
115
/*    _gx_multi_line_text_view_string_total_rows_compute                  */
116
/*                                          Calculate total rows          */
117
/*    _gx_multi_line_text_view_line_cache_update                          */
118
/*                                          Update line cache             */
119
/*    _gx_multi_line_text_input_cursor_pos_update                         */
120
/*                                          Update cursor position        */
121
/*                                            according to insert index   */
122
/*    _gx_utility_rectangle_resize          Increase/Shrink rectangle by  */
123
/*                                            specified value             */
124
/*    _gx_utility_string_length_check       Test string length            */
125
/*    _gx_system_dirty_partial_add          Add one dirty area to dirty   */
126
/*                                            list                        */
127
/*                                                                        */
128
/*  CALLED BY                                                             */
129
/*                                                                        */
130
/*    _gx_multi_line_text_input_keydown_process                           */
131
/*                                                                        */
132
/**************************************************************************/
133
1825
UINT _gx_multi_line_text_input_char_insert_ext(GX_MULTI_LINE_TEXT_INPUT *text_input, GX_CONST GX_STRING *str)
134
{
135
1825
GX_TEXT_INPUT_CURSOR *cursor_ptr = &text_input -> gx_multi_line_text_input_cursor_instance;
136
UINT                  string_size;
137
UINT                  buffer_size;
138
GX_CHAR              *input_buffer;
139
GX_UBYTE             *insert_char;
140
UINT                  index;
141
INT                   shift;
142
INT                   last_visible_line;
143
GX_RECTANGLE          client;
144
GX_RECTANGLE          dirty_rect;
145
GX_FONT              *font;
146
GX_VALUE              line_height;
147
GX_VALUE              old_cur_y;
148
GX_SCROLLBAR         *scroll;
149
UINT                  text_rows;
150
UINT                  glyph_len;
151
UINT                  handled_size;
152
GX_STRING             insert_str;
153
154
1825
    if (text_input -> gx_multi_line_text_input_start_mark != text_input -> gx_multi_line_text_input_end_mark)
155
    {
156
142
        if (text_input -> gx_multi_line_text_input_end_mark < text_input -> gx_multi_line_text_input_start_mark)
157
        {
158
68
            _gx_multi_line_text_input_delete(text_input);
159
        }
160
        else
161
        {
162
74
            _gx_multi_line_text_input_backspace(text_input);
163
        }
164
    }
165
166
1825
    string_size = text_input -> gx_multi_line_text_view_text.gx_string_length;
167
1825
    buffer_size = text_input -> gx_multi_line_text_input_buffer_size;
168
1825
    insert_str = *str;
169
170
1825
    _gx_widget_font_get((GX_WIDGET *)text_input, text_input -> gx_multi_line_text_view_font_id, &font);
171
172
1825
    if (!font)
173
    {
174
1
        return GX_FAILURE;
175
    }
176
177
    /* Check if the insert string size is bigger than the remaining buffer size.  */
178
1824
    if (buffer_size < string_size + insert_str.gx_string_length + 1)
179
    {
180
14
        return GX_FAILURE;
181
    }
182
183
    /* Insert a character. */
184
185
1810
    input_buffer = (GX_CHAR *)text_input -> gx_multi_line_text_view_text.gx_string_ptr;
186
1810
    glyph_len = 1;
187
188
4690
    while (insert_str.gx_string_length)
189
    {
190

2881
        if (insert_str.gx_string_ptr[0] == GX_KEY_CARRIAGE_RETURN || insert_str.gx_string_ptr[0] == GX_KEY_LINE_FEED)
191
        {
192
            /* If the inserted character is a new line character,
193
               exchange it with current used new line character. */
194
827
            insert_char = text_input -> gx_multi_line_text_input_new_line_character;
195
827
            glyph_len = text_input -> gx_multi_line_text_input_new_line_character_size;
196
197
827
            if (insert_str.gx_string_ptr[0] == GX_KEY_CARRIAGE_RETURN)
198
            {
199

825
                if ((insert_str.gx_string_length > 1) && insert_str.gx_string_ptr[1] == GX_KEY_LINE_FEED)
200
                {
201
2
                    handled_size = 2;
202
                }
203
                else
204
                {
205
823
                    handled_size = 1;
206
                }
207
            }
208
            else
209
            {
210
2
                handled_size = 1;
211
            }
212
213
827
            insert_str.gx_string_ptr += handled_size;
214
827
            insert_str.gx_string_length -= handled_size;
215
        }
216
        else
217
        {
218
2054
            insert_char = (GX_UBYTE *)insert_str.gx_string_ptr;
219
#if defined GX_UTF8_SUPPORT
220
2054
            _gx_utility_utf8_string_character_get(&insert_str, GX_NULL, &glyph_len);
221
#else
222
            glyph_len = 1;
223
            insert_str.gx_string_ptr++;
224
            insert_str.gx_string_length--;
225
#endif
226
        }
227
228
2881
        string_size = text_input -> gx_multi_line_text_view_text.gx_string_length;
229
230
2881
        if (buffer_size < string_size + glyph_len + 1)
231
        {
232
1
            break;
233
        }
234
2880
        index = text_input -> gx_multi_line_text_input_text_insert_position;
235
2880
        memmove(input_buffer + index + glyph_len, input_buffer + index, string_size - index + 1);
236
2880
        memmove(input_buffer + index, insert_char, glyph_len);
237
238
        /* Update insert position and string size. */
239
2880
        text_input -> gx_multi_line_text_input_text_insert_position += glyph_len;
240
2880
        text_input -> gx_multi_line_text_view_text.gx_string_length += glyph_len;
241
    }
242
243
    /* Save old text rows.  */
244
1810
    text_rows = text_input -> gx_multi_line_text_view_text_total_rows;
245
246
    /* Calculate new text rows. */
247
1810
    _gx_multi_line_text_view_string_total_rows_compute((GX_MULTI_LINE_TEXT_VIEW *)text_input);
248
249
1810
    if (text_rows != text_input -> gx_multi_line_text_view_text_total_rows)
250
    {
251
607
        _gx_window_scrollbar_find((GX_WINDOW *)text_input, GX_TYPE_VERTICAL_SCROLL, &scroll);
252
607
        if (scroll)
253
        {
254
            /* Reset scrollbar.  */
255
369
            _gx_scrollbar_reset(scroll, GX_NULL);
256
        }
257
        else
258
        {
259
260
238
            if (text_input -> gx_multi_line_text_view_text_total_rows >
261
238
                text_input -> gx_multi_line_text_view_cache_size)
262
            {
263
5
                _gx_multi_line_text_view_line_cache_update((GX_MULTI_LINE_TEXT_VIEW *)text_input);
264
            }
265
        }
266
    }
267
268
    /* Save old shift value. */
269
1810
    shift = text_input -> gx_multi_line_text_view_text_scroll_shift;
270
271
    /* Record old y coordinate of cursor position. */
272
1810
    old_cur_y = cursor_ptr -> gx_text_input_cursor_pos.gx_point_y;
273
274
    /* Update cursor position. */
275
1810
    _gx_multi_line_text_input_cursor_pos_update(text_input, GX_TRUE);
276
277
    /* Set the text modified flag to GX_TRUE. */
278
1810
    text_input -> gx_multi_line_text_input_text_was_modified = GX_TRUE;
279
280
1810
    if (shift == text_input -> gx_multi_line_text_view_text_scroll_shift)
281
    {
282
1505
        line_height = (GX_VALUE)(font -> gx_font_line_height +
283
1505
                                 text_input -> gx_multi_line_text_view_line_space);
284
285
1505
        if (line_height)
286
        {
287
            /* Calculate dirty area. */
288
1504
            client = text_input -> gx_window_client;
289
290
            /* Offset client area by length of whitespace.  */
291
1504
            _gx_utility_rectangle_resize(&client, (GX_VALUE)(-text_input -> gx_multi_line_text_view_whitespace));
292
293
1504
            dirty_rect = client;
294
295
            /* Calculate dirty area. */
296
1504
            dirty_rect.gx_rectangle_top = (GX_VALUE)(old_cur_y - line_height - (line_height >> 1));
297
298
            /* Calculate last visible line. */
299
1504
            last_visible_line = (-text_input -> gx_multi_line_text_view_text_scroll_shift) / line_height;
300
1504
            last_visible_line = last_visible_line + (INT)text_input -> gx_multi_line_text_view_text_visible_rows + 1;
301
302
1504
            if (last_visible_line > (INT)text_input -> gx_multi_line_text_view_text_total_rows)
303
            {
304
1371
                last_visible_line = (INT)text_input -> gx_multi_line_text_view_text_total_rows;
305
            }
306
307
1504
            shift = last_visible_line * line_height + shift;
308
1504
            shift += client.gx_rectangle_top;
309
310
1504
            if (shift < dirty_rect.gx_rectangle_bottom)
311
            {
312
954
                dirty_rect.gx_rectangle_bottom = (GX_VALUE)shift;
313
            }
314
315
1504
            _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &dirty_rect);
316
        }
317
    }
318
    else
319
    {
320
305
        _gx_system_dirty_mark((GX_WIDGET *)text_input);
321
    }
322
323
1810
    return GX_SUCCESS;
324
}
325