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