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