changed author email
[guish.git] / src / widgets / button.c
blob76db45e731c37de7da883e824efa03b4f4b417ca
1 /*************************************************************************
2 * Copyright (C) 2024 Francesco Palumbo <phranz.dev@gmail.com>, Naples (Italy)
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 *************************************************************************/
18 #include "button.h"
19 #include "../dectypes.h"
21 extern GC gc;
22 extern Atom delatom;
23 extern Display* display;
24 extern Window root;
25 extern int screen;
26 extern map_strcol_t* colors;
28 extern map_widloci_t* widlines;
29 extern map_widloci_t* widareas;
30 extern map_widarcssets_t* widarcssets;
31 extern map_widarcssets_t* widarcsareas;
32 extern map_widloci_t* widloci;
33 extern map_widloci_t* widpixels;
34 extern map_widnamedpoints_t* widnamedpoints;
36 static void draw(widget_t* w) {
37 if (w->flags & F_LPRESSED || w->flags & F_RPRESSED || w->flags & F_MPRESSED) {
38 XSetWindowBackground(display, w->wid, w->pbg->pixel);
39 XSetForeground(display, gc, w->pfg->pixel);
40 } else if (w->flags & F_HOVERED && w->flags & F_APPLYHOVER) {
41 XSetWindowBackground(display, w->wid, w->hbg->pixel);
42 XSetForeground(display, gc, w->hfg->pixel);
43 #ifdef ENABLE_IMAGES
44 } else if (w->img) {
45 updateimg(w);
46 #endif
47 } else {
48 XSetWindowBackground(display, w->wid, w->bg->pixel);
49 XSetForeground(display, gc, w->fg->pixel);
51 XSetFont(display, gc, w->fs->fid);
52 XClearWindow(display, w->wid);
53 write_align(w, w->data, w->align, NULL);
54 if (widareas)
55 drawareas(w);
56 if (widarcsareas)
57 drawarcsareas(w);
58 if (widlines)
59 drawlines(w);
60 if (widarcssets)
61 drawarcs(w);
62 if (widloci)
63 drawpoints(w);
64 if (widpixels)
65 drawpixels(w);
66 if (widnamedpoints)
67 drawnamedpoints(w);
70 void button_t_free(button_t* p) {
71 ((widget_t*)p)->free((widget_t*)p);
72 free(p);
75 button_t* button_t_init(button_t* p, int ww, int hh) {
76 if (!p)
77 return NULL;
78 memset(p, 0, sizeof(button_t));
80 widget_t* w = (widget_t*)p;
81 widget_t_init(w);
83 p->free = button_t_free;
84 w->draw = draw;
86 w->w = ww ? ww : 120;
87 w->h = hh ? hh : 40;
88 w->b = 2;
90 colors->get(colors, "lightgray", &w->bg);
91 colors->get(colors, "black", &w->fg);
93 colors->get(colors, "gray", &w->pbg);
94 colors->get(colors, "black", &w->pfg);
96 colors->get(colors, "lightgray", &w->hbg);
97 colors->get(colors, "black", &w->hfg);
99 XSetWindowAttributes wa = {0};
100 w->wid = XCreateWindow(display, root, w->x, w->y, w->w, w->h, w->b, DefaultDepth(display, screen), InputOutput, DefaultVisual(display, screen), CWOverrideRedirect, &wa);
102 w->mask = VisibilityChangeMask|ExposureMask|ButtonPressMask|ButtonReleaseMask|StructureNotifyMask;
103 XSelectInput(display, w->wid, w->mask);
104 XSetWMProtocols(display, w->wid, &delatom, 1);
105 fixed(w, 0);
107 return p;