changes to help strp silence
[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 #ifdef GTKOSX
25 #include <objc/objc.h>
26 #ifdef nil
27         /*Stupid OS X defining nil*/
28 #undef nil
29 #endif
30 #endif
31
32 #include "ardour_ui.h"
33 #include "actions.h"
34 #include "opts.h"
35 #include <gtkmm2ext/sync-menu.h>
36
37 #include <Appkit/Appkit.h>
38 #include <gdk/gdkquartz.h>
39
40 sigc::signal<void,bool> ApplicationActivationChanged;
41 static EventHandlerRef  application_event_handler_ref;
42
43 /* Called for clicks on the dock icon. Can be used to unminimize or
44  * create a new window for example.
45  */
46
47 static OSErr
48 handle_reopen_application (const AppleEvent *inAppleEvent, 
49                            AppleEvent       *outAppleEvent, 
50                            long              inHandlerRefcon)
51 {
52         return noErr;
53 }
54
55
56 static OSErr
57 handle_print_documents (const AppleEvent *inAppleEvent, 
58                            AppleEvent       *outAppleEvent, 
59                            long              inHandlerRefcon)
60 {
61         return noErr;
62 }
63
64
65 static OSErr
66 handle_open_documents (const AppleEvent *inAppleEvent, 
67                        AppleEvent       *outAppleEvent, 
68                        long              inHandlerRefcon)
69 {
70         AEDescList docs;
71
72         if (AEGetParamDesc(inAppleEvent, keyDirectObject, typeAEList, &docs) == noErr) {
73                 long n = 0;
74                 AECountItems(&docs, &n);
75                 UInt8 strBuffer[PATH_MAX+1];
76
77                 /* ardour only opens 1 session at a time */
78
79                 FSRef ref;
80
81                 if (AEGetNthPtr(&docs, 1, typeFSRef, 0, 0, &ref, sizeof(ref), 0) == noErr) {
82                         if (FSRefMakePath(&ref, strBuffer, sizeof(strBuffer)) == noErr) {
83                                 Glib::ustring utf8_path ((const char*) strBuffer);
84                                 ARDOUR_UI::instance()->idle_load (utf8_path);
85                         }
86                 }
87         }
88
89         return noErr;
90 }
91
92 static OSErr
93 handle_open_application (const AppleEvent *inAppleEvent, 
94                          AppleEvent       *outAppleEvent, 
95                          long              inHandlerRefcon)
96 {
97         return noErr;
98 }
99
100 static OSStatus 
101 application_event_handler (EventHandlerCallRef nextHandlerRef, EventRef event, void *userData) 
102 {
103         UInt32 eventKind = GetEventKind (event);
104         
105         switch (eventKind) {
106         case kEventAppActivated:
107                 ApplicationActivationChanged (true); // EMIT SIGNAL
108                 return eventNotHandledErr;
109
110         case kEventAppDeactivated:
111                 ApplicationActivationChanged (false); // EMIT SIGNAL
112                 return eventNotHandledErr;
113                 
114         default:
115                 // pass-thru all kEventClassApplication events we're not interested in.
116                 break;
117         }
118         return eventNotHandledErr;
119 }
120
121 void
122 ARDOUR_UI::platform_specific ()
123 {
124         Gtk::Widget* widget = ActionManager::get_widget ("/ui/Main/Session/Quit");
125         if (widget) {
126                 ige_mac_menu_set_quit_menu_item ((GtkMenuItem*) widget->gobj());
127         }
128
129         IgeMacMenuGroup* group = ige_mac_menu_add_app_menu_group ();
130
131         widget = ActionManager::get_widget ("/ui/Main/Session/About");
132         if (widget) {
133                 ige_mac_menu_add_app_menu_item (group, (GtkMenuItem*) widget->gobj(), 0);
134         }
135         widget = ActionManager::get_widget ("/ui/Main/Session/ToggleOptionsEditor");
136
137         if (widget) {
138                 ige_mac_menu_add_app_menu_item (group, (GtkMenuItem*) widget->gobj(), 0);
139         }
140 }
141
142 void
143 ARDOUR_UI::platform_setup ()
144 {
145         AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments, 
146                                handle_open_documents, 0, true);
147
148         AEInstallEventHandler (kCoreEventClass, kAEOpenApplication, 
149                                handle_open_application, 0, true);
150
151         AEInstallEventHandler (kCoreEventClass, kAEReopenApplication, 
152                                handle_reopen_application, 0, true);
153
154         AEInstallEventHandler (kCoreEventClass, kAEPrintDocuments, 
155                                handle_print_documents, 0, true);
156
157         EventTypeSpec applicationEventTypes[] = {
158                 {kEventClassApplication, kEventAppActivated },
159                 {kEventClassApplication, kEventAppDeactivated }
160         };      
161         
162         EventHandlerUPP ehUPP = NewEventHandlerUPP (application_event_handler);
163         
164         InstallApplicationEventHandler (ehUPP, sizeof(applicationEventTypes) / sizeof(EventTypeSpec), 
165                                         applicationEventTypes, 0, &application_event_handler_ref);
166         if (!ARDOUR_COMMAND_LINE::finder_invoked_ardour) {
167                 
168                 /* if invoked from the command line, make sure we're visible */
169                 
170                 [NSApp activateIgnoringOtherApps:1];
171         } 
172 }
173
174 bool
175 cocoa_open_url (const char* uri)
176 {
177         NSString* struri = [[NSString alloc] initWithUTF8String:uri];
178         NSURL* nsurl = [[NSURL alloc] initWithString:struri];
179
180         bool ret = [[NSWorkspace sharedWorkspace] openURL:nsurl];
181
182         [struri release];
183         [nsurl release];
184
185         return ret;
186 }