always compute range for a redirect automation line, even if it will not be drawn...
[ardour.git] / gtk2_ardour / au_pluginui.mm
index e1d7302b609de13f59ff7da3fbb794f4621ea16b..205364b34348168f357d2517f71b2f693403516d 100644 (file)
@@ -1,9 +1,17 @@
+#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;
-       
-       carbon_parented = false;
-       cocoa_parented = false;
+       /* stuff some stuff into the top of the window */
+
+       HBox* smaller_hbox = manage (new HBox);
+
+       smaller_hbox->set_spacing (6);
+       smaller_hbox->pack_start (preset_label, false, false, 4);
+       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);
+
+       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);
-
-       cerr << "plugin has carbon ? " << has_carbon <<  " cocoa ? " << has_cocoa << endl;
+       /* 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 (test_carbon_view_support()) {
+               create_carbon_view ();
        } else {
-               create_carbon_view (has_carbon);
+               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 && packView != au_view) {
+               [packView release];
+       }
 }
 
 bool
 AUPluginUI::test_carbon_view_support ()
 {
-       bool ret = true; // there is always the generic GUI
+       bool ret = false;
        
        carbon_descriptor.componentType = kAudioUnitCarbonViewComponentType;
        carbon_descriptor.componentSubType = 'gnrc';
@@ -126,7 +193,6 @@ AUPluginUI::plugin_class_valid (Class pluginClass)
 int
 AUPluginUI::create_cocoa_view ()
 {
-       NSView *AUView = nil;
        BOOL wasAbleToLoadCustomView = NO;
        AudioUnitCocoaViewInfo* cocoaViewInfo = NULL;
        UInt32               numberOfClasses = 0;
@@ -142,11 +208,6 @@ AUPluginUI::create_cocoa_view ()
                                                    &dataSize,
                                                    &isWritable );
 
-
-       if (result != noErr) {
-               return -1;
-       }
-
        numberOfClasses = (dataSize - sizeof(CFURLRef)) / sizeof(CFStringRef);
        
        // Does view have custom Cocoa UI?
@@ -174,15 +235,7 @@ AUPluginUI::create_cocoa_view ()
                }
        }
 
-       cocoa_window = [NSWindow alloc];
-
-       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];
-       
-       [cocoa_window setContentView:scroll_view];
+       NSRect crect = { { 0, 0 }, { 1, 1} };
 
        // [A] Show custom UI if view has it
 
@@ -211,8 +264,7 @@ AUPluginUI::create_cocoa_view ()
                        }
 
                        // make a view
-                       AUView = [factoryInstance uiViewForAudioUnit:*au->get_au()
-                                                 withSize:[[scroll_view contentView] bounds].size];
+                       au_view = [factoryInstance uiViewForAudioUnit:*au->get_au() withSize:crect.size];
                        
                        // cleanup
                        [CocoaViewBundlePath release];
@@ -228,39 +280,59 @@ AUPluginUI::create_cocoa_view ()
        }
 
        if (!wasAbleToLoadCustomView) {
-               // [B] Otherwise show generic Cocoa view
-               AUView = [[AUGenericView alloc] initWithAudioUnit:*au->get_au()];
-               [(AUGenericView *)AUView setShowsExpertParameters:YES];
+               // load generic Cocoa view
+               au_view = [[AUGenericView alloc] initWithAudioUnit:*au->get_au()];
+               [(AUGenericView *)au_view setShowsExpertParameters:YES];
        }
-       
-       // Display view
-       NSRect viewFrame = [AUView frame];
-       NSSize frameSize = [NSScrollView  frameSizeForContentSize:viewFrame.size
-                           hasHorizontalScroller:[scroll_view hasHorizontalScroller]
-                           hasVerticalScroller:[scroll_view hasVerticalScroller]
-                           borderType:[scroll_view borderType]];
-       
 
-       NSRect newFrame;
-       newFrame.origin = [scroll_view frame].origin;
-       newFrame.size = frameSize;
+       NSRect packFrame;
 
-       NSRect currentFrame = [scroll_view frame];
-       [scroll_view setFrame:newFrame];
-       [scroll_view setDocumentView:AUView];
-       
-       NSSize oldContentSize = [[cocoa_window contentView] frame].size;
-       NSSize newContentSize = oldContentSize;
-       newContentSize.width += (newFrame.size.width - currentFrame.size.width);
-       newContentSize.height += (newFrame.size.height - currentFrame.size.height);
-       
-       [cocoa_window setContentSize:newContentSize];
+       // Get the size of the new AU View's frame 
+       packFrame = [au_view frame];
+
+       packFrame.origin.x = 0;
+       packFrame.origin.y = 0;
+
+       if (packFrame.size.width > 500 || packFrame.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];
+
+               packFrame.size = [NSScrollView  frameSizeForContentSize:packFrame.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 = packFrame.size;
+               
+               // Set the new frame and document views on the scroll view
+               [scroll_view setFrame:newFrame];
+               [scroll_view setDocumentView:au_view];
+               
+               packView = scroll_view;
+
+       } else {
+
+               packView = au_view;
+       }
+
+       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;
@@ -279,7 +351,7 @@ AUPluginUI::create_carbon_view (bool generic)
                                                  kWindowNoShadowAttribute|
                                                  kWindowNoTitleBarAttribute);
 
-       if ((err = CreateNewWindow(kFloatingWindowClass, attr, &r, &carbon_window)) != noErr) {
+       if ((err = CreateNewWindow(kDocumentWindowClass, attr, &r, &carbon_window)) != noErr) {
                error << string_compose (_("AUPluginUI: cannot create carbon window (err: %1)"), err) << endmsg;
                return -1;
        }
@@ -304,14 +376,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;
 }
 
@@ -335,6 +406,28 @@ AUPluginUI::get_nswindow ()
        return true_parent;
 }
 
+void
+AUPluginUI::activate ()
+{
+       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 ()
+{
+       return;
+       cerr << "APP DEactivated, for " << insert->name() << endl;
+       _activating_from_app = true;
+       ActivateWindow (carbon_window, FALSE);
+       _activating_from_app = false;
+}
+
 
 OSStatus 
 _carbon_event (EventHandlerCallRef nextHandlerRef, EventRef event, void *userData) 
@@ -345,29 +438,40 @@ _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 << "Carbon event for " << au->name();
+       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:
-               // don't allow window to get deactivated while app is active
-               // (do this only if you are a floating window that doesn't hide)
-               cerr << " deactivate!";
-               ActivateWindow(carbon_window, TRUE);
+               cerr << "carbon window for " << insert->name() << " would have been deactivated\n";
+               // never deactivate the carbon window
+               return noErr;
                break;
                
        case kEventWindowGetClickActivation:
-               cerr << " click activate!";
+               cerr << "carbon window CLICK activated\n";
+               [win makeKeyAndOrderFront:nil];
                howToHandleClick = kActivateAndHandleClick;
                SetEventParameter(event, kEventParamClickActivation, typeClickActivationResult, 
                                  sizeof(ClickActivationResult), &howToHandleClick);
                break;
        }
 
-       cerr << "\n";
-
        return noErr;
 }
 
@@ -399,7 +503,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
@@ -419,10 +525,7 @@ AUPluginUI::parent_carbon_window ()
        }
 
        [win addChildWindow:cocoa_parent ordered:NSWindowAbove];
-       [win setLevel:NSFloatingWindowLevel];
 
-       carbon_parented = true;
-               
        return 0;
 }      
 
@@ -430,15 +533,31 @@ int
 AUPluginUI::parent_cocoa_window ()
 {
        NSWindow* win = get_nswindow ();
+       NSRect packFrame;
 
        if (!win) {
                return -1;
        }
 
-       [win addChildWindow:cocoa_window ordered:NSWindowAbove];
-       [win setLevel:NSFloatingWindowLevel];
+       [win setAutodisplay:YES]; // turn of GTK stuff for this window
 
-       cocoa_parented = true;
+       Gtk::Container* toplevel = get_toplevel();
+
+       if (!toplevel || !toplevel->is_toplevel()) {
+               error << _("AUPluginUI: no top level window!") << endmsg;
+               return -1;
+       }
+       
+       // Get the size of the new AU View's frame 
+       packFrame = [au_view frame];
+
+       NSView* view = gdk_quartz_window_get_nsview (low_box.get_window()->gobj());
+       
+
+       [view setFrame:packFrame];
+       [view addSubview:packView]; 
+
+       low_box.set_size_request (packFrame.size.width, packFrame.size.height);
 
        return 0;
 }
@@ -448,19 +567,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 ()
 {
@@ -468,10 +605,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];
+               [cocoa_parent setIsVisible:YES];
+               ShowWindow (carbon_window);
        }
 }
 
@@ -494,3 +634,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;
+}
+