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];
70 #if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
72 streambuf
* sBuf
= new wxDebugStreamBuf
;
73 ostream
* oStr
= new ostream(sBuf
) ;
74 wxDebugContext::SetStream(oStr
, sBuf
);
78 wxClassInfo::InitializeClasses();
80 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
81 wxTheColourDatabase
->Initialize();
83 wxInitializeStockLists();
84 wxInitializeStockObjects();
86 #if wxUSE_WX_RESOURCES
87 wxInitializeResourceSystem();
90 // For PostScript printing
92 /* Done using wxModule now
93 wxInitializePrintSetupData();
94 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
95 wxThePrintPaperDatabase->CreateDatabase();
99 wxBitmap::InitStandardHandlers();
101 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
103 wxModule::RegisterModules();
104 wxASSERT( wxModule::InitializeModules() == TRUE
);
109 void wxApp::CleanUp()
111 delete wxWidgetHashTable
;
112 wxWidgetHashTable
= NULL
;
114 wxModule::CleanUpModules();
116 #if wxUSE_WX_RESOURCES
117 wxCleanUpResourceSystem();
120 wxDeleteStockObjects() ;
122 // Destroy all GDI lists, etc.
124 delete wxTheBrushList
;
125 wxTheBrushList
= NULL
;
130 delete wxTheFontList
;
131 wxTheFontList
= NULL
;
133 delete wxTheBitmapList
;
134 wxTheBitmapList
= NULL
;
136 delete wxTheColourDatabase
;
137 wxTheColourDatabase
= NULL
;
140 /* Done using wxModule now
141 wxInitializePrintSetupData(FALSE);
142 delete wxThePrintPaperDatabase;
143 wxThePrintPaperDatabase = NULL;
147 wxBitmap::CleanUpHandlers();
152 wxClassInfo::CleanUpClasses();
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
);
160 int wxEntry( int argc
, char *argv
[] )
162 if (!wxApp::Initialize())
166 if (!wxApp::GetInitializerFunction())
168 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
172 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
177 printf( "wxWindows error: wxTheApp == NULL\n" );
181 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
182 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
184 wxTheApp
->argc
= argc
;
185 wxTheApp
->argv
= argv
;
187 // GUI-specific initialization, such as creating an app context.
188 wxTheApp
->OnInitGui();
190 // Here frames insert themselves automatically
191 // into wxTopLevelWindows by getting created
194 if (!wxTheApp
->OnInit()) return 0;
198 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
200 if (wxTheApp
->GetTopWindow())
202 delete wxTheApp
->GetTopWindow();
203 wxTheApp
->SetTopWindow(NULL
);
206 wxTheApp
->DeletePendingObjects();
215 #if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
216 // At this point we want to check if there are any memory
217 // blocks that aren't part of the wxDebugContext itself,
218 // as a special case. Then when dumping we need to ignore
219 // wxDebugContext, too.
220 if (wxDebugContext::CountObjectsLeft() > 0)
222 wxTrace("There were memory leaks.\n");
223 wxDebugContext::Dump();
224 wxDebugContext::PrintStatistics();
226 wxDebugContext::SetStream(NULL
, NULL
);
232 // Static member initialization
233 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
240 m_wantDebugOutput
= TRUE
;
244 m_printMode
= wxPRINT_POSTSCRIPT
;
245 m_exitOnFrameDelete
= TRUE
;
248 m_mainColormap
= (WXColormap
) NULL
;
249 m_appContext
= (WXAppContext
) NULL
;
250 m_topLevelWidget
= (WXWidget
) NULL
;
251 m_maxRequestSize
= 0;
252 m_initialDisplay
= (WXDisplay
*) 0;
255 bool wxApp::Initialized()
263 int wxApp::MainLoop()
268 * Sit around forever waiting to process X-events. Property Change
269 * event are handled special, because they have to refer to
270 * the root window rather than to a widget. therefore we can't
271 * use an Xt-eventhandler.
274 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
275 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
280 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
283 XtAppNextEvent( (XtAppContext
) wxTheApp
->GetAppContext(), &event
);
284 if(event
.type
== PropertyNotify
)
286 HandlePropertyChange((WXEvent
*) &event
);
289 // Terry Gitnick <terryg@scientech.com> - 1/21/98
290 /* if resize event, don't resize until the last resize event for this
291 window is recieved. Prevents flicker as windows are resized. */
292 if (event
.type
== ResizeRequest
)
294 Display
*disp
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget());
295 Window win
= event
.xany
.window
;
300 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
302 // TODO: when implementing refresh optimization, we can use
303 // XtAddExposureToRegion to expand the window's paint region.
305 XtDispatchEvent(&event
);
314 // Returns TRUE if more time is needed.
315 bool wxApp::ProcessIdle()
318 event
.SetEventObject(this);
321 return event
.MoreRequested();
324 void wxApp::ExitMainLoop()
329 // Is a message/event pending?
330 bool wxApp::Pending()
332 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
333 return (XtAppPending( (XtAppContext
) wxTheApp
->GetAppContext() ) != 0) ;
336 // Dispatch a message.
337 void wxApp::Dispatch()
339 XtAppProcessEvent( (XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
342 // This should be redefined in a derived class for
343 // handling property change events for XAtom IPC.
344 void wxApp::HandlePropertyChange(WXEvent
*event
)
346 // by default do nothing special
347 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
350 void wxApp::OnIdle(wxIdleEvent
& event
)
352 static bool inOnIdle
= FALSE
;
354 // Avoid recursion (via ProcessEvent default case)
360 // 'Garbage' collection of windows deleted with Close().
361 DeletePendingObjects();
363 // flush the logged messages if any
364 wxLog
*pLog
= wxLog::GetActiveTarget();
365 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
368 // Send OnIdle events to all windows
369 bool needMore
= SendIdleEvents();
372 event
.RequestMore(TRUE
);
377 // Send idle event to all top-level windows
378 bool wxApp::SendIdleEvents()
380 bool needMore
= FALSE
;
381 wxNode
* node
= wxTopLevelWindows
.First();
384 wxWindow
* win
= (wxWindow
*) node
->Data();
385 if (SendIdleEvents(win
))
393 // Send idle event to window and all subwindows
394 bool wxApp::SendIdleEvents(wxWindow
* win
)
396 bool needMore
= FALSE
;
399 event
.SetEventObject(win
);
400 win
->ProcessEvent(event
);
402 if (event
.MoreRequested())
405 wxNode
* node
= win
->GetChildren()->First();
408 wxWindow
* win
= (wxWindow
*) node
->Data();
409 if (SendIdleEvents(win
))
417 void wxApp::DeletePendingObjects()
419 wxNode
*node
= wxPendingDelete
.First();
422 wxObject
*obj
= (wxObject
*)node
->Data();
426 if (wxPendingDelete
.Member(obj
))
429 // Deleting one object may have deleted other pending
430 // objects, so start from beginning of list again.
431 node
= wxPendingDelete
.First();
435 wxLog
* wxApp::CreateLogTarget()
440 wxWindow
* wxApp::GetTopWindow() const
444 else if (wxTopLevelWindows
.Number() > 0)
445 return (wxWindow
*) wxTopLevelWindows
.First()->Data();
450 // Create an application context
451 bool wxApp::OnInitGui()
453 XtToolkitInitialize() ;
454 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext() ;
455 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
456 (const char*) wxTheApp
->GetClassName(), NULL
,
457 # if XtSpecificationRelease < 5
458 0,(Cardinal
*) &argc
,argv
) ;
463 cerr
<< "wxWindows could not open display for " << wxTheApp
->GetClassName() << ": exiting.\n";
466 m_initialDisplay
= (WXDisplay
*) dpy
;
468 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
469 applicationShellWidgetClass
,dpy
,
472 // Add general resize proc
474 rec
.string
= "resize";
475 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
476 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
478 GetMainColormap(dpy
);
479 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
484 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
486 if (!display
) /* Must be called first with non-NULL display */
487 return m_mainColormap
;
490 DefaultColormapOfScreen(XScreenOfDisplay((Display
*) display
,
491 DefaultScreen((Display
*) display
)));
494 m_mainColormap
= (WXColormap
) c
;
496 return (WXColormap
) c
;
503 retValue
= wxTheApp
->OnExit();
507 * Exit in some platform-specific way. Not recommended that the app calls this:
508 * only for emergencies.
513 // Yield to other processes
516 while (wxTheApp
&& wxTheApp
->Pending())
517 wxTheApp
->Dispatch();