2 #define Marker FuckYouAppleAndYourLackOfNameSpaces
5 #include <ardour/audio_unit.h>
6 #include <ardour/insert.h>
8 #undef check // stupid gtk, stupid apple
10 #include <gtkmm/button.h>
11 #include <gdk/gdkquartz.h>
13 #include <gtkmm2ext/utils.h>
15 #include "au_pluginui.h"
16 #include "gui_thread.h"
18 #include <appleutility/CAAudioUnit.h>
19 #include <appleutility/CAComponent.h>
21 #import <AudioUnit/AUCocoaUIView.h>
22 #import <CoreAudioKit/AUGenericView.h>
28 #include "public_editor.h"
31 using namespace ARDOUR;
33 using namespace Gtkmm2ext;
38 vector<string> AUPluginUI::automation_mode_strings;
40 static const gchar* _automation_mode_strings[] = {
48 AUPluginUI::AUPluginUI (boost::shared_ptr<PluginInsert> insert)
50 , automation_mode_label (_("Automation"))
51 , preset_label (_("Presets"))
54 if (automation_mode_strings.empty()) {
55 automation_mode_strings = I18N (_automation_mode_strings);
58 set_popdown_strings (automation_mode_selector, automation_mode_strings);
59 automation_mode_selector.set_active_text (automation_mode_strings.front());
61 if ((au = boost::dynamic_pointer_cast<AUPlugin> (insert->plugin())) == 0) {
62 error << _("unknown type of editor-supplying plugin (note: no AudioUnit support in this version of ardour)") << endmsg;
63 throw failed_constructor ();
66 /* stuff some stuff into the top of the window */
68 HBox* smaller_hbox = manage (new HBox);
70 smaller_hbox->pack_start (preset_label, false, false, 10);
71 smaller_hbox->pack_start (preset_combo, false, false);
72 smaller_hbox->pack_start (save_button, false, false);
73 smaller_hbox->pack_start (automation_mode_label, false, false);
74 smaller_hbox->pack_start (automation_mode_selector, false, false);
75 smaller_hbox->pack_start (bypass_button, false, true);
77 VBox* v1_box = manage (new VBox);
78 VBox* v2_box = manage (new VBox);
80 v1_box->pack_start (*smaller_hbox, false, true);
81 v2_box->pack_start (focus_button, false, true);
83 top_box.set_homogeneous (false);
84 top_box.set_spacing (6);
85 top_box.set_border_width (6);
87 top_box.pack_end (*v2_box, false, false);
88 top_box.pack_end (*v1_box, false, false);
91 pack_start (top_box, false, false);
92 pack_start (low_box, false, false);
96 automation_mode_label.show ();
97 automation_mode_selector.show ();
98 bypass_button.show ();
102 _activating_from_app = false;
108 /* prefer cocoa, fall back to cocoa, but use carbon if its there */
110 if (test_cocoa_view_support()) {
111 create_cocoa_view ();
112 } else if (test_carbon_view_support()) {
113 create_carbon_view ();
115 create_cocoa_view ();
118 low_box.signal_realize().connect (mem_fun (this, &AUPluginUI::lower_box_realized));
121 AUPluginUI::~AUPluginUI ()
124 NSWindow* win = get_nswindow();
125 RemoveEventHandler(carbon_event_handler);
126 [win removeChildWindow:cocoa_parent];
127 } else if (carbon_window) {
129 DisposeWindow (carbon_window);
132 if (packView && packView != au_view) {
138 AUPluginUI::test_carbon_view_support ()
142 carbon_descriptor.componentType = kAudioUnitCarbonViewComponentType;
143 carbon_descriptor.componentSubType = 'gnrc';
144 carbon_descriptor.componentManufacturer = 'appl';
145 carbon_descriptor.componentFlags = 0;
146 carbon_descriptor.componentFlagsMask = 0;
150 // ask the AU for its first editor component
152 err = AudioUnitGetPropertyInfo(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, &propertySize, NULL);
154 int nEditors = propertySize / sizeof(ComponentDescription);
155 ComponentDescription *editors = new ComponentDescription[nEditors];
156 err = AudioUnitGetProperty(*au->get_au(), kAudioUnitProperty_GetUIComponentList, kAudioUnitScope_Global, 0, editors, &propertySize);
158 // just pick the first one for now
159 carbon_descriptor = editors[0];
169 AUPluginUI::test_cocoa_view_support ()
172 Boolean isWritable = 0;
173 OSStatus err = AudioUnitGetPropertyInfo(*au->get_au(),
174 kAudioUnitProperty_CocoaUI, kAudioUnitScope_Global,
175 0, &dataSize, &isWritable);
177 return dataSize > 0 && err == noErr;
181 AUPluginUI::plugin_class_valid (Class pluginClass)
183 if([pluginClass conformsToProtocol: @protocol(AUCocoaUIBase)]) {
184 if([pluginClass instancesRespondToSelector: @selector(interfaceVersion)] &&
185 [pluginClass instancesRespondToSelector: @selector(uiViewForAudioUnit:withSize:)]) {
193 AUPluginUI::create_cocoa_view ()
195 BOOL wasAbleToLoadCustomView = NO;
196 AudioUnitCocoaViewInfo* cocoaViewInfo = NULL;
197 UInt32 numberOfClasses = 0;
200 NSString* factoryClassName = 0;
201 NSURL* CocoaViewBundlePath;
203 OSStatus result = AudioUnitGetPropertyInfo (*au->get_au(),
204 kAudioUnitProperty_CocoaUI,
205 kAudioUnitScope_Global,
210 numberOfClasses = (dataSize - sizeof(CFURLRef)) / sizeof(CFStringRef);
212 // Does view have custom Cocoa UI?
214 if ((result == noErr) && (numberOfClasses > 0) ) {
215 cocoaViewInfo = (AudioUnitCocoaViewInfo *)malloc(dataSize);
216 if(AudioUnitGetProperty(*au->get_au(),
217 kAudioUnitProperty_CocoaUI,
218 kAudioUnitScope_Global,
221 &dataSize) == noErr) {
223 CocoaViewBundlePath = (NSURL *)cocoaViewInfo->mCocoaAUViewBundleLocation;
225 // we only take the first view in this example.
226 factoryClassName = (NSString *)cocoaViewInfo->mCocoaAUViewClass[0];
230 if (cocoaViewInfo != NULL) {
231 free (cocoaViewInfo);
232 cocoaViewInfo = NULL;
237 NSRect crect = { { 0, 0 }, { 1, 1} };
239 // [A] Show custom UI if view has it
241 if (CocoaViewBundlePath && factoryClassName) {
242 NSBundle *viewBundle = [NSBundle bundleWithPath:[CocoaViewBundlePath path]];
243 if (viewBundle == nil) {
244 error << _("AUPluginUI: error loading AU view's bundle") << endmsg;
247 Class factoryClass = [viewBundle classNamed:factoryClassName];
249 error << _("AUPluginUI: error getting AU view's factory class from bundle") << endmsg;
253 // make sure 'factoryClass' implements the AUCocoaUIBase protocol
254 if (!plugin_class_valid (factoryClass)) {
255 error << _("AUPluginUI: U view's factory class does not properly implement the AUCocoaUIBase protocol") << endmsg;
259 id factoryInstance = [[[factoryClass alloc] init] autorelease];
260 if (factoryInstance == nil) {
261 error << _("AUPluginUI: Could not create an instance of the AU view factory") << endmsg;
266 au_view = [factoryInstance uiViewForAudioUnit:*au->get_au() withSize:crect.size];
269 [CocoaViewBundlePath release];
272 for (i = 0; i < numberOfClasses; i++)
273 CFRelease(cocoaViewInfo->mCocoaAUViewClass[i]);
275 free (cocoaViewInfo);
277 wasAbleToLoadCustomView = YES;
281 if (!wasAbleToLoadCustomView) {
282 // load generic Cocoa view
283 au_view = [[AUGenericView alloc] initWithAudioUnit:*au->get_au()];
284 [(AUGenericView *)au_view setShowsExpertParameters:YES];
289 // Get the size of the new AU View's frame
290 packFrame = [au_view frame];
292 packFrame.origin.x = 0;
293 packFrame.origin.y = 0;
295 if (packFrame.size.width > 500 || packFrame.size.height > 500) {
297 /* its too big - use a scrollview */
299 NSRect frameRect = [[cocoa_window contentView] frame];
300 scroll_view = [[[NSScrollView alloc] initWithFrame:frameRect] autorelease];
301 [scroll_view setDrawsBackground:NO];
302 [scroll_view setHasHorizontalScroller:YES];
303 [scroll_view setHasVerticalScroller:YES];
305 packFrame.size = [NSScrollView frameSizeForContentSize:packFrame.size
306 hasHorizontalScroller:[scroll_view hasHorizontalScroller]
307 hasVerticalScroller:[scroll_view hasVerticalScroller]
308 borderType:[scroll_view borderType]];
310 // Create a new frame with same origin as current
311 // frame but size equal to the size of the new view
313 newFrame.origin = [scroll_view frame].origin;
314 newFrame.size = packFrame.size;
316 // Set the new frame and document views on the scroll view
317 [scroll_view setFrame:newFrame];
318 [scroll_view setDocumentView:au_view];
320 packView = scroll_view;
327 prefwidth = packFrame.size.width;
328 prefheight = packFrame.size.height;
334 AUPluginUI::create_carbon_view ()
337 ControlRef root_control;
339 Component editComponent = FindNextComponent(NULL, &carbon_descriptor);
341 OpenAComponent(editComponent, &editView);
343 error << _("AU Carbon view: cannot open AU Component") << endmsg;
347 Rect r = { 100, 100, 100, 100 };
348 WindowAttributes attr = WindowAttributes (kWindowStandardHandlerAttribute |
349 kWindowCompositingAttribute|
350 kWindowNoShadowAttribute|
351 kWindowNoTitleBarAttribute);
353 if ((err = CreateNewWindow(kDocumentWindowClass, attr, &r, &carbon_window)) != noErr) {
354 error << string_compose (_("AUPluginUI: cannot create carbon window (err: %1)"), err) << endmsg;
358 if ((err = GetRootControl(carbon_window, &root_control)) != noErr) {
359 error << string_compose (_("AUPlugin: cannot get root control of carbon window (err: %1)"), err) << endmsg;
364 Float32Point location = { 0.0, 0.0 };
365 Float32Point size = { 0.0, 0.0 } ;
367 if ((err = AudioUnitCarbonViewCreate (editView, *au->get_au(), carbon_window, root_control, &location, &size, &viewPane)) != noErr) {
368 error << string_compose (_("AUPluginUI: cannot create carbon plugin view (err: %1)"), err) << endmsg;
375 GetControlBounds(viewPane, &bounds);
376 size.x = bounds.right-bounds.left;
377 size.y = bounds.bottom-bounds.top;
379 prefwidth = (int) (size.x + 0.5);
380 prefheight = (int) (size.y + 0.5);
382 SizeWindow (carbon_window, prefwidth, prefheight, true);
383 low_box.set_size_request (prefwidth, prefheight);
389 AUPluginUI::get_nswindow ()
391 Gtk::Container* toplevel = get_toplevel();
393 if (!toplevel || !toplevel->is_toplevel()) {
394 error << _("AUPluginUI: no top level window!") << endmsg;
398 NSWindow* true_parent = gdk_quartz_window_get_nswindow (toplevel->get_window()->gobj());
401 error << _("AUPluginUI: no top level window!") << endmsg;
409 AUPluginUI::activate ()
411 if (carbon_window && cocoa_parent) {
412 cerr << "APP activated, activate carbon window " << insert->name() << endl;
413 _activating_from_app = true;
414 ActivateWindow (carbon_window, TRUE);
415 _activating_from_app = false;
416 [cocoa_parent makeKeyAndOrderFront:nil];
421 AUPluginUI::deactivate ()
424 cerr << "APP DEactivated, for " << insert->name() << endl;
425 _activating_from_app = true;
426 ActivateWindow (carbon_window, FALSE);
427 _activating_from_app = false;
432 _carbon_event (EventHandlerCallRef nextHandlerRef, EventRef event, void *userData)
434 return ((AUPluginUI*)userData)->carbon_event (nextHandlerRef, event);
438 AUPluginUI::carbon_event (EventHandlerCallRef nextHandlerRef, EventRef event)
440 cerr << "CARBON EVENT\n";
442 UInt32 eventKind = GetEventKind(event);
443 ClickActivationResult howToHandleClick;
444 NSWindow* win = get_nswindow ();
446 cerr << "window " << win << " carbon event type " << eventKind << endl;
449 case kEventWindowHandleActivate:
450 cerr << "carbon window for " << insert->name() << " activated\n";
451 if (_activating_from_app) {
452 cerr << "app activation, ignore window activation\n";
455 [win makeMainWindow];
456 return eventNotHandledErr;
459 case kEventWindowHandleDeactivate:
460 cerr << "carbon window for " << insert->name() << " would have been deactivated\n";
461 // never deactivate the carbon window
465 case kEventWindowGetClickActivation:
466 cerr << "carbon window CLICK activated\n";
467 [win makeKeyAndOrderFront:nil];
468 howToHandleClick = kActivateAndHandleClick;
469 SetEventParameter(event, kEventParamClickActivation, typeClickActivationResult,
470 sizeof(ClickActivationResult), &howToHandleClick);
478 AUPluginUI::parent_carbon_window ()
480 NSWindow* win = get_nswindow ();
487 Gtk::Container* toplevel = get_toplevel();
489 if (!toplevel || !toplevel->is_toplevel()) {
490 error << _("AUPluginUI: no top level window!") << endmsg;
494 toplevel->get_window()->get_root_origin (x, y);
496 /* compute how tall the title bar is, because we have to offset the position of the carbon window
500 NSRect content_frame = [NSWindow contentRectForFrameRect:[win frame] styleMask:[win styleMask]];
501 NSRect wm_frame = [NSWindow frameRectForContentRect:content_frame styleMask:[win styleMask]];
503 int titlebar_height = wm_frame.size.height - content_frame.size.height;
505 int packing_extra = 6; // this is the total vertical packing in our top level window
507 MoveWindow (carbon_window, x, y + titlebar_height + top_box.get_height() + packing_extra, false);
508 ShowWindow (carbon_window);
510 // create the cocoa window for the carbon one and make it visible
511 cocoa_parent = [[NSWindow alloc] initWithWindowRef: carbon_window];
513 EventTypeSpec windowEventTypes[] = {
514 {kEventClassWindow, kEventWindowGetClickActivation },
515 {kEventClassWindow, kEventWindowHandleDeactivate }
518 EventHandlerUPP ehUPP = NewEventHandlerUPP(_carbon_event);
519 OSStatus result = InstallWindowEventHandler (carbon_window, ehUPP,
520 sizeof(windowEventTypes) / sizeof(EventTypeSpec),
521 windowEventTypes, this, &carbon_event_handler);
522 if (result != noErr) {
526 [win addChildWindow:cocoa_parent ordered:NSWindowAbove];
532 AUPluginUI::parent_cocoa_window ()
534 NSWindow* win = get_nswindow ();
541 [win setAutodisplay:YES]; // turn of GTK stuff for this window
543 Gtk::Container* toplevel = get_toplevel();
545 if (!toplevel || !toplevel->is_toplevel()) {
546 error << _("AUPluginUI: no top level window!") << endmsg;
550 // Get the size of the new AU View's frame
551 packFrame = [au_view frame];
553 NSView* view = gdk_quartz_window_get_nsview (low_box.get_window()->gobj());
556 [view setFrame:packFrame];
557 [view addSubview:packView];
559 low_box.set_size_request (packFrame.size.width, packFrame.size.height);
565 AUPluginUI::on_realize ()
569 /* our windows should not have that resize indicator */
571 NSWindow* win = get_nswindow ();
573 [win setShowsResizeIndicator:NO];
578 AUPluginUI::lower_box_realized ()
581 parent_cocoa_window ();
582 } else if (carbon_window) {
583 parent_carbon_window ();
588 AUPluginUI::on_hide ()
591 cerr << "AU plugin window hidden\n";
595 AUPluginUI::on_map_event (GdkEventAny* ev)
601 AUPluginUI::on_show ()
603 cerr << "AU plugin window shown\n";
607 gtk_widget_realize (GTK_WIDGET(low_box.gobj()));
611 } else if (carbon_window) {
612 [cocoa_parent setIsVisible:YES];
613 ShowWindow (carbon_window);
618 AUPluginUI::start_updating (GdkEventAny* any)
624 AUPluginUI::stop_updating (GdkEventAny* any)
630 create_au_gui (boost::shared_ptr<PluginInsert> plugin_insert, VBox** box)
632 AUPluginUI* aup = new AUPluginUI (plugin_insert);
638 AUPluginUI::on_focus_in_event (GdkEventFocus* ev)
640 //cerr << "au plugin focus in\n";
641 //Keyboard::magic_widget_grab_focus ();
646 AUPluginUI::on_focus_out_event (GdkEventFocus* ev)
648 //cerr << "au plugin focus out\n";
649 //Keyboard::magic_widget_drop_focus ();