blob: bd68e5fbf4aa229fcc1353c538c5ef116c8d8adb [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
61cputsn(char *str, unsigned char len)
62{
63 char c;
64
65 while(len > 0) {
66 --len;
67 c = *str;
68 if(c == 0) {
69 break;
70 }
71 cputc(c);
72 ++str;
73 }
74}
75/*-----------------------------------------------------------------------------------*/
76static void
77s_ctk_draw_init(void)
78{
79 (void)bgcolor(SCREENCOLOR);
80 (void)bordercolor(BORDERCOLOR);
PulkoMandy25ff0502014-07-03 09:55:24 +020081 clrscr();
PulkoMandyc53d0f62014-07-01 22:35:10 +020082 screensize(&sizex, &sizey);
PulkoMandyc53d0f62014-07-01 22:35:10 +020083}
84/*-----------------------------------------------------------------------------------*/
85static void
86draw_widget(struct ctk_widget *w,
87 unsigned char x, unsigned char y,
88 unsigned char clipx,
89 unsigned char clipy,
90 unsigned char clipy1, unsigned char clipy2,
91 unsigned char focus)
92{
93 unsigned char xpos, ypos, xscroll;
94 unsigned char i, j;
95 char c, *text;
96 unsigned char len, wfocus;
97
98 wfocus = 0;
99 if(focus & CTK_FOCUS_WINDOW) {
100 (void)textcolor(WIDGETCOLOR_FWIN);
101 if(focus & CTK_FOCUS_WIDGET) {
102 (void)textcolor(WIDGETCOLOR_FOCUS);
103 wfocus = 1;
104 }
105 } else if(focus & CTK_FOCUS_DIALOG) {
106 (void)textcolor(WIDGETCOLOR_DIALOG);
107 if(focus & CTK_FOCUS_WIDGET) {
108 (void)textcolor(WIDGETCOLOR_FOCUS);
109 wfocus = 1;
110 }
111 } else {
112 (void)textcolor(WIDGETCOLOR);
113 }
114
115 xpos = x + w->x;
116 ypos = y + w->y;
117
118 switch(w->type) {
119 case CTK_WIDGET_SEPARATOR:
120 if(ypos >= clipy1 && ypos < clipy2) {
121 chlinexy(xpos, ypos, w->w);
122 }
123 break;
124 case CTK_WIDGET_LABEL:
125 text = w->widget.label.text;
126 for(i = 0; i < w->h; ++i) {
127 if(ypos >= clipy1 && ypos < clipy2) {
128 gotoxy(xpos, ypos);
129 cputsn(text, w->w);
130 if(w->w - (wherex() - xpos) > 0) {
131 cclear(w->w - (wherex() - xpos));
132 }
133 }
134 ++ypos;
135 text += w->w;
136 }
137 break;
138 case CTK_WIDGET_BUTTON:
139 if(ypos >= clipy1 && ypos < clipy2) {
140 revers(wfocus != 0);
141 cputcxy(xpos, ypos, '[');
142 cputsn(w->widget.button.text, w->w);
143 cputc(']');
144 revers(0);
145 }
146 break;
147 case CTK_WIDGET_HYPERLINK:
148 if(ypos >= clipy1 && ypos < clipy2) {
149 (void)textcolor(WIDGETCOLOR_HLINK);
150 revers(wfocus == 0);
151 gotoxy(xpos, ypos);
152 cputsn(w->widget.button.text, w->w);
153 revers(0);
154 }
155 break;
156 case CTK_WIDGET_TEXTENTRY:
157 text = w->widget.textentry.text;
158 xscroll = 0;
159 if(w->widget.textentry.xpos >= w->w - 1) {
160 xscroll = w->widget.textentry.xpos - w->w + 1;
161 }
162 for(j = 0; j < w->h; ++j) {
163 if(ypos >= clipy1 && ypos < clipy2) {
164 if(w->widget.textentry.state == CTK_TEXTENTRY_EDIT &&
165 w->widget.textentry.ypos == j) {
166 revers(0);
167 cputcxy(xpos, ypos, '>');
168 c = 1;
169 for(i = 0; i < w->w; ++i) {
170 if(c != 0) {
171 c = text[i + xscroll];
172 }
173 revers(i == w->widget.textentry.xpos - xscroll);
174 if(c == 0) {
175 cputc(' ');
176 } else {
177 cputc(c);
178 }
179 }
180 revers(0);
181 cputc('<');
182 } else {
183 revers(wfocus != 0 && j == w->widget.textentry.ypos);
184 cvlinexy(xpos, ypos, 1);
185 gotoxy(xpos + 1, ypos);
186 cputsn(text, w->w);
187 i = wherex();
188 if(i - xpos - 1 < w->w) {
189 cclear(w->w - (i - xpos) + 1);
190 }
191 cvline(1);
192 }
193 }
194 ++ypos;
195 text += w->widget.textentry.len + 1;
196 }
197 revers(0);
198 break;
199 case CTK_WIDGET_ICON:
200 if(ypos >= clipy1 && ypos < clipy2) {
201 revers(wfocus != 0);
202 gotoxy(xpos, ypos);
203 if(w->widget.icon.textmap != NULL) {
204 for(i = 0; i < 3; ++i) {
205 gotoxy(xpos, ypos);
206 if(ypos >= clipy1 && ypos < clipy2) {
207 cputc(w->widget.icon.textmap[0 + 3 * i]);
208 cputc(w->widget.icon.textmap[1 + 3 * i]);
209 cputc(w->widget.icon.textmap[2 + 3 * i]);
210 }
211 ++ypos;
212 }
213 }
214 x = xpos;
215
216 len = strlen(w->widget.icon.title);
217 if(x + len >= sizex) {
218 x = sizex - len;
219 }
220
221 gotoxy(x, ypos);
222 if(ypos >= clipy1 && ypos < clipy2) {
223 cputs(w->widget.icon.title);
224 }
225 revers(0);
226 }
227 break;
228
229 default:
230 break;
231 }
232}
233/*-----------------------------------------------------------------------------------*/
234static void
235s_ctk_draw_widget(struct ctk_widget *w,
236 unsigned char focus,
237 unsigned char clipy1,
238 unsigned char clipy2)
239{
240 struct ctk_window *win = w->window;
241 unsigned char posx, posy;
242
243 posx = win->x + 1;
244 posy = win->y + 2;
245
246 if(w == win->focused) {
247 focus |= CTK_FOCUS_WIDGET;
248 }
249
250 draw_widget(w, posx, posy,
251 posx + win->w,
252 posy + win->h,
253 clipy1, clipy2,
254 focus);
255
256#ifdef CTK_CONIO_CONF_UPDATE
257 CTK_CONIO_CONF_UPDATE();
258#endif /* CTK_CONIO_CONF_UPDATE */
259}
260/*-----------------------------------------------------------------------------------*/
261
PulkoMandy25ff0502014-07-03 09:55:24 +0200262static void clearrect(unsigned char x2, unsigned char y2,
263 unsigned char x1, unsigned char y1)
PulkoMandyc53d0f62014-07-01 22:35:10 +0200264{
265 __asm
266 ld hl,#2
267 add hl,sp
268 ld d,(hl) ; X1
269 inc hl
270 ld e,(hl) ; Y1
271 inc hl
272 ld a,(hl) ; X2
273 inc hl
274 ld l,(hl) ; Y2
275
276 ld h,a
277
278 call 0xBB99 ; TXT GET PAPER
279 call 0xBC2C ; SCR INK ENCODE
280
281 jp 0xBC44 ; SCR FILL BOX
282 __endasm;
283}
284
285static void
286s_ctk_draw_clear_window(struct ctk_window *window,
287 unsigned char focus,
288 unsigned char clipy1,
289 unsigned char clipy2)
290{
291 unsigned char i;
292 unsigned char h;
293
294 /*
295 if(focus & CTK_FOCUS_WINDOW) {
296 (void)textcolor(WINDOWCOLOR_FOCUS);
297 } else {
298 (void)textcolor(WINDOWCOLOR);
299 }
300 */
301
PulkoMandy25ff0502014-07-03 09:55:24 +0200302 i = window->y + 2; // +1 for the border, +1 for ctk > cpc conversion
303 h = i + window->h;
PulkoMandyc53d0f62014-07-01 22:35:10 +0200304
PulkoMandy25ff0502014-07-03 09:55:24 +0200305 if (i < clipy1) i = clipy1;
306 --clipy2;
307 if (h > clipy2) h = clipy2;
308
309 clearrect(window->x + window->w, h, window->x + 1, i);
PulkoMandyc53d0f62014-07-01 22:35:10 +0200310}
311/*-----------------------------------------------------------------------------------*/
312static void
313draw_window_contents(struct ctk_window *window, unsigned char focus,
314 unsigned char clipy1, unsigned char clipy2,
315 unsigned char x1, unsigned char x2,
316 unsigned char y1, unsigned char y2)
317{
318 struct ctk_widget *w;
319 unsigned char wfocus;
320
321 /* Draw inactive widgets. */
322 for(w = window->inactive; w != NULL; w = w->next) {
323 draw_widget(w, x1, y1, x2, y2,
324 clipy1, clipy2,
325 focus);
326 }
327
328 /* Draw active widgets. */
329 for(w = window->active; w != NULL; w = w->next) {
330 wfocus = focus;
331 if(w == window->focused) {
332 wfocus |= CTK_FOCUS_WIDGET;
333 }
334
335 draw_widget(w, x1, y1, x2, y2,
336 clipy1, clipy2,
337 wfocus);
338 }
339
340#ifdef CTK_CONIO_CONF_UPDATE
341 CTK_CONIO_CONF_UPDATE();
342#endif /* CTK_CONIO_CONF_UPDATE */
343}
344/*-----------------------------------------------------------------------------------*/
345static void
346s_ctk_draw_window(struct ctk_window *window, unsigned char focus,
347 unsigned char clipy1, unsigned char clipy2,
348 unsigned char draw_borders)
349{
350 unsigned char x, y;
351 unsigned char h;
352 unsigned char x1, y1, x2, y2;
353
354 if(window->y + 1 >= clipy2) {
355 return;
356 }
357
358 x = window->x;
359 y = window->y + 1;
360 x1 = x + 1;
361 y1 = y + 1;
362 x2 = x1 + window->w;
363 y2 = y1 + window->h;
364
365 if(draw_borders) {
366
367 /* Draw window frame. */
368 if(focus & CTK_FOCUS_WINDOW) {
369 (void)textcolor(WINDOWCOLOR_FOCUS);
370 } else {
371 (void)textcolor(WINDOWCOLOR);
372 }
373
374 if(y >= clipy1) {
375 cputcxy(x, y, CH_ULCORNER);
376 gotoxy(wherex() + window->titlelen + CTK_CONF_WINDOWMOVE * 2, wherey());
377 chline(window->w - (wherex() - x) - 2);
378 cputcxy(x2, y, CH_URCORNER);
379 }
380
381 h = window->h;
382
383 if(clipy1 > y1) {
384 if(clipy1 - y1 < h) {
385 h = clipy1 - y1;
386 y1 = clipy1;
387 } else {
388 h = 0;
389 }
390 }
391
392 if(clipy2 < y1 + h) {
393 if(y1 >= clipy2) {
394 h = 0;
395 } else {
396 h = clipy2 - y1;
397 }
398 }
399
400 cvlinexy(x, y1, h);
401 cvlinexy(x2, y1, h);
402
403 if(y + window->h >= clipy1 &&
404 y + window->h < clipy2) {
405 cputcxy(x, y2, CH_LLCORNER);
406 chlinexy(x1, y2, window->w);
407 cputcxy(x2, y2, CH_LRCORNER);
408 }
409 }
410
411 draw_window_contents(window, focus, clipy1, clipy2,
412 x1, x2, y + 1, y2);
413}
414/*-----------------------------------------------------------------------------------*/
415static void
416s_ctk_draw_dialog(struct ctk_window *dialog)
417{
418 unsigned char x, y;
419 unsigned char i;
420 unsigned char x1, y1, x2, y2;
421
422 (void)textcolor(DIALOGCOLOR);
423
424 x = dialog->x;
425 y = dialog->y + 1;
426
427 x1 = x + 1;
428 y1 = y + 1;
429 x2 = x1 + dialog->w;
430 y2 = y1 + dialog->h;
431
432 /* Draw dialog frame. */
433 cvlinexy(x, y1,
434 dialog->h);
435 cvlinexy(x2, y1,
436 dialog->h);
437
438 chlinexy(x1, y,
439 dialog->w);
440 chlinexy(x1, y2,
441 dialog->w);
442
443 cputcxy(x, y, CH_ULCORNER);
444 cputcxy(x, y2, CH_LLCORNER);
445 cputcxy(x2, y, CH_URCORNER);
446 cputcxy(x2, y2, CH_LRCORNER);
447
448 /* Clear dialog contents. */
449 for(i = y1; i < y2; ++i) {
450 cclearxy(x1, i, dialog->w);
451 }
452
453 draw_window_contents(dialog, CTK_FOCUS_DIALOG, 0, sizey,
454 x1, x2, y1, y2);
455}
456/*-----------------------------------------------------------------------------------*/
457static void
458s_ctk_draw_clear(unsigned char y1, unsigned char y2) __naked
459{
460 __asm
461 ld hl,#2
462 add hl,sp
463 ld d,(hl) ; Y1
464 inc hl
465 ld e,(hl) ; Y2
466
467 push af
468
469 ld h,#1
470 ld l,d
471 ld d,#40
472
473 inc e
474
475 call 0xBB99 ; TXT GET PAPER
476 call 0xBC2C ; SCR INK ENCODE
477
478 call 0xBC44 ; SCR FILL BOX
479
480 pop af
481 ret
482 __endasm;
483}
484/*-----------------------------------------------------------------------------------*/
485static void
486draw_menu(struct ctk_menu *m, unsigned char open)
487{
488 unsigned char x, x2, y;
489
490 if(open) {
491 x = x2 = wherex();
492 if(x2 + CTK_CONF_MENUWIDTH > sizex) {
493 x2 = sizex - CTK_CONF_MENUWIDTH;
494 }
495
496 for(y = 0; y < m->nitems; ++y) {
497 if(y == m->active) {
498 (void)textcolor(ACTIVEMENUITEMCOLOR);
499 } else {
500 (void)textcolor(SCREENCOLOR);
501 }
502 gotoxy(x2, y + 1);
503 if(m->items[y].title[0] == '-') {
504 chline(CTK_CONF_MENUWIDTH);
505 } else {
506 cputs(m->items[y].title);
507 }
508 if(x2 + CTK_CONF_MENUWIDTH > wherex()) {
509 cclear(x2 + CTK_CONF_MENUWIDTH - wherex());
510 }
511 }
512
513 gotoxy(x, 0);
514 (void)textcolor(OPENMENUCOLOR);
515 }
516
517 cputs(m->title);
518 cputc(' ');
519}
520/*-----------------------------------------------------------------------------------*/
521static void
522s_ctk_draw_menus(struct ctk_menus *menus)
523{
524 struct ctk_menu *m;
525
526 /* Draw menus */
527 (void)bgcolor(MENUCOLOR);
528 (void)textcolor(SCREENCOLOR);
529 gotoxy(0, 0);
530 cputc(' ');
531 for(m = menus->menus->next; m != NULL; m = m->next) {
532 draw_menu(m, m == menus->open);
533 }
534
535 /* Draw desktopmenu */
536 if(wherex() + strlen(menus->desktopmenu->title) + 1 >= sizex) {
537 gotoxy(sizex - strlen(menus->desktopmenu->title) - 1, 0);
538 } else {
539 cclear(sizex - wherex() -
540 strlen(menus->desktopmenu->title) - 1);
541 }
542 draw_menu(menus->desktopmenu, menus->desktopmenu == menus->open);
543
544 (void)bgcolor(SCREENCOLOR);
545}
546/*-----------------------------------------------------------------------------------*/
547static unsigned char
548s_ctk_draw_height(void)
549{
550 return sizey;
551}
552/*-----------------------------------------------------------------------------------*/
553static unsigned char
554s_ctk_draw_width(void)
555{
556 return sizex;
557}
558/*-----------------------------------------------------------------------------------*/
559static unsigned short
560s_ctk_mouse_xtoc(unsigned short x)
561{
562 return x / 8;
563}
564/*-----------------------------------------------------------------------------------*/
565static unsigned short
566s_ctk_mouse_ytoc(unsigned short y)
567{
568 return y / 8;
569}
570/*-----------------------------------------------------------------------------------*/
571static const struct ctk_draw_service_interface interface =
572 {CTK_DRAW_SERVICE_VERSION,
573 1,
574 1,
575 1,
576 s_ctk_draw_init,
577 s_ctk_draw_clear,
578 s_ctk_draw_clear_window,
579 s_ctk_draw_window,
580 s_ctk_draw_dialog,
581 s_ctk_draw_widget,
582 s_ctk_draw_menus,
583 s_ctk_draw_width,
584 s_ctk_draw_height,
585 s_ctk_mouse_xtoc,
586 s_ctk_mouse_ytoc,
587 };
588
589EK_EVENTHANDLER(eventhandler, ev, data);
590EK_PROCESS(proc, CTK_DRAW_SERVICE_NAME ": text", EK_PRIO_NORMAL,
591 eventhandler, NULL, (void *)&interface);
592
593/*--------------------------------------------------------------------------*/
594LOADER_INIT_FUNC(ctk_conio_service_init, arg)
595{
596 s_ctk_draw_init();
597 ek_service_start(CTK_DRAW_SERVICE_NAME, &proc);
598}
599/*--------------------------------------------------------------------------*/
600EK_EVENTHANDLER(eventhandler, ev, data)
601{
602 EK_EVENTHANDLER_ARGS(ev, data);
603
604 switch(ev) {
605 case EK_EVENT_INIT:
606 case EK_EVENT_REPLACE:
607 s_ctk_draw_init();
608 ctk_restore();
609 break;
610 case EK_EVENT_REQUEST_REPLACE:
611 ek_replace((struct ek_proc *)data, NULL);
612 LOADER_UNLOAD();
613 break;
614 }
615}
616/*--------------------------------------------------------------------------*/