]> git.saurik.com Git - wxWidgets.git/blob - src/motif/app.cpp
More Motif additions: mdi and sashtest samples now just about work!
[wxWidgets.git] / src / motif / app.cpp
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"
31
32 #if wxUSE_WX_RESOURCES
33 #include "wx/resource.h"
34 #endif
35
36 #include <Xm/Xm.h>
37 #include <X11/Xlib.h>
38 #include <X11/Xutil.h>
39 #include <X11/Xresource.h>
40 #include <X11/Xatom.h>
41
42 #include "wx/motif/private.h"
43
44 #include <string.h>
45
46 extern char *wxBuffer;
47 extern wxList wxPendingDelete;
48
49 wxApp *wxTheApp = NULL;
50
51 wxHashTable *wxWidgetHashTable = NULL;
52
53 #if !USE_SHARED_LIBRARY
54 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
55 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
56 EVT_IDLE(wxApp::OnIdle)
57 END_EVENT_TABLE()
58 #endif
59
60 long wxApp::sm_lastMessageTime = 0;
61
62 bool wxApp::Initialize()
63 {
64 #ifdef __WXMSW__
65 wxBuffer = new char[1500];
66 #else
67 wxBuffer = new char[BUFSIZ + 512];
68 #endif
69
70 #if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
71
72 streambuf* sBuf = new wxDebugStreamBuf;
73 ostream* oStr = new ostream(sBuf) ;
74 wxDebugContext::SetStream(oStr, sBuf);
75
76 #endif
77
78 wxClassInfo::InitializeClasses();
79
80 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
81 wxTheColourDatabase->Initialize();
82 wxInitializeStockObjects();
83
84 #if wxUSE_WX_RESOURCES
85 wxInitializeResourceSystem();
86 #endif
87
88 // For PostScript printing
89 #if wxUSE_POSTSCRIPT
90 /* Done using wxModule now
91 wxInitializePrintSetupData();
92 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
93 wxThePrintPaperDatabase->CreateDatabase();
94 */
95 #endif
96
97 wxBitmap::InitStandardHandlers();
98
99 wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
100
101 wxModule::RegisterModules();
102 wxASSERT( wxModule::InitializeModules() == TRUE );
103
104 return TRUE;
105 }
106
107 void wxApp::CleanUp()
108 {
109 delete wxWidgetHashTable;
110 wxWidgetHashTable = NULL;
111
112 wxModule::CleanUpModules();
113
114 #if wxUSE_WX_RESOURCES
115 wxCleanUpResourceSystem();
116 #endif
117
118 wxDeleteStockObjects() ;
119
120 // Destroy all GDI lists, etc.
121
122 delete wxTheBrushList;
123 wxTheBrushList = NULL;
124
125 delete wxThePenList;
126 wxThePenList = NULL;
127
128 delete wxTheFontList;
129 wxTheFontList = NULL;
130
131 delete wxTheBitmapList;
132 wxTheBitmapList = NULL;
133
134 delete wxTheColourDatabase;
135 wxTheColourDatabase = NULL;
136
137 #if wxUSE_POSTSCRIPT
138 /* Done using wxModule now
139 wxInitializePrintSetupData(FALSE);
140 delete wxThePrintPaperDatabase;
141 wxThePrintPaperDatabase = NULL;
142 */
143 #endif
144
145 wxBitmap::CleanUpHandlers();
146
147 delete[] wxBuffer;
148 wxBuffer = NULL;
149
150 wxClassInfo::CleanUpClasses();
151
152 // do it as the very last thing because everything else can log messages
153 wxLog::DontCreateOnDemand();
154 // do it as the very last thing because everything else can log messages
155 delete wxLog::SetActiveTarget(NULL);
156 }
157
158 int wxEntry( int argc, char *argv[] )
159 {
160 if (!wxApp::Initialize())
161 return FALSE;
162 if (!wxTheApp)
163 {
164 if (!wxApp::GetInitializerFunction())
165 {
166 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
167 return 0;
168 };
169
170 wxTheApp = (wxApp*) (* wxApp::GetInitializerFunction()) ();
171 };
172
173 if (!wxTheApp)
174 {
175 printf( "wxWindows error: wxTheApp == NULL\n" );
176 return 0;
177 };
178
179 wxTheApp->SetClassName(wxFileNameFromPath(argv[0]));
180 wxTheApp->SetAppName(wxFileNameFromPath(argv[0]));
181
182 wxTheApp->argc = argc;
183 wxTheApp->argv = argv;
184
185 // GUI-specific initialization, such as creating an app context.
186 wxTheApp->OnInitGui();
187
188 // Here frames insert themselves automatically
189 // into wxTopLevelWindows by getting created
190 // in OnInit().
191
192 if (!wxTheApp->OnInit()) return 0;
193
194 int retValue = 0;
195
196 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
197
198 if (wxTheApp->GetTopWindow())
199 {
200 delete wxTheApp->GetTopWindow();
201 wxTheApp->SetTopWindow(NULL);
202 }
203
204 wxTheApp->DeletePendingObjects();
205
206 wxTheApp->OnExit();
207
208 wxApp::CleanUp();
209
210 delete wxTheApp;
211 wxTheApp = NULL;
212
213 #if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
214 // At this point we want to check if there are any memory
215 // blocks that aren't part of the wxDebugContext itself,
216 // as a special case. Then when dumping we need to ignore
217 // wxDebugContext, too.
218 if (wxDebugContext::CountObjectsLeft() > 0)
219 {
220 wxTrace("There were memory leaks.\n");
221 wxDebugContext::Dump();
222 wxDebugContext::PrintStatistics();
223 }
224 wxDebugContext::SetStream(NULL, NULL);
225 #endif
226
227 return retValue;
228 };
229
230 // Static member initialization
231 wxAppInitializerFunction wxApp::m_appInitFn = (wxAppInitializerFunction) NULL;
232
233 wxApp::wxApp()
234 {
235 m_topWindow = NULL;
236 wxTheApp = this;
237 m_className = "";
238 m_wantDebugOutput = TRUE ;
239 m_appName = "";
240 argc = 0;
241 argv = NULL;
242 m_printMode = wxPRINT_POSTSCRIPT;
243 m_exitOnFrameDelete = TRUE;
244 m_auto3D = TRUE;
245
246 m_mainColormap = (WXColormap) NULL;
247 m_appContext = (WXAppContext) NULL;
248 m_topLevelWidget = (WXWidget) NULL;
249 m_maxRequestSize = 0;
250 m_initialDisplay = (WXDisplay*) 0;
251 }
252
253 bool wxApp::Initialized()
254 {
255 if (GetTopWindow())
256 return TRUE;
257 else
258 return FALSE;
259 }
260
261 int wxApp::MainLoop()
262 {
263 m_keepGoing = TRUE;
264
265 /*
266 * Sit around forever waiting to process X-events. Property Change
267 * event are handled special, because they have to refer to
268 * the root window rather than to a widget. therefore we can't
269 * use an Xt-eventhandler.
270 */
271
272 XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()),
273 XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())),
274 PropertyChangeMask);
275
276 XEvent event;
277
278 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
279 while (m_keepGoing)
280 {
281 XtAppNextEvent( (XtAppContext) wxTheApp->GetAppContext(), &event);
282 if(event.type == PropertyNotify)
283 {
284 HandlePropertyChange((WXEvent*) &event);
285 } else
286 {
287 // Terry Gitnick <terryg@scientech.com> - 1/21/98
288 /* if resize event, don't resize until the last resize event for this
289 window is recieved. Prevents flicker as windows are resized. */
290 if (event.type == ResizeRequest)
291 {
292 Display *disp = XtDisplay((Widget) wxTheApp->GetTopLevelWidget());
293 Window win = event.xany.window;
294 XEvent report;
295
296 // to avoid flicker
297 report = event;
298 while( XCheckTypedWindowEvent (disp, win, ResizeRequest, &report));
299 }
300 // TODO: when implementing refresh optimization, we can use
301 // XtAddExposureToRegion to expand the window's paint region.
302
303 XtDispatchEvent(&event);
304
305 DeletePendingObjects();
306 }
307 }
308
309 return 0;
310 }
311
312 // Returns TRUE if more time is needed.
313 bool wxApp::ProcessIdle()
314 {
315 wxIdleEvent event;
316 event.SetEventObject(this);
317 ProcessEvent(event);
318
319 return event.MoreRequested();
320 }
321
322 void wxApp::ExitMainLoop()
323 {
324 m_keepGoing = FALSE;
325 }
326
327 // Is a message/event pending?
328 bool wxApp::Pending()
329 {
330 XFlush(XtDisplay( (Widget) wxTheApp->GetTopLevelWidget() ));
331 return (XtAppPending( (XtAppContext) wxTheApp->GetAppContext() ) != 0) ;
332 }
333
334 // Dispatch a message.
335 void wxApp::Dispatch()
336 {
337 XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
338 }
339
340 // This should be redefined in a derived class for
341 // handling property change events for XAtom IPC.
342 void wxApp::HandlePropertyChange(WXEvent *event)
343 {
344 // by default do nothing special
345 XtDispatchEvent((XEvent*) event); /* let Motif do the work */
346 }
347
348 void wxApp::OnIdle(wxIdleEvent& event)
349 {
350 static bool inOnIdle = FALSE;
351
352 // Avoid recursion (via ProcessEvent default case)
353 if (inOnIdle)
354 return;
355
356 inOnIdle = TRUE;
357
358 // 'Garbage' collection of windows deleted with Close().
359 DeletePendingObjects();
360
361 // flush the logged messages if any
362 wxLog *pLog = wxLog::GetActiveTarget();
363 if ( pLog != NULL && pLog->HasPendingMessages() )
364 pLog->Flush();
365
366 // Send OnIdle events to all windows
367 bool needMore = SendIdleEvents();
368
369 if (needMore)
370 event.RequestMore(TRUE);
371
372 inOnIdle = FALSE;
373 }
374
375 // Send idle event to all top-level windows
376 bool wxApp::SendIdleEvents()
377 {
378 bool needMore = FALSE;
379 wxNode* node = wxTopLevelWindows.First();
380 while (node)
381 {
382 wxWindow* win = (wxWindow*) node->Data();
383 if (SendIdleEvents(win))
384 needMore = TRUE;
385
386 node = node->Next();
387 }
388 return needMore;
389 }
390
391 // Send idle event to window and all subwindows
392 bool wxApp::SendIdleEvents(wxWindow* win)
393 {
394 bool needMore = FALSE;
395
396 wxIdleEvent event;
397 event.SetEventObject(win);
398 win->ProcessEvent(event);
399
400 if (event.MoreRequested())
401 needMore = TRUE;
402
403 wxNode* node = win->GetChildren()->First();
404 while (node)
405 {
406 wxWindow* win = (wxWindow*) node->Data();
407 if (SendIdleEvents(win))
408 needMore = TRUE;
409
410 node = node->Next();
411 }
412 return needMore ;
413 }
414
415 void wxApp::DeletePendingObjects()
416 {
417 wxNode *node = wxPendingDelete.First();
418 while (node)
419 {
420 wxObject *obj = (wxObject *)node->Data();
421
422 delete obj;
423
424 if (wxPendingDelete.Member(obj))
425 delete node;
426
427 // Deleting one object may have deleted other pending
428 // objects, so start from beginning of list again.
429 node = wxPendingDelete.First();
430 }
431 }
432
433 wxLog* wxApp::CreateLogTarget()
434 {
435 return new wxLogGui;
436 }
437
438 wxWindow* wxApp::GetTopWindow() const
439 {
440 if (m_topWindow)
441 return m_topWindow;
442 else if (wxTopLevelWindows.Number() > 0)
443 return (wxWindow*) wxTopLevelWindows.First()->Data();
444 else
445 return NULL;
446 }
447
448 // Create an application context
449 bool wxApp::OnInitGui()
450 {
451 XtToolkitInitialize() ;
452 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext() ;
453 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL,
454 (const char*) wxTheApp->GetClassName(), NULL,
455 # if XtSpecificationRelease < 5
456 0,(Cardinal*) &argc,argv) ;
457 # else
458 0,&argc,argv) ;
459 # endif
460 if (!dpy) {
461 cerr << "wxWindows could not open display for " << wxTheApp->GetClassName() << ": exiting.\n";
462 exit(-1);
463 }
464 m_initialDisplay = (WXDisplay*) dpy;
465
466 wxTheApp->m_topLevelWidget = (WXWidget) XtAppCreateShell((String)NULL, (const char*) wxTheApp->GetClassName(),
467 applicationShellWidgetClass,dpy,
468 NULL,0) ;
469
470 // Add general resize proc
471 XtActionsRec rec;
472 rec.string = "resize";
473 rec.proc = (XtActionProc)wxWidgetResizeProc;
474 XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1);
475
476 GetMainColormap(dpy);
477 m_maxRequestSize = XMaxRequestSize((Display*) dpy);
478
479 return TRUE;
480 }
481
482 WXColormap wxApp::GetMainColormap(WXDisplay* display)
483 {
484 if (!display) /* Must be called first with non-NULL display */
485 return m_mainColormap;
486
487 Colormap c =
488 DefaultColormapOfScreen(XScreenOfDisplay((Display*) display,
489 DefaultScreen((Display*) display)));
490
491 if (!m_mainColormap)
492 m_mainColormap = (WXColormap) c;
493
494 return (WXColormap) c;
495 }
496
497 void wxExit()
498 {
499 int retValue = 0;
500 if (wxTheApp)
501 retValue = wxTheApp->OnExit();
502
503 wxApp::CleanUp();
504 /*
505 * Exit in some platform-specific way. Not recommended that the app calls this:
506 * only for emergencies.
507 */
508 exit(retValue);
509 }
510
511 // Yield to other processes
512 bool wxYield()
513 {
514 while (wxTheApp && wxTheApp->Pending())
515 wxTheApp->Dispatch();
516 return TRUE;
517 }
518