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 SXEmacs.
10 ;; SXEmacs 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 3 of the License, or
13 ;; (at your option) any later version.
15 ;; SXEmacs 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 this program. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Synched up with: Not in FSF
27 ;; This file is dumped with SXEmacs.
29 (globally-declare-fboundp
31 gtk-widget-show-all gtk-signal-connect
32 gtk-window-new gtk-container-add gtk-vbox-new gtk-hbox-new
33 gtk-box-pack-start gtk-notebook-new
34 gtk-notebook-set-homogeneous-tabs gtk-notebook-set-scrollable
35 gtk-notebook-set-show-tabs gtk-notebook-set-tab-pos
36 gtk-notebook-append-page gtk-text-new gtk-text-set-editable
37 gtk-text-set-word-wrap gtk-text-set-line-wrap
38 gtk-widget-set-style gtk-text-insert gtk-label-set-line-wrap
39 gtk-label-set-justify gtk-radio-button-new
40 gtk-radio-button-group gtk-check-button-new
41 gtk-toggle-button-new gtk-button-new gtk-progress-bar-new
42 gtk-progress-bar-set-orientation gtk-progress-bar-set-bar-style))
46 (gtk-label-new "[empty]")
47 (let ((builder-func (intern-soft (format "build-ui::%s" (car ui))))
49 (if (and builder-func (fboundp builder-func))
51 (setq widget (funcall builder-func ui))
52 (setcdr ui (plist-put (cdr ui) :x-internal-widget widget))
54 (error "Unknown ui element: %s" (car ui))))))
57 (let ((widget (plist-get (cdr ui) :x-internal-widget)))
59 (error "Attempting to show unrealized UI"))
60 (gtk-widget-show-all widget)
61 (gtk-signal-connect widget 'destroy
63 (setcdr ui (plist-put (cdr ui) :x-internal-widget nil))) ui)))
66 (defun build-ui::window (spec)
67 "Create a top-level window for containing other widgets.
69 :items list A list of child UI specs. Only the first is used.
70 :type toplevel/dialog/popup What type of window to create. Window managers
71 can (and usually do) treat each type differently.
73 (let ((plist (cdr spec))
76 (setq window (gtk-window-new (plist-get plist :type 'toplevel))
77 child (build-ui (car (plist-get plist :items))))
78 (gtk-container-add window child)
81 (defun build-ui::box (spec)
82 "Create a box for containing other widgets.
84 :items list A list of child UI specs.
85 :homogeneous t/nil Whether all children are the same width/height.
86 :spacing number Spacing between children.
87 :orientation horizontal/vertical How the widgets are stacked.
89 Additional properties on child widgets:
90 :expand t/nil Whether the new child is to be given extra space
91 allocated to box. The extra space will be divided
92 evenly between all children of box that use this
94 :fill t/nil Whether space given to child by the expand option is
95 actually allocated to child, rather than just padding
96 it. This parameter has no effect if :expand is set to
97 nil. A child is always allocated the full height of a
98 horizontal box and the full width of a vertical box.
99 This option affects the other dimension.
100 :padding number Extra padding around this widget.
102 (let* ((plist (cdr spec))
103 (orientation (plist-get plist :orientation 'horizontal))
104 (children (plist-get plist :items))
109 (vertical (setq box (gtk-vbox-new (plist-get plist :homogeneous)
110 (plist-get plist :spacing))))
111 (horizontal (setq box (gtk-hbox-new (plist-get plist :homogeneous)
112 (plist-get plist :spacing))))
113 (otherwise (error "Unknown orientation for box: %s" orientation)))
116 (setq child-plist (cdr child)
117 child-widget (build-ui child))
118 (if (listp child-widget)
120 (gtk-box-pack-start box w
121 (plist-get child-plist :expand)
122 (plist-get child-plist :fill)
123 (plist-get child-plist :padding))) child-widget)
124 (gtk-box-pack-start box child-widget
125 (plist-get child-plist :expand)
126 (plist-get child-plist :fill)
127 (plist-get child-plist :padding))))
131 (defun build-ui::tab-control (spec)
132 "Create a notebook widget.
134 :items list A list of UI specs to use as notebook pages.
135 :homogeneous t/nil Whether all tabs are the same width.
136 :orientation top/bottom/left/right Position of tabs
137 :show-tabs t/nil Show the tabs on screen?
138 :scrollable t/nil Allow scrolling to view all tab widgets?
140 Additional properties on child widgets:
141 :tab-label ui A UI spec to use for the tab label.
143 (let* ((plist (cdr spec))
144 (notebook (gtk-notebook-new))
145 (children (plist-get plist :items))
150 ;; Set all the properties
151 (gtk-notebook-set-homogeneous-tabs notebook (plist-get plist :homogeneous))
152 (gtk-notebook-set-scrollable notebook (plist-get plist :scrollable t))
153 (gtk-notebook-set-show-tabs notebook (plist-get plist :show-tabs t))
154 (gtk-notebook-set-tab-pos notebook (plist-get plist :orientation 'top))
156 ;; Now fill in the tabs
159 (setq child-plist (cdr child)
160 child-widget (build-ui child)
161 label-widget (build-ui (plist-get child-plist :tab-label
162 (list 'label :text (format "tab %d" page-counter))))
163 page-counter (1+ page-counter))
164 (gtk-notebook-append-page notebook child-widget label-widget))
168 (defun build-ui::text (spec)
169 "Create a multi-line text widget.
171 :editable t/nil Whether the user can change the contents
172 :word-wrap t/nil Automatic word wrapping?
173 :line-wrap t/nil Automatic line wrapping?
174 :text string Initial contents of the widget
175 :file filename File for initial contents (takes precedence over :text)
176 :face facename XEmacs face to use in the widget.
178 (let* ((plist (cdr spec))
179 (text (gtk-text-new nil nil))
180 (face (plist-get plist :face 'default))
181 (info (plist-get plist :text))
182 (file (plist-get plist :file)))
183 (gtk-text-set-editable text (plist-get plist :editable))
184 (gtk-text-set-word-wrap text (plist-get plist :word-wrap))
185 (gtk-text-set-line-wrap text (plist-get plist :line-wrap))
186 (gtk-widget-set-style text 'default)
188 ;; Possible convert the file portion
189 (if (and file (not (stringp file)))
190 (setq file (eval file)))
192 (if (and info (not (stringp info)))
193 (setq info (eval info)))
195 (if (and file (file-exists-p file) (file-readable-p file))
197 (set-buffer (get-buffer-create " *improbable buffer name*"))
198 (insert-file-contents file)
199 (setq info (buffer-string))))
201 (gtk-text-insert text
203 (face-foreground face)
204 (face-background face)
208 (defun build-ui::label (spec)
209 "Create a label widget.
211 :text string Text inside the label
212 :face facename XEmacs face to use in the widget.
213 :justification right/left/center How to justify the text.
215 (let* ((plist (cdr spec))
216 (label (gtk-label-new (plist-get plist :text))))
217 (gtk-label-set-line-wrap label t)
218 (gtk-label-set-justify label (plist-get plist :justification))
219 (gtk-widget-set-style label (plist-get plist :face 'default))
222 (defun build-ui::pixmap (spec)
223 "Create a multi-line text widget.
225 :text string Text inside the label
226 :face facename XEmacs face to use in the widget.
227 :justification right/left/center How to justify the text.
229 (let* ((plist (cdr spec))
230 (label (gtk-label-new (plist-get plist :text))))
231 (gtk-label-set-line-wrap label t)
232 (gtk-label-set-justify label (plist-get plist :justification))
233 (gtk-widget-set-style label (plist-get plist :face 'default))
236 (defun build-ui::radio-group (spec)
237 "A convenience when specifying a group of radio buttons."
238 (declare (special build-ui::radio-group))
239 (let ((build-ui::radio-group nil))
240 (mapcar 'build-ui (plist-get (cdr spec) :items))))
242 (defun build-ui::button (spec)
243 "Create a button widget.
245 :type radio/check/toggle/nil What type of button to create.
246 :text string Text in the button.
247 :glyph glyph Image in the button.
248 :label ui A UI spec to use for the label.
249 :relief normal/half/none How to draw button edges.
251 NOTE: Radio buttons must be in a radio-group object for them to work.
253 (declare (special build-ui::radio-group))
254 (let* ((plist (cdr spec))
256 (button-type (plist-get plist :type 'normal))
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)))
266 (setq button (gtk-check-button-new)))
268 (setq button (gtk-toggle-button-new)))
270 (setq button (gtk-button-new)))
272 (error "Unknown button type: %s" button-type)))
275 (build-ui (plist-get plist :label
278 :text (format "%s button" button-type))))))
281 (defun build-ui::progress-gauge (spec)
282 "Create a progress meter.
284 :orientation left-to-right/right-to-left/top-to-bottom/bottom-to-top
285 :type discrete/continuous
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))
294 (provide 'generic-widgets)
296 (when (featurep 'gtk) ; just loading this file should be OK
299 '(window :type dialog
303 :items ((box :orientation vertical
304 :tab-label (label :text "vertical")
305 :items ((label :text "Vertical")
307 (label :text "Box stacking")))
308 (box :orientation horizontal
310 :items ((label :text "Horizontal box")
311 (label :text "stacking")))
313 (box :orientation vertical
316 :items ((button :type radio
331 (box :orientation vertical
332 :items ((button :type check
338 (button :type toggle)))
341 :file (locate-data-file "COPYING"))
343 :face display-time-mail-balloon-enhance-face
345 :text "Text with a face on it")))))))