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