]> git.saurik.com Git - wxWidgets.git/blob - src/motif/app.cpp
Dialog unit mods; wxProp tidying
[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 }
251
252 bool wxApp::Initialized()
253 {
254 if (GetTopWindow())
255 return TRUE;
256 else
257 return FALSE;
258 }
259
260 int wxApp::MainLoop()
261 {
262 m_keepGoing = TRUE;
263
264 /*
265 * Sit around forever waiting to process X-events. Property Change
266 * event are handled special, because they have to refer to
267 * the root window rather than to a widget. therefore we can't
268 * use an Xt-eventhandler.
269 */
270
271 XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()),
272 XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())),
273 PropertyChangeMask);
274
275 XEvent event;
276
277 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
278 while (m_keepGoing)
279 {
280 XtAppNextEvent( (XtAppContext) wxTheApp->GetAppContext(), &event);
281 if(event.type == PropertyNotify)
282 {
283 HandlePropertyChange((WXEvent*) &event);
284 } else
285 {
286 // Terry Gitnick <terryg@scientech.com> - 1/21/98
287 /* if resize event, don't resize until the last resize event for this
288 window is recieved. Prevents flicker as windows are resized. */
289 if (event.type == ResizeRequest)
290 {
291 Display *disp = XtDisplay((Widget) wxTheApp->GetTopLevelWidget());
292 Window win = event.xany.window;
293 XEvent report;
294
295 // to avoid flicker
296 report = event;
297 while( XCheckTypedWindowEvent (disp, win, ResizeRequest, &report));
298 }
299 // TODO: when implementing refresh optimization, we can use
300 // XtAddExposureToRegion to expand the window's paint region.
301
302 XtDispatchEvent(&event);
303
304 DeletePendingObjects();
305 }
306 }
307
308 return 0;
309 }
310
311 // Returns TRUE if more time is needed.
312 bool wxApp::ProcessIdle()
313 {
314 wxIdleEvent event;
315 event.SetEventObject(this);
316 ProcessEvent(event);
317
318 return event.MoreRequested();
319 }
320
321 void wxApp::ExitMainLoop()
322 {
323 m_keepGoing = FALSE;
324 }
325
326 // Is a message/event pending?
327 bool wxApp::Pending()
328 {
329 XFlush(XtDisplay( (Widget) wxTheApp->GetTopLevelWidget() ));
330 return (XtAppPending( (XtAppContext) wxTheApp->GetAppContext() ) != 0) ;
331 }
332
333 // Dispatch a message.
334 void wxApp::Dispatch()
335 {
336 XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
337 }
338
339 // This should be redefined in a derived class for
340 // handling property change events for XAtom IPC.
341 void wxApp::HandlePropertyChange(WXEvent *event)
342 {
343 // by default do nothing special
344 XtDispatchEvent((XEvent*) event); /* let Motif do the work */
345 }
346
347 void wxApp::OnIdle(wxIdleEvent& event)
348 {
349 static bool inOnIdle = FALSE;
350
351 // Avoid recursion (via ProcessEvent default case)
352 if (inOnIdle)
353 return;
354
355 inOnIdle = TRUE;
356
357 // 'Garbage' collection of windows deleted with Close().
358 DeletePendingObjects();
359
360 // flush the logged messages if any
361 wxLog *pLog = wxLog::GetActiveTarget();
362 if ( pLog != NULL && pLog->HasPendingMessages() )
363 pLog->Flush();
364
365 // Send OnIdle events to all windows
366 bool needMore = SendIdleEvents();
367
368 if (needMore)
369 event.RequestMore(TRUE);
370
371 inOnIdle = FALSE;
372 }
373
374 // Send idle event to all top-level windows
375 bool wxApp::SendIdleEvents()
376 {
377 bool needMore = FALSE;
378 wxNode* node = wxTopLevelWindows.First();
379 while (node)
380 {
381 wxWindow* win = (wxWindow*) node->Data();
382 if (SendIdleEvents(win))
383 needMore = TRUE;
384
385 node = node->Next();
386 }
387 return needMore;
388 }
389
390 // Send idle event to window and all subwindows
391 bool wxApp::SendIdleEvents(wxWindow* win)
392 {
393 bool needMore = FALSE;
394
395 wxIdleEvent event;
396 event.SetEventObject(win);
397 win->ProcessEvent(event);
398
399 if (event.MoreRequested())
400 needMore = TRUE;
401
402 wxNode* node = win->GetChildren()->First();
403 while (node)
404 {
405 wxWindow* win = (wxWindow*) node->Data();
406 if (SendIdleEvents(win))
407 needMore = TRUE;
408
409 node = node->Next();
410 }
411 return needMore ;
412 }
413
414 void wxApp::DeletePendingObjects()
415 {
416 wxNode *node = wxPendingDelete.First();
417 while (node)
418 {
419 wxObject *obj = (wxObject *)node->Data();
420
421 delete obj;
422
423 if (wxPendingDelete.Member(obj))
424 delete node;
425
426 // Deleting one object may have deleted other pending
427 // objects, so start from beginning of list again.
428 node = wxPendingDelete.First();
429 }
430 }
431
432 wxLog* wxApp::CreateLogTarget()
433 {
434 return new wxLogGui;
435 }
436
437 wxWindow* wxApp::GetTopWindow() const
438 {
439 if (m_topWindow)
440 return m_topWindow;
441 else if (wxTopLevelWindows.Number() > 0)
442 return (wxWindow*) wxTopLevelWindows.First()->Data();
443 else
444 return NULL;
445 }
446
447 // Create an application context
448 bool wxApp::OnInitGui()
449 {
450 XtToolkitInitialize() ;
451 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext() ;
452 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL,
453 (const char*) wxTheApp->GetClassName(), NULL,
454 # if XtSpecificationRelease < 5
455 0,(Cardinal*) &argc,argv) ;
456 # else
457 0,&argc,argv) ;
458 # endif
459 if (!dpy) {
460 cerr << "wxWindows could not open display for " << wxTheApp->GetClassName() << ": exiting.\n";
461 exit(-1);
462 }
463 wxTheApp->m_topLevelWidget = (WXWidget) XtAppCreateShell((String)NULL, (const char*) wxTheApp->GetClassName(),
464 applicationShellWidgetClass,dpy,
465 NULL,0) ;
466
467 // Add general resize proc
468 XtActionsRec rec;
469 rec.string = "resize";
470 rec.proc = (XtActionProc)wxWidgetResizeProc;
471 XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1);
472
473 GetMainColormap(dpy);
474 m_maxRequestSize = XMaxRequestSize((Display*) dpy);
475
476 return TRUE;
477 }
478
479 WXColormap wxApp::GetMainColormap(WXDisplay* display)
480 {
481 if (!display) /* Must be called first with non-NULL display */
482 return m_mainColormap;
483
484 Colormap c =
485 DefaultColormapOfScreen(XScreenOfDisplay((Display*) display,
486 DefaultScreen((Display*) display)));
487
488 if (!m_mainColormap)
489 m_mainColormap = (WXColormap) c;
490
491 return (WXColormap) c;
492 }
493
494 void wxExit()
495 {
496 int retValue = 0;
497 if (wxTheApp)
498 retValue = wxTheApp->OnExit();
499
500 wxApp::CleanUp();
501 /*
502 * Exit in some platform-specific way. Not recommended that the app calls this:
503 * only for emergencies.
504 */
505 exit(retValue);
506 }
507
508 // Yield to other processes
509 bool wxYield()
510 {
511 while (wxTheApp && wxTheApp->Pending())
512 wxTheApp->Dispatch();
513 return TRUE;
514 }
515