Initial git import
[sxemacs] / src / mule / input-method-motif.c
1 /* Various functions for X11R5+ input methods, using the Motif XmIm* functions.
2    input-method-xlib.c provides a lower-level implementation.
3    Copyright (C) 1996 Sun Microsystems.
4
5 This file is part of SXEmacs
6
7 SXEmacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 SXEmacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
19
20
21 /* Synched up with: Not in FSF. */
22
23 /* Written by Martin Buchholz. */
24
25 #include <config.h>
26 #include <X11/Xlocale.h>        /* More portable than <locale.h> ? */
27 #include "lisp.h"
28 #include "ui/X11/console-x.h"
29 #include "ui/device.h"
30 #include "ui/frame.h"
31 #include "EmacsFrame.h"
32 #include <Xm/Xm.h>
33
34 #ifndef XIM_MOTIF
35 #error  XIM_MOTIF is not defined??
36 #endif
37
38 void Initialize_Locale(void)
39 {
40         char *locale;
41
42         /* dverna - Nov. 98: #### DON'T DO THIS !!! The default XtLanguageProc
43            routine calls setlocale(LC_ALL, lang) which fucks up our lower-level
44            locale management, and especially the value of LC_NUMERIC. Anyway, since
45            at this point, we don't know yet whether we're gonna need an X11 frame,
46            we should really do it manually and not use Xlib's dumb default routine */
47         /*XtSetLanguageProc (NULL, (XtLanguageProc) NULL, NULL); */
48         if ((locale = setlocale(LC_ALL, "")) == NULL) {
49                 stderr_out("Can't set locale.\n");
50                 stderr_out("Using C locale instead.\n");
51                 putenv("LANG=C");
52                 putenv("LC_ALL=C");
53                 if ((locale = setlocale(LC_ALL, "C")) == NULL) {
54                         stderr_out("Can't even set locale to `C'!\n");
55                         return;
56                 }
57         }
58
59         if (!XSupportsLocale()) {
60                 stderr_out("X Windows does not support locale `%s'\n", locale);
61                 stderr_out("Using C Locale instead\n");
62                 putenv("LANG=C");
63                 putenv("LC_ALL=C");
64                 if ((locale = setlocale(LC_ALL, "C")) == NULL) {
65                         stderr_out("Can't even set locale to `C'!\n");
66                         return;
67                 }
68                 if (!XSupportsLocale()) {
69                         stderr_out
70                             ("X Windows does not even support locale `C'!\n");
71                         return;
72                 }
73         }
74
75         setlocale(LC_NUMERIC, "C");
76
77         if (XSetLocaleModifiers("") == NULL) {
78                 stderr_out("XSetLocaleModifiers(\"\") failed\n");
79                 stderr_out
80                     ("Check the value of the XMODIFIERS environment variable.\n");
81         }
82 }
83
84 /* Create X input method for device */
85 void XIM_init_device(struct device *d)
86 {
87         /* Nothing to do */
88 }
89
90 /* Callback for the deleting frame. */
91 static void
92 XIM_delete_frame(Widget w, XtPointer client_data, XtPointer call_data)
93 {
94         XmImUnregister((Widget) client_data);
95 }
96
97 void XIM_init_frame(struct frame *f)
98 {
99         Widget w = FRAME_X_TEXT_WIDGET(f);
100         XPoint spot = { 0, 0 };
101         XmFontList fontlist;
102         XmFontListEntry fontlistEntry;
103
104         typedef struct {
105                 XFontSet fontset;
106                 Pixel fg;
107                 Pixel bg;
108         } xim_resources_t;
109
110         xim_resources_t xim_resources;
111
112         /* mrb: #### Fix so that background and foreground is set from
113            default face, rather than foreground and background resources, or
114            that the user can use set-frame-parameters to set xic attributes */
115
116 #define res(name, class, representation, field, default_value)  \
117   { name, class, representation, sizeof(xim_resources.field),   \
118     XtOffsetOf(xim_resources_t, field),                         \
119     XtRString, (XtPointer) (default_value) }
120
121         static XtResource resources[] = {
122                 /*  name              class          represent'n field    default value */
123                 res(XtNfontSet, XtCFontSet, XtRFontSet, fontset,
124                     XtDefaultFontSet),
125                 res(XtNximForeground, XtCForeground, XtRPixel, fg,
126                     XtDefaultForeground),
127                 res(XtNximBackground, XtCBackground, XtRPixel, bg,
128                     XtDefaultBackground)
129         };
130
131         XtGetApplicationResources(w, &xim_resources,
132                                   resources, XtNumber(resources), NULL, 0);
133
134         if (!xim_resources.fontset) {
135                 stderr_out("Can't get fontset resource for Input Method\n");
136                 return;
137         }
138
139         fontlistEntry = XmFontListEntryCreate(XmFONTLIST_DEFAULT_TAG,
140                                               XmFONT_IS_FONTSET,
141                                               (XtPointer) xim_resources.
142                                               fontset);
143         fontlist = XmFontListAppendEntry(NULL, fontlistEntry);
144         XmImRegister(w, 0);
145         XmImVaSetValues(w,
146                         XmNfontList, fontlist,
147                         XmNforeground, xim_resources.fg,
148                         XmNbackground, xim_resources.bg, XmNspotLocation, &spot,
149                         /*   XmNlineSpace, 0, */
150                         NULL);
151
152         XmFontListEntryFree(&fontlistEntry);
153
154         XtAddCallback(w, XmNdestroyCallback, XIM_delete_frame, (XtPointer) w);
155 }
156
157 void XIM_SetGeometry(struct frame *f)
158 {
159 }
160
161 void XIM_SetSpotLocation(struct frame *f, int x, int y)
162 {
163         /* #### FIX: Must make sure spot fits within Preedit Area */
164         XPoint *spot = &(FRAME_X_XIC_SPOT(f));
165         if (spot->x == (short)x && spot->y == (short)y)
166                 return;
167
168         spot->x = (short)x;
169         spot->y = (short)y;
170
171         XmImVaSetValues(FRAME_X_TEXT_WIDGET(f), XmNspotLocation, spot, NULL);
172 }
173
174 void XIM_focus_event(struct frame *f, int in_p)
175 {
176         if (in_p)
177                 XmImVaSetFocusValues(FRAME_X_TEXT_WIDGET(f), NULL);
178         else
179                 XmImUnsetFocus(FRAME_X_TEXT_WIDGET(f));
180 }
181
182 void vars_of_input_method_motif(void)
183 {
184         Fprovide(intern("xim"));
185 }