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"
33 #include "wx/thread.h"
36 #if wxUSE_WX_RESOURCES
37 #include "wx/resource.h"
42 #include <X11/Xutil.h>
43 #include <X11/Xresource.h>
44 #include <X11/Xatom.h>
46 #include "wx/motif/private.h"
50 extern char *wxBuffer
;
51 extern wxList wxPendingDelete
;
54 extern wxList
*wxPendingEvents
;
55 extern wxCriticalSection
*wxPendingEventsLocker
;
56 #endif // wxUSE_THREADS
58 wxApp
*wxTheApp
= NULL
;
60 wxHashTable
*wxWidgetHashTable
= NULL
;
62 #if !USE_SHARED_LIBRARY
63 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
65 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
66 EVT_IDLE(wxApp::OnIdle
)
70 long wxApp::sm_lastMessageTime
= 0;
72 bool wxApp::Initialize()
74 wxBuffer
= new char[BUFSIZ
+ 512];
76 wxClassInfo::InitializeClasses();
78 // GL: I'm annoyed ... I don't know where to put this and I don't want to
79 // create a module for that as it's part of the core.
81 wxPendingEvents
= new wxList();
82 wxPendingEventsLocker
= new wxCriticalSection();
85 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
86 wxTheColourDatabase
->Initialize();
88 wxInitializeStockLists();
89 wxInitializeStockObjects();
91 #if wxUSE_WX_RESOURCES
92 wxInitializeResourceSystem();
95 // For PostScript printing
97 /* Done using wxModule now
98 wxInitializePrintSetupData();
99 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
100 wxThePrintPaperDatabase->CreateDatabase();
104 wxBitmap::InitStandardHandlers();
106 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
108 wxModule::RegisterModules();
109 if (!wxModule::InitializeModules()) return FALSE
;
114 void wxApp::CleanUp()
116 delete wxWidgetHashTable
;
117 wxWidgetHashTable
= NULL
;
119 wxModule::CleanUpModules();
121 #if wxUSE_WX_RESOURCES
122 wxCleanUpResourceSystem();
125 wxDeleteStockObjects() ;
127 // Destroy all GDI lists, etc.
129 delete wxTheBrushList
;
130 wxTheBrushList
= NULL
;
135 delete wxTheFontList
;
136 wxTheFontList
= NULL
;
138 delete wxTheBitmapList
;
139 wxTheBitmapList
= NULL
;
141 delete wxTheColourDatabase
;
142 wxTheColourDatabase
= NULL
;
145 /* Done using wxModule now
146 wxInitializePrintSetupData(FALSE);
147 delete wxThePrintPaperDatabase;
148 wxThePrintPaperDatabase = NULL;
152 wxBitmap::CleanUpHandlers();
157 wxClassInfo::CleanUpClasses();
162 // GL: I'm annoyed ... I don't know where to put this and I don't want to
163 // create a module for that as it's part of the core.
165 delete wxPendingEvents
;
166 delete wxPendingEventsLocker
;
169 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
170 // At this point we want to check if there are any memory
171 // blocks that aren't part of the wxDebugContext itself,
172 // as a special case. Then when dumping we need to ignore
173 // wxDebugContext, too.
174 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
176 wxLogDebug("There were memory leaks.\n");
177 wxDebugContext::Dump();
178 wxDebugContext::PrintStatistics();
182 // do it as the very last thing because everything else can log messages
183 wxLog::DontCreateOnDemand();
184 // do it as the very last thing because everything else can log messages
185 delete wxLog::SetActiveTarget(NULL
);
188 int wxEntry( int argc
, char *argv
[] )
190 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
191 // This seems to be necessary since there are 'rogue'
192 // objects present at this point (perhaps global objects?)
193 // Setting a checkpoint will ignore them as far as the
194 // memory checking facility is concerned.
195 // Of course you may argue that memory allocated in globals should be
196 // checked, but this is a reasonable compromise.
197 wxDebugContext::SetCheckpoint();
200 if (!wxApp::Initialize())
205 if (!wxApp::GetInitializerFunction())
207 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
211 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
216 printf( "wxWindows error: wxTheApp == NULL\n" );
220 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
221 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
223 wxTheApp
->argc
= argc
;
224 wxTheApp
->argv
= argv
;
226 // GUI-specific initialization, such as creating an app context.
227 wxTheApp
->OnInitGui();
229 // Here frames insert themselves automatically into wxTopLevelWindows by
230 // getting created in OnInit().
233 if (wxTheApp
->OnInit())
235 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
238 // flush the logged messages if any
239 wxLog
*pLog
= wxLog::GetActiveTarget();
240 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
243 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
244 // for further messages
246 if (wxTheApp
->GetTopWindow())
248 delete wxTheApp
->GetTopWindow();
249 wxTheApp
->SetTopWindow(NULL
);
252 wxTheApp
->DeletePendingObjects();
261 // Static member initialization
262 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
269 m_wantDebugOutput
= TRUE
;
273 m_printMode
= wxPRINT_POSTSCRIPT
;
274 m_exitOnFrameDelete
= TRUE
;
277 m_mainColormap
= (WXColormap
) NULL
;
278 m_appContext
= (WXAppContext
) NULL
;
279 m_topLevelWidget
= (WXWidget
) NULL
;
280 m_maxRequestSize
= 0;
281 m_initialDisplay
= (WXDisplay
*) 0;
284 bool wxApp::Initialized()
292 int wxApp::MainLoop()
297 * Sit around forever waiting to process X-events. Property Change
298 * event are handled special, because they have to refer to
299 * the root window rather than to a widget. therefore we can't
300 * use an Xt-eventhandler.
303 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
304 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
309 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
312 XtAppNextEvent( (XtAppContext
) wxTheApp
->GetAppContext(), &event
);
314 ProcessXEvent((WXEvent
*) & event
);
316 if (XtAppPending( (XtAppContext
) wxTheApp
->GetAppContext() ) == 0)
321 // leave the main loop to give other threads a chance to
322 // perform their GUI work
335 // Processes an X event.
336 void wxApp::ProcessXEvent(WXEvent
* _event
)
338 XEvent
* event
= (XEvent
*) _event
;
340 if ((event
->type
== KeyPress
) && CheckForAccelerator(_event
))
342 // Do nothing! We intercepted and processed the event as an accelerator.
345 else if (event
->type
== PropertyNotify
)
347 HandlePropertyChange(_event
);
350 else if (event
->type
== ResizeRequest
)
352 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
353 * If resize event, don't resize until the last resize event for this
354 * window is recieved. Prevents flicker as windows are resized.
357 Display
*disp
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget());
358 Window win
= event
->xany
.window
;
363 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
365 // TODO: when implementing refresh optimization, we can use
366 // XtAddExposureToRegion to expand the window's paint region.
368 XtDispatchEvent(event
);
372 XtDispatchEvent(event
);
376 // Returns TRUE if more time is needed.
377 bool wxApp::ProcessIdle()
380 event
.SetEventObject(this);
383 return event
.MoreRequested();
386 void wxApp::ExitMainLoop()
391 // Is a message/event pending?
392 bool wxApp::Pending()
394 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
396 // Fix by Doug from STI, to prevent a stall if non-X event
398 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
401 // Dispatch a message.
402 void wxApp::Dispatch()
404 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
407 XtAppNextEvent((XtAppContext
) GetAppContext(), &event
);
408 ProcessXEvent((WXEvent
*) & event
);
411 // This should be redefined in a derived class for
412 // handling property change events for XAtom IPC.
413 void wxApp::HandlePropertyChange(WXEvent
*event
)
415 // by default do nothing special
416 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
419 void wxApp::OnIdle(wxIdleEvent
& event
)
421 static bool inOnIdle
= FALSE
;
423 // Avoid recursion (via ProcessEvent default case)
429 // 'Garbage' collection of windows deleted with Close().
430 DeletePendingObjects();
433 // Flush pending events.
434 ProcessPendingEvents();
437 // flush the logged messages if any
438 wxLog
*pLog
= wxLog::GetActiveTarget();
439 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
442 // Send OnIdle events to all windows
443 bool needMore
= SendIdleEvents();
446 event
.RequestMore(TRUE
);
451 // Send idle event to all top-level windows
452 bool wxApp::SendIdleEvents()
454 bool needMore
= FALSE
;
456 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
459 wxWindow
* win
= node
->GetData();
460 if (SendIdleEvents(win
))
462 node
= node
->GetNext();
468 // Send idle event to window and all subwindows
469 bool wxApp::SendIdleEvents(wxWindow
* win
)
471 bool needMore
= FALSE
;
474 event
.SetEventObject(win
);
475 win
->ProcessEvent(event
);
477 if (event
.MoreRequested())
480 wxNode
* node
= win
->GetChildren().First();
483 wxWindow
* win
= (wxWindow
*) node
->Data();
484 if (SendIdleEvents(win
))
492 void wxApp::DeletePendingObjects()
494 wxNode
*node
= wxPendingDelete
.First();
497 wxObject
*obj
= (wxObject
*)node
->Data();
501 if (wxPendingDelete
.Member(obj
))
504 // Deleting one object may have deleted other pending
505 // objects, so start from beginning of list again.
506 node
= wxPendingDelete
.First();
511 void wxApp::ProcessPendingEvents()
513 wxNode
*node
= wxPendingEvents
->First();
514 wxCriticalSectionLocker
locker(*wxPendingEventsLocker
);
518 wxEvtHandler
*handler
= (wxEvtHandler
*)node
->Data();
520 handler
->ProcessPendingEvents();
523 node
= wxPendingEvents
->First();
526 #endif // wxUSE_THREADS
528 wxLog
* wxApp::CreateLogTarget()
533 wxWindow
* wxApp::GetTopWindow() const
537 else if (wxTopLevelWindows
.GetCount() > 0)
538 return wxTopLevelWindows
.GetFirst()->GetData();
543 // Create an application context
544 bool wxApp::OnInitGui()
546 XtToolkitInitialize() ;
547 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext() ;
548 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
549 (const char*) wxTheApp
->GetClassName(), NULL
, 0,
550 # if XtSpecificationRelease < 5
558 wxString
className(wxTheApp
->GetClassName());
559 wxLogError(_("wxWindows could not open display for '%s': exiting."),
560 (const char*) className
);
563 m_initialDisplay
= (WXDisplay
*) dpy
;
565 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
566 applicationShellWidgetClass
,dpy
,
569 // Add general resize proc
571 rec
.string
= "resize";
572 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
573 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
575 GetMainColormap(dpy
);
576 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
581 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
583 if (!display
) /* Must be called first with non-NULL display */
584 return m_mainColormap
;
586 int defaultScreen
= DefaultScreen((Display
*) display
);
587 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
589 Colormap c
= DefaultColormapOfScreen(screen
);
592 m_mainColormap
= (WXColormap
) c
;
594 return (WXColormap
) c
;
597 // Returns TRUE if an accelerator has been processed
598 bool wxApp::CheckForAccelerator(WXEvent
* event
)
600 XEvent
* xEvent
= (XEvent
*) event
;
601 if (xEvent
->xany
.type
== KeyPress
)
603 // Find a wxWindow for this window
604 // TODO: should get display for the window, not the current display
605 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(), xEvent
->xany
.window
);
606 wxWindow
* win
= NULL
;
608 // Find the first wxWindow that corresponds to this event window
609 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
610 widget
= XtParent(widget
);
615 wxKeyEvent
keyEvent(wxEVT_CHAR
);
616 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
618 // Now we have a wxKeyEvent and we have a wxWindow.
619 // Go up the hierarchy until we find a matching accelerator,
620 // or we get to the top.
623 if (win
->ProcessAccelerator(keyEvent
))
625 win
= win
->GetParent();
636 retValue
= wxTheApp
->OnExit();
640 * Exit in some platform-specific way. Not recommended that the app calls this:
641 * only for emergencies.
646 // Yield to other processes
649 while (wxTheApp
&& wxTheApp
->Pending())
650 wxTheApp
->Dispatch();
652 // VZ: is it the same as this (taken from old wxExecute)?
654 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);