Merge with trunk R2978.
[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 "sync-menu.h"
28
29 #include <Appkit/Appkit.h>
30
31 sigc::signal<void,bool> ApplicationActivationChanged;
32 static EventHandlerRef  application_event_handler_ref;
33
34 /* Called for clicks on the dock icon. Can be used to unminimize or
35  * create a new window for example.
36  */
37
38 static OSErr
39 handle_reopen_application (const AppleEvent *inAppleEvent, 
40                            AppleEvent       *outAppleEvent, 
41                            long              inHandlerRefcon)
42 {
43         cerr << "reopen app\n";
44         return noErr;
45 }
46
47 static OSErr
48 handle_quit_application (const AppleEvent *inAppleEvent, 
49                          AppleEvent       *outAppleEvent, 
50                          long              inHandlerRefcon)
51 {
52         cerr << "quit app\n";
53         ARDOUR_UI::instance()->quit ();
54
55         return noErr;
56 }
57
58 static OSStatus 
59 application_event_handler (EventHandlerCallRef nextHandlerRef, EventRef event, void *userData) 
60 {
61         UInt32 eventKind = GetEventKind (event);
62         
63         switch (eventKind) {
64         case kEventAppActivated:
65                 ApplicationActivationChanged (true); // EMIT SIGNAL
66                 return eventNotHandledErr;
67
68         case kEventAppDeactivated:
69                 ApplicationActivationChanged (false); // EMIT SIGNAL
70                 return eventNotHandledErr;
71                 
72         default:
73                 // pass-thru all kEventClassApplication events we're not interested in.
74                 break;
75         }
76         return eventNotHandledErr;
77 }
78
79 void
80 ARDOUR_UI::platform_specific ()
81 {
82         AEInstallEventHandler (kCoreEventClass, kAEReopenApplication, 
83                                handle_reopen_application, 0, true);
84
85         AEInstallEventHandler (kCoreEventClass, kAEQuitApplication, 
86                                handle_quit_application, 0, true);
87
88         Gtk::Widget* widget = ActionManager::get_widget ("/ui/Main/Session/Quit");
89         if (widget) {
90                 ige_mac_menu_set_quit_menu_item ((GtkMenuItem*) widget->gobj());
91         }
92
93         IgeMacMenuGroup* group = ige_mac_menu_add_app_menu_group ();
94
95         widget = ActionManager::get_widget ("/ui/Main/Session/About");
96         if (widget) {
97                 ige_mac_menu_add_app_menu_item (group, (GtkMenuItem*) widget->gobj(), 0);
98         }
99         widget = ActionManager::get_widget ("/ui/Main/Session/ToggleOptionsEditor");
100         if (widget) {
101                 ige_mac_menu_add_app_menu_item (group, (GtkMenuItem*) widget->gobj(), 0);
102         }
103
104         EventTypeSpec applicationEventTypes[] = {
105                 {kEventClassApplication, kEventAppActivated },
106                 {kEventClassApplication, kEventAppDeactivated }
107         };      
108         
109         EventHandlerUPP ehUPP = NewEventHandlerUPP (application_event_handler);
110         
111         InstallApplicationEventHandler (ehUPP, sizeof(applicationEventTypes) / sizeof(EventTypeSpec), 
112                                         applicationEventTypes, 0, &application_event_handler_ref);
113 }
114
115 void
116 ARDOUR_UI::platform_setup ()
117 {
118         if (!ARDOUR_COMMAND_LINE::finder_invoked_ardour) {
119                 
120                 /* if invoked from the command line, make sure we're visible */
121                 
122                 [NSApp activateIgnoringOtherApps:1];
123         } 
124 }