~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/addins/MacPlatform/Framework/ApplicationEvents.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// 
2
 
// ApplicationEvents.cs
3
 
//  
4
 
// Author:
5
 
//       Michael Hutchinson <mhutchinson@novell.com>
6
 
// 
7
 
// Copyright (c) 2010 Novell, Inc. (http://www.novell.com)
8
 
// 
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:
15
 
// 
16
 
// The above copyright notice and this permission notice shall be included in
17
 
// all copies or substantial portions of the Software.
18
 
// 
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
25
 
// THE SOFTWARE.
26
 
using System;
27
 
using System.Collections.Generic;
28
 
 
29
 
namespace OSXIntegration.Framework
30
 
{
31
 
        public static class ApplicationEvents
32
 
        {
33
 
                static object lockObj = new object ();
34
 
                
35
 
                #region Quit
36
 
                
37
 
                static EventHandler<ApplicationQuitEventArgs> quit;
38
 
                static IntPtr quitHandlerRef = IntPtr.Zero;
39
 
                
40
 
                public static event EventHandler<ApplicationQuitEventArgs> Quit {
41
 
                        add {
42
 
                                lock (lockObj) {
43
 
                                        quit += value;
44
 
                                        if (quitHandlerRef == IntPtr.Zero)
45
 
                                                quitHandlerRef = Carbon.InstallApplicationEventHandler (HandleQuit, CarbonEventApple.QuitApplication);
46
 
                                }
47
 
                        }
48
 
                        remove {
49
 
                                lock (lockObj) {
50
 
                                        quit -= value;
51
 
                                        if (quit == null && quitHandlerRef != IntPtr.Zero) {
52
 
                                                Carbon.RemoveEventHandler (quitHandlerRef);
53
 
                                                quitHandlerRef = IntPtr.Zero;
54
 
                                        }
55
 
                                }
56
 
                        }
57
 
                }
58
 
                
59
 
                static CarbonEventHandlerStatus HandleQuit (IntPtr callRef, IntPtr eventRef, IntPtr user_data)
60
 
                {
61
 
                        var args = new ApplicationQuitEventArgs ();
62
 
                        quit (null, args);
63
 
                        return args.UserCancelled? CarbonEventHandlerStatus.UserCancelled : args.HandledStatus;
64
 
                }
65
 
                
66
 
                #endregion
67
 
                
68
 
                #region Reopen
69
 
                
70
 
                static EventHandler<ApplicationEventArgs> reopen;
71
 
                static IntPtr reopenHandlerRef = IntPtr.Zero;
72
 
                
73
 
                public static event EventHandler<ApplicationEventArgs> Reopen {
74
 
                        add {
75
 
                                lock (lockObj) {
76
 
                                        reopen += value;
77
 
                                        if (reopenHandlerRef == IntPtr.Zero)
78
 
                                                reopenHandlerRef = Carbon.InstallApplicationEventHandler (HandleReopen, CarbonEventApple.ReopenApplication);
79
 
                                }
80
 
                        }
81
 
                        remove {
82
 
                                lock (lockObj) {
83
 
                                        reopen -= value;
84
 
                                        if (reopen == null && reopenHandlerRef != IntPtr.Zero) {
85
 
                                                Carbon.RemoveEventHandler (reopenHandlerRef);
86
 
                                                reopenHandlerRef = IntPtr.Zero;
87
 
                                        }
88
 
                                }
89
 
                        }
90
 
                }
91
 
                
92
 
                static CarbonEventHandlerStatus HandleReopen (IntPtr callRef, IntPtr eventRef, IntPtr user_data)
93
 
                {
94
 
                        var args = new ApplicationEventArgs ();
95
 
                        reopen (null, args);
96
 
                        return args.HandledStatus;
97
 
                }
98
 
                
99
 
                #endregion
100
 
                
101
 
                #region OpenDocuments
102
 
                
103
 
                static EventHandler<ApplicationDocumentEventArgs> openDocuments;
104
 
                static IntPtr openDocumentsHandlerRef = IntPtr.Zero;
105
 
                
106
 
                public static event EventHandler<ApplicationDocumentEventArgs> OpenDocuments {
107
 
                        add {
108
 
                                lock (lockObj) {
109
 
                                        openDocuments += value;
110
 
                                        if (openDocumentsHandlerRef == IntPtr.Zero)
111
 
                                                openDocumentsHandlerRef = Carbon.InstallApplicationEventHandler (HandleOpenDocuments, CarbonEventApple.OpenDocuments);
112
 
                                }
113
 
                        }
114
 
                        remove {
115
 
                                lock (lockObj) {
116
 
                                        openDocuments -= value;
117
 
                                        if (openDocuments == null && openDocumentsHandlerRef != IntPtr.Zero) {
118
 
                                                Carbon.RemoveEventHandler (openDocumentsHandlerRef);
119
 
                                                openDocumentsHandlerRef = IntPtr.Zero;
120
 
                                        }
121
 
                                }
122
 
                        }
123
 
                }
124
 
                
125
 
                static CarbonEventHandlerStatus HandleOpenDocuments (IntPtr callRef, IntPtr eventRef, IntPtr user_data)
126
 
                {
127
 
                        try {
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;
135
 
                        }
136
 
                }
137
 
                
138
 
                #endregion
139
 
                
140
 
                #region OpenUrls
141
 
                
142
 
                static EventHandler<ApplicationUrlEventArgs> openUrls;
143
 
                static IntPtr openUrlsHandlerRef = IntPtr.Zero;
144
 
                
145
 
                public static event EventHandler<ApplicationUrlEventArgs> OpenUrls {
146
 
                        add {
147
 
                                lock (lockObj) {
148
 
                                        openUrls += value;
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)
155
 
                                                        }
156
 
                                                );
157
 
                                }
158
 
                        }
159
 
                        remove {
160
 
                                lock (lockObj) {
161
 
                                        openUrls -= value;
162
 
                                        if (openUrls == null && openUrlsHandlerRef != IntPtr.Zero) {
163
 
                                                Carbon.RemoveEventHandler (openUrlsHandlerRef);
164
 
                                                openUrlsHandlerRef = IntPtr.Zero;
165
 
                                        }
166
 
                                }
167
 
                        }
168
 
                }
169
 
                
170
 
                static CarbonEventHandlerStatus HandleOpenUrls (IntPtr callRef, IntPtr eventRef, IntPtr user_data)
171
 
                {
172
 
                        try {
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;
180
 
                        }
181
 
                }
182
 
                
183
 
                #endregion
184
 
        }
185
 
        
186
 
        public class ApplicationEventArgs : EventArgs
187
 
        {
188
 
                public bool Handled { get; set; }
189
 
                
190
 
                internal CarbonEventHandlerStatus HandledStatus {
191
 
                        get {
192
 
                                return Handled? CarbonEventHandlerStatus.Handled : CarbonEventHandlerStatus.NotHandled;
193
 
                        }
194
 
                }
195
 
        }
196
 
        
197
 
        public class ApplicationQuitEventArgs : ApplicationEventArgs
198
 
        {
199
 
                public bool UserCancelled { get; set; }
200
 
        }
201
 
        
202
 
        public class ApplicationDocumentEventArgs : ApplicationEventArgs
203
 
        {
204
 
                public ApplicationDocumentEventArgs (IDictionary<string,int> documents)
205
 
                {
206
 
                        this.Documents = documents;
207
 
                }               
208
 
                
209
 
                public IDictionary<string,int> Documents { get; private set; }
210
 
        }
211
 
        
212
 
        public class ApplicationUrlEventArgs : ApplicationEventArgs
213
 
        {
214
 
                public ApplicationUrlEventArgs (IList<string> urls)
215
 
                {
216
 
                        this.Urls = urls;
217
 
                }               
218
 
                
219
 
                public IList<string> Urls { get; private set; }
220
 
        }
221
 
}
222