menu item says Fader and Pan, so the automation track should say the same for consistency
[ardour.git] / gtk2_ardour / utils.cc
index 68b5768dedda66abefba3e0ea387555f81ead14c..a762887d3f98f12eb0cd1381fc93018932e1935c 100644 (file)
@@ -408,10 +408,12 @@ key_press_focus_accelerator_handler (Gtk::Window& window, GdkEventKey* ev)
        GtkWindow* win = window.gobj();
        GtkWidget* focus = gtk_window_get_focus (win);
        bool special_handling_of_unmodified_accelerators = false;
+       bool allow_activating = true;
 
 #undef DEBUG_ACCELERATOR_HANDLING
 #ifdef  DEBUG_ACCELERATOR_HANDLING
-       bool debug = (getenv ("ARDOUR_DEBUG_ACCELERATOR_HANDLING") != 0);
+       //bool debug = (getenv ("ARDOUR_DEBUG_ACCELERATOR_HANDLING") != 0);
+       bool debug=true;
 #endif
        if (focus) {
                if (GTK_IS_ENTRY(focus) || Keyboard::some_magic_widget_has_focus()) {
@@ -419,10 +421,21 @@ key_press_focus_accelerator_handler (Gtk::Window& window, GdkEventKey* ev)
                } 
        } 
 
+#ifdef GTKOSX
+       /* should this be universally true? */
+       if (Keyboard::some_magic_widget_has_focus ()) {
+               allow_activating = false;
+       }
+#endif
+
 #ifdef DEBUG_ACCELERATOR_HANDLING
        if (debug) {
                cerr << "Win = " << win << " Key event: code = " << ev->keyval << " state = " << hex << ev->state << dec << " special handling ? " 
                     << special_handling_of_unmodified_accelerators
+                    << " magic widget focus ? "
+                    << Keyboard::some_magic_widget_has_focus()
+                    << " allow_activation ? "
+                    << allow_activating
                     << endl;
        }
 #endif
@@ -466,49 +479,22 @@ key_press_focus_accelerator_handler (Gtk::Window& window, GdkEventKey* ev)
                   it does allow.
                */
 
-               int fakekey = GDK_VoidSymbol;
-               int ret = false;
-
-               switch (ev->keyval) {
-               case GDK_Tab:
-               case GDK_ISO_Left_Tab:
-                       fakekey = GDK_nabla;
-                       break;
-
-               case GDK_Up:
-                       fakekey = GDK_uparrow;
-                       break;
-
-               case GDK_Down:
-                       fakekey = GDK_downarrow;
-                       break;
-
-               case GDK_Right:
-                       fakekey = GDK_rightarrow;
-                       break;
-
-               case GDK_Left:
-                       fakekey = GDK_leftarrow;
-                       break;
-
-               default:
-                       break;
-               }
+               uint32_t fakekey = ev->keyval;
 
-               if (fakekey != GDK_VoidSymbol) {
-                       ret = gtk_accel_groups_activate(G_OBJECT(win), fakekey, GdkModifierType(ev->state));
-                       
-                       if (ret) {
+               if (possibly_translate_keyval_to_make_legal_accelerator (fakekey)) {
+                       if (allow_activating && gtk_accel_groups_activate(G_OBJECT(win), fakekey, GdkModifierType(ev->state))) {
                                return true;
                        }
 
 #ifdef GTKOSX
-                       int oldval = ev->keyval;
-                       ev->keyval = fakekey;
-                       if (gdk_quartz_possibly_forward ((GdkEvent*) ev)) {
-                               return true;
+                       if (allow_activating) {
+                               int oldval = ev->keyval;
+                               ev->keyval = fakekey;
+                               if (gdk_quartz_possibly_forward ((GdkEvent*) ev)) {
+                                       return true;
+                               }
+                               ev->keyval = oldval;
                        }
-                       ev->keyval = oldval;
 #endif
                }
        }
@@ -526,26 +512,24 @@ key_press_focus_accelerator_handler (Gtk::Window& window, GdkEventKey* ev)
                        cerr << "\tactivate, then propagate\n";
                }
 #endif
+
+               if (allow_activating) {
 #ifdef GTKOSX
-               if (gdk_quartz_possibly_forward ((GdkEvent*) ev)) {
-                       return true;
-               }
-#endif
-               if (!gtk_window_activate_key (win, ev)) {
-#ifdef DEBUG_ACCELERATOR_HANDLING
-                       if (debug) {
-                               cerr << "\tnot accelerated, now propagate\n";
+                       if (gdk_quartz_possibly_forward ((GdkEvent*) ev)) {
+                               return true;
                        }
 #endif
-                       return gtk_window_propagate_key_event (win, ev);
-               } else {
-#ifdef DEBUG_ACCELERATOR_HANDLING
-                       if (debug) {
-                               cerr << "\taccelerated - done.\n";
+                       if (gtk_window_activate_key (win, ev)) {
+                               return true;
                        }
+               }
+
+#ifdef DEBUG_ACCELERATOR_HANDLING
+               if (debug) {
+                       cerr << "\tnot accelerated, now propagate\n";
+               }
 #endif
-                       return true;
-               } 
+               return gtk_window_propagate_key_event (win, ev);
        }
        
        /* no modifiers, propagate first */
@@ -561,12 +545,17 @@ key_press_focus_accelerator_handler (Gtk::Window& window, GdkEventKey* ev)
                        cerr << "\tpropagation didn't handle, so activate\n";
                }
 #endif
+
+               if (allow_activating) {
+                       
 #ifdef GTKOSX
-               if (gdk_quartz_possibly_forward ((GdkEvent*) ev)) {
-                       return true;
-               }
+                       if (gdk_quartz_possibly_forward ((GdkEvent*) ev)) {
+                               return true;
+                       }
 #endif
-               return gtk_window_activate_key (win, ev);
+                       return gtk_window_activate_key (win, ev);
+               } 
+                       
        } else {
 #ifdef DEBUG_ACCELERATOR_HANDLING
                if (debug) {
@@ -587,8 +576,14 @@ key_press_focus_accelerator_handler (Gtk::Window& window, GdkEventKey* ev)
 Glib::RefPtr<Gdk::Pixbuf>      
 get_xpm (std::string name)
 {
+       //cerr << "xpm path = " << ARDOUR::find_data_file(name, "pixmaps") << endl;//DEBUG
        if (!xpm_map[name]) {
-               xpm_map[name] = Gdk::Pixbuf::create_from_file (ARDOUR::find_data_file(name, "pixmaps"));
+               try {
+                       xpm_map[name] = Gdk::Pixbuf::create_from_file (ARDOUR::find_data_file(name, "pixmaps"));
+               }
+               catch(const Glib::Error& e)     {
+               warning << "Caught Glib::Error: " << e.what() << endmsg;
+               }
        }
                
        return (xpm_map[name]);
@@ -703,13 +698,13 @@ set_pango_fontsize ()
 {
        long val = ARDOUR::Config->get_font_scale();
 
-       /* FT2 rendering */
+       /* FT2 rendering - used by GnomeCanvas, sigh */
 
        pango_ft2_font_map_set_resolution ((PangoFT2FontMap*) pango_ft2_font_map_for_display(), val/1024, val/1024);
 
        /* Cairo rendering, in case there is any */
        
-       pango_cairo_font_map_set_resolution ((PangoCairoFontMap*) pango_cairo_font_map_get_default(), val/1024);
+       // pango_cairo_font_map_set_resolution ((PangoCairoFontMap*) pango_cairo_font_map_get_default(), val/1024);
 }
 
 void
@@ -724,3 +719,98 @@ reset_dpi ()
        DPIReset();//Emit Signal
 }
 
+bool
+possibly_translate_keyval_to_make_legal_accelerator (uint32_t& keyval)
+{
+       int fakekey = GDK_VoidSymbol;
+
+       switch (keyval) {
+       case GDK_Tab:
+       case GDK_ISO_Left_Tab:
+               fakekey = GDK_nabla;
+               break;
+               
+       case GDK_Up:
+               fakekey = GDK_uparrow;
+               break;
+               
+       case GDK_Down:
+               fakekey = GDK_downarrow;
+               break;
+               
+       case GDK_Right:
+               fakekey = GDK_rightarrow;
+               break;
+               
+       case GDK_Left:
+               fakekey = GDK_leftarrow;
+               break;
+               
+       default:
+               break;
+       }
+       
+       if (fakekey != GDK_VoidSymbol) {
+               keyval = fakekey;
+               return true;
+       } 
+
+       return false;
+}
+               
+inline guint8
+convert_color_channel (guint8 src,
+                      guint8 alpha)
+{
+       return alpha ? ((guint (src) << 8) - src) / alpha : 0;
+}
+
+void
+convert_bgra_to_rgba (guint8 const* src,
+                     guint8*       dst,
+                     int           width,
+                     int           height)
+{
+       guint8 const* src_pixel = src;
+       guint8*       dst_pixel = dst;
+       
+       for (int y = 0; y < height; y++)
+               for (int x = 0; x < width; x++)
+               {
+                       dst_pixel[0] = convert_color_channel (src_pixel[2],
+                                                             src_pixel[3]);
+                       dst_pixel[1] = convert_color_channel (src_pixel[1],
+                                                             src_pixel[3]);
+                       dst_pixel[2] = convert_color_channel (src_pixel[0],
+                                                             src_pixel[3]);
+                       dst_pixel[3] = src_pixel[3];
+                       
+                       dst_pixel += 4;
+                       src_pixel += 4;
+               }
+}
+
+Glib::RefPtr<Gdk::Pixbuf>
+pixbuf_from_ustring(const ustring& name, Pango::FontDescription* font, int clip_width, int clip_height)
+{
+       Glib::RefPtr<Gdk::Pixbuf> buf = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, true, 8, clip_width, clip_height);
+       cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, clip_width, clip_height);
+       cairo_t* cr = cairo_create (surface);
+       cairo_text_extents_t te;
+
+       cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 1.0);
+       cairo_select_font_face (cr, font->get_family().c_str(),
+                               CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
+       cairo_set_font_size (cr,  font->get_size() / Pango::SCALE);
+       cairo_text_extents (cr, name.c_str(), &te);
+       
+       cairo_move_to (cr, 0.5, 0.5 - te.height / 2 - te.y_bearing + clip_height / 2);
+       cairo_show_text (cr, name.c_str());
+       
+       convert_bgra_to_rgba(cairo_image_surface_get_data (surface), buf->get_pixels(), clip_width, clip_height);
+
+       cairo_destroy(cr);
+       cairo_surface_destroy(surface);
+
+       return buf;
+}