]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/app.cpp
key/char event changes for inline editing (japanese/hirgana etc)
[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
72055702 1257long wxMacTranslateKey(unsigned char key, unsigned char code) ;
7c551d95 1258long wxMacTranslateKey(unsigned char key, unsigned char code)
9779893b 1259{
8461e4c2 1260 long retval = key ;
9779893b 1261 switch (key)
519cb848 1262 {
12e049f6 1263 case kHomeCharCode :
902725ee
WS
1264 retval = WXK_HOME;
1265 break;
1266
12e049f6 1267 case kEnterCharCode :
902725ee
WS
1268 retval = WXK_RETURN;
1269 break;
12e049f6 1270 case kEndCharCode :
902725ee
WS
1271 retval = WXK_END;
1272 break;
1273
12e049f6 1274 case kHelpCharCode :
902725ee
WS
1275 retval = WXK_HELP;
1276 break;
1277
12e049f6 1278 case kBackspaceCharCode :
902725ee
WS
1279 retval = WXK_BACK;
1280 break;
1281
12e049f6 1282 case kTabCharCode :
902725ee
WS
1283 retval = WXK_TAB;
1284 break;
1285
12e049f6 1286 case kPageUpCharCode :
902725ee
WS
1287 retval = WXK_PAGEUP;
1288 break;
1289
12e049f6 1290 case kPageDownCharCode :
902725ee
WS
1291 retval = WXK_PAGEDOWN;
1292 break;
1293
12e049f6 1294 case kReturnCharCode :
902725ee
WS
1295 retval = WXK_RETURN;
1296 break;
1297
1298 case kFunctionKeyCharCode :
1299 {
8a9858ae 1300 switch ( code )
8461e4c2 1301 {
902725ee
WS
1302 case 0x7a :
1303 retval = WXK_F1 ;
1304 break;
8a9858ae 1305
902725ee
WS
1306 case 0x78 :
1307 retval = WXK_F2 ;
1308 break;
8a9858ae 1309
902725ee
WS
1310 case 0x63 :
1311 retval = WXK_F3 ;
1312 break;
8a9858ae 1313
902725ee
WS
1314 case 0x76 :
1315 retval = WXK_F4 ;
1316 break;
8a9858ae 1317
902725ee
WS
1318 case 0x60 :
1319 retval = WXK_F5 ;
1320 break;
8a9858ae 1321
902725ee
WS
1322 case 0x61 :
1323 retval = WXK_F6 ;
1324 break;
8a9858ae 1325
902725ee
WS
1326 case 0x62:
1327 retval = WXK_F7 ;
1328 break;
8a9858ae 1329
902725ee
WS
1330 case 0x64 :
1331 retval = WXK_F8 ;
1332 break;
8a9858ae 1333
902725ee
WS
1334 case 0x65 :
1335 retval = WXK_F9 ;
1336 break;
8a9858ae 1337
902725ee
WS
1338 case 0x6D :
1339 retval = WXK_F10 ;
1340 break;
8a9858ae 1341
902725ee
WS
1342 case 0x67 :
1343 retval = WXK_F11 ;
1344 break;
8a9858ae 1345
902725ee
WS
1346 case 0x6F :
1347 retval = WXK_F12 ;
1348 break;
8a9858ae 1349
902725ee
WS
1350 case 0x69 :
1351 retval = WXK_F13 ;
1352 break;
8a9858ae 1353
902725ee
WS
1354 case 0x6B :
1355 retval = WXK_F14 ;
1356 break;
8a9858ae 1357
902725ee
WS
1358 case 0x71 :
1359 retval = WXK_F15 ;
1360 break;
8a9858ae
DS
1361
1362 default:
1363 break;
8461e4c2 1364 }
902725ee
WS
1365 }
1366 break ;
1367
1368 case kEscapeCharCode :
1369 retval = WXK_ESCAPE ;
8461e4c2 1370 break ;
902725ee
WS
1371
1372 case kLeftArrowCharCode :
1373 retval = WXK_LEFT ;
8461e4c2 1374 break ;
902725ee
WS
1375
1376 case kRightArrowCharCode :
1377 retval = WXK_RIGHT ;
8461e4c2 1378 break ;
902725ee
WS
1379
1380 case kUpArrowCharCode :
1381 retval = WXK_UP ;
8461e4c2 1382 break ;
902725ee
WS
1383
1384 case kDownArrowCharCode :
1385 retval = WXK_DOWN ;
8461e4c2 1386 break ;
902725ee
WS
1387
1388 case kDeleteCharCode :
1389 retval = WXK_DELETE ;
8461e4c2 1390 break ;
902725ee
WS
1391
1392 default:
8461e4c2
VZ
1393 break ;
1394 } // end switch
1395
1396 return retval;
169935ad
SC
1397}
1398
facd6764 1399int wxMacKeyCodeToModifier(wxKeyCode key)
6ed892f3
RN
1400{
1401 switch (key)
1402 {
1403 case WXK_START:
15c2acd2 1404 case WXK_MENU:
6ed892f3
RN
1405 return cmdKey;
1406
1407 case WXK_SHIFT:
1408 return shiftKey;
1409
1410 case WXK_CAPITAL:
1411 return alphaLock;
1412
15c2acd2
RN
1413 case WXK_ALT:
1414 return optionKey;
6ed892f3
RN
1415
1416 case WXK_CONTROL:
1417 return controlKey;
1418
1419 default:
1420 return 0;
1421 }
1422}
1423
4cb1d3da 1424#ifndef __DARWIN__
1751226c 1425bool wxGetKeyState(wxKeyCode key) //virtual key code if < 10.2.x, else see below
6ed892f3 1426{
44353523
VZ
1427 wxASSERT_MSG(key != WXK_LBUTTON && key != WXK_RBUTTON && key !=
1428 WXK_MBUTTON, wxT("can't use wxGetKeyState() for mouse buttons"));
1429
6ed892f3
RN
1430//if OS X > 10.2 (i.e. 10.2.x)
1431//a known apple bug prevents the system from determining led
1432//states with GetKeys... can only determine caps lock led
902725ee 1433 return !!(GetCurrentKeyModifiers() & wxMacKeyCodeToModifier(key));
6ed892f3 1434//else
902725ee 1435// KeyMapByteArray keymap;
6ed892f3
RN
1436// GetKeys((BigEndianLong*)keymap);
1437// return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey));
1438}
4cb1d3da 1439#endif
6ed892f3 1440
c5c9378c 1441
7dd40b6f
RD
1442wxMouseState wxGetMouseState()
1443{
1444 wxMouseState ms;
1445
1446 wxPoint pt = wxGetMousePosition();
1447 ms.SetX(pt.x);
1448 ms.SetY(pt.y);
1449
1450 UInt32 buttons = GetCurrentButtonState();
1451 ms.SetLeftDown( (buttons & 0x01) != 0 );
1452 ms.SetMiddleDown( (buttons & 0x04) != 0 );
1453 ms.SetRightDown( (buttons & 0x02) != 0 );
1454
1455 UInt32 modifiers = GetCurrentKeyModifiers();
1456 ms.SetControlDown(modifiers & controlKey);
1457 ms.SetShiftDown(modifiers & shiftKey);
1458 ms.SetAltDown(modifiers & optionKey);
1459 ms.SetMetaDown(modifiers & cmdKey);
1460
1461 return ms;
1462}
1463
1464
8a9858ae 1465bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
ac2d1ca6 1466{
ba12463b
SC
1467 if ( !focus )
1468 return false ;
7c9955d1 1469
12e049f6
SC
1470 short keycode ;
1471 short keychar ;
1472 keychar = short(keymessage & charCodeMask);
1473 keycode = short(keymessage & keyCodeMask) >> 8 ;
7c9955d1 1474
ef08713a 1475 if ( modifiers & ( controlKey|shiftKey|optionKey ) )
12e049f6
SC
1476 {
1477 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1478 // and look at the character after
1479 UInt32 state = 0;
ef08713a 1480 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state);
12e049f6 1481 keychar = short(keyInfo & charCodeMask);
12e049f6 1482 }
8a9858ae 1483
12e049f6 1484 long keyval = wxMacTranslateKey(keychar, keycode) ;
e40298d5
JS
1485 long realkeyval = keyval ;
1486 if ( keyval == keychar )
1487 {
1488 // we are not on a special character combo -> pass the real os event-value to EVT_CHAR, but not to EVT_KEY (make upper first)
1489 realkeyval = short(keymessage & charCodeMask) ;
1490 keyval = wxToupper( keyval ) ;
1491 }
7c9955d1 1492
84ec90e9
JS
1493 // Check for NUMPAD keys
1494 if (keyval >= '0' && keyval <= '9' && keycode >= 82 && keycode <= 92)
1495 {
1496 keyval = keyval - '0' + WXK_NUMPAD0;
1497 }
1498 else if (keycode >= 67 && keycode <= 81)
1499 {
1500 switch (keycode)
1501 {
1502 case 76 :
1503 keyval = WXK_NUMPAD_ENTER;
1504 break;
8a9858ae 1505
84ec90e9
JS
1506 case 81:
1507 keyval = WXK_NUMPAD_EQUAL;
1508 break;
8a9858ae 1509
84ec90e9
JS
1510 case 67:
1511 keyval = WXK_NUMPAD_MULTIPLY;
1512 break;
8a9858ae 1513
84ec90e9
JS
1514 case 75:
1515 keyval = WXK_NUMPAD_DIVIDE;
1516 break;
8a9858ae 1517
84ec90e9
JS
1518 case 78:
1519 keyval = WXK_NUMPAD_SUBTRACT;
1520 break;
8a9858ae 1521
84ec90e9
JS
1522 case 69:
1523 keyval = WXK_NUMPAD_ADD;
1524 break;
8a9858ae 1525
84ec90e9
JS
1526 case 65:
1527 keyval = WXK_NUMPAD_DECIMAL;
1528 break;
8a9858ae
DS
1529
1530 default:
1531 break;
84ec90e9
JS
1532 } // end switch
1533 }
1534
ac2d1ca6 1535 wxKeyEvent event(wxEVT_KEY_DOWN);
41d368a4 1536 bool handled = false ;
ac2d1ca6
SC
1537 event.m_shiftDown = modifiers & shiftKey;
1538 event.m_controlDown = modifiers & controlKey;
1539 event.m_altDown = modifiers & optionKey;
1540 event.m_metaDown = modifiers & cmdKey;
ef08713a 1541 event.m_keyCode = keyval ;
8a9858ae 1542
2d17efa9
SC
1543#if wxUSE_UNICODE
1544 event.m_uniChar = uniChar ;
1545#endif
8a9858ae 1546
84ec90e9
JS
1547 event.m_rawCode = keymessage;
1548 event.m_rawFlags = modifiers;
ac2d1ca6
SC
1549 event.m_x = wherex;
1550 event.m_y = wherey;
687706f5 1551 event.SetTimestamp(when);
ac2d1ca6
SC
1552 event.SetEventObject(focus);
1553 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1554 if ( handled && event.GetSkipped() )
1555 handled = false ;
902725ee
WS
1556
1557#if wxUSE_ACCEL
ac2d1ca6
SC
1558 if ( !handled )
1559 {
902725ee
WS
1560 wxWindow *ancestor = focus;
1561 while (ancestor)
ac2d1ca6 1562 {
902725ee
WS
1563 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1564 if (command != -1)
ac2d1ca6 1565 {
902725ee
WS
1566 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1567 handled = ancestor->GetEventHandler()->ProcessEvent( command_event );
1568 break;
8461e4c2 1569 }
8a9858ae 1570
902725ee
WS
1571 if (ancestor->IsTopLevel())
1572 break;
8a9858ae 1573
902725ee 1574 ancestor = ancestor->GetParent();
ac2d1ca6 1575 }
ac2d1ca6 1576 }
902725ee
WS
1577#endif // wxUSE_ACCEL
1578
ac2d1ca6
SC
1579 if (!handled)
1580 {
e604288b
SC
1581 wxTopLevelWindowMac *tlw = focus->MacGetTopLevelWindow() ;
1582
1583 if (tlw)
1584 {
902725ee 1585 event.Skip( false ) ;
e604288b
SC
1586 event.SetEventType( wxEVT_CHAR_HOOK );
1587 // raw value again
1588 event.m_keyCode = realkeyval ;
1589
1590 handled = tlw->GetEventHandler()->ProcessEvent( event );
1591 if ( handled && event.GetSkipped() )
1592 handled = false ;
1593 }
1594 }
902725ee 1595
e604288b 1596 if ( !handled )
902725ee
WS
1597 {
1598 event.Skip( false ) ;
ac2d1ca6 1599 event.SetEventType( wxEVT_CHAR ) ;
8a9858ae 1600
12e049f6 1601 // raw value again
ef08713a 1602 event.m_keyCode = realkeyval ;
ac2d1ca6
SC
1603
1604 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1605 if ( handled && event.GetSkipped() )
1606 handled = false ;
1607 }
8a9858ae 1608
bdf956fb 1609 if ( !handled && (keyval == WXK_TAB) )
ac2d1ca6 1610 {
bdf956fb 1611 wxWindow* iter = focus->GetParent() ;
8a9858ae 1612 while ( iter && !handled )
bdf956fb
SC
1613 {
1614 if ( iter->HasFlag( wxTAB_TRAVERSAL ) )
1615 {
1616 wxNavigationKeyEvent new_event;
1617 new_event.SetEventObject( focus );
1618 new_event.SetDirection( !event.ShiftDown() );
1619 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
1620 new_event.SetWindowChange( event.ControlDown() );
1621 new_event.SetCurrentFocus( focus );
1622 handled = focus->GetParent()->GetEventHandler()->ProcessEvent( new_event );
1623 if ( handled && new_event.GetSkipped() )
1624 handled = false ;
1625 }
8a9858ae 1626
bdf956fb
SC
1627 iter = iter->GetParent() ;
1628 }
ac2d1ca6 1629 }
8a9858ae 1630
ac2d1ca6
SC
1631 // backdoor handler for default return and command escape
1632 if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) )
1633 {
8a9858ae
DS
1634 // if window is not having a focus still testing for default enter or cancel
1635 // TODO: add the UMA version for ActiveNonFloatingWindow
1636 wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ;
1637 if ( focus )
1638 {
e562df9b
SC
1639 if ( keyval == WXK_RETURN )
1640 {
8a9858ae
DS
1641 wxButton *def = wxDynamicCast(focus->GetDefaultItem(), wxButton);
1642 if ( def && def->IsEnabled() )
1643 {
1644 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
1645 event.SetEventObject(def);
1646 def->Command(event);
1647
1648 return true ;
1649 }
e562df9b 1650 }
ac2d1ca6 1651 else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) )
8461e4c2 1652 {
8a9858ae
DS
1653 // generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs)
1654 wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL);
1655 new_event.SetEventObject( focus );
1656 handled = focus->GetEventHandler()->ProcessEvent( new_event );
8461e4c2 1657 }
8a9858ae 1658 }
8461e4c2 1659 }
8a9858ae 1660
ac2d1ca6 1661 return handled ;
169935ad
SC
1662}
1663
8a9858ae 1664bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey , wxChar uniChar )
ac2d1ca6 1665{
ba12463b
SC
1666 if ( !focus )
1667 return false ;
1668
8a9858ae
DS
1669 short keycode, keychar ;
1670
12e049f6
SC
1671 keychar = short(keymessage & charCodeMask);
1672 keycode = short(keymessage & keyCodeMask) >> 8 ;
8a9858ae 1673 if ( modifiers & (controlKey | shiftKey | optionKey) )
12e049f6
SC
1674 {
1675 // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier
1676 // and look at the character after
1677 UInt32 state = 0;
8a9858ae 1678 UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey | shiftKey | optionKey))) | keycode, &state);
12e049f6 1679 keychar = short(keyInfo & charCodeMask);
12e049f6 1680 }
12e049f6 1681
8a9858ae 1682 long keyval = wxMacTranslateKey(keychar, keycode) ;
e40298d5 1683 if ( keyval == keychar )
7c9955d1 1684 keyval = wxToupper( keyval ) ;
84ec90e9
JS
1685
1686 // Check for NUMPAD keys
1687 if (keyval >= '0' && keyval <= '9' && keycode >= 82 && keycode <= 92)
1688 {
8a9858ae 1689 keyval = (keyval - '0') + WXK_NUMPAD0;
84ec90e9
JS
1690 }
1691 else if (keycode >= 67 && keycode <= 81)
1692 {
1693 switch (keycode)
1694 {
1695 case 76 :
1696 keyval = WXK_NUMPAD_ENTER;
1697 break;
8a9858ae 1698
84ec90e9
JS
1699 case 81:
1700 keyval = WXK_NUMPAD_EQUAL;
1701 break;
8a9858ae 1702
84ec90e9
JS
1703 case 67:
1704 keyval = WXK_NUMPAD_MULTIPLY;
1705 break;
8a9858ae 1706
84ec90e9
JS
1707 case 75:
1708 keyval = WXK_NUMPAD_DIVIDE;
1709 break;
8a9858ae 1710
84ec90e9
JS
1711 case 78:
1712 keyval = WXK_NUMPAD_SUBTRACT;
1713 break;
8a9858ae 1714
84ec90e9
JS
1715 case 69:
1716 keyval = WXK_NUMPAD_ADD;
1717 break;
8a9858ae 1718
84ec90e9
JS
1719 case 65:
1720 keyval = WXK_NUMPAD_DECIMAL;
1721 break;
8a9858ae
DS
1722
1723 default:
1724 break;
84ec90e9
JS
1725 } // end switch
1726 }
1727
ac2d1ca6 1728 bool handled = false ;
ac2d1ca6 1729
ba12463b
SC
1730 wxKeyEvent event(wxEVT_KEY_UP);
1731 event.m_shiftDown = modifiers & shiftKey;
1732 event.m_controlDown = modifiers & controlKey;
1733 event.m_altDown = modifiers & optionKey;
1734 event.m_metaDown = modifiers & cmdKey;
ef08713a 1735 event.m_keyCode = keyval ;
2d17efa9
SC
1736#if wxUSE_UNICODE
1737 event.m_uniChar = uniChar ;
1738#endif
ba12463b 1739
84ec90e9
JS
1740 event.m_rawCode = keymessage;
1741 event.m_rawFlags = modifiers;
ba12463b
SC
1742 event.m_x = wherex;
1743 event.m_y = wherey;
687706f5 1744 event.SetTimestamp(when);
ba12463b
SC
1745 event.SetEventObject(focus);
1746 handled = focus->GetEventHandler()->ProcessEvent( event ) ;
1747
ac2d1ca6
SC
1748 return handled ;
1749}