Fix toolbars on X frames when Emacs is started on a tty. (Reported by Richard Lewis.)
[bpt/emacs.git] / lisp / scroll-bar.el
1 ;;; scroll-bar.el --- window system-independent scroll bar support
2
3 ;; Copyright (C) 1993, 1994, 1995, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: hardware
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs 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 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; Window-system-independent bindings of mouse clicks on the scroll bar.
29 ;; Presently emulates the scroll-bar behavior of xterm.
30
31 ;;; Code:
32
33 (require 'mouse)
34
35 \f
36 ;;;; Utilities.
37
38 (defun scroll-bar-event-ratio (event)
39 "Given a scroll bar event EVENT, return the scroll bar position as a ratio.
40 The value is a cons cell (PORTION . WHOLE) containing two integers
41 whose ratio gives the event's vertical position in the scroll bar, with 0
42 referring to the top and 1 to the bottom."
43 (nth 2 event))
44
45 (defun scroll-bar-scale (num-denom whole)
46 "Given a pair (NUM . DENOM) and WHOLE, return (/ (* NUM WHOLE) DENOM).
47 This is handy for scaling a position on a scroll bar into real units,
48 like buffer positions. If SCROLL-BAR-POS is the (PORTION . WHOLE) pair
49 from a scroll bar event, then (scroll-bar-scale SCROLL-BAR-POS
50 \(buffer-size)) is the position in the current buffer corresponding to
51 that scroll bar position."
52 ;; We multiply before we divide to maintain precision.
53 ;; We use floating point because the product of a large buffer size
54 ;; with a large scroll bar portion can easily overflow a lisp int.
55 (truncate (/ (* (float (car num-denom)) whole) (cdr num-denom))))
56
57 (defun scroll-bar-columns (side)
58 "Return the width, measured in columns, of the vertical scrollbar on SIDE.
59 SIDE must be the symbol `left' or `right'."
60 (let* ((wsb (window-scroll-bars))
61 (vtype (nth 2 wsb))
62 (cols (nth 1 wsb)))
63 (cond
64 ((not (memq side '(left right)))
65 (error "`left' or `right' expected instead of %S" side))
66 ((and (eq vtype side) cols))
67 ((eq (frame-parameter nil 'vertical-scroll-bars) side)
68 ;; nil means it's a non-toolkit scroll bar, and its width in
69 ;; columns is 14 pixels rounded up.
70 (ceiling (or (frame-parameter nil 'scroll-bar-width) 14)
71 (frame-char-width)))
72 (0))))
73
74 \f
75 ;;;; Helpful functions for enabling and disabling scroll bars.
76
77 (defvar scroll-bar-mode)
78
79 (defvar scroll-bar-mode-explicit nil
80 "Non-nil means `set-scroll-bar-mode' should really do something.
81 This is nil while loading `scroll-bar.el', and t afterward.")
82
83 (defun set-scroll-bar-mode-1 (ignore value)
84 (set-scroll-bar-mode value))
85
86 (defun set-scroll-bar-mode (value)
87 "Set `scroll-bar-mode' to VALUE and put the new value into effect."
88 (setq scroll-bar-mode value)
89
90 (when scroll-bar-mode-explicit
91 (modify-all-frames-parameters (list (cons 'vertical-scroll-bars
92 scroll-bar-mode)))))
93
94 (defcustom scroll-bar-mode default-frame-scroll-bars
95 "*Specify whether to have vertical scroll bars, and on which side.
96 Possible values are nil (no scroll bars), `left' (scroll bars on left)
97 and `right' (scroll bars on right).
98 To set this variable in a Lisp program, use `set-scroll-bar-mode'
99 to make it take real effect.
100 Setting the variable with a customization buffer also takes effect."
101 :type '(choice (const :tag "none (nil)" nil)
102 (const left)
103 (const right))
104 :group 'frames
105 ;; The default value for :initialize would try to use :set
106 ;; when processing the file in cus-dep.el.
107 :initialize 'custom-initialize-default
108 :set 'set-scroll-bar-mode-1)
109
110 ;; We just set scroll-bar-mode, but that was the default.
111 ;; If it is set again, that is for real.
112 (setq scroll-bar-mode-explicit t)
113
114 (defun scroll-bar-mode (&optional flag)
115 "Toggle display of vertical scroll bars on all frames.
116 This command applies to all frames that exist and frames to be
117 created in the future.
118 With a numeric argument, if the argument is negative,
119 turn off scroll bars; otherwise, turn on scroll bars."
120 (interactive "P")
121
122 ;; Tweedle the variable according to the argument.
123 (set-scroll-bar-mode (if (if (null flag)
124 (not scroll-bar-mode)
125 (setq flag (prefix-numeric-value flag))
126 (or (not (numberp flag)) (>= flag 0)))
127 default-frame-scroll-bars)))
128
129 (defun toggle-scroll-bar (arg)
130 "Toggle whether or not the selected frame has vertical scroll bars.
131 With arg, turn vertical scroll bars on if and only if arg is positive.
132 The variable `scroll-bar-mode' controls which side the scroll bars are on
133 when they are turned on; if it is nil, they go on the left."
134 (interactive "P")
135 (if (null arg)
136 (setq arg
137 (if (cdr (assq 'vertical-scroll-bars
138 (frame-parameters (selected-frame))))
139 -1 1))
140 (setq arg (prefix-numeric-value arg)))
141 (modify-frame-parameters
142 (selected-frame)
143 (list (cons 'vertical-scroll-bars
144 (if (> arg 0)
145 (or scroll-bar-mode default-frame-scroll-bars))))))
146
147 (defun toggle-horizontal-scroll-bar (arg)
148 "Toggle whether or not the selected frame has horizontal scroll bars.
149 With arg, turn horizontal scroll bars on if and only if arg is positive.
150 Horizontal scroll bars aren't implemented yet."
151 (interactive "P")
152 (error "Horizontal scroll bars aren't implemented yet"))
153 \f
154 ;;;; Buffer navigation using the scroll bar.
155
156 ;;; This was used for up-events on button 2, but no longer.
157 (defun scroll-bar-set-window-start (event)
158 "Set the window start according to where the scroll bar is dragged.
159 EVENT should be a scroll bar click or drag event."
160 (interactive "e")
161 (let* ((end-position (event-end event))
162 (window (nth 0 end-position))
163 (portion-whole (nth 2 end-position)))
164 (save-excursion
165 (set-buffer (window-buffer window))
166 (save-excursion
167 (goto-char (+ (point-min)
168 (scroll-bar-scale portion-whole
169 (- (point-max) (point-min)))))
170 (beginning-of-line)
171 (set-window-start window (point))))))
172
173 (defun scroll-bar-drag-position (portion-whole)
174 "Calculate new window start for drag event."
175 (save-excursion
176 (goto-char (+ (point-min)
177 (scroll-bar-scale portion-whole
178 (- (point-max) (point-min)))))
179 (beginning-of-line)
180 (point)))
181
182 (defun scroll-bar-maybe-set-window-start (event)
183 "Set the window start according to where the scroll bar is dragged.
184 Only change window start if the new start is substantially different.
185 EVENT should be a scroll bar click or drag event."
186 (interactive "e")
187 (let* ((end-position (event-end event))
188 (window (nth 0 end-position))
189 (portion-whole (nth 2 end-position))
190 (next-portion-whole (cons (1+ (car portion-whole))
191 (cdr portion-whole)))
192 portion-start
193 next-portion-start
194 (current-start (window-start window)))
195 (save-excursion
196 (set-buffer (window-buffer window))
197 (setq portion-start (scroll-bar-drag-position portion-whole))
198 (setq next-portion-start (max
199 (scroll-bar-drag-position next-portion-whole)
200 (1+ portion-start)))
201 (if (or (>= current-start next-portion-start)
202 (< current-start portion-start))
203 (set-window-start window portion-start)
204 ;; Always set window start, to ensure scroll bar position is updated.
205 (set-window-start window current-start)))))
206
207 ;; Scroll the window to the proper position for EVENT.
208 (defun scroll-bar-drag-1 (event)
209 (let* ((start-position (event-start event))
210 (window (nth 0 start-position))
211 (portion-whole (nth 2 start-position)))
212 (save-excursion
213 (set-buffer (window-buffer window))
214 ;; Calculate position relative to the accessible part of the buffer.
215 (goto-char (+ (point-min)
216 (scroll-bar-scale portion-whole
217 (- (point-max) (point-min)))))
218 (vertical-motion 0 window)
219 (set-window-start window (point)))))
220
221 (defun scroll-bar-drag (event)
222 "Scroll the window by dragging the scroll bar slider.
223 If you click outside the slider, the window scrolls to bring the slider there."
224 (interactive "e")
225 (let* (done
226 (echo-keystrokes 0)
227 (end-position (event-end event))
228 (window (nth 0 end-position))
229 (before-scroll))
230 (with-current-buffer (window-buffer window)
231 (setq before-scroll point-before-scroll))
232 (save-selected-window
233 (select-window window)
234 (setq before-scroll
235 (or before-scroll (point))))
236 (scroll-bar-drag-1 event)
237 (track-mouse
238 (while (not done)
239 (setq event (read-event))
240 (if (eq (car-safe event) 'mouse-movement)
241 (setq event (read-event)))
242 (cond ((eq (car-safe event) 'scroll-bar-movement)
243 (scroll-bar-drag-1 event))
244 (t
245 ;; Exit when we get the drag event; ignore that event.
246 (setq done t)))))
247 (sit-for 0)
248 (with-current-buffer (window-buffer window)
249 (setq point-before-scroll before-scroll))))
250
251 (defun scroll-bar-scroll-down (event)
252 "Scroll the window's top line down to the location of the scroll bar click.
253 EVENT should be a scroll bar click."
254 (interactive "e")
255 (let* ((end-position (event-end event))
256 (window (nth 0 end-position))
257 (before-scroll))
258 (with-current-buffer (window-buffer window)
259 (setq before-scroll point-before-scroll))
260 (unwind-protect
261 (save-selected-window
262 (let ((portion-whole (nth 2 end-position)))
263 (select-window window)
264 (setq before-scroll
265 (or before-scroll (point)))
266 (scroll-down
267 (scroll-bar-scale portion-whole (1- (window-height)))))
268 (sit-for 0))
269 (with-current-buffer (window-buffer window)
270 (setq point-before-scroll before-scroll)))))
271
272 (defun scroll-bar-scroll-up (event)
273 "Scroll the line next to the scroll bar click to the top of the window.
274 EVENT should be a scroll bar click."
275 (interactive "e")
276 (let* ((end-position (event-end event))
277 (window (nth 0 end-position))
278 (before-scroll))
279 (with-current-buffer (window-buffer window)
280 (setq before-scroll point-before-scroll))
281 (unwind-protect
282 (save-selected-window
283 (let ((portion-whole (nth 2 end-position)))
284 (select-window window)
285 (setq before-scroll
286 (or before-scroll (point)))
287 (scroll-up
288 (scroll-bar-scale portion-whole (1- (window-height)))))
289 (sit-for 0))
290 (with-current-buffer (window-buffer window)
291 (setq point-before-scroll before-scroll)))))
292
293 \f
294 ;;; Tookit scroll bars.
295
296 (defun scroll-bar-toolkit-scroll (event)
297 (interactive "e")
298 (let* ((end-position (event-end event))
299 (window (nth 0 end-position))
300 (part (nth 4 end-position))
301 before-scroll)
302 (cond ((eq part 'end-scroll))
303 (t
304 (with-current-buffer (window-buffer window)
305 (setq before-scroll point-before-scroll))
306 (save-selected-window
307 (select-window window)
308 (setq before-scroll (or before-scroll (point)))
309 (cond ((eq part 'above-handle)
310 (scroll-up '-))
311 ((eq part 'below-handle)
312 (scroll-up nil))
313 ((eq part 'ratio)
314 (let* ((portion-whole (nth 2 end-position))
315 (lines (scroll-bar-scale portion-whole
316 (1- (window-height)))))
317 (scroll-up (cond ((not (zerop lines)) lines)
318 ((< (car portion-whole) 0) -1)
319 (t 1)))))
320 ((eq part 'up)
321 (scroll-up -1))
322 ((eq part 'down)
323 (scroll-up 1))
324 ((eq part 'top)
325 (set-window-start window (point-min)))
326 ((eq part 'bottom)
327 (goto-char (point-max))
328 (recenter))
329 ((eq part 'handle)
330 (scroll-bar-drag-1 event))))
331 (sit-for 0)
332 (with-current-buffer (window-buffer window)
333 (setq point-before-scroll before-scroll))))))
334
335
336 \f
337 ;;;; Bindings.
338
339 ;;; For now, we'll set things up to work like xterm.
340 (cond ((and (boundp 'x-toolkit-scroll-bars) x-toolkit-scroll-bars)
341 (global-set-key [vertical-scroll-bar mouse-1]
342 'scroll-bar-toolkit-scroll))
343 (t
344 (global-set-key [vertical-scroll-bar mouse-1]
345 'scroll-bar-scroll-up)
346 (global-set-key [vertical-scroll-bar drag-mouse-1]
347 'scroll-bar-scroll-up)
348 (global-set-key [vertical-scroll-bar down-mouse-2]
349 'scroll-bar-drag)
350 (global-set-key [vertical-scroll-bar mouse-3]
351 'scroll-bar-scroll-down)
352 (global-set-key [vertical-scroll-bar drag-mouse-3]
353 'scroll-bar-scroll-down)))
354
355 \f
356 (provide 'scroll-bar)
357
358 ;;; arch-tag: 6f1d01d0-0b1e-4bf8-86db-d491e0f399f3
359 ;;; scroll-bar.el ends here