GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: gx_multi_line_text_input_text_set.c Lines: 56 56 100.0 %
Date: 2026-08-28 02:37:13 Branches: 26 26 100.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 * Copyright (c) 2026 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
// Some portions generated by Claude Code (Opus 5).
13
14
15
/**************************************************************************/
16
/**************************************************************************/
17
/**                                                                       */
18
/** GUIX Component                                                        */
19
/**                                                                       */
20
/**   Multi Line Text Input Managment (Multi Line Text Input)             */
21
/**                                                                       */
22
/**************************************************************************/
23
24
#define GX_SOURCE_CODE
25
26
27
/* Include necessary system files.  */
28
29
#include "gx_api.h"
30
#include "gx_system.h"
31
#include "gx_multi_line_text_view.h"
32
#include "gx_multi_line_text_input.h"
33
#include "gx_scrollbar.h"
34
#include "gx_window.h"
35
#include "gx_widget.h"
36
#include "gx_utility.h"
37
38
/**************************************************************************/
39
/*                                                                        */
40
/*  FUNCTION                                               RELEASE        */
41
/*                                                                        */
42
/*    _gx_multi_line_text_input_new_line_character_get    PORTABLE C      */
43
/*                                                           6.1          */
44
/*  AUTHOR                                                                */
45
/*                                                                        */
46
/*    Kenneth Maxwell, Microsoft Corporation                              */
47
/*                                                                        */
48
/*  DESCRIPTION                                                           */
49
/*                                                                        */
50
/*    Internal helper function to retrieve the new line character of the  */
51
/*    input text.                                                         */
52
/*                                                                        */
53
/*  INPUT                                                                 */
54
/*                                                                        */
55
/*    text_view                             Multi-line text input widget  */
56
/*                                            control block               */
57
/*                                                                        */
58
/*  OUTPUT                                                                */
59
/*                                                                        */
60
/*    status                                Completion status             */
61
/*                                                                        */
62
/*  CALLS                                                                 */
63
/*                                                                        */
64
/*    memcpy                                Copy block of memory          */
65
/*    _gx_utility_utf8_string_character_get Parse utf8 string to          */
66
/*                                            multi-byte glyph            */
67
/*                                                                        */
68
/*  CALLED BY                                                             */
69
/*                                                                        */
70
/*    _gx_multi_line_text_input_text_set                                  */
71
/*                                                                        */
72
/**************************************************************************/
73
1111
static VOID _gx_multi_line_text_input_new_line_character_get(GX_MULTI_LINE_TEXT_INPUT *text_input)
74
{
75
GX_STRING         string;
76
GX_CONST GX_CHAR *ch;
77
78
#ifdef GX_UTF8_SUPPORT
79
UINT glyph_len;
80
#endif
81
82
1111
    string = text_input -> gx_multi_line_text_view_text;
83
84
✓✓
38687
    while (string.gx_string_length > 0)
85
    {
86
38470
        ch = string.gx_string_ptr;
87
88
#ifdef GX_UTF8_SUPPORT
89
38470
        _gx_utility_utf8_string_character_get(&string, GX_NULL, &glyph_len);
90
#else
91
        string.gx_string_ptr++;
92
        string.gx_string_length--;
93
#endif /* GX_UTF8_SUPPORT */
94
95
✓✓
38470
        if (ch[0] == GX_KEY_CARRIAGE_RETURN)
96
        {
97
            /* string now points at the byte after ch. Read it through string, so
98
               that its length bounds the access: ch[1] would rely on the caller
99
               having NUL terminated a GX_STRING, which carries its own length
100
               and need not be terminated at all. */
101
✓✓✓✓
887
            if ((string.gx_string_length > 0) && (string.gx_string_ptr[0] == GX_KEY_LINE_FEED))
102
            {
103
6
                memcpy(text_input -> gx_multi_line_text_input_new_line_character, "\r\n", 3); /* Use case of memcpy is verified. */
104
6
                text_input -> gx_multi_line_text_input_new_line_character_size = 2;
105
            }
106
            else
107
            {
108
881
                memcpy(text_input -> gx_multi_line_text_input_new_line_character, "\r", 2); /* Use case of memcpy is verified. */
109
881
                text_input -> gx_multi_line_text_input_new_line_character_size = 1;
110
            }
111
112
887
            break;
113
        }
114
✓✓
37583
        else if (ch[0] == GX_KEY_LINE_FEED)
115
        {
116
7
            memcpy(text_input -> gx_multi_line_text_input_new_line_character, "\n", 2); /* Use case of memcpy is verified. */
117
7
            text_input -> gx_multi_line_text_input_new_line_character_size = 1;
118
7
            break;
119
        }
120
    }
121
1111
}
122
123
/**************************************************************************/
124
/*                                                                        */
125
/*  FUNCTION                                               RELEASE        */
126
/*                                                                        */
127
/*    _gx_multi_line_text_input_text_set                  PORTABLE C      */
128
/*                                                           6.1          */
129
/*  AUTHOR                                                                */
130
/*                                                                        */
131
/*    Kenneth Maxwell, Microsoft Corporation                              */
132
/*                                                                        */
133
/*  DESCRIPTION (Deprecated)                                              */
134
/*                                                                        */
135
/*    This function assigns a text string to a multi line text input      */
136
/*    widget.                                                             */
137
/*                                                                        */
138
/*  INPUT                                                                 */
139
/*                                                                        */
140
/*    text_view                             Multi-line text input widget  */
141
/*                                            control block               */
142
/*    text                                  Null-terminated text string   */
143
/*                                                                        */
144
/*  OUTPUT                                                                */
145
/*                                                                        */
146
/*    status                                Completion status             */
147
/*                                                                        */
148
/*  CALLS                                                                 */
149
/*                                                                        */
150
/*    _gx_utility_string_length_check       Calculate string length       */
151
/*    _gx_multi_line_text_input_text_set_ext                              */
152
/*                                          New text set function         */
153
/*                                                                        */
154
/*  CALLED BY                                                             */
155
/*                                                                        */
156
/*    Application Code                                                    */
157
/*                                                                        */
158
/**************************************************************************/
159
#if defined(GX_ENABLE_DEPRECATED_STRING_API)
160
928
UINT _gx_multi_line_text_input_text_set(GX_MULTI_LINE_TEXT_INPUT *text_input, GX_CONST GX_CHAR *text)
161
{
162
UINT      status;
163
UINT      str_len;
164
GX_STRING new_string;
165
166
928
    new_string.gx_string_ptr = text;
167
928
    status = _gx_utility_string_length_check(text, &str_len, GX_MAX_STRING_LENGTH);
168
✓✓
928
    if (status == GX_SUCCESS)
169
    {
170
927
        new_string.gx_string_length = str_len;
171
927
        status = _gx_multi_line_text_input_text_set_ext(text_input, &new_string);
172
    }
173
174
928
    return(status);
175
}
176
#endif
177
178
/**************************************************************************/
179
/*                                                                        */
180
/*  FUNCTION                                               RELEASE        */
181
/*                                                                        */
182
/*    _gx_multi_line_text_input_text_set_ext              PORTABLE C      */
183
/*                                                           6.1          */
184
/*  AUTHOR                                                                */
185
/*                                                                        */
186
/*    Kenneth Maxwell, Microsoft Corporation                              */
187
/*                                                                        */
188
/*  DESCRIPTION                                                           */
189
/*                                                                        */
190
/*    This function assigns a text string to a multi line text input      */
191
/*    widget.                                                             */
192
/*                                                                        */
193
/*  INPUT                                                                 */
194
/*                                                                        */
195
/*    text_view                             Multi-line text input widget  */
196
/*                                            control block               */
197
/*    text                                  Null-terminated text string   */
198
/*                                                                        */
199
/*  OUTPUT                                                                */
200
/*                                                                        */
201
/*    status                                Completion status             */
202
/*                                                                        */
203
/*  CALLS                                                                 */
204
/*                                                                        */
205
/*    _gx_multi_line_text_input_new_line_character_get                    */
206
/*                                          Get the new line character    */
207
/*    _gx_multi_line_text_view_string_total_rows_compute                  */
208
/*                                          Compute the total rows of     */
209
/*                                            the input text              */
210
/*    _gx_multi_line_text_view_line_cache_update                          */
211
/*                                          Update line cache             */
212
/*    _gx_window_scrollbar_find             Find scrollbar for a window   */
213
/*    _gx_scrollbar_reset                   Reset scrollbar information   */
214
/*    _gx_system_dirty_mark                 Mark the widget dirty         */
215
/*                                                                        */
216
/*  CALLED BY                                                             */
217
/*                                                                        */
218
/*    Application Code                                                    */
219
/*                                                                        */
220
/**************************************************************************/
221
1111
UINT _gx_multi_line_text_input_text_set_ext(GX_MULTI_LINE_TEXT_INPUT *text_input, GX_CONST GX_STRING *text)
222
{
223
GX_SCROLLBAR *scroll;
224
UINT          copy_size;
225
226
#if defined GX_UTF8_SUPPORT
227
GX_STRING string;
228
UINT      index;
229
1111
UINT      glyph_len = 0;
230
#endif
231
232
✓✓✓✓
1111
    if (text && text -> gx_string_ptr)
233
    {
234
1109
        copy_size = text_input -> gx_multi_line_text_input_buffer_size - 1;
235
236
✓✓
1109
        if (text -> gx_string_length > copy_size - 1)
237
        {
238
#if defined GX_UTF8_SUPPORT
239
4
            string.gx_string_ptr = text -> gx_string_ptr;
240
4
            string.gx_string_length = copy_size;
241
242
4
            index = 0;
243
244
            /* Calculate maximum bytes that can be copied. */
245
✓✓
476
            while (_gx_utility_utf8_string_character_get(&string, GX_NULL, &glyph_len) == GX_SUCCESS)
246
            {
247
472
                index += glyph_len;
248
            }
249
4
            copy_size = index;
250
#endif
251
        }
252
        else
253
        {
254
1105
            copy_size = text -> gx_string_length;
255
        }
256
257
1109
        memcpy((GX_CHAR *)text_input -> gx_multi_line_text_view_text.gx_string_ptr, text -> gx_string_ptr, copy_size); /* Use case of memcpy is verified. */
258
1109
        text_input -> gx_multi_line_text_view_text.gx_string_length = copy_size;
259
    }
260
    else
261
    {
262
2
        copy_size = 0;
263
    }
264
265
1111
    ((GX_CHAR *)text_input -> gx_multi_line_text_view_text.gx_string_ptr)[copy_size] = '\0';
266
267
1111
    text_input -> gx_multi_line_text_view_text.gx_string_length = copy_size;
268
269
    /* Get new line character. */
270
1111
    _gx_multi_line_text_input_new_line_character_get(text_input);
271
272
✓✓
1111
    if (text_input -> gx_widget_status & GX_STATUS_VISIBLE)
273
    {
274
        /* Compute the total rows of input string. */
275
893
        _gx_multi_line_text_view_string_total_rows_compute((GX_MULTI_LINE_TEXT_VIEW *)text_input);
276
277
893
        text_input -> gx_multi_line_text_input_text_insert_position = copy_size;
278
279
893
        _gx_window_scrollbar_find((GX_WINDOW *)text_input, GX_TYPE_VERTICAL_SCROLL, &scroll);
280
✓✓
893
        if (scroll)
281
        {
282
            /* Reset scrollbar.  */
283
887
            _gx_scrollbar_reset(scroll, GX_NULL);
284
        }
285
        else
286
        {
287
288
6
            if (text_input -> gx_multi_line_text_view_text_total_rows >
289
✓✓
6
                text_input -> gx_multi_line_text_view_cache_size)
290
            {
291
                /* Update line cache. */
292
1
                _gx_multi_line_text_view_line_cache_update((GX_MULTI_LINE_TEXT_VIEW *)text_input);
293
            }
294
        }
295
296
        /* Update cursor position. */
297
893
        _gx_multi_line_text_input_cursor_pos_update(text_input, GX_TRUE);
298
299
893
        _gx_system_dirty_mark((GX_WIDGET *)text_input);
300
    }
301
    else
302
    {
303
218
        text_input -> gx_multi_line_text_view_line_index_old = GX_TRUE;
304
218
        text_input -> gx_multi_line_text_input_text_insert_position = 0;
305
    }
306
1111
    return(GX_SUCCESS);
307
}
308