]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: app.cpp | |
3 | // Purpose: wxApp | |
4 | // Author: AUTHOR | |
5 | // Modified by: | |
6 | // Created: ??/??/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) AUTHOR | |
8461e4c2 | 9 | // Licence: wxWindows licence |
e9576ca5 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "app.h" | |
14 | #endif | |
15 | ||
df06d1a4 GD |
16 | #include "wx/defs.h" |
17 | ||
03e11df5 | 18 | #include "wx/window.h" |
e9576ca5 | 19 | #include "wx/frame.h" |
e8566d35 | 20 | #include "wx/button.h" |
e9576ca5 SC |
21 | #include "wx/app.h" |
22 | #include "wx/utils.h" | |
23 | #include "wx/gdicmn.h" | |
24 | #include "wx/pen.h" | |
25 | #include "wx/brush.h" | |
26 | #include "wx/cursor.h" | |
ed5b9811 | 27 | #include "wx/intl.h" |
e9576ca5 SC |
28 | #include "wx/icon.h" |
29 | #include "wx/palette.h" | |
30 | #include "wx/dc.h" | |
31 | #include "wx/dialog.h" | |
32 | #include "wx/msgdlg.h" | |
33 | #include "wx/log.h" | |
34 | #include "wx/module.h" | |
35 | #include "wx/memory.h" | |
2f1ae414 | 36 | #include "wx/tooltip.h" |
b3c406a5 | 37 | #include "wx/textctrl.h" |
03e11df5 | 38 | #include "wx/menu.h" |
e9576ca5 | 39 | #if wxUSE_WX_RESOURCES |
df06d1a4 | 40 | # include "wx/resource.h" |
e9576ca5 SC |
41 | #endif |
42 | ||
43 | #include <string.h> | |
44 | ||
8be97d65 SC |
45 | // mac |
46 | ||
f5c6eb5c | 47 | #ifndef __DARWIN__ |
03e11df5 | 48 | #if __option(profile) |
8461e4c2 | 49 | #include <profiler.h> |
03e11df5 | 50 | #endif |
9779893b | 51 | #endif |
519cb848 | 52 | |
8be97d65 SC |
53 | #include "apprsrc.h" |
54 | ||
03e11df5 GD |
55 | #include "wx/mac/uma.h" |
56 | #include "wx/mac/macnotfy.h" | |
2f1ae414 | 57 | |
df06d1a4 GD |
58 | #ifdef __DARWIN__ |
59 | # include <CoreServices/CoreServices.h> | |
ed581ee2 GD |
60 | # if defined(WXMAKINGDLL) |
61 | # include <mach-o/dyld.h> | |
62 | # endif | |
df06d1a4 GD |
63 | #else |
64 | # include <Sound.h> | |
65 | # include <Threads.h> | |
66 | # include <ToolUtils.h> | |
67 | # include <DiskInit.h> | |
68 | # include <Devices.h> | |
2f1ae414 | 69 | #endif |
519cb848 | 70 | |
e9576ca5 SC |
71 | extern char *wxBuffer; |
72 | extern wxList wxPendingDelete; | |
519cb848 SC |
73 | extern wxList *wxWinMacWindowList; |
74 | extern wxList *wxWinMacControlList; | |
e9576ca5 | 75 | |
6418cb93 | 76 | wxApp *wxTheApp = NULL; |
e9576ca5 | 77 | |
2f1ae414 | 78 | #if !USE_SHARED_LIBRARY |
e9576ca5 SC |
79 | IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler) |
80 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) | |
81 | EVT_IDLE(wxApp::OnIdle) | |
2f1ae414 SC |
82 | EVT_END_SESSION(wxApp::OnEndSession) |
83 | EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession) | |
e9576ca5 | 84 | END_EVENT_TABLE() |
2f1ae414 | 85 | #endif |
e9576ca5 | 86 | |
169935ad | 87 | |
8461e4c2 | 88 | const short kMacMinHeap = (29 * 1024) ; |
8be97d65 | 89 | // platform specific static variables |
169935ad | 90 | |
519cb848 SC |
91 | const short kwxMacMenuBarResource = 1 ; |
92 | const short kwxMacAppleMenuId = 1 ; | |
93 | ||
fbbdb7cd GD |
94 | WXHRGN wxApp::s_macCursorRgn = NULL; |
95 | wxWindow* wxApp::s_captureWindow = NULL ; | |
96 | int wxApp::s_lastMouseDown = 0 ; | |
97 | long wxApp::sm_lastMessageTime = 0; | |
519cb848 | 98 | |
fbbdb7cd GD |
99 | bool wxApp::s_macDefaultEncodingIsPC = true ; |
100 | bool wxApp::s_macSupportPCMenuShortcuts = true ; | |
101 | long wxApp::s_macAboutMenuItemId = wxID_ABOUT ; | |
102 | wxString wxApp::s_macHelpMenuTitleName = "&Help" ; | |
519cb848 | 103 | |
72055702 SC |
104 | pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ; |
105 | pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ; | |
106 | pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ; | |
107 | pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon ) ; | |
108 | ||
109 | ||
007ae8d0 | 110 | pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long refcon ) |
519cb848 | 111 | { |
ba2928c6 | 112 | // GD: UNUSED wxApp* app = (wxApp*) refcon ; |
8461e4c2 | 113 | return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ; |
519cb848 SC |
114 | } |
115 | ||
007ae8d0 | 116 | pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon ) |
519cb848 | 117 | { |
ba2928c6 | 118 | // GD: UNUSED wxApp* app = (wxApp*) refcon ; |
8461e4c2 | 119 | return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ; |
519cb848 SC |
120 | } |
121 | ||
007ae8d0 | 122 | pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon ) |
519cb848 | 123 | { |
ba2928c6 | 124 | // GD: UNUSED wxApp* app = (wxApp*) refcon ; |
8461e4c2 | 125 | return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ; |
519cb848 SC |
126 | } |
127 | ||
007ae8d0 | 128 | pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon ) |
519cb848 | 129 | { |
ba2928c6 | 130 | // GD: UNUSED wxApp* app = (wxApp*) refcon ; |
8461e4c2 | 131 | return wxTheApp->MacHandleAEQuit( (AppleEvent*) event , reply) ; |
519cb848 SC |
132 | } |
133 | ||
76a5e5d2 | 134 | short wxApp::MacHandleAEODoc(const WXEVENTREF event , WXEVENTREF reply) |
519cb848 | 135 | { |
c1fb8167 | 136 | SysBeep(40) ; |
8461e4c2 VZ |
137 | ProcessSerialNumber PSN ; |
138 | PSN.highLongOfPSN = 0 ; | |
139 | PSN.lowLongOfPSN = kCurrentProcess ; | |
140 | SetFrontProcess( &PSN ) ; | |
141 | return noErr ; | |
519cb848 SC |
142 | } |
143 | ||
76a5e5d2 | 144 | short wxApp::MacHandleAEPDoc(const WXEVENTREF event , WXEVENTREF reply) |
519cb848 | 145 | { |
8461e4c2 | 146 | return noErr ; |
519cb848 SC |
147 | } |
148 | ||
76a5e5d2 | 149 | short wxApp::MacHandleAEOApp(const WXEVENTREF event , WXEVENTREF reply) |
519cb848 | 150 | { |
8461e4c2 | 151 | return noErr ; |
519cb848 SC |
152 | } |
153 | ||
76a5e5d2 | 154 | short wxApp::MacHandleAEQuit(const WXEVENTREF event , WXEVENTREF reply) |
519cb848 | 155 | { |
8461e4c2 VZ |
156 | wxWindow* win = GetTopWindow() ; |
157 | if ( win ) | |
158 | { | |
159 | win->Close(TRUE ) ; | |
160 | } | |
161 | else | |
162 | { | |
163 | ExitMainLoop() ; | |
164 | } | |
165 | return noErr ; | |
519cb848 SC |
166 | } |
167 | ||
df06d1a4 | 168 | char StringMac[] = "\x0d\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f" |
8461e4c2 VZ |
169 | "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" |
170 | "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xae\xaf" | |
171 | "\xb1\xb4\xb5\xb6\xbb\xbc\xbe\xbf" | |
172 | "\xc0\xc1\xc2\xc4\xc7\xc8\xc9\xcb\xcc\xcd\xce\xcf" | |
173 | "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xca\xdb" ; | |
519cb848 | 174 | |
2f1ae414 | 175 | char StringANSI[] = "\x0a\xC4\xC5\xC7\xC9\xD1\xD6\xDC\xE1\xE0\xE2\xE4\xE3\xE5\xE7\xE9\xE8" |
8461e4c2 VZ |
176 | "\xEA\xEB\xED\xEC\xEE\xEF\xF1\xF3\xF2\xF4\xF6\xF5\xFA\xF9\xFB\xFC" |
177 | "\x86\xBA\xA2\xA3\xA7\x95\xB6\xDF\xAE\xA9\x99\xB4\xA8\xC6\xD8" | |
178 | "\xB1\xA5\xB5\xF0\xAA\xBA\xE6\xF8" | |
179 | "\xBF\xA1\xAC\x83\xAB\xBB\x85\xC0\xC3\xD5\x8C\x9C" | |
180 | "\x96\x97\x93\x94\x91\x92\xF7\xFF\xA0\x80" ; | |
519cb848 SC |
181 | |
182 | void wxMacConvertFromPC( const char *from , char *to , int len ) | |
183 | { | |
8461e4c2 VZ |
184 | char *c ; |
185 | if ( from == to ) | |
186 | { | |
187 | for( int i = 0 ; i < len ; ++ i ) | |
188 | { | |
189 | c = strchr( StringANSI , *from ) ; | |
190 | if ( c != NULL ) | |
191 | { | |
192 | *to = StringMac[ c - StringANSI] ; | |
193 | } | |
194 | ++to ; | |
195 | ++from ; | |
196 | } | |
197 | } | |
198 | else | |
199 | { | |
200 | for( int i = 0 ; i < len ; ++ i ) | |
201 | { | |
202 | c = strchr( StringANSI , *from ) ; | |
203 | if ( c != NULL ) | |
204 | { | |
205 | *to = StringMac[ c - StringANSI] ; | |
206 | } | |
207 | else | |
208 | { | |
209 | *to = *from ; | |
210 | } | |
211 | ++to ; | |
212 | ++from ; | |
213 | } | |
214 | } | |
519cb848 SC |
215 | } |
216 | ||
217 | void wxMacConvertToPC( const char *from , char *to , int len ) | |
218 | { | |
8461e4c2 VZ |
219 | char *c ; |
220 | if ( from == to ) | |
221 | { | |
222 | for( int i = 0 ; i < len ; ++ i ) | |
223 | { | |
224 | c = strchr( StringMac , *from ) ; | |
225 | if ( c != NULL ) | |
226 | { | |
227 | *to = StringANSI[ c - StringMac] ; | |
228 | } | |
229 | ++to ; | |
230 | ++from ; | |
231 | } | |
232 | } | |
233 | else | |
234 | { | |
235 | for( int i = 0 ; i < len ; ++ i ) | |
236 | { | |
237 | c = strchr( StringMac , *from ) ; | |
238 | if ( c != NULL ) | |
239 | { | |
240 | *to = StringANSI[ c - StringMac] ; | |
241 | } | |
242 | else | |
243 | { | |
244 | *to = *from ; | |
245 | } | |
246 | ++to ; | |
247 | ++from ; | |
248 | } | |
249 | } | |
519cb848 SC |
250 | } |
251 | ||
9779893b | 252 | void wxMacConvertFromPC( char * p ) |
519cb848 | 253 | { |
8461e4c2 VZ |
254 | char *ptr = p ; |
255 | int len = strlen ( p ) ; | |
9779893b | 256 | |
8461e4c2 | 257 | wxMacConvertFromPC( ptr , ptr , len ) ; |
519cb848 SC |
258 | } |
259 | ||
9779893b | 260 | void wxMacConvertFromPCForControls( char * p ) |
519cb848 | 261 | { |
8461e4c2 VZ |
262 | char *ptr = p ; |
263 | int len = strlen ( p ) ; | |
264 | ||
265 | wxMacConvertFromPC( ptr , ptr , len ) ; | |
ba2928c6 | 266 | for ( unsigned int i = 0 ; i < strlen ( ptr ) ; i++ ) |
8461e4c2 VZ |
267 | { |
268 | if ( ptr[i] == '&' && ptr[i]+1 != ' ' ) | |
269 | { | |
270 | memmove( &ptr[i] , &ptr[i+1] , strlen( &ptr[i+1] ) + 1) ; | |
271 | } | |
272 | } | |
519cb848 SC |
273 | } |
274 | ||
9779893b | 275 | void wxMacConvertFromPC( unsigned char *p ) |
519cb848 | 276 | { |
8461e4c2 VZ |
277 | char *ptr = (char*) p + 1 ; |
278 | int len = p[0] ; | |
9779893b | 279 | |
8461e4c2 | 280 | wxMacConvertFromPC( ptr , ptr , len ) ; |
519cb848 SC |
281 | } |
282 | ||
283 | extern char *wxBuffer ; | |
284 | ||
9779893b | 285 | wxString wxMacMakeMacStringFromPC( const char * p ) |
519cb848 | 286 | { |
8461e4c2 VZ |
287 | const char *ptr = p ; |
288 | int len = strlen ( p ) ; | |
289 | char *buf = wxBuffer ; | |
290 | ||
291 | if ( len >= BUFSIZ + 512 ) | |
292 | { | |
293 | buf = new char [len+1] ; | |
294 | } | |
295 | ||
296 | wxMacConvertFromPC( ptr , buf , len ) ; | |
297 | buf[len] = 0 ; | |
298 | wxString result( buf ) ; | |
299 | if ( buf != wxBuffer ) | |
300 | delete buf ; | |
301 | return result ; | |
519cb848 SC |
302 | } |
303 | ||
304 | ||
9779893b | 305 | void wxMacConvertToPC( char * p ) |
519cb848 | 306 | { |
8461e4c2 VZ |
307 | char *ptr = p ; |
308 | int len = strlen ( p ) ; | |
9779893b | 309 | |
8461e4c2 | 310 | wxMacConvertToPC( ptr , ptr , len ) ; |
519cb848 SC |
311 | } |
312 | ||
9779893b | 313 | void wxMacConvertToPC( unsigned char *p ) |
519cb848 | 314 | { |
8461e4c2 VZ |
315 | char *ptr = (char*) p + 1 ; |
316 | int len = p[0] ; | |
9779893b | 317 | |
8461e4c2 | 318 | wxMacConvertToPC( ptr , ptr , len ) ; |
519cb848 SC |
319 | } |
320 | ||
9779893b | 321 | wxString wxMacMakePCStringFromMac( const char * p ) |
519cb848 | 322 | { |
8461e4c2 VZ |
323 | const char *ptr = p ; |
324 | int len = strlen ( p ) ; | |
325 | char *buf = wxBuffer ; | |
326 | ||
327 | if ( len >= BUFSIZ + 512 ) | |
328 | { | |
329 | buf = new char [len+1] ; | |
330 | } | |
331 | ||
332 | wxMacConvertToPC( ptr , buf , len ) ; | |
333 | buf[len] = 0 ; | |
334 | ||
335 | wxString result( buf ) ; | |
336 | if ( buf != wxBuffer ) | |
337 | delete buf ; | |
338 | return result ; | |
519cb848 | 339 | } |
169935ad | 340 | |
8461e4c2 | 341 | wxString wxMacMakeStringFromMacString( const char* from , bool mac2pcEncoding ) |
3d2791f1 | 342 | { |
8461e4c2 VZ |
343 | if (mac2pcEncoding) |
344 | { | |
345 | return wxMacMakePCStringFromMac( from ) ; | |
346 | } | |
347 | else | |
348 | { | |
349 | return wxString( from ) ; | |
350 | } | |
3d2791f1 SC |
351 | } |
352 | ||
8461e4c2 | 353 | wxString wxMacMakeStringFromPascal( StringPtr from , bool mac2pcEncoding ) |
3d2791f1 SC |
354 | { |
355 | // this is safe since a pascal string can never be larger than 256 bytes | |
356 | char s[256] ; | |
357 | CopyPascalStringToC( from , s ) ; | |
8461e4c2 VZ |
358 | if (mac2pcEncoding) |
359 | { | |
360 | return wxMacMakePCStringFromMac( s ) ; | |
361 | } | |
362 | else | |
363 | { | |
364 | return wxString( s ) ; | |
365 | } | |
3d2791f1 SC |
366 | } |
367 | ||
8461e4c2 | 368 | void wxMacStringToPascal( const char * from , StringPtr to , bool pc2macEncoding ) |
3d2791f1 | 369 | { |
8461e4c2 VZ |
370 | if (pc2macEncoding) |
371 | { | |
372 | CopyCStringToPascal( wxMacMakeMacStringFromPC( from ) , to ) ; | |
373 | } | |
374 | else | |
375 | { | |
376 | CopyCStringToPascal( from , to ) ; | |
377 | } | |
3d2791f1 | 378 | } |
ed581ee2 GD |
379 | |
380 | #if defined(WXMAKINGDLL) && !defined(__DARWIN__) | |
53fd991c SC |
381 | // we know it's there ;-) |
382 | WXIMPORT char std::__throws_bad_alloc ; | |
383 | #endif | |
384 | ||
e9576ca5 SC |
385 | bool wxApp::Initialize() |
386 | { | |
fbbdb7cd | 387 | int error = 0 ; |
9779893b | 388 | |
fbbdb7cd | 389 | // Mac-specific |
9779893b | 390 | |
fbbdb7cd GD |
391 | UMAInitToolbox( 4 ) ; |
392 | SetEventMask( everyEvent ) ; | |
8461e4c2 | 393 | UMAShowWatchCursor() ; |
8be97d65 | 394 | |
fbbdb7cd GD |
395 | #if defined(WXMAKINGDLL) && defined(__DARWIN__) |
396 | // open shared library resources from here since we don't have | |
397 | // __wxinitialize in Mach-O shared libraries | |
398 | wxStAppResource::OpenSharedLibraryResource(NULL); | |
399 | #endif | |
400 | ||
4114d0af | 401 | #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340) |
fbbdb7cd GD |
402 | AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments , |
403 | NewAEEventHandlerUPP(AEHandleODoc) , | |
03e11df5 | 404 | (long) wxTheApp , FALSE ) ; |
fbbdb7cd GD |
405 | AEInstallEventHandler( kCoreEventClass , kAEOpenApplication , |
406 | NewAEEventHandlerUPP(AEHandleOApp) , | |
03e11df5 | 407 | (long) wxTheApp , FALSE ) ; |
fbbdb7cd GD |
408 | AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments , |
409 | NewAEEventHandlerUPP(AEHandlePDoc) , | |
03e11df5 | 410 | (long) wxTheApp , FALSE ) ; |
fbbdb7cd GD |
411 | AEInstallEventHandler( kCoreEventClass , kAEQuitApplication , |
412 | NewAEEventHandlerUPP(AEHandleQuit) , | |
03e11df5 GD |
413 | (long) wxTheApp , FALSE ) ; |
414 | #else | |
fbbdb7cd GD |
415 | AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments , |
416 | NewAEEventHandlerProc(AEHandleODoc) , | |
417 | (long) wxTheApp , FALSE ) ; | |
418 | AEInstallEventHandler( kCoreEventClass , kAEOpenApplication , | |
419 | NewAEEventHandlerProc(AEHandleOApp) , | |
420 | (long) wxTheApp , FALSE ) ; | |
421 | AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments , | |
422 | NewAEEventHandlerProc(AEHandlePDoc) , | |
423 | (long) wxTheApp , FALSE ) ; | |
424 | AEInstallEventHandler( kCoreEventClass , kAEQuitApplication , | |
425 | NewAEEventHandlerProc(AEHandleQuit) , | |
426 | (long) wxTheApp , FALSE ) ; | |
03e11df5 | 427 | #endif |
8be97d65 | 428 | |
f5c6eb5c | 429 | #ifndef __DARWIN__ |
fbbdb7cd | 430 | // test the minimal configuration necessary |
8be97d65 | 431 | |
fbbdb7cd | 432 | # if !TARGET_CARBON |
8461e4c2 VZ |
433 | long theSystem ; |
434 | long theMachine; | |
435 | ||
436 | if (Gestalt(gestaltMachineType, &theMachine) != noErr) | |
437 | { | |
438 | error = kMacSTRWrongMachine; | |
439 | } | |
440 | else if (theMachine < gestaltMacPlus) | |
441 | { | |
442 | error = kMacSTRWrongMachine; | |
443 | } | |
444 | else if (Gestalt(gestaltSystemVersion, &theSystem) != noErr ) | |
445 | { | |
446 | error = kMacSTROldSystem ; | |
447 | } | |
72055702 | 448 | else if ( theSystem < 0x0860 ) |
8461e4c2 VZ |
449 | { |
450 | error = kMacSTROldSystem ; | |
451 | } | |
452 | else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap) | |
453 | { | |
454 | error = kMacSTRSmallSize; | |
455 | } | |
fbbdb7cd | 456 | # endif |
8461e4c2 VZ |
457 | /* |
458 | else | |
459 | { | |
460 | if ( !UMAHasAppearance() ) | |
461 | { | |
462 | error = kMacSTRNoPre8Yet ; | |
463 | } | |
464 | } | |
465 | */ | |
ed5b9811 | 466 | #endif |
8be97d65 | 467 | |
8461e4c2 | 468 | // if we encountered any problems so far, give the error code and exit immediately |
9779893b | 469 | |
169935ad | 470 | if ( error ) |
9779893b | 471 | { |
53fd991c | 472 | wxStAppResource resload ; |
8461e4c2 VZ |
473 | short itemHit; |
474 | Str255 message; | |
475 | ||
476 | GetIndString(message, 128, error); | |
477 | UMAShowArrowCursor() ; | |
478 | ParamText("\pFatal Error", message, (ConstStr255Param)"\p", (ConstStr255Param)"\p"); | |
479 | itemHit = Alert(128, nil); | |
480 | return FALSE ; | |
9779893b | 481 | } |
519cb848 | 482 | |
f5c6eb5c | 483 | #ifndef __DARWIN__ |
fbbdb7cd | 484 | # if __option(profile) |
8461e4c2 | 485 | ProfilerInit( collectDetailed, bestTimeBase , 20000 , 40 ) ; |
fbbdb7cd | 486 | # endif |
9779893b | 487 | #endif |
519cb848 | 488 | |
f5c6eb5c | 489 | #ifndef __DARWIN__ |
ed581ee2 GD |
490 | // now avoid exceptions thrown for new (bad_alloc) |
491 | std::__throws_bad_alloc = FALSE ; | |
03e11df5 | 492 | #endif |
9779893b | 493 | |
8461e4c2 | 494 | s_macCursorRgn = ::NewRgn() ; |
8be97d65 | 495 | |
ed581ee2 GD |
496 | wxBuffer = new char[BUFSIZ + 512]; |
497 | ||
498 | wxClassInfo::InitializeClasses(); | |
e9576ca5 | 499 | |
2f1ae414 SC |
500 | #if wxUSE_RESOURCES |
501 | // wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion); | |
e9576ca5 | 502 | #endif |
e9576ca5 | 503 | |
2f1ae414 SC |
504 | #if wxUSE_THREADS |
505 | wxPendingEventsLocker = new wxCriticalSection; | |
506 | #endif | |
ed581ee2 GD |
507 | |
508 | wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING); | |
509 | wxTheColourDatabase->Initialize(); | |
e9576ca5 | 510 | |
fc0daf84 | 511 | #ifdef __WXDEBUG__ |
0a67a93b SC |
512 | #if wxUSE_LOG |
513 | // flush the logged messages if any and install a 'safer' log target: the | |
514 | // default one (wxLogGui) can't be used after the resources are freed just | |
515 | // below and the user suppliedo ne might be even more unsafe (using any | |
516 | // wxWindows GUI function is unsafe starting from now) | |
517 | wxLog::DontCreateOnDemand(); | |
518 | ||
519 | // this will flush the old messages if any | |
520 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
521 | #endif // wxUSE_LOG | |
fc0daf84 | 522 | #endif |
0a67a93b | 523 | |
0a6cce3b SC |
524 | wxWinMacWindowList = new wxList(wxKEY_INTEGER); |
525 | wxWinMacControlList = new wxList(wxKEY_INTEGER); | |
526 | ||
e9576ca5 SC |
527 | wxInitializeStockLists(); |
528 | wxInitializeStockObjects(); | |
529 | ||
530 | #if wxUSE_WX_RESOURCES | |
531 | wxInitializeResourceSystem(); | |
532 | #endif | |
533 | ||
534 | wxBitmap::InitStandardHandlers(); | |
535 | ||
536 | wxModule::RegisterModules(); | |
03e11df5 | 537 | if (!wxModule::InitializeModules()) { |
519cb848 | 538 | return FALSE; |
03e11df5 | 539 | } |
e9576ca5 | 540 | |
2f1ae414 | 541 | wxMacCreateNotifierTable() ; |
9779893b | 542 | |
2f1ae414 | 543 | UMAShowArrowCursor() ; |
8461e4c2 | 544 | |
e9576ca5 SC |
545 | return TRUE; |
546 | } | |
547 | ||
548 | void wxApp::CleanUp() | |
549 | { | |
12cd5f34 | 550 | wxToolTip::RemoveToolTips() ; |
2f1ae414 SC |
551 | #if wxUSE_LOG |
552 | // flush the logged messages if any and install a 'safer' log target: the | |
553 | // default one (wxLogGui) can't be used after the resources are freed just | |
554 | // below and the user suppliedo ne might be even more unsafe (using any | |
555 | // wxWindows GUI function is unsafe starting from now) | |
556 | wxLog::DontCreateOnDemand(); | |
557 | ||
558 | // this will flush the old messages if any | |
559 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
560 | #endif // wxUSE_LOG | |
561 | ||
562 | // One last chance for pending objects to be cleaned up | |
563 | wxTheApp->DeletePendingObjects(); | |
564 | ||
fbbdb7cd | 565 | wxModule::CleanUpModules(); |
e9576ca5 SC |
566 | |
567 | #if wxUSE_WX_RESOURCES | |
fbbdb7cd | 568 | wxCleanUpResourceSystem(); |
e9576ca5 SC |
569 | #endif |
570 | ||
fbbdb7cd | 571 | wxDeleteStockObjects() ; |
e9576ca5 | 572 | |
2f1ae414 SC |
573 | // Destroy all GDI lists, etc. |
574 | wxDeleteStockLists(); | |
e9576ca5 | 575 | |
fbbdb7cd GD |
576 | delete wxTheColourDatabase; |
577 | wxTheColourDatabase = NULL; | |
e9576ca5 | 578 | |
fbbdb7cd | 579 | wxBitmap::CleanUpHandlers(); |
519cb848 | 580 | |
fbbdb7cd GD |
581 | delete[] wxBuffer; |
582 | wxBuffer = NULL; | |
12cd5f34 | 583 | |
fbbdb7cd GD |
584 | wxMacDestroyNotifierTable() ; |
585 | if (wxWinMacWindowList) { | |
586 | delete wxWinMacWindowList ; | |
587 | } | |
588 | if (wxWinMacControlList) { | |
589 | delete wxWinMacControlList ; | |
590 | } | |
2f1ae414 | 591 | delete wxPendingEvents; |
fbbdb7cd | 592 | |
2f1ae414 SC |
593 | #if wxUSE_THREADS |
594 | delete wxPendingEventsLocker; | |
595 | // If we don't do the following, we get an apparent memory leak. | |
596 | ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker(); | |
597 | #endif | |
598 | ||
fbbdb7cd | 599 | wxClassInfo::CleanUpClasses(); |
e9576ca5 | 600 | |
f5c6eb5c | 601 | #ifndef __DARWIN__ |
fbbdb7cd GD |
602 | # if __option(profile) |
603 | ProfilerDump( "\papp.prof" ) ; | |
604 | ProfilerTerm() ; | |
605 | # endif | |
9779893b | 606 | #endif |
519cb848 | 607 | |
fbbdb7cd GD |
608 | delete wxTheApp; |
609 | wxTheApp = NULL; | |
9779893b | 610 | |
e9576ca5 | 611 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT |
2f1ae414 SC |
612 | // At this point we want to check if there are any memory |
613 | // blocks that aren't part of the wxDebugContext itself, | |
614 | // as a special case. Then when dumping we need to ignore | |
615 | // wxDebugContext, too. | |
616 | if (wxDebugContext::CountObjectsLeft(TRUE) > 0) | |
617 | { | |
618 | wxLogDebug(wxT("There were memory leaks.")); | |
619 | wxDebugContext::Dump(); | |
620 | wxDebugContext::PrintStatistics(); | |
621 | } | |
622 | // wxDebugContext::SetStream(NULL, NULL); | |
e9576ca5 | 623 | #endif |
9779893b | 624 | |
2f1ae414 SC |
625 | #if wxUSE_LOG |
626 | // do it as the very last thing because everything else can log messages | |
627 | delete wxLog::SetActiveTarget(NULL); | |
628 | #endif // wxUSE_LOG | |
169935ad | 629 | |
fbbdb7cd GD |
630 | #if defined(WXMAKINGDLL) && defined(__DARWIN__) |
631 | // close shared library resources from here since we don't have | |
632 | // __wxterminate in Mach-O shared libraries | |
633 | wxStAppResource::CloseSharedLibraryResource(); | |
634 | #endif | |
635 | ||
8461e4c2 | 636 | UMACleanupToolbox() ; |
fbbdb7cd | 637 | if (s_macCursorRgn) { |
76a5e5d2 | 638 | ::DisposeRgn((RgnHandle)s_macCursorRgn); |
fbbdb7cd GD |
639 | } |
640 | ||
8461e4c2 VZ |
641 | #if 0 |
642 | TerminateAE() ; | |
643 | #endif | |
e9576ca5 SC |
644 | } |
645 | ||
92b002a4 RD |
646 | //---------------------------------------------------------------------- |
647 | // wxEntry | |
648 | //---------------------------------------------------------------------- | |
649 | ||
fbbdb7cd GD |
650 | // extern variable for shared library resource id |
651 | // need to be able to find it with NSLookupAndBindSymbol | |
652 | short gSharedLibraryResource = kResFileNotOpened ; | |
653 | ||
ed581ee2 | 654 | #if defined(WXMAKINGDLL) && defined(__DARWIN__) |
fbbdb7cd | 655 | CFBundleRef gSharedLibraryBundle = NULL; |
ed581ee2 | 656 | #endif /* WXMAKINGDLL && __DARWIN__ */ |
53fd991c SC |
657 | |
658 | wxStAppResource::wxStAppResource() | |
659 | { | |
fbbdb7cd GD |
660 | m_currentRefNum = CurResFile() ; |
661 | if ( gSharedLibraryResource != kResFileNotOpened ) | |
662 | { | |
663 | UseResFile( gSharedLibraryResource ) ; | |
664 | } | |
665 | } | |
666 | ||
667 | wxStAppResource::~wxStAppResource() | |
668 | { | |
669 | if ( m_currentRefNum != kResFileNotOpened ) | |
670 | { | |
671 | UseResFile( m_currentRefNum ) ; | |
672 | } | |
673 | } | |
674 | ||
675 | void wxStAppResource::OpenSharedLibraryResource(const void *initBlock) | |
676 | { | |
677 | gSharedLibraryResource = kResFileNotOpened; | |
678 | ||
679 | #ifdef WXMAKINGDLL | |
680 | if ( initBlock != NULL ) { | |
681 | const CFragInitBlock *theInitBlock = (const CFragInitBlock *)initBlock; | |
682 | FSSpec *fileSpec = NULL; | |
683 | ||
684 | if (theInitBlock->fragLocator.where == kDataForkCFragLocator) { | |
685 | fileSpec = theInitBlock->fragLocator.u.onDisk.fileSpec; | |
686 | } | |
687 | else if (theInitBlock->fragLocator.where == kResourceCFragLocator) { | |
688 | fileSpec = theInitBlock->fragLocator.u.inSegs.fileSpec; | |
689 | } | |
690 | ||
691 | if (fileSpec != NULL) { | |
692 | gSharedLibraryResource = FSpOpenResFile(fileSpec, fsRdPerm); | |
693 | } | |
694 | } | |
695 | else { | |
696 | #ifdef __DARWIN__ | |
697 | // Open the shared library resource file if it is not yet open | |
ed581ee2 GD |
698 | NSSymbol theSymbol; |
699 | NSModule theModule; | |
700 | const char *theLibPath; | |
701 | ||
fbbdb7cd GD |
702 | gSharedLibraryBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows")); |
703 | if (gSharedLibraryBundle != NULL) { | |
ed581ee2 GD |
704 | // wxWindows has been bundled into a framework |
705 | // load the framework resources | |
706 | ||
fbbdb7cd | 707 | gSharedLibraryResource = CFBundleOpenBundleResourceMap(gSharedLibraryBundle); |
ed581ee2 GD |
708 | } |
709 | else { | |
710 | // wxWindows is a simple dynamic shared library | |
711 | // load the resources from the data fork of a separate resource file | |
712 | char *theResPath; | |
713 | char *theName; | |
714 | char *theExt; | |
715 | FSRef theResRef; | |
716 | OSErr theErr = noErr; | |
717 | ||
718 | // get the library path | |
fbbdb7cd | 719 | theSymbol = NSLookupAndBindSymbol("_gSharedLibraryResource"); |
ed581ee2 GD |
720 | theModule = NSModuleForSymbol(theSymbol); |
721 | theLibPath = NSLibraryNameForModule(theModule); | |
ed581ee2 GD |
722 | |
723 | // allocate copy to replace .dylib.* extension with .rsrc | |
724 | theResPath = strdup(theLibPath); | |
725 | if (theResPath != NULL) { | |
726 | theName = strrchr(theResPath, '/'); | |
727 | if (theName == NULL) { | |
728 | // no directory elements in path | |
729 | theName = theResPath; | |
730 | } | |
731 | // find ".dylib" shared library extension | |
732 | theExt = strstr(theName, ".dylib"); | |
733 | // overwrite extension with ".rsrc" | |
734 | strcpy(theExt, ".rsrc"); | |
735 | ||
736 | wxLogDebug( theResPath ); | |
737 | ||
738 | theErr = FSPathMakeRef((UInt8 *) theResPath, &theResRef, false); | |
739 | if (theErr != noErr) { | |
740 | // try in current directory (using name only) | |
741 | theErr = FSPathMakeRef((UInt8 *) theName, &theResRef, false); | |
742 | } | |
743 | ||
744 | // free duplicated resource file path | |
745 | free(theResPath); | |
746 | ||
747 | // open the resource file | |
748 | if (theErr == noErr) { | |
749 | theErr = FSOpenResourceFile( &theResRef, 0, NULL, fsRdPerm, | |
fbbdb7cd | 750 | &gSharedLibraryResource); |
ed581ee2 GD |
751 | } |
752 | } | |
753 | } | |
fbbdb7cd | 754 | #endif /* __DARWIN__ */ |
53fd991c | 755 | } |
fbbdb7cd | 756 | #endif /* WXMAKINGDLL */ |
53fd991c SC |
757 | } |
758 | ||
fbbdb7cd | 759 | void wxStAppResource::CloseSharedLibraryResource() |
53fd991c | 760 | { |
fbbdb7cd | 761 | #ifdef WXMAKINGDLL |
ed581ee2 | 762 | // Close the shared library resource file |
fbbdb7cd GD |
763 | if (gSharedLibraryResource != kResFileNotOpened) { |
764 | #ifdef __DARWIN__ | |
765 | if (gSharedLibraryBundle != NULL) { | |
766 | CFBundleCloseBundleResourceMap(gSharedLibraryBundle, | |
767 | gSharedLibraryResource); | |
768 | gSharedLibraryBundle = NULL; | |
ed581ee2 | 769 | } |
fbbdb7cd GD |
770 | else |
771 | #endif /* __DARWIN__ */ | |
772 | { | |
773 | CloseResFile(gSharedLibraryResource); | |
ed581ee2 | 774 | } |
fbbdb7cd GD |
775 | gSharedLibraryResource = kResFileNotOpened; |
776 | } | |
777 | #endif /* WXMAKINGDLL */ | |
53fd991c SC |
778 | } |
779 | ||
ed581ee2 | 780 | #if defined(WXMAKINGDLL) && !defined(__DARWIN__) |
53fd991c | 781 | |
ed581ee2 GD |
782 | // for shared libraries we have to manually get the correct resource |
783 | // ref num upon initializing and releasing when terminating, therefore | |
784 | // the __wxinitialize and __wxterminate must be used | |
53fd991c | 785 | |
53fd991c | 786 | extern "C" { |
fbbdb7cd GD |
787 | void __sinit(void); /* (generated by linker) */ |
788 | pascal OSErr __initialize(const CFragInitBlock *theInitBlock); | |
789 | pascal void __terminate(void); | |
53fd991c | 790 | } |
53fd991c SC |
791 | |
792 | pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock) | |
793 | { | |
fbbdb7cd GD |
794 | wxStAppResource::OpenSharedLibraryResource( theInitBlock ) ; |
795 | return __initialize( theInitBlock ) ; | |
53fd991c SC |
796 | } |
797 | ||
798 | pascal void __wxterminate(void) | |
799 | { | |
fbbdb7cd | 800 | wxStAppResource::CloseSharedLibraryResource() ; |
53fd991c SC |
801 | __terminate() ; |
802 | } | |
ed581ee2 GD |
803 | |
804 | #endif /* WXMAKINGDLL && !__DARWIN__ */ | |
53fd991c SC |
805 | |
806 | int WXDLLEXPORT wxEntryStart( int argc, char *argv[] ) | |
92b002a4 RD |
807 | { |
808 | return wxApp::Initialize(); | |
809 | } | |
810 | ||
53fd991c | 811 | int WXDLLEXPORT wxEntryInitGui() |
92b002a4 RD |
812 | { |
813 | return wxTheApp->OnInitGui(); | |
814 | } | |
815 | ||
53fd991c | 816 | void WXDLLEXPORT wxEntryCleanup() |
92b002a4 RD |
817 | { |
818 | wxApp::CleanUp(); | |
819 | } | |
820 | ||
2f1ae414 | 821 | int wxEntry( int argc, char *argv[] , bool enterLoop ) |
e9576ca5 | 822 | { |
7c551d95 SC |
823 | #ifdef __MWERKS__ |
824 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT | |
825 | // This seems to be necessary since there are 'rogue' | |
826 | // objects present at this point (perhaps global objects?) | |
827 | // Setting a checkpoint will ignore them as far as the | |
828 | // memory checking facility is concerned. | |
829 | // Of course you may argue that memory allocated in globals should be | |
830 | // checked, but this is a reasonable compromise. | |
831 | wxDebugContext::SetCheckpoint(); | |
832 | #endif | |
833 | #endif | |
92b002a4 | 834 | if (!wxEntryStart(argc, argv)) { |
2f1ae414 | 835 | return 0; |
03e11df5 | 836 | } |
2f1ae414 SC |
837 | // create the application object or ensure that one already exists |
838 | if (!wxTheApp) | |
e9576ca5 | 839 | { |
2f1ae414 SC |
840 | // The app may have declared a global application object, but we recommend |
841 | // the IMPLEMENT_APP macro is used instead, which sets an initializer | |
842 | // function for delayed, dynamic app object construction. | |
843 | wxCHECK_MSG( wxApp::GetInitializerFunction(), 0, | |
844 | wxT("No initializer - use IMPLEMENT_APP macro.") ); | |
9779893b | 845 | |
2f1ae414 SC |
846 | wxTheApp = (wxApp*) (*wxApp::GetInitializerFunction()) (); |
847 | } | |
9779893b | 848 | |
2f1ae414 | 849 | wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") ); |
e9576ca5 | 850 | |
e94e53ac GD |
851 | #ifdef __DARWIN__ |
852 | // Mac OS X passes a process serial number command line argument when | |
853 | // the application is launched from the Finder. This argument must be | |
854 | // removed from the command line arguments before being handled by the | |
855 | // application (otherwise applications would need to handle it) | |
856 | ||
857 | if (argc > 1) { | |
ed581ee2 GD |
858 | char theArg[6] = ""; |
859 | strncpy(theArg, argv[1], 5); | |
e94e53ac | 860 | |
ed581ee2 | 861 | if (strcmp(theArg, "-psn_") == 0) { |
e94e53ac GD |
862 | // assume the argument is always the only one and remove it |
863 | --argc; | |
864 | } | |
865 | } | |
866 | #else | |
89ebf1c9 | 867 | argc = 0 ; // currently we don't support files as parameters |
519cb848 | 868 | #endif |
89ebf1c9 | 869 | // we could try to get the open apple events here to adjust argc and argv better |
519cb848 | 870 | |
89ebf1c9 RR |
871 | wxTheApp->argc = argc; |
872 | wxTheApp->argv = argv; | |
9779893b | 873 | |
89ebf1c9 RR |
874 | // GUI-specific initialization, such as creating an app context. |
875 | wxEntryInitGui(); | |
9779893b | 876 | |
89ebf1c9 RR |
877 | // Here frames insert themselves automatically |
878 | // into wxTopLevelWindows by getting created | |
879 | // in OnInit(). | |
9779893b | 880 | |
89ebf1c9 | 881 | int retValue = 0; |
9779893b | 882 | |
8461e4c2 VZ |
883 | if ( wxTheApp->OnInit() ) |
884 | { | |
885 | if ( enterLoop ) | |
886 | { | |
887 | retValue = wxTheApp->OnRun(); | |
888 | } | |
889 | else | |
890 | // We want to initialize, but not run or exit immediately. | |
891 | return 1; | |
892 | } | |
893 | //else: app initialization failed, so we skipped OnRun() | |
894 | ||
895 | wxWindow *topWindow = wxTheApp->GetTopWindow(); | |
896 | if ( topWindow ) | |
897 | { | |
898 | // Forcibly delete the window. | |
899 | if ( topWindow->IsKindOf(CLASSINFO(wxFrame)) || | |
900 | topWindow->IsKindOf(CLASSINFO(wxDialog)) ) | |
901 | { | |
902 | topWindow->Close(TRUE); | |
903 | wxTheApp->DeletePendingObjects(); | |
904 | } | |
905 | else | |
906 | { | |
907 | delete topWindow; | |
908 | wxTheApp->SetTopWindow(NULL); | |
909 | } | |
910 | } | |
911 | ||
912 | wxTheApp->OnExit(); | |
913 | ||
92b002a4 | 914 | wxEntryCleanup(); |
8461e4c2 VZ |
915 | |
916 | return retValue; | |
03e11df5 | 917 | } |
e9576ca5 SC |
918 | |
919 | // Static member initialization | |
2f1ae414 | 920 | wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL; |
e9576ca5 SC |
921 | |
922 | wxApp::wxApp() | |
923 | { | |
924 | m_topWindow = NULL; | |
925 | wxTheApp = this; | |
2f1ae414 | 926 | |
1b21409b JS |
927 | #if WXWIN_COMPATIBILITY_2_2 |
928 | m_wantDebugOutput = TRUE; | |
929 | #endif | |
2f1ae414 | 930 | |
e9576ca5 SC |
931 | argc = 0; |
932 | argv = NULL; | |
519cb848 | 933 | |
e9576ca5 | 934 | m_printMode = wxPRINT_WINDOWS; |
e9576ca5 SC |
935 | m_exitOnFrameDelete = TRUE; |
936 | m_auto3D = TRUE; | |
937 | } | |
938 | ||
939 | bool wxApp::Initialized() | |
940 | { | |
519cb848 | 941 | if (GetTopWindow()) |
e9576ca5 | 942 | return TRUE; |
519cb848 SC |
943 | else |
944 | return FALSE; | |
e9576ca5 SC |
945 | } |
946 | ||
947 | int wxApp::MainLoop() | |
948 | { | |
949 | m_keepGoing = TRUE; | |
950 | ||
e9576ca5 SC |
951 | while (m_keepGoing) |
952 | { | |
8461e4c2 | 953 | MacDoOneEvent() ; |
e9576ca5 | 954 | } |
e9576ca5 SC |
955 | |
956 | return 0; | |
957 | } | |
958 | ||
959 | // Returns TRUE if more time is needed. | |
960 | bool wxApp::ProcessIdle() | |
961 | { | |
962 | wxIdleEvent event; | |
963 | event.SetEventObject(this); | |
964 | ProcessEvent(event); | |
965 | ||
966 | return event.MoreRequested(); | |
967 | } | |
968 | ||
969 | void wxApp::ExitMainLoop() | |
970 | { | |
971 | m_keepGoing = FALSE; | |
972 | } | |
973 | ||
974 | // Is a message/event pending? | |
975 | bool wxApp::Pending() | |
976 | { | |
8461e4c2 | 977 | EventRecord event ; |
519cb848 SC |
978 | |
979 | return EventAvail( everyEvent , &event ) ; | |
e9576ca5 SC |
980 | } |
981 | ||
982 | // Dispatch a message. | |
983 | void wxApp::Dispatch() | |
984 | { | |
8461e4c2 | 985 | MacDoOneEvent() ; |
e9576ca5 SC |
986 | } |
987 | ||
988 | void wxApp::OnIdle(wxIdleEvent& event) | |
989 | { | |
2f1ae414 | 990 | static bool s_inOnIdle = FALSE; |
e9576ca5 | 991 | |
2f1ae414 SC |
992 | // Avoid recursion (via ProcessEvent default case) |
993 | if ( s_inOnIdle ) | |
994 | return; | |
e9576ca5 | 995 | |
2f1ae414 SC |
996 | |
997 | s_inOnIdle = TRUE; | |
e9576ca5 SC |
998 | |
999 | // 'Garbage' collection of windows deleted with Close(). | |
1000 | DeletePendingObjects(); | |
1001 | ||
1002 | // flush the logged messages if any | |
1003 | wxLog *pLog = wxLog::GetActiveTarget(); | |
1004 | if ( pLog != NULL && pLog->HasPendingMessages() ) | |
1005 | pLog->Flush(); | |
1006 | ||
1007 | // Send OnIdle events to all windows | |
1008 | bool needMore = SendIdleEvents(); | |
1009 | ||
1010 | if (needMore) | |
1011 | event.RequestMore(TRUE); | |
1012 | ||
2f1ae414 SC |
1013 | // If they are pending events, we must process them: pending events are |
1014 | // either events to the threads other than main or events posted with | |
1015 | // wxPostEvent() functions | |
1016 | wxMacProcessNotifierAndPendingEvents(); | |
1017 | ||
1018 | s_inOnIdle = FALSE; | |
e9576ca5 SC |
1019 | } |
1020 | ||
9779893b RD |
1021 | void wxWakeUpIdle() |
1022 | { | |
8461e4c2 | 1023 | wxMacWakeUp() ; |
9779893b RD |
1024 | } |
1025 | ||
e9576ca5 SC |
1026 | // Send idle event to all top-level windows |
1027 | bool wxApp::SendIdleEvents() | |
1028 | { | |
1029 | bool needMore = FALSE; | |
8461e4c2 VZ |
1030 | wxNode* node = wxTopLevelWindows.First(); |
1031 | while (node) | |
1032 | { | |
1033 | wxWindow* win = (wxWindow*) node->Data(); | |
1034 | if (SendIdleEvents(win)) | |
e9576ca5 SC |
1035 | needMore = TRUE; |
1036 | ||
8461e4c2 VZ |
1037 | node = node->Next(); |
1038 | } | |
e9576ca5 SC |
1039 | return needMore; |
1040 | } | |
1041 | ||
1042 | // Send idle event to window and all subwindows | |
1043 | bool wxApp::SendIdleEvents(wxWindow* win) | |
1044 | { | |
1045 | bool needMore = FALSE; | |
1046 | ||
8461e4c2 VZ |
1047 | wxIdleEvent event; |
1048 | event.SetEventObject(win); | |
1049 | win->ProcessEvent(event); | |
e9576ca5 SC |
1050 | |
1051 | if (event.MoreRequested()) | |
1052 | needMore = TRUE; | |
1053 | ||
8461e4c2 VZ |
1054 | wxNode* node = win->GetChildren().First(); |
1055 | while (node) | |
1056 | { | |
1057 | wxWindow* win = (wxWindow*) node->Data(); | |
1058 | if (SendIdleEvents(win)) | |
e9576ca5 SC |
1059 | needMore = TRUE; |
1060 | ||
8461e4c2 VZ |
1061 | node = node->Next(); |
1062 | } | |
e9576ca5 SC |
1063 | return needMore ; |
1064 | } | |
1065 | ||
1066 | void wxApp::DeletePendingObjects() | |
1067 | { | |
1068 | wxNode *node = wxPendingDelete.First(); | |
1069 | while (node) | |
1070 | { | |
1071 | wxObject *obj = (wxObject *)node->Data(); | |
9779893b | 1072 | |
e9576ca5 SC |
1073 | delete obj; |
1074 | ||
1075 | if (wxPendingDelete.Member(obj)) | |
1076 | delete node; | |
1077 | ||
1078 | // Deleting one object may have deleted other pending | |
1079 | // objects, so start from beginning of list again. | |
1080 | node = wxPendingDelete.First(); | |
1081 | } | |
1082 | } | |
1083 | ||
e9576ca5 SC |
1084 | void wxExit() |
1085 | { | |
2f1ae414 SC |
1086 | wxLogError(_("Fatal error: exiting")); |
1087 | ||
1088 | wxApp::CleanUp(); | |
8461e4c2 | 1089 | ::ExitToShell() ; |
e9576ca5 SC |
1090 | } |
1091 | ||
2f1ae414 SC |
1092 | void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event)) |
1093 | { | |
1094 | if (GetTopWindow()) | |
1095 | GetTopWindow()->Close(TRUE); | |
1096 | } | |
1097 | ||
1098 | // Default behaviour: close the application with prompts. The | |
1099 | // user can veto the close, and therefore the end session. | |
1100 | void wxApp::OnQueryEndSession(wxCloseEvent& event) | |
1101 | { | |
1102 | if (GetTopWindow()) | |
1103 | { | |
1104 | if (!GetTopWindow()->Close(!event.CanVeto())) | |
1105 | event.Veto(TRUE); | |
1106 | } | |
1107 | } | |
1108 | ||
1109 | extern "C" void wxCYield() ; | |
1110 | void wxCYield() | |
1111 | { | |
8461e4c2 | 1112 | wxYield() ; |
2f1ae414 SC |
1113 | } |
1114 | ||
e9576ca5 | 1115 | // Yield to other processes |
cb2713bf | 1116 | |
8461e4c2 | 1117 | bool wxApp::Yield(bool onlyIfNeeded) |
e9576ca5 | 1118 | { |
8461e4c2 VZ |
1119 | static bool s_inYield = FALSE; |
1120 | ||
1121 | if (s_inYield) | |
1122 | { | |
1123 | if ( !onlyIfNeeded ) | |
1124 | { | |
1125 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
1126 | } | |
1127 | ||
1128 | return FALSE; | |
1129 | } | |
1130 | ||
1131 | s_inYield = TRUE; | |
cb2713bf | 1132 | |
2f1ae414 | 1133 | #if wxUSE_THREADS |
cb2713bf | 1134 | YieldToAnyThread() ; |
2f1ae414 | 1135 | #endif |
cb2713bf | 1136 | EventRecord event ; |
2f1ae414 | 1137 | |
8461e4c2 | 1138 | long sleepTime = 1 ; //::GetCaretTime(); |
2f1ae414 | 1139 | |
76a5e5d2 | 1140 | while ( !wxTheApp->IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, (RgnHandle) wxApp::s_macCursorRgn)) |
8461e4c2 VZ |
1141 | { |
1142 | wxTheApp->MacHandleOneEvent( &event ); | |
1143 | if ( event.what != kHighLevelEvent ) | |
76a5e5d2 | 1144 | SetRectRgn( (RgnHandle) wxApp::s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ; |
8461e4c2 | 1145 | } |
2f1ae414 | 1146 | |
8461e4c2 | 1147 | wxMacProcessNotifierAndPendingEvents() ; |
cb2713bf | 1148 | |
8461e4c2 | 1149 | s_inYield = FALSE; |
cb2713bf | 1150 | |
8461e4c2 | 1151 | return TRUE; |
169935ad SC |
1152 | } |
1153 | ||
9779893b | 1154 | // platform specifics |
169935ad | 1155 | |
519cb848 SC |
1156 | void wxApp::MacSuspend( bool convertClipboard ) |
1157 | { | |
1c469f7f | 1158 | // we have to deactive the top level windows manually |
8461e4c2 | 1159 | |
1c469f7f SC |
1160 | wxNode* node = wxTopLevelWindows.First(); |
1161 | while (node) | |
1162 | { | |
1163 | wxTopLevelWindow* win = (wxTopLevelWindow*) node->Data(); | |
1164 | win->MacActivate( MacGetCurrentEvent() , false ) ; | |
8461e4c2 | 1165 | |
1c469f7f SC |
1166 | node = node->Next(); |
1167 | } | |
1168 | ||
1169 | s_lastMouseDown = 0 ; | |
1170 | if( convertClipboard ) | |
1171 | { | |
1172 | MacConvertPrivateToPublicScrap() ; | |
1173 | } | |
8461e4c2 | 1174 | |
1c469f7f | 1175 | ::HideFloatingWindows() ; |
519cb848 SC |
1176 | } |
1177 | ||
1178 | void wxApp::MacResume( bool convertClipboard ) | |
1179 | { | |
8461e4c2 VZ |
1180 | s_lastMouseDown = 0 ; |
1181 | if( convertClipboard ) | |
1182 | { | |
1183 | MacConvertPublicToPrivateScrap() ; | |
1184 | } | |
9779893b | 1185 | |
72055702 | 1186 | ::ShowFloatingWindows() ; |
519cb848 SC |
1187 | } |
1188 | ||
1189 | void wxApp::MacConvertPrivateToPublicScrap() | |
1190 | { | |
519cb848 SC |
1191 | } |
1192 | ||
1193 | void wxApp::MacConvertPublicToPrivateScrap() | |
1194 | { | |
519cb848 SC |
1195 | } |
1196 | ||
9779893b | 1197 | void wxApp::MacDoOneEvent() |
169935ad SC |
1198 | { |
1199 | EventRecord event ; | |
1200 | ||
6264b550 | 1201 | long sleepTime = 1; // GetCaretTime() / 4 ; |
169935ad | 1202 | |
76a5e5d2 | 1203 | if (WaitNextEvent(everyEvent, &event, sleepTime, (RgnHandle) s_macCursorRgn)) |
8461e4c2 | 1204 | { |
6264b550 | 1205 | MacHandleOneEvent( &event ); |
8461e4c2 VZ |
1206 | } |
1207 | else | |
1208 | { | |
1209 | // idlers | |
72055702 | 1210 | WindowPtr window = ::FrontWindow() ; |
8461e4c2 | 1211 | if ( window ) |
72055702 | 1212 | ::IdleControls( window ) ; |
9779893b | 1213 | |
8461e4c2 VZ |
1214 | wxTheApp->ProcessIdle() ; |
1215 | } | |
1216 | if ( event.what != kHighLevelEvent ) | |
76a5e5d2 | 1217 | SetRectRgn( (RgnHandle) s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ; |
519cb848 | 1218 | |
8461e4c2 | 1219 | // repeaters |
519cb848 | 1220 | |
6264b550 | 1221 | DeletePendingObjects() ; |
8461e4c2 | 1222 | wxMacProcessNotifierAndPendingEvents() ; |
169935ad SC |
1223 | } |
1224 | ||
76a5e5d2 | 1225 | void wxApp::MacHandleOneEvent( WXEVENTREF evr ) |
169935ad | 1226 | { |
76a5e5d2 | 1227 | EventRecord* ev = (EventRecord*) evr ; |
8461e4c2 VZ |
1228 | m_macCurrentEvent = ev ; |
1229 | ||
1230 | wxApp::sm_lastMessageTime = ev->when ; | |
1231 | ||
1232 | switch (ev->what) | |
1233 | { | |
1234 | case mouseDown: | |
1235 | MacHandleMouseDownEvent( ev ) ; | |
1236 | if ( ev->modifiers & controlKey ) | |
1237 | s_lastMouseDown = 2; | |
1238 | else | |
1239 | s_lastMouseDown = 1; | |
1240 | break; | |
1241 | case mouseUp: | |
1242 | if ( s_lastMouseDown == 2 ) | |
1243 | { | |
1244 | ev->modifiers |= controlKey ; | |
1245 | } | |
1246 | else | |
1247 | { | |
1248 | ev->modifiers &= ~controlKey ; | |
1249 | } | |
1250 | MacHandleMouseUpEvent( ev ) ; | |
1251 | s_lastMouseDown = 0; | |
1252 | break; | |
1253 | case activateEvt: | |
1254 | MacHandleActivateEvent( ev ) ; | |
1255 | break; | |
1256 | case updateEvt: | |
1257 | MacHandleUpdateEvent( ev ) ; | |
1258 | break; | |
1259 | case keyDown: | |
1260 | case autoKey: | |
1261 | MacHandleKeyDownEvent( ev ) ; | |
1262 | break; | |
1263 | case keyUp: | |
1264 | MacHandleKeyUpEvent( ev ) ; | |
1265 | break; | |
1266 | case diskEvt: | |
1267 | MacHandleDiskEvent( ev ) ; | |
1268 | break; | |
1269 | case osEvt: | |
1270 | MacHandleOSEvent( ev ) ; | |
1271 | break; | |
1272 | case kHighLevelEvent: | |
1273 | MacHandleHighLevelEvent( ev ) ; | |
1274 | break; | |
1275 | default: | |
1276 | break; | |
1277 | } | |
1278 | wxMacProcessNotifierAndPendingEvents() ; | |
169935ad SC |
1279 | } |
1280 | ||
76a5e5d2 | 1281 | void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr ) |
169935ad | 1282 | { |
76a5e5d2 | 1283 | EventRecord* ev = (EventRecord*) evr ; |
8461e4c2 | 1284 | ::AEProcessAppleEvent( ev ) ; |
169935ad SC |
1285 | } |
1286 | ||
519cb848 SC |
1287 | bool s_macIsInModalLoop = false ; |
1288 | ||
76a5e5d2 | 1289 | void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr ) |
169935ad | 1290 | { |
76a5e5d2 | 1291 | EventRecord* ev = (EventRecord*) evr ; |
8461e4c2 | 1292 | wxToolTip::RemoveToolTips() ; |
2f1ae414 | 1293 | |
8461e4c2 | 1294 | WindowRef window; |
72055702 | 1295 | WindowRef frontWindow = ::FrontNonFloatingWindow() ; |
8461e4c2 VZ |
1296 | WindowAttributes frontWindowAttributes = NULL ; |
1297 | if ( frontWindow ) | |
72055702 | 1298 | ::GetWindowAttributes( frontWindow , &frontWindowAttributes ) ; |
9779893b | 1299 | |
8461e4c2 | 1300 | short windowPart = ::FindWindow(ev->where, &window); |
a3722eeb | 1301 | wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ; |
0a67a93b SC |
1302 | if ( wxPendingDelete.Member(win) ) |
1303 | return ; | |
9779893b | 1304 | |
8461e4c2 VZ |
1305 | BitMap screenBits; |
1306 | GetQDGlobalsScreenBits( &screenBits ); | |
1307 | ||
1308 | switch (windowPart) | |
1309 | { | |
1310 | case inMenuBar : | |
1311 | if ( s_macIsInModalLoop ) | |
1312 | { | |
1313 | SysBeep ( 30 ) ; | |
1314 | } | |
1315 | else | |
1316 | { | |
1317 | UInt32 menuresult = MenuSelect(ev->where) ; | |
1318 | MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ); | |
1319 | s_lastMouseDown = 0; | |
1320 | } | |
1321 | break ; | |
2f1ae414 | 1322 | #if !TARGET_CARBON |
8461e4c2 VZ |
1323 | case inSysWindow : |
1324 | SystemClick( ev , window ) ; | |
1325 | s_lastMouseDown = 0; | |
1326 | break ; | |
2f1ae414 | 1327 | #endif |
8461e4c2 VZ |
1328 | case inDrag : |
1329 | if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) ) | |
1330 | { | |
1331 | SysBeep ( 30 ) ; | |
1332 | } | |
1333 | else | |
1334 | { | |
1335 | DragWindow(window, ev->where, &screenBits.bounds); | |
1336 | if (win) | |
1337 | { | |
1338 | GrafPtr port ; | |
1339 | GetPort( &port ) ; | |
1340 | Point pt = { 0, 0 } ; | |
66a09d47 | 1341 | SetPortWindowPort(window) ; |
8461e4c2 VZ |
1342 | LocalToGlobal( &pt ) ; |
1343 | SetPort( port ) ; | |
1344 | win->SetSize( pt.h , pt.v , -1 , | |
1345 | -1 , wxSIZE_USE_EXISTING); | |
1346 | } | |
1347 | s_lastMouseDown = 0; | |
1348 | } | |
1349 | break ; | |
1350 | case inGoAway: | |
1351 | if (TrackGoAway(window, ev->where)) | |
1352 | { | |
1353 | if ( win ) | |
1354 | win->Close() ; | |
1355 | } | |
1356 | s_lastMouseDown = 0; | |
1357 | break; | |
1358 | case inGrow: | |
1359 | { | |
1360 | int growResult = GrowWindow(window , ev->where, &screenBits.bounds); | |
1361 | if (growResult != 0) | |
1362 | { | |
1363 | int newWidth = LoWord(growResult); | |
1364 | int newHeight = HiWord(growResult); | |
1365 | int oldWidth, oldHeight; | |
1366 | ||
1367 | ||
1368 | if (win) | |
1369 | { | |
1370 | win->GetSize(&oldWidth, &oldHeight); | |
1371 | if (newWidth == 0) | |
1372 | newWidth = oldWidth; | |
1373 | if (newHeight == 0) | |
1374 | newHeight = oldHeight; | |
1375 | win->SetSize( -1, -1, newWidth, newHeight, wxSIZE_USE_EXISTING); | |
1376 | } | |
1377 | } | |
1378 | s_lastMouseDown = 0; | |
1379 | } | |
1380 | break; | |
1381 | case inZoomIn: | |
1382 | case inZoomOut: | |
1383 | if (TrackBox(window, ev->where, windowPart)) | |
1384 | { | |
1385 | // TODO setup size event | |
1386 | ZoomWindow( window , windowPart , false ) ; | |
1387 | if (win) | |
1388 | { | |
1389 | Rect tempRect ; | |
1390 | ||
1391 | GetWindowPortBounds(window, &tempRect ) ; | |
1392 | win->SetSize( -1, -1, tempRect.right-tempRect.left , | |
1393 | tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING); | |
1394 | } | |
1395 | } | |
1396 | s_lastMouseDown = 0; | |
1397 | break; | |
1398 | case inCollapseBox : | |
1399 | // TODO setup size event | |
1400 | s_lastMouseDown = 0; | |
1401 | break ; | |
1402 | ||
1403 | case inContent : | |
1404 | { | |
1405 | GrafPtr port ; | |
1406 | GetPort( &port ) ; | |
66a09d47 | 1407 | SetPortWindowPort(window) ; |
8461e4c2 VZ |
1408 | SetPort( port ) ; |
1409 | } | |
1410 | if ( window != frontWindow && wxTheApp->s_captureWindow == NULL ) | |
1411 | { | |
1412 | if ( s_macIsInModalLoop ) | |
1413 | { | |
1414 | SysBeep ( 30 ) ; | |
1415 | } | |
1416 | else if ( UMAIsWindowFloating( window ) ) | |
1417 | { | |
1418 | if ( win ) | |
1419 | win->MacMouseDown( ev , windowPart ) ; | |
1420 | } | |
1421 | else | |
1422 | { | |
1423 | if ( win ) | |
1424 | win->MacMouseDown( ev , windowPart ) ; | |
72055702 | 1425 | ::SelectWindow( window ) ; |
8461e4c2 VZ |
1426 | } |
1427 | } | |
1428 | else | |
1429 | { | |
1430 | if ( win ) | |
1431 | win->MacMouseDown( ev , windowPart ) ; | |
1432 | } | |
1433 | break ; | |
1434 | ||
1435 | default: | |
1436 | break; | |
1437 | } | |
169935ad SC |
1438 | } |
1439 | ||
76a5e5d2 | 1440 | void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr ) |
169935ad | 1441 | { |
76a5e5d2 | 1442 | EventRecord* ev = (EventRecord*) evr ; |
8461e4c2 VZ |
1443 | WindowRef window; |
1444 | ||
4f5a3250 SC |
1445 | short windowPart = inNoWindow ; |
1446 | if ( wxTheApp->s_captureWindow ) | |
1447 | { | |
1448 | window = (WindowRef) s_captureWindow->MacGetRootWindow() ; | |
1449 | windowPart = inContent ; | |
1450 | } | |
1451 | else | |
1452 | { | |
1453 | windowPart = ::FindWindow(ev->where, &window) ; | |
1454 | } | |
8461e4c2 VZ |
1455 | |
1456 | switch (windowPart) | |
1457 | { | |
1458 | case inMenuBar : | |
1459 | break ; | |
1460 | case inSysWindow : | |
1461 | break ; | |
1462 | default: | |
1463 | { | |
a3722eeb | 1464 | wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ; |
8461e4c2 VZ |
1465 | if ( win ) |
1466 | win->MacMouseUp( ev , windowPart ) ; | |
1467 | } | |
1468 | break; | |
1469 | } | |
169935ad SC |
1470 | } |
1471 | ||
72055702 | 1472 | long wxMacTranslateKey(unsigned char key, unsigned char code) ; |
7c551d95 | 1473 | long wxMacTranslateKey(unsigned char key, unsigned char code) |
9779893b | 1474 | { |
8461e4c2 | 1475 | long retval = key ; |
9779893b | 1476 | switch (key) |
519cb848 | 1477 | { |
8461e4c2 VZ |
1478 | case 0x01 : |
1479 | retval = WXK_HOME; | |
1480 | break; | |
1481 | case 0x03 : | |
1482 | retval = WXK_RETURN; | |
1483 | break; | |
1484 | case 0x04 : | |
1485 | retval = WXK_END; | |
1486 | break; | |
1487 | case 0x05 : | |
1488 | retval = WXK_HELP; | |
1489 | break; | |
1490 | case 0x08 : | |
1491 | retval = WXK_BACK; | |
1492 | break; | |
1493 | case 0x09 : | |
1494 | retval = WXK_TAB; | |
1495 | break; | |
1496 | case 0x0b : | |
1497 | retval = WXK_PAGEUP; | |
1498 | break; | |
1499 | case 0x0c : | |
1500 | retval = WXK_PAGEDOWN; | |
1501 | break; | |
1502 | case 0x0d : | |
1503 | retval = WXK_RETURN; | |
1504 | break; | |
1505 | case 0x10 : | |
1506 | { | |
1507 | switch( code ) | |
1508 | { | |
1509 | case 0x7a : | |
1510 | retval = WXK_F1 ; | |
1511 | break; | |
1512 | case 0x78 : | |
1513 | retval = WXK_F2 ; | |
1514 | break; | |
1515 | case 0x63 : | |
1516 | retval = WXK_F3 ; | |
1517 | break; | |
1518 | case 0x76 : | |
1519 | retval = WXK_F4 ; | |
1520 | break; | |
1521 | case 0x60 : | |
1522 | retval = WXK_F5 ; | |
1523 | break; | |
1524 | case 0x61 : | |
1525 | retval = WXK_F6 ; | |
1526 | break; | |
1527 | case 0x62: | |
1528 | retval = WXK_F7 ; | |
1529 | break; | |
1530 | case 0x64 : | |
1531 | retval = WXK_F8 ; | |
1532 | break; | |
1533 | case 0x65 : | |
1534 | retval = WXK_F9 ; | |
1535 | break; | |
1536 | case 0x6D : | |
1537 | retval = WXK_F10 ; | |
1538 | break; | |
1539 | case 0x67 : | |
1540 | retval = WXK_F11 ; | |
1541 | break; | |
1542 | case 0x6F : | |
1543 | retval = WXK_F12 ; | |
1544 | break; | |
1545 | case 0x69 : | |
1546 | retval = WXK_F13 ; | |
1547 | break; | |
1548 | case 0x6B : | |
1549 | retval = WXK_F14 ; | |
1550 | break; | |
1551 | case 0x71 : | |
1552 | retval = WXK_F15 ; | |
1553 | break; | |
1554 | } | |
1555 | } | |
1556 | break ; | |
1557 | case 0x1b : | |
1558 | retval = WXK_ESCAPE ; | |
1559 | break ; | |
1560 | case 0x1c : | |
1561 | retval = WXK_LEFT ; | |
1562 | break ; | |
1563 | case 0x1d : | |
1564 | retval = WXK_RIGHT ; | |
1565 | break ; | |
1566 | case 0x1e : | |
1567 | retval = WXK_UP ; | |
1568 | break ; | |
1569 | case 0x1f : | |
1570 | retval = WXK_DOWN ; | |
1571 | break ; | |
1572 | case 0x7F : | |
1573 | retval = WXK_DELETE ; | |
1574 | default: | |
1575 | break ; | |
1576 | } // end switch | |
1577 | ||
1578 | return retval; | |
169935ad SC |
1579 | } |
1580 | ||
76a5e5d2 | 1581 | void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr ) |
169935ad | 1582 | { |
76a5e5d2 | 1583 | EventRecord* ev = (EventRecord*) evr ; |
8461e4c2 VZ |
1584 | wxToolTip::RemoveToolTips() ; |
1585 | ||
1586 | UInt32 menuresult = UMAMenuEvent(ev) ; | |
1587 | if ( HiWord( menuresult ) ) | |
1588 | { | |
1589 | if ( !s_macIsInModalLoop ) | |
1590 | MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ; | |
1591 | } | |
1592 | else | |
1593 | { | |
1594 | short keycode ; | |
1595 | short keychar ; | |
1596 | keychar = short(ev->message & charCodeMask); | |
1597 | keycode = short(ev->message & keyCodeMask) >> 8 ; | |
e562df9b SC |
1598 | long keyval = wxMacTranslateKey(keychar, keycode) ; |
1599 | bool handled = false ; | |
8461e4c2 VZ |
1600 | wxWindow* focus = wxWindow::FindFocus() ; |
1601 | if ( focus ) | |
1602 | { | |
8461e4c2 VZ |
1603 | |
1604 | wxKeyEvent event(wxEVT_KEY_DOWN); | |
1605 | event.m_shiftDown = ev->modifiers & shiftKey; | |
1606 | event.m_controlDown = ev->modifiers & controlKey; | |
1607 | event.m_altDown = ev->modifiers & optionKey; | |
1608 | event.m_metaDown = ev->modifiers & cmdKey; | |
1609 | event.m_keyCode = keyval; | |
1610 | event.m_x = ev->where.h; | |
1611 | event.m_y = ev->where.v; | |
1612 | event.m_timeStamp = ev->when; | |
1613 | event.SetEventObject(focus); | |
e562df9b | 1614 | handled = focus->GetEventHandler()->ProcessEvent( event ) ; |
8461e4c2 VZ |
1615 | if ( !handled ) |
1616 | { | |
ba2928c6 | 1617 | #if wxUSE_ACCEL |
8461e4c2 VZ |
1618 | if (!handled) |
1619 | { | |
8461e4c2 | 1620 | /* |
ba2928c6 | 1621 | wxWindow *ancestor = focus; |
8461e4c2 VZ |
1622 | while (ancestor) |
1623 | { | |
1624 | int command = ancestor->GetAcceleratorTable()->GetCommand( event ); | |
1625 | if (command != -1) | |
1626 | { | |
1627 | wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command ); | |
1628 | handled = ancestor->GetEventHandler()->ProcessEvent( command_event ); | |
1629 | break; | |
1630 | } | |
1631 | if (ancestor->m_isFrame) | |
1632 | break; | |
1633 | ancestor = ancestor->GetParent(); | |
1634 | } | |
1635 | */ | |
1636 | } | |
ba2928c6 | 1637 | #endif // wxUSE_ACCEL |
8461e4c2 VZ |
1638 | } |
1639 | if (!handled) | |
1640 | { | |
1641 | wxKeyEvent event(wxEVT_CHAR); | |
1642 | event.m_shiftDown = ev->modifiers & shiftKey; | |
1643 | event.m_controlDown = ev->modifiers & controlKey; | |
1644 | event.m_altDown = ev->modifiers & optionKey; | |
1645 | event.m_metaDown = ev->modifiers & cmdKey; | |
1646 | event.m_keyCode = keyval; | |
1647 | event.m_x = ev->where.h; | |
1648 | event.m_y = ev->where.v; | |
1649 | event.m_timeStamp = ev->when; | |
1650 | event.SetEventObject(focus); | |
1651 | handled = focus->GetEventHandler()->ProcessEvent( event ) ; | |
1652 | } | |
1653 | if ( !handled && | |
1654 | (keyval == WXK_TAB) && | |
1655 | (!focus->HasFlag(wxTE_PROCESS_TAB)) && | |
1656 | (focus->GetParent()) && | |
1657 | (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) ) | |
1658 | { | |
1659 | wxNavigationKeyEvent new_event; | |
1660 | new_event.SetEventObject( focus ); | |
1661 | new_event.SetDirection( !event.ShiftDown() ); | |
1662 | /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */ | |
1663 | new_event.SetWindowChange( event.ControlDown() ); | |
1664 | new_event.SetCurrentFocus( focus ); | |
1665 | handled = focus->GetEventHandler()->ProcessEvent( new_event ); | |
1666 | } | |
e562df9b SC |
1667 | } |
1668 | if ( !handled ) | |
1669 | { | |
1670 | // if window is not having a focus still testing for default enter or cancel | |
1671 | // TODO add the UMA version for ActiveNonFloatingWindow | |
1672 | focus = wxFindWinFromMacWindow( FrontWindow() ) ; | |
1673 | if ( focus ) | |
1674 | { | |
1675 | if ( keyval == WXK_RETURN ) | |
1676 | { | |
1677 | wxButton *def = wxDynamicCast(focus->GetDefaultItem(), | |
1678 | wxButton); | |
1679 | if ( def && def->IsEnabled() ) | |
1680 | { | |
1681 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); | |
1682 | event.SetEventObject(def); | |
1683 | def->Command(event); | |
1684 | return ; | |
1685 | } | |
1686 | } | |
8461e4c2 | 1687 | /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */ |
e562df9b | 1688 | else if (keyval == WXK_ESCAPE || (keyval == '.' && ev->modifiers & cmdKey ) ) |
8461e4c2 | 1689 | { |
e562df9b SC |
1690 | wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL); |
1691 | new_event.SetEventObject( focus ); | |
1692 | handled = focus->GetEventHandler()->ProcessEvent( new_event ); | |
8461e4c2 | 1693 | } |
e562df9b | 1694 | } |
8461e4c2 VZ |
1695 | } |
1696 | } | |
169935ad SC |
1697 | } |
1698 | ||
76a5e5d2 | 1699 | void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr ) |
169935ad | 1700 | { |
76a5e5d2 | 1701 | EventRecord* ev = (EventRecord*) evr ; |
8461e4c2 VZ |
1702 | wxToolTip::RemoveToolTips() ; |
1703 | ||
1704 | UInt32 menuresult = UMAMenuEvent(ev) ; | |
1705 | if ( HiWord( menuresult ) ) | |
1706 | { | |
1707 | } | |
1708 | else | |
1709 | { | |
1710 | short keycode ; | |
1711 | short keychar ; | |
1712 | keychar = short(ev->message & charCodeMask); | |
1713 | keycode = short(ev->message & keyCodeMask) >> 8 ; | |
1714 | ||
1715 | wxWindow* focus = wxWindow::FindFocus() ; | |
1716 | if ( focus ) | |
1717 | { | |
1718 | long keyval = wxMacTranslateKey(keychar, keycode) ; | |
1719 | ||
1720 | wxKeyEvent event(wxEVT_KEY_UP); | |
1721 | event.m_shiftDown = ev->modifiers & shiftKey; | |
1722 | event.m_controlDown = ev->modifiers & controlKey; | |
1723 | event.m_altDown = ev->modifiers & optionKey; | |
1724 | event.m_metaDown = ev->modifiers & cmdKey; | |
1725 | event.m_keyCode = keyval; | |
1726 | event.m_x = ev->where.h; | |
1727 | event.m_y = ev->where.v; | |
1728 | event.m_timeStamp = ev->when; | |
1729 | event.SetEventObject(focus); | |
1730 | bool handled = focus->GetEventHandler()->ProcessEvent( event ) ; | |
1731 | } | |
1732 | } | |
169935ad SC |
1733 | } |
1734 | ||
76a5e5d2 | 1735 | void wxApp::MacHandleActivateEvent( WXEVENTREF evr ) |
169935ad | 1736 | { |
76a5e5d2 | 1737 | EventRecord* ev = (EventRecord*) evr ; |
8461e4c2 VZ |
1738 | WindowRef window = (WindowRef) ev->message ; |
1739 | if ( window ) | |
1740 | { | |
1741 | bool activate = (ev->modifiers & activeFlag ) ; | |
1742 | WindowClass wclass ; | |
72055702 | 1743 | ::GetWindowClass ( window , &wclass ) ; |
8461e4c2 VZ |
1744 | if ( wclass == kFloatingWindowClass ) |
1745 | { | |
1746 | // if it is a floater we activate/deactivate the front non-floating window instead | |
72055702 | 1747 | window = ::FrontNonFloatingWindow() ; |
8461e4c2 | 1748 | } |
a3722eeb | 1749 | wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ; |
8461e4c2 VZ |
1750 | if ( win ) |
1751 | win->MacActivate( ev , activate ) ; | |
1752 | } | |
169935ad SC |
1753 | } |
1754 | ||
76a5e5d2 | 1755 | void wxApp::MacHandleUpdateEvent( WXEVENTREF evr ) |
169935ad | 1756 | { |
76a5e5d2 | 1757 | EventRecord* ev = (EventRecord*) evr ; |
8461e4c2 | 1758 | WindowRef window = (WindowRef) ev->message ; |
a3722eeb | 1759 | wxTopLevelWindowMac * win = wxFindWinFromMacWindow( window ) ; |
8461e4c2 VZ |
1760 | if ( win ) |
1761 | { | |
0a67a93b | 1762 | if ( !wxPendingDelete.Member(win) ) |
a3722eeb | 1763 | win->MacUpdate( ev->when ) ; |
8461e4c2 VZ |
1764 | } |
1765 | else | |
1766 | { | |
1767 | // since there is no way of telling this foreign window to update itself | |
1768 | // we have to invalidate the update region otherwise we keep getting the same | |
1769 | // event over and over again | |
1770 | BeginUpdate( window ) ; | |
1771 | EndUpdate( window ) ; | |
1772 | } | |
169935ad SC |
1773 | } |
1774 | ||
76a5e5d2 | 1775 | void wxApp::MacHandleDiskEvent( WXEVENTREF evr ) |
169935ad | 1776 | { |
76a5e5d2 | 1777 | EventRecord* ev = (EventRecord*) evr ; |
8461e4c2 | 1778 | if ( HiWord( ev->message ) != noErr ) |
519cb848 | 1779 | { |
2f1ae414 | 1780 | #if !TARGET_CARBON |
8461e4c2 VZ |
1781 | OSErr err ; |
1782 | Point point ; | |
1783 | SetPt( &point , 100 , 100 ) ; | |
9779893b | 1784 | |
8461e4c2 VZ |
1785 | err = DIBadMount( point , ev->message ) ; |
1786 | wxASSERT( err == noErr ) ; | |
2f1ae414 | 1787 | #endif |
8461e4c2 | 1788 | } |
169935ad SC |
1789 | } |
1790 | ||
76a5e5d2 | 1791 | void wxApp::MacHandleOSEvent( WXEVENTREF evr ) |
169935ad | 1792 | { |
76a5e5d2 | 1793 | EventRecord* ev = (EventRecord*) evr ; |
8461e4c2 VZ |
1794 | switch( ( ev->message & osEvtMessageMask ) >> 24 ) |
1795 | { | |
1796 | case suspendResumeMessage : | |
1797 | { | |
1798 | bool isResuming = ev->message & resumeFlag ; | |
5fde6fcc | 1799 | #if !TARGET_CARBON |
8461e4c2 | 1800 | bool convertClipboard = ev->message & convertClipboardFlag ; |
5fde6fcc | 1801 | #else |
8461e4c2 | 1802 | bool convertClipboard = false; |
5fde6fcc | 1803 | #endif |
8461e4c2 VZ |
1804 | bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ; |
1805 | if ( isResuming ) | |
1806 | { | |
1807 | WindowRef oldFrontWindow = NULL ; | |
1808 | WindowRef newFrontWindow = NULL ; | |
1809 | ||
1810 | // in case we don't take care of activating ourselves, we have to synchronize | |
1811 | // our idea of the active window with the process manager's - which it already activated | |
1812 | ||
1813 | if ( !doesActivate ) | |
72055702 | 1814 | oldFrontWindow = ::FrontNonFloatingWindow() ; |
8461e4c2 VZ |
1815 | |
1816 | MacResume( convertClipboard ) ; | |
1817 | ||
72055702 | 1818 | newFrontWindow = ::FrontNonFloatingWindow() ; |
8461e4c2 VZ |
1819 | |
1820 | if ( oldFrontWindow ) | |
1821 | { | |
a3722eeb | 1822 | wxTopLevelWindowMac* win = wxFindWinFromMacWindow( oldFrontWindow ) ; |
8461e4c2 VZ |
1823 | if ( win ) |
1824 | win->MacActivate( ev , false ) ; | |
1825 | } | |
1826 | if ( newFrontWindow ) | |
1827 | { | |
a3722eeb | 1828 | wxTopLevelWindowMac* win = wxFindWinFromMacWindow( newFrontWindow ) ; |
8461e4c2 VZ |
1829 | if ( win ) |
1830 | win->MacActivate( ev , true ) ; | |
1831 | } | |
1832 | } | |
1833 | else | |
1834 | { | |
1835 | MacSuspend( convertClipboard ) ; | |
1836 | ||
1837 | // in case this suspending did close an active window, another one might | |
1838 | // have surfaced -> lets deactivate that one | |
1839 | ||
72055702 SC |
1840 | /* TODO : find out what to do on systems < 10 , perhaps FrontNonFloatingWindow |
1841 | WindowRef newActiveWindow = ::ActiveNonFloatingWindow() ; | |
8461e4c2 VZ |
1842 | if ( newActiveWindow ) |
1843 | { | |
1844 | wxWindow* win = wxFindWinFromMacWindow( newActiveWindow ) ; | |
1845 | if ( win ) | |
1846 | win->MacActivate( ev , false ) ; | |
1847 | } | |
72055702 | 1848 | */ |
8461e4c2 VZ |
1849 | } |
1850 | } | |
1851 | break ; | |
1852 | case mouseMovedMessage : | |
1853 | { | |
1854 | WindowRef window; | |
1855 | ||
1856 | wxWindow* currentMouseWindow = NULL ; | |
1857 | ||
1858 | wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) , | |
1859 | ¤tMouseWindow ) ; | |
8461e4c2 VZ |
1860 | if ( currentMouseWindow != wxWindow::s_lastMouseWindow ) |
1861 | { | |
1862 | wxMouseEvent event ; | |
1863 | ||
1864 | bool isDown = !(ev->modifiers & btnState) ; // 1 is for up | |
1865 | bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse | |
1866 | ||
1867 | event.m_leftDown = isDown && !controlDown; | |
1868 | event.m_middleDown = FALSE; | |
1869 | event.m_rightDown = isDown && controlDown; | |
1870 | event.m_shiftDown = ev->modifiers & shiftKey; | |
1871 | event.m_controlDown = ev->modifiers & controlKey; | |
1872 | event.m_altDown = ev->modifiers & optionKey; | |
1873 | event.m_metaDown = ev->modifiers & cmdKey; | |
1874 | event.m_x = ev->where.h; | |
1875 | event.m_y = ev->where.v; | |
1876 | event.m_timeStamp = ev->when; | |
1877 | event.SetEventObject(this); | |
ed60b502 | 1878 | |
8461e4c2 VZ |
1879 | if ( wxWindow::s_lastMouseWindow ) |
1880 | { | |
ed60b502 RR |
1881 | wxMouseEvent eventleave(event); |
1882 | eventleave.SetEventType( wxEVT_LEAVE_WINDOW ); | |
1883 | wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y ); | |
d1bdc41d | 1884 | eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ; |
ed60b502 | 1885 | |
8461e4c2 VZ |
1886 | wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave); |
1887 | } | |
1888 | if ( currentMouseWindow ) | |
1889 | { | |
ed60b502 RR |
1890 | wxMouseEvent evententer(event); |
1891 | evententer.SetEventType( wxEVT_ENTER_WINDOW ); | |
1892 | currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y ); | |
d1bdc41d | 1893 | evententer.SetEventObject( currentMouseWindow ) ; |
8461e4c2 VZ |
1894 | currentMouseWindow->GetEventHandler()->ProcessEvent(evententer); |
1895 | } | |
1896 | wxWindow::s_lastMouseWindow = currentMouseWindow ; | |
1897 | } | |
1898 | ||
1899 | short windowPart = ::FindWindow(ev->where, &window); | |
1900 | ||
1901 | switch (windowPart) | |
1902 | { | |
1903 | // fixes for setting the cursor back from dominic mazzoni | |
1904 | case inMenuBar : | |
1905 | UMAShowArrowCursor(); | |
1906 | break ; | |
1907 | case inSysWindow : | |
1908 | UMAShowArrowCursor(); | |
1909 | break ; | |
1910 | default: | |
1911 | { | |
6264b550 RR |
1912 | // if ( s_lastMouseDown == 0 ) |
1913 | // ev->modifiers |= btnState ; | |
1914 | ||
1915 | // Calling GetNextEvent with a zero event mask will always | |
1916 | // pass back a null event. However, it fills the EventRecord | |
1917 | // with the state of the modifier keys. This is needed since | |
1918 | // the modifier state returned by WaitForNextEvent often is | |
1919 | // wrong mouse move events. The attempt above to correct this | |
1920 | // didn't always work (under OS X at least). | |
1921 | ||
1922 | EventRecord tmp; | |
1923 | ::GetNextEvent(0, &tmp); | |
1924 | ev->modifiers = tmp.modifiers; | |
1925 | ||
a3722eeb | 1926 | wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ; |
8461e4c2 VZ |
1927 | if ( win ) |
1928 | win->MacMouseMoved( ev , windowPart ) ; | |
1929 | else | |
1930 | UMAShowArrowCursor(); | |
1931 | ||
1932 | } | |
1933 | break; | |
1934 | } | |
1935 | } | |
1936 | break ; | |
1937 | ||
1938 | } | |
169935ad SC |
1939 | } |
1940 | ||
519cb848 | 1941 | void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum ) |
169935ad | 1942 | { |
8461e4c2 VZ |
1943 | if (macMenuId == 0) |
1944 | return; // no menu item selected | |
1945 | ||
1946 | if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1) | |
1947 | { | |
1948 | #if ! TARGET_CARBON | |
1949 | Str255 deskAccessoryName ; | |
1950 | GrafPtr savedPort ; | |
1951 | ||
1952 | GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName); | |
1953 | GetPort(&savedPort); | |
1954 | OpenDeskAcc(deskAccessoryName); | |
1955 | SetPort(savedPort); | |
1956 | #endif | |
1957 | } | |
1958 | else | |
1959 | { | |
1960 | wxWindow* frontwindow = wxFindWinFromMacWindow( ::FrontWindow() ) ; | |
1961 | if ( frontwindow && wxMenuBar::MacGetInstalledMenuBar() ) | |
1962 | wxMenuBar::MacGetInstalledMenuBar()->MacMenuSelect( frontwindow->GetEventHandler() , 0 , macMenuId , macMenuItemNum ) ; | |
1963 | } | |
1964 | HiliteMenu(0); | |
169935ad SC |
1965 | } |
1966 | ||
519cb848 SC |
1967 | /* |
1968 | long wxApp::MacTranslateKey(char key, int mods) | |
169935ad | 1969 | { |
169935ad SC |
1970 | } |
1971 | ||
519cb848 | 1972 | void wxApp::MacAdjustCursor() |
169935ad | 1973 | { |
169935ad SC |
1974 | } |
1975 | ||
519cb848 SC |
1976 | */ |
1977 | /* | |
169935ad SC |
1978 | void |
1979 | wxApp::macAdjustCursor() | |
1980 | { | |
519cb848 | 1981 | if (ev->what != kHighLevelEvent) |
169935ad | 1982 | { |
8461e4c2 VZ |
1983 | wxWindow* theMacWxFrame = wxFrame::MacFindFrameOrDialog(::FrontWindow()); |
1984 | if (theMacWxFrame) | |
1985 | { | |
1986 | if (!theMacWxFrame->MacAdjustCursor(ev->where)) | |
1987 | ::SetCursor(&(qd.arrow)); | |
1988 | } | |
169935ad SC |
1989 | } |
1990 | } | |
9779893b | 1991 | */ |