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