HIG-ify the locations UI a bit. Should fix #3526.
[ardour.git] / gtk2_ardour / au_pluginui.mm
index 51f73199f75bd045436b2904ce1fa8d2dfd4fb44..cb58f67f6fb616bb2dc379f0324c43440deeda45 100644 (file)
@@ -1,9 +1,17 @@
-#include <pbd/error.h>
-#include <ardour/audio_unit.h>
-#include <ardour/insert.h>
+#undef  Marker
+#define Marker FuckYouAppleAndYourLackOfNameSpaces
 
+#include "pbd/error.h"
+#include "ardour/audio_unit.h"
+#include "ardour/insert.h"
+
+#undef check // stupid gtk, stupid apple
+
+#include <gtkmm/button.h>
 #include <gdk/gdkquartz.h>
 
+#include <gtkmm2ext/utils.h>
+
 #include "au_pluginui.h"
 #include "gui_thread.h"
 
 #import <AudioUnit/AUCocoaUIView.h>
 #import <CoreAudioKit/AUGenericView.h>
 
+#undef Marker
+
+#include "keyboard.h"
+#include "utils.h"
+#include "public_editor.h"
 #include "i18n.h"
 
 using namespace ARDOUR;
 using namespace Gtk;
+using namespace Gtkmm2ext;
 using namespace sigc;
 using namespace std;
 using namespace PBD;
 
-static const float kOffsetForAUView_X = 220;
-static const float kOffsetForAUView_Y = 90;
+vector<string> AUPluginUI::automation_mode_strings;
+
+static const gchar* _automation_mode_strings[] = {
+       X_("Manual"),
+       X_("Play"),
+       X_("Write"),
+       X_("Touch"),
+       0
+};
 
 AUPluginUI::AUPluginUI (boost::shared_ptr<PluginInsert> insert)
        : PlugUIBase (insert)
+       , automation_mode_label (_("Automation"))
+       , preset_label (_("Presets"))
+       
 {
+       if (automation_mode_strings.empty()) {
+               automation_mode_strings = I18N (_automation_mode_strings);
+       }
+       
+       set_popdown_strings (automation_mode_selector, automation_mode_strings);
+       automation_mode_selector.set_active_text (automation_mode_strings.front());
+
        if ((au = boost::dynamic_pointer_cast<AUPlugin> (insert->plugin())) == 0) {
                error << _("unknown type of editor-supplying plugin (note: no AudioUnit support in this version of ardour)") << endmsg;
                throw failed_constructor ();
        }
 
-       bool has_carbon;
-       bool has_cocoa;
+       /* stuff some stuff into the top of the window */
+
+       HBox* smaller_hbox = manage (new HBox);
+
+       smaller_hbox->pack_start (preset_label, false, false, 10);
+       smaller_hbox->pack_start (preset_combo, false, false);
+       smaller_hbox->pack_start (save_button, false, false);
+       smaller_hbox->pack_start (automation_mode_label, false, false);
+       smaller_hbox->pack_start (automation_mode_selector, false, false);
+       smaller_hbox->pack_start (bypass_button, false, true);
+
+       VBox* v1_box = manage (new VBox);
+       VBox* v2_box = manage (new VBox);
+
+       v1_box->pack_start (*smaller_hbox, false, true);
+       v2_box->pack_start (focus_button, false, true);
 
-       carbon_parented = false;
-       cocoa_parented = false;
+       top_box.set_homogeneous (false);
+       top_box.set_spacing (6);
+       top_box.set_border_width (6);
+
+       top_box.pack_end (*v2_box, false, false);
+       top_box.pack_end (*v1_box, false, false);
+
+       set_spacing (6);
+       pack_start (top_box, false, false);
+       pack_start (low_box, false, false);
+
+       preset_label.show ();
+       preset_combo.show ();
+       automation_mode_label.show ();
+       automation_mode_selector.show ();
+       bypass_button.show ();
+       top_box.show ();
+       low_box.show ();
+
+       _activating_from_app = false;
        cocoa_parent = 0;
        cocoa_window = 0;
+       au_view = 0;
+       packView = 0;
 
-       test_view_support (has_carbon, has_cocoa);
+       /* prefer cocoa, fall back to cocoa, but use carbon if its there */
 
-       if (has_cocoa) {
+       if (test_cocoa_view_support()) {
                create_cocoa_view ();
-       } else if (has_carbon) {
-               create_carbon_view (has_carbon);
+       } else if (test_carbon_view_support()) {
+               create_carbon_view ();
        } else {
-               /* fallback to cocoa */
                create_cocoa_view ();
        }
-}
 
+       low_box.signal_realize().connect (mem_fun (this, &AUPluginUI::lower_box_realized));
+}
 
 AUPluginUI::~AUPluginUI ()
 {
-       if (carbon_parented) {
+       if (cocoa_parent) {
                NSWindow* win = get_nswindow();
                RemoveEventHandler(carbon_event_handler);
                [win removeChildWindow:cocoa_parent];
+       } else if (carbon_window) {
+               /* never parented */
+               DisposeWindow (carbon_window);
        }
-}
 
-void
-AUPluginUI::test_view_support (bool& has_carbon, bool& has_cocoa)
-{
-       has_carbon = test_carbon_view_support();
-       has_cocoa = test_cocoa_view_support();
+       if (packView) {
+               /* remove whatever we packed into low_box so that GTK doesn't
+                  mess with it.
+               */
+
+               [packView removeFromSuperview];
+       }
 }
 
 bool
@@ -214,24 +283,24 @@ AUPluginUI::create_cocoa_view ()
        }
 
        if (!wasAbleToLoadCustomView) {
-               // [B] Otherwise show generic Cocoa view
+               // load generic Cocoa view
                au_view = [[AUGenericView alloc] initWithAudioUnit:*au->get_au()];
                [(AUGenericView *)au_view setShowsExpertParameters:YES];
        }
 
-       /* make a child cocoa window */
-
-       cocoa_window = [[NSWindow alloc] 
-                       initWithContentRect:crect
-                       styleMask:NSBorderlessWindowMask
-                       backing:NSBackingStoreBuffered
-                       defer:NO];
+       packView = au_view;
+       
+       // Get the size of the new AU View's frame 
+       NSRect packFrame;
+       packFrame = [au_view frame];
+       prefwidth = packFrame.size.width;
+       prefheight = packFrame.size.height;
 
        return 0;
 }
 
 int
-AUPluginUI::create_carbon_view (bool generic)
+AUPluginUI::create_carbon_view ()
 {
        OSStatus err;
        ControlRef root_control;
@@ -275,14 +344,13 @@ AUPluginUI::create_carbon_view (bool generic)
        GetControlBounds(viewPane, &bounds);
        size.x = bounds.right-bounds.left;
        size.y = bounds.bottom-bounds.top;
-       SizeWindow(carbon_window, (short) (size.x + 0.5), (short) (size.y + 0.5),  true);
 
        prefwidth = (int) (size.x + 0.5);
        prefheight = (int) (size.y + 0.5);
 
-#if 0  
-       mViewPaneResizer->WantEventTypes (GetControlEventTarget(mAUViewPane), GetEventTypeCount(resizeEvent), resizeEvent);
-#endif
+       SizeWindow (carbon_window, prefwidth, prefheight,  true);
+       low_box.set_size_request (prefwidth, prefheight);
+
        return 0;
 }
 
@@ -309,19 +377,23 @@ AUPluginUI::get_nswindow ()
 void
 AUPluginUI::activate ()
 {
-       NSWindow* win = get_nswindow ();
-       [win setLevel:NSFloatingWindowLevel];
-       
-       if (carbon_parented) {
-               [cocoa_parent makeKeyAndOrderFront:nil];
+       if (carbon_window && cocoa_parent) {
+               cerr << "APP activated, activate carbon window " << insert->name() << endl;
+               _activating_from_app = true;
                ActivateWindow (carbon_window, TRUE);
+               _activating_from_app = false;
+               [cocoa_parent makeKeyAndOrderFront:nil];
        } 
 }
 
 void
 AUPluginUI::deactivate ()
 {
-       /* nothing to do here */
+       return;
+       cerr << "APP DEactivated, for " << insert->name() << endl;
+       _activating_from_app = true;
+       ActivateWindow (carbon_window, FALSE);
+       _activating_from_app = false;
 }
 
 
@@ -334,21 +406,34 @@ _carbon_event (EventHandlerCallRef nextHandlerRef, EventRef event, void *userDat
 OSStatus 
 AUPluginUI::carbon_event (EventHandlerCallRef nextHandlerRef, EventRef event)
 {
+       cerr << "CARBON EVENT\n";
+
        UInt32 eventKind = GetEventKind(event);
        ClickActivationResult howToHandleClick;
        NSWindow* win = get_nswindow ();
 
+       cerr << "window " << win << " carbon event type " << eventKind << endl;
+
        switch (eventKind) {
        case kEventWindowHandleActivate:
+               cerr << "carbon window for " << insert->name() << " activated\n";
+               if (_activating_from_app) {
+                       cerr << "app activation, ignore window activation\n";
+                       return noErr;
+               }
                [win makeMainWindow];
                return eventNotHandledErr;
                break;
 
        case kEventWindowHandleDeactivate:
-               return eventNotHandledErr;
+               cerr << "carbon window for " << insert->name() << " would have been deactivated\n";
+               // never deactivate the carbon window
+               return noErr;
                break;
                
        case kEventWindowGetClickActivation:
+               cerr << "carbon window CLICK activated\n";
+               [win makeKeyAndOrderFront:nil];
                howToHandleClick = kActivateAndHandleClick;
                SetEventParameter(event, kEventParamClickActivation, typeClickActivationResult, 
                                  sizeof(ClickActivationResult), &howToHandleClick);
@@ -386,7 +471,9 @@ AUPluginUI::parent_carbon_window ()
 
        int titlebar_height = wm_frame.size.height - content_frame.size.height;
 
-       MoveWindow (carbon_window, x, y + titlebar_height, false);
+       int packing_extra = 6; // this is the total vertical packing in our top level window
+
+       MoveWindow (carbon_window, x, y + titlebar_height + top_box.get_height() + packing_extra, false);
        ShowWindow (carbon_window);
 
        // create the cocoa window for the carbon one and make it visible
@@ -394,8 +481,7 @@ AUPluginUI::parent_carbon_window ()
 
        EventTypeSpec   windowEventTypes[] = {
                {kEventClassWindow, kEventWindowGetClickActivation },
-               {kEventClassWindow, kEventWindowHandleDeactivate },
-               {kEventClassWindow, kEventWindowHandleActivate }
+               {kEventClassWindow, kEventWindowHandleDeactivate }
        };
        
        EventHandlerUPP   ehUPP = NewEventHandlerUPP(_carbon_event);
@@ -407,11 +493,7 @@ AUPluginUI::parent_carbon_window ()
        }
 
        [win addChildWindow:cocoa_parent ordered:NSWindowAbove];
-       [win setLevel:NSFloatingWindowLevel];
-       [win setHidesOnDeactivate:YES];
 
-       carbon_parented = true;
-               
        return 0;
 }      
 
@@ -419,11 +501,14 @@ int
 AUPluginUI::parent_cocoa_window ()
 {
        NSWindow* win = get_nswindow ();
+       NSRect packFrame;
 
        if (!win) {
                return -1;
        }
 
+       [win setAutodisplay:YES]; // turn of GTK stuff for this window
+
        Gtk::Container* toplevel = get_toplevel();
 
        if (!toplevel || !toplevel->is_toplevel()) {
@@ -432,84 +517,15 @@ AUPluginUI::parent_cocoa_window ()
        }
        
        // Get the size of the new AU View's frame 
-       NSRect au_view_frame = [au_view frame];
+       packFrame = [au_view frame];
 
-       if (au_view_frame.size.width > 500 || au_view_frame.size.height > 500) {
-               
-               /* its too big - use a scrollview */
-
-               NSRect frameRect = [[cocoa_window contentView] frame];
-               scroll_view = [[[NSScrollView alloc] initWithFrame:frameRect] autorelease];
-               [scroll_view setDrawsBackground:NO];
-               [scroll_view setHasHorizontalScroller:YES];
-               [scroll_view setHasVerticalScroller:YES];
-
-               NSSize frameSize = [NSScrollView  frameSizeForContentSize:au_view_frame.size
-                                   hasHorizontalScroller:[scroll_view hasHorizontalScroller]
-                                   hasVerticalScroller:[scroll_view hasVerticalScroller]
-                                   borderType:[scroll_view borderType]];
-               
-               // Create a new frame with same origin as current
-               // frame but size equal to the size of the new view
-               NSRect newFrame;
-               newFrame.origin = [scroll_view frame].origin;
-               newFrame.size = frameSize;
-               
-               // Set the new frame and document views on the scroll view
-               NSRect currentFrame = [scroll_view frame];
-               [scroll_view setFrame:newFrame];
-               [scroll_view setDocumentView:au_view];
-               
-               cerr << "scroll view size is " << newFrame.size.width << " x " << newFrame.size.height << endl;
-               
-               NSSize oldContentSize = [[cocoa_window contentView] frame].size;
-               NSSize newContentSize = oldContentSize;
-               
-               cerr << "original size is " << newContentSize.width << " x " << newContentSize.height << endl;
-               
-               newContentSize.width += (newFrame.size.width - currentFrame.size.width);
-               newContentSize.height += (newFrame.size.height - currentFrame.size.height);
-               
-               [cocoa_window setContentSize:newContentSize];
-               [cocoa_window setContentView:scroll_view];
-               
-       } else {
-
-               [cocoa_window setContentSize:au_view_frame.size];
-               [cocoa_window setContentView:au_view];
-
-       }
-
-       /* compute how tall the title bar is, because we have to offset the position of the child window
-          by that much.
-       */
-
-       NSRect content_frame = [NSWindow contentRectForFrameRect:[win frame] styleMask:[win styleMask]];
-       NSRect wm_frame = [NSWindow frameRectForContentRect:content_frame styleMask:[win styleMask]];
-       int titlebar_height = wm_frame.size.height - content_frame.size.height;
-
-       // move cocoa window into position relative to the toplevel window
-
-       NSRect view_frame = [[cocoa_window contentView] frame];
-       view_frame.origin.x = content_frame.origin.x;
-       view_frame.origin.y = content_frame.origin.y;
-
-       [cocoa_window setFrame:view_frame display:NO];
-
-       /* make top level window big enough to hold cocoa window and titlebar */
+       NSView* view = gdk_quartz_window_get_nsview (low_box.get_window()->gobj());
        
-       content_frame.size.width = view_frame.size.width;
-       content_frame.size.height = view_frame.size.height + titlebar_height;
 
-       [win setFrame:content_frame display:NO];
+       [view setFrame:packFrame];
+       [view addSubview:packView]; 
 
-       /* now make cocoa window a child of this top level */
-
-       [win addChildWindow:cocoa_window ordered:NSWindowAbove];
-       // [win setLevel:NSFloatingWindowLevel];
-       [win setHidesOnDeactivate:YES];
-
-       cocoa_parented = true;
+       low_box.set_size_request (packFrame.size.width, packFrame.size.height);
 
        return 0;
 }
@@ -519,19 +535,37 @@ AUPluginUI::on_realize ()
 {
        VBox::on_realize ();
 
-       if (cocoa_window) {
-               
-               if (parent_cocoa_window ()) {
-               }
+       /* our windows should not have that resize indicator */
 
-       } else if (carbon_window) {
+       NSWindow* win = get_nswindow ();
+       if (win) {
+               [win setShowsResizeIndicator:NO];
+       }
+}
 
-               if (parent_carbon_window ()) {
-                       // ShowWindow (carbon_window);
-               }
+void
+AUPluginUI::lower_box_realized ()
+{
+       if (au_view) {
+               parent_cocoa_window ();
+       } else if (carbon_window) {
+               parent_carbon_window ();
        }
 }
 
+void
+AUPluginUI::on_hide ()
+{
+       // VBox::on_hide ();
+       cerr << "AU plugin window hidden\n";
+}
+
+bool
+AUPluginUI::on_map_event (GdkEventAny* ev)
+{
+       return false;
+}
+
 void
 AUPluginUI::on_show ()
 {
@@ -539,10 +573,13 @@ AUPluginUI::on_show ()
 
        VBox::on_show ();
 
-       if (cocoa_window) {
-               [cocoa_window setIsVisible:YES];
+       gtk_widget_realize (GTK_WIDGET(low_box.gobj()));
+
+       if (au_view) {
+               show_all ();
        } else if (carbon_window) {
                [cocoa_parent setIsVisible:YES];
+               ShowWindow (carbon_window);
        }
 }
 
@@ -565,3 +602,20 @@ create_au_gui (boost::shared_ptr<PluginInsert> plugin_insert, VBox** box)
        (*box) = aup;
        return aup;
 }
+
+bool
+AUPluginUI::on_focus_in_event (GdkEventFocus* ev)
+{
+       //cerr << "au plugin focus in\n";
+       //Keyboard::magic_widget_grab_focus ();
+       return false;
+}
+
+bool
+AUPluginUI::on_focus_out_event (GdkEventFocus* ev)
+{
+       //cerr << "au plugin focus out\n";
+       //Keyboard::magic_widget_drop_focus ();
+       return false;
+}
+