blob: 1309c001b9e88edb1a6ed57bfc3abb229bb76850 [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 *
oliverschmidtd9a91072004-07-29 11:49:45 +000032 * $Id: ctk-conio.c,v 1.14 2004/07/29 11:49:45 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
286
287 h = window->y + 2 + window->h;
288 /* Clear window contents. */
289 for(i = window->y + 2; i < h; ++i) {
290 if(i >= clipy1 && i < clipy2) {
291 cclearxy(window->x + 1, i, window->w);
292 }
293 }
294}
295/*-----------------------------------------------------------------------------------*/
adamdunkelsa45d8a12003-04-08 07:22:13 +0000296static void
297draw_window_contents(struct ctk_window *window, unsigned char focus,
298 unsigned char clipy1, unsigned char clipy2,
299 unsigned char x1, unsigned char x2,
300 unsigned char y1, unsigned char y2)
301{
302 struct ctk_widget *w;
303 unsigned char wfocus;
304
305 /* Draw inactive widgets. */
306 for(w = window->inactive; w != NULL; w = w->next) {
307 draw_widget(w, x1, y1, x2, y2,
308 clipy1, clipy2,
309 focus);
310 }
311
312 /* Draw active widgets. */
313 for(w = window->active; w != NULL; w = w->next) {
314 wfocus = focus;
315 if(w == window->focused) {
316 wfocus |= CTK_FOCUS_WIDGET;
317 }
318
319 draw_widget(w, x1, y1, x2, y2,
320 clipy1, clipy2,
321 wfocus);
322 }
323
adamdunkelsfe32f8d2003-04-28 20:31:48 +0000324#ifdef CTK_CONIO_CONF_UPDATE
325 CTK_CONIO_CONF_UPDATE();
326#endif /* CTK_CONIO_CONF_UPDATE */
327
adamdunkelsa45d8a12003-04-08 07:22:13 +0000328}
329/*-----------------------------------------------------------------------------------*/
adamdunkels09c28b62003-04-02 10:17:52 +0000330void
331ctk_draw_window(struct ctk_window *window, unsigned char focus,
332 unsigned char clipy1, unsigned char clipy2)
333{
334 unsigned char x, y;
335 unsigned char h;
adamdunkels09c28b62003-04-02 10:17:52 +0000336 unsigned char x1, y1, x2, y2;
337
338 if(window->y + 1 >= clipy2) {
339 return;
340 }
341
342 x = window->x;
343 y = window->y + 1;
344
345 if(focus & CTK_FOCUS_WINDOW) {
346 textcolor(WINDOWCOLOR_FOCUS);
347 } else {
348 textcolor(WINDOWCOLOR);
349 }
350
351 x1 = x + 1;
352 y1 = y + 1;
353 x2 = x1 + window->w;
354 y2 = y1 + window->h;
355
356 /* Draw window frame. */
357 if(y >= clipy1) {
358 cputcxy(x, y, CH_ULCORNER);
adamdunkels09c28b62003-04-02 10:17:52 +0000359 gotoxy(wherex() + window->titlelen + 2, wherey());
360 chline(window->w - (wherex() - x) - 2);
361 cputcxy(x2, y, CH_URCORNER);
362 }
363
364 h = window->h;
365
366 if(clipy1 > y1) {
367 if(clipy1 - y1 < h) {
368 h = clipy1 - y1;
adamdunkels62393fb2003-07-30 23:31:56 +0000369 y1 = clipy1;
adamdunkels09c28b62003-04-02 10:17:52 +0000370 } else {
371 h = 0;
372 }
373 }
374
adamdunkels62393fb2003-07-30 23:31:56 +0000375 if(clipy2 < y1 + h) {
adamdunkels09c28b62003-04-02 10:17:52 +0000376 if(y1 >= clipy2) {
377 h = 0;
378 } else {
379 h = clipy2 - y1;
380 }
381 }
adamdunkels62393fb2003-07-30 23:31:56 +0000382
adamdunkels09c28b62003-04-02 10:17:52 +0000383 cvlinexy(x, y1, h);
adamdunkels62393fb2003-07-30 23:31:56 +0000384 cvlinexy(x2, y1, h);
adamdunkels09c28b62003-04-02 10:17:52 +0000385
386 if(y + window->h >= clipy1 &&
387 y + window->h < clipy2) {
388 cputcxy(x, y2, CH_LLCORNER);
389 chlinexy(x1, y2, window->w);
390 cputcxy(x2, y2, CH_LRCORNER);
391 }
392
adamdunkelsa45d8a12003-04-08 07:22:13 +0000393 draw_window_contents(window, focus & CTK_FOCUS_WINDOW, clipy1, clipy2,
adamdunkels62393fb2003-07-30 23:31:56 +0000394 x1, x2, y + 1, y2);
adamdunkels09c28b62003-04-02 10:17:52 +0000395}
396/*-----------------------------------------------------------------------------------*/
397void
398ctk_draw_dialog(struct ctk_window *dialog)
399{
400 unsigned char x, y;
401 unsigned char i;
adamdunkels09c28b62003-04-02 10:17:52 +0000402 unsigned char x1, y1, x2, y2;
403
404 textcolor(DIALOGCOLOR);
405
406 x = dialog->x;
407 y = dialog->y + 1;
408
409
410 x1 = x + 1;
411 y1 = y + 1;
412 x2 = x1 + dialog->w;
413 y2 = y1 + dialog->h;
414
415
416 /* Draw dialog frame. */
417
418 cvlinexy(x, y1,
419 dialog->h);
420 cvlinexy(x2, y1,
421 dialog->h);
422
423 chlinexy(x1, y,
424 dialog->w);
425 chlinexy(x1, y2,
426 dialog->w);
427
428 cputcxy(x, y, CH_ULCORNER);
429 cputcxy(x, y2, CH_LLCORNER);
430 cputcxy(x2, y, CH_URCORNER);
431 cputcxy(x2, y2, CH_LRCORNER);
432
433
adamdunkels09c28b62003-04-02 10:17:52 +0000434 /* Clear dialog contents. */
435 for(i = y1; i < y2; ++i) {
436 cclearxy(x1, i, dialog->w);
437 }
adamdunkels09c28b62003-04-02 10:17:52 +0000438
adamdunkelsa45d8a12003-04-08 07:22:13 +0000439 draw_window_contents(dialog, CTK_FOCUS_DIALOG, 0, sizey,
440 x1, x2, y1, y2);
adamdunkels09c28b62003-04-02 10:17:52 +0000441}
442/*-----------------------------------------------------------------------------------*/
443void
444ctk_draw_clear(unsigned char y1, unsigned char y2)
445{
446 unsigned char i;
447
448 for(i = y1; i < y2; ++i) {
449 cclearxy(0, i, sizex);
450 }
451}
452/*-----------------------------------------------------------------------------------*/
453static void
454draw_menu(struct ctk_menu *m)
455{
456 unsigned char x, x2, y;
457 textcolor(OPENMENUCOLOR);
oliverschmidtd9a91072004-07-29 11:49:45 +0000458 revers(0);
adamdunkels09c28b62003-04-02 10:17:52 +0000459 x = wherex();
460 cputs(m->title);
461 cputc(' ');
462 x2 = wherex();
463 if(x + CTK_CONF_MENUWIDTH > sizex) {
464 x = sizex - CTK_CONF_MENUWIDTH;
465 }
466
467
468 for(y = 0; y < m->nitems; ++y) {
469 if(y == m->active) {
470 textcolor(ACTIVEMENUITEMCOLOR);
471 revers(0);
472 } else {
473 textcolor(MENUCOLOR);
oliverschmidtd9a91072004-07-29 11:49:45 +0000474 revers(1);
adamdunkels09c28b62003-04-02 10:17:52 +0000475 }
476 gotoxy(x, y + 1);
477 if(m->items[y].title[0] == '-') {
478 chline(CTK_CONF_MENUWIDTH);
479 } else {
480 cputs(m->items[y].title);
481 }
482 if(x + CTK_CONF_MENUWIDTH > wherex()) {
483 cclear(x + CTK_CONF_MENUWIDTH - wherex());
484 }
adamdunkels09c28b62003-04-02 10:17:52 +0000485 }
486 gotoxy(x2, 0);
487 textcolor(MENUCOLOR);
oliverschmidtd9a91072004-07-29 11:49:45 +0000488 revers(1);
adamdunkels09c28b62003-04-02 10:17:52 +0000489}
490/*-----------------------------------------------------------------------------------*/
491void
492ctk_draw_menus(struct ctk_menus *menus)
493{
494 struct ctk_menu *m;
adamdunkels62393fb2003-07-30 23:31:56 +0000495
adamdunkels09c28b62003-04-02 10:17:52 +0000496 /* Draw menus */
497 textcolor(MENUCOLOR);
498 gotoxy(0, 0);
499 revers(1);
adamdunkelse68091d2003-04-16 18:29:10 +0000500 cputc(' ');
adamdunkels09c28b62003-04-02 10:17:52 +0000501 for(m = menus->menus->next; m != NULL; m = m->next) {
502 if(m != menus->open) {
503 cputs(m->title);
504 cputc(' ');
505 } else {
506 draw_menu(m);
507 }
508 }
509
510
511 if(wherex() + strlen(menus->desktopmenu->title) + 1>= sizex) {
512 gotoxy(sizex - strlen(menus->desktopmenu->title) - 1, 0);
513 } else {
514 cclear(sizex - wherex() -
515 strlen(menus->desktopmenu->title) - 1);
516 }
517
518 /* Draw desktopmenu */
519 if(menus->desktopmenu != menus->open) {
520 cputs(menus->desktopmenu->title);
521 cputc(' ');
522 } else {
523 draw_menu(menus->desktopmenu);
524 }
525
526 revers(0);
527}
528/*-----------------------------------------------------------------------------------*/
529unsigned char
530ctk_draw_height(void)
531{
532 return sizey;
533}
534/*-----------------------------------------------------------------------------------*/
535unsigned char
536ctk_draw_width(void)
537{
538 return sizex;
539}
540/*-----------------------------------------------------------------------------------*/
adamdunkelsca2cbe52004-07-04 11:38:43 +0000541int
542ctk_arch_isprint(char c)
543{
544 return isprint(c);
545}