fix up file renaming code a little bit
[ArdourMidi.git] / gtk2_ardour / cocoacarbon.mm
blob84e34bd74d82c1b830c185c42cd0e253fe5c7128
1 /*
2     Copyright (C) 2007 Paul Davis 
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.
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.
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.
19 #include <Carbon/Carbon.h>
20 #undef check // stupid, stupid carbon
21 #undef YES   // stupid, stupid gtkmm and/or NSObjC
22 #undef NO    // ditto
24 #ifdef GTKOSX
25 #include <objc/objc.h>
26 #ifdef nil
27         /*Stupid OS X defining nil*/
28 #undef nil
29 #endif
30 #endif
32 #include "ardour_ui.h"
33 #include "actions.h"
34 #include "opts.h"
35 #include <gtkmm2ext/sync-menu.h>
37 #include <Appkit/Appkit.h>
38 #include <gdk/gdkquartz.h>
40 sigc::signal<void,bool> ApplicationActivationChanged;
41 static EventHandlerRef  application_event_handler_ref;
43 /* Called for clicks on the dock icon. Can be used to unminimize or
44  * create a new window for example.
45  */
47 static OSErr
48 handle_reopen_application (const AppleEvent *inAppleEvent, 
49                            AppleEvent       *outAppleEvent, 
50                            long              inHandlerRefcon)
52         return noErr;
56 static OSErr
57 handle_print_documents (const AppleEvent *inAppleEvent, 
58                            AppleEvent       *outAppleEvent, 
59                            long              inHandlerRefcon)
61         return noErr;
65 static OSErr
66 handle_open_documents (const AppleEvent *inAppleEvent, 
67                        AppleEvent       *outAppleEvent, 
68                        long              inHandlerRefcon)
70         AEDescList docs;
72         if (AEGetParamDesc(inAppleEvent, keyDirectObject, typeAEList, &docs) == noErr) {
73                 long n = 0;
74                 AECountItems(&docs, &n);
75                 UInt8 strBuffer[PATH_MAX+1];
77                 /* ardour only opens 1 session at a time */
79                 FSRef ref;
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         }
89         return noErr;
92 static OSErr
93 handle_open_application (const AppleEvent *inAppleEvent, 
94                          AppleEvent       *outAppleEvent, 
95                          long              inHandlerRefcon)
97         return noErr;
100 static OSStatus 
101 application_event_handler (EventHandlerCallRef nextHandlerRef, EventRef event, void *userData) 
103         UInt32 eventKind = GetEventKind (event);
104         
105         switch (eventKind) {
106         case kEventAppActivated:
107                 ApplicationActivationChanged (true); // EMIT SIGNAL
108                 return eventNotHandledErr;
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;
121 void
122 ARDOUR_UI::platform_specific ()
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         }
129         IgeMacMenuGroup* group = ige_mac_menu_add_app_menu_group ();
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");
137         if (widget) {
138                 ige_mac_menu_add_app_menu_item (group, (GtkMenuItem*) widget->gobj(), 0);
139         }
142 void
143 ARDOUR_UI::platform_setup ()
145         AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments, 
146                                handle_open_documents, 0, true);
148         AEInstallEventHandler (kCoreEventClass, kAEOpenApplication, 
149                                handle_open_application, 0, true);
151         AEInstallEventHandler (kCoreEventClass, kAEReopenApplication, 
152                                handle_reopen_application, 0, true);
154         AEInstallEventHandler (kCoreEventClass, kAEPrintDocuments, 
155                                handle_print_documents, 0, true);
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         } 
174 bool
175 cocoa_open_url (const char* uri)
177         NSString* struri = [[NSString alloc] initWithUTF8String:uri];
178         NSURL* nsurl = [[NSURL alloc] initWithString:struri];
180         bool ret = [[NSWorkspace sharedWorkspace] openURL:nsurl];
182         [struri release];
183         [nsurl release];
185         return ret;