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