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