1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "app.h"
19 #include "wx/gdicmn.h"
22 #include "wx/cursor.h"
24 #include "wx/palette.h"
26 #include "wx/dialog.h"
27 #include "wx/msgdlg.h"
29 #include "wx/module.h"
30 #include "wx/memory.h"
32 #if wxUSE_WX_RESOURCES
33 #include "wx/resource.h"
38 #include <X11/Xutil.h>
39 #include <X11/Xresource.h>
40 #include <X11/Xatom.h>
42 #include "wx/motif/private.h"
46 extern char *wxBuffer
;
47 extern wxList wxPendingDelete
;
49 wxApp
*wxTheApp
= NULL
;
51 wxHashTable
*wxWidgetHashTable
= NULL
;
53 #if !USE_SHARED_LIBRARY
54 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
55 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
56 EVT_IDLE(wxApp::OnIdle
)
60 long wxApp::sm_lastMessageTime
= 0;
62 bool wxApp::Initialize()
65 wxBuffer
= new char[1500];
67 wxBuffer
= new char[BUFSIZ
+ 512];
71 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
73 streambuf* sBuf = new wxDebugStreamBuf;
74 ostream* oStr = new ostream(sBuf) ;
75 wxDebugContext::SetStream(oStr, sBuf);
80 wxClassInfo::InitializeClasses();
82 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
83 wxTheColourDatabase
->Initialize();
85 wxInitializeStockLists();
86 wxInitializeStockObjects();
88 #if wxUSE_WX_RESOURCES
89 wxInitializeResourceSystem();
92 // For PostScript printing
94 /* Done using wxModule now
95 wxInitializePrintSetupData();
96 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
97 wxThePrintPaperDatabase->CreateDatabase();
101 wxBitmap::InitStandardHandlers();
103 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
105 wxModule::RegisterModules();
106 wxASSERT( wxModule::InitializeModules() == TRUE
);
111 void wxApp::CleanUp()
113 delete wxWidgetHashTable
;
114 wxWidgetHashTable
= NULL
;
116 wxModule::CleanUpModules();
118 #if wxUSE_WX_RESOURCES
119 wxCleanUpResourceSystem();
122 wxDeleteStockObjects() ;
124 // Destroy all GDI lists, etc.
126 delete wxTheBrushList
;
127 wxTheBrushList
= NULL
;
132 delete wxTheFontList
;
133 wxTheFontList
= NULL
;
135 delete wxTheBitmapList
;
136 wxTheBitmapList
= NULL
;
138 delete wxTheColourDatabase
;
139 wxTheColourDatabase
= NULL
;
142 /* Done using wxModule now
143 wxInitializePrintSetupData(FALSE);
144 delete wxThePrintPaperDatabase;
145 wxThePrintPaperDatabase = NULL;
149 wxBitmap::CleanUpHandlers();
154 wxClassInfo::CleanUpClasses();
159 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
160 // At this point we want to check if there are any memory
161 // blocks that aren't part of the wxDebugContext itself,
162 // as a special case. Then when dumping we need to ignore
163 // wxDebugContext, too.
164 if (wxDebugContext::CountObjectsLeft() > 0)
166 wxLogDebug("There were memory leaks.\n");
167 wxDebugContext::Dump();
168 wxDebugContext::PrintStatistics();
170 // wxDebugContext::SetStream(NULL, NULL);
173 // do it as the very last thing because everything else can log messages
174 wxLog::DontCreateOnDemand();
175 // do it as the very last thing because everything else can log messages
176 delete wxLog::SetActiveTarget(NULL
);
179 int wxEntry( int argc
, char *argv
[] )
181 if (!wxApp::Initialize())
185 if (!wxApp::GetInitializerFunction())
187 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
191 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
196 printf( "wxWindows error: wxTheApp == NULL\n" );
200 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
201 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
203 wxTheApp
->argc
= argc
;
204 wxTheApp
->argv
= argv
;
206 // GUI-specific initialization, such as creating an app context.
207 wxTheApp
->OnInitGui();
209 // Here frames insert themselves automatically
210 // into wxTopLevelWindows by getting created
213 if (!wxTheApp
->OnInit()) return 0;
217 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
219 // flush the logged messages if any
220 wxLog
*pLog
= wxLog::GetActiveTarget();
221 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
224 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
225 // for further messages
227 if (wxTheApp
->GetTopWindow())
229 delete wxTheApp
->GetTopWindow();
230 wxTheApp
->SetTopWindow(NULL
);
233 wxTheApp
->DeletePendingObjects();
243 // Static member initialization
244 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
251 m_wantDebugOutput
= TRUE
;
255 m_printMode
= wxPRINT_POSTSCRIPT
;
256 m_exitOnFrameDelete
= TRUE
;
259 m_mainColormap
= (WXColormap
) NULL
;
260 m_appContext
= (WXAppContext
) NULL
;
261 m_topLevelWidget
= (WXWidget
) NULL
;
262 m_maxRequestSize
= 0;
263 m_initialDisplay
= (WXDisplay
*) 0;
266 bool wxApp::Initialized()
274 int wxApp::MainLoop()
279 * Sit around forever waiting to process X-events. Property Change
280 * event are handled special, because they have to refer to
281 * the root window rather than to a widget. therefore we can't
282 * use an Xt-eventhandler.
285 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
286 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
291 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
294 XtAppNextEvent( (XtAppContext
) wxTheApp
->GetAppContext(), &event
);
296 ProcessXEvent((WXEvent
*) & event
);
303 // Processes an X event.
304 void wxApp::ProcessXEvent(WXEvent
* _event
)
306 XEvent
* event
= (XEvent
*) _event
;
308 if ((event
->type
== KeyPress
) && CheckForAccelerator(_event
))
310 // Do nothing! We intercepted and processed the event as an accelerator.
313 else if (event
->type
== PropertyNotify
)
315 HandlePropertyChange(_event
);
318 else if (event
->type
== ResizeRequest
)
320 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
321 * If resize event, don't resize until the last resize event for this
322 * window is recieved. Prevents flicker as windows are resized.
325 Display
*disp
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget());
326 Window win
= event
->xany
.window
;
331 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
333 // TODO: when implementing refresh optimization, we can use
334 // XtAddExposureToRegion to expand the window's paint region.
336 XtDispatchEvent(event
);
340 XtDispatchEvent(event
);
344 // Returns TRUE if more time is needed.
345 bool wxApp::ProcessIdle()
348 event
.SetEventObject(this);
351 return event
.MoreRequested();
354 void wxApp::ExitMainLoop()
359 // Is a message/event pending?
360 bool wxApp::Pending()
362 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
364 // Fix by Doug from STI, to prevent a stall if non-X event
366 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
369 // Dispatch a message.
370 void wxApp::Dispatch()
372 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
375 XtAppNextEvent((XtAppContext
) GetAppContext(), &event
);
376 ProcessXEvent((WXEvent
*) & event
);
379 // This should be redefined in a derived class for
380 // handling property change events for XAtom IPC.
381 void wxApp::HandlePropertyChange(WXEvent
*event
)
383 // by default do nothing special
384 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
387 void wxApp::OnIdle(wxIdleEvent
& event
)
389 static bool inOnIdle
= FALSE
;
391 // Avoid recursion (via ProcessEvent default case)
397 // 'Garbage' collection of windows deleted with Close().
398 DeletePendingObjects();
400 // flush the logged messages if any
401 wxLog
*pLog
= wxLog::GetActiveTarget();
402 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
405 // Send OnIdle events to all windows
406 bool needMore
= SendIdleEvents();
409 event
.RequestMore(TRUE
);
414 // Send idle event to all top-level windows
415 bool wxApp::SendIdleEvents()
417 bool needMore
= FALSE
;
418 wxNode
* node
= wxTopLevelWindows
.First();
421 wxWindow
* win
= (wxWindow
*) node
->Data();
422 if (SendIdleEvents(win
))
430 // Send idle event to window and all subwindows
431 bool wxApp::SendIdleEvents(wxWindow
* win
)
433 bool needMore
= FALSE
;
436 event
.SetEventObject(win
);
437 win
->ProcessEvent(event
);
439 if (event
.MoreRequested())
442 wxNode
* node
= win
->GetChildren()->First();
445 wxWindow
* win
= (wxWindow
*) node
->Data();
446 if (SendIdleEvents(win
))
454 void wxApp::DeletePendingObjects()
456 wxNode
*node
= wxPendingDelete
.First();
459 wxObject
*obj
= (wxObject
*)node
->Data();
463 if (wxPendingDelete
.Member(obj
))
466 // Deleting one object may have deleted other pending
467 // objects, so start from beginning of list again.
468 node
= wxPendingDelete
.First();
472 wxLog
* wxApp::CreateLogTarget()
477 wxWindow
* wxApp::GetTopWindow() const
481 else if (wxTopLevelWindows
.Number() > 0)
482 return (wxWindow
*) wxTopLevelWindows
.First()->Data();
487 // Create an application context
488 bool wxApp::OnInitGui()
490 XtToolkitInitialize() ;
491 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext() ;
492 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
493 (const char*) wxTheApp
->GetClassName(), NULL
,
494 # if XtSpecificationRelease < 5
495 0,(Cardinal
*) &argc
,argv
) ;
500 cerr
<< "wxWindows could not open display for " << wxTheApp
->GetClassName() << ": exiting.\n";
503 m_initialDisplay
= (WXDisplay
*) dpy
;
505 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
506 applicationShellWidgetClass
,dpy
,
509 // Add general resize proc
511 rec
.string
= "resize";
512 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
513 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
515 GetMainColormap(dpy
);
516 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
521 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
523 if (!display
) /* Must be called first with non-NULL display */
524 return m_mainColormap
;
527 DefaultColormapOfScreen(XScreenOfDisplay((Display
*) display
,
528 DefaultScreen((Display
*) display
)));
531 m_mainColormap
= (WXColormap
) c
;
533 return (WXColormap
) c
;
536 // Returns TRUE if an accelerator has been processed
537 bool wxApp::CheckForAccelerator(WXEvent
* event
)
539 XEvent
* xEvent
= (XEvent
*) event
;
540 if (xEvent
->xany
.type
== KeyPress
)
542 // Find a wxWindow for this window
543 // TODO: should get display for the window, not the current display
544 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(), xEvent
->xany
.window
);
545 wxWindow
* win
= NULL
;
547 // Find the first wxWindow that corresponds to this event window
548 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
549 widget
= XtParent(widget
);
554 wxKeyEvent
keyEvent(wxEVT_CHAR
);
555 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
557 // Now we have a wxKeyEvent and we have a wxWindow.
558 // Go up the hierarchy until we find a matching accelerator,
559 // or we get to the top.
562 if (win
->ProcessAccelerator(keyEvent
))
564 win
= win
->GetParent();
575 retValue
= wxTheApp
->OnExit();
579 * Exit in some platform-specific way. Not recommended that the app calls this:
580 * only for emergencies.
585 // Yield to other processes
588 while (wxTheApp
&& wxTheApp
->Pending())
589 wxTheApp
->Dispatch();