App inst fixed on invalid image file; App zinstall for ZDE works - we've got GUI...
[ZeXOS.git] / apps / zde / button.c
blob9b96012cb064010be871a6730808a482f98542ef
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 "button.h"
33 #include "cursor.h"
34 #include "window.h"
35 #include "handler.h"
37 unsigned short *bitmap_button;
39 //zde_btn_t zde_btn_list;
41 extern zde_cursor_t *zde_cursor;
42 #ifdef TEST
43 zde_btn_t *create_button (char *name, int type, void *(*entry) (void *), void *arg)
45 zde_btn_t *button = (zde_btn_t *) malloc (sizeof (zde_btn_t));
47 if (!button)
48 return 0;
50 if (type == 0) {
51 button->arg = arg;
53 button->bitmap = bitmap_folder;
54 } else
55 button->bitmap = 0;
57 button->name = strdup (name);
58 button->entry = entry;
60 button->object = 0;
62 /* add into list */
63 button->next = &zde_btn_list;
64 button->prev = zde_btn_list.prev;
66 button->x = 14 + (button->prev->x * 40);
67 button->y = 14;
69 button->prev->next = button;
70 button->next->prev = button;
73 return button;
76 zde_btn_t *create_button_window (char *name, int type, void *(*entry) (void *), void *arg, zde_win_t *window)
78 if (!window)
79 return 0;
81 zde_btn_t *button = (zde_btn_t *) malloc (sizeof (zde_btn_t));
83 if (!button)
84 return 0;
86 button->x = window->x + 14 + (window->objectid * 40);
87 button->y = window->y + 28;
89 window->objectid ++;
91 if (type == 0) {
92 unsigned arg_len = strlen ((char *) arg);
93 unsigned name_len = strlen (name);
95 button->arg = malloc (arg_len + name_len + 2);
97 if (!button->arg)
98 return 0;
100 char *s = button->arg;
102 memcpy (s, arg, arg_len);
103 memcpy (s+arg_len, "/", 1);
104 memcpy (s+arg_len+1, name, name_len);
105 s[arg_len+name_len+1] = '\0';
107 button->bitmap = bitmap_folder;
108 } else
109 button->bitmap = 0;
111 button->name = strdup (name);
112 button->entry = entry;
113 button->object = (void *) window;
115 /* add into list */
116 button->next = &zde_btn_list;
117 button->prev = zde_btn_list.prev;
118 button->prev->next = button;
119 button->next->prev = button;
121 return button;
124 unsigned destroy_button_window (zde_win_t *window)
126 unsigned i;
128 while (1) {
129 i = 0;
131 zde_btn_t *button;
132 for (button = zde_btn_list.next; button != &zde_btn_list; button = button->next) {
133 if (button->object == (void *) window) {
134 button->next->prev = button->prev;
135 button->prev->next = button->next;
137 free (button);
139 i = 1;
140 break;
144 if (!i)
145 break;
148 return 1;
151 static void draw_bitmap (unsigned short x, unsigned short y)
153 unsigned i = 0;
154 unsigned j = 0;
155 unsigned k = 0;
156 unsigned pix = 0;
158 for (i = 0; i < 32*31; i ++) {
159 if (k >= 32) {
160 j ++;
161 k = 0;
164 pix = (1024-i)+2;
166 if (pix > 2100)
167 continue;
169 if (bitmap_folder[pix] != (unsigned short) 0)
170 if (k <= 32 && j <= 32)
171 xpixel ((unsigned) x+k, (unsigned) y+j, (unsigned) bitmap_folder[pix]);
173 k ++;
178 int draw_button_window (zde_win_t *window)
180 zde_btn_t *button;
181 for (button = zde_btn_list.next; button != &zde_btn_list; button = button->next) {
182 if (button->object != (void *) window)
183 continue;
185 /* we click on left button */
186 if (zde_cursor->state == XCURSOR_STATE_LBUTTON) {
187 if (zde_cursor->x >= button->x && zde_cursor->x <= button->x+32)
188 if (zde_cursor->y >= button->y && zde_cursor->y <= button->y+32) {
189 zde_cursor->state = 0;
191 pthread_t thread;
193 pthread_create (&thread, NULL, button->entry, button->arg);
197 /* 32x32 bitmap */
198 if (button->bitmap)
199 draw_bitmap (button->x, button->y);
200 else
201 xrectfill (button->x, button->y, button->x+32, button->y+32, ~0);
203 if (button->name)
204 xtext_puts (button->x-7, button->y+35, 0, button->name);
207 return 0;
209 #endif
210 int init_button ()
212 // zde_btn_list.next = &zde_btn_list;
213 // zde_btn_list.prev = &zde_btn_list;
215 /* FOLDER bitmap */
216 /* bitmap_button = (unsigned short *) malloc (1100);
218 if (!bitmap_button)
219 return -1;
221 memset (bitmap_button, 0, 1082);
223 // html web page
224 int fd = open ("button", O_RDONLY);
226 if (!fd) {
227 puts ("error -> file 'button' not found\n");
228 return -1;
231 if (!read (fd, (unsigned char *) bitmap_button, 1082)) {
232 puts ("error -> something was wrong !\n");
233 return -1;
237 /* create test button */
238 //create_button ("Filesystem", 0, handler_button_filesystem, "");
240 //pthread_t thread;
242 //pthread_create (&thread, NULL, handler_button_filesystem, "");
244 return 0;