Debian Codegraph @

src/libs/gui_tk/gui_tk.cpp

raw symbols SCIP
1 #if 0
2 /*
3 * gui_tk - framework-agnostic GUI toolkit
4 * Copyright (C) 2005-2007 Jörg Walter
5 *
6 * gui_tk is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * gui_tk is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 */
19
20 /* TODO:
21 - make menu a bufferedwindow with shadow
22 */
23 /* $Id: gui_tk.cpp,v 1.5 2009-02-01 16:06:26 qbix79 Exp $ */
24
25 /** \file
26 * \brief Implementation file for gui_tk.
27 *
28 * It contains implementations for all non-inlined class methods.
29 *
30 * Also contained is a small test program that demonstrates all features of
31 * gui_tk. It is enabled by defining the preprocessor macro TESTING.
32 */
33
34 #include <SDL.h>
35 #include "gui_tk.h"
36
37 namespace GUI {
38
39 namespace Color {
40
41 RGB Background3D = 0xffc0c0c0;
42
43 RGB Light3D = 0xfffcfcfc;
44
45 RGB Shadow3D = 0xff808080;
46
47 RGB Border = 0xff000000;
48
49 RGB Text = 0xff000000;
50
51 RGB Background = 0xffc0c0c0;
52
53 RGB SelectionBackground = 0xff000080;
54
55 RGB SelectionForeground = 0xffffffff;
56
57 RGB EditableBackground = 0xffffffff;
58
59 RGB Titlebar = 0xff000080;
60
61 RGB TitlebarText = 0xffffffff;
62
63 }
64
65 void Drawable::drawText(const String& text, bool interpret, Size start, Size len) {
66 if (interpret) {
67 if (len > text.size()-start) len = (Size)(text.size()-start);
68 len += start;
69
70 Size wordstart = start;
71 int width = 0;
72
73 while (start < len) {
74 switch (font->toSpecial(text[start])) {
75 case Font::CR:
76 if (wordstart != start) {
77 drawText(text,false,wordstart,start-wordstart);
78 wordstart = start;
79 width = 0;
80 }
81 wordstart++;
82 gotoXY(0,y);
83 break;
84 case Font::LF:
85 if (wordstart != start) {
86 drawText(text,false,wordstart,start-wordstart);
87 wordstart = start;
88 width = 0;
89 }
90 wordstart++;
91 gotoXY(0,y+font->getHeight());
92 break;
93 case Font::BS:
94 if (wordstart != start) {
95 drawText(text,false,wordstart,start-wordstart);
96 wordstart = start;
97 width = 0;
98 }
99 wordstart++;
100 gotoXY(imax(0,x-font->getWidth()),y);
101 break;
102 case Font::Tab:
103 if (wordstart != start) {
104 drawText(text,false,wordstart,start-wordstart);
105 wordstart = start;
106 width = 0;
107 }
108 wordstart++;
109 gotoXY((((int)(x/font->getWidth()/8))+1)*8*font->getWidth(),y);
110 break;
111 case Font::Space:
112 if (wordstart != start) {
113 drawText(text,false,wordstart,start-wordstart);
114 wordstart = start;
115 width = 0;
116 }
117 wordstart++;
118 font->drawString(this,text,start,1);
119 break;
120 case Font::ESC: // ignore ANSI sequences except for colors
121 if (wordstart != start) {
122 drawText(text,false,wordstart,start-wordstart);
123 wordstart = start;
124 width = 0;
125 }
126 wordstart++;
127 do {
128 int seqstart = start+1;
129 Char c;
130 do {
131 start++;
132 wordstart++;
133 c = font->toSpecial(text[start]);
134 } while (start < len && ((c >= '0' && c <= '9') || c == ';' || c == '['));
135 if (c == 'm' && start < len) {
136 if (font->toSpecial(text[seqstart++]) != '[') break;
137 c = font->toSpecial(text[seqstart++]);
138 while (c != 'm') {
139 int param = 0;
140 if (c == ';') c = '0';
141 while (c != 'm' && c != ';') {
142 param = param * 10 + c-'0';
143 c = font->toSpecial(text[seqstart++]);
144 }
145 const RGB bright = 0x00808080;
146 const RGB intensity = (color&bright?~0:~bright);
147 switch (param) {
148 case 0: setColor(Color::Black); break;
149 case 1: setColor(color | 0x00808080); break;
150 case 30: setColor((Color::Black|bright) & intensity); break;
151 case 31: setColor(Color::Red & intensity); break;
152 case 32: setColor(Color::Green & intensity); break;
153 case 33: setColor(Color::Yellow & intensity); break;
154 case 34: setColor(Color::Blue & intensity); break;
155 case 35: setColor(Color::Magenta & intensity); break;
156 case 36: setColor(Color::Cyan & intensity); break;
157 case 37: setColor(Color::White & intensity); break;
158 default: break;
159 }
160 }
161 }
162 } while (0);
163 default:
164 width += font->getWidth(text[start]);
165 if (x > 0 && x+width > cw) gotoXY(0,y+font->getHeight());
166 }
167 start++;
168 }
169 if (wordstart != start) drawText(text,false,wordstart,start-wordstart);
170 return;
171 }
172
173 font->drawString(this,text,start,len);
174 }
175
176 bool ToplevelWindow::mouseDown(int x, int y, MouseButton button) {
177 if (button == Left && x > 32 && x < width-6 && y > 4 && y < 31) {
178 dragx = x;
179 dragy = y;
180 mouseChild = NULL;
181 systemMenu->setVisible(false);
182 return true;
183 } else if (button == Left && x < 32 && x > 6 && y > 4 && y < 31) {
184 mouseChild = NULL;
185 raise();
186 systemMenu->setVisible(!systemMenu->isVisible());
187 return true;
188 }
189 systemMenu->setVisible(false);
190 BorderedWindow::mouseDown(x,y,button);
191 return true;
192 }
193
194 Drawable::Drawable(int w, int h, RGB clear) :
195 buffer(new RGB[w*h]),
196 width(w), height(h),
197 owner(true),
198 color(Color::Black),
199 font(NULL),
200 lineWidth(1),
201 tx(0), ty(0),
202 cx(0), cy(0),
203 cw(w), ch(h),
204 x(0), y(0)
205 {
206 this->clear(clear);
207 }
208
209 Drawable::Drawable(Drawable &src, RGB clear) :
210 buffer(new RGB[src.cw*src.ch]),
211 width(src.cw), height(src.ch),
212 owner(true),
213 color(src.color),
214 font(src.font),
215 lineWidth(src.lineWidth),
216 tx(0), ty(0),
217 cx(0), cy(0),
218 cw(src.cw), ch(src.ch),
219 x(src.x), y(src.y)
220 {
221 if (clear != 0) {
222 this->clear(clear);
223 } else {
224 for (int h = 0; h < src.ch; h++) {
225 memcpy(buffer+src.cw*h,src.buffer+src.width*(h+src.ty)+src.tx,4*src.cw);
226 }
227 }
228 }
229
230 Drawable::Drawable(Drawable &src, int x, int y, int w, int h) :
231 buffer(src.buffer),
232 width(src.width), height(src.height),
233 owner(false),
234 color(src.color),
235 font(src.font),
236 lineWidth(src.lineWidth),
237 tx(src.tx+x), ty(src.ty+y),
238 cx(imax(imax(-x,src.cx-x),0)), cy(imax(imax(-y,src.cy-y),0)),
239 cw(imax(0,imin(src.cw-x,w))), ch(imax(0,imin(src.ch-y,h))),
240 x(imax(0,imin(src.tx-tx,cw))), y(imax(0,imin(src.ty-ty,cw)))
241 {
242 }
243
244 Drawable::~Drawable()
245 {
246 if (owner) delete[] buffer;
247 }
248
249 void Drawable::clear(RGB clear)
250 {
251 for (int y = cy; y < ch; y++) {
252 for (int x = cx; x < cw; x++) {
253 buffer[(y+ty)*width+x+tx] = clear;
254 }
255 }
256 }
257
258 void Drawable::drawLine(int x2, int y2)
259 {
260 int x0 = x2, x1 = x, y0 = y2, y1 = y;
261 int dx = x2-x1, dy = y2-y1;
262 drawPixel();
263
264 if (abs(dx) > abs(dy)) {
265 if (x1 > x2) {
266 x = x2; x2 = x1; x1 = x;
267 y = y2; y2 = y1; y1 = y;
268 }
269 for (x = x1; x <= x2; x++) {
270 y = y1+(x-x1)*dy/dx-lineWidth/2;
271 for (int i = 0; i < lineWidth; i++, y++) {
272 drawPixel();
273 }
274 }
275 } else if (y1 != y2) {
276 if (y1 > y2) {
277 x = x2; x2 = x1; x1 = x;
278 y = y2; y2 = y1; y1 = y;
279 }
280 for (y = y1; y <= y2; y ++) {
281 x = x1+(y-y1)*dx/dy-lineWidth/2;
282 for (int i = 0; i < lineWidth; i++, x++) {
283 drawPixel();
284 }
285 }
286 }
287
288 drawPixel(x0,y0);
289 }
290
291 void Drawable::drawCircle(int d) {
292 int xo = 0, yo = d/2, rest = (d+1)/2-yo, x0 = x, y0 = y, rsq = d*d/4, lwo = lineWidth/2;
293
294 while (xo <= yo) {
295 while (xo*xo+(2*yo-1)*(2*yo-1)/4 > rsq) yo--;
296 for (int i = 0, yow = yo+lwo; i < lineWidth; i++, yow--) {
297 drawPixel(x0+xo,y0-yow-rest);
298 drawPixel(x0+yow,y0-xo-rest);
299 drawPixel(x0+yow,y0+xo);
300 drawPixel(x0+xo,y0+yow);
301
302 drawPixel(x0-xo-rest,y0-yow-rest);
303 drawPixel(x0-yow-rest,y0-xo-rest);
304 drawPixel(x0-yow-rest,y0+xo);
305 drawPixel(x0-xo-rest,y0+yow);
306 }
307
308 xo++;
309 }
310 gotoXY(x0,y0);
311 }
312
313 void Drawable::drawRect(int w, int h)
314 {
315 gotoXY(x-lineWidth/2,y);
316 drawLine(x+w+lineWidth-1,y);
317 gotoXY(x-(lineWidth-1)/2,y);
318 drawLine(x,y+h);
319 gotoXY(x+(lineWidth-1)/2,y);
320 drawLine(x-w-lineWidth+1,y);
321 gotoXY(x+lineWidth/2,y);
322 drawLine(x,y-h);
323 }
324
325 void Drawable::fill()
326 {
327 int x0 = x, xmin;
328 RGB color = getPixel();
329
330 if (color == this->color) return;
331
332 for (x--; x >= 0 && getPixel() == color; x--) drawPixel();
333 xmin = ++x;
334 for (x = x0; x < cw && getPixel() == color; x++) drawPixel();
335 y++;
336 for (x--; x >= xmin; x--) {
337 if (getPixel() == color) fill();
338 y -= 2;
339 if (getPixel() == color) fill();
340 y += 2;
341 }
342 y--;
343 x = x0;
344 }
345
346 void Drawable::fillCircle(int d)
347 {
348 int xo = 0, yo = d/2, rest = (d+1)/2-yo, x0 = x, y0 = y, rsq = d*d/4;
349
350 while (xo <= yo) {
351 while (xo*xo+(2*yo-1)*(2*yo-1)/4 > rsq) yo--;
352 x = x0+xo;
353 for (y = y0-yo-rest; y <= y0+yo; y++) drawPixel();
354 x = x0-xo-rest;
355 for (y = y0-yo-rest; y <= y0+yo; y++) drawPixel();
356
357 y = y0-xo-rest;
358 for (x = x0-yo-rest; x <= x0+yo; x++) drawPixel();
359 y = y0+xo;
360 for (x = x0-yo-rest; x <= x0+yo; x++) drawPixel();
361
362 xo++;
363 }
364 gotoXY(x0,y0);
365 }
366
367 void Drawable::fillRect(int w, int h)
368 {
369 int x0 = x, y0 = y, w0 = w;
370 for (; h > 0; h--, y++) {
371 for (x = x0, w = w0; w > 0; w--, x++) {
372 drawPixel();
373 }
374 }
375 gotoXY(x0,y0);
376 }
377
378 void Drawable::drawDrawable(Drawable &d, unsigned char alpha)
379 {
380 int scw = d.cw, sch = d.ch, w, h;
381 RGB *src, *dest;
382
383 for (h = imax(d.cy,-ty-y); h < sch && y+h < ch; h++) {
384 src = d.buffer+d.width*(h+d.ty)+d.tx;
385 dest = buffer+width*(y+ty+h)+tx+x;
386 for (w = imax(d.cx,-tx-x); w < scw && x+w < cw; w++) {
387 RGB srcb = src[w], destb = dest[w];
388 unsigned int sop = Color::A(srcb)*((unsigned int)alpha)/255;
389 unsigned int rop = Color::A(destb) + sop - Color::A(destb)*sop/255;
390 if (rop == 0) {
391 dest[w] = Color::Transparent;
392 } else {
393 unsigned int dop = Color::A(destb)*(255-sop)/255;
394 unsigned int magval = ((destb&Color::MagentaMask)*dop+(srcb&Color::MagentaMask)*sop);
395 dest[w] = (((magval&0xffff)/rop)&Color::BlueMask) |
396 (((magval&0xffff0000)/rop)&Color::RedMask) |
397 ((((destb&Color::GreenMask)*dop+(srcb&Color::GreenMask)*sop)/rop)&Color::GreenMask) |
398 (rop<<Color::AlphaShift);
399 }
400 }
401 }
402 }
403
404 void Drawable::drawText(const Char c, bool interpret)
405 {
406 if (interpret) {
407 switch (font->toSpecial(c)) {
408 case Font::CR: gotoXY(0,y); return;
409 case Font::LF: gotoXY(0,y+font->getHeight()); return;
410 case Font::BS: gotoXY(imax(0,x-font->getWidth()),y); return;
411 case Font::Tab: gotoXY((((int)(x/font->getWidth()/8))+1)*8*font->getWidth(),y); return;
412 default: break;
413 }
414 if (font->getWidth(c)+x > cw) gotoXY(0,y+font->getHeight());
415 }
416 font->drawChar(this,c);
417 }
418
419 #define move(x) (ptr += ((x)+bit)/8-(((x)+bit)<0), bit = ((x)+bit+(((x)+bit)<0?8:0))%8)
420 void BitmapFont::drawChar(Drawable *d, const Char c) const {
421 const unsigned char *ptr = bitmap;
422 int bit = 0;
423
424 if (c > last) return;
425
426 if (char_position != NULL) {
427 ptr = char_position[c];
428 bit = 0;
429 } else {
430 move(character_step*((int)c));
431 }
432
433 int rs = row_step;
434 int w = (widths != NULL?widths[c]:width);
435 int h = (ascents != NULL?ascents[c]:height);
436 Drawable out(*d,d->getX(),d->getY()-ascent,w,h);
437
438 if (rs == 0) rs = isign(col_step)*w;
439 if (rs < 0) move(-rs*(h-1));
440 if (col_step < 0) move(abs(rs)-1);
441
442 for (int row = height-h; row < height; row++, move(rs-w*col_step)) {
443 for (int col = 0; col < w; col++, move(col_step)) {
444 if (!background_set ^ !(*ptr&(1<<bit)))
445 out.drawPixel(col,row);
446 }
447 }
448 d->gotoXY(d->getX()+w,d->getY());
449 }
450 #undef move
451
452 std::map<const char *,Font *,Font::ltstr> Font::registry;
453
454 void Timer::check(unsigned int ticks)
455 {
456 if (timers.empty()) return;
457
458 if (Timer::ticks > (Timer::ticks+ticks)) {
459 ticks -= -1-Timer::ticks;
460 check(-1-Timer::ticks);
461 }
462
463 std::multimap<unsigned int,Timer_Callback*,Timer::ltuint>::iterator old, i = timers.lower_bound(Timer::ticks+1);
464 Timer::ticks += ticks;
465
466 while (i != timers.end() && (*i).first <= Timer::ticks) {
467 Timer_Callback *c = (*i).second;
468 unsigned int time = (*i).first;
469 old = i;
470 ++i;
471 timers.erase(old);
472 unsigned int next = c->timerExpired(time);
473 if (next) add(c, time+next-Timer::ticks);
474 }
475 }
476
477 void Timer::remove(const Timer_Callback *const timer)
478 {
479 if (timers.empty()) return;
480
481 std::multimap<unsigned int,Timer_Callback*,Timer::ltuint>::iterator old, i = timers.begin();
482
483 while (i != timers.end()) {
484 old = i;
485 ++i;
486 if ((*old).second == timer) timers.erase(old);
487 }
488 }
489
490 unsigned int Timer::next()
491 {
492 if (timers.empty()) return 0;
493
494 std::multimap<unsigned int,Timer_Callback*,Timer::ltuint>::iterator i = timers.upper_bound(ticks);
495
496 if (i == timers.end()) return 0;
497 return (*i).first-Timer::ticks;
498 }
499
500 std::multimap<unsigned int,Timer_Callback*,Timer::ltuint> Timer::timers;
501 unsigned int Timer::ticks = 0;
502
503 BitmapFont::BitmapFont(const unsigned char *data, int height, int ascent, bool owner,
504 int width, bool background_set,
505 int col_step, int row_step, int character_step, Char last,
506 const int *widths, const int *ascents, const unsigned char *const* char_position,
507 const Font::SpecialChar *special) :
508 bitmap(data),
509 width(width), height(height), ascent(ascent), widths(widths), ascents(ascents),
510 background_set(background_set), col_step(col_step), row_step(row_step),
511 character_step(character_step?character_step:abs((row_step?row_step:width*col_step)*height)),
512 char_position(char_position), special(special), owner(owner), last(last)
513 {
514 }
515
516 BitmapFont::~BitmapFont() {
517 if (owner) {
518 if (bitmap != NULL) delete bitmap;
519 if (ascents != NULL) delete ascents;
520 if (widths != NULL) delete widths;
521 if (special != NULL) delete special;
522 }
523 }
524
525 Window::Window(Window *parent, int x, int y, int w, int h) :
526 width(w), height(h),
527 x(x), y(y),
528 dirty(true),
529 visible(true),
530 parent(parent),
531 mouseChild(NULL)
532 {
533 parent->addChild(this);
534 }
535
536 Window::Window() :
537 width(0), height(0),
538 x(0), y(0),
539 dirty(false),
540 visible(true),
541 parent(NULL),
542 mouseChild(NULL)
543 {
544 }
545
546
547 Window::~Window()
548 {
549 while (!children.empty()) delete children.front();
550 if (parent) parent->removeChild(this);
551 if (parent && parent->mouseChild == this) parent->mouseChild = NULL;
552 }
553
554 void Window::addChild(Window *child)
555 {
556 children.push_back(child);
557 setDirty();
558 }
559
560 void Window::removeChild(Window *child)
561 {
562 children.remove(child);
563 setDirty();
564 }
565
566 void Window::move(int x, int y)
567 {
568 this->x = x;
569 this->y = y;
570 std::list<Window_Callback*>::iterator i = movehandlers.begin();
571 bool end = (i == movehandlers.end());
572 while (!end) {
573 Window_Callback *c = *i;
574 ++i;
575 end = (i == movehandlers.end());
576 c->windowMoved(this,x,y);
577 }
578 parent->setDirty();
579 }
580
581 void Window::resize(int w, int h)
582 {
583 this->width = w;
584 this->height = h;
585 setDirty();
586 }
587
588 void Window::paintAll(Drawable &d) const
589 {
590 paint(d);
591 std::list<Window *>::const_iterator i = children.begin();
592 while (i != children.end()) {
593 Window *child = *i;
594 ++i;
595 if (child->visible) {
596 Drawable *cd = new Drawable(d,child->x,child->y,child->width,child->height);
597 child->paintAll(*cd);
598 delete cd;
599 }
600 }
601 }
602
603 bool Window::keyDown(const Key &key)
604 {
605 if (children.empty()) return false;
606 if ((*children.rbegin())->keyDown(key)) return true;
607 if (key.ctrl || key.alt || key.windows || key.special != Key::Tab) return false;
608
609 if (key.shift) {
610 std::list<Window *>::reverse_iterator i = children.rbegin(), e = children.rend();
611 ++i;
612 while (i != e && !(*i)->raise()) ++i;
613 return i != e;
614 } else {
615 std::list<Window *>::iterator i = children.begin(), e = children.end();
616 while (i != e && !(*i)->raise()) ++i;
617 return (i != e);
618 }
619 }
620
621 bool Window::keyUp(const Key &key)
622 {
623 if (children.empty()) return false;
624 return (*children.rbegin())->keyUp(key);
625 }
626
627 bool Window::mouseMoved(int x, int y)
628 {
629 std::list<Window *>::reverse_iterator i = children.rbegin();
630 bool end = (i == children.rend());
631 while (!end) {
632 Window *w = *i;
633 i++;
634 end = (i == children.rend());
635 if (w->visible && x >= w->x && x <= w->x+w->width
636 && y >= w->y && y <= w->y+w->height
637 && w->mouseMoved(x-w->x, y-w->y)) return true;
638 }
639 return false;
640 }
641
642 bool Window::mouseDragged(int x, int y, MouseButton button)
643 {
644 if (mouseChild == NULL) return false;
645 return mouseChild->mouseDragged(x-mouseChild->x, y-mouseChild->y, button);
646 }
647
648 bool Window::mouseDown(int x, int y, MouseButton button)
649 {
650 Window *last = NULL;
651 std::list<Window *>::reverse_iterator i = children.rbegin();
652 bool end = (i == children.rend());
653 while (!end) {
654 Window *w = *i;
655 i++;
656 end = (i == children.rend());
657 if (w->visible && x >= w->x && x <= w->x+w->width
658 && y >= w->y && y <= w->y+w->height
659 && (mouseChild = last = w)
660 && w->mouseDown(x-w->x, y-w->y, button)
661 && w->raise()) {
662 return true;
663 }
664 }
665 mouseChild = NULL;
666 if (last != NULL) last->raise();
667 return false;
668 }
669
670 bool Window::mouseUp(int x, int y, MouseButton button)
671 {
672 if (mouseChild == NULL) return false;
673 return mouseChild->mouseUp(x-mouseChild->x, y-mouseChild->y, button);
674 }
675
676 bool Window::mouseClicked(int x, int y, MouseButton button)
677 {
678 if (mouseChild == NULL) return false;
679 return mouseChild->mouseClicked(x-mouseChild->x, y-mouseChild->y, button);
680 }
681
682 bool Window::mouseDoubleClicked(int x, int y, MouseButton button)
683 {
684 if (mouseChild == NULL) return false;
685 return mouseChild->mouseDoubleClicked(x-mouseChild->x, y-mouseChild->y, button);
686 }
687
688 bool BorderedWindow::mouseDown(int x, int y, MouseButton button)
689 {
690 mouseChild = NULL;
691 if (x > width-border_right || y > width-border_bottom) return false;
692 x -= border_left; y -= border_top;
693 if (x < 0 || y < 0) return false;
694 return Window::mouseDown(x,y,button);
695 }
696
697 bool BorderedWindow::mouseMoved(int x, int y)
698 {
699 if (x > width-border_right || y > width-border_bottom) return false;
700 x -= border_left; y -= border_top;
701 if (x < 0 || y < 0) return false;
702 return Window::mouseMoved(x,y);
703 }
704
705 bool BorderedWindow::mouseDragged(int x, int y, MouseButton button)
706 {
707 if (x > width-border_right || y > width-border_bottom) return false;
708 x -= border_left; y -= border_top;
709 if (x < 0 || y < 0) return false;
710 return Window::mouseDragged(x,y,button);
711 }
712
713 void ToplevelWindow::paint(Drawable &d) const
714 {
715 int mask = (systemMenu->isVisible()?Color::RedMask|Color::GreenMask|Color::BlueMask:0);
716 d.clear(Color::Background);
717
718 d.setColor(Color::Border);
719 d.drawLine(0,height-1,width-1,height-1);
720 d.drawLine(width-1,0,width-1,height-1);
721
722 d.setColor(Color::Shadow3D);
723 d.drawLine(0,0,width-2,0);
724 d.drawLine(0,0,0,height-2);
725 d.drawLine(0,height-2,width-2,height-2);
726 d.drawLine(width-2,0,width-2,height-2);
727
728 d.drawLine(5,4,width-7,4);
729 d.drawLine(5,4,5,30);
730
731 d.setColor(Color::Light3D);
732 d.drawLine(1,1,width-3,1);
733 d.drawLine(1,1,1,height-3);
734
735 d.drawLine(5,31,width-6,31);
736 d.drawLine(width-6,5,width-6,31);
737
738 d.setColor(Color::Background3D^mask);
739 d.fillRect(6,5,26,26);
740 d.setColor(Color::Grey50^mask);
741 d.fillRect(9,17,20,4);
742 d.setColor(Color::Black^mask);
743 d.fillRect(8,16,20,4);
744 d.setColor(Color::White^mask);
745 d.fillRect(9,17,18,2);
746
747 d.setColor(Color::Border);
748 d.drawLine(32,5,32,30);
749
750 d.setColor(Color::Titlebar);
751 d.fillRect(33,5,width-39,26);
752
753 const Font *font = Font::getFont("title");
754 d.setColor(Color::TitlebarText);
755 d.setFont(font);
756 d.drawText(31+(width-39-font->getWidth(title))/2,5+(26-font->getHeight())/2+font->getAscent(),title,false,0);
757 }
758
759 void Input::paint(Drawable &d) const
760 {
761 d.clear(Color::EditableBackground);
762
763 d.setColor(Color::Shadow3D);
764 d.drawLine(0,0,width-2,0);
765 d.drawLine(0,0,0,height-2);
766
767 d.setColor(Color::Background3D);
768 d.drawLine(1,height-2,width-2,height-2);
769 d.drawLine(width-2,1,width-2,height-2);
770
771 d.setColor(Color::Text);
772 d.drawLine(1,1,width-3,1);
773 d.drawLine(1,1,1,height-3);
774
775 const Font *f = Font::getFont("input");
776 d.setFont(f);
777
778 Drawable d1(d,3,4,width-6,height-8);
779 Drawable dr(d1,(multi?0:-offset),(multi?-offset:0),width-6+(multi?0:offset),height-8+(multi?offset:0));
780
781 const Size start = imin(start_sel, end_sel), end = imax(start_sel, end_sel);
782 dr.drawText(0,f->getAscent()+1,text,multi,0,start);
783
784 int sx = dr.getX(), sy = dr.getY();
785 dr.drawText(text, multi, start, end-start);
786 int ex = dr.getX(), ey = dr.getY();
787
788 if (sx != ex || sy != ey) {
789 dr.setColor(Color::SelectionBackground);
790 if (sy == ey) dr.fillRect(sx,sy-f->getAscent(),ex-sx,f->getHeight()+1);
791 else {
792 dr.fillRect(sx, sy-f->getAscent(), width-sx+offset, f->getHeight() );
793 dr.fillRect(0, sy-f->getAscent()+f->getHeight(), width+offset, ey-sy-f->getHeight());
794 dr.fillRect(0, ey-f->getAscent(), ex, f->getHeight() );
795 }
796 dr.setColor(Color::SelectionForeground);
797 dr.drawText(sx, sy, text, multi, start, end-start);
798 }
799
800 dr.setColor(Color::Text);
801
802 dr.drawText(text, multi, end);
803
804 if (blink && hasFocus()) {
805 if (insert) dr.drawLine(posx,posy,posx,posy+f->getHeight()+1);
806 else dr.fillRect(posx,posy,f->getWidth(text[pos]),f->getHeight()+1);
807 }
808 }
809
810 Size Input::findPos(int x, int y) {
811 const Font *f = Font::getFont("input");
812 if (multi) y += offset;
813 else x += offset;
814 y = (y-4) / f->getHeight();
815 int line = 0;
816 Size pos = 0;
817 while (line < y && pos < text.size()) if (f->toSpecial(text[pos++]) == Font::LF) line++;
818 Drawable d(width-6,1);
819 d.setFont(f);
820 while (pos <= text.size() && d.getY() == 0 && x > d.getX()) {
821 d.drawText(String(text), multi, pos, 1);
822 pos++;
823 }
824 if (pos > 0) pos--;
825 return pos;
826 }
827
828 bool Input::mouseDown(int x, int y, MouseButton button)
829 {
830 if (button == Left || (button == Middle && start_sel == end_sel)) {
831 end_sel = start_sel = pos = findPos(x,y);
832 blink = true;
833 checkOffset();
834 }
835 if (button == Middle) keyDown(Key(0,Key::Insert,true,false,false,false));
836 return true;
837 }
838
839 bool Input::mouseDragged(int x, int y, MouseButton button)
840 {
841 if (button == Left) {
842 end_sel = pos = findPos(x,y);
843 blink = true;
844 checkOffset();
845 }
846 return true;
847 }
848
849 bool Input::keyDown(const Key &key)
850 {
851 const Font *f = Font::getFont("input");
852 switch (key.special) {
853 case Key::None:
854 if (key.ctrl) {
855 switch (key.character) {
856 case 1:
857 case 'a':
858 case 'A':
859 if (key.shift) {
860 start_sel = end_sel = pos;
861 } else {
862 start_sel = 0;
863 pos = end_sel = (Size)text.size();
864 }
865 break;
866 case 24:
867 case 'x':
868 case 'X':
869 cutSelection();
870 break;
871 case 3:
872 case 'c':
873 case 'C':
874 copySelection();
875 break;
876 case 22:
877 case 'v':
878 case 'V':
879 pasteSelection();
880 break;
881 default: printf("Ctrl-0x%x\n",key.character); break;
882 }
883 break;
884 }
885 if (start_sel != end_sel) clearSelection();
886 if (insert || pos >= text.size() ) text.insert(text.begin()+pos++,key.character);
887 else text[pos++] = key.character;
888 break;
889 case Key::Left:
890 if (pos > 0) pos--;
891 break;
892 case Key::Right:
893 if (pos < text.size()) pos++;
894 break;
895 case Key::Down:
896 if (multi) pos = findPos(posx+3, posy-offset+f->getHeight()+4);
897 break;
898 case Key::Up:
899 if (multi) pos = findPos(posx+3, posy-offset-f->getHeight()+4);
900 break;
901 case Key::Home:
902 if (multi) {
903 while (pos > 0 && f->toSpecial(text[pos-1]) != Font::LF) pos--;
904 } else pos = 0;
905 break;
906 case Key::End:
907 if (multi) {
908 while (pos < text.size() && f->toSpecial(text[pos]) != Font::LF) pos++;
909 } else pos = (Size)text.size();
910 break;
911 case Key::Backspace:
912 if (!key.shift && start_sel != end_sel) clearSelection();
913 else if (pos > 0) text.erase(text.begin()+ --pos);
914 break;
915 case Key::Delete:
916 if (key.shift) cutSelection();
917 else if (start_sel != end_sel) clearSelection();
918 else if (pos < text.size()) text.erase(text.begin()+pos);
919 break;
920 case Key::Insert:
921 if (key.ctrl) copySelection();
922 else if (key.shift) pasteSelection();
923 else insert = !insert;
924 break;
925 case Key::Enter:
926 if (multi) {
927 if (start_sel != end_sel) clearSelection();
928 if (insert || pos >= text.size() ) text.insert(text.begin()+pos++,f->fromSpecial(Font::LF));
929 else text[pos++] = f->fromSpecial(Font::LF);
930 } else executeAction(text);
931 break;
932 case Key::Tab:
933 if (multi) {
934 if (start_sel != end_sel) clearSelection();
935 if (insert || pos >= text.size() ) text.insert(text.begin()+pos++,f->fromSpecial(Font::Tab));
936 else text[pos++] = f->fromSpecial(Font::Tab);
937 } else return false;
938 break;
939 default:
940 return false;
941 }
942 if (!key.ctrl) {
943 if (!key.shift || key.special == Key::None) start_sel = end_sel = pos;
944 else end_sel = pos;
945 }
946 checkOffset();
947 blink = true;
948 return true;
949 }
950
951 void BorderedWindow::paintAll(Drawable &d) const
952 {
953 this->paint(d);
954 Drawable dchild(d,border_left,border_top,width-border_left-border_right,height-border_top-border_bottom);
955 for (std::list<Window *>::const_iterator i = children.begin(); i != children.end(); ++i) {
956 Window *child = *i;
957 if (child->isVisible()) {
958 Drawable cd(dchild,child->getX(),child->getY(),child->getWidth(),child->getHeight());
959 child->paintAll(cd);
960 }
961 }
962 }
963
964 void Button::paint(Drawable &d) const
965 {
966 int offset = -1;
967
968 if (hasFocus()) {
969 offset = 0;
970 d.setColor(Color::Border);
971 d.drawLine(0,0,width,0);
972 d.drawLine(0,0,0,height);
973
974 d.drawLine(0,height-1,width,height-1);
975 d.drawLine(width-1,0,width-1,height);
976 }
977
978 d.setColor(Color::Background3D);
979 d.fillRect(2,2,width-4,height-4);
980
981 if (pressed) {
982 d.setColor(Color::Shadow3D);
983
984 d.drawLine(1+offset,1+offset,width-2-offset,1+offset);
985 d.drawLine(1+offset,1+offset,1+offset,height-2-offset);
986 } else {
987 d.setColor(Color::Background3D);
988
989 d.drawLine(1+offset,1+offset,width-3-offset,1+offset);
990 d.drawLine(1+offset,1+offset,1+offset,height-3-offset);
991
992 d.setColor(Color::Light3D);
993
994 d.drawLine(2+offset,2+offset,width-4-offset,2+offset);
995 d.drawLine(2+offset,2+offset,2+offset,height-4-offset);
996
997 d.setColor(Color::Shadow3D);
998
999 d.drawLine(2+offset,height-3-offset,width-2-offset,height-3-offset);
1000 d.drawLine(width-3-offset,2+offset,width-3-offset,height-2-offset);
1001
1002 d.setColor(Color::Border);
1003
1004 d.drawLine(width-2-offset,1+offset,width-2-offset,height-2-offset);
1005 d.drawLine(1+offset,height-2-offset,width-2-offset,height-2-offset);
1006 }
1007 }
1008
1009 bool Checkbox::keyDown(const Key &key)
1010 {
1011 switch (key.special) {
1012 case Key::None:
1013 if (key.character != ' ') return false;
1014 case Key::Enter:
1015 break;
1016 default: return false;
1017 }
1018 mouseDown(0,0,Left);
1019 return true;
1020 }
1021
1022 bool Checkbox::keyUp(const Key &key)
1023 {
1024 if (key.ctrl || key.alt || key.windows || (key.character != ' ' && key.special != Key::Enter)) return false;
1025 mouseUp(0,0,Left);
1026 mouseClicked(0,0,Left);
1027 return true;
1028 }
1029
1030 void Checkbox::paint(Drawable &d) const
1031 {
1032 d.setColor(Color::Background3D);
1033 d.fillRect(2,(height/2)-7,14,14);
1034
1035 d.setColor(Color::Shadow3D);
1036 d.drawLine(2,(height/2)-7,13,(height/2)-7);
1037 d.drawLine(2,(height/2)-7,2,(height/2)+5);
1038
1039 d.setColor(Color::Light3D);
1040 d.drawLine(2,(height/2)+5,14,(height/2)+5);
1041 d.drawLine(14,(height/2)-7,14,(height/2)+5);
1042
1043 d.setColor(Color::EditableBackground);
1044 d.fillRect(4,(height/2)-5,9,9);
1045
1046 d.setColor(Color::Border);
1047 d.drawLine(3,(height/2)-6,12,(height/2)-6);
1048 d.drawLine(3,(height/2)-6,3,(height/2)+4);
1049
1050 if (checked) {
1051 d.setColor(Color::Text);
1052 d.drawLine(5,(height/2)-2,7,(height/2) );
1053 d.drawLine(11,(height/2)-4);
1054 d.drawLine(5,(height/2)-1,7,(height/2)+1);
1055 d.drawLine(11,(height/2)-3);
1056 d.drawLine(5,(height/2) ,7,(height/2)+2);
1057 d.drawLine(11,(height/2)-2);
1058 }
1059 }
1060
1061 Radiobox::Radiobox(Frame *parent, int x, int y, int w, int h) : BorderedWindow(static_cast<Window *>(parent),x,y,w,h,16,0,0,0), ActionEventSource("GUI::Radiobox"), checked(0)
1062 {
1063 addActionHandler(parent);
1064 }
1065
1066 bool Radiobox::keyDown(const Key &key)
1067 {
1068 switch (key.special) {
1069 case Key::None:
1070 if (key.character != ' ') return false;
1071 case Key::Enter:
1072 break;
1073 default: return false;
1074 }
1075 mouseDown(0,0,Left);
1076 return true;
1077 }
1078
1079 bool Radiobox::keyUp(const Key &key)
1080 {
1081 if (key.ctrl || key.alt || key.windows || (key.character != ' ' && key.special != Key::Enter)) return false;
1082 mouseUp(0,0,Left);
1083 mouseClicked(0,0,Left);
1084 return true;
1085 }
1086
1087 void Radiobox::paint(Drawable &d) const
1088 {
1089 d.setColor(Color::Light3D);
1090 d.drawLine(6,(height/2)+6,9,(height/2)+6);
1091 d.drawLine(4,(height/2)+5,11,(height/2)+5);
1092 d.drawLine(13,(height/2)-1,13,(height/2)+2);
1093 d.drawLine(12,(height/2)-2,12,(height/2)+4);
1094
1095 d.setColor(Color::Background3D);
1096 d.drawLine(6,(height/2)+5,9,(height/2)+5);
1097 d.drawLine(4,(height/2)+4,11,(height/2)+4);
1098 d.drawLine(12,(height/2)-1,12,(height/2)+2);
1099 d.drawLine(11,(height/2)-2,11,(height/2)+4);
1100
1101 d.setColor(Color::Shadow3D);
1102 d.drawLine(6,(height/2)-5,9,(height/2)-5);
1103 d.drawLine(4,(height/2)-4,11,(height/2)-4);
1104 d.drawLine(2,(height/2)-1,2,(height/2)+2);
1105 d.drawLine(3,(height/2)-3,3,(height/2)+4);
1106
1107 d.setColor(Color::Border);
1108 d.drawLine(6,(height/2)-4,9,(height/2)-4);
1109 d.drawLine(4,(height/2)-3,11,(height/2)-3);
1110 d.drawLine(3,(height/2)-1,3,(height/2)+2);
1111 d.drawLine(4,(height/2)-3,4,(height/2)+3);
1112
1113 d.setColor(Color::EditableBackground);
1114 d.fillRect(5,(height/2)-2,6,6);
1115 d.fillRect(4,(height/2)-1,8,4);
1116 d.fillRect(6,(height/2)-3,4,8);
1117
1118 if (checked) {
1119 d.setColor(Color::Text);
1120 d.fillRect(6,(height/2),4,2);
1121 d.fillRect(7,(height/2)-1,2,4);
1122 }
1123 }
1124
1125 void Menu::paint(Drawable &d) const
1126 {
1127 d.clear(Color::Background3D);
1128
1129 d.setColor(Color::Border);
1130 d.drawLine(0,height-1,width-1,height-1);
1131 d.drawLine(width-1,0,width-1,height-1);
1132
1133 d.setColor(Color::Shadow3D);
1134 d.drawLine(0,0,width-2,0);
1135 d.drawLine(0,0,0,height-2);
1136 d.drawLine(0,height-2,width-2,height-2);
1137 d.drawLine(width-2,0,width-2,height-2);
1138
1139 d.setColor(Color::Light3D);
1140 d.drawLine(1,1,width-3,1);
1141 d.drawLine(1,1,1,height-3);
1142
1143 d.setFont(Font::getFont("menu"));
1144 const int asc = Font::getFont("menu")->getAscent()+1;
1145 const int height = Font::getFont("menu")->getHeight()+2;
1146 int y = asc+3;
1147 int index = 0;
1148 for (std::vector<String>::const_iterator i = items.begin(); i != items.end(); ++i) {
1149 if ((*i).empty()) {
1150 d.setColor(Color::Shadow3D);
1151 d.drawLine(4,y-asc+6,width-5,y-asc+6);
1152 d.setColor(Color::Light3D);
1153 d.drawLine(4,y-asc+7,width-5,y-asc+7);
1154 y += 12;
1155 } else {
1156 if (index == selected && hasFocus()) {
1157 d.setColor(Color::SelectionBackground);
1158 d.fillRect(3,y-asc,width-6,height);
1159 d.setColor(Color::SelectionForeground);
1160 } else {
1161 d.setColor(Color::Text);
1162 }
1163 d.drawText(20,y,(*i),false,0);
1164 y += height;
1165 }
1166 index++;
1167 }
1168 }
1169
1170 void Menubar::paint(Drawable &d) const
1171 {
1172 const Font *f = Font::getFont("menu");
1173
1174 d.setColor(Color::Light3D);
1175 d.drawLine(0,height-1,width-1,height-1);
1176 d.setColor(Color::Shadow3D);
1177 d.drawLine(0,height-2,width-1,height-2);
1178
1179 d.gotoXY(7,f->getAscent()+2);
1180
1181 int index = 0;
1182 for (std::vector<Menu*>::const_iterator i = menus.begin(); i != menus.end(); ++i, ++index) {
1183 if (index == selected && (*i)->isVisible()) {
1184 int w = f->getWidth((*i)->getName());
1185 d.setColor(Color::SelectionBackground);
1186 d.fillRect(d.getX()-7,0,w+14,height-2);
1187 d.setColor(Color::SelectionForeground);
1188 d.gotoXY(d.getX()+7,f->getAscent()+2);
1189 } else {
1190 d.setColor(Color::Text);
1191 }
1192 d.drawText((*i)->getName(),false);
1193 d.gotoXY(d.getX()+14,f->getAscent()+2);
1194 }
1195 }
1196
1197 bool Button::keyDown(const Key &key)
1198 {
1199 switch (key.special) {
1200 case Key::None:
1201 if (key.character != ' ') return false;
1202 case Key::Enter:
1203 break;
1204 default: return false;
1205 }
1206 mouseDown(0,0,Left);
1207 return true;
1208 }
1209
1210 bool Button::keyUp(const Key &key)
1211 {
1212 if (key.ctrl || key.alt || key.windows || (key.character != ' ' && key.special != Key::Enter)) return false;
1213 mouseUp(0,0,Left);
1214 mouseClicked(0,0,Left);
1215 return true;
1216 }
1217
1218 void Frame::paint(Drawable &d) const {
1219 const Font *f = Font::getFont("default");
1220 const int top = (label.empty()?1:f->getAscent()/2+1);
1221
1222 d.setColor(Color::Shadow3D);
1223 d.drawLine(1,height-2,1,top);
1224 d.drawLine(8,top);
1225 d.drawLine((label.empty()?8:f->getWidth(label)+14),top,width-2,top);
1226 d.drawLine(2,height-3,width-3,height-3);
1227 d.drawLine(width-3,top+1);
1228
1229 d.setColor(Color::Light3D);
1230 d.drawLine(2,height-3,2,top+1);
1231 d.drawLine(8,top+1);
1232 d.drawLine((label.empty()?8:f->getWidth(label)+14),top+1,width-3,top+1);
1233 d.drawLine(2,height-2,width-2,height-2);
1234 d.drawLine(width-2,top+1);
1235
1236 d.setColor(Color::Text);
1237 d.drawText(11,f->getAscent()+1,label,false,0);
1238 }
1239
1240 Screen *Window::getScreen() { return (parent == NULL?dynamic_cast<Screen*>(this):parent->getScreen()); }
1241
1242 Screen::Screen(unsigned int width, unsigned int height) :
1243 Window(),
1244 buffer(new Drawable(width, height))
1245 {
1246 this->width = width;
1247 this->height = height;
1248 }
1249
1250 Screen::Screen(Drawable *d) :
1251 Window(),
1252 buffer(d)
1253 {
1254 this->width = d->getClipWidth();
1255 this->height = d->getClipHeight();
1256 }
1257
1258 Screen::~Screen()
1259 {
1260 }
1261
1262 void Screen::paint(Drawable &d) const
1263 {
1264 d.clear(Color::Transparent);
1265 }
1266
1267 unsigned int Screen::update(void *surface, unsigned int ticks)
1268 {
1269 Timer::check(ticks);
1270
1271 paintAll(*buffer);
1272 RGB *buf = buffer->buffer;
1273 for (y = 0; y < height; y++) {
1274 for (x = 0; x < width; x++, buf++) {
1275 RGB sval = surfaceToRGB(surface);
1276 RGB bval = *buf;
1277 int a = Color::A(bval);
1278 bval = ((((sval&Color::MagentaMask)*a+(bval&Color::MagentaMask)*(256-a))>>8)&Color::MagentaMask)
1279 | ((((sval&Color::GreenMask)*a+(bval&Color::GreenMask)*(256-a))>>8)&Color::GreenMask);
1280 rgbToSurface(bval, &surface);
1281 }
1282 }
1283
1284 return Timer::next();
1285 }
1286
1287 void Screen::move(int x, int y)
1288 {
1289 }
1290
1291 void Screen::resize(int w, int h)
1292 {
1293 }
1294
1295 #ifdef TESTING
1296 static void test(Drawable &d) {
1297 const int width = d.getClipWidth();
1298 const int height = d.getClipHeight();
1299
1300 d.clear(Color::rgba(0,0,255,128));
1301
1302 d.setColor(Color::Black);
1303 for (int x = 0; x < width; x += 10) d.drawLine(x,0,x,height);
1304 for (int y = 0; y < height; y += 10) d.drawLine(0,y,width,y);
1305
1306 d.setColor(Color::Red);
1307 for (int x = 10; x <= 130 ; x += 10) {
1308 d.drawLine(x,10,70,70);
1309 d.drawLine(x,130);
1310 }
1311 for (int y = 10; y <= 130 ; y += 10) {
1312 d.drawLine(10,y,70,70);
1313 d.drawLine(130,y);
1314 }
1315
1316 d.setColor(Color::Yellow);
1317 d.fillRect(150,10,30,30);
1318 d.setColor(Color::Blue);
1319 d.drawRect(30,30);
1320
1321 d.drawRect(150,60,30,30);
1322 d.setColor(Color::Yellow);
1323 d.fillRect(30,30);
1324
1325 for (int x = 0; x <= 100 ; x += 10) {
1326 d.setColor(Color::rgba(0xff,0x00,0xff,(255*x/100)&255));
1327 d.fillRect(200+2*x,0,20,20);
1328 }
1329
1330 d.setColor(Color::Yellow);
1331 d.fillCircle(210,60,40);
1332 d.setColor(Color::Blue);
1333 d.drawCircle(40);
1334
1335 d.drawCircle(210,110,40);
1336 d.setColor(Color::Yellow);
1337 d.fillCircle(40);
1338
1339 d.setColor(Color::rgba(0,0,255,128));
1340 d.fillRect(251,41,9,59);
1341 d.fillRect(251,41,59,9);
1342 d.fillRect(301,41,9,59);
1343 d.fillRect(291,91,19,9);
1344 d.fillRect(291,51,9,49);
1345 d.fillRect(261,51,39,9);
1346 d.fillRect(261,51,9,49);
1347 d.fillRect(261,91,29,9);
1348 d.fillRect(281,61,9,39);
1349 d.fillRect(271,61,19,9);
1350 d.fillRect(271,61,9,29);
1351 d.fillRect(241,41,9,59);
1352 d.fillRect(241,41,19,9);
1353 d.fillRect(241,91,19,9);
1354 d.setColor(Color::rgba(255,0,0,128));
1355 d.fill(255,64);
1356
1357 d.setColor(Color::Green);
1358 Drawable(d,500,355,30,30).fillCircle(65);
1359
1360 for (int i = 0; i <= 100; i += 10) {
1361 Drawable(d,25,155+i*3,420,30).drawDrawable(0,0,d,255*i/100);
1362 }
1363
1364 d.setColor(Color::White);
1365 d.setFont(Font::getFont("VGA14"));
1366 d.drawText(270,110,"GUI:: Test Program\n");
1367 d.drawText("Still testing\tTable\n");
1368 d.drawText("More of...\tTable\n");
1369 d.drawText("Overwrite\rXXXXXXXXX\n");
1370 d.drawText("Fake int'l chars: O\b/e\b\"\n");
1371 d.drawText("Real ones: \211\222\234\345\246\321");
1372 }
1373 #else
1374 static void test(Drawable &d) { (void)d; }
1375 #endif
1376
1377
1378 void ScreenRGB32le::paint(Drawable &d) const
1379 {
1380 parent->paint(d);
1381 test(d);
1382 }
1383
1384 static MouseButton SDL_to_GUI(const int button)
1385 {
1386 switch (button) {
1387 case SDL_BUTTON_LEFT: return GUI::Left;
1388 case SDL_BUTTON_RIGHT: return GUI::Right;
1389 case SDL_BUTTON_MIDDLE: return GUI::Middle;
1390 case SDL_BUTTON_WHEELUP: return GUI::WheelUp;
1391 case SDL_BUTTON_WHEELDOWN: return GUI::WheelDown;
1392 default: return GUI::NoButton;
1393 }
1394 }
1395
1396 static const Key SDL_to_GUI(const SDL_keysym &key)
1397 {
1398 GUI::Key::Special ksym = GUI::Key::None;
1399 switch (key.sym) {
1400 case SDLK_ESCAPE: ksym = GUI::Key::Escape; break;
1401 case SDLK_BACKSPACE: ksym = GUI::Key::Backspace; break;
1402 case SDLK_TAB: ksym = GUI::Key::Tab; break;
1403 case SDLK_LEFT: ksym = GUI::Key::Left; break;
1404 case SDLK_RIGHT: ksym = GUI::Key::Right; break;
1405 case SDLK_UP: ksym = GUI::Key::Up; break;
1406 case SDLK_DOWN: ksym = GUI::Key::Down; break;
1407 case SDLK_HOME: ksym = GUI::Key::Home; break;
1408 case SDLK_END: ksym = GUI::Key::End; break;
1409 case SDLK_DELETE: ksym = GUI::Key::Delete; break;
1410 case SDLK_INSERT: ksym = GUI::Key::Insert; break;
1411 case SDLK_RETURN: ksym = GUI::Key::Enter; break;
1412 case SDLK_MENU: ksym = GUI::Key::Menu; break;
1413 case SDLK_PAGEUP: ksym = GUI::Key::PageUp; break;
1414 case SDLK_PAGEDOWN: ksym = GUI::Key::PageDown; break;
1415 case SDLK_PRINT: ksym = GUI::Key::Print; break;
1416 case SDLK_PAUSE: ksym = GUI::Key::Pause; break;
1417 case SDLK_BREAK: ksym = GUI::Key::Break; break;
1418 case SDLK_CAPSLOCK: ksym = GUI::Key::CapsLock; break;
1419 case SDLK_NUMLOCK: ksym = GUI::Key::NumLock; break;
1420 case SDLK_SCROLLOCK: ksym = GUI::Key::ScrollLock; break;
1421 case SDLK_F1:case SDLK_F2:case SDLK_F3:case SDLK_F4:case SDLK_F5:case SDLK_F6:
1422 case SDLK_F7:case SDLK_F8:case SDLK_F9:case SDLK_F10:case SDLK_F11:case SDLK_F12:
1423 ksym = (GUI::Key::Special)(GUI::Key::F1 + key.sym-SDLK_F1);
1424 default: break;
1425 }
1426 return Key(key.unicode, ksym,
1427 (key.mod&KMOD_SHIFT)>0,
1428 (key.mod&KMOD_CTRL)>0,
1429 (key.mod&KMOD_ALT)>0,
1430 (key.mod&KMOD_META)>0);
1431 }
1432
1433 /** \brief Internal class that handles different screen bit depths and layouts the SDL way */
1434 class SDL_Drawable : public Drawable {
1435 protected:
1436 SDL_Surface *const surface;
1437
1438 public:
1439 SDL_Drawable(int width, int height, RGB clear = Color::Transparent) : Drawable(width, height, clear), surface(SDL_CreateRGBSurfaceFrom(buffer, width, height, 32, width*4, Color::RedMask, Color::GreenMask, Color::BlueMask, Color::AlphaMask)) {
1440 surface->flags |= SDL_SRCALPHA;
1441 }
1442
1443 ~SDL_Drawable() {
1444 SDL_FreeSurface(surface);
1445 }
1446
1447 void update(SDL_Surface *dest) const {
1448 SDL_BlitSurface(surface, NULL, dest, NULL);
1449 }
1450 };
1451
1452 ScreenSDL::ScreenSDL(SDL_Surface *surface) : Screen(new SDL_Drawable(surface->w, surface->h)), surface(surface), downx(0), downy(0), lastclick(0) {}
1453
1454 Ticks ScreenSDL::update(Ticks ticks)
1455 {
1456 Timer::check(ticks);
1457
1458 paintAll(*buffer);
1459 static_cast<SDL_Drawable*>(buffer)->update(surface);
1460
1461 return Timer::next();
1462 }
1463
1464 void ScreenSDL::paint(Drawable &d) const {
1465 d.clear(Color::Transparent);
1466 test(d);
1467 }
1468
1469 bool ScreenSDL::event(const SDL_Event &event) {
1470 bool rc;
1471
1472 switch (event.type) {
1473 case SDL_KEYUP: {
1474 const Key &key = SDL_to_GUI(event.key.keysym);
1475 if (key.special == GUI::Key::None && key.character == 0) break;
1476 if (key.special == GUI::Key::CapsLock || key.special == GUI::Key::NumLock) keyDown(key);
1477 return keyUp(key);
1478 }
1479 case SDL_KEYDOWN: {
1480 const Key &key = SDL_to_GUI(event.key.keysym);
1481 if (key.special == GUI::Key::None && key.character == 0) break;
1482 rc = keyDown(key);
1483 if (key.special == GUI::Key::CapsLock || key.special == GUI::Key::NumLock) keyUp(key);
1484 return rc;
1485 }
1486 case SDL_MOUSEMOTION:
1487 if (event.motion.state) {
1488 if (abs(event.motion.x-downx) <= 10 && abs(event.motion.y-downy) <= 10)
1489 break;
1490 downx = -11; downy = -11;
1491 if (event.motion.state&SDL_BUTTON(1))
1492 return mouseDragged(event.motion.x, event.motion.y, GUI::Left);
1493 else if (event.motion.state&SDL_BUTTON(2))
1494 return mouseDragged(event.motion.x, event.motion.y, GUI::Middle);
1495 else if (event.motion.state&SDL_BUTTON(3))
1496 return mouseDragged(event.motion.x, event.motion.y, GUI::Right);
1497 break;
1498 }
1499
1500 return mouseMoved(event.motion.x, event.motion.y);
1501
1502 case SDL_MOUSEBUTTONDOWN:
1503 rc = mouseDown(event.button.x, event.button.y, SDL_to_GUI(event.button.button));
1504 if (abs(event.button.x-downx) > 10 || abs(event.button.y-downy) > 10) lastclick = 0;
1505 downx = event.button.x; downy = event.button.y;
1506 return rc;
1507
1508 case SDL_MOUSEBUTTONUP:
1509 rc = mouseUp(event.button.x, event.button.y, SDL_to_GUI(event.button.button));
1510 if (abs(event.button.x-downx) < 10 && abs(event.button.y-downy) < 10) {
1511 if (lastclick == 0 || (GUI::Timer::now()-lastclick) > 20) {
1512 lastclick = GUI::Timer::now();
1513 rc |= mouseClicked(downx, downy, SDL_to_GUI(event.button.button));
1514 } else if (lastclick != 0) {
1515 rc |= mouseDoubleClicked(downx, downy, SDL_to_GUI(event.button.button));
1516 lastclick = 0;
1517 } else {
1518 lastclick = 0;
1519 }
1520 } else {
1521 lastclick = 0;
1522 }
1523 return rc;
1524 }
1525
1526 return false;
1527 }
1528
1529 }
1530
1531
1532
1533 #ifdef TESTING
1534 #include <stdio.h>
1535
1536 /** \brief A test program that serves as an example for all GUI elements.
1537 *
1538 * Note that you need SDL installed and a file "testfont.h" for this
1539 * to compile, which must contain a bitmap font declared as
1540 *
1541 * \code
1542 * static const unsigned char testfont[256 * 14] = { ... };
1543 * \endcode
1544 *
1545 * (256 chars, 8x14 fixed-width font, for example the IBM PC VGA 14-line font,
1546 * you can get it from DOSBox)
1547 *
1548 * To compile it, use a command line like this:
1549 *
1550 * \code
1551 * g++ -DTESTING `sdl-config --cflags` `sdl-config --libs` gui_tk.cpp -o testgui_tk
1552 * \endcode
1553 *
1554 */
1555
1556 int main(int argc, char *argv[])
1557 {
1558 printf("GUI:: test program\n");
1559
1560 SDL_Surface *screen;
1561
1562 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
1563 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
1564 exit(1);
1565 }
1566
1567 atexit(SDL_Quit);
1568
1569 screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
1570 if (screen == NULL) {
1571 fprintf(stderr, "Couldn't set 640x480x32 video mode: %s\n", SDL_GetError());
1572 exit(1);
1573 }
1574 printf("GUI:: color depth %i\n",screen->format->BitsPerPixel);
1575
1576 SDL_EnableUNICODE(true);
1577 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
1578
1579 #include "testfont.h"
1580 GUI::Font::addFont("default",new GUI::BitmapFont(testfont,14,10));
1581
1582 GUI::ScreenSDL guiscreen(screen);
1583 GUI::ToplevelWindow *frame = new GUI::ToplevelWindow(&guiscreen,205,100,380,250,"GUI::Frame");
1584 static struct delwin : public GUI::ActionEventSource_Callback {
1585 void actionExecuted(GUI::ActionEventSource *b, const GUI::String &arg) {
1586 dynamic_cast<GUI::ToplevelWindow *>(dynamic_cast<GUI::Button*>(b)->getParent())->close();
1587 }
1588 } dw;
1589 struct newwin : public GUI::ActionEventSource_Callback {
1590 GUI::Screen *screen;
1591 int n;
1592 void actionExecuted(GUI::ActionEventSource *b, const GUI::String &arg) {
1593 char title[256];
1594 sprintf(title,"Window %i",++n);
1595 GUI::ToplevelWindow *w = new GUI::ToplevelWindow(screen,405,100,120,150,title);
1596 GUI::Button *close = new GUI::Button(w,5,5,"Close");
1597 close->addActionHandler(&dw);
1598 }
1599 } nw;
1600 nw.screen = &guiscreen;
1601 nw.n = 0;
1602 struct quit : public GUI::ActionEventSource_Callback {
1603 GUI::ToplevelWindow *frame;
1604 void actionExecuted(GUI::ActionEventSource *b, const GUI::String &arg) {
1605 if (arg == "Quit" && b->getName() != "GUI::Input") exit(0);
1606 frame->setTitle(arg);
1607 }
1608 } ex;
1609 ex.frame = frame;
1610
1611 GUI::Button *b = new GUI::Button(frame,8,20,"Open a new Window");
1612 b->addActionHandler(&nw);
1613 (new GUI::Button(frame,200,20,"1"))->addActionHandler(&ex);
1614 (new GUI::Button(frame,235,20,"2"))->addActionHandler(&ex);
1615 b = new GUI::Button(frame,270,20,"Quit");
1616 b->addActionHandler(&ex);
1617 (new GUI::Input(frame,16,55,150))->addActionHandler(&ex);
1618
1619 printf("Title: %s\n",(const char*)frame->getTitle());
1620
1621 struct movewin : public GUI::Timer_Callback, public GUI::ToplevelWindow_Callback {
1622 public:
1623 GUI::ToplevelWindow *frame;
1624 GUI::Checkbox *cb1, *cb2;
1625 int x, y;
1626 virtual unsigned int timerExpired(unsigned int t) {
1627 if (cb2->isChecked()) frame->move(frame->getX()+x,frame->getY()+y);
1628 if (frame->getX() <= -frame->getWidth()) x = 1;
1629 if (frame->getX() >= 640) x = -1;
1630 if (frame->getY() <= -frame->getHeight()) y = 1;
1631 if (frame->getY() >= 480) y = -1;
1632 return 10;
1633 }
1634 virtual bool windowClosing(GUI::ToplevelWindow *win) {
1635 if (!cb1->isChecked()) return false;
1636 return true;
1637 }
1638 virtual void windowClosed(GUI::ToplevelWindow *win) {
1639 GUI::Timer::remove(this);
1640 }
1641 } mw;
1642 mw.frame = frame;
1643 mw.x = -1;
1644 mw.y = 1;
1645 GUI::Frame *box = new GUI::Frame(frame,16,80,300,50);
1646 mw.cb1 = new GUI::Checkbox(box,0,0,"Allow to close this window");
1647 mw.cb2 = new GUI::Checkbox(box,0,20,"Move this window");
1648 box = new GUI::Frame(frame,16,130,300,80,"Radio Buttons");
1649 (new GUI::Radiobox(box,0,0,"Normal"))->setChecked(true);
1650 new GUI::Radiobox(box,0,20,"Dynamic");
1651 new GUI::Radiobox(box,0,40,"Simple");
1652 box->addActionHandler(&ex);
1653 GUI::Timer::add(&mw,10);
1654 frame->addWindowHandler(&mw);
1655 GUI::Menubar *bar = new GUI::Menubar(frame,0,0,frame->getWidth());
1656 bar->addMenu("File");
1657 bar->addItem(0,"New...");
1658 bar->addItem(0,"Open...");
1659 bar->addItem(0,"");
1660 bar->addItem(0,"Save");
1661 bar->addItem(0,"Save as...");
1662 bar->addItem(0,"");
1663 bar->addItem(0,"Close");
1664 bar->addItem(0,"Quit");
1665 bar->addMenu("Edit");
1666 bar->addItem(1,"Undo");
1667 bar->addItem(1,"Redo");
1668 bar->addItem(1,"");
1669 bar->addItem(1,"Cut");
1670 bar->addItem(1,"Copy");
1671 bar->addItem(1,"Paste");
1672 bar->addItem(1,"");
1673 bar->addItem(1,"Select all");
1674 bar->addItem(1,"Select none");
1675 bar->addMenu("View");
1676 bar->addItem(2,"Zoom...");
1677 bar->addItem(2,"Zoom in");
1678 bar->addItem(2,"Zoom out");
1679 bar->addMenu("?");
1680 bar->addItem(3,"Manual");
1681 bar->addItem(3,"Search...");
1682 bar->addItem(3,"");
1683 bar->addItem(3,"About");
1684 bar->addActionHandler(&ex);
1685
1686 SDL_Event event;
1687 while (1) {
1688 while (SDL_PollEvent(&event)) {
1689 if (!guiscreen.event(event)) {
1690 if (event.type == SDL_QUIT) exit(0);
1691 }
1692 }
1693
1694 if (SDL_MUSTLOCK(screen)) SDL_LockSurface(screen);
1695 memset(screen->pixels,0xff,4*640*15);
1696 memset(((char *)screen->pixels)+4*640*15,0x00,4*640*450);
1697 memset(((char *)screen->pixels)+4*640*465,0xff,4*640*15);
1698 if (SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
1699 guiscreen.update(4);
1700 SDL_UpdateRect(screen, 0, 0, screen->w, screen->h);
1701
1702 SDL_Delay(40);
1703 }
1704 }
1705 #endif
1706
1707 #endif