Vkeybd: use ArdourWidgets for all GUI elements
[ardour.git] / gtk2_ardour / splash.cc
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 #include "pbd/stacktrace.h"
28
29 #include "ardour/ardour.h"
30 #include "ardour/filesystem_paths.h"
31
32 #include "gtkmm2ext/utils.h"
33
34 #ifdef check
35 #undef check
36 #endif
37
38 #include "gui_thread.h"
39 #include "splash.h"
40
41 #include "pbd/i18n.h"
42
43 using namespace Gtk;
44 using namespace Glib;
45 using namespace PBD;
46 using namespace std;
47 using namespace ARDOUR;
48
49 Splash* Splash::the_splash = 0;
50
51 Splash*
52 Splash::instance()
53 {
54         if (!the_splash) {
55                 the_splash = new Splash;
56         }
57         return the_splash;
58 }
59
60 bool
61 Splash::exists ()
62 {
63         return the_splash;
64 }
65
66 void
67 Splash::drop ()
68 {
69         delete the_splash;
70         the_splash = 0;
71 }
72
73 Splash::Splash ()
74 {
75         assert (the_splash == 0);
76
77         std::string splash_file;
78
79         Searchpath rc (ARDOUR::ardour_data_search_path());
80         rc.add_subdirectory_to_paths ("resources");
81
82         if (!find_file (rc, PROGRAM_NAME "-splash.png", splash_file)) {
83                 cerr << "Cannot find splash screen image file\n";
84                 throw failed_constructor();
85         }
86
87         try {
88                 pixbuf = Gdk::Pixbuf::create_from_file (splash_file);
89         }
90
91         catch (...) {
92                 cerr << "Cannot construct splash screen image\n";
93                 throw failed_constructor();
94         }
95
96         darea.set_size_request (pixbuf->get_width(), pixbuf->get_height());
97         pop_front ();
98         set_position (WIN_POS_CENTER);
99         darea.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
100         darea.set_double_buffered (false);
101
102         layout = create_pango_layout ("");
103         string str = "<b>";
104         string i18n = string_compose (_("%1 loading ..."), PROGRAM_NAME);
105         str += i18n;
106         str += "</b>";
107
108         layout->set_markup (str);
109
110         darea.show ();
111         darea.signal_expose_event().connect (sigc::mem_fun (*this, &Splash::expose));
112
113         add (darea);
114
115         set_default_size (pixbuf->get_width(), pixbuf->get_height());
116         set_resizable (false);
117         set_type_hint(Gdk::WINDOW_TYPE_HINT_SPLASHSCREEN);
118         the_splash = this;
119
120         expose_done = false;
121         expose_is_the_one = false;
122
123         ARDOUR::BootMessage.connect (msg_connection, invalidator (*this), boost::bind (&Splash::boot_message, this, _1), gui_context());
124 }
125
126 Splash::~Splash ()
127 {
128         idle_connection.disconnect ();
129         expose_done = true;
130         hide ();
131         the_splash = 0;
132 }
133
134 void
135 Splash::pop_back_for (Gtk::Window& win)
136 {
137 #if defined  __APPLE__ || defined PLATFORM_WINDOWS
138         /* April 2013: window layering on OS X is a bit different to X Window. at present,
139          * the "restack()" functionality in GDK will only operate on windows in the same
140          * "level" (e.g. two normal top level windows, or two utility windows) and will not
141          * work across them. The splashscreen is on its own "StatusWindowLevel" so restacking
142          * is not going to work.
143          *
144          * So for OS X, we just hide ourselves.
145          *
146          * Oct 2014: The Windows situation is similar, although it should be possible
147          * to play tricks with gdk's set_type_hint() or directly hack things using
148          * SetWindowLong() and UpdateLayeredWindow()
149          */
150         (void) win;
151         hide();
152 #else
153         set_keep_above (false);
154         if (is_mapped()) {
155                 get_window()->restack (win.get_window(), false);
156         }
157 #endif
158 }
159
160 void
161 Splash::pop_front ()
162 {
163         if (get_window()) {
164 #if defined  __APPLE__ || defined PLATFORM_WINDOWS
165                 show ();
166 #else
167                 gdk_window_restack(get_window()->gobj(), NULL, true);
168 #endif
169         }
170 }
171
172 void
173 Splash::hide ()
174 {
175         Gtk::Window::hide();
176 }
177
178 void
179 Splash::on_realize ()
180 {
181         Window::on_realize ();
182         get_window()->set_decorations (Gdk::WMDecoration(0));
183         layout->set_font_description (get_style()->get_font());
184 }
185
186 bool
187 Splash::on_button_release_event (GdkEventButton* ev)
188 {
189         RefPtr<Gdk::Window> window = get_window();
190
191         if (!window || ev->window != window->gobj()) {
192                 return false;
193         }
194
195         hide ();
196         return true;
197 }
198
199 bool
200 Splash::expose (GdkEventExpose* ev)
201 {
202         RefPtr<Gdk::Window> window = darea.get_window();
203
204         /* note: height & width need to be constrained to the pixbuf size
205            in case a WM provides us with a screwy allocation
206         */
207
208         window->draw_pixbuf (get_style()->get_bg_gc (STATE_NORMAL), pixbuf,
209                              ev->area.x, ev->area.y,
210                              ev->area.x, ev->area.y,
211                              min ((pixbuf->get_width() - ev->area.x), ev->area.width),
212                              min ((pixbuf->get_height() - ev->area.y), ev->area.height),
213                              Gdk::RGB_DITHER_NONE, 0, 0);
214
215         Glib::RefPtr<Gtk::Style> style = darea.get_style();
216         Glib::RefPtr<Gdk::GC> white = style->get_white_gc();
217
218         window->draw_layout (white, 10, pixbuf->get_height() - 30, layout);
219
220         /* this must execute AFTER the GDK idle update mechanism
221          */
222
223         if (expose_is_the_one) {
224                 idle_connection = Glib::signal_idle().connect (
225                                 sigc::mem_fun (this, &Splash::idle_after_expose),
226                                 GDK_PRIORITY_REDRAW+2);
227         }
228
229         return true;
230 }
231
232 void
233 Splash::boot_message (std::string msg)
234 {
235         if (!is_visible()) {
236                 display ();
237         }
238         message (msg);
239 }
240
241 bool
242 Splash::idle_after_expose ()
243 {
244         expose_done = true;
245         return false;
246 }
247
248 void
249 Splash::display ()
250 {
251         bool was_mapped = is_mapped ();
252
253         if (!was_mapped) {
254                 expose_done = false;
255                 expose_is_the_one = false;
256         }
257
258         pop_front ();
259         present ();
260
261         if (!was_mapped) {
262                 while (!expose_done && gtk_events_pending()) {
263                         gtk_main_iteration ();
264                 }
265                 gdk_display_flush (gdk_display_get_default());
266         }
267 }
268
269 void
270 Splash::message (const string& msg)
271 {
272         string str ("<b>");
273         str += Gtkmm2ext::markup_escape_text (msg);
274         str += "</b>";
275
276         show ();
277
278         layout->set_markup (str);
279         Glib::RefPtr<Gdk::Window> win = darea.get_window();
280
281         if (win) {
282                 if (win->is_visible ()) {
283                         win->invalidate_rect (Gdk::Rectangle (0, darea.get_height() - 30, darea.get_width(), 30), true);
284                 } else {
285                         darea.queue_draw ();
286                 }
287                 if (expose_done) {
288                         ARDOUR::GUIIdle ();
289                 }
290         }
291 }
292
293 bool
294 Splash::on_map_event (GdkEventAny* ev)
295 {
296         expose_is_the_one = true;
297         return Window::on_map_event (ev);
298 }