/* * 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.4 2006-10-28 16:50:46 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() throw (GeneralException) { 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 * ************************/ 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), InputDialog_font(_font) { rect size = InputDialog_font->size(text); rect lsize = size; Button * b; size.w += 12; size.h += 90; resize(size.w, size.h); center(); b = new Button(new ActionAndDelete(this, a), sh, this, size.w / 2 - 25, size.h - 30, 50, 24, "Ok"); it = new InputText(b->cascade_action(), 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() throw (GeneralException) { unset_exclusive(Father()); } String mogltk::Widgets::InputDialog::GetText() { return it->GetText(); }