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