]> git.saurik.com Git - wxWidgets.git/blob - src/motif/app.cpp
fixed single word selection
[wxWidgets.git] / src / motif / app.cpp
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
51 struct wxPerDisplayData
52 {
53 wxPerDisplayData()
54 { m_visualInfo = NULL; m_topLevelWidget = NULL; }
55
56 wxXVisualInfo* m_visualInfo;
57 Widget m_topLevelWidget;
58 };
59
60 WX_DECLARE_VOIDPTR_HASH_MAP( wxPerDisplayData, wxPerDisplayDataMap );
61
62 static void wxTLWidgetDestroyCallback(Widget w, XtPointer clientData,
63 XtPointer ptr);
64 static WXWidget wxCreateTopLevelWidget( WXDisplay* display );
65
66 extern wxList wxPendingDelete;
67 extern bool wxAddIdleCallback();
68
69 wxHashTable *wxWidgetHashTable = NULL;
70
71 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
72
73 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
74 EVT_IDLE(wxAppBase::OnIdle)
75 END_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
89 bool 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
99 void wxApp::CleanUp()
100 {
101 delete wxWidgetHashTable;
102 wxWidgetHashTable = NULL;
103
104 wxAppBase::CleanUp();
105 }
106
107 void wxApp::Exit()
108 {
109 wxApp::CleanUp();
110
111 wxAppConsole::Exit();
112 }
113
114 // ============================================================================
115 // wxApp
116 // ============================================================================
117
118 wxApp::wxApp()
119 {
120 argc = 0;
121 argv = NULL;
122
123 m_eventLoop = new wxEventLoop;
124 m_mainColormap = (WXColormap) NULL;
125 m_appContext = (WXAppContext) NULL;
126 m_initialDisplay = (WXDisplay*) 0;
127 m_perDisplayData = new wxPerDisplayDataMap;
128 }
129
130 wxApp::~wxApp()
131 {
132 delete m_eventLoop;
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
147 bool wxApp::Initialized()
148 {
149 if (GetTopWindow())
150 return TRUE;
151 else
152 return FALSE;
153 }
154
155 int wxApp::MainLoop()
156 {
157 /*
158 * Sit around forever waiting to process X-events. Property Change
159 * event are handled special, because they have to refer to
160 * the root window rather than to a widget. therefore we can't
161 * use an Xt-eventhandler.
162 */
163
164 XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()),
165 XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())),
166 PropertyChangeMask);
167
168 m_eventLoop->Run();
169
170 return 0;
171 }
172
173 void wxApp::ExitMainLoop()
174 {
175 if( m_eventLoop->IsRunning() )
176 m_eventLoop->Exit();
177 }
178
179 // This should be redefined in a derived class for
180 // handling property change events for XAtom IPC.
181 void wxApp::HandlePropertyChange(WXEvent *event)
182 {
183 // by default do nothing special
184 XtDispatchEvent((XEvent*) event); /* let Motif do the work */
185 }
186
187 static char *fallbackResources[] = {
188 "*menuBar.marginHeight: 0",
189 "*menuBar.shadowThickness: 1",
190 "*background: #c0c0c0",
191 "*foreground: black",
192 NULL
193 };
194
195 // Create an application context
196 bool wxApp::OnInitGui()
197 {
198 if( !wxAppBase::OnInitGui() )
199 return FALSE;
200
201 XtToolkitInitialize() ;
202 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext();
203 XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources);
204
205 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,(String)NULL,NULL,
206 wxTheApp->GetClassName().c_str(), NULL, 0,
207 # if XtSpecificationRelease < 5
208 (Cardinal*) &argc,
209 # else
210 &argc,
211 # endif
212 argv);
213
214 if (!dpy) {
215 // if you don't log to stderr, nothing will be shown...
216 delete wxLog::SetActiveTarget(new wxLogStderr);
217 wxString className(wxTheApp->GetClassName());
218 wxLogError(_("wxWindows could not open display for '%s': exiting."),
219 className.c_str());
220 exit(-1);
221 }
222 m_initialDisplay = (WXDisplay*) dpy;
223
224 #ifdef __WXDEBUG__
225 // install the X error handler
226 gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler);
227 #endif // __WXDEBUG__
228
229 // Add general resize proc
230 XtActionsRec rec;
231 rec.string = "resize";
232 rec.proc = (XtActionProc)wxWidgetResizeProc;
233 XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1);
234
235 GetMainColormap(dpy);
236
237 wxAddIdleCallback();
238
239 return TRUE;
240 }
241
242 WXColormap wxApp::GetMainColormap(WXDisplay* display)
243 {
244 if (!display) /* Must be called first with non-NULL display */
245 return m_mainColormap;
246
247 int defaultScreen = DefaultScreen((Display*) display);
248 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
249
250 Colormap c = DefaultColormapOfScreen(screen);
251
252 if (!m_mainColormap)
253 m_mainColormap = (WXColormap) c;
254
255 return (WXColormap) c;
256 }
257
258 wxXVisualInfo* wxApp::GetVisualInfo( WXDisplay* display )
259 {
260 wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display );
261
262 if( it != m_perDisplayData->end() && it->second.m_visualInfo )
263 return it->second.m_visualInfo;
264
265 wxXVisualInfo* vi = new wxXVisualInfo;
266 wxFillXVisualInfo( vi, (Display*)display );
267
268 (*m_perDisplayData)[display].m_visualInfo = vi;
269
270 return vi;
271 }
272
273 static void wxTLWidgetDestroyCallback(Widget w, XtPointer clientData,
274 XtPointer ptr)
275 {
276 if( wxTheApp )
277 wxTheApp->SetTopLevelWidget( (WXDisplay*)XtDisplay(w),
278 (WXWidget)NULL );
279 }
280
281 WXWidget wxCreateTopLevelWidget( WXDisplay* display )
282 {
283 Widget tlw = XtAppCreateShell( (String)NULL,
284 wxTheApp->GetClassName().c_str(),
285 applicationShellWidgetClass,
286 (Display*)display,
287 NULL, 0 );
288 XtSetMappedWhenManaged( tlw, False );
289 XtRealizeWidget( tlw );
290
291 XtAddCallback( tlw, XmNdestroyCallback,
292 (XtCallbackProc)wxTLWidgetDestroyCallback,
293 (XtPointer)NULL );
294
295 return (WXWidget)tlw;
296 }
297
298 WXWidget wxApp::GetTopLevelWidget()
299 {
300 WXDisplay* display = wxGetDisplay();
301 wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display );
302
303 if( it != m_perDisplayData->end() && it->second.m_topLevelWidget )
304 return (WXWidget)it->second.m_topLevelWidget;
305
306 WXWidget tlw = wxCreateTopLevelWidget( display );
307 SetTopLevelWidget( display, tlw );
308
309 return tlw;
310 }
311
312 void wxApp::SetTopLevelWidget(WXDisplay* display, WXWidget widget)
313 {
314 (*m_perDisplayData)[display].m_topLevelWidget = (Widget)widget;
315 }
316
317 // Yield to other processes
318
319 bool wxApp::Yield(bool onlyIfNeeded)
320 {
321 bool s_inYield = FALSE;
322
323 if ( s_inYield )
324 {
325 if ( !onlyIfNeeded )
326 {
327 wxFAIL_MSG( wxT("wxYield called recursively" ) );
328 }
329
330 return FALSE;
331 }
332
333 s_inYield = TRUE;
334
335 while (wxTheApp && wxTheApp->Pending())
336 wxTheApp->Dispatch();
337
338 s_inYield = FALSE;
339
340 return TRUE;
341 }
342
343 // ----------------------------------------------------------------------------
344 // accessors for C modules
345 // ----------------------------------------------------------------------------
346
347 extern "C" XtAppContext wxGetAppContext()
348 {
349 return (XtAppContext)wxTheApp->GetAppContext();
350 }