]> git.saurik.com Git - wxWidgets.git/blame - src/mac/app.cpp
Added, documented and used wxGridCellChoiceEditor
[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_macDefaultEncodingIsPC = true ;
108bool wxApp::s_macSupportPCMenuShortcuts = true ;
109long wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
882b0479 110long wxApp::s_macPreferencesMenuItemId = wxID_PREFERENCES ;
2b5f62a0 111long wxApp::s_macExitMenuItemId = wxID_EXIT ;
427ff662 112wxString wxApp::s_macHelpMenuTitleName = wxT("&Help") ;
519cb848 113
15b41e90
SC
114//----------------------------------------------------------------------
115// Core Apple Event Support
116//----------------------------------------------------------------------
117
72055702
SC
118pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
119pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
120pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
121pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon ) ;
72055702 122
2a294857 123pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 124{
8461e4c2 125 return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ;
519cb848
SC
126}
127
2a294857 128pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 129{
8461e4c2 130 return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ;
519cb848
SC
131}
132
2a294857 133pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 134{
8461e4c2 135 return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ;
519cb848
SC
136}
137
2a294857 138pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) )
519cb848 139{
8461e4c2 140 return wxTheApp->MacHandleAEQuit( (AppleEvent*) event , reply) ;
519cb848
SC
141}
142
15b41e90 143// AEODoc Calls MacOpenFile on each of the files passed
5ab6aab8 144
2072fbbb
SC
145short wxApp::MacHandleAEODoc(const WXEVENTREF event, WXEVENTREF WXUNUSED(reply))
146{
147 AEDescList docList;
148 AEKeyword keywd;
149 DescType returnedType;
150 Size actualSize;
151 long itemsInList;
152 FSSpec theSpec;
153 OSErr err;
154 short i;
155 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
156 if (err != noErr)
157 return err;
7c9955d1 158
2072fbbb
SC
159 err = AECountItems(&docList, &itemsInList);
160 if (err != noErr)
161 return err;
7c9955d1 162
8461e4c2
VZ
163 ProcessSerialNumber PSN ;
164 PSN.highLongOfPSN = 0 ;
165 PSN.lowLongOfPSN = kCurrentProcess ;
166 SetFrontProcess( &PSN ) ;
7c9955d1 167
2072fbbb
SC
168 for (i = 1; i <= itemsInList; i++) {
169 AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
170 (Ptr) & theSpec, sizeof(theSpec), &actualSize);
171 wxString fName = wxMacFSSpec2MacFilename(&theSpec);
172 MacOpenFile(fName);
173 }
174 return noErr;
519cb848
SC
175}
176
15b41e90
SC
177// AEPDoc Calls MacPrintFile on each of the files passed
178
2072fbbb 179short wxApp::MacHandleAEPDoc(const WXEVENTREF event , WXEVENTREF WXUNUSED(reply))
519cb848 180{
2072fbbb
SC
181 AEDescList docList;
182 AEKeyword keywd;
183 DescType returnedType;
184 Size actualSize;
185 long itemsInList;
186 FSSpec theSpec;
187 OSErr err;
188 short i;
189 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
190 if (err != noErr)
191 return err;
7c9955d1 192
2072fbbb
SC
193 err = AECountItems(&docList, &itemsInList);
194 if (err != noErr)
195 return err;
7c9955d1 196
2072fbbb
SC
197 ProcessSerialNumber PSN ;
198 PSN.highLongOfPSN = 0 ;
199 PSN.lowLongOfPSN = kCurrentProcess ;
200 SetFrontProcess( &PSN ) ;
7c9955d1 201
2072fbbb
SC
202 for (i = 1; i <= itemsInList; i++) {
203 AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType,
204 (Ptr) & theSpec, sizeof(theSpec), &actualSize);
205 wxString fName = wxMacFSSpec2MacFilename(&theSpec);
206 MacPrintFile(fName);
207 }
208 return noErr;
519cb848
SC
209}
210
15b41e90
SC
211// AEOApp calls MacNewFile
212
2a294857 213short wxApp::MacHandleAEOApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
519cb848 214{
2072fbbb 215 MacNewFile() ;
8461e4c2 216 return noErr ;
519cb848
SC
217}
218
76a60a81 219// AEQuit attempts to quit the application
15b41e90 220
2a294857 221short wxApp::MacHandleAEQuit(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
519cb848 222{
c5c9378c 223 /* wxWindow* win = GetTopWindow() ;
8461e4c2
VZ
224 if ( win )
225 {
226 win->Close(TRUE ) ;
227 }
228 else
c5c9378c 229 */
8461e4c2
VZ
230 {
231 ExitMainLoop() ;
232 }
233 return noErr ;
519cb848
SC
234}
235
15b41e90
SC
236//----------------------------------------------------------------------
237// Support Routines linking the Mac...File Calls to the Document Manager
238//----------------------------------------------------------------------
239
240void wxApp::MacOpenFile(const wxString & fileName )
241{
242 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
243 if ( dm )
244 dm->CreateDocument(fileName , wxDOC_SILENT ) ;
245}
246
247void wxApp::MacPrintFile(const wxString & fileName )
248{
249 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
250 if ( dm )
251 {
252 wxDocument *doc = dm->CreateDocument(fileName , wxDOC_SILENT ) ;
253 if ( doc )
254 {
255 wxView* view = doc->GetFirstView() ;
256 if( view )
257 {
258 wxPrintout *printout = view->OnCreatePrintout();
259 if (printout)
260 {
261 wxPrinter printer;
262 printer.Print(view->GetFrame(), printout, TRUE);
263 delete printout;
264 }
265 }
266 if (doc->Close())
267 {
268 doc->DeleteAllViews();
269 dm->RemoveDocument(doc) ;
270 }
271 }
272 }
273}
274
275void wxApp::MacNewFile()
276{
277}
278
c5c9378c
SC
279//----------------------------------------------------------------------
280// Carbon Event Handler
281//----------------------------------------------------------------------
9779893b 282
c5c9378c 283#if TARGET_CARBON
519cb848 284
7c9955d1 285 static const EventTypeSpec eventList[] =
e40298d5
JS
286 {
287 { kEventClassCommand, kEventProcessCommand } ,
288 { kEventClassCommand, kEventCommandUpdateStatus } ,
7c9955d1 289
e32f4a9f
VZ
290 { kEventClassMenu, kEventMenuOpening },
291 { kEventClassMenu, kEventMenuClosed },
292
e40298d5
JS
293 { kEventClassApplication , kEventAppActivated } ,
294 { kEventClassApplication , kEventAppDeactivated } ,
295 // handling the quit event is not recommended by apple
296 // rather using the quit apple event - which we do
7c9955d1 297
e40298d5 298 { kEventClassAppleEvent , kEventAppleEvent } ,
7c9955d1 299
e40298d5 300 { kEventClassMouse , kEventMouseDown } ,
3c589c7f 301 { kEventClassMouse , kEventMouseMoved } ,
6d565349
SC
302 { kEventClassMouse , kEventMouseUp } ,
303 { kEventClassMouse , kEventMouseDragged } ,
e40298d5
JS
304 { 'WXMC' , 'WXMC' }
305 } ;
519cb848 306
e32f4a9f
VZ
307static pascal OSStatus
308MenuEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
519cb848 309{
e32f4a9f
VZ
310 // FIXME: this doesn't work for multiple windows
311 wxWindow *win = wxTheApp->GetTopWindow();
312 if ( win )
313 {
314 // VZ: we could find the menu from its handle here by examining all
315 // the menus in the menu bar recursively but knowing that neither
316 // wxMSW nor wxGTK do it why bother...
317#if 0
318 MenuRef menuRef;
319
320 GetEventParameter(event,
321 kEventParamDirectObject,
322 typeMenuRef, NULL,
323 sizeof(menuRef), NULL,
324 &menuRef);
325#endif // 0
326
5a036f13 327 wxMenuEvent wxevent(GetEventKind(event) == kEventMenuOpening
e32f4a9f
VZ
328 ? wxEVT_MENU_OPEN
329 : wxEVT_MENU_CLOSE);
5a036f13 330 wxevent.SetEventObject(win);
e32f4a9f 331
5a036f13 332 (void)win->GetEventHandler()->ProcessEvent(wxevent);
e32f4a9f
VZ
333 }
334
335 return eventNotHandledErr;
519cb848
SC
336}
337
7c9955d1 338// due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop
c5c9378c
SC
339// but have to use ReceiveNextEvent dealing with events manually, therefore we also have
340// deal with clicks in the menu bar explicitely
519cb848 341
6d565349
SC
342pascal OSStatus wxMacWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) ;
343
c5c9378c 344static pascal OSStatus MouseEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
519cb848 345{
c5c9378c 346 OSStatus result = eventNotHandledErr ;
7c9955d1 347
3c589c7f
SC
348 Point point ;
349 UInt32 modifiers = 0;
350 EventMouseButton button = 0 ;
351 UInt32 click = 0 ;
352
353 GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL,
354 sizeof( Point ), NULL, &point );
355 GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL,
356 sizeof( UInt32 ), NULL, &modifiers );
357 GetEventParameter( event, kEventParamMouseButton, typeMouseButton, NULL,
358 sizeof( EventMouseButton ), NULL, &button );
359 GetEventParameter( event, kEventParamClickCount, typeUInt32, NULL,
360 sizeof( UInt32 ), NULL, &click );
361
362 if ( button == 0 || GetEventKind( event ) == kEventMouseUp )
363 modifiers += btnState ;
364
365
c5c9378c 366 switch( GetEventKind(event) )
8461e4c2 367 {
e40298d5
JS
368 case kEventMouseDown :
369 {
e40298d5 370 WindowRef window ;
7c9955d1 371
e40298d5
JS
372 short windowPart = ::FindWindow(point, &window);
373
374 if ( windowPart == inMenuBar )
375 {
376 MenuSelect( point ) ;
377 result = noErr ;
378 }
379 }
380 break ;
6d565349
SC
381 case kEventMouseDragged :
382 case kEventMouseUp :
383 {
384 if ( wxTheApp->s_captureWindow )
385 wxMacWindowEventHandler( handler , event , (void*) wxTheApp->s_captureWindow->MacGetTopLevelWindow() ) ;
386 }
387 break ;
3c589c7f
SC
388 case kEventMouseMoved :
389 {
390 wxTheApp->MacHandleMouseMovedEvent( point.h , point.v , modifiers , EventTimeToTicks( GetEventTime( event ) ) ) ;
391 result = noErr ;
392 break ;
393 }
394 break ;
7c9955d1 395 }
8461e4c2 396
8461e4c2 397 return result ;
519cb848 398}
169935ad 399
15b41e90
SC
400static pascal OSStatus CommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
401{
402 OSStatus result = eventNotHandledErr ;
403
e40298d5 404 HICommand command ;
7c9955d1 405
e40298d5
JS
406 GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL,
407 sizeof( HICommand ), NULL, &command );
15b41e90 408
e40298d5
JS
409 MenuCommand id = command.commandID ;
410 if ( id == kHICommandPreferences )
411 id = wxApp::s_macPreferencesMenuItemId ;
7c9955d1 412
15b41e90
SC
413 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
414 wxMenu* menu = NULL ;
415 wxMenuItem* item = NULL ;
416
417 if ( mbar )
6d565349 418 {
15b41e90 419 item = mbar->FindItem( id , &menu ) ;
6d565349
SC
420 // it is not 100 % sure that an menu of id 0 is really ours, safety check
421 if ( id == 0 && menu != NULL && menu->GetHMenu() != command.menu.menuRef )
422 {
423 item = NULL ;
424 menu = NULL ;
425 }
426 }
15b41e90
SC
427
428 if ( item == NULL || menu == NULL || mbar == NULL )
e40298d5 429 return result ;
7c9955d1 430
e40298d5
JS
431 switch( GetEventKind( event ) )
432 {
433 case kEventProcessCommand :
434 {
435 if (item->IsCheckable())
436 {
437 item->Check( !item->IsChecked() ) ;
438 }
7c9955d1 439
e40298d5
JS
440 menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ;
441 result = noErr ;
442 }
443 break ;
444 case kEventCommandUpdateStatus:
445 // eventually trigger an updateui round
6d565349 446 result = noErr ;
e40298d5
JS
447 break ;
448 default :
449 break ;
7c9955d1
JS
450 }
451
15b41e90
SC
452 return result ;
453}
454
455static pascal OSStatus ApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
456{
457 OSStatus result = eventNotHandledErr ;
e40298d5
JS
458 switch ( GetEventKind( event ) )
459 {
460 case kEventAppActivated :
461 {
3470af1c
SC
462 if ( wxTheApp )
463 wxTheApp->MacResume( true ) ;
e40298d5
JS
464 result = noErr ;
465 }
466 break ;
467 case kEventAppDeactivated :
468 {
3470af1c
SC
469 if ( wxTheApp )
470 wxTheApp->MacSuspend( true ) ;
e40298d5
JS
471 result = noErr ;
472 }
473 break ;
474 default :
475 break ;
476 }
15b41e90
SC
477 return result ;
478}
479
480pascal OSStatus wxAppEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
481{
482 OSStatus result = eventNotHandledErr ;
483 switch( GetEventClass( event ) )
484 {
e40298d5
JS
485 case kEventClassCommand :
486 result = CommandEventHandler( handler , event , data ) ;
487 break ;
488 case kEventClassApplication :
489 result = ApplicationEventHandler( handler , event , data ) ;
490 break ;
491 case kEventClassMenu :
492 result = MenuEventHandler( handler , event , data ) ;
493 break ;
494 case kEventClassMouse :
495 result = MouseEventHandler( handler , event , data ) ;
496 break ;
497 case kEventClassAppleEvent :
498 {
499 EventRecord rec ;
500 wxMacConvertEventToRecord( event , &rec ) ;
501 result = AEProcessAppleEvent( &rec ) ;
502 }
503 break ;
504 default :
505 break ;
15b41e90
SC
506 }
507
508 return result ;
509}
510
511DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler )
512
513#endif
514
21870a5d 515#if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
53fd991c
SC
516// we know it's there ;-)
517WXIMPORT char std::__throws_bad_alloc ;
518#endif
519
05e2b077 520bool wxApp::Initialize(int& argc, wxChar **argv)
e9576ca5 521{
fbbdb7cd 522 int error = 0 ;
9779893b 523
fbbdb7cd 524 // Mac-specific
9779893b 525
fbbdb7cd
GD
526 UMAInitToolbox( 4 ) ;
527 SetEventMask( everyEvent ) ;
8461e4c2 528 UMAShowWatchCursor() ;
8be97d65 529
21870a5d 530#if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
fbbdb7cd
GD
531 // open shared library resources from here since we don't have
532 // __wxinitialize in Mach-O shared libraries
533 wxStAppResource::OpenSharedLibraryResource(NULL);
534#endif
84c1ffa9 535
f5c6eb5c 536#ifndef __DARWIN__
fbbdb7cd 537 // test the minimal configuration necessary
8be97d65 538
fbbdb7cd 539# if !TARGET_CARBON
8461e4c2
VZ
540 long theSystem ;
541 long theMachine;
542
543 if (Gestalt(gestaltMachineType, &theMachine) != noErr)
544 {
545 error = kMacSTRWrongMachine;
546 }
547 else if (theMachine < gestaltMacPlus)
548 {
549 error = kMacSTRWrongMachine;
550 }
551 else if (Gestalt(gestaltSystemVersion, &theSystem) != noErr )
552 {
553 error = kMacSTROldSystem ;
554 }
72055702 555 else if ( theSystem < 0x0860 )
8461e4c2
VZ
556 {
557 error = kMacSTROldSystem ;
558 }
559 else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap)
560 {
561 error = kMacSTRSmallSize;
562 }
fbbdb7cd 563# endif
8461e4c2
VZ
564 /*
565 else
566 {
567 if ( !UMAHasAppearance() )
568 {
569 error = kMacSTRNoPre8Yet ;
570 }
571 }
572 */
ed5b9811 573#endif
8be97d65 574
8461e4c2 575 // if we encountered any problems so far, give the error code and exit immediately
9779893b 576
169935ad 577 if ( error )
9779893b 578 {
53fd991c 579 wxStAppResource resload ;
8461e4c2
VZ
580 short itemHit;
581 Str255 message;
582
583 GetIndString(message, 128, error);
584 UMAShowArrowCursor() ;
585 ParamText("\pFatal Error", message, (ConstStr255Param)"\p", (ConstStr255Param)"\p");
586 itemHit = Alert(128, nil);
587 return FALSE ;
9779893b 588 }
519cb848 589
f5c6eb5c 590#ifndef __DARWIN__
fbbdb7cd 591# if __option(profile)
882b0479 592 ProfilerInit( collectDetailed, bestTimeBase , 40000 , 50 ) ;
fbbdb7cd 593# endif
9779893b 594#endif
519cb848 595
f5c6eb5c 596#ifndef __DARWIN__
ed581ee2 597 // now avoid exceptions thrown for new (bad_alloc)
e40298d5 598 // FIXME CS for some changes outside wxMac does not compile anymore
15b41e90
SC
599#if 0
600 std::__throws_bad_alloc = 0 ;
601#endif
602
03e11df5 603#endif
427ff662 604 wxMacSetupConverters() ;
7c9955d1 605
8461e4c2 606 s_macCursorRgn = ::NewRgn() ;
8be97d65 607
05e2b077
VZ
608 // Mac OS X passes a process serial number command line argument when
609 // the application is launched from the Finder. This argument must be
610 // removed from the command line arguments before being handled by the
611 // application (otherwise applications would need to handle it)
612 if ( argc > 1 )
613 {
614 static const wxChar *ARG_PSN = _T("-psn_");
615 if ( wxStrncmp(argv[1], ARG_PSN, sizeof(ARG_PSN) - 1) == 0 )
616 {
617 // remove this argument
618 memmove(argv, argv + 1, argc--);
619 }
620 }
621
94826170
VZ
622 if ( !wxAppBase::Initialize(argc, argv) )
623 return false;
e9576ca5 624
376b18ea
VZ
625 wxWinMacWindowList = new wxList(wxKEY_INTEGER);
626 wxWinMacControlList = new wxList(wxKEY_INTEGER);
0a67a93b 627
376b18ea 628 wxMacCreateNotifierTable() ;
9779893b 629
376b18ea 630 UMAShowArrowCursor() ;
8461e4c2 631
94826170 632 return true;
e9576ca5
SC
633}
634
15b41e90
SC
635bool wxApp::OnInitGui()
636{
e40298d5
JS
637 if( !wxAppBase::OnInitGui() )
638 return false ;
7c9955d1
JS
639
640#if TARGET_CARBON
e40298d5 641 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
7c9955d1 642
e40298d5 643 InstallApplicationEventHandler(
7c9955d1 644 GetwxAppEventHandlerUPP(),
73bdd73a 645 GetEventTypeCount(eventList), eventList, wxTheApp, (EventHandlerRef *)&(wxTheApp->m_macEventHandler));
15b41e90 646#endif
7c9955d1 647
15b41e90
SC
648#if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340)
649 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
650 NewAEEventHandlerUPP(AEHandleODoc) ,
651 0 , FALSE ) ;
652 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
653 NewAEEventHandlerUPP(AEHandleOApp) ,
654 0 , FALSE ) ;
655 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
656 NewAEEventHandlerUPP(AEHandlePDoc) ,
657 0 , FALSE ) ;
658 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
659 NewAEEventHandlerUPP(AEHandleQuit) ,
660 0 , FALSE ) ;
661#else
662 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
663 NewAEEventHandlerProc(AEHandleODoc) ,
664 0 , FALSE ) ;
665 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
666 NewAEEventHandlerProc(AEHandleOApp) ,
667 0 , FALSE ) ;
668 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
669 NewAEEventHandlerProc(AEHandlePDoc) ,
670 0 , FALSE ) ;
671 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
672 NewAEEventHandlerProc(AEHandleQuit) ,
673 0 , FALSE ) ;
674#endif
675
e40298d5 676 return TRUE ;
15b41e90
SC
677}
678
e9576ca5
SC
679void wxApp::CleanUp()
680{
12cd5f34 681 wxToolTip::RemoveToolTips() ;
2f1ae414
SC
682
683 // One last chance for pending objects to be cleaned up
684 wxTheApp->DeletePendingObjects();
685
fbbdb7cd 686 wxMacDestroyNotifierTable() ;
84c1ffa9 687
94826170
VZ
688 delete wxWinMacWindowList ;
689 wxWinMacWindowList = NULL;
2f1ae414 690
94826170
VZ
691 delete wxWinMacControlList ;
692 wxWinMacControlList = NULL;
e9576ca5 693
f5c6eb5c 694#ifndef __DARWIN__
fbbdb7cd 695# if __option(profile)
ac2153a6 696 ProfilerDump( (StringPtr)"\papp.prof" ) ;
fbbdb7cd
GD
697 ProfilerTerm() ;
698# endif
9779893b 699#endif
519cb848 700
21870a5d 701#if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
fbbdb7cd
GD
702 // close shared library resources from here since we don't have
703 // __wxterminate in Mach-O shared libraries
704 wxStAppResource::CloseSharedLibraryResource();
705#endif
427ff662 706 wxMacCleanupConverters() ;
7c9955d1 707
8461e4c2 708 UMACleanupToolbox() ;
fbbdb7cd 709 if (s_macCursorRgn) {
76a5e5d2 710 ::DisposeRgn((RgnHandle)s_macCursorRgn);
fbbdb7cd 711 }
84c1ffa9 712
8461e4c2
VZ
713 #if 0
714 TerminateAE() ;
715 #endif
94826170
VZ
716
717 wxAppBase::CleanUp();
e9576ca5
SC
718}
719
92b002a4 720//----------------------------------------------------------------------
05e2b077 721// misc initialization stuff
92b002a4
RD
722//----------------------------------------------------------------------
723
fbbdb7cd
GD
724// extern variable for shared library resource id
725// need to be able to find it with NSLookupAndBindSymbol
726short gSharedLibraryResource = kResFileNotOpened ;
727
21870a5d 728#if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__)
fbbdb7cd 729CFBundleRef gSharedLibraryBundle = NULL;
21870a5d 730#endif /* WXMAKINGDLL_CORE && __DARWIN__ */
53fd991c
SC
731
732wxStAppResource::wxStAppResource()
733{
fbbdb7cd
GD
734 m_currentRefNum = CurResFile() ;
735 if ( gSharedLibraryResource != kResFileNotOpened )
736 {
737 UseResFile( gSharedLibraryResource ) ;
738 }
739}
740
741wxStAppResource::~wxStAppResource()
742{
743 if ( m_currentRefNum != kResFileNotOpened )
744 {
745 UseResFile( m_currentRefNum ) ;
746 }
747}
748
749void wxStAppResource::OpenSharedLibraryResource(const void *initBlock)
750{
751 gSharedLibraryResource = kResFileNotOpened;
752
21870a5d 753#ifdef WXMAKINGDLL_CORE
fbbdb7cd
GD
754 if ( initBlock != NULL ) {
755 const CFragInitBlock *theInitBlock = (const CFragInitBlock *)initBlock;
756 FSSpec *fileSpec = NULL;
84c1ffa9 757
fbbdb7cd
GD
758 if (theInitBlock->fragLocator.where == kDataForkCFragLocator) {
759 fileSpec = theInitBlock->fragLocator.u.onDisk.fileSpec;
760 }
761 else if (theInitBlock->fragLocator.where == kResourceCFragLocator) {
762 fileSpec = theInitBlock->fragLocator.u.inSegs.fileSpec;
763 }
84c1ffa9 764
fbbdb7cd
GD
765 if (fileSpec != NULL) {
766 gSharedLibraryResource = FSpOpenResFile(fileSpec, fsRdPerm);
767 }
768 }
769 else {
770#ifdef __DARWIN__
771 // Open the shared library resource file if it is not yet open
ed581ee2
GD
772 NSSymbol theSymbol;
773 NSModule theModule;
774 const char *theLibPath;
84c1ffa9 775
fbbdb7cd
GD
776 gSharedLibraryBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows"));
777 if (gSharedLibraryBundle != NULL) {
ed581ee2
GD
778 // wxWindows has been bundled into a framework
779 // load the framework resources
84c1ffa9 780
fbbdb7cd 781 gSharedLibraryResource = CFBundleOpenBundleResourceMap(gSharedLibraryBundle);
ed581ee2
GD
782 }
783 else {
784 // wxWindows is a simple dynamic shared library
785 // load the resources from the data fork of a separate resource file
21870a5d
RD
786 wxString theResPath;
787 wxString theName;
ed581ee2
GD
788 FSRef theResRef;
789 OSErr theErr = noErr;
84c1ffa9 790
ed581ee2 791 // get the library path
fbbdb7cd 792 theSymbol = NSLookupAndBindSymbol("_gSharedLibraryResource");
ed581ee2
GD
793 theModule = NSModuleForSymbol(theSymbol);
794 theLibPath = NSLibraryNameForModule(theModule);
ed581ee2 795
376b18ea
VZ
796 // if we call wxLogDebug from here then, as wxTheApp hasn't been
797 // created yet when we're called from wxApp::Initialize(), wxLog
798 // is going to create a default stderr-based log target instead of
799 // the expected normal GUI one -- don't do it, if we really want
800 // to see this message just use fprintf() here
801#if 0
f346733b
GD
802 wxLogDebug( wxT("wxMac library installation name is '%s'"),
803 theLibPath );
376b18ea 804#endif
f346733b 805
ed581ee2 806 // allocate copy to replace .dylib.* extension with .rsrc
21870a5d
RD
807 if (theLibPath != NULL) {
808 theResPath = theLibPath;
809 // replace '_core' with '' in case of multi-lib build
810 theResPath.Replace(wxT("_core"), wxEmptyString);
811 // replace ".dylib" shared library extension with ".rsrc"
812 theResPath.Replace(wxT(".dylib"), wxT(".rsrc"));
813 // Find the begining of the filename
814 theName = theResPath.AfterLast('/');
84c1ffa9 815
376b18ea 816#if 0
f346733b 817 wxLogDebug( wxT("wxMac resources file name is '%s'"),
21870a5d 818 theResPath.mb_str() );
376b18ea 819#endif
ed581ee2 820
21870a5d 821 theErr = FSPathMakeRef((UInt8 *) theResPath.mb_str(), &theResRef, false);
ed581ee2
GD
822 if (theErr != noErr) {
823 // try in current directory (using name only)
21870a5d 824 theErr = FSPathMakeRef((UInt8 *) theName.mb_str(), &theResRef, false);
ed581ee2 825 }
84c1ffa9 826
ed581ee2
GD
827 // open the resource file
828 if (theErr == noErr) {
829 theErr = FSOpenResourceFile( &theResRef, 0, NULL, fsRdPerm,
fbbdb7cd 830 &gSharedLibraryResource);
ed581ee2 831 }
f346733b 832 if (theErr != noErr) {
376b18ea
VZ
833#ifdef __WXDEBUG__
834 fprintf(stderr,
835 wxT("unable to open wxMac resource file '%s'\n"),
21870a5d 836 theResPath.mb_str() );
376b18ea 837#endif // __WXDEBUG__
f346733b
GD
838 }
839
ed581ee2
GD
840 }
841 }
fbbdb7cd 842#endif /* __DARWIN__ */
53fd991c 843 }
21870a5d 844#endif /* WXMAKINGDLL_CORE */
53fd991c
SC
845}
846
fbbdb7cd 847void wxStAppResource::CloseSharedLibraryResource()
53fd991c 848{
21870a5d 849#ifdef WXMAKINGDLL_CORE
ed581ee2 850 // Close the shared library resource file
fbbdb7cd
GD
851 if (gSharedLibraryResource != kResFileNotOpened) {
852#ifdef __DARWIN__
853 if (gSharedLibraryBundle != NULL) {
854 CFBundleCloseBundleResourceMap(gSharedLibraryBundle,
855 gSharedLibraryResource);
856 gSharedLibraryBundle = NULL;
ed581ee2 857 }
fbbdb7cd
GD
858 else
859#endif /* __DARWIN__ */
860 {
861 CloseResFile(gSharedLibraryResource);
ed581ee2 862 }
fbbdb7cd 863 gSharedLibraryResource = kResFileNotOpened;
84c1ffa9 864 }
21870a5d 865#endif /* WXMAKINGDLL_CORE */
53fd991c
SC
866}
867
21870a5d 868#if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__)
53fd991c 869
ed581ee2
GD
870// for shared libraries we have to manually get the correct resource
871// ref num upon initializing and releasing when terminating, therefore
872// the __wxinitialize and __wxterminate must be used
53fd991c 873
53fd991c 874extern "C" {
e40298d5 875 void __sinit(void); /* (generated by linker) */
fbbdb7cd
GD
876 pascal OSErr __initialize(const CFragInitBlock *theInitBlock);
877 pascal void __terminate(void);
53fd991c 878}
53fd991c
SC
879
880pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock)
881{
fbbdb7cd
GD
882 wxStAppResource::OpenSharedLibraryResource( theInitBlock ) ;
883 return __initialize( theInitBlock ) ;
53fd991c
SC
884}
885
886pascal void __wxterminate(void)
887{
fbbdb7cd 888 wxStAppResource::CloseSharedLibraryResource() ;
53fd991c
SC
889 __terminate() ;
890}
ed581ee2 891
21870a5d 892#endif /* WXMAKINGDLL_CORE && !__DARWIN__ */
53fd991c 893
b4efa069
SC
894#if TARGET_CARBON
895
896bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec)
897{
898 bool converted = ConvertEventRefToEventRecord( event,rec) ;
899 OSStatus err = noErr ;
900 if ( !converted )
901 {
902 switch( GetEventClass( event ) )
903 {
904 case kEventClassKeyboard :
905 {
906 converted = true ;
84c1ffa9 907 switch( GetEventKind(event) )
b4efa069
SC
908 {
909 case kEventRawKeyDown :
910 rec->what = keyDown ;
911 break ;
912 case kEventRawKeyRepeat :
913 rec->what = autoKey ;
914 break ;
915 case kEventRawKeyUp :
916 rec->what = keyUp ;
917 break ;
918 case kEventRawKeyModifiersChanged :
919 rec->what = nullEvent ;
920 break ;
921 default :
922 converted = false ;
923 break ;
924 }
925 if ( converted )
926 {
927 UInt32 keyCode ;
928 unsigned char charCode ;
929 UInt32 modifiers ;
930 GetMouse( &rec->where) ;
931
932 err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
933 err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
934 err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
935 rec->modifiers = modifiers ;
936 rec->message = (keyCode << 8 ) + charCode ;
937 }
938 }
939 break ;
940 case kEventClassTextInput :
941 {
942 switch( GetEventKind( event ) )
943 {
944 case kEventTextInputUnicodeForKeyEvent :
945 {
946 EventRef rawEvent ;
947 err = GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ;
948 converted = true ;
949 {
950 UInt32 keyCode ;
951 unsigned char charCode ;
952 UInt32 modifiers ;
953 GetMouse( &rec->where) ;
954 rec->what = keyDown ;
955 err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
956 err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
957 err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
958 rec->modifiers = modifiers ;
959 rec->message = (keyCode << 8 ) + charCode ;
960 }
961 }
962 break ;
963 default :
964 break ;
965 }
966 }
967 break ;
968 }
969 }
84c1ffa9 970
b4efa069
SC
971 return converted ;
972}
973
15b41e90 974/*
b4efa069
SC
975pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
976{
977 OSStatus result = eventNotHandledErr ;
84c1ffa9 978
b4efa069
SC
979 EventRecord rec ;
980 switch ( GetEventClass( event ) )
981 {
982 case kEventClassKeyboard :
983 if ( wxMacConvertEventToRecord( event , &rec ) )
984 {
41d368a4 985 wxTheApp->MacHandleModifierEvents( &rec ) ;
b4efa069
SC
986 wxTheApp->MacHandleOneEvent( &rec ) ;
987 result = noErr ;
988 }
989 break ;
990 case kEventClassTextInput :
991 if ( wxMacConvertEventToRecord( event , &rec ) )
992 {
41d368a4 993 wxTheApp->MacHandleModifierEvents( &rec ) ;
b4efa069
SC
994 wxTheApp->MacHandleOneEvent( &rec ) ;
995 result = noErr ;
996 }
997 break ;
998 default :
999 break ;
1000 }
1001 return result ;
1002}
15b41e90 1003*/
b4efa069
SC
1004#endif
1005
e9576ca5
SC
1006wxApp::wxApp()
1007{
e9576ca5 1008 m_printMode = wxPRINT_WINDOWS;
e9576ca5 1009 m_auto3D = TRUE;
1ea39a03
SC
1010
1011 m_macCurrentEvent = NULL ;
1012 m_macCurrentEventHandlerCallRef = NULL ;
e9576ca5
SC
1013}
1014
1015bool wxApp::Initialized()
1016{
519cb848 1017 if (GetTopWindow())
e9576ca5 1018 return TRUE;
519cb848
SC
1019 else
1020 return FALSE;
e9576ca5
SC
1021}
1022
1023int wxApp::MainLoop()
1024{
e40298d5 1025 m_keepGoing = TRUE;
e9576ca5 1026
e40298d5
JS
1027 while (m_keepGoing)
1028 {
1029 MacDoOneEvent() ;
1030 }
e9576ca5 1031
e40298d5 1032 return 0;
e9576ca5
SC
1033}
1034
e9576ca5
SC
1035void wxApp::ExitMainLoop()
1036{
e40298d5 1037 m_keepGoing = FALSE;
e9576ca5
SC
1038}
1039
1040// Is a message/event pending?
1041bool wxApp::Pending()
1042{
c5c9378c 1043#if TARGET_CARBON
e40298d5 1044 return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ;
c5c9378c 1045#else
8461e4c2 1046 EventRecord event ;
519cb848 1047
e39af974 1048 return EventAvail( everyEvent , &event ) ;
c5c9378c 1049#endif
e9576ca5
SC
1050}
1051
1052// Dispatch a message.
1053void wxApp::Dispatch()
1054{
8461e4c2 1055 MacDoOneEvent() ;
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 }
b7aec135
SC
1399 if ( ev->modifiers ^ s_lastModifiers ) & cmdKey )
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 ) ;
1564
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;
2180
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