]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/app.cpp
TransferFrom/ToWindow() were reverted, fixed
[wxWidgets.git] / src / mac / carbon / 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
1013bool wxApp::Initialized()
1014{
519cb848 1015 if (GetTopWindow())
e9576ca5 1016 return TRUE;
519cb848
SC
1017 else
1018 return FALSE;
e9576ca5
SC
1019}
1020
1021int wxApp::MainLoop()
1022{
e40298d5 1023 m_keepGoing = TRUE;
e9576ca5 1024
e40298d5
JS
1025 while (m_keepGoing)
1026 {
1027 MacDoOneEvent() ;
1028 }
e9576ca5 1029
e40298d5 1030 return 0;
e9576ca5
SC
1031}
1032
e9576ca5
SC
1033void wxApp::ExitMainLoop()
1034{
e40298d5 1035 m_keepGoing = FALSE;
e9576ca5
SC
1036}
1037
1038// Is a message/event pending?
1039bool wxApp::Pending()
1040{
c5c9378c 1041#if TARGET_CARBON
e40298d5 1042 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
c5c9378c 1043#else
8461e4c2 1044 EventRecord event ;
519cb848 1045
e39af974 1046 return EventAvail( everyEvent , &event ) ;
c5c9378c 1047#endif
e9576ca5
SC
1048}
1049
1050// Dispatch a message.
1bf77ee5 1051bool wxApp::Dispatch()
e9576ca5 1052{
8461e4c2 1053 MacDoOneEvent() ;
1bf77ee5
VZ
1054
1055 return true;
e9576ca5
SC
1056}
1057
1058void wxApp::OnIdle(wxIdleEvent& event)
1059{
955a9197 1060 wxAppBase::OnIdle(event);
21870a5d 1061
2f1ae414
SC
1062 // If they are pending events, we must process them: pending events are
1063 // either events to the threads other than main or events posted with
1064 // wxPostEvent() functions
1065 wxMacProcessNotifierAndPendingEvents();
1066
3fdac2ab
DE
1067 if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
1068 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
e9576ca5
SC
1069}
1070
e2478fde 1071void wxApp::WakeUpIdle()
9779893b 1072{
8461e4c2 1073 wxMacWakeUp() ;
9779893b
RD
1074}
1075
e2478fde 1076void wxApp::Exit()
e9576ca5 1077{
2f1ae414 1078 wxApp::CleanUp();
8461e4c2 1079 ::ExitToShell() ;
e9576ca5
SC
1080}
1081
2f1ae414
SC
1082void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1083{
1084 if (GetTopWindow())
1085 GetTopWindow()->Close(TRUE);
1086}
1087
1088// Default behaviour: close the application with prompts. The
1089// user can veto the close, and therefore the end session.
1090void wxApp::OnQueryEndSession(wxCloseEvent& event)
1091{
1092 if (GetTopWindow())
1093 {
1094 if (!GetTopWindow()->Close(!event.CanVeto()))
1095 event.Veto(TRUE);
1096 }
1097}
1098
1099extern "C" void wxCYield() ;
1100void wxCYield()
1101{
8461e4c2 1102 wxYield() ;
2f1ae414
SC
1103}
1104
e9576ca5 1105// Yield to other processes
cb2713bf 1106
8461e4c2 1107bool wxApp::Yield(bool onlyIfNeeded)
e9576ca5 1108{
8461e4c2
VZ
1109 if (s_inYield)
1110 {
1111 if ( !onlyIfNeeded )
1112 {
1113 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1114 }
1115
1116 return FALSE;
1117 }
1118
1119 s_inYield = TRUE;
cb2713bf 1120
2f1ae414 1121#if wxUSE_THREADS
cb2713bf 1122 YieldToAnyThread() ;
2f1ae414 1123#endif
e40298d5 1124 // by definition yield should handle all non-processed events
c5c9378c 1125#if TARGET_CARBON
e40298d5
JS
1126 EventRef theEvent;
1127
1128 OSStatus status = noErr ;
1129 do
1130 {
1131 s_inReceiveEvent = true ;
1132 status = ReceiveNextEvent(0, NULL,kEventDurationNoWait,true,&theEvent) ;
1133 s_inReceiveEvent = false ;
7c9955d1 1134
e40298d5
JS
1135 if ( status == eventLoopTimedOutErr )
1136 {
1137 // make sure next time the event loop will trigger idle events
1138 sleepTime = kEventDurationNoWait ;
1139 }
1140 else if ( status == eventLoopQuitErr )
1141 {
1142 // according to QA1061 this may also occur when a WakeUp Process
1143 // is executed
1144 }
1145 else
1146 {
1147 MacHandleOneEvent( theEvent ) ;
1148 ReleaseEvent(theEvent);
1149 }
1150 } while( status == noErr ) ;
c5c9378c 1151#else
cb2713bf 1152 EventRecord event ;
2f1ae414 1153
e40298d5
JS
1154 // having a larger value here leads to large performance slowdowns
1155 // so we cannot give background apps more processor time here
1156 // we do so however having a large sleep value in the main event loop
7c9955d1 1157 sleepTime = 0 ;
2f1ae414 1158
c5c9378c 1159 while ( !IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, (RgnHandle) wxApp::s_macCursorRgn))
8461e4c2 1160 {
c5c9378c
SC
1161 MacHandleModifierEvents( &event ) ;
1162 MacHandleOneEvent( &event );
8461e4c2 1163 if ( event.what != kHighLevelEvent )
76a5e5d2 1164 SetRectRgn( (RgnHandle) wxApp::s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
8461e4c2 1165 }
c5c9378c
SC
1166 MacHandleModifierEvents( &event ) ;
1167#endif
2f1ae414 1168
8461e4c2 1169 wxMacProcessNotifierAndPendingEvents() ;
8461e4c2 1170 s_inYield = FALSE;
cb2713bf 1171
8461e4c2 1172 return TRUE;
169935ad
SC
1173}
1174
9779893b 1175// platform specifics
169935ad 1176
519cb848
SC
1177void wxApp::MacSuspend( bool convertClipboard )
1178{
15b41e90 1179#if !TARGET_CARBON
1c469f7f 1180 // we have to deactive the top level windows manually
8461e4c2 1181
653a3ed9 1182 wxWindowListNode* node = wxTopLevelWindows.GetFirst();
1c469f7f
SC
1183 while (node)
1184 {
1185 wxTopLevelWindow* win = (wxTopLevelWindow*) node->Data();
b9c10231
SC
1186#if TARGET_CARBON
1187#if 0 // having problems right now with that
6a7e6411 1188 if (!win->HasFlag(wxSTAY_ON_TOP))
21870a5d 1189#endif
b9c10231 1190#endif
6a7e6411 1191 win->MacActivate( ((EventRecord*) MacGetCurrentEvent())->when , false ) ;
8461e4c2 1192
653a3ed9 1193 node = node->GetNext();
1c469f7f 1194 }
84c1ffa9 1195
15b41e90
SC
1196 ::HideFloatingWindows() ;
1197#endif
2a294857 1198 s_lastMouseDown = 0 ;
15b41e90 1199
2a294857
GD
1200 if( convertClipboard )
1201 {
1202 MacConvertPrivateToPublicScrap() ;
1203 }
519cb848
SC
1204}
1205
2a294857
GD
1206extern wxList wxModalDialogs;
1207
519cb848
SC
1208void wxApp::MacResume( bool convertClipboard )
1209{
2a294857
GD
1210 s_lastMouseDown = 0 ;
1211 if( convertClipboard )
1212 {
1213 MacConvertPublicToPrivateScrap() ;
1214 }
84c1ffa9 1215
15b41e90 1216#if !TARGET_CARBON
2a294857 1217 ::ShowFloatingWindows() ;
2a294857 1218 // raise modal dialogs in case a non modal window was selected to activate the app
9779893b 1219
653a3ed9 1220 wxNode* node = wxModalDialogs.GetFirst();
2a294857
GD
1221 while (node)
1222 {
653a3ed9 1223 wxDialog* dialog = (wxDialog *) node->GetData();
2a294857 1224 dialog->Raise();
84c1ffa9 1225
653a3ed9 1226 node = node->GetNext();
2a294857 1227 }
15b41e90 1228#endif
519cb848
SC
1229}
1230
1231void wxApp::MacConvertPrivateToPublicScrap()
1232{
519cb848
SC
1233}
1234
1235void wxApp::MacConvertPublicToPrivateScrap()
1236{
519cb848
SC
1237}
1238
9779893b 1239void wxApp::MacDoOneEvent()
169935ad 1240{
c5c9378c 1241#if TARGET_CARBON
e40298d5 1242 EventRef theEvent;
c5c9378c 1243
e40298d5
JS
1244 s_inReceiveEvent = true ;
1245 OSStatus status = ReceiveNextEvent(0, NULL,sleepTime,true,&theEvent) ;
1246 s_inReceiveEvent = false ;
1247 if ( status == eventLoopTimedOutErr )
1248 {
c5c9378c 1249 if ( wxTheApp->ProcessIdle() )
e40298d5 1250 sleepTime = kEventDurationNoWait ;
c5c9378c 1251 else
e40298d5
JS
1252 sleepTime = kEventDurationForever ;
1253 }
1254 else if ( status == eventLoopQuitErr )
1255 {
1256 // according to QA1061 this may also occur when a WakeUp Process
1257 // is executed
1258 }
1259 else
1260 {
1261 MacHandleOneEvent( theEvent ) ;
1262 ReleaseEvent(theEvent);
3470af1c 1263 sleepTime = kEventDurationNoWait ;
e40298d5 1264 }
c5c9378c 1265#else
e40298d5 1266 EventRecord event ;
169935ad 1267
e40298d5 1268 EventMask eventMask = everyEvent ;
169935ad 1269
c5c9378c 1270 if (WaitNextEvent(eventMask, &event, sleepTime, (RgnHandle) s_macCursorRgn))
8461e4c2 1271 {
41d368a4 1272 MacHandleModifierEvents( &event ) ;
6264b550 1273 MacHandleOneEvent( &event );
8461e4c2
VZ
1274 }
1275 else
1276 {
41d368a4 1277 MacHandleModifierEvents( &event ) ;
8461e4c2 1278 // idlers
72055702 1279 WindowPtr window = ::FrontWindow() ;
8461e4c2 1280 if ( window )
72055702 1281 ::IdleControls( window ) ;
9779893b 1282
c5c9378c 1283 if ( wxTheApp->ProcessIdle() )
e40298d5 1284 sleepTime = 0 ;
c5c9378c 1285 else
e40298d5 1286 sleepTime = GetCaretTime() / 2 ;
8461e4c2
VZ
1287 }
1288 if ( event.what != kHighLevelEvent )
76a5e5d2 1289 SetRectRgn( (RgnHandle) s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ;
c5c9378c 1290#endif
8461e4c2 1291 // repeaters
519cb848 1292
6264b550 1293 DeletePendingObjects() ;
8461e4c2 1294 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1295}
1296
76a5e5d2 1297void wxApp::MacHandleOneEvent( WXEVENTREF evr )
169935ad 1298{
c5c9378c 1299#if TARGET_CARBON
e40298d5
JS
1300 EventTargetRef theTarget;
1301 theTarget = GetEventDispatcherTarget();
c5c9378c 1302 m_macCurrentEvent = evr ;
e40298d5 1303 SendEventToEventTarget ((EventRef) evr , theTarget);
c5c9378c 1304#else
76a5e5d2 1305 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1306 m_macCurrentEvent = ev ;
1307
1308 wxApp::sm_lastMessageTime = ev->when ;
1309
1310 switch (ev->what)
1311 {
1312 case mouseDown:
1313 MacHandleMouseDownEvent( ev ) ;
1314 if ( ev->modifiers & controlKey )
1315 s_lastMouseDown = 2;
1316 else
1317 s_lastMouseDown = 1;
1318 break;
1319 case mouseUp:
1320 if ( s_lastMouseDown == 2 )
1321 {
1322 ev->modifiers |= controlKey ;
1323 }
1324 else
1325 {
1326 ev->modifiers &= ~controlKey ;
1327 }
1328 MacHandleMouseUpEvent( ev ) ;
1329 s_lastMouseDown = 0;
1330 break;
1331 case activateEvt:
1332 MacHandleActivateEvent( ev ) ;
1333 break;
1334 case updateEvt:
1335 MacHandleUpdateEvent( ev ) ;
1336 break;
1337 case keyDown:
1338 case autoKey:
1339 MacHandleKeyDownEvent( ev ) ;
1340 break;
1341 case keyUp:
1342 MacHandleKeyUpEvent( ev ) ;
1343 break;
1344 case diskEvt:
1345 MacHandleDiskEvent( ev ) ;
1346 break;
1347 case osEvt:
1348 MacHandleOSEvent( ev ) ;
1349 break;
1350 case kHighLevelEvent:
1351 MacHandleHighLevelEvent( ev ) ;
1352 break;
1353 default:
1354 break;
1355 }
c5c9378c 1356#endif
8461e4c2 1357 wxMacProcessNotifierAndPendingEvents() ;
169935ad
SC
1358}
1359
c5c9378c
SC
1360#if !TARGET_CARBON
1361bool s_macIsInModalLoop = false ;
1362
1363void wxApp::MacHandleModifierEvents( WXEVENTREF evr )
1364{
1365 EventRecord* ev = (EventRecord*) evr ;
1366 if ( ev->modifiers != s_lastModifiers && wxWindow::FindFocus() != NULL )
1367 {
1368 wxKeyEvent event(wxEVT_KEY_DOWN);
1369
1370 event.m_shiftDown = ev->modifiers & shiftKey;
1371 event.m_controlDown = ev->modifiers & controlKey;
1372 event.m_altDown = ev->modifiers & optionKey;
1373 event.m_metaDown = ev->modifiers & cmdKey;
1374
1375 event.m_x = ev->where.h;
1376 event.m_y = ev->where.v;
1377 event.m_timeStamp = ev->when;
1378 wxWindow* focus = wxWindow::FindFocus() ;
1379 event.SetEventObject(focus);
1380
1381 if ( (ev->modifiers ^ s_lastModifiers ) & controlKey )
1382 {
1383 event.m_keyCode = WXK_CONTROL ;
1384 event.SetEventType( ( ev->modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1385 focus->GetEventHandler()->ProcessEvent( event ) ;
1386 }
1387 if ( (ev->modifiers ^ s_lastModifiers ) & shiftKey )
1388 {
1389 event.m_keyCode = WXK_SHIFT ;
1390 event.SetEventType( ( ev->modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1391 focus->GetEventHandler()->ProcessEvent( event ) ;
1392 }
1393 if ( (ev->modifiers ^ s_lastModifiers ) & optionKey )
1394 {
1395 event.m_keyCode = WXK_ALT ;
1396 event.SetEventType( ( ev->modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1397 focus->GetEventHandler()->ProcessEvent( event ) ;
1398 }
d16477fd 1399 if ( ( ev->modifiers ^ s_lastModifiers ) & cmdKey )
b7aec135
SC
1400 {
1401 event.m_keyCode = WXK_COMMAND ;
1402 event.SetEventType( ( ev->modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ;
1403 focus->GetEventHandler()->ProcessEvent( event ) ;
1404 }
c5c9378c
SC
1405 s_lastModifiers = ev->modifiers ;
1406 }
1407}
1408
76a5e5d2 1409void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr )
169935ad 1410{
738a6daa
SC
1411 // we must avoid reentrancy problems when processing high level events eg printing
1412 bool former = s_inYield ;
1413 s_inYield = TRUE ;
76a5e5d2 1414 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1415 ::AEProcessAppleEvent( ev ) ;
738a6daa 1416 s_inYield = former ;
169935ad
SC
1417}
1418
76a5e5d2 1419void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr )
169935ad 1420{
76a5e5d2 1421 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1422 wxToolTip::RemoveToolTips() ;
2f1ae414 1423
8461e4c2 1424 WindowRef window;
72055702 1425 WindowRef frontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
1426 WindowAttributes frontWindowAttributes = NULL ;
1427 if ( frontWindow )
72055702 1428 ::GetWindowAttributes( frontWindow , &frontWindowAttributes ) ;
9779893b 1429
8461e4c2 1430 short windowPart = ::FindWindow(ev->where, &window);
a3722eeb 1431 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
0a67a93b
SC
1432 if ( wxPendingDelete.Member(win) )
1433 return ;
9779893b 1434
8461e4c2
VZ
1435 BitMap screenBits;
1436 GetQDGlobalsScreenBits( &screenBits );
1437
1438 switch (windowPart)
1439 {
1440 case inMenuBar :
1441 if ( s_macIsInModalLoop )
1442 {
1443 SysBeep ( 30 ) ;
1444 }
1445 else
1446 {
1447 UInt32 menuresult = MenuSelect(ev->where) ;
15b41e90 1448 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) );
8461e4c2
VZ
1449 s_lastMouseDown = 0;
1450 }
1451 break ;
8461e4c2
VZ
1452 case inSysWindow :
1453 SystemClick( ev , window ) ;
1454 s_lastMouseDown = 0;
1455 break ;
8461e4c2
VZ
1456 case inDrag :
1457 if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) )
1458 {
1459 SysBeep ( 30 ) ;
1460 }
1461 else
1462 {
1463 DragWindow(window, ev->where, &screenBits.bounds);
1464 if (win)
1465 {
1466 GrafPtr port ;
1467 GetPort( &port ) ;
1468 Point pt = { 0, 0 } ;
66a09d47 1469 SetPortWindowPort(window) ;
8461e4c2
VZ
1470 LocalToGlobal( &pt ) ;
1471 SetPort( port ) ;
1472 win->SetSize( pt.h , pt.v , -1 ,
1473 -1 , wxSIZE_USE_EXISTING);
1474 }
1475 s_lastMouseDown = 0;
1476 }
1477 break ;
1478 case inGoAway:
1479 if (TrackGoAway(window, ev->where))
1480 {
1481 if ( win )
1482 win->Close() ;
1483 }
1484 s_lastMouseDown = 0;
1485 break;
1486 case inGrow:
1487 {
2b5f62a0
VZ
1488 Rect newContentRect ;
1489 Rect constraintRect ;
1490 constraintRect.top = win->GetMinHeight() ;
1491 if ( constraintRect.top == -1 )
1492 constraintRect.top = 0 ;
1493 constraintRect.left = win->GetMinWidth() ;
1494 if ( constraintRect.left == -1 )
1495 constraintRect.left = 0 ;
1496 constraintRect.right = win->GetMaxWidth() ;
1497 if ( constraintRect.right == -1 )
1498 constraintRect.right = 32000 ;
1499 constraintRect.bottom = win->GetMaxHeight() ;
1500 if ( constraintRect.bottom == -1 )
1501 constraintRect.bottom = 32000 ;
1502
1503 Boolean growResult = ResizeWindow( window , ev->where ,
1504 &constraintRect , &newContentRect ) ;
1505 if ( growResult )
8461e4c2 1506 {
7c9955d1
JS
1507 win->SetSize( newContentRect.left , newContentRect.top ,
1508 newContentRect.right - newContentRect.left ,
2b5f62a0 1509 newContentRect.bottom - newContentRect.top, wxSIZE_USE_EXISTING);
8461e4c2
VZ
1510 }
1511 s_lastMouseDown = 0;
1512 }
1513 break;
1514 case inZoomIn:
1515 case inZoomOut:
1516 if (TrackBox(window, ev->where, windowPart))
1517 {
1518 // TODO setup size event
1519 ZoomWindow( window , windowPart , false ) ;
1520 if (win)
1521 {
1522 Rect tempRect ;
0f493093
SC
1523 GrafPtr port ;
1524 GetPort( &port ) ;
1525 Point pt = { 0, 0 } ;
1526 SetPortWindowPort(window) ;
1527 LocalToGlobal( &pt ) ;
1528 SetPort( port ) ;
8461e4c2
VZ
1529
1530 GetWindowPortBounds(window, &tempRect ) ;
0f493093 1531 win->SetSize( pt.h , pt.v , tempRect.right-tempRect.left ,
8461e4c2
VZ
1532 tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING);
1533 }
1534 }
1535 s_lastMouseDown = 0;
1536 break;
1537 case inCollapseBox :
1538 // TODO setup size event
1539 s_lastMouseDown = 0;
1540 break ;
1541
1542 case inContent :
1543 {
1544 GrafPtr port ;
1545 GetPort( &port ) ;
66a09d47 1546 SetPortWindowPort(window) ;
8461e4c2
VZ
1547 SetPort( port ) ;
1548 }
1549 if ( window != frontWindow && wxTheApp->s_captureWindow == NULL )
1550 {
1551 if ( s_macIsInModalLoop )
1552 {
1553 SysBeep ( 30 ) ;
1554 }
1555 else if ( UMAIsWindowFloating( window ) )
1556 {
1557 if ( win )
1558 win->MacMouseDown( ev , windowPart ) ;
1559 }
1560 else
1561 {
827e7a48
RR
1562 // Activate window first
1563 ::SelectWindow( window ) ;
2dbc444a 1564
827e7a48 1565 // Send event later
8461e4c2
VZ
1566 if ( win )
1567 win->MacMouseDown( ev , windowPart ) ;
8461e4c2
VZ
1568 }
1569 }
1570 else
1571 {
1572 if ( win )
1573 win->MacMouseDown( ev , windowPart ) ;
1574 }
1575 break ;
8461e4c2
VZ
1576 default:
1577 break;
1578 }
169935ad
SC
1579}
1580
76a5e5d2 1581void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr )
169935ad 1582{
76a5e5d2 1583 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1584 WindowRef window;
1585
4f5a3250 1586 short windowPart = inNoWindow ;
e40298d5
JS
1587 if ( wxTheApp->s_captureWindow )
1588 {
1589 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
1590 windowPart = inContent ;
1591 }
1592 else
1593 {
1594 windowPart = ::FindWindow(ev->where, &window) ;
1595 }
8461e4c2
VZ
1596
1597 switch (windowPart)
1598 {
1599 case inMenuBar :
1600 break ;
1601 case inSysWindow :
1602 break ;
1603 default:
1604 {
a3722eeb 1605 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
1606 if ( win )
1607 win->MacMouseUp( ev , windowPart ) ;
1608 }
1609 break;
1610 }
169935ad
SC
1611}
1612
7c9955d1 1613#endif
15b41e90 1614
72055702 1615long wxMacTranslateKey(unsigned char key, unsigned char code) ;
7c551d95 1616long wxMacTranslateKey(unsigned char key, unsigned char code)
9779893b 1617{
8461e4c2 1618 long retval = key ;
9779893b 1619 switch (key)
519cb848 1620 {
12e049f6 1621 case kHomeCharCode :
8461e4c2
VZ
1622 retval = WXK_HOME;
1623 break;
12e049f6 1624 case kEnterCharCode :
8461e4c2
VZ
1625 retval = WXK_RETURN;
1626 break;
12e049f6 1627 case kEndCharCode :
8461e4c2
VZ
1628 retval = WXK_END;
1629 break;
12e049f6 1630 case kHelpCharCode :
8461e4c2
VZ
1631 retval = WXK_HELP;
1632 break;
12e049f6 1633 case kBackspaceCharCode :
8461e4c2
VZ
1634 retval = WXK_BACK;
1635 break;
12e049f6 1636 case kTabCharCode :
8461e4c2
VZ
1637 retval = WXK_TAB;
1638 break;
12e049f6 1639 case kPageUpCharCode :
8461e4c2
VZ
1640 retval = WXK_PAGEUP;
1641 break;
12e049f6 1642 case kPageDownCharCode :
8461e4c2
VZ
1643 retval = WXK_PAGEDOWN;
1644 break;
12e049f6 1645 case kReturnCharCode :
8461e4c2
VZ
1646 retval = WXK_RETURN;
1647 break;
12e049f6 1648 case kFunctionKeyCharCode :
8461e4c2
VZ
1649 {
1650 switch( code )
1651 {
1652 case 0x7a :
1653 retval = WXK_F1 ;
1654 break;
1655 case 0x78 :
1656 retval = WXK_F2 ;
1657 break;
1658 case 0x63 :
1659 retval = WXK_F3 ;
1660 break;
1661 case 0x76 :
1662 retval = WXK_F4 ;
1663 break;
1664 case 0x60 :
1665 retval = WXK_F5 ;
1666 break;
1667 case 0x61 :
1668 retval = WXK_F6 ;
1669 break;
1670 case 0x62:
1671 retval = WXK_F7 ;
1672 break;
1673 case 0x64 :
1674 retval = WXK_F8 ;
1675 break;
1676 case 0x65 :
1677 retval = WXK_F9 ;
1678 break;
1679 case 0x6D :
1680 retval = WXK_F10 ;
1681 break;
1682 case 0x67 :
1683 retval = WXK_F11 ;
1684 break;
1685 case 0x6F :
1686 retval = WXK_F12 ;
1687 break;
1688 case 0x69 :
1689 retval = WXK_F13 ;
1690 break;
1691 case 0x6B :
1692 retval = WXK_F14 ;
1693 break;
1694 case 0x71 :
1695 retval = WXK_F15 ;
1696 break;
1697 }
1698 }
1699 break ;
12e049f6 1700 case kEscapeCharCode :
8461e4c2
VZ
1701 retval = WXK_ESCAPE ;
1702 break ;
12e049f6 1703 case kLeftArrowCharCode :
8461e4c2
VZ
1704 retval = WXK_LEFT ;
1705 break ;
12e049f6 1706 case kRightArrowCharCode :
8461e4c2
VZ
1707 retval = WXK_RIGHT ;
1708 break ;
12e049f6 1709 case kUpArrowCharCode :
8461e4c2
VZ
1710 retval = WXK_UP ;
1711 break ;
12e049f6 1712 case kDownArrowCharCode :
8461e4c2
VZ
1713 retval = WXK_DOWN ;
1714 break ;
12e049f6 1715 case kDeleteCharCode :
8461e4c2
VZ
1716 retval = WXK_DELETE ;
1717 default:
1718 break ;
1719 } // end switch
1720
1721 return retval;
169935ad
SC
1722}
1723
c5c9378c 1724#if !TARGET_CARBON
76a5e5d2 1725void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr )
169935ad 1726{
76a5e5d2 1727 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1728 wxToolTip::RemoveToolTips() ;
1729
1730 UInt32 menuresult = UMAMenuEvent(ev) ;
1731 if ( HiWord( menuresult ) )
1732 {
1733 if ( !s_macIsInModalLoop )
ac2d1ca6 1734 MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ;
8461e4c2
VZ
1735 }
1736 else
1737 {
12e049f6 1738 wxWindow* focus = wxWindow::FindFocus() ;
7c9955d1 1739
12e049f6 1740 if ( MacSendKeyDownEvent( focus , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) == false )
ac2d1ca6 1741 {
1ea39a03
SC
1742#if 0
1743 // we must handle control keys the other way round, otherwise text content is updated too late
ac2d1ca6
SC
1744 // has not been handled -> perform default
1745 wxControl* control = wxDynamicCast( focus , wxControl ) ;
1746 if ( control && control->GetMacControl() != NULL )
8461e4c2 1747 {
12e049f6
SC
1748 short keycode ;
1749 short keychar ;
1750 keychar = short(ev->message & charCodeMask);
1751 keycode = short(ev->message & keyCodeMask) >> 8 ;
ac2d1ca6
SC
1752 ::HandleControlKey( (ControlHandle) control->GetMacControl() , keycode , keychar , ev->modifiers ) ;
1753 }
1ea39a03 1754#endif
ac2d1ca6
SC
1755 }
1756 }
1757}
1758
c5c9378c
SC
1759void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr )
1760{
1761 EventRecord* ev = (EventRecord*) evr ;
1762 wxToolTip::RemoveToolTips() ;
1763
1764 UInt32 menuresult = UMAMenuEvent(ev) ;
1765 if ( HiWord( menuresult ) )
1766 {
1767 }
1768 else
1769 {
1770 MacSendKeyUpEvent( wxWindow::FindFocus() , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) ;
1771 }
1772}
1773
1774#endif
1775
12e049f6 1776bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
ac2d1ca6 1777{
ba12463b
SC
1778 if ( !focus )
1779 return false ;
7c9955d1 1780
12e049f6
SC
1781 short keycode ;
1782 short keychar ;
1783 keychar = short(keymessage & charCodeMask);
1784 keycode = short(keymessage & keyCodeMask) >> 8 ;
7c9955d1 1785
ef08713a 1786 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
12e049f6
SC
1787 {
1788 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1789 // and look at the character after
1790 UInt32 state = 0;
ef08713a 1791 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
12e049f6
SC
1792 keychar = short(keyInfo & charCodeMask);
1793 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1794 }
1795 long keyval = wxMacTranslateKey(keychar, keycode) ;
e40298d5
JS
1796 long realkeyval = keyval ;
1797 if ( keyval == keychar )
1798 {
1799 // 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)
1800 realkeyval = short(keymessage & charCodeMask) ;
1801 keyval = wxToupper( keyval ) ;
1802 }
7c9955d1 1803
ac2d1ca6 1804 wxKeyEvent event(wxEVT_KEY_DOWN);
41d368a4 1805 bool handled = false ;
ac2d1ca6
SC
1806 event.m_shiftDown = modifiers & shiftKey;
1807 event.m_controlDown = modifiers & controlKey;
1808 event.m_altDown = modifiers & optionKey;
1809 event.m_metaDown = modifiers & cmdKey;
ef08713a 1810 event.m_keyCode = keyval ;
ac2d1ca6
SC
1811
1812 event.m_x = wherex;
1813 event.m_y = wherey;
1814 event.m_timeStamp = when;
1815 event.SetEventObject(focus);
1816 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1817 if ( handled && event.GetSkipped() )
1818 handled = false ;
1819 if ( !handled )
1820 {
ba2928c6 1821#if wxUSE_ACCEL
ac2d1ca6
SC
1822 if (!handled)
1823 {
1824 wxWindow *ancestor = focus;
1825 while (ancestor)
1826 {
1827 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1828 if (command != -1)
8461e4c2 1829 {
ac2d1ca6
SC
1830 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1831 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1832 break;
8461e4c2 1833 }
ac2d1ca6
SC
1834 if (ancestor->IsTopLevel())
1835 break;
1836 ancestor = ancestor->GetParent();
8461e4c2 1837 }
ac2d1ca6
SC
1838 }
1839#endif // wxUSE_ACCEL
1840 }
1841 if (!handled)
1842 {
1843 event.Skip( FALSE ) ;
1844 event.SetEventType( wxEVT_CHAR ) ;
12e049f6 1845 // raw value again
ef08713a 1846 event.m_keyCode = realkeyval ;
ac2d1ca6
SC
1847
1848 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1849 if ( handled && event.GetSkipped() )
1850 handled = false ;
1851 }
1852 if ( !handled &&
1853 (keyval == WXK_TAB) &&
35a8698b
SC
1854// CS: copied the change below from wxGTK
1855// VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may
1856// have this style, yet choose not to process this particular TAB in which
1857// case TAB must still work as a navigational character
1858#if 0
ac2d1ca6 1859 (!focus->HasFlag(wxTE_PROCESS_TAB)) &&
35a8698b 1860#endif
ac2d1ca6
SC
1861 (focus->GetParent()) &&
1862 (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
1863 {
1864 wxNavigationKeyEvent new_event;
1865 new_event.SetEventObject( focus );
1866 new_event.SetDirection( !event.ShiftDown() );
1867 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1868 new_event.SetWindowChange( event.ControlDown() );
1869 new_event.SetCurrentFocus( focus );
1870 handled = focus->GetEventHandler()->ProcessEvent( new_event );
1871 if ( handled && new_event.GetSkipped() )
1872 handled = false ;
1873 }
1874 // backdoor handler for default return and command escape
1875 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
1876 {
1877 // if window is not having a focus still testing for default enter or cancel
1878 // TODO add the UMA version for ActiveNonFloatingWindow
1879 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
e562df9b
SC
1880 if ( focus )
1881 {
1882 if ( keyval == WXK_RETURN )
1883 {
1884 wxButton *def = wxDynamicCast(focus->GetDefaultItem(),
1885 wxButton);
1886 if ( def && def->IsEnabled() )
1887 {
1888 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1889 event.SetEventObject(def);
1890 def->Command(event);
ac2d1ca6 1891 return true ;
e562df9b
SC
1892 }
1893 }
8461e4c2 1894 /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */
ac2d1ca6 1895 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
8461e4c2 1896 {
e562df9b
SC
1897 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1898 new_event.SetEventObject( focus );
1899 handled = focus->GetEventHandler()->ProcessEvent( new_event );
8461e4c2 1900 }
e562df9b 1901 }
8461e4c2 1902 }
ac2d1ca6 1903 return handled ;
169935ad
SC
1904}
1905
12e049f6 1906bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey )
ac2d1ca6 1907{
ba12463b
SC
1908 if ( !focus )
1909 return false ;
1910
12e049f6
SC
1911 short keycode ;
1912 short keychar ;
1913 keychar = short(keymessage & charCodeMask);
1914 keycode = short(keymessage & keyCodeMask) >> 8 ;
ef08713a 1915 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
12e049f6
SC
1916 {
1917 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1918 // and look at the character after
1919 UInt32 state = 0;
ef08713a 1920 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
12e049f6
SC
1921 keychar = short(keyInfo & charCodeMask);
1922 keycode = short(keyInfo & keyCodeMask) >> 8 ;
1923 }
1924 long keyval = wxMacTranslateKey(keychar, keycode) ;
1925
e40298d5
JS
1926 if ( keyval == keychar )
1927 {
7c9955d1 1928 keyval = wxToupper( keyval ) ;
e40298d5 1929 }
ac2d1ca6 1930 bool handled = false ;
ac2d1ca6 1931
ba12463b
SC
1932 wxKeyEvent event(wxEVT_KEY_UP);
1933 event.m_shiftDown = modifiers & shiftKey;
1934 event.m_controlDown = modifiers & controlKey;
1935 event.m_altDown = modifiers & optionKey;
1936 event.m_metaDown = modifiers & cmdKey;
ef08713a 1937 event.m_keyCode = keyval ;
ba12463b
SC
1938
1939 event.m_x = wherex;
1940 event.m_y = wherey;
1941 event.m_timeStamp = when;
1942 event.SetEventObject(focus);
1943 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1944
ac2d1ca6
SC
1945 return handled ;
1946}
c5c9378c 1947
15b41e90 1948#if !TARGET_CARBON
76a5e5d2 1949void wxApp::MacHandleActivateEvent( WXEVENTREF evr )
169935ad 1950{
76a5e5d2 1951 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
1952 WindowRef window = (WindowRef) ev->message ;
1953 if ( window )
1954 {
1955 bool activate = (ev->modifiers & activeFlag ) ;
1956 WindowClass wclass ;
72055702 1957 ::GetWindowClass ( window , &wclass ) ;
8461e4c2
VZ
1958 if ( wclass == kFloatingWindowClass )
1959 {
1960 // if it is a floater we activate/deactivate the front non-floating window instead
72055702 1961 window = ::FrontNonFloatingWindow() ;
8461e4c2 1962 }
a3722eeb 1963 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2 1964 if ( win )
15b41e90 1965 win->MacActivate( ev->when , activate ) ;
8461e4c2 1966 }
169935ad
SC
1967}
1968
76a5e5d2 1969void wxApp::MacHandleUpdateEvent( WXEVENTREF evr )
169935ad 1970{
76a5e5d2 1971 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1972 WindowRef window = (WindowRef) ev->message ;
a3722eeb 1973 wxTopLevelWindowMac * win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
1974 if ( win )
1975 {
0a67a93b 1976 if ( !wxPendingDelete.Member(win) )
a3722eeb 1977 win->MacUpdate( ev->when ) ;
8461e4c2
VZ
1978 }
1979 else
1980 {
1981 // since there is no way of telling this foreign window to update itself
1982 // we have to invalidate the update region otherwise we keep getting the same
1983 // event over and over again
1984 BeginUpdate( window ) ;
1985 EndUpdate( window ) ;
1986 }
169935ad
SC
1987}
1988
76a5e5d2 1989void wxApp::MacHandleDiskEvent( WXEVENTREF evr )
169935ad 1990{
76a5e5d2 1991 EventRecord* ev = (EventRecord*) evr ;
8461e4c2 1992 if ( HiWord( ev->message ) != noErr )
519cb848 1993 {
8461e4c2
VZ
1994 OSErr err ;
1995 Point point ;
1996 SetPt( &point , 100 , 100 ) ;
9779893b 1997
8461e4c2
VZ
1998 err = DIBadMount( point , ev->message ) ;
1999 wxASSERT( err == noErr ) ;
8461e4c2 2000 }
169935ad
SC
2001}
2002
76a5e5d2 2003void wxApp::MacHandleOSEvent( WXEVENTREF evr )
169935ad 2004{
76a5e5d2 2005 EventRecord* ev = (EventRecord*) evr ;
8461e4c2
VZ
2006 switch( ( ev->message & osEvtMessageMask ) >> 24 )
2007 {
2008 case suspendResumeMessage :
2009 {
2010 bool isResuming = ev->message & resumeFlag ;
8461e4c2 2011 bool convertClipboard = ev->message & convertClipboardFlag ;
15b41e90 2012
8461e4c2
VZ
2013 bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ;
2014 if ( isResuming )
2015 {
2016 WindowRef oldFrontWindow = NULL ;
2017 WindowRef newFrontWindow = NULL ;
2018
2019 // in case we don't take care of activating ourselves, we have to synchronize
2020 // our idea of the active window with the process manager's - which it already activated
2021
2022 if ( !doesActivate )
72055702 2023 oldFrontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
2024
2025 MacResume( convertClipboard ) ;
2026
72055702 2027 newFrontWindow = ::FrontNonFloatingWindow() ;
8461e4c2
VZ
2028
2029 if ( oldFrontWindow )
2030 {
a3722eeb 2031 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( oldFrontWindow ) ;
8461e4c2 2032 if ( win )
15b41e90 2033 win->MacActivate( ev->when , false ) ;
8461e4c2
VZ
2034 }
2035 if ( newFrontWindow )
2036 {
a3722eeb 2037 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( newFrontWindow ) ;
8461e4c2 2038 if ( win )
15b41e90 2039 win->MacActivate( ev->when , true ) ;
8461e4c2
VZ
2040 }
2041 }
2042 else
2043 {
2044 MacSuspend( convertClipboard ) ;
8461e4c2
VZ
2045 }
2046 }
2047 break ;
2048 case mouseMovedMessage :
2049 {
2050 WindowRef window;
2051
2052 wxWindow* currentMouseWindow = NULL ;
2053
e40298d5
JS
2054 if (s_captureWindow )
2055 {
2056 currentMouseWindow = s_captureWindow ;
2057 }
2058 else
2059 {
2060 wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) ,
2061 &currentMouseWindow ) ;
2062 }
12e049f6 2063
8461e4c2
VZ
2064 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
2065 {
2066 wxMouseEvent event ;
2067
2068 bool isDown = !(ev->modifiers & btnState) ; // 1 is for up
2069 bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse
2070
2071 event.m_leftDown = isDown && !controlDown;
2072 event.m_middleDown = FALSE;
2073 event.m_rightDown = isDown && controlDown;
2074 event.m_shiftDown = ev->modifiers & shiftKey;
2075 event.m_controlDown = ev->modifiers & controlKey;
2076 event.m_altDown = ev->modifiers & optionKey;
2077 event.m_metaDown = ev->modifiers & cmdKey;
2078 event.m_x = ev->where.h;
2079 event.m_y = ev->where.v;
2080 event.m_timeStamp = ev->when;
2081 event.SetEventObject(this);
84c1ffa9 2082
8461e4c2
VZ
2083 if ( wxWindow::s_lastMouseWindow )
2084 {
ed60b502
RR
2085 wxMouseEvent eventleave(event);
2086 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
2087 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
d1bdc41d 2088 eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ;
84c1ffa9 2089
8461e4c2
VZ
2090 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
2091 }
2092 if ( currentMouseWindow )
2093 {
ed60b502
RR
2094 wxMouseEvent evententer(event);
2095 evententer.SetEventType( wxEVT_ENTER_WINDOW );
2096 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
d1bdc41d 2097 evententer.SetEventObject( currentMouseWindow ) ;
8461e4c2
VZ
2098 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
2099 }
2100 wxWindow::s_lastMouseWindow = currentMouseWindow ;
2101 }
2102
12e049f6
SC
2103 short windowPart = inNoWindow ;
2104
e40298d5
JS
2105 if ( s_captureWindow )
2106 {
2107 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
2108 windowPart = inContent ;
2109 }
2110 else
2111 {
7c9955d1 2112 windowPart = ::FindWindow(ev->where, &window);
e40298d5 2113 }
7c9955d1 2114
8461e4c2
VZ
2115 switch (windowPart)
2116 {
49a9ce1b 2117 case inContent :
8461e4c2 2118 {
a3722eeb 2119 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
8461e4c2
VZ
2120 if ( win )
2121 win->MacMouseMoved( ev , windowPart ) ;
2122 else
49a9ce1b
SC
2123 {
2124 if ( wxIsBusy() )
2125 {
2126 }
2127 else
2128 UMAShowArrowCursor();
2129 }
8461e4c2
VZ
2130 }
2131 break;
49a9ce1b
SC
2132 default :
2133 {
2134 if ( wxIsBusy() )
2135 {
2136 }
2137 else
2138 UMAShowArrowCursor();
2139 }
2140 break ;
8461e4c2
VZ
2141 }
2142 }
2143 break ;
2144
2145 }
169935ad 2146}
3c589c7f
SC
2147#else
2148
2149void wxApp::MacHandleMouseMovedEvent(wxInt32 x , wxInt32 y ,wxUint32 modifiers , long timestamp)
2150{
2151 WindowRef window;
2152
2153 wxWindow* currentMouseWindow = NULL ;
2154
2155 if (s_captureWindow )
2156 {
2157 currentMouseWindow = s_captureWindow ;
2158 }
2159 else
2160 {
2161 wxWindow::MacGetWindowFromPoint( wxPoint( x, y ) , &currentMouseWindow ) ;
2162 }
2163
2164 if ( currentMouseWindow != wxWindow::s_lastMouseWindow )
2165 {
2166 wxMouseEvent event ;
2167
2168 bool isDown = !(modifiers & btnState) ; // 1 is for up
2169 bool controlDown = modifiers & controlKey ; // for simulating right mouse
2170
2171 event.m_leftDown = isDown && !controlDown;
2172
2173 event.m_middleDown = FALSE;
2174 event.m_rightDown = isDown && controlDown;
2175
2176 event.m_shiftDown = modifiers & shiftKey;
2177 event.m_controlDown = modifiers & controlKey;
2178 event.m_altDown = modifiers & optionKey;
2179 event.m_metaDown = modifiers & cmdKey;
2dbc444a 2180
3c589c7f
SC
2181 event.m_x = x;
2182 event.m_y = y;
2183 event.m_timeStamp = timestamp;
2184
2185 if ( wxWindow::s_lastMouseWindow )
2186 {
2187 wxMouseEvent eventleave(event);
2188 eventleave.SetEventType( wxEVT_LEAVE_WINDOW );
2189 wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y );
2190 eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ;
2191
2192#if wxUSE_TOOLTIPS
2193 wxToolTip::RelayEvent( wxWindow::s_lastMouseWindow , eventleave);
2194#endif // wxUSE_TOOLTIPS
2195 wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave);
2196 }
2197 if ( currentMouseWindow )
2198 {
2199 wxMouseEvent evententer(event);
2200 evententer.SetEventType( wxEVT_ENTER_WINDOW );
2201 currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y );
2202 evententer.SetEventObject( currentMouseWindow ) ;
2203#if wxUSE_TOOLTIPS
2204 wxToolTip::RelayEvent( currentMouseWindow , evententer);
2205#endif // wxUSE_TOOLTIPS
2206 currentMouseWindow->GetEventHandler()->ProcessEvent(evententer);
2207 }
2208 wxWindow::s_lastMouseWindow = currentMouseWindow ;
2209 }
2210
2211 short windowPart = inNoWindow ;
2212
2213 if ( s_captureWindow )
2214 {
2215 window = (WindowRef) s_captureWindow->MacGetRootWindow() ;
2216 windowPart = inContent ;
2217 }
2218 else
2219 {
2220 Point pt= { y , x } ;
2221 windowPart = ::FindWindow(pt , &window);
2222 }
2223
2224 switch (windowPart)
2225 {
2226 case inContent :
2227 {
2228 wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ;
2229 if ( win )
2230 win->MacFireMouseEvent( nullEvent , x , y , modifiers , timestamp ) ;
2231 else
2232 {
2233 if ( wxIsBusy() )
2234 {
2235 }
2236 else
2237 UMAShowArrowCursor();
2238 }
2239 }
2240 break;
2241 default :
2242 {
2243 if ( wxIsBusy() )
2244 {
2245 }
2246 else
2247 UMAShowArrowCursor();
2248 }
2249 break ;
2250 }
2251}
15b41e90
SC
2252#endif
2253
2254void wxApp::MacHandleMenuCommand( wxUint32 id )
2255{
a01fe3d6
RD
2256 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
2257 wxFrame* frame = mbar->GetFrame();
2258 wxCHECK_RET( mbar != NULL && frame != NULL, wxT("error in menu item callback") );
2259 if ( frame )
2260 {
2261 frame->ProcessCommand(id);
2262 }
15b41e90 2263}
169935ad 2264
15b41e90 2265#if !TARGET_CARBON
519cb848 2266void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum )
169935ad 2267{
8461e4c2
VZ
2268 if (macMenuId == 0)
2269 return; // no menu item selected
2270
2271 if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1)
2272 {
2273 #if ! TARGET_CARBON
2274 Str255 deskAccessoryName ;
2275 GrafPtr savedPort ;
2276
2277 GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName);
2278 GetPort(&savedPort);
2279 OpenDeskAcc(deskAccessoryName);
2280 SetPort(savedPort);
2281 #endif
2282 }
2283 else
2284 {
2b5f62a0
VZ
2285 MenuCommand id ;
2286 GetMenuItemCommandID( GetMenuHandle(macMenuId) , macMenuItemNum , &id ) ;
e40298d5 2287 MacHandleMenuCommand( id ) ;
8461e4c2
VZ
2288 }
2289 HiliteMenu(0);
169935ad 2290}
653a3ed9 2291#endif