1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/app.cpp
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
21 #include "wx/memory.h"
25 #include "wx/evtloop.h"
28 #include "wx/thread.h"
32 #pragma message disable nosimpint
36 #include <X11/Xutil.h>
37 #include <X11/Xresource.h>
38 #include <X11/Xatom.h>
40 #pragma message enable nosimpint
43 #include "wx/motif/private.h"
47 struct wxPerDisplayData
52 m_topLevelWidget
= NULL
;
53 m_topLevelRealizedWidget
= NULL
;
56 wxXVisualInfo
* m_visualInfo
;
57 Widget m_topLevelWidget
, m_topLevelRealizedWidget
;
60 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer clientData
,
62 static WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
);
64 extern bool wxAddIdleCallback();
66 wxHashTable
*wxWidgetHashTable
= NULL
;
68 IMPLEMENT_DYNAMIC_CLASS(wxApp
, wxEvtHandler
)
72 typedef int (*XErrorHandlerFunc
)(Display
*, XErrorEvent
*);
75 XErrorHandlerFunc gs_pfnXErrorHandler
= 0;
80 static int wxXErrorHandler(Display
*dpy
, XErrorEvent
*xevent
)
82 // just forward to the default handler for now
83 return gs_pfnXErrorHandler(dpy
, xevent
);
88 bool wxApp::Initialize(int& argc_
, wxChar
**argv_
)
91 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
94 if ( !wxAppBase::Initialize(argc_
, argv_
) )
97 wxWidgetHashTable
= new wxHashTable(wxKEY_INTEGER
);
100 // under HP-UX creating XmFontSet fails when the system locale is C and
101 // we're using a remote DISPLAY, presumably because HP-UX uses its own
102 // names for C and ISO locales (roman8 and iso8859n respectively) and so
103 // its Motif libraries have troubles with non-HP X server
105 // whatever the reason, the fact is that without this hack any wxMotif
106 // program crashes on startup because it can't create any font (HP programs
107 // still work but they do spit out messages about failing to create font
108 // sets and failing back on "fixed" font too)
110 // notice that calling setlocale() here is not enough because X(m) init
111 // functions call setlocale() later so we really have to change environment
112 bool fixAll
= false; // tweak LC_ALL (or just LC_CTYPE)?
113 const char *loc
= getenv("LC_CTYPE");
116 loc
= getenv("LC_ALL");
122 (loc
[0] == 'C' && loc
[1] == '\0') ||
123 strcmp(loc
, "POSIX") == 0 )
125 // we're using C locale, "fix" it
126 wxLogDebug(wxT("HP-UX fontset hack: forcing locale to en_US.iso88591"));
127 putenv(fixAll
? "LC_ALL=en_US.iso88591" : "LC_CTYPE=en_US.iso88591");
131 XtSetLanguageProc(NULL
, NULL
, NULL
);
132 XtToolkitInitialize() ;
133 wxTheApp
->m_appContext
= (WXAppContext
) XtCreateApplicationContext();
135 static char *fallbackResources
[] = {
136 // better defaults for CDE under Irix
138 // TODO: do something similar for the other systems, the hardcoded defaults
141 wxMOTIF_STR("*sgiMode: True"),
142 wxMOTIF_STR("*useSchemes: all"),
144 #if !wxMOTIF_USE_RENDER_TABLE
145 wxMOTIF_STR("*.fontList: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*"),
147 wxMOTIF_STR("*wxDefaultRendition.fontName: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*"),
148 wxMOTIF_STR("*wxDefaultRendition.fontType: FONT_IS_FONTSET"),
149 wxMOTIF_STR("*.renderTable: wxDefaultRendition"),
151 wxMOTIF_STR("*listBox.background: white"),
152 wxMOTIF_STR("*text.background: white"),
153 wxMOTIF_STR("*comboBox.Text.background: white"),
154 wxMOTIF_STR("*comboBox.List.background: white"),
155 #endif // __SGI__/!__SGI__
158 XtAppSetFallbackResources((XtAppContext
) wxTheApp
->m_appContext
, fallbackResources
);
160 // we shouldn't pass empty application/class name as it results in
161 // immediate crash inside XOpenIM() (if XIM is used) under IRIX
162 wxString appname
= wxTheApp
->GetAppName();
163 if ( appname
.empty() )
164 appname
= wxT("wxapp");
165 wxString clsname
= wxTheApp
->GetClassName();
166 if ( clsname
.empty() )
169 // FIXME-UTF8: This code is taken from wxGTK and duplicated here. This
170 // is just a temporary fix to make wxX11 compile in Unicode
171 // build, the real fix is to change Initialize()'s signature
172 // to use char* on Unix.
174 // XtOpenDisplay() wants char*, not wchar_t*, so convert
176 char **argvX11
= new char *[argc
+ 1];
177 for ( i
= 0; i
< argc
; i
++ )
179 argvX11
[i
] = strdup(wxConvLibc
.cWX2MB(argv_
[i
]));
182 argvX11
[argc
] = NULL
;
186 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,
190 NULL
, 0, // no options
191 # if XtSpecificationRelease < 5
192 (Cardinal
*) &argcX11
,
198 if ( argcX11
!= argc
)
200 // we have to drop the parameters which were consumed by X11+
201 for ( i
= 0; i
< argcX11
; i
++ )
203 while ( strcmp(wxConvLibc
.cWX2MB(argv_
[i
]), argvX11
[i
]) != 0 )
205 memmove(argv_
+ i
, argv_
+ i
+ 1, (argc
- i
)*sizeof(*argv_
));
211 // update internal arg[cv] as X11 may have removed processed options:
215 //else: XtOpenDisplay() didn't modify our parameters
218 for ( i
= 0; i
< argcX11
; i
++ )
227 Display
*dpy
= XtOpenDisplay((XtAppContext
) wxTheApp
->m_appContext
,
231 NULL
, 0, // no options
232 # if XtSpecificationRelease < 5
239 #endif // Unicode/ANSI
242 // if you don't log to stderr, nothing will be shown...
243 delete wxLog::SetActiveTarget(new wxLogStderr
);
244 wxString
className(wxTheApp
->GetClassName());
245 wxLogError(_("wxWidgets could not open display for '%s': exiting."),
249 m_initialDisplay
= (WXDisplay
*) dpy
;
251 // install the X error handler
252 gs_pfnXErrorHandler
= XSetErrorHandler(wxXErrorHandler
);
254 // Add general resize proc
256 rec
.string
= wxMOTIF_STR("resize");
257 rec
.proc
= (XtActionProc
)wxWidgetResizeProc
;
258 XtAppAddActions((XtAppContext
) wxTheApp
->m_appContext
, &rec
, 1);
260 GetMainColormap(dpy
);
267 void wxApp::CleanUp()
269 wxAppBase::CleanUp();
271 wxDELETE(wxWidgetHashTable
);
275 for( wxPerDisplayDataMap::iterator it
= m_perDisplayData
->begin(),
276 end
= m_perDisplayData
->end();
279 delete it
->second
->m_visualInfo
;
280 // On Solaris 10 calling XtDestroyWidget on the top level widget
281 // dumps core if the locale is set to something other than "C"
283 XtDestroyWidget( it
->second
->m_topLevelWidget
);
293 wxAppConsole::Exit();
296 // ============================================================================
298 // ============================================================================
302 m_mainLoop
= new wxEventLoop
;
303 m_mainColormap
= (WXColormap
) NULL
;
304 m_appContext
= (WXAppContext
) NULL
;
305 m_initialDisplay
= (WXDisplay
*) 0;
306 m_perDisplayData
= new wxPerDisplayDataMap
;
311 delete m_perDisplayData
;
314 int wxApp::MainLoop()
317 * Sit around forever waiting to process X-events. Property Change
318 * event are handled special, because they have to refer to
319 * the root window rather than to a widget. therefore we can't
320 * use an Xt-eventhandler.
323 XSelectInput(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()),
324 XDefaultRootWindow(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget())),
332 // This should be redefined in a derived class for
333 // handling property change events for XAtom IPC.
334 void wxApp::HandlePropertyChange(WXEvent
*event
)
336 // by default do nothing special
337 XtDispatchEvent((XEvent
*) event
); /* let Motif do the work */
340 WXColormap
wxApp::GetMainColormap(WXDisplay
* display
)
342 if (!display
) /* Must be called first with non-NULL display */
343 return m_mainColormap
;
345 int defaultScreen
= DefaultScreen((Display
*) display
);
346 Screen
* screen
= XScreenOfDisplay((Display
*) display
, defaultScreen
);
348 Colormap c
= DefaultColormapOfScreen(screen
);
351 m_mainColormap
= (WXColormap
) c
;
353 return (WXColormap
) c
;
356 static inline wxPerDisplayData
& GetOrCreatePerDisplayData
357 ( wxPerDisplayDataMap
& m
, WXDisplay
* display
)
359 wxPerDisplayDataMap::iterator it
= m
.find( display
);
360 if( it
!= m
.end() && it
->second
!= NULL
)
361 return *(it
->second
);
363 wxPerDisplayData
* nData
= new wxPerDisplayData();
369 wxXVisualInfo
* wxApp::GetVisualInfo( WXDisplay
* display
)
371 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
373 if( data
.m_visualInfo
)
374 return data
.m_visualInfo
;
376 wxXVisualInfo
* vi
= new wxXVisualInfo
;
377 wxFillXVisualInfo( vi
, (Display
*)display
);
379 data
.m_visualInfo
= vi
;
384 static void wxTLWidgetDestroyCallback(Widget w
, XtPointer
WXUNUSED(clientData
),
385 XtPointer
WXUNUSED(ptr
))
389 wxTheApp
->SetTopLevelWidget( (WXDisplay
*)XtDisplay(w
),
391 wxTheApp
->SetTopLevelRealizedWidget( (WXDisplay
*)XtDisplay(w
),
396 WXWidget
wxCreateTopLevelWidget( WXDisplay
* display
)
398 Widget tlw
= XtAppCreateShell( (String
)NULL
,
399 wxTheApp
->GetClassName().c_str(),
400 applicationShellWidgetClass
,
404 XmNoverrideRedirect
, True
,
407 XtAddCallback( tlw
, XmNdestroyCallback
,
408 (XtCallbackProc
)wxTLWidgetDestroyCallback
,
411 return (WXWidget
)tlw
;
414 WXWidget
wxCreateTopLevelRealizedWidget( WXDisplay
* WXUNUSED(display
) )
416 Widget rTlw
= XtVaCreateWidget( "dummy_widget", topLevelShellWidgetClass
,
417 (Widget
)wxTheApp
->GetTopLevelWidget(),
419 XtSetMappedWhenManaged( rTlw
, False
);
420 XtRealizeWidget( rTlw
);
422 return (WXWidget
)rTlw
;
425 WXWidget
wxApp::GetTopLevelWidget()
427 WXDisplay
* display
= wxGetDisplay();
428 wxPerDisplayData
& data
= GetOrCreatePerDisplayData( *m_perDisplayData
,
430 if( data
.m_topLevelWidget
)
431 return (WXWidget
)data
.m_topLevelWidget
;
433 WXWidget tlw
= wxCreateTopLevelWidget( display
);
434 SetTopLevelWidget( display
, tlw
);
439 WXWidget
wxApp::GetTopLevelRealizedWidget()
441 WXDisplay
* display
= wxGetDisplay();
442 wxPerDisplayDataMap::iterator it
= m_perDisplayData
->find( display
);
444 if( it
!= m_perDisplayData
->end() && it
->second
->m_topLevelRealizedWidget
)
445 return (WXWidget
)it
->second
->m_topLevelRealizedWidget
;
447 WXWidget rTlw
= wxCreateTopLevelRealizedWidget( display
);
448 SetTopLevelRealizedWidget( display
, rTlw
);
453 void wxApp::SetTopLevelWidget(WXDisplay
* display
, WXWidget widget
)
455 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
456 .m_topLevelWidget
= (Widget
)widget
;
459 void wxApp::SetTopLevelRealizedWidget(WXDisplay
* display
, WXWidget widget
)
461 GetOrCreatePerDisplayData( *m_perDisplayData
, display
)
462 .m_topLevelRealizedWidget
= (Widget
)widget
;
466 // ----------------------------------------------------------------------------
467 // accessors for C modules
468 // ----------------------------------------------------------------------------
470 extern "C" XtAppContext
wxGetAppContext()
472 return (XtAppContext
)wxTheApp
->GetAppContext();