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