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_icon_button_draw                                PORTABLE C      */
39 /*                                                           6.1.9        */
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                                Icon button control block     */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    None                                                                */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _gx_button_background_draw            Draw the button background    */
60 /*    _gx_context_pixelmap_get              Retrieve pixelmap image       */
61 /*    _gx_widget_border_width_get           Get the border width          */
62 /*    _gx_canvas_pixelmap_draw              Draw the pixelmap             */
63 /*    _gx_widget_children_draw              Draw children widgets         */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    Application Code                                                    */
68 /*    GUIX Internal Code                                                  */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
75 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
76 /*                                            resulting in version 6.1    */
77 /*  10-15-2021     Ting Zhu                 Modified comment(s),          */
78 /*                                            fixed pixelmap draw offset, */
79 /*                                            resulting in version 6.1.9  */
80 /*                                                                        */
81 /**************************************************************************/
_gx_icon_button_draw(GX_ICON_BUTTON * button)82 VOID  _gx_icon_button_draw(GX_ICON_BUTTON *button)
83 {
84 GX_WIDGET   *widget;
85 GX_PIXELMAP *map;
86 GX_VALUE     xpos;
87 GX_VALUE     ypos;
88 GX_VALUE     border;
89 INT          shift = 0;
90 
91     /* Setup the widget.  */
92     widget =  (GX_WIDGET *)button;
93 
94     /* draw the background button */
95     _gx_button_background_draw((GX_BUTTON *)button);
96 
97     /* draw the button icon */
98     _gx_context_pixelmap_get(button -> gx_icon_button_icon_id, &map);
99 
100     if (map)
101     {
102         _gx_widget_border_width_get(widget, &border);
103 
104         if (widget -> gx_widget_style & GX_STYLE_BUTTON_PUSHED)
105         {
106             shift = 1;
107         }
108 
109         switch (widget -> gx_widget_style & GX_PIXELMAP_HALIGN_MASK)
110         {
111         case GX_STYLE_HALIGN_CENTER:
112             xpos = (GX_VALUE)(widget -> gx_widget_size.gx_rectangle_right -
113                               widget -> gx_widget_size.gx_rectangle_left -
114                               map -> gx_pixelmap_width + 1);
115 
116             xpos /= 2;
117             xpos = (GX_VALUE)(xpos + widget -> gx_widget_size.gx_rectangle_left + shift);
118             break;
119 
120         case GX_STYLE_HALIGN_RIGHT:
121             xpos = (GX_VALUE)(widget -> gx_widget_size.gx_rectangle_right - map -> gx_pixelmap_width - border + shift + 1);
122             break;
123 
124         default:
125             xpos = (GX_VALUE)(widget -> gx_widget_size.gx_rectangle_left + border + shift);
126             break;
127         }
128 
129         switch (widget -> gx_widget_style & GX_PIXELMAP_VALIGN_MASK)
130         {
131         case GX_STYLE_VALIGN_CENTER:
132             ypos = (GX_VALUE)(widget -> gx_widget_size.gx_rectangle_bottom -
133                               widget -> gx_widget_size.gx_rectangle_top -
134                               map -> gx_pixelmap_height + 1);
135 
136             ypos /= 2;
137             ypos = (GX_VALUE)(ypos + widget -> gx_widget_size.gx_rectangle_top + shift);
138             break;
139 
140         case GX_STYLE_VALIGN_BOTTOM:
141             ypos = (GX_VALUE)(widget -> gx_widget_size.gx_rectangle_bottom - map -> gx_pixelmap_height - border + shift + 1);
142             break;
143 
144         default:
145             ypos = (GX_VALUE)(widget -> gx_widget_size.gx_rectangle_top + border + shift);
146             break;
147         }
148         _gx_widget_context_fill_set(widget);
149         _gx_canvas_pixelmap_draw(xpos, ypos, map);
150     }
151 
152 
153     /* Draw button's children.  */
154     _gx_widget_children_draw(widget);
155 }
156 
157