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"
35 #include "wx/thread.h"
38 #if wxUSE_WX_RESOURCES
39 #include "wx/resource.h"
43 #pragma message disable nosimpint
47 #include <X11/Xutil.h>
48 #include <X11/Xresource.h>
49 #include <X11/Xatom.h>
51 #pragma message enable nosimpint
54 #include "wx/motif/private.h"
58 extern char *wxBuffer
;
59 extern wxList wxPendingDelete
;
61 wxApp
*wxTheApp
= NULL
;
63 wxHashTable
*wxWidgetHashTable
= NULL
;
65 #if !USE_SHARED_LIBRARY
66 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
68 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
69 EVT_IDLE(wxApp::OnIdle
)
73 long wxApp::sm_lastMessageTime
= 0;
75 bool wxApp::Initialize()
77 wxBuffer
= new char[BUFSIZ
+ 512];
79 wxClassInfo::InitializeClasses();
81 // GL: I'm annoyed ... I don't know where to put this and I don't want to
82 // create a module for that as it's part of the core.
84 wxPendingEventsLocker
= new wxCriticalSection();
87 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
88 wxTheColourDatabase
->Initialize();
90 wxInitializeStockLists();
91 wxInitializeStockObjects();
93 #if wxUSE_WX_RESOURCES
94 wxInitializeResourceSystem();
97 // For PostScript printing
99 /* Done using wxModule now
100 wxInitializePrintSetupData();
101 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
102 wxThePrintPaperDatabase->CreateDatabase();
106 wxBitmap::InitStandardHandlers();
108 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
110 wxModule::RegisterModules();
111 if (!wxModule::InitializeModules()) return FALSE
;
116 void wxApp::CleanUp()
118 delete wxWidgetHashTable
;
119 wxWidgetHashTable
= NULL
;
121 wxModule::CleanUpModules();
123 #if wxUSE_WX_RESOURCES
124 wxCleanUpResourceSystem();
127 wxDeleteStockObjects() ;
129 // Destroy all GDI lists, etc.
131 delete wxTheBrushList
;
132 wxTheBrushList
= NULL
;
137 delete wxTheFontList
;
138 wxTheFontList
= NULL
;
140 delete wxTheBitmapList
;
141 wxTheBitmapList
= NULL
;
143 delete wxTheColourDatabase
;
144 wxTheColourDatabase
= NULL
;
147 /* Done using wxModule now
148 wxInitializePrintSetupData(FALSE);
149 delete wxThePrintPaperDatabase;
150 wxThePrintPaperDatabase = NULL;
154 wxBitmap::CleanUpHandlers();
159 wxClassInfo::CleanUpClasses();
164 // GL: I'm annoyed ... I don't know where to put this and I don't want to
165 // create a module for that as it's part of the core.
167 delete wxPendingEvents
;
168 delete wxPendingEventsLocker
;
171 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
172 // At this point we want to check if there are any memory
173 // blocks that aren't part of the wxDebugContext itself,
174 // as a special case. Then when dumping we need to ignore
175 // wxDebugContext, too.
176 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
178 wxLogDebug("There were memory leaks.\n");
179 wxDebugContext::Dump();
180 wxDebugContext::PrintStatistics();
184 // do it as the very last thing because everything else can log messages
185 wxLog::DontCreateOnDemand();
186 // do it as the very last thing because everything else can log messages
187 delete wxLog::SetActiveTarget(NULL
);
190 int wxEntry( int argc
, char *argv
[] )
192 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
193 // This seems to be necessary since there are 'rogue'
194 // objects present at this point (perhaps global objects?)
195 // Setting a checkpoint will ignore them as far as the
196 // memory checking facility is concerned.
197 // Of course you may argue that memory allocated in globals should be
198 // checked, but this is a reasonable compromise.
199 wxDebugContext::SetCheckpoint();
202 if (!wxApp::Initialize())
207 if (!wxApp::GetInitializerFunction())
209 printf( "wxWindows error: No initializer - use IMPLEMENT_APP macro.\n" );
213 wxTheApp
= (wxApp
*) (* wxApp::GetInitializerFunction()) ();
218 printf( "wxWindows error: wxTheApp == NULL\n" );
222 wxTheApp
->SetClassName(wxFileNameFromPath(argv
[0]));
223 wxTheApp
->SetAppName(wxFileNameFromPath(argv
[0]));
225 wxTheApp
->argc
= argc
;
226 wxTheApp
->argv
= argv
;
228 // GUI-specific initialization, such as creating an app context.
229 wxTheApp
->OnInitGui();
231 // Here frames insert themselves automatically into wxTopLevelWindows by
232 // getting created in OnInit().
235 if (wxTheApp
->OnInit())
237 if (wxTheApp
->Initialized()) retValue
= wxTheApp
->OnRun();
240 // flush the logged messages if any
241 wxLog
*pLog
= wxLog::GetActiveTarget();
242 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
245 delete wxLog::SetActiveTarget(new wxLogStderr
); // So dialog boxes aren't used
246 // for further messages
248 if (wxTheApp
->GetTopWindow())
250 delete wxTheApp
->GetTopWindow();
251 wxTheApp
->SetTopWindow(NULL
);
254 wxTheApp
->DeletePendingObjects();
263 // Static member initialization
264 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
271 m_wantDebugOutput
= TRUE
;
275 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
)
343 Widget widget
= XtWindowToWidget(event
->xany
.display
, event
->xany
.window
);
344 wxLogDebug("Got key press event for 0x%08x (parent = 0x%08x)",
345 widget
, XtParent(widget
));
348 if (CheckForAccelerator(_event
))
350 // Do nothing! We intercepted and processed the event as an
355 // It seemed before that this hack was redundant and
356 // key down events were being generated by wxCanvasInputEvent.
357 // But no longer - why ???
359 else if (CheckForKeyDown(_event
))
361 // We intercepted and processed the key down event
367 XtDispatchEvent(event
);
371 else if (event
->type
== KeyRelease
)
373 // TODO: work out why we still need this ! -michael
375 if (CheckForKeyUp(_event
))
377 // We intercepted and processed the key up event
382 XtDispatchEvent(event
);
386 else if (event
->type
== PropertyNotify
)
388 HandlePropertyChange(_event
);
391 else if (event
->type
== ResizeRequest
)
393 /* Terry Gitnick <terryg@scientech.com> - 1/21/98
394 * If resize event, don't resize until the last resize event for this
395 * window is recieved. Prevents flicker as windows are resized.
398 Display
*disp
= XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget());
399 Window win
= event
->xany
.window
;
404 while( XCheckTypedWindowEvent (disp
, win
, ResizeRequest
, &report
));
406 // TODO: when implementing refresh optimization, we can use
407 // XtAddExposureToRegion to expand the window's paint region.
409 XtDispatchEvent(event
);
413 XtDispatchEvent(event
);
417 // Returns TRUE if more time is needed.
418 bool wxApp::ProcessIdle()
421 event
.SetEventObject(this);
424 return event
.MoreRequested();
427 void wxApp::ExitMainLoop()
432 // Is a message/event pending?
433 bool wxApp::Pending()
435 XFlush(XtDisplay( (Widget
) wxTheApp
->GetTopLevelWidget() ));
437 // Fix by Doug from STI, to prevent a stall if non-X event
439 return ((XtAppPending( (XtAppContext
) GetAppContext() ) & XtIMXEvent
) != 0) ;
442 // Dispatch a message.
443 void wxApp::Dispatch()
445 // XtAppProcessEvent( (XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
448 XtAppNextEvent((XtAppContext
) GetAppContext(), &event
);
449 ProcessXEvent((WXEvent
*) & event
);
452 // This should be redefined in a derived class for
453 // handling property change events for XAtom IPC.
454 void wxApp::HandlePropertyChange(WXEvent
*event
)
456 // by default do nothing special
457 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
460 void wxApp::OnIdle(wxIdleEvent
& event
)
462 static bool inOnIdle
= FALSE
;
464 // Avoid recursion (via ProcessEvent default case)
470 // 'Garbage' collection of windows deleted with Close().
471 DeletePendingObjects();
474 // Flush pending events.
475 ProcessPendingEvents();
478 // flush the logged messages if any
479 wxLog
*pLog
= wxLog::GetActiveTarget();
480 if ( pLog
!= NULL
&& pLog
->HasPendingMessages() )
483 // Send OnIdle events to all windows
484 bool needMore
= SendIdleEvents();
487 event
.RequestMore(TRUE
);
494 // **** please implement me! ****
495 // Wake up the idle handler processor, even if it is in another thread...
499 // Send idle event to all top-level windows
500 bool wxApp::SendIdleEvents()
502 bool needMore
= FALSE
;
504 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
507 wxWindow
* win
= node
->GetData();
508 if (SendIdleEvents(win
))
510 node
= node
->GetNext();
516 // Send idle event to window and all subwindows
517 bool wxApp::SendIdleEvents(wxWindow
* win
)
519 bool needMore
= FALSE
;
522 event
.SetEventObject(win
);
523 win
->ProcessEvent(event
);
525 if (event
.MoreRequested())
528 wxNode
* node
= win
->GetChildren().First();
531 wxWindow
* win
= (wxWindow
*) node
->Data();
532 if (SendIdleEvents(win
))
540 void wxApp::DeletePendingObjects()
542 wxNode
*node
= wxPendingDelete
.First();
545 wxObject
*obj
= (wxObject
*)node
->Data();
549 if (wxPendingDelete
.Member(obj
))
552 // Deleting one object may have deleted other pending
553 // objects, so start from beginning of list again.
554 node
= wxPendingDelete
.First();
558 // Create an application context
559 bool wxApp::OnInitGui()
561 XtToolkitInitialize() ;
562 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext() ;
563 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,(String
)NULL
,NULL
,
564 (const char*) wxTheApp
->GetClassName(), NULL
, 0,
565 # if XtSpecificationRelease < 5
573 wxString
className(wxTheApp
->GetClassName());
574 wxLogError(_("wxWindows could not open display for '%s': exiting."),
575 (const char*) className
);
578 m_initialDisplay
= (WXDisplay
*) dpy
;
580 wxTheApp
->m_topLevelWidget
= (WXWidget
) XtAppCreateShell((String
)NULL
, (const char*) wxTheApp
->GetClassName(),
581 applicationShellWidgetClass
,dpy
,
584 // Add general resize proc
586 rec
.string
= "resize";
587 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
588 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
590 GetMainColormap(dpy
);
591 m_maxRequestSize
= XMaxRequestSize((Display
*) dpy
);
596 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
598 if (!display
) /* Must be called first with non-NULL display */
599 return m_mainColormap
;
601 int defaultScreen
= DefaultScreen((Display
*) display
);
602 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
604 Colormap c
= DefaultColormapOfScreen(screen
);
607 m_mainColormap
= (WXColormap
) c
;
609 return (WXColormap
) c
;
612 // Returns TRUE if an accelerator has been processed
613 bool wxApp::CheckForAccelerator(WXEvent
* event
)
615 XEvent
* xEvent
= (XEvent
*) event
;
616 if (xEvent
->xany
.type
== KeyPress
)
618 // Find a wxWindow for this window
619 // TODO: should get display for the window, not the current display
620 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(), xEvent
->xany
.window
);
621 wxWindow
* win
= NULL
;
623 // Find the first wxWindow that corresponds to this event window
624 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
625 widget
= XtParent(widget
);
630 wxKeyEvent
keyEvent(wxEVT_CHAR
);
631 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
633 // Now we have a wxKeyEvent and we have a wxWindow.
634 // Go up the hierarchy until we find a matching accelerator,
635 // or we get to the top.
638 if (win
->ProcessAccelerator(keyEvent
))
640 win
= win
->GetParent();
647 bool wxApp::CheckForKeyDown(WXEvent
* event
)
649 XEvent
* xEvent
= (XEvent
*) event
;
650 if (xEvent
->xany
.type
== KeyPress
)
652 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(),
653 xEvent
->xany
.window
);
654 wxWindow
* win
= NULL
;
656 // Find the first wxWindow that corresponds to this event window
657 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
658 widget
= XtParent(widget
);
663 wxKeyEvent
keyEvent(wxEVT_KEY_DOWN
);
664 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
666 return win
->ProcessEvent( keyEvent
);
672 bool wxApp::CheckForKeyUp(WXEvent
* event
)
674 XEvent
* xEvent
= (XEvent
*) event
;
675 if (xEvent
->xany
.type
== KeyRelease
)
677 Widget widget
= XtWindowToWidget((Display
*) wxGetDisplay(),
678 xEvent
->xany
.window
);
679 wxWindow
* win
= NULL
;
681 // Find the first wxWindow that corresponds to this event window
682 while (widget
&& !(win
= wxGetWindowFromTable(widget
)))
683 widget
= XtParent(widget
);
688 wxKeyEvent
keyEvent(wxEVT_KEY_UP
);
689 wxTranslateKeyEvent(keyEvent
, win
, (Widget
) 0, xEvent
);
691 return win
->ProcessEvent( keyEvent
);
701 retValue
= wxTheApp
->OnExit();
705 * Exit in some platform-specific way. Not recommended that the app calls this:
706 * only for emergencies.
711 // Yield to other processes
714 while (wxTheApp
&& wxTheApp
->Pending())
715 wxTheApp
->Dispatch();
717 // VZ: is it the same as this (taken from old wxExecute)?
719 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMAll
);
725 // TODO use XmGetPixmap (?) to get the really standard icons!
727 #include "wx/generic/info.xpm"
728 #include "wx/generic/error.xpm"
729 #include "wx/generic/question.xpm"
730 #include "wx/generic/warning.xpm"
733 wxApp::GetStdIcon(int which
) const
737 case wxICON_INFORMATION
:
738 return wxIcon(info_xpm
);
740 case wxICON_QUESTION
:
741 return wxIcon(question_xpm
);
743 case wxICON_EXCLAMATION
:
744 return wxIcon(warning_xpm
);
747 wxFAIL_MSG("requested non existent standard icon");
748 // still fall through
751 return wxIcon(error_xpm
);
755 // ----------------------------------------------------------------------------
756 // accessors for C modules
757 // ----------------------------------------------------------------------------
759 extern "C" XtAppContext
wxGetAppContext()
761 return (XtAppContext
)wxTheApp
->GetAppContext();