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