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