merge the formfield patch from ooo-build
[ooovba.git] / vcl / unx / source / app / i18n_wrp.cxx
blob15e4c039fddd746db2e7f85a20a40edf104be394
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: i18n_wrp.cxx,v $
10 * $Revision: 1.15 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
34 struct XIMArg
36 char *name;
37 char *value;
40 #if defined(SOLARIS) && !defined(__GNUC__)
41 #include <varargs.h>
42 #else
43 #include <stdarg.h>
44 #endif
45 #include <sal/alloca.h>
47 #include <string.h>
48 #include <dlfcn.h>
49 #include <X11/Xlib.h>
50 #include <X11/Xlibint.h>
51 #include "XIM.h"
53 #define XIIIMP_LIB "xiiimp.so.2"
55 #ifdef SOLARIS
56 #define XIIIMP_PATH "/usr/openwin/lib/locale/common/" XIIIMP_LIB
57 #else /* Linux */
58 #define XIIIMP_PATH "/usr/lib/im/" XIIIMP_LIB
59 #endif
61 extern "C" {
62 typedef XIM (*OpenFunction)(Display*, XrmDatabase, char*, char*, XIMArg*);
65 /* global variables */
66 static void *g_dlmodule = 0;
67 static OpenFunction g_open_im = (OpenFunction)NULL;
69 /* utility function to transform vararg list into an array of XIMArg */
71 int
72 XvaCountArgs( XIMArg *pInArgs )
74 int nArgs = 0;
75 char *pName, *pValue;
77 while ( (pName = pInArgs->name) != NULL )
79 pValue = pInArgs->value;
81 if ( strcmp(pName, XNVaNestedList) == 0 )
83 nArgs += XvaCountArgs( (XIMArg*)pValue );
85 else
87 nArgs += 1;
89 pInArgs++;
92 return nArgs;
95 int
96 XvaCountArgs( va_list pInArgs )
98 int nArgs = 0;
99 char *pName, *pValue;
101 while ( (pName = va_arg(pInArgs, char*)) != NULL)
103 pValue = va_arg(pInArgs, char*);
105 if ( strcmp(pName, XNVaNestedList) == 0 )
107 nArgs += XvaCountArgs( (XIMArg*)pValue );
109 else
111 nArgs += 1;
115 return nArgs;
118 XIMArg*
119 XvaGetArgs( XIMArg *pInArgs, XIMArg *pOutArgs )
121 char *pName, *pValue;
123 while ( (pName = pInArgs->name) != NULL )
125 pValue = pInArgs->value;
127 if ( strcmp(pName, XNVaNestedList) == 0 )
129 pOutArgs = XvaGetArgs( (XIMArg*)pValue, pOutArgs );
131 else
133 pOutArgs->name = pName;
134 pOutArgs->value = pValue;
135 pOutArgs++;
137 pInArgs++;
140 return pOutArgs;
143 void
144 XvaGetArgs( va_list pInArgs, XIMArg *pOutArgs )
146 char *pName, *pValue;
148 while ((pName = va_arg(pInArgs, char*)) != NULL)
150 pValue = va_arg(pInArgs, char*);
152 if ( strcmp(pName, XNVaNestedList) == 0 )
154 pOutArgs = XvaGetArgs( (XIMArg*)pValue, pOutArgs );
156 else
158 pOutArgs->name = pName;
159 pOutArgs->value = pValue;
160 pOutArgs++;
164 pOutArgs->name = NULL;
165 pOutArgs->value = NULL;
169 /* Puplic functions */
171 #ifdef __cplusplus
172 extern "C"
173 #endif
174 XIM
175 XvaOpenIM(Display *display, XrmDatabase rdb,
176 char *res_name, char *res_class, ...)
178 XIM xim = (XIM)0;
179 va_list variable;
180 int total_count = 0;
183 * so count the stuff dangling here
186 #if defined(SOLARIS) && !defined(__GNUC__)
187 va_start(variable);
188 #else
189 va_start(variable, res_class);
190 #endif
191 total_count = XvaCountArgs(variable);
192 va_end(variable);
194 if (total_count > 0)
196 /* call a new open IM method */
198 XIMArg* args = (XIMArg*)alloca( (total_count + 1) * sizeof(XIMArg) );
201 * now package it up so we can set it along
203 #if defined(SOLARIS) && !defined(__GNUC__)
204 va_start(variable);
205 #else
206 va_start(variable, res_class);
207 #endif
208 XvaGetArgs( variable, args );
209 va_end(variable);
211 if (!g_dlmodule)
213 g_dlmodule = dlopen(XIIIMP_LIB, RTLD_LAZY);
214 if(!g_dlmodule)
216 g_dlmodule = dlopen(XIIIMP_PATH, RTLD_LAZY);
217 if (!g_dlmodule)
218 goto legacy_XIM;
220 g_open_im = (OpenFunction)(long)dlsym(g_dlmodule, "__XOpenIM");
221 if (!g_open_im)
222 goto legacy_XIM;
224 xim = (*g_open_im)(display, (XrmDatabase)rdb,
225 (char*)res_name, (char *)res_class, (XIMArg*)args);
227 else
229 goto legacy_XIM;
233 // in #if to prevent warning "warning: label 'legacy_XIM' defined but not used"
234 legacy_XIM:
236 if (!xim)
237 xim = XOpenIM(display, rdb, res_name, res_class);
239 return xim;
243 * Close the connection to the input manager, and free the XIM structure
246 Status XvaCloseIM(XIM)
248 Status s = False;
250 if (!g_dlmodule)
252 /* assuming one XvaOpenIM call */
253 dlclose(g_dlmodule);
254 g_dlmodule = (void*)0;
255 g_open_im = (OpenFunction)NULL;
256 s = True;
258 return (s);