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