cast_dynamic<> => get_toggle_action()
[ardour.git] / gtk2_ardour / utils.cc
1 /*
2     Copyright (C) 2003 Paul Davis
3
4     This program is free software; you an 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 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <cstdlib>
25 #include <clocale>
26 #include <cstring>
27 #include <cctype>
28 #include <cmath>
29 #include <list>
30 #include <sys/stat.h>
31
32 #include <boost/algorithm/string.hpp>
33
34 #include <gtk/gtkpaned.h>
35 #include <gtkmm/combo.h>
36 #include <gtkmm/label.h>
37 #include <gtkmm/paned.h>
38 #include <gtkmm/rc.h>
39 #include <gtkmm/stock.h>
40 #include <gtkmm/window.h>
41
42 #include "pbd/basename.h"
43 #include "pbd/file_utils.h"
44 #include "pbd/stacktrace.h"
45
46 #include "ardour/audioengine.h"
47 #include "ardour/filesystem_paths.h"
48 #include "ardour/search_paths.h"
49
50 #include "gtkmm2ext/colors.h"
51 #include "gtkmm2ext/utils.h"
52
53 #include "canvas/item.h"
54
55 #include "actions.h"
56 #include "debug.h"
57 #include "public_editor.h"
58 #include "keyboard.h"
59 #include "utils.h"
60 #include "pbd/i18n.h"
61 #include "rgb_macros.h"
62 #include "gui_thread.h"
63 #include "ui_config.h"
64 #include "ardour_dialog.h"
65 #include "ardour_ui.h"
66
67 using namespace std;
68 using namespace Gtk;
69 using namespace Glib;
70 using namespace PBD;
71 using Gtkmm2ext::Keyboard;
72
73 namespace ARDOUR_UI_UTILS {
74         sigc::signal<void>  DPIReset;
75 }
76
77 #ifdef PLATFORM_WINDOWS
78 #define random() rand()
79 #endif
80
81
82 /** Add an element to a menu, settings its sensitivity.
83  * @param m Menu to add to.
84  * @param e Element to add.
85  * @param s true to make sensitive, false to make insensitive
86  */
87 void
88 ARDOUR_UI_UTILS::add_item_with_sensitivity (Menu_Helpers::MenuList& m, Menu_Helpers::MenuElem e, bool s)
89 {
90         m.push_back (e);
91         if (!s) {
92                 m.back().set_sensitive (false);
93         }
94 }
95
96
97 gint
98 ARDOUR_UI_UTILS::just_hide_it (GdkEventAny */*ev*/, Gtk::Window *win)
99 {
100         win->hide ();
101         return 0;
102 }
103
104 static bool
105 idle_notify_engine_stopped ()
106 {
107         Glib::RefPtr<ToggleAction> tact = ActionManager::get_toggle_action ("Window", "toggle-audio-midi-setup");
108
109         MessageDialog msg (
110                         _("The current operation is not possible because of an error communicating with the audio hardware."),
111                         false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE, true);
112
113         msg.add_button (_("Cancel"), Gtk::RESPONSE_CANCEL);
114
115         if (tact && !tact->get_active()) {
116                 msg.add_button (_("Configure Hardware"), Gtk::RESPONSE_OK);
117         }
118
119         if (msg.run () == Gtk::RESPONSE_OK) {
120                 tact->set_active ();
121         }
122         return false; /* do not call again */
123 }
124
125 bool
126 ARDOUR_UI_UTILS::engine_is_running ()
127 {
128         if (ARDOUR::AudioEngine::instance()->running ()) {
129                 return true;
130         }
131         Glib::signal_idle().connect (sigc::ptr_fun (&idle_notify_engine_stopped));
132         return false;
133 }
134
135
136 /* xpm2rgb copied from nixieclock, which bore the legend:
137
138     nixieclock - a nixie desktop timepiece
139     Copyright (C) 2000 Greg Ercolano, erco@3dsite.com
140
141     and was released under the GPL.
142 */
143
144 unsigned char*
145 ARDOUR_UI_UTILS::xpm2rgb (const char** xpm, uint32_t& w, uint32_t& h)
146 {
147         static long vals[256], val;
148         uint32_t t, x, y, colors, cpp;
149         unsigned char c;
150         unsigned char *savergb, *rgb;
151
152         // PARSE HEADER
153
154         if ( sscanf(xpm[0], "%u%u%u%u", &w, &h, &colors, &cpp) != 4 ) {
155                 error << string_compose (_("bad XPM header %1"), xpm[0])
156                       << endmsg;
157                 return 0;
158         }
159
160         savergb = rgb = (unsigned char*) malloc (h * w * 3);
161
162         // LOAD XPM COLORMAP LONG ENOUGH TO DO CONVERSION
163         for (t = 0; t < colors; ++t) {
164                 sscanf (xpm[t+1], "%c c #%lx", &c, &val);
165                 vals[c] = val;
166         }
167
168         // COLORMAP -> RGB CONVERSION
169         //    Get low 3 bytes from vals[]
170         //
171
172         const char *p;
173         for (y = h-1; y > 0; --y) {
174
175                 for (p = xpm[1+colors+(h-y-1)], x = 0; x < w; x++, rgb += 3) {
176                         val = vals[(int)*p++];
177                         *(rgb+2) = val & 0xff; val >>= 8;  // 2:B
178                         *(rgb+1) = val & 0xff; val >>= 8;  // 1:G
179                         *(rgb+0) = val & 0xff;             // 0:R
180                 }
181         }
182
183         return (savergb);
184 }
185
186 unsigned char*
187 ARDOUR_UI_UTILS::xpm2rgba (const char** xpm, uint32_t& w, uint32_t& h)
188 {
189         static long vals[256], val;
190         uint32_t t, x, y, colors, cpp;
191         unsigned char c;
192         unsigned char *savergb, *rgb;
193         char transparent;
194
195         // PARSE HEADER
196
197         if ( sscanf(xpm[0], "%u%u%u%u", &w, &h, &colors, &cpp) != 4 ) {
198                 error << string_compose (_("bad XPM header %1"), xpm[0])
199                       << endmsg;
200                 return 0;
201         }
202
203         savergb = rgb = (unsigned char*) malloc (h * w * 4);
204
205         // LOAD XPM COLORMAP LONG ENOUGH TO DO CONVERSION
206
207         if (strstr (xpm[1], "None")) {
208                 sscanf (xpm[1], "%c", &transparent);
209                 t = 1;
210         } else {
211                 transparent = 0;
212                 t = 0;
213         }
214
215         for (; t < colors; ++t) {
216                 sscanf (xpm[t+1], "%c c #%lx", &c, &val);
217                 vals[c] = val;
218         }
219
220         // COLORMAP -> RGB CONVERSION
221         //    Get low 3 bytes from vals[]
222         //
223
224         const char *p;
225         for (y = h-1; y > 0; --y) {
226
227                 char alpha;
228
229                 for (p = xpm[1+colors+(h-y-1)], x = 0; x < w; x++, rgb += 4) {
230
231                         if (transparent && (*p++ == transparent)) {
232                                 alpha = 0;
233                                 val = 0;
234                         } else {
235                                 alpha = 255;
236                                 val = vals[(int)*p];
237                         }
238
239                         *(rgb+3) = alpha;                  // 3: alpha
240                         *(rgb+2) = val & 0xff; val >>= 8;  // 2:B
241                         *(rgb+1) = val & 0xff; val >>= 8;  // 1:G
242                         *(rgb+0) = val & 0xff;             // 0:R
243                 }
244         }
245
246         return (savergb);
247 }
248
249 /** Returns a Pango::FontDescription given a string describing the font.
250  *
251  * If the returned FontDescription does not specify a family, then
252  * the family is set to "Sans". This mirrors GTK's behaviour in
253  * gtkstyle.c.
254  *
255  * Some environments will force Pango to specify the family
256  * even if it was not specified in the string describing the font.
257  * Such environments should be left unaffected by this function,
258  * since the font family will be left alone.
259  *
260  * There may be other similar font specification enforcement
261  * that we might add here later.
262  */
263 Pango::FontDescription
264 ARDOUR_UI_UTILS::sanitized_font (std::string const& name)
265 {
266         Pango::FontDescription fd (name);
267
268         if (fd.get_family().empty()) {
269                 fd.set_family ("Sans");
270         }
271
272         return fd;
273 }
274
275 Pango::FontDescription
276 ARDOUR_UI_UTILS::get_font_for_style (string widgetname)
277 {
278         Gtk::Window window (WINDOW_TOPLEVEL);
279         Gtk::Label foobar;
280         Glib::RefPtr<Gtk::Style> style;
281
282         window.add (foobar);
283         foobar.set_name (widgetname);
284         foobar.ensure_style();
285
286         style = foobar.get_style ();
287
288         Glib::RefPtr<const Pango::Layout> layout = foobar.get_layout();
289
290         PangoFontDescription *pfd = const_cast<PangoFontDescription *> (pango_layout_get_font_description(const_cast<PangoLayout *>(layout->gobj())));
291
292         if (!pfd) {
293
294                 /* layout inherited its font description from a PangoContext */
295
296                 PangoContext* ctxt = (PangoContext*) pango_layout_get_context (const_cast<PangoLayout*>(layout->gobj()));
297                 pfd =  pango_context_get_font_description (ctxt);
298                 return Pango::FontDescription (pfd); /* make a copy */
299         }
300
301         return Pango::FontDescription (pfd); /* make a copy */
302 }
303
304 Gdk::Color
305 ARDOUR_UI_UTILS::gdk_color_from_rgb (uint32_t rgb)
306 {
307         Gdk::Color c;
308         set_color_from_rgb (c, rgb);
309         return c;
310 }
311
312 Gdk::Color
313 ARDOUR_UI_UTILS::gdk_color_from_rgba (uint32_t rgba)
314 {
315         Gdk::Color c;
316         set_color_from_rgb (c, rgba >> 8);
317         return c;
318 }
319
320 void
321 ARDOUR_UI_UTILS::set_color_from_rgb (Gdk::Color& c, uint32_t rgb)
322 {
323         /* Gdk::Color color ranges are 16 bit, so scale from 8 bit by
324            multiplying by 256.
325         */
326         c.set_rgb ((rgb >> 16)*256, ((rgb & 0xff00) >> 8)*256, (rgb & 0xff)*256);
327 }
328
329 void
330 ARDOUR_UI_UTILS::set_color_from_rgba (Gdk::Color& c, uint32_t rgba)
331 {
332         /* Gdk::Color color ranges are 16 bit, so scale from 8 bit by
333            multiplying by 256.
334         */
335         c.set_rgb ((rgba >> 24)*256, ((rgba & 0xff0000) >> 16)*256, ((rgba & 0xff00) >> 8)*256);
336 }
337
338 uint32_t
339 ARDOUR_UI_UTILS::gdk_color_to_rgba (Gdk::Color const& c)
340 {
341         /* since alpha value is not available from a Gdk::Color, it is
342            hardcoded as 0xff (aka 255 or 1.0)
343         */
344
345         const uint32_t r = c.get_red_p () * 255.0;
346         const uint32_t g = c.get_green_p () * 255.0;
347         const uint32_t b = c.get_blue_p () * 255.0;
348         const uint32_t a = 0xff;
349
350         return RGBA_TO_UINT (r,g,b,a);
351 }
352
353 bool
354 ARDOUR_UI_UTILS::relay_key_press (GdkEventKey* ev, Gtk::Window* win)
355 {
356         return ARDOUR_UI::instance()->key_event_handler (ev, win);
357 }
358
359 bool
360 ARDOUR_UI_UTILS::emulate_key_event (unsigned int keyval)
361 {
362         GdkDisplay  *display = gtk_widget_get_display (GTK_WIDGET(ARDOUR_UI::instance()->main_window().gobj()));
363         GdkKeymap   *keymap  = gdk_keymap_get_for_display (display);
364         GdkKeymapKey *keymapkey = NULL;
365         gint n_keys;
366
367         if (!gdk_keymap_get_entries_for_keyval(keymap, keyval, &keymapkey, &n_keys)) return false;
368         if (n_keys !=1) { g_free(keymapkey); return false;}
369
370         Gtk::Window& main_window (ARDOUR_UI::instance()->main_window());
371
372         GdkEventKey ev;
373         ev.type = GDK_KEY_PRESS;
374         ev.window = main_window.get_window()->gobj();
375         ev.send_event = FALSE;
376         ev.time = 0;
377         ev.state = 0;
378         ev.keyval = keyval;
379         ev.length = 0;
380         ev.string = const_cast<gchar*> ("");
381         ev.hardware_keycode = keymapkey[0].keycode;
382         ev.group = keymapkey[0].group;
383         g_free(keymapkey);
384
385         relay_key_press (&ev, &main_window);
386         ev.type = GDK_KEY_RELEASE;
387         return relay_key_press(&ev, &main_window);
388 }
389
390 Glib::RefPtr<Gdk::Pixbuf>
391 ARDOUR_UI_UTILS::get_xpm (std::string name)
392 {
393         if (!xpm_map[name]) {
394
395                 Searchpath spath(ARDOUR::ardour_data_search_path());
396
397                 spath.add_subdirectory_to_paths("pixmaps");
398
399                 std::string data_file_path;
400
401                 if(!find_file (spath, name, data_file_path)) {
402                         fatal << string_compose (_("cannot find XPM file for %1"), name) << endmsg;
403                 }
404
405                 try {
406                         xpm_map[name] =  Gdk::Pixbuf::create_from_file (data_file_path);
407                 } catch(const Glib::Error& e)   {
408                         warning << "Caught Glib::Error: " << e.what() << endmsg;
409                 }
410         }
411
412         return xpm_map[name];
413 }
414
415 void
416 ARDOUR_UI_UTILS::get_color_themes (map<std::string,std::string>& themes)
417 {
418         Searchpath spath(ARDOUR::theme_search_path());
419
420         for (vector<string>::iterator s = spath.begin(); s != spath.end(); ++s) {
421
422                 vector<string> entries;
423
424                 find_files_matching_pattern (entries, *s, string ("*") + UIConfiguration::color_file_suffix);
425
426                 for (vector<string>::iterator e = entries.begin(); e != entries.end(); ++e) {
427
428                         XMLTree tree;
429
430                         tree.read ((*e).c_str());
431                         XMLNode* root = tree.root();
432
433                         if (!root || root->name() != X_("Ardour")) {
434                                 continue;
435                         }
436
437                         XMLProperty const* prop = root->property (X_("theme-name"));
438
439                         if (!prop) {
440                                 continue;
441                         }
442
443                         std::string color_name = basename_nosuffix(*e);
444                         size_t sep = color_name.find_first_of("-");
445                         if (sep != string::npos) {
446                                 color_name = color_name.substr (0, sep);
447                         }
448                         themes.insert (make_pair (prop->value(), color_name));
449                 }
450         }
451 }
452
453 vector<string>
454 ARDOUR_UI_UTILS::get_icon_sets ()
455 {
456         Searchpath spath(ARDOUR::ardour_data_search_path());
457         spath.add_subdirectory_to_paths ("icons");
458         vector<string> r;
459
460         r.push_back (_("default"));
461
462         for (vector<string>::iterator s = spath.begin(); s != spath.end(); ++s) {
463
464                 vector<string> entries;
465
466                 get_paths (entries, *s, false, false);
467
468                 for (vector<string>::iterator e = entries.begin(); e != entries.end(); ++e) {
469                         if (Glib::file_test (*e, Glib::FILE_TEST_IS_DIR)) {
470                                 r.push_back (Glib::filename_to_utf8 (Glib::path_get_basename(*e)));
471                         }
472                 }
473         }
474
475         return r;
476 }
477
478 std::string
479 ARDOUR_UI_UTILS::get_icon_path (const char* cname, string icon_set, bool is_image)
480 {
481         std::string data_file_path;
482         string name = cname;
483
484         if (is_image) {
485                 name += X_(".png");
486         }
487
488         Searchpath spath(ARDOUR::ardour_data_search_path());
489
490         if (!icon_set.empty() && icon_set != _("default")) {
491
492                 /* add "icons/icon_set" but .. not allowed to add both of these at once */
493                 spath.add_subdirectory_to_paths ("icons");
494                 spath.add_subdirectory_to_paths (icon_set);
495
496                 find_file (spath, name, data_file_path);
497         } else {
498                 spath.add_subdirectory_to_paths ("icons");
499                 find_file (spath, name, data_file_path);
500         }
501
502         if (data_file_path.empty()) {
503                 Searchpath rc (ARDOUR::ardour_data_search_path());
504                 rc.add_subdirectory_to_paths ("resources");
505                 find_file (rc, name, data_file_path);
506         }
507
508         if (is_image && data_file_path.empty()) {
509
510                 if (!icon_set.empty() && icon_set != _("default")) {
511                         warning << string_compose (_("icon \"%1\" not found for icon set \"%2\", fallback to default"), cname, icon_set) << endmsg;
512                 }
513
514                 Searchpath def (ARDOUR::ardour_data_search_path());
515                 def.add_subdirectory_to_paths ("icons");
516
517                 if (!find_file (def, name, data_file_path)) {
518                         fatal << string_compose (_("cannot find icon image for %1 using %2"), name, spath.to_string()) << endmsg;
519                         abort(); /*NOTREACHED*/
520                 }
521         }
522
523         return data_file_path;
524 }
525
526 Glib::RefPtr<Gdk::Pixbuf>
527 ARDOUR_UI_UTILS::get_icon (const char* cname, string icon_set)
528 {
529         Glib::RefPtr<Gdk::Pixbuf> img;
530         try {
531                 img = Gdk::Pixbuf::create_from_file (get_icon_path (cname, icon_set));
532         } catch (const Gdk::PixbufError &e) {
533                 cerr << "Caught PixbufError: " << e.what() << endl;
534         } catch (...) {
535                 error << string_compose (_("Caught exception while loading icon named %1"), cname) << endmsg;
536         }
537
538         return img;
539 }
540
541 namespace ARDOUR_UI_UTILS {
542 Glib::RefPtr<Gdk::Pixbuf>
543 get_icon (const char* cname)
544 {
545         Glib::RefPtr<Gdk::Pixbuf> img;
546         try {
547                 img = Gdk::Pixbuf::create_from_file (get_icon_path (cname));
548         } catch (const Gdk::PixbufError &e) {
549                 cerr << "Caught PixbufError: " << e.what() << endl;
550         } catch (...) {
551                 error << string_compose (_("Caught exception while loading icon named %1"), cname) << endmsg;
552         }
553
554         return img;
555 }
556 }
557
558 string
559 ARDOUR_UI_UTILS::longest (vector<string>& strings)
560 {
561         if (strings.empty()) {
562                 return string ("");
563         }
564
565         vector<string>::iterator longest = strings.begin();
566         string::size_type longest_length = (*longest).length();
567
568         vector<string>::iterator i = longest;
569         ++i;
570
571         while (i != strings.end()) {
572
573                 string::size_type len = (*i).length();
574
575                 if (len > longest_length) {
576                         longest = i;
577                         longest_length = len;
578                 }
579
580                 ++i;
581         }
582
583         return *longest;
584 }
585
586 bool
587 ARDOUR_UI_UTILS::key_is_legal_for_numeric_entry (guint keyval)
588 {
589         /* we assume that this does not change over the life of the process
590          */
591
592         static int comma_decimal = -1;
593
594         switch (keyval) {
595         case GDK_period:
596         case GDK_comma:
597                 if (comma_decimal < 0) {
598                         std::lconv* lc = std::localeconv();
599                         if (strchr (lc->decimal_point, ',') != 0) {
600                                 comma_decimal = 1;
601                         } else {
602                                 comma_decimal = 0;
603                         }
604                 }
605                 break;
606         default:
607                 break;
608         }
609
610         switch (keyval) {
611         case GDK_decimalpoint:
612         case GDK_KP_Separator:
613                 return true;
614
615         case GDK_period:
616                 if (comma_decimal) {
617                         return false;
618                 } else {
619                         return true;
620                 }
621                 break;
622         case GDK_comma:
623                 if (comma_decimal) {
624                         return true;
625                 } else {
626                         return false;
627                 }
628                 break;
629         case GDK_minus:
630         case GDK_plus:
631         case GDK_0:
632         case GDK_1:
633         case GDK_2:
634         case GDK_3:
635         case GDK_4:
636         case GDK_5:
637         case GDK_6:
638         case GDK_7:
639         case GDK_8:
640         case GDK_9:
641         case GDK_KP_Add:
642         case GDK_KP_Subtract:
643         case GDK_KP_Decimal:
644         case GDK_KP_0:
645         case GDK_KP_1:
646         case GDK_KP_2:
647         case GDK_KP_3:
648         case GDK_KP_4:
649         case GDK_KP_5:
650         case GDK_KP_6:
651         case GDK_KP_7:
652         case GDK_KP_8:
653         case GDK_KP_9:
654         case GDK_Return:
655         case GDK_BackSpace:
656         case GDK_Delete:
657         case GDK_KP_Enter:
658         case GDK_Home:
659         case GDK_End:
660         case GDK_Left:
661         case GDK_Right:
662                 return true;
663
664         default:
665                 break;
666         }
667
668         return false;
669 }
670
671 void
672 ARDOUR_UI_UTILS::resize_window_to_proportion_of_monitor (Gtk::Window* window, int max_width, int max_height)
673 {
674         Glib::RefPtr<Gdk::Screen> screen = window->get_screen ();
675         Gdk::Rectangle monitor_rect;
676         screen->get_monitor_geometry (0, monitor_rect);
677
678         int const w = std::min (int (monitor_rect.get_width() * 0.8), max_width);
679         int const h = std::min (int (monitor_rect.get_height() * 0.8), max_height);
680
681         window->resize (w, h);
682 }
683
684
685 /** Replace _ with __ in a string; for use with menu item text to make underscores displayed correctly */
686 string
687 ARDOUR_UI_UTILS::escape_underscores (string const & s)
688 {
689         string o;
690         string::size_type const N = s.length ();
691
692         for (string::size_type i = 0; i < N; ++i) {
693                 if (s[i] == '_') {
694                         o += "__";
695                 } else {
696                         o += s[i];
697                 }
698         }
699
700         return o;
701 }
702
703 Gdk::Color
704 ARDOUR_UI_UTILS::unique_random_color (list<Gdk::Color>& used_colors)
705 {
706         Gdk::Color newcolor;
707
708         while (1) {
709
710                 double h, s, v;
711
712                 h = fmod (random(), 360.0);
713                 s = (random() % 65535) / 65535.0;
714                 v = (random() % 65535) / 65535.0;
715
716                 s = min (0.5, s); /* not too saturated */
717                 v = max (0.9, v);  /* not too bright */
718                 newcolor.set_hsv (h, s, v);
719
720                 if (used_colors.size() == 0) {
721                         used_colors.push_back (newcolor);
722                         return newcolor;
723                 }
724
725                 for (list<Gdk::Color>::iterator i = used_colors.begin(); i != used_colors.end(); ++i) {
726                   Gdk::Color c = *i;
727                         float rdelta, bdelta, gdelta;
728
729                         rdelta = newcolor.get_red() - c.get_red();
730                         bdelta = newcolor.get_blue() - c.get_blue();
731                         gdelta = newcolor.get_green() - c.get_green();
732
733                         if (sqrt (rdelta*rdelta + bdelta*bdelta + gdelta*gdelta) > 25.0) {
734                                 /* different enough */
735                                 used_colors.push_back (newcolor);
736                                 return newcolor;
737                         }
738                 }
739
740                 /* XXX need throttle here to make sure we don't spin for ever */
741         }
742 }
743
744 string
745 ARDOUR_UI_UTILS::rate_as_string (float r)
746 {
747         char buf[32];
748         if (fmod (r, 1000.0f)) {
749                 snprintf (buf, sizeof (buf), "%.1f kHz", r/1000.0);
750         } else {
751                 snprintf (buf, sizeof (buf), "%.0f kHz", r/1000.0);
752         }
753         return buf;
754 }
755
756 bool
757 ARDOUR_UI_UTILS::windows_overlap (Gtk::Window *a, Gtk::Window *b)
758 {
759
760         if (!a || !b) {
761                 return false;
762         }
763         if (a->get_screen() == b->get_screen()) {
764                 gint ex, ey, ew, eh;
765                 gint mx, my, mw, mh;
766
767                 a->get_position (ex, ey);
768                 a->get_size (ew, eh);
769                 b->get_position (mx, my);
770                 b->get_size (mw, mh);
771
772                 GdkRectangle e;
773                 GdkRectangle m;
774                 GdkRectangle r;
775
776                 e.x = ex;
777                 e.y = ey;
778                 e.width = ew;
779                 e.height = eh;
780
781                 m.x = mx;
782                 m.y = my;
783                 m.width = mw;
784                 m.height = mh;
785
786                 if (gdk_rectangle_intersect (&e, &m, &r)) {
787                         return true;
788                 }
789         }
790         return false;
791 }
792
793 bool
794 ARDOUR_UI_UTILS::overwrite_file_dialog (Gtk::Window& parent, string title, string text)
795 {
796         ArdourDialog dialog (parent, title, true);
797         Label label (text);
798
799         dialog.get_vbox()->pack_start (label, true, true);
800         dialog.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
801         dialog.add_button (_("Overwrite"), Gtk::RESPONSE_ACCEPT);
802         dialog.show_all ();
803
804         switch (dialog.run()) {
805         case RESPONSE_ACCEPT:
806                 return true;
807         case RESPONSE_CANCEL:
808         default:
809                 return false;
810         }
811 }
812
813 bool
814 ARDOUR_UI_UTILS::running_from_source_tree ()
815 {
816         gchar const *x = g_getenv ("ARDOUR_THEMES_PATH");
817         return x && (string (x).find ("gtk2_ardour") != string::npos);
818 }