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