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 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _gx_pixelmap_button_draw                            PORTABLE C      */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    Kenneth Maxwell, Microsoft Corporation                              */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function draws the specified button, which is a special type   */
47 /*    of widget.                                                          */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    button                                Button control block          */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    None                                                                */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _gx_context_pixelmap_get              Retrieve pixelmap image       */
60 /*    _gx_canvas_pixelmap_draw              Draw the pixelmap             */
61 /*    _gx_button_draw                       Draw the button               */
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 /*                                            resulting in version 6.1    */
76 /*                                                                        */
77 /**************************************************************************/
_gx_pixelmap_button_draw(GX_PIXELMAP_BUTTON * button)78 VOID  _gx_pixelmap_button_draw(GX_PIXELMAP_BUTTON *button)
79 {
80 GX_RESOURCE_ID pixelmap;
81 GX_PIXELMAP   *map;
82 GX_WIDGET     *widget = (GX_WIDGET *)button;
83 INT            xpos;
84 INT            ypos;
85 
86 
87     /* default to using the normal pixlemap */
88     pixelmap = button -> gx_pixelmap_button_normal_id;
89 
90     /* If the button is not enabled and the user provided
91        a disabled pixelmap, use that */
92     if (button -> gx_pixelmap_button_disabled_id &&
93         !(button -> gx_widget_style & GX_STYLE_ENABLED))
94     {
95         pixelmap = button -> gx_pixelmap_button_disabled_id;
96     }
97     else
98     {
99         /* otherwise, if the button is pushed used the selected pixelmap */
100         if (button -> gx_widget_style & GX_STYLE_BUTTON_PUSHED)
101         {
102             if (button -> gx_pixelmap_button_selected_id)
103             {
104                 pixelmap = button -> gx_pixelmap_button_selected_id;
105             }
106         }
107     }
108 
109     if (!(button -> gx_widget_style & GX_STYLE_ENABLED))
110     {
111         if (button -> gx_pixelmap_button_disabled_id)
112         {
113             pixelmap = button -> gx_pixelmap_button_disabled_id;
114         }
115     }
116 
117     _gx_context_pixelmap_get(pixelmap, &map);
118 
119     if (map)
120     {
121         switch (button -> gx_widget_style & GX_PIXELMAP_HALIGN_MASK)
122         {
123         case GX_STYLE_HALIGN_CENTER:
124             xpos = button -> gx_widget_size.gx_rectangle_right -
125                 button -> gx_widget_size.gx_rectangle_left -
126                 map -> gx_pixelmap_width + 1;
127 
128             xpos /= 2;
129             xpos += button -> gx_widget_size.gx_rectangle_left;
130             break;
131 
132         case GX_STYLE_HALIGN_RIGHT:
133             xpos = button -> gx_widget_size.gx_rectangle_right - map -> gx_pixelmap_width + 1;
134             break;
135 
136         default:
137             xpos = button -> gx_widget_size.gx_rectangle_left;
138             break;
139         }
140 
141         switch (button -> gx_widget_style & GX_PIXELMAP_VALIGN_MASK)
142         {
143         case GX_STYLE_VALIGN_CENTER:
144             ypos = button -> gx_widget_size.gx_rectangle_bottom -
145                 button -> gx_widget_size.gx_rectangle_top -
146                 map -> gx_pixelmap_height + 1;
147 
148             ypos /= 2;
149             ypos += button -> gx_widget_size.gx_rectangle_top;
150             break;
151 
152         case GX_STYLE_VALIGN_BOTTOM:
153             ypos = button -> gx_widget_size.gx_rectangle_bottom - map -> gx_pixelmap_height + 1;
154             break;
155 
156         default:
157             ypos = button -> gx_widget_size.gx_rectangle_top;
158             break;
159         }
160         _gx_widget_context_fill_set(widget);
161         _gx_canvas_pixelmap_draw((GX_VALUE)xpos, (GX_VALUE)ypos, map);
162     }
163     else
164     {
165         /* If I don't have a pixelmap and not transparent,
166            draw something
167          */
168         if (!(widget -> gx_widget_style & GX_STYLE_TRANSPARENT))
169         {
170             _gx_button_draw((GX_BUTTON *)button);
171         }
172     }
173 
174     /* Draw button's children.  */
175     _gx_widget_children_draw(widget);
176 }
177 
178