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