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