]>
Commit | Line | Data |
---|---|---|
4bb6408c 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 | |
afb74891 | 9 | // Licence: wxWindows licence |
4bb6408c JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
ee31c392 | 13 | #pragma implementation "app.h" |
4bb6408c JS |
14 | #endif |
15 | ||
bcd055ae JJ |
16 | #ifdef __VMS |
17 | #define XtParent XTPARENT | |
18 | #define XtDisplay XTDISPLAY | |
19 | #endif | |
20 | ||
4bb6408c JS |
21 | #include "wx/frame.h" |
22 | #include "wx/app.h" | |
23 | #include "wx/utils.h" | |
24 | #include "wx/gdicmn.h" | |
25 | #include "wx/pen.h" | |
26 | #include "wx/brush.h" | |
27 | #include "wx/cursor.h" | |
28 | #include "wx/icon.h" | |
29 | #include "wx/palette.h" | |
30 | #include "wx/dc.h" | |
31 | #include "wx/dialog.h" | |
32 | #include "wx/msgdlg.h" | |
33 | #include "wx/log.h" | |
34 | #include "wx/module.h" | |
35 | #include "wx/memory.h" | |
8bedcdce RR |
36 | #include "wx/log.h" |
37 | #include "wx/intl.h" | |
b412f9be | 38 | |
7bcb11d3 | 39 | #if wxUSE_THREADS |
dfe1eee3 | 40 | #include "wx/thread.h" |
7bcb11d3 | 41 | #endif |
4bb6408c | 42 | |
47d67540 | 43 | #if wxUSE_WX_RESOURCES |
9c6e1335 | 44 | #include "wx/resource.h" |
4bb6408c JS |
45 | #endif |
46 | ||
338dd992 JJ |
47 | #ifdef __VMS__ |
48 | #pragma message disable nosimpint | |
49 | #endif | |
4bb6408c JS |
50 | #include <Xm/Xm.h> |
51 | #include <X11/Xlib.h> | |
52 | #include <X11/Xutil.h> | |
53 | #include <X11/Xresource.h> | |
54 | #include <X11/Xatom.h> | |
338dd992 JJ |
55 | #ifdef __VMS__ |
56 | #pragma message enable nosimpint | |
57 | #endif | |
4bb6408c JS |
58 | |
59 | #include "wx/motif/private.h" | |
60 | ||
61 | #include <string.h> | |
62 | ||
4bb6408c | 63 | extern wxList wxPendingDelete; |
7491d644 | 64 | |
4bb6408c JS |
65 | wxApp *wxTheApp = NULL; |
66 | ||
67 | wxHashTable *wxWidgetHashTable = NULL; | |
68 | ||
4bb6408c | 69 | IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler) |
9c6e1335 | 70 | |
4bb6408c | 71 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) |
9c6e1335 | 72 | EVT_IDLE(wxApp::OnIdle) |
4bb6408c | 73 | END_EVENT_TABLE() |
4bb6408c | 74 | |
e838cc14 VZ |
75 | #ifdef __WXDEBUG__ |
76 | typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *); | |
77 | ||
78 | XErrorHandlerFunc gs_pfnXErrorHandler = 0; | |
79 | ||
80 | static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent) | |
81 | { | |
82 | // just forward to the default handler for now | |
83 | return gs_pfnXErrorHandler(dpy, xevent); | |
84 | } | |
85 | #endif // __WXDEBUG__ | |
86 | ||
4bb6408c JS |
87 | long wxApp::sm_lastMessageTime = 0; |
88 | ||
89 | bool wxApp::Initialize() | |
90 | { | |
4bb6408c | 91 | wxClassInfo::InitializeClasses(); |
afb74891 | 92 | |
ee31c392 | 93 | // GL: I'm annoyed ... I don't know where to put this and I don't want to |
4d3a259a GL |
94 | // create a module for that as it's part of the core. |
95 | #if wxUSE_THREADS | |
4d3a259a GL |
96 | wxPendingEventsLocker = new wxCriticalSection(); |
97 | #endif | |
98 | ||
4bb6408c JS |
99 | wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING); |
100 | wxTheColourDatabase->Initialize(); | |
afb74891 | 101 | |
4b5f3fe6 | 102 | wxInitializeStockLists(); |
4bb6408c | 103 | wxInitializeStockObjects(); |
afb74891 | 104 | |
47d67540 | 105 | #if wxUSE_WX_RESOURCES |
4bb6408c JS |
106 | wxInitializeResourceSystem(); |
107 | #endif | |
afb74891 | 108 | |
2d120f83 | 109 | // For PostScript printing |
47d67540 | 110 | #if wxUSE_POSTSCRIPT |
2d120f83 | 111 | /* Done using wxModule now |
4bb6408c JS |
112 | wxInitializePrintSetupData(); |
113 | wxThePrintPaperDatabase = new wxPrintPaperDatabase; | |
114 | wxThePrintPaperDatabase->CreateDatabase(); | |
2d120f83 | 115 | */ |
4bb6408c | 116 | #endif |
afb74891 | 117 | |
4bb6408c | 118 | wxBitmap::InitStandardHandlers(); |
afb74891 | 119 | |
4bb6408c | 120 | wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER); |
afb74891 | 121 | |
4bb6408c | 122 | wxModule::RegisterModules(); |
aaa38880 | 123 | if (!wxModule::InitializeModules()) return FALSE; |
afb74891 | 124 | |
4bb6408c JS |
125 | return TRUE; |
126 | } | |
127 | ||
128 | void wxApp::CleanUp() | |
129 | { | |
130 | delete wxWidgetHashTable; | |
131 | wxWidgetHashTable = NULL; | |
afb74891 | 132 | |
4bb6408c | 133 | wxModule::CleanUpModules(); |
afb74891 | 134 | |
47d67540 | 135 | #if wxUSE_WX_RESOURCES |
4bb6408c JS |
136 | wxCleanUpResourceSystem(); |
137 | #endif | |
afb74891 | 138 | |
4bb6408c | 139 | wxDeleteStockObjects() ; |
afb74891 | 140 | |
4bb6408c | 141 | // Destroy all GDI lists, etc. |
afb74891 | 142 | |
7ecb8b06 | 143 | wxDeleteStockLists(); |
afb74891 | 144 | |
4bb6408c JS |
145 | delete wxTheColourDatabase; |
146 | wxTheColourDatabase = NULL; | |
afb74891 | 147 | |
47d67540 | 148 | #if wxUSE_POSTSCRIPT |
2d120f83 | 149 | /* Done using wxModule now |
4bb6408c JS |
150 | wxInitializePrintSetupData(FALSE); |
151 | delete wxThePrintPaperDatabase; | |
152 | wxThePrintPaperDatabase = NULL; | |
2d120f83 | 153 | */ |
4bb6408c | 154 | #endif |
afb74891 | 155 | |
4bb6408c | 156 | wxBitmap::CleanUpHandlers(); |
afb74891 | 157 | |
4bb6408c | 158 | wxClassInfo::CleanUpClasses(); |
afb74891 | 159 | |
184b5d99 JS |
160 | delete wxTheApp; |
161 | wxTheApp = NULL; | |
afb74891 | 162 | |
4d3a259a GL |
163 | // GL: I'm annoyed ... I don't know where to put this and I don't want to |
164 | // create a module for that as it's part of the core. | |
165 | #if wxUSE_THREADS | |
166 | delete wxPendingEvents; | |
167 | delete wxPendingEventsLocker; | |
168 | #endif | |
169 | ||
184b5d99 JS |
170 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT |
171 | // At this point we want to check if there are any memory | |
172 | // blocks that aren't part of the wxDebugContext itself, | |
173 | // as a special case. Then when dumping we need to ignore | |
174 | // wxDebugContext, too. | |
4fabb575 | 175 | if (wxDebugContext::CountObjectsLeft(TRUE) > 0) |
184b5d99 | 176 | { |
2d120f83 JS |
177 | wxLogDebug("There were memory leaks.\n"); |
178 | wxDebugContext::Dump(); | |
179 | wxDebugContext::PrintStatistics(); | |
184b5d99 | 180 | } |
184b5d99 | 181 | #endif |
afb74891 | 182 | |
4bb6408c JS |
183 | // do it as the very last thing because everything else can log messages |
184 | wxLog::DontCreateOnDemand(); | |
185 | // do it as the very last thing because everything else can log messages | |
186 | delete wxLog::SetActiveTarget(NULL); | |
187 | } | |
188 | ||
189 | int wxEntry( int argc, char *argv[] ) | |
190 | { | |
4fabb575 JS |
191 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT |
192 | // This seems to be necessary since there are 'rogue' | |
193 | // objects present at this point (perhaps global objects?) | |
194 | // Setting a checkpoint will ignore them as far as the | |
195 | // memory checking facility is concerned. | |
196 | // Of course you may argue that memory allocated in globals should be | |
197 | // checked, but this is a reasonable compromise. | |
198 | wxDebugContext::SetCheckpoint(); | |
199 | #endif | |
afb74891 | 200 | |
4bb6408c | 201 | if (!wxApp::Initialize()) |
2d120f83 | 202 | return FALSE; |
afb74891 | 203 | |
4bb6408c JS |
204 | if (!wxTheApp) |
205 | { | |
2d120f83 JS |
206 | if (!wxApp::GetInitializerFunction()) |
207 | { | |
208 | printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" ); | |
209 | return 0; | |
210 | }; | |
afb74891 | 211 | |
2d120f83 | 212 | wxTheApp = (wxApp*) (* wxApp::GetInitializerFunction()) (); |
4bb6408c | 213 | }; |
afb74891 | 214 | |
4bb6408c JS |
215 | if (!wxTheApp) |
216 | { | |
2d120f83 JS |
217 | printf( "wxWindows error: wxTheApp == NULL\n" ); |
218 | return 0; | |
4bb6408c | 219 | }; |
afb74891 | 220 | |
4bb6408c JS |
221 | wxTheApp->SetClassName(wxFileNameFromPath(argv[0])); |
222 | wxTheApp->SetAppName(wxFileNameFromPath(argv[0])); | |
afb74891 | 223 | |
4bb6408c JS |
224 | wxTheApp->argc = argc; |
225 | wxTheApp->argv = argv; | |
afb74891 | 226 | |
4bb6408c JS |
227 | // GUI-specific initialization, such as creating an app context. |
228 | wxTheApp->OnInitGui(); | |
afb74891 | 229 | |
e146b8c8 VZ |
230 | // Here frames insert themselves automatically into wxTopLevelWindows by |
231 | // getting created in OnInit(). | |
afb74891 | 232 | |
4bb6408c | 233 | int retValue = 0; |
4fabb575 JS |
234 | if (wxTheApp->OnInit()) |
235 | { | |
2d120f83 | 236 | if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun(); |
4fabb575 | 237 | } |
afb74891 | 238 | |
f57fe24c JS |
239 | // flush the logged messages if any |
240 | wxLog *pLog = wxLog::GetActiveTarget(); | |
241 | if ( pLog != NULL && pLog->HasPendingMessages() ) | |
2d120f83 | 242 | pLog->Flush(); |
afb74891 | 243 | |
f57fe24c | 244 | delete wxLog::SetActiveTarget(new wxLogStderr); // So dialog boxes aren't used |
2d120f83 | 245 | // for further messages |
afb74891 | 246 | |
4bb6408c JS |
247 | if (wxTheApp->GetTopWindow()) |
248 | { | |
2d120f83 JS |
249 | delete wxTheApp->GetTopWindow(); |
250 | wxTheApp->SetTopWindow(NULL); | |
4bb6408c | 251 | } |
afb74891 | 252 | |
4bb6408c | 253 | wxTheApp->DeletePendingObjects(); |
afb74891 | 254 | |
4bb6408c | 255 | wxTheApp->OnExit(); |
afb74891 | 256 | |
4bb6408c | 257 | wxApp::CleanUp(); |
afb74891 | 258 | |
4bb6408c JS |
259 | return retValue; |
260 | }; | |
261 | ||
262 | // Static member initialization | |
ee31c392 | 263 | wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL; |
4bb6408c JS |
264 | |
265 | wxApp::wxApp() | |
266 | { | |
4bb6408c JS |
267 | argc = 0; |
268 | argv = NULL; | |
afb74891 | 269 | |
4bb6408c JS |
270 | m_mainColormap = (WXColormap) NULL; |
271 | m_appContext = (WXAppContext) NULL; | |
272 | m_topLevelWidget = (WXWidget) NULL; | |
273 | m_maxRequestSize = 0; | |
47bc1060 | 274 | m_initialDisplay = (WXDisplay*) 0; |
4bb6408c JS |
275 | } |
276 | ||
277 | bool wxApp::Initialized() | |
278 | { | |
279 | if (GetTopWindow()) | |
2d120f83 | 280 | return TRUE; |
4bb6408c | 281 | else |
2d120f83 | 282 | return FALSE; |
4bb6408c JS |
283 | } |
284 | ||
285 | int wxApp::MainLoop() | |
286 | { | |
287 | m_keepGoing = TRUE; | |
afb74891 | 288 | |
2d120f83 | 289 | /* |
4bb6408c JS |
290 | * Sit around forever waiting to process X-events. Property Change |
291 | * event are handled special, because they have to refer to | |
292 | * the root window rather than to a widget. therefore we can't | |
293 | * use an Xt-eventhandler. | |
294 | */ | |
afb74891 | 295 | |
4bb6408c | 296 | XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), |
2d120f83 JS |
297 | XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())), |
298 | PropertyChangeMask); | |
afb74891 | 299 | |
4bb6408c | 300 | XEvent event; |
afb74891 | 301 | |
4bb6408c JS |
302 | // Use this flag to allow breaking the loop via wxApp::ExitMainLoop() |
303 | while (m_keepGoing) | |
304 | { | |
2d120f83 | 305 | XtAppNextEvent( (XtAppContext) wxTheApp->GetAppContext(), &event); |
afb74891 | 306 | |
2d120f83 | 307 | ProcessXEvent((WXEvent*) & event); |
afb74891 | 308 | |
2d120f83 JS |
309 | if (XtAppPending( (XtAppContext) wxTheApp->GetAppContext() ) == 0) |
310 | { | |
311 | if (!ProcessIdle()) | |
afb74891 | 312 | { |
7bcb11d3 | 313 | #if wxUSE_THREADS |
afb74891 VZ |
314 | // leave the main loop to give other threads a chance to |
315 | // perform their GUI work | |
2d120f83 | 316 | wxMutexGuiLeave(); |
afb74891 | 317 | wxUsleep(20); |
2d120f83 | 318 | wxMutexGuiEnter(); |
7bcb11d3 | 319 | #endif |
2d120f83 JS |
320 | } |
321 | } | |
afb74891 | 322 | |
8aa04e8b | 323 | } |
afb74891 | 324 | |
8aa04e8b JS |
325 | return 0; |
326 | } | |
327 | ||
328 | // Processes an X event. | |
329 | void wxApp::ProcessXEvent(WXEvent* _event) | |
330 | { | |
331 | XEvent* event = (XEvent*) _event; | |
afb74891 | 332 | |
ee31c392 | 333 | if (event->type == KeyPress) |
8aa04e8b | 334 | { |
dc1efb1d | 335 | #if 0 // def __WXDEBUG__ |
31528cd3 VZ |
336 | Widget widget = XtWindowToWidget(event->xany.display, event->xany.window); |
337 | wxLogDebug("Got key press event for 0x%08x (parent = 0x%08x)", | |
338 | widget, XtParent(widget)); | |
339 | #endif // DEBUG | |
340 | ||
8461e4c2 VZ |
341 | if (CheckForAccelerator(_event)) |
342 | { | |
3d9431bf MB |
343 | // Do nothing! We intercepted and processed the event as an |
344 | // accelerator. | |
345 | return; | |
8461e4c2 | 346 | } |
a3736ef4 | 347 | #if 1 |
9779893b | 348 | // It seemed before that this hack was redundant and |
a3736ef4 MB |
349 | // key down events were being generated by wxCanvasInputEvent. |
350 | // But no longer - why ??? | |
351 | // | |
8461e4c2 VZ |
352 | else if (CheckForKeyDown(_event)) |
353 | { | |
3d9431bf MB |
354 | // We intercepted and processed the key down event |
355 | return; | |
8461e4c2 | 356 | } |
9779893b | 357 | #endif |
8461e4c2 VZ |
358 | else |
359 | { | |
3d9431bf | 360 | XtDispatchEvent(event); |
8461e4c2 VZ |
361 | return; |
362 | } | |
3d9431bf MB |
363 | } |
364 | else if (event->type == KeyRelease) | |
365 | { | |
364c7f3c MB |
366 | // TODO: work out why we still need this ! -michael |
367 | // | |
3d9431bf | 368 | if (CheckForKeyUp(_event)) |
8461e4c2 VZ |
369 | { |
370 | // We intercepted and processed the key up event | |
371 | return; | |
372 | } | |
373 | else | |
374 | { | |
375 | XtDispatchEvent(event); | |
376 | return; | |
377 | } | |
8aa04e8b JS |
378 | } |
379 | else if (event->type == PropertyNotify) | |
380 | { | |
381 | HandlePropertyChange(_event); | |
382 | return; | |
383 | } | |
384 | else if (event->type == ResizeRequest) | |
385 | { | |
ee31c392 VZ |
386 | /* Terry Gitnick <terryg@scientech.com> - 1/21/98 |
387 | * If resize event, don't resize until the last resize event for this | |
388 | * window is recieved. Prevents flicker as windows are resized. | |
389 | */ | |
afb74891 | 390 | |
8aa04e8b JS |
391 | Display *disp = XtDisplay((Widget) wxTheApp->GetTopLevelWidget()); |
392 | Window win = event->xany.window; | |
393 | XEvent report; | |
afb74891 | 394 | |
8aa04e8b JS |
395 | // to avoid flicker |
396 | report = * event; | |
397 | while( XCheckTypedWindowEvent (disp, win, ResizeRequest, &report)); | |
afb74891 | 398 | |
f97c9854 JS |
399 | // TODO: when implementing refresh optimization, we can use |
400 | // XtAddExposureToRegion to expand the window's paint region. | |
afb74891 | 401 | |
8aa04e8b JS |
402 | XtDispatchEvent(event); |
403 | } | |
404 | else | |
405 | { | |
406 | XtDispatchEvent(event); | |
4bb6408c | 407 | } |
4bb6408c JS |
408 | } |
409 | ||
410 | // Returns TRUE if more time is needed. | |
411 | bool wxApp::ProcessIdle() | |
412 | { | |
413 | wxIdleEvent event; | |
414 | event.SetEventObject(this); | |
415 | ProcessEvent(event); | |
afb74891 | 416 | |
4bb6408c JS |
417 | return event.MoreRequested(); |
418 | } | |
419 | ||
420 | void wxApp::ExitMainLoop() | |
421 | { | |
422 | m_keepGoing = FALSE; | |
423 | } | |
424 | ||
425 | // Is a message/event pending? | |
426 | bool wxApp::Pending() | |
427 | { | |
428 | XFlush(XtDisplay( (Widget) wxTheApp->GetTopLevelWidget() )); | |
afb74891 | 429 | |
8aa04e8b JS |
430 | // Fix by Doug from STI, to prevent a stall if non-X event |
431 | // is found. | |
432 | return ((XtAppPending( (XtAppContext) GetAppContext() ) & XtIMXEvent) != 0) ; | |
4bb6408c JS |
433 | } |
434 | ||
435 | // Dispatch a message. | |
436 | void wxApp::Dispatch() | |
437 | { | |
2d120f83 | 438 | // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll); |
afb74891 | 439 | |
8aa04e8b JS |
440 | XEvent event; |
441 | XtAppNextEvent((XtAppContext) GetAppContext(), &event); | |
442 | ProcessXEvent((WXEvent*) & event); | |
4bb6408c JS |
443 | } |
444 | ||
445 | // This should be redefined in a derived class for | |
446 | // handling property change events for XAtom IPC. | |
447 | void wxApp::HandlePropertyChange(WXEvent *event) | |
448 | { | |
449 | // by default do nothing special | |
450 | XtDispatchEvent((XEvent*) event); /* let Motif do the work */ | |
451 | } | |
452 | ||
453 | void wxApp::OnIdle(wxIdleEvent& event) | |
454 | { | |
455 | static bool inOnIdle = FALSE; | |
afb74891 | 456 | |
4bb6408c JS |
457 | // Avoid recursion (via ProcessEvent default case) |
458 | if (inOnIdle) | |
2d120f83 | 459 | return; |
afb74891 | 460 | |
4bb6408c | 461 | inOnIdle = TRUE; |
afb74891 | 462 | |
0ca580f6 GRG |
463 | // If there are pending events, we must process them: pending events |
464 | // are either events to the threads other than main or events posted | |
465 | // with wxPostEvent() functions | |
466 | // GRG: I have moved this here so that all pending events are processed | |
467 | // before starting to delete any objects. This behaves better (in | |
468 | // particular, wrt wxPostEvent) and is coherent with wxGTK's current | |
469 | // behaviour. Also removed the '#if wxUSE_THREADS' around it. | |
470 | // Changed Mar/2000 before 2.1.14 | |
afb74891 | 471 | |
7214297d GL |
472 | // Flush pending events. |
473 | ProcessPendingEvents(); | |
0ca580f6 GRG |
474 | |
475 | // 'Garbage' collection of windows deleted with Close(). | |
476 | DeletePendingObjects(); | |
7214297d | 477 | |
4bb6408c JS |
478 | // flush the logged messages if any |
479 | wxLog *pLog = wxLog::GetActiveTarget(); | |
480 | if ( pLog != NULL && pLog->HasPendingMessages() ) | |
2d120f83 | 481 | pLog->Flush(); |
afb74891 | 482 | |
4bb6408c JS |
483 | // Send OnIdle events to all windows |
484 | bool needMore = SendIdleEvents(); | |
afb74891 | 485 | |
4bb6408c | 486 | if (needMore) |
2d120f83 | 487 | event.RequestMore(TRUE); |
afb74891 | 488 | |
4bb6408c JS |
489 | inOnIdle = FALSE; |
490 | } | |
491 | ||
9779893b RD |
492 | void wxWakeUpIdle() |
493 | { | |
494 | // **** please implement me! **** | |
495 | // Wake up the idle handler processor, even if it is in another thread... | |
496 | } | |
497 | ||
498 | ||
4bb6408c JS |
499 | // Send idle event to all top-level windows |
500 | bool wxApp::SendIdleEvents() | |
501 | { | |
502 | bool needMore = FALSE; | |
e146b8c8 VZ |
503 | |
504 | wxWindowList::Node* node = wxTopLevelWindows.GetFirst(); | |
2d120f83 JS |
505 | while (node) |
506 | { | |
e146b8c8 | 507 | wxWindow* win = node->GetData(); |
2d120f83 | 508 | if (SendIdleEvents(win)) |
4bb6408c | 509 | needMore = TRUE; |
e146b8c8 | 510 | node = node->GetNext(); |
2d120f83 | 511 | } |
e146b8c8 | 512 | |
4bb6408c JS |
513 | return needMore; |
514 | } | |
515 | ||
516 | // Send idle event to window and all subwindows | |
517 | bool wxApp::SendIdleEvents(wxWindow* win) | |
518 | { | |
519 | bool needMore = FALSE; | |
afb74891 | 520 | |
2d120f83 JS |
521 | wxIdleEvent event; |
522 | event.SetEventObject(win); | |
523 | win->ProcessEvent(event); | |
afb74891 | 524 | |
4bb6408c JS |
525 | if (event.MoreRequested()) |
526 | needMore = TRUE; | |
afb74891 | 527 | |
2d120f83 JS |
528 | wxNode* node = win->GetChildren().First(); |
529 | while (node) | |
530 | { | |
531 | wxWindow* win = (wxWindow*) node->Data(); | |
532 | if (SendIdleEvents(win)) | |
4bb6408c | 533 | needMore = TRUE; |
afb74891 | 534 | |
2d120f83 JS |
535 | node = node->Next(); |
536 | } | |
4bb6408c JS |
537 | return needMore ; |
538 | } | |
539 | ||
540 | void wxApp::DeletePendingObjects() | |
541 | { | |
542 | wxNode *node = wxPendingDelete.First(); | |
543 | while (node) | |
544 | { | |
2d120f83 | 545 | wxObject *obj = (wxObject *)node->Data(); |
afb74891 | 546 | |
2d120f83 | 547 | delete obj; |
afb74891 | 548 | |
2d120f83 JS |
549 | if (wxPendingDelete.Member(obj)) |
550 | delete node; | |
afb74891 | 551 | |
2d120f83 JS |
552 | // Deleting one object may have deleted other pending |
553 | // objects, so start from beginning of list again. | |
554 | node = wxPendingDelete.First(); | |
4bb6408c JS |
555 | } |
556 | } | |
557 | ||
edc1cd8b JS |
558 | static char *fallbackResources[] = { |
559 | "*menuBar.marginHeight: 0", | |
560 | "*menuBar.shadowThickness: 1", | |
561 | "*background: #c0c0c0", | |
562 | "*foreground: black", | |
563 | NULL | |
564 | }; | |
565 | ||
4bb6408c JS |
566 | // Create an application context |
567 | bool wxApp::OnInitGui() | |
568 | { | |
569 | XtToolkitInitialize() ; | |
edc1cd8b JS |
570 | wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext(); |
571 | XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources); | |
572 | ||
4bb6408c | 573 | Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL, |
9c6e1335 | 574 | (const char*) wxTheApp->GetClassName(), NULL, 0, |
4bb6408c | 575 | # if XtSpecificationRelease < 5 |
9c6e1335 | 576 | (Cardinal*) &argc, |
4bb6408c | 577 | # else |
9c6e1335 | 578 | &argc, |
4bb6408c | 579 | # endif |
9c6e1335 VZ |
580 | argv); |
581 | ||
4bb6408c | 582 | if (!dpy) { |
c88b736c MB |
583 | // if you don't log to stderr, nothing will be shown... |
584 | delete wxLog::SetActiveTarget(new wxLogStderr); | |
7bcb11d3 | 585 | wxString className(wxTheApp->GetClassName()); |
9c6e1335 | 586 | wxLogError(_("wxWindows could not open display for '%s': exiting."), |
7bcb11d3 | 587 | (const char*) className); |
2d120f83 | 588 | exit(-1); |
4bb6408c | 589 | } |
47bc1060 | 590 | m_initialDisplay = (WXDisplay*) dpy; |
afb74891 | 591 | |
e838cc14 VZ |
592 | #ifdef __WXDEBUG__ |
593 | // install the X error handler | |
594 | gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler); | |
595 | #endif // __WXDEBUG__ | |
596 | ||
4bb6408c | 597 | wxTheApp->m_topLevelWidget = (WXWidget) XtAppCreateShell((String)NULL, (const char*) wxTheApp->GetClassName(), |
2d120f83 JS |
598 | applicationShellWidgetClass,dpy, |
599 | NULL,0) ; | |
afb74891 | 600 | |
4bb6408c JS |
601 | // Add general resize proc |
602 | XtActionsRec rec; | |
603 | rec.string = "resize"; | |
604 | rec.proc = (XtActionProc)wxWidgetResizeProc; | |
605 | XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1); | |
afb74891 | 606 | |
4bb6408c JS |
607 | GetMainColormap(dpy); |
608 | m_maxRequestSize = XMaxRequestSize((Display*) dpy); | |
afb74891 | 609 | |
4bb6408c JS |
610 | return TRUE; |
611 | } | |
612 | ||
613 | WXColormap wxApp::GetMainColormap(WXDisplay* display) | |
614 | { | |
615 | if (!display) /* Must be called first with non-NULL display */ | |
2d120f83 | 616 | return m_mainColormap; |
a91b47e8 JS |
617 | |
618 | int defaultScreen = DefaultScreen((Display*) display); | |
619 | Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen); | |
afb74891 | 620 | |
a91b47e8 | 621 | Colormap c = DefaultColormapOfScreen(screen); |
afb74891 | 622 | |
4bb6408c | 623 | if (!m_mainColormap) |
2d120f83 | 624 | m_mainColormap = (WXColormap) c; |
afb74891 | 625 | |
4bb6408c JS |
626 | return (WXColormap) c; |
627 | } | |
628 | ||
8aa04e8b JS |
629 | // Returns TRUE if an accelerator has been processed |
630 | bool wxApp::CheckForAccelerator(WXEvent* event) | |
631 | { | |
632 | XEvent* xEvent = (XEvent*) event; | |
633 | if (xEvent->xany.type == KeyPress) | |
634 | { | |
635 | // Find a wxWindow for this window | |
636 | // TODO: should get display for the window, not the current display | |
637 | Widget widget = XtWindowToWidget((Display*) wxGetDisplay(), xEvent->xany.window); | |
638 | wxWindow* win = NULL; | |
afb74891 | 639 | |
8aa04e8b JS |
640 | // Find the first wxWindow that corresponds to this event window |
641 | while (widget && !(win = wxGetWindowFromTable(widget))) | |
642 | widget = XtParent(widget); | |
afb74891 | 643 | |
8aa04e8b JS |
644 | if (!widget || !win) |
645 | return FALSE; | |
afb74891 | 646 | |
8aa04e8b JS |
647 | wxKeyEvent keyEvent(wxEVT_CHAR); |
648 | wxTranslateKeyEvent(keyEvent, win, (Widget) 0, xEvent); | |
afb74891 | 649 | |
8aa04e8b JS |
650 | // Now we have a wxKeyEvent and we have a wxWindow. |
651 | // Go up the hierarchy until we find a matching accelerator, | |
652 | // or we get to the top. | |
653 | while (win) | |
654 | { | |
655 | if (win->ProcessAccelerator(keyEvent)) | |
656 | return TRUE; | |
657 | win = win->GetParent(); | |
658 | } | |
659 | return FALSE; | |
660 | } | |
661 | return FALSE; | |
662 | } | |
663 | ||
ee31c392 VZ |
664 | bool wxApp::CheckForKeyDown(WXEvent* event) |
665 | { | |
666 | XEvent* xEvent = (XEvent*) event; | |
ee31c392 VZ |
667 | if (xEvent->xany.type == KeyPress) |
668 | { | |
3d9431bf | 669 | Widget widget = XtWindowToWidget((Display*) wxGetDisplay(), |
8461e4c2 VZ |
670 | xEvent->xany.window); |
671 | wxWindow* win = NULL; | |
3d9431bf | 672 | |
8461e4c2 VZ |
673 | // Find the first wxWindow that corresponds to this event window |
674 | while (widget && !(win = wxGetWindowFromTable(widget))) | |
3d9431bf MB |
675 | widget = XtParent(widget); |
676 | ||
8461e4c2 | 677 | if (!widget || !win) |
3d9431bf MB |
678 | return FALSE; |
679 | ||
8461e4c2 VZ |
680 | wxKeyEvent keyEvent(wxEVT_KEY_DOWN); |
681 | wxTranslateKeyEvent(keyEvent, win, (Widget) 0, xEvent); | |
3d9431bf | 682 | |
8461e4c2 | 683 | return win->ProcessEvent( keyEvent ); |
3d9431bf MB |
684 | } |
685 | ||
686 | return FALSE; | |
687 | } | |
688 | ||
689 | bool wxApp::CheckForKeyUp(WXEvent* event) | |
690 | { | |
691 | XEvent* xEvent = (XEvent*) event; | |
692 | if (xEvent->xany.type == KeyRelease) | |
693 | { | |
694 | Widget widget = XtWindowToWidget((Display*) wxGetDisplay(), | |
8461e4c2 VZ |
695 | xEvent->xany.window); |
696 | wxWindow* win = NULL; | |
ee31c392 | 697 | |
8461e4c2 VZ |
698 | // Find the first wxWindow that corresponds to this event window |
699 | while (widget && !(win = wxGetWindowFromTable(widget))) | |
700 | widget = XtParent(widget); | |
ee31c392 | 701 | |
8461e4c2 VZ |
702 | if (!widget || !win) |
703 | return FALSE; | |
ee31c392 | 704 | |
8461e4c2 VZ |
705 | wxKeyEvent keyEvent(wxEVT_KEY_UP); |
706 | wxTranslateKeyEvent(keyEvent, win, (Widget) 0, xEvent); | |
ee31c392 | 707 | |
8461e4c2 | 708 | return win->ProcessEvent( keyEvent ); |
ee31c392 | 709 | } |
ee31c392 VZ |
710 | |
711 | return FALSE; | |
712 | } | |
713 | ||
4bb6408c JS |
714 | void wxExit() |
715 | { | |
716 | int retValue = 0; | |
717 | if (wxTheApp) | |
2d120f83 | 718 | retValue = wxTheApp->OnExit(); |
afb74891 | 719 | |
4bb6408c JS |
720 | wxApp::CleanUp(); |
721 | /* | |
2d120f83 JS |
722 | * Exit in some platform-specific way. Not recommended that the app calls this: |
723 | * only for emergencies. | |
724 | */ | |
4bb6408c JS |
725 | exit(retValue); |
726 | } | |
727 | ||
728 | // Yield to other processes | |
cb2713bf | 729 | |
8461e4c2 | 730 | bool wxApp::Yield(bool onlyIfNeeded) |
4bb6408c | 731 | { |
8461e4c2 VZ |
732 | bool s_inYield = FALSE; |
733 | ||
734 | if ( s_inYield ) | |
735 | { | |
736 | if ( !onlyIfNeeded ) | |
737 | { | |
738 | wxFAIL_MSG( wxT("wxYield called recursively" ) ); | |
739 | } | |
740 | ||
741 | return FALSE; | |
742 | } | |
743 | ||
744 | s_inYield = TRUE; | |
cb2713bf | 745 | |
4bb6408c | 746 | while (wxTheApp && wxTheApp->Pending()) |
2d120f83 | 747 | wxTheApp->Dispatch(); |
518b5d2f | 748 | |
8461e4c2 | 749 | s_inYield = FALSE; |
cb2713bf | 750 | |
4bb6408c JS |
751 | return TRUE; |
752 | } | |
753 | ||
d391a345 VZ |
754 | // ---------------------------------------------------------------------------- |
755 | // accessors for C modules | |
756 | // ---------------------------------------------------------------------------- | |
757 | ||
758 | extern "C" XtAppContext wxGetAppContext() | |
759 | { | |
760 | return (XtAppContext)wxTheApp->GetAppContext(); | |
761 | } |