summaryrefslogtreecommitdiff
path: root/lib/inputtext.cc
blob: 20aa34cb016e7a080fd0d9a29274ffd190d2133b (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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 <SDL.h>
#include <vector>
#include <Input.h>
#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);
}