d8e4e48b981bc2926ff892fb9aea2965f3a930cc
[sxemacs] / lisp / gtk-init.el
1 ;;; gtk-init.el --- initialization code for gtk
2 ;; Copyright (C) 1990, 1993, 1994 Free Software Foundation, Inc.
3 ;; Copyright (C) 1995 Board of Trustees, University of Illinois.
4 ;; Copyright (C) 1995, 1996 Ben Wing.
5
6 ;; Author: various
7 ;; Rewritten for Gtk by: William Perry
8
9 ;; This file is part of SXEmacs.
10
11 ;; SXEmacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; SXEmacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
24 (globally-declare-boundp
25  '(gtk-initial-argv-list
26    gtk-initial-geometry))
27
28 (globally-declare-fboundp
29  '(gtk-keysym-on-keyboard-p))
30
31 (defvar gtk-win-initted nil)
32 (defvar gtk-pre-win-initted nil)
33 (defvar gtk-post-win-initted nil)
34
35 (defvar gtk-command-switch-alist
36   '(
37     ;; GNOME Options
38     ("--disable-sound" . nil)
39     ("--enable-sound"  . nil)
40     ("--espeaker"      . t)
41
42     ;; GTK Options
43     ("--gdk-debug"    . t)
44     ("--gdk-no-debug" . t)
45     ("--display"      . t)
46     ("--sync"         . nil)
47     ("--no-xshm"      . nil)
48     ("--name"         . t)
49     ("--class"        . t)
50     ("--gxid_host"    . t)
51     ("--gxid_port"    . t)
52     ("--xim-preedit"  . t)
53     ("--xim-status"   . t)
54     ("--gtk-debug"    . t)
55     ("--gtk-no-debug" . t)
56     ("--gtk-module"   . t)
57
58     ;; Glib options
59     ("--g-fatal-warnings" . nil)
60
61     ;; Session management options
62     ("--sm-client-id"     . t)
63     ("--sm-config-prefix" . t)
64     ("--sm-disable"       . t)
65     )
66
67   "An assoc list of command line arguments that should in gtk-initial-argv-list.
68 This is necessary because GTK and GNOME consider it a fatal error if they receive
69 unknown command line arguments (perfectly reasonable).  But this means that if
70 the user specifies a file name on the command line they will be unable to start.
71 So we filter the command line and allow only items in this list in.
72
73 The CDR of the assoc list is whether it accepts an argument.  All options are in
74 GNU long form though.")
75
76 (defun init-pre-gtk-win ()
77   "Initialize Gtk GUI at startup (pre).  Don't call this."
78   (when (not gtk-pre-win-initted)
79     (setq initial-frame-plist (if initial-frame-unmapped-p
80                                   '(initially-unmapped t)
81                                 nil)
82           gtk-pre-win-initted t)))
83
84 (defun gtk-init-handle-geometry (arg)
85   "Set up initial geometry info for GTK devices."
86   (setq gtk-initial-geometry (pop command-line-args-left)))
87
88 (defun gtk-filter-arguments ()
89   (let ((accepted nil)
90         (rejected nil)
91         (todo nil))
92     (setq todo (mapcar (lambda (argdesc)
93                          (if (cdr argdesc)
94                              ;; Need to look for --foo=bar
95                              (concat "^" (car argdesc) "=")
96                            ;; Just a simple arg
97                            (concat "^" (regexp-quote (car argdesc)) "$")))
98                        gtk-command-switch-alist))
99
100     (while command-line-args-left
101       (if (catch 'found
102             (mapc (lambda (r)
103                     (if (string-match r (car command-line-args-left))
104                         (throw 'found t))) todo)
105             (mapc (lambda (argdesc)
106                     (if (cdr argdesc)
107                         ;; This time we only care about argument items
108                         ;; that take an argument.  We'll check to see if
109                         ;; someone used --foo bar instead of --foo=bar
110                         (if (string-match (concat "^" (car argdesc) "$") (car command-line-args-left))
111                             ;; Yup!  Need to push
112                             (progn
113                               (push (pop command-line-args-left) accepted)
114                               (throw 'found t)))))
115                   gtk-command-switch-alist)
116             nil)
117           (push (pop command-line-args-left) accepted)
118         (push (pop command-line-args-left) rejected)))
119     (setq command-line-args-left (nreverse rejected))
120     (nreverse accepted)))
121
122 (defun init-gtk-win ()
123   "Initialize Gtk GUI at startup.  Don't call this."
124   (unless gtk-win-initted
125     (init-pre-gtk-win)
126     (setq gtk-initial-argv-list (cons (car command-line-args) (gtk-filter-arguments))
127           gtk-initial-geometry (nth 1 (member "-geometry" command-line-args-left)))
128     (make-gtk-device)
129     (init-post-gtk-win)
130     (setq gtk-win-initted t)))
131
132 (defun init-post-gtk-win ()
133   (unless gtk-post-win-initted
134     (if (and (not (featurep 'infodock)) (featurep 'toolbar))
135         (init-x-toolbar))
136     (if (and (featurep 'infodock) (featurep 'toolbar))
137         (require 'id-x-toolbar))
138
139     (when (featurep 'mule)
140       (define-specifier-tag 'mule-fonts
141         (lambda (device) (eq 'gtk (device-type device))))
142       (set-face-font
143        'default
144        '("-*-fixed-medium-r-*--16-*-iso8859-1"
145          "-*-fixed-medium-r-*--*-iso8859-1"
146          "-*-fixed-medium-r-*--*-iso8859-2"
147          "-*-fixed-medium-r-*--*-iso8859-3"
148          "-*-fixed-medium-r-*--*-iso8859-4"
149          "-*-fixed-medium-r-*--*-iso8859-7"
150          "-*-fixed-medium-r-*--*-iso8859-8"
151          "-*-fixed-medium-r-*--*-iso8859-5"
152          "-*-fixed-medium-r-*--*-iso8859-9"
153
154          ;; Following 3 fonts proposed by Teruhiko.Kurosaka@Japan.eng.sun
155          "-sun-gothic-medium-r-normal--14-120-75-75-c-60-jisx0201.1976-0"
156          "-sun-gothic-medium-r-normal--14-120-75-75-c-120-jisx0208.1983-0"
157          "-wadalab-gothic-medium-r-normal--14-120-75-75-c-120-jisx0212.1990-0"
158          ;; Other Japanese fonts
159          "-*-fixed-medium-r-*--*-jisx0201.1976-*"
160          "-*-fixed-medium-r-*--*-jisx0208.1983-*"
161          "-*-fixed-medium-r-*--*-jisx0212*-*"
162
163          ;; Chinese fonts
164          "-*-*-medium-r-*--*-gb2312.1980-*"
165        
166          ;; Use One font specification for CNS chinese
167          ;; Too many variations in font naming
168          "-*-fixed-medium-r-*--*-cns11643*-*"
169          ;; "-*-fixed-medium-r-*--*-cns11643*2"
170          ;; "-*-fixed-medium-r-*--*-cns11643*3"
171          ;; "-*-fixed-medium-r-*--*-cns11643*4"
172          ;; "-*-fixed-medium-r-*--*-cns11643.5-0"
173          ;; "-*-fixed-medium-r-*--*-cns11643.6-0"
174          ;; "-*-fixed-medium-r-*--*-cns11643.7-0"
175        
176          "-*-fixed-medium-r-*--*-big5*-*"
177          "-*-fixed-medium-r-*--*-sisheng_cwnn-0"
178
179          ;; Other fonts
180        
181          ;; "-*-fixed-medium-r-*--*-viscii1.1-1"
182        
183          ;; "-*-fixed-medium-r-*--*-mulearabic-0"
184          ;; "-*-fixed-medium-r-*--*-mulearabic-1"
185          ;; "-*-fixed-medium-r-*--*-mulearabic-2"
186
187          ;; "-*-fixed-medium-r-*--*-muleipa-1"
188          ;; "-*-fixed-medium-r-*--*-ethio-*"
189
190          "-*-mincho-medium-r-*--*-ksc5601.1987-*" ; Korean
191          "-*-fixed-medium-r-*--*-tis620.2529-1" ; Thai
192          )
193        'global '(mule-fonts) 'append))
194     
195     (add-hook 'zmacs-deactivate-region-hook
196               (lambda ()
197                 (if (console-on-window-system-p)
198                     (disown-selection))))
199     (add-hook 'zmacs-activate-region-hook
200               (lambda ()
201                 (if (console-on-window-system-p)
202                     (activate-region-as-selection))))
203     (add-hook 'zmacs-update-region-hook
204               (lambda ()
205                 (if (console-on-window-system-p)
206                     (activate-region-as-selection))))
207
208     (define-key global-map 'menu 'popup-mode-menu)
209     (setq gtk-post-win-initted t)))
210     
211 (push '("-geometry" . gtk-init-handle-geometry) command-switch-alist)
212
213 ;;; Stuff to get compose keys working on GTK
214 (eval-when-compile
215   (defmacro gtk-define-dead-key (key map)
216     `(when (gtk-keysym-on-keyboard-p ',key)
217        (define-key function-key-map [,key] ',map))))
218
219 (defun gtk-initialize-compose ()
220   "Enable compose processing"
221   (autoload 'compose-map            "gtk-compose" nil t 'keymap)
222   (autoload 'compose-acute-map      "gtk-compose" nil t 'keymap)
223   (autoload 'compose-grave-map      "gtk-compose" nil t 'keymap)
224   (autoload 'compose-cedilla-map    "gtk-compose" nil t 'keymap)
225   (autoload 'compose-diaeresis-map  "gtk-compose" nil t 'keymap)
226   (autoload 'compose-circumflex-map "gtk-compose" nil t 'keymap)
227   (autoload 'compose-tilde-map      "gtk-compose" nil t 'keymap)
228
229   (when (gtk-keysym-on-keyboard-p 'multi-key)
230     (define-key function-key-map [multi-key] 'compose-map))
231
232   ;; The dead keys might really be called just about anything, depending
233   ;; on the vendor.  MIT thinks that the prefixes are "SunFA_", "D", and
234   ;; "hpmute_" for Sun, DEC, and HP respectively.  However, OpenWindows 3
235   ;; thinks that the prefixes are "SunXK_FA_", "DXK_", and "hpXK_mute_".
236   ;; And HP (who don't mention Sun and DEC at all) use "XK_mute_".
237   ;; Go figure.
238
239   ;; Presumably if someone is running OpenWindows, they won't be using
240   ;; the DEC or HP keysyms, but if they are defined then that is possible,
241   ;; so in that case we accept them all.
242
243   ;; If things seem not to be working, you might want to check your
244   ;; /usr/lib/X11/XKeysymDB file to see if your vendor has an equally
245   ;; mixed up view of what these keys should be called.
246
247   ;; Canonical names:
248   (gtk-define-dead-key acute                    compose-acute-map)
249   (gtk-define-dead-key grave                    compose-grave-map)
250   (gtk-define-dead-key cedilla                  compose-cedilla-map)
251   (gtk-define-dead-key diaeresis                compose-diaeresis-map)
252   (gtk-define-dead-key circumflex               compose-circumflex-map)
253   (gtk-define-dead-key tilde                    compose-tilde-map)
254   (gtk-define-dead-key degree                   compose-ring-map)
255
256   ;; Sun according to MIT:
257   (gtk-define-dead-key SunFA_Acute              compose-acute-map)
258   (gtk-define-dead-key SunFA_Grave              compose-grave-map)
259   (gtk-define-dead-key SunFA_Cedilla            compose-cedilla-map)
260   (gtk-define-dead-key SunFA_Diaeresis          compose-diaeresis-map)
261   (gtk-define-dead-key SunFA_Circum             compose-circumflex-map)
262   (gtk-define-dead-key SunFA_Tilde              compose-tilde-map)
263
264   ;; Sun according to OpenWindows 2:
265   (gtk-define-dead-key Dead_Grave               compose-grave-map)
266   (gtk-define-dead-key Dead_Circum              compose-circumflex-map)
267   (gtk-define-dead-key Dead_Tilde               compose-tilde-map)
268
269   ;; Sun according to OpenWindows 3:
270   (gtk-define-dead-key SunXK_FA_Acute           compose-acute-map)
271   (gtk-define-dead-key SunXK_FA_Grave           compose-grave-map)
272   (gtk-define-dead-key SunXK_FA_Cedilla         compose-cedilla-map)
273   (gtk-define-dead-key SunXK_FA_Diaeresis       compose-diaeresis-map)
274   (gtk-define-dead-key SunXK_FA_Circum          compose-circumflex-map)
275   (gtk-define-dead-key SunXK_FA_Tilde           compose-tilde-map)
276
277   ;; DEC according to MIT:
278   (gtk-define-dead-key Dacute_accent            compose-acute-map)
279   (gtk-define-dead-key Dgrave_accent            compose-grave-map)
280   (gtk-define-dead-key Dcedilla_accent          compose-cedilla-map)
281   (gtk-define-dead-key Dcircumflex_accent       compose-circumflex-map)
282   (gtk-define-dead-key Dtilde                   compose-tilde-map)
283   (gtk-define-dead-key Dring_accent             compose-ring-map)
284
285   ;; DEC according to OpenWindows 3:
286   (gtk-define-dead-key DXK_acute_accent         compose-acute-map)
287   (gtk-define-dead-key DXK_grave_accent         compose-grave-map)
288   (gtk-define-dead-key DXK_cedilla_accent       compose-cedilla-map)
289   (gtk-define-dead-key DXK_circumflex_accent    compose-circumflex-map)
290   (gtk-define-dead-key DXK_tilde                compose-tilde-map)
291   (gtk-define-dead-key DXK_ring_accent          compose-ring-map)
292
293   ;; HP according to MIT:
294   (gtk-define-dead-key hpmute_acute             compose-acute-map)
295   (gtk-define-dead-key hpmute_grave             compose-grave-map)
296   (gtk-define-dead-key hpmute_diaeresis         compose-diaeresis-map)
297   (gtk-define-dead-key hpmute_asciicircum       compose-circumflex-map)
298   (gtk-define-dead-key hpmute_asciitilde        compose-tilde-map)
299
300   ;; Empirically discovered on Linux XFree86 MetroX:
301   (gtk-define-dead-key usldead_acute            compose-acute-map)
302   (gtk-define-dead-key usldead_grave            compose-grave-map)
303   (gtk-define-dead-key usldead_diaeresis        compose-diaeresis-map)
304   (gtk-define-dead-key usldead_asciicircum      compose-circumflex-map)
305   (gtk-define-dead-key usldead_asciitilde       compose-tilde-map)
306
307   ;; HP according to OpenWindows 3:
308   (gtk-define-dead-key hpXK_mute_acute          compose-acute-map)
309   (gtk-define-dead-key hpXK_mute_grave          compose-grave-map)
310   (gtk-define-dead-key hpXK_mute_diaeresis      compose-diaeresis-map)
311   (gtk-define-dead-key hpXK_mute_asciicircum    compose-circumflex-map)
312   (gtk-define-dead-key hpXK_mute_asciitilde     compose-tilde-map)
313
314   ;; HP according to HP-UX 8.0:
315   (gtk-define-dead-key XK_mute_acute            compose-acute-map)
316   (gtk-define-dead-key XK_mute_grave            compose-grave-map)
317   (gtk-define-dead-key XK_mute_diaeresis        compose-diaeresis-map)
318   (gtk-define-dead-key XK_mute_asciicircum      compose-circumflex-map)
319   (gtk-define-dead-key XK_mute_asciitilde       compose-tilde-map)
320
321   ;; Xfree86 seems to use lower case and a hyphen
322   (gtk-define-dead-key dead-acute               compose-acute-map)
323   (gtk-define-dead-key dead-grave               compose-grave-map)
324   (gtk-define-dead-key dead-cedilla             compose-cedilla-map)
325   (gtk-define-dead-key dead-diaeresis           compose-diaeresis-map)
326   (gtk-define-dead-key dead-circum              compose-circumflex-map)
327   (gtk-define-dead-key dead-circumflex          compose-circumflex-map)
328   (gtk-define-dead-key dead-tilde               compose-tilde-map)
329   )
330
331 (when (featurep 'gtk)
332   (add-hook
333    'create-console-hook
334    (lambda (console)
335      (letf (((selected-console) console))
336        (when (eq 'gtk (console-type console))
337          (gtk-initialize-compose))))))