blob: a1ce2d45fb1fd6f597ece624e188fc61a04d1ad3 [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 *
oliverschmidt57d90a12004-07-29 11:50:24 +000032 * $Id: ctk-mousetext.c,v 1.8 2004/07/29 11:50:24 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"
40#include "config.h"
41
42#include "ctk-conio-conf.h"
43#include <string.h>
oliverschmidt9e652ff2004-07-11 12:24:52 +000044#include <ctype.h>
oliverschmidt19032c62004-06-14 22:30:32 +000045
46#ifndef NULL
47#define NULL (void *)0
48#endif /* NULL */
49
oliverschmidt9e652ff2004-07-11 12:24:52 +000050unsigned char ctk_draw_windowborder_height = 1;
51unsigned char ctk_draw_windowborder_width = 1;
52unsigned char ctk_draw_windowtitle_height = 1;
53
54
oliverschmidt19032c62004-06-14 22:30:32 +000055/*-----------------------------------------------------------------------------------*/
oliverschmidt19032c62004-06-14 22:30:32 +000056static void
57cputsn(char *str, unsigned char len)
58{
oliverschmidt280d1312004-06-27 21:01:03 +000059 char c;
60
61 while(len > 0) {
62 --len;
63 c = *str;
64 if(c == 0) {
65 break;
66 }
67 cputc(c);
68 ++str;
69 }
oliverschmidt19032c62004-06-14 22:30:32 +000070}
71/*-----------------------------------------------------------------------------------*/
72void
73ctk_draw_init(void)
74{
oliverschmidt5867b672004-07-12 21:35:20 +000075 ctk_draw_clear(0, 24);
oliverschmidt19032c62004-06-14 22:30:32 +000076}
77/*-----------------------------------------------------------------------------------*/
78static void
79draw_widget(struct ctk_widget *w,
80 unsigned char x, unsigned char y,
81 unsigned char clipx,
82 unsigned char clipy,
83 unsigned char clipy1, unsigned char clipy2,
84 unsigned char focus)
85{
86 unsigned char xpos, ypos, xscroll;
87 unsigned char i, j;
88 char c, *text;
89 unsigned char len, wfocus;
90
91 wfocus = 0;
92 if(focus & CTK_FOCUS_WINDOW) {
93 if(focus & CTK_FOCUS_WIDGET) {
94 wfocus = 1;
95 }
96 } else if(focus & CTK_FOCUS_DIALOG) {
97 if(focus & CTK_FOCUS_WIDGET) {
98 wfocus = 1;
99 }
100 }
101
102 xpos = x + w->x;
103 ypos = y + w->y;
104
105 switch(w->type) {
106 case CTK_WIDGET_SEPARATOR:
107 if(ypos >= clipy1 && ypos < clipy2) {
108 chlinexy(xpos, ypos, w->w);
109 }
110 break;
111 case CTK_WIDGET_LABEL:
112 text = w->widget.label.text;
113 for(i = 0; i < w->h; ++i) {
114 if(ypos >= clipy1 && ypos < clipy2) {
115 gotoxy(xpos, ypos);
116 cputsn(text, w->w);
117 if(w->w - (wherex() - xpos) > 0) {
118 cclear(w->w - (wherex() - xpos));
119 }
120 }
121 ++ypos;
122 text += w->w;
123 }
124 break;
125 case CTK_WIDGET_BUTTON:
126 if(ypos >= clipy1 && ypos < clipy2) {
127 if(wfocus != 0) {
128 revers(1);
129 } else {
130 revers(0);
131 }
132 cputcxy(xpos, ypos, '[');
133 cputsn(w->widget.button.text, w->w);
134 cputc(']');
135 revers(0);
136 }
137 break;
138 case CTK_WIDGET_HYPERLINK:
139 if(ypos >= clipy1 && ypos < clipy2) {
140 if(wfocus != 0) {
141 revers(0);
142 } else {
143 revers(1);
144 }
145 gotoxy(xpos, ypos);
146 cputsn(w->widget.button.text, w->w);
147 revers(0);
148 }
149 break;
150 case CTK_WIDGET_TEXTENTRY:
151 text = w->widget.textentry.text;
152 if(wfocus != 0) {
153 revers(1);
154 } else {
155 revers(0);
156 }
157 xscroll = 0;
158 if(w->widget.textentry.xpos >= w->w - 1) {
159 xscroll = w->widget.textentry.xpos - w->w + 1;
160 }
161 for(j = 0; j < w->h; ++j) {
162 if(ypos >= clipy1 && ypos < clipy2) {
163 if(w->widget.textentry.state == CTK_TEXTENTRY_EDIT &&
164 w->widget.textentry.ypos == j) {
165 revers(0);
166 cputcxy(xpos, ypos, '>');
167 for(i = 0; i < w->w; ++i) {
168 c = text[i + xscroll];
169 if(i == w->widget.textentry.xpos - xscroll) {
170 revers(1);
171 } else {
172 revers(0);
173 }
174 if(c == 0) {
175 cputc(' ');
176 } else {
177 cputc(c);
178 }
179 revers(0);
180 }
181 cputc('<');
182 } else {
183 cvlinexy(xpos, ypos, 1);
184 gotoxy(xpos + 1, ypos);
185 cputsn(text, w->w);
186 i = wherex();
187 if(i - xpos - 1 < w->w) {
188 cclear(w->w - (i - xpos) + 1);
189 }
190 cvline(1);
191 }
192 }
193 ++ypos;
194 text += w->w;
195 }
196 revers(0);
197 break;
oliverschmidtf0d728a2004-06-27 18:32:39 +0000198#if CTK_CONF_ICONS
oliverschmidt19032c62004-06-14 22:30:32 +0000199 case CTK_WIDGET_ICON:
200 if(ypos >= clipy1 && ypos < clipy2) {
oliverschmidt10cb4f52004-06-27 15:05:13 +0000201 if(wfocus != 0) {
oliverschmidt19032c62004-06-14 22:30:32 +0000202 revers(1);
203 } else {
204 revers(0);
205 }
oliverschmidtf0d728a2004-06-27 18:32:39 +0000206#if CTK_CONF_ICON_TEXTMAPS
oliverschmidt19032c62004-06-14 22:30:32 +0000207 if(w->widget.icon.textmap != NULL) {
208 for(i = 0; i < 3; ++i) {
209 gotoxy(xpos, ypos);
210 if(ypos >= clipy1 && ypos < clipy2) {
211 cputc(w->widget.icon.textmap[0 + 3 * i]);
212 cputc(w->widget.icon.textmap[1 + 3 * i]);
213 cputc(w->widget.icon.textmap[2 + 3 * i]);
214 }
215 ++ypos;
216 }
217 }
oliverschmidtf0d728a2004-06-27 18:32:39 +0000218#endif /* CTK_CONF_ICON_TEXTMAPS */
oliverschmidt19032c62004-06-14 22:30:32 +0000219
220 len = strlen(w->widget.icon.title);
oliverschmidt5867b672004-07-12 21:35:20 +0000221 if(xpos + len >= 80) {
222 xpos = 80 - len;
oliverschmidt19032c62004-06-14 22:30:32 +0000223 }
224
oliverschmidtf0d728a2004-06-27 18:32:39 +0000225 gotoxy(xpos, ypos);
oliverschmidt19032c62004-06-14 22:30:32 +0000226 if(ypos >= clipy1 && ypos < clipy2) {
227 cputs(w->widget.icon.title);
228 }
229 revers(0);
230 }
231 break;
oliverschmidtf0d728a2004-06-27 18:32:39 +0000232#endif /* CTK_CONF_ICONS */
oliverschmidt19032c62004-06-14 22:30:32 +0000233
234 default:
235 break;
236 }
237}
238/*-----------------------------------------------------------------------------------*/
239void
240ctk_draw_widget(struct ctk_widget *w,
241 unsigned char focus,
242 unsigned char clipy1,
243 unsigned char clipy2)
244{
245 struct ctk_window *win = w->window;
246 unsigned char posx, posy;
247
248 posx = win->x + 1;
249 posy = win->y + 2;
250
251 if(w == win->focused) {
252 focus |= CTK_FOCUS_WIDGET;
253 }
254
255 draw_widget(w, posx, posy,
256 posx + win->w,
257 posy + win->h,
258 clipy1, clipy2,
259 focus);
260}
261/*-----------------------------------------------------------------------------------*/
262void
263ctk_draw_clear_window(struct ctk_window *window,
264 unsigned char focus,
265 unsigned char clipy1,
266 unsigned char clipy2)
267{
268 unsigned char i;
269 unsigned char h;
270
271 h = window->y + 2 + window->h;
272 /* Clear window contents. */
273 for(i = window->y + 2; i < h; ++i) {
274 if(i >= clipy1 && i < clipy2) {
275 cclearxy(window->x + 1, i, window->w);
276 }
277 }
278}
279/*-----------------------------------------------------------------------------------*/
280static void
281draw_window_contents(struct ctk_window *window, unsigned char focus,
282 unsigned char clipy1, unsigned char clipy2,
283 unsigned char x1, unsigned char x2,
284 unsigned char y1, unsigned char y2)
285{
286 struct ctk_widget *w;
287 unsigned char wfocus;
288
289 /* Draw inactive widgets. */
290 for(w = window->inactive; w != NULL; w = w->next) {
291 draw_widget(w, x1, y1, x2, y2,
292 clipy1, clipy2,
293 focus);
294 }
295
296 /* Draw active widgets. */
297 for(w = window->active; w != NULL; w = w->next) {
298 wfocus = focus;
299 if(w == window->focused) {
300 wfocus |= CTK_FOCUS_WIDGET;
301 }
302
303 draw_widget(w, x1, y1, x2, y2,
304 clipy1, clipy2,
305 wfocus);
306 }
307}
308/*-----------------------------------------------------------------------------------*/
309void
310ctk_draw_window(struct ctk_window *window, unsigned char focus,
311 unsigned char clipy1, unsigned char clipy2)
312{
313 unsigned char x, y;
oliverschmidt19032c62004-06-14 22:30:32 +0000314 unsigned char x1, y1, x2, y2;
315
316 if(window->y + 1 >= clipy2) {
317 return;
318 }
319
320 x = window->x;
321 y = window->y + 1;
322
323 x1 = x + 1;
324 y1 = y + 1;
325 x2 = x1 + window->w;
326 y2 = y1 + window->h;
327
328 /* Draw window frame. */
329 _textframexy(x, y, window->w + 2, window->h + 2, _TEXTFRAME_TALL);
330
331 draw_window_contents(window, focus & CTK_FOCUS_WINDOW, clipy1, clipy2,
332 x1, x2, y + 1, y2);
333}
334/*-----------------------------------------------------------------------------------*/
335void
336ctk_draw_dialog(struct ctk_window *dialog)
337{
338 unsigned char x, y;
339 unsigned char i;
340 unsigned char x1, y1, x2, y2;
341
342 x = dialog->x;
343 y = dialog->y + 1;
344
345 x1 = x + 1;
346 y1 = y + 1;
347 x2 = x1 + dialog->w;
348 y2 = y1 + dialog->h;
349
350 /* Draw dialog frame. */
351 _textframexy(x, y, dialog->w + 2, dialog->h + 2, _TEXTFRAME_WIDE);
352
353 /* Clear dialog contents. */
354 for(i = y1; i < y2; ++i) {
355 cclearxy(x1, i, dialog->w);
356 }
357
oliverschmidt5867b672004-07-12 21:35:20 +0000358 draw_window_contents(dialog, CTK_FOCUS_DIALOG, 0, 24,
oliverschmidt19032c62004-06-14 22:30:32 +0000359 x1, x2, y1, y2);
360}
361/*-----------------------------------------------------------------------------------*/
362void
363ctk_draw_clear(unsigned char y1, unsigned char y2)
364{
oliverschmidt38e66332004-06-27 22:51:27 +0000365 char c1, c2;
366 unsigned char i;
oliverschmidt19032c62004-06-14 22:30:32 +0000367
368 if(*config.bkgnd == 'x') {
oliverschmidt38e66332004-06-27 22:51:27 +0000369 c1 = 'V';
370 c2 = 'W';
371 } else {
372 c1 = ' ' | 0x80;
373 c2 = ' ' | 0x80;
374 }
oliverschmidt19032c62004-06-14 22:30:32 +0000375
oliverschmidt38e66332004-06-27 22:51:27 +0000376 for(i = y1; i < y2; ++i) {
377 gotoxy(0, i);
oliverschmidt5867b672004-07-12 21:35:20 +0000378 *(char *)0xC055 = 0;
379 memset(*(char **)0x28, c1, 40);
380 *(char *)0xC054 = 0;
381 memset(*(char **)0x28, c2, 40);
oliverschmidt19032c62004-06-14 22:30:32 +0000382 }
oliverschmidt38e66332004-06-27 22:51:27 +0000383
oliverschmidt19032c62004-06-14 22:30:32 +0000384}
385/*-----------------------------------------------------------------------------------*/
386static void
387draw_menu(struct ctk_menu *m)
388{
389 unsigned char x, x2, y;
390
oliverschmidt57d90a12004-07-29 11:50:24 +0000391 revers(0);
oliverschmidt19032c62004-06-14 22:30:32 +0000392 x = wherex();
393 cputs(m->title);
394 cputc(' ');
395 x2 = wherex();
oliverschmidt5867b672004-07-12 21:35:20 +0000396 if(x + CTK_CONF_MENUWIDTH > 80) {
397 x = 80 - CTK_CONF_MENUWIDTH;
oliverschmidt19032c62004-06-14 22:30:32 +0000398 }
399
400 for(y = 0; y < m->nitems; ++y) {
401 if(y == m->active) {
402 revers(0);
oliverschmidt57d90a12004-07-29 11:50:24 +0000403 } else {
404 revers(1);
oliverschmidt19032c62004-06-14 22:30:32 +0000405 }
406 gotoxy(x, y + 1);
407 if(m->items[y].title[0] == '-') {
408 chline(CTK_CONF_MENUWIDTH);
409 } else {
410 cputs(m->items[y].title);
411 }
412 if(x + CTK_CONF_MENUWIDTH > wherex()) {
413 cclear(x + CTK_CONF_MENUWIDTH - wherex());
414 }
oliverschmidt19032c62004-06-14 22:30:32 +0000415 }
416 gotoxy(x2, 0);
oliverschmidt57d90a12004-07-29 11:50:24 +0000417 revers(1);
oliverschmidt19032c62004-06-14 22:30:32 +0000418}
419/*-----------------------------------------------------------------------------------*/
420void
421ctk_draw_menus(struct ctk_menus *menus)
422{
423 struct ctk_menu *m;
424
425 /* Draw menus */
426 gotoxy(0, 0);
427 revers(1);
428 cputc(' ');
429 for(m = menus->menus->next; m != NULL; m = m->next) {
430 if(m != menus->open) {
431 cputs(m->title);
432 cputc(' ');
433 } else {
434 draw_menu(m);
435 }
436 }
437
oliverschmidt5867b672004-07-12 21:35:20 +0000438 if(wherex() + strlen(menus->desktopmenu->title) + 1>= 80) {
439 gotoxy(80 - strlen(menus->desktopmenu->title) - 1, 0);
oliverschmidt19032c62004-06-14 22:30:32 +0000440 } else {
oliverschmidt5867b672004-07-12 21:35:20 +0000441 cclear(80 - wherex() -
oliverschmidt19032c62004-06-14 22:30:32 +0000442 strlen(menus->desktopmenu->title) - 1);
443 }
444
445 /* Draw desktopmenu */
446 if(menus->desktopmenu != menus->open) {
447 cputs(menus->desktopmenu->title);
448 cputc(' ');
449 } else {
450 draw_menu(menus->desktopmenu);
451 }
452
453 revers(0);
454}
455/*-----------------------------------------------------------------------------------*/
456unsigned char
457ctk_draw_height(void)
458{
oliverschmidt5867b672004-07-12 21:35:20 +0000459 return 24;
oliverschmidt19032c62004-06-14 22:30:32 +0000460}
461/*-----------------------------------------------------------------------------------*/
462unsigned char
463ctk_draw_width(void)
464{
oliverschmidt5867b672004-07-12 21:35:20 +0000465 return 80;
oliverschmidt19032c62004-06-14 22:30:32 +0000466}
467/*-----------------------------------------------------------------------------------*/
oliverschmidt9e652ff2004-07-11 12:24:52 +0000468int
469ctk_arch_isprint(char c)
470{
471 return isprint(c);
472}