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 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _gx_single_line_text_input_buffer_clear             PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Kenneth Maxwell, Microsoft Corporation                              */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This service deletes all characters from the text input buffer.     */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    text_input                            Single-line text input widget */
50 /*                                            control block               */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    status                                Completion status             */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _gx_widget_border_width_get           Get the widget border width   */
59 /*    _gx_widget_client_get                 Get the client rectangle      */
60 /*    _gx_system_dirty_mark                 Mark the widget area as dirty */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    Application Code                                                    */
65 /*    GUIX Internal Code                                                  */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
72 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*                                                                        */
75 /**************************************************************************/
_gx_single_line_text_input_buffer_clear(GX_SINGLE_LINE_TEXT_INPUT * text_input)76 UINT _gx_single_line_text_input_buffer_clear(GX_SINGLE_LINE_TEXT_INPUT *text_input)
77 {
78 GX_TEXT_INPUT_CURSOR *cursor_ptr;
79 GX_RECTANGLE          client;
80 GX_VALUE              client_width;
81 GX_VALUE              border_width;
82 
83     cursor_ptr = &text_input -> gx_single_line_text_input_cursor_instance;
84 
85     /* Calculate the widget width for showing text. */
86     _gx_widget_border_width_get((GX_WIDGET *)text_input, &border_width);
87     _gx_widget_client_get((GX_WIDGET *)text_input, border_width, &client);
88 
89     client_width = (GX_VALUE)(client.gx_rectangle_right - client.gx_rectangle_left + 1);
90 
91     /* Update the value of cursor position and xoffset. */
92     switch (text_input -> gx_widget_style & GX_STYLE_TEXT_ALIGNMENT_MASK)
93     {
94     case GX_STYLE_TEXT_RIGHT:
95         text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)(3 - client_width);
96         cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = (GX_VALUE)(client.gx_rectangle_right - 1);
97         break;
98 
99     case GX_STYLE_TEXT_CENTER:
100         text_input -> gx_single_line_text_input_xoffset = (client_width >> 1);
101         cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = (GX_VALUE)(client.gx_rectangle_left + (client_width >> 1) + 1);
102         break;
103 
104     case GX_STYLE_TEXT_LEFT:
105     default:
106         text_input -> gx_single_line_text_input_xoffset = 0;
107         cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = (GX_VALUE)(client.gx_rectangle_left + 1);
108         break;
109     }
110 
111     /* Reset the value of cursor position. */
112     text_input -> gx_single_line_text_input_buffer[0] = '\0';
113     text_input -> gx_single_line_text_input_string_size = 0;
114     text_input -> gx_single_line_text_input_insert_pos = 0;
115 
116     /* Mark the widget area dirty. */
117     _gx_system_dirty_mark((GX_WIDGET *)text_input);
118 
119     return GX_SUCCESS;
120 }
121 
122