OS X dbl-click open document debugging
[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         cerr << "\n\n\n\n HANDLE DOCUMENT\n\n\n";
65
66         if (AEGetParamDesc(inAppleEvent, keyDirectObject, typeAEList, &docs) == noErr) {
67                 long n = 0;
68                 AECountItems(&docs, &n);
69                 UInt8 strBuffer[PATH_MAX+1];
70
71                 /* ardour only opens 1 session at a time */
72
73                 FSRef ref;
74
75                 if (AEGetNthPtr(&docs, 1, typeFSRef, 0, 0, &ref, sizeof(ref), 0) == noErr) {
76                         if (FSRefMakePath(&ref, strBuffer, sizeof(strBuffer)) == noErr) {
77                                 Glib::ustring utf8_path ((const char*) strBuffer);
78                                 ARDOUR_UI::instance()->idle_load (utf8_path);
79                         }
80                 }
81         }
82
83         return noErr;
84 }
85
86 static OSErr
87 handle_open_application (const AppleEvent *inAppleEvent, 
88                          AppleEvent       *outAppleEvent, 
89                          long              inHandlerRefcon)
90 {
91         return noErr;
92 }
93
94 static OSStatus 
95 application_event_handler (EventHandlerCallRef nextHandlerRef, EventRef event, void *userData) 
96 {
97         UInt32 eventKind = GetEventKind (event);
98         
99         switch (eventKind) {
100         case kEventAppActivated:
101                 ApplicationActivationChanged (true); // EMIT SIGNAL
102                 return eventNotHandledErr;
103
104         case kEventAppDeactivated:
105                 ApplicationActivationChanged (false); // EMIT SIGNAL
106                 return eventNotHandledErr;
107                 
108         default:
109                 // pass-thru all kEventClassApplication events we're not interested in.
110                 break;
111         }
112         return eventNotHandledErr;
113 }
114
115 void
116 ARDOUR_UI::platform_specific ()
117 {
118         Gtk::Widget* widget = ActionManager::get_widget ("/ui/Main/Session/Quit");
119         if (widget) {
120                 ige_mac_menu_set_quit_menu_item ((GtkMenuItem*) widget->gobj());
121         }
122
123         IgeMacMenuGroup* group = ige_mac_menu_add_app_menu_group ();
124
125         widget = ActionManager::get_widget ("/ui/Main/Session/About");
126         if (widget) {
127                 ige_mac_menu_add_app_menu_item (group, (GtkMenuItem*) widget->gobj(), 0);
128         }
129         widget = ActionManager::get_widget ("/ui/Main/Session/ToggleOptionsEditor");
130
131         if (widget) {
132                 ige_mac_menu_add_app_menu_item (group, (GtkMenuItem*) widget->gobj(), 0);
133         }
134 }
135
136 void
137 ARDOUR_UI::platform_setup ()
138 {
139         AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments, 
140                                handle_open_documents, 0, true);
141
142         AEInstallEventHandler (kCoreEventClass, kAEOpenApplication, 
143                                handle_open_application, 0, true);
144
145         AEInstallEventHandler (kCoreEventClass, kAEReopenApplication, 
146                                handle_reopen_application, 0, true);
147
148         AEInstallEventHandler (kCoreEventClass, kAEPrintDocuments, 
149                                handle_print_documents, 0, true);
150
151         EventTypeSpec applicationEventTypes[] = {
152                 {kEventClassApplication, kEventAppActivated },
153                 {kEventClassApplication, kEventAppDeactivated }
154         };      
155         
156         EventHandlerUPP ehUPP = NewEventHandlerUPP (application_event_handler);
157         
158         InstallApplicationEventHandler (ehUPP, sizeof(applicationEventTypes) / sizeof(EventTypeSpec), 
159                                         applicationEventTypes, 0, &application_event_handler_ref);
160         if (!ARDOUR_COMMAND_LINE::finder_invoked_ardour) {
161                 
162                 /* if invoked from the command line, make sure we're visible */
163                 
164                 [NSApp activateIgnoringOtherApps:1];
165         } 
166 }
167