(scroll-bar-drag-1):
[bpt/emacs.git] / lisp / scroll-bar.el
1 ;;; scroll-bar.el --- window system-independent scroll bar support.
2
3 ;;; Copyright (C) 1993 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: hardware
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 2, or (at your option)
13 ;;; 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; see the file COPYING. If not, write to
22 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Code:
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 ;;; Code:
31
32 (require 'mouse)
33
34 \f
35 ;;;; Utilities.
36
37 (defun scroll-bar-scale (num-denom whole)
38 "Given a pair (NUM . DENOM) and WHOLE, return (/ (* NUM WHOLE) DENOM).
39 This is handy for scaling a position on a scroll bar into real units,
40 like buffer positions. If SCROLL-BAR-POS is the (PORTION . WHOLE) pair
41 from a scroll bar event, then (scroll-bar-scale SCROLL-BAR-POS
42 \(buffer-size)) is the position in the current buffer corresponding to
43 that scroll bar position."
44 ;; We multiply before we divide to maintain precision.
45 ;; We use floating point because the product of a large buffer size
46 ;; with a large scroll bar portion can easily overflow a lisp int.
47 (truncate (/ (* (float (car num-denom)) whole) (cdr num-denom))))
48
49 \f
50 ;;;; Helpful functions for enabling and disabling scroll bars.
51 ;;; This is not documented because you can't change the
52 ;;; mode properly by setting it.
53 (defvar scroll-bar-mode t)
54
55 (defun scroll-bar-mode (flag)
56 "Toggle display of vertical scroll bars on each frame.
57 This command applies to all frames that exist and frames to be
58 created in the future.
59 With a numeric argument, if the argument is negative,
60 turn off scroll bars; otherwise, turn on scroll bars."
61 (interactive "P")
62 (setq scroll-bar-mode (if (null flag) (not scroll-bar-mode)
63 (or (not (numberp flag)) (>= flag 0))))
64 (mapcar
65 (function
66 (lambda (param-name)
67 (let ((parameter (assq param-name default-frame-alist)))
68 (if (consp parameter)
69 (setcdr parameter scroll-bar-mode)
70 (setq default-frame-alist
71 (cons (cons param-name scroll-bar-mode)
72 default-frame-alist))))))
73 '(vertical-scroll-bars horizontal-scroll-bars))
74 (let ((frames (frame-list)))
75 (while frames
76 (modify-frame-parameters
77 (car frames)
78 (list (cons 'vertical-scroll-bars scroll-bar-mode)
79 (cons 'horizontal-scroll-bars scroll-bar-mode)))
80 (setq frames (cdr frames)))))
81 \f
82 ;;;; Buffer navigation using the scroll bar.
83
84 ;;; This was used for up-events on button 2, but no longer.
85 (defun scroll-bar-set-window-start (event)
86 "Set the window start according to where the scroll bar is dragged.
87 EVENT should be a scroll bar click or drag event."
88 (interactive "e")
89 (let* ((end-position (event-end event))
90 (window (nth 0 end-position))
91 (portion-whole (nth 2 end-position)))
92 (save-excursion
93 (set-buffer (window-buffer window))
94 (save-excursion
95 (goto-char (scroll-bar-scale portion-whole (buffer-size)))
96 (beginning-of-line)
97 (set-window-start window (point))))))
98
99 ;; Scroll the window to the proper position for EVENT.
100 (defun scroll-bar-drag-1 (event)
101 (let* ((start-position (event-start event))
102 (window (nth 0 start-position))
103 (portion-whole (nth 2 start-position)))
104 (save-excursion
105 (set-buffer (window-buffer window))
106 ;; Calculate position relative to the accessible part of the buffer.
107 (goto-char (+ (point-min)
108 (scroll-bar-scale portion-whole
109 (- (point-max) (point-min)))))
110 (beginning-of-line)
111 (set-window-start window (point)))))
112
113 (defun scroll-bar-drag (event)
114 "Scroll the window by dragging the scroll bar slider.
115 If you click outside the slider, the window scrolls to bring the slider there."
116 (interactive "e")
117 (let* (done)
118 (scroll-bar-drag-1 event)
119 (track-mouse
120 (while (not done)
121 (setq event (read-event))
122 (if (eq (car-safe event) 'mouse-movement)
123 (setq event (read-event)))
124 (cond ((eq (car-safe event) 'scroll-bar-movement)
125 (scroll-bar-drag-1 event))
126 (t
127 ;; Exit when we get the drag event; ignore that event.
128 (setq done t)))))))
129
130 (defun scroll-bar-scroll-down (event)
131 "Scroll the window's top line down to the location of the scroll bar click.
132 EVENT should be a scroll bar click."
133 (interactive "e")
134 (let ((old-selected-window (selected-window)))
135 (unwind-protect
136 (progn
137 (let* ((end-position (event-end event))
138 (window (nth 0 end-position))
139 (portion-whole (nth 2 end-position)))
140 (select-window window)
141 (scroll-down
142 (scroll-bar-scale portion-whole (1- (window-height))))))
143 (select-window old-selected-window))))
144
145 (defun scroll-bar-scroll-up (event)
146 "Scroll the line next to the scroll bar click to the top of the window.
147 EVENT should be a scroll bar click."
148 (interactive "e")
149 (let ((old-selected-window (selected-window)))
150 (unwind-protect
151 (progn
152 (let* ((end-position (event-end event))
153 (window (nth 0 end-position))
154 (portion-whole (nth 2 end-position)))
155 (select-window window)
156 (scroll-up
157 (scroll-bar-scale portion-whole (1- (window-height))))))
158 (select-window old-selected-window))))
159
160 \f
161 ;;;; Bindings.
162
163 ;;; For now, we'll set things up to work like xterm.
164 (global-set-key [vertical-scroll-bar mouse-1] 'scroll-bar-scroll-up)
165 (global-set-key [vertical-scroll-bar drag-mouse-1] 'scroll-bar-scroll-up)
166
167 (global-set-key [vertical-scroll-bar down-mouse-2] 'scroll-bar-drag)
168
169 (global-set-key [vertical-scroll-bar mouse-3] 'scroll-bar-scroll-down)
170 (global-set-key [vertical-scroll-bar drag-mouse-3] 'scroll-bar-scroll-down)
171
172 \f
173 (provide 'scroll-bar)
174
175 ;;; scroll-bar.el ends here