handle deletion of UI objects between the time that a callback is queued with the...
[ardour.git] / gtk2_ardour / splash.cc
1 #include <string>
2
3 #include "pbd/failed_constructor.h"
4 #include "pbd/file_utils.h"
5 #include "ardour/ardour.h"
6 #include "ardour/filesystem_paths.h"
7
8 #include "gui_thread.h"
9 #include "splash.h"
10
11 #include "i18n.h"
12
13 using namespace Gtk;
14 using namespace Glib;
15 using namespace PBD;
16 using namespace std;
17 using namespace ARDOUR;
18
19 Splash* Splash::the_splash = 0;
20
21 Splash::Splash ()
22 {
23         sys::path splash_file;
24
25         if (!find_file_in_search_path (ardour_search_path() + system_data_search_path(), "splash.png", splash_file)) {
26                 throw failed_constructor();
27         }
28
29         try {
30                 pixbuf = Gdk::Pixbuf::create_from_file (splash_file.to_string());
31         }
32
33         catch (...) {
34                 throw failed_constructor();
35         }
36
37         darea.set_size_request (pixbuf->get_width(), pixbuf->get_height());
38         set_keep_above (true);
39         set_position (WIN_POS_CENTER);
40         darea.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
41         darea.set_double_buffered (false);
42
43         layout = create_pango_layout ("");
44         string str = "<b>";
45         string i18n = string_compose (_("%1 loading ..."), PROGRAM_NAME);
46         str += i18n;
47         str += "</b>";
48
49         layout->set_markup (str);
50
51         darea.show ();
52         darea.signal_expose_event().connect (sigc::mem_fun (*this, &Splash::expose));
53
54         add (darea);
55
56         set_default_size (pixbuf->get_width(), pixbuf->get_height());
57         the_splash = this;
58
59         ARDOUR::BootMessage.connect (msg_connection, invalidator (*this), ui_bind (&Splash::boot_message, this, _1), gui_context());
60 }
61
62 void
63 Splash::pop_back ()
64 {
65         set_keep_above (false);
66 }
67
68 void
69 Splash::on_realize ()
70 {
71         Window::on_realize ();
72         get_window()->set_decorations (Gdk::WMDecoration(0));
73         layout->set_font_description (get_style()->get_font());
74 }
75
76
77 bool
78 Splash::on_button_release_event (GdkEventButton*)
79 {
80         hide ();
81         return true;
82 }
83
84 bool
85 Splash::expose (GdkEventExpose* ev)
86 {
87         RefPtr<Gdk::Window> window = darea.get_window();
88
89         /* note: height & width need to be constrained to the pixbuf size
90            in case a WM provides us with a screwy allocation
91         */
92
93         window->draw_pixbuf (get_style()->get_bg_gc (STATE_NORMAL), pixbuf,
94                              ev->area.x, ev->area.y,
95                              ev->area.x, ev->area.y,
96                              min ((pixbuf->get_width() - ev->area.x), ev->area.width),
97                              min ((pixbuf->get_height() - ev->area.y), ev->area.height),
98                              Gdk::RGB_DITHER_NONE, 0, 0);
99
100         Glib::RefPtr<Gtk::Style> style = darea.get_style();
101         Glib::RefPtr<Gdk::GC> white = style->get_white_gc();
102
103         window->draw_layout (white, 10, pixbuf->get_height() - 30, layout);
104
105         return true;
106 }
107
108 void
109 Splash::boot_message (std::string msg)
110 {
111         message (msg);
112 }
113
114 void
115 Splash::message (const string& msg)
116 {
117         string str ("<b>");
118         str += msg;
119         str += "</b>";
120
121         layout->set_markup (str);
122         Glib::RefPtr<Gdk::Window> win = darea.get_window();
123
124         if (win) {
125                 win->invalidate_rect (Gdk::Rectangle (0, darea.get_height() - 30,
126                                                       darea.get_width(), 30), true);
127                 win->process_updates (true);
128                 gdk_flush ();
129         }
130 }