From edd5fe68be2a2a9e23a87f65f6a89807c34b7c3f Mon Sep 17 00:00:00 2001 From: pixel Date: Fri, 2 Dec 2005 16:21:59 +0000 Subject: Added cascade stuff for buttons, and cleaned source a bit. --- lib/inputtext.cc | 153 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 lib/inputtext.cc (limited to 'lib/inputtext.cc') diff --git a/lib/inputtext.cc b/lib/inputtext.cc new file mode 100644 index 0000000..20aa34c --- /dev/null +++ b/lib/inputtext.cc @@ -0,0 +1,153 @@ +/* + * mogltk + * Copyright (C) 1999-2005 Nicolas "Pixel" Noble + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* $Id: inputtext.cc,v 1.1 2005-12-02 16:21:59 pixel Exp $ */ + +#include +#include +#include +#include "font.h" +#include "engine.h" +#include "widgets.h" +#include "sprite.h" + +/********************** +* The InputText entry * +**********************/ + +mogltk::widgets::InputText::InputText(mogltk::widgets::action * _a, shape * sh, mogltk::widget * father, int _x, int _y) : widget(father, _x, _y, 80, 20, 0, "InputText", sh), it_keyevent(0), a(_a), cursor_visible(false), cursor_pos(0) { + it_keyevent = new mogltk::widgets::InputText::inputtext_keyevent(this); + set_timed_event(500); +} + +mogltk::widgets::InputText::~InputText() { + delete it_keyevent; +} + +void mogltk::widgets::InputText::activate() { + if (a) + a->do_action(this); +} + +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: + father->activate(); + 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; +} + +bool mogltk::widgets::InputText::process_event(int x, int y, mogltk::event_t event) { + switch (event) { + case E_TIMER: + set_timed_event(500); + cursor_visible = !cursor_visible; + return true; + } + return false; +} + +void mogltk::widgets::InputText::draw() { + int x = MIN(SystemFont->singletextsize(text) + GetAX() + 2, GetAX2() - 2); + + Shaper()->box3d(GetAX(), GetAY(), GetAX2(), GetAY2(), DOS_WHITE, DOS_HIGH_WHITE, DOS_GRAY, 2, true); + Shaper()->text(x, GetAY() + 2, text, BLACK, RIGHT); + if (cursor_visible) { + Shaper()->box(x, GetAY() + 2, x + 2, GetAY2() - 2, Color(0, 0, 0, 128)); + } +} + + +/************************ +* 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(); + + (it = new InputText((new Button(new InputDialogAction(this), sh, this, size.w / 2 - 25, size.h - 30, 50, 24, "Ok"))->cascade_action(), sh, this, 10, size.h - 60))->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); +} -- cgit v1.2.3