]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/utils.mm
implement EVT_TEXT and EVT_TEXT_ENTER for NSTextView-derived text controls.
[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->GetEventHandler()->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 /*
142 allows ShowModal to work when using sheets.
143 see include/wx/osx/cocoa/private.h for more info
144 */
145 @implementation ModalDialogDelegate
146 - (id)init
147 {
148 [super init];
149 sheetFinished = NO;
150 resultCode = -1;
151 return self;
152 }
153
154 - (BOOL)finished
155 {
156 return sheetFinished;
157 }
158
159 - (int)code
160 {
161 return resultCode;
162 }
163
164 - (void)waitForSheetToFinish
165 {
166 while (!sheetFinished)
167 {
168 wxSafeYield();
169 }
170 }
171
172 - (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
173 {
174 resultCode = returnCode;
175 sheetFinished = YES;
176 // NSAlerts don't need nor respond to orderOut
177 if ([sheet respondsToSelector:@selector(orderOut:)])
178 [sheet orderOut: self];
179 }
180 @end
181
182 bool wxApp::DoInitGui()
183 {
184 wxMacAutoreleasePool pool;
185 [NSApplication sharedApplication];
186
187 if (!sm_isEmbedded)
188 {
189 wxNSAppController* controller = [[wxNSAppController alloc] init];
190 [[NSApplication sharedApplication] setDelegate:controller];
191
192 NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
193 [appleEventManager setEventHandler:controller andSelector:@selector(handleGetURLEvent:withReplyEvent:)
194 forEventClass:kInternetEventClass andEventID:kAEGetURL];
195 }
196 [NSApp finishLaunching];
197 return true;
198 }
199
200 void wxApp::DoCleanUp()
201 {
202 }
203
204 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
205 {
206 NSRect displayRect = [[NSScreen mainScreen] visibleFrame];
207 wxRect r = wxFromNSRect( NULL, displayRect );
208 if ( x )
209 *x = r.x;
210 if ( y )
211 *y = r.y;
212 if ( width )
213 *width = r.GetWidth();
214 if ( height )
215 *height = r.GetHeight();
216
217 }
218
219 void wxGetMousePosition( int* x, int* y )
220 {
221 wxPoint pt = wxFromNSPoint(NULL, [NSEvent mouseLocation]);
222 if ( x )
223 *x = pt.x;
224 if ( y )
225 *y = pt.y;
226 };
227
228 wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
229 {
230 return new wxOSXTimerImpl(timer);
231 }
232
233 int gs_wxBusyCursorCount = 0;
234 extern wxCursor gMacCurrentCursor;
235 wxCursor gMacStoredActiveCursor;
236
237 // Set the cursor to the busy cursor for all windows
238 void wxBeginBusyCursor(const wxCursor *cursor)
239 {
240 if (gs_wxBusyCursorCount++ == 0)
241 {
242 gMacStoredActiveCursor = gMacCurrentCursor;
243 cursor->MacInstall();
244
245 wxSetCursor(*cursor);
246 }
247 //else: nothing to do, already set
248 }
249
250 // Restore cursor to normal
251 void wxEndBusyCursor()
252 {
253 wxCHECK_RET( gs_wxBusyCursorCount > 0,
254 wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") );
255
256 if (--gs_wxBusyCursorCount == 0)
257 {
258 gMacStoredActiveCursor.MacInstall();
259 gMacStoredActiveCursor = wxNullCursor;
260
261 wxSetCursor(wxNullCursor);
262 }
263 }
264
265 // true if we're between the above two calls
266 bool wxIsBusy()
267 {
268 return (gs_wxBusyCursorCount > 0);
269 }
270
271 void wxMacGlobalToLocal( WindowRef window , Point*pt )
272 {
273 }
274
275 void wxMacLocalToGlobal( WindowRef window , Point*pt )
276 {
277 }
278
279 wxBitmap wxWindowDCImpl::DoGetAsBitmap(const wxRect *subrect) const
280 {
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.
283 if (!m_window)
284 return wxNullBitmap;
285
286 wxSize sz = m_window->GetSize();
287
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 ;
292
293 NSRect rect = NSMakeRect(left, top, width, height );
294 NSView* view = (NSView*) m_window->GetHandle();
295 [view lockFocus];
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];
299 [view unlockFocus];
300
301 CGImageRef cgImageRef = (CGImageRef)[rep CGImage];
302
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);
308 cgImageRef = NULL;
309 [rep release];
310
311 return bitmap;
312 }
313
314 #endif // wxUSE_GUI
315
316 #endif // wxOSX_USE_COCOA