2
// ApplicationEvents.cs
5
// Michael Hutchinson <mhutchinson@novell.com>
7
// Copyright (c) 2010 Novell, Inc. (http://www.novell.com)
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
using System.Collections.Generic;
29
namespace OSXIntegration.Framework
31
public static class ApplicationEvents
33
static object lockObj = new object ();
37
static EventHandler<ApplicationQuitEventArgs> quit;
38
static IntPtr quitHandlerRef = IntPtr.Zero;
40
public static event EventHandler<ApplicationQuitEventArgs> Quit {
44
if (quitHandlerRef == IntPtr.Zero)
45
quitHandlerRef = Carbon.InstallApplicationEventHandler (HandleQuit, CarbonEventApple.QuitApplication);
51
if (quit == null && quitHandlerRef != IntPtr.Zero) {
52
Carbon.RemoveEventHandler (quitHandlerRef);
53
quitHandlerRef = IntPtr.Zero;
59
static CarbonEventHandlerStatus HandleQuit (IntPtr callRef, IntPtr eventRef, IntPtr user_data)
61
var args = new ApplicationQuitEventArgs ();
63
return args.UserCancelled? CarbonEventHandlerStatus.UserCancelled : args.HandledStatus;
70
static EventHandler<ApplicationEventArgs> reopen;
71
static IntPtr reopenHandlerRef = IntPtr.Zero;
73
public static event EventHandler<ApplicationEventArgs> Reopen {
77
if (reopenHandlerRef == IntPtr.Zero)
78
reopenHandlerRef = Carbon.InstallApplicationEventHandler (HandleReopen, CarbonEventApple.ReopenApplication);
84
if (reopen == null && reopenHandlerRef != IntPtr.Zero) {
85
Carbon.RemoveEventHandler (reopenHandlerRef);
86
reopenHandlerRef = IntPtr.Zero;
92
static CarbonEventHandlerStatus HandleReopen (IntPtr callRef, IntPtr eventRef, IntPtr user_data)
94
var args = new ApplicationEventArgs ();
96
return args.HandledStatus;
101
#region OpenDocuments
103
static EventHandler<ApplicationDocumentEventArgs> openDocuments;
104
static IntPtr openDocumentsHandlerRef = IntPtr.Zero;
106
public static event EventHandler<ApplicationDocumentEventArgs> OpenDocuments {
109
openDocuments += value;
110
if (openDocumentsHandlerRef == IntPtr.Zero)
111
openDocumentsHandlerRef = Carbon.InstallApplicationEventHandler (HandleOpenDocuments, CarbonEventApple.OpenDocuments);
116
openDocuments -= value;
117
if (openDocuments == null && openDocumentsHandlerRef != IntPtr.Zero) {
118
Carbon.RemoveEventHandler (openDocumentsHandlerRef);
119
openDocumentsHandlerRef = IntPtr.Zero;
125
static CarbonEventHandlerStatus HandleOpenDocuments (IntPtr callRef, IntPtr eventRef, IntPtr user_data)
128
var docs = Carbon.GetFileListFromEventRef (eventRef);
129
var args = new ApplicationDocumentEventArgs (docs);
130
openDocuments (null, args);
131
return args.HandledStatus;
132
} catch (Exception ex) {
133
System.Console.WriteLine (ex);
134
return CarbonEventHandlerStatus.NotHandled;
142
static EventHandler<ApplicationUrlEventArgs> openUrls;
143
static IntPtr openUrlsHandlerRef = IntPtr.Zero;
145
public static event EventHandler<ApplicationUrlEventArgs> OpenUrls {
149
if (openUrlsHandlerRef == IntPtr.Zero)
150
openUrlsHandlerRef = Carbon.InstallApplicationEventHandler (HandleOpenUrls,
151
new CarbonEventTypeSpec[] {
152
//For some reason GetUrl doesn't take CarbonEventClass.AppleEvent
153
//need to use GURL, GURL
154
new CarbonEventTypeSpec (CarbonEventClass.Internet, (int)CarbonEventApple.GetUrl)
162
if (openUrls == null && openUrlsHandlerRef != IntPtr.Zero) {
163
Carbon.RemoveEventHandler (openUrlsHandlerRef);
164
openUrlsHandlerRef = IntPtr.Zero;
170
static CarbonEventHandlerStatus HandleOpenUrls (IntPtr callRef, IntPtr eventRef, IntPtr user_data)
173
var urls = Carbon.GetUrlListFromEventRef (eventRef);
174
var args = new ApplicationUrlEventArgs (urls);
175
openUrls (null, args);
176
return args.HandledStatus;
177
} catch (Exception ex) {
178
System.Console.WriteLine (ex);
179
return CarbonEventHandlerStatus.NotHandled;
186
public class ApplicationEventArgs : EventArgs
188
public bool Handled { get; set; }
190
internal CarbonEventHandlerStatus HandledStatus {
192
return Handled? CarbonEventHandlerStatus.Handled : CarbonEventHandlerStatus.NotHandled;
197
public class ApplicationQuitEventArgs : ApplicationEventArgs
199
public bool UserCancelled { get; set; }
202
public class ApplicationDocumentEventArgs : ApplicationEventArgs
204
public ApplicationDocumentEventArgs (IDictionary<string,int> documents)
206
this.Documents = documents;
209
public IDictionary<string,int> Documents { get; private set; }
212
public class ApplicationUrlEventArgs : ApplicationEventArgs
214
public ApplicationUrlEventArgs (IList<string> urls)
219
public IList<string> Urls { get; private set; }