System call sys_getchar () was improved; stdin is correspond with 0. fd and stdout...
[ZeXOS.git] / apps / zde / button.c
blobc17edd28b7e60b63971a97cb3f19bede487764a6
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
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 <fcntl.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <pthread.h>
25 #include <libx/base.h>
26 #include <libx/object.h>
27 #include <libx/image.h>
28 #include <libx/cursor.h>
29 #include <libx/text.h>
31 #include "button.h"
32 #include "cursor.h"
33 #include "window.h"
34 #include "handler.h"
36 unsigned short *bitmap_folder;
38 zde_btn_t zde_btn_list;
40 extern zde_cursor_t *zde_cursor;
42 zde_btn_t *create_button (char *name, int type, void *(*entry) (void *), void *arg)
44 zde_btn_t *button = (zde_btn_t *) malloc (sizeof (zde_btn_t));
46 if (!button)
47 return 0;
49 if (type == 0) {
50 button->arg = arg;
52 button->bitmap = bitmap_folder;
53 } else
54 button->bitmap = 0;
56 button->name = strdup (name);
57 button->entry = entry;
59 button->object = 0;
61 /* add into list */
62 button->next = &zde_btn_list;
63 button->prev = zde_btn_list.prev;
65 button->x = 14 + (button->prev->x * 40);
66 button->y = 14;
68 button->prev->next = button;
69 button->next->prev = button;
72 return button;
75 zde_btn_t *create_button_window (char *name, int type, void *(*entry) (void *), void *arg, zde_win_t *window)
77 if (!window)
78 return 0;
80 zde_btn_t *button = (zde_btn_t *) malloc (sizeof (zde_btn_t));
82 if (!button)
83 return 0;
85 button->x = window->x + 14 + (window->objectid * 40);
86 button->y = window->y + 28;
88 window->objectid ++;
90 if (type == 0) {
91 unsigned arg_len = strlen ((char *) arg);
92 unsigned name_len = strlen (name);
94 button->arg = malloc (arg_len + name_len + 2);
96 if (!button->arg)
97 return 0;
99 char *s = button->arg;
101 memcpy (s, arg, arg_len);
102 memcpy (s+arg_len, "/", 1);
103 memcpy (s+arg_len+1, name, name_len);
104 s[arg_len+name_len+1] = '\0';
106 button->bitmap = bitmap_folder;
107 } else
108 button->bitmap = 0;
110 button->name = strdup (name);
111 button->entry = entry;
112 button->object = (void *) window;
114 /* add into list */
115 button->next = &zde_btn_list;
116 button->prev = zde_btn_list.prev;
117 button->prev->next = button;
118 button->next->prev = button;
120 return button;
123 unsigned destroy_button_window (zde_win_t *window)
125 unsigned i;
127 while (1) {
128 i = 0;
130 zde_btn_t *button;
131 for (button = zde_btn_list.next; button != &zde_btn_list; button = button->next) {
132 if (button->object == (void *) window) {
133 button->next->prev = button->prev;
134 button->prev->next = button->next;
136 free (button);
138 i = 1;
139 break;
143 if (!i)
144 break;
147 return 1;
150 static void draw_bitmap (unsigned short x, unsigned short y)
152 unsigned i = 0;
153 unsigned j = 0;
154 unsigned k = 0;
155 unsigned pix = 0;
157 for (i = 0; i < 32*31; i ++) {
158 if (k >= 32) {
159 j ++;
160 k = 0;
163 pix = (1024-i)+2;
165 if (pix > 2100)
166 continue;
168 if (bitmap_folder[pix] != (unsigned short) 0)
169 if (k <= 32 && j <= 32)
170 xpixel ((unsigned) x+k, (unsigned) y+j, (unsigned) bitmap_folder[pix]);
172 k ++;
177 int draw_button_window (zde_win_t *window)
179 zde_btn_t *button;
180 for (button = zde_btn_list.next; button != &zde_btn_list; button = button->next) {
181 if (button->object != (void *) window)
182 continue;
184 /* we click on left button */
185 if (zde_cursor->state == XCURSOR_STATE_LBUTTON) {
186 if (zde_cursor->x >= button->x && zde_cursor->x <= button->x+32)
187 if (zde_cursor->y >= button->y && zde_cursor->y <= button->y+32) {
188 zde_cursor->state = 0;
190 pthread_t thread;
192 pthread_create (&thread, NULL, button->entry, button->arg);
196 /* 32x32 bitmap */
197 if (button->bitmap)
198 draw_bitmap (button->x, button->y);
199 else
200 xrectfill (button->x, button->y, button->x+32, button->y+32, ~0);
202 if (button->name)
203 xtext_puts (button->x-7, button->y+35, 0, button->name);
206 return 0;
209 int init_button ()
211 zde_btn_list.next = &zde_btn_list;
212 zde_btn_list.prev = &zde_btn_list;
214 /* FOLDER bitmap */
215 bitmap_folder = (unsigned short *) malloc (2120);
217 if (!bitmap_folder)
218 return -1;
220 memset (bitmap_folder, 0, 2048+70);
222 // html web page
223 int fd = open ("folder", O_RDONLY);
225 if (!fd) {
226 puts ("error -> file 'folder' not found\n");
227 return -1;
230 if (!read (fd, (unsigned char *) bitmap_folder, 2048+70)) {
231 puts ("error -> something was wrong !\n");
232 return -1;
236 /* create test button */
237 //create_button ("Filesystem", 0, handler_button_filesystem, "");
239 //pthread_t thread;
241 //pthread_create (&thread, NULL, handler_button_filesystem, "");
243 return 0;