ZDE improvements - global zbitmap_t structure added, added native support for buttons...
[ZeXOS.git] / apps / zde / button.c
blob5e768a47de0453db5fb4d19aeeeadbdbbe9d2517
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program 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 General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <pthread.h>
26 #include <libx/base.h>
27 #include <libx/object.h>
28 #include <libx/image.h>
29 #include <libx/cursor.h>
30 #include <libx/text.h>
32 #include "bitmap.h"
33 #include "button.h"
34 #include "cursor.h"
35 #include "window.h"
36 #include "handler.h"
38 static zbitmap_t bitmap_button;
40 zde_btn_t zde_btn_list;
42 extern zde_cursor_t *zde_cursor;
43 extern unsigned short image_button[];
45 #ifdef TEST
46 zde_btn_t *create_button (char *name, int type, void *(*entry) (void *), void *arg)
48 zde_btn_t *button = (zde_btn_t *) malloc (sizeof (zde_btn_t));
50 if (!button)
51 return 0;
53 if (type == 0) {
54 button->arg = arg;
56 button->bitmap = image_button;
57 } else
58 button->bitmap = 0;
60 button->name = strdup (name);
61 button->entry = entry;
63 button->object = 0;
65 /* add into list */
66 button->next = &zde_btn_list;
67 button->prev = zde_btn_list.prev;
69 button->x = 14 + (button->prev->x * 40);
70 button->y = 14;
72 button->prev->next = button;
73 button->next->prev = button;
76 return button;
78 #endif
80 zde_btn_t *create_button_window (char *name, int type, void *(*entry) (void *), void *arg, zde_win_t *window)
82 if (!window)
83 return 0;
85 zde_btn_t *button = (zde_btn_t *) malloc (sizeof (zde_btn_t));
87 if (!button)
88 return 0;
90 button->x = 0 + (window->objectid * 40);
91 button->y = 0;
93 window->objectid ++;
95 unsigned short name_len = strlen (name);
97 if (type == 0) {
98 unsigned arg_len = strlen ((char *) arg);
100 button->arg = malloc (arg_len + 1);
102 if (!button->arg)
103 return 0;
105 char *s = button->arg;
107 memcpy (s, arg, arg_len);
108 //memcpy (s+arg_len, "/", 1);
109 //memcpy (s+arg_len+1, name, name_len);
110 s[arg_len] = '\0';
112 button->bitmap = (unsigned short *) image_button;
113 } else
114 button->bitmap = 0;
116 button->name_len = name_len;
117 button->name = strdup (name);
118 button->entry = entry;
119 button->object = (void *) window;
121 /* add into list */
122 button->next = &zde_btn_list;
123 button->prev = zde_btn_list.prev;
124 button->prev->next = button;
125 button->next->prev = button;
127 return button;
130 unsigned destroy_button_window (zde_win_t *window)
132 unsigned i;
134 for (;;) {
135 i = 0;
137 zde_btn_t *button;
138 for (button = zde_btn_list.next; button != &zde_btn_list; button = button->next) {
139 if (button->object == (void *) window) {
140 button->next->prev = button->prev;
141 button->prev->next = button->next;
143 free (button);
145 i = 1;
146 break;
150 if (!i)
151 break;
154 return 1;
157 static void draw_bitmapspec (zbitmap_t *bitmap)
159 unsigned i = 0;
160 unsigned j = 0;
161 unsigned k = 0;
162 unsigned pix = 0;
164 if (!bitmap)
165 return;
167 if (!bitmap->data)
168 return;
170 unsigned short w = bitmap->data[299+34];
172 for (i = bitmap->posx+(bitmap->posy*13); i < 13*25; i ++) {
173 if (k >= 14) {
174 j ++;
175 k = 0;
178 pix = 355-i;
180 //if (pix < 355)
181 // continue;
183 if (bitmap->data[pix] != w)
184 if (k < bitmap->sizex && j < bitmap->sizey)
185 xpixel ((unsigned) bitmap->x+k, (unsigned) bitmap->y+j, image_button[pix]);
187 k ++;
191 int draw_button_window (zde_win_t *window)
193 zde_btn_t *button;
194 for (button = zde_btn_list.next; button != &zde_btn_list; button = button->next) {
195 if (button->object != (void *) window)
196 continue;
198 /* we click on left button */
199 if (zde_cursor->state == XCURSOR_STATE_LBUTTON)
200 if (!(zde_cursor->flags & CURSOR_FLAG_LBCLICK))
201 if (!(zde_cursor->flags & CURSOR_FLAG_MOVE)) {
202 if (window->x+4+button->x <= zde_cursor->x && window->x+button->x+12+((short) button->name_len*6) >= zde_cursor->x &&
203 window->y+button->y+22 <= zde_cursor->y && window->y+button->y+43 >= zde_cursor->y) {
204 zde_cursor->state = 0;
205 zde_cursor->flags |= CURSOR_FLAG_LBCLICK;
207 pthread_t thread;
208 pthread_create (&thread, NULL, button->entry, button->arg);
212 /* 32x32 bitmap */
213 if (button->bitmap) {
214 unsigned len = button->name_len;
216 bitmap_button.data = button->bitmap;
217 bitmap_button.x = window->x+4+button->x;
218 bitmap_button.y = window->y+21+button->y;
219 bitmap_button.posx = 0;
220 bitmap_button.posy = 0;
221 bitmap_button.sizex = 4;
222 bitmap_button.sizey = 23;
224 draw_bitmapspec (&bitmap_button);
226 unsigned l;
227 for (l = 0; l < len; l ++) {
228 bitmap_button.x = window->x+8+button->x+(l*6);
229 bitmap_button.y = window->y+21+button->y;
230 bitmap_button.posx = 4;
231 bitmap_button.posy = 0;
232 bitmap_button.sizex = 6;
233 bitmap_button.sizey = 23;
235 draw_bitmapspec (&bitmap_button);
238 bitmap_button.x = window->x+8+button->x+(len*6);
239 bitmap_button.y = window->y+21+button->y;
240 bitmap_button.posx = 9;
241 bitmap_button.posy = 0;
242 bitmap_button.sizex = 4;
243 bitmap_button.sizey = 23;
245 draw_bitmapspec (&bitmap_button);
246 } else
247 xrectfill (button->x+window->x, button->y+window->y, button->x+32+window->x, button->y+32+window->y, ~0);
249 if (button->name)
250 xtext_puts (button->x+8+window->x, button->y+28+window->y, 0, button->name);
253 return 0;
256 int init_button ()
258 zde_btn_list.next = &zde_btn_list;
259 zde_btn_list.prev = &zde_btn_list;
261 /* FOLDER bitmap */
262 /* bitmap_button = (unsigned short *) malloc (1100);
264 if (!bitmap_button)
265 return -1;
267 memset (bitmap_button, 0, 1082);
269 // html web page
270 int fd = open ("button", O_RDONLY);
272 if (!fd) {
273 puts ("error -> file 'button' not found\n");
274 return -1;
277 if (!read (fd, (unsigned char *) bitmap_button, 1082)) {
278 puts ("error -> something was wrong !\n");
279 return -1;
283 /* create test button */
284 //create_button ("Filesystem", 0, handler_button_filesystem, "");
286 //pthread_t thread;
288 //pthread_create (&thread, NULL, handler_button_filesystem, "");
290 return 0;