Fixed ZDE build - missing header file
[ZeXOS.git] / apps / wm / button.c
blob24626300d578a4abe9c12917c0fa30fa3a620547
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <libx/base.h>
25 #include <libx/object.h>
26 #include <libx/cursor.h>
27 #include <libx/text.h>
28 #include "button.h"
29 #include "cursor.h"
31 wmbutton wmbutton_list;
33 extern wmcursor *cursor;
35 wmbutton *button_create (unsigned x, unsigned y, unsigned size_x, unsigned size_y, unsigned char flags, const char *caption)
37 wmbutton *button = (wmbutton *) malloc (sizeof (wmbutton));
39 if (!button)
40 return 0;
42 button->x = x;
43 button->y = y;
44 button->flags = flags;
46 unsigned l = strlen (caption);
47 if (l) {
48 button->caption = (char *) malloc (sizeof (char) * l + 1);
49 memcpy (button->caption, caption, l);
50 button->caption[l] = '\0';
51 } else
52 button->caption = 0;
54 if (size_x < (l*6+4))
55 button->size_x = l*6+4;
56 else
57 button->size_x = size_x;
59 if (size_y < 12)
60 button->size_y = 12;
61 else
62 button->size_y = size_y;
64 /* add into list */
65 button->next = &wmbutton_list;
66 button->prev = wmbutton_list.prev;
67 button->prev->next = button;
68 button->next->prev = button;
70 return button;
73 unsigned button_sethandler (wmbutton *button, wmbutton_handler *handler)
75 if (!button)
76 return 0;
78 if (!handler)
79 button->handler = 0;
80 else {
81 button->handler = handler;
82 return 1;
85 return 0;
88 unsigned char button_flags (wmbutton *button)
90 if (!button)
91 return 0;
93 return button->flags;
96 unsigned button_draw (wmbutton *button)
98 if (button->flags & BUTTON_FLAG_CLICKED) {
99 xrect (button->x, button->y, button->x+button->size_x, button->y+button->size_y, 35*256);
100 xrect (button->x+1, button->y+1, button->x+button->size_x-1, button->y+button->size_y-1, 0*256);
101 xrectfill (button->x+2, button->y+2, button->x+button->size_x-2, button->y+button->size_y-2, 0x0084FF);
103 if (button->caption)
104 xtext_puts (button->x+3, button->y+3, 1*256, button->caption);
106 /* FIXME: general protection fault ! Something with pointer, fault is on OS side, 'cause syntax is correctly, and works on other systems .. */
107 /*if (button->handler)
108 button->handler ();*/ // call handler
109 } else {
110 xrect (button->x, button->y, button->x+button->size_x, button->y+button->size_y, 35*256);
111 xrect (button->x+1, button->y+1, button->x+button->size_x-1, button->y+button->size_y-1, 3*256);
112 xrectfill (button->x+2, button->y+2, button->x+button->size_x-2, button->y+button->size_y-2, 0x0075E2);
114 if (button->caption)
115 xtext_puts (button->x+2, button->y+2, 0, button->caption);
118 return 1;
121 unsigned button_draw_all ()
123 wmbutton *button;
124 for (button = wmbutton_list.next; button != &wmbutton_list; button = button->next) {
125 if (!button)
126 continue;
128 if (cursor->action)
129 if (cursor->state != XCURSOR_STATE_LBUTTON) {
130 if (button->flags & BUTTON_FLAG_CLICKED) {
131 button->flags &= ~BUTTON_FLAG_CLICKED;
132 cursor->action = 0;
136 /* mame mys na tlacitku ? */
137 if (cursor->x > (signed) button->x && cursor->x < (signed) button->x+(signed) button->size_x &&
138 cursor->y > (signed) button->y && cursor->y <= (signed) button->y+(signed) button->size_y) {
140 if (!cursor->action)
141 if (cursor->state == XCURSOR_STATE_LBUTTON) {
142 if (!(button->flags & BUTTON_FLAG_CLICKED)) {
143 button->flags |= BUTTON_FLAG_CLICKED;
144 cursor->action = 1;
149 button_draw (button);
152 return 1;
155 unsigned init_button ()
157 wmbutton_list.next = &wmbutton_list;
158 wmbutton_list.prev = &wmbutton_list;
160 return 1;