GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: gx_single_line_text_input_mark_previous.c Lines: 48 48 100.0 %
Date: 2026-03-06 19:21:09 Branches: 14 14 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
/**   Text Input Management (Single 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_context.h"
29
#include "gx_system.h"
30
#include "gx_widget.h"
31
#include "gx_single_line_text_input.h"
32
#include "gx_text_input_cursor.h"
33
#include "gx_utility.h"
34
35
/**************************************************************************/
36
/*                                                                        */
37
/*  FUNCTION                                               RELEASE        */
38
/*                                                                        */
39
/*    _gx_single_line_text_input_mark_previous            PORTABLE C      */
40
/*                                                           6.1          */
41
/*  AUTHOR                                                                */
42
/*                                                                        */
43
/*    Kenneth Maxwell, Microsoft Corporation                              */
44
/*                                                                        */
45
/*  DESCRIPTION                                                           */
46
/*                                                                        */
47
/*    This function highlights the character before the end mark position.*/
48
/*                                                                        */
49
/*  INPUT                                                                 */
50
/*                                                                        */
51
/*    text_input                            Single-line text input widget */
52
/*                                            control block               */
53
/*                                                                        */
54
/*  OUTPUT                                                                */
55
/*                                                                        */
56
/*    None                                                                */
57
/*                                                                        */
58
/*  CALLS                                                                 */
59
/*                                                                        */
60
/*    _gx_utility_utf8_string_character_get Parse utf8 string to          */
61
/*                                            multi-byte glyph            */
62
/*    _gx_widget_border_width_get           Get the widget border width   */
63
/*    _gx_widget_client_get                 Get widget client rectangle   */
64
/*    _gx_widget_font_get                   Get font by specified ID      */
65
/*    _gx_system_string_width_get           Get the width of a string     */
66
/*    _gx_system_dirty_partial_add          Mark the partial area of a    */
67
/*                                            widget as dirty             */
68
/*    _gx_text_input_cursor_dirty_rectangle_get                           */
69
/*                                          Get cursor rectangle          */
70
/*                                                                        */
71
/*  CALLED BY                                                             */
72
/*                                                                        */
73
/*    GUIX Internal Code                                                  */
74
/*                                                                        */
75
/**************************************************************************/
76
805
UINT _gx_single_line_text_input_mark_previous(GX_SINGLE_LINE_TEXT_INPUT *text_input)
77
{
78
805
GX_TEXT_INPUT_CURSOR *cursor_ptr = &text_input -> gx_single_line_text_input_cursor_instance;
79
GX_RECTANGLE          client;
80
GX_RECTANGLE          cursor_rect;
81
GX_VALUE              new_cursor_pos;
82
GX_VALUE              width;
83
GX_FONT              *gx_font;
84
805
UINT                  glyph_len = 1;
85
805
GX_BOOL               mark_all = GX_FALSE;
86
805
UINT                  start_mark = text_input -> gx_single_line_text_input_start_mark;
87
805
UINT                  end_mark = text_input -> gx_single_line_text_input_end_mark;
88
GX_STRING             string;
89
90
805
    if (start_mark == end_mark)
91
    {
92
34
        start_mark = text_input -> gx_single_line_text_input_insert_pos;
93
34
        end_mark = text_input -> gx_single_line_text_input_insert_pos;
94
95
34
        text_input -> gx_single_line_text_input_start_mark = start_mark;
96
34
        text_input -> gx_single_line_text_input_end_mark = end_mark;
97
    }
98
99
805
    string.gx_string_ptr = (GX_CONST GX_CHAR *)text_input -> gx_single_line_text_input_buffer;
100
805
    string.gx_string_length = text_input -> gx_single_line_text_input_string_size;
101
102
805
    if (end_mark > 0)
103
    {
104
#ifdef GX_UTF8_SUPPORT
105
        /* Get the glyph length of the character before end mark */
106
615
        _gx_utility_utf8_string_backward_character_length_get(&string, (INT)(end_mark - 1), &glyph_len);
107
#endif
108
109
615
        text_input -> gx_single_line_text_input_end_mark -= glyph_len;
110
615
        text_input -> gx_single_line_text_input_insert_pos -= glyph_len;
111
112
        /* Pick up widget border width. */
113
615
        _gx_widget_border_width_get((GX_WIDGET *)text_input, &width);
114
115
        /* Get widget client rectangle. */
116
615
        _gx_widget_client_get((GX_WIDGET *)text_input, width, &client);
117
118
        /* Pick up text font. */
119
615
        _gx_widget_font_get((GX_WIDGET *)text_input, text_input -> gx_prompt_font_id, &gx_font);
120
121
        /* Get width of the character before end mark. */
122
615
        string.gx_string_ptr += (end_mark - glyph_len);
123
615
        string.gx_string_length = glyph_len;
124
615
        _gx_system_string_width_get_ext(gx_font, &string, &width);
125
126
615
        new_cursor_pos = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_x - width);
127
128
615
        switch (text_input -> gx_widget_style & GX_STYLE_TEXT_ALIGNMENT_MASK)
129
        {
130
198
        case GX_STYLE_TEXT_CENTER:
131
198
            break;
132
133
417
        case GX_STYLE_TEXT_RIGHT:
134
        case GX_STYLE_TEXT_LEFT:
135
        default:
136
417
            if (new_cursor_pos < client.gx_rectangle_left + 1)
137
            {
138
                /* The previous marked character is out of client area, update x offset. */
139
50
                text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)(text_input -> gx_single_line_text_input_xoffset -
140
50
                                                                             client.gx_rectangle_left - 1 + new_cursor_pos);
141
142
50
                new_cursor_pos = (GX_VALUE)(client.gx_rectangle_left + 1);
143
144
                /* Should mark all widget as dirty*/
145
50
                mark_all = GX_TRUE;
146
            }
147
367
            else if (new_cursor_pos > client.gx_rectangle_right - 1)
148
            {
149
                /* Cursor position is out of client rectangle, update x offset. */
150
8
                text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)(text_input -> gx_single_line_text_input_xoffset +
151
8
                                                                             new_cursor_pos - client.gx_rectangle_right + 1);
152
8
                new_cursor_pos = (GX_VALUE)(client.gx_rectangle_right - 1);
153
154
                /* Should mark all widget as dirty.  */
155
8
                mark_all = GX_TRUE;
156
            }
157
417
            break;
158
        }
159
160
161
615
        if (!mark_all)
162
        {
163
557
            client.gx_rectangle_left = new_cursor_pos;
164
557
            client.gx_rectangle_right = (GX_VALUE)(cursor_ptr -> gx_text_input_cursor_pos.gx_point_x - 1);
165
166
557
            if (start_mark == end_mark)
167
            {
168
                /* Get old cursor rectangle. */
169
31
                _gx_text_input_cursor_dirty_rectangle_get(cursor_ptr, &cursor_rect);
170
171
                /* Combine with old cursor rectangle. */
172
31
                _gx_utility_rectangle_combine(&client, &cursor_rect);
173
            }
174
        }
175
176
615
        cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = new_cursor_pos;
177
178
615
        _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
179
    }
180
181
805
    return GX_SUCCESS;
182
}
183