]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/motif/app.cpp
byte length for interim UniChar String corrected
[wxWidgets.git] / src / motif / app.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: app.cpp
3// Purpose: wxApp
4// Author: Julian Smart
5// Modified by:
6// Created: 17/09/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "app.h"
14#endif
15
16#ifdef __VMS
17#define XtParent XTPARENT
18#define XtDisplay XTDISPLAY
19#endif
20
21#include "wx/app.h"
22#include "wx/utils.h"
23#include "wx/module.h"
24#include "wx/memory.h"
25#include "wx/log.h"
26#include "wx/intl.h"
27#include "wx/evtloop.h"
28#include "wx/hash.h"
29#include "wx/hashmap.h"
30
31#if wxUSE_THREADS
32 #include "wx/thread.h"
33#endif
34
35#ifdef __VMS__
36#pragma message disable nosimpint
37#endif
38#include <Xm/Xm.h>
39#include <X11/Xlib.h>
40#include <X11/Xutil.h>
41#include <X11/Xresource.h>
42#include <X11/Xatom.h>
43#ifdef __VMS__
44#pragma message enable nosimpint
45#endif
46
47#include "wx/motif/private.h"
48
49#include <string.h>
50
51struct wxPerDisplayData
52{
53 wxPerDisplayData()
54 { m_visualInfo = NULL; m_topLevelWidget = NULL; }
55
56 wxXVisualInfo* m_visualInfo;
57 Widget m_topLevelWidget;
58};
59
60WX_DECLARE_VOIDPTR_HASH_MAP( wxPerDisplayData, wxPerDisplayDataMap );
61
62static void wxTLWidgetDestroyCallback(Widget w, XtPointer clientData,
63 XtPointer ptr);
64static WXWidget wxCreateTopLevelWidget( WXDisplay* display );
65
66extern wxList wxPendingDelete;
67extern bool wxAddIdleCallback();
68
69wxHashTable *wxWidgetHashTable = NULL;
70
71IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
72
73BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
74 EVT_IDLE(wxAppBase::OnIdle)
75END_EVENT_TABLE()
76
77#ifdef __WXDEBUG__
78 typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *);
79
80 XErrorHandlerFunc gs_pfnXErrorHandler = 0;
81
82 static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent)
83 {
84 // just forward to the default handler for now
85 return gs_pfnXErrorHandler(dpy, xevent);
86 }
87#endif // __WXDEBUG__
88
89bool wxApp::Initialize(int& argc, wxChar **argv)
90{
91 if ( !wxAppBase::Initialize(argc, argv) )
92 return false;
93
94 wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
95
96 return true;
97}
98
99void wxApp::CleanUp()
100{
101 delete wxWidgetHashTable;
102 wxWidgetHashTable = NULL;
103
104 wxAppBase::CleanUp();
105}
106
107void wxApp::Exit()
108{
109 wxApp::CleanUp();
110
111 wxAppConsole::Exit();
112}
113
114// ============================================================================
115// wxApp
116// ============================================================================
117
118wxApp::wxApp()
119{
120 argc = 0;
121 argv = NULL;
122
123 m_mainLoop = new wxEventLoop;
124 m_mainColormap = (WXColormap) NULL;
125 m_appContext = (WXAppContext) NULL;
126 m_initialDisplay = (WXDisplay*) 0;
127 m_perDisplayData = new wxPerDisplayDataMap;
128}
129
130wxApp::~wxApp()
131{
132 delete m_mainLoop;
133
134 for( wxPerDisplayDataMap::iterator it = m_perDisplayData->begin(),
135 end = m_perDisplayData->end();
136 it != end; ++it )
137 {
138 delete it->second.m_visualInfo;
139 XtDestroyWidget( it->second.m_topLevelWidget );
140 }
141
142 delete m_perDisplayData;
143
144 wxApp::SetInstance(NULL);
145}
146
147int wxApp::MainLoop()
148{
149 /*
150 * Sit around forever waiting to process X-events. Property Change
151 * event are handled special, because they have to refer to
152 * the root window rather than to a widget. therefore we can't
153 * use an Xt-eventhandler.
154 */
155
156 XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()),
157 XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())),
158 PropertyChangeMask);
159
160 m_mainLoop->Run();
161
162 return 0;
163}
164
165// This should be redefined in a derived class for
166// handling property change events for XAtom IPC.
167void wxApp::HandlePropertyChange(WXEvent *event)
168{
169 // by default do nothing special
170 XtDispatchEvent((XEvent*) event); /* let Motif do the work */
171}
172
173static char *fallbackResources[] = {
174 "*menuBar.marginHeight: 0",
175 "*menuBar.shadowThickness: 1",
176 "*background: #c0c0c0",
177 "*foreground: black",
178 NULL
179};
180
181// Create an application context
182bool wxApp::OnInitGui()
183{
184 if( !wxAppBase::OnInitGui() )
185 return FALSE;
186
187 XtToolkitInitialize() ;
188 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext();
189 XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources);
190
191 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL,
192 wxTheApp->GetClassName().c_str(), NULL, 0,
193# if XtSpecificationRelease < 5
194 (Cardinal*) &argc,
195# else
196 &argc,
197# endif
198 argv);
199
200 if (!dpy) {
201 // if you don't log to stderr, nothing will be shown...
202 delete wxLog::SetActiveTarget(new wxLogStderr);
203 wxString className(wxTheApp->GetClassName());
204 wxLogError(_("wxWindows could not open display for '%s': exiting."),
205 className.c_str());
206 exit(-1);
207 }
208 m_initialDisplay = (WXDisplay*) dpy;
209
210#ifdef __WXDEBUG__
211 // install the X error handler
212 gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler);
213#endif // __WXDEBUG__
214
215 // Add general resize proc
216 XtActionsRec rec;
217 rec.string = "resize";
218 rec.proc = (XtActionProc)wxWidgetResizeProc;
219 XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1);
220
221 GetMainColormap(dpy);
222
223 wxAddIdleCallback();
224
225 return TRUE;
226}
227
228WXColormap wxApp::GetMainColormap(WXDisplay* display)
229{
230 if (!display) /* Must be called first with non-NULL display */
231 return m_mainColormap;
232
233 int defaultScreen = DefaultScreen((Display*) display);
234 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
235
236 Colormap c = DefaultColormapOfScreen(screen);
237
238 if (!m_mainColormap)
239 m_mainColormap = (WXColormap) c;
240
241 return (WXColormap) c;
242}
243
244wxXVisualInfo* wxApp::GetVisualInfo( WXDisplay* display )
245{
246 wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display );
247
248 if( it != m_perDisplayData->end() && it->second.m_visualInfo )
249 return it->second.m_visualInfo;
250
251 wxXVisualInfo* vi = new wxXVisualInfo;
252 wxFillXVisualInfo( vi, (Display*)display );
253
254 (*m_perDisplayData)[display].m_visualInfo = vi;
255
256 return vi;
257}
258
259static void wxTLWidgetDestroyCallback(Widget w, XtPointer clientData,
260 XtPointer ptr)
261{
262 if( wxTheApp )
263 wxTheApp->SetTopLevelWidget( (WXDisplay*)XtDisplay(w),
264 (WXWidget)NULL );
265}
266
267WXWidget wxCreateTopLevelWidget( WXDisplay* display )
268{
269 Widget tlw = XtAppCreateShell( (String)NULL,
270 wxTheApp->GetClassName().c_str(),
271 applicationShellWidgetClass,
272 (Display*)display,
273 NULL, 0 );
274 XtSetMappedWhenManaged( tlw, False );
275 XtRealizeWidget( tlw );
276
277 XtAddCallback( tlw, XmNdestroyCallback,
278 (XtCallbackProc)wxTLWidgetDestroyCallback,
279 (XtPointer)NULL );
280
281 return (WXWidget)tlw;
282}
283
284WXWidget wxApp::GetTopLevelWidget()
285{
286 WXDisplay* display = wxGetDisplay();
287 wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display );
288
289 if( it != m_perDisplayData->end() && it->second.m_topLevelWidget )
290 return (WXWidget)it->second.m_topLevelWidget;
291
292 WXWidget tlw = wxCreateTopLevelWidget( display );
293 SetTopLevelWidget( display, tlw );
294
295 return tlw;
296}
297
298void wxApp::SetTopLevelWidget(WXDisplay* display, WXWidget widget)
299{
300 (*m_perDisplayData)[display].m_topLevelWidget = (Widget)widget;
301}
302
303// Yield to other processes
304
305bool wxApp::Yield(bool onlyIfNeeded)
306{
307 static bool s_inYield = FALSE;
308
309 if ( s_inYield )
310 {
311 if ( !onlyIfNeeded )
312 {
313 wxFAIL_MSG( wxT("wxYield called recursively" ) );
314 }
315
316 return FALSE;
317 }
318
319 s_inYield = TRUE;
320
321 while (wxTheApp && wxTheApp->Pending())
322 wxTheApp->Dispatch();
323
324 s_inYield = FALSE;
325
326 return TRUE;
327}
328
329// ----------------------------------------------------------------------------
330// accessors for C modules
331// ----------------------------------------------------------------------------
332
333extern "C" XtAppContext wxGetAppContext()
334{
335 return (XtAppContext)wxTheApp->GetAppContext();
336}