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();
82 wxInitializeStockObjects();
84 #if wxUSE_WX_RESOURCES
85 wxInitializeResourceSystem();
88 // For PostScript printing
90 /* Done using wxModule now
91 wxInitializePrintSetupData();
92 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
93 wxThePrintPaperDatabase->CreateDatabase();
97 wxBitmap::InitStandardHandlers();
99 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
101 wxModule::RegisterModules();
102 wxASSERT( wxModule::InitializeModules() == TRUE
);
107 void wxApp::CleanUp()
109 delete wxWidgetHashTable
;
110 wxWidgetHashTable
= NULL
;
112 wxModule::CleanUpModules();
114 #if wxUSE_WX_RESOURCES
115 wxCleanUpResourceSystem();
118 wxDeleteStockObjects() ;
120 // Destroy all GDI lists, etc.
122 delete wxTheBrushList
;
123 wxTheBrushList
= NULL
;
128 delete wxTheFontList
;
129 wxTheFontList
= NULL
;
131 delete wxTheBitmapList
;
132 wxTheBitmapList
= NULL
;
134 delete wxTheColourDatabase
;
135 wxTheColourDatabase
= NULL
;
138 /* Done using wxModule now
139 wxInitializePrintSetupData(FALSE);
140 delete wxThePrintPaperDatabase;
141 wxThePrintPaperDatabase = NULL;
145 wxBitmap::CleanUpHandlers();
150 wxClassInfo::CleanUpClasses();
152 // do it as the very last thing because everything else can log messages
153 wxLog::DontCreateOnDemand();
154 // do it as the very last thing because everything else can log messages
155 delete wxLog::SetActiveTarget(NULL
);
158 int wxEntry( int argc
, char *argv
[] )
160 if (!wxApp::Initialize())
164 if (!wxApp::GetInitializerFunction())
166 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
170 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
175 printf( "wxWindows error: wxTheApp == NULL\n" );
179 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
180 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
182 wxTheApp
->argc
= argc
;
183 wxTheApp
->argv
= argv
;
185 // GUI-specific initialization, such as creating an app context.
186 wxTheApp
->OnInitGui();
188 // Here frames insert themselves automatically
189 // into wxTopLevelWindows by getting created
192 if (!wxTheApp
->OnInit()) return 0;
196 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
198 if (wxTheApp
->GetTopWindow())
200 delete wxTheApp
->GetTopWindow();
201 wxTheApp
->SetTopWindow(NULL
);
204 wxTheApp
->DeletePendingObjects();
213 #if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
214 // At this point we want to check if there are any memory
215 // blocks that aren't part of the wxDebugContext itself,
216 // as a special case. Then when dumping we need to ignore
217 // wxDebugContext, too.
218 if (wxDebugContext::CountObjectsLeft() > 0)
220 wxTrace("There were memory leaks.\n");
221 wxDebugContext::Dump();
222 wxDebugContext::PrintStatistics();
224 wxDebugContext::SetStream(NULL
, NULL
);
230 // Static member initialization
231 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
238 m_wantDebugOutput
= TRUE
;
242 m_printMode
= wxPRINT_POSTSCRIPT
;
243 m_exitOnFrameDelete
= TRUE
;
246 m_mainColormap
= (WXColormap
) NULL
;
247 m_appContext
= (WXAppContext
) NULL
;
248 m_topLevelWidget
= (WXWidget
) NULL
;
249 m_maxRequestSize
= 0;
252 bool wxApp::Initialized()
260 int wxApp::MainLoop()
265 * Sit around forever waiting to process X-events. Property Change
266 * event are handled special, because they have to refer to
267 * the root window rather than to a widget. therefore we can't
268 * use an Xt-eventhandler.
271 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
272 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
277 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
280 XtAppNextEvent( (XtAppContext
) wxTheApp
->GetAppContext(), &event
);
281 if(event
.type
== PropertyNotify
)
283 HandlePropertyChange((WXEvent
*) &event
);
286 // Terry Gitnick <terryg@scientech.com> - 1/21/98
287 /* if resize event, don't resize until the last resize event for this
288 window is recieved. Prevents flicker as windows are resized. */
289 if (event
.type
== ResizeRequest
)
291 Display
*disp
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget());
292 Window win
= event
.xany
.window
;
297 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
299 // TODO: when implementing refresh optimization, we can use
300 // XtAddExposureToRegion to expand the window's paint region.
302 XtDispatchEvent(&event
);
304 DeletePendingObjects();
311 // Returns TRUE if more time is needed.
312 bool wxApp::ProcessIdle()
315 event
.SetEventObject(this);
318 return event
.MoreRequested();
321 void wxApp::ExitMainLoop()
326 // Is a message/event pending?
327 bool wxApp::Pending()
329 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
330 return (XtAppPending( (XtAppContext
) wxTheApp
->GetAppContext() ) != 0) ;
333 // Dispatch a message.
334 void wxApp::Dispatch()
336 XtAppProcessEvent( (XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
339 // This should be redefined in a derived class for
340 // handling property change events for XAtom IPC.
341 void wxApp::HandlePropertyChange(WXEvent
*event
)
343 // by default do nothing special
344 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
347 void wxApp::OnIdle(wxIdleEvent
& event
)
349 static bool inOnIdle
= FALSE
;
351 // Avoid recursion (via ProcessEvent default case)
357 // 'Garbage' collection of windows deleted with Close().
358 DeletePendingObjects();
360 // flush the logged messages if any
361 wxLog
*pLog
= wxLog::GetActiveTarget();
362 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
365 // Send OnIdle events to all windows
366 bool needMore
= SendIdleEvents();
369 event
.RequestMore(TRUE
);
374 // Send idle event to all top-level windows
375 bool wxApp::SendIdleEvents()
377 bool needMore
= FALSE
;
378 wxNode
* node
= wxTopLevelWindows
.First();
381 wxWindow
* win
= (wxWindow
*) node
->Data();
382 if (SendIdleEvents(win
))
390 // Send idle event to window and all subwindows
391 bool wxApp::SendIdleEvents(wxWindow
* win
)
393 bool needMore
= FALSE
;
396 event
.SetEventObject(win
);
397 win
->ProcessEvent(event
);
399 if (event
.MoreRequested())
402 wxNode
* node
= win
->GetChildren()->First();
405 wxWindow
* win
= (wxWindow
*) node
->Data();
406 if (SendIdleEvents(win
))
414 void wxApp::DeletePendingObjects()
416 wxNode
*node
= wxPendingDelete
.First();
419 wxObject
*obj
= (wxObject
*)node
->Data();
423 if (wxPendingDelete
.Member(obj
))
426 // Deleting one object may have deleted other pending
427 // objects, so start from beginning of list again.
428 node
= wxPendingDelete
.First();
432 wxLog
* wxApp::CreateLogTarget()
437 wxWindow
* wxApp::GetTopWindow() const
441 else if (wxTopLevelWindows
.Number() > 0)
442 return (wxWindow
*) wxTopLevelWindows
.First()->Data();
447 // Create an application context
448 bool wxApp::OnInitGui()
450 XtToolkitInitialize() ;
451 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext() ;
452 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
453 (const char*) wxTheApp
->GetClassName(), NULL
,
454 # if XtSpecificationRelease < 5
455 0,(Cardinal
*) &argc
,argv
) ;
460 cerr
<< "wxWindows could not open display for " << wxTheApp
->GetClassName() << ": exiting.\n";
463 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
464 applicationShellWidgetClass
,dpy
,
467 // Add general resize proc
469 rec
.string
= "resize";
470 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
471 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
473 GetMainColormap(dpy
);
474 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
479 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
481 if (!display
) /* Must be called first with non-NULL display */
482 return m_mainColormap
;
485 DefaultColormapOfScreen(XScreenOfDisplay((Display
*) display
,
486 DefaultScreen((Display
*) display
)));
489 m_mainColormap
= (WXColormap
) c
;
491 return (WXColormap
) c
;
498 retValue
= wxTheApp
->OnExit();
502 * Exit in some platform-specific way. Not recommended that the app calls this:
503 * only for emergencies.
508 // Yield to other processes
511 while (wxTheApp
&& wxTheApp
->Pending())
512 wxTheApp
->Dispatch();