2 #include <ardour/audio_unit.h>
3 #include <ardour/insert.h>
5 #include <gdk/gdkquartz.h>
7 #include "au_pluginui.h"
8 #include "gui_thread.h"
10 #include <appleutility/CAAudioUnit.h>
11 #include <appleutility/CAComponent.h>
13 #import <AudioUnit/AUCocoaUIView.h>
14 #import <CoreAudioKit/AUGenericView.h>
18 using namespace ARDOUR;
24 static const float kOffsetForAUView_X = 220;
25 static const float kOffsetForAUView_Y = 90;
27 AUPluginUI::AUPluginUI (boost::shared_ptr<PluginInsert> insert)
30 if ((au = boost::dynamic_pointer_cast<AUPlugin> (insert->plugin())) == 0) {
31 error << _("unknown type of editor-supplying plugin (note: no AudioUnit support in this version of ardour)") << endmsg;
32 throw failed_constructor ();
38 carbon_parented = false;
39 cocoa_parented = false;
43 test_view_support (has_carbon, has_cocoa);
47 } else if (has_carbon) {
48 create_carbon_view (has_carbon);
50 /* fallback to cocoa */
56 AUPluginUI::~AUPluginUI ()
58 if (carbon_parented) {
59 NSWindow* win = get_nswindow();
60 RemoveEventHandler(carbon_event_handler);
61 [win removeChildWindow:cocoa_parent];
66 AUPluginUI::test_view_support (bool& has_carbon, bool& has_cocoa)
68 has_carbon = test_carbon_view_support();
69 has_cocoa = test_cocoa_view_support();
73 AUPluginUI::test_carbon_view_support ()
77 carbon_descriptor.componentType = kAudioUnitCarbonViewComponentType;
78 carbon_descriptor.componentSubType = 'gnrc';
79 carbon_descriptor.componentManufacturer = 'appl';
80 carbon_descriptor.componentFlags = 0;
81 carbon_descriptor.componentFlagsMask = 0;
85 // ask the AU for its first editor component
87 err = AudioUnitGetPropertyInfo(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, &propertySize, NULL);
89 int nEditors = propertySize / sizeof(ComponentDescription);
90 ComponentDescription *editors = new ComponentDescription[nEditors];
91 err = AudioUnitGetProperty(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, editors, &propertySize);
93 // just pick the first one for now
94 carbon_descriptor = editors[0];
104 AUPluginUI::test_cocoa_view_support ()
107 Boolean isWritable = 0;
108 OSStatus err = AudioUnitGetPropertyInfo(*au->get_au(),
109 kAudioUnitProperty_CocoaUI, kAudioUnitScope_Global,
110 0, &dataSize, &isWritable);
112 return dataSize > 0 && err == noErr;
116 AUPluginUI::plugin_class_valid (Class pluginClass)
118 if([pluginClass conformsToProtocol: @protocol(AUCocoaUIBase)]) {
119 if([pluginClass instancesRespondToSelector: @selector(interfaceVersion)] &&
120 [pluginClass instancesRespondToSelector: @selector(uiViewForAudioUnit:withSize:)]) {
128 AUPluginUI::create_cocoa_view ()
130 BOOL wasAbleToLoadCustomView = NO;
131 AudioUnitCocoaViewInfo* cocoaViewInfo = NULL;
132 UInt32 numberOfClasses = 0;
135 NSString* factoryClassName = 0;
136 NSURL* CocoaViewBundlePath;
138 OSStatus result = AudioUnitGetPropertyInfo (*au->get_au(),
139 kAudioUnitProperty_CocoaUI,
140 kAudioUnitScope_Global,
145 numberOfClasses = (dataSize - sizeof(CFURLRef)) / sizeof(CFStringRef);
147 // Does view have custom Cocoa UI?
149 if ((result == noErr) && (numberOfClasses > 0) ) {
150 cocoaViewInfo = (AudioUnitCocoaViewInfo *)malloc(dataSize);
151 if(AudioUnitGetProperty(*au->get_au(),
152 kAudioUnitProperty_CocoaUI,
153 kAudioUnitScope_Global,
156 &dataSize) == noErr) {
158 CocoaViewBundlePath = (NSURL *)cocoaViewInfo->mCocoaAUViewBundleLocation;
160 // we only take the first view in this example.
161 factoryClassName = (NSString *)cocoaViewInfo->mCocoaAUViewClass[0];
165 if (cocoaViewInfo != NULL) {
166 free (cocoaViewInfo);
167 cocoaViewInfo = NULL;
172 NSRect crect = { { 0, 0 }, { 1, 1} };
174 // [A] Show custom UI if view has it
176 if (CocoaViewBundlePath && factoryClassName) {
177 NSBundle *viewBundle = [NSBundle bundleWithPath:[CocoaViewBundlePath path]];
178 if (viewBundle == nil) {
179 error << _("AUPluginUI: error loading AU view's bundle") << endmsg;
182 Class factoryClass = [viewBundle classNamed:factoryClassName];
184 error << _("AUPluginUI: error getting AU view's factory class from bundle") << endmsg;
188 // make sure 'factoryClass' implements the AUCocoaUIBase protocol
189 if (!plugin_class_valid (factoryClass)) {
190 error << _("AUPluginUI: U view's factory class does not properly implement the AUCocoaUIBase protocol") << endmsg;
194 id factoryInstance = [[[factoryClass alloc] init] autorelease];
195 if (factoryInstance == nil) {
196 error << _("AUPluginUI: Could not create an instance of the AU view factory") << endmsg;
201 au_view = [factoryInstance uiViewForAudioUnit:*au->get_au() withSize:crect.size];
204 [CocoaViewBundlePath release];
207 for (i = 0; i < numberOfClasses; i++)
208 CFRelease(cocoaViewInfo->mCocoaAUViewClass[i]);
210 free (cocoaViewInfo);
212 wasAbleToLoadCustomView = YES;
216 if (!wasAbleToLoadCustomView) {
217 // [B] Otherwise show generic Cocoa view
218 au_view = [[AUGenericView alloc] initWithAudioUnit:*au->get_au()];
219 [(AUGenericView *)au_view setShowsExpertParameters:YES];
222 /* make a child cocoa window */
224 cocoa_window = [[NSWindow alloc]
225 initWithContentRect:crect
226 styleMask:NSBorderlessWindowMask
227 backing:NSBackingStoreBuffered
234 AUPluginUI::create_carbon_view (bool generic)
237 ControlRef root_control;
239 Component editComponent = FindNextComponent(NULL, &carbon_descriptor);
241 OpenAComponent(editComponent, &editView);
243 error << _("AU Carbon view: cannot open AU Component") << endmsg;
247 Rect r = { 100, 100, 100, 100 };
248 WindowAttributes attr = WindowAttributes (kWindowStandardHandlerAttribute |
249 kWindowCompositingAttribute|
250 kWindowNoShadowAttribute|
251 kWindowNoTitleBarAttribute);
253 if ((err = CreateNewWindow(kDocumentWindowClass, attr, &r, &carbon_window)) != noErr) {
254 error << string_compose (_("AUPluginUI: cannot create carbon window (err: %1)"), err) << endmsg;
258 if ((err = GetRootControl(carbon_window, &root_control)) != noErr) {
259 error << string_compose (_("AUPlugin: cannot get root control of carbon window (err: %1)"), err) << endmsg;
264 Float32Point location = { 0.0, 0.0 };
265 Float32Point size = { 0.0, 0.0 } ;
267 if ((err = AudioUnitCarbonViewCreate (editView, *au->get_au(), carbon_window, root_control, &location, &size, &viewPane)) != noErr) {
268 error << string_compose (_("AUPluginUI: cannot create carbon plugin view (err: %1)"), err) << endmsg;
275 GetControlBounds(viewPane, &bounds);
276 size.x = bounds.right-bounds.left;
277 size.y = bounds.bottom-bounds.top;
278 SizeWindow(carbon_window, (short) (size.x + 0.5), (short) (size.y + 0.5), true);
280 prefwidth = (int) (size.x + 0.5);
281 prefheight = (int) (size.y + 0.5);
284 mViewPaneResizer->WantEventTypes (GetControlEventTarget(mAUViewPane), GetEventTypeCount(resizeEvent), resizeEvent);
290 AUPluginUI::get_nswindow ()
292 Gtk::Container* toplevel = get_toplevel();
294 if (!toplevel || !toplevel->is_toplevel()) {
295 error << _("AUPluginUI: no top level window!") << endmsg;
299 NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
302 error << _("AUPluginUI: no top level window!") << endmsg;
310 AUPluginUI::activate ()
312 NSWindow* win = get_nswindow ();
313 [win setLevel:NSFloatingWindowLevel];
315 if (carbon_parented) {
316 [cocoa_parent makeKeyAndOrderFront:nil];
317 ActivateWindow (carbon_window, TRUE);
322 AUPluginUI::deactivate ()
324 /* nothing to do here */
329 _carbon_event (EventHandlerCallRef nextHandlerRef, EventRef event, void *userData)
331 return ((AUPluginUI*)userData)->carbon_event (nextHandlerRef, event);
335 AUPluginUI::carbon_event (EventHandlerCallRef nextHandlerRef, EventRef event)
337 UInt32 eventKind = GetEventKind(event);
338 ClickActivationResult howToHandleClick;
339 NSWindow* win = get_nswindow ();
342 case kEventWindowHandleActivate:
343 [win makeMainWindow];
344 return eventNotHandledErr;
347 case kEventWindowHandleDeactivate:
348 return eventNotHandledErr;
351 case kEventWindowGetClickActivation:
352 howToHandleClick = kActivateAndHandleClick;
353 SetEventParameter(event, kEventParamClickActivation, typeClickActivationResult,
354 sizeof(ClickActivationResult), &howToHandleClick);
362 AUPluginUI::parent_carbon_window ()
364 NSWindow* win = get_nswindow ();
371 Gtk::Container* toplevel = get_toplevel();
373 if (!toplevel || !toplevel->is_toplevel()) {
374 error << _("AUPluginUI: no top level window!") << endmsg;
378 toplevel->get_window()->get_root_origin (x, y);
380 /* compute how tall the title bar is, because we have to offset the position of the carbon window
384 NSRect content_frame = [NSWindow contentRectForFrameRect:[win frame] styleMask:[win styleMask]];
385 NSRect wm_frame = [NSWindow frameRectForContentRect:content_frame styleMask:[win styleMask]];
387 int titlebar_height = wm_frame.size.height - content_frame.size.height;
389 MoveWindow (carbon_window, x, y + titlebar_height, false);
390 ShowWindow (carbon_window);
392 // create the cocoa window for the carbon one and make it visible
393 cocoa_parent = [[NSWindow alloc] initWithWindowRef: carbon_window];
395 EventTypeSpec windowEventTypes[] = {
396 {kEventClassWindow, kEventWindowGetClickActivation },
397 {kEventClassWindow, kEventWindowHandleDeactivate },
398 {kEventClassWindow, kEventWindowHandleActivate }
401 EventHandlerUPP ehUPP = NewEventHandlerUPP(_carbon_event);
402 OSStatus result = InstallWindowEventHandler (carbon_window, ehUPP,
403 sizeof(windowEventTypes) / sizeof(EventTypeSpec),
404 windowEventTypes, this, &carbon_event_handler);
405 if (result != noErr) {
409 [win addChildWindow:cocoa_parent ordered:NSWindowAbove];
410 [win setLevel:NSFloatingWindowLevel];
411 [win setHidesOnDeactivate:YES];
413 carbon_parented = true;
419 AUPluginUI::parent_cocoa_window ()
421 NSWindow* win = get_nswindow ();
427 Gtk::Container* toplevel = get_toplevel();
429 if (!toplevel || !toplevel->is_toplevel()) {
430 error << _("AUPluginUI: no top level window!") << endmsg;
434 // Get the size of the new AU View's frame
435 NSRect au_view_frame = [au_view frame];
437 if (au_view_frame.size.width > 500 || au_view_frame.size.height > 500) {
439 /* its too big - use a scrollview */
441 NSRect frameRect = [[cocoa_window contentView] frame];
442 scroll_view = [[[NSScrollView alloc] initWithFrame:frameRect] autorelease];
443 [scroll_view setDrawsBackground:NO];
444 [scroll_view setHasHorizontalScroller:YES];
445 [scroll_view setHasVerticalScroller:YES];
447 NSSize frameSize = [NSScrollView frameSizeForContentSize:au_view_frame.size
448 hasHorizontalScroller:[scroll_view hasHorizontalScroller]
449 hasVerticalScroller:[scroll_view hasVerticalScroller]
450 borderType:[scroll_view borderType]];
452 // Create a new frame with same origin as current
453 // frame but size equal to the size of the new view
455 newFrame.origin = [scroll_view frame].origin;
456 newFrame.size = frameSize;
458 // Set the new frame and document views on the scroll view
459 NSRect currentFrame = [scroll_view frame];
460 [scroll_view setFrame:newFrame];
461 [scroll_view setDocumentView:au_view];
463 cerr << "scroll view size is " << newFrame.size.width << " x " << newFrame.size.height << endl;
465 NSSize oldContentSize = [[cocoa_window contentView] frame].size;
466 NSSize newContentSize = oldContentSize;
468 cerr << "original size is " << newContentSize.width << " x " << newContentSize.height << endl;
470 newContentSize.width += (newFrame.size.width - currentFrame.size.width);
471 newContentSize.height += (newFrame.size.height - currentFrame.size.height);
473 [cocoa_window setContentSize:newContentSize];
474 [cocoa_window setContentView:scroll_view];
478 [cocoa_window setContentSize:au_view_frame.size];
479 [cocoa_window setContentView:au_view];
483 /* compute how tall the title bar is, because we have to offset the position of the child window
487 NSRect content_frame = [NSWindow contentRectForFrameRect:[win frame] styleMask:[win styleMask]];
488 NSRect wm_frame = [NSWindow frameRectForContentRect:content_frame styleMask:[win styleMask]];
489 int titlebar_height = wm_frame.size.height - content_frame.size.height;
491 // move cocoa window into position relative to the toplevel window
493 NSRect view_frame = [[cocoa_window contentView] frame];
494 view_frame.origin.x = content_frame.origin.x;
495 view_frame.origin.y = content_frame.origin.y;
497 [cocoa_window setFrame:view_frame display:NO];
499 /* make top level window big enough to hold cocoa window and titlebar */
501 content_frame.size.width = view_frame.size.width;
502 content_frame.size.height = view_frame.size.height + titlebar_height;
504 [win setFrame:content_frame display:NO];
506 /* now make cocoa window a child of this top level */
508 [win addChildWindow:cocoa_window ordered:NSWindowAbove];
509 // [win setLevel:NSFloatingWindowLevel];
510 [win setHidesOnDeactivate:YES];
512 cocoa_parented = true;
518 AUPluginUI::on_realize ()
524 if (parent_cocoa_window ()) {
527 } else if (carbon_window) {
529 if (parent_carbon_window ()) {
530 // ShowWindow (carbon_window);
536 AUPluginUI::on_show ()
538 cerr << "AU plugin window shown\n";
543 [cocoa_window setIsVisible:YES];
544 } else if (carbon_window) {
545 [cocoa_parent setIsVisible:YES];
550 AUPluginUI::start_updating (GdkEventAny* any)
556 AUPluginUI::stop_updating (GdkEventAny* any)
562 create_au_gui (boost::shared_ptr<PluginInsert> plugin_insert, VBox** box)
564 AUPluginUI* aup = new AUPluginUI (plugin_insert);