* scrollbar.el: New file.
[bpt/emacs.git] / lisp / scroll-bar.el
1 ;;; scrollbar.el -- window system-independent scrollbar 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 \f
25 ;;;; Utilities.
26
27 (defun scrollbar-scale (num-denom whole)
28 "Given a pair (NUM . DENOM) and WHOLE, return (/ (* NUM WHOLE) DENOM).
29 This is handy for scaling a position on a scrollbar into real units,
30 like buffer positions. If SCROLLBAR-POS is the (PORTION . WHOLE) pair
31 from a scrollbar event, then (scrollbar-scale SCROLLBAR-POS
32 \(buffer-size)) is the position in the current buffer corresponding to
33 that scrollbar position."
34 ;; We multiply before we divide to maintain precision.
35 ;; We use floating point because the product of a large buffer size
36 ;; with a large scrollbar portion can easily overflow a lisp int.
37 (truncate (/ (* (float (car num-denom)) whole) (cdr num-denom))))
38
39 \f
40 ;;;; Buffer navigation using the scrollbar.
41
42 (defun scrollbar-set-window-start (event)
43 "Set the window start according to where the scrollbar is dragged.
44 EVENT should be a scrollbar click or drag event."
45 (interactive "e")
46 (let* ((end-position (nth (1- (length event)) event))
47 (window (nth 0 end-position))
48 (portion-whole (nth 2 end-position)))
49 (save-excursion
50 (set-buffer (window-buffer window))
51 (save-excursion
52 (goto-char (scrollbar-scale portion-whole (buffer-size)))
53 (beginning-of-line)
54 (set-window-start window (point))))))
55
56 (defun scrollbar-scroll-down (event)
57 "Scroll the window's top line down to the location of the scrollbar click.
58 EVENT should be a scrollbar click."
59 (interactive "e")
60 (let ((old-selected-window (selected-window)))
61 (unwind-protect
62 (progn
63 (let* ((end-position (nth (1- (length event)) event))
64 (window (nth 0 end-position))
65 (portion-whole (nth 2 end-position)))
66 (select-window window)
67 (scroll-down
68 (scrollbar-scale portion-whole (1- (window-height))))))
69 (select-window old-selected-window))))
70
71 (defun scrollbar-scroll-up (event)
72 "Scroll the line next to the scrollbar click to the top of the window.
73 EVENT should be a scrollbar click."
74 (interactive "e")
75 (let ((old-selected-window (selected-window)))
76 (unwind-protect
77 (progn
78 (let* ((end-position (nth (1- (length event)) event))
79 (window (nth 0 end-position))
80 (portion-whole (nth 2 end-position)))
81 (select-window window)
82 (scroll-up
83 (scrollbar-scale portion-whole (1- (window-height))))))
84 (select-window old-selected-window))))
85
86 \f
87 ;;;; Bindings.
88
89 ;;; For now, we'll set things up to work like xterm.
90 (global-set-key [vertical-scrollbar mouse-1] 'scrollbar-scroll-up)
91 (global-set-key [vertical-scrollbar drag-mouse-1] 'scrollbar-scroll-up)
92
93 (global-set-key [vertical-scrollbar mouse-2] 'scrollbar-set-window-start)
94 (global-set-key [vertical-scrollbar drag-mouse-2] 'scrollbar-set-window-start)
95
96 (global-set-key [vertical-scrollbar mouse-3] 'scrollbar-scroll-down)
97 (global-set-key [vertical-scrollbar drag-mouse-3] 'scrollbar-scroll-down)
98
99 \f
100 (provide 'scrollbar)
101
102 ;;; scrollbar.el ends here