| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
r |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
#ifndef GUI__TOOLKIT_H |
| 108 |
#define GUI__TOOLKIT_H |
| 109 |
|
| 110 |
#define imin(x,y) ((x)<(y)?(x):(y)) |
| 111 |
#define imax(x,y) ((x)>(y)?(x):(y)) |
| 112 |
#define isign(x) (((x)<0?-1:1)) |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
#ifdef _MSC_VER |
| 121 |
typedef signed __int8 int8_t; |
| 122 |
typedef signed __int16 int16_t; |
| 123 |
typedef signed __int32 int32_t; |
| 124 |
typedef unsigned __int8 uint8_t; |
| 125 |
typedef unsigned __int16 uint16_t; |
| 126 |
typedef unsigned __int32 uint32_t; |
| 127 |
#else |
| 128 |
#include <stdint.h> |
| 129 |
#endif |
| 130 |
|
| 131 |
#include <list> |
| 132 |
#include <map> |
| 133 |
#include <vector> |
| 134 |
#include <typeinfo> |
| 135 |
#include <string> |
| 136 |
#include <iostream> |
| 137 |
|
| 138 |
|
| 139 |
namespace GUI { |
| 140 |
|
| 141 |
|
| 142 |
typedef uint32_t RGB; |
| 143 |
|
| 144 |
|
| 145 |
namespace Color { |
| 146 |
|
| 147 |
const RGB Transparent = 0x00ffffff; |
| 148 |
|
| 149 |
|
| 150 |
const RGB Black = 0xff000000; |
| 151 |
|
| 152 |
|
| 153 |
const RGB White = 0xffffffff; |
| 154 |
|
| 155 |
|
| 156 |
const RGB AlphaMask = 0xff000000; |
| 157 |
|
| 158 |
|
| 159 |
const int AlphaShift = 24; |
| 160 |
|
| 161 |
|
| 162 |
const RGB RedMask = 0x00ff0000; |
| 163 |
|
| 164 |
|
| 165 |
const RGB Red = Black|RedMask; |
| 166 |
|
| 167 |
|
| 168 |
const int RedShift = 16; |
| 169 |
|
| 170 |
|
| 171 |
const RGB GreenMask = 0x0000ff00; |
| 172 |
|
| 173 |
|
| 174 |
const RGB Green = Black|GreenMask; |
| 175 |
|
| 176 |
|
| 177 |
const int GreenShift = 8; |
| 178 |
|
| 179 |
|
| 180 |
const RGB BlueMask = 0x000000ff; |
| 181 |
|
| 182 |
|
| 183 |
const RGB Blue = Black|BlueMask; |
| 184 |
|
| 185 |
|
| 186 |
const int BlueShift = 0; |
| 187 |
|
| 188 |
|
| 189 |
const RGB Magenta = Red|Blue; |
| 190 |
|
| 191 |
|
| 192 |
const RGB MagentaMask = RedMask|BlueMask; |
| 193 |
|
| 194 |
|
| 195 |
const RGB Cyan = Green|Blue; |
| 196 |
|
| 197 |
|
| 198 |
const RGB CyanMask = GreenMask|BlueMask; |
| 199 |
|
| 200 |
|
| 201 |
const RGB Yellow = Red|Green; |
| 202 |
|
| 203 |
|
| 204 |
const RGB YellowMask = RedMask|GreenMask; |
| 205 |
|
| 206 |
|
| 207 |
const RGB Grey50 = 0xff808080; |
| 208 |
|
| 209 |
|
| 210 |
extern RGB Background3D; |
| 211 |
|
| 212 |
|
| 213 |
extern RGB Light3D; |
| 214 |
|
| 215 |
|
| 216 |
extern RGB Shadow3D; |
| 217 |
|
| 218 |
|
| 219 |
extern RGB Border; |
| 220 |
|
| 221 |
|
| 222 |
extern RGB Text; |
| 223 |
|
| 224 |
|
| 225 |
extern RGB Background; |
| 226 |
|
| 227 |
|
| 228 |
extern RGB SelectionBackground; |
| 229 |
|
| 230 |
|
| 231 |
extern RGB SelectionForeground; |
| 232 |
|
| 233 |
|
| 234 |
extern RGB EditableBackground; |
| 235 |
|
| 236 |
|
| 237 |
extern RGB Titlebar; |
| 238 |
|
| 239 |
|
| 240 |
extern RGB TitlebarText; |
| 241 |
|
| 242 |
|
| 243 |
static inline RGB rgba(int r, int g, int b, int a=0) { |
| 244 |
return (((r&255)<<RedShift)|((g&255)<<GreenShift)|((b&255)<<BlueShift)|((a&255)<<AlphaShift)); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
static inline int R(RGB val) { return ((val&Color::RedMask)>>Color::RedShift); } |
| 249 |
|
| 250 |
static inline int G(RGB val) { return ((val&Color::GreenMask)>>Color::GreenShift); } |
| 251 |
|
| 252 |
static inline int B(RGB val) { return ((val&Color::BlueMask)>>Color::BlueShift); } |
| 253 |
|
| 254 |
static inline int A(RGB val) { return ((val&Color::AlphaMask)>>Color::AlphaShift); } |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
enum MouseButton { NoButton, Left, Right, Middle, WheelUp, WheelDown, WheelLeft, WheelRight }; |
| 260 |
|
| 261 |
|
| 262 |
typedef unsigned int Size; |
| 263 |
|
| 264 |
|
| 265 |
typedef uint32_t Char; |
| 266 |
|
| 267 |
|
| 268 |
typedef unsigned int Ticks; |
| 269 |
|
| 270 |
|
| 271 |
class Key { |
| 272 |
public: |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
Char character; |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
enum Special { |
| 282 |
None, |
| 283 |
F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, |
| 284 |
Up, Down, Left, Right, Backspace, Tab, Backtab, Enter, Escape, |
| 285 |
Home, End, PageUp, PageDown, Insert, Delete, Menu, |
| 286 |
Print, Pause, Break, CapsLock, NumLock, ScrollLock, |
| 287 |
Alt, Ctrl, Shift, Windows |
| 288 |
} special; |
| 289 |
|
| 290 |
bool shift; |
| 291 |
|
| 292 |
bool ctrl; |
| 293 |
|
| 294 |
bool alt; |
| 295 |
|
| 296 |
|
| 297 |
bool windows; |
| 298 |
|
| 299 |
|
| 300 |
Key(int character, Special special, bool shift, bool ctrl, bool alt, bool windows) : |
| 301 |
character(character), special(special), |
| 302 |
shift(shift), ctrl(ctrl), alt(alt), windows(windows) {} |
| 303 |
}; |
| 304 |
|
| 305 |
class Drawable; |
| 306 |
class String; |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
template <typename STR> class NativeString { |
| 326 |
protected: |
| 327 |
friend class String; |
| 328 |
|
| 329 |
|
| 330 |
static void getString(String &dest, const STR &src) { STR::_this_string_type_is_not_supported_(); } |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
static STR& getNative(const String &src) { STR::_this_string_type_is_not_supported_();return*new STR(); } |
| 338 |
}; |
| 339 |
|
| 340 |
template <typename STR> class NativeString<STR*> { |
| 341 |
protected: |
| 342 |
friend class String; |
| 343 |
static void getString(String &dest, const STR *src); |
| 344 |
static STR* getNative(const String &src); |
| 345 |
}; |
| 346 |
|
| 347 |
template <typename STR, Size N> class NativeString<STR[N]> : public NativeString<STR*> {}; |
| 348 |
template <typename STR, Size N> class NativeString<const STR[N]> : public NativeString<STR*> {}; |
| 349 |
template <typename STR> class NativeString<const STR*> : public NativeString<STR*> {}; |
| 350 |
|
| 351 |
|
| 352 |
struct ltvoid { bool operator()(const void* s1, const void* s2) const { return s1 < s2; } }; |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
class String : public std::vector<Char> { |
| 365 |
protected: |
| 366 |
template <typename STR> friend class NativeString; |
| 367 |
|
| 368 |
class Native { public: virtual ~Native() {}; }; |
| 369 |
|
| 370 |
template <typename STR> class NativeArray: public Native { |
| 371 |
STR *data; |
| 372 |
public: |
| 373 |
NativeArray(STR *data) : data(data) {} |
| 374 |
virtual ~NativeArray() { delete[] data; } |
| 375 |
}; |
| 376 |
template <typename STR> class NativeObject: public Native { |
| 377 |
STR *data; |
| 378 |
public: |
| 379 |
NativeObject(STR *data) : data(data) {} |
| 380 |
virtual ~NativeObject() { delete data; } |
| 381 |
}; |
| 382 |
|
| 383 |
private: |
| 384 |
|
| 385 |
mutable std::map<const class std::type_info *, Native *, ltvoid> strings; |
| 386 |
|
| 387 |
protected: |
| 388 |
|
| 389 |
void addNative(Native *dest) const { |
| 390 |
const class std::type_info &type = typeid(dest); |
| 391 |
if (strings[&type] != NULL) delete strings[&type]; |
| 392 |
strings[&type] = dest; |
| 393 |
} |
| 394 |
|
| 395 |
public: |
| 396 |
|
| 397 |
template <typename STR> String(const STR& src) { NativeString<STR>::getString(*this, src); } |
| 398 |
|
| 399 |
|
| 400 |
template <class InputIterator> String(InputIterator a, InputIterator b) : std::vector<Char>(a,b) {} |
| 401 |
|
| 402 |
|
| 403 |
String(const String &src) : std::vector<Char>(src), strings() {}; |
| 404 |
|
| 405 |
|
| 406 |
String() { } |
| 407 |
|
| 408 |
|
| 409 |
~String() { |
| 410 |
for (std::map<const class std::type_info *, Native *, ltvoid>::iterator i = strings.begin(); i != strings.end(); ++i) |
| 411 |
delete (*i).second; |
| 412 |
}; |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
template <typename T> operator T() const { return NativeString<T>::getNative(*this); } |
| 418 |
|
| 419 |
|
| 420 |
template <typename T> bool operator==(const T &src) const { return *this == String(src); } |
| 421 |
|
| 422 |
bool operator==(const String &src) const { return *(std::vector<Char>*)this == src; } |
| 423 |
|
| 424 |
|
| 425 |
template <typename T> bool operator!=(const T &src) const { return *this != String(src); } |
| 426 |
|
| 427 |
bool operator!=(const String &src) const { return *(std::vector<Char>*)this != src; } |
| 428 |
}; |
| 429 |
|
| 430 |
template <typename STR> void NativeString<STR*>::getString(String &dest, const STR* src) { |
| 431 |
Size strlen = 0; |
| 432 |
while (src[strlen]) strlen++; |
| 433 |
dest.resize(strlen); |
| 434 |
for (strlen = 0; src[strlen]; strlen++) dest[strlen] = (sizeof(STR)==1?(unsigned char)src[strlen]:sizeof(STR)==2?(unsigned short)src[strlen]:src[strlen]); |
| 435 |
} |
| 436 |
|
| 437 |
template <typename STR> STR* NativeString<STR*>::getNative(const String &src) { |
| 438 |
Size strlen = (Size)src.size(); |
| 439 |
STR* dest = new STR[strlen+1]; |
| 440 |
dest[strlen] = 0; |
| 441 |
for (; strlen > 0; strlen--) dest[strlen-1] = src[strlen-1]; |
| 442 |
src.addNative(new String::NativeArray<const STR>(dest)); |
| 443 |
return dest; |
| 444 |
} |
| 445 |
|
| 446 |
template <> class NativeString<std::string*> { |
| 447 |
protected: |
| 448 |
friend class String; |
| 449 |
static void getString(String &dest, const std::string *src) { |
| 450 |
Size strlen = (Size)src->length(); |
| 451 |
dest.resize(strlen); |
| 452 |
for (Size i = 0; i< strlen; i++) dest[i] = (*src)[i]; |
| 453 |
} |
| 454 |
static std::string* getNative(const String &src) { |
| 455 |
Size strlen = (Size)src.size(); |
| 456 |
std::string* dest = new std::string(); |
| 457 |
for (Size i = 0; i < strlen; i++) dest->append(1,src[i]); |
| 458 |
src.addNative(new String::NativeObject<std::string>(dest)); |
| 459 |
return dest; |
| 460 |
} |
| 461 |
}; |
| 462 |
|
| 463 |
template <> class NativeString<const std::string*> : public NativeString<std::string*> {}; |
| 464 |
|
| 465 |
template <> class NativeString<std::string> { |
| 466 |
protected: |
| 467 |
friend class String; |
| 468 |
static void getString(String &dest, const std::string &src) { |
| 469 |
Size strlen = (Size)src.length(); |
| 470 |
dest.resize(strlen); |
| 471 |
for (Size i = 0; i< strlen; i++) dest[i] = src[i]; |
| 472 |
} |
| 473 |
static std::string& getNative(const String &src) { |
| 474 |
Size strlen = (Size)src.size(); |
| 475 |
std::string* dest = new std::string(); |
| 476 |
for (Size i = 0; i < strlen; i++) dest->append(1,src[i]); |
| 477 |
src.addNative(new String::NativeObject<std::string>(dest)); |
| 478 |
return *dest; |
| 479 |
} |
| 480 |
}; |
| 481 |
|
| 482 |
template <> class NativeString<const std::string> : public NativeString<std::string> {}; |
| 483 |
|
| 484 |
class ToplevelWindow; |
| 485 |
class Menu; |
| 486 |
class TransientWindow; |
| 487 |
class Screen; |
| 488 |
class Window; |
| 489 |
|
| 490 |
|
| 491 |
struct Window_Callback { |
| 492 |
public: |
| 493 |
|
| 494 |
virtual void windowMoved(Window *win, int x, int y) = 0; |
| 495 |
virtual ~Window_Callback() {} |
| 496 |
}; |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
class Window { |
| 509 |
protected: |
| 510 |
friend class ToplevelWindow; |
| 511 |
friend class TransientWindow; |
| 512 |
friend class Menu; |
| 513 |
|
| 514 |
|
| 515 |
int width; |
| 516 |
|
| 517 |
int height; |
| 518 |
|
| 519 |
int x; |
| 520 |
|
| 521 |
int y; |
| 522 |
|
| 523 |
|
| 524 |
bool dirty; |
| 525 |
|
| 526 |
|
| 527 |
bool visible; |
| 528 |
|
| 529 |
|
| 530 |
Window *const parent; |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
Window *mouseChild; |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
std::list<Window *> children; |
| 540 |
|
| 541 |
|
| 542 |
std::list<Window_Callback *> movehandlers; |
| 543 |
|
| 544 |
|
| 545 |
virtual void addChild(Window *child); |
| 546 |
|
| 547 |
|
| 548 |
virtual void removeChild(Window *child); |
| 549 |
|
| 550 |
|
| 551 |
void setDirty() { if (dirty) return; dirty = true; if (parent != NULL) parent->setDirty(); }; |
| 552 |
|
| 553 |
|
| 554 |
virtual void setClipboard(const String &s) { parent->setClipboard(s); }; |
| 555 |
|
| 556 |
|
| 557 |
virtual const String& getClipboard() { return parent->getClipboard(); }; |
| 558 |
|
| 559 |
|
| 560 |
Window(); |
| 561 |
|
| 562 |
|
| 563 |
virtual void focusChanged(bool gained) { |
| 564 |
if (children.size() > 0) children.back()->focusChanged(gained); |
| 565 |
} |
| 566 |
|
| 567 |
public: |
| 568 |
|
| 569 |
|
| 570 |
void addWindowHandler(Window_Callback *handler) { movehandlers.push_back(handler); } |
| 571 |
|
| 572 |
|
| 573 |
void removeWindowHandler(Window_Callback *handler) { movehandlers.remove(handler); } |
| 574 |
|
| 575 |
|
| 576 |
Window(Window *parent, int x, int y, int w, int h); |
| 577 |
|
| 578 |
|
| 579 |
virtual ~Window(); |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
virtual void resize(int w, int h); |
| 585 |
|
| 586 |
virtual int getWidth() const { return width; } |
| 587 |
|
| 588 |
virtual int getHeight() const { return height; } |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
virtual void move(int x, int y); |
| 594 |
|
| 595 |
virtual int getX() const { return x; } |
| 596 |
|
| 597 |
virtual int getY() const { return y; } |
| 598 |
|
| 599 |
virtual int getScreenX() const { return (parent == NULL?0:parent->getScreenX()+x); } |
| 600 |
|
| 601 |
virtual int getScreenY() const { return (parent == NULL?0:parent->getScreenY()+y); } |
| 602 |
|
| 603 |
|
| 604 |
virtual void paintAll(Drawable &d) const; |
| 605 |
|
| 606 |
|
| 607 |
virtual void paint(Drawable &d) const {}; |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
virtual void setVisible(bool v) { visible = !!v; parent->setDirty(); } |
| 612 |
|
| 613 |
|
| 614 |
virtual bool isVisible() const { return (!parent || parent->isVisible()) && visible; } |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
Window *getParent() const { return parent; } |
| 619 |
|
| 620 |
|
| 621 |
Screen *getScreen(); |
| 622 |
|
| 623 |
|
| 624 |
virtual bool hasFocus() const { return parent->hasFocus() && *parent->children.rbegin() == this; } |
| 625 |
|
| 626 |
|
| 627 |
virtual bool mouseMoved(int x, int y); |
| 628 |
|
| 629 |
virtual bool mouseDragged(int x, int y, MouseButton button); |
| 630 |
|
| 631 |
virtual bool mouseDown(int x, int y, MouseButton button); |
| 632 |
|
| 633 |
virtual bool mouseUp(int x, int y, MouseButton button); |
| 634 |
|
| 635 |
|
| 636 |
virtual bool mouseClicked(int x, int y, MouseButton button); |
| 637 |
|
| 638 |
virtual bool mouseDoubleClicked(int x, int y, MouseButton button); |
| 639 |
|
| 640 |
|
| 641 |
virtual bool keyDown(const Key &key); |
| 642 |
|
| 643 |
virtual bool keyUp(const Key &key); |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
virtual bool raise() { |
| 648 |
Window *last = parent->children.back(); |
| 649 |
for (Window *cur = parent->children.back(); cur != this; cur = parent->children.back()) { |
| 650 |
parent->children.remove(cur); |
| 651 |
parent->children.push_front(cur); |
| 652 |
} |
| 653 |
if (last != this) { |
| 654 |
focusChanged(true); |
| 655 |
last->focusChanged(false); |
| 656 |
} |
| 657 |
parent->setDirty(); |
| 658 |
return true; |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
virtual void lower() { |
| 663 |
parent->children.remove(this); |
| 664 |
parent->children.push_front(this); |
| 665 |
if (this != parent->children.back()) { |
| 666 |
parent->children.back()->focusChanged(true); |
| 667 |
focusChanged(false); |
| 668 |
} |
| 669 |
parent->setDirty(); |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
Window *getChild(int n) { |
| 674 |
for (std::list<Window *>::const_iterator i = children.begin(); i != children.end(); ++i) { |
| 675 |
if (n--) return *i; |
| 676 |
} |
| 677 |
return NULL; |
| 678 |
} |
| 679 |
|
| 680 |
}; |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
class Screen : public Window { |
| 696 |
protected: |
| 697 |
|
| 698 |
Drawable *const buffer; |
| 699 |
|
| 700 |
|
| 701 |
String clipboard; |
| 702 |
|
| 703 |
|
| 704 |
MouseButton button; |
| 705 |
|
| 706 |
|
| 707 |
virtual void rgbToSurface(RGB color, void **pixel) = 0; |
| 708 |
|
| 709 |
|
| 710 |
virtual RGB surfaceToRGB(void *pixel) = 0; |
| 711 |
|
| 712 |
|
| 713 |
Screen(Size width, Size height); |
| 714 |
|
| 715 |
|
| 716 |
Screen(Drawable *d); |
| 717 |
|
| 718 |
public: |
| 719 |
|
| 720 |
virtual ~Screen(); |
| 721 |
|
| 722 |
|
| 723 |
template <typename STR> void setClipboard(const STR s) { this->setClipboard(String(s)); } |
| 724 |
|
| 725 |
|
| 726 |
virtual void setClipboard(const String &s) { clipboard = s; } |
| 727 |
|
| 728 |
|
| 729 |
virtual const String& getClipboard() { return clipboard; } |
| 730 |
|
| 731 |
|
| 732 |
virtual void resize(int w, int h); |
| 733 |
|
| 734 |
|
| 735 |
virtual void move(int x, int y); |
| 736 |
|
| 737 |
|
| 738 |
virtual bool hasFocus() const { return true; } |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
Ticks update(void *surface, Ticks ticks = 1); |
| 746 |
|
| 747 |
|
| 748 |
virtual void paint(Drawable &d) const; |
| 749 |
}; |
| 750 |
|
| 751 |
class Timer; |
| 752 |
|
| 753 |
|
| 754 |
struct Timer_Callback { |
| 755 |
public: |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
virtual Ticks timerExpired(Ticks time) = 0; |
| 763 |
virtual ~Timer_Callback() {} |
| 764 |
}; |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
class Timer { |
| 776 |
protected: |
| 777 |
|
| 778 |
static Ticks ticks; |
| 779 |
|
| 780 |
|
| 781 |
struct ltuint { bool operator()(Ticks i1, Ticks i2) const { |
| 782 |
return (i1 < i2); |
| 783 |
} }; |
| 784 |
|
| 785 |
|
| 786 |
static std::multimap<Ticks,Timer_Callback*,ltuint> timers; |
| 787 |
|
| 788 |
public: |
| 789 |
|
| 790 |
static void check(Ticks ticks); |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
static void add(Timer_Callback *cb, const Ticks ticks) { timers.insert(std::pair<const Ticks,Timer_Callback *>(ticks+Timer::ticks,cb)); } |
| 795 |
|
| 796 |
static void remove(const Timer_Callback *const cb); |
| 797 |
|
| 798 |
|
| 799 |
static Ticks now() { return ticks; } |
| 800 |
|
| 801 |
|
| 802 |
static Ticks next(); |
| 803 |
}; |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
class ScreenRGB32le : public Screen { |
| 812 |
protected: |
| 813 |
|
| 814 |
virtual void rgbToSurface(RGB color, void **pixel) { RGB **p = (RGB **)pixel; **p = color; (*p)++; }; |
| 815 |
|
| 816 |
|
| 817 |
virtual RGB surfaceToRGB(void *pixel) { return *(RGB*)pixel; }; |
| 818 |
public: |
| 819 |
ScreenRGB32le(Size width, Size height) : Screen(width,height) {}; |
| 820 |
|
| 821 |
virtual void paint(Drawable &d) const; |
| 822 |
}; |
| 823 |
|
| 824 |
|
| 825 |
#ifdef SDL_MAJOR_VERSION |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
class ScreenSDL : public Screen { |
| 841 |
protected: |
| 842 |
|
| 843 |
virtual void rgbToSurface(RGB color, void **pixel) {}; |
| 844 |
|
| 845 |
|
| 846 |
virtual RGB surfaceToRGB(void *pixel) { return 0; }; |
| 847 |
|
| 848 |
|
| 849 |
SDL_Surface *surface; |
| 850 |
|
| 851 |
|
| 852 |
int downx, downy; |
| 853 |
|
| 854 |
|
| 855 |
Ticks lastclick; |
| 856 |
|
| 857 |
public: |
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
ScreenSDL(SDL_Surface *surface); |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
void setSurface(SDL_Surface *surface) { this->surface = surface; } |
| 873 |
|
| 874 |
|
| 875 |
SDL_Surface *getSurface() { return surface; } |
| 876 |
|
| 877 |
|
| 878 |
virtual void paint(Drawable &d) const; |
| 879 |
|
| 880 |
|
| 881 |
Ticks update(Ticks ticks); |
| 882 |
|
| 883 |
|
| 884 |
bool event(const SDL_Event *ev) { return event(*ev); } |
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
bool event(const SDL_Event& ev); |
| 889 |
}; |
| 890 |
#endif |
| 891 |
|
| 892 |
class Font; |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
class Drawable { |
| 913 |
protected: |
| 914 |
friend Ticks Screen::update(void *, Ticks); |
| 915 |
|
| 916 |
RGB *const buffer; |
| 917 |
|
| 918 |
const int width; |
| 919 |
|
| 920 |
const int height; |
| 921 |
|
| 922 |
const bool owner; |
| 923 |
|
| 924 |
|
| 925 |
RGB color; |
| 926 |
|
| 927 |
const Font *font; |
| 928 |
|
| 929 |
int lineWidth; |
| 930 |
|
| 931 |
|
| 932 |
const int tx; |
| 933 |
|
| 934 |
const int ty; |
| 935 |
|
| 936 |
const int cx; |
| 937 |
|
| 938 |
const int cy; |
| 939 |
|
| 940 |
const int cw; |
| 941 |
|
| 942 |
const int ch; |
| 943 |
|
| 944 |
|
| 945 |
int x; |
| 946 |
|
| 947 |
int y; |
| 948 |
|
| 949 |
public: |
| 950 |
|
| 951 |
|
| 952 |
Drawable(int w, int h, RGB clear = Color::Transparent); |
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
Drawable(Drawable &src, RGB clear = 0); |
| 960 |
|
| 961 |
|
| 962 |
Drawable(Drawable &src, int x, int y, int w, int h); |
| 963 |
|
| 964 |
|
| 965 |
virtual ~Drawable(); |
| 966 |
|
| 967 |
|
| 968 |
void clear(RGB clear = Color::Transparent); |
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
void setColor(RGB c) { color = c; }; |
| 976 |
|
| 977 |
RGB getColor() { return color; }; |
| 978 |
|
| 979 |
|
| 980 |
void setFont(const Font *f) { font = f; }; |
| 981 |
|
| 982 |
const Font *getFont() { return font; }; |
| 983 |
|
| 984 |
|
| 985 |
void setLineWidth(int w) { lineWidth = w; }; |
| 986 |
|
| 987 |
int getLineWidth() { return lineWidth; }; |
| 988 |
|
| 989 |
|
| 990 |
void gotoXY(int x, int y) { this->x = x; this->y = y; }; |
| 991 |
|
| 992 |
int getX() { return x; } |
| 993 |
|
| 994 |
int getY() { return y; } |
| 995 |
|
| 996 |
|
| 997 |
int getClipX() { return cx; } |
| 998 |
|
| 999 |
int getClipY() { return cy; } |
| 1000 |
|
| 1001 |
int getClipWidth() { return cw; } |
| 1002 |
|
| 1003 |
int getClipHeight() { return ch; } |
| 1004 |
|
| 1005 |
|
| 1006 |
void drawPixel() { if (x >= cx && x < cw && y >= cy && y < ch) buffer[x+tx+(y+ty)*width] = color; }; |
| 1007 |
|
| 1008 |
void drawPixel(int x, int y) { gotoXY(x,y); drawPixel(); }; |
| 1009 |
|
| 1010 |
|
| 1011 |
RGB getPixel() { if (x >= cx && x < cw && y >= cy && y < ch) return buffer[x+tx+(y+ty)*width]; return Color::Transparent; }; |
| 1012 |
|
| 1013 |
RGB getPixel(int x, int y) { gotoXY(x,y); return getPixel(); }; |
| 1014 |
|
| 1015 |
|
| 1016 |
void drawLine(int x2, int y2); |
| 1017 |
|
| 1018 |
void drawLine(int x1, int y1, int x2, int y2) { gotoXY(x1,y1); drawLine(x2,y2); }; |
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
void drawCircle(int d); |
| 1023 |
|
| 1024 |
|
| 1025 |
void drawCircle(int x, int y, int d) { gotoXY(x, y); drawCircle(d); }; |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
void drawRect(int w, int h); |
| 1030 |
|
| 1031 |
|
| 1032 |
void drawRect(int x, int y, int w, int h) { gotoXY(x, y); drawRect(w, h); }; |
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
void fill(); |
| 1038 |
|
| 1039 |
|
| 1040 |
void fill(int x, int y) { gotoXY(x,y); fill(); }; |
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
void fillCircle(int d); |
| 1045 |
|
| 1046 |
|
| 1047 |
void fillCircle(int x, int y, int d) { gotoXY(x, y); fillCircle(d); }; |
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
void fillRect(int w, int h); |
| 1052 |
|
| 1053 |
|
| 1054 |
void fillRect(int x, int y, int w, int h) { gotoXY(x, y); fillRect(w, h); }; |
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
void drawText(const String& text, bool interpret = true, Size start = 0, Size len = (Size)-1); |
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
template <typename STR> void drawText(int x, int y, const STR text, bool interpret, Size start, Size len = (Size)-1) { gotoXY(x,y); drawText(String(text), interpret, start, len); }; |
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
void drawText(const Char c, bool interpret = false); |
| 1072 |
|
| 1073 |
|
| 1074 |
void drawText(int x, int y, const Char c, bool interpret = false) { gotoXY(x,y); drawText(c, interpret); }; |
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
void drawDrawable(Drawable &d, unsigned char alpha = 1); |
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
void drawDrawable(int x, int y, Drawable &d, unsigned char alpha = 1) { gotoXY(x,y); drawDrawable(d, alpha); }; |
| 1086 |
|
| 1087 |
}; |
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
class Font { |
| 1115 |
protected: |
| 1116 |
friend void Drawable::drawText(const Char c, bool interpret); |
| 1117 |
friend void Drawable::drawText(const String& s, bool interpret, Size start, Size len); |
| 1118 |
|
| 1119 |
|
| 1120 |
struct ltstr { bool operator()(const char* s1, const char* s2) const { |
| 1121 |
return strcmp(s1, s2) < 0; |
| 1122 |
} }; |
| 1123 |
|
| 1124 |
static std::map<const char *,Font *,ltstr> registry; |
| 1125 |
|
| 1126 |
|
| 1127 |
Font() {}; |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
virtual void drawChar(Drawable *d, const Char c) const = 0; |
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
virtual void drawString(Drawable *d, const String &s, Size start, Size len) const { |
| 1137 |
if (len > s.size()-start) len = (Size)(s.size()-start); |
| 1138 |
len += start; |
| 1139 |
while (start < len) drawChar(d,s[start++]); |
| 1140 |
} |
| 1141 |
|
| 1142 |
public: |
| 1143 |
|
| 1144 |
|
| 1145 |
static const Font *getFont(const char *name) { |
| 1146 |
std::map<const char *,Font *,ltstr>::iterator i = registry.find(name); |
| 1147 |
if (i == registry.end()) return(strcmp(name,"default")?getFont("default"):NULL); |
| 1148 |
return (*i).second; |
| 1149 |
} |
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
static void addFont(const char *name, Font *font) { |
| 1155 |
std::map<const char *,Font *,ltstr>::iterator i = registry.find(name); |
| 1156 |
if (i != registry.end()) delete (*i).second; |
| 1157 |
registry[name] = font; |
| 1158 |
} |
| 1159 |
|
| 1160 |
virtual ~Font() {}; |
| 1161 |
|
| 1162 |
|
| 1163 |
virtual int getHeight() const = 0; |
| 1164 |
|
| 1165 |
|
| 1166 |
virtual int getAscent() const = 0; |
| 1167 |
|
| 1168 |
|
| 1169 |
template <typename STR> int getWidth(const STR s, Size start = 0, Size len = (Size)-1) const { |
| 1170 |
return this->getWidth(String(s), start, len); |
| 1171 |
} |
| 1172 |
|
| 1173 |
|
| 1174 |
virtual int getWidth(Char c = 'M') const = 0; |
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
virtual int getWidth(const String &s, Size start = 0, Size len = (Size)-1) const { |
| 1179 |
int width = 0; |
| 1180 |
if (start+len > s.size()) len = (Size)(s.size()-start); |
| 1181 |
while (len--) width += getWidth(s[start++]); |
| 1182 |
return width; |
| 1183 |
} |
| 1184 |
|
| 1185 |
|
| 1186 |
enum SpecialChar { CR = '\r', LF = '\n', BS = '\b', Tab = '\t', Space = ' ', ESC = 27 }; |
| 1187 |
|
| 1188 |
|
| 1189 |
virtual SpecialChar toSpecial(Char c) const { return (SpecialChar)(c<255?c:255); } |
| 1190 |
|
| 1191 |
|
| 1192 |
virtual Char fromSpecial(SpecialChar c) const { return c; } |
| 1193 |
}; |
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
class BitmapFont : public Font { |
| 1202 |
protected: |
| 1203 |
|
| 1204 |
const unsigned char *const bitmap; |
| 1205 |
|
| 1206 |
|
| 1207 |
const int width; |
| 1208 |
|
| 1209 |
|
| 1210 |
const int height; |
| 1211 |
|
| 1212 |
|
| 1213 |
const int ascent; |
| 1214 |
|
| 1215 |
|
| 1216 |
const int *const widths; |
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
const int *const ascents; |
| 1221 |
|
| 1222 |
|
| 1223 |
const bool background_set; |
| 1224 |
|
| 1225 |
|
| 1226 |
const int col_step; |
| 1227 |
|
| 1228 |
|
| 1229 |
const int row_step; |
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
const int character_step; |
| 1234 |
|
| 1235 |
|
| 1236 |
const unsigned char *const*const char_position; |
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
const SpecialChar *const special; |
| 1241 |
|
| 1242 |
|
| 1243 |
const bool owner; |
| 1244 |
|
| 1245 |
|
| 1246 |
const Char last; |
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
virtual void drawChar(Drawable *d, const Char c) const; |
| 1252 |
|
| 1253 |
public: |
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
BitmapFont(const unsigned char *data, int height, int ascent, bool owner = false, |
| 1259 |
int width = 8, bool background_set = false, |
| 1260 |
int col_step = -1, int row_step = 8, int character_step = 0, Char last = 256, |
| 1261 |
const int *widths = NULL, const int *ascents = NULL, |
| 1262 |
const unsigned char *const* char_position = NULL, |
| 1263 |
const SpecialChar *special = NULL); |
| 1264 |
|
| 1265 |
virtual ~BitmapFont(); |
| 1266 |
|
| 1267 |
|
| 1268 |
virtual int getHeight() const { return height; }; |
| 1269 |
|
| 1270 |
|
| 1271 |
virtual int getAscent() const { return ascent; }; |
| 1272 |
|
| 1273 |
|
| 1274 |
virtual int getWidth(Char c = 'M') const { return (widths != NULL?widths[c]:width); }; |
| 1275 |
|
| 1276 |
|
| 1277 |
virtual SpecialChar toSpecial(Char c) const { return (special != NULL?special[c]:Font::toSpecial(c)); } |
| 1278 |
|
| 1279 |
|
| 1280 |
virtual Char fromSpecial(SpecialChar c) const { if (special == NULL) return Font::fromSpecial(c); Char i = 0; while(special[i] != c) i++; return i; } |
| 1281 |
|
| 1282 |
}; |
| 1283 |
|
| 1284 |
class ActionEventSource; |
| 1285 |
|
| 1286 |
struct ActionEventSource_Callback { |
| 1287 |
public: |
| 1288 |
|
| 1289 |
|
| 1290 |
virtual void actionExecuted(ActionEventSource *source, const String &arg) = 0; |
| 1291 |
virtual ~ActionEventSource_Callback() {} |
| 1292 |
}; |
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
class ActionEventSource { |
| 1301 |
protected: |
| 1302 |
|
| 1303 |
std::list<ActionEventSource_Callback *> actionHandlers; |
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
String name; |
| 1309 |
|
| 1310 |
public: |
| 1311 |
|
| 1312 |
template <typename STR> ActionEventSource(const STR name) : name(String(name)) { } |
| 1313 |
|
| 1314 |
|
| 1315 |
virtual ~ActionEventSource() {} |
| 1316 |
|
| 1317 |
|
| 1318 |
void addActionHandler(ActionEventSource_Callback *handler) { actionHandlers.push_back(handler); } |
| 1319 |
|
| 1320 |
|
| 1321 |
void removeActionHandler(ActionEventSource_Callback *handler) { actionHandlers.remove(handler); } |
| 1322 |
|
| 1323 |
|
| 1324 |
template <typename STR> void setName(const STR name) { this->name = String(name); } |
| 1325 |
|
| 1326 |
|
| 1327 |
const String &getName() const { return name; } |
| 1328 |
|
| 1329 |
|
| 1330 |
void executeAction(const String &arg) { |
| 1331 |
std::list<ActionEventSource_Callback*>::iterator i = actionHandlers.begin(); |
| 1332 |
bool end = (i == actionHandlers.end()); |
| 1333 |
while (!end) { |
| 1334 |
ActionEventSource_Callback *c = *i; |
| 1335 |
++i; |
| 1336 |
end = (i == actionHandlers.end()); |
| 1337 |
c->actionExecuted(this,arg); |
| 1338 |
} |
| 1339 |
} |
| 1340 |
|
| 1341 |
|
| 1342 |
void executeAction() { executeAction(name); } |
| 1343 |
}; |
| 1344 |
|
| 1345 |
|
| 1346 |
class BorderedWindow : public Window { |
| 1347 |
protected: |
| 1348 |
|
| 1349 |
int border_left, border_top, border_right, border_bottom; |
| 1350 |
|
| 1351 |
|
| 1352 |
BorderedWindow(Window *parent, int x, int y, int w, int h, int bl, int bt, int br, int bb) : |
| 1353 |
Window(parent,x,y,w,h), border_left(bl), border_top(bt), border_right(br), border_bottom(bb) {} |
| 1354 |
|
| 1355 |
public: |
| 1356 |
virtual void paintAll(Drawable &d) const; |
| 1357 |
virtual bool mouseMoved(int x, int y); |
| 1358 |
virtual bool mouseDown(int x, int y, MouseButton button); |
| 1359 |
virtual bool mouseDragged(int x, int y, MouseButton button); |
| 1360 |
virtual int getScreenX() const { return Window::getScreenX()+border_left; } |
| 1361 |
virtual int getScreenY() const { return Window::getScreenY()+border_top; } |
| 1362 |
}; |
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
class Label : public Window { |
| 1370 |
|
| 1371 |
const Font *font; |
| 1372 |
|
| 1373 |
|
| 1374 |
RGB color; |
| 1375 |
|
| 1376 |
|
| 1377 |
String text; |
| 1378 |
|
| 1379 |
|
| 1380 |
bool interpret; |
| 1381 |
|
| 1382 |
public: |
| 1383 |
|
| 1384 |
|
| 1385 |
template <typename STR> Label(Window *parent, int x, int y, const STR text, int width = 0, const Font *font = Font::getFont("default"), RGB color = Color::Text) : |
| 1386 |
Window(parent, x, y, (width?width:1), 1), font(font), color(color), text(text), interpret(width != 0) |
| 1387 |
{ resize(); } |
| 1388 |
|
| 1389 |
|
| 1390 |
template <typename STR> void setText(const STR text) { this->text = text; resize(); } |
| 1391 |
|
| 1392 |
const String& getText() { return text; } |
| 1393 |
|
| 1394 |
|
| 1395 |
void setFont(const Font *font) { this->font = font; resize(); } |
| 1396 |
|
| 1397 |
const Font *getFont() { return font; } |
| 1398 |
|
| 1399 |
|
| 1400 |
void setColor(const RGB color) { this->color = color; resize(); } |
| 1401 |
|
| 1402 |
RGB getColor() { return color; } |
| 1403 |
|
| 1404 |
|
| 1405 |
virtual void resize(int w = -1, int h = -1) { |
| 1406 |
if (w == -1) w = (interpret?getWidth():0); |
| 1407 |
else interpret = (w != 0); |
| 1408 |
Drawable d((w?w:1), 1); |
| 1409 |
d.setFont(font); |
| 1410 |
d.drawText(0, font->getAscent(), text, interpret, 0); |
| 1411 |
if (interpret) Window::resize(w, d.getY()-font->getAscent()+font->getHeight()); |
| 1412 |
else Window::resize(d.getX(), font->getHeight()); |
| 1413 |
} |
| 1414 |
|
| 1415 |
|
| 1416 |
virtual void paint(Drawable &d) const { d.setColor(color); d.drawText(0, font->getAscent(), text, interpret, 0); } |
| 1417 |
|
| 1418 |
virtual bool raise() { return false; } |
| 1419 |
}; |
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
class Input : public Window, public Timer_Callback, public ActionEventSource { |
| 1427 |
protected: |
| 1428 |
|
| 1429 |
String text; |
| 1430 |
|
| 1431 |
|
| 1432 |
Size pos; |
| 1433 |
|
| 1434 |
|
| 1435 |
Size lastpos; |
| 1436 |
|
| 1437 |
|
| 1438 |
int posx, posy; |
| 1439 |
|
| 1440 |
|
| 1441 |
Size start_sel, end_sel; |
| 1442 |
|
| 1443 |
|
| 1444 |
bool blink; |
| 1445 |
|
| 1446 |
|
| 1447 |
bool insert; |
| 1448 |
|
| 1449 |
|
| 1450 |
bool multi; |
| 1451 |
|
| 1452 |
|
| 1453 |
int offset; |
| 1454 |
|
| 1455 |
|
| 1456 |
void checkOffset() { |
| 1457 |
if (lastpos == pos) return; |
| 1458 |
const Font *f = Font::getFont("input"); |
| 1459 |
if (multi) { |
| 1460 |
Drawable d(width-6,1); |
| 1461 |
d.setFont(f); |
| 1462 |
d.drawText(0, 0, text, multi, 0, pos); |
| 1463 |
posy = d.getY(); |
| 1464 |
posx = d.getX(); |
| 1465 |
if (posy-offset > height-8-f->getHeight()) offset = posy-height+8+f->getHeight(); |
| 1466 |
if (posy-offset < 0) offset = posy; |
| 1467 |
} else { |
| 1468 |
posy = 0; |
| 1469 |
posx = f->getWidth(text,0,pos); |
| 1470 |
if (f->getWidth(text,0,pos+1)-offset > width-10) offset = f->getWidth(text,0,pos+1)-width+10; |
| 1471 |
if (f->getWidth(text,0,(pos>0?pos-1:0))-offset < 0) offset = f->getWidth(text,0,(pos>0?pos-1:0)); |
| 1472 |
} |
| 1473 |
lastpos = pos; |
| 1474 |
setDirty(); |
| 1475 |
} |
| 1476 |
|
| 1477 |
public: |
| 1478 |
|
| 1479 |
Input(Window *parent, int x, int y, int w, int h = 0) : |
| 1480 |
Window(parent,x,y,w,(h?h:Font::getFont("input")->getHeight()+10)), ActionEventSource("GUI::Input"), |
| 1481 |
text(""), pos(0), lastpos(0), posx(0), posy(0), start_sel(0), end_sel(0), blink(true), insert(true), multi(h != 0), offset(0) |
| 1482 |
{ Timer::add(this,30); } |
| 1483 |
|
| 1484 |
~Input() { |
| 1485 |
Timer::remove(this); |
| 1486 |
} |
| 1487 |
|
| 1488 |
|
| 1489 |
virtual void paint(Drawable &d) const; |
| 1490 |
|
| 1491 |
|
| 1492 |
void clearSelection() { |
| 1493 |
text.erase(text.begin()+(pos = imin(start_sel,end_sel)),text.begin()+imax(start_sel,end_sel)); |
| 1494 |
start_sel = end_sel = pos; |
| 1495 |
} |
| 1496 |
|
| 1497 |
|
| 1498 |
void copySelection() { |
| 1499 |
setClipboard(String(text.begin()+imin(start_sel,end_sel),text.begin()+imax(start_sel,end_sel))); |
| 1500 |
} |
| 1501 |
|
| 1502 |
|
| 1503 |
void cutSelection() { |
| 1504 |
setClipboard(String(text.begin()+imin(start_sel,end_sel),text.begin()+imax(start_sel,end_sel))); |
| 1505 |
clearSelection(); |
| 1506 |
} |
| 1507 |
|
| 1508 |
|
| 1509 |
void pasteSelection() { |
| 1510 |
String c = getClipboard(); |
| 1511 |
clearSelection(); |
| 1512 |
text.insert(text.begin()+pos,c.begin(),c.end()); |
| 1513 |
start_sel = end_sel = pos += (Size)c.size(); |
| 1514 |
} |
| 1515 |
|
| 1516 |
|
| 1517 |
Size findPos(int x, int y); |
| 1518 |
|
| 1519 |
|
| 1520 |
template <typename STR> void setText(const STR text) { this->text = text; setDirty(); }; |
| 1521 |
|
| 1522 |
const String& getText() { return text; }; |
| 1523 |
|
| 1524 |
|
| 1525 |
virtual bool keyDown(const Key &key); |
| 1526 |
|
| 1527 |
|
| 1528 |
virtual bool mouseDown(int x, int y, MouseButton button); |
| 1529 |
|
| 1530 |
|
| 1531 |
virtual bool mouseDragged(int x, int y, MouseButton button); |
| 1532 |
|
| 1533 |
|
| 1534 |
virtual Ticks timerExpired(Ticks time) |
| 1535 |
{ blink = !blink; setDirty(); return 30; } |
| 1536 |
|
| 1537 |
}; |
| 1538 |
|
| 1539 |
class ToplevelWindow; |
| 1540 |
|
| 1541 |
struct ToplevelWindow_Callback { |
| 1542 |
public: |
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
virtual bool windowClosing(ToplevelWindow *win) = 0; |
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
virtual void windowClosed(ToplevelWindow *win) = 0; |
| 1555 |
virtual ~ToplevelWindow_Callback() {} |
| 1556 |
}; |
| 1557 |
|
| 1558 |
|
| 1559 |
class ToplevelWindow : public BorderedWindow, public ActionEventSource_Callback { |
| 1560 |
protected: |
| 1561 |
|
| 1562 |
String title; |
| 1563 |
|
| 1564 |
|
| 1565 |
int dragx, dragy; |
| 1566 |
|
| 1567 |
|
| 1568 |
std::list<ToplevelWindow_Callback *> closehandlers; |
| 1569 |
|
| 1570 |
|
| 1571 |
Menu *systemMenu; |
| 1572 |
|
| 1573 |
public: |
| 1574 |
|
| 1575 |
template <typename STR> ToplevelWindow(Screen *parent, int x, int y, int w, int h, const STR title); |
| 1576 |
|
| 1577 |
|
| 1578 |
~ToplevelWindow() { |
| 1579 |
std::list<ToplevelWindow_Callback*>::iterator i = closehandlers.begin(); |
| 1580 |
bool end = (i == closehandlers.end()); |
| 1581 |
while (!end) { |
| 1582 |
ToplevelWindow_Callback *c = *i; |
| 1583 |
++i; |
| 1584 |
end = (i == closehandlers.end()); |
| 1585 |
c->windowClosed(this); |
| 1586 |
} |
| 1587 |
} |
| 1588 |
|
| 1589 |
|
| 1590 |
virtual void actionExecuted(ActionEventSource *src, const String &item) { |
| 1591 |
if (item == String("Close")) close(); |
| 1592 |
} |
| 1593 |
|
| 1594 |
|
| 1595 |
void addCloseHandler(ToplevelWindow_Callback *handler) { closehandlers.push_back(handler); } |
| 1596 |
|
| 1597 |
|
| 1598 |
void removeCloseHandler(ToplevelWindow_Callback *handler) { closehandlers.remove(handler); } |
| 1599 |
|
| 1600 |
virtual void paint(Drawable &d) const; |
| 1601 |
virtual bool mouseDown(int x, int y, MouseButton button); |
| 1602 |
virtual bool mouseDoubleClicked(int x, int y, MouseButton button) { |
| 1603 |
if (button == Left && x < 32 && x > 6 && y > 4 && y < 31) { |
| 1604 |
close(); |
| 1605 |
return true; |
| 1606 |
} |
| 1607 |
BorderedWindow::mouseClicked(x,y,button); |
| 1608 |
return true; |
| 1609 |
} |
| 1610 |
virtual bool mouseUp(int x, int y, MouseButton button) { |
| 1611 |
if (button == Left && dragx >= 0 && dragy >= 0) { |
| 1612 |
dragx = dragy = -1; |
| 1613 |
return true; |
| 1614 |
} |
| 1615 |
BorderedWindow::mouseUp(x,y,button); |
| 1616 |
return true; |
| 1617 |
} |
| 1618 |
virtual bool mouseDragged(int x, int y, MouseButton button) { |
| 1619 |
if (button == Left && dragx >= 0 && dragy >= 0) { |
| 1620 |
move(x-dragx+this->x,y-dragy+this->y); |
| 1621 |
return true; |
| 1622 |
} |
| 1623 |
BorderedWindow::mouseDragged(x,y,button); |
| 1624 |
return true; |
| 1625 |
} |
| 1626 |
virtual bool mouseMoved(int x, int y) { |
| 1627 |
BorderedWindow::mouseMoved(x,y); |
| 1628 |
return true; |
| 1629 |
} |
| 1630 |
|
| 1631 |
|
| 1632 |
virtual bool raise() { |
| 1633 |
Window *last = parent->children.back(); |
| 1634 |
parent->children.remove(this); |
| 1635 |
parent->children.push_back(this); |
| 1636 |
if (last != this) { |
| 1637 |
focusChanged(true); |
| 1638 |
last->focusChanged(false); |
| 1639 |
} |
| 1640 |
return true; |
| 1641 |
} |
| 1642 |
|
| 1643 |
|
| 1644 |
template <typename STR> void setTitle(const STR title) { this->title = title; setDirty(); } |
| 1645 |
|
| 1646 |
const String& getTitle() { return title; } |
| 1647 |
|
| 1648 |
|
| 1649 |
void close() { |
| 1650 |
bool doit = true; |
| 1651 |
std::list<ToplevelWindow_Callback*>::iterator i = closehandlers.begin(); |
| 1652 |
bool end = (i == closehandlers.end()); |
| 1653 |
while (!end) { |
| 1654 |
ToplevelWindow_Callback *c = *i; |
| 1655 |
++i; |
| 1656 |
end = (i == closehandlers.end()); |
| 1657 |
doit = doit && c->windowClosing(this); |
| 1658 |
} |
| 1659 |
if (doit) delete this; |
| 1660 |
} |
| 1661 |
}; |
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
class TransientWindow : public Window, Window_Callback, ToplevelWindow_Callback { |
| 1679 |
protected: |
| 1680 |
|
| 1681 |
Window *realparent; |
| 1682 |
|
| 1683 |
|
| 1684 |
int relx, rely; |
| 1685 |
|
| 1686 |
public: |
| 1687 |
|
| 1688 |
virtual void focusChanged(bool gained) { |
| 1689 |
Window::focusChanged(gained); |
| 1690 |
if (isVisible() && !gained) { |
| 1691 |
if (realparent->hasFocus()) raise(); |
| 1692 |
else setVisible(false); |
| 1693 |
} |
| 1694 |
} |
| 1695 |
|
| 1696 |
|
| 1697 |
void windowClosed(ToplevelWindow *win) { |
| 1698 |
delete this; |
| 1699 |
} |
| 1700 |
|
| 1701 |
|
| 1702 |
bool windowClosing(ToplevelWindow *win) { return true; } |
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
TransientWindow(Window *parent, int x, int y, int w, int h) : |
| 1708 |
Window(parent->getScreen(),x+parent->getScreenX(),y+parent->getScreenY(),w,h), |
| 1709 |
realparent(parent), relx(x), rely(y) { |
| 1710 |
Window *p = realparent, *last = NULL, *last2 = NULL; |
| 1711 |
while (p != NULL) { |
| 1712 |
p->addWindowHandler(this); |
| 1713 |
last2 = last; |
| 1714 |
last = p; |
| 1715 |
p = p->getParent(); |
| 1716 |
} |
| 1717 |
dynamic_cast<ToplevelWindow *>(last2)->addCloseHandler(this); |
| 1718 |
} |
| 1719 |
|
| 1720 |
~TransientWindow() { |
| 1721 |
Window *p = realparent, *last = NULL, *last2 = NULL; |
| 1722 |
while (p != NULL) { |
| 1723 |
p->removeWindowHandler(this); |
| 1724 |
last2 = last; |
| 1725 |
last = p; |
| 1726 |
p = p->getParent(); |
| 1727 |
} |
| 1728 |
dynamic_cast<ToplevelWindow *>(last2)->removeCloseHandler(this); |
| 1729 |
} |
| 1730 |
|
| 1731 |
virtual void move(int x, int y) { relx = x; rely = y; |
| 1732 |
Window::move(x+realparent->getScreenX(),y+realparent->getScreenY()); } |
| 1733 |
virtual int getX() const { return x-realparent->getScreenX(); } |
| 1734 |
virtual int getY() const { return y-realparent->getScreenY(); } |
| 1735 |
virtual void setVisible(bool v) { if (v) raise(); Window::setVisible(v); } |
| 1736 |
virtual void windowMoved(Window *src, int x, int y) { move(relx,rely); } |
| 1737 |
|
| 1738 |
|
| 1739 |
virtual bool raise() { |
| 1740 |
Window *last = parent->children.back(); |
| 1741 |
parent->children.remove(this); |
| 1742 |
parent->children.push_back(this); |
| 1743 |
if (last != this) { |
| 1744 |
focusChanged(true); |
| 1745 |
last->focusChanged(false); |
| 1746 |
} |
| 1747 |
return true; |
| 1748 |
} |
| 1749 |
|
| 1750 |
}; |
| 1751 |
|
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
class Menu : public TransientWindow, public ActionEventSource { |
| 1763 |
protected: |
| 1764 |
|
| 1765 |
std::vector<String> items; |
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
int selected; |
| 1770 |
|
| 1771 |
|
| 1772 |
bool firstMouseUp; |
| 1773 |
|
| 1774 |
|
| 1775 |
Window *mouseTakenFrom; |
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
virtual void selectItem(int x, int y) { |
| 1780 |
y -= 2; |
| 1781 |
selected = -1; |
| 1782 |
const int height = Font::getFont("menu")->getHeight()+2; |
| 1783 |
std::vector<String>::iterator i; |
| 1784 |
for (i = items.begin(); i != items.end() && y > 0; ++i) { |
| 1785 |
selected++; |
| 1786 |
if ((*i).size() > 0) y -= height; |
| 1787 |
else y -= 12; |
| 1788 |
} |
| 1789 |
if (y > 0 || (selected >= 0 && items[selected].size() == 0)) selected = -1; |
| 1790 |
} |
| 1791 |
|
| 1792 |
virtual Size getPreferredWidth() { |
| 1793 |
Size width = 0; |
| 1794 |
const Font *f = Font::getFont("menu"); |
| 1795 |
std::vector<String>::iterator i; |
| 1796 |
for (i = items.begin(); i != items.end() && y > 0; ++i) { |
| 1797 |
Size newwidth = f->getWidth(*i); |
| 1798 |
if (newwidth > width) width = newwidth; |
| 1799 |
} |
| 1800 |
return width+39; |
| 1801 |
} |
| 1802 |
|
| 1803 |
virtual Size getPreferredHeight() { |
| 1804 |
Size height = 0; |
| 1805 |
const Size h = Font::getFont("menu")->getHeight()+2; |
| 1806 |
std::vector<String>::iterator i; |
| 1807 |
for (i = items.begin(); i != items.end() && y > 0; ++i) { |
| 1808 |
height += ((*i).size() > 0?h:12); |
| 1809 |
} |
| 1810 |
return height+6; |
| 1811 |
} |
| 1812 |
|
| 1813 |
public: |
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
template <typename STR> Menu(Window *parent, int x, int y, const STR name) : |
| 1818 |
TransientWindow(parent,x,y,4,4), ActionEventSource(name), selected(-1) |
| 1819 |
{ setVisible(false); } |
| 1820 |
|
| 1821 |
~Menu() { |
| 1822 |
setVisible(false); |
| 1823 |
} |
| 1824 |
|
| 1825 |
|
| 1826 |
virtual void paint(Drawable &d) const; |
| 1827 |
|
| 1828 |
|
| 1829 |
virtual bool mouseMoved(int x, int y) { |
| 1830 |
selectItem(x,y); |
| 1831 |
return true; |
| 1832 |
} |
| 1833 |
|
| 1834 |
|
| 1835 |
virtual bool mouseDragged(int x, int y, MouseButton b) { |
| 1836 |
selectItem(x,y); |
| 1837 |
return true; |
| 1838 |
} |
| 1839 |
|
| 1840 |
virtual bool mouseDown(int x, int y, MouseButton button) { return true; } |
| 1841 |
|
| 1842 |
|
| 1843 |
virtual bool mouseUp(int x, int y, MouseButton button) { |
| 1844 |
selectItem(x,y); |
| 1845 |
if (firstMouseUp) firstMouseUp = false; |
| 1846 |
else setVisible(false); |
| 1847 |
execute(); |
| 1848 |
return true; |
| 1849 |
} |
| 1850 |
|
| 1851 |
|
| 1852 |
virtual bool keyDown(const Key &key) { |
| 1853 |
if (key.special == Key::Up) selected--; |
| 1854 |
else if (key.special == Key::Down) selected++; |
| 1855 |
else if (key.special == Key::Enter) { execute(); return true; } |
| 1856 |
else if (key.special == Key::Escape) { setVisible(false); return true; } |
| 1857 |
else return true; |
| 1858 |
if (items[selected].size() == 0 && items.size() > 1) return keyDown(key); |
| 1859 |
if (selected < 0) selected = (int)(items.size()-1); |
| 1860 |
if (selected >= (int)items.size()) selected = 0; |
| 1861 |
return true; |
| 1862 |
} |
| 1863 |
|
| 1864 |
|
| 1865 |
|
| 1866 |
template <typename T> void addItem(const T item) { |
| 1867 |
items.push_back(String(item)); |
| 1868 |
resize(getPreferredWidth(),getPreferredHeight()); |
| 1869 |
} |
| 1870 |
|
| 1871 |
|
| 1872 |
template <typename T> void removeItem(const T item) { |
| 1873 |
const String s(item); |
| 1874 |
std::vector<String>::iterator i = items.begin(); |
| 1875 |
while (i != items.end() && s != (*i)) ++i; |
| 1876 |
if (i != items.end()) items.erase(i); |
| 1877 |
resize(getPreferredWidth(),getPreferredHeight()); |
| 1878 |
} |
| 1879 |
|
| 1880 |
virtual void setVisible(bool v) { |
| 1881 |
TransientWindow::setVisible(v); |
| 1882 |
if (v) { |
| 1883 |
parent->mouseChild = this; |
| 1884 |
raise(); |
| 1885 |
firstMouseUp = true; |
| 1886 |
} |
| 1887 |
} |
| 1888 |
|
| 1889 |
|
| 1890 |
void execute() { |
| 1891 |
if (selected >= 0) { |
| 1892 |
setVisible(false); |
| 1893 |
executeAction(items[selected]); |
| 1894 |
} |
| 1895 |
} |
| 1896 |
}; |
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
class Button : public BorderedWindow, public ActionEventSource { |
| 1903 |
protected: |
| 1904 |
|
| 1905 |
bool pressed; |
| 1906 |
|
| 1907 |
public: |
| 1908 |
|
| 1909 |
Button(Window *parent, int x, int y, int w, int h) : BorderedWindow(parent,x,y,w,h,6,5,6,5), ActionEventSource("GUI::Button"), pressed(0) {} |
| 1910 |
|
| 1911 |
|
| 1912 |
|
| 1913 |
template <typename T> Button(Window *parent, int x, int y, const T text, int w = -1, int h = -1); |
| 1914 |
|
| 1915 |
|
| 1916 |
virtual void paint(Drawable &d) const; |
| 1917 |
|
| 1918 |
|
| 1919 |
virtual bool mouseDown(int x, int y, MouseButton button) { |
| 1920 |
border_left = 7; border_right = 5; border_top = 7; border_bottom = 3; |
| 1921 |
pressed = true; |
| 1922 |
return true; |
| 1923 |
} |
| 1924 |
|
| 1925 |
|
| 1926 |
virtual bool mouseUp(int x, int y, MouseButton button) { |
| 1927 |
border_left = 6; border_right = 6; border_top = 5; border_bottom = 5; |
| 1928 |
pressed = false; |
| 1929 |
return true; |
| 1930 |
} |
| 1931 |
|
| 1932 |
|
| 1933 |
virtual bool mouseClicked(int x, int y, MouseButton button) { |
| 1934 |
if (button == Left) { |
| 1935 |
executeAction(); |
| 1936 |
return true; |
| 1937 |
} |
| 1938 |
return false; |
| 1939 |
} |
| 1940 |
|
| 1941 |
|
| 1942 |
virtual bool keyDown(const Key &key); |
| 1943 |
|
| 1944 |
|
| 1945 |
virtual bool keyUp(const Key &key); |
| 1946 |
|
| 1947 |
}; |
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
|
| 1955 |
|
| 1956 |
class Menubar : public Window, public ActionEventSource, ActionEventSource_Callback { |
| 1957 |
protected: |
| 1958 |
|
| 1959 |
int selected; |
| 1960 |
|
| 1961 |
|
| 1962 |
int lastx; |
| 1963 |
|
| 1964 |
|
| 1965 |
std::vector<Menu*> menus; |
| 1966 |
|
| 1967 |
public: |
| 1968 |
|
| 1969 |
|
| 1970 |
Menubar(Window *parent, int x, int y, int w) : Window(parent,x,y,w,Font::getFont("menu")->getHeight()+5), ActionEventSource("GUI::Menubar"), selected(-1), lastx(0) {} |
| 1971 |
|
| 1972 |
|
| 1973 |
template <typename STR> void addMenu(const STR name) { |
| 1974 |
const String n(name); |
| 1975 |
menus.push_back(new Menu(this,lastx,height-2,n)); |
| 1976 |
menus.back()->addActionHandler(this); |
| 1977 |
lastx += Font::getFont("menu")->getWidth(n)+14; |
| 1978 |
} |
| 1979 |
|
| 1980 |
|
| 1981 |
template <typename STR> void addItem(int index, const STR name) { menus[index]->addItem(name); } |
| 1982 |
|
| 1983 |
|
| 1984 |
template <typename STR> void removeItem(int index, const STR name) { menus[index]->removeItem(name); } |
| 1985 |
|
| 1986 |
|
| 1987 |
virtual void paint(Drawable &d) const; |
| 1988 |
|
| 1989 |
|
| 1990 |
virtual bool mouseDown(int x, int y, MouseButton button) { |
| 1991 |
int oldselected = selected; |
| 1992 |
if (selected >= 0 && !menus[selected]->isVisible()) oldselected = -1; |
| 1993 |
if (selected >= 0) menus[selected]->setVisible(false); |
| 1994 |
if (x < 0 || x >= lastx) return true; |
| 1995 |
for (selected = (int)(menus.size()-1); menus[selected]->getX() > x; selected--) {}; |
| 1996 |
if (oldselected == selected) selected = -1; |
| 1997 |
else menus[selected]->setVisible(true); |
| 1998 |
return true; |
| 1999 |
} |
| 2000 |
|
| 2001 |
|
| 2002 |
virtual bool keyDown(const Key &key) { return true; }; |
| 2003 |
|
| 2004 |
|
| 2005 |
virtual bool keyUp(const Key &key) { return true; }; |
| 2006 |
|
| 2007 |
virtual void actionExecuted(ActionEventSource *src, const String &arg) { |
| 2008 |
std::list<ActionEventSource_Callback*>::iterator i = actionHandlers.begin(); |
| 2009 |
bool end = (i == actionHandlers.end()); |
| 2010 |
while (!end) { |
| 2011 |
ActionEventSource_Callback *c = *i; |
| 2012 |
++i; |
| 2013 |
end = (i == actionHandlers.end()); |
| 2014 |
c->actionExecuted(src,arg); |
| 2015 |
} |
| 2016 |
} |
| 2017 |
}; |
| 2018 |
|
| 2019 |
|
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
class Checkbox : public BorderedWindow, public ActionEventSource { |
| 2024 |
protected: |
| 2025 |
|
| 2026 |
bool checked; |
| 2027 |
|
| 2028 |
public: |
| 2029 |
|
| 2030 |
Checkbox(Window *parent, int x, int y, int w, int h) : BorderedWindow(parent,x,y,w,h,16,0,0,0), ActionEventSource("GUI::Checkbox"), checked(0) {} |
| 2031 |
|
| 2032 |
|
| 2033 |
|
| 2034 |
template <typename T> Checkbox(Window *parent, int x, int y, const T text, int w = -1, int h = -1); |
| 2035 |
|
| 2036 |
|
| 2037 |
virtual void paint(Drawable &d) const; |
| 2038 |
|
| 2039 |
|
| 2040 |
virtual void setChecked(bool checked) { this->checked = checked; } |
| 2041 |
|
| 2042 |
|
| 2043 |
virtual bool isChecked() { return checked; } |
| 2044 |
|
| 2045 |
|
| 2046 |
virtual bool mouseDown(int x, int y, MouseButton button) { |
| 2047 |
checked = !checked; |
| 2048 |
return true; |
| 2049 |
} |
| 2050 |
|
| 2051 |
|
| 2052 |
virtual bool mouseUp(int x, int y, MouseButton button) { |
| 2053 |
execute(); |
| 2054 |
return true; |
| 2055 |
} |
| 2056 |
|
| 2057 |
|
| 2058 |
virtual bool keyDown(const Key &key); |
| 2059 |
|
| 2060 |
|
| 2061 |
virtual bool keyUp(const Key &key); |
| 2062 |
|
| 2063 |
|
| 2064 |
virtual void execute() { |
| 2065 |
String arg(name); |
| 2066 |
if (!checked) arg.insert(arg.begin(),'!'); |
| 2067 |
executeAction(arg); |
| 2068 |
} |
| 2069 |
}; |
| 2070 |
|
| 2071 |
class Frame; |
| 2072 |
|
| 2073 |
|
| 2074 |
|
| 2075 |
|
| 2076 |
|
| 2077 |
class Radiobox : public BorderedWindow, public ActionEventSource { |
| 2078 |
protected: |
| 2079 |
|
| 2080 |
bool checked; |
| 2081 |
|
| 2082 |
public: |
| 2083 |
|
| 2084 |
Radiobox(Frame *parent, int x, int y, int w, int h); |
| 2085 |
|
| 2086 |
|
| 2087 |
|
| 2088 |
template <typename T> Radiobox(Frame *parent, int x, int y, const T text, int w = -1, int h = -1); |
| 2089 |
|
| 2090 |
|
| 2091 |
virtual void paint(Drawable &d) const; |
| 2092 |
|
| 2093 |
|
| 2094 |
virtual void setChecked(bool checked) { this->checked = checked; } |
| 2095 |
|
| 2096 |
|
| 2097 |
virtual bool isChecked() { return checked; } |
| 2098 |
|
| 2099 |
|
| 2100 |
virtual bool mouseDown(int x, int y, MouseButton button) { |
| 2101 |
checked = true; |
| 2102 |
return true; |
| 2103 |
} |
| 2104 |
|
| 2105 |
|
| 2106 |
virtual bool mouseUp(int x, int y, MouseButton button) { |
| 2107 |
executeAction(); |
| 2108 |
return true; |
| 2109 |
} |
| 2110 |
|
| 2111 |
|
| 2112 |
virtual bool keyDown(const Key &key); |
| 2113 |
|
| 2114 |
|
| 2115 |
virtual bool keyUp(const Key &key); |
| 2116 |
}; |
| 2117 |
|
| 2118 |
|
| 2119 |
|
| 2120 |
|
| 2121 |
class Frame : public BorderedWindow, public ActionEventSource, protected ActionEventSource_Callback { |
| 2122 |
protected: |
| 2123 |
friend class Radiobox; |
| 2124 |
|
| 2125 |
|
| 2126 |
int selected; |
| 2127 |
|
| 2128 |
|
| 2129 |
String label; |
| 2130 |
|
| 2131 |
|
| 2132 |
virtual void actionExecuted(ActionEventSource *src, const String &arg) { |
| 2133 |
for (std::list<Window *>::iterator i = children.begin(); i != children.end(); ++i) { |
| 2134 |
Radiobox *r = dynamic_cast<Radiobox*>(*i); |
| 2135 |
if (r != NULL && src != dynamic_cast<ActionEventSource*>(r)) r->setChecked(false); |
| 2136 |
} |
| 2137 |
executeAction(src->getName()); |
| 2138 |
} |
| 2139 |
|
| 2140 |
public: |
| 2141 |
|
| 2142 |
Frame(Window *parent, int x, int y, int w, int h) : BorderedWindow(parent,x,y,w,h,5,5,5,5), ActionEventSource("GUI::Frame"), selected(0) {} |
| 2143 |
|
| 2144 |
|
| 2145 |
template <typename T> Frame(Window *parent, int x, int y, int w, int h, const T text) : |
| 2146 |
BorderedWindow(parent,x,y,w,h,5,Font::getFont("default")->getHeight()+2,5,5), |
| 2147 |
ActionEventSource(text), selected(0), label(text) { } |
| 2148 |
|
| 2149 |
|
| 2150 |
virtual void paint(Drawable &d) const; |
| 2151 |
|
| 2152 |
}; |
| 2153 |
|
| 2154 |
|
| 2155 |
class MessageBox : public GUI::ToplevelWindow { |
| 2156 |
protected: |
| 2157 |
Label *message; |
| 2158 |
Button *close; |
| 2159 |
public: |
| 2160 |
|
| 2161 |
template <typename STR> MessageBox(Screen *parent, int x, int y, int width, const STR title, const STR text) : |
| 2162 |
ToplevelWindow(parent, x, y, width, 1, title) { |
| 2163 |
message = new Label(this, 5, 5, text, width-10); |
| 2164 |
close = new GUI::Button(this, width/2-40, 10, "Close", 70); |
| 2165 |
close->addActionHandler(this); |
| 2166 |
setText(text); |
| 2167 |
} |
| 2168 |
|
| 2169 |
|
| 2170 |
template <typename STR> void setText(const STR text) { |
| 2171 |
message->setText(text); |
| 2172 |
close->move(width/2-40, 20+message->getHeight()); |
| 2173 |
resize(width, message->getHeight()+100); |
| 2174 |
} |
| 2175 |
}; |
| 2176 |
|
| 2177 |
template <typename STR> ToplevelWindow::ToplevelWindow(Screen *parent, int x, int y, int w, int h, const STR title) : |
| 2178 |
BorderedWindow(parent, x, y, w, h, 6, 33, 6, 3), title(title), |
| 2179 |
dragx(-1), dragy(-1), closehandlers(), systemMenu(new Menu(this,-1,-2,"System Menu")) { |
| 2180 |
systemMenu->addItem("Move"); |
| 2181 |
systemMenu->addItem("Resize"); |
| 2182 |
systemMenu->addItem(""); |
| 2183 |
systemMenu->addItem("Minimize"); |
| 2184 |
systemMenu->addItem("Maximize"); |
| 2185 |
systemMenu->addItem("Restore"); |
| 2186 |
systemMenu->addItem(""); |
| 2187 |
systemMenu->addItem("Close"); |
| 2188 |
systemMenu->addActionHandler(this); |
| 2189 |
} |
| 2190 |
|
| 2191 |
template <typename STR> Button::Button(Window *parent, int x, int y, const STR text, int w, int h) : |
| 2192 |
BorderedWindow(parent,x,y,w,h,6,5,6,5), ActionEventSource(text), pressed(0) |
| 2193 |
{ |
| 2194 |
|
| 2195 |
Label *l = new Label(this,0,0,text); |
| 2196 |
if (width < 0) resize(l->getWidth()+border_left+border_right+10,height); |
| 2197 |
if (height < 0) resize(width,l->getHeight()+border_top+border_bottom+6); |
| 2198 |
l->move((width-border_left-border_right-l->getWidth())/2, |
| 2199 |
(height-border_top-border_bottom-l->getHeight())/2); |
| 2200 |
} |
| 2201 |
|
| 2202 |
template <typename STR> Checkbox::Checkbox(Window *parent, int x, int y, const STR text, int w, int h) : |
| 2203 |
BorderedWindow(parent,x,y,w,h,16,0,0,0), ActionEventSource(text), checked(0) |
| 2204 |
{ |
| 2205 |
Label *l = new Label(this,0,0,text); |
| 2206 |
if (width < 0) resize(l->getWidth()+border_left+border_right+4,height); |
| 2207 |
if (height < 0) resize(width,l->getHeight()+border_top+border_bottom+4); |
| 2208 |
l->move((width-border_left-border_right-l->getWidth())/2, |
| 2209 |
(height-border_top-border_bottom-l->getHeight())/2); |
| 2210 |
} |
| 2211 |
|
| 2212 |
template <typename STR> Radiobox::Radiobox(Frame *parent, int x, int y, const STR text, int w, int h) : |
| 2213 |
BorderedWindow(parent,x,y,w,h,16,0,0,0), ActionEventSource(text), checked(0) |
| 2214 |
{ |
| 2215 |
Label *l = new Label(this,0,0,text); |
| 2216 |
if (width < 0) resize(l->getWidth()+border_left+border_right+4,height); |
| 2217 |
if (height < 0) resize(width,l->getHeight()+border_top+border_bottom+4); |
| 2218 |
l->move((width-border_left-border_right-l->getWidth())/2, |
| 2219 |
(height-border_top-border_bottom-l->getHeight())/2); |
| 2220 |
addActionHandler(parent); |
| 2221 |
} |
| 2222 |
|
| 2223 |
}; |
| 2224 |
|
| 2225 |
#endif |