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 // ----------------------------------------------------------------------------
62 @interface wxNSAppController : NSObject
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;
75 @implementation wxNSAppController
77 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
79 // let wx do this, not cocoa
83 - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename;
85 wxCFStringRef cf(wxCFRetain(filename));
86 wxTheApp->MacOpenFile(cf.AsString()) ;
90 - (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender;
92 wxTheApp->MacNewFile() ;
96 - (BOOL)application:(NSApplication *)sender printFile:(NSString *)filename
98 wxCFStringRef cf(wxCFRetain(filename));
99 wxTheApp->MacPrintFile(cf.AsString()) ;
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.
110 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
112 wxWindow* win = wxTheApp->GetTopWindow() ;
115 wxCommandEvent exitEvent(wxEVT_COMMAND_MENU_SELECTED, wxApp::s_macExitMenuItemId);
116 if (!win->GetEventHandler()->ProcessEvent(exitEvent))
121 wxTheApp->ExitMainLoop() ;
123 return NSTerminateCancel;
126 - (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag
128 wxTheApp->MacReopenApp() ;
132 - (void)handleGetURLEvent:(NSAppleEventDescriptor *)event
133 withReplyEvent:(NSAppleEventDescriptor *)replyEvent
135 NSString* url = [[event descriptorAtIndex:1] stringValue];
136 wxCFStringRef cf(wxCFRetain(url));
137 wxTheApp->MacOpenURL(cf.AsString()) ;
142 allows ShowModal to work when using sheets.
143 see include/wx/osx/cocoa/private.h for more info
145 @implementation ModalDialogDelegate
156 return sheetFinished;
164 - (void)waitForSheetToFinish
166 while (!sheetFinished)
172 - (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
174 resultCode = returnCode;
176 // NSAlerts don't need nor respond to orderOut
177 if ([sheet respondsToSelector:@selector(orderOut:)])
178 [sheet orderOut: self];
182 bool wxApp::DoInitGui()
184 wxMacAutoreleasePool pool;
185 [NSApplication sharedApplication];
189 wxNSAppController* controller = [[wxNSAppController alloc] init];
190 [[NSApplication sharedApplication] setDelegate:controller];
192 NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
193 [appleEventManager setEventHandler:controller andSelector:@selector(handleGetURLEvent:withReplyEvent:)
194 forEventClass:kInternetEventClass andEventID:kAEGetURL];
196 [NSApp finishLaunching];
200 void wxApp::DoCleanUp()
204 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
206 NSRect displayRect = [[NSScreen mainScreen] visibleFrame];
207 wxRect r = wxFromNSRect( NULL, displayRect );
213 *width = r.GetWidth();
215 *height = r.GetHeight();
219 void wxGetMousePosition( int* x, int* y )
221 wxPoint pt = wxFromNSPoint(NULL, [NSEvent mouseLocation]);
228 wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
230 return new wxOSXTimerImpl(timer);
233 int gs_wxBusyCursorCount = 0;
234 extern wxCursor gMacCurrentCursor;
235 wxCursor gMacStoredActiveCursor;
237 // Set the cursor to the busy cursor for all windows
238 void wxBeginBusyCursor(const wxCursor *cursor)
240 if (gs_wxBusyCursorCount++ == 0)
242 gMacStoredActiveCursor = gMacCurrentCursor;
243 cursor->MacInstall();
245 wxSetCursor(*cursor);
247 //else: nothing to do, already set
250 // Restore cursor to normal
251 void wxEndBusyCursor()
253 wxCHECK_RET( gs_wxBusyCursorCount > 0,
254 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
256 if (--gs_wxBusyCursorCount == 0)
258 gMacStoredActiveCursor.MacInstall();
259 gMacStoredActiveCursor = wxNullCursor;
261 wxSetCursor(wxNullCursor);
265 // true if we're between the above two calls
268 return (gs_wxBusyCursorCount > 0);
271 void wxMacGlobalToLocal( WindowRef window , Point*pt )
275 void wxMacLocalToGlobal( WindowRef window , Point*pt )
279 wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
281 // wxScreenDC is derived from wxWindowDC, so a screen dc will
282 // call this method when a Blit is performed with it as a source.
286 wxSize sz = m_window->GetSize();
288 int left = subrect != NULL ? subrect->x : 0 ;
289 int top = subrect != NULL ? subrect->y : 0 ;
290 int width = subrect != NULL ? subrect->width : sz.x;
291 int height = subrect != NULL ? subrect->height : sz.y ;
293 NSRect rect = NSMakeRect(left, top, width, height );
294 NSView* view = (NSView*) m_window->GetHandle();
296 // we use this method as other methods force a repaint, and this method can be
297 // called from OnPaint, even with the window's paint dc as source (see wxHTMLWindow)
298 NSBitmapImageRep *rep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect: [view bounds]] retain];
301 CGImageRef cgImageRef = (CGImageRef)[rep CGImage];
303 wxBitmap bitmap(CGImageGetWidth(cgImageRef) , CGImageGetHeight(cgImageRef) );
304 CGRect r = CGRectMake( 0 , 0 , CGImageGetWidth(cgImageRef) , CGImageGetHeight(cgImageRef) );
305 // since our context is upside down we dont use CGContextDrawImage
306 wxMacDrawCGImage( (CGContextRef) bitmap.GetHBITMAP() , &r, cgImageRef ) ;
307 CGImageRelease(cgImageRef);
316 #endif // wxOSX_USE_COCOA