merge with 2.0-ongoing @ rev 3147
[ardour.git] / gtk2_ardour / cocoacarbon.mm
1 /*
2     Copyright (C) 2007 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <Carbon/Carbon.h>
20 #undef check // stupid, stupid carbon
21 #undef YES   // stupid, stupid gtkmm and/or NSObjC
22 #undef NO    // ditto
23
24 #include "ardour_ui.h"
25 #include "actions.h"
26 #include "opts.h"
27 #include <gtkmm2ext/sync-menu.h>
28
29 #include <Appkit/Appkit.h>
30 #include <gdk/gdkquartz.h>
31
32 sigc::signal<void,bool> ApplicationActivationChanged;
33 static EventHandlerRef  application_event_handler_ref;
34
35 /* Called for clicks on the dock icon. Can be used to unminimize or
36  * create a new window for example.
37  */
38
39 static OSErr
40 handle_reopen_application (const AppleEvent *inAppleEvent, 
41                            AppleEvent       *outAppleEvent, 
42                            long              inHandlerRefcon)
43 {
44         return noErr;
45 }
46
47
48 static OSErr
49 handle_print_documents (const AppleEvent *inAppleEvent, 
50                            AppleEvent       *outAppleEvent, 
51                            long              inHandlerRefcon)
52 {
53         return noErr;
54 }
55
56
57 static OSErr
58 handle_open_documents (const AppleEvent *inAppleEvent, 
59                        AppleEvent       *outAppleEvent, 
60                        long              inHandlerRefcon)
61 {
62         AEDescList docs;
63
64         if (AEGetParamDesc(inAppleEvent, keyDirectObject, typeAEList, &docs) == noErr) {
65                 long n = 0;
66                 AECountItems(&docs, &n);
67                 UInt8 strBuffer[PATH_MAX+1];
68
69                 /* ardour only opens 1 session at a time */
70
71                 FSRef ref;
72
73                 if (AEGetNthPtr(&docs, 1, typeFSRef, 0, 0, &ref, sizeof(ref), 0) == noErr) {
74                         if (FSRefMakePath(&ref, strBuffer, sizeof(strBuffer)) == noErr) {
75                                 Glib::ustring utf8_path ((const char*) strBuffer);
76                                 ARDOUR_UI::instance()->idle_load (utf8_path);
77                         }
78                 }
79         }
80
81         return noErr;
82 }
83
84 static OSErr
85 handle_open_application (const AppleEvent *inAppleEvent, 
86                          AppleEvent       *outAppleEvent, 
87                          long              inHandlerRefcon)
88 {
89         return noErr;
90 }
91
92 static OSStatus 
93 application_event_handler (EventHandlerCallRef nextHandlerRef, EventRef event, void *userData) 
94 {
95         UInt32 eventKind = GetEventKind (event);
96         
97         switch (eventKind) {
98         case kEventAppActivated:
99                 ApplicationActivationChanged (true); // EMIT SIGNAL
100                 return eventNotHandledErr;
101
102         case kEventAppDeactivated:
103                 ApplicationActivationChanged (false); // EMIT SIGNAL
104                 return eventNotHandledErr;
105                 
106         default:
107                 // pass-thru all kEventClassApplication events we're not interested in.
108                 break;
109         }
110         return eventNotHandledErr;
111 }
112
113 void
114 ARDOUR_UI::platform_specific ()
115 {
116         Gtk::Widget* widget = ActionManager::get_widget ("/ui/Main/Session/Quit");
117         if (widget) {
118                 ige_mac_menu_set_quit_menu_item ((GtkMenuItem*) widget->gobj());
119         }
120
121         IgeMacMenuGroup* group = ige_mac_menu_add_app_menu_group ();
122
123         widget = ActionManager::get_widget ("/ui/Main/Session/About");
124         if (widget) {
125                 ige_mac_menu_add_app_menu_item (group, (GtkMenuItem*) widget->gobj(), 0);
126         }
127         widget = ActionManager::get_widget ("/ui/Main/Session/ToggleOptionsEditor");
128
129         if (widget) {
130                 ige_mac_menu_add_app_menu_item (group, (GtkMenuItem*) widget->gobj(), 0);
131         }
132 }
133
134 void
135 ARDOUR_UI::platform_setup ()
136 {
137         AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments, 
138                                handle_open_documents, 0, true);
139
140         AEInstallEventHandler (kCoreEventClass, kAEOpenApplication, 
141                                handle_open_application, 0, true);
142
143         AEInstallEventHandler (kCoreEventClass, kAEReopenApplication, 
144                                handle_reopen_application, 0, true);
145
146         AEInstallEventHandler (kCoreEventClass, kAEPrintDocuments, 
147                                handle_print_documents, 0, true);
148
149         EventTypeSpec applicationEventTypes[] = {
150                 {kEventClassApplication, kEventAppActivated },
151                 {kEventClassApplication, kEventAppDeactivated }
152         };      
153         
154         EventHandlerUPP ehUPP = NewEventHandlerUPP (application_event_handler);
155         
156         InstallApplicationEventHandler (ehUPP, sizeof(applicationEventTypes) / sizeof(EventTypeSpec), 
157                                         applicationEventTypes, 0, &application_event_handler_ref);
158         if (!ARDOUR_COMMAND_LINE::finder_invoked_ardour) {
159                 
160                 /* if invoked from the command line, make sure we're visible */
161                 
162                 [NSApp activateIgnoringOtherApps:1];
163         } 
164 }
165