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