initial commit, lordsawar source, slightly modified
[lordsawar] / src / gui / timed-message-dialog.cpp
1 //  Copyright (C) 2008 Ben Asselstine
2 //
3 //  This program is free software; you can redistribute it and/or modify
4 //  it under the terms of the GNU General Public License as published by
5 //  the Free Software Foundation; either version 3 of the License, or
6 //  (at your option) any later version.
7 //
8 //  This program is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 //  GNU Library General Public License for more details.
12 //
13 //  You should have received a copy of the GNU General Public License
14 //  along with this program; if not, write to the Free Software
15 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
16 //  02110-1301, USA.
17
18 #include <config.h>
19
20 #include <assert.h>
21 #include <gtkmm.h>
22
23 #include "timed-message-dialog.h"
24
25 #include "ucompose.hpp"
26 #include "decorated.h"
27 #include "defs.h"
28 #include "timing.h"
29 #include "decorated.h"
30
31 TimedMessageDialog::TimedMessageDialog(Gtk::Window &parent, std::string message, int timeout, int grace)
32 {
33   d_timeout = timeout;
34   d_timer_count = 0;
35   d_grace = grace;
36     
37   window = new Gtk::MessageDialog(parent, message);
38   //Gtk::MessageDialog dialog(parent, message);
39   //window.reset(&dialog);
40   window->set_message(message);
41   window->signal_response().connect
42        (sigc::mem_fun(*this, &TimedMessageDialog::on_response));
43   Decorated decorator;
44   decorator.decorate(window);
45   decorator.window_closed.connect(sigc::mem_fun(window, &Gtk::Dialog::hide));
46   window->set_transient_for(parent);
47 }
48
49 void TimedMessageDialog::on_response(int id)
50 {
51   window->hide();
52   main_loop->quit();
53 }
54
55 TimedMessageDialog::~TimedMessageDialog()
56 {
57   delete window;
58 }
59
60 void TimedMessageDialog::show_all()
61 {
62     window->show_all();
63 }
64
65 void TimedMessageDialog::hide()
66 {
67   window->hide();
68 }
69
70 void TimedMessageDialog::run()
71 {
72   if (d_timeout > 0)
73     {
74       Timing::instance().register_timer
75         (sigc::mem_fun(this, &TimedMessageDialog::tick), 1000);
76     }
77     
78     window->show_all();
79     main_loop = Glib::MainLoop::create();
80     main_loop->run();
81 }
82
83 bool TimedMessageDialog::tick()
84 {
85   d_timer_count++;
86   if (d_grace)
87     {
88       if (d_timer_count >= d_grace)
89         {
90           d_grace = 0;
91           d_timer_count = 0;
92           return Timing::CONTINUE;
93         }
94     }
95   else
96     {
97       int secs = d_timeout - d_timer_count;
98       Glib::ustring s;
99       s = String::ucompose(ngettext("This message will disappear in %1 second.",
100                                     "This message will disappear in %1 seconds.",
101                                     secs), secs);
102       window->set_secondary_text(s);
103     }
104
105   if (d_timer_count <= d_timeout)
106     return Timing::CONTINUE;
107
108   window->hide();
109   main_loop->quit();
110
111   return Timing::STOP;
112 }
113     
114 void TimedMessageDialog::set_title(std::string title)
115 {
116   window->set_title(title);
117 }
118     
119 void TimedMessageDialog::set_image(Glib::RefPtr<Gdk::Pixbuf> picture)
120 {
121   Gtk::Image *image = new Gtk::Image(picture);
122   window->property_image() = image;
123 }