]> git.saurik.com Git - wxWidgets.git/blame - src/motif/app.cpp
wxSplitPath() bugs corrected and it's documented
[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
9// Licence: wxWindows licence
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
2d120f83 73
4bb6408c 74 wxClassInfo::InitializeClasses();
2d120f83 75
4bb6408c
JS
76 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
77 wxTheColourDatabase->Initialize();
2d120f83 78
4b5f3fe6 79 wxInitializeStockLists();
4bb6408c 80 wxInitializeStockObjects();
2d120f83 81
47d67540 82#if wxUSE_WX_RESOURCES
4bb6408c
JS
83 wxInitializeResourceSystem();
84#endif
2d120f83
JS
85
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
2d120f83 94
4bb6408c 95 wxBitmap::InitStandardHandlers();
2d120f83 96
4bb6408c 97 wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
2d120f83 98
4bb6408c 99 wxModule::RegisterModules();
aaa38880 100 if (!wxModule::InitializeModules()) return FALSE;
2d120f83 101
4bb6408c
JS
102 return TRUE;
103}
104
105void wxApp::CleanUp()
106{
107 delete wxWidgetHashTable;
108 wxWidgetHashTable = NULL;
2d120f83 109
4bb6408c 110 wxModule::CleanUpModules();
2d120f83 111
47d67540 112#if wxUSE_WX_RESOURCES
4bb6408c
JS
113 wxCleanUpResourceSystem();
114#endif
2d120f83 115
4bb6408c 116 wxDeleteStockObjects() ;
2d120f83 117
4bb6408c 118 // Destroy all GDI lists, etc.
2d120f83 119
4bb6408c
JS
120 delete wxTheBrushList;
121 wxTheBrushList = NULL;
2d120f83 122
4bb6408c
JS
123 delete wxThePenList;
124 wxThePenList = NULL;
2d120f83 125
4bb6408c
JS
126 delete wxTheFontList;
127 wxTheFontList = NULL;
2d120f83 128
4bb6408c
JS
129 delete wxTheBitmapList;
130 wxTheBitmapList = NULL;
2d120f83 131
4bb6408c
JS
132 delete wxTheColourDatabase;
133 wxTheColourDatabase = NULL;
2d120f83 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
2d120f83 142
4bb6408c 143 wxBitmap::CleanUpHandlers();
2d120f83 144
4bb6408c
JS
145 delete[] wxBuffer;
146 wxBuffer = NULL;
2d120f83 147
4bb6408c 148 wxClassInfo::CleanUpClasses();
2d120f83 149
184b5d99
JS
150 delete wxTheApp;
151 wxTheApp = NULL;
2d120f83 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
2d120f83 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
2d120f83 183
4bb6408c 184 if (!wxApp::Initialize())
2d120f83
JS
185 return FALSE;
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 };
194
195 wxTheApp = (wxApp*) (* wxApp::GetInitializerFunction()) ();
4bb6408c 196 };
2d120f83 197
4bb6408c
JS
198 if (!wxTheApp)
199 {
2d120f83
JS
200 printf( "wxWindows error: wxTheApp == NULL\n" );
201 return 0;
4bb6408c 202 };
2d120f83 203
4bb6408c
JS
204 wxTheApp->SetClassName(wxFileNameFromPath(argv[0]));
205 wxTheApp->SetAppName(wxFileNameFromPath(argv[0]));
2d120f83 206
4bb6408c
JS
207 wxTheApp->argc = argc;
208 wxTheApp->argv = argv;
2d120f83 209
4bb6408c
JS
210 // GUI-specific initialization, such as creating an app context.
211 wxTheApp->OnInitGui();
2d120f83 212
4bb6408c
JS
213 // Here frames insert themselves automatically
214 // into wxTopLevelWindows by getting created
215 // in OnInit().
2d120f83 216
4bb6408c 217 int retValue = 0;
4fabb575
JS
218 if (wxTheApp->OnInit())
219 {
2d120f83 220 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
4fabb575 221 }
2d120f83 222
f57fe24c
JS
223 // flush the logged messages if any
224 wxLog *pLog = wxLog::GetActiveTarget();
225 if ( pLog != NULL && pLog->HasPendingMessages() )
2d120f83
JS
226 pLog->Flush();
227
f57fe24c 228 delete wxLog::SetActiveTarget(new wxLogStderr); // So dialog boxes aren't used
2d120f83
JS
229 // for further messages
230
4bb6408c
JS
231 if (wxTheApp->GetTopWindow())
232 {
2d120f83
JS
233 delete wxTheApp->GetTopWindow();
234 wxTheApp->SetTopWindow(NULL);
4bb6408c 235 }
2d120f83 236
4bb6408c 237 wxTheApp->DeletePendingObjects();
2d120f83 238
4bb6408c 239 wxTheApp->OnExit();
2d120f83 240
4bb6408c 241 wxApp::CleanUp();
2d120f83 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;
2d120f83 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;
2d120f83
JS
280
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 */
2d120f83 287
4bb6408c 288 XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()),
2d120f83
JS
289 XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())),
290 PropertyChangeMask);
291
4bb6408c 292 XEvent event;
2d120f83 293
4bb6408c
JS
294 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
295 while (m_keepGoing)
296 {
2d120f83
JS
297 XtAppNextEvent( (XtAppContext) wxTheApp->GetAppContext(), &event);
298
299 ProcessXEvent((WXEvent*) & event);
300
301 if (XtAppPending( (XtAppContext) wxTheApp->GetAppContext() ) == 0)
302 {
303 if (!ProcessIdle())
304 {
305 // TODO: Robert, what's this for?
b412f9be 306#if wxUSE_THREADS
2d120f83
JS
307 wxMutexGuiLeave();
308 usleep(20);
309 wxMutexGuiEnter();
b412f9be 310#endif
2d120f83
JS
311 }
312 }
313
8aa04e8b 314 }
2d120f83 315
8aa04e8b
JS
316 return 0;
317}
318
319// Processes an X event.
320void wxApp::ProcessXEvent(WXEvent* _event)
321{
322 XEvent* event = (XEvent*) _event;
2d120f83 323
f57fe24c 324 if ((event->type == KeyPress) && CheckForAccelerator(_event))
8aa04e8b
JS
325 {
326 // Do nothing! We intercepted and processed the event as an accelerator.
327 return;
328 }
329 else if (event->type == PropertyNotify)
330 {
331 HandlePropertyChange(_event);
332 return;
333 }
334 else if (event->type == ResizeRequest)
335 {
2d120f83
JS
336 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
337 * If resize event, don't resize until the last resize event for this
338 * window is recieved. Prevents flicker as windows are resized.
339 */
340
8aa04e8b
JS
341 Display *disp = XtDisplay((Widget) wxTheApp->GetTopLevelWidget());
342 Window win = event->xany.window;
343 XEvent report;
2d120f83 344
8aa04e8b
JS
345 // to avoid flicker
346 report = * event;
347 while( XCheckTypedWindowEvent (disp, win, ResizeRequest, &report));
2d120f83 348
f97c9854
JS
349 // TODO: when implementing refresh optimization, we can use
350 // XtAddExposureToRegion to expand the window's paint region.
2d120f83 351
8aa04e8b
JS
352 XtDispatchEvent(event);
353 }
354 else
355 {
356 XtDispatchEvent(event);
4bb6408c 357 }
4bb6408c
JS
358}
359
360// Returns TRUE if more time is needed.
361bool wxApp::ProcessIdle()
362{
363 wxIdleEvent event;
364 event.SetEventObject(this);
365 ProcessEvent(event);
2d120f83 366
4bb6408c
JS
367 return event.MoreRequested();
368}
369
370void wxApp::ExitMainLoop()
371{
372 m_keepGoing = FALSE;
373}
374
375// Is a message/event pending?
376bool wxApp::Pending()
377{
378 XFlush(XtDisplay( (Widget) wxTheApp->GetTopLevelWidget() ));
2d120f83 379
8aa04e8b
JS
380 // Fix by Doug from STI, to prevent a stall if non-X event
381 // is found.
382 return ((XtAppPending( (XtAppContext) GetAppContext() ) & XtIMXEvent) != 0) ;
4bb6408c
JS
383}
384
385// Dispatch a message.
386void wxApp::Dispatch()
387{
2d120f83
JS
388 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
389
8aa04e8b
JS
390 XEvent event;
391 XtAppNextEvent((XtAppContext) GetAppContext(), &event);
392 ProcessXEvent((WXEvent*) & event);
4bb6408c
JS
393}
394
395// This should be redefined in a derived class for
396// handling property change events for XAtom IPC.
397void wxApp::HandlePropertyChange(WXEvent *event)
398{
399 // by default do nothing special
400 XtDispatchEvent((XEvent*) event); /* let Motif do the work */
401}
402
403void wxApp::OnIdle(wxIdleEvent& event)
404{
405 static bool inOnIdle = FALSE;
2d120f83 406
4bb6408c
JS
407 // Avoid recursion (via ProcessEvent default case)
408 if (inOnIdle)
2d120f83
JS
409 return;
410
4bb6408c 411 inOnIdle = TRUE;
2d120f83 412
4bb6408c
JS
413 // 'Garbage' collection of windows deleted with Close().
414 DeletePendingObjects();
2d120f83 415
4bb6408c
JS
416 // flush the logged messages if any
417 wxLog *pLog = wxLog::GetActiveTarget();
418 if ( pLog != NULL && pLog->HasPendingMessages() )
2d120f83
JS
419 pLog->Flush();
420
4bb6408c
JS
421 // Send OnIdle events to all windows
422 bool needMore = SendIdleEvents();
2d120f83 423
4bb6408c 424 if (needMore)
2d120f83
JS
425 event.RequestMore(TRUE);
426
4bb6408c
JS
427 inOnIdle = FALSE;
428}
429
430// Send idle event to all top-level windows
431bool wxApp::SendIdleEvents()
432{
433 bool needMore = FALSE;
2d120f83
JS
434 wxNode* node = wxTopLevelWindows.First();
435 while (node)
436 {
437 wxWindow* win = (wxWindow*) node->Data();
438 if (SendIdleEvents(win))
4bb6408c 439 needMore = TRUE;
2d120f83
JS
440
441 node = node->Next();
442 }
4bb6408c
JS
443 return needMore;
444}
445
446// Send idle event to window and all subwindows
447bool wxApp::SendIdleEvents(wxWindow* win)
448{
449 bool needMore = FALSE;
2d120f83
JS
450
451 wxIdleEvent event;
452 event.SetEventObject(win);
453 win->ProcessEvent(event);
454
4bb6408c
JS
455 if (event.MoreRequested())
456 needMore = TRUE;
2d120f83
JS
457
458 wxNode* node = win->GetChildren().First();
459 while (node)
460 {
461 wxWindow* win = (wxWindow*) node->Data();
462 if (SendIdleEvents(win))
4bb6408c 463 needMore = TRUE;
2d120f83
JS
464
465 node = node->Next();
466 }
4bb6408c
JS
467 return needMore ;
468}
469
470void wxApp::DeletePendingObjects()
471{
472 wxNode *node = wxPendingDelete.First();
473 while (node)
474 {
2d120f83
JS
475 wxObject *obj = (wxObject *)node->Data();
476
477 delete obj;
478
479 if (wxPendingDelete.Member(obj))
480 delete node;
481
482 // Deleting one object may have deleted other pending
483 // objects, so start from beginning of list again.
484 node = wxPendingDelete.First();
4bb6408c
JS
485 }
486}
487
488wxLog* wxApp::CreateLogTarget()
489{
490 return new wxLogGui;
491}
492
493wxWindow* wxApp::GetTopWindow() const
494{
495 if (m_topWindow)
496 return m_topWindow;
497 else if (wxTopLevelWindows.Number() > 0)
498 return (wxWindow*) wxTopLevelWindows.First()->Data();
499 else
500 return NULL;
501}
502
503// Create an application context
504bool wxApp::OnInitGui()
505{
506 XtToolkitInitialize() ;
507 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext() ;
508 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL,
2d120f83 509 (const char*) wxTheApp->GetClassName(), NULL,
4bb6408c 510# if XtSpecificationRelease < 5
2d120f83 511 0,(Cardinal*) &argc,argv) ;
4bb6408c 512# else
2d120f83 513 0,&argc,argv) ;
4bb6408c
JS
514# endif
515 if (!dpy) {
2d120f83
JS
516 cerr << "wxWindows could not open display for " << wxTheApp->GetClassName() << ": exiting.\n";
517 exit(-1);
4bb6408c 518 }
47bc1060 519 m_initialDisplay = (WXDisplay*) dpy;
2d120f83 520
4bb6408c 521 wxTheApp->m_topLevelWidget = (WXWidget) XtAppCreateShell((String)NULL, (const char*) wxTheApp->GetClassName(),
2d120f83
JS
522 applicationShellWidgetClass,dpy,
523 NULL,0) ;
524
4bb6408c
JS
525 // Add general resize proc
526 XtActionsRec rec;
527 rec.string = "resize";
528 rec.proc = (XtActionProc)wxWidgetResizeProc;
529 XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1);
2d120f83 530
4bb6408c
JS
531 GetMainColormap(dpy);
532 m_maxRequestSize = XMaxRequestSize((Display*) dpy);
2d120f83 533
4bb6408c
JS
534 return TRUE;
535}
536
537WXColormap wxApp::GetMainColormap(WXDisplay* display)
538{
539 if (!display) /* Must be called first with non-NULL display */
2d120f83 540 return m_mainColormap;
a91b47e8
JS
541
542 int defaultScreen = DefaultScreen((Display*) display);
543 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
2d120f83 544
a91b47e8 545 Colormap c = DefaultColormapOfScreen(screen);
2d120f83 546
4bb6408c 547 if (!m_mainColormap)
2d120f83
JS
548 m_mainColormap = (WXColormap) c;
549
4bb6408c
JS
550 return (WXColormap) c;
551}
552
8aa04e8b
JS
553// Returns TRUE if an accelerator has been processed
554bool wxApp::CheckForAccelerator(WXEvent* event)
555{
556 XEvent* xEvent = (XEvent*) event;
557 if (xEvent->xany.type == KeyPress)
558 {
559 // Find a wxWindow for this window
560 // TODO: should get display for the window, not the current display
561 Widget widget = XtWindowToWidget((Display*) wxGetDisplay(), xEvent->xany.window);
562 wxWindow* win = NULL;
2d120f83 563
8aa04e8b
JS
564 // Find the first wxWindow that corresponds to this event window
565 while (widget && !(win = wxGetWindowFromTable(widget)))
566 widget = XtParent(widget);
2d120f83 567
8aa04e8b
JS
568 if (!widget || !win)
569 return FALSE;
2d120f83 570
8aa04e8b
JS
571 wxKeyEvent keyEvent(wxEVT_CHAR);
572 wxTranslateKeyEvent(keyEvent, win, (Widget) 0, xEvent);
2d120f83 573
8aa04e8b
JS
574 // Now we have a wxKeyEvent and we have a wxWindow.
575 // Go up the hierarchy until we find a matching accelerator,
576 // or we get to the top.
577 while (win)
578 {
579 if (win->ProcessAccelerator(keyEvent))
580 return TRUE;
581 win = win->GetParent();
582 }
583 return FALSE;
584 }
585 return FALSE;
586}
587
4bb6408c
JS
588void wxExit()
589{
590 int retValue = 0;
591 if (wxTheApp)
2d120f83
JS
592 retValue = wxTheApp->OnExit();
593
4bb6408c
JS
594 wxApp::CleanUp();
595 /*
2d120f83
JS
596 * Exit in some platform-specific way. Not recommended that the app calls this:
597 * only for emergencies.
598 */
4bb6408c
JS
599 exit(retValue);
600}
601
602// Yield to other processes
603bool wxYield()
604{
605 while (wxTheApp && wxTheApp->Pending())
2d120f83 606 wxTheApp->Dispatch();
4bb6408c
JS
607 return TRUE;
608}
609