GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: gx_single_line_text_input_end.c Lines: 49 49 100.0 %
Date: 2024-12-05 08:52:37 Branches: 15 15 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
33
/**************************************************************************/
34
/*                                                                        */
35
/*  FUNCTION                                               RELEASE        */
36
/*                                                                        */
37
/*    _gx_single_line_text_input_end                      PORTABLE C      */
38
/*                                                           6.1          */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    Kenneth Maxwell, Microsoft Corporation                              */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This service positions the text input widget cursor at the end of   */
46
/*    the input string.                                                   */
47
/*                                                                        */
48
/*  INPUT                                                                 */
49
/*                                                                        */
50
/*    text_input                            Single line text input widget */
51
/*                                            control blcok               */
52
/*                                                                        */
53
/*  OUTPUT                                                                */
54
/*                                                                        */
55
/*    status                                Completion status             */
56
/*                                                                        */
57
/*  CALLS                                                                 */
58
/*                                                                        */
59
/*    _gx_widget_font_get                   Get font by specified ID      */
60
/*    _gx_widget_border_width_get           Get the widget border width   */
61
/*    _gx_widget_client_get                 Retrieves client area of the  */
62
/*                                            widget                      */
63
/*    _gx_system_string_width_get           Get the width of a string     */
64
/*    _gx_system_dirty_mark                 Mart the area of the widget   */
65
/*                                            dirty                       */
66
/*    _gx_system_dirty_partial_add          Mark the partial area of a    */
67
/*                                            widget as dirty             */
68
/*                                                                        */
69
/*  CALLED BY                                                             */
70
/*                                                                        */
71
/*    Application Code                                                    */
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
661
UINT _gx_single_line_text_input_end(GX_SINGLE_LINE_TEXT_INPUT *text_input)
84
{
85
GX_TEXT_INPUT_CURSOR *cursor_ptr;
86
GX_VALUE              text_width;
87
GX_RECTANGLE          client;
88
GX_VALUE              new_xoffset;
89
GX_VALUE              new_cursor_pos;
90
GX_VALUE              border_width;
91
GX_FONT              *gx_font;
92
661
UINT                  start_mark = text_input -> gx_single_line_text_input_start_mark;
93
661
UINT                  end_mark = text_input -> gx_single_line_text_input_end_mark;
94
661
GX_BOOL               mark_new_cursor_dirty = GX_FALSE;
95
GX_STRING             string;
96
97
661
    if (start_mark != end_mark)
98
    {
99
79
        text_input -> gx_single_line_text_input_start_mark = 0;
100
79
        text_input -> gx_single_line_text_input_end_mark = 0;
101
    }
102
103
661
    cursor_ptr = &text_input -> gx_single_line_text_input_cursor_instance;
104
661
    string.gx_string_ptr = text_input -> gx_single_line_text_input_buffer;
105
661
    string.gx_string_length = text_input -> gx_single_line_text_input_string_size;
106
107
    /* Pick up text width. */
108
661
    _gx_widget_font_get((GX_WIDGET *)text_input, text_input -> gx_prompt_font_id, &gx_font);
109
110
    /* Calculate the widget width for showing text. */
111
661
    _gx_widget_border_width_get((GX_WIDGET *)text_input, &border_width);
112
661
    _gx_widget_client_get((GX_WIDGET *)text_input, border_width, &client);
113
114
    /* Update the value of cursor position and xoffset. */
115
661
    switch (text_input -> gx_widget_style & GX_STYLE_TEXT_ALIGNMENT_MASK)
116
    {
117
176
    case GX_STYLE_TEXT_RIGHT:
118
176
        new_xoffset = 0;
119
176
        new_cursor_pos = (GX_VALUE)(client.gx_rectangle_right - 1);
120
176
        break;
121
122
176
    case GX_STYLE_TEXT_CENTER:
123
176
        new_xoffset = 0;
124
176
        new_cursor_pos = (GX_VALUE)(client.gx_rectangle_left + 1 + ((client.gx_rectangle_right - client.gx_rectangle_left + 1) >> 1));
125
176
        new_cursor_pos = (GX_VALUE)(new_cursor_pos + text_input -> gx_single_line_text_input_xoffset);
126
176
        break;
127
128
309
    case GX_STYLE_TEXT_LEFT:
129
    default:
130
        /* Calculate new xoffset.  */
131
309
        _gx_system_string_width_get_ext(gx_font, &string, &text_width);
132
309
        new_xoffset = (GX_VALUE)(client.gx_rectangle_right - client.gx_rectangle_left + 1);
133
309
        new_xoffset = (GX_VALUE)(new_xoffset - 3);
134
309
        new_xoffset = (GX_VALUE)(text_width - new_xoffset);
135
136
309
        if (new_xoffset < 0)
137
        {
138
67
            new_xoffset = 0;
139
        }
140
141
        /* Calculate new cursor position. */
142
309
        new_cursor_pos = (GX_VALUE)(client.gx_rectangle_left + 1 - new_xoffset + text_width);
143
309
        break;
144
    }
145
146
661
    if (text_input -> gx_single_line_text_input_xoffset != new_xoffset)
147
    {
148
        /* We need to update text xoffset, mark whole dirty area dirty.  */
149
573
        _gx_system_dirty_mark((GX_WIDGET *)text_input);
150
    }
151
    else
152
    {
153
88
        if (start_mark != end_mark)
154
        {
155
14
            _gx_single_line_text_input_text_rectangle_get(text_input, (INT)(start_mark - end_mark), &client);
156
157
14
            _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
158
159
14
            mark_new_cursor_dirty = GX_TRUE;
160
        }
161
74
        else if (text_input -> gx_single_line_text_input_insert_pos != string.gx_string_length)
162
        {
163
164
            /* No need to update text xoffset, mark old and new dirty area */
165
51
            _gx_text_input_cursor_dirty_rectangle_get(cursor_ptr, &client);
166
51
            _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
167
168
51
            mark_new_cursor_dirty = GX_TRUE;
169
        }
170
    }
171
172
    /* Update the value of cursor position. */
173
661
    cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = new_cursor_pos;
174
175
    /* Update character insert position.  */
176
661
    text_input -> gx_single_line_text_input_insert_pos = string.gx_string_length;
177
178
    /* Update text inpu x offset.  */
179
661
    text_input -> gx_single_line_text_input_xoffset = new_xoffset;
180
181
661
    if (mark_new_cursor_dirty)
182
    {
183
        /* Mark new cursor area as dirty. */
184
65
        _gx_text_input_cursor_dirty_rectangle_get(cursor_ptr, &client);
185
65
        _gx_system_dirty_partial_add((GX_WIDGET *)text_input, &client);
186
    }
187
188
661
    return GX_SUCCESS;
189
}
190