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