]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/utils.mm
disable new event code unconditionally for now
[wxWidgets.git] / src / osx / cocoa / utils.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/utils.mm
3 // Purpose: various cocoa utility functions
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id: utils.mm 48805 2007-09-19 14:52:25Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/wxprec.h"
15
16 #include "wx/utils.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/intl.h"
20 #include "wx/app.h"
21 #if wxUSE_GUI
22 #include "wx/toplevel.h"
23 #include "wx/font.h"
24 #endif
25 #endif
26
27 #include "wx/apptrait.h"
28
29 #include "wx/osx/private.h"
30
31 #if wxUSE_GUI
32 #if wxOSX_USE_COCOA_OR_CARBON
33 #include <CoreServices/CoreServices.h>
34 #include "wx/osx/dcclient.h"
35 #include "wx/osx/private/timer.h"
36 #endif
37 #endif // wxUSE_GUI
38
39 #if wxOSX_USE_COCOA
40
41 #if wxUSE_BASE
42
43 // Emit a beeeeeep
44 void wxBell()
45 {
46 NSBeep();
47 }
48
49 // ----------------------------------------------------------------------------
50 // Common Event Support
51 // ----------------------------------------------------------------------------
52
53 void wxMacWakeUp()
54 {
55 // TODO
56 }
57
58 #endif // wxUSE_BASE
59
60 #if wxUSE_GUI
61
62 @interface wxNSAppController : NSObject
63 {
64 }
65
66 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender;
67 - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename;
68 - (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender;
69 - (BOOL)application:(NSApplication *)sender printFile:(NSString *)filename;
70 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
71 - (void)handleGetURLEvent:(NSAppleEventDescriptor *)event
72 withReplyEvent:(NSAppleEventDescriptor *)replyEvent;
73 @end
74
75 @implementation wxNSAppController
76
77 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
78 {
79 // let wx do this, not cocoa
80 return NO;
81 }
82
83 - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename;
84 {
85 wxCFStringRef cf(wxCFRetain(filename));
86 wxTheApp->MacOpenFile(cf.AsString()) ;
87 return YES;
88 }
89
90 - (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender;
91 {
92 wxTheApp->MacNewFile() ;
93 return NO;
94 }
95
96 - (BOOL)application:(NSApplication *)sender printFile:(NSString *)filename
97 {
98 wxCFStringRef cf(wxCFRetain(filename));
99 wxTheApp->MacPrintFile(cf.AsString()) ;
100 return YES;
101 }
102
103 /*
104 Allowable return values are:
105 NSTerminateNow - it is ok to proceed with termination
106 NSTerminateCancel - the application should not be terminated
107 NSTerminateLater - it may be ok to proceed with termination later. The application must call -replyToApplicationShouldTerminate: with YES or NO once the answer is known
108 this return value is for delegates who need to provide document modal alerts (sheets) in order to decide whether to quit.
109 */
110 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
111 {
112 wxWindow* win = wxTheApp->GetTopWindow() ;
113 if ( win )
114 {
115 wxCommandEvent exitEvent(wxEVT_COMMAND_MENU_SELECTED, wxApp::s_macExitMenuItemId);
116 if (!win->ProcessEvent(exitEvent))
117 win->Close(true) ;
118 }
119 else
120 {
121 wxTheApp->ExitMainLoop() ;
122 }
123 return NSTerminateCancel;
124 }
125
126 - (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag
127 {
128 wxTheApp->MacReopenApp() ;
129 return NO;
130 }
131
132 - (void)handleGetURLEvent:(NSAppleEventDescriptor *)event
133 withReplyEvent:(NSAppleEventDescriptor *)replyEvent
134 {
135 NSString* url = [[event descriptorAtIndex:1] stringValue];
136 wxCFStringRef cf(wxCFRetain(url));
137 wxTheApp->MacOpenURL(cf.AsString()) ;
138 }
139 @end
140
141 bool wxApp::DoInitGui()
142 {
143 [NSApplication sharedApplication];
144
145 if (!sm_isEmbedded)
146 {
147 wxNSAppController* controller = [[wxNSAppController alloc] init];
148 [[NSApplication sharedApplication] setDelegate:controller];
149
150 NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
151 [appleEventManager setEventHandler:controller andSelector:@selector(handleGetURLEvent:withReplyEvent:)
152 forEventClass:kInternetEventClass andEventID:kAEGetURL];
153 }
154 [NSApp finishLaunching];
155 return true;
156 }
157
158 void wxApp::DoCleanUp()
159 {
160 }
161
162 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
163 {
164 NSRect displayRect = [[NSScreen mainScreen] visibleFrame];
165 wxRect r = wxFromNSRect( NULL, displayRect );
166 if ( x )
167 *x = r.x;
168 if ( y )
169 *y = r.y;
170 if ( width )
171 *width = r.GetWidth();
172 if ( height )
173 *height = r.GetHeight();
174
175 }
176
177 void wxGetMousePosition( int* x, int* y )
178 {
179 wxPoint pt = wxFromNSPoint(NULL, [NSEvent mouseLocation]);
180 };
181
182 wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
183 {
184 return new wxOSXTimerImpl(timer);
185 }
186
187 int gs_wxBusyCursorCount = 0;
188 extern wxCursor gMacCurrentCursor;
189 wxCursor gMacStoredActiveCursor;
190
191 // Set the cursor to the busy cursor for all windows
192 void wxBeginBusyCursor(const wxCursor *cursor)
193 {
194 if (gs_wxBusyCursorCount++ == 0)
195 {
196 gMacStoredActiveCursor = gMacCurrentCursor;
197 cursor->MacInstall();
198
199 wxSetCursor(*cursor);
200 }
201 //else: nothing to do, already set
202 }
203
204 // Restore cursor to normal
205 void wxEndBusyCursor()
206 {
207 wxCHECK_RET( gs_wxBusyCursorCount > 0,
208 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
209
210 if (--gs_wxBusyCursorCount == 0)
211 {
212 gMacStoredActiveCursor.MacInstall();
213 gMacStoredActiveCursor = wxNullCursor;
214
215 wxSetCursor(wxNullCursor);
216 }
217 }
218
219 // true if we're between the above two calls
220 bool wxIsBusy()
221 {
222 return (gs_wxBusyCursorCount > 0);
223 }
224
225 void wxMacGlobalToLocal( WindowRef window , Point*pt )
226 {
227 }
228
229 void wxMacLocalToGlobal( WindowRef window , Point*pt )
230 {
231 }
232
233 wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
234 {
235 // wxScreenDC is derived from wxWindowDC, so a screen dc will
236 // call this method when a Blit is performed with it as a source.
237 if (!m_window)
238 return wxNullBitmap;
239
240 wxSize sz = m_window->GetSize();
241
242 int left = subrect != NULL ? subrect->x : 0 ;
243 int top = subrect != NULL ? subrect->y : 0 ;
244 int width = subrect != NULL ? subrect->width : sz.x;
245 int height = subrect != NULL ? subrect->height : sz.y ;
246
247 NSRect rect = NSMakeRect(left, top, width, height );
248 NSView* view = (NSView*) m_window->GetHandle();
249 [view lockFocus];
250 // we use this method as other methods force a repaint, and this method can be
251 // called from OnPaint, even with the window's paint dc as source (see wxHTMLWindow)
252 NSBitmapImageRep *rep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect: [view bounds]] retain];
253 [view unlockFocus];
254
255 CGImageRef cgImageRef = (CGImageRef)[rep CGImage];
256
257 wxBitmap bitmap(CGImageGetWidth(cgImageRef) , CGImageGetHeight(cgImageRef) );
258 CGRect r = CGRectMake( 0 , 0 , CGImageGetWidth(cgImageRef) , CGImageGetHeight(cgImageRef) );
259 // since our context is upside down we dont use CGContextDrawImage
260 wxMacDrawCGImage( (CGContextRef) bitmap.GetHBITMAP() , &r, cgImageRef ) ;
261 CGImageRelease(cgImageRef);
262 cgImageRef = NULL;
263 [rep release];
264
265 return bitmap;
266 }
267
268 #endif // wxUSE_GUI
269
270
271
272 #endif // wxOSX_USE_COCOA