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_single_line_text_input.h"
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _gx_single_line_text_input_position_update          PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Kenneth Maxwell, Microsoft Corporation                              */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function update the cursor position according to character     */
44 /*      insert index.                                                     */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    text_input                            Single-line text input widget */
49 /*                                            control block               */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    Complete status                                                     */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _gx_widget_font_get                   Get font by specified ID      */
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 /*                                                                        */
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_position_update(GX_SINGLE_LINE_TEXT_INPUT * text_input)76 UINT _gx_single_line_text_input_position_update(GX_SINGLE_LINE_TEXT_INPUT *text_input)
77 {
78 GX_TEXT_INPUT_CURSOR *cursor_ptr;
79 UINT                  insert_pos;
80 GX_FONT              *gx_font;
81 GX_RECTANGLE          client;
82 GX_VALUE              width;
83 GX_VALUE              x_pos;
84 GX_STRING             string;
85 
86     cursor_ptr = &text_input -> gx_single_line_text_input_cursor_instance;
87     insert_pos = text_input -> gx_single_line_text_input_insert_pos;
88 
89     _gx_widget_font_get((GX_WIDGET *)text_input, text_input -> gx_prompt_font_id, &gx_font);
90     _gx_widget_border_width_get((GX_WIDGET *)text_input, &width);
91     _gx_widget_client_get((GX_WIDGET *)text_input, width, &client);
92 
93     /* Calculate distance from first character to character insert position.  */
94     string.gx_string_ptr = text_input -> gx_single_line_text_input_buffer;
95     string.gx_string_length = insert_pos;
96     _gx_system_string_width_get_ext(gx_font, &string, &width);
97 
98     switch (text_input -> gx_widget_style & GX_STYLE_TEXT_ALIGNMENT_MASK)
99     {
100     case GX_STYLE_TEXT_RIGHT:
101         x_pos = (GX_VALUE)(client.gx_rectangle_right - 1);
102         break;
103 
104     case GX_STYLE_TEXT_CENTER:
105         x_pos = client.gx_rectangle_left;
106         x_pos = (GX_VALUE)(x_pos + 1 + ((client.gx_rectangle_right - client.gx_rectangle_left + 1) >> 1));
107         break;
108 
109     case GX_STYLE_TEXT_LEFT:
110     default:
111         x_pos = (GX_VALUE)(client.gx_rectangle_left + 1);
112         break;
113     }
114 
115     /* Pick up draw start position.  */
116     x_pos = (GX_VALUE)(x_pos - text_input -> gx_single_line_text_input_xoffset);
117 
118     /* Update cursor position.  */
119     cursor_ptr -> gx_text_input_cursor_pos.gx_point_x = (GX_VALUE)(x_pos + width);
120 
121     return GX_SUCCESS;
122 }
123 
124