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