]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: app.cpp | |
3 | // Purpose: wxApp | |
a31a5f85 | 4 | // Author: Stefan Csomor |
e9576ca5 | 5 | // Modified by: |
a31a5f85 | 6 | // Created: 1998-01-01 |
e9576ca5 | 7 | // RCS-ID: $Id$ |
a31a5f85 | 8 | // Copyright: (c) Stefan Csomor |
8461e4c2 | 9 | // Licence: wxWindows licence |
e9576ca5 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "app.h" | |
14 | #endif | |
15 | ||
df06d1a4 GD |
16 | #include "wx/defs.h" |
17 | ||
03e11df5 | 18 | #include "wx/window.h" |
e9576ca5 | 19 | #include "wx/frame.h" |
e8566d35 | 20 | #include "wx/button.h" |
e9576ca5 SC |
21 | #include "wx/app.h" |
22 | #include "wx/utils.h" | |
23 | #include "wx/gdicmn.h" | |
24 | #include "wx/pen.h" | |
25 | #include "wx/brush.h" | |
26 | #include "wx/cursor.h" | |
ed5b9811 | 27 | #include "wx/intl.h" |
e9576ca5 SC |
28 | #include "wx/icon.h" |
29 | #include "wx/palette.h" | |
30 | #include "wx/dc.h" | |
31 | #include "wx/dialog.h" | |
32 | #include "wx/msgdlg.h" | |
33 | #include "wx/log.h" | |
34 | #include "wx/module.h" | |
35 | #include "wx/memory.h" | |
2f1ae414 | 36 | #include "wx/tooltip.h" |
b3c406a5 | 37 | #include "wx/textctrl.h" |
03e11df5 | 38 | #include "wx/menu.h" |
738a6daa | 39 | #include "wx/docview.h" |
1c32ded3 | 40 | #include "wx/filename.h" |
738a6daa | 41 | |
e9576ca5 SC |
42 | #include <string.h> |
43 | ||
8be97d65 SC |
44 | // mac |
45 | ||
f5c6eb5c | 46 | #ifndef __DARWIN__ |
03e11df5 | 47 | #if __option(profile) |
8461e4c2 | 48 | #include <profiler.h> |
03e11df5 | 49 | #endif |
9779893b | 50 | #endif |
519cb848 | 51 | |
8be97d65 SC |
52 | #include "apprsrc.h" |
53 | ||
03e11df5 GD |
54 | #include "wx/mac/uma.h" |
55 | #include "wx/mac/macnotfy.h" | |
2f1ae414 | 56 | |
df06d1a4 GD |
57 | #ifdef __DARWIN__ |
58 | # include <CoreServices/CoreServices.h> | |
21870a5d | 59 | # if defined(WXMAKINGDLL_CORE) |
ed581ee2 GD |
60 | # include <mach-o/dyld.h> |
61 | # endif | |
df06d1a4 GD |
62 | #else |
63 | # include <Sound.h> | |
64 | # include <Threads.h> | |
65 | # include <ToolUtils.h> | |
66 | # include <DiskInit.h> | |
67 | # include <Devices.h> | |
2f1ae414 | 68 | #endif |
519cb848 | 69 | |
e9576ca5 | 70 | extern wxList wxPendingDelete; |
519cb848 SC |
71 | extern wxList *wxWinMacWindowList; |
72 | extern wxList *wxWinMacControlList; | |
c944ce35 | 73 | #if wxUSE_THREADS |
e64313ba | 74 | extern size_t g_numberOfThreads; |
c944ce35 | 75 | #endif // wxUSE_THREADS |
e9576ca5 | 76 | |
c5c9378c SC |
77 | // statics for implementation |
78 | ||
738a6daa | 79 | static bool s_inYield = FALSE; |
c5c9378c SC |
80 | |
81 | #if TARGET_CARBON | |
82 | static bool s_inReceiveEvent = FALSE ; | |
83 | static EventTime sleepTime = kEventDurationNoWait ; | |
84 | #else | |
85 | static long sleepTime = 0 ; | |
86 | #endif | |
738a6daa | 87 | |
2f1ae414 | 88 | #if !USE_SHARED_LIBRARY |
e9576ca5 SC |
89 | IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler) |
90 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) | |
91 | EVT_IDLE(wxApp::OnIdle) | |
15b41e90 SC |
92 | EVT_END_SESSION(wxApp::OnEndSession) |
93 | EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession) | |
e9576ca5 | 94 | END_EVENT_TABLE() |
2f1ae414 | 95 | #endif |
e9576ca5 | 96 | |
169935ad | 97 | |
8461e4c2 | 98 | const short kMacMinHeap = (29 * 1024) ; |
8be97d65 | 99 | // platform specific static variables |
169935ad | 100 | |
519cb848 SC |
101 | const short kwxMacMenuBarResource = 1 ; |
102 | const short kwxMacAppleMenuId = 1 ; | |
103 | ||
fbbdb7cd GD |
104 | WXHRGN wxApp::s_macCursorRgn = NULL; |
105 | wxWindow* wxApp::s_captureWindow = NULL ; | |
106 | int wxApp::s_lastMouseDown = 0 ; | |
107 | long wxApp::sm_lastMessageTime = 0; | |
41d368a4 SC |
108 | long wxApp::s_lastModifiers = 0 ; |
109 | ||
519cb848 | 110 | |
fbbdb7cd GD |
111 | bool wxApp::s_macSupportPCMenuShortcuts = true ; |
112 | long wxApp::s_macAboutMenuItemId = wxID_ABOUT ; | |
882b0479 | 113 | long wxApp::s_macPreferencesMenuItemId = wxID_PREFERENCES ; |
2b5f62a0 | 114 | long wxApp::s_macExitMenuItemId = wxID_EXIT ; |
427ff662 | 115 | wxString wxApp::s_macHelpMenuTitleName = wxT("&Help") ; |
519cb848 | 116 | |
e128397f DS |
117 | // Normally we're not a plugin |
118 | bool wxApp::sm_isEmbedded = false; | |
15b41e90 SC |
119 | //---------------------------------------------------------------------- |
120 | // Core Apple Event Support | |
121 | //---------------------------------------------------------------------- | |
122 | ||
72055702 SC |
123 | pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ; |
124 | pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ; | |
125 | pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long refcon ) ; | |
126 | pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long refcon ) ; | |
1c32ded3 | 127 | pascal OSErr AEHandleRApp( const AppleEvent *event , AppleEvent *reply , long refcon ) ; |
72055702 | 128 | |
2a294857 | 129 | pascal OSErr AEHandleODoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) ) |
519cb848 | 130 | { |
8461e4c2 | 131 | return wxTheApp->MacHandleAEODoc( (AppleEvent*) event , reply) ; |
519cb848 SC |
132 | } |
133 | ||
2a294857 | 134 | pascal OSErr AEHandleOApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) ) |
519cb848 | 135 | { |
8461e4c2 | 136 | return wxTheApp->MacHandleAEOApp( (AppleEvent*) event , reply ) ; |
519cb848 SC |
137 | } |
138 | ||
2a294857 | 139 | pascal OSErr AEHandlePDoc( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) ) |
519cb848 | 140 | { |
8461e4c2 | 141 | return wxTheApp->MacHandleAEPDoc( (AppleEvent*) event , reply ) ; |
519cb848 SC |
142 | } |
143 | ||
2a294857 | 144 | pascal OSErr AEHandleQuit( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) ) |
519cb848 | 145 | { |
8461e4c2 | 146 | return wxTheApp->MacHandleAEQuit( (AppleEvent*) event , reply) ; |
519cb848 SC |
147 | } |
148 | ||
1c32ded3 SC |
149 | pascal OSErr AEHandleRApp( const AppleEvent *event , AppleEvent *reply , long WXUNUSED(refcon) ) |
150 | { | |
151 | return wxTheApp->MacHandleAERApp( (AppleEvent*) event , reply) ; | |
152 | } | |
153 | ||
15b41e90 | 154 | // AEODoc Calls MacOpenFile on each of the files passed |
5ab6aab8 | 155 | |
2072fbbb SC |
156 | short wxApp::MacHandleAEODoc(const WXEVENTREF event, WXEVENTREF WXUNUSED(reply)) |
157 | { | |
158 | AEDescList docList; | |
159 | AEKeyword keywd; | |
160 | DescType returnedType; | |
161 | Size actualSize; | |
162 | long itemsInList; | |
163 | FSSpec theSpec; | |
164 | OSErr err; | |
165 | short i; | |
166 | err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList); | |
167 | if (err != noErr) | |
168 | return err; | |
7c9955d1 | 169 | |
2072fbbb SC |
170 | err = AECountItems(&docList, &itemsInList); |
171 | if (err != noErr) | |
172 | return err; | |
7c9955d1 | 173 | |
8461e4c2 VZ |
174 | ProcessSerialNumber PSN ; |
175 | PSN.highLongOfPSN = 0 ; | |
176 | PSN.lowLongOfPSN = kCurrentProcess ; | |
177 | SetFrontProcess( &PSN ) ; | |
7c9955d1 | 178 | |
2072fbbb SC |
179 | for (i = 1; i <= itemsInList; i++) { |
180 | AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType, | |
181 | (Ptr) & theSpec, sizeof(theSpec), &actualSize); | |
182 | wxString fName = wxMacFSSpec2MacFilename(&theSpec); | |
183 | MacOpenFile(fName); | |
184 | } | |
185 | return noErr; | |
519cb848 SC |
186 | } |
187 | ||
15b41e90 SC |
188 | // AEPDoc Calls MacPrintFile on each of the files passed |
189 | ||
2072fbbb | 190 | short wxApp::MacHandleAEPDoc(const WXEVENTREF event , WXEVENTREF WXUNUSED(reply)) |
519cb848 | 191 | { |
2072fbbb SC |
192 | AEDescList docList; |
193 | AEKeyword keywd; | |
194 | DescType returnedType; | |
195 | Size actualSize; | |
196 | long itemsInList; | |
197 | FSSpec theSpec; | |
198 | OSErr err; | |
199 | short i; | |
200 | err = AEGetParamDesc((AppleEvent *)event, keyDirectObject, typeAEList,&docList); | |
201 | if (err != noErr) | |
202 | return err; | |
7c9955d1 | 203 | |
2072fbbb SC |
204 | err = AECountItems(&docList, &itemsInList); |
205 | if (err != noErr) | |
206 | return err; | |
7c9955d1 | 207 | |
2072fbbb SC |
208 | ProcessSerialNumber PSN ; |
209 | PSN.highLongOfPSN = 0 ; | |
210 | PSN.lowLongOfPSN = kCurrentProcess ; | |
211 | SetFrontProcess( &PSN ) ; | |
7c9955d1 | 212 | |
2072fbbb SC |
213 | for (i = 1; i <= itemsInList; i++) { |
214 | AEGetNthPtr(&docList, i, typeFSS, &keywd, &returnedType, | |
215 | (Ptr) & theSpec, sizeof(theSpec), &actualSize); | |
216 | wxString fName = wxMacFSSpec2MacFilename(&theSpec); | |
217 | MacPrintFile(fName); | |
218 | } | |
219 | return noErr; | |
519cb848 SC |
220 | } |
221 | ||
15b41e90 SC |
222 | // AEOApp calls MacNewFile |
223 | ||
2a294857 | 224 | short wxApp::MacHandleAEOApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply)) |
519cb848 | 225 | { |
2072fbbb | 226 | MacNewFile() ; |
8461e4c2 | 227 | return noErr ; |
519cb848 SC |
228 | } |
229 | ||
76a60a81 | 230 | // AEQuit attempts to quit the application |
15b41e90 | 231 | |
2a294857 | 232 | short wxApp::MacHandleAEQuit(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply)) |
519cb848 | 233 | { |
38106243 | 234 | wxWindow* win = GetTopWindow() ; |
8461e4c2 VZ |
235 | if ( win ) |
236 | { | |
38106243 JS |
237 | wxCommandEvent exitEvent(wxEVT_COMMAND_MENU_SELECTED, s_macExitMenuItemId); |
238 | if (!win->ProcessEvent(exitEvent)) | |
239 | win->Close(TRUE ) ; | |
8461e4c2 | 240 | } |
38106243 | 241 | else |
8461e4c2 VZ |
242 | { |
243 | ExitMainLoop() ; | |
244 | } | |
245 | return noErr ; | |
519cb848 SC |
246 | } |
247 | ||
1c32ded3 SC |
248 | // AEROApp calls MacReopenApp |
249 | ||
250 | short wxApp::MacHandleAERApp(const WXEVENTREF WXUNUSED(event) , WXEVENTREF WXUNUSED(reply)) | |
251 | { | |
252 | MacReopenApp() ; | |
253 | return noErr ; | |
254 | } | |
255 | ||
256 | ||
15b41e90 SC |
257 | //---------------------------------------------------------------------- |
258 | // Support Routines linking the Mac...File Calls to the Document Manager | |
259 | //---------------------------------------------------------------------- | |
260 | ||
261 | void wxApp::MacOpenFile(const wxString & fileName ) | |
262 | { | |
263 | wxDocManager* dm = wxDocManager::GetDocumentManager() ; | |
264 | if ( dm ) | |
265 | dm->CreateDocument(fileName , wxDOC_SILENT ) ; | |
266 | } | |
267 | ||
268 | void wxApp::MacPrintFile(const wxString & fileName ) | |
269 | { | |
270 | wxDocManager* dm = wxDocManager::GetDocumentManager() ; | |
271 | if ( dm ) | |
272 | { | |
273 | wxDocument *doc = dm->CreateDocument(fileName , wxDOC_SILENT ) ; | |
274 | if ( doc ) | |
275 | { | |
276 | wxView* view = doc->GetFirstView() ; | |
277 | if( view ) | |
278 | { | |
279 | wxPrintout *printout = view->OnCreatePrintout(); | |
280 | if (printout) | |
281 | { | |
282 | wxPrinter printer; | |
283 | printer.Print(view->GetFrame(), printout, TRUE); | |
284 | delete printout; | |
285 | } | |
286 | } | |
287 | if (doc->Close()) | |
288 | { | |
289 | doc->DeleteAllViews(); | |
290 | dm->RemoveDocument(doc) ; | |
291 | } | |
292 | } | |
293 | } | |
294 | } | |
295 | ||
296 | void wxApp::MacNewFile() | |
297 | { | |
298 | } | |
299 | ||
1c32ded3 SC |
300 | void wxApp::MacReopenApp() |
301 | { | |
302 | // eventually check for open docs, if none, call MacNewFile | |
303 | } | |
304 | ||
c5c9378c SC |
305 | //---------------------------------------------------------------------- |
306 | // Carbon Event Handler | |
307 | //---------------------------------------------------------------------- | |
9779893b | 308 | |
c5c9378c | 309 | #if TARGET_CARBON |
519cb848 | 310 | |
7c9955d1 | 311 | static const EventTypeSpec eventList[] = |
e40298d5 JS |
312 | { |
313 | { kEventClassCommand, kEventProcessCommand } , | |
314 | { kEventClassCommand, kEventCommandUpdateStatus } , | |
7c9955d1 | 315 | |
e32f4a9f VZ |
316 | { kEventClassMenu, kEventMenuOpening }, |
317 | { kEventClassMenu, kEventMenuClosed }, | |
852b1185 | 318 | { kEventClassMenu, kEventMenuTargetItem }, |
e32f4a9f | 319 | |
e40298d5 JS |
320 | { kEventClassApplication , kEventAppActivated } , |
321 | { kEventClassApplication , kEventAppDeactivated } , | |
322 | // handling the quit event is not recommended by apple | |
323 | // rather using the quit apple event - which we do | |
7c9955d1 | 324 | |
e40298d5 | 325 | { kEventClassAppleEvent , kEventAppleEvent } , |
7c9955d1 | 326 | |
e40298d5 | 327 | { kEventClassMouse , kEventMouseDown } , |
3c589c7f | 328 | { kEventClassMouse , kEventMouseMoved } , |
6d565349 SC |
329 | { kEventClassMouse , kEventMouseUp } , |
330 | { kEventClassMouse , kEventMouseDragged } , | |
e40298d5 JS |
331 | { 'WXMC' , 'WXMC' } |
332 | } ; | |
519cb848 | 333 | |
e32f4a9f VZ |
334 | static pascal OSStatus |
335 | MenuEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) | |
519cb848 | 336 | { |
852b1185 RD |
337 | wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar(); |
338 | wxFrame* win = mbar->GetFrame(); | |
339 | ||
e32f4a9f VZ |
340 | if ( win ) |
341 | { | |
342 | // VZ: we could find the menu from its handle here by examining all | |
343 | // the menus in the menu bar recursively but knowing that neither | |
344 | // wxMSW nor wxGTK do it why bother... | |
345 | #if 0 | |
346 | MenuRef menuRef; | |
347 | ||
348 | GetEventParameter(event, | |
349 | kEventParamDirectObject, | |
350 | typeMenuRef, NULL, | |
351 | sizeof(menuRef), NULL, | |
352 | &menuRef); | |
353 | #endif // 0 | |
354 | ||
852b1185 RD |
355 | wxEventType type=0; |
356 | MenuCommand cmd=0; | |
357 | switch (GetEventKind(event)) | |
358 | { | |
359 | case kEventMenuOpening: | |
360 | type = wxEVT_MENU_OPEN; | |
361 | break; | |
362 | case kEventMenuClosed: | |
363 | type = wxEVT_MENU_CLOSE; | |
364 | break; | |
365 | case kEventMenuTargetItem: | |
366 | type = wxEVT_MENU_HIGHLIGHT; | |
367 | GetEventParameter(event, kEventParamMenuCommand, | |
368 | typeMenuCommand, NULL, | |
369 | sizeof(cmd), NULL, &cmd); | |
370 | if (cmd == 0) return eventNotHandledErr; | |
371 | break; | |
372 | default: | |
373 | wxFAIL_MSG(wxT("Unexpected menu event kind")); | |
374 | break; | |
375 | } | |
376 | ||
377 | wxMenuEvent wxevent(type, cmd); | |
5a036f13 | 378 | wxevent.SetEventObject(win); |
e32f4a9f | 379 | |
5a036f13 | 380 | (void)win->GetEventHandler()->ProcessEvent(wxevent); |
e32f4a9f VZ |
381 | } |
382 | ||
383 | return eventNotHandledErr; | |
519cb848 SC |
384 | } |
385 | ||
7c9955d1 | 386 | // due to the rather low-level event API of wxWindows, we cannot use RunApplicationEventLoop |
c5c9378c SC |
387 | // but have to use ReceiveNextEvent dealing with events manually, therefore we also have |
388 | // deal with clicks in the menu bar explicitely | |
519cb848 | 389 | |
6d565349 SC |
390 | pascal OSStatus wxMacWindowEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) ; |
391 | ||
c5c9378c | 392 | static pascal OSStatus MouseEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) |
519cb848 | 393 | { |
c5c9378c | 394 | OSStatus result = eventNotHandledErr ; |
7c9955d1 | 395 | |
3c589c7f SC |
396 | Point point ; |
397 | UInt32 modifiers = 0; | |
398 | EventMouseButton button = 0 ; | |
399 | UInt32 click = 0 ; | |
400 | ||
401 | GetEventParameter( event, kEventParamMouseLocation, typeQDPoint, NULL, | |
402 | sizeof( Point ), NULL, &point ); | |
403 | GetEventParameter( event, kEventParamKeyModifiers, typeUInt32, NULL, | |
404 | sizeof( UInt32 ), NULL, &modifiers ); | |
405 | GetEventParameter( event, kEventParamMouseButton, typeMouseButton, NULL, | |
406 | sizeof( EventMouseButton ), NULL, &button ); | |
407 | GetEventParameter( event, kEventParamClickCount, typeUInt32, NULL, | |
408 | sizeof( UInt32 ), NULL, &click ); | |
409 | ||
410 | if ( button == 0 || GetEventKind( event ) == kEventMouseUp ) | |
411 | modifiers += btnState ; | |
412 | ||
413 | ||
c5c9378c | 414 | switch( GetEventKind(event) ) |
8461e4c2 | 415 | { |
e40298d5 JS |
416 | case kEventMouseDown : |
417 | { | |
e40298d5 | 418 | WindowRef window ; |
7c9955d1 | 419 | |
e40298d5 JS |
420 | short windowPart = ::FindWindow(point, &window); |
421 | ||
422 | if ( windowPart == inMenuBar ) | |
423 | { | |
424 | MenuSelect( point ) ; | |
425 | result = noErr ; | |
426 | } | |
427 | } | |
428 | break ; | |
6d565349 SC |
429 | case kEventMouseDragged : |
430 | case kEventMouseUp : | |
431 | { | |
432 | if ( wxTheApp->s_captureWindow ) | |
433 | wxMacWindowEventHandler( handler , event , (void*) wxTheApp->s_captureWindow->MacGetTopLevelWindow() ) ; | |
434 | } | |
435 | break ; | |
3c589c7f SC |
436 | case kEventMouseMoved : |
437 | { | |
438 | wxTheApp->MacHandleMouseMovedEvent( point.h , point.v , modifiers , EventTimeToTicks( GetEventTime( event ) ) ) ; | |
439 | result = noErr ; | |
440 | break ; | |
441 | } | |
442 | break ; | |
7c9955d1 | 443 | } |
8461e4c2 | 444 | |
8461e4c2 | 445 | return result ; |
519cb848 | 446 | } |
169935ad | 447 | |
15b41e90 SC |
448 | static pascal OSStatus CommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) |
449 | { | |
450 | OSStatus result = eventNotHandledErr ; | |
451 | ||
e40298d5 | 452 | HICommand command ; |
7c9955d1 | 453 | |
e40298d5 JS |
454 | GetEventParameter( event, kEventParamDirectObject, typeHICommand, NULL, |
455 | sizeof( HICommand ), NULL, &command ); | |
15b41e90 | 456 | |
e40298d5 JS |
457 | MenuCommand id = command.commandID ; |
458 | if ( id == kHICommandPreferences ) | |
459 | id = wxApp::s_macPreferencesMenuItemId ; | |
7c9955d1 | 460 | |
15b41e90 SC |
461 | wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ; |
462 | wxMenu* menu = NULL ; | |
463 | wxMenuItem* item = NULL ; | |
464 | ||
465 | if ( mbar ) | |
6d565349 | 466 | { |
15b41e90 | 467 | item = mbar->FindItem( id , &menu ) ; |
6d565349 SC |
468 | // it is not 100 % sure that an menu of id 0 is really ours, safety check |
469 | if ( id == 0 && menu != NULL && menu->GetHMenu() != command.menu.menuRef ) | |
470 | { | |
471 | item = NULL ; | |
472 | menu = NULL ; | |
473 | } | |
474 | } | |
15b41e90 SC |
475 | |
476 | if ( item == NULL || menu == NULL || mbar == NULL ) | |
e40298d5 | 477 | return result ; |
7c9955d1 | 478 | |
e40298d5 JS |
479 | switch( GetEventKind( event ) ) |
480 | { | |
481 | case kEventProcessCommand : | |
482 | { | |
483 | if (item->IsCheckable()) | |
484 | { | |
485 | item->Check( !item->IsChecked() ) ; | |
486 | } | |
7c9955d1 | 487 | |
e40298d5 JS |
488 | menu->SendEvent( id , item->IsCheckable() ? item->IsChecked() : -1 ) ; |
489 | result = noErr ; | |
490 | } | |
491 | break ; | |
492 | case kEventCommandUpdateStatus: | |
493 | // eventually trigger an updateui round | |
6d565349 | 494 | result = noErr ; |
e40298d5 JS |
495 | break ; |
496 | default : | |
497 | break ; | |
7c9955d1 JS |
498 | } |
499 | ||
15b41e90 SC |
500 | return result ; |
501 | } | |
502 | ||
503 | static pascal OSStatus ApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) | |
504 | { | |
505 | OSStatus result = eventNotHandledErr ; | |
e40298d5 JS |
506 | switch ( GetEventKind( event ) ) |
507 | { | |
508 | case kEventAppActivated : | |
509 | { | |
3470af1c SC |
510 | if ( wxTheApp ) |
511 | wxTheApp->MacResume( true ) ; | |
e40298d5 JS |
512 | result = noErr ; |
513 | } | |
514 | break ; | |
515 | case kEventAppDeactivated : | |
516 | { | |
3470af1c SC |
517 | if ( wxTheApp ) |
518 | wxTheApp->MacSuspend( true ) ; | |
e40298d5 JS |
519 | result = noErr ; |
520 | } | |
521 | break ; | |
522 | default : | |
523 | break ; | |
524 | } | |
15b41e90 SC |
525 | return result ; |
526 | } | |
527 | ||
528 | pascal OSStatus wxAppEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) | |
529 | { | |
530 | OSStatus result = eventNotHandledErr ; | |
531 | switch( GetEventClass( event ) ) | |
532 | { | |
e40298d5 JS |
533 | case kEventClassCommand : |
534 | result = CommandEventHandler( handler , event , data ) ; | |
535 | break ; | |
536 | case kEventClassApplication : | |
537 | result = ApplicationEventHandler( handler , event , data ) ; | |
538 | break ; | |
539 | case kEventClassMenu : | |
540 | result = MenuEventHandler( handler , event , data ) ; | |
541 | break ; | |
542 | case kEventClassMouse : | |
543 | result = MouseEventHandler( handler , event , data ) ; | |
544 | break ; | |
545 | case kEventClassAppleEvent : | |
546 | { | |
547 | EventRecord rec ; | |
548 | wxMacConvertEventToRecord( event , &rec ) ; | |
549 | result = AEProcessAppleEvent( &rec ) ; | |
550 | } | |
551 | break ; | |
552 | default : | |
553 | break ; | |
15b41e90 SC |
554 | } |
555 | ||
556 | return result ; | |
557 | } | |
558 | ||
559 | DEFINE_ONE_SHOT_HANDLER_GETTER( wxAppEventHandler ) | |
560 | ||
561 | #endif | |
562 | ||
21870a5d | 563 | #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__) |
53fd991c SC |
564 | // we know it's there ;-) |
565 | WXIMPORT char std::__throws_bad_alloc ; | |
566 | #endif | |
567 | ||
05e2b077 | 568 | bool wxApp::Initialize(int& argc, wxChar **argv) |
e9576ca5 | 569 | { |
fbbdb7cd | 570 | int error = 0 ; |
9779893b | 571 | |
fbbdb7cd | 572 | // Mac-specific |
9779893b | 573 | |
e128397f | 574 | UMAInitToolbox( 4, sm_isEmbedded ) ; |
fbbdb7cd | 575 | SetEventMask( everyEvent ) ; |
8461e4c2 | 576 | UMAShowWatchCursor() ; |
8be97d65 | 577 | |
21870a5d | 578 | #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__) |
fbbdb7cd GD |
579 | // open shared library resources from here since we don't have |
580 | // __wxinitialize in Mach-O shared libraries | |
581 | wxStAppResource::OpenSharedLibraryResource(NULL); | |
582 | #endif | |
84c1ffa9 | 583 | |
f5c6eb5c | 584 | #ifndef __DARWIN__ |
fbbdb7cd | 585 | // test the minimal configuration necessary |
8be97d65 | 586 | |
fbbdb7cd | 587 | # if !TARGET_CARBON |
8461e4c2 VZ |
588 | long theSystem ; |
589 | long theMachine; | |
590 | ||
591 | if (Gestalt(gestaltMachineType, &theMachine) != noErr) | |
592 | { | |
593 | error = kMacSTRWrongMachine; | |
594 | } | |
595 | else if (theMachine < gestaltMacPlus) | |
596 | { | |
597 | error = kMacSTRWrongMachine; | |
598 | } | |
599 | else if (Gestalt(gestaltSystemVersion, &theSystem) != noErr ) | |
600 | { | |
601 | error = kMacSTROldSystem ; | |
602 | } | |
72055702 | 603 | else if ( theSystem < 0x0860 ) |
8461e4c2 VZ |
604 | { |
605 | error = kMacSTROldSystem ; | |
606 | } | |
607 | else if ((long)GetApplLimit() - (long)ApplicationZone() < kMacMinHeap) | |
608 | { | |
609 | error = kMacSTRSmallSize; | |
610 | } | |
fbbdb7cd | 611 | # endif |
8461e4c2 VZ |
612 | /* |
613 | else | |
614 | { | |
615 | if ( !UMAHasAppearance() ) | |
616 | { | |
617 | error = kMacSTRNoPre8Yet ; | |
618 | } | |
619 | } | |
620 | */ | |
ed5b9811 | 621 | #endif |
8be97d65 | 622 | |
8461e4c2 | 623 | // if we encountered any problems so far, give the error code and exit immediately |
9779893b | 624 | |
169935ad | 625 | if ( error ) |
9779893b | 626 | { |
53fd991c | 627 | wxStAppResource resload ; |
8461e4c2 VZ |
628 | short itemHit; |
629 | Str255 message; | |
630 | ||
631 | GetIndString(message, 128, error); | |
632 | UMAShowArrowCursor() ; | |
633 | ParamText("\pFatal Error", message, (ConstStr255Param)"\p", (ConstStr255Param)"\p"); | |
634 | itemHit = Alert(128, nil); | |
635 | return FALSE ; | |
9779893b | 636 | } |
519cb848 | 637 | |
f5c6eb5c | 638 | #ifndef __DARWIN__ |
fbbdb7cd | 639 | # if __option(profile) |
882b0479 | 640 | ProfilerInit( collectDetailed, bestTimeBase , 40000 , 50 ) ; |
fbbdb7cd | 641 | # endif |
9779893b | 642 | #endif |
519cb848 | 643 | |
f5c6eb5c | 644 | #ifndef __DARWIN__ |
ed581ee2 | 645 | // now avoid exceptions thrown for new (bad_alloc) |
e40298d5 | 646 | // FIXME CS for some changes outside wxMac does not compile anymore |
15b41e90 SC |
647 | #if 0 |
648 | std::__throws_bad_alloc = 0 ; | |
649 | #endif | |
650 | ||
03e11df5 | 651 | #endif |
7c9955d1 | 652 | |
8461e4c2 | 653 | s_macCursorRgn = ::NewRgn() ; |
8be97d65 | 654 | |
05e2b077 VZ |
655 | // Mac OS X passes a process serial number command line argument when |
656 | // the application is launched from the Finder. This argument must be | |
657 | // removed from the command line arguments before being handled by the | |
658 | // application (otherwise applications would need to handle it) | |
659 | if ( argc > 1 ) | |
660 | { | |
661 | static const wxChar *ARG_PSN = _T("-psn_"); | |
46e2a2e7 | 662 | if ( wxStrncmp(argv[1], ARG_PSN, wxStrlen(ARG_PSN)) == 0 ) |
05e2b077 VZ |
663 | { |
664 | // remove this argument | |
33f39af3 GD |
665 | --argc; |
666 | memmove(argv + 1, argv + 2, argc * sizeof(char *)); | |
05e2b077 VZ |
667 | } |
668 | } | |
669 | ||
94826170 VZ |
670 | if ( !wxAppBase::Initialize(argc, argv) ) |
671 | return false; | |
e9576ca5 | 672 | |
d9d19e75 SC |
673 | #if wxUSE_INTL |
674 | wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding()); | |
675 | #endif | |
676 | ||
677 | ||
376b18ea VZ |
678 | wxWinMacWindowList = new wxList(wxKEY_INTEGER); |
679 | wxWinMacControlList = new wxList(wxKEY_INTEGER); | |
0a67a93b | 680 | |
376b18ea | 681 | wxMacCreateNotifierTable() ; |
9779893b | 682 | |
376b18ea | 683 | UMAShowArrowCursor() ; |
8461e4c2 | 684 | |
94826170 | 685 | return true; |
e9576ca5 SC |
686 | } |
687 | ||
15b41e90 SC |
688 | bool wxApp::OnInitGui() |
689 | { | |
e40298d5 JS |
690 | if( !wxAppBase::OnInitGui() ) |
691 | return false ; | |
7c9955d1 JS |
692 | |
693 | #if TARGET_CARBON | |
e40298d5 | 694 | InstallStandardEventHandler( GetApplicationEventTarget() ) ; |
7c9955d1 | 695 | |
e128397f DS |
696 | if (!sm_isEmbedded) |
697 | { | |
698 | InstallApplicationEventHandler( | |
699 | GetwxAppEventHandlerUPP(), | |
700 | GetEventTypeCount(eventList), eventList, wxTheApp, (EventHandlerRef *)&(wxTheApp->m_macEventHandler)); | |
701 | } | |
15b41e90 | 702 | #endif |
7c9955d1 | 703 | |
e128397f DS |
704 | if (!sm_isEmbedded) |
705 | { | |
15b41e90 | 706 | #if defined(UNIVERSAL_INTERFACES_VERSION) && (UNIVERSAL_INTERFACES_VERSION >= 0x0340) |
e128397f DS |
707 | AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments , |
708 | NewAEEventHandlerUPP(AEHandleODoc) , | |
709 | 0 , FALSE ) ; | |
710 | AEInstallEventHandler( kCoreEventClass , kAEOpenApplication , | |
711 | NewAEEventHandlerUPP(AEHandleOApp) , | |
712 | 0 , FALSE ) ; | |
713 | AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments , | |
714 | NewAEEventHandlerUPP(AEHandlePDoc) , | |
715 | 0 , FALSE ) ; | |
1c32ded3 SC |
716 | AEInstallEventHandler( kCoreEventClass , kAEReopenApplication , |
717 | NewAEEventHandlerUPP(AEHandleRApp) , | |
718 | 0 , FALSE ) ; | |
e128397f DS |
719 | AEInstallEventHandler( kCoreEventClass , kAEQuitApplication , |
720 | NewAEEventHandlerUPP(AEHandleQuit) , | |
721 | 0 , FALSE ) ; | |
15b41e90 | 722 | #else |
e128397f DS |
723 | AEInstallEventHandler( kCoreEventClass , kAEOpenDocuments , |
724 | NewAEEventHandlerProc(AEHandleODoc) , | |
725 | 0 , FALSE ) ; | |
726 | AEInstallEventHandler( kCoreEventClass , kAEOpenApplication , | |
727 | NewAEEventHandlerProc(AEHandleOApp) , | |
728 | 0 , FALSE ) ; | |
729 | AEInstallEventHandler( kCoreEventClass , kAEPrintDocuments , | |
730 | NewAEEventHandlerProc(AEHandlePDoc) , | |
731 | 0 , FALSE ) ; | |
1c32ded3 SC |
732 | AEInstallEventHandler( kCoreEventClass , kAEReopenApplication , |
733 | NewAEEventHandlerProc(AEHandleRApp) , | |
734 | 0 , FALSE ) ; | |
e128397f DS |
735 | AEInstallEventHandler( kCoreEventClass , kAEQuitApplication , |
736 | NewAEEventHandlerProc(AEHandleQuit) , | |
737 | 0 , FALSE ) ; | |
15b41e90 | 738 | #endif |
e128397f | 739 | } |
15b41e90 | 740 | |
e40298d5 | 741 | return TRUE ; |
15b41e90 SC |
742 | } |
743 | ||
e9576ca5 SC |
744 | void wxApp::CleanUp() |
745 | { | |
12cd5f34 | 746 | wxToolTip::RemoveToolTips() ; |
2f1ae414 SC |
747 | |
748 | // One last chance for pending objects to be cleaned up | |
749 | wxTheApp->DeletePendingObjects(); | |
750 | ||
fbbdb7cd | 751 | wxMacDestroyNotifierTable() ; |
84c1ffa9 | 752 | |
94826170 VZ |
753 | delete wxWinMacWindowList ; |
754 | wxWinMacWindowList = NULL; | |
2f1ae414 | 755 | |
94826170 VZ |
756 | delete wxWinMacControlList ; |
757 | wxWinMacControlList = NULL; | |
e9576ca5 | 758 | |
f5c6eb5c | 759 | #ifndef __DARWIN__ |
fbbdb7cd | 760 | # if __option(profile) |
ac2153a6 | 761 | ProfilerDump( (StringPtr)"\papp.prof" ) ; |
fbbdb7cd GD |
762 | ProfilerTerm() ; |
763 | # endif | |
9779893b | 764 | #endif |
519cb848 | 765 | |
21870a5d | 766 | #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__) |
fbbdb7cd GD |
767 | // close shared library resources from here since we don't have |
768 | // __wxterminate in Mach-O shared libraries | |
769 | wxStAppResource::CloseSharedLibraryResource(); | |
770 | #endif | |
7c9955d1 | 771 | |
8461e4c2 | 772 | UMACleanupToolbox() ; |
fbbdb7cd | 773 | if (s_macCursorRgn) { |
76a5e5d2 | 774 | ::DisposeRgn((RgnHandle)s_macCursorRgn); |
fbbdb7cd | 775 | } |
84c1ffa9 | 776 | |
8461e4c2 VZ |
777 | #if 0 |
778 | TerminateAE() ; | |
779 | #endif | |
94826170 VZ |
780 | |
781 | wxAppBase::CleanUp(); | |
e9576ca5 SC |
782 | } |
783 | ||
92b002a4 | 784 | //---------------------------------------------------------------------- |
05e2b077 | 785 | // misc initialization stuff |
92b002a4 RD |
786 | //---------------------------------------------------------------------- |
787 | ||
fbbdb7cd GD |
788 | // extern variable for shared library resource id |
789 | // need to be able to find it with NSLookupAndBindSymbol | |
790 | short gSharedLibraryResource = kResFileNotOpened ; | |
791 | ||
21870a5d | 792 | #if defined(WXMAKINGDLL_CORE) && defined(__DARWIN__) |
fbbdb7cd | 793 | CFBundleRef gSharedLibraryBundle = NULL; |
21870a5d | 794 | #endif /* WXMAKINGDLL_CORE && __DARWIN__ */ |
53fd991c SC |
795 | |
796 | wxStAppResource::wxStAppResource() | |
797 | { | |
fbbdb7cd GD |
798 | m_currentRefNum = CurResFile() ; |
799 | if ( gSharedLibraryResource != kResFileNotOpened ) | |
800 | { | |
801 | UseResFile( gSharedLibraryResource ) ; | |
802 | } | |
803 | } | |
804 | ||
805 | wxStAppResource::~wxStAppResource() | |
806 | { | |
807 | if ( m_currentRefNum != kResFileNotOpened ) | |
808 | { | |
809 | UseResFile( m_currentRefNum ) ; | |
810 | } | |
811 | } | |
812 | ||
813 | void wxStAppResource::OpenSharedLibraryResource(const void *initBlock) | |
814 | { | |
815 | gSharedLibraryResource = kResFileNotOpened; | |
816 | ||
21870a5d | 817 | #ifdef WXMAKINGDLL_CORE |
fbbdb7cd GD |
818 | if ( initBlock != NULL ) { |
819 | const CFragInitBlock *theInitBlock = (const CFragInitBlock *)initBlock; | |
820 | FSSpec *fileSpec = NULL; | |
84c1ffa9 | 821 | |
fbbdb7cd GD |
822 | if (theInitBlock->fragLocator.where == kDataForkCFragLocator) { |
823 | fileSpec = theInitBlock->fragLocator.u.onDisk.fileSpec; | |
824 | } | |
825 | else if (theInitBlock->fragLocator.where == kResourceCFragLocator) { | |
826 | fileSpec = theInitBlock->fragLocator.u.inSegs.fileSpec; | |
827 | } | |
84c1ffa9 | 828 | |
fbbdb7cd GD |
829 | if (fileSpec != NULL) { |
830 | gSharedLibraryResource = FSpOpenResFile(fileSpec, fsRdPerm); | |
831 | } | |
832 | } | |
833 | else { | |
834 | #ifdef __DARWIN__ | |
835 | // Open the shared library resource file if it is not yet open | |
ed581ee2 GD |
836 | NSSymbol theSymbol; |
837 | NSModule theModule; | |
838 | const char *theLibPath; | |
84c1ffa9 | 839 | |
fbbdb7cd GD |
840 | gSharedLibraryBundle = CFBundleGetBundleWithIdentifier(CFSTR("com.wxwindows.wxWindows")); |
841 | if (gSharedLibraryBundle != NULL) { | |
ed581ee2 GD |
842 | // wxWindows has been bundled into a framework |
843 | // load the framework resources | |
84c1ffa9 | 844 | |
fbbdb7cd | 845 | gSharedLibraryResource = CFBundleOpenBundleResourceMap(gSharedLibraryBundle); |
ed581ee2 GD |
846 | } |
847 | else { | |
848 | // wxWindows is a simple dynamic shared library | |
849 | // load the resources from the data fork of a separate resource file | |
21870a5d RD |
850 | wxString theResPath; |
851 | wxString theName; | |
ed581ee2 GD |
852 | FSRef theResRef; |
853 | OSErr theErr = noErr; | |
84c1ffa9 | 854 | |
ed581ee2 | 855 | // get the library path |
fbbdb7cd | 856 | theSymbol = NSLookupAndBindSymbol("_gSharedLibraryResource"); |
ed581ee2 GD |
857 | theModule = NSModuleForSymbol(theSymbol); |
858 | theLibPath = NSLibraryNameForModule(theModule); | |
ed581ee2 | 859 | |
376b18ea VZ |
860 | // if we call wxLogDebug from here then, as wxTheApp hasn't been |
861 | // created yet when we're called from wxApp::Initialize(), wxLog | |
862 | // is going to create a default stderr-based log target instead of | |
863 | // the expected normal GUI one -- don't do it, if we really want | |
864 | // to see this message just use fprintf() here | |
865 | #if 0 | |
f346733b GD |
866 | wxLogDebug( wxT("wxMac library installation name is '%s'"), |
867 | theLibPath ); | |
376b18ea | 868 | #endif |
f346733b | 869 | |
ed581ee2 | 870 | // allocate copy to replace .dylib.* extension with .rsrc |
21870a5d | 871 | if (theLibPath != NULL) { |
bc93670d GD |
872 | #if wxUSE_UNICODE |
873 | theResPath = wxString(theLibPath, wxConvLocal); | |
874 | #else | |
875 | theResPath = wxString(theLibPath); | |
876 | #endif | |
21870a5d RD |
877 | // replace '_core' with '' in case of multi-lib build |
878 | theResPath.Replace(wxT("_core"), wxEmptyString); | |
879 | // replace ".dylib" shared library extension with ".rsrc" | |
880 | theResPath.Replace(wxT(".dylib"), wxT(".rsrc")); | |
881 | // Find the begining of the filename | |
882 | theName = theResPath.AfterLast('/'); | |
84c1ffa9 | 883 | |
376b18ea | 884 | #if 0 |
f346733b | 885 | wxLogDebug( wxT("wxMac resources file name is '%s'"), |
21870a5d | 886 | theResPath.mb_str() ); |
376b18ea | 887 | #endif |
ed581ee2 | 888 | |
21870a5d | 889 | theErr = FSPathMakeRef((UInt8 *) theResPath.mb_str(), &theResRef, false); |
ed581ee2 GD |
890 | if (theErr != noErr) { |
891 | // try in current directory (using name only) | |
21870a5d | 892 | theErr = FSPathMakeRef((UInt8 *) theName.mb_str(), &theResRef, false); |
ed581ee2 | 893 | } |
84c1ffa9 | 894 | |
ed581ee2 GD |
895 | // open the resource file |
896 | if (theErr == noErr) { | |
897 | theErr = FSOpenResourceFile( &theResRef, 0, NULL, fsRdPerm, | |
fbbdb7cd | 898 | &gSharedLibraryResource); |
ed581ee2 | 899 | } |
f346733b | 900 | if (theErr != noErr) { |
376b18ea | 901 | #ifdef __WXDEBUG__ |
bc93670d GD |
902 | wxLogDebug( wxT("unable to open wxMac resource file '%s'\n"), |
903 | theResPath.mb_str() ); | |
376b18ea | 904 | #endif // __WXDEBUG__ |
f346733b GD |
905 | } |
906 | ||
ed581ee2 GD |
907 | } |
908 | } | |
fbbdb7cd | 909 | #endif /* __DARWIN__ */ |
53fd991c | 910 | } |
21870a5d | 911 | #endif /* WXMAKINGDLL_CORE */ |
53fd991c SC |
912 | } |
913 | ||
fbbdb7cd | 914 | void wxStAppResource::CloseSharedLibraryResource() |
53fd991c | 915 | { |
21870a5d | 916 | #ifdef WXMAKINGDLL_CORE |
ed581ee2 | 917 | // Close the shared library resource file |
fbbdb7cd GD |
918 | if (gSharedLibraryResource != kResFileNotOpened) { |
919 | #ifdef __DARWIN__ | |
920 | if (gSharedLibraryBundle != NULL) { | |
921 | CFBundleCloseBundleResourceMap(gSharedLibraryBundle, | |
922 | gSharedLibraryResource); | |
923 | gSharedLibraryBundle = NULL; | |
ed581ee2 | 924 | } |
fbbdb7cd GD |
925 | else |
926 | #endif /* __DARWIN__ */ | |
927 | { | |
928 | CloseResFile(gSharedLibraryResource); | |
ed581ee2 | 929 | } |
fbbdb7cd | 930 | gSharedLibraryResource = kResFileNotOpened; |
84c1ffa9 | 931 | } |
21870a5d | 932 | #endif /* WXMAKINGDLL_CORE */ |
53fd991c SC |
933 | } |
934 | ||
21870a5d | 935 | #if defined(WXMAKINGDLL_CORE) && !defined(__DARWIN__) |
53fd991c | 936 | |
ed581ee2 GD |
937 | // for shared libraries we have to manually get the correct resource |
938 | // ref num upon initializing and releasing when terminating, therefore | |
939 | // the __wxinitialize and __wxterminate must be used | |
53fd991c | 940 | |
53fd991c | 941 | extern "C" { |
e40298d5 | 942 | void __sinit(void); /* (generated by linker) */ |
fbbdb7cd GD |
943 | pascal OSErr __initialize(const CFragInitBlock *theInitBlock); |
944 | pascal void __terminate(void); | |
53fd991c | 945 | } |
53fd991c SC |
946 | |
947 | pascal OSErr __wxinitialize(const CFragInitBlock *theInitBlock) | |
948 | { | |
fbbdb7cd GD |
949 | wxStAppResource::OpenSharedLibraryResource( theInitBlock ) ; |
950 | return __initialize( theInitBlock ) ; | |
53fd991c SC |
951 | } |
952 | ||
953 | pascal void __wxterminate(void) | |
954 | { | |
fbbdb7cd | 955 | wxStAppResource::CloseSharedLibraryResource() ; |
53fd991c SC |
956 | __terminate() ; |
957 | } | |
ed581ee2 | 958 | |
21870a5d | 959 | #endif /* WXMAKINGDLL_CORE && !__DARWIN__ */ |
53fd991c | 960 | |
b4efa069 SC |
961 | #if TARGET_CARBON |
962 | ||
963 | bool wxMacConvertEventToRecord( EventRef event , EventRecord *rec) | |
964 | { | |
965 | bool converted = ConvertEventRefToEventRecord( event,rec) ; | |
966 | OSStatus err = noErr ; | |
967 | if ( !converted ) | |
968 | { | |
969 | switch( GetEventClass( event ) ) | |
970 | { | |
971 | case kEventClassKeyboard : | |
972 | { | |
973 | converted = true ; | |
84c1ffa9 | 974 | switch( GetEventKind(event) ) |
b4efa069 SC |
975 | { |
976 | case kEventRawKeyDown : | |
977 | rec->what = keyDown ; | |
978 | break ; | |
979 | case kEventRawKeyRepeat : | |
980 | rec->what = autoKey ; | |
981 | break ; | |
982 | case kEventRawKeyUp : | |
983 | rec->what = keyUp ; | |
984 | break ; | |
985 | case kEventRawKeyModifiersChanged : | |
986 | rec->what = nullEvent ; | |
987 | break ; | |
988 | default : | |
989 | converted = false ; | |
990 | break ; | |
991 | } | |
992 | if ( converted ) | |
993 | { | |
994 | UInt32 keyCode ; | |
995 | unsigned char charCode ; | |
996 | UInt32 modifiers ; | |
997 | GetMouse( &rec->where) ; | |
998 | ||
999 | err = GetEventParameter(event, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers); | |
1000 | err = GetEventParameter(event, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode); | |
1001 | err = GetEventParameter(event, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode); | |
1002 | rec->modifiers = modifiers ; | |
1003 | rec->message = (keyCode << 8 ) + charCode ; | |
1004 | } | |
1005 | } | |
1006 | break ; | |
1007 | case kEventClassTextInput : | |
1008 | { | |
1009 | switch( GetEventKind( event ) ) | |
1010 | { | |
1011 | case kEventTextInputUnicodeForKeyEvent : | |
1012 | { | |
1013 | EventRef rawEvent ; | |
1014 | err = GetEventParameter( event , kEventParamTextInputSendKeyboardEvent ,typeEventRef,NULL,sizeof(rawEvent),NULL,&rawEvent ) ; | |
1015 | converted = true ; | |
1016 | { | |
1017 | UInt32 keyCode ; | |
1018 | unsigned char charCode ; | |
1019 | UInt32 modifiers ; | |
1020 | GetMouse( &rec->where) ; | |
1021 | rec->what = keyDown ; | |
1022 | err = GetEventParameter(rawEvent, kEventParamKeyModifiers, typeUInt32, NULL, 4, NULL, &modifiers); | |
1023 | err = GetEventParameter(rawEvent, kEventParamKeyCode, typeUInt32, NULL, 4, NULL, &keyCode); | |
1024 | err = GetEventParameter(rawEvent, kEventParamKeyMacCharCodes, typeChar, NULL, 1, NULL, &charCode); | |
1025 | rec->modifiers = modifiers ; | |
1026 | rec->message = (keyCode << 8 ) + charCode ; | |
1027 | } | |
1028 | } | |
1029 | break ; | |
1030 | default : | |
1031 | break ; | |
1032 | } | |
1033 | } | |
1034 | break ; | |
1035 | } | |
1036 | } | |
84c1ffa9 | 1037 | |
b4efa069 SC |
1038 | return converted ; |
1039 | } | |
1040 | ||
15b41e90 | 1041 | /* |
b4efa069 SC |
1042 | pascal OSStatus wxMacApplicationEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) |
1043 | { | |
1044 | OSStatus result = eventNotHandledErr ; | |
84c1ffa9 | 1045 | |
b4efa069 SC |
1046 | EventRecord rec ; |
1047 | switch ( GetEventClass( event ) ) | |
1048 | { | |
1049 | case kEventClassKeyboard : | |
1050 | if ( wxMacConvertEventToRecord( event , &rec ) ) | |
1051 | { | |
41d368a4 | 1052 | wxTheApp->MacHandleModifierEvents( &rec ) ; |
b4efa069 SC |
1053 | wxTheApp->MacHandleOneEvent( &rec ) ; |
1054 | result = noErr ; | |
1055 | } | |
1056 | break ; | |
1057 | case kEventClassTextInput : | |
1058 | if ( wxMacConvertEventToRecord( event , &rec ) ) | |
1059 | { | |
41d368a4 | 1060 | wxTheApp->MacHandleModifierEvents( &rec ) ; |
b4efa069 SC |
1061 | wxTheApp->MacHandleOneEvent( &rec ) ; |
1062 | result = noErr ; | |
1063 | } | |
1064 | break ; | |
1065 | default : | |
1066 | break ; | |
1067 | } | |
1068 | return result ; | |
1069 | } | |
15b41e90 | 1070 | */ |
b4efa069 SC |
1071 | #endif |
1072 | ||
e9576ca5 SC |
1073 | wxApp::wxApp() |
1074 | { | |
e9576ca5 | 1075 | m_printMode = wxPRINT_WINDOWS; |
e9576ca5 | 1076 | m_auto3D = TRUE; |
1ea39a03 SC |
1077 | |
1078 | m_macCurrentEvent = NULL ; | |
d16477fd | 1079 | #if TARGET_CARBON |
1ea39a03 | 1080 | m_macCurrentEventHandlerCallRef = NULL ; |
d16477fd | 1081 | #endif |
e9576ca5 SC |
1082 | } |
1083 | ||
e9576ca5 SC |
1084 | int wxApp::MainLoop() |
1085 | { | |
e40298d5 | 1086 | m_keepGoing = TRUE; |
e9576ca5 | 1087 | |
e40298d5 JS |
1088 | while (m_keepGoing) |
1089 | { | |
1090 | MacDoOneEvent() ; | |
1091 | } | |
e9576ca5 | 1092 | |
e40298d5 | 1093 | return 0; |
e9576ca5 SC |
1094 | } |
1095 | ||
e9576ca5 SC |
1096 | void wxApp::ExitMainLoop() |
1097 | { | |
e40298d5 | 1098 | m_keepGoing = FALSE; |
e9576ca5 SC |
1099 | } |
1100 | ||
1101 | // Is a message/event pending? | |
1102 | bool wxApp::Pending() | |
1103 | { | |
c5c9378c | 1104 | #if TARGET_CARBON |
e40298d5 | 1105 | return GetNumEventsInQueue( GetMainEventQueue() ) > 0 ; |
c5c9378c | 1106 | #else |
8461e4c2 | 1107 | EventRecord event ; |
519cb848 | 1108 | |
e39af974 | 1109 | return EventAvail( everyEvent , &event ) ; |
c5c9378c | 1110 | #endif |
e9576ca5 SC |
1111 | } |
1112 | ||
1113 | // Dispatch a message. | |
1bf77ee5 | 1114 | bool wxApp::Dispatch() |
e9576ca5 | 1115 | { |
8461e4c2 | 1116 | MacDoOneEvent() ; |
1bf77ee5 VZ |
1117 | |
1118 | return true; | |
e9576ca5 SC |
1119 | } |
1120 | ||
1121 | void wxApp::OnIdle(wxIdleEvent& event) | |
1122 | { | |
955a9197 | 1123 | wxAppBase::OnIdle(event); |
21870a5d | 1124 | |
2f1ae414 SC |
1125 | // If they are pending events, we must process them: pending events are |
1126 | // either events to the threads other than main or events posted with | |
1127 | // wxPostEvent() functions | |
1128 | wxMacProcessNotifierAndPendingEvents(); | |
1129 | ||
3fdac2ab DE |
1130 | if(!wxMenuBar::MacGetInstalledMenuBar() && wxMenuBar::MacGetCommonMenuBar()) |
1131 | wxMenuBar::MacGetCommonMenuBar()->MacInstallMenuBar(); | |
e9576ca5 SC |
1132 | } |
1133 | ||
e2478fde | 1134 | void wxApp::WakeUpIdle() |
9779893b | 1135 | { |
8461e4c2 | 1136 | wxMacWakeUp() ; |
9779893b RD |
1137 | } |
1138 | ||
e2478fde | 1139 | void wxApp::Exit() |
e9576ca5 | 1140 | { |
2f1ae414 | 1141 | wxApp::CleanUp(); |
8461e4c2 | 1142 | ::ExitToShell() ; |
e9576ca5 SC |
1143 | } |
1144 | ||
2f1ae414 SC |
1145 | void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event)) |
1146 | { | |
1147 | if (GetTopWindow()) | |
1148 | GetTopWindow()->Close(TRUE); | |
1149 | } | |
1150 | ||
1151 | // Default behaviour: close the application with prompts. The | |
1152 | // user can veto the close, and therefore the end session. | |
1153 | void wxApp::OnQueryEndSession(wxCloseEvent& event) | |
1154 | { | |
1155 | if (GetTopWindow()) | |
1156 | { | |
1157 | if (!GetTopWindow()->Close(!event.CanVeto())) | |
1158 | event.Veto(TRUE); | |
1159 | } | |
1160 | } | |
1161 | ||
1162 | extern "C" void wxCYield() ; | |
1163 | void wxCYield() | |
1164 | { | |
8461e4c2 | 1165 | wxYield() ; |
2f1ae414 SC |
1166 | } |
1167 | ||
e9576ca5 | 1168 | // Yield to other processes |
cb2713bf | 1169 | |
8461e4c2 | 1170 | bool wxApp::Yield(bool onlyIfNeeded) |
e9576ca5 | 1171 | { |
8461e4c2 VZ |
1172 | if (s_inYield) |
1173 | { | |
1174 | if ( !onlyIfNeeded ) | |
1175 | { | |
1176 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
1177 | } | |
1178 | ||
1179 | return FALSE; | |
1180 | } | |
1181 | ||
1182 | s_inYield = TRUE; | |
cb2713bf | 1183 | |
2f1ae414 | 1184 | #if wxUSE_THREADS |
cb2713bf | 1185 | YieldToAnyThread() ; |
2f1ae414 | 1186 | #endif |
e40298d5 | 1187 | // by definition yield should handle all non-processed events |
c5c9378c | 1188 | #if TARGET_CARBON |
e40298d5 JS |
1189 | EventRef theEvent; |
1190 | ||
1191 | OSStatus status = noErr ; | |
1192 | do | |
1193 | { | |
1194 | s_inReceiveEvent = true ; | |
1195 | status = ReceiveNextEvent(0, NULL,kEventDurationNoWait,true,&theEvent) ; | |
1196 | s_inReceiveEvent = false ; | |
7c9955d1 | 1197 | |
e40298d5 JS |
1198 | if ( status == eventLoopTimedOutErr ) |
1199 | { | |
1200 | // make sure next time the event loop will trigger idle events | |
1201 | sleepTime = kEventDurationNoWait ; | |
1202 | } | |
1203 | else if ( status == eventLoopQuitErr ) | |
1204 | { | |
1205 | // according to QA1061 this may also occur when a WakeUp Process | |
1206 | // is executed | |
1207 | } | |
1208 | else | |
1209 | { | |
1210 | MacHandleOneEvent( theEvent ) ; | |
1211 | ReleaseEvent(theEvent); | |
1212 | } | |
1213 | } while( status == noErr ) ; | |
c5c9378c | 1214 | #else |
cb2713bf | 1215 | EventRecord event ; |
2f1ae414 | 1216 | |
e40298d5 JS |
1217 | // having a larger value here leads to large performance slowdowns |
1218 | // so we cannot give background apps more processor time here | |
1219 | // we do so however having a large sleep value in the main event loop | |
7c9955d1 | 1220 | sleepTime = 0 ; |
2f1ae414 | 1221 | |
c5c9378c | 1222 | while ( !IsExiting() && WaitNextEvent(everyEvent, &event,sleepTime, (RgnHandle) wxApp::s_macCursorRgn)) |
8461e4c2 | 1223 | { |
c5c9378c SC |
1224 | MacHandleModifierEvents( &event ) ; |
1225 | MacHandleOneEvent( &event ); | |
8461e4c2 | 1226 | if ( event.what != kHighLevelEvent ) |
76a5e5d2 | 1227 | SetRectRgn( (RgnHandle) wxApp::s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ; |
8461e4c2 | 1228 | } |
c5c9378c SC |
1229 | MacHandleModifierEvents( &event ) ; |
1230 | #endif | |
2f1ae414 | 1231 | |
8461e4c2 | 1232 | wxMacProcessNotifierAndPendingEvents() ; |
8461e4c2 | 1233 | s_inYield = FALSE; |
cb2713bf | 1234 | |
8461e4c2 | 1235 | return TRUE; |
169935ad SC |
1236 | } |
1237 | ||
9779893b | 1238 | // platform specifics |
169935ad | 1239 | |
519cb848 SC |
1240 | void wxApp::MacSuspend( bool convertClipboard ) |
1241 | { | |
15b41e90 | 1242 | #if !TARGET_CARBON |
1c469f7f | 1243 | // we have to deactive the top level windows manually |
8461e4c2 | 1244 | |
653a3ed9 | 1245 | wxWindowListNode* node = wxTopLevelWindows.GetFirst(); |
1c469f7f SC |
1246 | while (node) |
1247 | { | |
1248 | wxTopLevelWindow* win = (wxTopLevelWindow*) node->Data(); | |
b9c10231 SC |
1249 | #if TARGET_CARBON |
1250 | #if 0 // having problems right now with that | |
6a7e6411 | 1251 | if (!win->HasFlag(wxSTAY_ON_TOP)) |
21870a5d | 1252 | #endif |
b9c10231 | 1253 | #endif |
6a7e6411 | 1254 | win->MacActivate( ((EventRecord*) MacGetCurrentEvent())->when , false ) ; |
8461e4c2 | 1255 | |
653a3ed9 | 1256 | node = node->GetNext(); |
1c469f7f | 1257 | } |
84c1ffa9 | 1258 | |
15b41e90 SC |
1259 | ::HideFloatingWindows() ; |
1260 | #endif | |
2a294857 | 1261 | s_lastMouseDown = 0 ; |
15b41e90 | 1262 | |
2a294857 GD |
1263 | if( convertClipboard ) |
1264 | { | |
1265 | MacConvertPrivateToPublicScrap() ; | |
1266 | } | |
519cb848 SC |
1267 | } |
1268 | ||
2a294857 GD |
1269 | extern wxList wxModalDialogs; |
1270 | ||
519cb848 SC |
1271 | void wxApp::MacResume( bool convertClipboard ) |
1272 | { | |
2a294857 GD |
1273 | s_lastMouseDown = 0 ; |
1274 | if( convertClipboard ) | |
1275 | { | |
1276 | MacConvertPublicToPrivateScrap() ; | |
1277 | } | |
84c1ffa9 | 1278 | |
15b41e90 | 1279 | #if !TARGET_CARBON |
2a294857 | 1280 | ::ShowFloatingWindows() ; |
2a294857 | 1281 | // raise modal dialogs in case a non modal window was selected to activate the app |
9779893b | 1282 | |
653a3ed9 | 1283 | wxNode* node = wxModalDialogs.GetFirst(); |
2a294857 GD |
1284 | while (node) |
1285 | { | |
653a3ed9 | 1286 | wxDialog* dialog = (wxDialog *) node->GetData(); |
2a294857 | 1287 | dialog->Raise(); |
84c1ffa9 | 1288 | |
653a3ed9 | 1289 | node = node->GetNext(); |
2a294857 | 1290 | } |
15b41e90 | 1291 | #endif |
519cb848 SC |
1292 | } |
1293 | ||
1294 | void wxApp::MacConvertPrivateToPublicScrap() | |
1295 | { | |
519cb848 SC |
1296 | } |
1297 | ||
1298 | void wxApp::MacConvertPublicToPrivateScrap() | |
1299 | { | |
519cb848 SC |
1300 | } |
1301 | ||
9779893b | 1302 | void wxApp::MacDoOneEvent() |
169935ad | 1303 | { |
c5c9378c | 1304 | #if TARGET_CARBON |
e40298d5 | 1305 | EventRef theEvent; |
c5c9378c | 1306 | |
e40298d5 JS |
1307 | s_inReceiveEvent = true ; |
1308 | OSStatus status = ReceiveNextEvent(0, NULL,sleepTime,true,&theEvent) ; | |
1309 | s_inReceiveEvent = false ; | |
1310 | if ( status == eventLoopTimedOutErr ) | |
1311 | { | |
c5c9378c | 1312 | if ( wxTheApp->ProcessIdle() ) |
e40298d5 | 1313 | sleepTime = kEventDurationNoWait ; |
c5c9378c | 1314 | else |
e64313ba | 1315 | { |
c944ce35 | 1316 | #if wxUSE_THREADS |
e64313ba DS |
1317 | if (g_numberOfThreads) |
1318 | { | |
1319 | sleepTime = kEventDurationNoWait; | |
1320 | } | |
1321 | else | |
c944ce35 | 1322 | #endif // wxUSE_THREADS |
e64313ba DS |
1323 | { |
1324 | sleepTime = kEventDurationSecond; | |
1325 | } | |
1326 | } | |
e40298d5 JS |
1327 | } |
1328 | else if ( status == eventLoopQuitErr ) | |
1329 | { | |
1330 | // according to QA1061 this may also occur when a WakeUp Process | |
1331 | // is executed | |
1332 | } | |
1333 | else | |
1334 | { | |
1335 | MacHandleOneEvent( theEvent ) ; | |
1336 | ReleaseEvent(theEvent); | |
3470af1c | 1337 | sleepTime = kEventDurationNoWait ; |
e40298d5 | 1338 | } |
c5c9378c | 1339 | #else |
e40298d5 | 1340 | EventRecord event ; |
169935ad | 1341 | |
e40298d5 | 1342 | EventMask eventMask = everyEvent ; |
169935ad | 1343 | |
c5c9378c | 1344 | if (WaitNextEvent(eventMask, &event, sleepTime, (RgnHandle) s_macCursorRgn)) |
8461e4c2 | 1345 | { |
41d368a4 | 1346 | MacHandleModifierEvents( &event ) ; |
6264b550 | 1347 | MacHandleOneEvent( &event ); |
8461e4c2 VZ |
1348 | } |
1349 | else | |
1350 | { | |
41d368a4 | 1351 | MacHandleModifierEvents( &event ) ; |
8461e4c2 | 1352 | // idlers |
72055702 | 1353 | WindowPtr window = ::FrontWindow() ; |
8461e4c2 | 1354 | if ( window ) |
72055702 | 1355 | ::IdleControls( window ) ; |
9779893b | 1356 | |
c5c9378c | 1357 | if ( wxTheApp->ProcessIdle() ) |
e128397f | 1358 | sleepTime = kEventDurationNoWait; |
c5c9378c | 1359 | else |
e64313ba | 1360 | { |
c944ce35 | 1361 | #if wxUSE_THREADS |
e64313ba DS |
1362 | if (g_numberOfThreads) |
1363 | { | |
1364 | sleepTime = kEventDurationNoWait; | |
1365 | } | |
1366 | else | |
c944ce35 | 1367 | #endif // wxUSE_THREADS |
e64313ba DS |
1368 | { |
1369 | sleepTime = kEventDurationSecond; | |
1370 | } | |
1371 | } | |
8461e4c2 VZ |
1372 | } |
1373 | if ( event.what != kHighLevelEvent ) | |
76a5e5d2 | 1374 | SetRectRgn( (RgnHandle) s_macCursorRgn , event.where.h , event.where.v , event.where.h + 1 , event.where.v + 1 ) ; |
c5c9378c | 1375 | #endif |
8461e4c2 | 1376 | // repeaters |
519cb848 | 1377 | |
6264b550 | 1378 | DeletePendingObjects() ; |
8461e4c2 | 1379 | wxMacProcessNotifierAndPendingEvents() ; |
169935ad SC |
1380 | } |
1381 | ||
e128397f DS |
1382 | /*virtual*/ void wxApp::MacHandleUnhandledEvent( WXEVENTREF evr ) |
1383 | { | |
1384 | // Override to process unhandled events as you please | |
1385 | } | |
1386 | ||
76a5e5d2 | 1387 | void wxApp::MacHandleOneEvent( WXEVENTREF evr ) |
169935ad | 1388 | { |
c5c9378c | 1389 | #if TARGET_CARBON |
e40298d5 JS |
1390 | EventTargetRef theTarget; |
1391 | theTarget = GetEventDispatcherTarget(); | |
c5c9378c | 1392 | m_macCurrentEvent = evr ; |
e128397f DS |
1393 | OSStatus status = SendEventToEventTarget ((EventRef) evr , theTarget); |
1394 | if(status == eventNotHandledErr) | |
1395 | { | |
1396 | MacHandleUnhandledEvent(evr); | |
1397 | } | |
c5c9378c | 1398 | #else |
76a5e5d2 | 1399 | EventRecord* ev = (EventRecord*) evr ; |
8461e4c2 VZ |
1400 | m_macCurrentEvent = ev ; |
1401 | ||
1402 | wxApp::sm_lastMessageTime = ev->when ; | |
1403 | ||
1404 | switch (ev->what) | |
1405 | { | |
1406 | case mouseDown: | |
1407 | MacHandleMouseDownEvent( ev ) ; | |
1408 | if ( ev->modifiers & controlKey ) | |
1409 | s_lastMouseDown = 2; | |
1410 | else | |
1411 | s_lastMouseDown = 1; | |
1412 | break; | |
1413 | case mouseUp: | |
1414 | if ( s_lastMouseDown == 2 ) | |
1415 | { | |
1416 | ev->modifiers |= controlKey ; | |
1417 | } | |
1418 | else | |
1419 | { | |
1420 | ev->modifiers &= ~controlKey ; | |
1421 | } | |
1422 | MacHandleMouseUpEvent( ev ) ; | |
1423 | s_lastMouseDown = 0; | |
1424 | break; | |
1425 | case activateEvt: | |
1426 | MacHandleActivateEvent( ev ) ; | |
1427 | break; | |
1428 | case updateEvt: | |
e128397f DS |
1429 | // In embedded mode we first let the UnhandledEvent function |
1430 | // try to handle the update event. If we handle it ourselves | |
1431 | // first and then pass it on, the host's windows won't update. | |
1432 | MacHandleUnhandledEvent(ev); | |
8461e4c2 VZ |
1433 | MacHandleUpdateEvent( ev ) ; |
1434 | break; | |
1435 | case keyDown: | |
1436 | case autoKey: | |
1437 | MacHandleKeyDownEvent( ev ) ; | |
1438 | break; | |
1439 | case keyUp: | |
1440 | MacHandleKeyUpEvent( ev ) ; | |
1441 | break; | |
1442 | case diskEvt: | |
1443 | MacHandleDiskEvent( ev ) ; | |
1444 | break; | |
1445 | case osEvt: | |
1446 | MacHandleOSEvent( ev ) ; | |
1447 | break; | |
1448 | case kHighLevelEvent: | |
1449 | MacHandleHighLevelEvent( ev ) ; | |
1450 | break; | |
1451 | default: | |
1452 | break; | |
1453 | } | |
c5c9378c | 1454 | #endif |
8461e4c2 | 1455 | wxMacProcessNotifierAndPendingEvents() ; |
169935ad SC |
1456 | } |
1457 | ||
c5c9378c SC |
1458 | #if !TARGET_CARBON |
1459 | bool s_macIsInModalLoop = false ; | |
1460 | ||
1461 | void wxApp::MacHandleModifierEvents( WXEVENTREF evr ) | |
1462 | { | |
1463 | EventRecord* ev = (EventRecord*) evr ; | |
1464 | if ( ev->modifiers != s_lastModifiers && wxWindow::FindFocus() != NULL ) | |
1465 | { | |
1466 | wxKeyEvent event(wxEVT_KEY_DOWN); | |
1467 | ||
1468 | event.m_shiftDown = ev->modifiers & shiftKey; | |
1469 | event.m_controlDown = ev->modifiers & controlKey; | |
1470 | event.m_altDown = ev->modifiers & optionKey; | |
1471 | event.m_metaDown = ev->modifiers & cmdKey; | |
1472 | ||
1473 | event.m_x = ev->where.h; | |
1474 | event.m_y = ev->where.v; | |
1475 | event.m_timeStamp = ev->when; | |
1476 | wxWindow* focus = wxWindow::FindFocus() ; | |
1477 | event.SetEventObject(focus); | |
1478 | ||
1479 | if ( (ev->modifiers ^ s_lastModifiers ) & controlKey ) | |
1480 | { | |
1481 | event.m_keyCode = WXK_CONTROL ; | |
1482 | event.SetEventType( ( ev->modifiers & controlKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ; | |
1483 | focus->GetEventHandler()->ProcessEvent( event ) ; | |
1484 | } | |
1485 | if ( (ev->modifiers ^ s_lastModifiers ) & shiftKey ) | |
1486 | { | |
1487 | event.m_keyCode = WXK_SHIFT ; | |
1488 | event.SetEventType( ( ev->modifiers & shiftKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ; | |
1489 | focus->GetEventHandler()->ProcessEvent( event ) ; | |
1490 | } | |
1491 | if ( (ev->modifiers ^ s_lastModifiers ) & optionKey ) | |
1492 | { | |
1493 | event.m_keyCode = WXK_ALT ; | |
1494 | event.SetEventType( ( ev->modifiers & optionKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ; | |
1495 | focus->GetEventHandler()->ProcessEvent( event ) ; | |
1496 | } | |
d16477fd | 1497 | if ( ( ev->modifiers ^ s_lastModifiers ) & cmdKey ) |
b7aec135 SC |
1498 | { |
1499 | event.m_keyCode = WXK_COMMAND ; | |
1500 | event.SetEventType( ( ev->modifiers & cmdKey ) ? wxEVT_KEY_DOWN : wxEVT_KEY_UP ) ; | |
1501 | focus->GetEventHandler()->ProcessEvent( event ) ; | |
1502 | } | |
c5c9378c SC |
1503 | s_lastModifiers = ev->modifiers ; |
1504 | } | |
1505 | } | |
1506 | ||
76a5e5d2 | 1507 | void wxApp::MacHandleHighLevelEvent( WXEVENTREF evr ) |
169935ad | 1508 | { |
738a6daa SC |
1509 | // we must avoid reentrancy problems when processing high level events eg printing |
1510 | bool former = s_inYield ; | |
1511 | s_inYield = TRUE ; | |
76a5e5d2 | 1512 | EventRecord* ev = (EventRecord*) evr ; |
8461e4c2 | 1513 | ::AEProcessAppleEvent( ev ) ; |
738a6daa | 1514 | s_inYield = former ; |
169935ad SC |
1515 | } |
1516 | ||
76a5e5d2 | 1517 | void wxApp::MacHandleMouseDownEvent( WXEVENTREF evr ) |
169935ad | 1518 | { |
76a5e5d2 | 1519 | EventRecord* ev = (EventRecord*) evr ; |
8461e4c2 | 1520 | wxToolTip::RemoveToolTips() ; |
2f1ae414 | 1521 | |
8461e4c2 | 1522 | WindowRef window; |
72055702 | 1523 | WindowRef frontWindow = ::FrontNonFloatingWindow() ; |
8461e4c2 VZ |
1524 | WindowAttributes frontWindowAttributes = NULL ; |
1525 | if ( frontWindow ) | |
72055702 | 1526 | ::GetWindowAttributes( frontWindow , &frontWindowAttributes ) ; |
9779893b | 1527 | |
8461e4c2 | 1528 | short windowPart = ::FindWindow(ev->where, &window); |
a3722eeb | 1529 | wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ; |
0a67a93b SC |
1530 | if ( wxPendingDelete.Member(win) ) |
1531 | return ; | |
9779893b | 1532 | |
8461e4c2 VZ |
1533 | BitMap screenBits; |
1534 | GetQDGlobalsScreenBits( &screenBits ); | |
1535 | ||
1536 | switch (windowPart) | |
1537 | { | |
1538 | case inMenuBar : | |
1539 | if ( s_macIsInModalLoop ) | |
1540 | { | |
1541 | SysBeep ( 30 ) ; | |
1542 | } | |
1543 | else | |
1544 | { | |
1545 | UInt32 menuresult = MenuSelect(ev->where) ; | |
15b41e90 | 1546 | MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ); |
8461e4c2 VZ |
1547 | s_lastMouseDown = 0; |
1548 | } | |
1549 | break ; | |
8461e4c2 VZ |
1550 | case inSysWindow : |
1551 | SystemClick( ev , window ) ; | |
1552 | s_lastMouseDown = 0; | |
1553 | break ; | |
8461e4c2 VZ |
1554 | case inDrag : |
1555 | if ( window != frontWindow && s_macIsInModalLoop && !(ev->modifiers & cmdKey ) ) | |
1556 | { | |
1557 | SysBeep ( 30 ) ; | |
1558 | } | |
1559 | else | |
1560 | { | |
1561 | DragWindow(window, ev->where, &screenBits.bounds); | |
1562 | if (win) | |
1563 | { | |
1564 | GrafPtr port ; | |
1565 | GetPort( &port ) ; | |
1566 | Point pt = { 0, 0 } ; | |
66a09d47 | 1567 | SetPortWindowPort(window) ; |
8461e4c2 VZ |
1568 | LocalToGlobal( &pt ) ; |
1569 | SetPort( port ) ; | |
1570 | win->SetSize( pt.h , pt.v , -1 , | |
1571 | -1 , wxSIZE_USE_EXISTING); | |
1572 | } | |
1573 | s_lastMouseDown = 0; | |
1574 | } | |
1575 | break ; | |
1576 | case inGoAway: | |
1577 | if (TrackGoAway(window, ev->where)) | |
1578 | { | |
1579 | if ( win ) | |
1580 | win->Close() ; | |
1581 | } | |
1582 | s_lastMouseDown = 0; | |
1583 | break; | |
1584 | case inGrow: | |
1585 | { | |
2b5f62a0 VZ |
1586 | Rect newContentRect ; |
1587 | Rect constraintRect ; | |
1588 | constraintRect.top = win->GetMinHeight() ; | |
1589 | if ( constraintRect.top == -1 ) | |
1590 | constraintRect.top = 0 ; | |
1591 | constraintRect.left = win->GetMinWidth() ; | |
1592 | if ( constraintRect.left == -1 ) | |
1593 | constraintRect.left = 0 ; | |
1594 | constraintRect.right = win->GetMaxWidth() ; | |
1595 | if ( constraintRect.right == -1 ) | |
1596 | constraintRect.right = 32000 ; | |
1597 | constraintRect.bottom = win->GetMaxHeight() ; | |
1598 | if ( constraintRect.bottom == -1 ) | |
1599 | constraintRect.bottom = 32000 ; | |
1600 | ||
1601 | Boolean growResult = ResizeWindow( window , ev->where , | |
1602 | &constraintRect , &newContentRect ) ; | |
1603 | if ( growResult ) | |
8461e4c2 | 1604 | { |
7c9955d1 JS |
1605 | win->SetSize( newContentRect.left , newContentRect.top , |
1606 | newContentRect.right - newContentRect.left , | |
2b5f62a0 | 1607 | newContentRect.bottom - newContentRect.top, wxSIZE_USE_EXISTING); |
8461e4c2 VZ |
1608 | } |
1609 | s_lastMouseDown = 0; | |
1610 | } | |
1611 | break; | |
1612 | case inZoomIn: | |
1613 | case inZoomOut: | |
1614 | if (TrackBox(window, ev->where, windowPart)) | |
1615 | { | |
1616 | // TODO setup size event | |
1617 | ZoomWindow( window , windowPart , false ) ; | |
1618 | if (win) | |
1619 | { | |
1620 | Rect tempRect ; | |
0f493093 SC |
1621 | GrafPtr port ; |
1622 | GetPort( &port ) ; | |
1623 | Point pt = { 0, 0 } ; | |
1624 | SetPortWindowPort(window) ; | |
1625 | LocalToGlobal( &pt ) ; | |
1626 | SetPort( port ) ; | |
8461e4c2 VZ |
1627 | |
1628 | GetWindowPortBounds(window, &tempRect ) ; | |
0f493093 | 1629 | win->SetSize( pt.h , pt.v , tempRect.right-tempRect.left , |
8461e4c2 VZ |
1630 | tempRect.bottom-tempRect.top, wxSIZE_USE_EXISTING); |
1631 | } | |
1632 | } | |
1633 | s_lastMouseDown = 0; | |
1634 | break; | |
1635 | case inCollapseBox : | |
1636 | // TODO setup size event | |
1637 | s_lastMouseDown = 0; | |
1638 | break ; | |
1639 | ||
1640 | case inContent : | |
1641 | { | |
1642 | GrafPtr port ; | |
1643 | GetPort( &port ) ; | |
66a09d47 | 1644 | SetPortWindowPort(window) ; |
8461e4c2 VZ |
1645 | SetPort( port ) ; |
1646 | } | |
1647 | if ( window != frontWindow && wxTheApp->s_captureWindow == NULL ) | |
1648 | { | |
1649 | if ( s_macIsInModalLoop ) | |
1650 | { | |
1651 | SysBeep ( 30 ) ; | |
1652 | } | |
1653 | else if ( UMAIsWindowFloating( window ) ) | |
1654 | { | |
1655 | if ( win ) | |
1656 | win->MacMouseDown( ev , windowPart ) ; | |
1657 | } | |
1658 | else | |
1659 | { | |
827e7a48 RR |
1660 | // Activate window first |
1661 | ::SelectWindow( window ) ; | |
2dbc444a | 1662 | |
827e7a48 | 1663 | // Send event later |
8461e4c2 VZ |
1664 | if ( win ) |
1665 | win->MacMouseDown( ev , windowPart ) ; | |
8461e4c2 VZ |
1666 | } |
1667 | } | |
1668 | else | |
1669 | { | |
1670 | if ( win ) | |
1671 | win->MacMouseDown( ev , windowPart ) ; | |
1672 | } | |
1673 | break ; | |
8461e4c2 VZ |
1674 | default: |
1675 | break; | |
1676 | } | |
169935ad SC |
1677 | } |
1678 | ||
76a5e5d2 | 1679 | void wxApp::MacHandleMouseUpEvent( WXEVENTREF evr ) |
169935ad | 1680 | { |
76a5e5d2 | 1681 | EventRecord* ev = (EventRecord*) evr ; |
8461e4c2 VZ |
1682 | WindowRef window; |
1683 | ||
4f5a3250 | 1684 | short windowPart = inNoWindow ; |
e40298d5 JS |
1685 | if ( wxTheApp->s_captureWindow ) |
1686 | { | |
1687 | window = (WindowRef) s_captureWindow->MacGetRootWindow() ; | |
1688 | windowPart = inContent ; | |
1689 | } | |
1690 | else | |
1691 | { | |
1692 | windowPart = ::FindWindow(ev->where, &window) ; | |
1693 | } | |
8461e4c2 VZ |
1694 | |
1695 | switch (windowPart) | |
1696 | { | |
1697 | case inMenuBar : | |
1698 | break ; | |
1699 | case inSysWindow : | |
1700 | break ; | |
1701 | default: | |
1702 | { | |
a3722eeb | 1703 | wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ; |
8461e4c2 VZ |
1704 | if ( win ) |
1705 | win->MacMouseUp( ev , windowPart ) ; | |
1706 | } | |
1707 | break; | |
1708 | } | |
169935ad SC |
1709 | } |
1710 | ||
7c9955d1 | 1711 | #endif |
15b41e90 | 1712 | |
72055702 | 1713 | long wxMacTranslateKey(unsigned char key, unsigned char code) ; |
7c551d95 | 1714 | long wxMacTranslateKey(unsigned char key, unsigned char code) |
9779893b | 1715 | { |
8461e4c2 | 1716 | long retval = key ; |
9779893b | 1717 | switch (key) |
519cb848 | 1718 | { |
12e049f6 | 1719 | case kHomeCharCode : |
8461e4c2 VZ |
1720 | retval = WXK_HOME; |
1721 | break; | |
12e049f6 | 1722 | case kEnterCharCode : |
8461e4c2 VZ |
1723 | retval = WXK_RETURN; |
1724 | break; | |
12e049f6 | 1725 | case kEndCharCode : |
8461e4c2 VZ |
1726 | retval = WXK_END; |
1727 | break; | |
12e049f6 | 1728 | case kHelpCharCode : |
8461e4c2 VZ |
1729 | retval = WXK_HELP; |
1730 | break; | |
12e049f6 | 1731 | case kBackspaceCharCode : |
8461e4c2 VZ |
1732 | retval = WXK_BACK; |
1733 | break; | |
12e049f6 | 1734 | case kTabCharCode : |
8461e4c2 VZ |
1735 | retval = WXK_TAB; |
1736 | break; | |
12e049f6 | 1737 | case kPageUpCharCode : |
8461e4c2 VZ |
1738 | retval = WXK_PAGEUP; |
1739 | break; | |
12e049f6 | 1740 | case kPageDownCharCode : |
8461e4c2 VZ |
1741 | retval = WXK_PAGEDOWN; |
1742 | break; | |
12e049f6 | 1743 | case kReturnCharCode : |
8461e4c2 VZ |
1744 | retval = WXK_RETURN; |
1745 | break; | |
12e049f6 | 1746 | case kFunctionKeyCharCode : |
8461e4c2 VZ |
1747 | { |
1748 | switch( code ) | |
1749 | { | |
1750 | case 0x7a : | |
1751 | retval = WXK_F1 ; | |
1752 | break; | |
1753 | case 0x78 : | |
1754 | retval = WXK_F2 ; | |
1755 | break; | |
1756 | case 0x63 : | |
1757 | retval = WXK_F3 ; | |
1758 | break; | |
1759 | case 0x76 : | |
1760 | retval = WXK_F4 ; | |
1761 | break; | |
1762 | case 0x60 : | |
1763 | retval = WXK_F5 ; | |
1764 | break; | |
1765 | case 0x61 : | |
1766 | retval = WXK_F6 ; | |
1767 | break; | |
1768 | case 0x62: | |
1769 | retval = WXK_F7 ; | |
1770 | break; | |
1771 | case 0x64 : | |
1772 | retval = WXK_F8 ; | |
1773 | break; | |
1774 | case 0x65 : | |
1775 | retval = WXK_F9 ; | |
1776 | break; | |
1777 | case 0x6D : | |
1778 | retval = WXK_F10 ; | |
1779 | break; | |
1780 | case 0x67 : | |
1781 | retval = WXK_F11 ; | |
1782 | break; | |
1783 | case 0x6F : | |
1784 | retval = WXK_F12 ; | |
1785 | break; | |
1786 | case 0x69 : | |
1787 | retval = WXK_F13 ; | |
1788 | break; | |
1789 | case 0x6B : | |
1790 | retval = WXK_F14 ; | |
1791 | break; | |
1792 | case 0x71 : | |
1793 | retval = WXK_F15 ; | |
1794 | break; | |
1795 | } | |
1796 | } | |
1797 | break ; | |
12e049f6 | 1798 | case kEscapeCharCode : |
8461e4c2 VZ |
1799 | retval = WXK_ESCAPE ; |
1800 | break ; | |
12e049f6 | 1801 | case kLeftArrowCharCode : |
8461e4c2 VZ |
1802 | retval = WXK_LEFT ; |
1803 | break ; | |
12e049f6 | 1804 | case kRightArrowCharCode : |
8461e4c2 VZ |
1805 | retval = WXK_RIGHT ; |
1806 | break ; | |
12e049f6 | 1807 | case kUpArrowCharCode : |
8461e4c2 VZ |
1808 | retval = WXK_UP ; |
1809 | break ; | |
12e049f6 | 1810 | case kDownArrowCharCode : |
8461e4c2 VZ |
1811 | retval = WXK_DOWN ; |
1812 | break ; | |
12e049f6 | 1813 | case kDeleteCharCode : |
8461e4c2 VZ |
1814 | retval = WXK_DELETE ; |
1815 | default: | |
1816 | break ; | |
1817 | } // end switch | |
1818 | ||
1819 | return retval; | |
169935ad SC |
1820 | } |
1821 | ||
6ed892f3 RN |
1822 | int wxKeyCodeToMacModifier(wxKeyCode key) |
1823 | { | |
1824 | switch (key) | |
1825 | { | |
1826 | case WXK_START: | |
15c2acd2 | 1827 | case WXK_MENU: |
6ed892f3 RN |
1828 | return cmdKey; |
1829 | ||
1830 | case WXK_SHIFT: | |
1831 | return shiftKey; | |
1832 | ||
1833 | case WXK_CAPITAL: | |
1834 | return alphaLock; | |
1835 | ||
15c2acd2 RN |
1836 | case WXK_ALT: |
1837 | return optionKey; | |
6ed892f3 RN |
1838 | |
1839 | case WXK_CONTROL: | |
1840 | return controlKey; | |
1841 | ||
1842 | default: | |
1843 | return 0; | |
1844 | } | |
1845 | } | |
1846 | ||
1847 | bool wxGetKeyState(wxKeyCode key) //virtual key code if < 10.2.x, else see below | |
1848 | { | |
7f71c4c8 RN |
1849 | //#ifdef __DARWIN__ |
1850 | // wxHIDKeyboard keyboard; | |
1851 | // return keyboard.IsActive(key); | |
1852 | //#else | |
104cdef6 | 1853 | // TODO: Have it use HID Manager on OSX... |
6ed892f3 RN |
1854 | //if OS X > 10.2 (i.e. 10.2.x) |
1855 | //a known apple bug prevents the system from determining led | |
1856 | //states with GetKeys... can only determine caps lock led | |
1857 | return !!(GetCurrentKeyModifiers() & wxKeyCodeToMacModifier(key)); | |
1858 | //else | |
1859 | // KeyMapByteArray keymap; | |
1860 | // GetKeys((BigEndianLong*)keymap); | |
1861 | // return !!(BitTst(keymap, (sizeof(KeyMapByteArray)*8) - iKey)); | |
7f71c4c8 | 1862 | //#endif |
6ed892f3 RN |
1863 | } |
1864 | ||
c5c9378c | 1865 | #if !TARGET_CARBON |
76a5e5d2 | 1866 | void wxApp::MacHandleKeyDownEvent( WXEVENTREF evr ) |
169935ad | 1867 | { |
76a5e5d2 | 1868 | EventRecord* ev = (EventRecord*) evr ; |
8461e4c2 VZ |
1869 | wxToolTip::RemoveToolTips() ; |
1870 | ||
1871 | UInt32 menuresult = UMAMenuEvent(ev) ; | |
1872 | if ( HiWord( menuresult ) ) | |
1873 | { | |
1874 | if ( !s_macIsInModalLoop ) | |
ac2d1ca6 | 1875 | MacHandleMenuSelect( HiWord( menuresult ) , LoWord( menuresult ) ) ; |
8461e4c2 VZ |
1876 | } |
1877 | else | |
1878 | { | |
12e049f6 | 1879 | wxWindow* focus = wxWindow::FindFocus() ; |
7c9955d1 | 1880 | |
12e049f6 | 1881 | if ( MacSendKeyDownEvent( focus , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) == false ) |
ac2d1ca6 | 1882 | { |
1ea39a03 SC |
1883 | #if 0 |
1884 | // we must handle control keys the other way round, otherwise text content is updated too late | |
ac2d1ca6 SC |
1885 | // has not been handled -> perform default |
1886 | wxControl* control = wxDynamicCast( focus , wxControl ) ; | |
1887 | if ( control && control->GetMacControl() != NULL ) | |
8461e4c2 | 1888 | { |
12e049f6 SC |
1889 | short keycode ; |
1890 | short keychar ; | |
1891 | keychar = short(ev->message & charCodeMask); | |
1892 | keycode = short(ev->message & keyCodeMask) >> 8 ; | |
ac2d1ca6 SC |
1893 | ::HandleControlKey( (ControlHandle) control->GetMacControl() , keycode , keychar , ev->modifiers ) ; |
1894 | } | |
1ea39a03 | 1895 | #endif |
ac2d1ca6 SC |
1896 | } |
1897 | } | |
1898 | } | |
1899 | ||
c5c9378c SC |
1900 | void wxApp::MacHandleKeyUpEvent( WXEVENTREF evr ) |
1901 | { | |
1902 | EventRecord* ev = (EventRecord*) evr ; | |
1903 | wxToolTip::RemoveToolTips() ; | |
1904 | ||
1905 | UInt32 menuresult = UMAMenuEvent(ev) ; | |
1906 | if ( HiWord( menuresult ) ) | |
1907 | { | |
1908 | } | |
1909 | else | |
1910 | { | |
1911 | MacSendKeyUpEvent( wxWindow::FindFocus() , ev->message , ev->modifiers , ev->when , ev->where.h , ev->where.v ) ; | |
1912 | } | |
1913 | } | |
1914 | ||
1915 | #endif | |
1916 | ||
12e049f6 | 1917 | bool wxApp::MacSendKeyDownEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey ) |
ac2d1ca6 | 1918 | { |
ba12463b SC |
1919 | if ( !focus ) |
1920 | return false ; | |
7c9955d1 | 1921 | |
12e049f6 SC |
1922 | short keycode ; |
1923 | short keychar ; | |
1924 | keychar = short(keymessage & charCodeMask); | |
1925 | keycode = short(keymessage & keyCodeMask) >> 8 ; | |
7c9955d1 | 1926 | |
ef08713a | 1927 | if ( modifiers & ( controlKey|shiftKey|optionKey ) ) |
12e049f6 SC |
1928 | { |
1929 | // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier | |
1930 | // and look at the character after | |
1931 | UInt32 state = 0; | |
ef08713a | 1932 | UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state); |
12e049f6 SC |
1933 | keychar = short(keyInfo & charCodeMask); |
1934 | keycode = short(keyInfo & keyCodeMask) >> 8 ; | |
1935 | } | |
1936 | long keyval = wxMacTranslateKey(keychar, keycode) ; | |
e40298d5 JS |
1937 | long realkeyval = keyval ; |
1938 | if ( keyval == keychar ) | |
1939 | { | |
1940 | // we are not on a special character combo -> pass the real os event-value to EVT_CHAR, but not to EVT_KEY (make upper first) | |
1941 | realkeyval = short(keymessage & charCodeMask) ; | |
1942 | keyval = wxToupper( keyval ) ; | |
1943 | } | |
7c9955d1 | 1944 | |
ac2d1ca6 | 1945 | wxKeyEvent event(wxEVT_KEY_DOWN); |
41d368a4 | 1946 | bool handled = false ; |
ac2d1ca6 SC |
1947 | event.m_shiftDown = modifiers & shiftKey; |
1948 | event.m_controlDown = modifiers & controlKey; | |
1949 | event.m_altDown = modifiers & optionKey; | |
1950 | event.m_metaDown = modifiers & cmdKey; | |
ef08713a | 1951 | event.m_keyCode = keyval ; |
ac2d1ca6 SC |
1952 | |
1953 | event.m_x = wherex; | |
1954 | event.m_y = wherey; | |
1955 | event.m_timeStamp = when; | |
1956 | event.SetEventObject(focus); | |
1957 | handled = focus->GetEventHandler()->ProcessEvent( event ) ; | |
1958 | if ( handled && event.GetSkipped() ) | |
1959 | handled = false ; | |
1960 | if ( !handled ) | |
1961 | { | |
ba2928c6 | 1962 | #if wxUSE_ACCEL |
ac2d1ca6 SC |
1963 | if (!handled) |
1964 | { | |
1965 | wxWindow *ancestor = focus; | |
1966 | while (ancestor) | |
1967 | { | |
1968 | int command = ancestor->GetAcceleratorTable()->GetCommand( event ); | |
1969 | if (command != -1) | |
8461e4c2 | 1970 | { |
ac2d1ca6 SC |
1971 | wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command ); |
1972 | handled = ancestor->GetEventHandler()->ProcessEvent( command_event ); | |
1973 | break; | |
8461e4c2 | 1974 | } |
ac2d1ca6 SC |
1975 | if (ancestor->IsTopLevel()) |
1976 | break; | |
1977 | ancestor = ancestor->GetParent(); | |
8461e4c2 | 1978 | } |
ac2d1ca6 SC |
1979 | } |
1980 | #endif // wxUSE_ACCEL | |
1981 | } | |
1982 | if (!handled) | |
1983 | { | |
1984 | event.Skip( FALSE ) ; | |
1985 | event.SetEventType( wxEVT_CHAR ) ; | |
12e049f6 | 1986 | // raw value again |
ef08713a | 1987 | event.m_keyCode = realkeyval ; |
ac2d1ca6 SC |
1988 | |
1989 | handled = focus->GetEventHandler()->ProcessEvent( event ) ; | |
1990 | if ( handled && event.GetSkipped() ) | |
1991 | handled = false ; | |
1992 | } | |
1993 | if ( !handled && | |
1994 | (keyval == WXK_TAB) && | |
35a8698b SC |
1995 | // CS: copied the change below from wxGTK |
1996 | // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here the control may | |
1997 | // have this style, yet choose not to process this particular TAB in which | |
1998 | // case TAB must still work as a navigational character | |
1999 | #if 0 | |
ac2d1ca6 | 2000 | (!focus->HasFlag(wxTE_PROCESS_TAB)) && |
35a8698b | 2001 | #endif |
ac2d1ca6 SC |
2002 | (focus->GetParent()) && |
2003 | (focus->GetParent()->HasFlag( wxTAB_TRAVERSAL)) ) | |
2004 | { | |
2005 | wxNavigationKeyEvent new_event; | |
2006 | new_event.SetEventObject( focus ); | |
2007 | new_event.SetDirection( !event.ShiftDown() ); | |
2008 | /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */ | |
2009 | new_event.SetWindowChange( event.ControlDown() ); | |
2010 | new_event.SetCurrentFocus( focus ); | |
2011 | handled = focus->GetEventHandler()->ProcessEvent( new_event ); | |
2012 | if ( handled && new_event.GetSkipped() ) | |
2013 | handled = false ; | |
2014 | } | |
2015 | // backdoor handler for default return and command escape | |
2016 | if ( !handled && (!focus->IsKindOf(CLASSINFO(wxControl) ) || !focus->MacCanFocus() ) ) | |
2017 | { | |
2018 | // if window is not having a focus still testing for default enter or cancel | |
2019 | // TODO add the UMA version for ActiveNonFloatingWindow | |
2020 | wxWindow* focus = wxFindWinFromMacWindow( FrontWindow() ) ; | |
e562df9b SC |
2021 | if ( focus ) |
2022 | { | |
2023 | if ( keyval == WXK_RETURN ) | |
2024 | { | |
2025 | wxButton *def = wxDynamicCast(focus->GetDefaultItem(), | |
2026 | wxButton); | |
2027 | if ( def && def->IsEnabled() ) | |
2028 | { | |
2029 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); | |
2030 | event.SetEventObject(def); | |
2031 | def->Command(event); | |
ac2d1ca6 | 2032 | return true ; |
e562df9b SC |
2033 | } |
2034 | } | |
8461e4c2 | 2035 | /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */ |
ac2d1ca6 | 2036 | else if (keyval == WXK_ESCAPE || (keyval == '.' && modifiers & cmdKey ) ) |
8461e4c2 | 2037 | { |
e562df9b SC |
2038 | wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL); |
2039 | new_event.SetEventObject( focus ); | |
2040 | handled = focus->GetEventHandler()->ProcessEvent( new_event ); | |
8461e4c2 | 2041 | } |
e562df9b | 2042 | } |
8461e4c2 | 2043 | } |
ac2d1ca6 | 2044 | return handled ; |
169935ad SC |
2045 | } |
2046 | ||
12e049f6 | 2047 | bool wxApp::MacSendKeyUpEvent( wxWindow* focus , long keymessage , long modifiers , long when , short wherex , short wherey ) |
ac2d1ca6 | 2048 | { |
ba12463b SC |
2049 | if ( !focus ) |
2050 | return false ; | |
2051 | ||
12e049f6 SC |
2052 | short keycode ; |
2053 | short keychar ; | |
2054 | keychar = short(keymessage & charCodeMask); | |
2055 | keycode = short(keymessage & keyCodeMask) >> 8 ; | |
ef08713a | 2056 | if ( modifiers & ( controlKey|shiftKey|optionKey ) ) |
12e049f6 SC |
2057 | { |
2058 | // control interferes with some built-in keys like pgdown, return etc. therefore we remove the controlKey modifier | |
2059 | // and look at the character after | |
2060 | UInt32 state = 0; | |
ef08713a | 2061 | UInt32 keyInfo = KeyTranslate((Ptr)GetScriptManagerVariable(smKCHRCache), ( modifiers & (~(controlKey|shiftKey|optionKey))) | keycode, &state); |
12e049f6 SC |
2062 | keychar = short(keyInfo & charCodeMask); |
2063 | keycode = short(keyInfo & keyCodeMask) >> 8 ; | |
2064 | } | |
2065 | long keyval = wxMacTranslateKey(keychar, keycode) ; | |
2066 | ||
e40298d5 JS |
2067 | if ( keyval == keychar ) |
2068 | { | |
7c9955d1 | 2069 | keyval = wxToupper( keyval ) ; |
e40298d5 | 2070 | } |
ac2d1ca6 | 2071 | bool handled = false ; |
ac2d1ca6 | 2072 | |
ba12463b SC |
2073 | wxKeyEvent event(wxEVT_KEY_UP); |
2074 | event.m_shiftDown = modifiers & shiftKey; | |
2075 | event.m_controlDown = modifiers & controlKey; | |
2076 | event.m_altDown = modifiers & optionKey; | |
2077 | event.m_metaDown = modifiers & cmdKey; | |
ef08713a | 2078 | event.m_keyCode = keyval ; |
ba12463b SC |
2079 | |
2080 | event.m_x = wherex; | |
2081 | event.m_y = wherey; | |
2082 | event.m_timeStamp = when; | |
2083 | event.SetEventObject(focus); | |
2084 | handled = focus->GetEventHandler()->ProcessEvent( event ) ; | |
2085 | ||
ac2d1ca6 SC |
2086 | return handled ; |
2087 | } | |
c5c9378c | 2088 | |
15b41e90 | 2089 | #if !TARGET_CARBON |
76a5e5d2 | 2090 | void wxApp::MacHandleActivateEvent( WXEVENTREF evr ) |
169935ad | 2091 | { |
76a5e5d2 | 2092 | EventRecord* ev = (EventRecord*) evr ; |
8461e4c2 VZ |
2093 | WindowRef window = (WindowRef) ev->message ; |
2094 | if ( window ) | |
2095 | { | |
2096 | bool activate = (ev->modifiers & activeFlag ) ; | |
2097 | WindowClass wclass ; | |
72055702 | 2098 | ::GetWindowClass ( window , &wclass ) ; |
8461e4c2 VZ |
2099 | if ( wclass == kFloatingWindowClass ) |
2100 | { | |
2101 | // if it is a floater we activate/deactivate the front non-floating window instead | |
72055702 | 2102 | window = ::FrontNonFloatingWindow() ; |
8461e4c2 | 2103 | } |
a3722eeb | 2104 | wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ; |
8461e4c2 | 2105 | if ( win ) |
15b41e90 | 2106 | win->MacActivate( ev->when , activate ) ; |
8461e4c2 | 2107 | } |
169935ad SC |
2108 | } |
2109 | ||
76a5e5d2 | 2110 | void wxApp::MacHandleUpdateEvent( WXEVENTREF evr ) |
169935ad | 2111 | { |
76a5e5d2 | 2112 | EventRecord* ev = (EventRecord*) evr ; |
8461e4c2 | 2113 | WindowRef window = (WindowRef) ev->message ; |
a3722eeb | 2114 | wxTopLevelWindowMac * win = wxFindWinFromMacWindow( window ) ; |
8461e4c2 VZ |
2115 | if ( win ) |
2116 | { | |
0a67a93b | 2117 | if ( !wxPendingDelete.Member(win) ) |
a3722eeb | 2118 | win->MacUpdate( ev->when ) ; |
8461e4c2 VZ |
2119 | } |
2120 | else | |
2121 | { | |
2122 | // since there is no way of telling this foreign window to update itself | |
2123 | // we have to invalidate the update region otherwise we keep getting the same | |
2124 | // event over and over again | |
2125 | BeginUpdate( window ) ; | |
2126 | EndUpdate( window ) ; | |
2127 | } | |
169935ad SC |
2128 | } |
2129 | ||
76a5e5d2 | 2130 | void wxApp::MacHandleDiskEvent( WXEVENTREF evr ) |
169935ad | 2131 | { |
76a5e5d2 | 2132 | EventRecord* ev = (EventRecord*) evr ; |
8461e4c2 | 2133 | if ( HiWord( ev->message ) != noErr ) |
519cb848 | 2134 | { |
8461e4c2 VZ |
2135 | OSErr err ; |
2136 | Point point ; | |
2137 | SetPt( &point , 100 , 100 ) ; | |
9779893b | 2138 | |
8461e4c2 VZ |
2139 | err = DIBadMount( point , ev->message ) ; |
2140 | wxASSERT( err == noErr ) ; | |
8461e4c2 | 2141 | } |
169935ad SC |
2142 | } |
2143 | ||
76a5e5d2 | 2144 | void wxApp::MacHandleOSEvent( WXEVENTREF evr ) |
169935ad | 2145 | { |
76a5e5d2 | 2146 | EventRecord* ev = (EventRecord*) evr ; |
8461e4c2 VZ |
2147 | switch( ( ev->message & osEvtMessageMask ) >> 24 ) |
2148 | { | |
2149 | case suspendResumeMessage : | |
2150 | { | |
2151 | bool isResuming = ev->message & resumeFlag ; | |
8461e4c2 | 2152 | bool convertClipboard = ev->message & convertClipboardFlag ; |
15b41e90 | 2153 | |
8461e4c2 VZ |
2154 | bool doesActivate = UMAGetProcessModeDoesActivateOnFGSwitch() ; |
2155 | if ( isResuming ) | |
2156 | { | |
2157 | WindowRef oldFrontWindow = NULL ; | |
2158 | WindowRef newFrontWindow = NULL ; | |
2159 | ||
2160 | // in case we don't take care of activating ourselves, we have to synchronize | |
2161 | // our idea of the active window with the process manager's - which it already activated | |
2162 | ||
2163 | if ( !doesActivate ) | |
72055702 | 2164 | oldFrontWindow = ::FrontNonFloatingWindow() ; |
8461e4c2 VZ |
2165 | |
2166 | MacResume( convertClipboard ) ; | |
2167 | ||
72055702 | 2168 | newFrontWindow = ::FrontNonFloatingWindow() ; |
8461e4c2 VZ |
2169 | |
2170 | if ( oldFrontWindow ) | |
2171 | { | |
a3722eeb | 2172 | wxTopLevelWindowMac* win = wxFindWinFromMacWindow( oldFrontWindow ) ; |
8461e4c2 | 2173 | if ( win ) |
15b41e90 | 2174 | win->MacActivate( ev->when , false ) ; |
8461e4c2 VZ |
2175 | } |
2176 | if ( newFrontWindow ) | |
2177 | { | |
a3722eeb | 2178 | wxTopLevelWindowMac* win = wxFindWinFromMacWindow( newFrontWindow ) ; |
8461e4c2 | 2179 | if ( win ) |
15b41e90 | 2180 | win->MacActivate( ev->when , true ) ; |
8461e4c2 VZ |
2181 | } |
2182 | } | |
2183 | else | |
2184 | { | |
2185 | MacSuspend( convertClipboard ) ; | |
8461e4c2 VZ |
2186 | } |
2187 | } | |
2188 | break ; | |
2189 | case mouseMovedMessage : | |
2190 | { | |
2191 | WindowRef window; | |
2192 | ||
2193 | wxWindow* currentMouseWindow = NULL ; | |
2194 | ||
e40298d5 JS |
2195 | if (s_captureWindow ) |
2196 | { | |
2197 | currentMouseWindow = s_captureWindow ; | |
2198 | } | |
2199 | else | |
2200 | { | |
2201 | wxWindow::MacGetWindowFromPoint( wxPoint( ev->where.h , ev->where.v ) , | |
2202 | ¤tMouseWindow ) ; | |
2203 | } | |
12e049f6 | 2204 | |
8461e4c2 VZ |
2205 | if ( currentMouseWindow != wxWindow::s_lastMouseWindow ) |
2206 | { | |
2207 | wxMouseEvent event ; | |
2208 | ||
2209 | bool isDown = !(ev->modifiers & btnState) ; // 1 is for up | |
2210 | bool controlDown = ev->modifiers & controlKey ; // for simulating right mouse | |
2211 | ||
2212 | event.m_leftDown = isDown && !controlDown; | |
2213 | event.m_middleDown = FALSE; | |
2214 | event.m_rightDown = isDown && controlDown; | |
2215 | event.m_shiftDown = ev->modifiers & shiftKey; | |
2216 | event.m_controlDown = ev->modifiers & controlKey; | |
2217 | event.m_altDown = ev->modifiers & optionKey; | |
2218 | event.m_metaDown = ev->modifiers & cmdKey; | |
2219 | event.m_x = ev->where.h; | |
2220 | event.m_y = ev->where.v; | |
2221 | event.m_timeStamp = ev->when; | |
2222 | event.SetEventObject(this); | |
84c1ffa9 | 2223 | |
8461e4c2 VZ |
2224 | if ( wxWindow::s_lastMouseWindow ) |
2225 | { | |
ed60b502 RR |
2226 | wxMouseEvent eventleave(event); |
2227 | eventleave.SetEventType( wxEVT_LEAVE_WINDOW ); | |
2228 | wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y ); | |
d1bdc41d | 2229 | eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ; |
84c1ffa9 | 2230 | |
8461e4c2 VZ |
2231 | wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave); |
2232 | } | |
2233 | if ( currentMouseWindow ) | |
2234 | { | |
ed60b502 RR |
2235 | wxMouseEvent evententer(event); |
2236 | evententer.SetEventType( wxEVT_ENTER_WINDOW ); | |
2237 | currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y ); | |
d1bdc41d | 2238 | evententer.SetEventObject( currentMouseWindow ) ; |
8461e4c2 VZ |
2239 | currentMouseWindow->GetEventHandler()->ProcessEvent(evententer); |
2240 | } | |
2241 | wxWindow::s_lastMouseWindow = currentMouseWindow ; | |
2242 | } | |
2243 | ||
12e049f6 SC |
2244 | short windowPart = inNoWindow ; |
2245 | ||
e40298d5 JS |
2246 | if ( s_captureWindow ) |
2247 | { | |
2248 | window = (WindowRef) s_captureWindow->MacGetRootWindow() ; | |
2249 | windowPart = inContent ; | |
2250 | } | |
2251 | else | |
2252 | { | |
7c9955d1 | 2253 | windowPart = ::FindWindow(ev->where, &window); |
e40298d5 | 2254 | } |
7c9955d1 | 2255 | |
8461e4c2 VZ |
2256 | switch (windowPart) |
2257 | { | |
49a9ce1b | 2258 | case inContent : |
8461e4c2 | 2259 | { |
a3722eeb | 2260 | wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ; |
8461e4c2 VZ |
2261 | if ( win ) |
2262 | win->MacMouseMoved( ev , windowPart ) ; | |
2263 | else | |
49a9ce1b SC |
2264 | { |
2265 | if ( wxIsBusy() ) | |
2266 | { | |
2267 | } | |
2268 | else | |
2269 | UMAShowArrowCursor(); | |
2270 | } | |
8461e4c2 VZ |
2271 | } |
2272 | break; | |
49a9ce1b SC |
2273 | default : |
2274 | { | |
2275 | if ( wxIsBusy() ) | |
2276 | { | |
2277 | } | |
2278 | else | |
2279 | UMAShowArrowCursor(); | |
2280 | } | |
2281 | break ; | |
8461e4c2 VZ |
2282 | } |
2283 | } | |
2284 | break ; | |
2285 | ||
2286 | } | |
169935ad | 2287 | } |
3c589c7f SC |
2288 | #else |
2289 | ||
2290 | void wxApp::MacHandleMouseMovedEvent(wxInt32 x , wxInt32 y ,wxUint32 modifiers , long timestamp) | |
2291 | { | |
2292 | WindowRef window; | |
2293 | ||
2294 | wxWindow* currentMouseWindow = NULL ; | |
2295 | ||
2296 | if (s_captureWindow ) | |
2297 | { | |
2298 | currentMouseWindow = s_captureWindow ; | |
2299 | } | |
2300 | else | |
2301 | { | |
2302 | wxWindow::MacGetWindowFromPoint( wxPoint( x, y ) , ¤tMouseWindow ) ; | |
2303 | } | |
2304 | ||
2305 | if ( currentMouseWindow != wxWindow::s_lastMouseWindow ) | |
2306 | { | |
2307 | wxMouseEvent event ; | |
2308 | ||
2309 | bool isDown = !(modifiers & btnState) ; // 1 is for up | |
2310 | bool controlDown = modifiers & controlKey ; // for simulating right mouse | |
2311 | ||
2312 | event.m_leftDown = isDown && !controlDown; | |
2313 | ||
2314 | event.m_middleDown = FALSE; | |
2315 | event.m_rightDown = isDown && controlDown; | |
2316 | ||
2317 | event.m_shiftDown = modifiers & shiftKey; | |
2318 | event.m_controlDown = modifiers & controlKey; | |
2319 | event.m_altDown = modifiers & optionKey; | |
2320 | event.m_metaDown = modifiers & cmdKey; | |
2dbc444a | 2321 | |
3c589c7f SC |
2322 | event.m_x = x; |
2323 | event.m_y = y; | |
2324 | event.m_timeStamp = timestamp; | |
2325 | ||
2326 | if ( wxWindow::s_lastMouseWindow ) | |
2327 | { | |
2328 | wxMouseEvent eventleave(event); | |
2329 | eventleave.SetEventType( wxEVT_LEAVE_WINDOW ); | |
2330 | wxWindow::s_lastMouseWindow->ScreenToClient( &eventleave.m_x, &eventleave.m_y ); | |
2331 | eventleave.SetEventObject( wxWindow::s_lastMouseWindow ) ; | |
2332 | ||
2333 | #if wxUSE_TOOLTIPS | |
2334 | wxToolTip::RelayEvent( wxWindow::s_lastMouseWindow , eventleave); | |
2335 | #endif // wxUSE_TOOLTIPS | |
2336 | wxWindow::s_lastMouseWindow->GetEventHandler()->ProcessEvent(eventleave); | |
2337 | } | |
2338 | if ( currentMouseWindow ) | |
2339 | { | |
2340 | wxMouseEvent evententer(event); | |
2341 | evententer.SetEventType( wxEVT_ENTER_WINDOW ); | |
2342 | currentMouseWindow->ScreenToClient( &evententer.m_x, &evententer.m_y ); | |
2343 | evententer.SetEventObject( currentMouseWindow ) ; | |
2344 | #if wxUSE_TOOLTIPS | |
2345 | wxToolTip::RelayEvent( currentMouseWindow , evententer); | |
2346 | #endif // wxUSE_TOOLTIPS | |
2347 | currentMouseWindow->GetEventHandler()->ProcessEvent(evententer); | |
2348 | } | |
2349 | wxWindow::s_lastMouseWindow = currentMouseWindow ; | |
2350 | } | |
2351 | ||
2352 | short windowPart = inNoWindow ; | |
2353 | ||
2354 | if ( s_captureWindow ) | |
2355 | { | |
2356 | window = (WindowRef) s_captureWindow->MacGetRootWindow() ; | |
2357 | windowPart = inContent ; | |
2358 | } | |
2359 | else | |
2360 | { | |
2361 | Point pt= { y , x } ; | |
2362 | windowPart = ::FindWindow(pt , &window); | |
2363 | } | |
2364 | ||
2365 | switch (windowPart) | |
2366 | { | |
2367 | case inContent : | |
2368 | { | |
2369 | wxTopLevelWindowMac* win = wxFindWinFromMacWindow( window ) ; | |
2370 | if ( win ) | |
2371 | win->MacFireMouseEvent( nullEvent , x , y , modifiers , timestamp ) ; | |
2372 | else | |
2373 | { | |
2374 | if ( wxIsBusy() ) | |
2375 | { | |
2376 | } | |
2377 | else | |
2378 | UMAShowArrowCursor(); | |
2379 | } | |
2380 | } | |
2381 | break; | |
2382 | default : | |
2383 | { | |
2384 | if ( wxIsBusy() ) | |
2385 | { | |
2386 | } | |
2387 | else | |
2388 | UMAShowArrowCursor(); | |
2389 | } | |
2390 | break ; | |
2391 | } | |
2392 | } | |
15b41e90 SC |
2393 | #endif |
2394 | ||
2395 | void wxApp::MacHandleMenuCommand( wxUint32 id ) | |
2396 | { | |
a01fe3d6 RD |
2397 | wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar() ; |
2398 | wxFrame* frame = mbar->GetFrame(); | |
2399 | wxCHECK_RET( mbar != NULL && frame != NULL, wxT("error in menu item callback") ); | |
2400 | if ( frame ) | |
2401 | { | |
2402 | frame->ProcessCommand(id); | |
2403 | } | |
15b41e90 | 2404 | } |
169935ad | 2405 | |
15b41e90 | 2406 | #if !TARGET_CARBON |
519cb848 | 2407 | void wxApp::MacHandleMenuSelect( int macMenuId , int macMenuItemNum ) |
169935ad | 2408 | { |
8461e4c2 VZ |
2409 | if (macMenuId == 0) |
2410 | return; // no menu item selected | |
2411 | ||
2412 | if (macMenuId == kwxMacAppleMenuId && macMenuItemNum > 1) | |
2413 | { | |
2414 | #if ! TARGET_CARBON | |
2415 | Str255 deskAccessoryName ; | |
2416 | GrafPtr savedPort ; | |
2417 | ||
2418 | GetMenuItemText(GetMenuHandle(kwxMacAppleMenuId), macMenuItemNum, deskAccessoryName); | |
2419 | GetPort(&savedPort); | |
2420 | OpenDeskAcc(deskAccessoryName); | |
2421 | SetPort(savedPort); | |
2422 | #endif | |
2423 | } | |
2424 | else | |
2425 | { | |
2b5f62a0 VZ |
2426 | MenuCommand id ; |
2427 | GetMenuItemCommandID( GetMenuHandle(macMenuId) , macMenuItemNum , &id ) ; | |
e40298d5 | 2428 | MacHandleMenuCommand( id ) ; |
8461e4c2 VZ |
2429 | } |
2430 | HiliteMenu(0); | |
169935ad | 2431 | } |
653a3ed9 | 2432 | #endif |