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