blob: 0a3a92567ef5cccc5a8a37cf538294b58071d4db [file] [log] [blame]
oliverschmidt19032c62004-06-14 22:30:32 +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.
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 *
oliverschmidt17a6fab2006-05-29 20:57:32 +000032 * $Id: ctk-mousetext.c,v 1.22 2006/05/29 20:57:32 oliverschmidt Exp $
oliverschmidt19032c62004-06-14 22:30:32 +000033 *
34 */
35
36#include <conio.h>
37
38#include "ctk.h"
39#include "ctk-draw.h"
oliverschmidt76872e82004-12-26 14:13:34 +000040#include "uip.h"
oliverschmidt19032c62004-06-14 22:30:32 +000041#include "config.h"
42
43#include "ctk-conio-conf.h"
44#include <string.h>
oliverschmidt9e652ff2004-07-11 12:24:52 +000045#include <ctype.h>
oliverschmidt19032c62004-06-14 22:30:32 +000046
47#ifndef NULL
48#define NULL (void *)0
49#endif /* NULL */
50
oliverschmidt9e652ff2004-07-11 12:24:52 +000051unsigned char ctk_draw_windowborder_height = 1;
52unsigned char ctk_draw_windowborder_width = 1;
53unsigned char ctk_draw_windowtitle_height = 1;
54
oliverschmidt76872e82004-12-26 14:13:34 +000055unsigned char ctk_draw_background;
oliverschmidt9e652ff2004-07-11 12:24:52 +000056
oliverschmidt19032c62004-06-14 22:30:32 +000057/*-----------------------------------------------------------------------------------*/
oliverschmidt19032c62004-06-14 22:30:32 +000058static void
59cputsn(char *str, unsigned char len)
60{
oliverschmidt280d1312004-06-27 21:01:03 +000061 char c;
62
63 while(len > 0) {
64 --len;
65 c = *str;
66 if(c == 0) {
67 break;
68 }
69 cputc(c);
70 ++str;
71 }
oliverschmidt19032c62004-06-14 22:30:32 +000072}
73/*-----------------------------------------------------------------------------------*/
74void
75ctk_draw_init(void)
76{
oliverschmidt5867b672004-07-12 21:35:20 +000077 ctk_draw_clear(0, 24);
oliverschmidt19032c62004-06-14 22:30:32 +000078}
79/*-----------------------------------------------------------------------------------*/
80static void
81draw_widget(struct ctk_widget *w,
82 unsigned char x, unsigned char y,
83 unsigned char clipx,
84 unsigned char clipy,
oliverschmidt19032c62004-06-14 22:30:32 +000085 unsigned char focus)
86{
87 unsigned char xpos, ypos, xscroll;
88 unsigned char i, j;
89 char c, *text;
90 unsigned char len, wfocus;
91
92 wfocus = 0;
93 if(focus & CTK_FOCUS_WINDOW) {
94 if(focus & CTK_FOCUS_WIDGET) {
95 wfocus = 1;
96 }
97 } else if(focus & CTK_FOCUS_DIALOG) {
98 if(focus & CTK_FOCUS_WIDGET) {
99 wfocus = 1;
100 }
101 }
102
103 xpos = x + w->x;
104 ypos = y + w->y;
105
106 switch(w->type) {
107 case CTK_WIDGET_SEPARATOR:
oliverschmidt99480c12005-02-17 23:50:04 +0000108 chlinexy(xpos, ypos, w->w);
oliverschmidt19032c62004-06-14 22:30:32 +0000109 break;
110 case CTK_WIDGET_LABEL:
111 text = w->widget.label.text;
112 for(i = 0; i < w->h; ++i) {
oliverschmidt99480c12005-02-17 23:50:04 +0000113 gotoxy(xpos, ypos);
114 cputsn(text, w->w);
115 if(w->w - (wherex() - xpos) > 0) {
116 cclear(w->w - (wherex() - xpos));
oliverschmidt19032c62004-06-14 22:30:32 +0000117 }
118 ++ypos;
119 text += w->w;
120 }
121 break;
122 case CTK_WIDGET_BUTTON:
oliverschmidt28b56122005-05-04 19:55:30 +0000123 revers(wfocus != 0);
oliverschmidt99480c12005-02-17 23:50:04 +0000124 cputcxy(xpos, ypos, '[');
125 cputsn(w->widget.button.text, w->w);
126 cputc(']');
127 revers(0);
oliverschmidt19032c62004-06-14 22:30:32 +0000128 break;
129 case CTK_WIDGET_HYPERLINK:
oliverschmidt28b56122005-05-04 19:55:30 +0000130 revers(wfocus == 0);
oliverschmidt99480c12005-02-17 23:50:04 +0000131 gotoxy(xpos, ypos);
132 cputsn(w->widget.button.text, w->w);
133 revers(0);
oliverschmidt19032c62004-06-14 22:30:32 +0000134 break;
135 case CTK_WIDGET_TEXTENTRY:
136 text = w->widget.textentry.text;
oliverschmidt19032c62004-06-14 22:30:32 +0000137 xscroll = 0;
138 if(w->widget.textentry.xpos >= w->w - 1) {
139 xscroll = w->widget.textentry.xpos - w->w + 1;
140 }
141 for(j = 0; j < w->h; ++j) {
oliverschmidt99480c12005-02-17 23:50:04 +0000142 if(w->widget.textentry.state == CTK_TEXTENTRY_EDIT &&
143 w->widget.textentry.ypos == j) {
144 revers(0);
145 cputcxy(xpos, ypos, '>');
oliverschmidtb5b84d82006-05-28 20:38:48 +0000146 c = 1;
oliverschmidt99480c12005-02-17 23:50:04 +0000147 for(i = 0; i < w->w; ++i) {
oliverschmidtb5b84d82006-05-28 20:38:48 +0000148 if(c != 0) {
149 c = text[i + xscroll];
150 }
oliverschmidt28b56122005-05-04 19:55:30 +0000151 revers(i == w->widget.textentry.xpos - xscroll);
oliverschmidt99480c12005-02-17 23:50:04 +0000152 if(c == 0) {
153 cputc(' ');
154 } else {
155 cputc(c);
oliverschmidt19032c62004-06-14 22:30:32 +0000156 }
oliverschmidt19032c62004-06-14 22:30:32 +0000157 }
oliverschmidt206a3b52005-05-04 22:05:37 +0000158 revers(0);
oliverschmidt99480c12005-02-17 23:50:04 +0000159 cputc('<');
160 } else {
oliverschmidt206a3b52005-05-04 22:05:37 +0000161 revers(wfocus != 0 && j == w->widget.textentry.ypos);
oliverschmidt99480c12005-02-17 23:50:04 +0000162 cvlinexy(xpos, ypos, 1);
163 gotoxy(xpos + 1, ypos);
164 cputsn(text, w->w);
165 i = wherex();
166 if(i - xpos - 1 < w->w) {
167 cclear(w->w - (i - xpos) + 1);
168 }
169 cvline(1);
oliverschmidt19032c62004-06-14 22:30:32 +0000170 }
171 ++ypos;
oliverschmidt57a90e42005-05-05 20:55:02 +0000172 text += w->widget.textentry.len + 1;
oliverschmidt19032c62004-06-14 22:30:32 +0000173 }
174 revers(0);
175 break;
oliverschmidtf0d728a2004-06-27 18:32:39 +0000176#if CTK_CONF_ICONS
oliverschmidt19032c62004-06-14 22:30:32 +0000177 case CTK_WIDGET_ICON:
oliverschmidt28b56122005-05-04 19:55:30 +0000178 revers(wfocus != 0);
oliverschmidt99480c12005-02-17 23:50:04 +0000179#if CTK_CONF_ICON_TEXTMAPS
180 if(w->widget.icon.textmap != NULL) {
181 for(i = 0; i < 3; ++i) {
182 gotoxy(xpos, ypos);
183 cputc(w->widget.icon.textmap[0 + 3 * i]);
184 cputc(w->widget.icon.textmap[1 + 3 * i]);
185 cputc(w->widget.icon.textmap[2 + 3 * i]);
186 ++ypos;
187 }
188 }
189#endif /* CTK_CONF_ICON_TEXTMAPS */
190
191 len = strlen(w->widget.icon.title);
192 if(xpos + len >= 80) {
193 xpos = 80 - len;
194 }
195
196 gotoxy(xpos, ypos);
197 cputs(w->widget.icon.title);
198 revers(0);
oliverschmidt19032c62004-06-14 22:30:32 +0000199 break;
oliverschmidtf0d728a2004-06-27 18:32:39 +0000200#endif /* CTK_CONF_ICONS */
oliverschmidt19032c62004-06-14 22:30:32 +0000201
202 default:
203 break;
204 }
205}
206/*-----------------------------------------------------------------------------------*/
207void
208ctk_draw_widget(struct ctk_widget *w,
209 unsigned char focus,
210 unsigned char clipy1,
211 unsigned char clipy2)
212{
213 struct ctk_window *win = w->window;
214 unsigned char posx, posy;
215
216 posx = win->x + 1;
217 posy = win->y + 2;
218
219 if(w == win->focused) {
220 focus |= CTK_FOCUS_WIDGET;
221 }
222
oliverschmidt99480c12005-02-17 23:50:04 +0000223 draw_widget(w, posx, posy, posx + win->w, posy + win->h, focus);
oliverschmidt19032c62004-06-14 22:30:32 +0000224}
225/*-----------------------------------------------------------------------------------*/
226void
227ctk_draw_clear_window(struct ctk_window *window,
228 unsigned char focus,
229 unsigned char clipy1,
230 unsigned char clipy2)
231{
232 unsigned char i;
233 unsigned char h;
234
235 h = window->y + 2 + window->h;
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000236
oliverschmidt19032c62004-06-14 22:30:32 +0000237 /* Clear window contents. */
238 for(i = window->y + 2; i < h; ++i) {
oliverschmidt99480c12005-02-17 23:50:04 +0000239 cclearxy(window->x + 1, i, window->w);
oliverschmidt19032c62004-06-14 22:30:32 +0000240 }
241}
242/*-----------------------------------------------------------------------------------*/
243static void
244draw_window_contents(struct ctk_window *window, unsigned char focus,
oliverschmidt19032c62004-06-14 22:30:32 +0000245 unsigned char x1, unsigned char x2,
246 unsigned char y1, unsigned char y2)
247{
248 struct ctk_widget *w;
249 unsigned char wfocus;
250
251 /* Draw inactive widgets. */
252 for(w = window->inactive; w != NULL; w = w->next) {
oliverschmidt99480c12005-02-17 23:50:04 +0000253 draw_widget(w, x1, y1, x2, y2, focus);
oliverschmidt19032c62004-06-14 22:30:32 +0000254 }
255
256 /* Draw active widgets. */
257 for(w = window->active; w != NULL; w = w->next) {
258 wfocus = focus;
259 if(w == window->focused) {
260 wfocus |= CTK_FOCUS_WIDGET;
261 }
262
oliverschmidt99480c12005-02-17 23:50:04 +0000263 draw_widget(w, x1, y1, x2, y2, wfocus);
oliverschmidt19032c62004-06-14 22:30:32 +0000264 }
265}
266/*-----------------------------------------------------------------------------------*/
oliverschmidt17a6fab2006-05-29 20:57:32 +0000267#pragma codeseg(push, "CODE");
268/*-----------------------------------------------------------------------------------*/
oliverschmidt19032c62004-06-14 22:30:32 +0000269void
270ctk_draw_window(struct ctk_window *window, unsigned char focus,
oliverschmidtbeb0c882005-03-15 15:55:06 +0000271 unsigned char clipy1, unsigned char clipy2,
272 unsigned char draw_borders)
oliverschmidt19032c62004-06-14 22:30:32 +0000273{
274 unsigned char x, y;
oliverschmidt19032c62004-06-14 22:30:32 +0000275 unsigned char x1, y1, x2, y2;
276
oliverschmidt19032c62004-06-14 22:30:32 +0000277 x = window->x;
278 y = window->y + 1;
oliverschmidt19032c62004-06-14 22:30:32 +0000279 x1 = x + 1;
280 y1 = y + 1;
281 x2 = x1 + window->w;
282 y2 = y1 + window->h;
283
oliverschmidtbeb0c882005-03-15 15:55:06 +0000284 if(draw_borders) {
285
286 /* Draw window frame. */
287 _textframexy(x, y, window->w + 2, window->h + 2, _TEXTFRAME_TALL);
288 }
oliverschmidt19032c62004-06-14 22:30:32 +0000289
oliverschmidt99480c12005-02-17 23:50:04 +0000290 draw_window_contents(window, focus, x1, x2, y1, y2);
oliverschmidt19032c62004-06-14 22:30:32 +0000291}
292/*-----------------------------------------------------------------------------------*/
293void
294ctk_draw_dialog(struct ctk_window *dialog)
295{
296 unsigned char x, y;
297 unsigned char i;
298 unsigned char x1, y1, x2, y2;
299
300 x = dialog->x;
301 y = dialog->y + 1;
oliverschmidt19032c62004-06-14 22:30:32 +0000302 x1 = x + 1;
303 y1 = y + 1;
304 x2 = x1 + dialog->w;
305 y2 = y1 + dialog->h;
306
307 /* Draw dialog frame. */
308 _textframexy(x, y, dialog->w + 2, dialog->h + 2, _TEXTFRAME_WIDE);
309
310 /* Clear dialog contents. */
311 for(i = y1; i < y2; ++i) {
312 cclearxy(x1, i, dialog->w);
313 }
314
oliverschmidt99480c12005-02-17 23:50:04 +0000315 draw_window_contents(dialog, CTK_FOCUS_DIALOG, x1, x2, y1, y2);
oliverschmidt19032c62004-06-14 22:30:32 +0000316}
317/*-----------------------------------------------------------------------------------*/
oliverschmidt17a6fab2006-05-29 20:57:32 +0000318#pragma codeseg(pop);
319/*-----------------------------------------------------------------------------------*/
oliverschmidt19032c62004-06-14 22:30:32 +0000320void
321ctk_draw_clear(unsigned char y1, unsigned char y2)
322{
oliverschmidt38e66332004-06-27 22:51:27 +0000323 char c1, c2;
324 unsigned char i;
oliverschmidt19032c62004-06-14 22:30:32 +0000325
oliverschmidt76872e82004-12-26 14:13:34 +0000326 if(ctk_draw_background) {
oliverschmidt38e66332004-06-27 22:51:27 +0000327 c1 = 'V';
328 c2 = 'W';
329 } else {
330 c1 = ' ' | 0x80;
331 c2 = ' ' | 0x80;
332 }
oliverschmidt19032c62004-06-14 22:30:32 +0000333
oliverschmidt99480c12005-02-17 23:50:04 +0000334 for(i = 1; i < 24; ++i) {
oliverschmidt2c67f162005-05-12 23:57:37 +0000335 gotoy(i);
oliverschmidt5867b672004-07-12 21:35:20 +0000336 *(char *)0xC055 = 0;
337 memset(*(char **)0x28, c1, 40);
338 *(char *)0xC054 = 0;
339 memset(*(char **)0x28, c2, 40);
oliverschmidt19032c62004-06-14 22:30:32 +0000340 }
oliverschmidt19032c62004-06-14 22:30:32 +0000341}
342/*-----------------------------------------------------------------------------------*/
343static void
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000344draw_menu(struct ctk_menu *m, unsigned char open)
oliverschmidt19032c62004-06-14 22:30:32 +0000345{
346 unsigned char x, x2, y;
347
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000348 if(open) {
349 x = x2 = wherex();
350 if(x2 + CTK_CONF_MENUWIDTH > 80) {
351 x2 = 80 - CTK_CONF_MENUWIDTH;
352 }
353
354 for(y = 0; y < m->nitems; ++y) {
355 if(y == m->active) {
356 revers(0);
357 } else {
358 revers(1);
359 }
360 gotoxy(x2, y + 1);
361 if(m->items[y].title[0] == '-') {
362 chline(CTK_CONF_MENUWIDTH);
363 } else {
364 cputs(m->items[y].title);
365 }
366 if(x2 + CTK_CONF_MENUWIDTH > wherex()) {
367 cclear(x2 + CTK_CONF_MENUWIDTH - wherex());
368 }
369 }
370
371 gotoxy(x, 0);
372 revers(0);
373 }
374
oliverschmidt19032c62004-06-14 22:30:32 +0000375 cputs(m->title);
376 cputc(' ');
oliverschmidt57d90a12004-07-29 11:50:24 +0000377 revers(1);
oliverschmidt19032c62004-06-14 22:30:32 +0000378}
379/*-----------------------------------------------------------------------------------*/
380void
381ctk_draw_menus(struct ctk_menus *menus)
382{
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000383 struct ctk_menu *m;
oliverschmidt19032c62004-06-14 22:30:32 +0000384
385 /* Draw menus */
386 gotoxy(0, 0);
387 revers(1);
388 cputc(' ');
389 for(m = menus->menus->next; m != NULL; m = m->next) {
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000390 draw_menu(m, m == menus->open);
oliverschmidt19032c62004-06-14 22:30:32 +0000391 }
392
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000393 /* Draw desktopmenu */
394 if(wherex() + strlen(menus->desktopmenu->title) + 1 >= 80) {
oliverschmidt5867b672004-07-12 21:35:20 +0000395 gotoxy(80 - strlen(menus->desktopmenu->title) - 1, 0);
oliverschmidt19032c62004-06-14 22:30:32 +0000396 } else {
oliverschmidt5867b672004-07-12 21:35:20 +0000397 cclear(80 - wherex() -
oliverschmidt19032c62004-06-14 22:30:32 +0000398 strlen(menus->desktopmenu->title) - 1);
399 }
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000400 draw_menu(menus->desktopmenu, menus->desktopmenu == menus->open);
oliverschmidt19032c62004-06-14 22:30:32 +0000401
402 revers(0);
403}
404/*-----------------------------------------------------------------------------------*/
405unsigned char
406ctk_draw_height(void)
407{
oliverschmidt5867b672004-07-12 21:35:20 +0000408 return 24;
oliverschmidt19032c62004-06-14 22:30:32 +0000409}
410/*-----------------------------------------------------------------------------------*/
411unsigned char
412ctk_draw_width(void)
413{
oliverschmidt5867b672004-07-12 21:35:20 +0000414 return 80;
oliverschmidt19032c62004-06-14 22:30:32 +0000415}
416/*-----------------------------------------------------------------------------------*/
oliverschmidtf88ee842005-04-17 22:40:11 +0000417int
418ctk_arch_isprint(char c)
419{
420 return isprint(c);
421}