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
)
28 command_when_successful(command_when_successful
),
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);
53 x
=-1; /* if fits on screen (+1 for cursor), centered */
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();
62 void InputTextActivity::keypress_event(KeyCode keycode
, int gfxlib_keycode
) {
65 command_when_successful
->set_param1(text
->str
);
66 app
->enqueue_command(new PopActivityCommand(app
));
67 app
->enqueue_command(command_when_successful
);
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
);
78 app
->enqueue_command(new PopActivityCommand(app
));
82 g_string_append_unichar(text
, keycode
);
90 void InputTextActivity::timer_event(int ms_elapsed
) {