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
;
53 wxApp
*wxTheApp
= NULL
;
55 wxHashTable
*wxWidgetHashTable
= NULL
;
57 #if !USE_SHARED_LIBRARY
58 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
59 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
60 EVT_IDLE(wxApp::OnIdle
)
64 long wxApp::sm_lastMessageTime
= 0;
66 bool wxApp::Initialize()
69 wxBuffer
= new char[1500];
71 wxBuffer
= new char[BUFSIZ
+ 512];
74 wxClassInfo::InitializeClasses();
76 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
77 wxTheColourDatabase
->Initialize();
79 wxInitializeStockLists();
80 wxInitializeStockObjects();
82 #if wxUSE_WX_RESOURCES
83 wxInitializeResourceSystem();
86 // For PostScript printing
88 /* Done using wxModule now
89 wxInitializePrintSetupData();
90 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
91 wxThePrintPaperDatabase->CreateDatabase();
95 wxBitmap::InitStandardHandlers();
97 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
99 wxModule::RegisterModules();
100 if (!wxModule::InitializeModules()) return FALSE
;
105 void wxApp::CleanUp()
107 delete wxWidgetHashTable
;
108 wxWidgetHashTable
= NULL
;
110 wxModule::CleanUpModules();
112 #if wxUSE_WX_RESOURCES
113 wxCleanUpResourceSystem();
116 wxDeleteStockObjects() ;
118 // Destroy all GDI lists, etc.
120 delete wxTheBrushList
;
121 wxTheBrushList
= NULL
;
126 delete wxTheFontList
;
127 wxTheFontList
= NULL
;
129 delete wxTheBitmapList
;
130 wxTheBitmapList
= NULL
;
132 delete wxTheColourDatabase
;
133 wxTheColourDatabase
= NULL
;
136 /* Done using wxModule now
137 wxInitializePrintSetupData(FALSE);
138 delete wxThePrintPaperDatabase;
139 wxThePrintPaperDatabase = NULL;
143 wxBitmap::CleanUpHandlers();
148 wxClassInfo::CleanUpClasses();
153 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
154 // At this point we want to check if there are any memory
155 // blocks that aren't part of the wxDebugContext itself,
156 // as a special case. Then when dumping we need to ignore
157 // wxDebugContext, too.
158 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
160 wxLogDebug("There were memory leaks.\n");
161 wxDebugContext::Dump();
162 wxDebugContext::PrintStatistics();
166 // do it as the very last thing because everything else can log messages
167 wxLog::DontCreateOnDemand();
168 // do it as the very last thing because everything else can log messages
169 delete wxLog::SetActiveTarget(NULL
);
172 int wxEntry( int argc
, char *argv
[] )
174 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
175 // This seems to be necessary since there are 'rogue'
176 // objects present at this point (perhaps global objects?)
177 // Setting a checkpoint will ignore them as far as the
178 // memory checking facility is concerned.
179 // Of course you may argue that memory allocated in globals should be
180 // checked, but this is a reasonable compromise.
181 wxDebugContext::SetCheckpoint();
184 if (!wxApp::Initialize())
189 if (!wxApp::GetInitializerFunction())
191 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
195 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
200 printf( "wxWindows error: wxTheApp == NULL\n" );
204 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
205 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
207 wxTheApp
->argc
= argc
;
208 wxTheApp
->argv
= argv
;
210 // GUI-specific initialization, such as creating an app context.
211 wxTheApp
->OnInitGui();
213 // Here frames insert themselves automatically
214 // into wxTopLevelWindows by getting created
218 if (wxTheApp
->OnInit())
220 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
223 // flush the logged messages if any
224 wxLog
*pLog
= wxLog::GetActiveTarget();
225 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
228 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
229 // for further messages
231 if (wxTheApp
->GetTopWindow())
233 delete wxTheApp
->GetTopWindow();
234 wxTheApp
->SetTopWindow(NULL
);
237 wxTheApp
->DeletePendingObjects();
246 // Static member initialization
247 wxAppInitializerFunction
wxApp::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
254 m_wantDebugOutput
= TRUE
;
258 m_printMode
= wxPRINT_POSTSCRIPT
;
259 m_exitOnFrameDelete
= TRUE
;
262 m_mainColormap
= (WXColormap
) NULL
;
263 m_appContext
= (WXAppContext
) NULL
;
264 m_topLevelWidget
= (WXWidget
) NULL
;
265 m_maxRequestSize
= 0;
266 m_initialDisplay
= (WXDisplay
*) 0;
269 bool wxApp::Initialized()
277 int wxApp::MainLoop()
282 * Sit around forever waiting to process X-events. Property Change
283 * event are handled special, because they have to refer to
284 * the root window rather than to a widget. therefore we can't
285 * use an Xt-eventhandler.
288 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
289 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
294 // Use this flag to allow breaking the loop via wxApp::ExitMainLoop()
297 XtAppNextEvent( (XtAppContext
) wxTheApp
->GetAppContext(), &event
);
299 ProcessXEvent((WXEvent
*) & event
);
301 if (XtAppPending( (XtAppContext
) wxTheApp
->GetAppContext() ) == 0)
305 // leave the main loop to give other threads a chance to
306 // perform their GUI work
318 // Processes an X event.
319 void wxApp::ProcessXEvent(WXEvent
* _event
)
321 XEvent
* event
= (XEvent
*) _event
;
323 if ((event
->type
== KeyPress
) && CheckForAccelerator(_event
))
325 // Do nothing! We intercepted and processed the event as an accelerator.
328 else if (event
->type
== PropertyNotify
)
330 HandlePropertyChange(_event
);
333 else if (event
->type
== ResizeRequest
)
335 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
336 * If resize event, don't resize until the last resize event for this
337 * window is recieved. Prevents flicker as windows are resized.
340 Display
*disp
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget());
341 Window win
= event
->xany
.window
;
346 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
348 // TODO: when implementing refresh optimization, we can use
349 // XtAddExposureToRegion to expand the window's paint region.
351 XtDispatchEvent(event
);
355 XtDispatchEvent(event
);
359 // Returns TRUE if more time is needed.
360 bool wxApp::ProcessIdle()
363 event
.SetEventObject(this);
366 return event
.MoreRequested();
369 void wxApp::ExitMainLoop()
374 // Is a message/event pending?
375 bool wxApp::Pending()
377 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
379 // Fix by Doug from STI, to prevent a stall if non-X event
381 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
384 // Dispatch a message.
385 void wxApp::Dispatch()
387 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
390 XtAppNextEvent((XtAppContext
) GetAppContext(), &event
);
391 ProcessXEvent((WXEvent
*) & event
);
394 // This should be redefined in a derived class for
395 // handling property change events for XAtom IPC.
396 void wxApp::HandlePropertyChange(WXEvent
*event
)
398 // by default do nothing special
399 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
402 void wxApp::OnIdle(wxIdleEvent
& event
)
404 static bool inOnIdle
= FALSE
;
406 // Avoid recursion (via ProcessEvent default case)
412 // 'Garbage' collection of windows deleted with Close().
413 DeletePendingObjects();
415 // flush the logged messages if any
416 wxLog
*pLog
= wxLog::GetActiveTarget();
417 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
420 // Send OnIdle events to all windows
421 bool needMore
= SendIdleEvents();
424 event
.RequestMore(TRUE
);
429 // Send idle event to all top-level windows
430 bool wxApp::SendIdleEvents()
432 bool needMore
= FALSE
;
433 wxNode
* node
= wxTopLevelWindows
.First();
436 wxWindow
* win
= (wxWindow
*) node
->Data();
437 if (SendIdleEvents(win
))
445 // Send idle event to window and all subwindows
446 bool wxApp::SendIdleEvents(wxWindow
* win
)
448 bool needMore
= FALSE
;
451 event
.SetEventObject(win
);
452 win
->ProcessEvent(event
);
454 if (event
.MoreRequested())
457 wxNode
* node
= win
->GetChildren().First();
460 wxWindow
* win
= (wxWindow
*) node
->Data();
461 if (SendIdleEvents(win
))
469 void wxApp::DeletePendingObjects()
471 wxNode
*node
= wxPendingDelete
.First();
474 wxObject
*obj
= (wxObject
*)node
->Data();
478 if (wxPendingDelete
.Member(obj
))
481 // Deleting one object may have deleted other pending
482 // objects, so start from beginning of list again.
483 node
= wxPendingDelete
.First();
487 wxLog
* wxApp::CreateLogTarget()
492 wxWindow
* wxApp::GetTopWindow() const
496 else if (wxTopLevelWindows
.Number() > 0)
497 return (wxWindow
*) wxTopLevelWindows
.First()->Data();
502 // Create an application context
503 bool wxApp::OnInitGui()
505 XtToolkitInitialize() ;
506 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext() ;
507 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
508 (const char*) wxTheApp
->GetClassName(), NULL
,
509 # if XtSpecificationRelease < 5
510 0,(Cardinal
*) &argc
,argv
) ;
515 cerr
<< "wxWindows could not open display for " << wxTheApp
->GetClassName() << ": exiting.\n";
518 m_initialDisplay
= (WXDisplay
*) dpy
;
520 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
521 applicationShellWidgetClass
,dpy
,
524 // Add general resize proc
526 rec
.string
= "resize";
527 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
528 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
530 GetMainColormap(dpy
);
531 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
536 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
538 if (!display
) /* Must be called first with non-NULL display */
539 return m_mainColormap
;
541 int defaultScreen
= DefaultScreen((Display
*) display
);
542 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
544 Colormap c
= DefaultColormapOfScreen(screen
);
547 m_mainColormap
= (WXColormap
) c
;
549 return (WXColormap
) c
;
552 // Returns TRUE if an accelerator has been processed
553 bool wxApp::CheckForAccelerator(WXEvent
* event
)
555 XEvent
* xEvent
= (XEvent
*) event
;
556 if (xEvent
->xany
.type
== KeyPress
)
558 // Find a wxWindow for this window
559 // TODO: should get display for the window, not the current display
560 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(), xEvent
->xany
.window
);
561 wxWindow
* win
= NULL
;
563 // Find the first wxWindow that corresponds to this event window
564 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
565 widget
= XtParent(widget
);
570 wxKeyEvent
keyEvent(wxEVT_CHAR
);
571 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
573 // Now we have a wxKeyEvent and we have a wxWindow.
574 // Go up the hierarchy until we find a matching accelerator,
575 // or we get to the top.
578 if (win
->ProcessAccelerator(keyEvent
))
580 win
= win
->GetParent();
591 retValue
= wxTheApp
->OnExit();
595 * Exit in some platform-specific way. Not recommended that the app calls this:
596 * only for emergencies.
601 // Yield to other processes
604 while (wxTheApp
&& wxTheApp
->Pending())
605 wxTheApp
->Dispatch();
607 // VZ: is it the same as this (taken from old wxExecute)?
609 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);