1 #include <gtkmm/stock.h>
4 #define Marker FuckYouAppleAndYourLackOfNameSpaces
6 #include "pbd/convert.h"
9 #include "ardour/audio_unit.h"
10 #include "ardour/debug.h"
11 #include "ardour/plugin_insert.h"
13 #undef check // stupid gtk, stupid apple
15 #include <gtkmm/button.h>
16 #include <gdk/gdkquartz.h>
18 #include <gtkmm2ext/utils.h>
20 #include "au_pluginui.h"
21 #include "gui_thread.h"
23 #include "appleutility/CAAudioUnit.h"
24 #include "appleutility/CAComponent.h"
26 #import <AudioUnit/AUCocoaUIView.h>
27 #import <CoreAudioKit/AUGenericView.h>
33 #include "public_editor.h"
36 using namespace ARDOUR;
38 using namespace Gtkmm2ext;
43 vector<string> AUPluginUI::automation_mode_strings;
45 static const gchar* _automation_mode_strings[] = {
53 @implementation NotificationObject
55 - (NotificationObject*) initWithPluginUI: (AUPluginUI*) apluginui andCocoaParent: (NSWindow*) cp andTopLevelParent: (NSWindow*) tlp
57 self = [ super init ];
60 plugin_ui = apluginui;
61 top_level_parent = tlp;
66 [[NSNotificationCenter defaultCenter] addObserver:self
67 selector:@selector(cocoaParentActivationHandler:)
68 name:NSWindowDidBecomeMainNotification
71 [[NSNotificationCenter defaultCenter] addObserver:self
72 selector:@selector(cocoaParentBecameKeyHandler:)
73 name:NSWindowDidBecomeKeyNotification
81 - (void)cocoaParentActivationHandler:(NSNotification *)notification
83 NSWindow* notification_window = (NSWindow *)[notification object];
85 if (top_level_parent == notification_window || cocoa_parent == notification_window) {
86 if ([notification_window isMainWindow]) {
87 plugin_ui->activate();
89 plugin_ui->deactivate();
94 - (void)cocoaParentBecameKeyHandler:(NSNotification *)notification
96 NSWindow* notification_window = (NSWindow *)[notification object];
98 if (top_level_parent == notification_window || cocoa_parent == notification_window) {
99 if ([notification_window isKeyWindow]) {
100 plugin_ui->activate();
102 plugin_ui->deactivate();
107 - (void)auViewResized:(NSNotification *)notification;
109 (void) notification; // stop complaints about unusued argument
110 plugin_ui->cocoa_view_resized();
115 AUPluginUI::AUPluginUI (boost::shared_ptr<PluginInsert> insert)
116 : PlugUIBase (insert)
117 , automation_mode_label (_("Automation"))
118 , preset_label (_("Presets"))
121 if (automation_mode_strings.empty()) {
122 automation_mode_strings = I18N (_automation_mode_strings);
125 set_popdown_strings (automation_mode_selector, automation_mode_strings);
126 automation_mode_selector.set_active_text (automation_mode_strings.front());
128 if ((au = boost::dynamic_pointer_cast<AUPlugin> (insert->plugin())) == 0) {
129 error << _("unknown type of editor-supplying plugin (note: no AudioUnit support in this version of ardour)") << endmsg;
130 throw failed_constructor ();
133 /* stuff some stuff into the top of the window */
135 HBox* smaller_hbox = manage (new HBox);
137 smaller_hbox->set_spacing (6);
138 smaller_hbox->pack_start (preset_label, false, false, 4);
139 smaller_hbox->pack_start (_preset_combo, false, false);
140 smaller_hbox->pack_start (save_button, false, false);
142 /* one day these might be useful with an AU plugin, but not yet */
143 smaller_hbox->pack_start (automation_mode_label, false, false);
144 smaller_hbox->pack_start (automation_mode_selector, false, false);
146 smaller_hbox->pack_start (bypass_button, false, true);
148 VBox* v1_box = manage (new VBox);
149 VBox* v2_box = manage (new VBox);
151 v1_box->pack_start (*smaller_hbox, false, true);
152 v2_box->pack_start (focus_button, false, true);
154 top_box.set_homogeneous (false);
155 top_box.set_spacing (6);
156 top_box.set_border_width (6);
158 top_box.pack_end (*v2_box, false, false);
159 top_box.pack_end (*v1_box, false, false);
162 pack_start (top_box, false, false);
163 pack_start (low_box, false, false);
165 preset_label.show ();
166 _preset_combo.show ();
167 automation_mode_label.show ();
168 automation_mode_selector.show ();
169 bypass_button.show ();
173 _activating_from_app = false;
181 /* prefer cocoa, fall back to cocoa, but use carbon if its there */
183 if (test_cocoa_view_support()) {
184 create_cocoa_view ();
186 } else if (test_carbon_view_support()) {
187 create_carbon_view ();
190 create_cocoa_view ();
193 low_box.signal_realize().connect (mem_fun (this, &AUPluginUI::lower_box_realized));
196 AUPluginUI::~AUPluginUI ()
199 NSWindow* win = get_nswindow();
200 [[NSNotificationCenter defaultCenter] removeObserver:_notify];
201 [win removeChildWindow:cocoa_parent];
207 /* not parented, just overlaid on top of our window */
208 DisposeWindow (carbon_window);
213 CloseComponent (editView);
217 /* remove whatever we packed into low_box so that GTK doesn't
221 [au_view removeFromSuperview];
226 AUPluginUI::test_carbon_view_support ()
230 carbon_descriptor.componentType = kAudioUnitCarbonViewComponentType;
231 carbon_descriptor.componentSubType = 'gnrc';
232 carbon_descriptor.componentManufacturer = 'appl';
233 carbon_descriptor.componentFlags = 0;
234 carbon_descriptor.componentFlagsMask = 0;
238 // ask the AU for its first editor component
240 err = AudioUnitGetPropertyInfo(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, &propertySize, NULL);
242 int nEditors = propertySize / sizeof(ComponentDescription);
243 ComponentDescription *editors = new ComponentDescription[nEditors];
244 err = AudioUnitGetProperty(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, editors, &propertySize);
246 // just pick the first one for now
247 carbon_descriptor = editors[0];
257 AUPluginUI::test_cocoa_view_support ()
260 Boolean isWritable = 0;
261 OSStatus err = AudioUnitGetPropertyInfo(*au->get_au(),
262 kAudioUnitProperty_CocoaUI, kAudioUnitScope_Global,
263 0, &dataSize, &isWritable);
265 return dataSize > 0 && err == noErr;
269 AUPluginUI::plugin_class_valid (Class pluginClass)
271 if([pluginClass conformsToProtocol: @protocol(AUCocoaUIBase)]) {
272 if([pluginClass instancesRespondToSelector: @selector(interfaceVersion)] &&
273 [pluginClass instancesRespondToSelector: @selector(uiViewForAudioUnit:withSize:)]) {
281 AUPluginUI::create_cocoa_view ()
283 BOOL wasAbleToLoadCustomView = NO;
284 AudioUnitCocoaViewInfo* cocoaViewInfo = NULL;
285 UInt32 numberOfClasses = 0;
288 NSString* factoryClassName = 0;
289 NSURL* CocoaViewBundlePath = NULL;
291 OSStatus result = AudioUnitGetPropertyInfo (*au->get_au(),
292 kAudioUnitProperty_CocoaUI,
293 kAudioUnitScope_Global,
298 numberOfClasses = (dataSize - sizeof(CFURLRef)) / sizeof(CFStringRef);
300 // Does view have custom Cocoa UI?
302 if ((result == noErr) && (numberOfClasses > 0) ) {
304 DEBUG_TRACE(DEBUG::AudioUnits,
305 string_compose ( "based on %1, there are %2 cocoa UI classes\n", dataSize, numberOfClasses));
307 cocoaViewInfo = (AudioUnitCocoaViewInfo *)malloc(dataSize);
309 if(AudioUnitGetProperty(*au->get_au(),
310 kAudioUnitProperty_CocoaUI,
311 kAudioUnitScope_Global,
314 &dataSize) == noErr) {
316 CocoaViewBundlePath = (NSURL *)cocoaViewInfo->mCocoaAUViewBundleLocation;
318 // we only take the first view in this example.
319 factoryClassName = (NSString *)cocoaViewInfo->mCocoaAUViewClass[0];
321 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("the factory name is %1 bundle is %2\n",
322 [factoryClassName UTF8String], CocoaViewBundlePath));
326 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("No cocoaUI property cocoaViewInfo = %1\n", cocoaViewInfo));
328 if (cocoaViewInfo != NULL) {
329 free (cocoaViewInfo);
330 cocoaViewInfo = NULL;
335 // [A] Show custom UI if view has it
337 if (CocoaViewBundlePath && factoryClassName) {
338 NSBundle *viewBundle = [NSBundle bundleWithPath:[CocoaViewBundlePath path]];
340 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("tried to create bundle, result = %1\n", viewBundle));
342 if (viewBundle == nil) {
343 error << _("AUPluginUI: error loading AU view's bundle") << endmsg;
346 Class factoryClass = [viewBundle classNamed:factoryClassName];
347 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("tried to create factory class, result = %1\n", factoryClass));
349 error << _("AUPluginUI: error getting AU view's factory class from bundle") << endmsg;
353 // make sure 'factoryClass' implements the AUCocoaUIBase protocol
354 if (!plugin_class_valid (factoryClass)) {
355 error << _("AUPluginUI: U view's factory class does not properly implement the AUCocoaUIBase protocol") << endmsg;
359 id factory = [[[factoryClass alloc] init] autorelease];
360 if (factory == nil) {
361 error << _("AUPluginUI: Could not create an instance of the AU view factory") << endmsg;
365 DEBUG_TRACE (DEBUG::AudioUnits, "got a factory instance\n");
368 au_view = [factory uiViewForAudioUnit:*au->get_au() withSize:NSZeroSize];
370 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("view created @ %1\n", au_view));
373 [CocoaViewBundlePath release];
376 for (i = 0; i < numberOfClasses; i++)
377 CFRelease(cocoaViewInfo->mCocoaAUViewClass[i]);
379 free (cocoaViewInfo);
381 wasAbleToLoadCustomView = YES;
385 if (!wasAbleToLoadCustomView) {
386 // load generic Cocoa view
387 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("Loading generic view using %1 -> %2\n", au,
389 au_view = [[AUGenericView alloc] initWithAudioUnit:*au->get_au()];
390 DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("view created @ %1\n", au_view));
391 [(AUGenericView *)au_view setShowsExpertParameters:YES];
394 // Get the initial size of the new AU View's frame
396 NSRect rect = [au_view frame];
397 low_box.set_size_request (rect.size.width, rect.size.height);
403 AUPluginUI::cocoa_view_resized ()
405 GtkRequisition topsize = top_box.size_request ();
406 NSWindow* window = get_nswindow ();
407 NSSize oldContentSize= [window contentRectForFrameRect:[window frame]].size;
408 NSSize newContentSize= [au_view frame].size;
409 NSRect windowFrame= [window frame];
411 oldContentSize.height -= topsize.height;
413 float dy = oldContentSize.height - newContentSize.height;
414 float dx = oldContentSize.width - newContentSize.width;
416 windowFrame.origin.y += dy;
417 windowFrame.origin.x += dx;
418 windowFrame.size.height -= dy;
419 windowFrame.size.width -= dx;
421 [[NSNotificationCenter defaultCenter] removeObserver:_notify
422 name:NSViewFrameDidChangeNotification
425 NSUInteger old_auto_resize = [au_view autoresizingMask];
427 [au_view setAutoresizingMask:NSViewNotSizable];
428 [window setFrame:windowFrame display:YES];
429 [au_view setAutoresizingMask:old_auto_resize];
431 [[NSNotificationCenter defaultCenter] addObserver:_notify
432 selector:@selector(auViewResized:) name:NSViewFrameDidChangeNotification
437 AUPluginUI::create_carbon_view ()
441 ControlRef root_control;
443 Component editComponent = FindNextComponent(NULL, &carbon_descriptor);
445 OpenAComponent(editComponent, &editView);
447 error << _("AU Carbon view: cannot open AU Component") << endmsg;
451 Rect r = { 100, 100, 100, 100 };
452 WindowAttributes attr = WindowAttributes (kWindowStandardHandlerAttribute |
453 kWindowCompositingAttribute|
454 kWindowNoShadowAttribute|
455 kWindowNoTitleBarAttribute);
457 if ((err = CreateNewWindow(kDocumentWindowClass, attr, &r, &carbon_window)) != noErr) {
458 error << string_compose (_("AUPluginUI: cannot create carbon window (err: %1)"), err) << endmsg;
459 CloseComponent (editView);
463 if ((err = GetRootControl(carbon_window, &root_control)) != noErr) {
464 error << string_compose (_("AUPlugin: cannot get root control of carbon window (err: %1)"), err) << endmsg;
465 DisposeWindow (carbon_window);
466 CloseComponent (editView);
471 Float32Point location = { 0.0, 0.0 };
472 Float32Point size = { 0.0, 0.0 } ;
474 if ((err = AudioUnitCarbonViewCreate (editView, *au->get_au(), carbon_window, root_control, &location, &size, &viewPane)) != noErr) {
475 error << string_compose (_("AUPluginUI: cannot create carbon plugin view (err: %1)"), err) << endmsg;
476 DisposeWindow (carbon_window);
477 CloseComponent (editView);
484 GetControlBounds(viewPane, &bounds);
485 size.x = bounds.right-bounds.left;
486 size.y = bounds.bottom-bounds.top;
488 prefwidth = (int) (size.x + 0.5);
489 prefheight = (int) (size.y + 0.5);
491 SizeWindow (carbon_window, prefwidth, prefheight, true);
492 low_box.set_size_request (prefwidth, prefheight);
496 error << _("AU Carbon GUI is not supported.") << endmsg;
502 AUPluginUI::get_nswindow ()
504 Gtk::Container* toplevel = get_toplevel();
506 if (!toplevel || !toplevel->is_toplevel()) {
507 error << _("AUPluginUI: no top level window!") << endmsg;
511 NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
514 error << _("AUPluginUI: no top level window!") << endmsg;
522 AUPluginUI::activate ()
525 ActivateWindow (carbon_window, TRUE);
527 // [cocoa_parent makeKeyAndOrderFront:nil];
531 AUPluginUI::deactivate ()
534 ActivateWindow (carbon_window, FALSE);
539 AUPluginUI::parent_carbon_window ()
542 NSWindow* win = get_nswindow ();
549 Gtk::Container* toplevel = get_toplevel();
551 if (!toplevel || !toplevel->is_toplevel()) {
552 error << _("AUPluginUI: no top level window!") << endmsg;
556 toplevel->get_window()->get_root_origin (x, y);
558 /* compute how tall the title bar is, because we have to offset the position of the carbon window
562 NSRect content_frame = [NSWindow contentRectForFrameRect:[win frame] styleMask:[win styleMask]];
563 NSRect wm_frame = [NSWindow frameRectForContentRect:content_frame styleMask:[win styleMask]];
565 int titlebar_height = wm_frame.size.height - content_frame.size.height;
567 int packing_extra = 6; // this is the total vertical packing in our top level window
569 MoveWindow (carbon_window, x, y + titlebar_height + top_box.get_height() + packing_extra, false);
570 ShowWindow (carbon_window);
572 // create the cocoa window for the carbon one and make it visible
573 cocoa_parent = [[NSWindow alloc] initWithWindowRef: carbon_window];
575 SetWindowActivationScope (carbon_window, kWindowActivationScopeNone);
577 _notify = [ [NotificationObject alloc] initWithPluginUI:this andCocoaParent:cocoa_parent andTopLevelParent:win ];
579 [win addChildWindow:cocoa_parent ordered:NSWindowAbove];
588 AUPluginUI::parent_cocoa_window ()
590 NSWindow* win = get_nswindow ();
596 [win setAutodisplay:YES]; // turn of GTK stuff for this window
598 Gtk::Container* toplevel = get_toplevel();
600 if (!toplevel || !toplevel->is_toplevel()) {
601 error << _("AUPluginUI: no top level window!") << endmsg;
605 NSView* view = gdk_quartz_window_get_nsview (get_toplevel()->get_window()->gobj());
606 GtkRequisition a = top_box.size_request ();
608 /* move the au_view down so that it doesn't overlap the top_box contents */
610 NSPoint origin = { 0, a.height };
612 [au_view setFrameOrigin:origin];
613 [view addSubview:au_view positioned:NSWindowBelow relativeTo:nil];
615 // watch for size changes of the view
617 _notify = [ [NotificationObject alloc] initWithPluginUI:this andCocoaParent:nil andTopLevelParent:win ];
619 [[NSNotificationCenter defaultCenter] addObserver:_notify
620 selector:@selector(auViewResized:) name:NSViewFrameDidChangeNotification
627 dump_view_tree (NSView* view, int depth)
629 NSArray* subviews = [view subviews];
630 unsigned long cnt = [subviews count];
632 for (int d = 0; d < depth; d++) {
635 cerr << " view @ " << view << endl;
637 for (unsigned long i = 0; i < cnt; ++i) {
638 NSView* subview = [subviews objectAtIndex:i];
639 dump_view_tree (subview, depth+1);
644 AUPluginUI::forward_key_event (GdkEventKey* ev)
646 NSEvent* nsevent = gdk_quartz_event_get_nsevent ((GdkEvent*)ev);
648 if (au_view && nsevent) {
650 /* filter on nsevent type here because GDK massages FlagsChanged
651 messages into GDK_KEY_{PRESS,RELEASE} but Cocoa won't
652 handle a FlagsChanged message as a keyDown or keyUp
655 if ([nsevent type] == NSKeyDown) {
656 [[[au_view window] firstResponder] keyDown:nsevent];
657 } else if ([nsevent type] == NSKeyUp) {
658 [[[au_view window] firstResponder] keyUp:nsevent];
659 } else if ([nsevent type] == NSFlagsChanged) {
660 [[[au_view window] firstResponder] flagsChanged:nsevent];
666 AUPluginUI::on_realize ()
670 /* our windows should not have that resize indicator */
672 NSWindow* win = get_nswindow ();
674 [win setShowsResizeIndicator:NO];
679 AUPluginUI::lower_box_realized ()
682 parent_cocoa_window ();
683 } else if (carbon_window) {
684 parent_carbon_window ();
689 AUPluginUI::on_map_event (GdkEventAny*)
695 AUPluginUI::on_window_hide ()
699 HideWindow (carbon_window);
700 ActivateWindow (carbon_window, FALSE);
708 AUPluginUI::on_window_show (const string& /*title*/)
710 /* this is idempotent so just call it every time we show the window */
712 gtk_widget_realize (GTK_WIDGET(low_box.gobj()));
718 ShowWindow (carbon_window);
719 ActivateWindow (carbon_window, TRUE);
727 AUPluginUI::start_updating (GdkEventAny*)
733 AUPluginUI::stop_updating (GdkEventAny*)
739 create_au_gui (boost::shared_ptr<PluginInsert> plugin_insert, VBox** box)
741 AUPluginUI* aup = new AUPluginUI (plugin_insert);
747 AUPluginUI::on_focus_in_event (GdkEventFocus*)
749 //cerr << "au plugin focus in\n";
750 //Keyboard::magic_widget_grab_focus ();
755 AUPluginUI::on_focus_out_event (GdkEventFocus*)
757 //cerr << "au plugin focus out\n";
758 //Keyboard::magic_widget_drop_focus ();