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