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