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 /**   Button Management (Button)                                          */
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_canvas.h"
29 #include "gx_context.h"
30 #include "gx_widget.h"
31 #include "gx_button.h"
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _gx_radio_button_draw                               PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Kenneth Maxwell, Microsoft Corporation                              */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This service draws a radio button widget.                           */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    button                                Pointer to radio button       */
50 /*                                            widget control block        */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    None                                                                */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _gx_context_pixelmap_get              Retrieve pixelmap image       */
59 /*    _gx_canvas_pixelmap_draw              Draw the pixelmap             */
60 /*    _gx_widget_text_id_draw               Draw the text based on ID     */
61 /*    _gx_widget_text_draw                  Draw the text string          */
62 /*    _gx_widget_children_draw              Draw children widgets         */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Application Code                                                    */
67 /*    GUIX Internal Code                                                  */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
74 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
75 /*                                            improved logic,             */
76 /*                                            resulting in version 6.1    */
77 /*                                                                        */
78 /**************************************************************************/
_gx_radio_button_draw(GX_RADIO_BUTTON * button)79 VOID  _gx_radio_button_draw(GX_RADIO_BUTTON *button)
80 {
81 
82 GX_VALUE       xoffset = 0;
83 GX_VALUE       yoffset = 0;
84 GX_RESOURCE_ID text_color;
85 GX_RESOURCE_ID fill_color;
86 GX_RESOURCE_ID pixelmap_id;
87 GX_PIXELMAP   *pixelmap = GX_NULL;
88 GX_STRING      string;
89 
90     if (button -> gx_widget_style & GX_STYLE_ENABLED)
91     {
92 
93         if (button -> gx_widget_style & GX_STYLE_BUTTON_PUSHED)
94         {
95             fill_color = button -> gx_widget_selected_fill_color;
96             text_color = button -> gx_text_button_selected_text_color;
97             pixelmap_id = button -> gx_radio_button_on_pixelmap_id;
98         }
99         else
100         {
101             fill_color = button -> gx_widget_normal_fill_color;
102             text_color = button -> gx_text_button_normal_text_color;
103             pixelmap_id = button -> gx_radio_button_off_pixelmap_id;
104         }
105     }
106     else
107     {
108         fill_color = button -> gx_widget_disabled_fill_color;
109         text_color = button -> gx_text_button_disabled_text_color;
110 
111         if (button -> gx_widget_style & GX_STYLE_BUTTON_PUSHED)
112         {
113             pixelmap_id = button -> gx_radio_button_on_disabled_pixelmap_id;
114         }
115         else
116         {
117             pixelmap_id = button -> gx_radio_button_off_disabled_pixelmap_id;
118         }
119     }
120 
121     if (!(button -> gx_widget_status & GX_STATUS_TRANSPARENT))
122     {
123         _gx_widget_border_draw((GX_WIDGET *)button, GX_COLOR_ID_DEFAULT_BORDER, fill_color, fill_color, GX_TRUE);
124     }
125 
126     if (pixelmap_id)
127     {
128         _gx_context_pixelmap_get(pixelmap_id, &pixelmap);
129     }
130     if (pixelmap)
131     {
132         _gx_widget_height_get((GX_WIDGET *)button, &yoffset);
133         yoffset = (GX_VALUE)((yoffset - pixelmap -> gx_pixelmap_height) / 2);
134 
135         switch (button -> gx_widget_style & GX_STYLE_TEXT_ALIGNMENT_MASK)
136         {
137         case GX_STYLE_TEXT_RIGHT:
138             _gx_widget_width_get((GX_WIDGET *)button, &xoffset);
139             xoffset = (GX_VALUE)(xoffset - pixelmap -> gx_pixelmap_width + 1);
140             /* draw the pixelmap on the right */
141             _gx_canvas_pixelmap_draw((GX_VALUE)(button -> gx_widget_size.gx_rectangle_right -
142                                                 pixelmap -> gx_pixelmap_width + 1),
143                                      (GX_VALUE)(button -> gx_widget_size.gx_rectangle_top + yoffset), pixelmap);
144 
145             /* draw the text to the left of the bitmap */
146             xoffset = (GX_VALUE)(-(pixelmap -> gx_pixelmap_width * 3 / 2));
147             break;
148 
149         case GX_STYLE_TEXT_CENTER:
150             /* draw the pixelmap and text centered         */
151             _gx_widget_width_get((GX_WIDGET *)button, &xoffset);
152             xoffset = (GX_VALUE)((xoffset - pixelmap -> gx_pixelmap_width) / 2);
153 
154             /* draw the pixelmap centered */
155             _gx_canvas_pixelmap_draw((GX_VALUE)(button -> gx_widget_size.gx_rectangle_left + xoffset),
156                                      (GX_VALUE)(button -> gx_widget_size.gx_rectangle_top + yoffset), pixelmap);
157             /* draw the text centered: */
158             xoffset = 0;
159             break;
160 
161         case GX_STYLE_TEXT_LEFT:
162         default:
163             /* draw the pixelmap on the left */
164             _gx_canvas_pixelmap_draw(button -> gx_widget_size.gx_rectangle_left,
165                                      (GX_VALUE)(button -> gx_widget_size.gx_rectangle_top + yoffset), pixelmap);
166 
167             /* draw the text to the right of the pixelmap */
168             xoffset = (GX_VALUE)(pixelmap -> gx_pixelmap_width * 3 / 2);
169             break;
170         }
171     }
172 
173 
174     _gx_text_button_text_get_ext((GX_TEXT_BUTTON *)button, &string);
175 
176     if (string.gx_string_ptr)
177     {
178         _gx_widget_text_draw_ext((GX_WIDGET *)button, text_color,
179                                  button -> gx_text_button_font_id,
180                                  &string, xoffset, 0);
181     }
182 
183     /* Draw button's children.  */
184     _gx_widget_children_draw((GX_WIDGET *)button);
185 }
186 
187