Optimize automation-event process splitting
[ardour.git] / gtk2_ardour / splash.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <string>
21
22 #include "pbd/failed_constructor.h"
23 #include "pbd/file_utils.h"
24 #include "pbd/stacktrace.h"
25
26 #include "ardour/ardour.h"
27 #include "ardour/filesystem_paths.h"
28
29 #include "gtkmm2ext/utils.h"
30
31 #ifdef check
32 #undef check
33 #endif
34
35 #include "gui_thread.h"
36 #include "splash.h"
37
38 #include "pbd/i18n.h"
39
40 using namespace Gtk;
41 using namespace Glib;
42 using namespace PBD;
43 using namespace std;
44 using namespace ARDOUR;
45
46 Splash* Splash::the_splash = 0;
47
48 Splash::Splash ()
49 {
50         assert (the_splash == 0);
51
52         std::string splash_file;
53
54         Searchpath rc (ARDOUR::ardour_data_search_path());
55         rc.add_subdirectory_to_paths ("resources");
56
57         if (!find_file (rc, PROGRAM_NAME "-splash.png", splash_file)) {
58                 cerr << "Cannot find splash screen image file\n";
59                 throw failed_constructor();
60         }
61
62         try {
63                 pixbuf = Gdk::Pixbuf::create_from_file (splash_file);
64         }
65
66         catch (...) {
67                 cerr << "Cannot construct splash screen image\n";
68                 throw failed_constructor();
69         }
70
71         darea.set_size_request (pixbuf->get_width(), pixbuf->get_height());
72         pop_front ();
73         set_position (WIN_POS_CENTER);
74         darea.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
75         darea.set_double_buffered (false);
76
77         layout = create_pango_layout ("");
78         string str = "<b>";
79         string i18n = string_compose (_("%1 loading ..."), PROGRAM_NAME);
80         str += i18n;
81         str += "</b>";
82
83         layout->set_markup (str);
84
85         darea.show ();
86         darea.signal_expose_event().connect (sigc::mem_fun (*this, &Splash::expose));
87
88         add (darea);
89
90         set_default_size (pixbuf->get_width(), pixbuf->get_height());
91         set_resizable (false);
92         set_type_hint(Gdk::WINDOW_TYPE_HINT_SPLASHSCREEN);
93         the_splash = this;
94
95         expose_done = false;
96         expose_is_the_one = false;
97
98         ARDOUR::BootMessage.connect (msg_connection, invalidator (*this), boost::bind (&Splash::boot_message, this, _1), gui_context());
99 }
100
101 Splash::~Splash ()
102 {
103         idle_connection.disconnect ();
104         expose_done = true;
105         hide ();
106         the_splash = 0;
107 }
108
109 void
110 Splash::pop_back_for (Gtk::Window& win)
111 {
112 #if defined  __APPLE__ || defined PLATFORM_WINDOWS
113         /* April 2013: window layering on OS X is a bit different to X Window. at present,
114          * the "restack()" functionality in GDK will only operate on windows in the same
115          * "level" (e.g. two normal top level windows, or two utility windows) and will not
116          * work across them. The splashscreen is on its own "StatusWindowLevel" so restacking
117          * is not going to work.
118          *
119          * So for OS X, we just hide ourselves.
120          *
121          * Oct 2014: The Windows situation is similar, although it should be possible
122          * to play tricks with gdk's set_type_hint() or directly hack things using
123          * SetWindowLong() and UpdateLayeredWindow()
124          */
125         (void) win;
126         hide();
127 #else
128         set_keep_above (false);
129         get_window()->restack (win.get_window(), false);
130 #endif
131 }
132
133 void
134 Splash::pop_front ()
135 {
136         if (get_window()) {
137 #if defined  __APPLE__ || defined PLATFORM_WINDOWS
138                 show ();
139 #else
140                 gdk_window_restack(get_window()->gobj(), NULL, true);
141 #endif
142         }
143 }
144
145 void
146 Splash::hide ()
147 {
148         Gtk::Window::hide();
149 }
150
151 void
152 Splash::on_realize ()
153 {
154         Window::on_realize ();
155         get_window()->set_decorations (Gdk::WMDecoration(0));
156         layout->set_font_description (get_style()->get_font());
157 }
158
159 bool
160 Splash::on_button_release_event (GdkEventButton* ev)
161 {
162         RefPtr<Gdk::Window> window = get_window();
163
164         if (!window || ev->window != window->gobj()) {
165                 return false;
166         }
167
168         hide ();
169         return true;
170 }
171
172 bool
173 Splash::expose (GdkEventExpose* ev)
174 {
175         RefPtr<Gdk::Window> window = darea.get_window();
176
177         /* note: height & width need to be constrained to the pixbuf size
178            in case a WM provides us with a screwy allocation
179         */
180
181         window->draw_pixbuf (get_style()->get_bg_gc (STATE_NORMAL), pixbuf,
182                              ev->area.x, ev->area.y,
183                              ev->area.x, ev->area.y,
184                              min ((pixbuf->get_width() - ev->area.x), ev->area.width),
185                              min ((pixbuf->get_height() - ev->area.y), ev->area.height),
186                              Gdk::RGB_DITHER_NONE, 0, 0);
187
188         Glib::RefPtr<Gtk::Style> style = darea.get_style();
189         Glib::RefPtr<Gdk::GC> white = style->get_white_gc();
190
191         window->draw_layout (white, 10, pixbuf->get_height() - 30, layout);
192
193         /* this must execute AFTER the GDK idle update mechanism
194          */
195
196         if (expose_is_the_one) {
197                 idle_connection = Glib::signal_idle().connect (
198                                 sigc::mem_fun (this, &Splash::idle_after_expose),
199                                 GDK_PRIORITY_REDRAW+2);
200         }
201
202         return true;
203 }
204
205 void
206 Splash::boot_message (std::string msg)
207 {
208         message (msg);
209 }
210
211 bool
212 Splash::idle_after_expose ()
213 {
214         expose_done = true;
215         return false;
216 }
217
218 void
219 Splash::display ()
220 {
221         bool was_mapped = is_mapped ();
222
223         if (!was_mapped) {
224                 expose_done = false;
225                 expose_is_the_one = false;
226         }
227
228         pop_front ();
229         present ();
230
231         if (!was_mapped) {
232                 while (!expose_done && gtk_events_pending()) {
233                         gtk_main_iteration ();
234                 }
235                 gdk_display_flush (gdk_display_get_default());
236         }
237 }
238
239 void
240 Splash::message (const string& msg)
241 {
242         string str ("<b>");
243         str += Gtkmm2ext::markup_escape_text (msg);
244         str += "</b>";
245
246         show ();
247
248         layout->set_markup (str);
249         Glib::RefPtr<Gdk::Window> win = darea.get_window();
250
251         if (win) {
252                 if (win->is_visible ()) {
253                         win->invalidate_rect (Gdk::Rectangle (0, darea.get_height() - 30, darea.get_width(), 30), true);
254                 } else {
255                         darea.queue_draw ();
256                 }
257                 if (expose_done) {
258                         ARDOUR::GUIIdle ();
259                 }
260         }
261 }
262
263 bool
264 Splash::on_map_event (GdkEventAny* ev)
265 {
266         expose_is_the_one = true;
267         return Window::on_map_event (ev);
268 }