1 ;;; scroll-bar.el --- window system-independent scroll bar support
3 ;; Copyright (C) 1993, 1994, 1995, 1999, 2000, 2001
4 ;; Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
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)
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.
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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Window-system-independent bindings of mouse clicks on the scroll bar.
29 ;; Presently emulates the scroll-bar behavior of xterm.
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."
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
))))
58 ;;;; Helpful functions for enabling and disabling scroll bars.
60 (defvar scroll-bar-mode
)
62 (defvar scroll-bar-mode-explicit nil
63 "Non-nil means `set-scroll-bar-mode' should really do something.
64 This is nil while loading `scroll-bar.el', and t afterward.")
66 (defun set-scroll-bar-mode-1 (ignore value
)
67 (set-scroll-bar-mode value
))
69 (defun set-scroll-bar-mode (value)
70 "Set `scroll-bar-mode' to VALUE and put the new value into effect."
71 (setq scroll-bar-mode value
)
73 (when scroll-bar-mode-explicit
74 ;; Apply it to default-frame-alist.
75 (let ((parameter (assq 'vertical-scroll-bars default-frame-alist
)))
77 (setcdr parameter scroll-bar-mode
)
78 (setq default-frame-alist
79 (cons (cons 'vertical-scroll-bars scroll-bar-mode
)
80 default-frame-alist
))))
82 ;; Apply it to existing frames.
83 (let ((frames (frame-list)))
85 (modify-frame-parameters
87 (list (cons 'vertical-scroll-bars scroll-bar-mode
)))
88 (setq frames
(cdr frames
))))))
90 (defcustom scroll-bar-mode
91 (if (eq system-type
'windows-nt
) 'right
'left
)
92 "*Specify whether to have vertical scroll bars, and on which side.
93 Possible values are nil (no scroll bars), `left' (scroll bars on left)
94 and `right' (scroll bars on right).
95 To set this variable in a Lisp program, use `set-scroll-bar-mode'
96 to make it take real effect.
97 Setting the variable with a customization buffer also takes effect."
98 :type
'(choice (const :tag
"none (nil)")
102 ;; The default value for :initialize would try to use :set
103 ;; when processing the file in cus-dep.el.
104 :initialize
'custom-initialize-default
105 :set
'set-scroll-bar-mode-1
)
107 ;; We just set scroll-bar-mode, but that was the default.
108 ;; If it is set again, that is for real.
109 (setq scroll-bar-mode-explicit t
)
111 (defun scroll-bar-mode (&optional flag
)
112 "Toggle display of vertical scroll bars on all frames.
113 This command applies to all frames that exist and frames to be
114 created in the future.
115 With a numeric argument, if the argument is negative,
116 turn off scroll bars; otherwise, turn on scroll bars."
118 (if flag
(setq flag
(prefix-numeric-value flag
)))
120 ;; Tweedle the variable according to the argument.
121 (set-scroll-bar-mode (if (null flag
) (not scroll-bar-mode
)
122 (and (or (not (numberp flag
)) (>= flag
0))
123 (if (eq system-type
'windows-nt
) 'right
'left
)))))
125 (defun toggle-scroll-bar (arg)
126 "Toggle whether or not the selected frame has vertical scroll bars.
127 With arg, turn vertical scroll bars on if and only if arg is positive.
128 The variable `scroll-bar-mode' controls which side the scroll bars are on
129 when they are turned on; if it is nil, they go on the left."
133 (if (cdr (assq 'vertical-scroll-bars
134 (frame-parameters (selected-frame))))
136 (setq arg
(prefix-numeric-value arg
)))
137 (modify-frame-parameters
139 (list (cons 'vertical-scroll-bars
142 (if (eq system-type
'windows-nt
) 'right
'left
)))))))
144 (defun toggle-horizontal-scroll-bar (arg)
145 "Toggle whether or not the selected frame has horizontal scroll bars.
146 With arg, turn horizontal scroll bars on if and only if arg is positive.
147 Horizontal scroll bars aren't implemented yet."
149 (error "Horizontal scroll bars aren't implemented yet"))
151 ;;;; Buffer navigation using the scroll bar.
153 ;;; This was used for up-events on button 2, but no longer.
154 (defun scroll-bar-set-window-start (event)
155 "Set the window start according to where the scroll bar is dragged.
156 EVENT should be a scroll bar click or drag event."
158 (let* ((end-position (event-end event
))
159 (window (nth 0 end-position
))
160 (portion-whole (nth 2 end-position
)))
162 (set-buffer (window-buffer window
))
164 (goto-char (+ (point-min)
165 (scroll-bar-scale portion-whole
166 (- (point-max) (point-min)))))
168 (set-window-start window
(point))))))
170 (defun scroll-bar-drag-position (portion-whole)
171 "Calculate new window start for drag event."
173 (goto-char (+ (point-min)
174 (scroll-bar-scale portion-whole
175 (- (point-max) (point-min)))))
179 (defun scroll-bar-maybe-set-window-start (event)
180 "Set the window start according to where the scroll bar is dragged.
181 Only change window start if the new start is substantially different.
182 EVENT should be a scroll bar click or drag event."
184 (let* ((end-position (event-end event
))
185 (window (nth 0 end-position
))
186 (portion-whole (nth 2 end-position
))
187 (next-portion-whole (cons (1+ (car portion-whole
))
188 (cdr portion-whole
)))
191 (current-start (window-start window
)))
193 (set-buffer (window-buffer window
))
194 (setq portion-start
(scroll-bar-drag-position portion-whole
))
195 (setq next-portion-start
(max
196 (scroll-bar-drag-position next-portion-whole
)
198 (if (or (>= current-start next-portion-start
)
199 (< current-start portion-start
))
200 (set-window-start window portion-start
)
201 ;; Always set window start, to ensure scroll bar position is updated.
202 (set-window-start window current-start
)))))
204 ;; Scroll the window to the proper position for EVENT.
205 (defun scroll-bar-drag-1 (event)
206 (let* ((start-position (event-start event
))
207 (window (nth 0 start-position
))
208 (portion-whole (nth 2 start-position
)))
210 (set-buffer (window-buffer window
))
211 ;; Calculate position relative to the accessible part of the buffer.
212 (goto-char (+ (point-min)
213 (scroll-bar-scale portion-whole
214 (- (point-max) (point-min)))))
216 (set-window-start window
(point)))))
218 (defun scroll-bar-drag (event)
219 "Scroll the window by dragging the scroll bar slider.
220 If you click outside the slider, the window scrolls to bring the slider there."
224 (end-position (event-end event
))
225 (window (nth 0 end-position
))
227 (with-current-buffer (window-buffer window
)
228 (setq before-scroll point-before-scroll
))
229 (save-selected-window
230 (select-window window
)
232 (or before-scroll
(point))))
233 (scroll-bar-drag-1 event
)
236 (setq event
(read-event))
237 (if (eq (car-safe event
) 'mouse-movement
)
238 (setq event
(read-event)))
239 (cond ((eq (car-safe event
) 'scroll-bar-movement
)
240 (scroll-bar-drag-1 event
))
242 ;; Exit when we get the drag event; ignore that event.
245 (with-current-buffer (window-buffer window
)
246 (setq point-before-scroll before-scroll
))))
248 (defun scroll-bar-scroll-down (event)
249 "Scroll the window's top line down to the location of the scroll bar click.
250 EVENT should be a scroll bar click."
252 (let* ((end-position (event-end event
))
253 (window (nth 0 end-position
))
255 (with-current-buffer (window-buffer window
)
256 (setq before-scroll point-before-scroll
))
258 (save-selected-window
259 (let ((portion-whole (nth 2 end-position
)))
260 (select-window window
)
262 (or before-scroll
(point)))
264 (scroll-bar-scale portion-whole
(1- (window-height)))))
266 (with-current-buffer (window-buffer window
)
267 (setq point-before-scroll before-scroll
)))))
269 (defun scroll-bar-scroll-up (event)
270 "Scroll the line next to the scroll bar click to the top of the window.
271 EVENT should be a scroll bar click."
273 (let* ((end-position (event-end event
))
274 (window (nth 0 end-position
))
276 (with-current-buffer (window-buffer window
)
277 (setq before-scroll point-before-scroll
))
279 (save-selected-window
280 (let ((portion-whole (nth 2 end-position
)))
281 (select-window window
)
283 (or before-scroll
(point)))
285 (scroll-bar-scale portion-whole
(1- (window-height)))))
287 (with-current-buffer (window-buffer window
)
288 (setq point-before-scroll before-scroll
)))))
291 ;;; Tookit scroll bars.
293 (defun scroll-bar-toolkit-scroll (event)
295 (let* ((end-position (event-end event
))
296 (window (nth 0 end-position
))
297 (part (nth 4 end-position
))
299 (cond ((eq part
'end-scroll
))
301 (with-current-buffer (window-buffer window
)
302 (setq before-scroll point-before-scroll
))
303 (save-selected-window
304 (select-window window
)
305 (setq before-scroll
(or before-scroll
(point)))
306 (cond ((eq part
'above-handle
)
308 ((eq part
'below-handle
)
311 (let* ((portion-whole (nth 2 end-position
))
312 (lines (scroll-bar-scale portion-whole
313 (1- (window-height)))))
314 (scroll-up (cond ((not (zerop lines
)) lines
)
315 ((< (car portion-whole
) 0) -
1)
322 (set-window-start window
(point-min)))
324 (goto-char (point-max))
327 (scroll-bar-drag-1 event
))))
329 (with-current-buffer (window-buffer window
)
330 (setq point-before-scroll before-scroll
))))))
336 ;;; For now, we'll set things up to work like xterm.
337 (cond ((and (boundp 'x-toolkit-scroll-bars
) x-toolkit-scroll-bars
)
338 (global-set-key [vertical-scroll-bar mouse-1
]
339 'scroll-bar-toolkit-scroll
))
341 (global-set-key [vertical-scroll-bar mouse-1
]
342 'scroll-bar-scroll-up
)
343 (global-set-key [vertical-scroll-bar drag-mouse-1
]
344 'scroll-bar-scroll-up
)
345 (global-set-key [vertical-scroll-bar down-mouse-2
]
347 (global-set-key [vertical-scroll-bar mouse-3
]
348 'scroll-bar-scroll-down
)
349 (global-set-key [vertical-scroll-bar drag-mouse-3
]
350 'scroll-bar-scroll-down
)))
353 (provide 'scroll-bar
)
355 ;;; scroll-bar.el ends here