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 // TODO: Robert, what's this for?
319 // Processes an X event.
320 void wxApp::ProcessXEvent(WXEvent
* _event
)
322 XEvent
* event
= (XEvent
*) _event
;
324 if ((event
->type
== KeyPress
) && CheckForAccelerator(_event
))
326 // Do nothing! We intercepted and processed the event as an accelerator.
329 else if (event
->type
== PropertyNotify
)
331 HandlePropertyChange(_event
);
334 else if (event
->type
== ResizeRequest
)
336 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
337 * If resize event, don't resize until the last resize event for this
338 * window is recieved. Prevents flicker as windows are resized.
341 Display
*disp
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget());
342 Window win
= event
->xany
.window
;
347 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
349 // TODO: when implementing refresh optimization, we can use
350 // XtAddExposureToRegion to expand the window's paint region.
352 XtDispatchEvent(event
);
356 XtDispatchEvent(event
);
360 // Returns TRUE if more time is needed.
361 bool wxApp::ProcessIdle()
364 event
.SetEventObject(this);
367 return event
.MoreRequested();
370 void wxApp::ExitMainLoop()
375 // Is a message/event pending?
376 bool wxApp::Pending()
378 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
380 // Fix by Doug from STI, to prevent a stall if non-X event
382 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
385 // Dispatch a message.
386 void wxApp::Dispatch()
388 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
391 XtAppNextEvent((XtAppContext
) GetAppContext(), &event
);
392 ProcessXEvent((WXEvent
*) & event
);
395 // This should be redefined in a derived class for
396 // handling property change events for XAtom IPC.
397 void wxApp::HandlePropertyChange(WXEvent
*event
)
399 // by default do nothing special
400 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
403 void wxApp::OnIdle(wxIdleEvent
& event
)
405 static bool inOnIdle
= FALSE
;
407 // Avoid recursion (via ProcessEvent default case)
413 // 'Garbage' collection of windows deleted with Close().
414 DeletePendingObjects();
416 // flush the logged messages if any
417 wxLog
*pLog
= wxLog::GetActiveTarget();
418 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
421 // Send OnIdle events to all windows
422 bool needMore
= SendIdleEvents();
425 event
.RequestMore(TRUE
);
430 // Send idle event to all top-level windows
431 bool wxApp::SendIdleEvents()
433 bool needMore
= FALSE
;
434 wxNode
* node
= wxTopLevelWindows
.First();
437 wxWindow
* win
= (wxWindow
*) node
->Data();
438 if (SendIdleEvents(win
))
446 // Send idle event to window and all subwindows
447 bool wxApp::SendIdleEvents(wxWindow
* win
)
449 bool needMore
= FALSE
;
452 event
.SetEventObject(win
);
453 win
->ProcessEvent(event
);
455 if (event
.MoreRequested())
458 wxNode
* node
= win
->GetChildren().First();
461 wxWindow
* win
= (wxWindow
*) node
->Data();
462 if (SendIdleEvents(win
))
470 void wxApp::DeletePendingObjects()
472 wxNode
*node
= wxPendingDelete
.First();
475 wxObject
*obj
= (wxObject
*)node
->Data();
479 if (wxPendingDelete
.Member(obj
))
482 // Deleting one object may have deleted other pending
483 // objects, so start from beginning of list again.
484 node
= wxPendingDelete
.First();
488 wxLog
* wxApp::CreateLogTarget()
493 wxWindow
* wxApp::GetTopWindow() const
497 else if (wxTopLevelWindows
.Number() > 0)
498 return (wxWindow
*) wxTopLevelWindows
.First()->Data();
503 // Create an application context
504 bool wxApp::OnInitGui()
506 XtToolkitInitialize() ;
507 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext() ;
508 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
509 (const char*) wxTheApp
->GetClassName(), NULL
,
510 # if XtSpecificationRelease < 5
511 0,(Cardinal
*) &argc
,argv
) ;
516 cerr
<< "wxWindows could not open display for " << wxTheApp
->GetClassName() << ": exiting.\n";
519 m_initialDisplay
= (WXDisplay
*) dpy
;
521 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
522 applicationShellWidgetClass
,dpy
,
525 // Add general resize proc
527 rec
.string
= "resize";
528 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
529 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
531 GetMainColormap(dpy
);
532 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
537 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
539 if (!display
) /* Must be called first with non-NULL display */
540 return m_mainColormap
;
542 int defaultScreen
= DefaultScreen((Display
*) display
);
543 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
545 Colormap c
= DefaultColormapOfScreen(screen
);
548 m_mainColormap
= (WXColormap
) c
;
550 return (WXColormap
) c
;
553 // Returns TRUE if an accelerator has been processed
554 bool wxApp::CheckForAccelerator(WXEvent
* event
)
556 XEvent
* xEvent
= (XEvent
*) event
;
557 if (xEvent
->xany
.type
== KeyPress
)
559 // Find a wxWindow for this window
560 // TODO: should get display for the window, not the current display
561 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(), xEvent
->xany
.window
);
562 wxWindow
* win
= NULL
;
564 // Find the first wxWindow that corresponds to this event window
565 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
566 widget
= XtParent(widget
);
571 wxKeyEvent
keyEvent(wxEVT_CHAR
);
572 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
574 // Now we have a wxKeyEvent and we have a wxWindow.
575 // Go up the hierarchy until we find a matching accelerator,
576 // or we get to the top.
579 if (win
->ProcessAccelerator(keyEvent
))
581 win
= win
->GetParent();
592 retValue
= wxTheApp
->OnExit();
596 * Exit in some platform-specific way. Not recommended that the app calls this:
597 * only for emergencies.
602 // Yield to other processes
605 while (wxTheApp
&& wxTheApp
->Pending())
606 wxTheApp
->Dispatch();