GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: gx_single_line_text_input_draw_position_get.c Lines: 62 62 100.0 %
Date: 2024-12-05 08:52:37 Branches: 29 29 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
32
/**************************************************************************/
33
/*                                                                        */
34
/*  FUNCTION                                               RELEASE        */
35
/*                                                                        */
36
/*    _gx_single_line_text_input_draw_position_get        PORTABLE C      */
37
/*                                                           6.1          */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Kenneth Maxwell, Microsoft Corporation                              */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This service calculates draw start position of text input text.     */
45
/*                                                                        */
46
/*  INPUT                                                                 */
47
/*                                                                        */
48
/*    text_input                            Single-line text input widget */
49
/*                                            control block.              */
50
/*                                                                        */
51
/*  OUTPUT                                                                */
52
/*                                                                        */
53
/*    None                                                                */
54
/*                                                                        */
55
/*  CALLS                                                                 */
56
/*                                                                        */
57
/*    _gx_context_brush_get                 Get brush of current context  */
58
/*    _gx_widget_border_width_get           Get widget border width       */
59
/*    _gx_widget_client_get                 Get widget client rectangle   */
60
/*    _gx_system_string_width_get           Get the width of a string     */
61
/*    _gx_single_line_text_input_position_update                          */
62
/*                                          Update cursor position by     */
63
/*                                            insert index                */
64
/*                                                                        */
65
/*                                                                        */
66
/*  CALLED BY                                                             */
67
/*                                                                        */
68
/*    _gx_single_line_text_input_draw                                     */
69
/*                                                                        */
70
/*  RELEASE HISTORY                                                       */
71
/*                                                                        */
72
/*    DATE              NAME                      DESCRIPTION             */
73
/*                                                                        */
74
/*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
75
/*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
76
/*                                            resulting in version 6.1    */
77
/*                                                                        */
78
/**************************************************************************/
79
50741
UINT _gx_single_line_text_input_draw_position_get(GX_SINGLE_LINE_TEXT_INPUT *text_input, GX_VALUE *xpos, GX_VALUE *ypos)
80
{
81
GX_VALUE     border_width;
82
UINT         text_height;
83
GX_VALUE     text_width;
84
GX_VALUE     client_height;
85
GX_VALUE     client_width;
86
GX_VALUE     x_pos;
87
GX_VALUE     y_pos;
88
GX_RECTANGLE client;
89
GX_BRUSH    *brush;
90
GX_BOOL      update_cursor_pos;
91
GX_STRING    string;
92
93
50741
    _gx_context_brush_get(&brush);
94
95
50741
    if (brush -> gx_brush_font == GX_NULL)
96
    {
97
7
        return(GX_FAILURE);
98
    }
99
100
50734
    if (text_input -> gx_widget_style & GX_STYLE_CURSOR_ALWAYS)
101
    {
102
11905
        update_cursor_pos = GX_TRUE;
103
    }
104
    else
105
    {
106
38829
        update_cursor_pos = GX_FALSE;
107
    }
108
109
    /* Pickup widget width. */
110
50734
    _gx_widget_border_width_get((GX_WIDGET *)text_input, &border_width);
111
112
50734
    _gx_widget_client_get((GX_WIDGET *)text_input, border_width, &client);
113
114
50734
    client_width = (GX_VALUE)(client.gx_rectangle_right - client.gx_rectangle_left + 1);
115
50734
    client_height = (GX_VALUE)(client.gx_rectangle_bottom - client.gx_rectangle_top + 1);
116
117
118
50734
    text_height = brush -> gx_brush_font -> gx_font_line_height;
119
50734
    string.gx_string_ptr = text_input -> gx_single_line_text_input_buffer;
120
50734
    string.gx_string_length = text_input -> gx_single_line_text_input_string_size;
121
50734
    _gx_system_string_width_get_ext(brush -> gx_brush_font, &string, &text_width);
122
123
50734
    switch (text_input -> gx_widget_style & GX_STYLE_TEXT_ALIGNMENT_MASK)
124
    {
125
3931
    case GX_STYLE_TEXT_RIGHT:
126
3931
        x_pos = (GX_VALUE)(client.gx_rectangle_right - 1);
127
128
3931
        x_pos = (GX_VALUE)(x_pos - text_input -> gx_single_line_text_input_xoffset);
129
130
3931
        if (text_width < (client_width - 3))
131
        {
132
            /* Text width is shorter than client width. */
133
689
            if (text_input -> gx_single_line_text_input_xoffset != text_width)
134
            {
135
                /* If xoffset is not equal to text width,
136
                   reset xoffset and update cursor position. */
137
84
                text_input -> gx_single_line_text_input_xoffset = text_width;
138
84
                update_cursor_pos = GX_TRUE;
139
            }
140
        }
141
3242
        else if (x_pos > client.gx_rectangle_left + 1)
142
        {
143
            /* If text start/end position is inside client area,
144
               reset xoffset value and update cursor position. */
145
157
            text_input -> gx_single_line_text_input_xoffset = text_width;
146
157
            update_cursor_pos = GX_TRUE;
147
        }
148
149
3931
        x_pos = (GX_VALUE)(client.gx_rectangle_right - 1);
150
3931
        break;
151
152
6492
    case GX_STYLE_TEXT_CENTER:
153
6492
        x_pos = (GX_VALUE)(client.gx_rectangle_left + 1 + (client_width >> 1));
154
6492
        x_pos = (GX_VALUE)(x_pos - text_input -> gx_single_line_text_input_xoffset);
155
156
6492
        if (text_width < (client_width - 3))
157
        {
158
            /* Text width is shorter than client width. */
159
1066
            if (text_input -> gx_single_line_text_input_xoffset != ((text_width + 1) >> 1))
160
            {
161
                /* If xoffset is not equal to 0,
162
                   reset xoffset value and update cursor position. */
163
67
                text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)((text_width + 1) >> 1);
164
67
                update_cursor_pos = GX_TRUE;
165
            }
166
        }
167
5426
        else if ((x_pos > client.gx_rectangle_left + 1) ||
168
5108
                 (x_pos + text_width < client.gx_rectangle_right - 1))
169
        {
170
            /* If text start/end position is inside client area,
171
               reset xoffset value and update cursor position. */
172
319
            text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)((text_width + 1) >> 1);
173
319
            update_cursor_pos = GX_TRUE;
174
        }
175
6492
        x_pos = (GX_VALUE)(client.gx_rectangle_left + 1 + (client_width >> 1));
176
6492
        break;
177
178
40311
    case GX_STYLE_TEXT_LEFT:
179
    default:
180
40311
        x_pos = (GX_VALUE)(client.gx_rectangle_left + 1);
181
182
40311
        x_pos = (GX_VALUE)(x_pos - text_input -> gx_single_line_text_input_xoffset);
183
184
40311
        if (text_width < (client_width - 3))
185
        {
186
            /* Text width is shorter than client width. */
187
33486
            if (text_input -> gx_single_line_text_input_xoffset != 0)
188
            {
189
                /* If xoffset is not equal to 0,
190
                   reset xoffset value and update cursor position. */
191
2
                text_input -> gx_single_line_text_input_xoffset = 0;
192
2
                update_cursor_pos = GX_TRUE;
193
            }
194
        }
195
6825
        else if (x_pos + text_width < client.gx_rectangle_right - 1)
196
        {
197
            /* If text start/end position is inside client area,
198
               reset xoffset value and update cursor position. */
199
1
            text_input -> gx_single_line_text_input_xoffset = 0;
200
1
            update_cursor_pos = GX_TRUE;
201
        }
202
203
40311
        x_pos = (GX_VALUE)(client.gx_rectangle_left + 1);
204
40311
        break;
205
    }
206
207
50734
    if (update_cursor_pos)
208
    {
209
12160
        _gx_single_line_text_input_position_update(text_input);
210
    }
211
212
50734
    x_pos = (GX_VALUE)(x_pos - text_input -> gx_single_line_text_input_xoffset);
213
214
50734
    y_pos = (GX_VALUE)(text_input -> gx_widget_size.gx_rectangle_top + border_width);
215
50734
    y_pos = (GX_VALUE)(y_pos - text_input -> gx_single_line_text_input_yoffset);
216
50734
    y_pos = (GX_VALUE)(y_pos + (GX_VALUE)(((INT)client_height - (INT)text_height) >> 1));
217
218
50734
    *xpos = x_pos;
219
50734
    *ypos = y_pos;
220
221
50734
    return GX_SUCCESS;
222
}
223