1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/utils.mm
3 // Purpose: various cocoa utility functions
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: utils.mm 48805 2007-09-19 14:52:25Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/wxprec.h"
22 #include "wx/toplevel.h"
27 #include "wx/apptrait.h"
29 #include "wx/osx/private.h"
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"
49 // ----------------------------------------------------------------------------
50 // Common Event Support
51 // ----------------------------------------------------------------------------
55 NSEvent* wakeupEvent = [NSEvent otherEventWithType:NSApplicationDefined location:NSZeroPoint
56 modifierFlags:NSAnyEventMask timestamp:0 windowNumber:0 context:nil subtype:0 data1:0 data2:0];
57 [NSApp postEvent:wakeupEvent atStart:NO];
64 @interface wxNSAppController : NSObject
68 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender;
69 - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename;
70 - (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender;
71 - (BOOL)application:(NSApplication *)sender printFile:(NSString *)filename;
72 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
73 - (void)handleGetURLEvent:(NSAppleEventDescriptor *)event
74 withReplyEvent:(NSAppleEventDescriptor *)replyEvent;
77 @implementation wxNSAppController
79 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
82 // let wx do this, not cocoa
86 - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename;
89 wxCFStringRef cf(wxCFRetain(filename));
90 wxTheApp->MacOpenFile(cf.AsString()) ;
94 - (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender;
97 wxTheApp->MacNewFile() ;
101 - (BOOL)application:(NSApplication *)sender printFile:(NSString *)filename
104 wxCFStringRef cf(wxCFRetain(filename));
105 wxTheApp->MacPrintFile(cf.AsString()) ;
110 Allowable return values are:
111 NSTerminateNow - it is ok to proceed with termination
112 NSTerminateCancel - the application should not be terminated
113 NSTerminateLater - it may be ok to proceed with termination later. The application must call -replyToApplicationShouldTerminate: with YES or NO once the answer is known
114 this return value is for delegates who need to provide document modal alerts (sheets) in order to decide whether to quit.
116 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
119 wxWindow* win = wxTheApp->GetTopWindow() ;
122 wxCommandEvent exitEvent(wxEVT_COMMAND_MENU_SELECTED, wxApp::s_macExitMenuItemId);
123 if (!win->GetEventHandler()->ProcessEvent(exitEvent))
128 wxTheApp->ExitMainLoop() ;
130 return NSTerminateCancel;
133 - (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag
137 wxTheApp->MacReopenApp() ;
141 - (void)handleGetURLEvent:(NSAppleEventDescriptor *)event
142 withReplyEvent:(NSAppleEventDescriptor *)replyEvent
144 wxUnusedVar(replyEvent);
145 NSString* url = [[event descriptorAtIndex:1] stringValue];
146 wxCFStringRef cf(wxCFRetain(url));
147 wxTheApp->MacOpenURL(cf.AsString()) ;
152 allows ShowModal to work when using sheets.
153 see include/wx/osx/cocoa/private.h for more info
155 @implementation ModalDialogDelegate
166 return sheetFinished;
174 - (void)waitForSheetToFinish
176 while (!sheetFinished)
182 - (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
184 wxUnusedVar(contextInfo);
185 resultCode = returnCode;
187 // NSAlerts don't need nor respond to orderOut
188 if ([sheet respondsToSelector:@selector(orderOut:)])
189 [sheet orderOut: self];
193 bool wxApp::DoInitGui()
195 wxMacAutoreleasePool pool;
196 [NSApplication sharedApplication];
200 wxNSAppController* controller = [[wxNSAppController alloc] init];
201 [[NSApplication sharedApplication] setDelegate:controller];
203 NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
204 [appleEventManager setEventHandler:controller andSelector:@selector(handleGetURLEvent:withReplyEvent:)
205 forEventClass:kInternetEventClass andEventID:kAEGetURL];
206 [NSApp finishLaunching];
211 void wxApp::DoCleanUp()
215 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
217 NSRect displayRect = [[NSScreen mainScreen] visibleFrame];
218 wxRect r = wxFromNSRect( NULL, displayRect );
224 *width = r.GetWidth();
226 *height = r.GetHeight();
230 void wxGetMousePosition( int* x, int* y )
232 wxPoint pt = wxFromNSPoint(NULL, [NSEvent mouseLocation]);
239 wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
241 return new wxOSXTimerImpl(timer);
244 int gs_wxBusyCursorCount = 0;
245 extern wxCursor gMacCurrentCursor;
246 wxCursor gMacStoredActiveCursor;
248 // Set the cursor to the busy cursor for all windows
249 void wxBeginBusyCursor(const wxCursor *cursor)
251 if (gs_wxBusyCursorCount++ == 0)
253 gMacStoredActiveCursor = gMacCurrentCursor;
254 cursor->MacInstall();
256 wxSetCursor(*cursor);
258 //else: nothing to do, already set
261 // Restore cursor to normal
262 void wxEndBusyCursor()
264 wxCHECK_RET( gs_wxBusyCursorCount > 0,
265 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
267 if (--gs_wxBusyCursorCount == 0)
269 gMacStoredActiveCursor.MacInstall();
270 gMacStoredActiveCursor = wxNullCursor;
272 wxSetCursor(wxNullCursor);
276 // true if we're between the above two calls
279 return (gs_wxBusyCursorCount > 0);
282 wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
284 // wxScreenDC is derived from wxWindowDC, so a screen dc will
285 // call this method when a Blit is performed with it as a source.
289 wxSize sz = m_window->GetSize();
291 int left = subrect != NULL ? subrect->x : 0 ;
292 int top = subrect != NULL ? subrect->y : 0 ;
293 int width = subrect != NULL ? subrect->width : sz.x;
294 int height = subrect != NULL ? subrect->height : sz.y ;
296 NSRect rect = NSMakeRect(left, top, width, height );
297 NSView* view = (NSView*) m_window->GetHandle();
299 // we use this method as other methods force a repaint, and this method can be
300 // called from OnPaint, even with the window's paint dc as source (see wxHTMLWindow)
301 NSBitmapImageRep *rep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect: [view bounds]] retain];
304 wxBitmap bitmap(width, height);
305 if ( [rep respondsToSelector:@selector(CGImage)] )
307 CGImageRef cgImageRef = (CGImageRef)[rep CGImage];
309 CGRect r = CGRectMake( 0 , 0 , CGImageGetWidth(cgImageRef) , CGImageGetHeight(cgImageRef) );
310 // since our context is upside down we dont use CGContextDrawImage
311 wxMacDrawCGImage( (CGContextRef) bitmap.GetHBITMAP() , &r, cgImageRef ) ;
312 CGImageRelease(cgImageRef);
317 // TODO for 10.4 in case we can support this for osx_cocoa
326 #endif // wxOSX_USE_COCOA