summaryrefslogtreecommitdiff
path: root/gtk2_ardour/splash.cc
blob: 70a855ded46e829b7d644956fd431e48f8fd4442 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * Copyright (C) 2008-2012 David Robillard <d@drobilla.net>
 * Copyright (C) 2008-2016 Paul Davis <paul@linuxaudiosystems.com>
 * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
 * Copyright (C) 2012-2014 Tim Mayberry <mojofunk@gmail.com>
 * Copyright (C) 2012-2017 Robin Gareus <robin@gareus.org>
 *
 * 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 2 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 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 <string>

#include "pbd/failed_constructor.h"
#include "pbd/file_utils.h"
#include "pbd/stacktrace.h"

#include "ardour/ardour.h"
#include "ardour/filesystem_paths.h"

#include "gtkmm2ext/utils.h"

#ifdef check
#undef check
#endif

#include "gui_thread.h"
#include "splash.h"

#include "pbd/i18n.h"

using namespace Gtk;
using namespace Glib;
using namespace PBD;
using namespace std;
using namespace ARDOUR;

Splash* Splash::the_splash = 0;

Splash*
Splash::instance()
{
	if (!the_splash) {
		the_splash = new Splash;
	}
	return the_splash;
}

bool
Splash::exists ()
{
	return the_splash;
}

void
Splash::drop ()
{
	delete the_splash;
	the_splash = 0;
}

Splash::Splash ()
{
	assert (the_splash == 0);

	std::string splash_file;

	Searchpath rc (ARDOUR::ardour_data_search_path());
	rc.add_subdirectory_to_paths ("resources");

	if (!find_file (rc, PROGRAM_NAME "-splash.png", splash_file)) {
		cerr << "Cannot find splash screen image file\n";
		throw failed_constructor();
	}

	try {
		pixbuf = Gdk::Pixbuf::create_from_file (splash_file);
	}

	catch (...) {
		cerr << "Cannot construct splash screen image\n";
		throw failed_constructor();
	}

	darea.set_size_request (pixbuf->get_width(), pixbuf->get_height());
	pop_front ();
	set_position (WIN_POS_CENTER);
	darea.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
	darea.set_double_buffered (false);

	layout = create_pango_layout ("");
	string str = "<b>";
	string i18n = string_compose (_("%1 loading ..."), PROGRAM_NAME);
	str += i18n;
	str += "</b>";

	layout->set_markup (str);

	darea.show ();
	darea.signal_expose_event().connect (sigc::mem_fun (*this, &Splash::expose));

	add (darea);

	set_default_size (pixbuf->get_width(), pixbuf->get_height());
	set_resizable (false);
	set_type_hint(Gdk::WINDOW_TYPE_HINT_SPLASHSCREEN);
	the_splash = this;

	expose_done = false;
	expose_is_the_one = false;

	ARDOUR::BootMessage.connect (msg_connection, invalidator (*this), boost::bind (&Splash::boot_message, this, _1), gui_context());
}

Splash::~Splash ()
{
	idle_connection.disconnect ();
	expose_done = true;
	hide ();
	the_splash = 0;
}

void
Splash::pop_back_for (Gtk::Window& win)
{
#if defined  __APPLE__ || defined PLATFORM_WINDOWS
	/* April 2013: window layering on OS X is a bit different to X Window. at present,
	 * the "restack()" functionality in GDK will only operate on windows in the same
	 * "level" (e.g. two normal top level windows, or two utility windows) and will not
	 * work across them. The splashscreen is on its own "StatusWindowLevel" so restacking
	 * is not going to work.
	 *
	 * So for OS X, we just hide ourselves.
	 *
	 * Oct 2014: The Windows situation is similar, although it should be possible
	 * to play tricks with gdk's set_type_hint() or directly hack things using
	 * SetWindowLong() and UpdateLayeredWindow()
	 */
	(void) win;
	hide();
#else
	set_keep_above (false);
	if (is_mapped()) {
		get_window()->restack (win.get_window(), false);
	}
#endif
}

void
Splash::pop_front ()
{
	if (get_window()) {
#if defined  __APPLE__ || defined PLATFORM_WINDOWS
		show ();
#else
		gdk_window_restack(get_window()->gobj(), NULL, true);
#endif
	}
}

void
Splash::hide ()
{
	Gtk::Window::hide();
}

void
Splash::on_realize ()
{
	Window::on_realize ();
	get_window()->set_decorations (Gdk::WMDecoration(0));
	layout->set_font_description (get_style()->get_font());
}

bool
Splash::on_button_release_event (GdkEventButton* ev)
{
	RefPtr<Gdk::Window> window = get_window();

	if (!window || ev->window != window->gobj()) {
		return false;
	}

	hide ();
	return true;
}

bool
Splash::expose (GdkEventExpose* ev)
{
	RefPtr<Gdk::Window> window = darea.get_window();

	/* note: height & width need to be constrained to the pixbuf size
	   in case a WM provides us with a screwy allocation
	*/

	window->draw_pixbuf (get_style()->get_bg_gc (STATE_NORMAL), pixbuf,
			     ev->area.x, ev->area.y,
			     ev->area.x, ev->area.y,
			     min ((pixbuf->get_width() - ev->area.x), ev->area.width),
			     min ((pixbuf->get_height() - ev->area.y), ev->area.height),
			     Gdk::RGB_DITHER_NONE, 0, 0);

	Glib::RefPtr<Gtk::Style> style = darea.get_style();
	Glib::RefPtr<Gdk::GC> white = style->get_white_gc();

	window->draw_layout (white, 10, pixbuf->get_height() - 30, layout);

	/* this must execute AFTER the GDK idle update mechanism
	 */

	if (expose_is_the_one) {
		idle_connection = Glib::signal_idle().connect (
				sigc::mem_fun (this, &Splash::idle_after_expose),
				GDK_PRIORITY_REDRAW+2);
	}

	return true;
}

void
Splash::boot_message (std::string msg)
{
	if (!is_visible()) {
		display ();
	}
	message (msg);
}

bool
Splash::idle_after_expose ()
{
	expose_done = true;
	return false;
}

void
Splash::display ()
{
	bool was_mapped = is_mapped ();

	if (!was_mapped) {
		expose_done = false;
		expose_is_the_one = false;
	}

	pop_front ();
	present ();

	if (!was_mapped) {
		while (!expose_done && gtk_events_pending()) {
			gtk_main_iteration ();
		}
		gdk_display_flush (gdk_display_get_default());
	}
}

void
Splash::message (const string& msg)
{
	string str ("<b>");
	str += Gtkmm2ext::markup_escape_text (msg);
	str += "</b>";

	show ();

	layout->set_markup (str);
	Glib::RefPtr<Gdk::Window> win = darea.get_window();

	if (win) {
		if (win->is_visible ()) {
			win->invalidate_rect (Gdk::Rectangle (0, darea.get_height() - 30, darea.get_width(), 30), true);
		} else {
			darea.queue_draw ();
		}
		if (expose_done) {
			ARDOUR::GUIIdle ();
		}
	}
}

bool
Splash::on_map_event (GdkEventAny* ev)
{
	expose_is_the_one = true;
	return Window::on_map_event (ev);
}