]> git.saurik.com Git - wxWidgets.git/blame - src/mac/app.cpp
use LVSICF_NOINVALIDATEALL in SetItemCount() to reduce flicker
[wxWidgets.git] / src / mac / app.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: app.cpp
3// Purpose: wxApp
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
8461e4c2 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "app.h"
14#endif
15
df06d1a4
GD
16#include "wx/defs.h"
17
03e11df5 18#include "wx/window.h"
e9576ca5 19#include "wx/frame.h"
e8566d35 20#include "wx/button.h"
e9576ca5
SC
21#include "wx/app.h"
22#include "wx/utils.h"
23#include "wx/gdicmn.h"
24#include "wx/pen.h"
25#include "wx/brush.h"
26#include "wx/cursor.h"
ed5b9811 27#include "wx/intl.h"
e9576ca5
SC
28#include "wx/icon.h"
29#include "wx/palette.h"
30#include "wx/dc.h"
31#include "wx/dialog.h"
32#include "wx/msgdlg.h"
33#include "wx/log.h"
34#include "wx/module.h"
35#include "wx/memory.h"
2f1ae414 36#include "wx/tooltip.h"
b3c406a5 37#include "wx/textctrl.h"
03e11df5 38#include "wx/menu.h"
738a6daa
SC
39#include "wx/docview.h"
40
e9576ca5
SC
41#include <string.h>
42
8be97d65
SC
43// mac
44
f5c6eb5c 45#ifndef __DARWIN__
03e11df5 46 #if __option(profile)
8461e4c2 47 #include <profiler.h>
03e11df5 48 #endif
9779893b 49#endif
519cb848 50
8be97d65
SC
51#include "apprsrc.h"
52
03e11df5
GD
53#include "wx/mac/uma.h"
54#include "wx/mac/macnotfy.h"
2f1ae414 55
df06d1a4
GD
56#ifdef __DARWIN__
57# include <CoreServices/CoreServices.h>
21870a5d 58# if defined(WXMAKINGDLL_CORE)
ed581ee2
GD
59# include <mach-o/dyld.h>
60# endif
df06d1a4
GD
61#else
62# include <Sound.h>
63# include <Threads.h>
64# include <ToolUtils.h>
65# include <DiskInit.h>
66# include <Devices.h>
2f1ae414 67#endif
519cb848 68
e9576ca5 69extern wxList wxPendingDelete;
519cb848
SC
70extern wxList *wxWinMacWindowList;
71extern wxList *wxWinMacControlList;
e9576ca5 72
c5c9378c
SC
73// statics for implementation
74
738a6daa 75static bool s_inYield = FALSE;
c5c9378c
SC
76
77#if TARGET_CARBON
78static bool s_inReceiveEvent = FALSE ;
79static EventTime sleepTime = kEventDurationNoWait ;
80#else
81static long sleepTime = 0 ;
82#endif
738a6daa 83
2f1ae414 84#if !USE_SHARED_LIBRARY
e9576ca5
SC
85IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
86BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
87 EVT_IDLE(wxApp::OnIdle)
15b41e90
SC
88 EVT_END_SESSION(wxApp::OnEndSession)
89 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
e9576ca5 90END_EVENT_TABLE()
2f1ae414 91#endif
e9576ca5 92
169935ad 93
8461e4c2 94const short kMacMinHeap = (29 * 1024) ;
8be97d65 95// platform specific static variables
169935ad 96
519cb848
SC
97const short kwxMacMenuBarResource = 1 ;
98const short kwxMacAppleMenuId = 1 ;
99
fbbdb7cd
GD
100WXHRGN wxApp::s_macCursorRgn = NULL;
101wxWindow* wxApp::s_captureWindow = NULL ;
102int wxApp::s_lastMouseDown = 0 ;
103long wxApp::sm_lastMessageTime = 0;
41d368a4
SC
104long wxApp::s_lastModifiers = 0 ;
105
519cb848 106
fbbdb7cd
GD
107bool wxApp::s_macSupportPCMenuShortcuts = true ;
108long wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
882b0479 109long wxApp::s_macPreferencesMenuItemId = wxID_PREFERENCES ;
2b5f62a0 110long wxApp::s_macExitMenuItemId = wxID_EXIT ;
427ff662 111wxString wxApp::s_macHelpMenuTitleName = wxT("&Help") ;
519cb848 112
15b41e90
SC
113//----------------------------------------------------------------------
114// Core Apple Event Support
115//----------------------------------------------------------------------
116
72055702
SC
117pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
118pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
119pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
120pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
72055702 121
2a294857 122pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 123{
8461e4c2 124 return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ;
519cb848
SC
125}
126
2a294857 127pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 128{
8461e4c2 129 return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ;
519cb848
SC
130}
131
2a294857 132pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 133{
8461e4c2 134 return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ;
519cb848
SC
135}
136
2a294857 137pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 138{
8461e4c2 139 return wxTheApp->MacHandleAEQuit( (AppleEvent*) event , reply) ;
519cb848
SC
140}
141
15b41e90 142// AEODoc Calls MacOpenFile on each of the files passed
5ab6aab8 143
2072fbbb
SC
144short wxApp::MacHandleAEODoc(const WXEVENTREF event, WXEVENTREF WXUNUSED(reply))
145{
146 AEDescList docList;
147 AEKeyword keywd;
148 DescType returnedType;
149 Size actualSize;
150 long itemsInList;
151 FSSpec theSpec;
152 OSErr err;
153 short i;
154 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
155 if (err != noErr)
156 return err;
7c9955d1 157
2072fbbb
SC
158 err = AECountItems(&docList, &itemsInList);
159 if (err != noErr)
160 return err;
7c9955d1 161
8461e4c2
VZ
162 ProcessSerialNumber PSN ;
163 PSN.highLongOfPSN = 0 ;
164 PSN.lowLongOfPSN = kCurrentProcess ;
165 SetFrontProcess( &PSN ) ;
7c9955d1 166
2072fbbb
SC
167 for (i = 1; i <= itemsInList; i++) {
168 AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
169 (Ptr) & theSpec, sizeof(theSpec), &actualSize);
170 wxString fName = wxMacFSSpec2MacFilename(&theSpec);
171 MacOpenFile(fName);
172 }
173 return noErr;
519cb848
SC
174}
175
15b41e90
SC
176// AEPDoc Calls MacPrintFile on each of the files passed
177
2072fbbb 178short wxApp::MacHandleAEPDoc(const WXEVENTREF event , WXEVENTREF WXUNUSED(reply))
519cb848 179{
2072fbbb
SC
180 AEDescList docList;
181 AEKeyword keywd;
182 DescType returnedType;
183 Size actualSize;
184 long itemsInList;
185 FSSpec theSpec;
186 OSErr err;
187 short i;
188 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
189 if (err != noErr)
190 return err;
7c9955d1 191
2072fbbb
SC
192 err = AECountItems(&docList, &itemsInList);
193 if (err != noErr)
194 return err;
7c9955d1 195
2072fbbb
SC
196 ProcessSerialNumber PSN ;
197 PSN.highLongOfPSN = 0 ;
198 PSN.lowLongOfPSN = kCurrentProcess ;
199 SetFrontProcess( &PSN ) ;
7c9955d1 200
2072fbbb
SC
201 for (i = 1; i <= itemsInList; i++) {
202 AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
203 (Ptr) & theSpec, sizeof(theSpec), &actualSize);
204 wxString fName = wxMacFSSpec2MacFilename(&theSpec);
205 MacPrintFile(fName);
206 }
207 return noErr;
519cb848
SC
208}
209
15b41e90
SC
210// AEOApp calls MacNewFile
211
2a294857 212short wxApp::MacHandleAEOApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
519cb848 213{
2072fbbb 214 MacNewFile() ;
8461e4c2 215 return noErr ;
519cb848
SC
216}
217
76a60a81 218// AEQuit attempts to quit the application
15b41e90 219
2a294857 220short wxApp::MacHandleAEQuit(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
519cb848 221{
8f793b40 222 wxWindow* win = GetTopWindow() ;
8461e4c2
VZ
223 if ( win )
224 {
225 win->Close(TRUE ) ;
226 }
8f793b40 227 else
8461e4c2
VZ
228 {
229 ExitMainLoop() ;
230 }
231 return noErr ;
519cb848
SC
232}
233
15b41e90
SC
234//----------------------------------------------------------------------
235// Support Routines linking the Mac...File Calls to the Document Manager
236//----------------------------------------------------------------------
237
238void wxApp::MacOpenFile(const wxString & fileName )
239{
240 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
241 if ( dm )
242 dm->CreateDocument(fileName , wxDOC_SILENT ) ;
243}
244
245void wxApp::MacPrintFile(const wxString & fileName )
246{
247 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
248 if ( dm )
249 {
250 wxDocument *doc = dm->CreateDocument(fileName , wxDOC_SILENT ) ;
251 if ( doc )
252 {
253 wxView* view = doc->GetFirstView() ;
254 if( view )
255 {
256 wxPrintout *printout = view->OnCreatePrintout();
257 if (printout)
258 {
259 wxPrinter printer;
260 printer.Print(view->GetFrame(), printout, TRUE);
261 delete printout;
262 }
263 }
264 if (doc->Close())
265 {
266 doc->DeleteAllViews();
267 dm->RemoveDocument(doc) ;
268 }
269 }
270 }
271}
272
273void wxApp::MacNewFile()
274{
275}
276
c5c9378c
SC
277//----------------------------------------------------------------------
278// Carbon Event Handler
279//----------------------------------------------------------------------
9779893b 280
c5c9378c 281#if TARGET_CARBON
519cb848 282
7c9955d1 283 static const EventTypeSpec eventList[] =
e40298d5
JS
284 {
285 { kEventClassCommand, kEventProcessCommand } ,
286 { kEventClassCommand, kEventCommandUpdateStatus } ,
7c9955d1 287
e32f4a9f
VZ
288 { kEventClassMenu, kEventMenuOpening },
289 { kEventClassMenu, kEventMenuClosed },
290
e40298d5
JS
291 { kEventClassApplication , kEventAppActivated } ,
292 { kEventClassApplication , kEventAppDeactivated } ,
293 // handling the quit event is not recommended by apple
294 // rather using the quit apple event - which we do
7c9955d1 295
e40298d5 296 { kEventClassAppleEvent , kEventAppleEvent } ,
7c9955d1 297
e40298d5 298 { kEventClassMouse , kEventMouseDown } ,
3c589c7f 299 { kEventClassMouse , kEventMouseMoved } ,
6d565349
SC
300 { kEventClassMouse , kEventMouseUp } ,
301 { kEventClassMouse , kEventMouseDragged } ,
e40298d5
JS
302 { 'WXMC' , 'WXMC' }
303 } ;
519cb848 304
e32f4a9f
VZ
305static pascal OSStatus
306MenuEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
519cb848 307{
e32f4a9f
VZ
308 // FIXME: this doesn't work for multiple windows
309 wxWindow *win = wxTheApp->GetTopWindow();
310 if ( win )
311 {
312 // VZ: we could find the menu from its handle here by examining all
313 // the menus in the menu bar recursively but knowing that neither
314 // wxMSW nor wxGTK do it why bother...
315#if 0
316 MenuRef menuRef;
317
318 GetEventParameter(event,
319 kEventParamDirectObject,
320 typeMenuRef, NULL,
321 sizeof(menuRef), NULL,
322 &menuRef);
323#endif // 0
324
5a036f13 325 wxMenuEvent wxevent(GetEventKind(event) == kEventMenuOpening
e32f4a9f
VZ
326 ? wxEVT_MENU_OPEN
327 : wxEVT_MENU_CLOSE);
5a036f13 328 wxevent.SetEventObject(win);
e32f4a9f 329
5a036f13 330 (void)win->GetEventHandler()->ProcessEvent(wxevent);
e32f4a9f
VZ
331 }
332
333 return eventNotHandledErr;
519cb848
SC
334}
335
7c9955d1 336// due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop
c5c9378c
SC
337// but have to use ReceiveNextEvent dealing with events manually, therefore we also have
338// deal with clicks in the menu bar explicitely
519cb848 339
6d565349
SC
340pascal OSStatus wxMacWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) ;
341
c5c9378c 342static pascal OSStatus MouseEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
519cb848 343{
c5c9378c 344 OSStatus result = eventNotHandledErr ;
7c9955d1 345
3c589c7f
SC
346 Point point ;
347 UInt32 modifiers = 0;
348 EventMouseButton button = 0 ;
349 UInt32 click = 0 ;
350
351 GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL,
352 sizeof( Point ), NULL, &point );
353 GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL,
354 sizeof( UInt32 ), NULL, &modifiers );
355 GetEventParameter( event, kEventParamMouseButton, typeMouseButton, NULL,
356 sizeof( EventMouseButton ), NULL, &button );
357 GetEventParameter( event, kEventParamClickCount, typeUInt32, NULL,
358 sizeof( UInt32 ), NULL, &click );
359
360 if ( button == 0 || GetEventKind( event ) == kEventMouseUp )
361 modifiers += btnState ;
362
363
c5c9378c 364 switch( GetEventKind(event) )
8461e4c2 365 {
e40298d5
JS
366 case kEventMouseDown :
367 {
e40298d5 368 WindowRef window ;
7c9955d1 369
e40298d5
JS
370 short windowPart = ::FindWindow(point, &window);
371
372 if ( windowPart == inMenuBar )
373 {
374 MenuSelect( point ) ;
375 result = noErr ;
376 }
377 }
378 break ;
6d565349
SC
379 case kEventMouseDragged :
380 case kEventMouseUp :
381 {
382 if ( wxTheApp->s_captureWindow )
383 wxMacWindowEventHandler( handler , event , (void*) wxTheApp->s_captureWindow->MacGetTopLevelWindow() ) ;
384 }
385 break ;
3c589c7f
SC
386 case kEventMouseMoved :
387 {
388 wxTheApp->MacHandleMouseMovedEvent( point.h , point.v , modifiers , EventTimeToTicks( GetEventTime( event ) ) ) ;
389 result = noErr ;
390 break ;
391 }
392 break ;
7c9955d1 393 }
8461e4c2 394
8461e4c2 395 return result ;
519cb848 396}
169935ad 397
15b41e90
SC
398static pascal OSStatus CommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
399{
400 OSStatus result = eventNotHandledErr ;
401
e40298d5 402 HICommand command ;
7c9955d1 403
e40298d5
JS
404 GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL,
405 sizeof( HICommand ), NULL, &command );
15b41e90 406
e40298d5
JS
407 MenuCommand id = command.commandID ;
408 if ( id == kHICommandPreferences )
409 id = wxApp::s_macPreferencesMenuItemId ;
7c9955d1 410
15b41e90
SC
411 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
412 wxMenu* menu = NULL ;
413 wxMenuItem* item = NULL ;
414
415 if ( mbar )
6d565349 416 {
15b41e90 417 item = mbar->FindItem( id , &menu ) ;
6d565349
SC
418 // it is not 100 % sure that an menu of id 0 is really ours, safety check
419 if ( id == 0 && menu != NULL && menu->GetHMenu() != command.menu.menuRef )
420 {
421 item = NULL ;
422 menu = NULL ;
423 }
424 }
15b41e90
SC
425
426 if ( item == NULL || menu == NULL || mbar == NULL )
e40298d5 427 return result ;
7c9955d1 428
e40298d5
JS
429 switch( GetEventKind( event ) )
430 {
431 case kEventProcessCommand :
432 {
433 if (item->IsCheckable())
434 {
435 item->Check( !item->IsChecked() ) ;
436 }
7c9955d1 437
e40298d5
JS
438 menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
439 result = noErr ;
440 }
441 break ;
442 case kEventCommandUpdateStatus:
443 // eventually trigger an updateui round
6d565349 444 result = noErr ;
e40298d5
JS
445 break ;
446 default :
447 break ;
7c9955d1
JS
448 }
449
15b41e90
SC
450 return result ;
451}
452
453static pascal OSStatus ApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
454{
455 OSStatus result = eventNotHandledErr ;
e40298d5
JS
456 switch ( GetEventKind( event ) )
457 {
458 case kEventAppActivated :
459 {
3470af1c
SC
460 if ( wxTheApp )
461 wxTheApp->MacResume( true ) ;
e40298d5
JS
462 result = noErr ;
463 }
464 break ;
465 case kEventAppDeactivated :
466 {
3470af1c
SC
467 if ( wxTheApp )
468 wxTheApp->MacSuspend( true ) ;
e40298d5
JS
469 result = noErr ;
470 }
471 break ;
472 default :
473 break ;
474 }
15b41e90
SC
475 return result ;
476}
477
478pascal OSStatus wxAppEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
479{
480 OSStatus result = eventNotHandledErr ;
481 switch( GetEventClass( event ) )
482 {
e40298d5
JS
483 case kEventClassCommand :
484 result = CommandEventHandler( handler , event , data ) ;
485 break ;
486 case kEventClassApplication :
487 result = ApplicationEventHandler( handler , event , data ) ;
488 break ;
489 case kEventClassMenu :
490 result = MenuEventHandler( handler , event , data ) ;
491 break ;
492 case kEventClassMouse :
493 result = MouseEventHandler( handler , event , data ) ;
494 break ;
495 case kEventClassAppleEvent :
496 {
497 EventRecord rec ;
498 wxMacConvertEventToRecord( event , &rec ) ;
499 result = AEProcessAppleEvent( &rec ) ;
500 }
501 break ;
502 default :
503 break ;
15b41e90
SC
504 }
505
506 return result ;
507}
508
509DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler )
510
511#endif
512
21870a5d 513#if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
53fd991c
SC
514// we know it's there ;-)
515WXIMPORT char std::__throws_bad_alloc ;
516#endif
517
05e2b077 518bool wxApp::Initialize(int& argc, wxChar **argv)
e9576ca5 519{
fbbdb7cd 520 int error = 0 ;
9779893b 521
fbbdb7cd 522 // Mac-specific
9779893b 523
fbbdb7cd
GD
524 UMAInitToolbox( 4 ) ;
525 SetEventMask( everyEvent ) ;
8461e4c2 526 UMAShowWatchCursor() ;
8be97d65 527
21870a5d 528#if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
fbbdb7cd
GD
529 // open shared library resources from here since we don't have
530 // __wxinitialize in Mach-O shared libraries
531 wxStAppResource::OpenSharedLibraryResource(NULL);
532#endif
84c1ffa9 533
f5c6eb5c 534#ifndef __DARWIN__
fbbdb7cd 535 // test the minimal configuration necessary
8be97d65 536
fbbdb7cd 537# if !TARGET_CARBON
8461e4c2
VZ
538 long theSystem ;
539 long theMachine;
540
541 if (Gestalt(gestaltMachineType, &theMachine) != noErr)
542 {
543 error = kMacSTRWrongMachine;
544 }
545 else if (theMachine < gestaltMacPlus)
546 {
547 error = kMacSTRWrongMachine;
548 }
549 else if (Gestalt(gestaltSystemVersion, &theSystem) != noErr )
550 {
551 error = kMacSTROldSystem ;
552 }
72055702 553 else if ( theSystem < 0x0860 )
8461e4c2
VZ
554 {
555 error = kMacSTROldSystem ;
556 }
557 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap)
558 {
559 error = kMacSTRSmallSize;
560 }
fbbdb7cd 561# endif
8461e4c2
VZ
562 /*
563 else
564 {
565 if ( !UMAHasAppearance() )
566 {
567 error = kMacSTRNoPre8Yet ;
568 }
569 }
570 */
ed5b9811 571#endif
8be97d65 572
8461e4c2 573 // if we encountered any problems so far, give the error code and exit immediately
9779893b 574
169935ad 575 if ( error )
9779893b 576 {
53fd991c 577 wxStAppResource resload ;
8461e4c2
VZ
578 short itemHit;
579 Str255 message;
580
581 GetIndString(message, 128, error);
582 UMAShowArrowCursor() ;
583 ParamText("\pFatal Error", message, (ConstStr255Param)"\p", (ConstStr255Param)"\p");
584 itemHit = Alert(128, nil);
585 return FALSE ;
9779893b 586 }
519cb848 587
f5c6eb5c 588#ifndef __DARWIN__
fbbdb7cd 589# if __option(profile)
882b0479 590 ProfilerInit( collectDetailed, bestTimeBase , 40000 , 50 ) ;
fbbdb7cd 591# endif
9779893b 592#endif
519cb848 593
f5c6eb5c 594#ifndef __DARWIN__
ed581ee2 595 // now avoid exceptions thrown for new (bad_alloc)
e40298d5 596 // FIXME CS for some changes outside wxMac does not compile anymore
15b41e90
SC
597#if 0
598 std::__throws_bad_alloc = 0 ;
599#endif
600
03e11df5 601#endif
7c9955d1 602
8461e4c2 603 s_macCursorRgn = ::NewRgn() ;
8be97d65 604
05e2b077
VZ
605 // Mac OS X passes a process serial number command line argument when
606 // the application is launched from the Finder. This argument must be
607 // removed from the command line arguments before being handled by the
608 // application (otherwise applications would need to handle it)
609 if ( argc > 1 )
610 {
611 static const wxChar *ARG_PSN = _T("-psn_");
612 if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
613 {
614 // remove this argument
615 memmove(argv, argv + 1, argc--);
616 }
617 }
618
94826170
VZ
619 if ( !wxAppBase::Initialize(argc, argv) )
620 return false;
e9576ca5 621
376b18ea
VZ
622 wxWinMacWindowList = new wxList(wxKEY_INTEGER);
623 wxWinMacControlList = new wxList(wxKEY_INTEGER);
0a67a93b 624
376b18ea 625 wxMacCreateNotifierTable() ;
9779893b 626
376b18ea 627 UMAShowArrowCursor() ;
8461e4c2 628
94826170 629 return true;
e9576ca5
SC
630}
631
15b41e90
SC
632bool wxApp::OnInitGui()
633{
e40298d5
JS
634 if( !wxAppBase::OnInitGui() )
635 return false ;
7c9955d1
JS
636
637#if TARGET_CARBON
e40298d5 638 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
7c9955d1 639
e40298d5 640 InstallApplicationEventHandler(
7c9955d1 641 GetwxAppEventHandlerUPP(),
73bdd73a 642 GetEventTypeCount(eventList), eventList, wxTheApp, (EventHandlerRef *)&(wxTheApp->m_macEventHandler));
15b41e90 643#endif
7c9955d1 644
15b41e90
SC
645#if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
646 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
647 NewAEEventHandlerUPP(AEHandleODoc) ,
648 0 , FALSE ) ;
649 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
650 NewAEEventHandlerUPP(AEHandleOApp) ,
651 0 , FALSE ) ;
652 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
653 NewAEEventHandlerUPP(AEHandlePDoc) ,
654 0 , FALSE ) ;
655 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
656 NewAEEventHandlerUPP(AEHandleQuit) ,
657 0 , FALSE ) ;
658#else
659 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
660 NewAEEventHandlerProc(AEHandleODoc) ,
661 0 , FALSE ) ;
662 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
663 NewAEEventHandlerProc(AEHandleOApp) ,
664 0 , FALSE ) ;
665 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
666 NewAEEventHandlerProc(AEHandlePDoc) ,
667 0 , FALSE ) ;
668 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
669 NewAEEventHandlerProc(AEHandleQuit) ,
670 0 , FALSE ) ;
671#endif
672
e40298d5 673 return TRUE ;
15b41e90
SC
674}
675
e9576ca5
SC
676void wxApp::CleanUp()
677{
12cd5f34 678 wxToolTip::RemoveToolTips() ;
2f1ae414
SC
679
680 // One last chance for pending objects to be cleaned up
681 wxTheApp->DeletePendingObjects();
682
fbbdb7cd 683 wxMacDestroyNotifierTable() ;
84c1ffa9 684
94826170
VZ
685 delete wxWinMacWindowList ;
686 wxWinMacWindowList = NULL;
2f1ae414 687
94826170
VZ
688 delete wxWinMacControlList ;
689 wxWinMacControlList = NULL;
e9576ca5 690
f5c6eb5c 691#ifndef __DARWIN__
fbbdb7cd 692# if __option(profile)
ac2153a6 693 ProfilerDump( (StringPtr)"\papp.prof" ) ;
fbbdb7cd
GD
694 ProfilerTerm() ;
695# endif
9779893b 696#endif
519cb848 697
21870a5d 698#if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
fbbdb7cd
GD
699 // close shared library resources from here since we don't have
700 // __wxterminate in Mach-O shared libraries
701 wxStAppResource::CloseSharedLibraryResource();
702#endif
7c9955d1 703
8461e4c2 704 UMACleanupToolbox() ;
fbbdb7cd 705 if (s_macCursorRgn) {
76a5e5d2 706 ::DisposeRgn((RgnHandle)s_macCursorRgn);
fbbdb7cd 707 }
84c1ffa9 708
8461e4c2
VZ
709 #if 0
710 TerminateAE() ;
711 #endif
94826170
VZ
712
713 wxAppBase::CleanUp();
e9576ca5
SC
714}
715
92b002a4 716//----------------------------------------------------------------------
05e2b077 717// misc initialization stuff
92b002a4
RD
718//----------------------------------------------------------------------
719
fbbdb7cd
GD
720// extern variable for shared library resource id
721// need to be able to find it with NSLookupAndBindSymbol
722short gSharedLibraryResource = kResFileNotOpened ;
723
21870a5d 724#if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
fbbdb7cd 725CFBundleRef gSharedLibraryBundle = NULL;
21870a5d 726#endif /* WXMAKINGDLL_CORE && __DARWIN__ */
53fd991c
SC
727
728wxStAppResource::wxStAppResource()
729{
fbbdb7cd
GD
730 m_currentRefNum = CurResFile() ;
731 if ( gSharedLibraryResource != kResFileNotOpened )
732 {
733 UseResFile( gSharedLibraryResource ) ;
734 }
735}
736
737wxStAppResource::~wxStAppResource()
738{
739 if ( m_currentRefNum != kResFileNotOpened )
740 {
741 UseResFile( m_currentRefNum ) ;
742 }
743}
744
745void wxStAppResource::OpenSharedLibraryResource(const void *initBlock)
746{
747 gSharedLibraryResource = kResFileNotOpened;
748
21870a5d 749#ifdef WXMAKINGDLL_CORE
fbbdb7cd
GD
750 if ( initBlock != NULL ) {
751 const CFragInitBlock *theInitBlock = (const CFragInitBlock *)initBlock;
752 FSSpec *fileSpec = NULL;
84c1ffa9 753
fbbdb7cd
GD
754 if (theInitBlock->fragLocator.where == kDataForkCFragLocator) {
755 fileSpec = theInitBlock->fragLocator.u.onDisk.fileSpec;
756 }
757 else if (theInitBlock->fragLocator.where == kResourceCFragLocator) {
758 fileSpec = theInitBlock->fragLocator.u.inSegs.fileSpec;
759 }
84c1ffa9 760
fbbdb7cd
GD
761 if (fileSpec != NULL) {
762 gSharedLibraryResource = FSpOpenResFile(fileSpec, fsRdPerm);
763 }
764 }
765 else {
766#ifdef __DARWIN__
767 // Open the shared library resource file if it is not yet open
ed581ee2
GD
768 NSSymbol theSymbol;
769 NSModule theModule;
770 const char *theLibPath;
84c1ffa9 771
fbbdb7cd
GD
772 gSharedLibraryBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
773 if (gSharedLibraryBundle != NULL) {
ed581ee2
GD
774 // wxWindows has been bundled into a framework
775 // load the framework resources
84c1ffa9 776
fbbdb7cd 777 gSharedLibraryResource = CFBundleOpenBundleResourceMap(gSharedLibraryBundle);
ed581ee2
GD
778 }
779 else {
780 // wxWindows is a simple dynamic shared library
781 // load the resources from the data fork of a separate resource file
21870a5d
RD
782 wxString theResPath;
783 wxString theName;
ed581ee2
GD
784 FSRef theResRef;
785 OSErr theErr = noErr;
84c1ffa9 786
ed581ee2 787 // get the library path
fbbdb7cd 788 theSymbol = NSLookupAndBindSymbol("_gSharedLibraryResource");
ed581ee2
GD
789 theModule = NSModuleForSymbol(theSymbol);
790 theLibPath = NSLibraryNameForModule(theModule);
ed581ee2 791
376b18ea
VZ
792 // if we call wxLogDebug from here then, as wxTheApp hasn't been
793 // created yet when we're called from wxApp::Initialize(), wxLog
794 // is going to create a default stderr-based log target instead of
795 // the expected normal GUI one -- don't do it, if we really want
796 // to see this message just use fprintf() here
797#if 0
f346733b
GD
798 wxLogDebug( wxT("wxMac library installation name is '%s'"),
799 theLibPath );
376b18ea 800#endif
f346733b 801
ed581ee2 802 // allocate copy to replace .dylib.* extension with .rsrc
21870a5d
RD
803 if (theLibPath != NULL) {
804 theResPath = theLibPath;
805 // replace '_core' with '' in case of multi-lib build
806 theResPath.Replace(wxT("_core"), wxEmptyString);
807 // replace ".dylib" shared library extension with ".rsrc"
808 theResPath.Replace(wxT(".dylib"), wxT(".rsrc"));
809 // Find the begining of the filename
810 theName = theResPath.AfterLast('/');
84c1ffa9 811
376b18ea 812#if 0
f346733b 813 wxLogDebug( wxT("wxMac resources file name is '%s'"),
21870a5d 814 theResPath.mb_str() );
376b18ea 815#endif
ed581ee2 816
21870a5d 817 theErr = FSPathMakeRef((UInt8 *) theResPath.mb_str(), &theResRef, false);
ed581ee2
GD
818 if (theErr != noErr) {
819 // try in current directory (using name only)
21870a5d 820 theErr = FSPathMakeRef((UInt8 *) theName.mb_str(), &theResRef, false);
ed581ee2 821 }
84c1ffa9 822
ed581ee2
GD
823 // open the resource file
824 if (theErr == noErr) {
825 theErr = FSOpenResourceFile( &theResRef, 0, NULL, fsRdPerm,
fbbdb7cd 826 &gSharedLibraryResource);
ed581ee2 827 }
f346733b 828 if (theErr != noErr) {
376b18ea
VZ
829#ifdef __WXDEBUG__
830 fprintf(stderr,
831 wxT("unable to open wxMac resource file '%s'\n"),
21870a5d 832 theResPath.mb_str() );
376b18ea 833#endif // __WXDEBUG__
f346733b
GD
834 }
835
ed581ee2
GD
836 }
837 }
fbbdb7cd 838#endif /* __DARWIN__ */
53fd991c 839 }
21870a5d 840#endif /* WXMAKINGDLL_CORE */
53fd991c
SC
841}
842
fbbdb7cd 843void wxStAppResource::CloseSharedLibraryResource()
53fd991c 844{
21870a5d 845#ifdef WXMAKINGDLL_CORE
ed581ee2 846 // Close the shared library resource file
fbbdb7cd
GD
847 if (gSharedLibraryResource != kResFileNotOpened) {
848#ifdef __DARWIN__
849 if (gSharedLibraryBundle != NULL) {
850 CFBundleCloseBundleResourceMap(gSharedLibraryBundle,
851 gSharedLibraryResource);
852 gSharedLibraryBundle = NULL;
ed581ee2 853 }
fbbdb7cd
GD
854 else
855#endif /* __DARWIN__ */
856 {
857 CloseResFile(gSharedLibraryResource);
ed581ee2 858 }
fbbdb7cd 859 gSharedLibraryResource = kResFileNotOpened;
84c1ffa9 860 }
21870a5d 861#endif /* WXMAKINGDLL_CORE */
53fd991c
SC
862}
863
21870a5d 864#if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
53fd991c 865
ed581ee2
GD
866// for shared libraries we have to manually get the correct resource
867// ref num upon initializing and releasing when terminating, therefore
868// the __wxinitialize and __wxterminate must be used
53fd991c 869
53fd991c 870extern "C" {
e40298d5 871 void __sinit(void); /* (generated by linker) */
fbbdb7cd
GD
872 pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
873 pascal void __terminate(void);
53fd991c 874}
53fd991c
SC
875
876pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock)
877{
fbbdb7cd
GD
878 wxStAppResource::OpenSharedLibraryResource( theInitBlock ) ;
879 return __initialize( theInitBlock ) ;
53fd991c
SC
880}
881
882pascal void __wxterminate(void)
883{
fbbdb7cd 884 wxStAppResource::CloseSharedLibraryResource() ;
53fd991c
SC
885 __terminate() ;
886}
ed581ee2 887
21870a5d 888#endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
53fd991c 889
b4efa069
SC
890#if TARGET_CARBON
891
892bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec)
893{
894 bool converted = ConvertEventRefToEventRecord( event,rec) ;
895 OSStatus err = noErr ;
896 if ( !converted )
897 {
898 switch( GetEventClass( event ) )
899 {
900 case kEventClassKeyboard :
901 {
902 converted = true ;
84c1ffa9 903 switch( GetEventKind(event) )
b4efa069
SC
904 {
905 case kEventRawKeyDown :
906 rec->what = keyDown ;
907 break ;
908 case kEventRawKeyRepeat :
909 rec->what = autoKey ;
910 break ;
911 case kEventRawKeyUp :
912 rec->what = keyUp ;
913 break ;
914 case kEventRawKeyModifiersChanged :
915 rec->what = nullEvent ;
916 break ;
917 default :
918 converted = false ;
919 break ;
920 }
921 if ( converted )
922 {
923 UInt32 keyCode ;
924 unsigned char charCode ;
925 UInt32 modifiers ;
926 GetMouse( &rec->where) ;
927
928 err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
929 err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
930 err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
931 rec->modifiers = modifiers ;
932 rec->message = (keyCode << 8 ) + charCode ;
933 }
934 }
935 break ;
936 case kEventClassTextInput :
937 {
938 switch( GetEventKind( event ) )
939 {
940 case kEventTextInputUnicodeForKeyEvent :
941 {
942 EventRef rawEvent ;
943 err = GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
944 converted = true ;
945 {
946 UInt32 keyCode ;
947 unsigned char charCode ;
948 UInt32 modifiers ;
949 GetMouse( &rec->where) ;
950 rec->what = keyDown ;
951 err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
952 err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
953 err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
954 rec->modifiers = modifiers ;
955 rec->message = (keyCode << 8 ) + charCode ;
956 }
957 }
958 break ;
959 default :
960 break ;
961 }
962 }
963 break ;
964 }
965 }
84c1ffa9 966
b4efa069
SC
967 return converted ;
968}
969
15b41e90 970/*
b4efa069
SC
971pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
972{
973 OSStatus result = eventNotHandledErr ;
84c1ffa9 974
b4efa069
SC
975 EventRecord rec ;
976 switch ( GetEventClass( event ) )
977 {
978 case kEventClassKeyboard :
979 if ( wxMacConvertEventToRecord( event , &rec ) )
980 {
41d368a4 981 wxTheApp->MacHandleModifierEvents( &rec ) ;
b4efa069
SC
982 wxTheApp->MacHandleOneEvent( &rec ) ;
983 result = noErr ;
984 }
985 break ;
986 case kEventClassTextInput :
987 if ( wxMacConvertEventToRecord( event , &rec ) )
988 {
41d368a4 989 wxTheApp->MacHandleModifierEvents( &rec ) ;
b4efa069
SC
990 wxTheApp->MacHandleOneEvent( &rec ) ;
991 result = noErr ;
992 }
993 break ;
994 default :
995 break ;
996 }
997 return result ;
998}
15b41e90 999*/
b4efa069
SC
1000#endif
1001
e9576ca5
SC
1002wxApp::wxApp()
1003{
e9576ca5 1004 m_printMode = wxPRINT_WINDOWS;
e9576ca5 1005 m_auto3D = TRUE;
1ea39a03
SC
1006
1007 m_macCurrentEvent = NULL ;
d16477fd 1008#if TARGET_CARBON
1ea39a03 1009 m_macCurrentEventHandlerCallRef = NULL ;
d16477fd 1010#endif
e9576ca5
SC
1011}
1012
e9576ca5
SC
1013int wxApp::MainLoop()
1014{
e40298d5 1015 m_keepGoing = TRUE;
e9576ca5 1016
e40298d5
JS
1017 while (m_keepGoing)
1018 {
1019 MacDoOneEvent() ;
1020 }
e9576ca5 1021
e40298d5 1022 return 0;
e9576ca5
SC
1023}
1024
e9576ca5
SC
1025void wxApp::ExitMainLoop()
1026{
e40298d5 1027 m_keepGoing = FALSE;
e9576ca5
SC
1028}
1029
1030// Is a message/event pending?
1031bool wxApp::Pending()
1032{
c5c9378c 1033#if TARGET_CARBON
e40298d5 1034 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
c5c9378c 1035#else
8461e4c2 1036 EventRecord event ;
519cb848 1037
e39af974 1038 return EventAvail( everyEvent , &event ) ;
c5c9378c 1039#endif
e9576ca5
SC
1040}
1041
1042// Dispatch a message.
1bf77ee5 1043bool wxApp::Dispatch()
e9576ca5 1044{
8461e4c2 1045 MacDoOneEvent() ;
1bf77ee5
VZ
1046
1047 return true;
e9576ca5
SC
1048}
1049
1050void wxApp::OnIdle(wxIdleEvent& event)
1051{
955a9197 1052 wxAppBase::OnIdle(event);
21870a5d 1053
2f1ae414
SC
1054 // If they are pending events, we must process them: pending events are
1055 // either events to the threads other than main or events posted with
1056 // wxPostEvent() functions
1057 wxMacProcessNotifierAndPendingEvents();
1058
3fdac2ab
DE
1059 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1060 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
e9576ca5
SC
1061}
1062
e2478fde 1063void wxApp::WakeUpIdle()
9779893b 1064{
8461e4c2 1065 wxMacWakeUp() ;
9779893b
RD
1066}
1067
e2478fde 1068void wxApp::Exit()
e9576ca5 1069{
2f1ae414 1070 wxApp::CleanUp();
8461e4c2 1071 ::ExitToShell() ;
e9576ca5
SC
1072}
1073
2f1ae414
SC
1074void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1075{
1076 if (GetTopWindow())
1077 GetTopWindow()->Close(TRUE);
1078}
1079
1080// Default behaviour: close the application with prompts. The
1081// user can veto the close, and therefore the end session.
1082void wxApp::OnQueryEndSession(wxCloseEvent& event)
1083{
1084 if (GetTopWindow())
1085 {
1086 if (!GetTopWindow()->Close(!event.CanVeto()))
1087 event.Veto(TRUE);
1088 }
1089}
1090
1091extern "C" void wxCYield() ;
1092void wxCYield()
1093{
8461e4c2 1094 wxYield() ;
2f1ae414
SC
1095}
1096
e9576ca5 1097// Yield to other processes
cb2713bf 1098
8461e4c2 1099bool wxApp::Yield(bool onlyIfNeeded)
e9576ca5 1100{
8461e4c2
VZ
1101 if (s_inYield)
1102 {
1103 if ( !onlyIfNeeded )
1104 {
1105 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1106 }
1107
1108 return FALSE;
1109 }
1110
1111 s_inYield = TRUE;
cb2713bf 1112
2f1ae414 1113#if wxUSE_THREADS
cb2713bf 1114 YieldToAnyThread() ;
2f1ae414 1115#endif
e40298d5 1116 // by definition yield should handle all non-processed events
c5c9378c 1117#if TARGET_CARBON
e40298d5
JS
1118 EventRef theEvent;
1119
1120 OSStatus status = noErr ;
1121 do
1122 {
1123 s_inReceiveEvent = true ;
1124 status = ReceiveNextEvent(0, NULL,kEventDurationNoWait,true,&theEvent) ;
1125 s_inReceiveEvent = false ;
7c9955d1 1126
e40298d5
JS
1127 if ( status == eventLoopTimedOutErr )
1128 {
1129 // make sure next time the event loop will trigger idle events
1130 sleepTime = kEventDurationNoWait ;
1131 }
1132 else if ( status == eventLoopQuitErr )
1133 {
1134 // according to QA1061 this may also occur when a WakeUp Process
1135 // is executed
1136 }
1137 else
1138 {
1139 MacHandleOneEvent( theEvent ) ;
1140 ReleaseEvent(theEvent);
1141 }
1142 } while( status == noErr ) ;
c5c9378c 1143#else
cb2713bf 1144 EventRecord event ;
2f1ae414 1145
e40298d5
JS
1146 // having a larger value here leads to large performance slowdowns
1147 // so we cannot give background apps more processor time here
1148 // we do so however having a large sleep value in the main event loop
7c9955d1 1149 sleepTime = 0 ;
2f1ae414 1150
c5c9378c 1151 while ( !IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, (RgnHandle) wxApp::s_macCursorRgn))
8461e4c2 1152 {
c5c9378c
SC
1153 MacHandleModifierEvents( &event ) ;
1154 MacHandleOneEvent( &event );
8461e4c2 1155 if ( event.what != kHighLevelEvent )
76a5e5d2 1156 SetRectRgn( (RgnHandle) wxApp::s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
8461e4c2 1157 }
c5c9378c
SC
1158 MacHandleModifierEvents( &event ) ;
1159#endif
2f1ae414 1160
8461e4c2 1161 wxMacProcessNotifierAndPendingEvents() ;
8461e4c2 1162 s_inYield = FALSE;
cb2713bf 1163
8461e4c2 1164 return TRUE;
169935ad
SC
1165}
1166
9779893b 1167// platform specifics
169935ad 1168
519cb848
SC
1169void wxApp::MacSuspend( bool convertClipboard )
1170{
15b41e90 1171#if !TARGET_CARBON
1c469f7f 1172 // we have to deactive the top level windows manually
8461e4c2 1173
653a3ed9 1174 wxWindowListNode* node = wxTopLevelWindows.GetFirst();
1c469f7f
SC
1175 while (node)
1176 {
1177 wxTopLevelWindow* win = (wxTopLevelWindow*) node->Data();
b9c10231
SC
1178#if TARGET_CARBON
1179#if 0 // having problems right now with that
6a7e6411 1180 if (!win->HasFlag(wxSTAY_ON_TOP))
21870a5d 1181#endif
b9c10231 1182#endif
6a7e6411 1183 win->MacActivate( ((EventRecord*) MacGetCurrentEvent())->when , false ) ;
8461e4c2 1184
653a3ed9 1185 node = node->GetNext();
1c469f7f 1186 }
84c1ffa9 1187
15b41e90
SC
1188 ::HideFloatingWindows() ;
1189#endif
2a294857 1190 s_lastMouseDown = 0 ;
15b41e90 1191
2a294857
GD
1192 if( convertClipboard )
1193 {
1194 MacConvertPrivateToPublicScrap() ;
1195 }
519cb848
SC
1196}
1197
2a294857
GD
1198extern wxList wxModalDialogs;
1199
519cb848
SC
1200void wxApp::MacResume( bool convertClipboard )
1201{
2a294857
GD
1202 s_lastMouseDown = 0 ;
1203 if( convertClipboard )
1204 {
1205 MacConvertPublicToPrivateScrap() ;
1206 }
84c1ffa9 1207
15b41e90 1208#if !TARGET_CARBON
2a294857 1209 ::ShowFloatingWindows() ;
2a294857 1210 // raise modal dialogs in case a non modal window was selected to activate the app
9779893b 1211
653a3ed9 1212 wxNode* node = wxModalDialogs.GetFirst();
2a294857
GD
1213 while (node)
1214 {
653a3ed9 1215 wxDialog* dialog = (wxDialog *) node->GetData();
2a294857 1216 dialog->Raise();
84c1ffa9 1217
653a3ed9 1218 node = node->GetNext();
2a294857 1219 }
15b41e90 1220#endif
519cb848
SC
1221}
1222
1223void wxApp::MacConvertPrivateToPublicScrap()
1224{
519cb848
SC
1225}
1226
1227void wxApp::MacConvertPublicToPrivateScrap()
1228{
519cb848
SC
1229}
1230
9779893b 1231void wxApp::MacDoOneEvent()
169935ad 1232{
c5c9378c 1233#if TARGET_CARBON
e40298d5 1234 EventRef theEvent;
c5c9378c 1235
e40298d5
JS
1236 s_inReceiveEvent = true ;
1237 OSStatus status = ReceiveNextEvent(0, NULL,sleepTime,true,&theEvent) ;
1238 s_inReceiveEvent = false ;
1239 if ( status == eventLoopTimedOutErr )
1240 {
c5c9378c 1241 if ( wxTheApp->ProcessIdle() )
e40298d5 1242 sleepTime = kEventDurationNoWait ;
c5c9378c 1243 else
e40298d5
JS
1244 sleepTime = kEventDurationForever ;
1245 }
1246 else if ( status == eventLoopQuitErr )
1247 {
1248 // according to QA1061 this may also occur when a WakeUp Process
1249 // is executed
1250 }
1251 else
1252 {
1253 MacHandleOneEvent( theEvent ) ;
1254 ReleaseEvent(theEvent);
3470af1c 1255 sleepTime = kEventDurationNoWait ;
e40298d5 1256 }
c5c9378c 1257#else
e40298d5 1258 EventRecord event ;
169935ad 1259
e40298d5 1260 EventMask eventMask = everyEvent ;
169935ad 1261
c5c9378c 1262 if (WaitNextEvent(eventMask, &event, sleepTime, (RgnHandle) s_macCursorRgn))
8461e4c2 1263 {
41d368a4 1264 MacHandleModifierEvents( &event ) ;
6264b550 1265 MacHandleOneEvent( &event );
8461e4c2
VZ
1266 }
1267 else
1268 {
41d368a4 1269 MacHandleModifierEvents( &event ) ;
8461e4c2 1270 // idlers
72055702 1271 WindowPtr window = ::FrontWindow() ;
8461e4c2 1272 if ( window )
72055702 1273 ::IdleControls( window ) ;
9779893b 1274
c5c9378c 1275 if ( wxTheApp->ProcessIdle() )
e40298d5 1276 sleepTime = 0 ;
c5c9378c 1277 else
e40298d5 1278 sleepTime = GetCaretTime() / 2 ;
8461e4c2
VZ
1279 }
1280 if ( event.what != kHighLevelEvent )
76a5e5d2 1281 SetRectRgn( (RgnHandle) s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
c5c9378c 1282#endif
8461e4c2 1283 // repeaters
519cb848 1284
6264b550 1285 DeletePendingObjects() ;
8461e4c2 1286 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1287}
1288
76a5e5d2 1289void wxApp::MacHandleOneEvent( WXEVENTREF evr )
169935ad 1290{
c5c9378c 1291#if TARGET_CARBON
e40298d5
JS
1292 EventTargetRef theTarget;
1293 theTarget = GetEventDispatcherTarget();
c5c9378c 1294 m_macCurrentEvent = evr ;
e40298d5 1295 SendEventToEventTarget ((EventRef) evr , theTarget);
c5c9378c 1296#else
76a5e5d2 1297 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1298 m_macCurrentEvent = ev ;
1299
1300 wxApp::sm_lastMessageTime = ev->when ;
1301
1302 switch (ev->what)
1303 {
1304 case mouseDown:
1305 MacHandleMouseDownEvent( ev ) ;
1306 if ( ev->modifiers & controlKey )
1307 s_lastMouseDown = 2;
1308 else
1309 s_lastMouseDown = 1;
1310 break;
1311 case mouseUp:
1312 if ( s_lastMouseDown == 2 )
1313 {
1314 ev->modifiers |= controlKey ;
1315 }
1316 else
1317 {
1318 ev->modifiers &= ~controlKey ;
1319 }
1320 MacHandleMouseUpEvent( ev ) ;
1321 s_lastMouseDown = 0;
1322 break;
1323 case activateEvt:
1324 MacHandleActivateEvent( ev ) ;
1325 break;
1326 case updateEvt:
1327 MacHandleUpdateEvent( ev ) ;
1328 break;
1329 case keyDown:
1330 case autoKey:
1331 MacHandleKeyDownEvent( ev ) ;
1332 break;
1333 case keyUp:
1334 MacHandleKeyUpEvent( ev ) ;
1335 break;
1336 case diskEvt:
1337 MacHandleDiskEvent( ev ) ;
1338 break;
1339 case osEvt:
1340 MacHandleOSEvent( ev ) ;
1341 break;
1342 case kHighLevelEvent:
1343 MacHandleHighLevelEvent( ev ) ;
1344 break;
1345 default:
1346 break;
1347 }
c5c9378c 1348#endif
8461e4c2 1349 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1350}
1351
c5c9378c
SC
1352#if !TARGET_CARBON
1353bool s_macIsInModalLoop = false ;
1354
1355void wxApp::MacHandleModifierEvents( WXEVENTREF evr )
1356{
1357 EventRecord* ev = (EventRecord*) evr ;
1358 if ( ev->modifiers != s_lastModifiers && wxWindow::FindFocus() != NULL )
1359 {
1360 wxKeyEvent event(wxEVT_KEY_DOWN);
1361
1362 event.m_shiftDown = ev->modifiers & shiftKey;
1363 event.m_controlDown = ev->modifiers & controlKey;
1364 event.m_altDown = ev->modifiers & optionKey;
1365 event.m_metaDown = ev->modifiers & cmdKey;
1366
1367 event.m_x = ev->where.h;
1368 event.m_y = ev->where.v;
1369 event.m_timeStamp = ev->when;
1370 wxWindow* focus = wxWindow::FindFocus() ;
1371 event.SetEventObject(focus);
1372
1373 if ( (ev->modifiers ^ s_lastModifiers ) & controlKey )
1374 {
1375 event.m_keyCode = WXK_CONTROL ;
1376 event.SetEventType( ( ev->modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1377 focus->GetEventHandler()->ProcessEvent( event ) ;
1378 }
1379 if ( (ev->modifiers ^ s_lastModifiers ) & shiftKey )
1380 {
1381 event.m_keyCode = WXK_SHIFT ;
1382 event.SetEventType( ( ev->modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1383 focus->GetEventHandler()->ProcessEvent( event ) ;
1384 }
1385 if ( (ev->modifiers ^ s_lastModifiers ) & optionKey )
1386 {
1387 event.m_keyCode = WXK_ALT ;
1388 event.SetEventType( ( ev->modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1389 focus->GetEventHandler()->ProcessEvent( event ) ;
1390 }
d16477fd 1391 if ( ( ev->modifiers ^ s_lastModifiers ) & cmdKey )
b7aec135
SC
1392 {
1393 event.m_keyCode = WXK_COMMAND ;
1394 event.SetEventType( ( ev->modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1395 focus->GetEventHandler()->ProcessEvent( event ) ;
1396 }
c5c9378c
SC
1397 s_lastModifiers = ev->modifiers ;
1398 }
1399}
1400
76a5e5d2 1401void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr )
169935ad 1402{
738a6daa
SC
1403 // we must avoid reentrancy problems when processing high level events eg printing
1404 bool former = s_inYield ;
1405 s_inYield = TRUE ;
76a5e5d2 1406 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1407 ::AEProcessAppleEvent( ev ) ;
738a6daa 1408 s_inYield = former ;
169935ad
SC
1409}
1410
76a5e5d2 1411void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr )
169935ad 1412{
76a5e5d2 1413 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1414 wxToolTip::RemoveToolTips() ;
2f1ae414 1415
8461e4c2 1416 WindowRef window;
72055702 1417 WindowRef frontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
1418 WindowAttributes frontWindowAttributes = NULL ;
1419 if ( frontWindow )
72055702 1420 ::GetWindowAttributes( frontWindow , &frontWindowAttributes ) ;
9779893b 1421
8461e4c2 1422 short windowPart = ::FindWindow(ev->where, &window);
a3722eeb 1423 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
0a67a93b
SC
1424 if ( wxPendingDelete.Member(win) )
1425 return ;
9779893b 1426
8461e4c2
VZ
1427 BitMap screenBits;
1428 GetQDGlobalsScreenBits( &screenBits );
1429
1430 switch (windowPart)
1431 {
1432 case inMenuBar :
1433 if ( s_macIsInModalLoop )
1434 {
1435 SysBeep ( 30 ) ;
1436 }
1437 else
1438 {
1439 UInt32 menuresult = MenuSelect(ev->where) ;
15b41e90 1440 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) );
8461e4c2
VZ
1441 s_lastMouseDown = 0;
1442 }
1443 break ;
8461e4c2
VZ
1444 case inSysWindow :
1445 SystemClick( ev , window ) ;
1446 s_lastMouseDown = 0;
1447 break ;
8461e4c2
VZ
1448 case inDrag :
1449 if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) )
1450 {
1451 SysBeep ( 30 ) ;
1452 }
1453 else
1454 {
1455 DragWindow(window, ev->where, &screenBits.bounds);
1456 if (win)
1457 {
1458 GrafPtr port ;
1459 GetPort( &port ) ;
1460 Point pt = { 0, 0 } ;
66a09d47 1461 SetPortWindowPort(window) ;
8461e4c2
VZ
1462 LocalToGlobal( &pt ) ;
1463 SetPort( port ) ;
1464 win->SetSize( pt.h , pt.v , -1 ,
1465 -1 , wxSIZE_USE_EXISTING);
1466 }
1467 s_lastMouseDown = 0;
1468 }
1469 break ;
1470 case inGoAway:
1471 if (TrackGoAway(window, ev->where))
1472 {
1473 if ( win )
1474 win->Close() ;
1475 }
1476 s_lastMouseDown = 0;
1477 break;
1478 case inGrow:
1479 {
2b5f62a0
VZ
1480 Rect newContentRect ;
1481 Rect constraintRect ;
1482 constraintRect.top = win->GetMinHeight() ;
1483 if ( constraintRect.top == -1 )
1484 constraintRect.top = 0 ;
1485 constraintRect.left = win->GetMinWidth() ;
1486 if ( constraintRect.left == -1 )
1487 constraintRect.left = 0 ;
1488 constraintRect.right = win->GetMaxWidth() ;
1489 if ( constraintRect.right == -1 )
1490 constraintRect.right = 32000 ;
1491 constraintRect.bottom = win->GetMaxHeight() ;
1492 if ( constraintRect.bottom == -1 )
1493 constraintRect.bottom = 32000 ;
1494
1495 Boolean growResult = ResizeWindow( window , ev->where ,
1496 &constraintRect , &newContentRect ) ;
1497 if ( growResult )
8461e4c2 1498 {
7c9955d1
JS
1499 win->SetSize( newContentRect.left , newContentRect.top ,
1500 newContentRect.right - newContentRect.left ,
2b5f62a0 1501 newContentRect.bottom - newContentRect.top, wxSIZE_USE_EXISTING);
8461e4c2
VZ
1502 }
1503 s_lastMouseDown = 0;
1504 }
1505 break;
1506 case inZoomIn:
1507 case inZoomOut:
1508 if (TrackBox(window, ev->where, windowPart))
1509 {
1510 // TODO setup size event
1511 ZoomWindow( window , windowPart , false ) ;
1512 if (win)
1513 {
1514 Rect tempRect ;
0f493093
SC
1515 GrafPtr port ;
1516 GetPort( &port ) ;
1517 Point pt = { 0, 0 } ;
1518 SetPortWindowPort(window) ;
1519 LocalToGlobal( &pt ) ;
1520 SetPort( port ) ;
8461e4c2
VZ
1521
1522 GetWindowPortBounds(window, &tempRect ) ;
0f493093 1523 win->SetSize( pt.h , pt.v , tempRect.right-tempRect.left ,
8461e4c2
VZ
1524 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
1525 }
1526 }
1527 s_lastMouseDown = 0;
1528 break;
1529 case inCollapseBox :
1530 // TODO setup size event
1531 s_lastMouseDown = 0;
1532 break ;
1533
1534 case inContent :
1535 {
1536 GrafPtr port ;
1537 GetPort( &port ) ;
66a09d47 1538 SetPortWindowPort(window) ;
8461e4c2
VZ
1539 SetPort( port ) ;
1540 }
1541 if ( window != frontWindow && wxTheApp->s_captureWindow == NULL )
1542 {
1543 if ( s_macIsInModalLoop )
1544 {
1545 SysBeep ( 30 ) ;
1546 }
1547 else if ( UMAIsWindowFloating( window ) )
1548 {
1549 if ( win )
1550 win->MacMouseDown( ev , windowPart ) ;
1551 }
1552 else
1553 {
827e7a48
RR
1554 // Activate window first
1555 ::SelectWindow( window ) ;
2dbc444a 1556
827e7a48 1557 // Send event later
8461e4c2
VZ
1558 if ( win )
1559 win->MacMouseDown( ev , windowPart ) ;
8461e4c2
VZ
1560 }
1561 }
1562 else
1563 {
1564 if ( win )
1565 win->MacMouseDown( ev , windowPart ) ;
1566 }
1567 break ;
8461e4c2
VZ
1568 default:
1569 break;
1570 }
169935ad
SC
1571}
1572
76a5e5d2 1573void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr )
169935ad 1574{
76a5e5d2 1575 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1576 WindowRef window;
1577
4f5a3250 1578 short windowPart = inNoWindow ;
e40298d5
JS
1579 if ( wxTheApp->s_captureWindow )
1580 {
1581 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
1582 windowPart = inContent ;
1583 }
1584 else
1585 {
1586 windowPart = ::FindWindow(ev->where, &window) ;
1587 }
8461e4c2
VZ
1588
1589 switch (windowPart)
1590 {
1591 case inMenuBar :
1592 break ;
1593 case inSysWindow :
1594 break ;
1595 default:
1596 {
a3722eeb 1597 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
1598 if ( win )
1599 win->MacMouseUp( ev , windowPart ) ;
1600 }
1601 break;
1602 }
169935ad
SC
1603}
1604
7c9955d1 1605#endif
15b41e90 1606
72055702 1607long wxMacTranslateKey(unsigned char key, unsigned char code) ;
7c551d95 1608long wxMacTranslateKey(unsigned char key, unsigned char code)
9779893b 1609{
8461e4c2 1610 long retval = key ;
9779893b 1611 switch (key)
519cb848 1612 {
12e049f6 1613 case kHomeCharCode :
8461e4c2
VZ
1614 retval = WXK_HOME;
1615 break;
12e049f6 1616 case kEnterCharCode :
8461e4c2
VZ
1617 retval = WXK_RETURN;
1618 break;
12e049f6 1619 case kEndCharCode :
8461e4c2
VZ
1620 retval = WXK_END;
1621 break;
12e049f6 1622 case kHelpCharCode :
8461e4c2
VZ
1623 retval = WXK_HELP;
1624 break;
12e049f6 1625 case kBackspaceCharCode :
8461e4c2
VZ
1626 retval = WXK_BACK;
1627 break;
12e049f6 1628 case kTabCharCode :
8461e4c2
VZ
1629 retval = WXK_TAB;
1630 break;
12e049f6 1631 case kPageUpCharCode :
8461e4c2
VZ
1632 retval = WXK_PAGEUP;
1633 break;
12e049f6 1634 case kPageDownCharCode :
8461e4c2
VZ
1635 retval = WXK_PAGEDOWN;
1636 break;
12e049f6 1637 case kReturnCharCode :
8461e4c2
VZ
1638 retval = WXK_RETURN;
1639 break;
12e049f6 1640 case kFunctionKeyCharCode :
8461e4c2
VZ
1641 {
1642 switch( code )
1643 {
1644 case 0x7a :
1645 retval = WXK_F1 ;
1646 break;
1647 case 0x78 :
1648 retval = WXK_F2 ;
1649 break;
1650 case 0x63 :
1651 retval = WXK_F3 ;
1652 break;
1653 case 0x76 :
1654 retval = WXK_F4 ;
1655 break;
1656 case 0x60 :
1657 retval = WXK_F5 ;
1658 break;
1659 case 0x61 :
1660 retval = WXK_F6 ;
1661 break;
1662 case 0x62:
1663 retval = WXK_F7 ;
1664 break;
1665 case 0x64 :
1666 retval = WXK_F8 ;
1667 break;
1668 case 0x65 :
1669 retval = WXK_F9 ;
1670 break;
1671 case 0x6D :
1672 retval = WXK_F10 ;
1673 break;
1674 case 0x67 :
1675 retval = WXK_F11 ;
1676 break;
1677 case 0x6F :
1678 retval = WXK_F12 ;
1679 break;
1680 case 0x69 :
1681 retval = WXK_F13 ;
1682 break;
1683 case 0x6B :
1684 retval = WXK_F14 ;
1685 break;
1686 case 0x71 :
1687 retval = WXK_F15 ;
1688 break;
1689 }
1690 }
1691 break ;
12e049f6 1692 case kEscapeCharCode :
8461e4c2
VZ
1693 retval = WXK_ESCAPE ;
1694 break ;
12e049f6 1695 case kLeftArrowCharCode :
8461e4c2
VZ
1696 retval = WXK_LEFT ;
1697 break ;
12e049f6 1698 case kRightArrowCharCode :
8461e4c2
VZ
1699 retval = WXK_RIGHT ;
1700 break ;
12e049f6 1701 case kUpArrowCharCode :
8461e4c2
VZ
1702 retval = WXK_UP ;
1703 break ;
12e049f6 1704 case kDownArrowCharCode :
8461e4c2
VZ
1705 retval = WXK_DOWN ;
1706 break ;
12e049f6 1707 case kDeleteCharCode :
8461e4c2
VZ
1708 retval = WXK_DELETE ;
1709 default:
1710 break ;
1711 } // end switch
1712
1713 return retval;
169935ad
SC
1714}
1715
c5c9378c 1716#if !TARGET_CARBON
76a5e5d2 1717void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr )
169935ad 1718{
76a5e5d2 1719 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1720 wxToolTip::RemoveToolTips() ;
1721
1722 UInt32 menuresult = UMAMenuEvent(ev) ;
1723 if ( HiWord( menuresult ) )
1724 {
1725 if ( !s_macIsInModalLoop )
ac2d1ca6 1726 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
8461e4c2
VZ
1727 }
1728 else
1729 {
12e049f6 1730 wxWindow* focus = wxWindow::FindFocus() ;
7c9955d1 1731
12e049f6 1732 if ( MacSendKeyDownEvent( focus , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) == false )
ac2d1ca6 1733 {
1ea39a03
SC
1734#if 0
1735 // we must handle control keys the other way round, otherwise text content is updated too late
ac2d1ca6
SC
1736 // has not been handled -> perform default
1737 wxControl* control = wxDynamicCast( focus , wxControl ) ;
1738 if ( control && control->GetMacControl() != NULL )
8461e4c2 1739 {
12e049f6
SC
1740 short keycode ;
1741 short keychar ;
1742 keychar = short(ev->message & charCodeMask);
1743 keycode = short(ev->message & keyCodeMask) >> 8 ;
ac2d1ca6
SC
1744 ::HandleControlKey( (ControlHandle) control->GetMacControl() , keycode , keychar , ev->modifiers ) ;
1745 }
1ea39a03 1746#endif
ac2d1ca6
SC
1747 }
1748 }
1749}
1750
c5c9378c
SC
1751void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr )
1752{
1753 EventRecord* ev = (EventRecord*) evr ;
1754 wxToolTip::RemoveToolTips() ;
1755
1756 UInt32 menuresult = UMAMenuEvent(ev) ;
1757 if ( HiWord( menuresult ) )
1758 {
1759 }
1760 else
1761 {
1762 MacSendKeyUpEvent( wxWindow::FindFocus() , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) ;
1763 }
1764}
1765
1766#endif
1767
12e049f6 1768bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
ac2d1ca6 1769{
ba12463b
SC
1770 if ( !focus )
1771 return false ;
7c9955d1 1772
12e049f6
SC
1773 short keycode ;
1774 short keychar ;
1775 keychar = short(keymessage & charCodeMask);
1776 keycode = short(keymessage & keyCodeMask) >> 8 ;
7c9955d1 1777
ef08713a 1778 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
12e049f6
SC
1779 {
1780 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1781 // and look at the character after
1782 UInt32 state = 0;
ef08713a 1783 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
12e049f6
SC
1784 keychar = short(keyInfo & charCodeMask);
1785 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1786 }
1787 long keyval = wxMacTranslateKey(keychar, keycode) ;
e40298d5
JS
1788 long realkeyval = keyval ;
1789 if ( keyval == keychar )
1790 {
1791 // we are not on a special character combo -> pass the real os event-value to EVT_CHAR, but not to EVT_KEY (make upper first)
1792 realkeyval = short(keymessage & charCodeMask) ;
1793 keyval = wxToupper( keyval ) ;
1794 }
7c9955d1 1795
ac2d1ca6 1796 wxKeyEvent event(wxEVT_KEY_DOWN);
41d368a4 1797 bool handled = false ;
ac2d1ca6
SC
1798 event.m_shiftDown = modifiers & shiftKey;
1799 event.m_controlDown = modifiers & controlKey;
1800 event.m_altDown = modifiers & optionKey;
1801 event.m_metaDown = modifiers & cmdKey;
ef08713a 1802 event.m_keyCode = keyval ;
ac2d1ca6
SC
1803
1804 event.m_x = wherex;
1805 event.m_y = wherey;
1806 event.m_timeStamp = when;
1807 event.SetEventObject(focus);
1808 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1809 if ( handled && event.GetSkipped() )
1810 handled = false ;
1811 if ( !handled )
1812 {
ba2928c6 1813#if wxUSE_ACCEL
ac2d1ca6
SC
1814 if (!handled)
1815 {
1816 wxWindow *ancestor = focus;
1817 while (ancestor)
1818 {
1819 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1820 if (command != -1)
8461e4c2 1821 {
ac2d1ca6
SC
1822 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1823 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1824 break;
8461e4c2 1825 }
ac2d1ca6
SC
1826 if (ancestor->IsTopLevel())
1827 break;
1828 ancestor = ancestor->GetParent();
8461e4c2 1829 }
ac2d1ca6
SC
1830 }
1831#endif // wxUSE_ACCEL
1832 }
1833 if (!handled)
1834 {
1835 event.Skip( FALSE ) ;
1836 event.SetEventType( wxEVT_CHAR ) ;
12e049f6 1837 // raw value again
ef08713a 1838 event.m_keyCode = realkeyval ;
ac2d1ca6
SC
1839
1840 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1841 if ( handled && event.GetSkipped() )
1842 handled = false ;
1843 }
1844 if ( !handled &&
1845 (keyval == WXK_TAB) &&
35a8698b
SC
1846// CS: copied the change below from wxGTK
1847// VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1848// have this style, yet choose not to process this particular TAB in which
1849// case TAB must still work as a navigational character
1850#if 0
ac2d1ca6 1851 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
35a8698b 1852#endif
ac2d1ca6
SC
1853 (focus->GetParent()) &&
1854 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
1855 {
1856 wxNavigationKeyEvent new_event;
1857 new_event.SetEventObject( focus );
1858 new_event.SetDirection( !event.ShiftDown() );
1859 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1860 new_event.SetWindowChange( event.ControlDown() );
1861 new_event.SetCurrentFocus( focus );
1862 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1863 if ( handled && new_event.GetSkipped() )
1864 handled = false ;
1865 }
1866 // backdoor handler for default return and command escape
1867 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
1868 {
1869 // if window is not having a focus still testing for default enter or cancel
1870 // TODO add the UMA version for ActiveNonFloatingWindow
1871 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
e562df9b
SC
1872 if ( focus )
1873 {
1874 if ( keyval == WXK_RETURN )
1875 {
1876 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
1877 wxButton);
1878 if ( def && def->IsEnabled() )
1879 {
1880 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1881 event.SetEventObject(def);
1882 def->Command(event);
ac2d1ca6 1883 return true ;
e562df9b
SC
1884 }
1885 }
8461e4c2 1886 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
ac2d1ca6 1887 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
8461e4c2 1888 {
e562df9b
SC
1889 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1890 new_event.SetEventObject( focus );
1891 handled = focus->GetEventHandler()->ProcessEvent( new_event );
8461e4c2 1892 }
e562df9b 1893 }
8461e4c2 1894 }
ac2d1ca6 1895 return handled ;
169935ad
SC
1896}
1897
12e049f6 1898bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
ac2d1ca6 1899{
ba12463b
SC
1900 if ( !focus )
1901 return false ;
1902
12e049f6
SC
1903 short keycode ;
1904 short keychar ;
1905 keychar = short(keymessage & charCodeMask);
1906 keycode = short(keymessage & keyCodeMask) >> 8 ;
ef08713a 1907 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
12e049f6
SC
1908 {
1909 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1910 // and look at the character after
1911 UInt32 state = 0;
ef08713a 1912 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
12e049f6
SC
1913 keychar = short(keyInfo & charCodeMask);
1914 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1915 }
1916 long keyval = wxMacTranslateKey(keychar, keycode) ;
1917
e40298d5
JS
1918 if ( keyval == keychar )
1919 {
7c9955d1 1920 keyval = wxToupper( keyval ) ;
e40298d5 1921 }
ac2d1ca6 1922 bool handled = false ;
ac2d1ca6 1923
ba12463b
SC
1924 wxKeyEvent event(wxEVT_KEY_UP);
1925 event.m_shiftDown = modifiers & shiftKey;
1926 event.m_controlDown = modifiers & controlKey;
1927 event.m_altDown = modifiers & optionKey;
1928 event.m_metaDown = modifiers & cmdKey;
ef08713a 1929 event.m_keyCode = keyval ;
ba12463b
SC
1930
1931 event.m_x = wherex;
1932 event.m_y = wherey;
1933 event.m_timeStamp = when;
1934 event.SetEventObject(focus);
1935 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1936
ac2d1ca6
SC
1937 return handled ;
1938}
c5c9378c 1939
15b41e90 1940#if !TARGET_CARBON
76a5e5d2 1941void wxApp::MacHandleActivateEvent( WXEVENTREF evr )
169935ad 1942{
76a5e5d2 1943 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1944 WindowRef window = (WindowRef) ev->message ;
1945 if ( window )
1946 {
1947 bool activate = (ev->modifiers & activeFlag ) ;
1948 WindowClass wclass ;
72055702 1949 ::GetWindowClass ( window , &wclass ) ;
8461e4c2
VZ
1950 if ( wclass == kFloatingWindowClass )
1951 {
1952 // if it is a floater we activate/deactivate the front non-floating window instead
72055702 1953 window = ::FrontNonFloatingWindow() ;
8461e4c2 1954 }
a3722eeb 1955 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2 1956 if ( win )
15b41e90 1957 win->MacActivate( ev->when , activate ) ;
8461e4c2 1958 }
169935ad
SC
1959}
1960
76a5e5d2 1961void wxApp::MacHandleUpdateEvent( WXEVENTREF evr )
169935ad 1962{
76a5e5d2 1963 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1964 WindowRef window = (WindowRef) ev->message ;
a3722eeb 1965 wxTopLevelWindowMac * win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
1966 if ( win )
1967 {
0a67a93b 1968 if ( !wxPendingDelete.Member(win) )
a3722eeb 1969 win->MacUpdate( ev->when ) ;
8461e4c2
VZ
1970 }
1971 else
1972 {
1973 // since there is no way of telling this foreign window to update itself
1974 // we have to invalidate the update region otherwise we keep getting the same
1975 // event over and over again
1976 BeginUpdate( window ) ;
1977 EndUpdate( window ) ;
1978 }
169935ad
SC
1979}
1980
76a5e5d2 1981void wxApp::MacHandleDiskEvent( WXEVENTREF evr )
169935ad 1982{
76a5e5d2 1983 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1984 if ( HiWord( ev->message ) != noErr )
519cb848 1985 {
8461e4c2
VZ
1986 OSErr err ;
1987 Point point ;
1988 SetPt( &point , 100 , 100 ) ;
9779893b 1989
8461e4c2
VZ
1990 err = DIBadMount( point , ev->message ) ;
1991 wxASSERT( err == noErr ) ;
8461e4c2 1992 }
169935ad
SC
1993}
1994
76a5e5d2 1995void wxApp::MacHandleOSEvent( WXEVENTREF evr )
169935ad 1996{
76a5e5d2 1997 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1998 switch( ( ev->message & osEvtMessageMask ) >> 24 )
1999 {
2000 case suspendResumeMessage :
2001 {
2002 bool isResuming = ev->message & resumeFlag ;
8461e4c2 2003 bool convertClipboard = ev->message & convertClipboardFlag ;
15b41e90 2004
8461e4c2
VZ
2005 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
2006 if ( isResuming )
2007 {
2008 WindowRef oldFrontWindow = NULL ;
2009 WindowRef newFrontWindow = NULL ;
2010
2011 // in case we don't take care of activating ourselves, we have to synchronize
2012 // our idea of the active window with the process manager's - which it already activated
2013
2014 if ( !doesActivate )
72055702 2015 oldFrontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
2016
2017 MacResume( convertClipboard ) ;
2018
72055702 2019 newFrontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
2020
2021 if ( oldFrontWindow )
2022 {
a3722eeb 2023 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( oldFrontWindow ) ;
8461e4c2 2024 if ( win )
15b41e90 2025 win->MacActivate( ev->when , false ) ;
8461e4c2
VZ
2026 }
2027 if ( newFrontWindow )
2028 {
a3722eeb 2029 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( newFrontWindow ) ;
8461e4c2 2030 if ( win )
15b41e90 2031 win->MacActivate( ev->when , true ) ;
8461e4c2
VZ
2032 }
2033 }
2034 else
2035 {
2036 MacSuspend( convertClipboard ) ;
8461e4c2
VZ
2037 }
2038 }
2039 break ;
2040 case mouseMovedMessage :
2041 {
2042 WindowRef window;
2043
2044 wxWindow* currentMouseWindow = NULL ;
2045
e40298d5
JS
2046 if (s_captureWindow )
2047 {
2048 currentMouseWindow = s_captureWindow ;
2049 }
2050 else
2051 {
2052 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
2053 &currentMouseWindow ) ;
2054 }
12e049f6 2055
8461e4c2
VZ
2056 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
2057 {
2058 wxMouseEvent event ;
2059
2060 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
2061 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
2062
2063 event.m_leftDown = isDown && !controlDown;
2064 event.m_middleDown = FALSE;
2065 event.m_rightDown = isDown && controlDown;
2066 event.m_shiftDown = ev->modifiers & shiftKey;
2067 event.m_controlDown = ev->modifiers & controlKey;
2068 event.m_altDown = ev->modifiers & optionKey;
2069 event.m_metaDown = ev->modifiers & cmdKey;
2070 event.m_x = ev->where.h;
2071 event.m_y = ev->where.v;
2072 event.m_timeStamp = ev->when;
2073 event.SetEventObject(this);
84c1ffa9 2074
8461e4c2
VZ
2075 if ( wxWindow::s_lastMouseWindow )
2076 {
ed60b502
RR
2077 wxMouseEvent eventleave(event);
2078 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
2079 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
d1bdc41d 2080 eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ;
84c1ffa9 2081
8461e4c2
VZ
2082 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
2083 }
2084 if ( currentMouseWindow )
2085 {
ed60b502
RR
2086 wxMouseEvent evententer(event);
2087 evententer.SetEventType( wxEVT_ENTER_WINDOW );
2088 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
d1bdc41d 2089 evententer.SetEventObject( currentMouseWindow ) ;
8461e4c2
VZ
2090 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
2091 }
2092 wxWindow::s_lastMouseWindow = currentMouseWindow ;
2093 }
2094
12e049f6
SC
2095 short windowPart = inNoWindow ;
2096
e40298d5
JS
2097 if ( s_captureWindow )
2098 {
2099 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
2100 windowPart = inContent ;
2101 }
2102 else
2103 {
7c9955d1 2104 windowPart = ::FindWindow(ev->where, &window);
e40298d5 2105 }
7c9955d1 2106
8461e4c2
VZ
2107 switch (windowPart)
2108 {
49a9ce1b 2109 case inContent :
8461e4c2 2110 {
a3722eeb 2111 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
2112 if ( win )
2113 win->MacMouseMoved( ev , windowPart ) ;
2114 else
49a9ce1b
SC
2115 {
2116 if ( wxIsBusy() )
2117 {
2118 }
2119 else
2120 UMAShowArrowCursor();
2121 }
8461e4c2
VZ
2122 }
2123 break;
49a9ce1b
SC
2124 default :
2125 {
2126 if ( wxIsBusy() )
2127 {
2128 }
2129 else
2130 UMAShowArrowCursor();
2131 }
2132 break ;
8461e4c2
VZ
2133 }
2134 }
2135 break ;
2136
2137 }
169935ad 2138}
3c589c7f
SC
2139#else
2140
2141void wxApp::MacHandleMouseMovedEvent(wxInt32 x , wxInt32 y ,wxUint32 modifiers , long timestamp)
2142{
2143 WindowRef window;
2144
2145 wxWindow* currentMouseWindow = NULL ;
2146
2147 if (s_captureWindow )
2148 {
2149 currentMouseWindow = s_captureWindow ;
2150 }
2151 else
2152 {
2153 wxWindow::MacGetWindowFromPoint( wxPoint( x, y ) , &currentMouseWindow ) ;
2154 }
2155
2156 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
2157 {
2158 wxMouseEvent event ;
2159
2160 bool isDown = !(modifiers & btnState) ; // 1 is for up
2161 bool controlDown = modifiers & controlKey ; // for simulating right mouse
2162
2163 event.m_leftDown = isDown && !controlDown;
2164
2165 event.m_middleDown = FALSE;
2166 event.m_rightDown = isDown && controlDown;
2167
2168 event.m_shiftDown = modifiers & shiftKey;
2169 event.m_controlDown = modifiers & controlKey;
2170 event.m_altDown = modifiers & optionKey;
2171 event.m_metaDown = modifiers & cmdKey;
2dbc444a 2172
3c589c7f
SC
2173 event.m_x = x;
2174 event.m_y = y;
2175 event.m_timeStamp = timestamp;
2176
2177 if ( wxWindow::s_lastMouseWindow )
2178 {
2179 wxMouseEvent eventleave(event);
2180 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
2181 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
2182 eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ;
2183
2184#if wxUSE_TOOLTIPS
2185 wxToolTip::RelayEvent( wxWindow::s_lastMouseWindow , eventleave);
2186#endif // wxUSE_TOOLTIPS
2187 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
2188 }
2189 if ( currentMouseWindow )
2190 {
2191 wxMouseEvent evententer(event);
2192 evententer.SetEventType( wxEVT_ENTER_WINDOW );
2193 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
2194 evententer.SetEventObject( currentMouseWindow ) ;
2195#if wxUSE_TOOLTIPS
2196 wxToolTip::RelayEvent( currentMouseWindow , evententer);
2197#endif // wxUSE_TOOLTIPS
2198 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
2199 }
2200 wxWindow::s_lastMouseWindow = currentMouseWindow ;
2201 }
2202
2203 short windowPart = inNoWindow ;
2204
2205 if ( s_captureWindow )
2206 {
2207 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
2208 windowPart = inContent ;
2209 }
2210 else
2211 {
2212 Point pt= { y , x } ;
2213 windowPart = ::FindWindow(pt , &window);
2214 }
2215
2216 switch (windowPart)
2217 {
2218 case inContent :
2219 {
2220 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
2221 if ( win )
2222 win->MacFireMouseEvent( nullEvent , x , y , modifiers , timestamp ) ;
2223 else
2224 {
2225 if ( wxIsBusy() )
2226 {
2227 }
2228 else
2229 UMAShowArrowCursor();
2230 }
2231 }
2232 break;
2233 default :
2234 {
2235 if ( wxIsBusy() )
2236 {
2237 }
2238 else
2239 UMAShowArrowCursor();
2240 }
2241 break ;
2242 }
2243}
15b41e90
SC
2244#endif
2245
2246void wxApp::MacHandleMenuCommand( wxUint32 id )
2247{
a01fe3d6
RD
2248 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
2249 wxFrame* frame = mbar->GetFrame();
2250 wxCHECK_RET( mbar != NULL && frame != NULL, wxT("error in menu item callback") );
2251 if ( frame )
2252 {
2253 frame->ProcessCommand(id);
2254 }
15b41e90 2255}
169935ad 2256
15b41e90 2257#if !TARGET_CARBON
519cb848 2258void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
169935ad 2259{
8461e4c2
VZ
2260 if (macMenuId == 0)
2261 return; // no menu item selected
2262
2263 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
2264 {
2265 #if ! TARGET_CARBON
2266 Str255 deskAccessoryName ;
2267 GrafPtr savedPort ;
2268
2269 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
2270 GetPort(&savedPort);
2271 OpenDeskAcc(deskAccessoryName);
2272 SetPort(savedPort);
2273 #endif
2274 }
2275 else
2276 {
2b5f62a0
VZ
2277 MenuCommand id ;
2278 GetMenuItemCommandID( GetMenuHandle(macMenuId) , macMenuItemNum , &id ) ;
e40298d5 2279 MacHandleMenuCommand( id ) ;
8461e4c2
VZ
2280 }
2281 HiliteMenu(0);
169935ad 2282}
653a3ed9 2283#endif