initial commit, lordsawar source, slightly modified
[lordsawar] / src / gui / timed-message-dialog.cpp
diff --git a/src/gui/timed-message-dialog.cpp b/src/gui/timed-message-dialog.cpp
new file mode 100644 (file)
index 0000000..023f733
--- /dev/null
@@ -0,0 +1,123 @@
+//  Copyright (C) 2008 Ben Asselstine
+//
+//  This program is free software; you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation; either version 3 of the License, or
+//  (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU Library General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
+//  02110-1301, USA.
+
+#include <config.h>
+
+#include <assert.h>
+#include <gtkmm.h>
+
+#include "timed-message-dialog.h"
+
+#include "ucompose.hpp"
+#include "decorated.h"
+#include "defs.h"
+#include "timing.h"
+#include "decorated.h"
+
+TimedMessageDialog::TimedMessageDialog(Gtk::Window &parent, std::string message, int timeout, int grace)
+{
+  d_timeout = timeout;
+  d_timer_count = 0;
+  d_grace = grace;
+    
+  window = new Gtk::MessageDialog(parent, message);
+  //Gtk::MessageDialog dialog(parent, message);
+  //window.reset(&dialog);
+  window->set_message(message);
+  window->signal_response().connect
+       (sigc::mem_fun(*this, &TimedMessageDialog::on_response));
+  Decorated decorator;
+  decorator.decorate(window);
+  decorator.window_closed.connect(sigc::mem_fun(window, &Gtk::Dialog::hide));
+  window->set_transient_for(parent);
+}
+
+void TimedMessageDialog::on_response(int id)
+{
+  window->hide();
+  main_loop->quit();
+}
+
+TimedMessageDialog::~TimedMessageDialog()
+{
+  delete window;
+}
+
+void TimedMessageDialog::show_all()
+{
+    window->show_all();
+}
+
+void TimedMessageDialog::hide()
+{
+  window->hide();
+}
+
+void TimedMessageDialog::run()
+{
+  if (d_timeout > 0)
+    {
+      Timing::instance().register_timer
+       (sigc::mem_fun(this, &TimedMessageDialog::tick), 1000);
+    }
+    
+    window->show_all();
+    main_loop = Glib::MainLoop::create();
+    main_loop->run();
+}
+
+bool TimedMessageDialog::tick()
+{
+  d_timer_count++;
+  if (d_grace)
+    {
+      if (d_timer_count >= d_grace)
+       {
+         d_grace = 0;
+         d_timer_count = 0;
+         return Timing::CONTINUE;
+       }
+    }
+  else
+    {
+      int secs = d_timeout - d_timer_count;
+      Glib::ustring s;
+      s = String::ucompose(ngettext("This message will disappear in %1 second.",
+                                   "This message will disappear in %1 seconds.",
+                                   secs), secs);
+      window->set_secondary_text(s);
+    }
+
+  if (d_timer_count <= d_timeout)
+    return Timing::CONTINUE;
+
+  window->hide();
+  main_loop->quit();
+
+  return Timing::STOP;
+}
+    
+void TimedMessageDialog::set_title(std::string title)
+{
+  window->set_title(title);
+}
+    
+void TimedMessageDialog::set_image(Glib::RefPtr<Gdk::Pixbuf> picture)
+{
+  Gtk::Image *image = new Gtk::Image(picture);
+  window->property_image() = image;
+}