20130313
[gdash.git] / src / framework / inputtextactivity.cpp
blobd64774b2ded78b53a7dde2244465e51798d6e8ba
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)
31 text = g_string_new(default_text);
35 InputTextActivity::~InputTextActivity() {
36 g_string_free(text, TRUE);
40 void InputTextActivity::redraw_event() {
41 int height=6*app->font_manager->get_line_height();
42 int y1=(app->screen->get_height()-height)/2; /* middle of the screen */
43 int cx=2*app->font_manager->get_font_width_narrow(), cy=y1, cw=app->screen->get_width()-2*cx, ch=height;
45 app->draw_window(cx, cy, cw, ch);
46 app->screen->set_clip_rect(cx, cy, cw, ch);
47 int width=cw/app->font_manager->get_font_width_narrow();
49 app->set_color(GD_GDASH_WHITE);
50 app->blittext_n(-1, y1+app->font_manager->get_line_height(), title.c_str());
51 int len = g_utf8_strlen(text->str, -1);
52 int x;
53 if (len<width-1)
54 x=-1; /* if fits on screen (+1 for cursor), centered */
55 else
56 x=cx+cw-(len+1)*app->font_manager->get_font_width_narrow(); /* otherwise show end, +1 for cursor */
57 app->blittext_n(x, y1+3*app->font_manager->get_line_height(), CPrintf("%s%c") % text->str % (blink?'_':' '));
58 app->screen->remove_clip_rect();
59 app->screen->flip();
63 void InputTextActivity::keypress_event(KeyCode keycode, int gfxlib_keycode) {
64 switch (keycode) {
65 case App::Enter:
66 command_when_successful->set_param1(text->str);
67 app->enqueue_command(new PopActivityCommand(app));
68 app->enqueue_command(command_when_successful);
69 break;
70 case App::BackSpace:
71 if (text->len!=0) {
72 char *ptr=text->str+text->len; /* string pointer + length: points to the terminating zero */
73 ptr=g_utf8_prev_char(ptr); /* step back one utf8 character */
74 g_string_truncate(text, ptr-text->str);
75 redraw_event();
77 break;
78 case App::Escape:
79 app->enqueue_command(new PopActivityCommand(app));
80 break;
81 default:
82 if (keycode >= ' ') {
83 g_string_append_unichar(text, keycode);
84 redraw_event();
86 break;
91 void InputTextActivity::timer_event(int ms_elapsed) {
92 ms += ms_elapsed;
93 if (ms > 400) {
94 blink = !blink;
95 ms -= 400;
96 redraw_event();