blob: 0e9553048f3ceda0187175299fea8302e26754af [file] [log] [blame]
PulkoMandyc53d0f62014-07-01 22:35:10 +02001/*
2 * Copyright (c) 2002, Adam Dunkels.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided
13 * with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior
16 * written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * This file is part of the "ctk" console GUI toolkit for cc65
31 *
32 * $Id: ctk-conio-service.c,v 1.12 2006/05/28 20:38:19 oliverschmidt Exp $
33 *
34 */
35
36#include <conio.h>
37
38#include "ctk.h"
39#include "ctk-draw.h"
40
41#include "ctk-draw-service.h"
42
43#include "ctk-conio-conf.h"
44#include <string.h>
45
46#ifndef NULL
47#define NULL (void *)0
48#endif /* NULL */
49
50static unsigned char sizex, sizey;
51
52/*unsigned char ctk_draw_windowborder_height = 1;
53unsigned char ctk_draw_windowborder_width = 1;
54unsigned char ctk_draw_windowtitle_height = 1;*/
55
56// The revers function does nothing in our conio driver anyway.
57#define revers(x)
58
59/*-----------------------------------------------------------------------------------*/
60static void
PulkoMandyc53d0f62014-07-01 22:35:10 +020061s_ctk_draw_init(void)
62{
63 (void)bgcolor(SCREENCOLOR);
64 (void)bordercolor(BORDERCOLOR);
PulkoMandy25ff0502014-07-03 09:55:24 +020065 clrscr();
PulkoMandyc53d0f62014-07-01 22:35:10 +020066 screensize(&sizex, &sizey);
PulkoMandyc53d0f62014-07-01 22:35:10 +020067}
68/*-----------------------------------------------------------------------------------*/
PulkoMandydf52c592014-07-03 15:36:50 +020069
70
71static void customchr(unsigned char* data) __naked
72{
73 __asm
74 pop de
75 pop hl
76 push hl
77 push de
PulkoMandy0627a362014-07-03 18:55:34 +020078 ; Can't use SCR SET MATRIX because some of our icons are in RAM under 0x4000.
79 ; SCR SET MATRIX then gets data from the firmware ROM...
80 ld a,#0x19
81 call 0xBB5a
PulkoMandydf52c592014-07-03 15:36:50 +020082 ld a,#0xff
PulkoMandy0627a362014-07-03 18:55:34 +020083 call 0xBB5a
84 ld b,#8
8500001$:
86 ld a,(hl)
87 call 0xbb5a
88 inc hl
89 djnz 00001$
90 ret
PulkoMandydf52c592014-07-03 15:36:50 +020091 __endasm;
92}
93
94
PulkoMandyc53d0f62014-07-01 22:35:10 +020095static void
96draw_widget(struct ctk_widget *w,
97 unsigned char x, unsigned char y,
98 unsigned char clipx,
99 unsigned char clipy,
100 unsigned char clipy1, unsigned char clipy2,
101 unsigned char focus)
102{
103 unsigned char xpos, ypos, xscroll;
104 unsigned char i, j;
105 char c, *text;
106 unsigned char len, wfocus;
107
108 wfocus = 0;
109 if(focus & CTK_FOCUS_WINDOW) {
110 (void)textcolor(WIDGETCOLOR_FWIN);
111 if(focus & CTK_FOCUS_WIDGET) {
112 (void)textcolor(WIDGETCOLOR_FOCUS);
113 wfocus = 1;
114 }
115 } else if(focus & CTK_FOCUS_DIALOG) {
116 (void)textcolor(WIDGETCOLOR_DIALOG);
117 if(focus & CTK_FOCUS_WIDGET) {
118 (void)textcolor(WIDGETCOLOR_FOCUS);
119 wfocus = 1;
120 }
121 } else {
122 (void)textcolor(WIDGETCOLOR);
123 }
124
125 xpos = x + w->x;
126 ypos = y + w->y;
127
128 switch(w->type) {
129 case CTK_WIDGET_SEPARATOR:
130 if(ypos >= clipy1 && ypos < clipy2) {
131 chlinexy(xpos, ypos, w->w);
132 }
133 break;
134 case CTK_WIDGET_LABEL:
135 text = w->widget.label.text;
136 for(i = 0; i < w->h; ++i) {
137 if(ypos >= clipy1 && ypos < clipy2) {
138 gotoxy(xpos, ypos);
139 cputsn(text, w->w);
140 if(w->w - (wherex() - xpos) > 0) {
141 cclear(w->w - (wherex() - xpos));
142 }
143 }
144 ++ypos;
145 text += w->w;
146 }
147 break;
148 case CTK_WIDGET_BUTTON:
149 if(ypos >= clipy1 && ypos < clipy2) {
150 revers(wfocus != 0);
151 cputcxy(xpos, ypos, '[');
152 cputsn(w->widget.button.text, w->w);
153 cputc(']');
154 revers(0);
155 }
156 break;
157 case CTK_WIDGET_HYPERLINK:
158 if(ypos >= clipy1 && ypos < clipy2) {
159 (void)textcolor(WIDGETCOLOR_HLINK);
160 revers(wfocus == 0);
161 gotoxy(xpos, ypos);
162 cputsn(w->widget.button.text, w->w);
163 revers(0);
164 }
165 break;
166 case CTK_WIDGET_TEXTENTRY:
167 text = w->widget.textentry.text;
168 xscroll = 0;
169 if(w->widget.textentry.xpos >= w->w - 1) {
170 xscroll = w->widget.textentry.xpos - w->w + 1;
171 }
172 for(j = 0; j < w->h; ++j) {
173 if(ypos >= clipy1 && ypos < clipy2) {
174 if(w->widget.textentry.state == CTK_TEXTENTRY_EDIT &&
175 w->widget.textentry.ypos == j) {
176 revers(0);
177 cputcxy(xpos, ypos, '>');
178 c = 1;
179 for(i = 0; i < w->w; ++i) {
180 if(c != 0) {
181 c = text[i + xscroll];
182 }
183 revers(i == w->widget.textentry.xpos - xscroll);
184 if(c == 0) {
185 cputc(' ');
186 } else {
187 cputc(c);
188 }
189 }
190 revers(0);
191 cputc('<');
192 } else {
193 revers(wfocus != 0 && j == w->widget.textentry.ypos);
194 cvlinexy(xpos, ypos, 1);
195 gotoxy(xpos + 1, ypos);
196 cputsn(text, w->w);
197 i = wherex();
198 if(i - xpos - 1 < w->w) {
199 cclear(w->w - (i - xpos) + 1);
200 }
201 cvline(1);
202 }
203 }
204 ++ypos;
205 text += w->widget.textentry.len + 1;
206 }
207 revers(0);
208 break;
209 case CTK_WIDGET_ICON:
PulkoMandydf52c592014-07-03 15:36:50 +0200210
PulkoMandyc53d0f62014-07-01 22:35:10 +0200211 if(ypos >= clipy1 && ypos < clipy2) {
212 revers(wfocus != 0);
PulkoMandydf52c592014-07-03 15:36:50 +0200213#if CTK_CONF_ICON_BITMAPS
214 gotoxy(xpos, ypos);
215 if(w->widget.icon.bitmap != NULL) {
216 unsigned char* ptr = w->widget.icon.bitmap;
PulkoMandydf52c592014-07-03 15:36:50 +0200217 for(i = 0; i < 3; ++i) {
218 gotoxy(xpos, ypos);
219 if(ypos >= clipy1 && ypos < clipy2) {
220 customchr(ptr);
221 cputc(0xff);
222 ptr += 8;
223 customchr(ptr);
224 cputc(0xff);
225 ptr += 8;
226 customchr(ptr);
227 cputc(0xff);
228 ptr += 8;
229 }
230 ++ypos;
231 }
232 }
233#elif CTK_CONF_ICON_TEXTMAPS
PulkoMandyc53d0f62014-07-01 22:35:10 +0200234 gotoxy(xpos, ypos);
235 if(w->widget.icon.textmap != NULL) {
236 for(i = 0; i < 3; ++i) {
237 gotoxy(xpos, ypos);
238 if(ypos >= clipy1 && ypos < clipy2) {
239 cputc(w->widget.icon.textmap[0 + 3 * i]);
240 cputc(w->widget.icon.textmap[1 + 3 * i]);
241 cputc(w->widget.icon.textmap[2 + 3 * i]);
242 }
243 ++ypos;
244 }
245 }
PulkoMandydf52c592014-07-03 15:36:50 +0200246#endif
PulkoMandyc53d0f62014-07-01 22:35:10 +0200247 x = xpos;
248
249 len = strlen(w->widget.icon.title);
250 if(x + len >= sizex) {
251 x = sizex - len;
252 }
253
254 gotoxy(x, ypos);
255 if(ypos >= clipy1 && ypos < clipy2) {
256 cputs(w->widget.icon.title);
257 }
258 revers(0);
259 }
260 break;
261
262 default:
263 break;
264 }
265}
266/*-----------------------------------------------------------------------------------*/
267static void
268s_ctk_draw_widget(struct ctk_widget *w,
269 unsigned char focus,
270 unsigned char clipy1,
271 unsigned char clipy2)
272{
273 struct ctk_window *win = w->window;
274 unsigned char posx, posy;
275
276 posx = win->x + 1;
277 posy = win->y + 2;
278
279 if(w == win->focused) {
280 focus |= CTK_FOCUS_WIDGET;
281 }
282
283 draw_widget(w, posx, posy,
284 posx + win->w,
285 posy + win->h,
286 clipy1, clipy2,
287 focus);
288
289#ifdef CTK_CONIO_CONF_UPDATE
290 CTK_CONIO_CONF_UPDATE();
291#endif /* CTK_CONIO_CONF_UPDATE */
292}
293/*-----------------------------------------------------------------------------------*/
294
Adrien Destugues9333c982016-01-23 18:38:47 +0100295static void clearrect(unsigned char y2, unsigned char x2,
296 unsigned char y1, unsigned char x1) __naked
PulkoMandyc53d0f62014-07-01 22:35:10 +0200297{
298 __asm
Adrien Destugues9333c982016-01-23 18:38:47 +0100299 pop bc ; RV
300 pop de ; x2 y2
301 pop hl ; x1 y1
302 push hl
303 push de
304 push bc
PulkoMandyc53d0f62014-07-01 22:35:10 +0200305
306 call 0xBB99 ; TXT GET PAPER
307 call 0xBC2C ; SCR INK ENCODE
308
309 jp 0xBC44 ; SCR FILL BOX
310 __endasm;
311}
312
313static void
314s_ctk_draw_clear_window(struct ctk_window *window,
315 unsigned char focus,
316 unsigned char clipy1,
317 unsigned char clipy2)
318{
319 unsigned char i;
320 unsigned char h;
321
322 /*
323 if(focus & CTK_FOCUS_WINDOW) {
324 (void)textcolor(WINDOWCOLOR_FOCUS);
325 } else {
326 (void)textcolor(WINDOWCOLOR);
327 }
328 */
329
PulkoMandy25ff0502014-07-03 09:55:24 +0200330 i = window->y + 2; // +1 for the border, +1 for ctk > cpc conversion
331 h = i + window->h;
PulkoMandyc53d0f62014-07-01 22:35:10 +0200332
PulkoMandy0627a362014-07-03 18:55:34 +0200333 if (i >= clipy2 || h < clipy1)
334 return;
335
PulkoMandy25ff0502014-07-03 09:55:24 +0200336 if (i < clipy1) i = clipy1;
Adrien Destugues9333c982016-01-23 18:38:47 +0100337 if (h >= clipy2) h = clipy2 - 1;
PulkoMandy25ff0502014-07-03 09:55:24 +0200338
Adrien Destugues9333c982016-01-23 18:38:47 +0100339 clearrect(h, window->x + window->w, i, window->x + 1);
PulkoMandyc53d0f62014-07-01 22:35:10 +0200340}
341/*-----------------------------------------------------------------------------------*/
342static void
343draw_window_contents(struct ctk_window *window, unsigned char focus,
344 unsigned char clipy1, unsigned char clipy2,
345 unsigned char x1, unsigned char x2,
346 unsigned char y1, unsigned char y2)
347{
348 struct ctk_widget *w;
349 unsigned char wfocus;
350
351 /* Draw inactive widgets. */
352 for(w = window->inactive; w != NULL; w = w->next) {
353 draw_widget(w, x1, y1, x2, y2,
354 clipy1, clipy2,
355 focus);
356 }
357
358 /* Draw active widgets. */
359 for(w = window->active; w != NULL; w = w->next) {
360 wfocus = focus;
361 if(w == window->focused) {
362 wfocus |= CTK_FOCUS_WIDGET;
363 }
364
365 draw_widget(w, x1, y1, x2, y2,
366 clipy1, clipy2,
367 wfocus);
368 }
369
370#ifdef CTK_CONIO_CONF_UPDATE
371 CTK_CONIO_CONF_UPDATE();
372#endif /* CTK_CONIO_CONF_UPDATE */
373}
374/*-----------------------------------------------------------------------------------*/
375static void
376s_ctk_draw_window(struct ctk_window *window, unsigned char focus,
377 unsigned char clipy1, unsigned char clipy2,
378 unsigned char draw_borders)
379{
380 unsigned char x, y;
381 unsigned char h;
382 unsigned char x1, y1, x2, y2;
383
384 if(window->y + 1 >= clipy2) {
385 return;
386 }
387
388 x = window->x;
389 y = window->y + 1;
390 x1 = x + 1;
391 y1 = y + 1;
392 x2 = x1 + window->w;
393 y2 = y1 + window->h;
394
395 if(draw_borders) {
396
397 /* Draw window frame. */
398 if(focus & CTK_FOCUS_WINDOW) {
399 (void)textcolor(WINDOWCOLOR_FOCUS);
400 } else {
401 (void)textcolor(WINDOWCOLOR);
402 }
403
404 if(y >= clipy1) {
405 cputcxy(x, y, CH_ULCORNER);
406 gotoxy(wherex() + window->titlelen + CTK_CONF_WINDOWMOVE * 2, wherey());
407 chline(window->w - (wherex() - x) - 2);
408 cputcxy(x2, y, CH_URCORNER);
409 }
410
411 h = window->h;
412
413 if(clipy1 > y1) {
414 if(clipy1 - y1 < h) {
415 h = clipy1 - y1;
416 y1 = clipy1;
417 } else {
418 h = 0;
419 }
420 }
421
422 if(clipy2 < y1 + h) {
423 if(y1 >= clipy2) {
424 h = 0;
425 } else {
426 h = clipy2 - y1;
427 }
428 }
429
430 cvlinexy(x, y1, h);
431 cvlinexy(x2, y1, h);
432
433 if(y + window->h >= clipy1 &&
434 y + window->h < clipy2) {
435 cputcxy(x, y2, CH_LLCORNER);
436 chlinexy(x1, y2, window->w);
437 cputcxy(x2, y2, CH_LRCORNER);
438 }
439 }
440
441 draw_window_contents(window, focus, clipy1, clipy2,
442 x1, x2, y + 1, y2);
443}
444/*-----------------------------------------------------------------------------------*/
445static void
446s_ctk_draw_dialog(struct ctk_window *dialog)
447{
448 unsigned char x, y;
449 unsigned char i;
450 unsigned char x1, y1, x2, y2;
451
452 (void)textcolor(DIALOGCOLOR);
453
454 x = dialog->x;
455 y = dialog->y + 1;
456
457 x1 = x + 1;
458 y1 = y + 1;
459 x2 = x1 + dialog->w;
460 y2 = y1 + dialog->h;
461
462 /* Draw dialog frame. */
463 cvlinexy(x, y1,
464 dialog->h);
465 cvlinexy(x2, y1,
466 dialog->h);
467
468 chlinexy(x1, y,
469 dialog->w);
470 chlinexy(x1, y2,
471 dialog->w);
472
473 cputcxy(x, y, CH_ULCORNER);
474 cputcxy(x, y2, CH_LLCORNER);
475 cputcxy(x2, y, CH_URCORNER);
476 cputcxy(x2, y2, CH_LRCORNER);
477
478 /* Clear dialog contents. */
479 for(i = y1; i < y2; ++i) {
480 cclearxy(x1, i, dialog->w);
481 }
482
483 draw_window_contents(dialog, CTK_FOCUS_DIALOG, 0, sizey,
484 x1, x2, y1, y2);
485}
486/*-----------------------------------------------------------------------------------*/
487static void
488s_ctk_draw_clear(unsigned char y1, unsigned char y2) __naked
489{
490 __asm
491 ld hl,#2
492 add hl,sp
493 ld d,(hl) ; Y1
494 inc hl
495 ld e,(hl) ; Y2
496
497 push af
498
499 ld h,#1
500 ld l,d
501 ld d,#40
502
PulkoMandy0627a362014-07-03 18:55:34 +0200503 dec e
504 ;inc e
PulkoMandyc53d0f62014-07-01 22:35:10 +0200505
506 call 0xBB99 ; TXT GET PAPER
507 call 0xBC2C ; SCR INK ENCODE
508
509 call 0xBC44 ; SCR FILL BOX
510
511 pop af
512 ret
513 __endasm;
514}
515/*-----------------------------------------------------------------------------------*/
516static void
517draw_menu(struct ctk_menu *m, unsigned char open)
518{
519 unsigned char x, x2, y;
520
521 if(open) {
522 x = x2 = wherex();
523 if(x2 + CTK_CONF_MENUWIDTH > sizex) {
524 x2 = sizex - CTK_CONF_MENUWIDTH;
525 }
526
527 for(y = 0; y < m->nitems; ++y) {
528 if(y == m->active) {
529 (void)textcolor(ACTIVEMENUITEMCOLOR);
530 } else {
531 (void)textcolor(SCREENCOLOR);
532 }
533 gotoxy(x2, y + 1);
534 if(m->items[y].title[0] == '-') {
535 chline(CTK_CONF_MENUWIDTH);
536 } else {
537 cputs(m->items[y].title);
538 }
539 if(x2 + CTK_CONF_MENUWIDTH > wherex()) {
540 cclear(x2 + CTK_CONF_MENUWIDTH - wherex());
541 }
542 }
543
544 gotoxy(x, 0);
545 (void)textcolor(OPENMENUCOLOR);
546 }
547
548 cputs(m->title);
549 cputc(' ');
550}
551/*-----------------------------------------------------------------------------------*/
552static void
553s_ctk_draw_menus(struct ctk_menus *menus)
554{
555 struct ctk_menu *m;
556
557 /* Draw menus */
558 (void)bgcolor(MENUCOLOR);
559 (void)textcolor(SCREENCOLOR);
560 gotoxy(0, 0);
561 cputc(' ');
562 for(m = menus->menus->next; m != NULL; m = m->next) {
563 draw_menu(m, m == menus->open);
564 }
565
566 /* Draw desktopmenu */
567 if(wherex() + strlen(menus->desktopmenu->title) + 1 >= sizex) {
568 gotoxy(sizex - strlen(menus->desktopmenu->title) - 1, 0);
569 } else {
570 cclear(sizex - wherex() -
571 strlen(menus->desktopmenu->title) - 1);
572 }
573 draw_menu(menus->desktopmenu, menus->desktopmenu == menus->open);
574
575 (void)bgcolor(SCREENCOLOR);
576}
577/*-----------------------------------------------------------------------------------*/
578static unsigned char
579s_ctk_draw_height(void)
580{
581 return sizey;
582}
583/*-----------------------------------------------------------------------------------*/
584static unsigned char
585s_ctk_draw_width(void)
586{
587 return sizex;
588}
589/*-----------------------------------------------------------------------------------*/
590static unsigned short
591s_ctk_mouse_xtoc(unsigned short x)
592{
593 return x / 8;
594}
595/*-----------------------------------------------------------------------------------*/
596static unsigned short
597s_ctk_mouse_ytoc(unsigned short y)
598{
599 return y / 8;
600}
601/*-----------------------------------------------------------------------------------*/
602static const struct ctk_draw_service_interface interface =
603 {CTK_DRAW_SERVICE_VERSION,
604 1,
605 1,
606 1,
607 s_ctk_draw_init,
608 s_ctk_draw_clear,
609 s_ctk_draw_clear_window,
610 s_ctk_draw_window,
611 s_ctk_draw_dialog,
612 s_ctk_draw_widget,
613 s_ctk_draw_menus,
614 s_ctk_draw_width,
615 s_ctk_draw_height,
616 s_ctk_mouse_xtoc,
617 s_ctk_mouse_ytoc,
618 };
619
620EK_EVENTHANDLER(eventhandler, ev, data);
621EK_PROCESS(proc, CTK_DRAW_SERVICE_NAME ": text", EK_PRIO_NORMAL,
622 eventhandler, NULL, (void *)&interface);
623
624/*--------------------------------------------------------------------------*/
625LOADER_INIT_FUNC(ctk_conio_service_init, arg)
626{
627 s_ctk_draw_init();
628 ek_service_start(CTK_DRAW_SERVICE_NAME, &proc);
629}
630/*--------------------------------------------------------------------------*/
631EK_EVENTHANDLER(eventhandler, ev, data)
632{
633 EK_EVENTHANDLER_ARGS(ev, data);
634
635 switch(ev) {
636 case EK_EVENT_INIT:
637 case EK_EVENT_REPLACE:
638 s_ctk_draw_init();
639 ctk_restore();
640 break;
641 case EK_EVENT_REQUEST_REPLACE:
642 ek_replace((struct ek_proc *)data, NULL);
643 LOADER_UNLOAD();
644 break;
645 }
646}
647/*--------------------------------------------------------------------------*/