blob: 3c0d96c7807e950169879844d2cdec08923cc7f7 [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);
81 screensize(&sizex, &sizey);
82 ctk_draw_clear(0, sizey);
83}
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
262static void clearrect(unsigned char x1, unsigned char y1,
263 unsigned char x2, unsigned char y2)
264{
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
302 h = window->y + 2 + window->h;
303
304 clearrect(
305 window->x + window->w, h,
306 window->x + 1, window->y + 2
307 );
308}
309/*-----------------------------------------------------------------------------------*/
310static void
311draw_window_contents(struct ctk_window *window, unsigned char focus,
312 unsigned char clipy1, unsigned char clipy2,
313 unsigned char x1, unsigned char x2,
314 unsigned char y1, unsigned char y2)
315{
316 struct ctk_widget *w;
317 unsigned char wfocus;
318
319 /* Draw inactive widgets. */
320 for(w = window->inactive; w != NULL; w = w->next) {
321 draw_widget(w, x1, y1, x2, y2,
322 clipy1, clipy2,
323 focus);
324 }
325
326 /* Draw active widgets. */
327 for(w = window->active; w != NULL; w = w->next) {
328 wfocus = focus;
329 if(w == window->focused) {
330 wfocus |= CTK_FOCUS_WIDGET;
331 }
332
333 draw_widget(w, x1, y1, x2, y2,
334 clipy1, clipy2,
335 wfocus);
336 }
337
338#ifdef CTK_CONIO_CONF_UPDATE
339 CTK_CONIO_CONF_UPDATE();
340#endif /* CTK_CONIO_CONF_UPDATE */
341}
342/*-----------------------------------------------------------------------------------*/
343static void
344s_ctk_draw_window(struct ctk_window *window, unsigned char focus,
345 unsigned char clipy1, unsigned char clipy2,
346 unsigned char draw_borders)
347{
348 unsigned char x, y;
349 unsigned char h;
350 unsigned char x1, y1, x2, y2;
351
352 if(window->y + 1 >= clipy2) {
353 return;
354 }
355
356 x = window->x;
357 y = window->y + 1;
358 x1 = x + 1;
359 y1 = y + 1;
360 x2 = x1 + window->w;
361 y2 = y1 + window->h;
362
363 if(draw_borders) {
364
365 /* Draw window frame. */
366 if(focus & CTK_FOCUS_WINDOW) {
367 (void)textcolor(WINDOWCOLOR_FOCUS);
368 } else {
369 (void)textcolor(WINDOWCOLOR);
370 }
371
372 if(y >= clipy1) {
373 cputcxy(x, y, CH_ULCORNER);
374 gotoxy(wherex() + window->titlelen + CTK_CONF_WINDOWMOVE * 2, wherey());
375 chline(window->w - (wherex() - x) - 2);
376 cputcxy(x2, y, CH_URCORNER);
377 }
378
379 h = window->h;
380
381 if(clipy1 > y1) {
382 if(clipy1 - y1 < h) {
383 h = clipy1 - y1;
384 y1 = clipy1;
385 } else {
386 h = 0;
387 }
388 }
389
390 if(clipy2 < y1 + h) {
391 if(y1 >= clipy2) {
392 h = 0;
393 } else {
394 h = clipy2 - y1;
395 }
396 }
397
398 cvlinexy(x, y1, h);
399 cvlinexy(x2, y1, h);
400
401 if(y + window->h >= clipy1 &&
402 y + window->h < clipy2) {
403 cputcxy(x, y2, CH_LLCORNER);
404 chlinexy(x1, y2, window->w);
405 cputcxy(x2, y2, CH_LRCORNER);
406 }
407 }
408
409 draw_window_contents(window, focus, clipy1, clipy2,
410 x1, x2, y + 1, y2);
411}
412/*-----------------------------------------------------------------------------------*/
413static void
414s_ctk_draw_dialog(struct ctk_window *dialog)
415{
416 unsigned char x, y;
417 unsigned char i;
418 unsigned char x1, y1, x2, y2;
419
420 (void)textcolor(DIALOGCOLOR);
421
422 x = dialog->x;
423 y = dialog->y + 1;
424
425 x1 = x + 1;
426 y1 = y + 1;
427 x2 = x1 + dialog->w;
428 y2 = y1 + dialog->h;
429
430 /* Draw dialog frame. */
431 cvlinexy(x, y1,
432 dialog->h);
433 cvlinexy(x2, y1,
434 dialog->h);
435
436 chlinexy(x1, y,
437 dialog->w);
438 chlinexy(x1, y2,
439 dialog->w);
440
441 cputcxy(x, y, CH_ULCORNER);
442 cputcxy(x, y2, CH_LLCORNER);
443 cputcxy(x2, y, CH_URCORNER);
444 cputcxy(x2, y2, CH_LRCORNER);
445
446 /* Clear dialog contents. */
447 for(i = y1; i < y2; ++i) {
448 cclearxy(x1, i, dialog->w);
449 }
450
451 draw_window_contents(dialog, CTK_FOCUS_DIALOG, 0, sizey,
452 x1, x2, y1, y2);
453}
454/*-----------------------------------------------------------------------------------*/
455static void
456s_ctk_draw_clear(unsigned char y1, unsigned char y2) __naked
457{
458 __asm
459 ld hl,#2
460 add hl,sp
461 ld d,(hl) ; Y1
462 inc hl
463 ld e,(hl) ; Y2
464
465 push af
466
467 ld h,#1
468 ld l,d
469 ld d,#40
470
471 inc e
472
473 call 0xBB99 ; TXT GET PAPER
474 call 0xBC2C ; SCR INK ENCODE
475
476 call 0xBC44 ; SCR FILL BOX
477
478 pop af
479 ret
480 __endasm;
481}
482/*-----------------------------------------------------------------------------------*/
483static void
484draw_menu(struct ctk_menu *m, unsigned char open)
485{
486 unsigned char x, x2, y;
487
488 if(open) {
489 x = x2 = wherex();
490 if(x2 + CTK_CONF_MENUWIDTH > sizex) {
491 x2 = sizex - CTK_CONF_MENUWIDTH;
492 }
493
494 for(y = 0; y < m->nitems; ++y) {
495 if(y == m->active) {
496 (void)textcolor(ACTIVEMENUITEMCOLOR);
497 } else {
498 (void)textcolor(SCREENCOLOR);
499 }
500 gotoxy(x2, y + 1);
501 if(m->items[y].title[0] == '-') {
502 chline(CTK_CONF_MENUWIDTH);
503 } else {
504 cputs(m->items[y].title);
505 }
506 if(x2 + CTK_CONF_MENUWIDTH > wherex()) {
507 cclear(x2 + CTK_CONF_MENUWIDTH - wherex());
508 }
509 }
510
511 gotoxy(x, 0);
512 (void)textcolor(OPENMENUCOLOR);
513 }
514
515 cputs(m->title);
516 cputc(' ');
517}
518/*-----------------------------------------------------------------------------------*/
519static void
520s_ctk_draw_menus(struct ctk_menus *menus)
521{
522 struct ctk_menu *m;
523
524 /* Draw menus */
525 (void)bgcolor(MENUCOLOR);
526 (void)textcolor(SCREENCOLOR);
527 gotoxy(0, 0);
528 cputc(' ');
529 for(m = menus->menus->next; m != NULL; m = m->next) {
530 draw_menu(m, m == menus->open);
531 }
532
533 /* Draw desktopmenu */
534 if(wherex() + strlen(menus->desktopmenu->title) + 1 >= sizex) {
535 gotoxy(sizex - strlen(menus->desktopmenu->title) - 1, 0);
536 } else {
537 cclear(sizex - wherex() -
538 strlen(menus->desktopmenu->title) - 1);
539 }
540 draw_menu(menus->desktopmenu, menus->desktopmenu == menus->open);
541
542 (void)bgcolor(SCREENCOLOR);
543}
544/*-----------------------------------------------------------------------------------*/
545static unsigned char
546s_ctk_draw_height(void)
547{
548 return sizey;
549}
550/*-----------------------------------------------------------------------------------*/
551static unsigned char
552s_ctk_draw_width(void)
553{
554 return sizex;
555}
556/*-----------------------------------------------------------------------------------*/
557static unsigned short
558s_ctk_mouse_xtoc(unsigned short x)
559{
560 return x / 8;
561}
562/*-----------------------------------------------------------------------------------*/
563static unsigned short
564s_ctk_mouse_ytoc(unsigned short y)
565{
566 return y / 8;
567}
568/*-----------------------------------------------------------------------------------*/
569static const struct ctk_draw_service_interface interface =
570 {CTK_DRAW_SERVICE_VERSION,
571 1,
572 1,
573 1,
574 s_ctk_draw_init,
575 s_ctk_draw_clear,
576 s_ctk_draw_clear_window,
577 s_ctk_draw_window,
578 s_ctk_draw_dialog,
579 s_ctk_draw_widget,
580 s_ctk_draw_menus,
581 s_ctk_draw_width,
582 s_ctk_draw_height,
583 s_ctk_mouse_xtoc,
584 s_ctk_mouse_ytoc,
585 };
586
587EK_EVENTHANDLER(eventhandler, ev, data);
588EK_PROCESS(proc, CTK_DRAW_SERVICE_NAME ": text", EK_PRIO_NORMAL,
589 eventhandler, NULL, (void *)&interface);
590
591/*--------------------------------------------------------------------------*/
592LOADER_INIT_FUNC(ctk_conio_service_init, arg)
593{
594 s_ctk_draw_init();
595 ek_service_start(CTK_DRAW_SERVICE_NAME, &proc);
596}
597/*--------------------------------------------------------------------------*/
598EK_EVENTHANDLER(eventhandler, ev, data)
599{
600 EK_EVENTHANDLER_ARGS(ev, data);
601
602 switch(ev) {
603 case EK_EVENT_INIT:
604 case EK_EVENT_REPLACE:
605 s_ctk_draw_init();
606 ctk_restore();
607 break;
608 case EK_EVENT_REQUEST_REPLACE:
609 ek_replace((struct ek_proc *)data, NULL);
610 LOADER_UNLOAD();
611 break;
612 }
613}
614/*--------------------------------------------------------------------------*/