1 /*
2  * Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org>
3  *
4  * This file is part of ZToolkit
5  *
6  * ZToolkit is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as
8  * published by the Free Software Foundation, either version 3 of the
9  * License, or (at your option) any later version.
10  *
11  * ZToolkit is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Affero Public License
17  * along with ZToolkit.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef __Z_TOOLKIT_ZTK_BUTTON_H__
21 #define __Z_TOOLKIT_ZTK_BUTTON_H__
22 
23 #include "rsvg.h"
24 #include "ztk_color.h"
25 #include "ztk_widget.h"
26 
27 typedef struct ZtkButton ZtkButton;
28 
29 /**
30  * Button type.
31  */
32 typedef enum ZtkButtonType
33 {
34   /** Button is a label. */
35   ZTK_BTN_LBL,
36 
37   /** Button has 3 SVGs, one for each state (normal,
38    * hover, press). */
39   ZTK_BTN_SVG,
40 
41   /** Button will be drawn using user callback. */
42   ZTK_BTN_CUSTOM,
43 } ZtkButtonType;
44 
45 /**
46  * Getter for whether the button is toggled or not.
47  */
48 typedef int (*ZtkButtonToggledGetter) (
49   ZtkButton * btn,
50   void *      data);
51 
52 /**
53  * Button widget.
54  */
55 typedef struct ZtkButton
56 {
57   /** Base widget. */
58   ZtkWidget         base;
59 
60   /** Label, if this is a label button. */
61   char *            lbl;
62 
63   /** If this button is a toggle or not. */
64   int               is_toggle;
65 
66   /** Getter to check if the button is toggled or not,
67    * if toggle button. */
68   ZtkButtonToggledGetter toggled_getter;
69 
70   /**
71    * Activate callback.
72    *
73    * This will be called when the button is pressed
74    * and released (on release).
75    */
76   ZtkWidgetActivateCallback activate_cb;
77 
78   /** Button type. */
79   ZtkButtonType     type;
80 
81 #ifdef HAVE_RSVG
82   /** Normal state SVG. */
83   ZtkRsvgHandle *   normal_svg;
84 
85   /** Hovered SVG. */
86   ZtkRsvgHandle *   hover_svg;
87 
88   /**
89    * Clicked SVG.
90    *
91    * This will also be used for when toggled.
92    */
93   ZtkRsvgHandle *   clicked_svg;
94 #endif
95 
96   /** Padding to add when using SVGs to control
97    * their size. */
98   int               hpadding;
99   int               vpadding;
100 
101   /** If this is 1, background colors will be drawn,
102    * regardless if there is a \ref
103    * ZtkButton.bg_draw_cb. */
104   int                     has_bg_colors;
105 
106   ZtkColor          normal_color;
107   ZtkColor          hovered_color;
108   ZtkColor          clicked_color;
109 
110   /** Set to 1 while pressed, and the activate
111    * callback will be fired when released. */
112   int                     was_pressed;
113 
114   /** Custom draw callback for backgrounds. */
115   ZtkWidgetDrawCallback  bg_draw_cb;
116 
117   /** Custom draw callback for completely custom
118    * buttons. */
119   ZtkWidgetDrawCallback  custom_draw_cb;
120 
121 } ZtkButton;
122 
123 /**
124  * Creates a new ZtkButton.
125  *
126  * This must then be set to its type using the setters
127  * below.
128  */
129 ZtkButton *
130 ztk_button_new (
131   ZtkRect *                 rect,
132   ZtkWidgetActivateCallback activate_cb,
133   void *                    data);
134 
135 void
136 ztk_button_make_toggled (
137   ZtkButton *            self,
138   ZtkButtonToggledGetter toggled_getter);
139 
140 /**
141  * Makes a button with a label.
142  */
143 void
144 ztk_button_make_labeled (
145   ZtkButton *           self,
146   const char *          label);
147 
148 #ifdef HAVE_RSVG
149 /**
150  * Makes a button with SVGs.
151  */
152 void
153 ztk_button_make_svged (
154   ZtkButton *     self,
155   int             hpadding,
156   int             vpadding,
157   ZtkRsvgHandle * svg_normal,
158   ZtkRsvgHandle * svg_hover,
159   ZtkRsvgHandle * svg_clicked);
160 #endif
161 
162 /**
163  * Makes a customly drawn button.
164  */
165 void
166 ztk_button_make_custom (
167   ZtkButton *           self,
168   ZtkWidgetDrawCallback draw_cb);
169 
170 void
171 ztk_button_set_background_colors (
172   ZtkButton * self,
173   ZtkColor *  normal,
174   ZtkColor *  hovered,
175   ZtkColor *  clicked);
176 
177 /**
178  * Add callback for drawing the background.
179  */
180 void
181 ztk_button_add_background_callback (
182   ZtkButton *           self,
183   ZtkWidgetDrawCallback draw_cb);
184 
185 #endif
186