From 6f594ad00a07365eec68e16d338151dde23bb648 Mon Sep 17 00:00:00 2001 From: pixel Date: Thu, 1 Dec 2005 13:48:12 +0000 Subject: Lots of fixes and adds in mogltk: -) fixed a compilation bug in the engine.h file (widget cyclic dependancy) -) fixed font system so to have unsigned chars instead of chars -) updated the shaper system so to have different fonts to print -) updated the widgets with: -) new methods: center, set_viewport, delete_me -) Label now has a font -) MsgBox now has a font -) InputText widget added -) InputDialog message box added -) fixed a bug in the engine causing unwanted mouse move events -) fixed a bug in the various lists of widgets (.clear != .empty) --- lib/widgets.cc | 178 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 158 insertions(+), 20 deletions(-) (limited to 'lib/widgets.cc') diff --git a/lib/widgets.cc b/lib/widgets.cc index 94cbd73..246a182 100644 --- a/lib/widgets.cc +++ b/lib/widgets.cc @@ -17,7 +17,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -/* $Id: widgets.cc,v 1.14 2005-06-20 22:43:35 pixel Exp $ */ +/* $Id: widgets.cc,v 1.15 2005-12-01 13:48:12 pixel Exp $ */ #include #include @@ -39,6 +39,7 @@ mogltk::widget * dragged_widget = 0; std::vector out_move, out_click; std::vector mogltk::widget::timed_events; +std::vector to_delete; class widget_mouse_event : public mogltk::engine::mouseevent { @@ -107,7 +108,7 @@ void widget_mouse_event::move(SDL_MouseMotionEvent m) { } } - out_move_stack.empty(); + out_move_stack.clear(); } void widget_mouse_event::action(SDL_MouseButtonEvent b) { @@ -137,7 +138,7 @@ void widget_mouse_event::action(SDL_MouseButtonEvent b) { } } - out_click_stack.empty(); + out_click_stack.clear(); } else { mouse_down = false; @@ -168,7 +169,7 @@ void widget_mouse_event::action(SDL_MouseButtonEvent b) { } } - out_click_stack.empty(); + out_click_stack.clear(); if ((SDL_GetTicks() - old_click) < 500) { printm(M_INFO, "mouse_dbl_click\n"); @@ -382,14 +383,21 @@ void mogltk::widget::fulldraw() { } void mogltk::widget::idraw() { - int x1, y1, x2, y2; - if (next) next->idraw(); if (!visible) return; + set_viewport(); + draw(); + + if (child) + child->idraw(); +} + +void mogltk::widget::set_viewport() { + int x1, y1, x2, y2; x1 = MAX(GetAX(), father->GetAX()); y1 = MAX(GetAY(), father->GetAY()); @@ -397,11 +405,6 @@ void mogltk::widget::idraw() { y2 = MIN(GetAY2(), father->GetAY2()); engine::base_o->changeviewport(x1, y1, x2 - x1, y2 - y1); - - draw(); - - if (child) - child->idraw(); } bool mogltk::widget::inside(int xe, int ye) { @@ -587,6 +590,28 @@ void mogltk::widget::set_absolute_timed_event(Uint32 t) { timed_events.push_back(timed_event(this, t)); } +void mogltk::widget::center(bool cx, bool cy) { + rect tr, fr; + tr.x = GetX(); + tr.y = GetY(); + tr.w = GetW(); + tr.h = GetH(); + fr.w = Father()->GetW(); + fr.h = Father()->GetH(); + + if (cx) + tr.x = fr.w / 2 - tr.w / 2; + + if (cy) + tr.y = fr.h / 2 - tr.h / 2; + + move(tr.x, tr.y); +} + +void mogltk::widget::delete_me() { + to_delete.push_back(this); +} + /* * Predefined widgets. @@ -743,20 +768,18 @@ class MessageBoxAction : public mogltk::widgets::action { mogltk::widget * parent; }; -mogltk::widgets::MsgBox::MsgBox(shape * sh, mogltk::widget * father, const String & caption, const String & text) : SmartBoxClose(sh, father, 0, 0, 0, 0, caption) { - rect size = SystemFont->size(text); +mogltk::widgets::MsgBox::MsgBox(shape * sh, mogltk::widget * father, const String & caption, const String & text, mogltk::font * _font) : SmartBox(sh, father, 0, 0, 0, 0, caption), msgbox_font(_font) { + rect size = msgbox_font->size(text); rect lsize = size; size.w += 12; size.h += 60; - size.x = (father->GetW() - size.w) / 2; - size.y = (father->GetH() - size.h) / 2; resize(size.w, size.h); - move(size.x, size.y); + center(); new Button(new MessageBoxAction(this), sh, this, size.w / 2 - 25, size.h - 30, 50, 24, "Ok"); - new Label(sh, this, 6, 24, lsize.w, lsize.h, text, BLACK); + new Label(sh, this, 6, 24, lsize.w, lsize.h, text, BLACK, msgbox_font); set_exclusive(father); } @@ -770,12 +793,12 @@ mogltk::widgets::MsgBox::~MsgBox() { * A simple Label * *****************/ -mogltk::widgets::Label::Label(shape * sh, mogltk::widget * father, int x, int y, int w, int h, const String & _caption, const ColorP & _color) : widget(father, x, y, w, h, 0, "Label", sh), color(_color) { +mogltk::widgets::Label::Label(shape * sh, mogltk::widget * father, int x, int y, int w, int h, const String & _caption, const ColorP & _color, mogltk::font * _font) : widget(father, x, y, w, h, 0, "Label", sh), color(_color), label_font(_font) { caption = _caption; } void mogltk::widgets::Label::draw() { - Shaper()->text(GetAX(), GetAY(), caption, color); + Shaper()->text(GetAX(), GetAY(), caption, color, LEFT, label_font); } @@ -1089,7 +1112,7 @@ void mogltk::widgets::Menu::node::SetEnabled(bool _enabled) { enabled = _enabled; } -mogltk::widgets::Menu::Menu(shape * sh, mogltk::widget * father) : widget(father, 0, 0, 0, 0, 0, "Menu", sh), cur_x(0), selected(-1) { +mogltk::widgets::Menu::Menu(shape * sh, mogltk::widget * father) : widget(father, 0, 0, 0, 0, 0, "Menu", sh), cur_x(0), selected(-1), subselected(0) { rect r = father->GetDrawRect(); move(r.x, r.y); resize(r.w, 16); @@ -1190,6 +1213,116 @@ bool mogltk::widgets::Menu::process_event(int mx, int my, mogltk::event_t event) return false; } +/********************** +* The InputText entry * +**********************/ + +mogltk::widgets::InputText::InputText(shape * sh, mogltk::widget * father, int _x, int _y) : widget(father, _x, _y, 80, 20, 0, "InputText", sh), it_keyevent(0) { + it_keyevent = new mogltk::widgets::InputText::inputtext_keyevent(this); +} + +mogltk::widgets::InputText::~InputText() { + delete it_keyevent; +} + +mogltk::widgets::InputText::inputtext_keyevent::inputtext_keyevent(mogltk::widgets::InputText * _father) : father(_father) { +} + +void mogltk::widgets::InputText::inputtext_keyevent::up(SDL_keysym k) { +} + +void mogltk::widgets::InputText::inputtext_keyevent::down(SDL_keysym k) { + if (father->GetVisible()) { + switch (k.sym) { + case SDLK_BACKSPACE: + if (father->GetText().strlen() == 1) { + father->SetText(""); + } else if (father->GetText().strlen()) { + father->SetText(father->GetText().extract(0, father->GetText().strlen() - 2)); + } + break; + case SDLK_RETURN: + break; + default: + if (k.unicode) { + if (((k.unicode >= 'a') && (k.unicode <= 'z')) || + ((k.unicode >= 'A') && (k.unicode <= 'Z')) || + ((k.unicode >= '0') && (k.unicode <= '9'))) + father->SetText(father->GetText() + ((char) k.unicode)); + } + } + } else { + if (old_handler) + old_handler->down(k); + } +} + +const String & mogltk::widgets::InputText::GetText() const{ + return text; +} + +void mogltk::widgets::InputText::SetText(const String & _text) { + text = _text; +} + +void mogltk::widgets::InputText::draw() { + int rx; + rect r = SystemFont->size(text); + + rx = r.w + GetAX() + 2; + + Shaper()->box3d(GetAX(), GetAY(), GetAX2(), GetAY2(), DOS_WHITE, DOS_HIGH_WHITE, DOS_GRAY, 2, true); + Shaper()->text(MIN(rx, GetAX2() - 2), GetAY() + 2, text, BLACK, RIGHT); +} + + +/************************ +* The InputDialog child * +************************/ + +class InputDialogAction : public mogltk::widgets::action { + public: + InputDialogAction(mogltk::widgets::InputDialog * _parent) : parent(_parent) { } + virtual void do_action(mogltk::widget *) { + parent->do_action(); + delete parent; + delete this; + } + private: + mogltk::widgets::InputDialog * parent; +}; + +mogltk::widgets::InputDialog::InputDialog(mogltk::widgets::action * _a, shape * sh, mogltk::widget * father, const String & caption, const String & text, mogltk::font * _font) : SmartBox(sh, father, 0, 0, 0, 0, caption), a(_a), InputDialog_font(_font) { + rect size = InputDialog_font->size(text); + rect lsize = size; + + size.w += 12; + size.h += 90; + + resize(size.w, size.h); + center(); + + new Button(new InputDialogAction(this), sh, this, size.w / 2 - 25, size.h - 30, 50, 24, "Ok"); + it = new InputText(sh, this, 10, size.h - 60); + it->resize(size.w - 20, 20); + new Label(sh, this, 6, 24, lsize.w, lsize.h, text, BLACK, InputDialog_font); + + set_exclusive(father); +} + +mogltk::widgets::InputDialog::~InputDialog() { + unset_exclusive(Father()); +} + +String mogltk::widgets::InputDialog::GetText() { + return it->GetText(); +} + +void mogltk::widgets::InputDialog::do_action() { + if (a) + a->do_action(this); +} + /* * The mainloop widget thing. @@ -1215,6 +1348,11 @@ void mogltk::widget::mainloop() { engine::base_o->Flip(); widget::check_timed_events(); + + for (std::vector::iterator i = to_delete.begin(); i != to_delete.end(); i++) { + delete *i; + } + to_delete.clear(); } delete mouse; -- cgit v1.2.3