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