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