462
+ − 1 ;;; generic-widgets.el --- Generic UI building
+ − 2
+ − 3 ;; Copyright (C) 2000 Free Software Foundation
+ − 4
+ − 5 ;; Maintainer: William Perry <wmperry@gnu.org>
+ − 6 ;; Keywords: extensions, dumped
+ − 7
+ − 8 ;; This file is part of XEmacs.
+ − 9
+ − 10 ;; XEmacs is free software; you can redistribute it and/or modify it
+ − 11 ;; 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 ;; XEmacs is distributed in the hope that it will be useful, but
+ − 16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
+ − 17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ − 18 ;; General Public License for more details.
+ − 19
+ − 20 ;; You should have received a copy of the GNU General Public License
+ − 21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
+ − 22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ − 23 ;; 02111-1307, USA.
+ − 24
+ − 25 ;;; Synched up with: Not in FSF
+ − 26
+ − 27 ;;; Commentary:
+ − 28
+ − 29 ;; This file is dumped with XEmacs.
+ − 30
502
+ − 31 (globally-declare-fboundp
+ − 32 '(gtk-label-new
+ − 33 gtk-widget-show-all gtk-signal-connect
+ − 34 gtk-window-new gtk-container-add gtk-vbox-new gtk-hbox-new
+ − 35 gtk-box-pack-start gtk-notebook-new
+ − 36 gtk-notebook-set-homogeneous-tabs gtk-notebook-set-scrollable
+ − 37 gtk-notebook-set-show-tabs gtk-notebook-set-tab-pos
+ − 38 gtk-notebook-append-page gtk-text-new gtk-text-set-editable
+ − 39 gtk-text-set-word-wrap gtk-text-set-line-wrap
+ − 40 gtk-widget-set-style gtk-text-insert gtk-label-set-line-wrap
+ − 41 gtk-label-set-justify gtk-radio-button-new
+ − 42 gtk-radio-button-group gtk-check-button-new
+ − 43 gtk-toggle-button-new gtk-button-new gtk-progress-bar-new
+ − 44 gtk-progress-bar-set-orientation gtk-progress-bar-set-bar-style))
+ − 45
462
+ − 46 (defun build-ui (ui)
+ − 47 (if (null ui)
+ − 48 (gtk-label-new "[empty]")
+ − 49 (let ((builder-func (intern-soft (format "build-ui::%s" (car ui))))
+ − 50 (widget nil))
+ − 51 (if (and builder-func (fboundp builder-func))
+ − 52 (progn
+ − 53 (setq widget (funcall builder-func ui))
+ − 54 (setcdr ui (plist-put (cdr ui) :x-internal-widget widget))
+ − 55 widget)
+ − 56 (error "Unknown ui element: %s" (car ui))))))
+ − 57
+ − 58 (defun show-ui (ui)
+ − 59 (let ((widget (plist-get (cdr ui) :x-internal-widget)))
+ − 60 (if (not widget)
+ − 61 (error "Attempting to show unrealized UI"))
+ − 62 (gtk-widget-show-all widget)
+ − 63 (gtk-signal-connect widget 'destroy
+ − 64 (lambda (widget ui)
+ − 65 (setcdr ui (plist-put (cdr ui) :x-internal-widget nil))) ui)))
+ − 66
+ − 67
+ − 68 (defun build-ui::window (spec)
+ − 69 "Create a top-level window for containing other widgets.
+ − 70 Properties:
+ − 71 :items list A list of child UI specs. Only the first is used.
+ − 72 :type toplevel/dialog/popup What type of window to create. Window managers
+ − 73 can (and usually do) treat each type differently.
+ − 74 "
+ − 75 (let ((plist (cdr spec))
+ − 76 (window nil)
+ − 77 (child nil))
+ − 78 (setq window (gtk-window-new (plist-get plist :type 'toplevel))
+ − 79 child (build-ui (car (plist-get plist :items))))
+ − 80 (gtk-container-add window child)
+ − 81 window))
+ − 82
+ − 83 (defun build-ui::box (spec)
+ − 84 "Create a box for containing other widgets.
+ − 85 Properties:
+ − 86 :items list A list of child UI specs.
+ − 87 :homogeneous t/nil Whether all children are the same width/height.
+ − 88 :spacing number Spacing between children.
+ − 89 :orientation horizontal/vertical How the widgets are stacked.
+ − 90
+ − 91 Additional properties on child widgets:
+ − 92 :expand t/nil Whether the new child is to be given extra space
+ − 93 allocated to box. The extra space will be divided
+ − 94 evenly between all children of box that use this
+ − 95 option.
+ − 96 :fill t/nil Whether space given to child by the expand option is
+ − 97 actually allocated to child, rather than just padding
+ − 98 it. This parameter has no effect if :expand is set to
+ − 99 nil. A child is always allocated the full height of a
+ − 100 horizontal box and the full width of a vertical box.
+ − 101 This option affects the other dimension.
+ − 102 :padding number Extra padding around this widget.
+ − 103 "
+ − 104 (let* ((plist (cdr spec))
+ − 105 (orientation (plist-get plist :orientation 'horizontal))
+ − 106 (children (plist-get plist :items))
+ − 107 (box nil)
+ − 108 (child-widget nil)
+ − 109 (child-plist nil))
+ − 110 (case orientation
+ − 111 (vertical (setq box (gtk-vbox-new (plist-get plist :homogeneous)
+ − 112 (plist-get plist :spacing))))
+ − 113 (horizontal (setq box (gtk-hbox-new (plist-get plist :homogeneous)
+ − 114 (plist-get plist :spacing))))
+ − 115 (otherwise (error "Unknown orientation for box: %s" orientation)))
+ − 116 (mapc
+ − 117 (lambda (child)
+ − 118 (setq child-plist (cdr child)
+ − 119 child-widget (build-ui child))
+ − 120 (if (listp child-widget)
+ − 121 (mapc (lambda (w)
+ − 122 (gtk-box-pack-start box w
+ − 123 (plist-get child-plist :expand)
+ − 124 (plist-get child-plist :fill)
+ − 125 (plist-get child-plist :padding))) child-widget)
+ − 126 (gtk-box-pack-start box child-widget
+ − 127 (plist-get child-plist :expand)
+ − 128 (plist-get child-plist :fill)
+ − 129 (plist-get child-plist :padding))))
+ − 130 children)
+ − 131 box))
+ − 132
+ − 133 (defun build-ui::tab-control (spec)
+ − 134 "Create a notebook widget.
+ − 135 Properties:
+ − 136 :items list A list of UI specs to use as notebook pages.
+ − 137 :homogeneous t/nil Whether all tabs are the same width.
+ − 138 :orientation top/bottom/left/right Position of tabs
+ − 139 :show-tabs t/nil Show the tabs on screen?
+ − 140 :scrollable t/nil Allow scrolling to view all tab widgets?
+ − 141
+ − 142 Additional properties on child widgets:
+ − 143 :tab-label ui A UI spec to use for the tab label.
+ − 144 "
+ − 145 (let* ((plist (cdr spec))
+ − 146 (notebook (gtk-notebook-new))
+ − 147 (children (plist-get plist :items))
+ − 148 (page-counter 1)
+ − 149 (label-widget nil)
+ − 150 (child-widget nil)
+ − 151 (child-plist nil))
+ − 152 ;; Set all the properties
+ − 153 (gtk-notebook-set-homogeneous-tabs notebook (plist-get plist :homogeneous))
+ − 154 (gtk-notebook-set-scrollable notebook (plist-get plist :scrollable t))
+ − 155 (gtk-notebook-set-show-tabs notebook (plist-get plist :show-tabs t))
+ − 156 (gtk-notebook-set-tab-pos notebook (plist-get plist :orientation 'top))
+ − 157
+ − 158 ;; Now fill in the tabs
+ − 159 (mapc
+ − 160 (lambda (child)
+ − 161 (setq child-plist (cdr child)
+ − 162 child-widget (build-ui child)
+ − 163 label-widget (build-ui (plist-get child-plist :tab-label
+ − 164 (list 'label :text (format "tab %d" page-counter))))
+ − 165 page-counter (1+ page-counter))
+ − 166 (gtk-notebook-append-page notebook child-widget label-widget))
+ − 167 children)
+ − 168 notebook))
+ − 169
+ − 170 (defun build-ui::text (spec)
+ − 171 "Create a multi-line text widget.
+ − 172 Properties:
+ − 173 :editable t/nil Whether the user can change the contents
+ − 174 :word-wrap t/nil Automatic word wrapping?
+ − 175 :line-wrap t/nil Automatic line wrapping?
+ − 176 :text string Initial contents of the widget
+ − 177 :file filename File for initial contents (takes precedence over :text)
+ − 178 :face facename XEmacs face to use in the widget.
+ − 179 "
+ − 180 (let* ((plist (cdr spec))
+ − 181 (text (gtk-text-new nil nil))
+ − 182 (face (plist-get plist :face 'default))
+ − 183 (info (plist-get plist :text))
+ − 184 (file (plist-get plist :file)))
+ − 185 (gtk-text-set-editable text (plist-get plist :editable))
+ − 186 (gtk-text-set-word-wrap text (plist-get plist :word-wrap))
+ − 187 (gtk-text-set-line-wrap text (plist-get plist :line-wrap))
+ − 188 (gtk-widget-set-style text 'default)
+ − 189
+ − 190 ;; Possible convert the file portion
+ − 191 (if (and file (not (stringp file)))
+ − 192 (setq file (eval file)))
+ − 193
+ − 194 (if (and info (not (stringp info)))
+ − 195 (setq info (eval info)))
+ − 196
+ − 197 (if (and file (file-exists-p file) (file-readable-p file))
+ − 198 (save-excursion
+ − 199 (set-buffer (get-buffer-create " *improbable buffer name*"))
+ − 200 (insert-file-contents file)
+ − 201 (setq info (buffer-string))))
+ − 202
+ − 203 (gtk-text-insert text
+ − 204 (face-font face)
+ − 205 (face-foreground face)
+ − 206 (face-background face)
+ − 207 info (length info))
+ − 208 text))
+ − 209
+ − 210 (defun build-ui::label (spec)
+ − 211 "Create a label widget.
+ − 212 Properties:
+ − 213 :text string Text inside the label
+ − 214 :face facename XEmacs face to use in the widget.
+ − 215 :justification right/left/center How to justify the text.
+ − 216 "
+ − 217 (let* ((plist (cdr spec))
+ − 218 (label (gtk-label-new (plist-get plist :text))))
+ − 219 (gtk-label-set-line-wrap label t)
+ − 220 (gtk-label-set-justify label (plist-get plist :justification))
+ − 221 (gtk-widget-set-style label (plist-get plist :face 'default))
+ − 222 label))
+ − 223
+ − 224 (defun build-ui::pixmap (spec)
+ − 225 "Create a multi-line text widget.
+ − 226 Properties:
+ − 227 :text string Text inside the label
+ − 228 :face facename XEmacs face to use in the widget.
+ − 229 :justification right/left/center How to justify the text.
+ − 230 "
+ − 231 (let* ((plist (cdr spec))
+ − 232 (label (gtk-label-new (plist-get plist :text))))
+ − 233 (gtk-label-set-line-wrap label t)
+ − 234 (gtk-label-set-justify label (plist-get plist :justification))
+ − 235 (gtk-widget-set-style label (plist-get plist :face 'default))
+ − 236 label))
+ − 237
+ − 238 (defun build-ui::radio-group (spec)
+ − 239 "A convenience when specifying a group of radio buttons."
502
+ − 240 (declare (special build-ui::radio-group))
462
+ − 241 (let ((build-ui::radio-group nil))
+ − 242 (mapcar 'build-ui (plist-get (cdr spec) :items))))
+ − 243
+ − 244 (defun build-ui::button (spec)
+ − 245 "Create a button widget.
+ − 246 Properties:
+ − 247 :type radio/check/toggle/nil What type of button to create.
+ − 248 :text string Text in the button.
+ − 249 :glyph glyph Image in the button.
+ − 250 :label ui A UI spec to use for the label.
+ − 251 :relief normal/half/none How to draw button edges.
+ − 252
+ − 253 NOTE: Radio buttons must be in a radio-group object for them to work.
+ − 254 "
502
+ − 255 (declare (special build-ui::radio-group))
+ − 256 (let* ((plist (cdr spec))
+ − 257 (button nil)
+ − 258 (button-type (plist-get plist :type 'normal)))
462
+ − 259 (case button-type
+ − 260 (radio
+ − 261 (if (not (boundp 'build-ui::radio-group))
+ − 262 (error "Attempt to use a radio button outside a radio-group"))
+ − 263 (setq button (gtk-radio-button-new build-ui::radio-group)
+ − 264 build-ui::radio-group (gtk-radio-button-group button)))
+ − 265 (check
+ − 266 (setq button (gtk-check-button-new)))
+ − 267 (toggle
+ − 268 (setq button (gtk-toggle-button-new)))
+ − 269 (normal
+ − 270 (setq button (gtk-button-new)))
+ − 271 (otherwise
+ − 272 (error "Unknown button type: %s" button-type)))
+ − 273 (gtk-container-add
+ − 274 button
+ − 275 (build-ui (plist-get plist :label
+ − 276 (list 'label :text
+ − 277 (plist-get plist
+ − 278 :text (format "%s button" button-type))))))
+ − 279 button))
+ − 280
+ − 281 (defun build-ui::progress-gauge (spec)
+ − 282 "Create a progress meter.
+ − 283 Properties:
+ − 284 :orientation left-to-right/right-to-left/top-to-bottom/bottom-to-top
+ − 285 :type discrete/continuous
+ − 286
+ − 287 "
+ − 288 (let ((plist (cdr spec))
+ − 289 (gauge (gtk-progress-bar-new)))
+ − 290 (gtk-progress-bar-set-orientation gauge (plist-get plist :orientation 'left-to-right))
+ − 291 (gtk-progress-bar-set-bar-style gauge (plist-get plist :type 'continuous))
+ − 292 gauge))
+ − 293
+ − 294 (provide 'generic-widgets)
+ − 295
+ − 296 (when (featurep 'gtk) ; just loading this file should be OK
+ − 297 (gtk-widget-show-all
+ − 298 (build-ui
+ − 299 '(window :type dialog
+ − 300 :items ((tab-control
+ − 301 :homogeneous t
+ − 302 :orientation bottom
+ − 303 :items ((box :orientation vertical
+ − 304 :tab-label (label :text "vertical")
+ − 305 :items ((label :text "Vertical")
+ − 306 (progress-gauge)
+ − 307 (label :text "Box stacking")))
+ − 308 (box :orientation horizontal
+ − 309 :spacing 10
+ − 310 :items ((label :text "Horizontal box")
+ − 311 (label :text "stacking")))
+ − 312
+ − 313 (box :orientation vertical
+ − 314 :items
+ − 315 ((radio-group
+ − 316 :items ((button :type radio
+ − 317 :expand nil
+ − 318 :fill nil
+ − 319 :text "Item 1")
+ − 320 (button :type radio
+ − 321 :expand nil
+ − 322 :fill nil
+ − 323 :text "Item 2")
+ − 324 (button :type radio
+ − 325 :expand nil
+ − 326 :fill nil
+ − 327 :text "Item 3")
+ − 328 (button :type radio
+ − 329 :expand nil
+ − 330 :fill nil)))))
+ − 331 (box :orientation vertical
+ − 332 :items ((button :type check
+ − 333 :text "Item 1")
+ − 334 (button :type check
+ − 335 :text "Item 2")
+ − 336 (button :type normal
+ − 337 :text "Item 3")
+ − 338 (button :type toggle)))
+ − 339 (text :editable t
+ − 340 :word-wrap t
+ − 341 :file (locate-data-file "COPYING"))
+ − 342 (text :editable t
+ − 343 :face display-time-mail-balloon-enhance-face
+ − 344 :word-wrap t
+ − 345 :text "Text with a face on it")))))))
+ − 346 )