]>
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 VZ |
13 | #pragma implementation "app.h" |
14 | #pragma implementation "appbase.h" | |
4bb6408c JS |
15 | #endif |
16 | ||
17 | #include "wx/frame.h" | |
18 | #include "wx/app.h" | |
19 | #include "wx/utils.h" | |
20 | #include "wx/gdicmn.h" | |
21 | #include "wx/pen.h" | |
22 | #include "wx/brush.h" | |
23 | #include "wx/cursor.h" | |
24 | #include "wx/icon.h" | |
25 | #include "wx/palette.h" | |
26 | #include "wx/dc.h" | |
27 | #include "wx/dialog.h" | |
28 | #include "wx/msgdlg.h" | |
29 | #include "wx/log.h" | |
30 | #include "wx/module.h" | |
31 | #include "wx/memory.h" | |
8bedcdce RR |
32 | #include "wx/log.h" |
33 | #include "wx/intl.h" | |
b412f9be | 34 | |
7bcb11d3 | 35 | #if wxUSE_THREADS |
dfe1eee3 | 36 | #include "wx/thread.h" |
7bcb11d3 | 37 | #endif |
4bb6408c | 38 | |
47d67540 | 39 | #if wxUSE_WX_RESOURCES |
9c6e1335 | 40 | #include "wx/resource.h" |
4bb6408c JS |
41 | #endif |
42 | ||
4bb6408c JS |
43 | #include <Xm/Xm.h> |
44 | #include <X11/Xlib.h> | |
45 | #include <X11/Xutil.h> | |
46 | #include <X11/Xresource.h> | |
47 | #include <X11/Xatom.h> | |
48 | ||
49 | #include "wx/motif/private.h" | |
50 | ||
51 | #include <string.h> | |
52 | ||
53 | extern char *wxBuffer; | |
54 | extern wxList wxPendingDelete; | |
7491d644 | 55 | |
7214297d | 56 | #if wxUSE_THREADS |
4d3a259a GL |
57 | extern wxList *wxPendingEvents; |
58 | extern wxCriticalSection *wxPendingEventsLocker; | |
7491d644 | 59 | #endif // wxUSE_THREADS |
4bb6408c JS |
60 | |
61 | wxApp *wxTheApp = NULL; | |
62 | ||
63 | wxHashTable *wxWidgetHashTable = NULL; | |
64 | ||
65 | #if !USE_SHARED_LIBRARY | |
66 | IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler) | |
9c6e1335 | 67 | |
4bb6408c | 68 | BEGIN_EVENT_TABLE(wxApp, wxEvtHandler) |
9c6e1335 | 69 | EVT_IDLE(wxApp::OnIdle) |
4bb6408c JS |
70 | END_EVENT_TABLE() |
71 | #endif | |
72 | ||
73 | long wxApp::sm_lastMessageTime = 0; | |
74 | ||
75 | bool wxApp::Initialize() | |
76 | { | |
4bb6408c | 77 | wxBuffer = new char[BUFSIZ + 512]; |
afb74891 | 78 | |
4bb6408c | 79 | wxClassInfo::InitializeClasses(); |
afb74891 | 80 | |
ee31c392 | 81 | // GL: I'm annoyed ... I don't know where to put this and I don't want to |
4d3a259a GL |
82 | // create a module for that as it's part of the core. |
83 | #if wxUSE_THREADS | |
84 | wxPendingEvents = new wxList(); | |
85 | wxPendingEventsLocker = new wxCriticalSection(); | |
86 | #endif | |
87 | ||
4bb6408c JS |
88 | wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING); |
89 | wxTheColourDatabase->Initialize(); | |
afb74891 | 90 | |
4b5f3fe6 | 91 | wxInitializeStockLists(); |
4bb6408c | 92 | wxInitializeStockObjects(); |
afb74891 | 93 | |
47d67540 | 94 | #if wxUSE_WX_RESOURCES |
4bb6408c JS |
95 | wxInitializeResourceSystem(); |
96 | #endif | |
afb74891 | 97 | |
2d120f83 | 98 | // For PostScript printing |
47d67540 | 99 | #if wxUSE_POSTSCRIPT |
2d120f83 | 100 | /* Done using wxModule now |
4bb6408c JS |
101 | wxInitializePrintSetupData(); |
102 | wxThePrintPaperDatabase = new wxPrintPaperDatabase; | |
103 | wxThePrintPaperDatabase->CreateDatabase(); | |
2d120f83 | 104 | */ |
4bb6408c | 105 | #endif |
afb74891 | 106 | |
4bb6408c | 107 | wxBitmap::InitStandardHandlers(); |
afb74891 | 108 | |
4bb6408c | 109 | wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER); |
afb74891 | 110 | |
4bb6408c | 111 | wxModule::RegisterModules(); |
aaa38880 | 112 | if (!wxModule::InitializeModules()) return FALSE; |
afb74891 | 113 | |
4bb6408c JS |
114 | return TRUE; |
115 | } | |
116 | ||
117 | void wxApp::CleanUp() | |
118 | { | |
119 | delete wxWidgetHashTable; | |
120 | wxWidgetHashTable = NULL; | |
afb74891 | 121 | |
4bb6408c | 122 | wxModule::CleanUpModules(); |
afb74891 | 123 | |
47d67540 | 124 | #if wxUSE_WX_RESOURCES |
4bb6408c JS |
125 | wxCleanUpResourceSystem(); |
126 | #endif | |
afb74891 | 127 | |
4bb6408c | 128 | wxDeleteStockObjects() ; |
afb74891 | 129 | |
4bb6408c | 130 | // Destroy all GDI lists, etc. |
afb74891 | 131 | |
4bb6408c JS |
132 | delete wxTheBrushList; |
133 | wxTheBrushList = NULL; | |
afb74891 | 134 | |
4bb6408c JS |
135 | delete wxThePenList; |
136 | wxThePenList = NULL; | |
afb74891 | 137 | |
4bb6408c JS |
138 | delete wxTheFontList; |
139 | wxTheFontList = NULL; | |
afb74891 | 140 | |
4bb6408c JS |
141 | delete wxTheBitmapList; |
142 | wxTheBitmapList = NULL; | |
afb74891 | 143 | |
4bb6408c JS |
144 | delete wxTheColourDatabase; |
145 | wxTheColourDatabase = NULL; | |
afb74891 | 146 | |
47d67540 | 147 | #if wxUSE_POSTSCRIPT |
2d120f83 | 148 | /* Done using wxModule now |
4bb6408c JS |
149 | wxInitializePrintSetupData(FALSE); |
150 | delete wxThePrintPaperDatabase; | |
151 | wxThePrintPaperDatabase = NULL; | |
2d120f83 | 152 | */ |
4bb6408c | 153 | #endif |
afb74891 | 154 | |
4bb6408c | 155 | wxBitmap::CleanUpHandlers(); |
afb74891 | 156 | |
4bb6408c JS |
157 | delete[] wxBuffer; |
158 | wxBuffer = NULL; | |
afb74891 | 159 | |
4bb6408c | 160 | wxClassInfo::CleanUpClasses(); |
afb74891 | 161 | |
184b5d99 JS |
162 | delete wxTheApp; |
163 | wxTheApp = NULL; | |
afb74891 | 164 | |
4d3a259a GL |
165 | // GL: I'm annoyed ... I don't know where to put this and I don't want to |
166 | // create a module for that as it's part of the core. | |
167 | #if wxUSE_THREADS | |
168 | delete wxPendingEvents; | |
169 | delete wxPendingEventsLocker; | |
170 | #endif | |
171 | ||
184b5d99 JS |
172 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT |
173 | // At this point we want to check if there are any memory | |
174 | // blocks that aren't part of the wxDebugContext itself, | |
175 | // as a special case. Then when dumping we need to ignore | |
176 | // wxDebugContext, too. | |
4fabb575 | 177 | if (wxDebugContext::CountObjectsLeft(TRUE) > 0) |
184b5d99 | 178 | { |
2d120f83 JS |
179 | wxLogDebug("There were memory leaks.\n"); |
180 | wxDebugContext::Dump(); | |
181 | wxDebugContext::PrintStatistics(); | |
184b5d99 | 182 | } |
184b5d99 | 183 | #endif |
afb74891 | 184 | |
4bb6408c JS |
185 | // do it as the very last thing because everything else can log messages |
186 | wxLog::DontCreateOnDemand(); | |
187 | // do it as the very last thing because everything else can log messages | |
188 | delete wxLog::SetActiveTarget(NULL); | |
189 | } | |
190 | ||
191 | int wxEntry( int argc, char *argv[] ) | |
192 | { | |
4fabb575 JS |
193 | #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT |
194 | // This seems to be necessary since there are 'rogue' | |
195 | // objects present at this point (perhaps global objects?) | |
196 | // Setting a checkpoint will ignore them as far as the | |
197 | // memory checking facility is concerned. | |
198 | // Of course you may argue that memory allocated in globals should be | |
199 | // checked, but this is a reasonable compromise. | |
200 | wxDebugContext::SetCheckpoint(); | |
201 | #endif | |
afb74891 | 202 | |
4bb6408c | 203 | if (!wxApp::Initialize()) |
2d120f83 | 204 | return FALSE; |
afb74891 | 205 | |
4bb6408c JS |
206 | if (!wxTheApp) |
207 | { | |
2d120f83 JS |
208 | if (!wxApp::GetInitializerFunction()) |
209 | { | |
210 | printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" ); | |
211 | return 0; | |
212 | }; | |
afb74891 | 213 | |
2d120f83 | 214 | wxTheApp = (wxApp*) (* wxApp::GetInitializerFunction()) (); |
4bb6408c | 215 | }; |
afb74891 | 216 | |
4bb6408c JS |
217 | if (!wxTheApp) |
218 | { | |
2d120f83 JS |
219 | printf( "wxWindows error: wxTheApp == NULL\n" ); |
220 | return 0; | |
4bb6408c | 221 | }; |
afb74891 | 222 | |
4bb6408c JS |
223 | wxTheApp->SetClassName(wxFileNameFromPath(argv[0])); |
224 | wxTheApp->SetAppName(wxFileNameFromPath(argv[0])); | |
afb74891 | 225 | |
4bb6408c JS |
226 | wxTheApp->argc = argc; |
227 | wxTheApp->argv = argv; | |
afb74891 | 228 | |
4bb6408c JS |
229 | // GUI-specific initialization, such as creating an app context. |
230 | wxTheApp->OnInitGui(); | |
afb74891 | 231 | |
e146b8c8 VZ |
232 | // Here frames insert themselves automatically into wxTopLevelWindows by |
233 | // getting created in OnInit(). | |
afb74891 | 234 | |
4bb6408c | 235 | int retValue = 0; |
4fabb575 JS |
236 | if (wxTheApp->OnInit()) |
237 | { | |
2d120f83 | 238 | if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun(); |
4fabb575 | 239 | } |
afb74891 | 240 | |
f57fe24c JS |
241 | // flush the logged messages if any |
242 | wxLog *pLog = wxLog::GetActiveTarget(); | |
243 | if ( pLog != NULL && pLog->HasPendingMessages() ) | |
2d120f83 | 244 | pLog->Flush(); |
afb74891 | 245 | |
f57fe24c | 246 | delete wxLog::SetActiveTarget(new wxLogStderr); // So dialog boxes aren't used |
2d120f83 | 247 | // for further messages |
afb74891 | 248 | |
4bb6408c JS |
249 | if (wxTheApp->GetTopWindow()) |
250 | { | |
2d120f83 JS |
251 | delete wxTheApp->GetTopWindow(); |
252 | wxTheApp->SetTopWindow(NULL); | |
4bb6408c | 253 | } |
afb74891 | 254 | |
4bb6408c | 255 | wxTheApp->DeletePendingObjects(); |
afb74891 | 256 | |
4bb6408c | 257 | wxTheApp->OnExit(); |
afb74891 | 258 | |
4bb6408c | 259 | wxApp::CleanUp(); |
afb74891 | 260 | |
4bb6408c JS |
261 | return retValue; |
262 | }; | |
263 | ||
264 | // Static member initialization | |
ee31c392 | 265 | wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL; |
4bb6408c JS |
266 | |
267 | wxApp::wxApp() | |
268 | { | |
269 | m_topWindow = NULL; | |
270 | wxTheApp = this; | |
271 | m_className = ""; | |
272 | m_wantDebugOutput = TRUE ; | |
273 | m_appName = ""; | |
274 | argc = 0; | |
275 | argv = NULL; | |
4bb6408c | 276 | m_exitOnFrameDelete = TRUE; |
afb74891 | 277 | |
4bb6408c JS |
278 | m_mainColormap = (WXColormap) NULL; |
279 | m_appContext = (WXAppContext) NULL; | |
280 | m_topLevelWidget = (WXWidget) NULL; | |
281 | m_maxRequestSize = 0; | |
47bc1060 | 282 | m_initialDisplay = (WXDisplay*) 0; |
4bb6408c JS |
283 | } |
284 | ||
285 | bool wxApp::Initialized() | |
286 | { | |
287 | if (GetTopWindow()) | |
2d120f83 | 288 | return TRUE; |
4bb6408c | 289 | else |
2d120f83 | 290 | return FALSE; |
4bb6408c JS |
291 | } |
292 | ||
293 | int wxApp::MainLoop() | |
294 | { | |
295 | m_keepGoing = TRUE; | |
afb74891 | 296 | |
2d120f83 | 297 | /* |
4bb6408c JS |
298 | * Sit around forever waiting to process X-events. Property Change |
299 | * event are handled special, because they have to refer to | |
300 | * the root window rather than to a widget. therefore we can't | |
301 | * use an Xt-eventhandler. | |
302 | */ | |
afb74891 | 303 | |
4bb6408c | 304 | XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), |
2d120f83 JS |
305 | XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())), |
306 | PropertyChangeMask); | |
afb74891 | 307 | |
4bb6408c | 308 | XEvent event; |
afb74891 | 309 | |
4bb6408c JS |
310 | // Use this flag to allow breaking the loop via wxApp::ExitMainLoop() |
311 | while (m_keepGoing) | |
312 | { | |
2d120f83 | 313 | XtAppNextEvent( (XtAppContext) wxTheApp->GetAppContext(), &event); |
afb74891 | 314 | |
2d120f83 | 315 | ProcessXEvent((WXEvent*) & event); |
afb74891 | 316 | |
2d120f83 JS |
317 | if (XtAppPending( (XtAppContext) wxTheApp->GetAppContext() ) == 0) |
318 | { | |
319 | if (!ProcessIdle()) | |
afb74891 | 320 | { |
7bcb11d3 | 321 | #if wxUSE_THREADS |
afb74891 VZ |
322 | // leave the main loop to give other threads a chance to |
323 | // perform their GUI work | |
2d120f83 | 324 | wxMutexGuiLeave(); |
afb74891 | 325 | wxUsleep(20); |
2d120f83 | 326 | wxMutexGuiEnter(); |
7bcb11d3 | 327 | #endif |
2d120f83 JS |
328 | } |
329 | } | |
afb74891 | 330 | |
8aa04e8b | 331 | } |
afb74891 | 332 | |
8aa04e8b JS |
333 | return 0; |
334 | } | |
335 | ||
336 | // Processes an X event. | |
337 | void wxApp::ProcessXEvent(WXEvent* _event) | |
338 | { | |
339 | XEvent* event = (XEvent*) _event; | |
afb74891 | 340 | |
ee31c392 | 341 | if (event->type == KeyPress) |
8aa04e8b | 342 | { |
31528cd3 VZ |
343 | #ifdef __WXDEBUG__ |
344 | Widget widget = XtWindowToWidget(event->xany.display, event->xany.window); | |
345 | wxLogDebug("Got key press event for 0x%08x (parent = 0x%08x)", | |
346 | widget, XtParent(widget)); | |
347 | #endif // DEBUG | |
348 | ||
3d9431bf MB |
349 | if (CheckForAccelerator(_event)) |
350 | { | |
351 | // Do nothing! We intercepted and processed the event as an | |
352 | // accelerator. | |
353 | return; | |
354 | } | |
355 | else if (CheckForKeyDown(_event)) | |
356 | { | |
357 | // We intercepted and processed the key down event | |
358 | return; | |
359 | } | |
360 | else | |
361 | { | |
362 | XtDispatchEvent(event); | |
363 | return; | |
364 | } | |
365 | } | |
366 | else if (event->type == KeyRelease) | |
367 | { | |
368 | if (CheckForKeyUp(_event)) | |
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 | |
4bb6408c JS |
463 | // 'Garbage' collection of windows deleted with Close(). |
464 | DeletePendingObjects(); | |
afb74891 | 465 | |
7214297d GL |
466 | #if wxUSE_THREADS |
467 | // Flush pending events. | |
468 | ProcessPendingEvents(); | |
469 | #endif | |
470 | ||
4bb6408c JS |
471 | // flush the logged messages if any |
472 | wxLog *pLog = wxLog::GetActiveTarget(); | |
473 | if ( pLog != NULL && pLog->HasPendingMessages() ) | |
2d120f83 | 474 | pLog->Flush(); |
afb74891 | 475 | |
4bb6408c JS |
476 | // Send OnIdle events to all windows |
477 | bool needMore = SendIdleEvents(); | |
afb74891 | 478 | |
4bb6408c | 479 | if (needMore) |
2d120f83 | 480 | event.RequestMore(TRUE); |
afb74891 | 481 | |
4bb6408c JS |
482 | inOnIdle = FALSE; |
483 | } | |
484 | ||
485 | // Send idle event to all top-level windows | |
486 | bool wxApp::SendIdleEvents() | |
487 | { | |
488 | bool needMore = FALSE; | |
e146b8c8 VZ |
489 | |
490 | wxWindowList::Node* node = wxTopLevelWindows.GetFirst(); | |
2d120f83 JS |
491 | while (node) |
492 | { | |
e146b8c8 | 493 | wxWindow* win = node->GetData(); |
2d120f83 | 494 | if (SendIdleEvents(win)) |
4bb6408c | 495 | needMore = TRUE; |
e146b8c8 | 496 | node = node->GetNext(); |
2d120f83 | 497 | } |
e146b8c8 | 498 | |
4bb6408c JS |
499 | return needMore; |
500 | } | |
501 | ||
502 | // Send idle event to window and all subwindows | |
503 | bool wxApp::SendIdleEvents(wxWindow* win) | |
504 | { | |
505 | bool needMore = FALSE; | |
afb74891 | 506 | |
2d120f83 JS |
507 | wxIdleEvent event; |
508 | event.SetEventObject(win); | |
509 | win->ProcessEvent(event); | |
afb74891 | 510 | |
4bb6408c JS |
511 | if (event.MoreRequested()) |
512 | needMore = TRUE; | |
afb74891 | 513 | |
2d120f83 JS |
514 | wxNode* node = win->GetChildren().First(); |
515 | while (node) | |
516 | { | |
517 | wxWindow* win = (wxWindow*) node->Data(); | |
518 | if (SendIdleEvents(win)) | |
4bb6408c | 519 | needMore = TRUE; |
afb74891 | 520 | |
2d120f83 JS |
521 | node = node->Next(); |
522 | } | |
4bb6408c JS |
523 | return needMore ; |
524 | } | |
525 | ||
526 | void wxApp::DeletePendingObjects() | |
527 | { | |
528 | wxNode *node = wxPendingDelete.First(); | |
529 | while (node) | |
530 | { | |
2d120f83 | 531 | wxObject *obj = (wxObject *)node->Data(); |
afb74891 | 532 | |
2d120f83 | 533 | delete obj; |
afb74891 | 534 | |
2d120f83 JS |
535 | if (wxPendingDelete.Member(obj)) |
536 | delete node; | |
afb74891 | 537 | |
2d120f83 JS |
538 | // Deleting one object may have deleted other pending |
539 | // objects, so start from beginning of list again. | |
540 | node = wxPendingDelete.First(); | |
4bb6408c JS |
541 | } |
542 | } | |
543 | ||
7214297d GL |
544 | #if wxUSE_THREADS |
545 | void wxApp::ProcessPendingEvents() | |
7491d644 | 546 | { |
4d3a259a GL |
547 | wxNode *node = wxPendingEvents->First(); |
548 | wxCriticalSectionLocker locker(*wxPendingEventsLocker); | |
7214297d GL |
549 | |
550 | while (node) | |
551 | { | |
552 | wxEvtHandler *handler = (wxEvtHandler *)node->Data(); | |
553 | ||
554 | handler->ProcessPendingEvents(); | |
555 | ||
556 | delete node; | |
4d3a259a | 557 | node = wxPendingEvents->First(); |
7214297d | 558 | } |
7491d644 VZ |
559 | } |
560 | #endif // wxUSE_THREADS | |
7214297d | 561 | |
4bb6408c JS |
562 | // Create an application context |
563 | bool wxApp::OnInitGui() | |
564 | { | |
565 | XtToolkitInitialize() ; | |
566 | wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext() ; | |
567 | Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL, | |
9c6e1335 | 568 | (const char*) wxTheApp->GetClassName(), NULL, 0, |
4bb6408c | 569 | # if XtSpecificationRelease < 5 |
9c6e1335 | 570 | (Cardinal*) &argc, |
4bb6408c | 571 | # else |
9c6e1335 | 572 | &argc, |
4bb6408c | 573 | # endif |
9c6e1335 VZ |
574 | argv); |
575 | ||
4bb6408c | 576 | if (!dpy) { |
7bcb11d3 | 577 | wxString className(wxTheApp->GetClassName()); |
9c6e1335 | 578 | wxLogError(_("wxWindows could not open display for '%s': exiting."), |
7bcb11d3 | 579 | (const char*) className); |
2d120f83 | 580 | exit(-1); |
4bb6408c | 581 | } |
47bc1060 | 582 | m_initialDisplay = (WXDisplay*) dpy; |
afb74891 | 583 | |
4bb6408c | 584 | wxTheApp->m_topLevelWidget = (WXWidget) XtAppCreateShell((String)NULL, (const char*) wxTheApp->GetClassName(), |
2d120f83 JS |
585 | applicationShellWidgetClass,dpy, |
586 | NULL,0) ; | |
afb74891 | 587 | |
4bb6408c JS |
588 | // Add general resize proc |
589 | XtActionsRec rec; | |
590 | rec.string = "resize"; | |
591 | rec.proc = (XtActionProc)wxWidgetResizeProc; | |
592 | XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1); | |
afb74891 | 593 | |
4bb6408c JS |
594 | GetMainColormap(dpy); |
595 | m_maxRequestSize = XMaxRequestSize((Display*) dpy); | |
afb74891 | 596 | |
4bb6408c JS |
597 | return TRUE; |
598 | } | |
599 | ||
600 | WXColormap wxApp::GetMainColormap(WXDisplay* display) | |
601 | { | |
602 | if (!display) /* Must be called first with non-NULL display */ | |
2d120f83 | 603 | return m_mainColormap; |
a91b47e8 JS |
604 | |
605 | int defaultScreen = DefaultScreen((Display*) display); | |
606 | Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen); | |
afb74891 | 607 | |
a91b47e8 | 608 | Colormap c = DefaultColormapOfScreen(screen); |
afb74891 | 609 | |
4bb6408c | 610 | if (!m_mainColormap) |
2d120f83 | 611 | m_mainColormap = (WXColormap) c; |
afb74891 | 612 | |
4bb6408c JS |
613 | return (WXColormap) c; |
614 | } | |
615 | ||
8aa04e8b JS |
616 | // Returns TRUE if an accelerator has been processed |
617 | bool wxApp::CheckForAccelerator(WXEvent* event) | |
618 | { | |
619 | XEvent* xEvent = (XEvent*) event; | |
620 | if (xEvent->xany.type == KeyPress) | |
621 | { | |
622 | // Find a wxWindow for this window | |
623 | // TODO: should get display for the window, not the current display | |
624 | Widget widget = XtWindowToWidget((Display*) wxGetDisplay(), xEvent->xany.window); | |
625 | wxWindow* win = NULL; | |
afb74891 | 626 | |
8aa04e8b JS |
627 | // Find the first wxWindow that corresponds to this event window |
628 | while (widget && !(win = wxGetWindowFromTable(widget))) | |
629 | widget = XtParent(widget); | |
afb74891 | 630 | |
8aa04e8b JS |
631 | if (!widget || !win) |
632 | return FALSE; | |
afb74891 | 633 | |
8aa04e8b JS |
634 | wxKeyEvent keyEvent(wxEVT_CHAR); |
635 | wxTranslateKeyEvent(keyEvent, win, (Widget) 0, xEvent); | |
afb74891 | 636 | |
8aa04e8b JS |
637 | // Now we have a wxKeyEvent and we have a wxWindow. |
638 | // Go up the hierarchy until we find a matching accelerator, | |
639 | // or we get to the top. | |
640 | while (win) | |
641 | { | |
642 | if (win->ProcessAccelerator(keyEvent)) | |
643 | return TRUE; | |
644 | win = win->GetParent(); | |
645 | } | |
646 | return FALSE; | |
647 | } | |
648 | return FALSE; | |
649 | } | |
650 | ||
ee31c392 VZ |
651 | bool wxApp::CheckForKeyDown(WXEvent* event) |
652 | { | |
653 | XEvent* xEvent = (XEvent*) event; | |
ee31c392 VZ |
654 | if (xEvent->xany.type == KeyPress) |
655 | { | |
3d9431bf MB |
656 | Widget widget = XtWindowToWidget((Display*) wxGetDisplay(), |
657 | xEvent->xany.window); | |
658 | wxWindow* win = NULL; | |
659 | ||
660 | // Find the first wxWindow that corresponds to this event window | |
661 | while (widget && !(win = wxGetWindowFromTable(widget))) | |
662 | widget = XtParent(widget); | |
663 | ||
664 | if (!widget || !win) | |
665 | return FALSE; | |
666 | ||
667 | wxKeyEvent keyEvent(wxEVT_KEY_DOWN); | |
668 | wxTranslateKeyEvent(keyEvent, win, (Widget) 0, xEvent); | |
669 | ||
670 | return win->ProcessEvent( keyEvent ); | |
671 | } | |
672 | ||
673 | return FALSE; | |
674 | } | |
675 | ||
676 | bool wxApp::CheckForKeyUp(WXEvent* event) | |
677 | { | |
678 | XEvent* xEvent = (XEvent*) event; | |
679 | if (xEvent->xany.type == KeyRelease) | |
680 | { | |
681 | Widget widget = XtWindowToWidget((Display*) wxGetDisplay(), | |
682 | xEvent->xany.window); | |
683 | wxWindow* win = NULL; | |
ee31c392 | 684 | |
3d9431bf MB |
685 | // Find the first wxWindow that corresponds to this event window |
686 | while (widget && !(win = wxGetWindowFromTable(widget))) | |
687 | widget = XtParent(widget); | |
ee31c392 | 688 | |
3d9431bf MB |
689 | if (!widget || !win) |
690 | return FALSE; | |
ee31c392 | 691 | |
3d9431bf MB |
692 | wxKeyEvent keyEvent(wxEVT_KEY_UP); |
693 | wxTranslateKeyEvent(keyEvent, win, (Widget) 0, xEvent); | |
ee31c392 | 694 | |
3d9431bf | 695 | return win->ProcessEvent( keyEvent ); |
ee31c392 | 696 | } |
ee31c392 VZ |
697 | |
698 | return FALSE; | |
699 | } | |
700 | ||
4bb6408c JS |
701 | void wxExit() |
702 | { | |
703 | int retValue = 0; | |
704 | if (wxTheApp) | |
2d120f83 | 705 | retValue = wxTheApp->OnExit(); |
afb74891 | 706 | |
4bb6408c JS |
707 | wxApp::CleanUp(); |
708 | /* | |
2d120f83 JS |
709 | * Exit in some platform-specific way. Not recommended that the app calls this: |
710 | * only for emergencies. | |
711 | */ | |
4bb6408c JS |
712 | exit(retValue); |
713 | } | |
714 | ||
715 | // Yield to other processes | |
716 | bool wxYield() | |
717 | { | |
718 | while (wxTheApp && wxTheApp->Pending()) | |
2d120f83 | 719 | wxTheApp->Dispatch(); |
518b5d2f VZ |
720 | |
721 | // VZ: is it the same as this (taken from old wxExecute)? | |
722 | #if 0 | |
723 | XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll); | |
724 | #endif | |
725 | ||
4bb6408c JS |
726 | return TRUE; |
727 | } | |
728 | ||
ee31c392 VZ |
729 | // TODO use XmGetPixmap (?) to get the really standard icons! |
730 | ||
731 | #include "wx/generic/info.xpm" | |
732 | #include "wx/generic/error.xpm" | |
733 | #include "wx/generic/question.xpm" | |
734 | #include "wx/generic/warning.xpm" | |
735 | ||
736 | wxIcon | |
737 | wxApp::GetStdIcon(int which) const | |
738 | { | |
739 | switch(which) | |
740 | { | |
741 | case wxICON_INFORMATION: | |
742 | return wxIcon(info_xpm); | |
743 | ||
744 | case wxICON_QUESTION: | |
745 | return wxIcon(question_xpm); | |
746 | ||
747 | case wxICON_EXCLAMATION: | |
748 | return wxIcon(warning_xpm); | |
749 | ||
750 | default: | |
751 | wxFAIL_MSG("requested non existent standard icon"); | |
752 | // still fall through | |
753 | ||
754 | case wxICON_HAND: | |
755 | return wxIcon(error_xpm); | |
756 | } | |
757 | } |