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