blob: c541df8dc3cc543c22d834031660d6b795f86de9 [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 *
oliverschmidtbeb0c882005-03-15 15:55:06 +000032 * $Id: ctk-mousetext.c,v 1.13 2005/03/15 15:55:06 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:
oliverschmidt99480c12005-02-17 23:50:04 +0000123 if(wfocus != 0) {
124 revers(1);
125 } else {
oliverschmidt19032c62004-06-14 22:30:32 +0000126 revers(0);
127 }
oliverschmidt99480c12005-02-17 23:50:04 +0000128 cputcxy(xpos, ypos, '[');
129 cputsn(w->widget.button.text, w->w);
130 cputc(']');
131 revers(0);
oliverschmidt19032c62004-06-14 22:30:32 +0000132 break;
133 case CTK_WIDGET_HYPERLINK:
oliverschmidt99480c12005-02-17 23:50:04 +0000134 if(wfocus != 0) {
oliverschmidt19032c62004-06-14 22:30:32 +0000135 revers(0);
oliverschmidt99480c12005-02-17 23:50:04 +0000136 } else {
137 revers(1);
oliverschmidt19032c62004-06-14 22:30:32 +0000138 }
oliverschmidt99480c12005-02-17 23:50:04 +0000139 gotoxy(xpos, ypos);
140 cputsn(w->widget.button.text, w->w);
141 revers(0);
oliverschmidt19032c62004-06-14 22:30:32 +0000142 break;
143 case CTK_WIDGET_TEXTENTRY:
144 text = w->widget.textentry.text;
145 if(wfocus != 0) {
146 revers(1);
147 } else {
148 revers(0);
149 }
150 xscroll = 0;
151 if(w->widget.textentry.xpos >= w->w - 1) {
152 xscroll = w->widget.textentry.xpos - w->w + 1;
153 }
154 for(j = 0; j < w->h; ++j) {
oliverschmidt99480c12005-02-17 23:50:04 +0000155 if(w->widget.textentry.state == CTK_TEXTENTRY_EDIT &&
156 w->widget.textentry.ypos == j) {
157 revers(0);
158 cputcxy(xpos, ypos, '>');
159 for(i = 0; i < w->w; ++i) {
160 c = text[i + xscroll];
161 if(i == w->widget.textentry.xpos - xscroll) {
162 revers(1);
163 } else {
oliverschmidt19032c62004-06-14 22:30:32 +0000164 revers(0);
165 }
oliverschmidt99480c12005-02-17 23:50:04 +0000166 if(c == 0) {
167 cputc(' ');
168 } else {
169 cputc(c);
oliverschmidt19032c62004-06-14 22:30:32 +0000170 }
oliverschmidt99480c12005-02-17 23:50:04 +0000171 revers(0);
oliverschmidt19032c62004-06-14 22:30:32 +0000172 }
oliverschmidt99480c12005-02-17 23:50:04 +0000173 cputc('<');
174 } else {
175 cvlinexy(xpos, ypos, 1);
176 gotoxy(xpos + 1, ypos);
177 cputsn(text, w->w);
178 i = wherex();
179 if(i - xpos - 1 < w->w) {
180 cclear(w->w - (i - xpos) + 1);
181 }
182 cvline(1);
oliverschmidt19032c62004-06-14 22:30:32 +0000183 }
184 ++ypos;
185 text += w->w;
186 }
187 revers(0);
188 break;
oliverschmidtf0d728a2004-06-27 18:32:39 +0000189#if CTK_CONF_ICONS
oliverschmidt19032c62004-06-14 22:30:32 +0000190 case CTK_WIDGET_ICON:
oliverschmidt99480c12005-02-17 23:50:04 +0000191 if(wfocus != 0) {
192 revers(1);
193 } else {
oliverschmidt19032c62004-06-14 22:30:32 +0000194 revers(0);
195 }
oliverschmidt99480c12005-02-17 23:50:04 +0000196#if CTK_CONF_ICON_TEXTMAPS
197 if(w->widget.icon.textmap != NULL) {
198 for(i = 0; i < 3; ++i) {
199 gotoxy(xpos, ypos);
200 cputc(w->widget.icon.textmap[0 + 3 * i]);
201 cputc(w->widget.icon.textmap[1 + 3 * i]);
202 cputc(w->widget.icon.textmap[2 + 3 * i]);
203 ++ypos;
204 }
205 }
206#endif /* CTK_CONF_ICON_TEXTMAPS */
207
208 len = strlen(w->widget.icon.title);
209 if(xpos + len >= 80) {
210 xpos = 80 - len;
211 }
212
213 gotoxy(xpos, ypos);
214 cputs(w->widget.icon.title);
215 revers(0);
oliverschmidt19032c62004-06-14 22:30:32 +0000216 break;
oliverschmidtf0d728a2004-06-27 18:32:39 +0000217#endif /* CTK_CONF_ICONS */
oliverschmidt19032c62004-06-14 22:30:32 +0000218
219 default:
220 break;
221 }
222}
223/*-----------------------------------------------------------------------------------*/
224void
225ctk_draw_widget(struct ctk_widget *w,
226 unsigned char focus,
227 unsigned char clipy1,
228 unsigned char clipy2)
229{
230 struct ctk_window *win = w->window;
231 unsigned char posx, posy;
232
233 posx = win->x + 1;
234 posy = win->y + 2;
235
236 if(w == win->focused) {
237 focus |= CTK_FOCUS_WIDGET;
238 }
239
oliverschmidt99480c12005-02-17 23:50:04 +0000240 draw_widget(w, posx, posy, posx + win->w, posy + win->h, focus);
oliverschmidt19032c62004-06-14 22:30:32 +0000241}
242/*-----------------------------------------------------------------------------------*/
243void
244ctk_draw_clear_window(struct ctk_window *window,
245 unsigned char focus,
246 unsigned char clipy1,
247 unsigned char clipy2)
248{
249 unsigned char i;
250 unsigned char h;
251
252 h = window->y + 2 + window->h;
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000253
oliverschmidt19032c62004-06-14 22:30:32 +0000254 /* Clear window contents. */
255 for(i = window->y + 2; i < h; ++i) {
oliverschmidt99480c12005-02-17 23:50:04 +0000256 cclearxy(window->x + 1, i, window->w);
oliverschmidt19032c62004-06-14 22:30:32 +0000257 }
258}
259/*-----------------------------------------------------------------------------------*/
260static void
261draw_window_contents(struct ctk_window *window, unsigned char focus,
oliverschmidt19032c62004-06-14 22:30:32 +0000262 unsigned char x1, unsigned char x2,
263 unsigned char y1, unsigned char y2)
264{
265 struct ctk_widget *w;
266 unsigned char wfocus;
267
268 /* Draw inactive widgets. */
269 for(w = window->inactive; w != NULL; w = w->next) {
oliverschmidt99480c12005-02-17 23:50:04 +0000270 draw_widget(w, x1, y1, x2, y2, focus);
oliverschmidt19032c62004-06-14 22:30:32 +0000271 }
272
273 /* Draw active widgets. */
274 for(w = window->active; w != NULL; w = w->next) {
275 wfocus = focus;
276 if(w == window->focused) {
277 wfocus |= CTK_FOCUS_WIDGET;
278 }
279
oliverschmidt99480c12005-02-17 23:50:04 +0000280 draw_widget(w, x1, y1, x2, y2, wfocus);
oliverschmidt19032c62004-06-14 22:30:32 +0000281 }
282}
283/*-----------------------------------------------------------------------------------*/
284void
285ctk_draw_window(struct ctk_window *window, unsigned char focus,
oliverschmidtbeb0c882005-03-15 15:55:06 +0000286 unsigned char clipy1, unsigned char clipy2,
287 unsigned char draw_borders)
oliverschmidt19032c62004-06-14 22:30:32 +0000288{
289 unsigned char x, y;
oliverschmidt19032c62004-06-14 22:30:32 +0000290 unsigned char x1, y1, x2, y2;
291
oliverschmidt19032c62004-06-14 22:30:32 +0000292 x = window->x;
293 y = window->y + 1;
oliverschmidt19032c62004-06-14 22:30:32 +0000294 x1 = x + 1;
295 y1 = y + 1;
296 x2 = x1 + window->w;
297 y2 = y1 + window->h;
298
oliverschmidtbeb0c882005-03-15 15:55:06 +0000299 if(draw_borders) {
300
301 /* Draw window frame. */
302 _textframexy(x, y, window->w + 2, window->h + 2, _TEXTFRAME_TALL);
303 }
oliverschmidt19032c62004-06-14 22:30:32 +0000304
oliverschmidt99480c12005-02-17 23:50:04 +0000305 draw_window_contents(window, focus, x1, x2, y1, y2);
oliverschmidt19032c62004-06-14 22:30:32 +0000306}
307/*-----------------------------------------------------------------------------------*/
308void
309ctk_draw_dialog(struct ctk_window *dialog)
310{
311 unsigned char x, y;
312 unsigned char i;
313 unsigned char x1, y1, x2, y2;
314
315 x = dialog->x;
316 y = dialog->y + 1;
oliverschmidt19032c62004-06-14 22:30:32 +0000317 x1 = x + 1;
318 y1 = y + 1;
319 x2 = x1 + dialog->w;
320 y2 = y1 + dialog->h;
321
322 /* Draw dialog frame. */
323 _textframexy(x, y, dialog->w + 2, dialog->h + 2, _TEXTFRAME_WIDE);
324
325 /* Clear dialog contents. */
326 for(i = y1; i < y2; ++i) {
327 cclearxy(x1, i, dialog->w);
328 }
329
oliverschmidt99480c12005-02-17 23:50:04 +0000330 draw_window_contents(dialog, CTK_FOCUS_DIALOG, x1, x2, y1, y2);
oliverschmidt19032c62004-06-14 22:30:32 +0000331}
332/*-----------------------------------------------------------------------------------*/
333void
334ctk_draw_clear(unsigned char y1, unsigned char y2)
335{
oliverschmidt38e66332004-06-27 22:51:27 +0000336 char c1, c2;
337 unsigned char i;
oliverschmidt19032c62004-06-14 22:30:32 +0000338
oliverschmidt76872e82004-12-26 14:13:34 +0000339 if(ctk_draw_background) {
oliverschmidt38e66332004-06-27 22:51:27 +0000340 c1 = 'V';
341 c2 = 'W';
342 } else {
343 c1 = ' ' | 0x80;
344 c2 = ' ' | 0x80;
345 }
oliverschmidt19032c62004-06-14 22:30:32 +0000346
oliverschmidt99480c12005-02-17 23:50:04 +0000347 for(i = 1; i < 24; ++i) {
oliverschmidt38e66332004-06-27 22:51:27 +0000348 gotoxy(0, i);
oliverschmidt5867b672004-07-12 21:35:20 +0000349 *(char *)0xC055 = 0;
350 memset(*(char **)0x28, c1, 40);
351 *(char *)0xC054 = 0;
352 memset(*(char **)0x28, c2, 40);
oliverschmidt19032c62004-06-14 22:30:32 +0000353 }
oliverschmidt38e66332004-06-27 22:51:27 +0000354
oliverschmidt19032c62004-06-14 22:30:32 +0000355}
356/*-----------------------------------------------------------------------------------*/
357static void
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000358draw_menu(struct ctk_menu *m, unsigned char open)
oliverschmidt19032c62004-06-14 22:30:32 +0000359{
360 unsigned char x, x2, y;
361
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000362 if(open) {
363 x = x2 = wherex();
364 if(x2 + CTK_CONF_MENUWIDTH > 80) {
365 x2 = 80 - CTK_CONF_MENUWIDTH;
366 }
367
368 for(y = 0; y < m->nitems; ++y) {
369 if(y == m->active) {
370 revers(0);
371 } else {
372 revers(1);
373 }
374 gotoxy(x2, y + 1);
375 if(m->items[y].title[0] == '-') {
376 chline(CTK_CONF_MENUWIDTH);
377 } else {
378 cputs(m->items[y].title);
379 }
380 if(x2 + CTK_CONF_MENUWIDTH > wherex()) {
381 cclear(x2 + CTK_CONF_MENUWIDTH - wherex());
382 }
383 }
384
385 gotoxy(x, 0);
386 revers(0);
387 }
388
oliverschmidt19032c62004-06-14 22:30:32 +0000389 cputs(m->title);
390 cputc(' ');
oliverschmidt57d90a12004-07-29 11:50:24 +0000391 revers(1);
oliverschmidt19032c62004-06-14 22:30:32 +0000392}
393/*-----------------------------------------------------------------------------------*/
394void
395ctk_draw_menus(struct ctk_menus *menus)
396{
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000397 struct ctk_menu *m;
oliverschmidt19032c62004-06-14 22:30:32 +0000398
399 /* Draw menus */
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000400 textcolor(MENUCOLOR);
oliverschmidt19032c62004-06-14 22:30:32 +0000401 gotoxy(0, 0);
402 revers(1);
403 cputc(' ');
404 for(m = menus->menus->next; m != NULL; m = m->next) {
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000405 draw_menu(m, m == menus->open);
oliverschmidt19032c62004-06-14 22:30:32 +0000406 }
407
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000408 /* Draw desktopmenu */
409 if(wherex() + strlen(menus->desktopmenu->title) + 1 >= 80) {
oliverschmidt5867b672004-07-12 21:35:20 +0000410 gotoxy(80 - strlen(menus->desktopmenu->title) - 1, 0);
oliverschmidt19032c62004-06-14 22:30:32 +0000411 } else {
oliverschmidt5867b672004-07-12 21:35:20 +0000412 cclear(80 - wherex() -
oliverschmidt19032c62004-06-14 22:30:32 +0000413 strlen(menus->desktopmenu->title) - 1);
414 }
oliverschmidtcef6d6c2005-03-15 14:20:18 +0000415 draw_menu(menus->desktopmenu, menus->desktopmenu == menus->open);
oliverschmidt19032c62004-06-14 22:30:32 +0000416
417 revers(0);
418}
419/*-----------------------------------------------------------------------------------*/
420unsigned char
421ctk_draw_height(void)
422{
oliverschmidt5867b672004-07-12 21:35:20 +0000423 return 24;
oliverschmidt19032c62004-06-14 22:30:32 +0000424}
425/*-----------------------------------------------------------------------------------*/
426unsigned char
427ctk_draw_width(void)
428{
oliverschmidt5867b672004-07-12 21:35:20 +0000429 return 80;
oliverschmidt19032c62004-06-14 22:30:32 +0000430}
431/*-----------------------------------------------------------------------------------*/
oliverschmidt9e652ff2004-07-11 12:24:52 +0000432int
433ctk_arch_isprint(char c)
434{
435 return isprint(c);
436}