App inst fixed on invalid image file; App zinstall for ZDE works - we've got GUI...
[ZeXOS.git] / apps / zde / icon.c
blob286c617cccff0c98dc5e88d498fcedd8d1b62d84
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 "icon.h"
33 #include "cursor.h"
34 #include "window.h"
35 #include "handler.h"
37 unsigned short *bitmap_folder;
38 unsigned short *bitmap_file;
40 zde_icon_t zde_icon_list;
42 extern zde_cursor_t *zde_cursor;
44 zde_icon_t *create_icon (char *name, int type, void *(*entry) (void *), void *arg)
46 zde_icon_t *icon = (zde_icon_t *) malloc (sizeof (zde_icon_t));
48 if (!icon)
49 return 0;
51 if (type == 0) {
52 icon->arg = arg;
54 icon->bitmap = bitmap_folder;
55 } else
56 icon->bitmap = 0;
58 icon->name_len = strlen (name);
59 icon->name = strdup (name);
60 icon->entry = entry;
62 icon->object = 0;
64 /* add into list */
65 icon->next = &zde_icon_list;
66 icon->prev = zde_icon_list.prev;
68 icon->x = 14;
69 icon->y = icon->prev->y + 54;
71 icon->prev->next = icon;
72 icon->next->prev = icon;
74 return icon;
77 zde_icon_t *create_icon_window (char *name, int type, void *(*entry) (void *), void *arg, zde_win_t *window)
79 if (!window)
80 return 0;
82 zde_icon_t *icon = (zde_icon_t *) malloc (sizeof (zde_icon_t));
84 if (!icon)
85 return 0;
87 icon->x = window->x + 14 + (window->objectid * 40);
88 icon->y = window->y + 28;
90 window->objectid ++;
92 char *s = 0;
93 unsigned arg_len;
94 unsigned name_len;
96 switch (type) {
97 case 0:
98 arg_len = strlen ((char *) arg);
99 name_len = strlen (name);
101 icon->arg = malloc (arg_len + name_len + 2);
103 if (!icon->arg)
104 return 0;
106 s = icon->arg;
108 memcpy (s, arg, arg_len);
109 memcpy (s+arg_len, "/", 1);
110 memcpy (s+arg_len+1, name, name_len);
111 s[arg_len+name_len+1] = '\0';
113 icon->bitmap = bitmap_folder;
114 break;
115 case 1:
116 arg_len = strlen ((char *) arg);
117 name_len = strlen (name);
119 icon->arg = malloc (arg_len + name_len + 2);
121 if (!icon->arg)
122 return 0;
124 s = icon->arg;
126 memcpy (s, arg, arg_len);
127 memcpy (s+arg_len, "/", 1);
128 memcpy (s+arg_len+1, name, name_len);
129 s[arg_len+name_len+1] = '\0';
131 icon->bitmap = bitmap_file;
132 break;
136 icon->name_len = strlen (name);
137 icon->name = strdup (name);
138 icon->entry = entry;
139 icon->object = (void *) window;
141 /* add into list */
142 icon->next = &zde_icon_list;
143 icon->prev = zde_icon_list.prev;
144 icon->prev->next = icon;
145 icon->next->prev = icon;
147 return icon;
150 unsigned destroy_icon_window (zde_win_t *window)
152 unsigned i;
154 while (1) {
155 i = 0;
157 zde_icon_t *icon;
158 for (icon = zde_icon_list.next; icon != &zde_icon_list; icon = icon->next) {
159 if (icon->object == (void *) window) {
160 icon->next->prev = icon->prev;
161 icon->prev->next = icon->next;
163 free (icon);
165 i = 1;
166 break;
170 if (!i)
171 break;
174 return 1;
177 static void draw_bitmap (unsigned short *bitmap, unsigned short x, unsigned short y)
179 unsigned i = 0;
180 unsigned j = 0;
181 unsigned k = 0;
182 unsigned pix = 0;
184 for (i = 0; i < 32*32; i ++) {
185 if (k >= 32) {
186 j ++;
187 k = 0;
190 pix = (1024-i)+34;
192 if (pix > 2100)
193 continue;
195 if (bitmap[pix] != (unsigned short) 0)
196 if (k <= 32 && j <= 32)
197 xpixel ((unsigned) x+k, (unsigned) y+j, (unsigned) bitmap[pix]);
199 k ++;
203 int draw_icon_desktop ()
205 zde_icon_t *icon;
206 for (icon = zde_icon_list.next; icon != &zde_icon_list; icon = icon->next) {
207 if (icon->object)
208 continue;
210 /* we click on left button */
211 if (zde_cursor->state == XCURSOR_STATE_LBUTTON) {
212 if (!(zde_cursor->flags & CURSOR_FLAG_LBCLICK))
213 if (!(zde_cursor->flags & CURSOR_FLAG_MOVE))
214 if (zde_cursor->x >= icon->x && zde_cursor->x <= icon->x+32)
215 if (zde_cursor->y >= icon->y && zde_cursor->y <= icon->y+32) {
216 zde_cursor->state = 0;
217 zde_cursor->flags |= CURSOR_FLAG_LBCLICK;
219 pthread_t thread;
221 pthread_create (&thread, NULL, icon->entry, icon->arg);
225 /* 32x32 bitmap */
226 if (icon->bitmap)
227 draw_bitmap (icon->bitmap, icon->x, icon->y);
228 else
229 xrectfill (icon->x, icon->y, icon->x+32, icon->y+32, ~0);
231 if (icon->name)
232 xtext_puts (icon->x-(icon->name_len*3)+16, icon->y+35, ~0, icon->name);
235 return 0;
238 int draw_icon_window (zde_win_t *window)
240 unsigned obj = 0;
241 unsigned obj_x = 0;
242 unsigned obj_y = 0;
244 zde_icon_t *icon;
245 for (icon = zde_icon_list.next; icon != &zde_icon_list; icon = icon->next) {
246 if (icon->object != (void *) window)
247 continue;
249 /* update icon position */
250 icon->x = window->x + 14 + obj_x;
251 icon->y = window->y + 28 + obj_y;
253 obj ++;
255 obj_x += 48;
257 if (icon->x >= window->x+window->sizex-48) {
258 obj_x = 0;
259 obj_y += 50;
262 /* we click on left button */
263 if (zde_cursor->state == XCURSOR_STATE_LBUTTON) {
264 if (!(zde_cursor->flags & CURSOR_FLAG_LBCLICK))
265 if (!(zde_cursor->flags & CURSOR_FLAG_MOVE))
266 if (zde_cursor->x >= icon->x && zde_cursor->x <= icon->x+32)
267 if (zde_cursor->y >= icon->y && zde_cursor->y <= icon->y+32) {
268 if (isactive_window () != window)
269 continue;
271 zde_cursor->state = 0;
272 zde_cursor->flags |= CURSOR_FLAG_LBCLICK;
274 pthread_t thread;
276 pthread_create (&thread, NULL, icon->entry, icon->arg);
280 /* 32x32 bitmap */
281 if (icon->bitmap)
282 draw_bitmap (icon->bitmap, icon->x, icon->y);
283 else
284 xrectfill (icon->x, icon->y, icon->x+32, icon->y+32, ~0);
286 if (icon->name)
287 xtext_puts (icon->x-(icon->name_len*3)+16, icon->y+35, 0, icon->name);
290 return 0;
293 unsigned short *load_icon (char *filename)
295 /* FOLDER bitmap */
296 unsigned short *bitmap = (unsigned short *) malloc (2120);
298 if (!bitmap)
299 return 0;
301 memset (bitmap, 0, 2048+70);
303 // open icon bitmap
304 int fd = open (filename, O_RDONLY);
306 if (!fd) {
307 printf ("error -> file '%s' not found\n", filename);
308 return 0;
311 /* read icon bitmap data */
312 if (!read (fd, (unsigned char *) bitmap, 2048+70)) {
313 puts ("error -> something was wrong !\n");
314 return 0;
317 return bitmap;
320 int init_icon ()
322 zde_icon_list.next = &zde_icon_list;
323 zde_icon_list.prev = &zde_icon_list;
325 zde_icon_list.prev->x = 0;
326 zde_icon_list.prev->y = 0;
328 /* FOLDER bitmap */
329 bitmap_folder = load_icon ("folder");
331 if (!bitmap_folder)
332 return -1;
334 /* FILE bitmap */
335 bitmap_file = load_icon ("file");
337 if (!bitmap_file)
338 return -1;
340 /* create test icon */
341 create_icon ("Filesystem", 0, handler_icon_filesystem, "");
343 /* create test icon */
344 create_icon ("Terminal", 0, handler_icon_terminal, "");
346 return 0;