~ubuntu-branches/ubuntu/lucid/sawfish/lucid-updates

« back to all changes in this revision

Viewing changes to lisp/sawfish/wm/ext/tooltips.jl

  • Committer: Bazaar Package Importer
  • Author(s): Christian Marillat
  • Date: 2002-01-20 17:42:28 UTC
  • Revision ID: james.westby@ubuntu.com-20020120174228-4q1ydztbkvfq1ht2
Tags: upstream-1.0.1.20020116
ImportĀ upstreamĀ versionĀ 1.0.1.20020116

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
;; tooltips.jl -- display frame-part keymap descriptions
 
2
;; $Id: tooltips.jl,v 1.29 2001/04/05 22:02:01 jsh Exp $
 
3
 
 
4
;; Copyright (C) 1999 John Harper <john@dcs.warwick.ac.uk>
 
5
 
 
6
;; This file is part of sawmill.
 
7
 
 
8
;; sawmill is free software; you can redistribute it and/or modify it
 
9
;; under the terms of the GNU General Public License as published by
 
10
;; the Free Software Foundation; either version 2, or (at your option)
 
11
;; any later version.
 
12
 
 
13
;; sawmill is distributed in the hope that it will be useful, but
 
14
;; WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
;; GNU General Public License for more details.
 
17
 
 
18
;; You should have received a copy of the GNU General Public License
 
19
;; along with sawmill; see the file COPYING.  If not, write to
 
20
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 
21
 
 
22
(define-structure sawfish.wm.ext.tooltips
 
23
 
 
24
    (export display-tooltip
 
25
            display-tooltip-after-delay
 
26
            remove-tooltip)
 
27
 
 
28
    (open rep
 
29
          rep.system
 
30
          rep.regexp
 
31
          rep.io.timers
 
32
          sawfish.wm.commands
 
33
          sawfish.wm.custom
 
34
          sawfish.wm.misc
 
35
          sawfish.wm.windows
 
36
          sawfish.wm.frames
 
37
          sawfish.wm.events
 
38
          sawfish.wm.util.keymap)
 
39
 
 
40
  (define-structure-alias tooltips sawfish.wm.ext.tooltips)
 
41
 
 
42
  (define tooltips-timer nil)
 
43
 
 
44
  ;;###autoload (defgroup tooltips "Tooltips" :group misc :require sawfish.wm.ext.tooltips)
 
45
 
 
46
  (defgroup tooltips "Tooltips"
 
47
    :group misc
 
48
    :require sawfish.wm.ext.tooltips)
 
49
 
 
50
  (defcustom tooltips-enabled nil
 
51
    "Display tooltips for window frames."
 
52
    :type boolean
 
53
    :group (misc tooltips)
 
54
    :user-level novice
 
55
    :require sawfish.wm.ext.tooltips)
 
56
 
 
57
  (defcustom tooltips-timeout-enabled nil
 
58
    "Remove tooltips after a period of time."
 
59
    :type boolean
 
60
    :depends tooltips-enabled
 
61
    :group (misc tooltips))
 
62
 
 
63
  (defcustom tooltips-show-doc-strings t
 
64
    "Show full documentation in tooltips."
 
65
    :type boolean
 
66
    :depends tooltips-enabled
 
67
    :group (misc tooltips))
 
68
 
 
69
  (defcustom tooltips-delay 500
 
70
    "Number of milliseconds before displaying tooltips."
 
71
    :type number
 
72
    :range (1)
 
73
    :depends tooltips-enabled
 
74
    :group (misc tooltips))
 
75
 
 
76
  (defcustom tooltips-timeout-delay 5000
 
77
    "Number of milliseconds before removing tooltips."
 
78
    :type number
 
79
    :user-level expert
 
80
    :range (1)
 
81
    :depends tooltips-enabled
 
82
    :group (misc tooltips))
 
83
 
 
84
  (defcustom tooltips-font "-*-lucidatypewriter-medium-*-*-*-10-*-*-*-*-*-*-*"
 
85
    "Font used to display tooltips."
 
86
    :type font
 
87
    :user-level expert
 
88
    :depends tooltips-enabled
 
89
    :group (misc tooltips))
 
90
 
 
91
  (defcustom tooltips-background-color "grey85"
 
92
    "Color used for the tooltips background"
 
93
    :group (misc tooltips)
 
94
    :user-level expert
 
95
    :depends tooltips-enabled
 
96
    :type color)
 
97
 
 
98
  (defcustom tooltips-foreground-color "black"
 
99
    "Color used for the tooltips foreground"
 
100
    :group (misc tooltips)
 
101
    :user-level expert
 
102
    :depends tooltips-enabled
 
103
    :type color)
 
104
 
 
105
;;; displaying tooltips
 
106
 
 
107
  ;; the window it's displayed for (or t)
 
108
  (define tooltips-displayed nil)
 
109
 
 
110
  (define (display-tooltip text #!optional win)
 
111
    (let ((pos (query-pointer))
 
112
          (pos-fn (lambda (in size inc)
 
113
                    (if (< in (/ size 2))
 
114
                        (+ in inc)
 
115
                      (- (+ (- size in) inc))))))
 
116
      (rplaca pos (pos-fn (car pos) (screen-width) 0))
 
117
      (rplacd pos (pos-fn (cdr pos) (screen-height) 16))
 
118
      (display-message (if (functionp text) (text) text)
 
119
                       `((position . ,pos)
 
120
                         (background . ,tooltips-background-color)
 
121
                         (foreground . ,tooltips-foreground-color)
 
122
                         (x-justify . left)
 
123
                         (spacing . 2)
 
124
                         (font . ,tooltips-font)))
 
125
      (setq tooltips-displayed (or win t))
 
126
      (when tooltips-timeout-enabled 
 
127
        (setq tooltips-timer
 
128
              (make-timer remove-tooltip
 
129
                          (quotient tooltips-timeout-delay 1000)
 
130
                          (mod tooltips-timeout-delay 1000))))
 
131
      (unless (in-hook-p 'pre-command-hook remove-tooltip)
 
132
        (add-hook 'pre-command-hook remove-tooltip))))
 
133
 
 
134
  (define (remove-tooltip)
 
135
    (when (in-hook-p 'pre-command-hook remove-tooltip)
 
136
      (remove-hook 'pre-command-hook remove-tooltip))
 
137
    (when tooltips-displayed
 
138
      (display-message nil)
 
139
      (setq tooltips-displayed nil))
 
140
    (when tooltips-timer
 
141
      (delete-timer tooltips-timer)
 
142
      (setq tooltips-timer nil)))
 
143
 
 
144
  (define (tooltips-unmapped win)
 
145
    (when (eq win tooltips-displayed)
 
146
      (remove-tooltip)))
 
147
 
 
148
  (add-hook 'unmap-notify-hook tooltips-unmapped)
 
149
 
 
150
  (define (call-after-delay thunk)
 
151
    (remove-tooltip)
 
152
    (when tooltips-enabled
 
153
      (setq tooltips-timer (make-timer (lambda ()
 
154
                                         (setq tooltips-timer nil)
 
155
                                         (thunk))
 
156
                                       (quotient tooltips-delay 1000)
 
157
                                       (mod tooltips-delay 1000)))
 
158
      (unless (in-hook-p 'pre-command-hook remove-tooltip)
 
159
        (add-hook 'pre-command-hook remove-tooltip))))
 
160
 
 
161
  (define (display-tooltip-after-delay . args)
 
162
    (call-after-delay (lambda ()
 
163
                        (apply display-tooltip args))))
 
164
 
 
165
;;; frame-part tooltips
 
166
 
 
167
  ;; each item is (EVENT-DESC . DOC)
 
168
  (define (tooltips-format items)
 
169
    (let ((max-event-width 0)
 
170
          (split (lambda (s)
 
171
                   (let (point parts)
 
172
                     (while (string-match "\n" s point)
 
173
                       (setq parts (cons (substring
 
174
                                          s (or point 0) (match-start)) parts))
 
175
                       (setq point (match-end)))
 
176
                     (setq parts (cons (substring s (or point 0)) parts))
 
177
                     (nreverse parts))))
 
178
          out)
 
179
      (mapc (lambda (cell)
 
180
              (setq max-event-width (max max-event-width (length (car cell)))))
 
181
            items)
 
182
      (setq max-event-width (1+ max-event-width))
 
183
      (mapc (lambda (cell)
 
184
              (setq out (cons (car cell) out))
 
185
              (setq out (cons (make-string (- max-event-width
 
186
                                              (length (car cell))) ? ) out))
 
187
              (let ((parts (split (cdr cell))))
 
188
                (when parts
 
189
                  (setq out (cons (car parts) out))
 
190
                  (setq out (cons ?\n out)))
 
191
                (mapc (lambda (string)
 
192
                        (setq out (cons (make-string max-event-width ? ) out))
 
193
                        (setq out (cons string out))
 
194
                        (setq out (cons ?\n out))) (cdr parts)))) items)
 
195
      (apply concat (nreverse out))))
 
196
 
 
197
  (define (command-info command)
 
198
    (let (doc)
 
199
      (if (and tooltips-show-doc-strings command
 
200
               (symbolp command)
 
201
               (setq doc (command-documentation command)))
 
202
          (_ doc)
 
203
        (format nil "%S" command))))
 
204
 
 
205
  (define (display-fp-tooltip fp)
 
206
    (let ((keymap (frame-part-get fp 'keymap))
 
207
          items)
 
208
      (when (symbolp keymap)
 
209
        (setq keymap (symbol-value keymap)))
 
210
      (map-keymap (lambda (cell)
 
211
                    (setq items (cons (cons (event-name (cdr cell))
 
212
                                            (command-info (car cell))) items)))
 
213
                  keymap)
 
214
      (display-tooltip (tooltips-format (nreverse items)))))
 
215
 
 
216
  (define (tooltips-fp-enter win fp)
 
217
    (declare (unused win))
 
218
    (when tooltips-enabled
 
219
      (call-after-delay (lambda ()
 
220
                          (unless (clicked-frame-part)
 
221
                            (display-fp-tooltip fp))))))
 
222
 
 
223
  (add-hook 'enter-frame-part-hook tooltips-fp-enter)
 
224
  (add-hook 'leave-frame-part-hook remove-tooltip))