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