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