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 wxClassInfo::InitializeClasses();
72 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
73 wxTheColourDatabase
->Initialize();
75 wxInitializeStockLists();
76 wxInitializeStockObjects();
78 #if wxUSE_WX_RESOURCES
79 wxInitializeResourceSystem();
82 // For PostScript printing
84 /* Done using wxModule now
85 wxInitializePrintSetupData();
86 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
87 wxThePrintPaperDatabase->CreateDatabase();
91 wxBitmap::InitStandardHandlers();
93 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
95 wxModule::RegisterModules();
96 wxASSERT( wxModule::InitializeModules() == TRUE
);
101 void wxApp::CleanUp()
103 delete wxWidgetHashTable
;
104 wxWidgetHashTable
= NULL
;
106 wxModule::CleanUpModules();
108 #if wxUSE_WX_RESOURCES
109 wxCleanUpResourceSystem();
112 wxDeleteStockObjects() ;
114 // Destroy all GDI lists, etc.
116 delete wxTheBrushList
;
117 wxTheBrushList
= NULL
;
122 delete wxTheFontList
;
123 wxTheFontList
= NULL
;
125 delete wxTheBitmapList
;
126 wxTheBitmapList
= NULL
;
128 delete wxTheColourDatabase
;
129 wxTheColourDatabase
= NULL
;
132 /* Done using wxModule now
133 wxInitializePrintSetupData(FALSE);
134 delete wxThePrintPaperDatabase;
135 wxThePrintPaperDatabase = NULL;
139 wxBitmap::CleanUpHandlers();
144 wxClassInfo::CleanUpClasses();
149 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
150 // At this point we want to check if there are any memory
151 // blocks that aren't part of the wxDebugContext itself,
152 // as a special case. Then when dumping we need to ignore
153 // wxDebugContext, too.
154 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
156 wxLogDebug("There were memory leaks.\n");
157 wxDebugContext::Dump();
158 wxDebugContext::PrintStatistics();
162 // do it as the very last thing because everything else can log messages
163 wxLog::DontCreateOnDemand();
164 // do it as the very last thing because everything else can log messages
165 delete wxLog::SetActiveTarget(NULL
);
168 int wxEntry( int argc
, char *argv
[] )
170 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
171 // This seems to be necessary since there are 'rogue'
172 // objects present at this point (perhaps global objects?)
173 // Setting a checkpoint will ignore them as far as the
174 // memory checking facility is concerned.
175 // Of course you may argue that memory allocated in globals should be
176 // checked, but this is a reasonable compromise.
177 wxDebugContext::SetCheckpoint();
180 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
214 if (wxTheApp
->OnInit())
216 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();
242 // Static member initialization
243 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
250 m_wantDebugOutput
= TRUE
;
254 m_printMode
= wxPRINT_POSTSCRIPT
;
255 m_exitOnFrameDelete
= TRUE
;
258 m_mainColormap
= (WXColormap
) NULL
;
259 m_appContext
= (WXAppContext
) NULL
;
260 m_topLevelWidget
= (WXWidget
) NULL
;
261 m_maxRequestSize
= 0;
262 m_initialDisplay
= (WXDisplay
*) 0;
265 bool wxApp::Initialized()
273 int wxApp::MainLoop()
278 * Sit around forever waiting to process X-events. Property Change
279 * event are handled special, because they have to refer to
280 * the root window rather than to a widget. therefore we can't
281 * use an Xt-eventhandler.
284 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
285 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
290 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
293 XtAppNextEvent( (XtAppContext
) wxTheApp
->GetAppContext(), &event
);
295 ProcessXEvent((WXEvent
*) & event
);
302 // Processes an X event.
303 void wxApp::ProcessXEvent(WXEvent
* _event
)
305 XEvent
* event
= (XEvent
*) _event
;
307 if ((event
->type
== KeyPress
) && CheckForAccelerator(_event
))
309 // Do nothing! We intercepted and processed the event as an accelerator.
312 else if (event
->type
== PropertyNotify
)
314 HandlePropertyChange(_event
);
317 else if (event
->type
== ResizeRequest
)
319 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
320 * If resize event, don't resize until the last resize event for this
321 * window is recieved. Prevents flicker as windows are resized.
324 Display
*disp
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget());
325 Window win
= event
->xany
.window
;
330 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
332 // TODO: when implementing refresh optimization, we can use
333 // XtAddExposureToRegion to expand the window's paint region.
335 XtDispatchEvent(event
);
339 XtDispatchEvent(event
);
343 // Returns TRUE if more time is needed.
344 bool wxApp::ProcessIdle()
347 event
.SetEventObject(this);
350 return event
.MoreRequested();
353 void wxApp::ExitMainLoop()
358 // Is a message/event pending?
359 bool wxApp::Pending()
361 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
363 // Fix by Doug from STI, to prevent a stall if non-X event
365 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
368 // Dispatch a message.
369 void wxApp::Dispatch()
371 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
374 XtAppNextEvent((XtAppContext
) GetAppContext(), &event
);
375 ProcessXEvent((WXEvent
*) & event
);
378 // This should be redefined in a derived class for
379 // handling property change events for XAtom IPC.
380 void wxApp::HandlePropertyChange(WXEvent
*event
)
382 // by default do nothing special
383 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
386 void wxApp::OnIdle(wxIdleEvent
& event
)
388 static bool inOnIdle
= FALSE
;
390 // Avoid recursion (via ProcessEvent default case)
396 // 'Garbage' collection of windows deleted with Close().
397 DeletePendingObjects();
399 // flush the logged messages if any
400 wxLog
*pLog
= wxLog::GetActiveTarget();
401 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
404 // Send OnIdle events to all windows
405 bool needMore
= SendIdleEvents();
408 event
.RequestMore(TRUE
);
413 // Send idle event to all top-level windows
414 bool wxApp::SendIdleEvents()
416 bool needMore
= FALSE
;
417 wxNode
* node
= wxTopLevelWindows
.First();
420 wxWindow
* win
= (wxWindow
*) node
->Data();
421 if (SendIdleEvents(win
))
429 // Send idle event to window and all subwindows
430 bool wxApp::SendIdleEvents(wxWindow
* win
)
432 bool needMore
= FALSE
;
435 event
.SetEventObject(win
);
436 win
->ProcessEvent(event
);
438 if (event
.MoreRequested())
441 wxNode
* node
= win
->GetChildren().First();
444 wxWindow
* win
= (wxWindow
*) node
->Data();
445 if (SendIdleEvents(win
))
453 void wxApp::DeletePendingObjects()
455 wxNode
*node
= wxPendingDelete
.First();
458 wxObject
*obj
= (wxObject
*)node
->Data();
462 if (wxPendingDelete
.Member(obj
))
465 // Deleting one object may have deleted other pending
466 // objects, so start from beginning of list again.
467 node
= wxPendingDelete
.First();
471 wxLog
* wxApp::CreateLogTarget()
476 wxWindow
* wxApp::GetTopWindow() const
480 else if (wxTopLevelWindows
.Number() > 0)
481 return (wxWindow
*) wxTopLevelWindows
.First()->Data();
486 // Create an application context
487 bool wxApp::OnInitGui()
489 XtToolkitInitialize() ;
490 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext() ;
491 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
492 (const char*) wxTheApp
->GetClassName(), NULL
,
493 # if XtSpecificationRelease < 5
494 0,(Cardinal
*) &argc
,argv
) ;
499 cerr
<< "wxWindows could not open display for " << wxTheApp
->GetClassName() << ": exiting.\n";
502 m_initialDisplay
= (WXDisplay
*) dpy
;
504 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
505 applicationShellWidgetClass
,dpy
,
508 // Add general resize proc
510 rec
.string
= "resize";
511 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
512 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
514 GetMainColormap(dpy
);
515 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
520 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
522 if (!display
) /* Must be called first with non-NULL display */
523 return m_mainColormap
;
526 DefaultColormapOfScreen(XScreenOfDisplay((Display
*) display
,
527 DefaultScreen((Display
*) display
)));
530 m_mainColormap
= (WXColormap
) c
;
532 return (WXColormap
) c
;
535 // Returns TRUE if an accelerator has been processed
536 bool wxApp::CheckForAccelerator(WXEvent
* event
)
538 XEvent
* xEvent
= (XEvent
*) event
;
539 if (xEvent
->xany
.type
== KeyPress
)
541 // Find a wxWindow for this window
542 // TODO: should get display for the window, not the current display
543 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(), xEvent
->xany
.window
);
544 wxWindow
* win
= NULL
;
546 // Find the first wxWindow that corresponds to this event window
547 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
548 widget
= XtParent(widget
);
553 wxKeyEvent
keyEvent(wxEVT_CHAR
);
554 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
556 // Now we have a wxKeyEvent and we have a wxWindow.
557 // Go up the hierarchy until we find a matching accelerator,
558 // or we get to the top.
561 if (win
->ProcessAccelerator(keyEvent
))
563 win
= win
->GetParent();
574 retValue
= wxTheApp
->OnExit();
578 * Exit in some platform-specific way. Not recommended that the app calls this:
579 * only for emergencies.
584 // Yield to other processes
587 while (wxTheApp
&& wxTheApp
->Pending())
588 wxTheApp
->Dispatch();