20130313
[gdash.git] / src / framework / showtextactivity.cpp
blob9f1d6cd974ba5b5d96b47e9894e55f6f75a85ed1
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/showtextactivity.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 ShowTextActivity::ShowTextActivity(App *app, char const *title_line, std::string const &text, SmartPtr<Command> command_after_exit)
32 Activity(app),
33 command_after_exit(command_after_exit),
34 title_line(title_line)
36 wrapped_text = gd_wrap_text(text.c_str(), app->screen->get_width() / app->font_manager->get_font_width_narrow()-4);
37 linesavailable = app->screen->get_height() / app->font_manager->get_line_height()-4;
38 if ((int)wrapped_text.size() < linesavailable)
39 scroll_max_y = 0;
40 else
41 scroll_max_y = wrapped_text.size()-linesavailable;
42 scroll_y = 0;
46 void ShowTextActivity::redraw_event() {
47 app->clear_screen();
49 app->title_line(title_line.c_str());
50 // TRANSLATORS: 40 chars max
51 app->status_line(_("Crsr: move Esc: exit"));
53 // up & down arrow
54 app->set_color(GD_GDASH_GRAY2);
55 if (scroll_y<scroll_max_y)
56 app->blittext_n(app->screen->get_width() - app->font_manager->get_font_width_narrow(),
57 app->screen->get_height()-3*app->font_manager->get_line_height(), CPrintf("%c") % GD_DOWN_CHAR);
58 if (scroll_y>0)
59 app->blittext_n(app->screen->get_width() - app->font_manager->get_font_width_narrow(),
60 app->font_manager->get_line_height()*2, CPrintf("%c") % GD_UP_CHAR);
61 // text
62 app->set_color(GD_GDASH_LIGHTBLUE);
63 for (int l=0; l<linesavailable && scroll_y + l<(int)wrapped_text.size(); ++l)
64 app->blittext_n(app->font_manager->get_font_width_narrow()*2,
65 l*app->font_manager->get_line_height()+app->font_manager->get_line_height()*2, wrapped_text[scroll_y+l].c_str());
67 app->screen->flip();
71 ShowTextActivity::~ShowTextActivity() {
72 app->enqueue_command(command_after_exit);
76 void ShowTextActivity::keypress_event(KeyCode keycode, int gfxlib_keycode) {
77 switch (keycode) {
78 case App::PageUp:
79 scroll_y -= linesavailable-1;
80 if (scroll_y < 0)
81 scroll_y = 0;
82 redraw_event();
83 break;
84 case App::Up:
85 if (scroll_y > 0) {
86 scroll_y--;
87 redraw_event();
89 break;
90 case App::PageDown:
91 scroll_y += linesavailable-1;
92 if (scroll_y > scroll_max_y)
93 scroll_y = scroll_max_y;
94 redraw_event();
95 break;
96 case App::Down:
97 if (scroll_y < scroll_max_y) {
98 scroll_y++;
99 redraw_event();
101 break;
102 default:
103 app->enqueue_command(new PopActivityCommand(app));
104 break;
105 case 0:
106 /* unknown key or modifier - do nothing */
107 break;