blob: f90c5b860b7bd1283386cf80e6b65446da24c3b2 [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
Adrien Destugues69a6e722017-06-03 10:17:17 +020071static void customchr(const unsigned char* data) __naked __z88dk_callee
PulkoMandydf52c592014-07-03 15:36:50 +020072{
73 __asm
74 pop de
75 pop hl
PulkoMandydf52c592014-07-03 15:36:50 +020076 push de
Adrien Destugues69a6e722017-06-03 10:17:17 +020077 ; Cant use SCR SET MATRIX because some of our icons are in RAM under 0x4000.
PulkoMandy0627a362014-07-03 18:55:34 +020078 ; SCR SET MATRIX then gets data from the firmware ROM...
79 ld a,#0x19
80 call 0xBB5a
PulkoMandydf52c592014-07-03 15:36:50 +020081 ld a,#0xff
PulkoMandy0627a362014-07-03 18:55:34 +020082 call 0xBB5a
83 ld b,#8
8400001$:
85 ld a,(hl)
86 call 0xbb5a
87 inc hl
88 djnz 00001$
89 ret
PulkoMandydf52c592014-07-03 15:36:50 +020090 __endasm;
91}
92
93
PulkoMandyc53d0f62014-07-01 22:35:10 +020094static void
95draw_widget(struct ctk_widget *w,
96 unsigned char x, unsigned char y,
97 unsigned char clipx,
98 unsigned char clipy,
99 unsigned char clipy1, unsigned char clipy2,
100 unsigned char focus)
101{
102 unsigned char xpos, ypos, xscroll;
103 unsigned char i, j;
Adrien Destugues69a6e722017-06-03 10:17:17 +0200104 char c;
105 const char* text;
PulkoMandyc53d0f62014-07-01 22:35:10 +0200106 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) {
Adrien Destugues69a6e722017-06-03 10:17:17 +0200216 const 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;
PulkoMandyc53d0f62014-07-01 22:35:10 +0200449 unsigned char x1, y1, x2, y2;
450
451 (void)textcolor(DIALOGCOLOR);
452
453 x = dialog->x;
454 y = dialog->y + 1;
455
456 x1 = x + 1;
457 y1 = y + 1;
458 x2 = x1 + dialog->w;
459 y2 = y1 + dialog->h;
460
461 /* Draw dialog frame. */
462 cvlinexy(x, y1,
463 dialog->h);
464 cvlinexy(x2, y1,
465 dialog->h);
466
467 chlinexy(x1, y,
468 dialog->w);
469 chlinexy(x1, y2,
470 dialog->w);
471
472 cputcxy(x, y, CH_ULCORNER);
473 cputcxy(x, y2, CH_LLCORNER);
474 cputcxy(x2, y, CH_URCORNER);
475 cputcxy(x2, y2, CH_LRCORNER);
476
477 /* Clear dialog contents. */
Adrien Destugues715554f2016-01-23 19:48:57 +0100478 clearrect(y2 - 1, x1 + dialog->w - 1, y1, x1);
PulkoMandyc53d0f62014-07-01 22:35:10 +0200479
480 draw_window_contents(dialog, CTK_FOCUS_DIALOG, 0, sizey,
481 x1, x2, y1, y2);
482}
483/*-----------------------------------------------------------------------------------*/
484static void
485s_ctk_draw_clear(unsigned char y1, unsigned char y2) __naked
486{
487 __asm
Adrien Destugues715554f2016-01-23 19:48:57 +0100488 pop de
489 pop hl
490 push hl
491 push de
PulkoMandyc53d0f62014-07-01 22:35:10 +0200492
493 push af
494
Adrien Destugues715554f2016-01-23 19:48:57 +0100495 ld e,h
PulkoMandyc53d0f62014-07-01 22:35:10 +0200496 ld h,#1
PulkoMandyc53d0f62014-07-01 22:35:10 +0200497 ld d,#40
498
PulkoMandy0627a362014-07-03 18:55:34 +0200499 dec e
PulkoMandyc53d0f62014-07-01 22:35:10 +0200500
501 call 0xBB99 ; TXT GET PAPER
502 call 0xBC2C ; SCR INK ENCODE
503
504 call 0xBC44 ; SCR FILL BOX
505
506 pop af
507 ret
508 __endasm;
509}
510/*-----------------------------------------------------------------------------------*/
511static void
512draw_menu(struct ctk_menu *m, unsigned char open)
513{
514 unsigned char x, x2, y;
515
516 if(open) {
517 x = x2 = wherex();
518 if(x2 + CTK_CONF_MENUWIDTH > sizex) {
519 x2 = sizex - CTK_CONF_MENUWIDTH;
520 }
521
522 for(y = 0; y < m->nitems; ++y) {
523 if(y == m->active) {
524 (void)textcolor(ACTIVEMENUITEMCOLOR);
525 } else {
526 (void)textcolor(SCREENCOLOR);
527 }
528 gotoxy(x2, y + 1);
529 if(m->items[y].title[0] == '-') {
530 chline(CTK_CONF_MENUWIDTH);
531 } else {
532 cputs(m->items[y].title);
533 }
534 if(x2 + CTK_CONF_MENUWIDTH > wherex()) {
535 cclear(x2 + CTK_CONF_MENUWIDTH - wherex());
536 }
537 }
538
539 gotoxy(x, 0);
540 (void)textcolor(OPENMENUCOLOR);
541 }
542
543 cputs(m->title);
544 cputc(' ');
545}
546/*-----------------------------------------------------------------------------------*/
547static void
548s_ctk_draw_menus(struct ctk_menus *menus)
549{
550 struct ctk_menu *m;
551
552 /* Draw menus */
553 (void)bgcolor(MENUCOLOR);
554 (void)textcolor(SCREENCOLOR);
555 gotoxy(0, 0);
556 cputc(' ');
557 for(m = menus->menus->next; m != NULL; m = m->next) {
558 draw_menu(m, m == menus->open);
559 }
560
561 /* Draw desktopmenu */
562 if(wherex() + strlen(menus->desktopmenu->title) + 1 >= sizex) {
563 gotoxy(sizex - strlen(menus->desktopmenu->title) - 1, 0);
564 } else {
565 cclear(sizex - wherex() -
566 strlen(menus->desktopmenu->title) - 1);
567 }
568 draw_menu(menus->desktopmenu, menus->desktopmenu == menus->open);
569
570 (void)bgcolor(SCREENCOLOR);
571}
572/*-----------------------------------------------------------------------------------*/
573static unsigned char
574s_ctk_draw_height(void)
575{
576 return sizey;
577}
578/*-----------------------------------------------------------------------------------*/
579static unsigned char
580s_ctk_draw_width(void)
581{
582 return sizex;
583}
584/*-----------------------------------------------------------------------------------*/
585static unsigned short
586s_ctk_mouse_xtoc(unsigned short x)
587{
588 return x / 8;
589}
590/*-----------------------------------------------------------------------------------*/
591static unsigned short
592s_ctk_mouse_ytoc(unsigned short y)
593{
594 return y / 8;
595}
596/*-----------------------------------------------------------------------------------*/
597static const struct ctk_draw_service_interface interface =
598 {CTK_DRAW_SERVICE_VERSION,
599 1,
600 1,
601 1,
602 s_ctk_draw_init,
603 s_ctk_draw_clear,
604 s_ctk_draw_clear_window,
605 s_ctk_draw_window,
606 s_ctk_draw_dialog,
607 s_ctk_draw_widget,
608 s_ctk_draw_menus,
609 s_ctk_draw_width,
610 s_ctk_draw_height,
611 s_ctk_mouse_xtoc,
612 s_ctk_mouse_ytoc,
613 };
614
615EK_EVENTHANDLER(eventhandler, ev, data);
616EK_PROCESS(proc, CTK_DRAW_SERVICE_NAME ": text", EK_PRIO_NORMAL,
617 eventhandler, NULL, (void *)&interface);
618
619/*--------------------------------------------------------------------------*/
620LOADER_INIT_FUNC(ctk_conio_service_init, arg)
621{
622 s_ctk_draw_init();
623 ek_service_start(CTK_DRAW_SERVICE_NAME, &proc);
624}
625/*--------------------------------------------------------------------------*/
626EK_EVENTHANDLER(eventhandler, ev, data)
627{
628 EK_EVENTHANDLER_ARGS(ev, data);
629
630 switch(ev) {
631 case EK_EVENT_INIT:
632 case EK_EVENT_REPLACE:
633 s_ctk_draw_init();
634 ctk_restore();
635 break;
636 case EK_EVENT_REQUEST_REPLACE:
637 ek_replace((struct ek_proc *)data, NULL);
638 LOADER_UNLOAD();
639 break;
640 }
641}
642/*--------------------------------------------------------------------------*/