-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.c
More file actions
607 lines (507 loc) · 18.8 KB
/
gui.c
File metadata and controls
607 lines (507 loc) · 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
* gui.c - GUI System 2.0 for GegOS
* Complete rewrite with proper cursor handling
*
* Key changes in 2.0:
* - Cursor drawn LAST after all rendering
* - No save/restore needed - full screen redraws each frame
* - Simpler, more reliable rendering pipeline
*/
#include "gui.h"
#include "vga.h"
#include "mouse.h"
#include "keyboard.h"
#include "io.h"
/* External declarations */
extern void redraw_cursor_area_kernel(int x, int y);
/* GUI Colors (Windows 95/XP classic theme) */
#define GUI_COLOR_DESKTOP COLOR_CYAN /* Teal desktop */
#define GUI_COLOR_WINDOW_BG COLOR_LIGHT_GRAY /* Gray window background */
#define GUI_COLOR_WINDOW_FG COLOR_BLACK
#define GUI_COLOR_TITLEBAR COLOR_BLUE /* Blue active titlebar */
#define GUI_COLOR_TITLE_TEXT COLOR_WHITE
#define GUI_COLOR_BORDER COLOR_BLACK
#define GUI_COLOR_BUTTON_BG COLOR_LIGHT_GRAY /* Gray buttons */
#define GUI_COLOR_BUTTON_FG COLOR_BLACK
#define GUI_COLOR_BUTTON_HOVER COLOR_LIGHT_CYAN
#define GUI_COLOR_BUTTON_PRESS COLOR_DARK_GRAY
#define GUI_COLOR_TASKBAR COLOR_LIGHT_GRAY /* Gray taskbar */
/* Window storage */
static gui_window_t windows[MAX_WINDOWS];
static int num_windows = 0;
static int active_window = -1;
/* Button storage */
static gui_button_t buttons[MAX_BUTTONS];
static int num_buttons = 0;
/* ============================================================================
* CURSOR SYSTEM 2.0 - Optimized rectangle redraws
* ============================================================================ */
/* Cursor state */
static int cursor_visible = 0;
static int cursor_x = 0;
static int cursor_y = 0;
static int cursor_last_x = -1;
static int cursor_last_y = -1;
/* Cursor dimensions */
#define CURSOR_WIDTH 12
#define CURSOR_BUFFER 32 /* Extra pixels to redraw around cursor */
/* Arrow cursor shape - 1=black border, 2=white fill, 0=transparent */
static const uint8_t cursor_shape[16][12] = {
{1,0,0,0,0,0,0,0,0,0,0,0},
{1,1,0,0,0,0,0,0,0,0,0,0},
{1,2,1,0,0,0,0,0,0,0,0,0},
{1,2,2,1,0,0,0,0,0,0,0,0},
{1,2,2,2,1,0,0,0,0,0,0,0},
{1,2,2,2,2,1,0,0,0,0,0,0},
{1,2,2,2,2,2,1,0,0,0,0,0},
{1,2,2,2,2,2,2,1,0,0,0,0},
{1,2,2,2,2,2,2,2,1,0,0,0},
{1,2,2,2,2,2,2,2,2,1,0,0},
{1,2,2,2,2,2,1,1,1,1,1,0},
{1,2,2,1,2,2,1,0,0,0,0,0},
{1,2,1,0,1,2,2,1,0,0,0,0},
{1,1,0,0,1,2,2,1,0,0,0,0},
{1,0,0,0,0,1,2,2,1,0,0,0},
{0,0,0,0,0,1,1,1,1,0,0,0}
};
/* Draw cursor at position - called AFTER all other rendering */
static void draw_cursor_at(int x, int y) {
for (int j = 0; j < 16; j++) {
for (int i = 0; i < 12; i++) {
int px = x + i;
int py = y + j;
if (px >= 0 && px < SCREEN_WIDTH && py >= 0 && py < SCREEN_HEIGHT) {
uint8_t val = cursor_shape[j][i];
if (val == 1) {
vga_putpixel(px, py, COLOR_BLACK);
} else if (val == 2) {
vga_putpixel(px, py, COLOR_WHITE);
}
}
}
}
}
/* Point in rect check */
int point_in_rect(int px, int py, int rx, int ry, int rw, int rh) {
return px >= rx && px < rx + rw && py >= ry && py < ry + rh;
}
/* Initialize GUI */
void gui_init(void) {
num_windows = 0;
num_buttons = 0;
active_window = -1;
cursor_visible = 0;
cursor_x = SCREEN_WIDTH / 2;
cursor_y = SCREEN_HEIGHT / 2;
}
/* Create window */
int gui_create_window(int x, int y, int width, int height, const char* title) {
if (num_windows >= MAX_WINDOWS) return -1;
int id = num_windows++;
gui_window_t* win = &windows[id];
win->x = x;
win->y = y;
win->width = width;
win->height = height;
win->title = title;
win->active = 0;
win->dragging = 0;
win->visible = 1;
win->dirty_region.dirty = 0;
return id;
}
/* Create button */
int gui_create_button(int x, int y, int width, int height, const char* label, void (*callback)(void)) {
if (num_buttons >= MAX_BUTTONS) return -1;
int id = num_buttons++;
gui_button_t* btn = &buttons[id];
btn->x = x;
btn->y = y;
btn->width = width;
btn->height = height;
btn->label = label;
btn->callback = callback;
btn->pressed = 0;
btn->hovered = 0;
btn->visible = 1;
btn->window_id = -1;
return id;
}
/* Create button in window */
int gui_create_window_button(int window_id, int x, int y, int width, int height,
const char* label, void (*callback)(void)) {
int id = gui_create_button(x, y, width, height, label, callback);
if (id >= 0) {
buttons[id].window_id = window_id;
}
return id;
}
/* Get window */
gui_window_t* gui_get_window(int id) {
if (id >= 0 && id < num_windows) {
return &windows[id];
}
return 0;
}
/* Show/hide window */
void gui_show_window(int window_id, int visible) {
if (window_id >= 0 && window_id < num_windows) {
windows[window_id].visible = visible;
}
}
/* Set active window */
void gui_set_active_window(int window_id) {
for (int i = 0; i < num_windows; i++) {
windows[i].active = (i == window_id);
}
active_window = window_id;
}
/* Get active window */
int gui_get_active_window(void) {
return active_window;
}
/* Close window (hide it) */
void gui_close_window(int window_id) {
if (window_id >= 0 && window_id < num_windows) {
windows[window_id].visible = 0;
windows[window_id].active = 0;
if (active_window == window_id) {
active_window = -1;
}
}
}
/* ============================================================================
* CURSOR API 2.0 - Optimized rectangle redraws
* ============================================================================ */
/* Draw cursor - optimized: only redraws cursor area when moved */
void gui_draw_cursor(int x, int y) {
/* Clamp position */
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x > SCREEN_WIDTH - CURSOR_WIDTH) x = SCREEN_WIDTH - CURSOR_WIDTH;
if (y > SCREEN_HEIGHT - CURSOR_HEIGHT) y = SCREEN_HEIGHT - CURSOR_HEIGHT;
/* If cursor moved, erase old position by redrawing that area */
if (cursor_last_x >= 0 && cursor_last_y >= 0 &&
(cursor_last_x != x || cursor_last_y != y)) {
/* Call kernel function to redraw cursor area */
extern void redraw_cursor_area_kernel(int x, int y);
redraw_cursor_area_kernel(cursor_last_x, cursor_last_y);
}
/* Update position */
cursor_x = x;
cursor_y = y;
cursor_visible = 1;
/* Draw cursor at new position */
draw_cursor_at(x, y);
/* Update last position */
cursor_last_x = x;
cursor_last_y = y;
}
/* Erase cursor - in 2.0, this is a no-op since we redraw everything */
void gui_erase_cursor(void) {
cursor_visible = 0;
}
/* Invalidate cursor - in 2.0, just marks cursor as not drawn */
void gui_cursor_invalidate(void) {
cursor_visible = 0;
}
/* ============================================================================
* DIRTY RECT STUBS (kept for API compatibility)
* ============================================================================ */
static int num_dirty_rects = 0;
void gui_add_dirty_rect(int x, int y, int width, int height) {
(void)x; (void)y; (void)width; (void)height;
}
int gui_has_dirty_rects(void) {
return num_dirty_rects > 0;
}
void gui_clear_dirty_rects(void) {
num_dirty_rects = 0;
}
void gui_redraw_dirty(void) {
num_dirty_rects = 0;
}
/* ============================================================================
* DRAWING FUNCTIONS
* ============================================================================ */
/* Draw desktop */
void gui_draw_desktop(void) {
vga_fillrect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 32, GUI_COLOR_DESKTOP);
}
/* Draw menubar/taskbar - Enhanced Windows-style taskbar */
void gui_draw_menubar(void) {
int taskbar_height = 32; /* Increased height for better appearance */
int taskbar_y = SCREEN_HEIGHT - taskbar_height;
/* Taskbar background with gradient effect */
for (int i = 0; i < taskbar_height; i++) {
uint8_t color = GUI_COLOR_TASKBAR;
if (i < 2) color = COLOR_WHITE; /* Top highlight */
else if (i > taskbar_height - 3) color = COLOR_DARK_GRAY; /* Bottom shadow */
vga_hline(0, taskbar_y + i, SCREEN_WIDTH, color);
}
/* Top border (raised effect) */
vga_hline(0, taskbar_y, SCREEN_WIDTH, COLOR_WHITE);
vga_hline(0, taskbar_y + 1, SCREEN_WIDTH, COLOR_LIGHT_GRAY);
/* Bottom border (sunken effect) */
vga_hline(0, taskbar_y + taskbar_height - 1, SCREEN_WIDTH, COLOR_DARK_GRAY);
vga_hline(0, taskbar_y + taskbar_height - 2, SCREEN_WIDTH, COLOR_BLACK);
/* Start button (Windows XP style) */
int start_w = 80; /* Wider for "Start" text */
int start_h = 24;
int start_x = 4;
int start_y = taskbar_y + 4;
/* Start button with 3D effect */
vga_fillrect(start_x, start_y, start_w, start_h, GUI_COLOR_BUTTON_BG);
/* Raised border */
vga_hline(start_x, start_y, start_w, COLOR_WHITE);
vga_vline(start_x, start_y, start_h, COLOR_WHITE);
vga_hline(start_x, start_y + start_h - 1, start_w, COLOR_DARK_GRAY);
vga_vline(start_x + start_w - 1, start_y, start_h, COLOR_DARK_GRAY);
/* Windows logo (4 colored squares) */
int logo_x = start_x + 6;
int logo_y = start_y + 6;
vga_fillrect(logo_x, logo_y, 4, 4, COLOR_LIGHT_GREEN); /* Top-left */
vga_fillrect(logo_x + 4, logo_y, 4, 4, COLOR_LIGHT_CYAN); /* Top-right */
vga_fillrect(logo_x, logo_y + 4, 4, 4, COLOR_LIGHT_RED); /* Bottom-left */
vga_fillrect(logo_x + 4, logo_y + 4, 4, 4, COLOR_YELLOW); /* Bottom-right */
/* Start text */
vga_putstring(start_x + 20, start_y + 8, "Start", COLOR_BLACK, GUI_COLOR_BUTTON_BG);
/* Quick launch area - app shortcuts */
int quick_x = start_x + start_w + 8;
int quick_y = taskbar_y + 6;
/* Browser shortcut */
vga_fillrect(quick_x, quick_y, 24, 20, COLOR_LIGHT_GRAY);
vga_rect(quick_x, quick_y, 24, 20, COLOR_BLACK);
vga_fillrect(quick_x + 4, quick_y + 4, 16, 12, COLOR_BLUE);
vga_putstring(quick_x + 28, quick_y + 6, "Browser", COLOR_BLACK, GUI_COLOR_TASKBAR);
/* File manager shortcut */
int file_x = quick_x + 80;
vga_fillrect(file_x, quick_y, 24, 20, COLOR_LIGHT_GRAY);
vga_rect(file_x, quick_y, 24, 20, COLOR_BLACK);
vga_fillrect(file_x + 4, quick_y + 4, 16, 12, COLOR_GREEN);
vga_putstring(file_x + 28, quick_y + 6, "Files", COLOR_BLACK, GUI_COLOR_TASKBAR);
/* Task area - show running applications */
int task_x = file_x + 80;
/* Sample running app button */
if (num_windows > 0) {
vga_fillrect(task_x, quick_y, 100, 20, GUI_COLOR_BUTTON_BG);
vga_rect(task_x, quick_y, 100, 20, COLOR_BLACK);
vga_putstring(task_x + 8, quick_y + 6, "Terminal", COLOR_BLACK, GUI_COLOR_BUTTON_BG);
}
/* System tray area */
int tray_x = SCREEN_WIDTH - 120;
int tray_y = taskbar_y + 6;
/* Network icon */
vga_fillrect(tray_x, tray_y, 16, 12, COLOR_LIGHT_GRAY);
vga_rect(tray_x, tray_y, 16, 12, COLOR_BLACK);
vga_fillrect(tray_x + 2, tray_y + 2, 12, 8, COLOR_GREEN);
/* Volume icon */
int vol_x = tray_x + 20;
vga_fillrect(vol_x, tray_y, 16, 12, COLOR_LIGHT_GRAY);
vga_rect(vol_x, tray_y, 16, 12, COLOR_BLACK);
vga_fillrect(vol_x + 2, tray_y + 2, 12, 8, COLOR_BLUE);
/* Clock (larger and more prominent) */
int clock_x = SCREEN_WIDTH - 70;
int clock_y = taskbar_y + 4;
int clock_w = 64;
int clock_h = 24;
/* Clock background with border */
vga_fillrect(clock_x, clock_y, clock_w, clock_h, GUI_COLOR_BUTTON_BG);
vga_rect(clock_x, clock_y, clock_w, clock_h, COLOR_BLACK);
/* Time display */
vga_putstring(clock_x + 8, clock_y + 8, "12:34 PM", COLOR_BLACK, GUI_COLOR_BUTTON_BG);
}
/* Draw window */
void gui_draw_window(gui_window_t* win) {
if (!win->visible) return;
int x = win->x;
int y = win->y;
int w = win->width;
int h = win->height;
/* Window background */
vga_fillrect(x, y, w, h, GUI_COLOR_WINDOW_BG);
/* Light edges (top-left) */
vga_hline(x, y, w, COLOR_WHITE);
vga_vline(x, y, h, COLOR_WHITE);
vga_hline(x + 1, y + 1, w - 2, COLOR_WHITE);
vga_vline(x + 1, y + 1, h - 2, COLOR_WHITE);
/* Dark edges (bottom-right) */
vga_hline(x, y + h - 1, w, COLOR_BLACK);
vga_vline(x + w - 1, y, h, COLOR_BLACK);
vga_hline(x + 1, y + h - 2, w - 2, COLOR_DARK_GRAY);
vga_vline(x + w - 2, y + 1, h - 2, COLOR_DARK_GRAY);
/* Title bar */
uint8_t titlebar_color = win->active ? GUI_COLOR_TITLEBAR : COLOR_DARK_GRAY;
vga_fillrect(x + 3, y + 3, w - 6, 18, titlebar_color);
/* Gradient effect (lighter at top) */
if (win->active) {
vga_hline(x + 3, y + 3, w - 6, COLOR_LIGHT_BLUE);
vga_hline(x + 3, y + 4, w - 6, COLOR_LIGHT_BLUE);
}
/* Title text */
if (win->title) {
vga_putstring(x + 8, y + 7, win->title, GUI_COLOR_TITLE_TEXT, titlebar_color);
}
/* Close button (X) */
int btn_width = 16;
int btn_height = 14;
int btn_y = y + 5;
int close_x = x + w - btn_width - 6;
/* Red background for close button */
vga_fillrect(close_x, btn_y, btn_width, btn_height, COLOR_RED);
vga_hline(close_x, btn_y, btn_width, COLOR_LIGHT_RED);
vga_vline(close_x, btn_y, btn_height, COLOR_LIGHT_RED);
vga_hline(close_x, btn_y + btn_height - 1, btn_width, COLOR_BROWN);
vga_vline(close_x + btn_width - 1, btn_y, btn_height, COLOR_BROWN);
/* Draw X */
int cx = close_x + btn_width / 2;
int cy = btn_y + btn_height / 2;
for (int d = -3; d <= 3; d++) {
vga_putpixel(cx + d, cy + d, COLOR_WHITE);
vga_putpixel(cx + d, cy - d, COLOR_WHITE);
vga_putpixel(cx + d + 1, cy + d, COLOR_WHITE);
vga_putpixel(cx + d + 1, cy - d, COLOR_WHITE);
}
}
/* Draw button */
void gui_draw_button(gui_button_t* btn) {
if (!btn->visible) return;
int x = btn->x;
int y = btn->y;
/* Offset for window-relative buttons */
if (btn->window_id >= 0 && btn->window_id < num_windows) {
gui_window_t* win = &windows[btn->window_id];
if (!win->visible) return;
x += win->x;
y += win->y + 16;
}
uint8_t bg_color = GUI_COLOR_BUTTON_BG;
if (btn->pressed) {
bg_color = GUI_COLOR_BUTTON_PRESS;
} else if (btn->hovered) {
bg_color = GUI_COLOR_BUTTON_HOVER;
}
/* Button background */
vga_fillrect(x, y, btn->width, btn->height, bg_color);
/* Button border */
vga_rect(x, y, btn->width, btn->height, GUI_COLOR_BORDER);
/* Button 3D effect */
if (!btn->pressed) {
vga_hline(x + 1, y + 1, btn->width - 2, COLOR_WHITE);
vga_vline(x + 1, y + 1, btn->height - 2, COLOR_WHITE);
vga_hline(x + 1, y + btn->height - 2, btn->width - 2, COLOR_DARK_GRAY);
vga_vline(x + btn->width - 2, y + 1, btn->height - 2, COLOR_DARK_GRAY);
} else {
vga_hline(x + 1, y + 1, btn->width - 2, COLOR_DARK_GRAY);
vga_vline(x + 1, y + 1, btn->height - 2, COLOR_DARK_GRAY);
}
/* Text (centered) */
int text_len = 0;
const char* s = btn->label;
while (*s++) text_len++;
int text_x = x + (btn->width - text_len * 8) / 2;
int text_y = y + (btn->height - 8) / 2;
if (btn->pressed) {
text_x++;
text_y++;
}
vga_putstring(text_x, text_y, btn->label, GUI_COLOR_BUTTON_FG, bg_color);
}
/* Update GUI - handle input */
void gui_update(void) {
int mx = mouse_get_x();
int my = mouse_get_y();
int clicked = mouse_button_clicked(MOUSE_LEFT);
int down = mouse_button_down(MOUSE_LEFT);
int released = mouse_button_released(MOUSE_LEFT);
/* Handle window dragging */
for (int i = num_windows - 1; i >= 0; i--) {
gui_window_t* win = &windows[i];
if (!win->visible) continue;
if (win->dragging) {
if (down) {
win->x = mx - win->drag_offset_x;
win->y = my - win->drag_offset_y;
/* Keep on screen */
if (win->x < 0) win->x = 0;
if (win->y < 13) win->y = 13;
if (win->x + win->width > SCREEN_WIDTH)
win->x = SCREEN_WIDTH - win->width;
if (win->y + win->height > SCREEN_HEIGHT - 32)
win->y = SCREEN_HEIGHT - 32 - win->height;
} else {
win->dragging = 0;
}
return;
}
}
/* Check for window clicks */
for (int i = num_windows - 1; i >= 0; i--) {
gui_window_t* win = &windows[i];
if (!win->visible) continue;
/* Check close button */
int close_x = win->x + win->width - 22;
int close_y = win->y + 5;
if (clicked && point_in_rect(mx, my, close_x, close_y, 16, 14)) {
win->visible = 0;
return;
}
/* Check title bar for dragging */
if (clicked && point_in_rect(mx, my, win->x, win->y, win->width, 20)) {
win->dragging = 1;
win->drag_offset_x = mx - win->x;
win->drag_offset_y = my - win->y;
gui_set_active_window(i);
return;
}
/* Check window body click to activate */
if (clicked && point_in_rect(mx, my, win->x, win->y, win->width, win->height)) {
gui_set_active_window(i);
}
}
/* Update buttons */
for (int i = 0; i < num_buttons; i++) {
gui_button_t* btn = &buttons[i];
if (!btn->visible) continue;
int bx = btn->x;
int by = btn->y;
if (btn->window_id >= 0 && btn->window_id < num_windows) {
gui_window_t* win = &windows[btn->window_id];
if (!win->visible) continue;
bx += win->x;
by += win->y + 16;
}
int hover = point_in_rect(mx, my, bx, by, btn->width, btn->height);
btn->hovered = hover;
if (hover) {
if (clicked) {
btn->pressed = 1;
}
if (released && btn->pressed) {
btn->pressed = 0;
if (btn->callback) {
btn->callback();
}
}
} else {
btn->pressed = 0;
}
}
}
/* Draw entire GUI (windows and buttons only) */
void gui_draw(void) {
/* Draw windows (back to front) */
for (int i = 0; i < num_windows; i++) {
if (i != active_window) {
gui_draw_window(&windows[i]);
}
}
/* Draw active window last (on top) */
if (active_window >= 0 && active_window < num_windows) {
gui_draw_window(&windows[active_window]);
}
/* Draw buttons */
for (int i = 0; i < num_buttons; i++) {
gui_draw_button(&buttons[i]);
}
}