20130320
[gdash.git] / src / framework / inputtextactivity.cpp
blob538739e25c1311fdd628e952a6c9860f61d76994
1 /*
2 * Copyright (c) 2007-2013, Czirkos Zoltan http://code.google.com/p/gdash/
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "framework/inputtextactivity.hpp"
18 #include "framework/inputtextactivity.hpp"
19 #include "gfx/fontmanager.hpp"
20 #include "gfx/screen.hpp"
21 #include "cave/helper/colors.hpp"
22 #include "misc/printf.hpp"
24 InputTextActivity::InputTextActivity(App *app, char const *title_line, const char *default_text, SmartPtr<Command1Param<std::string> > command_when_successful)
26 Activity(app),
27 title(title_line),
28 command_when_successful(command_when_successful),
29 ms(0), blink(false) {
30 text = g_string_new(default_text);
34 InputTextActivity::~InputTextActivity() {
35 g_string_free(text, TRUE);
39 void InputTextActivity::redraw_event() {
40 int height=6*app->font_manager->get_line_height();
41 int y1=(app->screen->get_height()-height)/2; /* middle of the screen */
42 int cx=2*app->font_manager->get_font_width_narrow(), cy=y1, cw=app->screen->get_width()-2*cx, ch=height;
44 app->draw_window(cx, cy, cw, ch);
45 app->screen->set_clip_rect(cx, cy, cw, ch);
46 int width=cw/app->font_manager->get_font_width_narrow();
48 app->set_color(GD_GDASH_WHITE);
49 app->blittext_n(-1, y1+app->font_manager->get_line_height(), title.c_str());
50 int len = g_utf8_strlen(text->str, -1);
51 int x;
52 if (len<width-1)
53 x=-1; /* if fits on screen (+1 for cursor), centered */
54 else
55 x=cx+cw-(len+1)*app->font_manager->get_font_width_narrow(); /* otherwise show end, +1 for cursor */
56 app->blittext_n(x, y1+3*app->font_manager->get_line_height(), CPrintf("%s%c") % text->str % (blink?'_':' '));
57 app->screen->remove_clip_rect();
58 app->screen->flip();
62 void InputTextActivity::keypress_event(KeyCode keycode, int gfxlib_keycode) {
63 switch (keycode) {
64 case App::Enter:
65 command_when_successful->set_param1(text->str);
66 app->enqueue_command(new PopActivityCommand(app));
67 app->enqueue_command(command_when_successful);
68 break;
69 case App::BackSpace:
70 if (text->len!=0) {
71 char *ptr=text->str+text->len; /* string pointer + length: points to the terminating zero */
72 ptr=g_utf8_prev_char(ptr); /* step back one utf8 character */
73 g_string_truncate(text, ptr-text->str);
74 redraw_event();
76 break;
77 case App::Escape:
78 app->enqueue_command(new PopActivityCommand(app));
79 break;
80 default:
81 if (keycode >= ' ') {
82 g_string_append_unichar(text, keycode);
83 redraw_event();
85 break;
90 void InputTextActivity::timer_event(int ms_elapsed) {
91 ms += ms_elapsed;
92 if (ms > 400) {
93 blink = !blink;
94 ms -= 400;
95 redraw_event();