]>
Commit | Line | Data |
---|---|---|
83df96d6 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: app.cpp | |
3 | // Purpose: wxApp | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "app.h" | |
14 | #endif | |
15 | ||
83df96d6 JS |
16 | #include "wx/frame.h" |
17 | #include "wx/app.h" | |
18 | #include "wx/utils.h" | |
19 | #include "wx/gdicmn.h" | |
83df96d6 | 20 | #include "wx/icon.h" |
83df96d6 | 21 | #include "wx/dialog.h" |
83df96d6 JS |
22 | #include "wx/log.h" |
23 | #include "wx/module.h" | |
24 | #include "wx/memory.h" | |
25 | #include "wx/log.h" | |
26 | #include "wx/intl.h" | |
1b0fb34b | 27 | #include "wx/evtloop.h" |
83df96d6 | 28 | |
ef8c973b VS |
29 | #include "wx/univ/theme.h" |
30 | #include "wx/univ/renderer.h" | |
31 | ||
83df96d6 JS |
32 | #if wxUSE_THREADS |
33 | #include "wx/thread.h" | |
34 | #endif | |
35 | ||
36 | #if wxUSE_WX_RESOURCES | |
37 | #include "wx/resource.h" | |
38 | #endif | |
39 | ||
40 | #ifdef __VMS__ | |
41 | #pragma message disable nosimpint | |
42 | #endif | |
83df96d6 JS |
43 | #include <X11/Xlib.h> |
44 | #include <X11/Xutil.h> | |
83df96d6 | 45 | #include <X11/Xatom.h> |
256d631a | 46 | |
83df96d6 JS |
47 | #ifdef __VMS__ |
48 | #pragma message enable nosimpint | |
49 | #endif | |
50 | ||
7eaac9f5 | 51 | #include "wx/x11/private.h" |
83df96d6 JS |
52 | |
53 | #include <string.h> | |
54 | ||
83df96d6 JS |
55 | extern wxList wxPendingDelete; |
56 | ||
57 | wxApp *wxTheApp = NULL; | |
58 | ||
59 | wxHashTable *wxWidgetHashTable = NULL; | |
60 | ||
61 | IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler) | |
62 | ||
63 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) | |
64 | EVT_IDLE(wxApp::OnIdle) | |
65 | END_EVENT_TABLE() | |
66 | ||
67 | #ifdef __WXDEBUG__ | |
1b0fb34b | 68 | typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *); |
83df96d6 | 69 | |
1b0fb34b | 70 | XErrorHandlerFunc gs_pfnXErrorHandler = 0; |
83df96d6 | 71 | |
1b0fb34b JS |
72 | static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent) |
73 | { | |
74 | // just forward to the default handler for now | |
75 | return gs_pfnXErrorHandler(dpy, xevent); | |
76 | } | |
83df96d6 JS |
77 | #endif // __WXDEBUG__ |
78 | ||
79 | long wxApp::sm_lastMessageTime = 0; | |
a11672a4 | 80 | WXDisplay *wxApp::ms_display = NULL; |
83df96d6 | 81 | |
256d631a JS |
82 | // This is set within wxEntryStart -- too early on |
83 | // to put these in wxTheApp | |
84 | static int g_newArgc = 0; | |
85 | static wxChar** g_newArgv = NULL; | |
86 | static bool g_showIconic = FALSE; | |
87 | static wxSize g_initialSize = wxDefaultSize; | |
88 | ||
83df96d6 JS |
89 | bool wxApp::Initialize() |
90 | { | |
83df96d6 JS |
91 | wxClassInfo::InitializeClasses(); |
92 | ||
93 | // GL: I'm annoyed ... I don't know where to put this and I don't want to | |
94 | // create a module for that as it's part of the core. | |
95 | #if wxUSE_THREADS | |
96 | wxPendingEventsLocker = new wxCriticalSection(); | |
97 | #endif | |
98 | ||
99 | wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING); | |
100 | wxTheColourDatabase->Initialize(); | |
101 | ||
102 | wxInitializeStockLists(); | |
103 | wxInitializeStockObjects(); | |
104 | ||
105 | #if wxUSE_WX_RESOURCES | |
106 | wxInitializeResourceSystem(); | |
107 | #endif | |
108 | ||
83df96d6 JS |
109 | wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER); |
110 | ||
111 | wxModule::RegisterModules(); | |
112 | if (!wxModule::InitializeModules()) return FALSE; | |
113 | ||
114 | return TRUE; | |
115 | } | |
116 | ||
117 | void wxApp::CleanUp() | |
118 | { | |
256d631a JS |
119 | if (g_newArgv) |
120 | delete[] g_newArgv; | |
121 | g_newArgv = NULL; | |
122 | ||
83df96d6 JS |
123 | delete wxWidgetHashTable; |
124 | wxWidgetHashTable = NULL; | |
125 | ||
126 | wxModule::CleanUpModules(); | |
127 | ||
128 | #if wxUSE_WX_RESOURCES | |
129 | wxCleanUpResourceSystem(); | |
130 | #endif | |
131 | ||
83df96d6 JS |
132 | delete wxTheColourDatabase; |
133 | wxTheColourDatabase = NULL; | |
134 | ||
a11672a4 RR |
135 | wxDeleteStockObjects(); |
136 | ||
137 | wxDeleteStockLists(); | |
138 | ||
139 | delete wxTheApp; | |
140 | wxTheApp = NULL; | |
83df96d6 | 141 | |
83df96d6 JS |
142 | wxClassInfo::CleanUpClasses(); |
143 | ||
83df96d6 JS |
144 | #if wxUSE_THREADS |
145 | delete wxPendingEvents; | |
146 | delete wxPendingEventsLocker; | |
147 | #endif | |
148 | ||
149 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT | |
150 | // At this point we want to check if there are any memory | |
151 | // blocks that aren't part of the wxDebugContext itself, | |
152 | // as a special case. Then when dumping we need to ignore | |
153 | // wxDebugContext, too. | |
154 | if (wxDebugContext::CountObjectsLeft(TRUE) > 0) | |
155 | { | |
ba696cfa | 156 | wxLogDebug("There were memory leaks."); |
83df96d6 JS |
157 | wxDebugContext::Dump(); |
158 | wxDebugContext::PrintStatistics(); | |
159 | } | |
160 | #endif | |
161 | ||
162 | // do it as the very last thing because everything else can log messages | |
163 | wxLog::DontCreateOnDemand(); | |
164 | // do it as the very last thing because everything else can log messages | |
165 | delete wxLog::SetActiveTarget(NULL); | |
166 | } | |
167 | ||
a11672a4 RR |
168 | // NB: argc and argv may be changed here, pass by reference! |
169 | int wxEntryStart( int& argc, char *argv[] ) | |
170 | { | |
171 | #ifdef __WXDEBUG__ | |
172 | // install the X error handler | |
173 | gs_pfnXErrorHandler = XSetErrorHandler( wxXErrorHandler ); | |
174 | #endif // __WXDEBUG__ | |
175 | ||
256d631a JS |
176 | wxString displayName; |
177 | bool syncDisplay = FALSE; | |
178 | ||
3a0b23eb | 179 | // Parse the arguments. |
256d631a JS |
180 | // We can't use wxCmdLineParser or OnInitCmdLine and friends because |
181 | // we have to create the Display earlier. If we can find a way to | |
182 | // use the wxAppBase API then I'll be quite happy to change it. | |
183 | g_newArgv = new wxChar*[argc]; | |
184 | g_newArgc = 0; | |
185 | int i; | |
186 | for (i = 0; i < argc; i++) | |
187 | { | |
188 | wxString arg(argv[i]); | |
189 | if (arg == wxT("-display")) | |
190 | { | |
191 | if (i < (argc - 1)) | |
192 | { | |
193 | i ++; | |
194 | displayName = argv[i]; | |
195 | continue; | |
196 | } | |
197 | } | |
198 | else if (arg == wxT("-geometry")) | |
199 | { | |
200 | if (i < (argc - 1)) | |
201 | { | |
202 | i ++; | |
eb90fb3e | 203 | wxString windowGeometry = argv[i]; |
256d631a JS |
204 | int w, h; |
205 | if (wxSscanf(windowGeometry.c_str(), _T("%dx%d"), &w, &h) != 2) | |
206 | { | |
eb90fb3e | 207 | wxLogError(_("Invalid geometry specification '%s'"), windowGeometry.c_str()); |
256d631a JS |
208 | } |
209 | else | |
210 | { | |
211 | g_initialSize = wxSize(w, h); | |
212 | } | |
213 | continue; | |
214 | } | |
215 | } | |
216 | else if (arg == wxT("-sync")) | |
217 | { | |
218 | syncDisplay = TRUE; | |
219 | continue; | |
220 | } | |
221 | else if (arg == wxT("-iconic")) | |
222 | { | |
223 | g_showIconic = TRUE; | |
45ff6421 | 224 | |
256d631a JS |
225 | continue; |
226 | } | |
227 | ||
228 | // Not eaten by wxWindows, so pass through | |
229 | g_newArgv[g_newArgc] = argv[i]; | |
230 | g_newArgc ++; | |
231 | } | |
232 | ||
233 | Display* xdisplay; | |
234 | if (displayName.IsEmpty()) | |
235 | xdisplay = XOpenDisplay(NULL); | |
236 | else | |
237 | xdisplay = XOpenDisplay(displayName); | |
a11672a4 RR |
238 | |
239 | if (!xdisplay) | |
240 | { | |
241 | wxLogError( _("wxWindows could not open display. Exiting.") ); | |
242 | return -1; | |
243 | } | |
256d631a JS |
244 | |
245 | if (syncDisplay) | |
246 | { | |
247 | XSynchronize(xdisplay, True); | |
248 | } | |
a11672a4 RR |
249 | |
250 | wxApp::ms_display = (WXDisplay*) xdisplay; | |
251 | ||
252 | XSelectInput( xdisplay, XDefaultRootWindow(xdisplay), PropertyChangeMask); | |
253 | ||
254 | // wxSetDetectableAutoRepeat( TRUE ); | |
255 | ||
256 | if (!wxApp::Initialize()) | |
257 | return -1; | |
258 | ||
259 | return 0; | |
260 | } | |
261 | ||
a11672a4 RR |
262 | int wxEntryInitGui() |
263 | { | |
264 | int retValue = 0; | |
265 | ||
266 | if ( !wxTheApp->OnInitGui() ) | |
267 | retValue = -1; | |
268 | ||
269 | return retValue; | |
270 | } | |
271 | ||
272 | ||
83df96d6 JS |
273 | int wxEntry( int argc, char *argv[] ) |
274 | { | |
275 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT | |
276 | // This seems to be necessary since there are 'rogue' | |
277 | // objects present at this point (perhaps global objects?) | |
278 | // Setting a checkpoint will ignore them as far as the | |
279 | // memory checking facility is concerned. | |
280 | // Of course you may argue that memory allocated in globals should be | |
281 | // checked, but this is a reasonable compromise. | |
282 | wxDebugContext::SetCheckpoint(); | |
283 | #endif | |
a11672a4 RR |
284 | int err = wxEntryStart(argc, argv); |
285 | if (err) | |
286 | return err; | |
83df96d6 JS |
287 | |
288 | if (!wxTheApp) | |
289 | { | |
290 | if (!wxApp::GetInitializerFunction()) | |
291 | { | |
292 | printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" ); | |
293 | return 0; | |
294 | }; | |
295 | ||
296 | wxTheApp = (wxApp*) (* wxApp::GetInitializerFunction()) (); | |
297 | }; | |
298 | ||
299 | if (!wxTheApp) | |
300 | { | |
301 | printf( "wxWindows error: wxTheApp == NULL\n" ); | |
302 | return 0; | |
303 | }; | |
304 | ||
305 | wxTheApp->SetClassName(wxFileNameFromPath(argv[0])); | |
306 | wxTheApp->SetAppName(wxFileNameFromPath(argv[0])); | |
307 | ||
45ff6421 JS |
308 | // The command line may have been changed |
309 | // by stripping out -display etc. | |
310 | if (g_newArgc > 0) | |
311 | { | |
312 | wxTheApp->argc = g_newArgc; | |
313 | wxTheApp->argv = g_newArgv; | |
314 | } | |
315 | else | |
316 | { | |
317 | wxTheApp->argc = argc; | |
318 | wxTheApp->argv = argv; | |
319 | } | |
256d631a JS |
320 | wxTheApp->m_showIconic = g_showIconic; |
321 | wxTheApp->m_initialSize = g_initialSize; | |
83df96d6 | 322 | |
a11672a4 RR |
323 | int retValue; |
324 | retValue = wxEntryInitGui(); | |
83df96d6 JS |
325 | |
326 | // Here frames insert themselves automatically into wxTopLevelWindows by | |
327 | // getting created in OnInit(). | |
a11672a4 RR |
328 | if ( retValue == 0 ) |
329 | { | |
330 | if ( !wxTheApp->OnInit() ) | |
331 | retValue = -1; | |
332 | } | |
83df96d6 | 333 | |
a11672a4 | 334 | if ( retValue == 0 ) |
83df96d6 JS |
335 | { |
336 | if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun(); | |
337 | } | |
338 | ||
339 | // flush the logged messages if any | |
340 | wxLog *pLog = wxLog::GetActiveTarget(); | |
341 | if ( pLog != NULL && pLog->HasPendingMessages() ) | |
342 | pLog->Flush(); | |
343 | ||
344 | delete wxLog::SetActiveTarget(new wxLogStderr); // So dialog boxes aren't used | |
345 | // for further messages | |
346 | ||
347 | if (wxTheApp->GetTopWindow()) | |
348 | { | |
349 | delete wxTheApp->GetTopWindow(); | |
350 | wxTheApp->SetTopWindow(NULL); | |
351 | } | |
352 | ||
353 | wxTheApp->DeletePendingObjects(); | |
354 | ||
355 | wxTheApp->OnExit(); | |
356 | ||
357 | wxApp::CleanUp(); | |
358 | ||
359 | return retValue; | |
360 | }; | |
361 | ||
362 | // Static member initialization | |
363 | wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL; | |
364 | ||
365 | wxApp::wxApp() | |
366 | { | |
367 | m_topWindow = NULL; | |
368 | wxTheApp = this; | |
369 | m_className = ""; | |
370 | m_wantDebugOutput = TRUE ; | |
371 | m_appName = ""; | |
372 | argc = 0; | |
373 | argv = NULL; | |
374 | m_exitOnFrameDelete = TRUE; | |
83df96d6 | 375 | m_mainColormap = (WXColormap) NULL; |
7eaac9f5 | 376 | m_topLevelWidget = (WXWindow) NULL; |
83df96d6 | 377 | m_maxRequestSize = 0; |
1b0fb34b | 378 | m_mainLoop = NULL; |
256d631a JS |
379 | m_showIconic = FALSE; |
380 | m_initialSize = wxDefaultSize; | |
83df96d6 JS |
381 | } |
382 | ||
383 | bool wxApp::Initialized() | |
384 | { | |
385 | if (GetTopWindow()) | |
386 | return TRUE; | |
387 | else | |
388 | return FALSE; | |
389 | } | |
390 | ||
391 | int wxApp::MainLoop() | |
392 | { | |
1b0fb34b JS |
393 | int rt; |
394 | m_mainLoop = new wxEventLoop; | |
83df96d6 | 395 | |
1b0fb34b | 396 | rt = m_mainLoop->Run(); |
83df96d6 | 397 | |
1b0fb34b JS |
398 | delete m_mainLoop; |
399 | m_mainLoop = NULL; | |
400 | return rt; | |
401 | } | |
83df96d6 | 402 | |
1b0fb34b JS |
403 | // Processes an X event. |
404 | void wxApp::ProcessXEvent(WXEvent* _event) | |
405 | { | |
406 | XEvent* event = (XEvent*) _event; | |
83df96d6 | 407 | |
1b0fb34b JS |
408 | wxWindow* win = NULL; |
409 | Window window = event->xany.window; | |
410 | Window actualWindow = window; | |
4125131b | 411 | |
1b0fb34b | 412 | // Find the first wxWindow that corresponds to this event window |
774b90fb JS |
413 | // Because we're receiving events after a window |
414 | // has been destroyed, assume a 1:1 match between | |
415 | // Window and wxWindow, so if it's not in the table, | |
416 | // it must have been destroyed. | |
417 | ||
418 | win = wxGetWindowFromTable(window); | |
419 | if (!win) | |
45ff6421 | 420 | return; |
83df96d6 | 421 | |
1b0fb34b JS |
422 | switch (event->type) |
423 | { | |
424 | case KeyPress: | |
83df96d6 | 425 | { |
b513212d JS |
426 | if (win && !win->IsEnabled()) |
427 | return; | |
428 | ||
1b0fb34b JS |
429 | { |
430 | if (win) | |
431 | { | |
432 | wxKeyEvent keyEvent(wxEVT_KEY_DOWN); | |
7266b672 | 433 | wxTranslateKeyEvent(keyEvent, win, window, event); |
ba696cfa RR |
434 | |
435 | wxLogDebug( "OnKey from %s", win->GetName().c_str() ); | |
1b0fb34b JS |
436 | |
437 | // We didn't process wxEVT_KEY_DOWN, so send | |
7266b672 | 438 | // wxEVT_CHAR |
b513212d | 439 | if (!win->GetEventHandler()->ProcessEvent( keyEvent )) |
1b0fb34b | 440 | { |
7266b672 | 441 | keyEvent.SetEventType(wxEVT_CHAR); |
b513212d | 442 | win->GetEventHandler()->ProcessEvent( keyEvent ); |
1b0fb34b JS |
443 | } |
444 | ||
445 | // We intercepted and processed the key down event | |
446 | return; | |
447 | } | |
448 | } | |
449 | return; | |
83df96d6 | 450 | } |
1b0fb34b | 451 | case KeyRelease: |
7eaac9f5 | 452 | { |
b513212d JS |
453 | if (win && !win->IsEnabled()) |
454 | return; | |
455 | ||
1b0fb34b JS |
456 | if (win) |
457 | { | |
458 | wxKeyEvent keyEvent(wxEVT_KEY_UP); | |
459 | wxTranslateKeyEvent(keyEvent, win, window, event); | |
460 | ||
b513212d | 461 | win->GetEventHandler()->ProcessEvent( keyEvent ); |
1b0fb34b | 462 | } |
83df96d6 | 463 | return; |
7eaac9f5 | 464 | } |
256d631a JS |
465 | case ConfigureNotify: |
466 | { | |
467 | // Not clear if this is the same in NanoX | |
468 | if (win) | |
469 | { | |
470 | wxSizeEvent sizeEvent( wxSize(event->xconfigure.width,event->xconfigure.height), win->GetId() ); | |
471 | sizeEvent.SetEventObject( win ); | |
472 | ||
473 | win->GetEventHandler()->ProcessEvent( sizeEvent ); | |
474 | } | |
475 | } | |
476 | #if !wxUSE_NANOX | |
1b0fb34b | 477 | case PropertyNotify: |
7eaac9f5 | 478 | { |
1b0fb34b | 479 | HandlePropertyChange(_event); |
83df96d6 | 480 | return; |
7eaac9f5 | 481 | } |
b513212d | 482 | case ClientMessage: |
3a0b23eb JS |
483 | { |
484 | if (win && !win->IsEnabled()) | |
485 | return; | |
486 | ||
b513212d JS |
487 | Atom wm_delete_window = XInternAtom(wxGlobalDisplay(), "WM_DELETE_WINDOW", True);; |
488 | Atom wm_protocols = XInternAtom(wxGlobalDisplay(), "WM_PROTOCOLS", True);; | |
489 | ||
490 | if (event->xclient.message_type == wm_protocols) | |
491 | { | |
6a44bffd | 492 | if ((Atom) (event->xclient.data.l[0]) == wm_delete_window) |
b513212d JS |
493 | { |
494 | if (win) | |
495 | { | |
496 | win->Close(FALSE); | |
497 | } | |
498 | } | |
499 | } | |
500 | return; | |
501 | } | |
1b0fb34b | 502 | case ResizeRequest: |
7eaac9f5 | 503 | { |
256d631a | 504 | /* |
1b0fb34b JS |
505 | * If resize event, don't resize until the last resize event for this |
506 | * window is recieved. Prevents flicker as windows are resized. | |
507 | */ | |
508 | ||
7266b672 | 509 | Display *disp = (Display*) wxGetDisplay(); |
1b0fb34b JS |
510 | XEvent report; |
511 | ||
512 | // to avoid flicker | |
513 | report = * event; | |
514 | while( XCheckTypedWindowEvent (disp, actualWindow, ResizeRequest, &report)); | |
515 | ||
1b0fb34b JS |
516 | if (win) |
517 | { | |
518 | wxSize sz = win->GetSize(); | |
519 | wxSizeEvent sizeEvent(sz, win->GetId()); | |
520 | sizeEvent.SetEventObject(win); | |
521 | ||
b513212d | 522 | win->GetEventHandler()->ProcessEvent( sizeEvent ); |
1b0fb34b JS |
523 | } |
524 | ||
7eaac9f5 JS |
525 | return; |
526 | } | |
256d631a JS |
527 | #endif |
528 | #if wxUSE_NANOX | |
529 | case GR_EVENT_TYPE_CLOSE_REQ: | |
530 | { | |
531 | if (win) | |
532 | { | |
533 | win->Close(FALSE); | |
534 | } | |
535 | break; | |
536 | } | |
537 | #endif | |
1b0fb34b | 538 | case Expose: |
7eaac9f5 | 539 | { |
1b0fb34b JS |
540 | if (win) |
541 | { | |
1934d291 RR |
542 | win->GetUpdateRegion().Union( event->xexpose.x, event->xexpose.y, |
543 | event->xexpose.width, event->xexpose.height); | |
ba696cfa RR |
544 | |
545 | win->GetClearRegion().Union( event->xexpose.x, event->xexpose.y, | |
546 | event->xexpose.width, event->xexpose.height); | |
547 | ||
548 | // if (event->xexpose.count == 0) | |
549 | // win->Update(); | |
1b0fb34b JS |
550 | } |
551 | ||
7eaac9f5 JS |
552 | return; |
553 | } | |
4125131b RR |
554 | case GraphicsExpose: |
555 | { | |
556 | if (win) | |
557 | { | |
558 | wxLogDebug( "GraphicsExpose from %s", win->GetName().c_str(), | |
559 | event->xgraphicsexpose.x, event->xgraphicsexpose.y, | |
560 | event->xgraphicsexpose.width, event->xgraphicsexpose.height); | |
561 | ||
562 | win->GetUpdateRegion().Union( event->xgraphicsexpose.x, event->xgraphicsexpose.y, | |
563 | event->xgraphicsexpose.width, event->xgraphicsexpose.height); | |
564 | ||
565 | win->GetClearRegion().Union( event->xgraphicsexpose.x, event->xgraphicsexpose.y, | |
566 | event->xgraphicsexpose.width, event->xgraphicsexpose.height); | |
567 | ||
568 | // if (event->xgraphicsexpose.count == 0) | |
569 | // win->Update(); | |
570 | } | |
571 | ||
572 | return; | |
573 | } | |
1b0fb34b JS |
574 | case EnterNotify: |
575 | case LeaveNotify: | |
576 | case ButtonPress: | |
577 | case ButtonRelease: | |
578 | case MotionNotify: | |
7eaac9f5 | 579 | { |
b513212d JS |
580 | if (win && !win->IsEnabled()) |
581 | return; | |
582 | ||
1b0fb34b JS |
583 | if (win) |
584 | { | |
585 | wxMouseEvent wxevent; | |
586 | wxTranslateMouseEvent(wxevent, win, window, event); | |
b513212d | 587 | win->GetEventHandler()->ProcessEvent( wxevent ); |
1b0fb34b | 588 | } |
7eaac9f5 JS |
589 | return; |
590 | } | |
1b0fb34b JS |
591 | case FocusIn: |
592 | { | |
256d631a | 593 | #if !wxUSE_NANOX |
1b0fb34b | 594 | if (win && event->xfocus.detail != NotifyPointer) |
256d631a | 595 | #endif |
1b0fb34b | 596 | { |
ba696cfa RR |
597 | wxLogDebug( "FocusIn from %s", win->GetName().c_str() ); |
598 | ||
1b0fb34b JS |
599 | wxFocusEvent focusEvent(wxEVT_SET_FOCUS, win->GetId()); |
600 | focusEvent.SetEventObject(win); | |
b513212d | 601 | win->GetEventHandler()->ProcessEvent(focusEvent); |
1b0fb34b JS |
602 | } |
603 | break; | |
604 | } | |
605 | case FocusOut: | |
606 | { | |
256d631a | 607 | #if !wxUSE_NANOX |
1b0fb34b | 608 | if (win && event->xfocus.detail != NotifyPointer) |
256d631a | 609 | #endif |
1b0fb34b | 610 | { |
4125131b | 611 | wxLogDebug( "FocusOut from %s", win->GetName().c_str() ); |
ba696cfa | 612 | |
1b0fb34b JS |
613 | wxFocusEvent focusEvent(wxEVT_KILL_FOCUS, win->GetId()); |
614 | focusEvent.SetEventObject(win); | |
b513212d | 615 | win->GetEventHandler()->ProcessEvent(focusEvent); |
1b0fb34b JS |
616 | } |
617 | break; | |
618 | } | |
b513212d JS |
619 | case DestroyNotify: |
620 | { | |
621 | // Do we want to process this (for top-level windows)? | |
622 | // But we want to be able to veto closes, anyway | |
623 | break; | |
624 | } | |
1b0fb34b JS |
625 | default: |
626 | { | |
45ff6421 JS |
627 | #ifdef __WXDEBUG__ |
628 | //wxString eventName = wxGetXEventName(XEvent& event); | |
629 | //wxLogDebug(wxT("Event %s not handled"), eventName.c_str()); | |
630 | #endif | |
1b0fb34b JS |
631 | break; |
632 | } | |
83df96d6 JS |
633 | } |
634 | } | |
635 | ||
636 | // Returns TRUE if more time is needed. | |
1b0fb34b JS |
637 | // Note that this duplicates wxEventLoopImpl::SendIdleEvent |
638 | // but ProcessIdle may be needed by apps, so is kept. | |
83df96d6 JS |
639 | bool wxApp::ProcessIdle() |
640 | { | |
641 | wxIdleEvent event; | |
642 | event.SetEventObject(this); | |
643 | ProcessEvent(event); | |
644 | ||
645 | return event.MoreRequested(); | |
646 | } | |
647 | ||
648 | void wxApp::ExitMainLoop() | |
649 | { | |
1b0fb34b JS |
650 | if (m_mainLoop) |
651 | m_mainLoop->Exit(0); | |
83df96d6 JS |
652 | } |
653 | ||
654 | // Is a message/event pending? | |
655 | bool wxApp::Pending() | |
656 | { | |
1b0fb34b | 657 | return wxEventLoop::GetActive()->Pending(); |
83df96d6 JS |
658 | } |
659 | ||
660 | // Dispatch a message. | |
661 | void wxApp::Dispatch() | |
662 | { | |
1b0fb34b | 663 | wxEventLoop::GetActive()->Dispatch(); |
83df96d6 JS |
664 | } |
665 | ||
666 | // This should be redefined in a derived class for | |
667 | // handling property change events for XAtom IPC. | |
668 | void wxApp::HandlePropertyChange(WXEvent *event) | |
669 | { | |
670 | // by default do nothing special | |
7eaac9f5 | 671 | // TODO: what to do for X11 |
256d631a | 672 | // XtDispatchEvent((XEvent*) event); |
83df96d6 JS |
673 | } |
674 | ||
675 | void wxApp::OnIdle(wxIdleEvent& event) | |
676 | { | |
0d1dff01 | 677 | static bool s_inOnIdle = FALSE; |
83df96d6 JS |
678 | |
679 | // Avoid recursion (via ProcessEvent default case) | |
0d1dff01 | 680 | if (s_inOnIdle) |
83df96d6 JS |
681 | return; |
682 | ||
0d1dff01 | 683 | s_inOnIdle = TRUE; |
83df96d6 | 684 | |
0d1dff01 RR |
685 | // Resend in the main thread events which have been prepared in other |
686 | // threads | |
83df96d6 JS |
687 | ProcessPendingEvents(); |
688 | ||
0d1dff01 | 689 | // 'Garbage' collection of windows deleted with Close() |
83df96d6 JS |
690 | DeletePendingObjects(); |
691 | ||
83df96d6 JS |
692 | // Send OnIdle events to all windows |
693 | bool needMore = SendIdleEvents(); | |
694 | ||
695 | if (needMore) | |
696 | event.RequestMore(TRUE); | |
697 | ||
0d1dff01 | 698 | s_inOnIdle = FALSE; |
83df96d6 JS |
699 | } |
700 | ||
701 | void wxWakeUpIdle() | |
702 | { | |
703 | // **** please implement me! **** | |
704 | // Wake up the idle handler processor, even if it is in another thread... | |
705 | } | |
706 | ||
707 | ||
708 | // Send idle event to all top-level windows | |
709 | bool wxApp::SendIdleEvents() | |
710 | { | |
711 | bool needMore = FALSE; | |
712 | ||
713 | wxWindowList::Node* node = wxTopLevelWindows.GetFirst(); | |
714 | while (node) | |
715 | { | |
716 | wxWindow* win = node->GetData(); | |
717 | if (SendIdleEvents(win)) | |
718 | needMore = TRUE; | |
719 | node = node->GetNext(); | |
720 | } | |
721 | ||
722 | return needMore; | |
723 | } | |
724 | ||
725 | // Send idle event to window and all subwindows | |
726 | bool wxApp::SendIdleEvents(wxWindow* win) | |
727 | { | |
728 | bool needMore = FALSE; | |
729 | ||
730 | wxIdleEvent event; | |
731 | event.SetEventObject(win); | |
0d1dff01 RR |
732 | |
733 | win->GetEventHandler()->ProcessEvent(event); | |
734 | ||
735 | win->OnInternalIdle(); | |
83df96d6 JS |
736 | |
737 | if (event.MoreRequested()) | |
738 | needMore = TRUE; | |
739 | ||
740 | wxNode* node = win->GetChildren().First(); | |
741 | while (node) | |
742 | { | |
743 | wxWindow* win = (wxWindow*) node->Data(); | |
744 | if (SendIdleEvents(win)) | |
745 | needMore = TRUE; | |
746 | ||
747 | node = node->Next(); | |
748 | } | |
0d1dff01 RR |
749 | |
750 | return needMore; | |
83df96d6 JS |
751 | } |
752 | ||
753 | void wxApp::DeletePendingObjects() | |
754 | { | |
755 | wxNode *node = wxPendingDelete.First(); | |
756 | while (node) | |
757 | { | |
758 | wxObject *obj = (wxObject *)node->Data(); | |
759 | ||
760 | delete obj; | |
761 | ||
762 | if (wxPendingDelete.Member(obj)) | |
763 | delete node; | |
764 | ||
765 | // Deleting one object may have deleted other pending | |
766 | // objects, so start from beginning of list again. | |
767 | node = wxPendingDelete.First(); | |
768 | } | |
769 | } | |
770 | ||
256d631a | 771 | // Create display, and other initialization |
83df96d6 JS |
772 | bool wxApp::OnInitGui() |
773 | { | |
ca7497c2 JS |
774 | // Eventually this line will be removed, but for |
775 | // now we don't want to try popping up a dialog | |
776 | // for error messages. | |
777 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
a11672a4 | 778 | |
ea596687 JS |
779 | if (!wxAppBase::OnInitGui()) |
780 | return FALSE; | |
781 | ||
a11672a4 | 782 | GetMainColormap( wxApp::GetDisplay() ); |
256d631a | 783 | |
a11672a4 | 784 | m_maxRequestSize = XMaxRequestSize( (Display*) wxApp::GetDisplay() ); |
83df96d6 JS |
785 | |
786 | return TRUE; | |
787 | } | |
788 | ||
789 | WXColormap wxApp::GetMainColormap(WXDisplay* display) | |
790 | { | |
791 | if (!display) /* Must be called first with non-NULL display */ | |
792 | return m_mainColormap; | |
793 | ||
794 | int defaultScreen = DefaultScreen((Display*) display); | |
795 | Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen); | |
796 | ||
797 | Colormap c = DefaultColormapOfScreen(screen); | |
798 | ||
799 | if (!m_mainColormap) | |
800 | m_mainColormap = (WXColormap) c; | |
801 | ||
802 | return (WXColormap) c; | |
803 | } | |
804 | ||
8354aa92 | 805 | Window wxGetWindowParent(Window window) |
7eaac9f5 | 806 | { |
86fd8bda RR |
807 | wxASSERT_MSG( window, "invalid window" ); |
808 | ||
809 | return (Window) 0; | |
810 | ||
7eaac9f5 JS |
811 | Window parent, root = 0; |
812 | unsigned int noChildren = 0; | |
ea596687 JS |
813 | Window* children = NULL; |
814 | int res = XQueryTree((Display*) wxGetDisplay(), window, & root, & parent, | |
815 | & children, & noChildren); | |
816 | if (children) | |
817 | XFree(children); | |
818 | if (res) | |
7eaac9f5 JS |
819 | return parent; |
820 | else | |
821 | return (Window) 0; | |
822 | } | |
823 | ||
83df96d6 JS |
824 | void wxExit() |
825 | { | |
826 | int retValue = 0; | |
827 | if (wxTheApp) | |
828 | retValue = wxTheApp->OnExit(); | |
829 | ||
830 | wxApp::CleanUp(); | |
831 | /* | |
832 | * Exit in some platform-specific way. Not recommended that the app calls this: | |
833 | * only for emergencies. | |
834 | */ | |
835 | exit(retValue); | |
836 | } | |
837 | ||
838 | // Yield to other processes | |
839 | ||
840 | bool wxApp::Yield(bool onlyIfNeeded) | |
841 | { | |
842 | bool s_inYield = FALSE; | |
843 | ||
844 | if ( s_inYield ) | |
845 | { | |
846 | if ( !onlyIfNeeded ) | |
847 | { | |
848 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
849 | } | |
850 | ||
851 | return FALSE; | |
852 | } | |
853 | ||
854 | s_inYield = TRUE; | |
855 | ||
856 | while (wxTheApp && wxTheApp->Pending()) | |
857 | wxTheApp->Dispatch(); | |
858 | ||
859 | s_inYield = FALSE; | |
860 | ||
861 | return TRUE; | |
862 | } | |
863 | ||
ef8c973b | 864 | wxIcon wxApp::GetStdIcon(int which) const |
83df96d6 | 865 | { |
ef8c973b | 866 | return wxTheme::Get()->GetRenderer()->GetStdIcon(which); |
83df96d6 JS |
867 | } |
868 | ||
7edcafa4 JS |
869 | void wxApp::OnAssert(const wxChar *file, int line, const wxChar *msg) |
870 | { | |
871 | // While the GUI isn't working that well, just print out the | |
872 | // message. | |
873 | #if 0 | |
874 | wxAppBase::OnAssert(file, line, msg); | |
875 | #else | |
876 | wxString msg2; | |
877 | msg2.Printf("At file %s:%d: %s", file, line, msg); | |
878 | wxLogDebug(msg2); | |
879 | #endif | |
880 | } | |
881 |