]> git.saurik.com Git - wxWidgets.git/blame - src/motif/app.cpp
use iterator in wxTextBuffer::Translate() instead of c_str()
[wxWidgets.git] / src / motif / app.cpp
CommitLineData
4bb6408c 1/////////////////////////////////////////////////////////////////////////////
55034339 2// Name: src/motif/app.cpp
4bb6408c
JS
3// Purpose: wxApp
4// Author: Julian Smart
5// Modified by:
6// Created: 17/09/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
1248b41f
MB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
bcd055ae
JJ
15#ifdef __VMS
16#define XtParent XTPARENT
17#define XtDisplay XTDISPLAY
18#endif
19
88a7a4e1
WS
20#include "wx/app.h"
21
32d4c30a
WS
22#ifndef WX_PRECOMP
23 #include "wx/hash.h"
88a7a4e1 24 #include "wx/intl.h"
e4db172a 25 #include "wx/log.h"
de6185e2 26 #include "wx/utils.h"
5b56bffb 27 #include "wx/memory.h"
73216ce6 28 #include "wx/font.h"
32d4c30a
WS
29#endif
30
7e1bcfa8 31#include "wx/evtloop.h"
b412f9be 32
7bcb11d3 33#if wxUSE_THREADS
dfe1eee3 34 #include "wx/thread.h"
7bcb11d3 35#endif
4bb6408c 36
338dd992 37#ifdef __VMS__
02761f6c 38 #pragma message disable nosimpint
338dd992 39#endif
4bb6408c
JS
40#include <Xm/Xm.h>
41#include <X11/Xlib.h>
42#include <X11/Xutil.h>
43#include <X11/Xresource.h>
44#include <X11/Xatom.h>
338dd992 45#ifdef __VMS__
02761f6c 46 #pragma message enable nosimpint
338dd992 47#endif
4bb6408c
JS
48
49#include "wx/motif/private.h"
50
51#include <string.h>
52
eb6fa4b4
MB
53struct wxPerDisplayData
54{
55 wxPerDisplayData()
5a2e3d8c
MB
56 {
57 m_visualInfo = NULL;
58 m_topLevelWidget = NULL;
59 m_topLevelRealizedWidget = NULL;
60 }
eb6fa4b4
MB
61
62 wxXVisualInfo* m_visualInfo;
5a2e3d8c 63 Widget m_topLevelWidget, m_topLevelRealizedWidget;
eb6fa4b4
MB
64};
65
eb6fa4b4
MB
66static void wxTLWidgetDestroyCallback(Widget w, XtPointer clientData,
67 XtPointer ptr);
68static WXWidget wxCreateTopLevelWidget( WXDisplay* display );
9ce8d6a2 69
7e1bcfa8 70extern bool wxAddIdleCallback();
7491d644 71
4bb6408c
JS
72wxHashTable *wxWidgetHashTable = NULL;
73
4bb6408c 74IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
9c6e1335 75
e838cc14 76#ifdef __WXDEBUG__
86ac4a22
VZ
77extern "C"
78{
e838cc14 79 typedef int (*XErrorHandlerFunc)(Display *, XErrorEvent *);
86ac4a22 80}
e838cc14 81
86ac4a22 82XErrorHandlerFunc gs_pfnXErrorHandler = 0;
e838cc14 83
86ac4a22
VZ
84extern "C"
85{
86
87static int wxXErrorHandler(Display *dpy, XErrorEvent *xevent)
88{
89 // just forward to the default handler for now
90 return gs_pfnXErrorHandler(dpy, xevent);
91}
92
93}
e838cc14
VZ
94#endif // __WXDEBUG__
95
8ecf7346 96bool wxApp::Initialize(int& argc_, wxChar **argv_)
4bb6408c 97{
105fbe1f
MB
98#if wxUSE_INTL
99 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
100#endif
101
8ecf7346 102 if ( !wxAppBase::Initialize(argc_, argv_) )
94826170 103 return false;
afb74891 104
4bb6408c 105 wxWidgetHashTable = new wxHashTable(wxKEY_INTEGER);
afb74891 106
0fad0dfd
VZ
107#ifdef __HPUX__
108 // under HP-UX creating XmFontSet fails when the system locale is C and
109 // we're using a remote DISPLAY, presumably because HP-UX uses its own
110 // names for C and ISO locales (roman8 and iso8859n respectively) and so
111 // its Motif libraries have troubles with non-HP X server
112 //
113 // whatever the reason, the fact is that without this hack any wxMotif
114 // program crashes on startup because it can't create any font (HP programs
115 // still work but they do spit out messages about failing to create font
116 // sets and failing back on "fixed" font too)
117 //
118 // notice that calling setlocale() here is not enough because X(m) init
119 // functions call setlocale() later so we really have to change environment
120 bool fixAll = false; // tweak LC_ALL (or just LC_CTYPE)?
121 const char *loc = getenv("LC_CTYPE");
122 if ( !loc )
123 {
124 loc = getenv("LC_ALL");
125 if ( loc )
126 fixAll = true;
127 }
128
129 if ( !loc ||
130 (loc[0] == 'C' && loc[1] == '\0') ||
131 strcmp(loc, "POSIX") == 0 )
132 {
133 // we're using C locale, "fix" it
134 wxLogDebug(_T("HP-UX fontset hack: forcing locale to en_US.iso88591"));
135 putenv(fixAll ? "LC_ALL=en_US.iso88591" : "LC_CTYPE=en_US.iso88591");
136 }
137#endif // __HPUX__
138
9d02485e 139 XtSetLanguageProc(NULL, NULL, NULL);
4bb6408c 140 XtToolkitInitialize() ;
edc1cd8b 141 wxTheApp->m_appContext = (WXAppContext) XtCreateApplicationContext();
8ecf7346
VZ
142
143 static char *fallbackResources[] = {
144 // better defaults for CDE under Irix
145 //
146 // TODO: do something similar for the other systems, the hardcoded defaults
147 // below are ugly
148#ifdef __SGI__
149 wxMOTIF_STR("*sgiMode: True"),
150 wxMOTIF_STR("*useSchemes: all"),
151#else // !__SGI__
152#if !wxMOTIF_USE_RENDER_TABLE
153 wxMOTIF_STR("*.fontList: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*"),
154#else
155 wxMOTIF_STR("*wxDefaultRendition.fontName: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*"),
156 wxMOTIF_STR("*wxDefaultRendition.fontType: FONT_IS_FONTSET"),
157 wxMOTIF_STR("*.renderTable: wxDefaultRendition"),
158#endif
159 wxMOTIF_STR("*listBox.background: white"),
160 wxMOTIF_STR("*text.background: white"),
161 wxMOTIF_STR("*comboBox.Text.background: white"),
162 wxMOTIF_STR("*comboBox.List.background: white"),
163#endif // __SGI__/!__SGI__
164 NULL
165 };
edc1cd8b
JS
166 XtAppSetFallbackResources((XtAppContext) wxTheApp->m_appContext, fallbackResources);
167
0b6d76bf
VZ
168 // we shouldn't pass empty application/class name as it results in
169 // immediate crash inside XOpenIM() (if XIM is used) under IRIX
170 wxString appname = wxTheApp->GetAppName();
171 if ( appname.empty() )
172 appname = _T("wxapp");
173 wxString clsname = wxTheApp->GetClassName();
174 if ( clsname.empty() )
175 clsname = _T("wx");
176
6991087b
VS
177 // FIXME-UTF8: This code is taken from wxGTK and duplicated here. This
178 // is just a temporary fix to make wxX11 compile in Unicode
179 // build, the real fix is to change Initialize()'s signature
180 // to use char* on Unix.
181#if wxUSE_UNICODE
182 // XtOpenDisplay() wants char*, not wchar_t*, so convert
183 int i;
184 char **argvX11 = new char *[argc + 1];
185 for ( i = 0; i < argc; i++ )
186 {
8ecf7346 187 argvX11[i] = strdup(wxConvLibc.cWX2MB(argv_[i]));
6991087b
VS
188 }
189
190 argvX11[argc] = NULL;
191
192 int argcX11 = argc;
193
194 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,
195 (String)NULL,
196 appname.c_str(),
197 clsname.c_str(),
198 NULL, 0, // no options
199# if XtSpecificationRelease < 5
200 (Cardinal*) &argcX11,
201# else
202 &argcX11,
203# endif
204 argvX11);
205
206 if ( argcX11 != argc )
207 {
208 // we have to drop the parameters which were consumed by X11+
209 for ( i = 0; i < argcX11; i++ )
210 {
8ecf7346 211 while ( strcmp(wxConvLibc.cWX2MB(argv_[i]), argvX11[i]) != 0 )
6991087b 212 {
8ecf7346 213 memmove(argv_ + i, argv_ + i + 1, (argc - i)*sizeof(*argv_));
6991087b
VS
214 }
215 }
216
217 argc = argcX11;
8ecf7346
VZ
218
219 // update internal arg[cv] as X11 may have removed processed options:
220 argc = argc_;
221 argv = argv_;
6991087b
VS
222 }
223 //else: XtOpenDisplay() didn't modify our parameters
224
225 // free our copy
226 for ( i = 0; i < argcX11; i++ )
227 {
228 free(argvX11[i]);
229 }
230
231 delete [] argvX11;
232
233#else // ANSI
234
0b6d76bf
VZ
235 Display *dpy = XtOpenDisplay((XtAppContext) wxTheApp->m_appContext,
236 (String)NULL,
237 appname.c_str(),
238 clsname.c_str(),
239 NULL, 0, // no options
4bb6408c 240# if XtSpecificationRelease < 5
9c6e1335 241 (Cardinal*) &argc,
4bb6408c 242# else
9c6e1335 243 &argc,
4bb6408c 244# endif
9c6e1335
VZ
245 argv);
246
6991087b
VS
247#endif // Unicode/ANSI
248
4bb6408c 249 if (!dpy) {
c88b736c
MB
250 // if you don't log to stderr, nothing will be shown...
251 delete wxLog::SetActiveTarget(new wxLogStderr);
7bcb11d3 252 wxString className(wxTheApp->GetClassName());
77ffb593 253 wxLogError(_("wxWidgets could not open display for '%s': exiting."),
d3a80c92 254 className.c_str());
2d120f83 255 exit(-1);
4bb6408c 256 }
47bc1060 257 m_initialDisplay = (WXDisplay*) dpy;
afb74891 258
e838cc14
VZ
259#ifdef __WXDEBUG__
260 // install the X error handler
261 gs_pfnXErrorHandler = XSetErrorHandler(wxXErrorHandler);
262#endif // __WXDEBUG__
263
4bb6408c
JS
264 // Add general resize proc
265 XtActionsRec rec;
f1db433a 266 rec.string = wxMOTIF_STR("resize");
4bb6408c
JS
267 rec.proc = (XtActionProc)wxWidgetResizeProc;
268 XtAppAddActions((XtAppContext) wxTheApp->m_appContext, &rec, 1);
afb74891 269
4bb6408c 270 GetMainColormap(dpy);
afb74891 271
7e1bcfa8
MB
272 wxAddIdleCallback();
273
96be256b 274 return true;
4bb6408c
JS
275}
276
8ecf7346
VZ
277void wxApp::CleanUp()
278{
279 wxAppBase::CleanUp();
280
281 delete wxWidgetHashTable;
282 wxWidgetHashTable = NULL;
283
284 delete m_mainLoop;
285
286 for( wxPerDisplayDataMap::iterator it = m_perDisplayData->begin(),
287 end = m_perDisplayData->end();
288 it != end; ++it )
289 {
290 delete it->second->m_visualInfo;
291 // On Solaris 10 calling XtDestroyWidget on the top level widget
292 // dumps core if the locale is set to something other than "C"
293#ifndef __SUN__
294 XtDestroyWidget( it->second->m_topLevelWidget );
295#endif
296 delete it->second;
297 }
298}
299
300void wxApp::Exit()
301{
302 wxApp::CleanUp();
303
304 wxAppConsole::Exit();
305}
306
307// ============================================================================
308// wxApp
309// ============================================================================
310
311wxApp::wxApp()
312{
313 m_mainLoop = new wxEventLoop;
314 m_mainColormap = (WXColormap) NULL;
315 m_appContext = (WXAppContext) NULL;
316 m_initialDisplay = (WXDisplay*) 0;
317 m_perDisplayData = new wxPerDisplayDataMap;
318}
319
320wxApp::~wxApp()
321{
322 delete m_perDisplayData;
323}
324
325int wxApp::MainLoop()
326{
327 /*
328 * Sit around forever waiting to process X-events. Property Change
329 * event are handled special, because they have to refer to
330 * the root window rather than to a widget. therefore we can't
331 * use an Xt-eventhandler.
332 */
333
334 XSelectInput(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()),
335 XDefaultRootWindow(XtDisplay((Widget) wxTheApp->GetTopLevelWidget())),
336 PropertyChangeMask);
337
338 m_mainLoop->Run();
339
340 return 0;
341}
342
343// This should be redefined in a derived class for
344// handling property change events for XAtom IPC.
345void wxApp::HandlePropertyChange(WXEvent *event)
346{
347 // by default do nothing special
348 XtDispatchEvent((XEvent*) event); /* let Motif do the work */
349}
350
4bb6408c
JS
351WXColormap wxApp::GetMainColormap(WXDisplay* display)
352{
353 if (!display) /* Must be called first with non-NULL display */
2d120f83 354 return m_mainColormap;
a91b47e8
JS
355
356 int defaultScreen = DefaultScreen((Display*) display);
357 Screen* screen = XScreenOfDisplay((Display*) display, defaultScreen);
afb74891 358
a91b47e8 359 Colormap c = DefaultColormapOfScreen(screen);
afb74891 360
4bb6408c 361 if (!m_mainColormap)
2d120f83 362 m_mainColormap = (WXColormap) c;
afb74891 363
4bb6408c
JS
364 return (WXColormap) c;
365}
366
bdcade0a
MB
367static inline wxPerDisplayData& GetOrCreatePerDisplayData
368 ( wxPerDisplayDataMap& m, WXDisplay* display )
9ce8d6a2 369{
bdcade0a
MB
370 wxPerDisplayDataMap::iterator it = m.find( display );
371 if( it != m.end() && it->second != NULL )
372 return *(it->second);
373
374 wxPerDisplayData* nData = new wxPerDisplayData();
375 m[display] = nData;
376
377 return *nData;
378}
9ce8d6a2 379
bdcade0a
MB
380wxXVisualInfo* wxApp::GetVisualInfo( WXDisplay* display )
381{
382 wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData,
383 display );
384 if( data.m_visualInfo )
385 return data.m_visualInfo;
9ce8d6a2
MB
386
387 wxXVisualInfo* vi = new wxXVisualInfo;
388 wxFillXVisualInfo( vi, (Display*)display );
389
bdcade0a 390 data.m_visualInfo = vi;
9ce8d6a2
MB
391
392 return vi;
393}
394
55034339
WS
395static void wxTLWidgetDestroyCallback(Widget w, XtPointer WXUNUSED(clientData),
396 XtPointer WXUNUSED(ptr))
eb6fa4b4
MB
397{
398 if( wxTheApp )
5a2e3d8c 399 {
eb6fa4b4
MB
400 wxTheApp->SetTopLevelWidget( (WXDisplay*)XtDisplay(w),
401 (WXWidget)NULL );
5a2e3d8c
MB
402 wxTheApp->SetTopLevelRealizedWidget( (WXDisplay*)XtDisplay(w),
403 (WXWidget)NULL );
404 }
eb6fa4b4
MB
405}
406
407WXWidget wxCreateTopLevelWidget( WXDisplay* display )
408{
409 Widget tlw = XtAppCreateShell( (String)NULL,
410 wxTheApp->GetClassName().c_str(),
411 applicationShellWidgetClass,
412 (Display*)display,
413 NULL, 0 );
5a2e3d8c
MB
414 XtVaSetValues( tlw,
415 XmNoverrideRedirect, True,
416 NULL );
eb6fa4b4
MB
417
418 XtAddCallback( tlw, XmNdestroyCallback,
419 (XtCallbackProc)wxTLWidgetDestroyCallback,
420 (XtPointer)NULL );
421
422 return (WXWidget)tlw;
423}
424
55034339 425WXWidget wxCreateTopLevelRealizedWidget( WXDisplay* WXUNUSED(display) )
5a2e3d8c 426{
cb81f1a6 427 Widget rTlw = XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass,
5a2e3d8c 428 (Widget)wxTheApp->GetTopLevelWidget(),
b09a59f5 429 NULL );
5a2e3d8c
MB
430 XtSetMappedWhenManaged( rTlw, False );
431 XtRealizeWidget( rTlw );
432
433 return (WXWidget)rTlw;
434}
435
eb6fa4b4
MB
436WXWidget wxApp::GetTopLevelWidget()
437{
438 WXDisplay* display = wxGetDisplay();
bdcade0a
MB
439 wxPerDisplayData& data = GetOrCreatePerDisplayData( *m_perDisplayData,
440 display );
441 if( data.m_topLevelWidget )
442 return (WXWidget)data.m_topLevelWidget;
eb6fa4b4
MB
443
444 WXWidget tlw = wxCreateTopLevelWidget( display );
445 SetTopLevelWidget( display, tlw );
446
447 return tlw;
448}
449
5a2e3d8c
MB
450WXWidget wxApp::GetTopLevelRealizedWidget()
451{
452 WXDisplay* display = wxGetDisplay();
453 wxPerDisplayDataMap::iterator it = m_perDisplayData->find( display );
454
455 if( it != m_perDisplayData->end() && it->second->m_topLevelRealizedWidget )
456 return (WXWidget)it->second->m_topLevelRealizedWidget;
457
458 WXWidget rTlw = wxCreateTopLevelRealizedWidget( display );
459 SetTopLevelRealizedWidget( display, rTlw );
460
461 return rTlw;
462}
463
eb6fa4b4
MB
464void wxApp::SetTopLevelWidget(WXDisplay* display, WXWidget widget)
465{
5a2e3d8c
MB
466 GetOrCreatePerDisplayData( *m_perDisplayData, display )
467 .m_topLevelWidget = (Widget)widget;
468}
469
470void wxApp::SetTopLevelRealizedWidget(WXDisplay* display, WXWidget widget)
471{
472 GetOrCreatePerDisplayData( *m_perDisplayData, display )
473 .m_topLevelRealizedWidget = (Widget)widget;
eb6fa4b4
MB
474}
475
4bb6408c 476// Yield to other processes
cb2713bf 477
8461e4c2 478bool wxApp::Yield(bool onlyIfNeeded)
4bb6408c 479{
96be256b 480 static bool s_inYield = false;
8461e4c2
VZ
481
482 if ( s_inYield )
483 {
484 if ( !onlyIfNeeded )
485 {
486 wxFAIL_MSG( wxT("wxYield called recursively" ) );
487 }
488
96be256b 489 return false;
8461e4c2
VZ
490 }
491
96be256b 492 s_inYield = true;
cb2713bf 493
4bb6408c 494 while (wxTheApp && wxTheApp->Pending())
2d120f83 495 wxTheApp->Dispatch();
518b5d2f 496
96be256b 497 s_inYield = false;
cb2713bf 498
96be256b 499 return true;
4bb6408c
JS
500}
501
d391a345
VZ
502// ----------------------------------------------------------------------------
503// accessors for C modules
504// ----------------------------------------------------------------------------
505
506extern "C" XtAppContext wxGetAppContext()
507{
508 return (XtAppContext)wxTheApp->GetAppContext();
509}