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