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