1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/app.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
22 #include "wx/memory.h"
26 #include "wx/evtloop.h"
29 #include "wx/thread.h"
33 #pragma message disable nosimpint
37 #include <X11/Xutil.h>
38 #include <X11/Xresource.h>
39 #include <X11/Xatom.h>
41 #pragma message enable nosimpint
44 #include "wx/motif/private.h"
48 struct wxPerDisplayData
53 m_topLevelWidget
= NULL
;
54 m_topLevelRealizedWidget
= NULL
;
57 wxXVisualInfo
* m_visualInfo
;
58 Widget m_topLevelWidget
, m_topLevelRealizedWidget
;
61 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
63 static WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
);
65 extern bool wxAddIdleCallback();
67 wxHashTable
*wxWidgetHashTable
= NULL
;
69 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
73 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
76 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
81 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
83 // just forward to the default handler for now
84 return gs_pfnXErrorHandler(dpy
, xevent
);
89 bool wxApp::Initialize(int& argc_
, wxChar
**argv_
)
92 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
95 if ( !wxAppBase::Initialize(argc_
, argv_
) )
98 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
101 // under HP-UX creating XmFontSet fails when the system locale is C and
102 // we're using a remote DISPLAY, presumably because HP-UX uses its own
103 // names for C and ISO locales (roman8 and iso8859n respectively) and so
104 // its Motif libraries have troubles with non-HP X server
106 // whatever the reason, the fact is that without this hack any wxMotif
107 // program crashes on startup because it can't create any font (HP programs
108 // still work but they do spit out messages about failing to create font
109 // sets and failing back on "fixed" font too)
111 // notice that calling setlocale() here is not enough because X(m) init
112 // functions call setlocale() later so we really have to change environment
113 bool fixAll
= false; // tweak LC_ALL (or just LC_CTYPE)?
114 const char *loc
= getenv("LC_CTYPE");
117 loc
= getenv("LC_ALL");
123 (loc
[0] == 'C' && loc
[1] == '\0') ||
124 strcmp(loc
, "POSIX") == 0 )
126 // we're using C locale, "fix" it
127 wxLogDebug(_T("HP-UX fontset hack: forcing locale to en_US.iso88591"));
128 putenv(fixAll
? "LC_ALL=en_US.iso88591" : "LC_CTYPE=en_US.iso88591");
132 XtSetLanguageProc(NULL
, NULL
, NULL
);
133 XtToolkitInitialize() ;
134 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
136 static char *fallbackResources
[] = {
137 // better defaults for CDE under Irix
139 // TODO: do something similar for the other systems, the hardcoded defaults
142 wxMOTIF_STR("*sgiMode: True"),
143 wxMOTIF_STR("*useSchemes: all"),
145 #if !wxMOTIF_USE_RENDER_TABLE
146 wxMOTIF_STR("*.fontList: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*"),
148 wxMOTIF_STR("*wxDefaultRendition.fontName: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*"),
149 wxMOTIF_STR("*wxDefaultRendition.fontType: FONT_IS_FONTSET"),
150 wxMOTIF_STR("*.renderTable: wxDefaultRendition"),
152 wxMOTIF_STR("*listBox.background: white"),
153 wxMOTIF_STR("*text.background: white"),
154 wxMOTIF_STR("*comboBox.Text.background: white"),
155 wxMOTIF_STR("*comboBox.List.background: white"),
156 #endif // __SGI__/!__SGI__
159 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
161 // we shouldn't pass empty application/class name as it results in
162 // immediate crash inside XOpenIM() (if XIM is used) under IRIX
163 wxString appname
= wxTheApp
->GetAppName();
164 if ( appname
.empty() )
165 appname
= _T("wxapp");
166 wxString clsname
= wxTheApp
->GetClassName();
167 if ( clsname
.empty() )
170 // FIXME-UTF8: This code is taken from wxGTK and duplicated here. This
171 // is just a temporary fix to make wxX11 compile in Unicode
172 // build, the real fix is to change Initialize()'s signature
173 // to use char* on Unix.
175 // XtOpenDisplay() wants char*, not wchar_t*, so convert
177 char **argvX11
= new char *[argc
+ 1];
178 for ( i
= 0; i
< argc
; i
++ )
180 argvX11
[i
] = strdup(wxConvLibc
.cWX2MB(argv_
[i
]));
183 argvX11
[argc
] = NULL
;
187 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,
191 NULL
, 0, // no options
192 # if XtSpecificationRelease < 5
193 (Cardinal
*) &argcX11
,
199 if ( argcX11
!= argc
)
201 // we have to drop the parameters which were consumed by X11+
202 for ( i
= 0; i
< argcX11
; i
++ )
204 while ( strcmp(wxConvLibc
.cWX2MB(argv_
[i
]), argvX11
[i
]) != 0 )
206 memmove(argv_
+ i
, argv_
+ i
+ 1, (argc
- i
)*sizeof(*argv_
));
212 // update internal arg[cv] as X11 may have removed processed options:
216 //else: XtOpenDisplay() didn't modify our parameters
219 for ( i
= 0; i
< argcX11
; i
++ )
228 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,
232 NULL
, 0, // no options
233 # if XtSpecificationRelease < 5
240 #endif // Unicode/ANSI
243 // if you don't log to stderr, nothing will be shown...
244 delete wxLog::SetActiveTarget(new wxLogStderr
);
245 wxString
className(wxTheApp
->GetClassName());
246 wxLogError(_("wxWidgets could not open display for '%s': exiting."),
250 m_initialDisplay
= (WXDisplay
*) dpy
;
252 // install the X error handler
253 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
255 // Add general resize proc
257 rec
.string
= wxMOTIF_STR("resize");
258 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
259 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
261 GetMainColormap(dpy
);
268 void wxApp::CleanUp()
270 wxAppBase::CleanUp();
272 delete wxWidgetHashTable
;
273 wxWidgetHashTable
= NULL
;
277 for( wxPerDisplayDataMap::iterator it
= m_perDisplayData
->begin(),
278 end
= m_perDisplayData
->end();
281 delete it
->second
->m_visualInfo
;
282 // On Solaris 10 calling XtDestroyWidget on the top level widget
283 // dumps core if the locale is set to something other than "C"
285 XtDestroyWidget( it
->second
->m_topLevelWidget
);
295 wxAppConsole::Exit();
298 // ============================================================================
300 // ============================================================================
304 m_mainLoop
= new wxEventLoop
;
305 m_mainColormap
= (WXColormap
) NULL
;
306 m_appContext
= (WXAppContext
) NULL
;
307 m_initialDisplay
= (WXDisplay
*) 0;
308 m_perDisplayData
= new wxPerDisplayDataMap
;
313 delete m_perDisplayData
;
316 int wxApp::MainLoop()
319 * Sit around forever waiting to process X-events. Property Change
320 * event are handled special, because they have to refer to
321 * the root window rather than to a widget. therefore we can't
322 * use an Xt-eventhandler.
325 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
326 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
334 // This should be redefined in a derived class for
335 // handling property change events for XAtom IPC.
336 void wxApp::HandlePropertyChange(WXEvent
*event
)
338 // by default do nothing special
339 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
342 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
344 if (!display
) /* Must be called first with non-NULL display */
345 return m_mainColormap
;
347 int defaultScreen
= DefaultScreen((Display
*) display
);
348 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
350 Colormap c
= DefaultColormapOfScreen(screen
);
353 m_mainColormap
= (WXColormap
) c
;
355 return (WXColormap
) c
;
358 static inline wxPerDisplayData
& GetOrCreatePerDisplayData
359 ( wxPerDisplayDataMap
& m
, WXDisplay
* display
)
361 wxPerDisplayDataMap::iterator it
= m
.find( display
);
362 if( it
!= m
.end() && it
->second
!= NULL
)
363 return *(it
->second
);
365 wxPerDisplayData
* nData
= new wxPerDisplayData();
371 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
373 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
375 if( data
.m_visualInfo
)
376 return data
.m_visualInfo
;
378 wxXVisualInfo
* vi
= new wxXVisualInfo
;
379 wxFillXVisualInfo( vi
, (Display
*)display
);
381 data
.m_visualInfo
= vi
;
386 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer
WXUNUSED(clientData
),
387 XtPointer
WXUNUSED(ptr
))
391 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
393 wxTheApp
->SetTopLevelRealizedWidget( (WXDisplay
*)XtDisplay(w
),
398 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
400 Widget tlw
= XtAppCreateShell( (String
)NULL
,
401 wxTheApp
->GetClassName().c_str(),
402 applicationShellWidgetClass
,
406 XmNoverrideRedirect
, True
,
409 XtAddCallback( tlw
, XmNdestroyCallback
,
410 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
413 return (WXWidget
)tlw
;
416 WXWidget
wxCreateTopLevelRealizedWidget( WXDisplay
* WXUNUSED(display
) )
418 Widget rTlw
= XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass
,
419 (Widget
)wxTheApp
->GetTopLevelWidget(),
421 XtSetMappedWhenManaged( rTlw
, False
);
422 XtRealizeWidget( rTlw
);
424 return (WXWidget
)rTlw
;
427 WXWidget
wxApp::GetTopLevelWidget()
429 WXDisplay
* display
= wxGetDisplay();
430 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
432 if( data
.m_topLevelWidget
)
433 return (WXWidget
)data
.m_topLevelWidget
;
435 WXWidget tlw
= wxCreateTopLevelWidget( display
);
436 SetTopLevelWidget( display
, tlw
);
441 WXWidget
wxApp::GetTopLevelRealizedWidget()
443 WXDisplay
* display
= wxGetDisplay();
444 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
446 if( it
!= m_perDisplayData
->end() && it
->second
->m_topLevelRealizedWidget
)
447 return (WXWidget
)it
->second
->m_topLevelRealizedWidget
;
449 WXWidget rTlw
= wxCreateTopLevelRealizedWidget( display
);
450 SetTopLevelRealizedWidget( display
, rTlw
);
455 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
457 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
458 .m_topLevelWidget
= (Widget
)widget
;
461 void wxApp::SetTopLevelRealizedWidget(WXDisplay
* display
, WXWidget widget
)
463 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
464 .m_topLevelRealizedWidget
= (Widget
)widget
;
468 // ----------------------------------------------------------------------------
469 // accessors for C modules
470 // ----------------------------------------------------------------------------
472 extern "C" XtAppContext
wxGetAppContext()
474 return (XtAppContext
)wxTheApp
->GetAppContext();