]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/motif/app.cpp
Layout things in wxMessageBox
[wxWidgets.git] / src / motif / app.cpp
... / ...
CommitLineData
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
50extern char *wxBuffer;
51extern wxList wxPendingDelete;
52
53#if wxUSE_THREADS
54extern wxList *wxPendingEvents;
55extern wxCriticalSection *wxPendingEventsLocker;
56#endif // wxUSE_THREADS
57
58wxApp *wxTheApp = NULL;
59
60wxHashTable *wxWidgetHashTable = NULL;
61
62#if !USE_SHARED_LIBRARY
63IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
64
65BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
66 EVT_IDLE(wxApp::OnIdle)
67END_EVENT_TABLE()
68#endif
69
70long wxApp::sm_lastMessageTime = 0;
71
72bool wxApp::Initialize()
73{
74 wxBuffer = new char[BUFSIZ + 512];
75
76 wxClassInfo::InitializeClasses();
77
78 // GL: I'm annoyed ... I don't know where to put this and I don't want to
79 // create a module for that as it's part of the core.
80#if wxUSE_THREADS
81 wxPendingEvents = new wxList();
82 wxPendingEventsLocker = new wxCriticalSection();
83#endif
84
85 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
86 wxTheColourDatabase->Initialize();
87
88 wxInitializeStockLists();
89 wxInitializeStockObjects();
90
91#if wxUSE_WX_RESOURCES
92 wxInitializeResourceSystem();
93#endif
94
95 // For PostScript printing
96#if wxUSE_POSTSCRIPT
97 /* Done using wxModule now
98 wxInitializePrintSetupData();
99 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
100 wxThePrintPaperDatabase->CreateDatabase();
101 */
102#endif
103
104 wxBitmap::InitStandardHandlers();
105
106 wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
107
108 wxModule::RegisterModules();
109 if (!wxModule::InitializeModules()) return FALSE;
110
111 return TRUE;
112}
113
114void wxApp::CleanUp()
115{
116 delete wxWidgetHashTable;
117 wxWidgetHashTable = NULL;
118
119 wxModule::CleanUpModules();
120
121#if wxUSE_WX_RESOURCES
122 wxCleanUpResourceSystem();
123#endif
124
125 wxDeleteStockObjects() ;
126
127 // Destroy all GDI lists, etc.
128
129 delete wxTheBrushList;
130 wxTheBrushList = NULL;
131
132 delete wxThePenList;
133 wxThePenList = NULL;
134
135 delete wxTheFontList;
136 wxTheFontList = NULL;
137
138 delete wxTheBitmapList;
139 wxTheBitmapList = NULL;
140
141 delete wxTheColourDatabase;
142 wxTheColourDatabase = NULL;
143
144#if wxUSE_POSTSCRIPT
145 /* Done using wxModule now
146 wxInitializePrintSetupData(FALSE);
147 delete wxThePrintPaperDatabase;
148 wxThePrintPaperDatabase = NULL;
149 */
150#endif
151
152 wxBitmap::CleanUpHandlers();
153
154 delete[] wxBuffer;
155 wxBuffer = NULL;
156
157 wxClassInfo::CleanUpClasses();
158
159 delete wxTheApp;
160 wxTheApp = NULL;
161
162 // GL: I'm annoyed ... I don't know where to put this and I don't want to
163 // create a module for that as it's part of the core.
164#if wxUSE_THREADS
165 delete wxPendingEvents;
166 delete wxPendingEventsLocker;
167#endif
168
169#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
170 // At this point we want to check if there are any memory
171 // blocks that aren't part of the wxDebugContext itself,
172 // as a special case. Then when dumping we need to ignore
173 // wxDebugContext, too.
174 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
175 {
176 wxLogDebug("There were memory leaks.\n");
177 wxDebugContext::Dump();
178 wxDebugContext::PrintStatistics();
179 }
180#endif
181
182 // do it as the very last thing because everything else can log messages
183 wxLog::DontCreateOnDemand();
184 // do it as the very last thing because everything else can log messages
185 delete wxLog::SetActiveTarget(NULL);
186}
187
188int wxEntry( int argc, char *argv[] )
189{
190#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
191 // This seems to be necessary since there are 'rogue'
192 // objects present at this point (perhaps global objects?)
193 // Setting a checkpoint will ignore them as far as the
194 // memory checking facility is concerned.
195 // Of course you may argue that memory allocated in globals should be
196 // checked, but this is a reasonable compromise.
197 wxDebugContext::SetCheckpoint();
198#endif
199
200 if (!wxApp::Initialize())
201 return FALSE;
202
203 if (!wxTheApp)
204 {
205 if (!wxApp::GetInitializerFunction())
206 {
207 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
208 return 0;
209 };
210
211 wxTheApp = (wxApp*) (* wxApp::GetInitializerFunction()) ();
212 };
213
214 if (!wxTheApp)
215 {
216 printf( "wxWindows error: wxTheApp == NULL\n" );
217 return 0;
218 };
219
220 wxTheApp->SetClassName(wxFileNameFromPath(argv[0]));
221 wxTheApp->SetAppName(wxFileNameFromPath(argv[0]));
222
223 wxTheApp->argc = argc;
224 wxTheApp->argv = argv;
225
226 // GUI-specific initialization, such as creating an app context.
227 wxTheApp->OnInitGui();
228
229 // Here frames insert themselves automatically into wxTopLevelWindows by
230 // getting created in OnInit().
231
232 int retValue = 0;
233 if (wxTheApp->OnInit())
234 {
235 if (wxTheApp->Initialized()) retValue = wxTheApp->OnRun();
236 }
237
238 // flush the logged messages if any
239 wxLog *pLog = wxLog::GetActiveTarget();
240 if ( pLog != NULL && pLog->HasPendingMessages() )
241 pLog->Flush();
242
243 delete wxLog::SetActiveTarget(new wxLogStderr); // So dialog boxes aren't used
244 // for further messages
245
246 if (wxTheApp->GetTopWindow())
247 {
248 delete wxTheApp->GetTopWindow();
249 wxTheApp->SetTopWindow(NULL);
250 }
251
252 wxTheApp->DeletePendingObjects();
253
254 wxTheApp->OnExit();
255
256 wxApp::CleanUp();
257
258 return retValue;
259};
260
261// Static member initialization
262wxAppInitializerFunction wxApp::m_appInitFn = (wxAppInitializerFunction) NULL;
263
264wxApp::wxApp()
265{
266 m_topWindow = NULL;
267 wxTheApp = this;
268 m_className = "";
269 m_wantDebugOutput = TRUE ;
270 m_appName = "";
271 argc = 0;
272 argv = NULL;
273 m_printMode = wxPRINT_POSTSCRIPT;
274 m_exitOnFrameDelete = TRUE;
275 m_auto3D = TRUE;
276
277 m_mainColormap = (WXColormap) NULL;
278 m_appContext = (WXAppContext) NULL;
279 m_topLevelWidget = (WXWidget) NULL;
280 m_maxRequestSize = 0;
281 m_initialDisplay = (WXDisplay*) 0;
282}
283
284bool wxApp::Initialized()
285{
286 if (GetTopWindow())
287 return TRUE;
288 else
289 return FALSE;
290}
291
292int wxApp::MainLoop()
293{
294 m_keepGoing = TRUE;
295
296 /*
297 * Sit around forever waiting to process X-events. Property Change
298 * event are handled special, because they have to refer to
299 * the root window rather than to a widget. therefore we can't
300 * use an Xt-eventhandler.
301 */
302
303 XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()),
304 XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())),
305 PropertyChangeMask);
306
307 XEvent event;
308
309 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
310 while (m_keepGoing)
311 {
312 XtAppNextEvent( (XtAppContext) wxTheApp->GetAppContext(), &event);
313
314 ProcessXEvent((WXEvent*) & event);
315
316 if (XtAppPending( (XtAppContext) wxTheApp->GetAppContext() ) == 0)
317 {
318 if (!ProcessIdle())
319 {
320#if wxUSE_THREADS
321 // leave the main loop to give other threads a chance to
322 // perform their GUI work
323 wxMutexGuiLeave();
324 wxUsleep(20);
325 wxMutexGuiEnter();
326#endif
327 }
328 }
329
330 }
331
332 return 0;
333}
334
335// Processes an X event.
336void wxApp::ProcessXEvent(WXEvent* _event)
337{
338 XEvent* event = (XEvent*) _event;
339
340 if ((event->type == KeyPress) && CheckForAccelerator(_event))
341 {
342 // Do nothing! We intercepted and processed the event as an accelerator.
343 return;
344 }
345 else if (event->type == PropertyNotify)
346 {
347 HandlePropertyChange(_event);
348 return;
349 }
350 else if (event->type == ResizeRequest)
351 {
352 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
353 * If resize event, don't resize until the last resize event for this
354 * window is recieved. Prevents flicker as windows are resized.
355 */
356
357 Display *disp = XtDisplay((Widget) wxTheApp->GetTopLevelWidget());
358 Window win = event->xany.window;
359 XEvent report;
360
361 // to avoid flicker
362 report = * event;
363 while( XCheckTypedWindowEvent (disp, win, ResizeRequest, &report));
364
365 // TODO: when implementing refresh optimization, we can use
366 // XtAddExposureToRegion to expand the window's paint region.
367
368 XtDispatchEvent(event);
369 }
370 else
371 {
372 XtDispatchEvent(event);
373 }
374}
375
376// Returns TRUE if more time is needed.
377bool wxApp::ProcessIdle()
378{
379 wxIdleEvent event;
380 event.SetEventObject(this);
381 ProcessEvent(event);
382
383 return event.MoreRequested();
384}
385
386void wxApp::ExitMainLoop()
387{
388 m_keepGoing = FALSE;
389}
390
391// Is a message/event pending?
392bool wxApp::Pending()
393{
394 XFlush(XtDisplay( (Widget) wxTheApp->GetTopLevelWidget() ));
395
396 // Fix by Doug from STI, to prevent a stall if non-X event
397 // is found.
398 return ((XtAppPending( (XtAppContext) GetAppContext() ) & XtIMXEvent) != 0) ;
399}
400
401// Dispatch a message.
402void wxApp::Dispatch()
403{
404 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
405
406 XEvent event;
407 XtAppNextEvent((XtAppContext) GetAppContext(), &event);
408 ProcessXEvent((WXEvent*) & event);
409}
410
411// This should be redefined in a derived class for
412// handling property change events for XAtom IPC.
413void wxApp::HandlePropertyChange(WXEvent *event)
414{
415 // by default do nothing special
416 XtDispatchEvent((XEvent*) event); /* let Motif do the work */
417}
418
419void wxApp::OnIdle(wxIdleEvent& event)
420{
421 static bool inOnIdle = FALSE;
422
423 // Avoid recursion (via ProcessEvent default case)
424 if (inOnIdle)
425 return;
426
427 inOnIdle = TRUE;
428
429 // 'Garbage' collection of windows deleted with Close().
430 DeletePendingObjects();
431
432#if wxUSE_THREADS
433 // Flush pending events.
434 ProcessPendingEvents();
435#endif
436
437 // flush the logged messages if any
438 wxLog *pLog = wxLog::GetActiveTarget();
439 if ( pLog != NULL && pLog->HasPendingMessages() )
440 pLog->Flush();
441
442 // Send OnIdle events to all windows
443 bool needMore = SendIdleEvents();
444
445 if (needMore)
446 event.RequestMore(TRUE);
447
448 inOnIdle = FALSE;
449}
450
451// Send idle event to all top-level windows
452bool wxApp::SendIdleEvents()
453{
454 bool needMore = FALSE;
455
456 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
457 while (node)
458 {
459 wxWindow* win = node->GetData();
460 if (SendIdleEvents(win))
461 needMore = TRUE;
462 node = node->GetNext();
463 }
464
465 return needMore;
466}
467
468// Send idle event to window and all subwindows
469bool wxApp::SendIdleEvents(wxWindow* win)
470{
471 bool needMore = FALSE;
472
473 wxIdleEvent event;
474 event.SetEventObject(win);
475 win->ProcessEvent(event);
476
477 if (event.MoreRequested())
478 needMore = TRUE;
479
480 wxNode* node = win->GetChildren().First();
481 while (node)
482 {
483 wxWindow* win = (wxWindow*) node->Data();
484 if (SendIdleEvents(win))
485 needMore = TRUE;
486
487 node = node->Next();
488 }
489 return needMore ;
490}
491
492void wxApp::DeletePendingObjects()
493{
494 wxNode *node = wxPendingDelete.First();
495 while (node)
496 {
497 wxObject *obj = (wxObject *)node->Data();
498
499 delete obj;
500
501 if (wxPendingDelete.Member(obj))
502 delete node;
503
504 // Deleting one object may have deleted other pending
505 // objects, so start from beginning of list again.
506 node = wxPendingDelete.First();
507 }
508}
509
510#if wxUSE_THREADS
511void wxApp::ProcessPendingEvents()
512{
513 wxNode *node = wxPendingEvents->First();
514 wxCriticalSectionLocker locker(*wxPendingEventsLocker);
515
516 while (node)
517 {
518 wxEvtHandler *handler = (wxEvtHandler *)node->Data();
519
520 handler->ProcessPendingEvents();
521
522 delete node;
523 node = wxPendingEvents->First();
524 }
525}
526#endif // wxUSE_THREADS
527
528wxLog* wxApp::CreateLogTarget()
529{
530 return new wxLogGui;
531}
532
533wxWindow* wxApp::GetTopWindow() const
534{
535 if (m_topWindow)
536 return m_topWindow;
537 else if (wxTopLevelWindows.GetCount() > 0)
538 return wxTopLevelWindows.GetFirst()->GetData();
539 else
540 return NULL;
541}
542
543// Create an application context
544bool wxApp::OnInitGui()
545{
546 XtToolkitInitialize() ;
547 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext() ;
548 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL,
549 (const char*) wxTheApp->GetClassName(), NULL, 0,
550# if XtSpecificationRelease < 5
551 (Cardinal*) &argc,
552# else
553 &argc,
554# endif
555 argv);
556
557 if (!dpy) {
558 wxString className(wxTheApp->GetClassName());
559 wxLogError(_("wxWindows could not open display for '%s': exiting."),
560 (const char*) className);
561 exit(-1);
562 }
563 m_initialDisplay = (WXDisplay*) dpy;
564
565 wxTheApp->m_topLevelWidget = (WXWidget) XtAppCreateShell((String)NULL, (const char*) wxTheApp->GetClassName(),
566 applicationShellWidgetClass,dpy,
567 NULL,0) ;
568
569 // Add general resize proc
570 XtActionsRec rec;
571 rec.string = "resize";
572 rec.proc = (XtActionProc)wxWidgetResizeProc;
573 XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1);
574
575 GetMainColormap(dpy);
576 m_maxRequestSize = XMaxRequestSize((Display*) dpy);
577
578 return TRUE;
579}
580
581WXColormap wxApp::GetMainColormap(WXDisplay* display)
582{
583 if (!display) /* Must be called first with non-NULL display */
584 return m_mainColormap;
585
586 int defaultScreen = DefaultScreen((Display*) display);
587 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
588
589 Colormap c = DefaultColormapOfScreen(screen);
590
591 if (!m_mainColormap)
592 m_mainColormap = (WXColormap) c;
593
594 return (WXColormap) c;
595}
596
597// Returns TRUE if an accelerator has been processed
598bool wxApp::CheckForAccelerator(WXEvent* event)
599{
600 XEvent* xEvent = (XEvent*) event;
601 if (xEvent->xany.type == KeyPress)
602 {
603 // Find a wxWindow for this window
604 // TODO: should get display for the window, not the current display
605 Widget widget = XtWindowToWidget((Display*) wxGetDisplay(), xEvent->xany.window);
606 wxWindow* win = NULL;
607
608 // Find the first wxWindow that corresponds to this event window
609 while (widget && !(win = wxGetWindowFromTable(widget)))
610 widget = XtParent(widget);
611
612 if (!widget || !win)
613 return FALSE;
614
615 wxKeyEvent keyEvent(wxEVT_CHAR);
616 wxTranslateKeyEvent(keyEvent, win, (Widget) 0, xEvent);
617
618 // Now we have a wxKeyEvent and we have a wxWindow.
619 // Go up the hierarchy until we find a matching accelerator,
620 // or we get to the top.
621 while (win)
622 {
623 if (win->ProcessAccelerator(keyEvent))
624 return TRUE;
625 win = win->GetParent();
626 }
627 return FALSE;
628 }
629 return FALSE;
630}
631
632void wxExit()
633{
634 int retValue = 0;
635 if (wxTheApp)
636 retValue = wxTheApp->OnExit();
637
638 wxApp::CleanUp();
639 /*
640 * Exit in some platform-specific way. Not recommended that the app calls this:
641 * only for emergencies.
642 */
643 exit(retValue);
644}
645
646// Yield to other processes
647bool wxYield()
648{
649 while (wxTheApp && wxTheApp->Pending())
650 wxTheApp->Dispatch();
651
652 // VZ: is it the same as this (taken from old wxExecute)?
653#if 0
654 XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
655#endif
656
657 return TRUE;
658}
659