1 ;;; generic-widgets.el --- Generic UI building
3 ;; Copyright (C) 2000 Free Software Foundation
5 ;; Maintainer: William Perry <wmperry@gnu.org>
6 ;; Keywords: extensions, dumped
8 ;; This file is part of XEmacs.
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)
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.
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
25 ;;; Synched up with: Not in FSF
29 ;; This file is dumped with XEmacs.
33 (gtk-label-new "[empty]")
34 (let ((builder-func (intern-soft (format "build-ui::%s" (car ui))))
36 (if (and builder-func (fboundp builder-func))
38 (setq widget (funcall builder-func ui))
39 (setcdr ui (plist-put (cdr ui) :x-internal-widget widget))
41 (error "Unknown ui element: %s" (car ui))))))
44 (let ((widget (plist-get (cdr ui) :x-internal-widget)))
46 (error "Attempting to show unrealized UI"))
47 (gtk-widget-show-all widget)
48 (gtk-signal-connect widget 'destroy
50 (setcdr ui (plist-put (cdr ui) :x-internal-widget nil))) ui)))
53 (defun build-ui::window (spec)
54 "Create a top-level window for containing other widgets.
56 :items list A list of child UI specs. Only the first is used.
57 :type toplevel/dialog/popup What type of window to create. Window managers
58 can (and usually do) treat each type differently.
60 (let ((plist (cdr spec))
63 (setq window (gtk-window-new (plist-get plist :type 'toplevel))
64 child (build-ui (car (plist-get plist :items))))
65 (gtk-container-add window child)
68 (defun build-ui::box (spec)
69 "Create a box for containing other widgets.
71 :items list A list of child UI specs.
72 :homogeneous t/nil Whether all children are the same width/height.
73 :spacing number Spacing between children.
74 :orientation horizontal/vertical How the widgets are stacked.
76 Additional properties on child widgets:
77 :expand t/nil Whether the new child is to be given extra space
78 allocated to box. The extra space will be divided
79 evenly between all children of box that use this
81 :fill t/nil Whether space given to child by the expand option is
82 actually allocated to child, rather than just padding
83 it. This parameter has no effect if :expand is set to
84 nil. A child is always allocated the full height of a
85 horizontal box and the full width of a vertical box.
86 This option affects the other dimension.
87 :padding number Extra padding around this widget.
89 (let* ((plist (cdr spec))
90 (orientation (plist-get plist :orientation 'horizontal))
91 (children (plist-get plist :items))
96 (vertical (setq box (gtk-vbox-new (plist-get plist :homogeneous)
97 (plist-get plist :spacing))))
98 (horizontal (setq box (gtk-hbox-new (plist-get plist :homogeneous)
99 (plist-get plist :spacing))))
100 (otherwise (error "Unknown orientation for box: %s" orientation)))
103 (setq child-plist (cdr child)
104 child-widget (build-ui child))
105 (if (listp child-widget)
107 (gtk-box-pack-start box w
108 (plist-get child-plist :expand)
109 (plist-get child-plist :fill)
110 (plist-get child-plist :padding))) child-widget)
111 (gtk-box-pack-start box child-widget
112 (plist-get child-plist :expand)
113 (plist-get child-plist :fill)
114 (plist-get child-plist :padding))))
118 (defun build-ui::tab-control (spec)
119 "Create a notebook widget.
121 :items list A list of UI specs to use as notebook pages.
122 :homogeneous t/nil Whether all tabs are the same width.
123 :orientation top/bottom/left/right Position of tabs
124 :show-tabs t/nil Show the tabs on screen?
125 :scrollable t/nil Allow scrolling to view all tab widgets?
127 Additional properties on child widgets:
128 :tab-label ui A UI spec to use for the tab label.
130 (let* ((plist (cdr spec))
131 (notebook (gtk-notebook-new))
132 (children (plist-get plist :items))
137 ;; Set all the properties
138 (gtk-notebook-set-homogeneous-tabs notebook (plist-get plist :homogeneous))
139 (gtk-notebook-set-scrollable notebook (plist-get plist :scrollable t))
140 (gtk-notebook-set-show-tabs notebook (plist-get plist :show-tabs t))
141 (gtk-notebook-set-tab-pos notebook (plist-get plist :orientation 'top))
143 ;; Now fill in the tabs
146 (setq child-plist (cdr child)
147 child-widget (build-ui child)
148 label-widget (build-ui (plist-get child-plist :tab-label
149 (list 'label :text (format "tab %d" page-counter))))
150 page-counter (1+ page-counter))
151 (gtk-notebook-append-page notebook child-widget label-widget))
155 (defun build-ui::text (spec)
156 "Create a multi-line text widget.
158 :editable t/nil Whether the user can change the contents
159 :word-wrap t/nil Automatic word wrapping?
160 :line-wrap t/nil Automatic line wrapping?
161 :text string Initial contents of the widget
162 :file filename File for initial contents (takes precedence over :text)
163 :face facename XEmacs face to use in the widget.
165 (let* ((plist (cdr spec))
166 (text (gtk-text-new nil nil))
167 (face (plist-get plist :face 'default))
168 (info (plist-get plist :text))
169 (file (plist-get plist :file)))
170 (gtk-text-set-editable text (plist-get plist :editable))
171 (gtk-text-set-word-wrap text (plist-get plist :word-wrap))
172 (gtk-text-set-line-wrap text (plist-get plist :line-wrap))
173 (gtk-widget-set-style text 'default)
175 ;; Possible convert the file portion
176 (if (and file (not (stringp file)))
177 (setq file (eval file)))
179 (if (and info (not (stringp info)))
180 (setq info (eval info)))
182 (if (and file (file-exists-p file) (file-readable-p file))
184 (set-buffer (get-buffer-create " *improbable buffer name*"))
185 (insert-file-contents file)
186 (setq info (buffer-string))))
188 (gtk-text-insert text
190 (face-foreground face)
191 (face-background face)
195 (defun build-ui::label (spec)
196 "Create a label widget.
198 :text string Text inside the label
199 :face facename XEmacs face to use in the widget.
200 :justification right/left/center How to justify the text.
202 (let* ((plist (cdr spec))
203 (label (gtk-label-new (plist-get plist :text))))
204 (gtk-label-set-line-wrap label t)
205 (gtk-label-set-justify label (plist-get plist :justification))
206 (gtk-widget-set-style label (plist-get plist :face 'default))
209 (defun build-ui::pixmap (spec)
210 "Create a multi-line text widget.
212 :text string Text inside the label
213 :face facename XEmacs face to use in the widget.
214 :justification right/left/center How to justify the text.
216 (let* ((plist (cdr spec))
217 (label (gtk-label-new (plist-get plist :text))))
218 (gtk-label-set-line-wrap label t)
219 (gtk-label-set-justify label (plist-get plist :justification))
220 (gtk-widget-set-style label (plist-get plist :face 'default))
223 (defun build-ui::radio-group (spec)
224 "A convenience when specifying a group of radio buttons."
225 (let ((build-ui::radio-group nil))
226 (mapcar 'build-ui (plist-get (cdr spec) :items))))
228 (defun build-ui::button (spec)
229 "Create a button widget.
231 :type radio/check/toggle/nil What type of button to create.
232 :text string Text in the button.
233 :glyph glyph Image in the button.
234 :label ui A UI spec to use for the label.
235 :relief normal/half/none How to draw button edges.
237 NOTE: Radio buttons must be in a radio-group object for them to work.
239 (let ((plist (cdr spec))
241 (button-type (plist-get plist :type 'normal))
245 (if (not (boundp 'build-ui::radio-group))
246 (error "Attempt to use a radio button outside a radio-group"))
247 (setq button (gtk-radio-button-new build-ui::radio-group)
248 build-ui::radio-group (gtk-radio-button-group button)))
250 (setq button (gtk-check-button-new)))
252 (setq button (gtk-toggle-button-new)))
254 (setq button (gtk-button-new)))
256 (error "Unknown button type: %s" button-type)))
259 (build-ui (plist-get plist :label
262 :text (format "%s button" button-type))))))
265 (defun build-ui::progress-gauge (spec)
266 "Create a progress meter.
268 :orientation left-to-right/right-to-left/top-to-bottom/bottom-to-top
269 :type discrete/continuous
272 (let ((plist (cdr spec))
273 (gauge (gtk-progress-bar-new)))
274 (gtk-progress-bar-set-orientation gauge (plist-get plist :orientation 'left-to-right))
275 (gtk-progress-bar-set-bar-style gauge (plist-get plist :type 'continuous))
278 (provide 'generic-widgets)
280 (when (featurep 'gtk) ; just loading this file should be OK
283 '(window :type dialog
287 :items ((box :orientation vertical
288 :tab-label (label :text "vertical")
289 :items ((label :text "Vertical")
291 (label :text "Box stacking")))
292 (box :orientation horizontal
294 :items ((label :text "Horizontal box")
295 (label :text "stacking")))
297 (box :orientation vertical
300 :items ((button :type radio
315 (box :orientation vertical
316 :items ((button :type check
322 (button :type toggle)))
325 :file (locate-data-file "COPYING"))
327 :face display-time-mail-balloon-enhance-face
329 :text "Text with a face on it")))))))