]> git.saurik.com Git - wxWidgets.git/blob - src/motif/app.cpp
Motif files added.
[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 USE_WX_RESOURCES
33 #include "wx/resource.h"
34 #endif
35
36 #if USE_POSTSCRIPT
37 #include "wx/postscrp.h"
38 #endif
39
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
50 extern char *wxBuffer;
51 extern wxList wxPendingDelete;
52
53 wxApp *wxTheApp = NULL;
54
55 wxHashTable *wxWidgetHashTable = NULL;
56
57 #if !USE_SHARED_LIBRARY
58 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
59 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
60 EVT_IDLE(wxApp::OnIdle)
61 END_EVENT_TABLE()
62 #endif
63
64 long wxApp::sm_lastMessageTime = 0;
65
66 bool wxApp::Initialize()
67 {
68 #ifdef __WXMSW__
69 wxBuffer = new char[1500];
70 #else
71 wxBuffer = new char[BUFSIZ + 512];
72 #endif
73
74 #if (WXDEBUG && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
75
76 streambuf* sBuf = new wxDebugStreamBuf;
77 ostream* oStr = new ostream(sBuf) ;
78 wxDebugContext::SetStream(oStr, sBuf);
79
80 #endif
81
82 wxClassInfo::InitializeClasses();
83
84 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
85 wxTheColourDatabase->Initialize();
86 wxInitializeStockObjects();
87
88 #if USE_WX_RESOURCES
89 wxInitializeResourceSystem();
90 #endif
91
92 // For PostScript printing
93 #if USE_POSTSCRIPT
94 wxInitializePrintSetupData();
95 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
96 wxThePrintPaperDatabase->CreateDatabase();
97 #endif
98
99 wxBitmap::InitStandardHandlers();
100
101 wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
102
103 wxModule::RegisterModules();
104 wxASSERT( wxModule::InitializeModules() == TRUE );
105
106 return TRUE;
107 }
108
109 void wxApp::CleanUp()
110 {
111 delete wxWidgetHashTable;
112 wxWidgetHashTable = NULL;
113
114 wxModule::CleanUpModules();
115
116 #if USE_WX_RESOURCES
117 wxCleanUpResourceSystem();
118 #endif
119
120 wxDeleteStockObjects() ;
121
122 // Destroy all GDI lists, etc.
123
124 delete wxTheBrushList;
125 wxTheBrushList = NULL;
126
127 delete wxThePenList;
128 wxThePenList = NULL;
129
130 delete wxTheFontList;
131 wxTheFontList = NULL;
132
133 delete wxTheBitmapList;
134 wxTheBitmapList = NULL;
135
136 delete wxTheColourDatabase;
137 wxTheColourDatabase = NULL;
138
139 #if USE_POSTSCRIPT
140 wxInitializePrintSetupData(FALSE);
141 delete wxThePrintPaperDatabase;
142 wxThePrintPaperDatabase = NULL;
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 && USE_MEMORY_TRACING) || USE_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
300 XtDispatchEvent(&event);
301
302 DeletePendingObjects();
303 }
304 }
305
306 return 0;
307 }
308
309 // Returns TRUE if more time is needed.
310 bool wxApp::ProcessIdle()
311 {
312 wxIdleEvent event;
313 event.SetEventObject(this);
314 ProcessEvent(event);
315
316 return event.MoreRequested();
317 }
318
319 void wxApp::ExitMainLoop()
320 {
321 m_keepGoing = FALSE;
322 }
323
324 // Is a message/event pending?
325 bool wxApp::Pending()
326 {
327 XFlush(XtDisplay( (Widget) wxTheApp->GetTopLevelWidget() ));
328 return (XtAppPending( (XtAppContext) wxTheApp->GetAppContext() ) != 0) ;
329 }
330
331 // Dispatch a message.
332 void wxApp::Dispatch()
333 {
334 XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
335 }
336
337 // This should be redefined in a derived class for
338 // handling property change events for XAtom IPC.
339 void wxApp::HandlePropertyChange(WXEvent *event)
340 {
341 // by default do nothing special
342 XtDispatchEvent((XEvent*) event); /* let Motif do the work */
343 }
344
345 void wxApp::OnIdle(wxIdleEvent& event)
346 {
347 static bool inOnIdle = FALSE;
348
349 // Avoid recursion (via ProcessEvent default case)
350 if (inOnIdle)
351 return;
352
353 inOnIdle = TRUE;
354
355 // 'Garbage' collection of windows deleted with Close().
356 DeletePendingObjects();
357
358 // flush the logged messages if any
359 wxLog *pLog = wxLog::GetActiveTarget();
360 if ( pLog != NULL && pLog->HasPendingMessages() )
361 pLog->Flush();
362
363 // Send OnIdle events to all windows
364 bool needMore = SendIdleEvents();
365
366 if (needMore)
367 event.RequestMore(TRUE);
368
369 inOnIdle = FALSE;
370 }
371
372 // Send idle event to all top-level windows
373 bool wxApp::SendIdleEvents()
374 {
375 bool needMore = FALSE;
376 wxNode* node = wxTopLevelWindows.First();
377 while (node)
378 {
379 wxWindow* win = (wxWindow*) node->Data();
380 if (SendIdleEvents(win))
381 needMore = TRUE;
382
383 node = node->Next();
384 }
385 return needMore;
386 }
387
388 // Send idle event to window and all subwindows
389 bool wxApp::SendIdleEvents(wxWindow* win)
390 {
391 bool needMore = FALSE;
392
393 wxIdleEvent event;
394 event.SetEventObject(win);
395 win->ProcessEvent(event);
396
397 if (event.MoreRequested())
398 needMore = TRUE;
399
400 wxNode* node = win->GetChildren()->First();
401 while (node)
402 {
403 wxWindow* win = (wxWindow*) node->Data();
404 if (SendIdleEvents(win))
405 needMore = TRUE;
406
407 node = node->Next();
408 }
409 return needMore ;
410 }
411
412 void wxApp::DeletePendingObjects()
413 {
414 wxNode *node = wxPendingDelete.First();
415 while (node)
416 {
417 wxObject *obj = (wxObject *)node->Data();
418
419 delete obj;
420
421 if (wxPendingDelete.Member(obj))
422 delete node;
423
424 // Deleting one object may have deleted other pending
425 // objects, so start from beginning of list again.
426 node = wxPendingDelete.First();
427 }
428 }
429
430 wxLog* wxApp::CreateLogTarget()
431 {
432 return new wxLogGui;
433 }
434
435 wxWindow* wxApp::GetTopWindow() const
436 {
437 if (m_topWindow)
438 return m_topWindow;
439 else if (wxTopLevelWindows.Number() > 0)
440 return (wxWindow*) wxTopLevelWindows.First()->Data();
441 else
442 return NULL;
443 }
444
445 // Create an application context
446 bool wxApp::OnInitGui()
447 {
448 XtToolkitInitialize() ;
449 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext() ;
450 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL,
451 (const char*) wxTheApp->GetClassName(), NULL,
452 # if XtSpecificationRelease < 5
453 0,(Cardinal*) &argc,argv) ;
454 # else
455 0,&argc,argv) ;
456 # endif
457 if (!dpy) {
458 cerr << "wxWindows could not open display for " << wxTheApp->GetClassName() << ": exiting.\n";
459 exit(-1);
460 }
461 wxTheApp->m_topLevelWidget = (WXWidget) XtAppCreateShell((String)NULL, (const char*) wxTheApp->GetClassName(),
462 applicationShellWidgetClass,dpy,
463 NULL,0) ;
464
465 // Add general resize proc
466 XtActionsRec rec;
467 rec.string = "resize";
468 rec.proc = (XtActionProc)wxWidgetResizeProc;
469 XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1);
470
471 GetMainColormap(dpy);
472 m_maxRequestSize = XMaxRequestSize((Display*) dpy);
473
474 return TRUE;
475 }
476
477 WXColormap wxApp::GetMainColormap(WXDisplay* display)
478 {
479 if (!display) /* Must be called first with non-NULL display */
480 return m_mainColormap;
481
482 Colormap c =
483 DefaultColormapOfScreen(XScreenOfDisplay((Display*) display,
484 DefaultScreen((Display*) display)));
485
486 if (!m_mainColormap)
487 m_mainColormap = (WXColormap) c;
488
489 return (WXColormap) c;
490 }
491
492 void wxExit()
493 {
494 int retValue = 0;
495 if (wxTheApp)
496 retValue = wxTheApp->OnExit();
497
498 wxApp::CleanUp();
499 /*
500 * Exit in some platform-specific way. Not recommended that the app calls this:
501 * only for emergencies.
502 */
503 exit(retValue);
504 }
505
506 // Yield to other processes
507 bool wxYield()
508 {
509 while (wxTheApp && wxTheApp->Pending())
510 wxTheApp->Dispatch();
511 return TRUE;
512 }
513