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 (defined(__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 (defined(__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
);
285 ProcessXEvent((WXEvent
*) & event
);
292 // Processes an X event.
293 void wxApp::ProcessXEvent(WXEvent
* _event
)
295 XEvent
* event
= (XEvent
*) _event
;
297 if (CheckForAccelerator(_event
))
299 // Do nothing! We intercepted and processed the event as an accelerator.
302 else if (event
->type
== PropertyNotify
)
304 HandlePropertyChange(_event
);
307 else if (event
->type
== ResizeRequest
)
309 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
310 * If resize event, don't resize until the last resize event for this
311 * window is recieved. Prevents flicker as windows are resized.
314 Display
*disp
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget());
315 Window win
= event
->xany
.window
;
320 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
322 // TODO: when implementing refresh optimization, we can use
323 // XtAddExposureToRegion to expand the window's paint region.
325 XtDispatchEvent(event
);
329 XtDispatchEvent(event
);
333 // Returns TRUE if more time is needed.
334 bool wxApp::ProcessIdle()
337 event
.SetEventObject(this);
340 return event
.MoreRequested();
343 void wxApp::ExitMainLoop()
348 // Is a message/event pending?
349 bool wxApp::Pending()
351 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
353 // Fix by Doug from STI, to prevent a stall if non-X event
355 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
358 // Dispatch a message.
359 void wxApp::Dispatch()
361 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
364 XtAppNextEvent((XtAppContext
) GetAppContext(), &event
);
365 ProcessXEvent((WXEvent
*) & event
);
368 // This should be redefined in a derived class for
369 // handling property change events for XAtom IPC.
370 void wxApp::HandlePropertyChange(WXEvent
*event
)
372 // by default do nothing special
373 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
376 void wxApp::OnIdle(wxIdleEvent
& event
)
378 static bool inOnIdle
= FALSE
;
380 // Avoid recursion (via ProcessEvent default case)
386 // 'Garbage' collection of windows deleted with Close().
387 DeletePendingObjects();
389 // flush the logged messages if any
390 wxLog
*pLog
= wxLog::GetActiveTarget();
391 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
394 // Send OnIdle events to all windows
395 bool needMore
= SendIdleEvents();
398 event
.RequestMore(TRUE
);
403 // Send idle event to all top-level windows
404 bool wxApp::SendIdleEvents()
406 bool needMore
= FALSE
;
407 wxNode
* node
= wxTopLevelWindows
.First();
410 wxWindow
* win
= (wxWindow
*) node
->Data();
411 if (SendIdleEvents(win
))
419 // Send idle event to window and all subwindows
420 bool wxApp::SendIdleEvents(wxWindow
* win
)
422 bool needMore
= FALSE
;
425 event
.SetEventObject(win
);
426 win
->ProcessEvent(event
);
428 if (event
.MoreRequested())
431 wxNode
* node
= win
->GetChildren()->First();
434 wxWindow
* win
= (wxWindow
*) node
->Data();
435 if (SendIdleEvents(win
))
443 void wxApp::DeletePendingObjects()
445 wxNode
*node
= wxPendingDelete
.First();
448 wxObject
*obj
= (wxObject
*)node
->Data();
452 if (wxPendingDelete
.Member(obj
))
455 // Deleting one object may have deleted other pending
456 // objects, so start from beginning of list again.
457 node
= wxPendingDelete
.First();
461 wxLog
* wxApp::CreateLogTarget()
466 wxWindow
* wxApp::GetTopWindow() const
470 else if (wxTopLevelWindows
.Number() > 0)
471 return (wxWindow
*) wxTopLevelWindows
.First()->Data();
476 // Create an application context
477 bool wxApp::OnInitGui()
479 XtToolkitInitialize() ;
480 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext() ;
481 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
482 (const char*) wxTheApp
->GetClassName(), NULL
,
483 # if XtSpecificationRelease < 5
484 0,(Cardinal
*) &argc
,argv
) ;
489 cerr
<< "wxWindows could not open display for " << wxTheApp
->GetClassName() << ": exiting.\n";
492 m_initialDisplay
= (WXDisplay
*) dpy
;
494 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
495 applicationShellWidgetClass
,dpy
,
498 // Add general resize proc
500 rec
.string
= "resize";
501 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
502 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
504 GetMainColormap(dpy
);
505 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
510 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
512 if (!display
) /* Must be called first with non-NULL display */
513 return m_mainColormap
;
516 DefaultColormapOfScreen(XScreenOfDisplay((Display
*) display
,
517 DefaultScreen((Display
*) display
)));
520 m_mainColormap
= (WXColormap
) c
;
522 return (WXColormap
) c
;
525 // Returns TRUE if an accelerator has been processed
526 bool wxApp::CheckForAccelerator(WXEvent
* event
)
528 XEvent
* xEvent
= (XEvent
*) event
;
529 if (xEvent
->xany
.type
== KeyPress
)
531 // Find a wxWindow for this window
532 // TODO: should get display for the window, not the current display
533 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(), xEvent
->xany
.window
);
534 wxWindow
* win
= NULL
;
536 // Find the first wxWindow that corresponds to this event window
537 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
538 widget
= XtParent(widget
);
543 wxKeyEvent
keyEvent(wxEVT_CHAR
);
544 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
546 // Now we have a wxKeyEvent and we have a wxWindow.
547 // Go up the hierarchy until we find a matching accelerator,
548 // or we get to the top.
551 if (win
->ProcessAccelerator(keyEvent
))
553 win
= win
->GetParent();
564 retValue
= wxTheApp
->OnExit();
568 * Exit in some platform-specific way. Not recommended that the app calls this:
569 * only for emergencies.
574 // Yield to other processes
577 while (wxTheApp
&& wxTheApp
->Pending())
578 wxTheApp
->Dispatch();