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