blob: 014af48d5d237ccec80b11fbcc09d7173447de4a [file] [log] [blame]
adamdunkels09c28b62003-04-02 10:17:52 +00001/*
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.
adamdunkels76a4c4a2004-06-06 05:52:21 +000014 * 3. The name of the author may not be used to endorse or promote
adamdunkels09c28b62003-04-02 10:17:52 +000015 * 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 *
oliverschmidt0e144fb2004-08-12 21:52:03 +000032 * $Id: ctk-conio.c,v 1.15 2004/08/12 21:52:03 oliverschmidt Exp $
adamdunkels09c28b62003-04-02 10:17:52 +000033 *
34 */
35
adamdunkels09e27572004-03-25 09:50:13 +000036#include <conio.h>
37
adamdunkels09c28b62003-04-02 10:17:52 +000038#include "ctk.h"
39#include "ctk-draw.h"
40
41#include "ctk-conio-conf.h"
adamdunkels09c28b62003-04-02 10:17:52 +000042#include <string.h>
adamdunkelsca2cbe52004-07-04 11:38:43 +000043#include <ctype.h>
adamdunkels09c28b62003-04-02 10:17:52 +000044
45#ifndef NULL
46#define NULL (void *)0
47#endif /* NULL */
48
49static unsigned char sizex, sizey;
50
adamdunkelsca2cbe52004-07-04 11:38:43 +000051unsigned char ctk_draw_windowborder_height = 1;
52unsigned char ctk_draw_windowborder_width = 1;
53unsigned char ctk_draw_windowtitle_height = 1;
54
55
adamdunkels09c28b62003-04-02 10:17:52 +000056/*-----------------------------------------------------------------------------------*/
adamdunkels09c28b62003-04-02 10:17:52 +000057static void
58cputsn(char *str, unsigned char len)
59{
oliverschmidt4037b262004-06-27 20:59:05 +000060 char c;
61
62 while(len > 0) {
63 --len;
64 c = *str;
65 if(c == 0) {
66 break;
67 }
68 cputc(c);
69 ++str;
70 }
adamdunkels09c28b62003-04-02 10:17:52 +000071}
72/*-----------------------------------------------------------------------------------*/
73void
74ctk_draw_init(void)
75{
76 bgcolor(SCREENCOLOR);
77 bordercolor(BORDERCOLOR);
78 screensize(&sizex, &sizey);
79 ctk_draw_clear(0, sizey);
80}
81/*-----------------------------------------------------------------------------------*/
82static void
83draw_widget(struct ctk_widget *w,
84 unsigned char x, unsigned char y,
85 unsigned char clipx,
86 unsigned char clipy,
87 unsigned char clipy1, unsigned char clipy2,
88 unsigned char focus)
89{
90 unsigned char xpos, ypos, xscroll;
91 unsigned char i, j;
92 char c, *text;
adamdunkels09e27572004-03-25 09:50:13 +000093 unsigned char len, wfocus;
94
95 wfocus = 0;
adamdunkels09c28b62003-04-02 10:17:52 +000096 if(focus & CTK_FOCUS_WINDOW) {
97 textcolor(WIDGETCOLOR_FWIN);
98 if(focus & CTK_FOCUS_WIDGET) {
99 textcolor(WIDGETCOLOR_FOCUS);
adamdunkels09e27572004-03-25 09:50:13 +0000100 wfocus = 1;
adamdunkels09c28b62003-04-02 10:17:52 +0000101 }
102 } else if(focus & CTK_FOCUS_DIALOG) {
103 textcolor(WIDGETCOLOR_DIALOG);
104 if(focus & CTK_FOCUS_WIDGET) {
105 textcolor(WIDGETCOLOR_FOCUS);
adamdunkels09e27572004-03-25 09:50:13 +0000106 wfocus = 1;
adamdunkels09c28b62003-04-02 10:17:52 +0000107 }
108 } else {
109 textcolor(WIDGETCOLOR);
110 }
111
112 xpos = x + w->x;
113 ypos = y + w->y;
114
115 switch(w->type) {
116 case CTK_WIDGET_SEPARATOR:
117 if(ypos >= clipy1 && ypos < clipy2) {
118 chlinexy(xpos, ypos, w->w);
119 }
120 break;
121 case CTK_WIDGET_LABEL:
122 text = w->widget.label.text;
adamdunkels62393fb2003-07-30 23:31:56 +0000123 for(i = 0; i < w->h; ++i) {
adamdunkels09c28b62003-04-02 10:17:52 +0000124 if(ypos >= clipy1 && ypos < clipy2) {
125 gotoxy(xpos, ypos);
126 cputsn(text, w->w);
127 if(w->w - (wherex() - xpos) > 0) {
128 cclear(w->w - (wherex() - xpos));
129 }
130 }
131 ++ypos;
132 text += w->w;
133 }
134 break;
135 case CTK_WIDGET_BUTTON:
136 if(ypos >= clipy1 && ypos < clipy2) {
adamdunkels09e27572004-03-25 09:50:13 +0000137 if(wfocus != 0) {
adamdunkels09c28b62003-04-02 10:17:52 +0000138 revers(1);
139 } else {
140 revers(0);
141 }
142 cputcxy(xpos, ypos, '[');
143 cputsn(w->widget.button.text, w->w);
144 cputc(']');
145 revers(0);
146 }
147 break;
148 case CTK_WIDGET_HYPERLINK:
149 if(ypos >= clipy1 && ypos < clipy2) {
adamdunkels09e27572004-03-25 09:50:13 +0000150 if(wfocus != 0) {
adamdunkels09c28b62003-04-02 10:17:52 +0000151 revers(0);
152 } else {
153 revers(1);
154 }
155 gotoxy(xpos, ypos);
156 textcolor(WIDGETCOLOR_HLINK);
157 cputsn(w->widget.button.text, w->w);
158 revers(0);
159 }
160 break;
161 case CTK_WIDGET_TEXTENTRY:
162 text = w->widget.textentry.text;
adamdunkels09e27572004-03-25 09:50:13 +0000163 if(wfocus != 0) {
adamdunkels09c28b62003-04-02 10:17:52 +0000164 revers(1);
165 } else {
166 revers(0);
167 }
168 xscroll = 0;
169 if(w->widget.textentry.xpos >= w->w - 1) {
170 xscroll = w->widget.textentry.xpos - w->w + 1;
171 }
adamdunkels62393fb2003-07-30 23:31:56 +0000172 for(j = 0; j < w->h; ++j) {
adamdunkels09c28b62003-04-02 10:17:52 +0000173 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 for(i = 0; i < w->w; ++i) {
179 c = text[i + xscroll];
180 if(i == w->widget.textentry.xpos - xscroll) {
181 revers(1);
182 } else {
183 revers(0);
184 }
185 if(c == 0) {
186 cputc(' ');
187 } else {
188 cputc(c);
189 }
190 revers(0);
191 }
192 cputc('<');
193 } else {
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->w;
206 }
207 revers(0);
208 break;
oliverschmidta98def52004-06-27 18:32:10 +0000209#if CTK_CONF_ICONS
adamdunkels09c28b62003-04-02 10:17:52 +0000210 case CTK_WIDGET_ICON:
211 if(ypos >= clipy1 && ypos < clipy2) {
oliverschmidtf42b7bc2004-06-27 15:04:01 +0000212 if(wfocus != 0) {
adamdunkels09c28b62003-04-02 10:17:52 +0000213 revers(1);
214 } else {
215 revers(0);
216 }
oliverschmidta98def52004-06-27 18:32:10 +0000217#if CTK_CONF_ICON_TEXTMAPS
adamdunkels09c28b62003-04-02 10:17:52 +0000218 if(w->widget.icon.textmap != NULL) {
219 for(i = 0; i < 3; ++i) {
220 gotoxy(xpos, ypos);
221 if(ypos >= clipy1 && ypos < clipy2) {
222 cputc(w->widget.icon.textmap[0 + 3 * i]);
223 cputc(w->widget.icon.textmap[1 + 3 * i]);
224 cputc(w->widget.icon.textmap[2 + 3 * i]);
225 }
226 ++ypos;
227 }
228 }
oliverschmidta98def52004-06-27 18:32:10 +0000229#endif /* CTK_CONF_ICON_TEXTMAPS */
adamdunkels09c28b62003-04-02 10:17:52 +0000230
231 len = strlen(w->widget.icon.title);
oliverschmidta98def52004-06-27 18:32:10 +0000232 if(xpos + len >= sizex) {
233 xpos = sizex - len;
adamdunkels09c28b62003-04-02 10:17:52 +0000234 }
235
oliverschmidta98def52004-06-27 18:32:10 +0000236 gotoxy(xpos, ypos);
adamdunkels09c28b62003-04-02 10:17:52 +0000237 if(ypos >= clipy1 && ypos < clipy2) {
238 cputs(w->widget.icon.title);
239 }
240 revers(0);
241 }
242 break;
oliverschmidta98def52004-06-27 18:32:10 +0000243#endif /* CTK_CONF_ICONS */
adamdunkels09c28b62003-04-02 10:17:52 +0000244
245 default:
246 break;
247 }
248}
249/*-----------------------------------------------------------------------------------*/
250void
251ctk_draw_widget(struct ctk_widget *w,
252 unsigned char focus,
253 unsigned char clipy1,
254 unsigned char clipy2)
255{
256 struct ctk_window *win = w->window;
257 unsigned char posx, posy;
258
259 posx = win->x + 1;
260 posy = win->y + 2;
261
262 if(w == win->focused) {
263 focus |= CTK_FOCUS_WIDGET;
264 }
265
266 draw_widget(w, posx, posy,
267 posx + win->w,
268 posy + win->h,
269 clipy1, clipy2,
270 focus);
adamdunkelsfe32f8d2003-04-28 20:31:48 +0000271
272#ifdef CTK_CONIO_CONF_UPDATE
273 CTK_CONIO_CONF_UPDATE();
274#endif /* CTK_CONIO_CONF_UPDATE */
adamdunkels09c28b62003-04-02 10:17:52 +0000275}
276/*-----------------------------------------------------------------------------------*/
277void
278ctk_draw_clear_window(struct ctk_window *window,
279 unsigned char focus,
280 unsigned char clipy1,
281 unsigned char clipy2)
282{
283 unsigned char i;
284 unsigned char h;
285
oliverschmidt0e144fb2004-08-12 21:52:03 +0000286 if(focus & CTK_FOCUS_WINDOW) {
287 textcolor(WINDOWCOLOR_FOCUS);
288 } else {
289 textcolor(WINDOWCOLOR);
290 }
adamdunkels09c28b62003-04-02 10:17:52 +0000291
292 h = window->y + 2 + window->h;
293 /* Clear window contents. */
294 for(i = window->y + 2; i < h; ++i) {
295 if(i >= clipy1 && i < clipy2) {
296 cclearxy(window->x + 1, i, window->w);
297 }
298 }
299}
300/*-----------------------------------------------------------------------------------*/
adamdunkelsa45d8a12003-04-08 07:22:13 +0000301static void
302draw_window_contents(struct ctk_window *window, unsigned char focus,
303 unsigned char clipy1, unsigned char clipy2,
304 unsigned char x1, unsigned char x2,
305 unsigned char y1, unsigned char y2)
306{
307 struct ctk_widget *w;
308 unsigned char wfocus;
309
310 /* Draw inactive widgets. */
311 for(w = window->inactive; w != NULL; w = w->next) {
312 draw_widget(w, x1, y1, x2, y2,
313 clipy1, clipy2,
314 focus);
315 }
316
317 /* Draw active widgets. */
318 for(w = window->active; w != NULL; w = w->next) {
319 wfocus = focus;
320 if(w == window->focused) {
321 wfocus |= CTK_FOCUS_WIDGET;
322 }
323
324 draw_widget(w, x1, y1, x2, y2,
325 clipy1, clipy2,
326 wfocus);
327 }
328
adamdunkelsfe32f8d2003-04-28 20:31:48 +0000329#ifdef CTK_CONIO_CONF_UPDATE
330 CTK_CONIO_CONF_UPDATE();
331#endif /* CTK_CONIO_CONF_UPDATE */
332
adamdunkelsa45d8a12003-04-08 07:22:13 +0000333}
334/*-----------------------------------------------------------------------------------*/
adamdunkels09c28b62003-04-02 10:17:52 +0000335void
336ctk_draw_window(struct ctk_window *window, unsigned char focus,
337 unsigned char clipy1, unsigned char clipy2)
338{
339 unsigned char x, y;
340 unsigned char h;
adamdunkels09c28b62003-04-02 10:17:52 +0000341 unsigned char x1, y1, x2, y2;
342
343 if(window->y + 1 >= clipy2) {
344 return;
345 }
346
347 x = window->x;
348 y = window->y + 1;
349
350 if(focus & CTK_FOCUS_WINDOW) {
351 textcolor(WINDOWCOLOR_FOCUS);
352 } else {
353 textcolor(WINDOWCOLOR);
354 }
355
356 x1 = x + 1;
357 y1 = y + 1;
358 x2 = x1 + window->w;
359 y2 = y1 + window->h;
360
361 /* Draw window frame. */
362 if(y >= clipy1) {
363 cputcxy(x, y, CH_ULCORNER);
adamdunkels09c28b62003-04-02 10:17:52 +0000364 gotoxy(wherex() + window->titlelen + 2, wherey());
365 chline(window->w - (wherex() - x) - 2);
366 cputcxy(x2, y, CH_URCORNER);
367 }
368
369 h = window->h;
370
371 if(clipy1 > y1) {
372 if(clipy1 - y1 < h) {
373 h = clipy1 - y1;
adamdunkels62393fb2003-07-30 23:31:56 +0000374 y1 = clipy1;
adamdunkels09c28b62003-04-02 10:17:52 +0000375 } else {
376 h = 0;
377 }
378 }
379
adamdunkels62393fb2003-07-30 23:31:56 +0000380 if(clipy2 < y1 + h) {
adamdunkels09c28b62003-04-02 10:17:52 +0000381 if(y1 >= clipy2) {
382 h = 0;
383 } else {
384 h = clipy2 - y1;
385 }
386 }
adamdunkels62393fb2003-07-30 23:31:56 +0000387
adamdunkels09c28b62003-04-02 10:17:52 +0000388 cvlinexy(x, y1, h);
adamdunkels62393fb2003-07-30 23:31:56 +0000389 cvlinexy(x2, y1, h);
adamdunkels09c28b62003-04-02 10:17:52 +0000390
391 if(y + window->h >= clipy1 &&
392 y + window->h < clipy2) {
393 cputcxy(x, y2, CH_LLCORNER);
394 chlinexy(x1, y2, window->w);
395 cputcxy(x2, y2, CH_LRCORNER);
396 }
397
adamdunkelsa45d8a12003-04-08 07:22:13 +0000398 draw_window_contents(window, focus & CTK_FOCUS_WINDOW, clipy1, clipy2,
adamdunkels62393fb2003-07-30 23:31:56 +0000399 x1, x2, y + 1, y2);
adamdunkels09c28b62003-04-02 10:17:52 +0000400}
401/*-----------------------------------------------------------------------------------*/
402void
403ctk_draw_dialog(struct ctk_window *dialog)
404{
405 unsigned char x, y;
406 unsigned char i;
adamdunkels09c28b62003-04-02 10:17:52 +0000407 unsigned char x1, y1, x2, y2;
408
409 textcolor(DIALOGCOLOR);
410
411 x = dialog->x;
412 y = dialog->y + 1;
413
414
415 x1 = x + 1;
416 y1 = y + 1;
417 x2 = x1 + dialog->w;
418 y2 = y1 + dialog->h;
419
420
421 /* Draw dialog frame. */
422
423 cvlinexy(x, y1,
424 dialog->h);
425 cvlinexy(x2, y1,
426 dialog->h);
427
428 chlinexy(x1, y,
429 dialog->w);
430 chlinexy(x1, y2,
431 dialog->w);
432
433 cputcxy(x, y, CH_ULCORNER);
434 cputcxy(x, y2, CH_LLCORNER);
435 cputcxy(x2, y, CH_URCORNER);
436 cputcxy(x2, y2, CH_LRCORNER);
437
438
adamdunkels09c28b62003-04-02 10:17:52 +0000439 /* Clear dialog contents. */
440 for(i = y1; i < y2; ++i) {
441 cclearxy(x1, i, dialog->w);
442 }
adamdunkels09c28b62003-04-02 10:17:52 +0000443
adamdunkelsa45d8a12003-04-08 07:22:13 +0000444 draw_window_contents(dialog, CTK_FOCUS_DIALOG, 0, sizey,
445 x1, x2, y1, y2);
adamdunkels09c28b62003-04-02 10:17:52 +0000446}
447/*-----------------------------------------------------------------------------------*/
448void
449ctk_draw_clear(unsigned char y1, unsigned char y2)
450{
451 unsigned char i;
452
oliverschmidt0e144fb2004-08-12 21:52:03 +0000453 textcolor(BACKGROUNDCOLOR);
adamdunkels09c28b62003-04-02 10:17:52 +0000454 for(i = y1; i < y2; ++i) {
455 cclearxy(0, i, sizex);
456 }
457}
458/*-----------------------------------------------------------------------------------*/
459static void
460draw_menu(struct ctk_menu *m)
461{
462 unsigned char x, x2, y;
463 textcolor(OPENMENUCOLOR);
oliverschmidtd9a91072004-07-29 11:49:45 +0000464 revers(0);
adamdunkels09c28b62003-04-02 10:17:52 +0000465 x = wherex();
466 cputs(m->title);
467 cputc(' ');
468 x2 = wherex();
469 if(x + CTK_CONF_MENUWIDTH > sizex) {
470 x = sizex - CTK_CONF_MENUWIDTH;
471 }
472
473
474 for(y = 0; y < m->nitems; ++y) {
475 if(y == m->active) {
476 textcolor(ACTIVEMENUITEMCOLOR);
477 revers(0);
478 } else {
479 textcolor(MENUCOLOR);
oliverschmidtd9a91072004-07-29 11:49:45 +0000480 revers(1);
adamdunkels09c28b62003-04-02 10:17:52 +0000481 }
482 gotoxy(x, y + 1);
483 if(m->items[y].title[0] == '-') {
484 chline(CTK_CONF_MENUWIDTH);
485 } else {
486 cputs(m->items[y].title);
487 }
488 if(x + CTK_CONF_MENUWIDTH > wherex()) {
489 cclear(x + CTK_CONF_MENUWIDTH - wherex());
490 }
adamdunkels09c28b62003-04-02 10:17:52 +0000491 }
492 gotoxy(x2, 0);
493 textcolor(MENUCOLOR);
oliverschmidtd9a91072004-07-29 11:49:45 +0000494 revers(1);
adamdunkels09c28b62003-04-02 10:17:52 +0000495}
496/*-----------------------------------------------------------------------------------*/
497void
498ctk_draw_menus(struct ctk_menus *menus)
499{
500 struct ctk_menu *m;
adamdunkels62393fb2003-07-30 23:31:56 +0000501
adamdunkels09c28b62003-04-02 10:17:52 +0000502 /* Draw menus */
503 textcolor(MENUCOLOR);
504 gotoxy(0, 0);
505 revers(1);
adamdunkelse68091d2003-04-16 18:29:10 +0000506 cputc(' ');
adamdunkels09c28b62003-04-02 10:17:52 +0000507 for(m = menus->menus->next; m != NULL; m = m->next) {
508 if(m != menus->open) {
509 cputs(m->title);
510 cputc(' ');
511 } else {
512 draw_menu(m);
513 }
514 }
515
516
517 if(wherex() + strlen(menus->desktopmenu->title) + 1>= sizex) {
518 gotoxy(sizex - strlen(menus->desktopmenu->title) - 1, 0);
519 } else {
520 cclear(sizex - wherex() -
521 strlen(menus->desktopmenu->title) - 1);
522 }
523
524 /* Draw desktopmenu */
525 if(menus->desktopmenu != menus->open) {
526 cputs(menus->desktopmenu->title);
527 cputc(' ');
528 } else {
529 draw_menu(menus->desktopmenu);
530 }
531
532 revers(0);
533}
534/*-----------------------------------------------------------------------------------*/
535unsigned char
536ctk_draw_height(void)
537{
538 return sizey;
539}
540/*-----------------------------------------------------------------------------------*/
541unsigned char
542ctk_draw_width(void)
543{
544 return sizex;
545}
546/*-----------------------------------------------------------------------------------*/
adamdunkelsca2cbe52004-07-04 11:38:43 +0000547int
548ctk_arch_isprint(char c)
549{
550 return isprint(c);
551}