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