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