summaryrefslogtreecommitdiff
path: root/lib/inputtext.cc
blob: 23b05a147297f4d6c60754edc8180978b9013928 (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
/*
 *  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.5 2007-07-27 14:10:20 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() 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();
}