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"
24 #include <pango/pangoft2.h> // for fontmap resolution control for GnomeCanvas
25 #include <pango/pangocairo.h> // for fontmap resolution control for GnomeCanvas
32 #include <libart_lgpl/art_misc.h>
34 #include <gtkmm/window.h>
35 #include <gtkmm/combo.h>
36 #include <gtkmm/label.h>
37 #include <gtkmm/paned.h>
38 #include <gtk/gtkpaned.h>
40 #include "pbd/file_utils.h"
42 #include <gtkmm2ext/utils.h>
43 #include "ardour/configuration.h"
44 #include "ardour/rc_configuration.h"
46 #include "ardour/filesystem_paths.h"
48 #include "ardour_ui.h"
50 #include "public_editor.h"
54 #include "rgb_macros.h"
55 #include "canvas_impl.h"
56 #include "gui_thread.h"
62 using Gtkmm2ext::Keyboard;
64 sigc::signal<void> DPIReset;
67 /** Add an element to a menu, settings its sensitivity.
68 * @param m Menu to add to.
69 * @param e Element to add.
70 * @param s true to make sensitive, false to make insensitive
73 add_item_with_sensitivity (Menu_Helpers::MenuList& m, Menu_Helpers::MenuElem e, bool s)
77 m.back().set_sensitive (false);
83 just_hide_it (GdkEventAny */*ev*/, Gtk::Window *win)
89 /* xpm2rgb copied from nixieclock, which bore the legend:
91 nixieclock - a nixie desktop timepiece
92 Copyright (C) 2000 Greg Ercolano, erco@3dsite.com
94 and was released under the GPL.
98 xpm2rgb (const char** xpm, uint32_t& w, uint32_t& h)
100 static long vals[256], val;
101 uint32_t t, x, y, colors, cpp;
103 unsigned char *savergb, *rgb;
107 if ( sscanf(xpm[0], "%u%u%u%u", &w, &h, &colors, &cpp) != 4 ) {
108 error << string_compose (_("bad XPM header %1"), xpm[0])
113 savergb = rgb = (unsigned char*) malloc (h * w * 3);
115 // LOAD XPM COLORMAP LONG ENOUGH TO DO CONVERSION
116 for (t = 0; t < colors; ++t) {
117 sscanf (xpm[t+1], "%c c #%lx", &c, &val);
121 // COLORMAP -> RGB CONVERSION
122 // Get low 3 bytes from vals[]
126 for (y = h-1; y > 0; --y) {
128 for (p = xpm[1+colors+(h-y-1)], x = 0; x < w; x++, rgb += 3) {
129 val = vals[(int)*p++];
130 *(rgb+2) = val & 0xff; val >>= 8; // 2:B
131 *(rgb+1) = val & 0xff; val >>= 8; // 1:G
132 *(rgb+0) = val & 0xff; // 0:R
140 xpm2rgba (const char** xpm, uint32_t& w, uint32_t& h)
142 static long vals[256], val;
143 uint32_t t, x, y, colors, cpp;
145 unsigned char *savergb, *rgb;
150 if ( sscanf(xpm[0], "%u%u%u%u", &w, &h, &colors, &cpp) != 4 ) {
151 error << string_compose (_("bad XPM header %1"), xpm[0])
156 savergb = rgb = (unsigned char*) malloc (h * w * 4);
158 // LOAD XPM COLORMAP LONG ENOUGH TO DO CONVERSION
160 if (strstr (xpm[1], "None")) {
161 sscanf (xpm[1], "%c", &transparent);
168 for (; t < colors; ++t) {
169 sscanf (xpm[t+1], "%c c #%lx", &c, &val);
173 // COLORMAP -> RGB CONVERSION
174 // Get low 3 bytes from vals[]
178 for (y = h-1; y > 0; --y) {
182 for (p = xpm[1+colors+(h-y-1)], x = 0; x < w; x++, rgb += 4) {
184 if (transparent && (*p++ == transparent)) {
192 *(rgb+3) = alpha; // 3: alpha
193 *(rgb+2) = val & 0xff; val >>= 8; // 2:B
194 *(rgb+1) = val & 0xff; val >>= 8; // 1:G
195 *(rgb+0) = val & 0xff; // 0:R
202 ArdourCanvas::Points*
203 get_canvas_points (string /*who*/, uint32_t npoints)
205 // cerr << who << ": wants " << npoints << " canvas points" << endl;
206 #ifdef TRAP_EXCESSIVE_POINT_REQUESTS
207 if (npoints > (uint32_t) gdk_screen_width() + 4) {
211 return new ArdourCanvas::Points (npoints);
214 Pango::FontDescription
215 get_font_for_style (string widgetname)
217 Gtk::Window window (WINDOW_TOPLEVEL);
219 Glib::RefPtr<Gtk::Style> style;
222 foobar.set_name (widgetname);
223 foobar.ensure_style();
225 style = foobar.get_style ();
227 Glib::RefPtr<const Pango::Layout> layout = foobar.get_layout();
229 PangoFontDescription *pfd = (PangoFontDescription *)pango_layout_get_font_description((PangoLayout *)layout->gobj());
233 /* layout inherited its font description from a PangoContext */
235 PangoContext* ctxt = (PangoContext*) pango_layout_get_context ((PangoLayout*) layout->gobj());
236 pfd = pango_context_get_font_description (ctxt);
237 return Pango::FontDescription (pfd); /* make a copy */
240 return Pango::FontDescription (pfd); /* make a copy */
244 rgba_from_style (string style, uint32_t r, uint32_t g, uint32_t b, uint32_t a, string attr, int state, bool rgba)
246 /* In GTK+2, styles aren't set up correctly if the widget is not
247 attached to a toplevel window that has a screen pointer.
250 static Gtk::Window* window = 0;
253 window = new Window (WINDOW_TOPLEVEL);
260 foo.set_name (style);
263 GtkRcStyle* rc = foo.get_style()->gobj()->rc_style;
267 r = rc->fg[state].red / 257;
268 g = rc->fg[state].green / 257;
269 b = rc->fg[state].blue / 257;
271 /* what a hack ... "a" is for "active" */
272 if (state == Gtk::STATE_NORMAL && rgba) {
273 a = rc->fg[GTK_STATE_ACTIVE].red / 257;
275 } else if (attr == "bg") {
277 r = rc->bg[state].red / 257;
278 g = rc->bg[state].green / 257;
279 b = rc->bg[state].blue / 257;
280 } else if (attr == "base") {
281 r = rc->base[state].red / 257;
282 g = rc->base[state].green / 257;
283 b = rc->base[state].blue / 257;
284 } else if (attr == "text") {
285 r = rc->text[state].red / 257;
286 g = rc->text[state].green / 257;
287 b = rc->text[state].blue / 257;
290 warning << string_compose (_("missing RGBA style for \"%1\""), style) << endl;
295 if (state == Gtk::STATE_NORMAL && rgba) {
296 return (uint32_t) RGBA_TO_UINT(r,g,b,a);
298 return (uint32_t) RGB_TO_UINT(r,g,b);
303 canvas_item_visible (ArdourCanvas::Item* item)
305 return (item->gobj()->object.flags & GNOME_CANVAS_ITEM_VISIBLE) ? true : false;
309 set_color (Gdk::Color& c, int rgb)
311 c.set_rgb((rgb >> 16)*256, ((rgb & 0xff00) >> 8)*256, (rgb & 0xff)*256);
315 relay_key_press (GdkEventKey* ev, Gtk::Window* win)
317 if (!key_press_focus_accelerator_handler (*win, ev)) {
318 return PublicEditor::instance().on_key_press_event(ev);
325 forward_key_press (GdkEventKey* ev)
327 return PublicEditor::instance().on_key_press_event(ev);
331 key_press_focus_accelerator_handler (Gtk::Window& window, GdkEventKey* ev)
333 GtkWindow* win = window.gobj();
334 GtkWidget* focus = gtk_window_get_focus (win);
335 bool special_handling_of_unmodified_accelerators = false;
336 bool allow_activating = true;
337 /* consider all relevant modifiers but not LOCK or SHIFT */
338 const guint mask = (Keyboard::RelevantModifierKeyMask & ~(Gdk::SHIFT_MASK|Gdk::LOCK_MASK));
341 if (GTK_IS_ENTRY(focus) || Keyboard::some_magic_widget_has_focus()) {
342 special_handling_of_unmodified_accelerators = true;
347 /* should this be universally true? */
348 if (Keyboard::some_magic_widget_has_focus ()) {
349 allow_activating = false;
354 DEBUG_TRACE (DEBUG::Accelerators, string_compose ("Win = %1 focus = %7 Key event: code = %2 state = %3 special handling ? %4 magic widget focus ? %5 allow_activation ? %6\n",
358 special_handling_of_unmodified_accelerators,
359 Keyboard::some_magic_widget_has_focus(),
363 /* This exists to allow us to override the way GTK handles
364 key events. The normal sequence is:
366 a) event is delivered to a GtkWindow
367 b) accelerators/mnemonics are activated
368 c) if (b) didn't handle the event, propagate to
369 the focus widget and/or focus chain
371 The problem with this is that if the accelerators include
372 keys without modifiers, such as the space bar or the
373 letter "e", then pressing the key while typing into
374 a text entry widget results in the accelerator being
375 activated, instead of the desired letter appearing
378 There is no good way of fixing this, but this
379 represents a compromise. The idea is that
380 key events involving modifiers (not Shift)
381 get routed into the activation pathway first, then
382 get propagated to the focus widget if necessary.
384 If the key event doesn't involve modifiers,
385 we deliver to the focus widget first, thus allowing
386 it to get "normal text" without interference
389 Of course, this can also be problematic: if there
390 is a widget with focus, then it will swallow
391 all "normal text" accelerators.
394 if (!special_handling_of_unmodified_accelerators) {
396 /* XXX note that for a brief moment, the conditional above
397 * included "|| (ev->state & mask)" so as to enforce the
398 * implication of special_handling_of_UNMODIFIED_accelerators.
399 * however, this forces any key that GTK doesn't allow and that
400 * we have an alternative (see next comment) for to be
401 * automatically sent through the accel groups activation
402 * pathway, which prevents individual widgets & canvas items
403 * from ever seeing it if is used by a key binding.
405 * specifically, this hid Ctrl-down-arrow from MIDI region
406 * views because it is also bound to an action.
408 * until we have a robust, clean binding system, this
409 * quirk will have to remain in place.
412 /* pretend that certain key events that GTK does not allow
413 to be used as accelerators are actually something that
414 it does allow. but only where there are no modifiers.
417 uint32_t fakekey = ev->keyval;
419 if (Gtkmm2ext::possibly_translate_keyval_to_make_legal_accelerator (fakekey)) {
420 if (allow_activating && gtk_accel_groups_activate(G_OBJECT(win), fakekey, GdkModifierType(ev->state))) {
421 DEBUG_TRACE (DEBUG::Accelerators, "\taccel group activated by fakekey\n");
427 if (!special_handling_of_unmodified_accelerators || (ev->state & mask)) {
429 /* no special handling or there are modifiers in effect: accelerate first */
431 DEBUG_TRACE (DEBUG::Accelerators, "\tactivate, then propagate\n");
433 if (allow_activating) {
434 if (gtk_window_activate_key (win, ev)) {
438 DEBUG_TRACE (DEBUG::Accelerators, "\tactivation skipped\n");
441 DEBUG_TRACE (DEBUG::Accelerators, "\tnot accelerated, now propagate\n");
443 return gtk_window_propagate_key_event (win, ev);
446 /* no modifiers, propagate first */
448 DEBUG_TRACE (DEBUG::Accelerators, "\tpropagate, then activate\n");
450 if (!gtk_window_propagate_key_event (win, ev)) {
451 DEBUG_TRACE (DEBUG::Accelerators, "\tpropagation didn't handle, so activate\n");
452 if (allow_activating) {
453 return gtk_window_activate_key (win, ev);
455 DEBUG_TRACE (DEBUG::Accelerators, "\tactivation skipped\n");
459 DEBUG_TRACE (DEBUG::Accelerators, "\thandled by propagate\n");
463 DEBUG_TRACE (DEBUG::Accelerators, "\tnot handled\n");
467 Glib::RefPtr<Gdk::Pixbuf>
468 get_xpm (std::string name)
470 if (!xpm_map[name]) {
472 SearchPath spath(ARDOUR::ardour_search_path());
473 spath += ARDOUR::system_data_search_path();
475 spath.add_subdirectory_to_paths("pixmaps");
477 sys::path data_file_path;
479 if(!find_file_in_search_path (spath, name, data_file_path)) {
480 fatal << string_compose (_("cannot find XPM file for %1"), name) << endmsg;
484 xpm_map[name] = Gdk::Pixbuf::create_from_file (data_file_path.to_string());
485 } catch(const Glib::Error& e) {
486 warning << "Caught Glib::Error: " << e.what() << endmsg;
490 return xpm_map[name];
494 get_icon_path (const char* cname)
499 SearchPath spath(ARDOUR::ardour_search_path());
500 spath += ARDOUR::system_data_search_path();
502 spath.add_subdirectory_to_paths("icons");
504 sys::path data_file_path;
506 if (!find_file_in_search_path (spath, name, data_file_path)) {
507 fatal << string_compose (_("cannot find icon image for %1"), name) << endmsg;
510 return data_file_path.to_string();
513 Glib::RefPtr<Gdk::Pixbuf>
514 get_icon (const char* cname)
516 Glib::RefPtr<Gdk::Pixbuf> img;
518 img = Gdk::Pixbuf::create_from_file (get_icon_path (cname));
519 } catch (const Gdk::PixbufError &e) {
520 cerr << "Caught PixbufError: " << e.what() << endl;
522 g_message("Caught ... ");
529 longest (vector<string>& strings)
531 if (strings.empty()) {
535 vector<string>::iterator longest = strings.begin();
536 string::size_type longest_length = (*longest).length();
538 vector<string>::iterator i = longest;
541 while (i != strings.end()) {
543 string::size_type len = (*i).length();
545 if (len > longest_length) {
547 longest_length = len;
557 key_is_legal_for_numeric_entry (guint keyval)
575 case GDK_KP_Subtract:
604 set_pango_fontsize ()
606 long val = ARDOUR::Config->get_font_scale();
608 /* FT2 rendering - used by GnomeCanvas, sigh */
610 pango_ft2_font_map_set_resolution ((PangoFT2FontMap*) pango_ft2_font_map_for_display(), val/1024, val/1024);
612 /* Cairo rendering, in case there is any */
614 pango_cairo_font_map_set_resolution ((PangoCairoFontMap*) pango_cairo_font_map_get_default(), val/1024);
620 long val = ARDOUR::Config->get_font_scale();
621 set_pango_fontsize ();
624 gtk_settings_set_long_property (gtk_settings_get_default(),
625 "gtk-xft-dpi", val, "ardour");
626 DPIReset();//Emit Signal
630 resize_window_to_proportion_of_monitor (Gtk::Window* window, int max_width, int max_height)
632 Glib::RefPtr<Gdk::Screen> screen = window->get_screen ();
633 Gdk::Rectangle monitor_rect;
634 screen->get_monitor_geometry (0, monitor_rect);
636 int const w = std::min (int (monitor_rect.get_width() * 0.8), max_width);
637 int const h = std::min (int (monitor_rect.get_height() * 0.8), max_height);
639 window->resize (w, h);
643 /** Replace _ with __ in a string; for use with menu item text to make underscores displayed correctly */
645 escape_underscores (string const & s)
648 string::size_type const N = s.length ();
650 for (string::size_type i = 0; i < N; ++i) {
662 unique_random_color (list<Gdk::Color>& used_colors)
668 /* avoid neon/glowing tones by limiting them to the
669 "inner section" (paler) of a color wheel/circle.
672 const int32_t max_saturation = 48000; // 65535 would open up the whole color wheel
674 newcolor.set_red (random() % max_saturation);
675 newcolor.set_blue (random() % max_saturation);
676 newcolor.set_green (random() % max_saturation);
678 if (used_colors.size() == 0) {
679 used_colors.push_back (newcolor);
683 for (list<Gdk::Color>::iterator i = used_colors.begin(); i != used_colors.end(); ++i) {
685 float rdelta, bdelta, gdelta;
687 rdelta = newcolor.get_red() - c.get_red();
688 bdelta = newcolor.get_blue() - c.get_blue();
689 gdelta = newcolor.get_green() - c.get_green();
691 if (sqrt (rdelta*rdelta + bdelta*bdelta + gdelta*gdelta) > 25.0) {
692 used_colors.push_back (newcolor);
697 /* XXX need throttle here to make sure we don't spin for ever */