Implement cygw32
[bpt/emacs.git] / lisp / term / w32-win.el
1 ;;; w32-win.el --- parse switches controlling interface with W32 window system -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 1993-1994, 2001-2012 Free Software Foundation, Inc.
4
5 ;; Author: Kevin Gallo
6 ;; Keywords: terminals
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; w32-win.el: this file is loaded from ../lisp/startup.el when it recognizes
26 ;; that W32 windows are to be used. Command line switches are parsed and those
27 ;; pertaining to W32 are processed and removed from the command line. The
28 ;; W32 display is opened and hooks are set for popping up the initial window.
29
30 ;; startup.el will then examine startup files, and eventually call the hooks
31 ;; which create the first window (s).
32
33 ;;; Code:
34 \f
35
36 ;; These are the standard X switches from the Xt Initialize.c file of
37 ;; Release 4.
38
39 ;; Command line Resource Manager string
40
41 ;; +rv *reverseVideo
42 ;; +synchronous *synchronous
43 ;; -background *background
44 ;; -bd *borderColor
45 ;; -bg *background
46 ;; -bordercolor *borderColor
47 ;; -borderwidth .borderWidth
48 ;; -bw .borderWidth
49 ;; -display .display
50 ;; -fg *foreground
51 ;; -fn *font
52 ;; -font *font
53 ;; -foreground *foreground
54 ;; -geometry .geometry
55 ;; -i .iconType
56 ;; -itype .iconType
57 ;; -iconic .iconic
58 ;; -name .name
59 ;; -reverse *reverseVideo
60 ;; -rv *reverseVideo
61 ;; -selectionTimeout .selectionTimeout
62 ;; -synchronous *synchronous
63 ;; -xrm
64
65 ;; An alist of X options and the function which handles them. See
66 ;; ../startup.el.
67
68 ;; (if (not (eq window-system 'w32))
69 ;; (error "%s: Loading w32-win.el but not compiled for w32" (invocation-name)))
70
71 (require 'frame)
72 (require 'mouse)
73 (require 'scroll-bar)
74 (require 'faces)
75 (require 'select)
76 (require 'menu-bar)
77 (require 'dnd)
78 (require 'w32-vars)
79
80 ;; Keep an obsolete alias for w32-focus-frame and w32-select-font in case
81 ;; they are used by code outside Emacs.
82 (define-obsolete-function-alias 'w32-focus-frame 'x-focus-frame "23.1")
83 (declare-function x-select-font "w32font.c"
84 (&optional frame exclude-proportional))
85 (define-obsolete-function-alias 'w32-select-font 'x-select-font "23.1")
86
87 (defvar w32-color-map) ;; defined in w32fns.c
88 (make-obsolete 'w32-default-color-map nil "24.1")
89
90 (declare-function w32-send-sys-command "w32fns.c")
91 (declare-function set-message-beep "w32console.c")
92
93 ;; Conditional on new-fontset so bootstrapping works on non-GUI compiles
94 (if (fboundp 'new-fontset)
95 (require 'fontset))
96
97 ;; The following definition is used for debugging scroll bar events.
98 ;(defun w32-handle-scroll-bar-event (event) (interactive "e") (princ event))
99
100 ;; (defun w32-drag-n-drop-debug (event)
101 ;; "Print the drag-n-drop EVENT in a readable form."
102 ;; (interactive "e")
103 ;; (princ event))
104
105 (defun w32-handle-dropped-file (window file-name)
106 (let ((f (if (eq system-type 'cygwin)
107 (cygwin-convert-path-from-windows file-name t)
108 (subst-char-in-string ?\\ ?/ file-name)))
109 (coding (or file-name-coding-system
110 default-file-name-coding-system)))
111
112 (setq file-name
113 (mapconcat 'url-hexify-string
114 (split-string (encode-coding-string f coding)
115 "/")
116 "/")))
117 (dnd-handle-one-url window 'private
118 (concat "file:" file-name)))
119
120 (defun w32-drag-n-drop (event &optional new-frame)
121 "Edit the files listed in the drag-n-drop EVENT.
122 Switch to a buffer editing the last file dropped."
123 (interactive "e")
124 (save-excursion
125 ;; Make sure the drop target has positive co-ords
126 ;; before setting the selected frame - otherwise it
127 ;; won't work. <skx@tardis.ed.ac.uk>
128 (let* ((window (posn-window (event-start event)))
129 (coords (posn-x-y (event-start event)))
130 (x (car coords))
131 (y (cdr coords)))
132 (if (and (> x 0) (> y 0))
133 (set-frame-selected-window nil window))
134
135 (when new-frame
136 (select-frame (make-frame)))
137 (raise-frame)
138 (setq window (selected-window))
139
140 (mapc (apply-partially #'w32-handle-dropped-file window)
141 (car (cdr (cdr event)))))))
142
143 (defun w32-drag-n-drop-other-frame (event)
144 "Edit the files listed in the drag-n-drop EVENT, in other frames.
145 May create new frames, or reuse existing ones. The frame editing
146 the last file dropped is selected."
147 (interactive "e")
148 (w32-drag-n-drop event t))
149
150 ;; Bind the drag-n-drop event.
151 (global-set-key [drag-n-drop] 'w32-drag-n-drop)
152 (global-set-key [C-drag-n-drop] 'w32-drag-n-drop-other-frame)
153
154 ;; Keyboard layout/language change events
155 ;; For now ignore language-change events; in the future
156 ;; we should switch the Emacs Input Method to match the
157 ;; new layout/language selected by the user.
158 (global-set-key [language-change] 'ignore)
159
160 (defvar x-resource-name)
161
162 \f
163 ;;;; Function keys
164
165 ;;; make f10 activate the real menubar rather than the mini-buffer menu
166 ;;; navigation feature.
167 (defun w32-menu-bar-open (&optional frame)
168 "Start key navigation of the menu bar in FRAME.
169
170 This initially activates the first menu-bar item, and you can then navigate
171 with the arrow keys, select a menu entry with the Return key or cancel with
172 the Escape key. If FRAME has no menu bar, this function does nothing.
173
174 If FRAME is nil or not given, use the selected frame.
175 If FRAME does not have the menu bar enabled, display a text menu using
176 `tmm-menubar'."
177 (interactive "i")
178 (if menu-bar-mode
179 (w32-send-sys-command ?\xf100 frame)
180 (with-selected-frame (or frame (selected-frame))
181 (tmm-menubar))))
182 \f
183
184 ;; W32 systems have different fonts than commonly found on X, so
185 ;; we define our own standard fontset here.
186 (defvar w32-standard-fontset-spec
187 "-*-Courier New-normal-r-*-*-13-*-*-*-c-*-fontset-standard"
188 "String of fontset spec of the standard fontset.
189 This defines a fontset consisting of the Courier New variations for
190 European languages which are distributed with Windows as
191 \"Multilanguage Support\".
192
193 See the documentation of `create-fontset-from-fontset-spec' for the format.")
194
195 (defun x-win-suspend-error ()
196 "Report an error when a suspend is attempted."
197 (error "Suspending an Emacs running under W32 makes no sense"))
198
199 (defvar dynamic-library-alist)
200 (defvar libpng-version) ; image.c #ifdef HAVE_NTGUI
201
202 ;;; Set default known names for external libraries
203 (setq dynamic-library-alist
204 (list
205 '(xpm "libxpm.dll" "xpm4.dll" "libXpm-nox4.dll")
206 ;; Versions of libpng 1.4.x and later are incompatible with
207 ;; earlier versions. Set up the list of libraries according to
208 ;; the version we were compiled against. (If we were compiled
209 ;; without PNG support, libpng-version's value is -1.)
210 (if (>= libpng-version 10400)
211 ;; libpng14-14.dll is libpng 1.4.3 from GTK+
212 '(png "libpng14-14.dll" "libpng14.dll")
213 '(png "libpng12d.dll" "libpng12.dll" "libpng3.dll" "libpng.dll"
214 ;; these are libpng 1.2.8 from GTK+
215 "libpng13d.dll" "libpng13.dll"))
216 '(jpeg "jpeg62.dll" "libjpeg.dll" "jpeg-62.dll" "jpeg.dll")
217 '(tiff "libtiff3.dll" "libtiff.dll")
218 '(gif "giflib4.dll" "libungif4.dll" "libungif.dll")
219 '(svg "librsvg-2-2.dll")
220 '(gdk-pixbuf "libgdk_pixbuf-2.0-0.dll")
221 '(glib "libglib-2.0-0.dll")
222 '(gobject "libgobject-2.0-0.dll")
223 '(gnutls "libgnutls-28.dll" "libgnutls-26.dll")
224 '(libxml2 "libxml2-2.dll" "libxml2.dll")))
225
226 ;;; multi-tty support
227 (defvar w32-initialized nil
228 "Non-nil if the w32 window system has been initialized.")
229
230 (declare-function x-open-connection "w32fns.c"
231 (display &optional xrm-string must-succeed))
232 (declare-function create-fontset-from-fontset-spec "fontset"
233 (fontset-spec &optional style-variant noerror))
234 (declare-function create-fontset-from-x-resource "fontset" ())
235 (declare-function x-get-resource "frame.c"
236 (attribute class &optional component subclass))
237 (declare-function x-handle-args "common-win" (args))
238 (declare-function x-parse-geometry "frame.c" (string))
239 (defvar x-command-line-resources)
240
241 (defun w32-initialize-window-system ()
242 "Initialize Emacs for W32 GUI frames."
243
244 ;; Do the actual Windows setup here; the above code just defines
245 ;; functions and variables that we use now.
246
247 (setq command-line-args (x-handle-args command-line-args))
248
249 ;; Make sure we have a valid resource name.
250 (or (stringp x-resource-name)
251 (setq x-resource-name
252 ;; Change any . or * characters in x-resource-name to hyphens,
253 ;; so as not to choke when we use it in X resource queries.
254 (replace-regexp-in-string "[.*]" "-" (invocation-name))))
255
256 (x-open-connection "" x-command-line-resources
257 ;; Exit with a fatal error if this fails and we
258 ;; are the initial display
259 (eq initial-window-system 'w32))
260
261 ;; Create the default fontset.
262 (create-default-fontset)
263 ;; Create the standard fontset.
264 (condition-case err
265 (create-fontset-from-fontset-spec w32-standard-fontset-spec t)
266 (error (display-warning
267 'initialization
268 (format "Creation of the standard fontset failed: %s" err)
269 :error)))
270 ;; Create fontset specified in X resources "Fontset-N" (N is 0, 1,...).
271 (create-fontset-from-x-resource)
272
273 ;; Apply a geometry resource to the initial frame. Put it at the end
274 ;; of the alist, so that anything specified on the command line takes
275 ;; precedence.
276 (let* ((res-geometry (x-get-resource "geometry" "Geometry"))
277 parsed)
278 (if res-geometry
279 (progn
280 (setq parsed (x-parse-geometry res-geometry))
281 ;; If the resource specifies a position,
282 ;; call the position and size "user-specified".
283 (if (or (assq 'top parsed) (assq 'left parsed))
284 (setq parsed (cons '(user-position . t)
285 (cons '(user-size . t) parsed))))
286 ;; All geometry parms apply to the initial frame.
287 (setq initial-frame-alist (append initial-frame-alist parsed))
288 ;; The size parms apply to all frames.
289 (if (and (assq 'height parsed)
290 (not (assq 'height default-frame-alist)))
291 (setq default-frame-alist
292 (cons (cons 'height (cdr (assq 'height parsed)))
293 default-frame-alist))
294 (if (and (assq 'width parsed)
295 (not (assq 'width default-frame-alist)))
296 (setq default-frame-alist
297 (cons (cons 'width (cdr (assq 'width parsed)))
298 default-frame-alist)))))))
299
300 ;; Check the reverseVideo resource.
301 (let ((case-fold-search t))
302 (let ((rv (x-get-resource "reverseVideo" "ReverseVideo")))
303 (if (and rv (string-match "^\\(true\\|yes\\|on\\)$" rv))
304 (setq default-frame-alist
305 (cons '(reverse . t) default-frame-alist)))))
306
307 ;; Don't let Emacs suspend under w32 gui
308 (add-hook 'suspend-hook 'x-win-suspend-error)
309
310 ;; Turn off window-splitting optimization; w32 is usually fast enough
311 ;; that this is only annoying.
312 (setq split-window-keep-point t)
313
314 ;; W32 expects the menu bar cut and paste commands to use the clipboard.
315 (menu-bar-enable-clipboard)
316
317 ;; Don't show the frame name; that's redundant.
318 (setq-default mode-line-frame-identification " ")
319
320 ;; Set to a system sound if you want a fancy bell.
321 (set-message-beep 'ok)
322 (x-apply-session-resources)
323 (setq w32-initialized t))
324
325 (add-to-list 'handle-args-function-alist '(w32 . x-handle-args))
326 (add-to-list 'frame-creation-function-alist '(w32 . x-create-frame-with-faces))
327 (add-to-list 'window-system-initialization-alist '(w32 . w32-initialize-window-system))
328
329 (provide 'w32-win)
330
331 ;;; w32-win.el ends here