blob: 384276a677b18ff5f45e9d75ce91093c0ed90eb0 [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 *
oliverschmidt57a90e42005-05-05 20:55:02 +000032 * $Id: ctk-mousetext.c,v 1.19 2005/05/05 20:55:02 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, '>');
146 for(i = 0; i < w->w; ++i) {
147 c = text[i + xscroll];
oliverschmidt28b56122005-05-04 19:55:30 +0000148 revers(i == w->widget.textentry.xpos - xscroll);
oliverschmidt99480c12005-02-17 23:50:04 +0000149 if(c == 0) {
150 cputc(' ');
151 } else {
152 cputc(c);
oliverschmidt19032c62004-06-14 22:30:32 +0000153 }
oliverschmidt19032c62004-06-14 22:30:32 +0000154 }
oliverschmidt206a3b52005-05-04 22:05:37 +0000155 revers(0);
oliverschmidt99480c12005-02-17 23:50:04 +0000156 cputc('<');
157 } else {
oliverschmidt206a3b52005-05-04 22:05:37 +0000158 revers(wfocus != 0 && j == w->widget.textentry.ypos);
oliverschmidt99480c12005-02-17 23:50:04 +0000159 cvlinexy(xpos, ypos, 1);
160 gotoxy(xpos + 1, ypos);
161 cputsn(text, w->w);
162 i = wherex();
163 if(i - xpos - 1 < w->w) {
164 cclear(w->w - (i - xpos) + 1);
165 }
166 cvline(1);
oliverschmidt19032c62004-06-14 22:30:32 +0000167 }
168 ++ypos;
oliverschmidt57a90e42005-05-05 20:55:02 +0000169 text += w->widget.textentry.len + 1;
oliverschmidt19032c62004-06-14 22:30:32 +0000170 }
171 revers(0);
172 break;
oliverschmidtf0d728a2004-06-27 18:32:39 +0000173#if CTK_CONF_ICONS
oliverschmidt19032c62004-06-14 22:30:32 +0000174 case CTK_WIDGET_ICON:
oliverschmidt28b56122005-05-04 19:55:30 +0000175 revers(wfocus != 0);
oliverschmidt99480c12005-02-17 23:50:04 +0000176#if CTK_CONF_ICON_TEXTMAPS
177 if(w->widget.icon.textmap != NULL) {
178 for(i = 0; i < 3; ++i) {
179 gotoxy(xpos, ypos);
180 cputc(w->widget.icon.textmap[0 + 3 * i]);
181 cputc(w->widget.icon.textmap[1 + 3 * i]);
182 cputc(w->widget.icon.textmap[2 + 3 * i]);
183 ++ypos;
184 }
185 }
186#endif /* CTK_CONF_ICON_TEXTMAPS */
187
188 len = strlen(w->widget.icon.title);
189 if(xpos + len >= 80) {
190 xpos = 80 - len;
191 }
192
193 gotoxy(xpos, ypos);
194 cputs(w->widget.icon.title);
195 revers(0);
oliverschmidt19032c62004-06-14 22:30:32 +0000196 break;
oliverschmidtf0d728a2004-06-27 18:32:39 +0000197#endif /* CTK_CONF_ICONS */
oliverschmidt19032c62004-06-14 22:30:32 +0000198
199 default:
200 break;
201 }
202}
203/*-----------------------------------------------------------------------------------*/
204void
205ctk_draw_widget(struct ctk_widget *w,
206 unsigned char focus,
207 unsigned char clipy1,
208 unsigned char clipy2)
209{
210 struct ctk_window *win = w->window;
211 unsigned char posx, posy;
212
213 posx = win->x + 1;
214 posy = win->y + 2;
215
216 if(w == win->focused) {
217 focus |= CTK_FOCUS_WIDGET;
218 }
219
oliverschmidt99480c12005-02-17 23:50:04 +0000220 draw_widget(w, posx, posy, posx + win->w, posy + win->h, focus);
oliverschmidt19032c62004-06-14 22:30:32 +0000221}
222/*-----------------------------------------------------------------------------------*/
223void
224ctk_draw_clear_window(struct ctk_window *window,
225 unsigned char focus,
226 unsigned char clipy1,
227 unsigned char clipy2)
228{
229 unsigned char i;
230 unsigned char h;
231
232 h = window->y + 2 + window->h;
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000233
oliverschmidt19032c62004-06-14 22:30:32 +0000234 /* Clear window contents. */
235 for(i = window->y + 2; i < h; ++i) {
oliverschmidt99480c12005-02-17 23:50:04 +0000236 cclearxy(window->x + 1, i, window->w);
oliverschmidt19032c62004-06-14 22:30:32 +0000237 }
238}
239/*-----------------------------------------------------------------------------------*/
240static void
241draw_window_contents(struct ctk_window *window, unsigned char focus,
oliverschmidt19032c62004-06-14 22:30:32 +0000242 unsigned char x1, unsigned char x2,
243 unsigned char y1, unsigned char y2)
244{
245 struct ctk_widget *w;
246 unsigned char wfocus;
247
248 /* Draw inactive widgets. */
249 for(w = window->inactive; w != NULL; w = w->next) {
oliverschmidt99480c12005-02-17 23:50:04 +0000250 draw_widget(w, x1, y1, x2, y2, focus);
oliverschmidt19032c62004-06-14 22:30:32 +0000251 }
252
253 /* Draw active widgets. */
254 for(w = window->active; w != NULL; w = w->next) {
255 wfocus = focus;
256 if(w == window->focused) {
257 wfocus |= CTK_FOCUS_WIDGET;
258 }
259
oliverschmidt99480c12005-02-17 23:50:04 +0000260 draw_widget(w, x1, y1, x2, y2, wfocus);
oliverschmidt19032c62004-06-14 22:30:32 +0000261 }
262}
263/*-----------------------------------------------------------------------------------*/
264void
265ctk_draw_window(struct ctk_window *window, unsigned char focus,
oliverschmidtbeb0c882005-03-15 15:55:06 +0000266 unsigned char clipy1, unsigned char clipy2,
267 unsigned char draw_borders)
oliverschmidt19032c62004-06-14 22:30:32 +0000268{
269 unsigned char x, y;
oliverschmidt19032c62004-06-14 22:30:32 +0000270 unsigned char x1, y1, x2, y2;
271
oliverschmidt19032c62004-06-14 22:30:32 +0000272 x = window->x;
273 y = window->y + 1;
oliverschmidt19032c62004-06-14 22:30:32 +0000274 x1 = x + 1;
275 y1 = y + 1;
276 x2 = x1 + window->w;
277 y2 = y1 + window->h;
278
oliverschmidtbeb0c882005-03-15 15:55:06 +0000279 if(draw_borders) {
280
281 /* Draw window frame. */
282 _textframexy(x, y, window->w + 2, window->h + 2, _TEXTFRAME_TALL);
283 }
oliverschmidt19032c62004-06-14 22:30:32 +0000284
oliverschmidt99480c12005-02-17 23:50:04 +0000285 draw_window_contents(window, focus, x1, x2, y1, y2);
oliverschmidt19032c62004-06-14 22:30:32 +0000286}
287/*-----------------------------------------------------------------------------------*/
288void
289ctk_draw_dialog(struct ctk_window *dialog)
290{
291 unsigned char x, y;
292 unsigned char i;
293 unsigned char x1, y1, x2, y2;
294
295 x = dialog->x;
296 y = dialog->y + 1;
oliverschmidt19032c62004-06-14 22:30:32 +0000297 x1 = x + 1;
298 y1 = y + 1;
299 x2 = x1 + dialog->w;
300 y2 = y1 + dialog->h;
301
302 /* Draw dialog frame. */
303 _textframexy(x, y, dialog->w + 2, dialog->h + 2, _TEXTFRAME_WIDE);
304
305 /* Clear dialog contents. */
306 for(i = y1; i < y2; ++i) {
307 cclearxy(x1, i, dialog->w);
308 }
309
oliverschmidt99480c12005-02-17 23:50:04 +0000310 draw_window_contents(dialog, CTK_FOCUS_DIALOG, x1, x2, y1, y2);
oliverschmidt19032c62004-06-14 22:30:32 +0000311}
312/*-----------------------------------------------------------------------------------*/
313void
314ctk_draw_clear(unsigned char y1, unsigned char y2)
315{
oliverschmidt38e66332004-06-27 22:51:27 +0000316 char c1, c2;
317 unsigned char i;
oliverschmidt19032c62004-06-14 22:30:32 +0000318
oliverschmidt76872e82004-12-26 14:13:34 +0000319 if(ctk_draw_background) {
oliverschmidt38e66332004-06-27 22:51:27 +0000320 c1 = 'V';
321 c2 = 'W';
322 } else {
323 c1 = ' ' | 0x80;
324 c2 = ' ' | 0x80;
325 }
oliverschmidt19032c62004-06-14 22:30:32 +0000326
oliverschmidt99480c12005-02-17 23:50:04 +0000327 for(i = 1; i < 24; ++i) {
oliverschmidt38e66332004-06-27 22:51:27 +0000328 gotoxy(0, i);
oliverschmidt5867b672004-07-12 21:35:20 +0000329 *(char *)0xC055 = 0;
330 memset(*(char **)0x28, c1, 40);
331 *(char *)0xC054 = 0;
332 memset(*(char **)0x28, c2, 40);
oliverschmidt19032c62004-06-14 22:30:32 +0000333 }
oliverschmidt19032c62004-06-14 22:30:32 +0000334}
335/*-----------------------------------------------------------------------------------*/
336static void
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000337draw_menu(struct ctk_menu *m, unsigned char open)
oliverschmidt19032c62004-06-14 22:30:32 +0000338{
339 unsigned char x, x2, y;
340
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000341 if(open) {
342 x = x2 = wherex();
343 if(x2 + CTK_CONF_MENUWIDTH > 80) {
344 x2 = 80 - CTK_CONF_MENUWIDTH;
345 }
346
347 for(y = 0; y < m->nitems; ++y) {
348 if(y == m->active) {
349 revers(0);
350 } else {
351 revers(1);
352 }
353 gotoxy(x2, y + 1);
354 if(m->items[y].title[0] == '-') {
355 chline(CTK_CONF_MENUWIDTH);
356 } else {
357 cputs(m->items[y].title);
358 }
359 if(x2 + CTK_CONF_MENUWIDTH > wherex()) {
360 cclear(x2 + CTK_CONF_MENUWIDTH - wherex());
361 }
362 }
363
364 gotoxy(x, 0);
365 revers(0);
366 }
367
oliverschmidt19032c62004-06-14 22:30:32 +0000368 cputs(m->title);
369 cputc(' ');
oliverschmidt57d90a12004-07-29 11:50:24 +0000370 revers(1);
oliverschmidt19032c62004-06-14 22:30:32 +0000371}
372/*-----------------------------------------------------------------------------------*/
373void
374ctk_draw_menus(struct ctk_menus *menus)
375{
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000376 struct ctk_menu *m;
oliverschmidt19032c62004-06-14 22:30:32 +0000377
378 /* Draw menus */
379 gotoxy(0, 0);
380 revers(1);
381 cputc(' ');
382 for(m = menus->menus->next; m != NULL; m = m->next) {
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000383 draw_menu(m, m == menus->open);
oliverschmidt19032c62004-06-14 22:30:32 +0000384 }
385
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000386 /* Draw desktopmenu */
387 if(wherex() + strlen(menus->desktopmenu->title) + 1 >= 80) {
oliverschmidt5867b672004-07-12 21:35:20 +0000388 gotoxy(80 - strlen(menus->desktopmenu->title) - 1, 0);
oliverschmidt19032c62004-06-14 22:30:32 +0000389 } else {
oliverschmidt5867b672004-07-12 21:35:20 +0000390 cclear(80 - wherex() -
oliverschmidt19032c62004-06-14 22:30:32 +0000391 strlen(menus->desktopmenu->title) - 1);
392 }
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000393 draw_menu(menus->desktopmenu, menus->desktopmenu == menus->open);
oliverschmidt19032c62004-06-14 22:30:32 +0000394
395 revers(0);
396}
397/*-----------------------------------------------------------------------------------*/
398unsigned char
399ctk_draw_height(void)
400{
oliverschmidt5867b672004-07-12 21:35:20 +0000401 return 24;
oliverschmidt19032c62004-06-14 22:30:32 +0000402}
403/*-----------------------------------------------------------------------------------*/
404unsigned char
405ctk_draw_width(void)
406{
oliverschmidt5867b672004-07-12 21:35:20 +0000407 return 80;
oliverschmidt19032c62004-06-14 22:30:32 +0000408}
409/*-----------------------------------------------------------------------------------*/
oliverschmidtf88ee842005-04-17 22:40:11 +0000410int
411ctk_arch_isprint(char c)
412{
413 return isprint(c);
414}