1 ;;; scrollbar.el -- window system-independent scrollbar support.
3 ;;; Copyright (C) 1993 Free Software Foundation, Inc.
8 ;;; This file is part of GNU Emacs.
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.
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.
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.
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
))))
40 ;;;; Buffer navigation using the scrollbar.
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."
46 (let* ((end-position (nth (1- (length event
)) event
))
47 (window (nth 0 end-position
))
48 (portion-whole (nth 2 end-position
)))
50 (set-buffer (window-buffer window
))
52 (goto-char (scrollbar-scale portion-whole
(buffer-size)))
54 (set-window-start window
(point))))))
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."
60 (let ((old-selected-window (selected-window)))
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
)
68 (scrollbar-scale portion-whole
(1- (window-height))))))
69 (select-window old-selected-window
))))
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."
75 (let ((old-selected-window (selected-window)))
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
)
83 (scrollbar-scale portion-whole
(1- (window-height))))))
84 (select-window old-selected-window
))))
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
)
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
)
96 (global-set-key [vertical-scrollbar mouse-3
] 'scrollbar-scroll-down
)
97 (global-set-key [vertical-scrollbar drag-mouse-3
] 'scrollbar-scroll-down
)
102 ;;; scrollbar.el ends here