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