(Fx_open_connection): Make `data' unsigned char.
[bpt/emacs.git] / lisp / scroll-bar.el
CommitLineData
aae56ea7 1;;; scroll-bar.el --- window system-independent scroll bar support.
6d62a90e
JB
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
76550a57
ER
24;;; Code:
25
d9ecc911
ER
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
dbc4e1c1
JB
32(require 'mouse)
33
6d62a90e
JB
34\f
35;;;; Utilities.
36
bf3c8a70 37(defun scroll-bar-scale (num-denom whole)
6d62a90e 38 "Given a pair (NUM . DENOM) and WHOLE, return (/ (* NUM WHOLE) DENOM).
bf3c8a70
JB
39This is handy for scaling a position on a scroll bar into real units,
40like buffer positions. If SCROLL-BAR-POS is the (PORTION . WHOLE) pair
41from a scroll bar event, then (scroll-bar-scale SCROLL-BAR-POS
6d62a90e 42\(buffer-size)) is the position in the current buffer corresponding to
bf3c8a70 43that scroll bar position."
6d62a90e
JB
44 ;; We multiply before we divide to maintain precision.
45 ;; We use floating point because the product of a large buffer size
bf3c8a70 46 ;; with a large scroll bar portion can easily overflow a lisp int.
6d62a90e
JB
47 (truncate (/ (* (float (car num-denom)) whole) (cdr num-denom))))
48
49\f
fe48f821 50;;;; Helpful functions for enabling and disabling scroll bars.
79161a45
RS
51;;; This is not documented because you can't change the
52;;; mode properly by setting it.
53(defvar scroll-bar-mode t)
fe48f821
JB
54
55(defun scroll-bar-mode (flag)
56 "Toggle display of vertical scroll bars on each frame.
57This command applies to all frames that exist and frames to be
58created in the future.
59With a numeric argument, if the argument is negative,
60turn 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))))))
bf3c8a70 73 '(vertical-scroll-bars horizontal-scroll-bars))
fe48f821
JB
74 (let ((frames (frame-list)))
75 (while frames
76 (modify-frame-parameters
77 (car frames)
bf3c8a70
JB
78 (list (cons 'vertical-scroll-bars scroll-bar-mode)
79 (cons 'horizontal-scroll-bars scroll-bar-mode)))
fe48f821
JB
80 (setq frames (cdr frames)))))
81\f
bf3c8a70 82;;;; Buffer navigation using the scroll bar.
6d62a90e 83
e532b016 84;;; This was used for up-events on button 2, but no longer.
bf3c8a70
JB
85(defun scroll-bar-set-window-start (event)
86 "Set the window start according to where the scroll bar is dragged.
87EVENT should be a scroll bar click or drag event."
6d62a90e 88 (interactive "e")
dbc4e1c1 89 (let* ((end-position (event-end event))
6d62a90e
JB
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
bf3c8a70 95 (goto-char (scroll-bar-scale portion-whole (buffer-size)))
6d62a90e
JB
96 (beginning-of-line)
97 (set-window-start window (point))))))
98
e532b016
RS
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 (goto-char (scroll-bar-scale portion-whole (buffer-size)))
107 (beginning-of-line)
108 (set-window-start window (point)))))
109
110(defun scroll-bar-drag (event)
111 "Scroll the window by dragging the scroll bar slider.
112If you click outside the slider, the window scrolls to bring the slider there."
113 (interactive "e")
114 (let* (done)
115 (scroll-bar-drag-1 event)
116 (track-mouse
117 (while (not done)
118 (setq event (read-event))
119 (if (eq (car-safe event) 'mouse-movement)
120 (setq event (read-event)))
121 (cond ((eq (car-safe event) 'scroll-bar-movement)
122 (scroll-bar-drag-1 event))
123 (t
124 ;; Exit when we get the drag event; ignore that event.
125 (setq done t)))))))
126
bf3c8a70
JB
127(defun scroll-bar-scroll-down (event)
128 "Scroll the window's top line down to the location of the scroll bar click.
129EVENT should be a scroll bar click."
6d62a90e
JB
130 (interactive "e")
131 (let ((old-selected-window (selected-window)))
132 (unwind-protect
133 (progn
dbc4e1c1 134 (let* ((end-position (event-end event))
6d62a90e
JB
135 (window (nth 0 end-position))
136 (portion-whole (nth 2 end-position)))
137 (select-window window)
138 (scroll-down
bf3c8a70 139 (scroll-bar-scale portion-whole (1- (window-height))))))
6d62a90e
JB
140 (select-window old-selected-window))))
141
bf3c8a70
JB
142(defun scroll-bar-scroll-up (event)
143 "Scroll the line next to the scroll bar click to the top of the window.
144EVENT should be a scroll bar click."
6d62a90e
JB
145 (interactive "e")
146 (let ((old-selected-window (selected-window)))
147 (unwind-protect
148 (progn
dbc4e1c1 149 (let* ((end-position (event-end event))
6d62a90e
JB
150 (window (nth 0 end-position))
151 (portion-whole (nth 2 end-position)))
152 (select-window window)
153 (scroll-up
bf3c8a70 154 (scroll-bar-scale portion-whole (1- (window-height))))))
6d62a90e
JB
155 (select-window old-selected-window))))
156
157\f
158;;;; Bindings.
159
160;;; For now, we'll set things up to work like xterm.
bf3c8a70
JB
161(global-set-key [vertical-scroll-bar mouse-1] 'scroll-bar-scroll-up)
162(global-set-key [vertical-scroll-bar drag-mouse-1] 'scroll-bar-scroll-up)
6d62a90e 163
e532b016
RS
164(global-set-key [vertical-scroll-bar down-mouse-2] 'scroll-bar-drag)
165
bf3c8a70
JB
166(global-set-key [vertical-scroll-bar mouse-3] 'scroll-bar-scroll-down)
167(global-set-key [vertical-scroll-bar drag-mouse-3] 'scroll-bar-scroll-down)
6d62a90e
JB
168
169\f
dc14eed2 170(provide 'scroll-bar)
6d62a90e 171
bf3c8a70 172;;; scroll-bar.el ends here