Fixed ZDE build - missing header file
[ZeXOS.git] / apps / zde / icon.c
blob0e990c40459c5e5c09f9611ca2871314b740e16f
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * Copyright (C) 2010 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <fcntl.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <pthread.h>
27 #include <libx/base.h>
28 #include <libx/object.h>
29 #include <libx/image.h>
30 #include <libx/cursor.h>
31 #include <libx/text.h>
33 #include "icon.h"
34 #include "button.h"
35 #include "cursor.h"
36 #include "window.h"
37 #include "dialog.h"
38 #include "handler.h"
40 unsigned short *bitmap_folder;
41 unsigned short *bitmap_file;
43 zde_icon_t zde_icon_list;
45 extern zde_cursor_t *zde_cursor;
47 zde_icon_t *create_icon (char *name, int type, void *(*entry) (void *), void *arg)
49 zde_icon_t *icon = (zde_icon_t *) malloc (sizeof (zde_icon_t));
51 if (!icon)
52 return 0;
54 if (type == 0) {
55 icon->arg = arg;
57 icon->bitmap = bitmap_folder;
58 } else
59 icon->bitmap = 0;
61 icon->name_len = strlen (name);
62 icon->name = strdup (name);
63 icon->entry = entry;
64 icon->object = 0;
65 icon->type = (char) type;
67 /* add into list */
68 icon->next = &zde_icon_list;
69 icon->prev = zde_icon_list.prev;
71 icon->x = 14;
72 icon->y = icon->prev->y + 54;
74 icon->prev->next = icon;
75 icon->next->prev = icon;
77 return icon;
80 zde_icon_t *create_icon_window (char *name, int type, void *(*entry) (void *), void *arg, zde_win_t *window)
82 if (!window)
83 return 0;
85 zde_icon_t *icon = (zde_icon_t *) malloc (sizeof (zde_icon_t));
87 if (!icon)
88 return 0;
90 icon->x = window->x + 14 + (window->objectid * 40);
91 icon->y = window->y + 28;
93 window->objectid ++;
95 char *s = 0;
96 unsigned arg_len;
97 unsigned name_len;
99 arg_len = strlen ((char *) arg);
100 name_len = strlen (name);
102 icon->arg = malloc (arg_len + name_len + 2);
104 if (!icon->arg)
105 return 0;
107 s = icon->arg;
109 memcpy (s, arg, arg_len);
110 memcpy (s+arg_len, "/", 1);
111 memcpy (s+arg_len+1, name, name_len);
112 s[arg_len+name_len+1] = '\0';
114 switch (type) {
115 case 0:
116 icon->bitmap = bitmap_folder;
117 break;
118 case 1:
119 icon->bitmap = bitmap_file;
120 break;
123 icon->name_len = strlen (name);
124 icon->name = strdup (name);
125 icon->entry = entry;
126 icon->object = (void *) window;
127 icon->type = (char) type;
129 /* add into list */
130 icon->next = &zde_icon_list;
131 icon->prev = zde_icon_list.prev;
132 icon->prev->next = icon;
133 icon->next->prev = icon;
135 return icon;
138 unsigned destroy_icon_window (zde_win_t *window)
140 unsigned i;
142 for (;;) {
143 i = 0;
145 zde_icon_t *icon;
146 for (icon = zde_icon_list.next; icon != &zde_icon_list; icon = icon->next) {
147 if (icon->object == (void *) window) {
148 icon->next->prev = icon->prev;
149 icon->prev->next = icon->next;
151 free (icon);
153 i = 1;
154 break;
158 if (!i)
159 break;
162 return 1;
165 static void draw_bitmap (unsigned short *bitmap, unsigned short x, unsigned short y)
167 unsigned i = 0;
168 unsigned j = 0;
169 unsigned k = 0;
170 unsigned pix = 0;
172 for (i = 0; i < 32*32; i ++) {
173 if (k >= 32) {
174 j ++;
175 k = 0;
178 pix = (1024-i)+34;
180 if (pix > 2100)
181 continue;
183 if (bitmap[pix] != (unsigned short) 0)
184 if (k <= 32 && j <= 32)
185 xpixel ((unsigned) x+k, (unsigned) y+j, (unsigned) bitmap[pix]);
187 k ++;
191 int draw_icon_desktop ()
193 zde_icon_t *icon;
194 for (icon = zde_icon_list.next; icon != &zde_icon_list; icon = icon->next) {
195 if (icon->object)
196 continue;
198 /* button event */
199 switch (zde_cursor->state) {
200 case XCURSOR_STATE_LBUTTON:
202 if (!(zde_cursor->flags & CURSOR_FLAG_LBCLICK))
203 if (!(zde_cursor->flags & CURSOR_FLAG_MOVE)) {
204 if (zde_cursor->x >= icon->x && zde_cursor->x <= icon->x+32)
205 if (zde_cursor->y >= icon->y && zde_cursor->y <= icon->y+32) {
206 zde_cursor->state = 0;
207 zde_cursor->flags |= CURSOR_FLAG_LBCLICK;
209 /* create new thread as entry function */
210 pthread_t thread;
211 pthread_create (&thread, NULL, icon->entry, icon->arg);
214 break;
216 case XCURSOR_STATE_RBUTTON:
218 if (!(zde_cursor->flags & CURSOR_FLAG_RBCLICK))
219 if (!(zde_cursor->flags & CURSOR_FLAG_MOVE)) {
220 if (zde_cursor->x >= icon->x && zde_cursor->x <= icon->x+32)
221 if (zde_cursor->y >= icon->y && zde_cursor->y <= icon->y+32) {
222 zde_cursor->state = 0;
223 zde_cursor->flags |= CURSOR_FLAG_RBCLICK;
225 /* create test window */
226 zde_win_t *window = create_window ("Properties", 1);
228 if (!window)
229 return (0);
231 window->sizex = 95;
232 window->sizey = 100;
234 if (icon->type == 0)
235 create_button_window ("Open", 0, handler_icon_filesystem, icon->arg, window);
236 if (icon->type == 1)
237 create_button_window ("Read", 0, handler_icon_reader, icon->arg, window);
238 //create_button_window ("Delete", 0, handler_icon_filesystem, icon->arg, window);
242 break;
246 /* 32x32 bitmap */
247 if (icon->bitmap)
248 draw_bitmap (icon->bitmap, icon->x, icon->y);
249 else
250 xrectfill (icon->x, icon->y, icon->x+32, icon->y+32, ~0);
252 if (icon->name)
253 xtext_puts (icon->x-(icon->name_len*3)+16, icon->y+35, ~0, icon->name);
256 return 0;
259 int draw_icon_window (zde_win_t *window)
261 unsigned obj = 0;
262 unsigned obj_x = 0;
263 unsigned obj_y = 0;
265 zde_icon_t *icon;
266 for (icon = zde_icon_list.next; icon != &zde_icon_list; icon = icon->next) {
267 if (icon->object != (void *) window)
268 continue;
270 /* update icon position */
271 icon->x = window->x + 14 + obj_x;
272 icon->y = window->y + 28 + obj_y;
274 obj ++;
276 obj_x += 48;
278 if (icon->x >= window->x+window->sizex-48) {
279 obj_x = 0;
280 obj_y += 50;
283 /* button event */
284 switch (zde_cursor->state) {
285 case XCURSOR_STATE_LBUTTON:
287 if (!(zde_cursor->flags & CURSOR_FLAG_LBCLICK))
288 if (!(zde_cursor->flags & CURSOR_FLAG_MOVE)) {
289 if (zde_cursor->x >= icon->x && zde_cursor->x <= icon->x+32)
290 if (zde_cursor->y >= icon->y && zde_cursor->y <= icon->y+32) {
291 if (isactive_window () != window)
292 continue;
294 zde_cursor->state = 0;
295 zde_cursor->flags |= CURSOR_FLAG_LBCLICK;
297 pthread_t thread;
299 pthread_create (&thread, NULL, icon->entry, icon->arg);
303 break;
305 case XCURSOR_STATE_RBUTTON:
307 if (!(zde_cursor->flags & CURSOR_FLAG_RBCLICK))
308 if (!(zde_cursor->flags & CURSOR_FLAG_MOVE)) {
309 if (zde_cursor->x >= icon->x && zde_cursor->x <= icon->x+32)
310 if (zde_cursor->y >= icon->y && zde_cursor->y <= icon->y+32) {
311 zde_cursor->state = 0;
312 zde_cursor->flags |= CURSOR_FLAG_RBCLICK;
314 /* create test window */
315 zde_win_t *window = create_window ("Properties", 1);
317 if (!window)
318 return (0);
320 window->sizex = 95;
321 window->sizey = 100;
323 if (icon->type == 0)
324 create_button_window ("Open", 0, handler_icon_filesystem, icon->arg, window);
325 if (icon->type == 1)
326 create_button_window ("Read", 0, handler_icon_reader, icon->arg, window);
327 //create_button_window ("Delete", 0, handler_icon_filesystem, icon->arg, window);
331 break;
335 /* 32x32 bitmap */
336 if (icon->bitmap)
337 draw_bitmap (icon->bitmap, icon->x, icon->y);
338 else
339 xrectfill (icon->x, icon->y, icon->x+32, icon->y+32, ~0);
341 if (icon->name)
342 xtext_puts (icon->x-(icon->name_len*3)+16, icon->y+35, 0, icon->name);
345 return 0;
348 unsigned short *load_icon (char *filename)
350 /* FOLDER bitmap */
351 unsigned short *bitmap = (unsigned short *) malloc (2120);
353 if (!bitmap)
354 return 0;
356 memset (bitmap, 0, 2048+70);
358 // open icon bitmap
359 int fd = open (filename, O_RDONLY);
361 if (!fd) {
362 printf ("error -> file '%s' not found\n", filename);
363 return 0;
366 /* read icon bitmap data */
367 if (!read (fd, (unsigned char *) bitmap, 2048+70)) {
368 puts ("error -> something was wrong !\n");
369 return 0;
372 return bitmap;
375 int init_icon ()
377 zde_icon_list.next = &zde_icon_list;
378 zde_icon_list.prev = &zde_icon_list;
380 zde_icon_list.prev->x = 0;
381 zde_icon_list.prev->y = 0;
383 /* FOLDER bitmap */
384 bitmap_folder = load_icon ("folder");
386 if (!bitmap_folder)
387 return -1;
389 /* FILE bitmap */
390 bitmap_file = load_icon ("file");
392 if (!bitmap_file)
393 return -1;
395 /* create test icon */
396 create_icon ("Filesystem", 0, handler_icon_filesystem, "");
398 /* create test icon */
399 create_icon ("Terminal", 0, handler_icon_terminal, "");
401 return 0;