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_system.h"
28 #include "gx_widget.h"
29 #include "gx_prompt.h"
30 #include "gx_single_line_text_input.h"
31 #include "gx_utility.h"
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _gx_single_line_text_input_create                   PORTABLE C      */
38 /*                                                           6.1.3        */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Kenneth Maxwell, Microsoft Corporation                              */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This service creates a single-line text input widget.               */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    text_input                        Single-line text input widget     */
50 /*                                        control block                   */
51 /*    name                              Name of text input widget         */
52 /*    parent                            Pointer to parent widget          */
53 /*    input_buffer                      Pointer to text input buffer      */
54 /*    buffer_size                       Size of text input buffer         */
55 /*    style                             Style of text input widget.       */
56 /*    text_input_id                     Application-defined ID for text   */
57 /*                                        input                           */
58 /*    size                              Dimensions of text input widget   */
59 /*                                                                        */
60 /*  OUTPUT                                                                */
61 /*                                                                        */
62 /*    status                            Completion status                 */
63 /*                                                                        */
64 /*  CALLS                                                                 */
65 /*                                                                        */
66 /*    _gx_prompt_create                 Create a prompt                   */
67 /*    _gx_widget_border_width_get       Get widget border width           */
68 /*    _gx_widget_client_get             Get widget client rectangle       */
69 /*    _gx_widget_link                   Link the widget to its parent     */
70 /*    _gx_utility_string_length_check   Test string length                */
71 /*                                                                        */
72 /*  CALLED BY                                                             */
73 /*                                                                        */
74 /*    Application Code                                                    */
75 /*                                                                        */
76 /*  RELEASE HISTORY                                                       */
77 /*                                                                        */
78 /*    DATE              NAME                      DESCRIPTION             */
79 /*                                                                        */
80 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
81 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
82 /*                                            resulting in version 6.1    */
83 /*  12-31-2020     Kenneth Maxwell          Modified comment(s),          */
84 /*                                            supported dynamic buffer,   */
85 /*                                            resulting in version 6.1.3  */
86 /*                                                                        */
87 /**************************************************************************/
_gx_single_line_text_input_create(GX_SINGLE_LINE_TEXT_INPUT * text_input,GX_CONST GX_CHAR * name,GX_WIDGET * parent,GX_CHAR * input_buffer,UINT buffer_size,UINT style,USHORT text_input_id,GX_CONST GX_RECTANGLE * size)88 UINT  _gx_single_line_text_input_create(GX_SINGLE_LINE_TEXT_INPUT *text_input,
89                                         GX_CONST GX_CHAR *name,
90                                         GX_WIDGET *parent,
91                                         GX_CHAR *input_buffer,
92                                         UINT buffer_size,
93                                         UINT style,
94                                         USHORT text_input_id,
95                                         GX_CONST GX_RECTANGLE *size)
96 {
97 GX_TEXT_INPUT_CURSOR *cursor_ptr;
98 GX_VALUE              width;
99 GX_RECTANGLE          client;
100 
101     /* Don't allow TEXT_COPY style, the text input widget always uses a dynamic buffer */
102     style &= (ULONG) ~GX_STYLE_TEXT_COPY;
103 
104     /* Call the prompt widget create function.  */
105     _gx_prompt_create((GX_PROMPT *)text_input, name, GX_NULL, 0, style, text_input_id, size);
106 
107 
108     if ((!input_buffer) && buffer_size)
109     {
110         if (!_gx_system_memory_allocator)
111         {
112             return GX_SYSTEM_MEMORY_ERROR;
113         }
114 
115         input_buffer = _gx_system_memory_allocator(buffer_size);
116 
117         if (!input_buffer)
118         {
119             return GX_SYSTEM_MEMORY_ERROR;
120         }
121 
122         text_input -> gx_widget_status |= GX_STATUS_DYNAMIC_BUFFER;
123     }
124 
125     cursor_ptr = &text_input -> gx_single_line_text_input_cursor_instance;
126 
127     /* Finish the rest of the single line text input widget creation process. */
128     text_input -> gx_widget_type = GX_TYPE_SINGLE_LINE_TEXT_INPUT;
129     text_input -> gx_prompt_font_id = GX_FONT_ID_TEXT_INPUT;
130     text_input -> gx_prompt_normal_text_color = GX_COLOR_ID_TEXT_INPUT_TEXT;
131     text_input -> gx_widget_normal_fill_color = GX_COLOR_ID_TEXT_INPUT_FILL;
132     text_input -> gx_single_line_text_input_readonly_text_color = GX_COLOR_ID_READONLY_TEXT;
133     text_input -> gx_single_line_text_input_readonly_fill_color = GX_COLOR_ID_READONLY_FILL;
134     text_input -> gx_widget_draw_function = (VOID (*)(GX_WIDGET *))_gx_single_line_text_input_draw;
135     text_input -> gx_widget_event_process_function = (UINT (*)(GX_WIDGET *, GX_EVENT *))_gx_single_line_text_input_event_process;
136     text_input -> gx_single_line_text_input_buffer = input_buffer;
137     text_input -> gx_single_line_text_input_buffer_size = buffer_size;
138     text_input -> gx_widget_style |= GX_STYLE_CURSOR_BLINK;
139     text_input -> gx_single_line_text_input_was_modified = GX_FALSE;
140     text_input -> gx_single_line_text_input_yoffset = 0;
141     text_input -> gx_widget_selected_fill_color = GX_COLOR_ID_SELECTED_FILL;
142     text_input -> gx_prompt_selected_text_color = GX_COLOR_ID_SELECTED_TEXT;
143     text_input -> gx_single_line_text_input_start_mark = 0;
144     text_input -> gx_single_line_text_input_end_mark = 0;
145 
146     cursor_ptr -> gx_text_input_cursor_blink_interval = GX_CURSOR_BLINK_INTERVAL;
147     cursor_ptr -> gx_text_input_cursor_height = 0;
148     cursor_ptr -> gx_text_input_cursor_width = 1;
149     cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = 0;
150     cursor_ptr -> gx_text_input_cursor_pos.gx_point_y = 0;
151     cursor_ptr -> gx_text_input_cursor_flags = 0;
152 
153     /* Pickup widget width. */
154     _gx_widget_border_width_get((GX_WIDGET *)text_input, &width);
155 
156     /* Pickup client rectangle. */
157     _gx_widget_client_get((GX_WIDGET *)text_input, width, &client);
158 
159     /* Calculate client width.  */
160     width = (GX_VALUE)(client.gx_rectangle_right - client.gx_rectangle_left + 1);
161 
162     switch (text_input -> gx_widget_style & GX_STYLE_TEXT_ALIGNMENT_MASK)
163     {
164     case GX_STYLE_TEXT_RIGHT:
165         /* Right aliagnment. */
166         text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)(3 - width);
167         break;
168 
169     case GX_STYLE_TEXT_CENTER:
170         /* Center aligned. */
171         text_input -> gx_single_line_text_input_xoffset = (GX_VALUE)(width >> 1);
172         break;
173 
174     case GX_STYLE_TEXT_LEFT:
175     default:
176         /* Default: Left aliagnment. */
177         text_input -> gx_single_line_text_input_xoffset = 0;
178         break;
179     }
180 
181     if (buffer_size)
182     {
183         if (_gx_utility_string_length_check(input_buffer, &text_input -> gx_single_line_text_input_string_size, buffer_size - 1) != GX_SUCCESS)
184         {
185             text_input -> gx_single_line_text_input_string_size = 0;
186         }
187     }
188     else
189     {
190         text_input -> gx_single_line_text_input_string_size = 0;
191     }
192 
193     text_input -> gx_single_line_text_input_insert_pos = text_input -> gx_single_line_text_input_string_size;
194 
195     if (text_input -> gx_widget_style & GX_STYLE_CURSOR_ALWAYS)
196     {
197         text_input -> gx_widget_status |= (GX_STATUS_CURSOR_SHOW | GX_STATUS_CURSOR_DRAW);
198 
199         /* this widget wants to be notified if it is moved or re-sized */
200         text_input -> gx_widget_status |= GX_STATUS_RESIZE_NOTIFY;
201     }
202 
203     /* Determine if a parent widget was provided.  */
204     if (parent)
205     {
206         _gx_widget_link(parent, (GX_WIDGET *)text_input);
207     }
208 
209     /* Return the complete status */
210     return(GX_SUCCESS);
211 }
212 
213