2 Copyright (C) 2003 Paul Davis
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.
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.
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.
21 #include "gtk2ardour-config.h"
32 #include <gtkmm/window.h>
33 #include <gtkmm/combo.h>
34 #include <gtkmm/label.h>
35 #include <gtkmm/paned.h>
36 #include <gtk/gtkpaned.h>
37 #include <boost/algorithm/string.hpp>
39 #include "pbd/basename.h"
40 #include "pbd/file_utils.h"
41 #include "pbd/stacktrace.h"
43 #include <gtkmm2ext/utils.h>
45 #include "ardour/filesystem_paths.h"
46 #include "ardour/search_paths.h"
48 #include "canvas/item.h"
49 #include "canvas/utils.h"
52 #include "public_editor.h"
56 #include "rgb_macros.h"
57 #include "gui_thread.h"
58 #include "ui_config.h"
59 #include "ardour_dialog.h"
60 #include "ardour_ui.h"
66 using Gtkmm2ext::Keyboard;
68 namespace ARDOUR_UI_UTILS {
69 sigc::signal<void> DPIReset;
72 #ifdef PLATFORM_WINDOWS
73 #define random() rand()
77 /** Add an element to a menu, settings its sensitivity.
78 * @param m Menu to add to.
79 * @param e Element to add.
80 * @param s true to make sensitive, false to make insensitive
83 ARDOUR_UI_UTILS::add_item_with_sensitivity (Menu_Helpers::MenuList& m, Menu_Helpers::MenuElem e, bool s)
87 m.back().set_sensitive (false);
93 ARDOUR_UI_UTILS::just_hide_it (GdkEventAny */*ev*/, Gtk::Window *win)
99 /* xpm2rgb copied from nixieclock, which bore the legend:
101 nixieclock - a nixie desktop timepiece
102 Copyright (C) 2000 Greg Ercolano, erco@3dsite.com
104 and was released under the GPL.
108 ARDOUR_UI_UTILS::xpm2rgb (const char** xpm, uint32_t& w, uint32_t& h)
110 static long vals[256], val;
111 uint32_t t, x, y, colors, cpp;
113 unsigned char *savergb, *rgb;
117 if ( sscanf(xpm[0], "%u%u%u%u", &w, &h, &colors, &cpp) != 4 ) {
118 error << string_compose (_("bad XPM header %1"), xpm[0])
123 savergb = rgb = (unsigned char*) malloc (h * w * 3);
125 // LOAD XPM COLORMAP LONG ENOUGH TO DO CONVERSION
126 for (t = 0; t < colors; ++t) {
127 sscanf (xpm[t+1], "%c c #%lx", &c, &val);
131 // COLORMAP -> RGB CONVERSION
132 // Get low 3 bytes from vals[]
136 for (y = h-1; y > 0; --y) {
138 for (p = xpm[1+colors+(h-y-1)], x = 0; x < w; x++, rgb += 3) {
139 val = vals[(int)*p++];
140 *(rgb+2) = val & 0xff; val >>= 8; // 2:B
141 *(rgb+1) = val & 0xff; val >>= 8; // 1:G
142 *(rgb+0) = val & 0xff; // 0:R
150 ARDOUR_UI_UTILS::xpm2rgba (const char** xpm, uint32_t& w, uint32_t& h)
152 static long vals[256], val;
153 uint32_t t, x, y, colors, cpp;
155 unsigned char *savergb, *rgb;
160 if ( sscanf(xpm[0], "%u%u%u%u", &w, &h, &colors, &cpp) != 4 ) {
161 error << string_compose (_("bad XPM header %1"), xpm[0])
166 savergb = rgb = (unsigned char*) malloc (h * w * 4);
168 // LOAD XPM COLORMAP LONG ENOUGH TO DO CONVERSION
170 if (strstr (xpm[1], "None")) {
171 sscanf (xpm[1], "%c", &transparent);
178 for (; t < colors; ++t) {
179 sscanf (xpm[t+1], "%c c #%lx", &c, &val);
183 // COLORMAP -> RGB CONVERSION
184 // Get low 3 bytes from vals[]
188 for (y = h-1; y > 0; --y) {
192 for (p = xpm[1+colors+(h-y-1)], x = 0; x < w; x++, rgb += 4) {
194 if (transparent && (*p++ == transparent)) {
202 *(rgb+3) = alpha; // 3: alpha
203 *(rgb+2) = val & 0xff; val >>= 8; // 2:B
204 *(rgb+1) = val & 0xff; val >>= 8; // 1:G
205 *(rgb+0) = val & 0xff; // 0:R
212 /** Returns a Pango::FontDescription given a string describing the font.
214 * If the returned FontDescription does not specify a family, then
215 * the family is set to "Sans". This mirrors GTK's behaviour in
218 * Some environments will force Pango to specify the family
219 * even if it was not specified in the string describing the font.
220 * Such environments should be left unaffected by this function,
221 * since the font family will be left alone.
223 * There may be other similar font specification enforcement
224 * that we might add here later.
226 Pango::FontDescription
227 ARDOUR_UI_UTILS::sanitized_font (std::string const& name)
229 Pango::FontDescription fd (name);
231 if (fd.get_family().empty()) {
232 fd.set_family ("Sans");
238 Pango::FontDescription
239 ARDOUR_UI_UTILS::get_font_for_style (string widgetname)
241 Gtk::Window window (WINDOW_TOPLEVEL);
243 Glib::RefPtr<Gtk::Style> style;
246 foobar.set_name (widgetname);
247 foobar.ensure_style();
249 style = foobar.get_style ();
251 Glib::RefPtr<const Pango::Layout> layout = foobar.get_layout();
253 PangoFontDescription *pfd = const_cast<PangoFontDescription *> (pango_layout_get_font_description(const_cast<PangoLayout *>(layout->gobj())));
257 /* layout inherited its font description from a PangoContext */
259 PangoContext* ctxt = (PangoContext*) pango_layout_get_context (const_cast<PangoLayout*>(layout->gobj()));
260 pfd = pango_context_get_font_description (ctxt);
261 return Pango::FontDescription (pfd); /* make a copy */
264 return Pango::FontDescription (pfd); /* make a copy */
268 ARDOUR_UI_UTILS::gdk_color_from_rgb (uint32_t rgb)
271 set_color_from_rgb (c, rgb);
276 ARDOUR_UI_UTILS::gdk_color_from_rgba (uint32_t rgba)
279 set_color_from_rgb (c, rgba);
284 ARDOUR_UI_UTILS::set_color_from_rgb (Gdk::Color& c, uint32_t rgb)
286 /* Gdk::Color color ranges are 16 bit, so scale from 8 bit by
289 c.set_rgb ((rgb >> 16)*256, ((rgb & 0xff00) >> 8)*256, (rgb & 0xff)*256);
293 ARDOUR_UI_UTILS::set_color_from_rgba (Gdk::Color& c, uint32_t rgba)
295 /* Gdk::Color color ranges are 16 bit, so scale from 8 bit by
298 c.set_rgb ((rgba >> 24)*256, ((rgba & 0xff0000) >> 16)*256, ((rgba & 0xff00) >> 8)*256);
302 ARDOUR_UI_UTILS::gdk_color_to_rgba (Gdk::Color const& c)
304 /* since alpha value is not available from a Gdk::Color, it is
305 hardcoded as 0xff (aka 255 or 1.0)
308 const uint32_t r = c.get_red_p () * 255.0;
309 const uint32_t g = c.get_green_p () * 255.0;
310 const uint32_t b = c.get_blue_p () * 255.0;
311 const uint32_t a = 0xff;
313 return RGBA_TO_UINT (r,g,b,a);
318 ARDOUR_UI_UTILS::relay_key_press (GdkEventKey* ev, Gtk::Window* win)
320 return ARDOUR_UI::instance()->key_event_handler (ev, win);
324 ARDOUR_UI_UTILS::emulate_key_event (unsigned int keyval)
326 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET(ARDOUR_UI::instance()->main_window().gobj()));
327 GdkKeymap *keymap = gdk_keymap_get_for_display (display);
328 GdkKeymapKey *keymapkey = NULL;
331 if (!gdk_keymap_get_entries_for_keyval(keymap, keyval, &keymapkey, &n_keys)) return false;
332 if (n_keys !=1) { g_free(keymapkey); return false;}
334 Gtk::Window& main_window (ARDOUR_UI::instance()->main_window());
337 ev.type = GDK_KEY_PRESS;
338 ev.window = main_window.get_window()->gobj();
339 ev.send_event = FALSE;
344 ev.string = const_cast<gchar*> ("");
345 ev.hardware_keycode = keymapkey[0].keycode;
346 ev.group = keymapkey[0].group;
349 relay_key_press (&ev, &main_window);
350 ev.type = GDK_KEY_RELEASE;
351 return relay_key_press(&ev, &main_window);
355 ARDOUR_UI_UTILS::show_gdk_event_state (int state)
358 if (state & GDK_SHIFT_MASK) {
361 if (state & GDK_LOCK_MASK) {
364 if (state & GDK_CONTROL_MASK) {
367 if (state & GDK_MOD1_MASK) {
370 if (state & GDK_MOD2_MASK) {
373 if (state & GDK_MOD3_MASK) {
376 if (state & GDK_MOD4_MASK) {
379 if (state & GDK_MOD5_MASK) {
382 if (state & GDK_BUTTON1_MASK) {
385 if (state & GDK_BUTTON2_MASK) {
388 if (state & GDK_BUTTON3_MASK) {
391 if (state & GDK_BUTTON4_MASK) {
394 if (state & GDK_BUTTON5_MASK) {
397 if (state & GDK_SUPER_MASK) {
400 if (state & GDK_HYPER_MASK) {
403 if (state & GDK_META_MASK) {
406 if (state & GDK_RELEASE_MASK) {
413 Glib::RefPtr<Gdk::Pixbuf>
414 ARDOUR_UI_UTILS::get_xpm (std::string name)
416 if (!xpm_map[name]) {
418 Searchpath spath(ARDOUR::ardour_data_search_path());
420 spath.add_subdirectory_to_paths("pixmaps");
422 std::string data_file_path;
424 if(!find_file (spath, name, data_file_path)) {
425 fatal << string_compose (_("cannot find XPM file for %1"), name) << endmsg;
429 xpm_map[name] = Gdk::Pixbuf::create_from_file (data_file_path);
430 } catch(const Glib::Error& e) {
431 warning << "Caught Glib::Error: " << e.what() << endmsg;
435 return xpm_map[name];
439 ARDOUR_UI_UTILS::get_color_themes (map<std::string,std::string>& themes)
441 Searchpath spath(ARDOUR::theme_search_path());
443 for (vector<string>::iterator s = spath.begin(); s != spath.end(); ++s) {
445 vector<string> entries;
447 find_files_matching_pattern (entries, *s, string ("*") + UIConfiguration::color_file_suffix);
449 for (vector<string>::iterator e = entries.begin(); e != entries.end(); ++e) {
453 tree.read ((*e).c_str());
454 XMLNode* root = tree.root();
456 if (!root || root->name() != X_("Ardour")) {
460 XMLProperty const* prop = root->property (X_("theme-name"));
466 themes.insert (make_pair (prop->value(), Glib::filename_to_utf8 (basename_nosuffix(*e))));
472 ARDOUR_UI_UTILS::get_icon_sets ()
474 Searchpath spath(ARDOUR::ardour_data_search_path());
475 spath.add_subdirectory_to_paths ("icons");
478 r.push_back (_("default"));
480 for (vector<string>::iterator s = spath.begin(); s != spath.end(); ++s) {
482 vector<string> entries;
484 get_paths (entries, *s, false, false);
486 for (vector<string>::iterator e = entries.begin(); e != entries.end(); ++e) {
487 if (Glib::file_test (*e, Glib::FILE_TEST_IS_DIR)) {
488 r.push_back (Glib::filename_to_utf8 (Glib::path_get_basename(*e)));
497 ARDOUR_UI_UTILS::get_icon_path (const char* cname, string icon_set, bool is_image)
499 std::string data_file_path;
506 Searchpath spath(ARDOUR::ardour_data_search_path());
508 if (!icon_set.empty() && icon_set != _("default")) {
510 /* add "icons/icon_set" but .. not allowed to add both of these at once */
511 spath.add_subdirectory_to_paths ("icons");
512 spath.add_subdirectory_to_paths (icon_set);
514 find_file (spath, name, data_file_path);
516 spath.add_subdirectory_to_paths ("icons");
517 find_file (spath, name, data_file_path);
520 if (data_file_path.empty()) {
521 Searchpath rc (ARDOUR::ardour_data_search_path());
522 rc.add_subdirectory_to_paths ("resources");
523 find_file (rc, name, data_file_path);
526 if (is_image && data_file_path.empty()) {
528 if (!icon_set.empty() && icon_set != _("default")) {
529 warning << string_compose (_("icon \"%1\" not found for icon set \"%2\", fallback to default"), cname, icon_set) << endmsg;
532 Searchpath def (ARDOUR::ardour_data_search_path());
533 def.add_subdirectory_to_paths ("icons");
535 if (!find_file (def, name, data_file_path)) {
536 fatal << string_compose (_("cannot find icon image for %1 using %2"), name, spath.to_string()) << endmsg;
537 abort(); /*NOTREACHED*/
541 return data_file_path;
544 Glib::RefPtr<Gdk::Pixbuf>
545 ARDOUR_UI_UTILS::get_icon (const char* cname, string icon_set)
547 Glib::RefPtr<Gdk::Pixbuf> img;
549 img = Gdk::Pixbuf::create_from_file (get_icon_path (cname, icon_set));
550 } catch (const Gdk::PixbufError &e) {
551 cerr << "Caught PixbufError: " << e.what() << endl;
553 error << string_compose (_("Caught exception while loading icon named %1"), cname) << endmsg;
559 namespace ARDOUR_UI_UTILS {
560 Glib::RefPtr<Gdk::Pixbuf>
561 get_icon (const char* cname)
563 Glib::RefPtr<Gdk::Pixbuf> img;
565 img = Gdk::Pixbuf::create_from_file (get_icon_path (cname));
566 } catch (const Gdk::PixbufError &e) {
567 cerr << "Caught PixbufError: " << e.what() << endl;
569 error << string_compose (_("Caught exception while loading icon named %1"), cname) << endmsg;
577 ARDOUR_UI_UTILS::longest (vector<string>& strings)
579 if (strings.empty()) {
583 vector<string>::iterator longest = strings.begin();
584 string::size_type longest_length = (*longest).length();
586 vector<string>::iterator i = longest;
589 while (i != strings.end()) {
591 string::size_type len = (*i).length();
593 if (len > longest_length) {
595 longest_length = len;
605 ARDOUR_UI_UTILS::key_is_legal_for_numeric_entry (guint keyval)
607 /* we assume that this does not change over the life of the process
610 static int comma_decimal = -1;
615 if (comma_decimal < 0) {
616 std::lconv* lc = std::localeconv();
617 if (strchr (lc->decimal_point, ',') != 0) {
629 case GDK_decimalpoint:
630 case GDK_KP_Separator:
660 case GDK_KP_Subtract:
690 ARDOUR_UI_UTILS::resize_window_to_proportion_of_monitor (Gtk::Window* window, int max_width, int max_height)
692 Glib::RefPtr<Gdk::Screen> screen = window->get_screen ();
693 Gdk::Rectangle monitor_rect;
694 screen->get_monitor_geometry (0, monitor_rect);
696 int const w = std::min (int (monitor_rect.get_width() * 0.8), max_width);
697 int const h = std::min (int (monitor_rect.get_height() * 0.8), max_height);
699 window->resize (w, h);
703 /** Replace _ with __ in a string; for use with menu item text to make underscores displayed correctly */
705 ARDOUR_UI_UTILS::escape_underscores (string const & s)
708 string::size_type const N = s.length ();
710 for (string::size_type i = 0; i < N; ++i) {
722 ARDOUR_UI_UTILS::unique_random_color (list<Gdk::Color>& used_colors)
730 h = fmod (random(), 360.0);
731 s = (random() % 65535) / 65535.0;
732 v = (random() % 65535) / 65535.0;
734 s = min (0.5, s); /* not too saturated */
735 v = max (0.9, v); /* not too bright */
736 newcolor.set_hsv (h, s, v);
738 if (used_colors.size() == 0) {
739 used_colors.push_back (newcolor);
743 for (list<Gdk::Color>::iterator i = used_colors.begin(); i != used_colors.end(); ++i) {
745 float rdelta, bdelta, gdelta;
747 rdelta = newcolor.get_red() - c.get_red();
748 bdelta = newcolor.get_blue() - c.get_blue();
749 gdelta = newcolor.get_green() - c.get_green();
751 if (sqrt (rdelta*rdelta + bdelta*bdelta + gdelta*gdelta) > 25.0) {
752 /* different enough */
753 used_colors.push_back (newcolor);
758 /* XXX need throttle here to make sure we don't spin for ever */
763 ARDOUR_UI_UTILS::rate_as_string (float r)
766 if (fmod (r, 1000.0f)) {
767 snprintf (buf, sizeof (buf), "%.1f kHz", r/1000.0);
769 snprintf (buf, sizeof (buf), "%.0f kHz", r/1000.0);
775 ARDOUR_UI_UTILS::windows_overlap (Gtk::Window *a, Gtk::Window *b)
781 if (a->get_screen() == b->get_screen()) {
785 a->get_position (ex, ey);
786 a->get_size (ew, eh);
787 b->get_position (mx, my);
788 b->get_size (mw, mh);
804 if (gdk_rectangle_intersect (&e, &m, &r)) {
812 ARDOUR_UI_UTILS::overwrite_file_dialog (Gtk::Window& parent, string title, string text)
814 ArdourDialog dialog (parent, title, true);
817 dialog.get_vbox()->pack_start (label, true, true);
818 dialog.add_button (Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
819 dialog.add_button (_("Overwrite"), Gtk::RESPONSE_ACCEPT);
822 switch (dialog.run()) {
823 case RESPONSE_ACCEPT:
825 case RESPONSE_CANCEL: