2 * Copyright (C) 2005-2006 Taybin Rutkin <taybin@taybin.com>
3 * Copyright (C) 2005-2009 Nick Mainsbridge <mainsbridge@gmail.com>
4 * Copyright (C) 2005-2018 Paul Davis <paul@linuxaudiosystems.com>
5 * Copyright (C) 2005 Karsten Wiese <fzuuzf@googlemail.com>
6 * Copyright (C) 2006-2007 Doug McLain <doug@nostar.net>
7 * Copyright (C) 2006-2009 Sampo Savolainen <v2@iki.fi>
8 * Copyright (C) 2007-2015 David Robillard <d@drobilla.net>
9 * Copyright (C) 2007-2015 Tim Mayberry <mojofunk@gmail.com>
10 * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
11 * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
12 * Copyright (C) 2013 John Emmas <john@creativepost.co.uk>
13 * Copyright (C) 2015 André Nusser <andre.nusser@googlemail.com>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include "gtk2ardour-config.h"
42 #include <boost/algorithm/string.hpp>
44 #include <gtk/gtkpaned.h>
45 #include <gtkmm/combo.h>
46 #include <gtkmm/label.h>
47 #include <gtkmm/paned.h>
49 #include <gtkmm/stock.h>
50 #include <gtkmm/window.h>
52 #include "pbd/basename.h"
53 #include "pbd/file_utils.h"
54 #include "pbd/stacktrace.h"
56 #include "ardour/audioengine.h"
57 #include "ardour/filesystem_paths.h"
58 #include "ardour/search_paths.h"
60 #include "gtkmm2ext/colors.h"
61 #include "gtkmm2ext/utils.h"
63 #include "canvas/item.h"
66 #include "context_menu_helper.h"
68 #include "public_editor.h"
72 #include "rgb_macros.h"
73 #include "gui_thread.h"
74 #include "ui_config.h"
75 #include "ardour_dialog.h"
76 #include "ardour_ui.h"
82 using Gtkmm2ext::Keyboard;
84 namespace ARDOUR_UI_UTILS {
85 sigc::signal<void> DPIReset;
88 #ifdef PLATFORM_WINDOWS
89 #define random() rand()
93 /** Add an element to a menu, settings its sensitivity.
94 * @param m Menu to add to.
95 * @param e Element to add.
96 * @param s true to make sensitive, false to make insensitive
99 ARDOUR_UI_UTILS::add_item_with_sensitivity (Menu_Helpers::MenuList& m, Menu_Helpers::MenuElem e, bool s)
103 m.back().set_sensitive (false);
109 ARDOUR_UI_UTILS::just_hide_it (GdkEventAny */*ev*/, Gtk::Window *win)
116 idle_notify_engine_stopped ()
118 Glib::RefPtr<ToggleAction> tact = ActionManager::get_toggle_action ("Window", "toggle-audio-midi-setup");
121 _("The current operation is not possible because of an error communicating with the audio hardware."),
122 false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_NONE, true);
124 msg.add_button (_("Cancel"), Gtk::RESPONSE_CANCEL);
126 if (tact && !tact->get_active()) {
127 msg.add_button (_("Configure Hardware"), Gtk::RESPONSE_OK);
130 if (msg.run () == Gtk::RESPONSE_OK) {
133 return false; /* do not call again */
137 ARDOUR_UI_UTILS::engine_is_running ()
139 if (ARDOUR::AudioEngine::instance()->running ()) {
142 Glib::signal_idle().connect (sigc::ptr_fun (&idle_notify_engine_stopped));
147 /* xpm2rgb copied from nixieclock, which bore the legend:
149 nixieclock - a nixie desktop timepiece
150 Copyright (C) 2000 Greg Ercolano, erco@3dsite.com
152 and was released under the GPL.
156 ARDOUR_UI_UTILS::xpm2rgb (const char** xpm, uint32_t& w, uint32_t& h)
158 static long vals[256], val;
159 uint32_t t, x, y, colors, cpp;
161 unsigned char *savergb, *rgb;
165 if ( sscanf(xpm[0], "%u%u%u%u", &w, &h, &colors, &cpp) != 4 ) {
166 error << string_compose (_("bad XPM header %1"), xpm[0])
171 savergb = rgb = (unsigned char*) malloc (h * w * 3);
173 // LOAD XPM COLORMAP LONG ENOUGH TO DO CONVERSION
174 for (t = 0; t < colors; ++t) {
175 sscanf (xpm[t+1], "%c c #%lx", &c, &val);
179 // COLORMAP -> RGB CONVERSION
180 // Get low 3 bytes from vals[]
184 for (y = h-1; y > 0; --y) {
186 for (p = xpm[1+colors+(h-y-1)], x = 0; x < w; x++, rgb += 3) {
187 val = vals[(int)*p++];
188 *(rgb+2) = val & 0xff; val >>= 8; // 2:B
189 *(rgb+1) = val & 0xff; val >>= 8; // 1:G
190 *(rgb+0) = val & 0xff; // 0:R
198 ARDOUR_UI_UTILS::xpm2rgba (const char** xpm, uint32_t& w, uint32_t& h)
200 static long vals[256], val;
201 uint32_t t, x, y, colors, cpp;
203 unsigned char *savergb, *rgb;
208 if ( sscanf(xpm[0], "%u%u%u%u", &w, &h, &colors, &cpp) != 4 ) {
209 error << string_compose (_("bad XPM header %1"), xpm[0])
214 savergb = rgb = (unsigned char*) malloc (h * w * 4);
216 // LOAD XPM COLORMAP LONG ENOUGH TO DO CONVERSION
218 if (strstr (xpm[1], "None")) {
219 sscanf (xpm[1], "%c", &transparent);
226 for (; t < colors; ++t) {
227 sscanf (xpm[t+1], "%c c #%lx", &c, &val);
231 // COLORMAP -> RGB CONVERSION
232 // Get low 3 bytes from vals[]
236 for (y = h-1; y > 0; --y) {
240 for (p = xpm[1+colors+(h-y-1)], x = 0; x < w; x++, rgb += 4) {
242 if (transparent && (*p++ == transparent)) {
250 *(rgb+3) = alpha; // 3: alpha
251 *(rgb+2) = val & 0xff; val >>= 8; // 2:B
252 *(rgb+1) = val & 0xff; val >>= 8; // 1:G
253 *(rgb+0) = val & 0xff; // 0:R
260 /** Returns a Pango::FontDescription given a string describing the font.
262 * If the returned FontDescription does not specify a family, then
263 * the family is set to "Sans". This mirrors GTK's behaviour in
266 * Some environments will force Pango to specify the family
267 * even if it was not specified in the string describing the font.
268 * Such environments should be left unaffected by this function,
269 * since the font family will be left alone.
271 * There may be other similar font specification enforcement
272 * that we might add here later.
274 Pango::FontDescription
275 ARDOUR_UI_UTILS::sanitized_font (std::string const& name)
277 Pango::FontDescription fd (name);
279 if (fd.get_family().empty()) {
280 fd.set_family ("Sans");
286 Pango::FontDescription
287 ARDOUR_UI_UTILS::get_font_for_style (string widgetname)
289 Gtk::Window window (WINDOW_TOPLEVEL);
291 Glib::RefPtr<Gtk::Style> style;
294 foobar.set_name (widgetname);
295 foobar.ensure_style();
297 style = foobar.get_style ();
299 Glib::RefPtr<const Pango::Layout> layout = foobar.get_layout();
301 PangoFontDescription *pfd = const_cast<PangoFontDescription *> (pango_layout_get_font_description(const_cast<PangoLayout *>(layout->gobj())));
305 /* layout inherited its font description from a PangoContext */
307 PangoContext* ctxt = (PangoContext*) pango_layout_get_context (const_cast<PangoLayout*>(layout->gobj()));
308 pfd = pango_context_get_font_description (ctxt);
309 return Pango::FontDescription (pfd); /* make a copy */
312 return Pango::FontDescription (pfd); /* make a copy */
316 ARDOUR_UI_UTILS::gdk_color_from_rgb (uint32_t rgb)
319 set_color_from_rgb (c, rgb);
324 ARDOUR_UI_UTILS::gdk_color_from_rgba (uint32_t rgba)
327 set_color_from_rgb (c, rgba >> 8);
332 ARDOUR_UI_UTILS::set_color_from_rgb (Gdk::Color& c, uint32_t rgb)
334 /* Gdk::Color color ranges are 16 bit, so scale from 8 bit by
337 c.set_rgb ((rgb >> 16)*256, ((rgb & 0xff00) >> 8)*256, (rgb & 0xff)*256);
341 ARDOUR_UI_UTILS::set_color_from_rgba (Gdk::Color& c, uint32_t rgba)
343 /* Gdk::Color color ranges are 16 bit, so scale from 8 bit by
346 c.set_rgb ((rgba >> 24)*256, ((rgba & 0xff0000) >> 16)*256, ((rgba & 0xff00) >> 8)*256);
350 ARDOUR_UI_UTILS::gdk_color_to_rgba (Gdk::Color const& c)
352 /* since alpha value is not available from a Gdk::Color, it is
353 hardcoded as 0xff (aka 255 or 1.0)
356 const uint32_t r = c.get_red_p () * 255.0;
357 const uint32_t g = c.get_green_p () * 255.0;
358 const uint32_t b = c.get_blue_p () * 255.0;
359 const uint32_t a = 0xff;
361 return RGBA_TO_UINT (r,g,b,a);
365 ARDOUR_UI_UTILS::relay_key_press (GdkEventKey* ev, Gtk::Window* win)
367 return ARDOUR_UI::instance()->key_event_handler (ev, win);
371 ARDOUR_UI_UTILS::emulate_key_event (unsigned int keyval)
373 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET(ARDOUR_UI::instance()->main_window().gobj()));
374 GdkKeymap *keymap = gdk_keymap_get_for_display (display);
375 GdkKeymapKey *keymapkey = NULL;
378 if (!gdk_keymap_get_entries_for_keyval(keymap, keyval, &keymapkey, &n_keys)) return false;
379 if (n_keys !=1) { g_free(keymapkey); return false;}
381 Gtk::Window& main_window (ARDOUR_UI::instance()->main_window());
384 ev.type = GDK_KEY_PRESS;
385 ev.window = main_window.get_window()->gobj();
386 ev.send_event = FALSE;
391 ev.string = const_cast<gchar*> ("");
392 ev.hardware_keycode = keymapkey[0].keycode;
393 ev.group = keymapkey[0].group;
396 relay_key_press (&ev, &main_window);
397 ev.type = GDK_KEY_RELEASE;
398 return relay_key_press(&ev, &main_window);
401 Glib::RefPtr<Gdk::Pixbuf>
402 ARDOUR_UI_UTILS::get_xpm (std::string name)
404 if (!xpm_map[name]) {
406 Searchpath spath(ARDOUR::ardour_data_search_path());
408 spath.add_subdirectory_to_paths("pixmaps");
410 std::string data_file_path;
412 if(!find_file (spath, name, data_file_path)) {
413 fatal << string_compose (_("cannot find XPM file for %1"), name) << endmsg;
417 xpm_map[name] = Gdk::Pixbuf::create_from_file (data_file_path);
418 } catch (const Glib::Error& e) {
419 warning << "Caught Glib::Error: " << e.what() << endmsg;
423 return xpm_map[name];
427 ARDOUR_UI_UTILS::get_color_themes (map<std::string,std::string>& themes)
429 Searchpath spath(ARDOUR::theme_search_path());
431 for (vector<string>::iterator s = spath.begin(); s != spath.end(); ++s) {
433 vector<string> entries;
435 find_files_matching_pattern (entries, *s, string ("*") + UIConfiguration::color_file_suffix);
437 for (vector<string>::iterator e = entries.begin(); e != entries.end(); ++e) {
441 tree.read ((*e).c_str());
442 XMLNode* root = tree.root();
444 if (!root || root->name() != X_("Ardour")) {
448 XMLProperty const* prop = root->property (X_("theme-name"));
454 std::string color_name = basename_nosuffix(*e);
455 size_t sep = color_name.find_first_of("-");
456 if (sep != string::npos) {
457 color_name = color_name.substr (0, sep);
459 themes.insert (make_pair (prop->value(), color_name));
465 ARDOUR_UI_UTILS::get_icon_sets ()
467 Searchpath spath(ARDOUR::ardour_data_search_path());
468 spath.add_subdirectory_to_paths ("icons");
471 r.push_back (_("default"));
473 for (vector<string>::iterator s = spath.begin(); s != spath.end(); ++s) {
475 vector<string> entries;
477 get_paths (entries, *s, false, false);
479 for (vector<string>::iterator e = entries.begin(); e != entries.end(); ++e) {
480 if (Glib::file_test (*e, Glib::FILE_TEST_IS_DIR)) {
481 r.push_back (Glib::filename_to_utf8 (Glib::path_get_basename(*e)));
490 ARDOUR_UI_UTILS::get_icon_path (const char* cname, string icon_set, bool is_image)
492 std::string data_file_path;
499 Searchpath spath(ARDOUR::ardour_data_search_path());
501 if (!icon_set.empty() && icon_set != _("default")) {
503 /* add "icons/icon_set" but .. not allowed to add both of these at once */
504 spath.add_subdirectory_to_paths ("icons");
505 spath.add_subdirectory_to_paths (icon_set);
507 find_file (spath, name, data_file_path);
509 spath.add_subdirectory_to_paths ("icons");
510 find_file (spath, name, data_file_path);
513 if (data_file_path.empty()) {
514 Searchpath rc (ARDOUR::ardour_data_search_path());
515 rc.add_subdirectory_to_paths ("resources");
516 find_file (rc, name, data_file_path);
519 if (is_image && data_file_path.empty()) {
521 if (!icon_set.empty() && icon_set != _("default")) {
522 warning << string_compose (_("icon \"%1\" not found for icon set \"%2\", fallback to default"), cname, icon_set) << endmsg;
525 Searchpath def (ARDOUR::ardour_data_search_path());
526 def.add_subdirectory_to_paths ("icons");
528 if (!find_file (def, name, data_file_path)) {
529 fatal << string_compose (_("cannot find icon image for %1 using %2"), name, spath.to_string()) << endmsg;
530 abort(); /*NOTREACHED*/
534 return data_file_path;
537 Glib::RefPtr<Gdk::Pixbuf>
538 ARDOUR_UI_UTILS::get_icon (const char* cname, string icon_set)
540 Glib::RefPtr<Gdk::Pixbuf> img;
542 img = Gdk::Pixbuf::create_from_file (get_icon_path (cname, icon_set));
543 } catch (const Gdk::PixbufError &e) {
544 cerr << "Caught PixbufError: " << e.what() << endl;
546 error << string_compose (_("Caught exception while loading icon named %1"), cname) << endmsg;
552 namespace ARDOUR_UI_UTILS {
553 Glib::RefPtr<Gdk::Pixbuf>
554 get_icon (const char* cname)
556 Glib::RefPtr<Gdk::Pixbuf> img;
558 img = Gdk::Pixbuf::create_from_file (get_icon_path (cname));
559 } catch (const Gdk::PixbufError &e) {
560 cerr << "Caught PixbufError: " << e.what() << endl;
562 error << string_compose (_("Caught exception while loading icon named %1"), cname) << endmsg;
570 ARDOUR_UI_UTILS::longest (vector<string>& strings)
572 if (strings.empty()) {
576 vector<string>::iterator longest = strings.begin();
577 string::size_type longest_length = (*longest).length();
579 vector<string>::iterator i = longest;
582 while (i != strings.end()) {
584 string::size_type len = (*i).length();
586 if (len > longest_length) {
588 longest_length = len;
598 ARDOUR_UI_UTILS::key_is_legal_for_numeric_entry (guint keyval)
600 /* we assume that this does not change over the life of the process
603 static int comma_decimal = -1;
608 if (comma_decimal < 0) {
609 std::lconv* lc = std::localeconv();
610 if (strchr (lc->decimal_point, ',') != 0) {
622 case GDK_decimalpoint:
623 case GDK_KP_Separator:
653 case GDK_KP_Subtract:
683 ARDOUR_UI_UTILS::resize_window_to_proportion_of_monitor (Gtk::Window* window, int max_width, int max_height)
685 Glib::RefPtr<Gdk::Screen> screen = window->get_screen ();
686 Gdk::Rectangle monitor_rect;
687 screen->get_monitor_geometry (0, monitor_rect);
689 int const w = std::min (int (monitor_rect.get_width() * 0.8), max_width);
690 int const h = std::min (int (monitor_rect.get_height() * 0.8), max_height);
692 window->resize (w, h);
696 /** Replace _ with __ in a string; for use with menu item text to make underscores displayed correctly */
698 ARDOUR_UI_UTILS::escape_underscores (string const & s)
701 string::size_type const N = s.length ();
703 for (string::size_type i = 0; i < N; ++i) {
715 ARDOUR_UI_UTILS::unique_random_color (list<Gdk::Color>& used_colors)
723 h = fmod (random(), 360.0);
724 s = (random() % 65535) / 65535.0;
725 v = (random() % 65535) / 65535.0;
727 s = min (0.5, s); /* not too saturated */
728 v = max (0.9, v); /* not too bright */
729 newcolor.set_hsv (h, s, v);
731 if (used_colors.size() == 0) {
732 used_colors.push_back (newcolor);
736 for (list<Gdk::Color>::iterator i = used_colors.begin(); i != used_colors.end(); ++i) {
738 float rdelta, bdelta, gdelta;
740 rdelta = newcolor.get_red() - c.get_red();
741 bdelta = newcolor.get_blue() - c.get_blue();
742 gdelta = newcolor.get_green() - c.get_green();
744 if (sqrt (rdelta*rdelta + bdelta*bdelta + gdelta*gdelta) > 25.0) {
745 /* different enough */
746 used_colors.push_back (newcolor);
751 /* XXX need throttle here to make sure we don't spin for ever */
756 ARDOUR_UI_UTILS::rate_as_string (float r)
759 if (fmod (r, 1000.0f)) {
760 snprintf (buf, sizeof (buf), "%.1f kHz", r/1000.0);
762 snprintf (buf, sizeof (buf), "%.0f kHz", r/1000.0);
768 ARDOUR_UI_UTILS::windows_overlap (Gtk::Window *a, Gtk::Window *b)
774 if (a->get_screen() == b->get_screen()) {
778 a->get_position (ex, ey);
779 a->get_size (ew, eh);
780 b->get_position (mx, my);
781 b->get_size (mw, mh);
797 if (gdk_rectangle_intersect (&e, &m, &r)) {
805 ARDOUR_UI_UTILS::overwrite_file_dialog (Gtk::Window& parent, string title, string text)
807 ArdourDialog dialog (parent, title, true);
810 dialog.get_vbox()->pack_start (label, true, true);
811 dialog.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
812 dialog.add_button (_("Overwrite"), Gtk::RESPONSE_ACCEPT);
815 switch (dialog.run()) {
816 case RESPONSE_ACCEPT:
818 case RESPONSE_CANCEL:
825 ARDOUR_UI_UTILS::running_from_source_tree ()
827 gchar const *x = g_getenv ("ARDOUR_THEMES_PATH");
828 return x && (string (x).find ("gtk2_ardour") != string::npos);
832 ARDOUR_UI_UTILS::shared_popup_menu ()
834 return ARDOUR_UI::instance()->shared_popup_menu ();