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