Basic region naming test.
[ardour.git] / gtk2_ardour / au_pluginui.mm
index 51f73199f75bd045436b2904ce1fa8d2dfd4fb44..359b32478aef7ab14fcc509ff74dc2acfc0892f6 100644 (file)
-#include <pbd/error.h>
-#include <ardour/audio_unit.h>
-#include <ardour/insert.h>
+#include <gtkmm/stock.h>
 
+#undef  Marker
+#define Marker FuckYouAppleAndYourLackOfNameSpaces
+
+#include "pbd/convert.h"
+#include "pbd/error.h"
+
+#include "ardour/audio_unit.h"
+#include "ardour/debug.h"
+#include "ardour/plugin_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"
 
-#include <appleutility/CAAudioUnit.h>
-#include <appleutility/CAComponent.h>
+#include "appleutility/CAAudioUnit.h"
+#include "appleutility/CAComponent.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
+};
+
+@implementation NotificationObject
+
+- (NotificationObject*) initWithPluginUI: (AUPluginUI*) apluginui andCocoaParent: (NSWindow*) cp andTopLevelParent: (NSWindow*) tlp
+{
+       self = [ super init ];
+
+       if (self) {
+               plugin_ui = apluginui; 
+               top_level_parent = tlp;
+                
+                if (cp) {
+                        cocoa_parent = cp;
+                        
+                        [[NSNotificationCenter defaultCenter] addObserver:self
+                         selector:@selector(cocoaParentActivationHandler:)
+                         name:NSWindowDidBecomeMainNotification
+                         object:nil];
+                        
+                        [[NSNotificationCenter defaultCenter] addObserver:self
+                         selector:@selector(cocoaParentBecameKeyHandler:)
+                         name:NSWindowDidBecomeKeyNotification
+                         object:nil];
+                }
+        }
+
+       return self;
+}
+               
+- (void)cocoaParentActivationHandler:(NSNotification *)notification
+{
+       NSWindow* notification_window = (NSWindow *)[notification object];
+
+       if (top_level_parent == notification_window || cocoa_parent == notification_window) {
+               if ([notification_window isMainWindow]) {
+                       plugin_ui->activate();
+               } else {
+                       plugin_ui->deactivate();
+               }
+       } 
+}
+
+- (void)cocoaParentBecameKeyHandler:(NSNotification *)notification
+{
+       NSWindow* notification_window = (NSWindow *)[notification object];
+
+       if (top_level_parent == notification_window || cocoa_parent == notification_window) {
+               if ([notification_window isKeyWindow]) {
+                       plugin_ui->activate();
+               } else {
+                       plugin_ui->deactivate();
+               }
+       } 
+}
+
+- (void)auViewResized:(NSNotification *)notification;
+{
+        (void) notification; // stop complaints about unusued argument
+       plugin_ui->cocoa_view_resized();
+}
+
+@end
 
 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);
 
-       carbon_parented = false;
-       cocoa_parented = false;
+       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);
+#if 0
+       /* one day these might be useful with an AU plugin, but not yet */
+       smaller_hbox->pack_start (automation_mode_label, false, false);
+       smaller_hbox->pack_start (automation_mode_selector, false, false);
+#endif
+       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;
+       _notify = 0;
        cocoa_window = 0;
+       carbon_window = 0;
+       au_view = 0;
+       editView = 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);
+#ifdef WITH_CARBON
+       } else if (test_carbon_view_support()) {
+               create_carbon_view ();
+#endif
        } 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 (_notify) {
+                [[NSNotificationCenter defaultCenter] removeObserver:_notify];
+        }
+
+       if (cocoa_parent) {
                NSWindow* win = get_nswindow();
-               RemoveEventHandler(carbon_event_handler);
                [win removeChildWindow:cocoa_parent];
+       } 
+
+#ifdef WITH_CARBON
+       if (carbon_window) {
+               /* not parented, just overlaid on top of our window */
+               DisposeWindow (carbon_window);
        }
-}
+#endif
 
-void
-AUPluginUI::test_view_support (bool& has_carbon, bool& has_cocoa)
-{
-       has_carbon = test_carbon_view_support();
-       has_cocoa = test_cocoa_view_support();
+       if (editView) {
+               CloseComponent (editView);
+       }
+
+       if (au_view) {
+               /* remove whatever we packed into low_box so that GTK doesn't
+                  mess with it.
+               */
+
+               [au_view removeFromSuperview];
+       }
 }
 
 bool
@@ -133,7 +288,7 @@ AUPluginUI::create_cocoa_view ()
        UInt32     dataSize;
        Boolean    isWritable;
        NSString*           factoryClassName = 0;
-       NSURL*              CocoaViewBundlePath;
+       NSURL*              CocoaViewBundlePath = NULL;
 
        OSStatus result = AudioUnitGetPropertyInfo (*au->get_au(),
                                                    kAudioUnitProperty_CocoaUI,
@@ -143,11 +298,16 @@ AUPluginUI::create_cocoa_view ()
                                                    &isWritable );
 
        numberOfClasses = (dataSize - sizeof(CFURLRef)) / sizeof(CFStringRef);
-       
+
        // Does view have custom Cocoa UI?
        
        if ((result == noErr) && (numberOfClasses > 0) ) {
+
+               DEBUG_TRACE(DEBUG::AudioUnits,
+                           string_compose ( "based on %1, there are %2 cocoa UI classes\n", dataSize, numberOfClasses));
+
                cocoaViewInfo = (AudioUnitCocoaViewInfo *)malloc(dataSize);
+
                if(AudioUnitGetProperty(*au->get_au(),
                                        kAudioUnitProperty_CocoaUI,
                                        kAudioUnitScope_Global,
@@ -156,12 +316,17 @@ AUPluginUI::create_cocoa_view ()
                                        &dataSize) == noErr) {
 
                        CocoaViewBundlePath     = (NSURL *)cocoaViewInfo->mCocoaAUViewBundleLocation;
-                       
+                               
                        // we only take the first view in this example.
                        factoryClassName        = (NSString *)cocoaViewInfo->mCocoaAUViewClass[0];
-
+                       
+                       DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("the factory name is %1 bundle is %2\n",
+                                                                       [factoryClassName UTF8String], CocoaViewBundlePath));
+                        
                } else {
 
+                       DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("No cocoaUI property cocoaViewInfo = %1\n", cocoaViewInfo));
+
                        if (cocoaViewInfo != NULL) {
                                free (cocoaViewInfo);
                                cocoaViewInfo = NULL;
@@ -169,17 +334,19 @@ AUPluginUI::create_cocoa_view ()
                }
        }
 
-       NSRect crect = { { 0, 0 }, { 1, 1} };
-
        // [A] Show custom UI if view has it
 
        if (CocoaViewBundlePath && factoryClassName) {
                NSBundle *viewBundle    = [NSBundle bundleWithPath:[CocoaViewBundlePath path]];
+
+               DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("tried to create bundle, result = %1\n", viewBundle));
+
                if (viewBundle == nil) {
                        error << _("AUPluginUI: error loading AU view's bundle") << endmsg;
                        return -1;
                } else {
                        Class factoryClass = [viewBundle classNamed:factoryClassName];
+                       DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("tried to create factory class, result = %1\n", factoryClass));
                        if (!factoryClass) {
                                error << _("AUPluginUI: error getting AU view's factory class from bundle") << endmsg;
                                return -1;
@@ -191,14 +358,18 @@ AUPluginUI::create_cocoa_view ()
                                return -1;
                        }
                        // make a factory
-                       id factoryInstance = [[[factoryClass alloc] init] autorelease];
-                       if (factoryInstance == nil) {
+                       id factory = [[[factoryClass alloc] init] autorelease];
+                       if (factory == nil) {
                                error << _("AUPluginUI: Could not create an instance of the AU view factory") << endmsg;
                                return -1;
                        }
 
+                       DEBUG_TRACE (DEBUG::AudioUnits, "got a factory instance\n");
+
                        // make a view
-                       au_view = [factoryInstance uiViewForAudioUnit:*au->get_au() withSize:crect.size];
+                       au_view = [factory uiViewForAudioUnit:*au->get_au() withSize:NSZeroSize];
+
+                       DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("view created @ %1\n", au_view));
                        
                        // cleanup
                        [CocoaViewBundlePath release];
@@ -214,25 +385,60 @@ AUPluginUI::create_cocoa_view ()
        }
 
        if (!wasAbleToLoadCustomView) {
-               // [B] Otherwise show generic Cocoa view
+               // load generic Cocoa view
+               DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("Loading generic view using %1 -> %2\n", au,
+                                                               au->get_au()));
                au_view = [[AUGenericView alloc] initWithAudioUnit:*au->get_au()];
+               DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("view created @ %1\n", au_view));
                [(AUGenericView *)au_view setShowsExpertParameters:YES];
        }
 
-       /* make a child cocoa window */
-
-       cocoa_window = [[NSWindow alloc] 
-                       initWithContentRect:crect
-                       styleMask:NSBorderlessWindowMask
-                       backing:NSBackingStoreBuffered
-                       defer:NO];
-
+       // Get the initial size of the new AU View's frame 
+       
+       NSRect rect = [au_view frame];
+       low_box.set_size_request (rect.size.width, rect.size.height);
+       
        return 0;
 }
 
+void
+AUPluginUI::cocoa_view_resized ()
+{
+        GtkRequisition topsize = top_box.size_request ();
+        NSWindow* window = get_nswindow ();
+        NSSize oldContentSize= [window contentRectForFrameRect:[window frame]].size;
+        NSSize newContentSize= [au_view frame].size;
+        NSRect windowFrame= [window frame];
+        
+        oldContentSize.height -= topsize.height;
+
+        float dy = oldContentSize.height - newContentSize.height;
+        float dx = oldContentSize.width - newContentSize.width;
+
+        windowFrame.origin.y    += dy;
+        windowFrame.origin.x    += dx;
+        windowFrame.size.height -= dy;
+        windowFrame.size.width  -= dx;
+        
+        [[NSNotificationCenter defaultCenter] removeObserver:_notify
+         name:NSViewFrameDidChangeNotification 
+         object:au_view];
+
+        NSUInteger old_auto_resize = [au_view autoresizingMask];
+
+        [au_view setAutoresizingMask:NSViewNotSizable];
+        [window setFrame:windowFrame display:YES];
+        [au_view setAutoresizingMask:old_auto_resize];
+
+        [[NSNotificationCenter defaultCenter] addObserver:_notify
+         selector:@selector(auViewResized:) name:NSViewFrameDidChangeNotification
+         object:au_view];
+}
+
 int
-AUPluginUI::create_carbon_view (bool generic)
+AUPluginUI::create_carbon_view ()
 {
+#ifdef WITH_CARBON
        OSStatus err;
        ControlRef root_control;
 
@@ -252,11 +458,14 @@ AUPluginUI::create_carbon_view (bool generic)
 
        if ((err = CreateNewWindow(kDocumentWindowClass, attr, &r, &carbon_window)) != noErr) {
                error << string_compose (_("AUPluginUI: cannot create carbon window (err: %1)"), err) << endmsg;
+               CloseComponent (editView);
                return -1;
        }
        
        if ((err = GetRootControl(carbon_window, &root_control)) != noErr) {
                error << string_compose (_("AUPlugin: cannot get root control of carbon window (err: %1)"), err) << endmsg;
+               DisposeWindow (carbon_window);
+               CloseComponent (editView);
                return -1;
        }
 
@@ -266,6 +475,8 @@ AUPluginUI::create_carbon_view (bool generic)
 
        if ((err = AudioUnitCarbonViewCreate (editView, *au->get_au(), carbon_window, root_control, &location, &size, &viewPane)) != noErr) {
                error << string_compose (_("AUPluginUI: cannot create carbon plugin view (err: %1)"), err) << endmsg;
+               DisposeWindow (carbon_window);
+               CloseComponent (editView);
                return -1;
        }
 
@@ -275,15 +486,18 @@ 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;
+#else
+       error << _("AU Carbon GUI is not supported.") << endmsg;
+       return -1;
+#endif
 }
 
 NSWindow*
@@ -309,58 +523,24 @@ AUPluginUI::get_nswindow ()
 void
 AUPluginUI::activate ()
 {
-       NSWindow* win = get_nswindow ();
-       [win setLevel:NSFloatingWindowLevel];
-       
-       if (carbon_parented) {
-               [cocoa_parent makeKeyAndOrderFront:nil];
-               ActivateWindow (carbon_window, TRUE);
-       } 
+#ifdef WITH_CARBON
+       ActivateWindow (carbon_window, TRUE);
+#endif
+       // [cocoa_parent makeKeyAndOrderFront:nil];
 }
 
 void
 AUPluginUI::deactivate ()
 {
-       /* nothing to do here */
-}
-
-
-OSStatus 
-_carbon_event (EventHandlerCallRef nextHandlerRef, EventRef event, void *userData) 
-{
-       return ((AUPluginUI*)userData)->carbon_event (nextHandlerRef, event);
-}
-
-OSStatus 
-AUPluginUI::carbon_event (EventHandlerCallRef nextHandlerRef, EventRef event)
-{
-       UInt32 eventKind = GetEventKind(event);
-       ClickActivationResult howToHandleClick;
-       NSWindow* win = get_nswindow ();
-
-       switch (eventKind) {
-       case kEventWindowHandleActivate:
-               [win makeMainWindow];
-               return eventNotHandledErr;
-               break;
-
-       case kEventWindowHandleDeactivate:
-               return eventNotHandledErr;
-               break;
-               
-       case kEventWindowGetClickActivation:
-               howToHandleClick = kActivateAndHandleClick;
-               SetEventParameter(event, kEventParamClickActivation, typeClickActivationResult, 
-                                 sizeof(ClickActivationResult), &howToHandleClick);
-               break;
-       }
-
-       return noErr;
+#ifdef WITH_CARBON
+       ActivateWindow (carbon_window, FALSE);
+#endif
 }
 
 int
 AUPluginUI::parent_carbon_window ()
 {
+#ifdef WITH_CARBON
        NSWindow* win = get_nswindow ();
        int x, y;
 
@@ -386,33 +566,24 @@ 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
        cocoa_parent = [[NSWindow alloc] initWithWindowRef: carbon_window];
 
-       EventTypeSpec   windowEventTypes[] = {
-               {kEventClassWindow, kEventWindowGetClickActivation },
-               {kEventClassWindow, kEventWindowHandleDeactivate },
-               {kEventClassWindow, kEventWindowHandleActivate }
-       };
-       
-       EventHandlerUPP   ehUPP = NewEventHandlerUPP(_carbon_event);
-       OSStatus result = InstallWindowEventHandler (carbon_window, ehUPP, 
-                                                    sizeof(windowEventTypes) / sizeof(EventTypeSpec), 
-                                                    windowEventTypes, this, &carbon_event_handler);
-       if (result != noErr) {
-               return -1;
-       }
+       SetWindowActivationScope (carbon_window, kWindowActivationScopeNone);
+
+       _notify = [ [NotificationObject alloc] initWithPluginUI:this andCocoaParent:cocoa_parent andTopLevelParent:win ]; 
 
        [win addChildWindow:cocoa_parent ordered:NSWindowAbove];
-       [win setLevel:NSFloatingWindowLevel];
-       [win setHidesOnDeactivate:YES];
 
-       carbon_parented = true;
-               
        return 0;
+#else
+       return -1;
+#endif
 }      
 
 int
@@ -424,94 +595,73 @@ AUPluginUI::parent_cocoa_window ()
                return -1;
        }
 
+       [win setAutodisplay:YES]; // turn of GTK stuff for this window
+
        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 
-       NSRect au_view_frame = [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 {
+       NSView* view = gdk_quartz_window_get_nsview (get_toplevel()->get_window()->gobj());
+       GtkRequisition a = top_box.size_request ();
 
-               [cocoa_window setContentSize:au_view_frame.size];
-               [cocoa_window setContentView:au_view];
+       /* move the au_view down so that it doesn't overlap the top_box contents */
 
-       }
+       NSPoint origin = { 0, a.height };
 
-       /* compute how tall the title bar is, because we have to offset the position of the child window
-          by that much.
-       */
+       [au_view setFrameOrigin:origin];
+       [view addSubview:au_view positioned:NSWindowBelow relativeTo:nil]; 
 
-       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;
+       // watch for size changes of the view
 
-       // move cocoa window into position relative to the toplevel window
+       _notify = [ [NotificationObject alloc] initWithPluginUI:this andCocoaParent:nil andTopLevelParent:win ]; 
 
-       NSRect view_frame = [[cocoa_window contentView] frame];
-       view_frame.origin.x = content_frame.origin.x;
-       view_frame.origin.y = content_frame.origin.y;
+        [[NSNotificationCenter defaultCenter] addObserver:_notify
+         selector:@selector(auViewResized:) name:NSViewFrameDidChangeNotification
+         object:au_view];
 
-       [cocoa_window setFrame:view_frame display:NO];
+       return 0;
+}
 
-       /* make top level window big enough to hold cocoa window and titlebar */
-       
-       content_frame.size.width = view_frame.size.width;
-       content_frame.size.height = view_frame.size.height + titlebar_height;
+static void
+dump_view_tree (NSView* view, int depth)
+{
+       NSArray* subviews = [view subviews];
+       unsigned long cnt = [subviews count];
 
-       [win setFrame:content_frame display:NO];
+       for (int d = 0; d < depth; d++) {
+               cerr << '\t';
+       }
+       cerr << " view @ " << view << endl;
+       
+       for (unsigned long i = 0; i < cnt; ++i) {
+               NSView* subview = [subviews objectAtIndex:i];
+               dump_view_tree (subview, depth+1);
+       }
+}
 
-       /* now make cocoa window a child of this top level */
+void
+AUPluginUI::forward_key_event (GdkEventKey* ev)
+{
+       NSEvent* nsevent = gdk_quartz_event_get_nsevent ((GdkEvent*)ev);
 
-       [win addChildWindow:cocoa_window ordered:NSWindowAbove];
-       // [win setLevel:NSFloatingWindowLevel];
-       [win setHidesOnDeactivate:YES];
+       if (au_view && nsevent) {
 
-       cocoa_parented = true;
+               /* filter on nsevent type here because GDK massages FlagsChanged
+                  messages into GDK_KEY_{PRESS,RELEASE} but Cocoa won't
+                  handle a FlagsChanged message as a keyDown or keyUp
+               */
 
-       return 0;
+               if ([nsevent type] == NSKeyDown) {
+                       [[[au_view window] firstResponder] keyDown:nsevent];
+               } else if ([nsevent type] == NSKeyUp) {
+                       [[[au_view window] firstResponder] keyUp:nsevent];
+               } else if ([nsevent type] == NSFlagsChanged) {
+                       [[[au_view window] firstResponder] flagsChanged:nsevent];
+               }
+       }
 }
 
 void
@@ -519,41 +669,70 @@ 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 ();
        }
 }
 
+bool
+AUPluginUI::on_map_event (GdkEventAny*)
+{
+       return false;
+}
+
 void
-AUPluginUI::on_show ()
+AUPluginUI::on_window_hide ()
+{
+#ifdef WITH_CARBON
+       if (carbon_window) {
+               HideWindow (carbon_window);
+               ActivateWindow (carbon_window, FALSE);
+       }
+#endif
+
+       hide_all ();
+}
+
+bool
+AUPluginUI::on_window_show (const string& /*title*/)
 {
-       cerr << "AU plugin window shown\n";
+       /* this is idempotent so just call it every time we show the window */
 
-       VBox::on_show ();
+       gtk_widget_realize (GTK_WIDGET(low_box.gobj()));
 
-       if (cocoa_window) {
-               [cocoa_window setIsVisible:YES];
-       } else if (carbon_window) {
-               [cocoa_parent setIsVisible:YES];
+       show_all ();
+
+#ifdef WITH_CARBON
+       if (carbon_window) {
+               ShowWindow (carbon_window);
+               ActivateWindow (carbon_window, TRUE);
        }
+#endif
+
+       return true;
 }
 
 bool
-AUPluginUI::start_updating (GdkEventAny* any)
+AUPluginUI::start_updating (GdkEventAny*)
 {
        return false;
 }
 
 bool
-AUPluginUI::stop_updating (GdkEventAny* any)
+AUPluginUI::stop_updating (GdkEventAny*)
 {
        return false;
 }
@@ -565,3 +744,20 @@ create_au_gui (boost::shared_ptr<PluginInsert> plugin_insert, VBox** box)
        (*box) = aup;
        return aup;
 }
+
+bool
+AUPluginUI::on_focus_in_event (GdkEventFocus*)
+{
+       //cerr << "au plugin focus in\n";
+       //Keyboard::magic_widget_grab_focus ();
+       return false;
+}
+
+bool
+AUPluginUI::on_focus_out_event (GdkEventFocus*)
+{
+       //cerr << "au plugin focus out\n";
+       //Keyboard::magic_widget_drop_focus ();
+       return false;
+}
+