20130313
[gdash.git] / src / framework / messageactivity.cpp
blobfa82337fcfdef05e46fb2e6c10e2cf7dfa8ef795
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 <glib.h>
18 #include <glib/gi18n.h>
19 #include <cstring>
21 #include "framework/messageactivity.hpp"
22 #include "framework/commands.hpp"
23 #include "cave/helper/colors.hpp"
24 #include "gfx/screen.hpp"
25 #include "gfx/fontmanager.hpp"
26 #include "misc/util.hpp"
27 #include "misc/printf.hpp"
30 MessageActivity::MessageActivity(App *app, std::string const &primary, std::string const &secondary, SmartPtr<Command> command_after_exit)
32 Activity(app),
33 command_after_exit(command_after_exit)
35 std::string text;
36 if (secondary != "")
37 text = SPrintf("%c%s\n\n%c%s") % GD_COLOR_INDEX_WHITE % primary % GD_COLOR_INDEX_GRAY3 % secondary;
38 else
39 text = SPrintf("%c%s") % GD_COLOR_INDEX_WHITE % primary;
40 wrapped_text = gd_wrap_text(text.c_str(), app->screen->get_width() / app->font_manager->get_font_width_narrow()-6);
44 void MessageActivity::redraw_event() {
45 int height = (wrapped_text.size() + 2) * app->font_manager->get_line_height(); /* +2 empty lines */
46 int y1=(app->screen->get_height()-height)/2; /* middle of the screen */
47 int cx=2*app->font_manager->get_font_width_narrow(), cy=y1, cw=app->screen->get_width()-2*cx, ch=height;
49 app->draw_window(cx, cy, cw, ch);
50 app->screen->set_clip_rect(cx, cy, cw, ch);
52 app->set_color(GD_GDASH_WHITE);
53 for (size_t i = 0; i < wrapped_text.size(); ++i)
54 app->blittext_n(-1, y1+(i+1)*app->font_manager->get_line_height(), wrapped_text[i].c_str());
56 app->screen->remove_clip_rect();
58 app->screen->flip();
62 MessageActivity::~MessageActivity() {
63 app->enqueue_command(command_after_exit);
67 void MessageActivity::keypress_event(KeyCode keycode, int gfxlib_keycode) {
68 switch (keycode) {
69 default:
70 app->enqueue_command(new PopActivityCommand(app));
71 break;
72 case 0:
73 /* unknown key or modifier - do nothing */
74 break;