blob: 4c4aec738b92f40cb5832e763c40a12d2a38b61b [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 *
32 * $Id: ctk-mousetext.c,v 1.1 2004/06/14 22:30:33 oliverschmidt Exp $
33 *
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>
44
45#ifndef NULL
46#define NULL (void *)0
47#endif /* NULL */
48
49static unsigned char sizex, sizey;
50
51/*-----------------------------------------------------------------------------------*/
52static char tmp[80];
53static void
54cputsn(char *str, unsigned char len)
55{
56 strncpy(tmp, str, len);
57 tmp[len] = 0;
58 cputs(tmp);
59}
60/*-----------------------------------------------------------------------------------*/
61void
62ctk_draw_init(void)
63{
64 screensize(&sizex, &sizey);
65 ctk_draw_clear(0, sizey);
66}
67/*-----------------------------------------------------------------------------------*/
68static void
69draw_widget(struct ctk_widget *w,
70 unsigned char x, unsigned char y,
71 unsigned char clipx,
72 unsigned char clipy,
73 unsigned char clipy1, unsigned char clipy2,
74 unsigned char focus)
75{
76 unsigned char xpos, ypos, xscroll;
77 unsigned char i, j;
78 char c, *text;
79 unsigned char len, wfocus;
80
81 wfocus = 0;
82 if(focus & CTK_FOCUS_WINDOW) {
83 if(focus & CTK_FOCUS_WIDGET) {
84 wfocus = 1;
85 }
86 } else if(focus & CTK_FOCUS_DIALOG) {
87 if(focus & CTK_FOCUS_WIDGET) {
88 wfocus = 1;
89 }
90 }
91
92 xpos = x + w->x;
93 ypos = y + w->y;
94
95 switch(w->type) {
96 case CTK_WIDGET_SEPARATOR:
97 if(ypos >= clipy1 && ypos < clipy2) {
98 chlinexy(xpos, ypos, w->w);
99 }
100 break;
101 case CTK_WIDGET_LABEL:
102 text = w->widget.label.text;
103 for(i = 0; i < w->h; ++i) {
104 if(ypos >= clipy1 && ypos < clipy2) {
105 gotoxy(xpos, ypos);
106 cputsn(text, w->w);
107 if(w->w - (wherex() - xpos) > 0) {
108 cclear(w->w - (wherex() - xpos));
109 }
110 }
111 ++ypos;
112 text += w->w;
113 }
114 break;
115 case CTK_WIDGET_BUTTON:
116 if(ypos >= clipy1 && ypos < clipy2) {
117 if(wfocus != 0) {
118 revers(1);
119 } else {
120 revers(0);
121 }
122 cputcxy(xpos, ypos, '[');
123 cputsn(w->widget.button.text, w->w);
124 cputc(']');
125 revers(0);
126 }
127 break;
128 case CTK_WIDGET_HYPERLINK:
129 if(ypos >= clipy1 && ypos < clipy2) {
130 if(wfocus != 0) {
131 revers(0);
132 } else {
133 revers(1);
134 }
135 gotoxy(xpos, ypos);
136 cputsn(w->widget.button.text, w->w);
137 revers(0);
138 }
139 break;
140 case CTK_WIDGET_TEXTENTRY:
141 text = w->widget.textentry.text;
142 if(wfocus != 0) {
143 revers(1);
144 } else {
145 revers(0);
146 }
147 xscroll = 0;
148 if(w->widget.textentry.xpos >= w->w - 1) {
149 xscroll = w->widget.textentry.xpos - w->w + 1;
150 }
151 for(j = 0; j < w->h; ++j) {
152 if(ypos >= clipy1 && ypos < clipy2) {
153 if(w->widget.textentry.state == CTK_TEXTENTRY_EDIT &&
154 w->widget.textentry.ypos == j) {
155 revers(0);
156 cputcxy(xpos, ypos, '>');
157 for(i = 0; i < w->w; ++i) {
158 c = text[i + xscroll];
159 if(i == w->widget.textentry.xpos - xscroll) {
160 revers(1);
161 } else {
162 revers(0);
163 }
164 if(c == 0) {
165 cputc(' ');
166 } else {
167 cputc(c);
168 }
169 revers(0);
170 }
171 cputc('<');
172 } else {
173 cvlinexy(xpos, ypos, 1);
174 gotoxy(xpos + 1, ypos);
175 cputsn(text, w->w);
176 i = wherex();
177 if(i - xpos - 1 < w->w) {
178 cclear(w->w - (i - xpos) + 1);
179 }
180 cvline(1);
181 }
182 }
183 ++ypos;
184 text += w->w;
185 }
186 revers(0);
187 break;
188 case CTK_WIDGET_ICON:
189 if(ypos >= clipy1 && ypos < clipy2) {
190 if(focus & 1) {
191 revers(1);
192 } else {
193 revers(0);
194 }
195 gotoxy(xpos, ypos);
196 if(w->widget.icon.textmap != NULL) {
197 for(i = 0; i < 3; ++i) {
198 gotoxy(xpos, ypos);
199 if(ypos >= clipy1 && ypos < clipy2) {
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 }
204 ++ypos;
205 }
206 }
207 x = xpos;
208
209 len = strlen(w->widget.icon.title);
210 if(x + len >= sizex) {
211 x = sizex - len;
212 }
213
214 gotoxy(x, ypos);
215 if(ypos >= clipy1 && ypos < clipy2) {
216 cputs(w->widget.icon.title);
217 }
218 revers(0);
219 }
220 break;
221
222 default:
223 break;
224 }
225}
226/*-----------------------------------------------------------------------------------*/
227void
228ctk_draw_widget(struct ctk_widget *w,
229 unsigned char focus,
230 unsigned char clipy1,
231 unsigned char clipy2)
232{
233 struct ctk_window *win = w->window;
234 unsigned char posx, posy;
235
236 posx = win->x + 1;
237 posy = win->y + 2;
238
239 if(w == win->focused) {
240 focus |= CTK_FOCUS_WIDGET;
241 }
242
243 draw_widget(w, posx, posy,
244 posx + win->w,
245 posy + win->h,
246 clipy1, clipy2,
247 focus);
248}
249/*-----------------------------------------------------------------------------------*/
250void
251ctk_draw_clear_window(struct ctk_window *window,
252 unsigned char focus,
253 unsigned char clipy1,
254 unsigned char clipy2)
255{
256 unsigned char i;
257 unsigned char h;
258
259 h = window->y + 2 + window->h;
260 /* Clear window contents. */
261 for(i = window->y + 2; i < h; ++i) {
262 if(i >= clipy1 && i < clipy2) {
263 cclearxy(window->x + 1, i, window->w);
264 }
265 }
266}
267/*-----------------------------------------------------------------------------------*/
268static void
269draw_window_contents(struct ctk_window *window, unsigned char focus,
270 unsigned char clipy1, unsigned char clipy2,
271 unsigned char x1, unsigned char x2,
272 unsigned char y1, unsigned char y2)
273{
274 struct ctk_widget *w;
275 unsigned char wfocus;
276
277 /* Draw inactive widgets. */
278 for(w = window->inactive; w != NULL; w = w->next) {
279 draw_widget(w, x1, y1, x2, y2,
280 clipy1, clipy2,
281 focus);
282 }
283
284 /* Draw active widgets. */
285 for(w = window->active; w != NULL; w = w->next) {
286 wfocus = focus;
287 if(w == window->focused) {
288 wfocus |= CTK_FOCUS_WIDGET;
289 }
290
291 draw_widget(w, x1, y1, x2, y2,
292 clipy1, clipy2,
293 wfocus);
294 }
295}
296/*-----------------------------------------------------------------------------------*/
297void
298ctk_draw_window(struct ctk_window *window, unsigned char focus,
299 unsigned char clipy1, unsigned char clipy2)
300{
301 unsigned char x, y;
302 unsigned char h;
303 unsigned char x1, y1, x2, y2;
304
305 if(window->y + 1 >= clipy2) {
306 return;
307 }
308
309 x = window->x;
310 y = window->y + 1;
311
312 x1 = x + 1;
313 y1 = y + 1;
314 x2 = x1 + window->w;
315 y2 = y1 + window->h;
316
317 /* Draw window frame. */
318 _textframexy(x, y, window->w + 2, window->h + 2, _TEXTFRAME_TALL);
319
320 draw_window_contents(window, focus & CTK_FOCUS_WINDOW, clipy1, clipy2,
321 x1, x2, y + 1, y2);
322}
323/*-----------------------------------------------------------------------------------*/
324void
325ctk_draw_dialog(struct ctk_window *dialog)
326{
327 unsigned char x, y;
328 unsigned char i;
329 unsigned char x1, y1, x2, y2;
330
331 x = dialog->x;
332 y = dialog->y + 1;
333
334 x1 = x + 1;
335 y1 = y + 1;
336 x2 = x1 + dialog->w;
337 y2 = y1 + dialog->h;
338
339 /* Draw dialog frame. */
340 _textframexy(x, y, dialog->w + 2, dialog->h + 2, _TEXTFRAME_WIDE);
341
342 /* Clear dialog contents. */
343 for(i = y1; i < y2; ++i) {
344 cclearxy(x1, i, dialog->w);
345 }
346
347 draw_window_contents(dialog, CTK_FOCUS_DIALOG, 0, sizey,
348 x1, x2, y1, y2);
349}
350/*-----------------------------------------------------------------------------------*/
351void
352ctk_draw_clear(unsigned char y1, unsigned char y2)
353{
354 unsigned char x, y;
355
356 if(*config.bkgnd == 'x') {
357
358 /* Set INVFLAG to mousetext output */
359 *(unsigned char *)0x32 = 0x7F;
360
361 for(y = y1; y < y2; ++y) {
362 gotoxy(0, y);
363 for(x = 0; x < sizex; x += 2) {
364 cputs("VW");
365 }
366 }
367
368 /* Set INVFLAG to normal text output */
369 *(unsigned char *)0x32 = 0xFF;
370
371 } else {
372
373 for(y = y1; y < y2; ++y) {
374 cclearxy(0, y, sizex);
375
376 }
377 }
378}
379/*-----------------------------------------------------------------------------------*/
380static void
381draw_menu(struct ctk_menu *m)
382{
383 unsigned char x, x2, y;
384
385 x = wherex();
386 cputs(m->title);
387 cputc(' ');
388 x2 = wherex();
389 if(x + CTK_CONF_MENUWIDTH > sizex) {
390 x = sizex - CTK_CONF_MENUWIDTH;
391 }
392
393 for(y = 0; y < m->nitems; ++y) {
394 if(y == m->active) {
395 revers(0);
396 }
397 gotoxy(x, y + 1);
398 if(m->items[y].title[0] == '-') {
399 chline(CTK_CONF_MENUWIDTH);
400 } else {
401 cputs(m->items[y].title);
402 }
403 if(x + CTK_CONF_MENUWIDTH > wherex()) {
404 cclear(x + CTK_CONF_MENUWIDTH - wherex());
405 }
406 revers(1);
407 }
408 gotoxy(x2, 0);
409}
410/*-----------------------------------------------------------------------------------*/
411void
412ctk_draw_menus(struct ctk_menus *menus)
413{
414 struct ctk_menu *m;
415
416 /* Draw menus */
417 gotoxy(0, 0);
418 revers(1);
419 cputc(' ');
420 for(m = menus->menus->next; m != NULL; m = m->next) {
421 if(m != menus->open) {
422 cputs(m->title);
423 cputc(' ');
424 } else {
425 draw_menu(m);
426 }
427 }
428
429 if(wherex() + strlen(menus->desktopmenu->title) + 1>= sizex) {
430 gotoxy(sizex - strlen(menus->desktopmenu->title) - 1, 0);
431 } else {
432 cclear(sizex - wherex() -
433 strlen(menus->desktopmenu->title) - 1);
434 }
435
436 /* Draw desktopmenu */
437 if(menus->desktopmenu != menus->open) {
438 cputs(menus->desktopmenu->title);
439 cputc(' ');
440 } else {
441 draw_menu(menus->desktopmenu);
442 }
443
444 revers(0);
445}
446/*-----------------------------------------------------------------------------------*/
447unsigned char
448ctk_draw_height(void)
449{
450 return sizey;
451}
452/*-----------------------------------------------------------------------------------*/
453unsigned char
454ctk_draw_width(void)
455{
456 return sizex;
457}
458/*-----------------------------------------------------------------------------------*/