]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/app.cpp
guard code for mac / quickdraw
[wxWidgets.git] / src / mac / carbon / app.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
88a7a4e1 2// Name: src/mac/carbon/app.cpp
e9576ca5 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
65571936 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
2996adee 12#include "wx/wxprec.h"
df06d1a4 13
88a7a4e1
WS
14#include "wx/app.h"
15
16#ifndef WX_PRECOMP
17 #include "wx/intl.h"
e4db172a 18 #include "wx/log.h"
de6185e2 19 #include "wx/utils.h"
cdccdfab 20 #include "wx/window.h"
76b49cf4 21 #include "wx/frame.h"
da80ae71 22 #include "wx/dc.h"
f1e01716 23 #include "wx/button.h"
3b3dc801 24 #include "wx/menu.h"
f5590243 25 #include "wx/pen.h"
c64755ed 26 #include "wx/brush.h"
559a723c 27 #include "wx/palette.h"
923d28da 28 #include "wx/icon.h"
c8326d64 29 #include "wx/cursor.h"
fdf565fe 30 #include "wx/dialog.h"
246c5004 31 #include "wx/msgdlg.h"
fec9cc08 32 #include "wx/textctrl.h"
5b56bffb 33 #include "wx/memory.h"
dd05139a 34 #include "wx/gdicmn.h"
02761f6c 35 #include "wx/module.h"
88a7a4e1
WS
36#endif
37
2f1ae414 38#include "wx/tooltip.h"
738a6daa 39#include "wx/docview.h"
1c32ded3 40#include "wx/filename.h"
2260bc5f 41#include "wx/link.h"
738a6daa 42
e9576ca5
SC
43#include <string.h>
44
8be97d65
SC
45// mac
46
03e11df5 47#include "wx/mac/uma.h"
2f1ae414 48
df06d1a4
GD
49#ifdef __DARWIN__
50# include <CoreServices/CoreServices.h>
21870a5d 51# if defined(WXMAKINGDLL_CORE)
ed581ee2
GD
52# include <mach-o/dyld.h>
53# endif
2f1ae414 54#endif
519cb848 55
2260bc5f
PC
56// Keep linker from discarding wxStockGDIMac
57wxFORCE_LINK_MODULE(gdiobj)
58
c5c9378c 59// statics for implementation
902725ee 60static bool s_inYield = false;
902725ee 61static bool s_inReceiveEvent = false ;
c5c9378c 62static EventTime sleepTime = kEventDurationNoWait ;
738a6daa 63
8a9858ae 64
e9576ca5
SC
65IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
66BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
67 EVT_IDLE(wxApp::OnIdle)
15b41e90
SC
68 EVT_END_SESSION(wxApp::OnEndSession)
69 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
e9576ca5 70END_EVENT_TABLE()
e9576ca5 71
169935ad 72
8be97d65 73// platform specific static variables
716d0327 74static const short kwxMacAppleMenuId = 1 ;
519cb848 75
fbbdb7cd 76wxWindow* wxApp::s_captureWindow = NULL ;
41d368a4
SC
77long wxApp::s_lastModifiers = 0 ;
78
fbbdb7cd 79long wxApp::s_macAboutMenuItemId = wxID_ABOUT ;
882b0479 80long wxApp::s_macPreferencesMenuItemId = wxID_PREFERENCES ;
2b5f62a0 81long wxApp::s_macExitMenuItemId = wxID_EXIT ;
427ff662 82wxString wxApp::s_macHelpMenuTitleName = wxT("&Help") ;
519cb848 83
8a9858ae
DS
84bool wxApp::sm_isEmbedded = false; // Normally we're not a plugin
85
15b41e90
SC
86//----------------------------------------------------------------------
87// Core Apple Event Support
88//----------------------------------------------------------------------
89
fb743cab
SC
90pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , SRefCon refcon ) ;
91pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , SRefCon refcon ) ;
92pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , SRefCon refcon ) ;
93pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , SRefCon refcon ) ;
94pascal OSErr AEHandleRApp( const AppleEvent *event , AppleEvent *reply , SRefCon refcon ) ;
72055702 95
fb743cab 96pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , SRefCon WXUNUSED(refcon) )
519cb848 97{
8461e4c2 98 return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ;
519cb848
SC
99}
100
fb743cab 101pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , SRefCon WXUNUSED(refcon) )
519cb848 102{
8461e4c2 103 return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ;
519cb848
SC
104}
105
fb743cab 106pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , SRefCon WXUNUSED(refcon) )
519cb848 107{
8461e4c2 108 return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ;
519cb848
SC
109}
110
fb743cab 111pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , SRefCon WXUNUSED(refcon) )
519cb848 112{
8461e4c2 113 return wxTheApp->MacHandleAEQuit( (AppleEvent*) event , reply) ;
519cb848
SC
114}
115
fb743cab 116pascal OSErr AEHandleRApp( const AppleEvent *event , AppleEvent *reply , SRefCon WXUNUSED(refcon) )
1c32ded3
SC
117{
118 return wxTheApp->MacHandleAERApp( (AppleEvent*) event , reply) ;
119}
120
8786868c 121pascal OSErr AEHandleGURL( const AppleEvent *event , AppleEvent *reply , SRefCon WXUNUSED(refcon) )
947848a1
VZ
122{
123 return wxTheApp->MacHandleAEGURL((WXEVENTREF *)event , reply) ;
124}
125
126
15b41e90 127// AEODoc Calls MacOpenFile on each of the files passed
5ab6aab8 128
2072fbbb
SC
129short wxApp::MacHandleAEODoc(const WXEVENTREF event, WXEVENTREF WXUNUSED(reply))
130{
131 AEDescList docList;
132 AEKeyword keywd;
133 DescType returnedType;
134 Size actualSize;
135 long itemsInList;
2072fbbb
SC
136 OSErr err;
137 short i;
8a9858ae 138
2072fbbb
SC
139 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
140 if (err != noErr)
141 return err;
7c9955d1 142
2072fbbb
SC
143 err = AECountItems(&docList, &itemsInList);
144 if (err != noErr)
145 return err;
7c9955d1 146
8461e4c2
VZ
147 ProcessSerialNumber PSN ;
148 PSN.highLongOfPSN = 0 ;
149 PSN.lowLongOfPSN = kCurrentProcess ;
150 SetFrontProcess( &PSN ) ;
7c9955d1 151
8a9858ae
DS
152 wxString fName ;
153 FSRef theRef ;
154
902725ee 155 for (i = 1; i <= itemsInList; i++)
a2b77260 156 {
8a9858ae
DS
157 AEGetNthPtr(
158 &docList, i, typeFSRef, &keywd, &returnedType,
159 (Ptr)&theRef, sizeof(theRef), &actualSize);
a2b77260
SC
160 fName = wxMacFSRefToPath( &theRef ) ;
161
2072fbbb
SC
162 MacOpenFile(fName);
163 }
8a9858ae 164
2072fbbb 165 return noErr;
519cb848
SC
166}
167
947848a1
VZ
168// AEODoc Calls MacOpenURL on the url passed
169
170short wxApp::MacHandleAEGURL(const WXEVENTREF event, WXEVENTREF WXUNUSED(reply))
171{
172 DescType returnedType;
173 Size actualSize;
174 char url[255];
175 OSErr err = AEGetParamPtr((AppleEvent *)event, keyDirectObject, typeChar,
176 &returnedType, url, sizeof(url)-1,
177 &actualSize);
178 if (err != noErr)
179 return err;
180
181 url[actualSize] = '\0'; // Terminate the C string
182
183 ProcessSerialNumber PSN ;
184 PSN.highLongOfPSN = 0 ;
185 PSN.lowLongOfPSN = kCurrentProcess ;
186 SetFrontProcess( &PSN ) ;
187
188 MacOpenURL(wxString(url, wxConvUTF8));
189
190 return noErr;
191}
192
15b41e90
SC
193// AEPDoc Calls MacPrintFile on each of the files passed
194
2072fbbb 195short wxApp::MacHandleAEPDoc(const WXEVENTREF event , WXEVENTREF WXUNUSED(reply))
519cb848 196{
2072fbbb
SC
197 AEDescList docList;
198 AEKeyword keywd;
199 DescType returnedType;
200 Size actualSize;
201 long itemsInList;
2072fbbb
SC
202 OSErr err;
203 short i;
8a9858ae 204
2072fbbb
SC
205 err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList);
206 if (err != noErr)
207 return err;
7c9955d1 208
2072fbbb
SC
209 err = AECountItems(&docList, &itemsInList);
210 if (err != noErr)
211 return err;
7c9955d1 212
2072fbbb
SC
213 ProcessSerialNumber PSN ;
214 PSN.highLongOfPSN = 0 ;
215 PSN.lowLongOfPSN = kCurrentProcess ;
216 SetFrontProcess( &PSN ) ;
7c9955d1 217
8a9858ae
DS
218 wxString fName ;
219 FSRef theRef ;
a2b77260 220
8a9858ae
DS
221 for (i = 1; i <= itemsInList; i++)
222 {
223 AEGetNthPtr(
224 &docList, i, typeFSRef, &keywd, &returnedType,
225 (Ptr)&theRef, sizeof(theRef), &actualSize);
a2b77260
SC
226 fName = wxMacFSRefToPath( &theRef ) ;
227
2072fbbb
SC
228 MacPrintFile(fName);
229 }
8a9858ae 230
2072fbbb 231 return noErr;
519cb848
SC
232}
233
15b41e90
SC
234// AEOApp calls MacNewFile
235
2a294857 236short wxApp::MacHandleAEOApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
519cb848 237{
2072fbbb 238 MacNewFile() ;
8461e4c2 239 return noErr ;
519cb848
SC
240}
241
76a60a81 242// AEQuit attempts to quit the application
15b41e90 243
2a294857 244short wxApp::MacHandleAEQuit(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
519cb848 245{
38106243 246 wxWindow* win = GetTopWindow() ;
8461e4c2
VZ
247 if ( win )
248 {
38106243
JS
249 wxCommandEvent exitEvent(wxEVT_COMMAND_MENU_SELECTED, s_macExitMenuItemId);
250 if (!win->ProcessEvent(exitEvent))
902725ee 251 win->Close(true) ;
8461e4c2 252 }
38106243 253 else
8461e4c2
VZ
254 {
255 ExitMainLoop() ;
256 }
8a9858ae 257
8461e4c2 258 return noErr ;
519cb848
SC
259}
260
1c32ded3
SC
261// AEROApp calls MacReopenApp
262
263short wxApp::MacHandleAERApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply))
264{
265 MacReopenApp() ;
8a9858ae 266
1c32ded3
SC
267 return noErr ;
268}
269
15b41e90
SC
270//----------------------------------------------------------------------
271// Support Routines linking the Mac...File Calls to the Document Manager
272//----------------------------------------------------------------------
273
274void wxApp::MacOpenFile(const wxString & fileName )
275{
179e085f 276#if wxUSE_DOC_VIEW_ARCHITECTURE
15b41e90
SC
277 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
278 if ( dm )
279 dm->CreateDocument(fileName , wxDOC_SILENT ) ;
179e085f 280#endif
15b41e90
SC
281}
282
89954433 283void wxApp::MacOpenURL(const wxString & WXUNUSED(url) )
947848a1
VZ
284{
285}
179e085f 286
15b41e90
SC
287void wxApp::MacPrintFile(const wxString & fileName )
288{
179e085f
RN
289#if wxUSE_DOC_VIEW_ARCHITECTURE
290
291#if wxUSE_PRINTING_ARCHITECTURE
15b41e90
SC
292 wxDocManager* dm = wxDocManager::GetDocumentManager() ;
293 if ( dm )
294 {
295 wxDocument *doc = dm->CreateDocument(fileName , wxDOC_SILENT ) ;
296 if ( doc )
297 {
298 wxView* view = doc->GetFirstView() ;
8a9858ae 299 if ( view )
15b41e90
SC
300 {
301 wxPrintout *printout = view->OnCreatePrintout();
302 if (printout)
303 {
304 wxPrinter printer;
902725ee 305 printer.Print(view->GetFrame(), printout, true);
15b41e90
SC
306 delete printout;
307 }
308 }
8a9858ae 309
15b41e90
SC
310 if (doc->Close())
311 {
312 doc->DeleteAllViews();
313 dm->RemoveDocument(doc) ;
314 }
315 }
316 }
179e085f
RN
317#endif //print
318
319#endif //docview
15b41e90
SC
320}
321
179e085f
RN
322
323
15b41e90
SC
324void wxApp::MacNewFile()
325{
326}
327
1c32ded3
SC
328void wxApp::MacReopenApp()
329{
3f42061b
SC
330 // HIG says :
331 // if there is no open window -> create a new one
332 // if all windows are hidden -> show the first
333 // if some windows are not hidden -> do nothing
902725ee 334
3f42061b
SC
335 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
336 if ( node == NULL )
902725ee 337 {
3f42061b
SC
338 MacNewFile() ;
339 }
340 else
341 {
3f42061b 342 wxTopLevelWindow* firstIconized = NULL ;
6239ee05 343 wxTopLevelWindow* firstHidden = NULL ;
3f42061b
SC
344 while (node)
345 {
346 wxTopLevelWindow* win = (wxTopLevelWindow*) node->GetData();
6239ee05 347 if ( !win->IsShown() )
3f42061b 348 {
6239ee05
SC
349 // make sure we don't show 'virtual toplevel windows' like wxTaskBarIconWindow
350 if ( firstHidden == NULL && ( wxDynamicCast( win, wxFrame ) || wxDynamicCast( win, wxDialog ) ) )
351 firstHidden = win ;
352 }
353 else if ( win->IsIconized() )
354 {
355 if ( firstIconized == NULL )
356 firstIconized = win ;
3f42061b
SC
357 }
358 else
359 {
6239ee05
SC
360 // we do have a visible, non-iconized toplevelwindow -> do nothing
361 return;
3f42061b 362 }
8a9858ae 363
3f42061b
SC
364 node = node->GetNext();
365 }
8a9858ae 366
3f42061b
SC
367 if ( firstIconized )
368 firstIconized->Iconize( false ) ;
6239ee05
SC
369 else if ( firstHidden )
370 firstHidden->Show( true );
3f42061b 371 }
1c32ded3
SC
372}
373
ead6269a
SC
374//----------------------------------------------------------------------
375// Macintosh CommandID support - converting between native and wx IDs
376//----------------------------------------------------------------------
377
378// if no native match they just return the passed-in id
379
380struct IdPair
381{
382 UInt32 macId ;
383 int wxId ;
384} ;
385
88a7a4e1 386IdPair gCommandIds [] =
ead6269a
SC
387{
388 { kHICommandCut , wxID_CUT } ,
389 { kHICommandCopy , wxID_COPY } ,
390 { kHICommandPaste , wxID_PASTE } ,
391 { kHICommandSelectAll , wxID_SELECTALL } ,
392 { kHICommandClear , wxID_CLEAR } ,
393 { kHICommandUndo , wxID_UNDO } ,
394 { kHICommandRedo , wxID_REDO } ,
395} ;
396
88a7a4e1 397int wxMacCommandToId( UInt32 macCommandId )
ead6269a
SC
398{
399 int wxid = 0 ;
88a7a4e1 400
8a9858ae 401 switch ( macCommandId )
ead6269a 402 {
8a9858ae
DS
403 case kHICommandPreferences :
404 wxid = wxApp::s_macPreferencesMenuItemId ;
405 break ;
406
407 case kHICommandQuit :
408 wxid = wxApp::s_macExitMenuItemId ;
409 break ;
410
411 case kHICommandAbout :
412 wxid = wxApp::s_macAboutMenuItemId ;
413 break ;
414
415 default :
ead6269a 416 {
8a9858ae
DS
417 for ( size_t i = 0 ; i < WXSIZEOF(gCommandIds) ; ++i )
418 {
419 if ( gCommandIds[i].macId == macCommandId )
420 {
421 wxid = gCommandIds[i].wxId ;
422 break ;
423 }
424 }
ead6269a 425 }
8a9858ae 426 break ;
ead6269a 427 }
8a9858ae 428
ead6269a
SC
429 if ( wxid == 0 )
430 wxid = (int) macCommandId ;
8a9858ae 431
ead6269a
SC
432 return wxid ;
433}
434
88a7a4e1 435UInt32 wxIdToMacCommand( int wxId )
ead6269a
SC
436{
437 UInt32 macId = 0 ;
88a7a4e1 438
ead6269a
SC
439 if ( wxId == wxApp::s_macPreferencesMenuItemId )
440 macId = kHICommandPreferences ;
441 else if (wxId == wxApp::s_macExitMenuItemId)
442 macId = kHICommandQuit ;
443 else if (wxId == wxApp::s_macAboutMenuItemId)
444 macId = kHICommandAbout ;
445 else
446 {
447 for ( size_t i = 0 ; i < WXSIZEOF(gCommandIds) ; ++i )
448 {
449 if ( gCommandIds[i].wxId == wxId )
450 {
451 macId = gCommandIds[i].macId ;
452 break ;
453 }
454 }
455 }
8a9858ae 456
ead6269a
SC
457 if ( macId == 0 )
458 macId = (int) wxId ;
8a9858ae 459
ead6269a
SC
460 return macId ;
461}
462
88a7a4e1 463wxMenu* wxFindMenuFromMacCommand( const HICommand &command , wxMenuItem* &item )
ead6269a
SC
464{
465 wxMenu* itemMenu = NULL ;
6239ee05 466#ifndef __WXUNIVERSAL__
ead6269a 467 int id = 0 ;
8a9858ae 468
ead6269a 469 // for 'standard' commands which don't have a wx-menu
8a9858ae 470 if ( command.commandID == kHICommandPreferences || command.commandID == kHICommandQuit || command.commandID == kHICommandAbout )
ead6269a
SC
471 {
472 id = wxMacCommandToId( command.commandID ) ;
8a9858ae 473
ead6269a
SC
474 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
475 if ( mbar )
ead6269a 476 item = mbar->FindItem( id , &itemMenu ) ;
ead6269a
SC
477 }
478 else if ( command.commandID != 0 && command.menu.menuRef != 0 && command.menu.menuItemIndex != 0 )
479 {
480 id = wxMacCommandToId( command.commandID ) ;
481 // make sure it is one of our own menus, or of the 'synthetic' apple and help menus , otherwise don't touch
482 MenuItemIndex firstUserHelpMenuItem ;
4dd25308
VZ
483 static MenuHandle helpMenuHandle = NULL ;
484 if ( helpMenuHandle == NULL )
ead6269a 485 {
4dd25308
VZ
486 if ( UMAGetHelpMenuDontCreate( &helpMenuHandle , &firstUserHelpMenuItem) != noErr )
487 helpMenuHandle = NULL ;
ead6269a 488 }
8a9858ae
DS
489
490 // is it part of the application or the Help menu, then look for the id directly
ead6269a 491 if ( ( GetMenuHandle( kwxMacAppleMenuId ) != NULL && command.menu.menuRef == GetMenuHandle( kwxMacAppleMenuId ) ) ||
a930afc7
KO
492 ( helpMenuHandle != NULL && command.menu.menuRef == helpMenuHandle ) ||
493 wxMenuBar::MacGetWindowMenuHMenu() != NULL && command.menu.menuRef == wxMenuBar::MacGetWindowMenuHMenu() )
ead6269a
SC
494 {
495 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ;
496 if ( mbar )
ead6269a 497 item = mbar->FindItem( id , &itemMenu ) ;
ead6269a
SC
498 }
499 else
500 {
fb743cab 501 URefCon refCon ;
8a9858ae 502
ead6269a
SC
503 GetMenuItemRefCon( command.menu.menuRef , command.menu.menuItemIndex , &refCon ) ;
504 itemMenu = wxFindMenuFromMacMenu( command.menu.menuRef ) ;
505 if ( itemMenu != NULL )
ead6269a 506 item = (wxMenuItem*) refCon ;
ead6269a
SC
507 }
508 }
6239ee05 509#endif
ead6269a
SC
510 return itemMenu ;
511}
512
c5c9378c
SC
513//----------------------------------------------------------------------
514// Carbon Event Handler
515//----------------------------------------------------------------------
9779893b 516
facd6764
SC
517static const EventTypeSpec eventList[] =
518{
519 { kEventClassCommand, kEventProcessCommand } ,
520 { kEventClassCommand, kEventCommandUpdateStatus } ,
7c9955d1 521
facd6764
SC
522 { kEventClassMenu, kEventMenuOpening },
523 { kEventClassMenu, kEventMenuClosed },
524 { kEventClassMenu, kEventMenuTargetItem },
e32f4a9f 525
facd6764
SC
526 { kEventClassApplication , kEventAppActivated } ,
527 { kEventClassApplication , kEventAppDeactivated } ,
528 // handling the quit event is not recommended by apple
529 // rather using the quit apple event - which we do
7c9955d1 530
facd6764 531 { kEventClassAppleEvent , kEventAppleEvent } ,
7c9955d1 532
facd6764
SC
533 { kEventClassMouse , kEventMouseDown } ,
534 { kEventClassMouse , kEventMouseMoved } ,
535 { kEventClassMouse , kEventMouseUp } ,
536 { kEventClassMouse , kEventMouseDragged } ,
537 { 'WXMC' , 'WXMC' }
538} ;
519cb848 539
e32f4a9f 540static pascal OSStatus
89954433
VZ
541wxMacAppMenuEventHandler( EventHandlerCallRef WXUNUSED(handler),
542 EventRef event,
543 void *WXUNUSED(data) )
902725ee 544{
dae6d730
SC
545 wxMacCarbonEvent cEvent( event ) ;
546 MenuRef menuRef = cEvent.GetParameter<MenuRef>(kEventParamDirectObject) ;
6239ee05 547#ifndef __WXUNIVERSAL__
dae6d730 548 wxMenu* menu = wxFindMenuFromMacMenu( menuRef ) ;
902725ee 549
dae6d730 550 if ( menu )
e32f4a9f 551 {
902725ee 552 wxEventType type=0;
dae6d730
SC
553 MenuCommand cmd=0;
554 switch (GetEventKind(event))
852b1185 555 {
dae6d730
SC
556 case kEventMenuOpening:
557 type = wxEVT_MENU_OPEN;
558 break;
8a9858ae 559
dae6d730
SC
560 case kEventMenuClosed:
561 type = wxEVT_MENU_CLOSE;
562 break;
8a9858ae 563
dae6d730
SC
564 case kEventMenuTargetItem:
565 cmd = cEvent.GetParameter<MenuCommand>(kEventParamMenuCommand,typeMenuCommand) ;
902725ee 566 if (cmd != 0)
9f2bfce7 567 type = wxEVT_MENU_HIGHLIGHT;
dae6d730 568 break;
8a9858ae 569
dae6d730
SC
570 default:
571 wxFAIL_MSG(wxT("Unexpected menu event kind"));
572 break;
573 }
852b1185 574
dae6d730
SC
575 if ( type )
576 {
37a4035b 577 wxMenuEvent wxevent(type, cmd, menu);
dae6d730 578 wxevent.SetEventObject(menu);
e32f4a9f 579
dae6d730
SC
580 wxEvtHandler* handler = menu->GetEventHandler();
581 if (handler && handler->ProcessEvent(wxevent))
582 {
583 // handled
584 }
585 else
586 {
587 wxWindow *win = menu->GetInvokingWindow();
902725ee 588 if (win)
dae6d730
SC
589 win->GetEventHandler()->ProcessEvent(wxevent);
590 }
591 }
e32f4a9f 592 }
6239ee05 593#endif
e32f4a9f 594 return eventNotHandledErr;
519cb848
SC
595}
596
1bd568fa 597#ifndef __LP64__
89954433
VZ
598static pascal OSStatus
599wxMacAppCommandEventHandler( EventHandlerCallRef WXUNUSED(handler) ,
600 EventRef event ,
601 void *WXUNUSED(data) )
15b41e90
SC
602{
603 OSStatus result = eventNotHandledErr ;
604
e40298d5 605 HICommand command ;
7c9955d1 606
dae6d730
SC
607 wxMacCarbonEvent cEvent( event ) ;
608 cEvent.GetParameter<HICommand>(kEventParamDirectObject,typeHICommand,&command) ;
902725ee 609
dae6d730 610 wxMenuItem* item = NULL ;
ead6269a
SC
611 wxMenu* itemMenu = wxFindMenuFromMacCommand( command , item ) ;
612 int id = wxMacCommandToId( command.commandID ) ;
902725ee 613
ead6269a 614 if ( item )
6567c540 615 {
ead6269a 616 wxASSERT( itemMenu != NULL ) ;
88a7a4e1 617
8a9858ae 618 switch ( cEvent.GetKind() )
6d565349 619 {
ead6269a 620 case kEventProcessCommand :
6239ee05 621 result = itemMenu->MacHandleCommandProcess( item, id );
dae6d730 622 break ;
8a9858ae 623
e40298d5 624 case kEventCommandUpdateStatus:
6239ee05 625 result = itemMenu->MacHandleCommandUpdateStatus( item, id );
e40298d5 626 break ;
8a9858ae 627
dae6d730
SC
628 default :
629 break ;
630 }
631 }
15b41e90
SC
632 return result ;
633}
1bd568fa 634#endif
15b41e90 635
89954433
VZ
636static pascal OSStatus
637wxMacAppApplicationEventHandler( EventHandlerCallRef WXUNUSED(handler) ,
638 EventRef event ,
639 void *WXUNUSED(data) )
15b41e90
SC
640{
641 OSStatus result = eventNotHandledErr ;
e40298d5
JS
642 switch ( GetEventKind( event ) )
643 {
644 case kEventAppActivated :
8a9858ae
DS
645 if ( wxTheApp )
646 wxTheApp->SetActive( true , NULL ) ;
647 result = noErr ;
e40298d5 648 break ;
8a9858ae 649
e40298d5 650 case kEventAppDeactivated :
8a9858ae
DS
651 if ( wxTheApp )
652 wxTheApp->SetActive( false , NULL ) ;
653 result = noErr ;
e40298d5 654 break ;
8a9858ae 655
e40298d5
JS
656 default :
657 break ;
658 }
8a9858ae 659
15b41e90
SC
660 return result ;
661}
662
facd6764 663pascal OSStatus wxMacAppEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
15b41e90 664{
27740109
SC
665 EventRef formerEvent = (EventRef) wxTheApp->MacGetCurrentEvent() ;
666 EventHandlerCallRef formerEventHandlerCallRef = (EventHandlerCallRef) wxTheApp->MacGetCurrentEventHandlerCallRef() ;
667 wxTheApp->MacSetCurrentEvent( event , handler ) ;
668
15b41e90 669 OSStatus result = eventNotHandledErr ;
8a9858ae 670 switch ( GetEventClass( event ) )
15b41e90 671 {
1bd568fa 672#ifndef __LP64__
e40298d5 673 case kEventClassCommand :
facd6764 674 result = wxMacAppCommandEventHandler( handler , event , data ) ;
e40298d5 675 break ;
1bd568fa 676#endif
e40298d5 677 case kEventClassApplication :
facd6764 678 result = wxMacAppApplicationEventHandler( handler , event , data ) ;
e40298d5 679 break ;
1bd568fa 680#ifndef __LP64__
e40298d5 681 case kEventClassMenu :
facd6764 682 result = wxMacAppMenuEventHandler( handler , event , data ) ;
e40298d5 683 break ;
8a9858ae 684
e40298d5 685 case kEventClassMouse :
95a8b77a
SC
686 {
687 wxMacCarbonEvent cEvent( event ) ;
902725ee 688
95a8b77a
SC
689 WindowRef window ;
690 Point screenMouseLocation = cEvent.GetParameter<Point>(kEventParamMouseLocation) ;
255df092 691 ::FindWindow(screenMouseLocation, &window);
95a8b77a
SC
692 // only send this event in case it had not already been sent to a tlw, as we get
693 // double events otherwise (in case event.skip) was called
694 if ( window == NULL )
695 result = wxMacTopLevelMouseEventHandler( handler , event , NULL ) ;
696 }
e40298d5 697 break ;
1bd568fa 698#endif
e40298d5
JS
699 case kEventClassAppleEvent :
700 {
1bd568fa
SC
701#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
702 if ( AEProcessEvent != NULL )
703 {
704 result = AEProcessEvent(event);
705 }
706#endif
707#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
708 {
709 EventRecord rec ;
8a9858ae 710
1bd568fa
SC
711 wxMacConvertEventToRecord( event , &rec ) ;
712 result = AEProcessAppleEvent( &rec ) ;
713 }
714#endif
e40298d5
JS
715 }
716 break ;
8a9858ae 717
e40298d5
JS
718 default :
719 break ;
15b41e90
SC
720 }
721
27740109
SC
722 wxTheApp->MacSetCurrentEvent( formerEvent, formerEventHandlerCallRef ) ;
723
15b41e90
SC
724 return result ;
725}
726
facd6764 727DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacAppEventHandler )
15b41e90 728
e822273a 729#ifdef __WXDEBUG__
8d1147f9 730
89954433
VZ
731pascal static void
732wxMacAssertOutputHandler(OSType WXUNUSED(componentSignature),
733 UInt32 WXUNUSED(options),
734 const char *assertionString,
735 const char *exceptionLabelString,
736 const char *errorString,
737 const char *fileName,
738 long lineNumber,
739 void *value,
740 ConstStr255Param WXUNUSED(outputMsg))
e9576ca5 741{
facd6764
SC
742 // flow into assert handling
743 wxString fileNameStr ;
744 wxString assertionStr ;
745 wxString exceptionStr ;
746 wxString errorStr ;
8a9858ae 747
facd6764
SC
748#if wxUSE_UNICODE
749 fileNameStr = wxString(fileName, wxConvLocal);
750 assertionStr = wxString(assertionString, wxConvLocal);
751 exceptionStr = wxString((exceptionLabelString!=0) ? exceptionLabelString : "", wxConvLocal) ;
752 errorStr = wxString((errorString!=0) ? errorString : "", wxConvLocal) ;
753#else
754 fileNameStr = fileName;
755 assertionStr = assertionString;
756 exceptionStr = (exceptionLabelString!=0) ? exceptionLabelString : "" ;
757 errorStr = (errorString!=0) ? errorString : "" ;
758#endif
9779893b 759
facd6764
SC
760#if 1
761 // flow into log
902725ee
WS
762 wxLogDebug( wxT("AssertMacros: %s %s %s file: %s, line: %ld (value %p)\n"),
763 assertionStr.c_str() ,
764 exceptionStr.c_str() ,
765 errorStr.c_str(),
facd6764
SC
766 fileNameStr.c_str(), lineNumber ,
767 value ) ;
768#else
769
770 wxOnAssert(fileNameStr, lineNumber , assertionStr ,
8a9858ae 771 wxString::Format( wxT("%s %s value (%p)") , exceptionStr, errorStr , value ) ) ;
facd6764
SC
772#endif
773}
774
8d1147f9
RN
775#endif //__WXDEBUG__
776
89954433 777extern "C" void macPostedEventCallback(void *WXUNUSED(unused))
8a9858ae
DS
778{
779 wxTheApp->ProcessPendingEvents();
780}
ec8565f3 781
9d463591
SC
782ProcessSerialNumber gAppProcess ;
783
facd6764
SC
784bool wxApp::Initialize(int& argc, wxChar **argv)
785{
fbbdb7cd 786 // Mac-specific
9779893b 787
e822273a 788#ifdef __WXDEBUG__
8a9858ae 789 InstallDebugAssertOutputHandler( NewDebugAssertOutputHandlerUPP( wxMacAssertOutputHandler ) );
facd6764 790#endif
8a9858ae 791
e128397f 792 UMAInitToolbox( 4, sm_isEmbedded ) ;
1bd568fa 793// TODO CHECK Can Be Removed SetEventMask( everyEvent ) ;
8461e4c2 794 UMAShowWatchCursor() ;
8be97d65 795
05e2b077
VZ
796 // Mac OS X passes a process serial number command line argument when
797 // the application is launched from the Finder. This argument must be
798 // removed from the command line arguments before being handled by the
799 // application (otherwise applications would need to handle it)
800 if ( argc > 1 )
801 {
802 static const wxChar *ARG_PSN = _T("-psn_");
46e2a2e7 803 if ( wxStrncmp(argv[1], ARG_PSN, wxStrlen(ARG_PSN)) == 0 )
05e2b077
VZ
804 {
805 // remove this argument
33f39af3
GD
806 --argc;
807 memmove(argv + 1, argv + 2, argc * sizeof(char *));
05e2b077
VZ
808 }
809 }
810
94826170
VZ
811 if ( !wxAppBase::Initialize(argc, argv) )
812 return false;
e9576ca5 813
9d463591
SC
814 GetCurrentProcess(&gAppProcess);
815
d9d19e75
SC
816#if wxUSE_INTL
817 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
818#endif
819
e256c692
SC
820 // these might be the startup dirs, set them to the 'usual' dir containing the app bundle
821 wxString startupCwd = wxGetCwd() ;
6601362a 822 if ( startupCwd == wxT("/") || startupCwd.Right(15) == wxT("/Contents/MacOS") )
e256c692 823 {
e256c692
SC
824 CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle() ) ;
825 CFURLRef urlParent = CFURLCreateCopyDeletingLastPathComponent( kCFAllocatorDefault , url ) ;
826 CFRelease( url ) ;
827 CFStringRef path = CFURLCopyFileSystemPath ( urlParent , kCFURLPOSIXPathStyle ) ;
828 CFRelease( urlParent ) ;
902725ee 829 wxString cwd = wxMacCFStringHolder(path).AsString(wxLocale::GetSystemEncoding());
e256c692
SC
830 wxSetWorkingDirectory( cwd ) ;
831 }
9779893b 832
95e568ee
KH
833 /* connect posted events to common-mode run loop so that wxPostEvent events
834 are handled even while we're in the menu or on a scrollbar */
835 CFRunLoopSourceContext event_posted_context = {0};
836 event_posted_context.perform = macPostedEventCallback;
837 m_macEventPosted = CFRunLoopSourceCreate(NULL,0,&event_posted_context);
838 CFRunLoopAddSource(CFRunLoopGetCurrent(), m_macEventPosted, kCFRunLoopCommonModes);
7113b61a
SC
839 // run loop takes ownership
840 CFRelease(m_macEventPosted);
95e568ee 841
376b18ea 842 UMAShowArrowCursor() ;
8461e4c2 843
94826170 844 return true;
e9576ca5
SC
845}
846
9b58521d 847AEEventHandlerUPP sODocHandler = NULL ;
947848a1 848AEEventHandlerUPP sGURLHandler = NULL ;
9b58521d
SC
849AEEventHandlerUPP sOAppHandler = NULL ;
850AEEventHandlerUPP sPDocHandler = NULL ;
851AEEventHandlerUPP sRAppHandler = NULL ;
852AEEventHandlerUPP sQuitHandler = NULL ;
853
15b41e90
SC
854bool wxApp::OnInitGui()
855{
8a9858ae 856 if ( !wxAppBase::OnInitGui() )
e40298d5 857 return false ;
1bd568fa 858#ifndef __LP64__
e40298d5 859 InstallStandardEventHandler( GetApplicationEventTarget() ) ;
e128397f
DS
860 if (!sm_isEmbedded)
861 {
862 InstallApplicationEventHandler(
facd6764 863 GetwxMacAppEventHandlerUPP(),
e128397f
DS
864 GetEventTypeCount(eventList), eventList, wxTheApp, (EventHandlerRef *)&(wxTheApp->m_macEventHandler));
865 }
1bd568fa 866#endif
7c9955d1 867
e128397f
DS
868 if (!sm_isEmbedded)
869 {
9b58521d 870 sODocHandler = NewAEEventHandlerUPP(AEHandleODoc) ;
947848a1 871 sGURLHandler = NewAEEventHandlerUPP(AEHandleGURL) ;
9b58521d
SC
872 sOAppHandler = NewAEEventHandlerUPP(AEHandleOApp) ;
873 sPDocHandler = NewAEEventHandlerUPP(AEHandlePDoc) ;
874 sRAppHandler = NewAEEventHandlerUPP(AEHandleRApp) ;
875 sQuitHandler = NewAEEventHandlerUPP(AEHandleQuit) ;
876
e128397f 877 AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments ,
9b58521d 878 sODocHandler , 0 , FALSE ) ;
947848a1
VZ
879 AEInstallEventHandler( kInternetEventClass, kAEGetURL,
880 sGURLHandler , 0 , FALSE ) ;
e128397f 881 AEInstallEventHandler( kCoreEventClass , kAEOpenApplication ,
9b58521d 882 sOAppHandler , 0 , FALSE ) ;
e128397f 883 AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments ,
9b58521d 884 sPDocHandler , 0 , FALSE ) ;
1c32ded3 885 AEInstallEventHandler( kCoreEventClass , kAEReopenApplication ,
9b58521d 886 sRAppHandler , 0 , FALSE ) ;
e128397f 887 AEInstallEventHandler( kCoreEventClass , kAEQuitApplication ,
9b58521d 888 sQuitHandler , 0 , FALSE ) ;
e128397f 889 }
15b41e90 890
6239ee05
SC
891 if ( !wxMacInitCocoa() )
892 return false;
893
902725ee 894 return true ;
15b41e90
SC
895}
896
e9576ca5
SC
897void wxApp::CleanUp()
898{
e3e34b26 899#if wxUSE_TOOLTIPS
12cd5f34 900 wxToolTip::RemoveToolTips() ;
e3e34b26 901#endif
2f1ae414 902
95e568ee 903 if (m_macEventPosted)
7113b61a
SC
904 {
905 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), m_macEventPosted, kCFRunLoopCommonModes);
906 m_macEventPosted = NULL;
907 }
95e568ee 908
2f1ae414
SC
909 // One last chance for pending objects to be cleaned up
910 wxTheApp->DeletePendingObjects();
911
8461e4c2 912 UMACleanupToolbox() ;
84c1ffa9 913
9b58521d 914 if (!sm_isEmbedded)
9b58521d 915 RemoveEventHandler( (EventHandlerRef)(wxTheApp->m_macEventHandler) );
902725ee 916
9b58521d
SC
917 if (!sm_isEmbedded)
918 {
919 AERemoveEventHandler( kCoreEventClass , kAEOpenDocuments ,
920 sODocHandler , FALSE ) ;
947848a1
VZ
921 AERemoveEventHandler( kInternetEventClass, kAEGetURL,
922 sGURLHandler , FALSE ) ;
9b58521d
SC
923 AERemoveEventHandler( kCoreEventClass , kAEOpenApplication ,
924 sOAppHandler , FALSE ) ;
925 AERemoveEventHandler( kCoreEventClass , kAEPrintDocuments ,
926 sPDocHandler , FALSE ) ;
927 AERemoveEventHandler( kCoreEventClass , kAEReopenApplication ,
928 sRAppHandler , FALSE ) ;
929 AERemoveEventHandler( kCoreEventClass , kAEQuitApplication ,
930 sQuitHandler , FALSE ) ;
902725ee 931
9b58521d 932 DisposeAEEventHandlerUPP( sODocHandler ) ;
947848a1 933 DisposeAEEventHandlerUPP( sGURLHandler ) ;
9b58521d
SC
934 DisposeAEEventHandlerUPP( sOAppHandler ) ;
935 DisposeAEEventHandlerUPP( sPDocHandler ) ;
936 DisposeAEEventHandlerUPP( sRAppHandler ) ;
937 DisposeAEEventHandlerUPP( sQuitHandler ) ;
938 }
94826170
VZ
939
940 wxAppBase::CleanUp();
e9576ca5
SC
941}
942
92b002a4 943//----------------------------------------------------------------------
05e2b077 944// misc initialization stuff
92b002a4
RD
945//----------------------------------------------------------------------
946
1bd568fa 947#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
b4efa069
SC
948bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec)
949{
b4efa069 950 OSStatus err = noErr ;
8a9858ae
DS
951 bool converted = ConvertEventRefToEventRecord( event, rec) ;
952
b4efa069
SC
953 if ( !converted )
954 {
8a9858ae 955 switch ( GetEventClass( event ) )
b4efa069
SC
956 {
957 case kEventClassKeyboard :
958 {
959 converted = true ;
8a9858ae 960 switch ( GetEventKind(event) )
b4efa069
SC
961 {
962 case kEventRawKeyDown :
963 rec->what = keyDown ;
964 break ;
8a9858ae 965
b4efa069
SC
966 case kEventRawKeyRepeat :
967 rec->what = autoKey ;
968 break ;
8a9858ae 969
b4efa069
SC
970 case kEventRawKeyUp :
971 rec->what = keyUp ;
972 break ;
8a9858ae 973
b4efa069
SC
974 case kEventRawKeyModifiersChanged :
975 rec->what = nullEvent ;
976 break ;
8a9858ae 977
b4efa069
SC
978 default :
979 converted = false ;
980 break ;
981 }
8a9858ae 982
b4efa069
SC
983 if ( converted )
984 {
985 UInt32 keyCode ;
986 unsigned char charCode ;
987 UInt32 modifiers ;
988 GetMouse( &rec->where) ;
b4efa069
SC
989 err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
990 err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
991 err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
992 rec->modifiers = modifiers ;
993 rec->message = (keyCode << 8 ) + charCode ;
994 }
995 }
996 break ;
8a9858ae 997
b4efa069
SC
998 case kEventClassTextInput :
999 {
8a9858ae 1000 switch ( GetEventKind( event ) )
b4efa069
SC
1001 {
1002 case kEventTextInputUnicodeForKeyEvent :
1003 {
1004 EventRef rawEvent ;
8a9858ae
DS
1005 err = GetEventParameter(
1006 event, kEventParamTextInputSendKeyboardEvent, typeEventRef, NULL,
1007 sizeof(rawEvent), NULL, &rawEvent ) ;
b4efa069 1008 converted = true ;
8a9858ae 1009
b4efa069 1010 {
8a9858ae 1011 UInt32 keyCode, modifiers;
b4efa069 1012 unsigned char charCode ;
b4efa069
SC
1013 GetMouse( &rec->where) ;
1014 rec->what = keyDown ;
1015 err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers);
1016 err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode);
1017 err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode);
1018 rec->modifiers = modifiers ;
1019 rec->message = (keyCode << 8 ) + charCode ;
1020 }
1021 }
8a9858ae
DS
1022 break ;
1023
b4efa069
SC
1024 default :
1025 break ;
1026 }
1027 }
1028 break ;
8a9858ae
DS
1029
1030 default :
1031 break ;
b4efa069
SC
1032 }
1033 }
84c1ffa9 1034
b4efa069
SC
1035 return converted ;
1036}
1bd568fa 1037#endif
b4efa069 1038
e9576ca5
SC
1039wxApp::wxApp()
1040{
ec8565f3 1041 m_printMode = wxPRINT_WINDOWS;
1ea39a03 1042
ec8565f3
KH
1043 m_macCurrentEvent = NULL ;
1044 m_macCurrentEventHandlerCallRef = NULL ;
ec8565f3 1045 m_macEventPosted = NULL ;
e9576ca5
SC
1046}
1047
89954433 1048void wxApp::OnIdle(wxIdleEvent& WXUNUSED(event))
e9576ca5 1049{
6239ee05
SC
1050 // If they are pending events, we must process them: pending events are
1051 // either events to the threads other than main or events posted with
1052 // wxPostEvent() functions
6239ee05 1053#ifndef __WXUNIVERSAL__
8a9858ae 1054 if (!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar())
3fdac2ab 1055 wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar();
6239ee05 1056#endif
e9576ca5
SC
1057}
1058
e2478fde 1059void wxApp::WakeUpIdle()
9779893b 1060{
95e568ee
KH
1061 if (m_macEventPosted)
1062 {
1063 CFRunLoopSourceSignal(m_macEventPosted);
1064 }
8a9858ae 1065
8461e4c2 1066 wxMacWakeUp() ;
9779893b
RD
1067}
1068
2f1ae414
SC
1069void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
1070{
1071 if (GetTopWindow())
902725ee 1072 GetTopWindow()->Close(true);
2f1ae414
SC
1073}
1074
1075// Default behaviour: close the application with prompts. The
1076// user can veto the close, and therefore the end session.
1077void wxApp::OnQueryEndSession(wxCloseEvent& event)
1078{
1079 if (GetTopWindow())
1080 {
1081 if (!GetTopWindow()->Close(!event.CanVeto()))
902725ee 1082 event.Veto(true);
2f1ae414
SC
1083 }
1084}
1085
1086extern "C" void wxCYield() ;
1087void wxCYield()
1088{
8461e4c2 1089 wxYield() ;
2f1ae414
SC
1090}
1091
e9576ca5 1092// Yield to other processes
cb2713bf 1093
8461e4c2 1094bool wxApp::Yield(bool onlyIfNeeded)
e9576ca5 1095{
8461e4c2
VZ
1096 if (s_inYield)
1097 {
1098 if ( !onlyIfNeeded )
1099 {
1100 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1101 }
1102
902725ee 1103 return false;
8461e4c2
VZ
1104 }
1105
b3cbba47
RD
1106#if wxUSE_THREADS
1107 // Yielding from a non-gui thread needs to bail out, otherwise we end up
1108 // possibly sending events in the thread too.
1109 if ( !wxThread::IsMain() )
1110 {
1111 return true;
1112 }
1113#endif // wxUSE_THREADS
1114
902725ee 1115 s_inYield = true;
cb2713bf 1116
e40298d5 1117 // by definition yield should handle all non-processed events
facd6764 1118
e40298d5
JS
1119 EventRef theEvent;
1120
1121 OSStatus status = noErr ;
8a9858ae
DS
1122
1123 while ( status == noErr )
e40298d5
JS
1124 {
1125 s_inReceiveEvent = true ;
1126 status = ReceiveNextEvent(0, NULL,kEventDurationNoWait,true,&theEvent) ;
1127 s_inReceiveEvent = false ;
7c9955d1 1128
e40298d5
JS
1129 if ( status == eventLoopTimedOutErr )
1130 {
1131 // make sure next time the event loop will trigger idle events
1132 sleepTime = kEventDurationNoWait ;
1133 }
1134 else if ( status == eventLoopQuitErr )
1135 {
1136 // according to QA1061 this may also occur when a WakeUp Process
1137 // is executed
1138 }
1139 else
1140 {
1141 MacHandleOneEvent( theEvent ) ;
1142 ReleaseEvent(theEvent);
1143 }
8a9858ae 1144 }
2f1ae414 1145
902725ee 1146 s_inYield = false;
cb2713bf 1147
902725ee 1148 return true;
169935ad
SC
1149}
1150
9779893b 1151void wxApp::MacDoOneEvent()
169935ad 1152{
6239ee05 1153 wxMacAutoreleasePool autoreleasepool;
e40298d5 1154 EventRef theEvent;
c5c9378c 1155
e40298d5 1156 s_inReceiveEvent = true ;
8a9858ae 1157 OSStatus status = ReceiveNextEvent(0, NULL, sleepTime, true, &theEvent) ;
e40298d5 1158 s_inReceiveEvent = false ;
8a9858ae
DS
1159
1160 switch (status)
e40298d5 1161 {
f3078f07 1162 case eventLoopTimedOutErr :
8a9858ae
DS
1163 if ( wxTheApp->ProcessIdle() )
1164 sleepTime = kEventDurationNoWait ;
1165 else
1166 sleepTime = kEventDurationSecond;
1167 break;
1168
f3078f07 1169 case eventLoopQuitErr :
8a9858ae
DS
1170 // according to QA1061 this may also occur
1171 // when a WakeUp Process is executed
1172 break;
1173
1174 default:
1175 MacHandleOneEvent( theEvent ) ;
1176 ReleaseEvent( theEvent );
e40298d5 1177 sleepTime = kEventDurationNoWait ;
8a9858ae 1178 break;
e40298d5 1179 }
8461e4c2 1180 // repeaters
519cb848 1181
6264b550 1182 DeletePendingObjects() ;
169935ad
SC
1183}
1184
8a9858ae 1185// virtual
89954433 1186void wxApp::MacHandleUnhandledEvent( WXEVENTREF WXUNUSED(evr) )
e128397f 1187{
902725ee 1188 // Override to process unhandled events as you please
e128397f
DS
1189}
1190
96116866
SC
1191CFMutableArrayRef GetAutoReleaseArray()
1192{
1193 static CFMutableArrayRef array = 0;
1194 if ( array == 0)
1195 array= CFArrayCreateMutable(kCFAllocatorDefault,0,&kCFTypeArrayCallBacks);
1196 return array;
1197}
1198
76a5e5d2 1199void wxApp::MacHandleOneEvent( WXEVENTREF evr )
169935ad 1200{
e40298d5
JS
1201 EventTargetRef theTarget;
1202 theTarget = GetEventDispatcherTarget();
c5c9378c 1203 m_macCurrentEvent = evr ;
8a9858ae
DS
1204
1205 OSStatus status = SendEventToEventTarget((EventRef) evr , theTarget);
1206 if (status == eventNotHandledErr)
e128397f 1207 MacHandleUnhandledEvent(evr);
8a9858ae 1208
f8df60f2
SC
1209#if wxUSE_THREADS
1210 wxMutexGuiLeaveOrEnter();
1211#endif // wxUSE_THREADS
96116866
SC
1212
1213 CFArrayRemoveAllValues( GetAutoReleaseArray() );
1214}
1215
1216void wxApp::MacAddToAutorelease( void* cfrefobj )
1217{
1218 CFArrayAppendValue( GetAutoReleaseArray(), cfrefobj );
169935ad
SC
1219}
1220
7c551d95 1221long wxMacTranslateKey(unsigned char key, unsigned char code)
9779893b 1222{
8461e4c2 1223 long retval = key ;
9779893b 1224 switch (key)
519cb848 1225 {
12e049f6 1226 case kHomeCharCode :
902725ee
WS
1227 retval = WXK_HOME;
1228 break;
1229
12e049f6 1230 case kEnterCharCode :
902725ee
WS
1231 retval = WXK_RETURN;
1232 break;
12e049f6 1233 case kEndCharCode :
902725ee
WS
1234 retval = WXK_END;
1235 break;
1236
12e049f6 1237 case kHelpCharCode :
902725ee
WS
1238 retval = WXK_HELP;
1239 break;
1240
12e049f6 1241 case kBackspaceCharCode :
902725ee
WS
1242 retval = WXK_BACK;
1243 break;
1244
12e049f6 1245 case kTabCharCode :
902725ee
WS
1246 retval = WXK_TAB;
1247 break;
1248
12e049f6 1249 case kPageUpCharCode :
902725ee
WS
1250 retval = WXK_PAGEUP;
1251 break;
1252
12e049f6 1253 case kPageDownCharCode :
902725ee
WS
1254 retval = WXK_PAGEDOWN;
1255 break;
1256
12e049f6 1257 case kReturnCharCode :
902725ee
WS
1258 retval = WXK_RETURN;
1259 break;
1260
1261 case kFunctionKeyCharCode :
1262 {
8a9858ae 1263 switch ( code )
8461e4c2 1264 {
902725ee
WS
1265 case 0x7a :
1266 retval = WXK_F1 ;
1267 break;
8a9858ae 1268
902725ee
WS
1269 case 0x78 :
1270 retval = WXK_F2 ;
1271 break;
8a9858ae 1272
902725ee
WS
1273 case 0x63 :
1274 retval = WXK_F3 ;
1275 break;
8a9858ae 1276
902725ee
WS
1277 case 0x76 :
1278 retval = WXK_F4 ;
1279 break;
8a9858ae 1280
902725ee
WS
1281 case 0x60 :
1282 retval = WXK_F5 ;
1283 break;
8a9858ae 1284
902725ee
WS
1285 case 0x61 :
1286 retval = WXK_F6 ;
1287 break;
8a9858ae 1288
902725ee
WS
1289 case 0x62:
1290 retval = WXK_F7 ;
1291 break;
8a9858ae 1292
902725ee
WS
1293 case 0x64 :
1294 retval = WXK_F8 ;
1295 break;
8a9858ae 1296
902725ee
WS
1297 case 0x65 :
1298 retval = WXK_F9 ;
1299 break;
8a9858ae 1300
902725ee
WS
1301 case 0x6D :
1302 retval = WXK_F10 ;
1303 break;
8a9858ae 1304
902725ee
WS
1305 case 0x67 :
1306 retval = WXK_F11 ;
1307 break;
8a9858ae 1308
902725ee
WS
1309 case 0x6F :
1310 retval = WXK_F12 ;
1311 break;
8a9858ae 1312
902725ee
WS
1313 case 0x69 :
1314 retval = WXK_F13 ;
1315 break;
8a9858ae 1316
902725ee
WS
1317 case 0x6B :
1318 retval = WXK_F14 ;
1319 break;
8a9858ae 1320
902725ee
WS
1321 case 0x71 :
1322 retval = WXK_F15 ;
1323 break;
8a9858ae
DS
1324
1325 default:
1326 break;
8461e4c2 1327 }
902725ee
WS
1328 }
1329 break ;
1330
1331 case kEscapeCharCode :
1332 retval = WXK_ESCAPE ;
8461e4c2 1333 break ;
902725ee
WS
1334
1335 case kLeftArrowCharCode :
1336 retval = WXK_LEFT ;
8461e4c2 1337 break ;
902725ee
WS
1338
1339 case kRightArrowCharCode :
1340 retval = WXK_RIGHT ;
8461e4c2 1341 break ;
902725ee
WS
1342
1343 case kUpArrowCharCode :
1344 retval = WXK_UP ;
8461e4c2 1345 break ;
902725ee
WS
1346
1347 case kDownArrowCharCode :
1348 retval = WXK_DOWN ;
8461e4c2 1349 break ;
902725ee
WS
1350
1351 case kDeleteCharCode :
1352 retval = WXK_DELETE ;
8461e4c2 1353 break ;
902725ee
WS
1354
1355 default:
8461e4c2
VZ
1356 break ;
1357 } // end switch
1358
1359 return retval;
169935ad
SC
1360}
1361
facd6764 1362int wxMacKeyCodeToModifier(wxKeyCode key)
6ed892f3
RN
1363{
1364 switch (key)
1365 {
1366 case WXK_START:
15c2acd2 1367 case WXK_MENU:
6ed892f3
RN
1368 return cmdKey;
1369
1370 case WXK_SHIFT:
1371 return shiftKey;
1372
1373 case WXK_CAPITAL:
1374 return alphaLock;
1375
15c2acd2
RN
1376 case WXK_ALT:
1377 return optionKey;
6ed892f3
RN
1378
1379 case WXK_CONTROL:
1380 return controlKey;
1381
1382 default:
1383 return 0;
1384 }
1385}
1386
7dd40b6f
RD
1387wxMouseState wxGetMouseState()
1388{
1389 wxMouseState ms;
1390
1391 wxPoint pt = wxGetMousePosition();
1392 ms.SetX(pt.x);
1393 ms.SetY(pt.y);
1394
1395 UInt32 buttons = GetCurrentButtonState();
1396 ms.SetLeftDown( (buttons & 0x01) != 0 );
1397 ms.SetMiddleDown( (buttons & 0x04) != 0 );
1398 ms.SetRightDown( (buttons & 0x02) != 0 );
2a2e6365 1399
7dd40b6f
RD
1400 UInt32 modifiers = GetCurrentKeyModifiers();
1401 ms.SetControlDown(modifiers & controlKey);
1402 ms.SetShiftDown(modifiers & shiftKey);
1403 ms.SetAltDown(modifiers & optionKey);
1404 ms.SetMetaDown(modifiers & cmdKey);
1405
1406 return ms;
1407}
1408
980ee83f 1409// TODO : once the new key/char handling is tested, move all the code to wxWindow
7dd40b6f 1410
8a9858ae 1411bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
ac2d1ca6 1412{
ba12463b
SC
1413 if ( !focus )
1414 return false ;
7c9955d1 1415
88a7a4e1 1416 bool handled;
980ee83f
SC
1417 wxKeyEvent event(wxEVT_KEY_DOWN) ;
1418 MacCreateKeyEvent( event, focus , keymessage , modifiers , when , wherex , wherey , uniChar ) ;
88a7a4e1 1419
ac2d1ca6
SC
1420 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1421 if ( handled && event.GetSkipped() )
1422 handled = false ;
902725ee
WS
1423
1424#if wxUSE_ACCEL
ac2d1ca6
SC
1425 if ( !handled )
1426 {
902725ee
WS
1427 wxWindow *ancestor = focus;
1428 while (ancestor)
ac2d1ca6 1429 {
902725ee
WS
1430 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1431 if (command != -1)
ac2d1ca6 1432 {
7816e624
VZ
1433 wxEvtHandler * const handler = ancestor->GetEventHandler();
1434
902725ee 1435 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
7816e624
VZ
1436 handled = handler->ProcessEvent( command_event );
1437
1438 if ( !handled )
1439 {
1440 // accelerators can also be used with buttons, try them too
1441 command_event.SetEventType(wxEVT_COMMAND_BUTTON_CLICKED);
1442 handled = handler->ProcessEvent( command_event );
1443 }
1444
902725ee 1445 break;
8461e4c2 1446 }
8a9858ae 1447
902725ee
WS
1448 if (ancestor->IsTopLevel())
1449 break;
8a9858ae 1450
902725ee 1451 ancestor = ancestor->GetParent();
ac2d1ca6 1452 }
ac2d1ca6 1453 }
902725ee
WS
1454#endif // wxUSE_ACCEL
1455
980ee83f
SC
1456 return handled ;
1457}
e604288b 1458
980ee83f
SC
1459bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1460{
1461 if ( !focus )
1462 return false ;
902725ee 1463
980ee83f
SC
1464 bool handled;
1465 wxKeyEvent event( wxEVT_KEY_UP ) ;
1466 MacCreateKeyEvent( event, focus , keymessage , modifiers , when , wherex , wherey , uniChar ) ;
1467 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
88a7a4e1 1468
980ee83f
SC
1469 return handled ;
1470}
8a9858ae 1471
980ee83f
SC
1472bool wxApp::MacSendCharEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
1473{
1474 if ( !focus )
1475 return false ;
88a7a4e1 1476
980ee83f
SC
1477 wxKeyEvent event(wxEVT_CHAR) ;
1478 MacCreateKeyEvent( event, focus , keymessage , modifiers , when , wherex , wherey , uniChar ) ;
1479 long keyval = event.m_keyCode ;
ac2d1ca6 1480
980ee83f
SC
1481 bool handled = false ;
1482
1483 wxTopLevelWindowMac *tlw = focus->MacGetTopLevelWindow() ;
88a7a4e1 1484
980ee83f
SC
1485 if (tlw)
1486 {
1487 event.SetEventType( wxEVT_CHAR_HOOK );
980ee83f 1488 handled = tlw->GetEventHandler()->ProcessEvent( event );
ac2d1ca6
SC
1489 if ( handled && event.GetSkipped() )
1490 handled = false ;
1491 }
8a9858ae 1492
980ee83f
SC
1493 if ( !handled )
1494 {
1495 event.SetEventType( wxEVT_CHAR );
1496 event.Skip( false ) ;
1497 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1498 }
88a7a4e1 1499
bdf956fb 1500 if ( !handled && (keyval == WXK_TAB) )
ac2d1ca6 1501 {
bdf956fb 1502 wxWindow* iter = focus->GetParent() ;
8a9858ae 1503 while ( iter && !handled )
bdf956fb
SC
1504 {
1505 if ( iter->HasFlag( wxTAB_TRAVERSAL ) )
1506 {
1507 wxNavigationKeyEvent new_event;
1508 new_event.SetEventObject( focus );
1509 new_event.SetDirection( !event.ShiftDown() );
1510 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1511 new_event.SetWindowChange( event.ControlDown() );
1512 new_event.SetCurrentFocus( focus );
1513 handled = focus->GetParent()->GetEventHandler()->ProcessEvent( new_event );
1514 if ( handled && new_event.GetSkipped() )
1515 handled = false ;
1516 }
8a9858ae 1517
bdf956fb
SC
1518 iter = iter->GetParent() ;
1519 }
ac2d1ca6 1520 }
8a9858ae 1521
ac2d1ca6
SC
1522 // backdoor handler for default return and command escape
1523 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
1524 {
8a9858ae
DS
1525 // if window is not having a focus still testing for default enter or cancel
1526 // TODO: add the UMA version for ActiveNonFloatingWindow
1bd568fa 1527#ifndef __LP64__
8a9858ae
DS
1528 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
1529 if ( focus )
1530 {
deda45e8 1531 if ( keyval == WXK_RETURN || keyval == WXK_NUMPAD_ENTER )
e562df9b 1532 {
6c20e8f8
VZ
1533 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(focus), wxTopLevelWindow);
1534 if ( tlw && tlw->GetDefaultItem() )
8a9858ae 1535 {
6c20e8f8
VZ
1536 wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton);
1537 if ( def && def->IsEnabled() )
1538 {
1539 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1540 event.SetEventObject(def);
1541 def->Command(event);
8a9858ae 1542
6c20e8f8
VZ
1543 return true ;
1544 }
8a9858ae 1545 }
e562df9b 1546 }
ac2d1ca6 1547 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
8461e4c2 1548 {
8a9858ae
DS
1549 // generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs)
1550 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1551 new_event.SetEventObject( focus );
1552 handled = focus->GetEventHandler()->ProcessEvent( new_event );
8461e4c2 1553 }
8a9858ae 1554 }
1bd568fa 1555#endif
8461e4c2 1556 }
ac2d1ca6 1557 return handled ;
169935ad
SC
1558}
1559
88a7a4e1 1560// This method handles common code for SendKeyDown, SendKeyUp, and SendChar events.
980ee83f 1561void wxApp::MacCreateKeyEvent( wxKeyEvent& event, wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
ac2d1ca6 1562{
8a9858ae
DS
1563 short keycode, keychar ;
1564
12e049f6
SC
1565 keychar = short(keymessage & charCodeMask);
1566 keycode = short(keymessage & keyCodeMask) >> 8 ;
980ee83f 1567 if ( !(event.GetEventType() == wxEVT_CHAR) && (modifiers & (controlKey | shiftKey | optionKey) ) )
12e049f6
SC
1568 {
1569 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1570 // and look at the character after
6239ee05
SC
1571#ifdef __LP64__
1572 // TODO new implementation using TextInputSources
1573#else
12e049f6 1574 UInt32 state = 0;
8a9858ae 1575 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey | shiftKey | optionKey))) | keycode, &state);
12e049f6 1576 keychar = short(keyInfo & charCodeMask);
6239ee05 1577#endif
12e049f6 1578 }
12e049f6 1579
8a9858ae 1580 long keyval = wxMacTranslateKey(keychar, keycode) ;
980ee83f 1581 if ( keyval == keychar && ( event.GetEventType() == wxEVT_KEY_UP || event.GetEventType() == wxEVT_KEY_DOWN ) )
7c9955d1 1582 keyval = wxToupper( keyval ) ;
84ec90e9 1583
6239ee05
SC
1584 // Check for NUMPAD keys. For KEY_UP/DOWN events we need to use the
1585 // WXK_NUMPAD constants, but for the CHAR event we want to use the
1586 // standard ascii values
1587 if ( event.GetEventType() != wxEVT_CHAR )
84ec90e9 1588 {
6239ee05 1589 if (keyval >= '0' && keyval <= '9' && keycode >= 82 && keycode <= 92)
84ec90e9 1590 {
6239ee05
SC
1591 keyval = (keyval - '0') + WXK_NUMPAD0;
1592 }
1593 else if (keycode >= 65 && keycode <= 81)
1594 {
1595 switch (keycode)
1596 {
1597 case 76 :
1598 keyval = WXK_NUMPAD_ENTER;
1599 break;
1600
1601 case 81:
1602 keyval = WXK_NUMPAD_EQUAL;
1603 break;
1604
1605 case 67:
1606 keyval = WXK_NUMPAD_MULTIPLY;
1607 break;
1608
1609 case 75:
1610 keyval = WXK_NUMPAD_DIVIDE;
1611 break;
1612
1613 case 78:
1614 keyval = WXK_NUMPAD_SUBTRACT;
1615 break;
1616
1617 case 69:
1618 keyval = WXK_NUMPAD_ADD;
1619 break;
1620
1621 case 65:
1622 keyval = WXK_NUMPAD_DECIMAL;
1623 break;
1624 default:
1625 break;
1626 }
1627 }
84ec90e9 1628 }
6239ee05 1629
ba12463b
SC
1630 event.m_shiftDown = modifiers & shiftKey;
1631 event.m_controlDown = modifiers & controlKey;
1632 event.m_altDown = modifiers & optionKey;
1633 event.m_metaDown = modifiers & cmdKey;
ef08713a 1634 event.m_keyCode = keyval ;
2d17efa9
SC
1635#if wxUSE_UNICODE
1636 event.m_uniChar = uniChar ;
1637#endif
ba12463b 1638
84ec90e9
JS
1639 event.m_rawCode = keymessage;
1640 event.m_rawFlags = modifiers;
ba12463b
SC
1641 event.m_x = wherex;
1642 event.m_y = wherey;
687706f5 1643 event.SetTimestamp(when);
ba12463b 1644 event.SetEventObject(focus);
ac2d1ca6 1645}