Refactor gtype-from-id, gtype-from-name
[cl-gtk2.git] / gtk / gtk.demo.lisp
bloba83846042a91d5a294aa3b27f4bce5264832ee08
1 (defpackage :gtk-demo
2 (:use :cl :gtk :gdk :gobject :iter)
3 (:export #:demo))
5 (in-package :gtk-demo)
7 (defparameter *src-location* (asdf:component-pathname (asdf:find-system :cl-gtk2-gtk)))
9 (defclass link-text-tag (text-tag)
11 (:metaclass gobject-class))
13 (defun make-link-fn-tag (buffer fn)
14 (let ((tag (make-instance 'link-text-tag :foreground "blue" :underline :single)))
15 (text-tag-table-add (text-buffer-tag-table buffer) tag)
16 (connect-signal tag "event"
17 (lambda (tag object event it)
18 (declare (ignore tag object it))
19 (when (and (eq (event-type event) :button-release)
20 (eq (event-button-button event) 1))
21 (when fn
22 (funcall fn)))))
23 tag))
25 (defun get-page (name)
26 (or (get name 'demo-page)
27 (get 'page-404 'demo-page)))
29 (defun (setf get-page) (page name)
30 (setf (get name 'demo-page) page))
32 (defmacro def-demo-page ((name &key (index 'index)) &body body)
33 `(setf (get-page ',name)
34 '(,@(when index (list `(:p (:link "To main" ,index))))
35 ,@body)))
37 (def-demo-page (page-404)
38 (:p "Non-existent page"))
40 (def-demo-page (index :index nil)
41 (:p (:b "cl-gtk2 demonstration"))
42 (:p "")
43 (:p "This demo application is a demonstration of what cl-gtk2 can do. You can click on any of blue underlined links to invoke the demonstration.")
44 (:p "")
45 (:p "You may try these demos:")
46 (:ol (:fn "Demonstrates usage of tree store" test-tree-store)
47 (:fn "Simple test of packing widgets into GtkTable"
48 table-packing)
49 (:fn "Test of GtkStatusbar" test-statusbar)
50 (:fn "Not working example of GtkEntryCompletion"
51 test-entry-completion)
52 (:fn "Simple test of non-GObject subclass of GtkWindow"
53 test-custom-window)
54 (:fn "Testing progress-bar" test-progress-bar)
55 (:fn "Simple test of GtkAssistant wizard" test-assistant)
56 (:fn "Using GtkImage with stock icon" test-image)
57 (:fn "Test of GtkCalendar" test-calendar)
58 (:fn "Test of GtkBuilder" test-builder)
59 (:fn "Test of GtkColorButton" test-color-button)
60 (:fn "Test of UI Markup" test-ui-markup)
61 (:fn "Test of scale button with icons" test-scale-button)
62 (:fn "Testing GtkComboBox" test-combo-box)
63 (:fn "Advanced demo: show s-expression tree structure"
64 demo-treeview-tree)
65 (:fn "Test of child-property usage" test-box-child-property)
66 (:fn "Demonstrates usage of list store" test-list-store)
67 (:fn "Test various gdk primitives" test-gdk)
68 (:fn "Test GtkNotebook" test-notebook)
69 (:fn "More advanced example: text editor with ability to evaluate lisp expressions"
70 demo-text-editor)
71 (:fn "(not completed)" test-pixbuf)
72 (:fn "Testing GtkTextEntry" test-entry)
73 (:fn "Test of treeview with CL-GTK2-GTK:ARRAY-LIST-STORE"
74 test-treeview-list)
75 (:fn "Test of GtkFileChooser" test-file-chooser)
76 (:fn "Test of GtkColorSelection" test-color-selection)
77 (:fn "Test of GtkTextView" test-text-view)
78 (:fn "A simple test of 'on-expose' event" test)
79 (:fn "Show slots of a given class" demo-class-browser)
80 (:fn "Testing GtkUIManager" test-ui-manager)
81 (:fn "GtkFontChooser" test-font-chooser)))
83 (defun clear-text-tag-table (table)
84 (let (tags)
85 (text-tag-table-foreach table
86 (lambda (tag)
87 (push tag tags)))
88 (iter (for tag in tags)
89 (text-tag-table-remove table tag))))
91 (defun fill-demo-text-buffer (buffer text-view &optional (page 'index))
92 (declare (ignorable text-view))
93 (clear-text-tag-table (text-buffer-tag-table buffer))
94 (setf (text-buffer-text buffer) "")
95 (text-tag-table-add (text-buffer-tag-table buffer) (make-instance 'text-tag :name "bold" :weight 700))
96 (labels ((insert-text (text)
97 (text-buffer-insert buffer text))
98 (insert-link (text fn)
99 (let ((offset (text-iter-offset (text-buffer-get-end-iter buffer))))
100 (text-buffer-insert buffer text)
101 (text-buffer-apply-tag buffer (make-link-fn-tag buffer fn)
102 (text-buffer-get-iter-at-offset buffer offset)
103 (text-buffer-get-end-iter buffer))))
104 (insert-newline ()
105 (text-buffer-insert buffer (format nil "~%")))
106 (process-paragraph (node)
107 (map nil #'process (rest node))
108 (insert-newline))
109 (process-link (node)
110 (insert-link (second node) (lambda () (fill-demo-text-buffer buffer text-view (third node)))))
111 (process-fn (node)
112 (insert-link (second node) (third node)))
113 (process-ul (node)
114 (iter (for n in (rest node))
115 (for i from 1)
116 (insert-text "* ")
117 (process n)
118 (insert-newline)))
119 (process-ol (node)
120 (iter (for n in (rest node))
121 (for i from 1)
122 (insert-text (format nil "~A. " i))
123 (process n)
124 (insert-newline)))
125 (process-bold (node)
126 (let ((offset (text-iter-offset (text-buffer-get-end-iter buffer))))
127 (map nil #'process (rest node))
128 (text-buffer-apply-tag buffer "bold" (text-buffer-get-iter-at-offset buffer offset) (text-buffer-get-end-iter buffer))))
129 (process (node)
130 (cond
131 ((stringp node) (insert-text node))
132 ((and (listp node) (eq (car node) :p)) (process-paragraph node))
133 ((and (listp node) (eq (car node) :link)) (process-link node))
134 ((and (listp node) (eq (car node) :fn)) (process-fn node))
135 ((and (listp node) (eq (car node) :ul)) (process-ul node))
136 ((and (listp node) (eq (car node) :ol)) (process-ol node))
137 ((and (listp node) (eq (car node) :b)) (process-bold node))
138 ((listp node) (map nil #'process node))
139 (t (error "Do not know how to proceed")))))
140 (process (get-page page))))
142 (defun make-demo-text-buffer (text-view)
143 (let ((buffer (make-instance 'text-buffer)))
144 (fill-demo-text-buffer buffer text-view)
145 buffer))
147 (defvar *active-tag* nil)
149 (defun tv-motion-notify (tv event)
150 (multiple-value-bind (x y)
151 (text-view-window-to-buffer-coords tv :text
152 (round (event-motion-x event)) (round (event-motion-y event)))
153 (let ((it (text-view-get-iter-at-location tv x y)))
154 (if it
155 (let ((tags (text-iter-tags it)))
156 (if tags
157 (loop
158 for tag in tags
159 when (typep tag 'link-text-tag)
160 do (progn
161 (when *active-tag*
162 (setf (text-tag-foreground *active-tag*) "blue"
163 *active-tag* nil))
164 (setf (gdk-window-cursor (text-view-get-window tv :text))
165 (cursor-new-for-display (drawable-display (text-view-get-window tv :text))
166 :hand2)
167 *active-tag* tag
168 (text-tag-foreground *active-tag*) "red")))
169 (progn
170 (setf (gdk-window-cursor (text-view-get-window tv :text)) nil)
171 (when *active-tag*
172 (setf (text-tag-foreground *active-tag*) "blue"
173 *active-tag* nil)))))
174 (progn
175 (setf (gdk-window-cursor (text-view-get-window tv :text)) nil)
176 (when *active-tag*
177 (setf (text-tag-foreground *active-tag*) "blue"
178 *active-tag* nil)))))))
180 (defun make-demo-text-view ()
181 (let ((tv (make-instance 'text-view :editable nil :cursor-visible nil :wrap-mode :word :pixels-below-lines 1 :left-margin 5 :right-margin 5)))
182 (setf (text-view-buffer tv)
183 (make-demo-text-buffer tv))
184 (connect-signal tv "motion-notify-event" #'tv-motion-notify)
185 tv))
187 (defun demo ()
188 (within-main-loop
189 (let-ui
190 (gtk-window
191 :var w
192 :title "Gtk+ demo for Lisp"
193 :window-position :center
194 :default-width 500
195 :default-height 500
196 (scrolled-window
197 :hscrollbar-policy :automatic
198 :vscrollbar-policy :automatic
199 (:expr (make-demo-text-view))))
200 (connect-signal w "destroy"
201 (lambda (w)
202 (declare (ignore w))
203 (leave-gtk-main)))
204 (widget-show w))))
206 (defun test ()
207 "A simple test of 'on-expose' event"
208 (within-main-loop
209 (let ((window (make-instance 'gtk-window :type :toplevel))
210 (area (make-instance 'drawing-area))
211 x y)
212 (container-add window area)
213 (connect-signal window "destroy" (lambda (widget)
214 (declare (ignore widget))
215 (leave-gtk-main)))
216 (connect-signal area "motion-notify-event"
217 (lambda (widget event)
218 (declare (ignore widget))
219 (setf x (event-motion-x event)
220 y (event-motion-y event))
221 (widget-queue-draw window)))
222 (connect-signal area "expose-event"
223 (lambda (widget event)
224 (declare (ignore widget event))
225 (let* ((gdk-window (widget-window area))
226 (gc (graphics-context-new gdk-window))
227 (layout (widget-create-pango-layout area (format nil "X: ~F~%Y: ~F" x y))))
228 (draw-layout gdk-window gc 0 0 layout)
229 (setf (graphics-context-rgb-fg-color gc) (make-color :red 65535 :green 0 :blue 0))
230 (multiple-value-bind (x y) (drawable-get-size gdk-window)
231 (draw-line gdk-window gc 0 0 x y)))))
232 (connect-signal area "realize"
233 (lambda (widget)
234 (declare (ignore widget))
235 (pushnew :pointer-motion-mask (gdk-window-events (widget-window area)))))
236 (connect-signal area "configure-event"
237 (lambda (widget event)
238 (declare (ignore widget event))
239 (widget-queue-draw area)))
240 (widget-show window))))
242 (defun test-entry ()
243 "Testing GtkTextEntry"
244 (within-main-loop
245 (let* ((window (make-instance 'gtk-window :type :toplevel :title "Testing entry" :border-width 10))
246 (box (make-instance 'v-box))
247 (entry (make-instance 'entry))
248 (button (make-instance 'button :label "OK"))
249 (text-buffer (make-instance 'text-buffer))
250 (text-view (make-instance 'text-view :buffer text-buffer))
251 (button-select (make-instance 'button :label "Select"))
252 (button-insert (make-instance 'button :label "Insert")))
253 (box-pack-start box (make-instance 'label :label "Enter <b>anything</b> you wish:" :use-markup t) :expand nil)
254 (box-pack-start box entry :expand nil)
255 (box-pack-start box button :expand nil)
256 (box-pack-start box button-select :expand nil)
257 (box-pack-start box button-insert :expand nil)
258 (let* ((w (make-instance 'scrolled-window)))
259 (box-pack-start box w)
260 (container-add w text-view))
261 (container-add window box)
262 (connect-signal window "destroy" (lambda (widget) (declare (ignore widget)) (leave-gtk-main)))
263 (connect-signal window "delete-event" (lambda (widget event)
264 (declare (ignore widget event))
265 (let ((dlg (make-instance 'message-dialog
266 :text "Are you sure?"
267 :buttons :yes-no)))
268 (let ((response (dialog-run dlg)))
269 (object-destroy dlg)
270 (not (eq :yes response))))))
271 (connect-signal button "clicked" (lambda (button)
272 (declare (ignore button))
273 (setf (text-buffer-text text-buffer)
274 (format nil "~A~%~A" (text-buffer-text text-buffer) (entry-text entry))
275 (entry-text entry) "")))
276 (connect-signal button-select "clicked" (lambda (button)
277 (declare (ignore button))
278 (editable-select-region entry 5 10)))
279 (connect-signal button-insert "clicked" (lambda (button)
280 (declare (ignore button))
281 (editable-insert-text entry "hello" 2)))
282 (widget-show window))))
284 (defun table-packing ()
285 "Simple test of packing widgets into GtkTable"
286 (within-main-loop
287 (let* ((window (make-instance 'gtk-window :type :toplevel :title "Table packing" :border-width 20))
288 (table (make-instance 'table :n-rows 2 :n-columns 2 :homogeneous t))
289 (button-1 (make-instance 'button :label "Button 1"))
290 (button-2 (make-instance 'button :label "Button 2"))
291 (button-q (make-instance 'button :label "Quit")))
292 (container-add window table)
293 (table-attach table button-1 0 1 0 1)
294 (table-attach table button-2 1 2 0 1)
295 (table-attach table button-q 0 2 1 2)
296 (connect-signal window "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
297 (connect-signal button-q "clicked" (lambda (b) (declare (ignore b)) (object-destroy window)))
298 (widget-show window))))
300 (defun test-pixbuf ()
301 "(not completed)"
302 (within-main-loop
303 (let* ((window (make-instance 'gtk-window :title "Test pixbuf" :width-request 600 :height-request 240))
304 (vbox (make-instance 'v-box))
305 (eventbox (make-instance 'event-box))
306 (vbox-1 (make-instance 'v-box)))
307 (container-add window vbox)
308 (box-pack-start vbox (make-instance 'label :text "Placing bg image" :font "Times New Roman Italic 10" :color "#00f" :height-request 40))
309 (connect-signal window "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
310 (box-pack-start vbox eventbox)
311 (container-add eventbox vbox-1)
312 (box-pack-start vbox-1 (make-instance 'label :text "This is the eventbox"))
313 (box-pack-start vbox-1 (make-instance 'label :text "The green ball is the bg"))
314 (widget-show window))))
316 (defun test-image ()
317 "Using GtkImage with stock icon"
318 (within-main-loop
319 (let* ((window (make-instance 'gtk-window :title "Test images"))
320 (image (make-instance 'image :icon-name "applications-development" :icon-size 6)))
321 (container-add window image)
322 (connect-signal window "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
323 (widget-show window))))
325 (defun test-progress-bar ()
326 "Testing progress-bar"
327 (within-main-loop
328 (let* ((window (make-instance 'gtk-window :title "Test progress bar"))
329 (v-box (make-instance 'v-box))
330 (p-bar (make-instance 'progress-bar :test "A process"))
331 (button-pulse (make-instance 'button :label "Pulse"))
332 (button-set (make-instance 'button :label "Set"))
333 (entry (make-instance 'entry)))
334 (connect-signal window "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
335 (container-add window v-box)
336 (box-pack-start v-box p-bar)
337 (box-pack-start v-box button-pulse)
338 (box-pack-start v-box button-set)
339 (box-pack-start v-box entry)
340 (connect-signal button-pulse "clicked" (lambda (w) (declare (ignore w)) (progress-bar-pulse p-bar)))
341 (connect-signal button-set "clicked" (lambda (w)
342 (declare (ignore w))
343 (setf (progress-bar-fraction p-bar)
344 (coerce (read-from-string (entry-text entry)) 'real))))
345 (widget-show window))))
347 (defun test-statusbar ()
348 "Test of GtkStatusbar"
349 (within-main-loop
350 (let* ((window (make-instance 'gtk-window :title "Text status bar"))
351 (v-box (make-instance 'v-box))
352 (h-box (make-instance 'h-box))
353 (label (make-instance 'label :label "Test of status bar" :xalign 0.5 :yalign 0.5))
354 (statusbar (make-instance 'statusbar :has-resize-grip t))
355 (button-push (make-instance 'button :label "Push"))
356 (button-pop (make-instance 'button :label "Pop"))
357 (entry (make-instance 'entry))
358 (icon (make-instance 'status-icon :icon-name "applications-development")))
359 (set-status-icon-tooltip icon "An icon from lisp program")
360 (connect-signal window "destroy" (lambda (w)
361 (declare (ignore w))
362 #+ (or) (setf (status-icon-visible icon) nil)
363 (leave-gtk-main)))
364 (connect-signal button-push "clicked" (lambda (b)
365 (declare (ignore b))
366 (statusbar-push statusbar "lisp-prog" (entry-text entry))))
367 (connect-signal button-pop "clicked" (lambda (b)
368 (declare (ignore b))
369 (statusbar-pop statusbar "lisp-prog")))
370 (connect-signal icon "activate" (lambda (i)
371 (declare (ignore i))
372 (let ((message-dialog (make-instance 'message-dialog
373 :buttons :ok
374 :text "You clicked on icon!")))
375 (dialog-run message-dialog)
376 (object-destroy message-dialog))))
377 (container-add window v-box)
378 (box-pack-start v-box h-box :expand nil)
379 (box-pack-start h-box entry)
380 (box-pack-start h-box button-push :expand nil)
381 (box-pack-start h-box button-pop :expand nil)
382 (box-pack-start v-box label)
383 (box-pack-start v-box statusbar :expand nil)
384 (widget-show window)
385 (setf (status-icon-screen icon) (gtk-window-screen window)))))
387 (defun test-scale-button ()
388 "Test of scale button with icons"
389 (within-main-loop
390 (let* ((window (make-instance 'gtk-window :type :toplevel :title "Testing scale button"))
391 (button (make-instance 'scale-button :icons (list "media-seek-backward" "media-seek-forward" "media-playback-stop" "media-playback-start") :adjustment (make-instance 'adjustment :lower -40 :upper 50 :value 20))))
392 (connect-signal window "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
393 (container-add window button)
394 (widget-show window))))
396 (defun test-text-view ()
397 "Test of GtkTextView"
398 (within-main-loop
399 (let* ((window (make-instance 'gtk-window :type :toplevel :title "Testing text view" :width-request 400 :height-request 300))
400 (button (make-instance 'button :label "Do"))
401 (button-insert (make-instance 'button :label "Insert a button!"))
402 (bold-btn (make-instance 'button :label "Bold"))
403 (buffer (make-instance 'text-buffer :text "Some text buffer with some text inside"))
404 (v (make-instance 'text-view :buffer buffer :wrap-mode :word))
405 (box (make-instance 'v-box))
406 (scrolled (make-instance 'scrolled-window :hscrollbar-policy :automatic :vscrollbar-policy :automatic)))
407 (connect-signal window "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
408 (connect-signal button "clicked" (lambda (b)
409 (declare (ignore b))
410 (multiple-value-bind (i1 i2) (text-buffer-get-selection-bounds buffer)
411 (when (and i1 i2)
412 (let* ((i1 i1) (i2 i2)
413 (dialog (make-instance 'message-dialog :buttons :ok)))
414 (setf (message-dialog-text dialog)
415 (format nil "selection: from (~A,~A) to (~A,~A)"
416 (text-iter-line i1) (text-iter-line-offset i1)
417 (text-iter-line i2) (text-iter-line-offset i2)))
418 (dialog-run dialog)
419 (object-destroy dialog))))))
420 (connect-signal bold-btn "clicked" (Lambda (b)
421 (declare (ignore b))
422 (multiple-value-bind (start end) (text-buffer-get-selection-bounds buffer)
423 (when (and start end)
424 (let* ((start start)
425 (end end)
426 (tag (text-tag-table-lookup (text-buffer-tag-table buffer) "bold")))
427 (if (text-iter-has-tag start tag)
428 (text-buffer-remove-tag buffer tag start end)
429 (text-buffer-apply-tag buffer tag start end)))))))
430 (connect-signal button-insert "clicked" (lambda (b)
431 (declare (ignore b))
432 (let* ((iter (text-buffer-get-iter-at-mark buffer (text-buffer-get-mark buffer "insert")))
433 (anchor (text-buffer-insert-child-anchor buffer iter))
434 (button (make-instance 'button :label "A button!")))
435 (widget-show button)
436 (text-view-add-child-at-anchor v button anchor))))
437 (let ((tag (make-instance 'text-tag :name "bold" :weight 700)))
438 (text-tag-table-add (text-buffer-tag-table buffer) tag)
439 (connect-signal tag "event"
440 (lambda (tag object event iter)
441 (declare (ignore tag object iter))
442 (when (eq (event-type event) :button-release)
443 (let ((dlg (make-instance 'message-dialog :text "You clicked on bold text." :buttons :ok)))
444 (dialog-run dlg)
445 (object-destroy dlg))))))
446 (container-add window box)
447 (container-add scrolled v)
448 (box-pack-start box button :expand nil)
449 (box-pack-start box button-insert :expand nil)
450 (box-pack-start box bold-btn :expand nil)
451 (box-pack-start box scrolled)
452 (widget-show window))))
454 (defun demo-code-editor ()
455 "(unfinished)"
456 (within-main-loop
457 (let* ((window (make-instance 'gtk-window :type :toplevel :title "Code editor" :width-request 400 :height-request 400 :window-position :center))
458 (scrolled (make-instance 'scrolled-window :hscrollbar-policy :automatic :vscrollbar-policy :automatic))
459 (buffer (make-instance 'text-buffer))
460 (view (make-instance 'text-view :buffer buffer)))
461 (connect-signal window "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
462 (container-add window scrolled)
463 (container-add scrolled view)
464 (widget-show window)
465 (connect-signal buffer "insert-text" (lambda (buffer location text len)
466 (let* ((buffer buffer)
467 (location location))
468 (format t "~A~%" (list buffer location text len))))))))
470 (defstruct tvi title value)
472 (defun test-treeview-list ()
473 "Test of treeview with CL-GTK2-GTK:ARRAY-LIST-STORE"
474 (within-main-loop
475 (let* ((window (make-instance 'gtk-window :type :toplevel :title "Treeview (list)"))
476 (model (make-instance 'array-list-store))
477 (scroll (make-instance 'scrolled-window :hscrollbar-policy :automatic :vscrollbar-policy :automatic))
478 (tv (make-instance 'tree-view :headers-visible t :width-request 100 :height-request 400 :rules-hint t))
479 (h-box (make-instance 'h-box))
480 (v-box (make-instance 'v-box))
481 (title-entry (make-instance 'entry))
482 (value-entry (make-instance 'entry))
483 (button (make-instance 'button :label "Add")))
484 (store-add-column model "gchararray" #'tvi-title)
485 (store-add-column model "gint" #'tvi-value)
486 (store-add-item model (make-tvi :title "Monday" :value 1))
487 (store-add-item model (make-tvi :title "Tuesday" :value 2))
488 (store-add-item model (make-tvi :title "Wednesday" :value 3))
489 (store-add-item model (make-tvi :title "Thursday" :value 4))
490 (store-add-item model (make-tvi :title "Friday" :value 5))
491 (store-add-item model (make-tvi :title "Saturday" :value 6))
492 (store-add-item model (make-tvi :title "Sunday" :value 7))
493 (setf (tree-view-model tv) model (tree-view-tooltip-column tv) 0)
494 (gobject:connect-signal window "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
495 (gobject:connect-signal button "clicked" (lambda (b)
496 (declare (ignore b))
497 (store-add-item model (make-tvi :title (entry-text title-entry)
498 :value (or (parse-integer (entry-text value-entry)
499 :junk-allowed t)
500 0)))))
501 (connect-signal tv "row-activated" (lambda (tv path column)
502 (declare (ignore tv column))
503 (show-message (format nil "You clicked on row ~A" (tree-path-indices path)))))
504 (container-add window v-box)
505 (box-pack-start v-box h-box :expand nil)
506 (box-pack-start h-box title-entry :expand nil)
507 (box-pack-start h-box value-entry :expand nil)
508 (box-pack-start h-box button :expand nil)
509 (box-pack-start v-box scroll)
510 (container-add scroll tv)
511 (let ((column (make-instance 'tree-view-column :title "Title" :sort-column-id 0))
512 (renderer (make-instance 'cell-renderer-text :text "A text")))
513 (tree-view-column-pack-start column renderer)
514 (tree-view-column-add-attribute column renderer "text" 0)
515 (tree-view-append-column tv column)
516 (print (tree-view-column-tree-view column))
517 (print (tree-view-column-cell-renderers column)))
518 (let ((column (make-instance 'tree-view-column :title "Value"))
519 (renderer (make-instance 'cell-renderer-text :text "A text")))
520 (tree-view-column-pack-start column renderer)
521 (tree-view-column-add-attribute column renderer "text" 1)
522 (tree-view-append-column tv column)
523 (print (tree-view-column-tree-view column))
524 (print (tree-view-column-cell-renderers column)))
525 (widget-show window))))
527 (defun test-combo-box ()
528 "Testing GtkComboBox"
529 (within-main-loop
530 (let* ((window (make-instance 'gtk-window :type :toplevel :title "Treeview (list)"))
531 (model (make-instance 'array-list-store))
532 (combo-box (make-instance 'combo-box :model model))
533 (h-box (make-instance 'h-box))
534 (v-box (make-instance 'v-box))
535 (title-entry (make-instance 'entry))
536 (value-entry (make-instance 'entry))
537 (button (make-instance 'button :label "Add")))
538 (store-add-column model "gchararray" #'tvi-title)
539 (store-add-column model "gint" #'tvi-value)
540 (store-add-item model (make-tvi :title "Monday" :value 1))
541 (store-add-item model (make-tvi :title "Tuesday" :value 2))
542 (store-add-item model (make-tvi :title "Wednesday" :value 3))
543 (store-add-item model (make-tvi :title "Thursday" :value 4))
544 (store-add-item model (make-tvi :title "Friday" :value 5))
545 (store-add-item model (make-tvi :title "Saturday" :value 6))
546 (store-add-item model (make-tvi :title "Sunday" :value 7))
547 (gobject:connect-signal window "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
548 (gobject:connect-signal button "clicked" (lambda (b)
549 (declare (ignore b))
550 (store-add-item model (make-tvi :title (entry-text title-entry)
551 :value (or (parse-integer (entry-text value-entry)
552 :junk-allowed t)
553 0)))))
554 (connect-signal combo-box "changed" (lambda (c)
555 (declare (ignore c))
556 (show-message (format nil "You clicked on row ~A~%" (combo-box-active combo-box)))))
557 (container-add window v-box)
558 (box-pack-start v-box h-box :expand nil)
559 (box-pack-start h-box title-entry :expand nil)
560 (box-pack-start h-box value-entry :expand nil)
561 (box-pack-start h-box button :expand nil)
562 (box-pack-start v-box combo-box)
563 (let ((renderer (make-instance 'cell-renderer-text :text "A text")))
564 (cell-layout-pack-start combo-box renderer :expand t)
565 (cell-layout-add-attribute combo-box renderer "text" 0))
566 (let ((renderer (make-instance 'cell-renderer-text :text "A number")))
567 (cell-layout-pack-start combo-box renderer :expand nil)
568 (cell-layout-add-attribute combo-box renderer "text" 1))
569 (widget-show window))))
571 (defun test-ui-manager ()
572 "Testing GtkUIManager"
573 (within-main-loop
574 (let* ((window (make-instance 'gtk-window :type :toplevel :title "UI Manager" :default-width 200 :default-height 100 :window-position :center))
575 (ui-manager (make-instance 'ui-manager))
576 (print-confirmation t))
577 (ui-manager-add-ui-from-string ui-manager
579 <ui>
580 <toolbar action='toolbar1'>
581 <separator/>
582 <toolitem name='Left' action='justify-left'/>
583 <toolitem name='Center' action='justify-center'/>
584 <toolitem name='Right' action='justify-right'/>
585 <toolitem name='Zoom in' action='zoom-in' />
586 <toolitem name='print-confirm' action='print-confirm' />
587 <separator/>
588 </toolbar>
589 </ui>")
590 (gobject:connect-signal window "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
591 (iter (with fn = (lambda (action) (when print-confirmation (format t "Action ~A with name ~A activated~%" action (action-name action)))))
592 (with action-group = (make-instance 'action-group :name "Actions"))
593 (finally (let ((a (make-instance 'toggle-action :name "print-confirm" :label "Print" :stock-id "gtk-print-report" :active t)))
594 (connect-signal a "toggled" (lambda (action) (setf print-confirmation (toggle-action-active action))))
595 (action-group-add-action action-group a))
596 (ui-manager-insert-action-group ui-manager action-group 0))
597 (for (name stock-id) in '(("justify-left" "gtk-justify-left")
598 ("justify-center" "gtk-justify-center")
599 ("justify-right" "gtk-justify-right")
600 ("zoom-in" "gtk-zoom-in")))
601 (for action = (make-instance 'action :name name :stock-id stock-id))
602 (connect-signal action "activate" fn)
603 (action-group-add-action action-group action))
604 (let ((widget (ui-manager-widget ui-manager "/toolbar1")))
605 (when widget
606 (container-add window widget)))
607 (widget-show window))))
609 (defun test-color-button ()
610 "Test of GtkColorButton"
611 (within-main-loop
612 (let ((window (make-instance 'gtk-window :title "Color button" :type :toplevel :window-position :center :width-request 100 :height-request 100))
613 (button (make-instance 'color-button :title "Color button")))
614 (connect-signal window "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
615 (connect-signal button "color-set" (lambda (b)
616 (declare (ignore b))
617 (show-message (format nil "Chose color ~A" (color-button-color button)))))
618 (container-add window button)
619 (widget-show window))))
621 (defun test-color-selection ()
622 "Test of GtkColorSelection"
623 (within-main-loop
624 (let ((window (make-instance 'gtk-window :title "Color selection" :type :toplevel :window-position :center))
625 (selection (make-instance 'color-selection :has-opacity-control t)))
626 (connect-signal window "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
627 (connect-signal selection "color-changed" (lambda (s) (declare (ignore s)) (unless (color-selection-adjusting-p selection) (format t "color: ~A~%" (color-selection-current-color selection)))))
628 (container-add window selection)
629 (widget-show window))))
631 (defun test-file-chooser ()
632 "Test of GtkFileChooser"
633 (within-main-loop
634 (let ((window (make-instance 'gtk-window :title "file chooser" :type :toplevel :window-position :center :default-width 100 :default-height 100))
635 (v-box (make-instance 'v-box))
636 (button (make-instance 'file-chooser-button :action :open))
637 (b (make-instance 'button :label "Choose for save" :stock-id "gtk-save")))
638 (connect-signal window "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
639 (connect-signal button "file-set" (lambda (b) (declare (ignore b)) (format t "File set: ~A~%" (file-chooser-filename button))))
640 (connect-signal b "clicked" (lambda (b)
641 (declare (ignore b))
642 (let ((d (make-instance 'file-chooser-dialog :action :save :title "Choose file to save")))
643 (dialog-add-button d "gtk-save" :accept)
644 (dialog-add-button d "gtk-cancel" :cancel)
645 (when (eq (dialog-run d) :accept)
646 (format t "saved to file ~A~%" (file-chooser-filename d)))
647 (object-destroy d))))
648 (container-add window v-box)
649 (box-pack-start v-box button)
650 (box-pack-start v-box b)
651 (widget-show window))))
653 (defun test-font-chooser ()
654 "GtkFontChooser"
655 (within-main-loop
656 (let ((window (make-instance 'gtk-window :title "fonts" :type :toplevel :window-position :center :default-width 100 :default-height 100))
657 (v-box (make-instance 'v-box))
658 (button (make-instance 'font-button :title "Choose font" :font-name "Sans 10")))
659 (connect-signal window "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
660 (connect-signal button "font-set" (lambda (b) (declare (ignore b)) (format t "Chose font ~A~%" (font-button-font-name button))))
661 (container-add window v-box)
662 (box-pack-start v-box button)
663 (widget-show window))))
665 (defun test-notebook ()
666 "Test GtkNotebook"
667 (within-main-loop
668 (let ((window (make-instance 'gtk-window :title "Notebook" :type :toplevel :window-position :center :default-width 100 :default-height 100))
669 (expander (make-instance 'expander :expanded t :label "notebook"))
670 (notebook (make-instance 'notebook :enable-popup t)))
671 (connect-signal window "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
672 (iter (for i from 0 to 5)
673 (for page = (make-instance 'label :label (format nil "Label for page ~A" i)))
674 (for tab-label = (make-instance 'label :label (format nil "Tab ~A" i)))
675 (for tab-button = (make-instance 'button
676 :image (make-instance 'image :stock "gtk-close" :icon-size 1)
677 :relief :none))
678 (connect-signal tab-button "clicked"
679 (let ((page page))
680 (lambda (button)
681 (declare (ignore button))
682 (format t "Removing page ~A~%" page)
683 (notebook-remove-page notebook page))))
684 (for tab-hbox = (make-instance 'h-box))
685 (box-pack-start tab-hbox tab-label)
686 (box-pack-start tab-hbox tab-button)
687 (widget-show tab-hbox)
688 (notebook-add-page notebook page tab-hbox))
689 (container-add window expander)
690 (container-add expander notebook)
691 (widget-show window))))
693 (defun calendar-detail (calendar year month day)
694 (declare (ignore calendar year month))
695 (when (= day 23)
696 "!"))
698 (defun test-calendar ()
699 "Test of GtkCalendar"
700 (within-main-loop
701 (let ((window (make-instance 'gtk-window :title "Calendar" :type :toplevel :window-position :center :default-width 100 :default-height 100))
702 (calendar (make-instance 'calendar :detail-function #'calendar-detail)))
703 (connect-signal window "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
704 (connect-signal calendar "day-selected" (lambda (c) (declare (ignore c)) (format t "selected: year ~A month ~A day ~A~%"
705 (calendar-year calendar)
706 (calendar-month calendar)
707 (calendar-day calendar))))
708 (container-add window calendar)
709 (widget-show window))))
711 (defun test-box-child-property ()
712 "Test of child-property usage"
713 (within-main-loop
714 (let ((window (make-instance 'gtk-window :title "Text box child property" :type :toplevel :window-position :center :width-request 200 :height-request 200))
715 (box (make-instance 'h-box))
716 (button (make-instance 'toggle-button :active t :label "Expand")))
717 (connect-signal window "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
718 (connect-signal button "toggled" (lambda (b) (declare (ignore b)) (setf (box-child-expand box button) (toggle-button-active button))))
719 (container-add window box)
720 (box-pack-start box button)
721 (widget-show window))))
723 (defun test-builder ()
724 "Test of GtkBuilder"
725 (within-main-loop
726 (let ((builder (make-instance 'builder)))
727 (builder-add-from-file builder (namestring (merge-pathnames "demo/demo1.ui" *src-location*)))
728 (let ((text-view (builder-get-object builder "textview1"))
729 (c 0))
730 (builder-connect-signals-simple builder `(("toolbutton1_clicked_cb" ,(lambda (b)
731 (declare (ignore b))
732 #+nil(print (current-event))
733 (setf (text-buffer-text (text-view-buffer text-view))
734 (format nil "Clicked ~A times~%" (incf c)))
735 (statusbar-pop (builder-get-object builder "statusbar1")
736 "times")
737 (statusbar-push (builder-get-object builder "statusbar1")
738 "times"
739 (format nil "~A times" c))))
740 ("quit_cb" ,(lambda (&rest args)
741 (print args)
742 (object-destroy (builder-get-object builder "window1"))))
743 ("about_cb" ,(lambda (&rest args)
744 (print args)
745 (let ((d (make-instance 'about-dialog
746 :program-name "GtkBuilder text"
747 :version "0.00001"
748 :authors '("Dmitry Kalyanov")
749 :logo-icon-name "gtk-apply")))
750 (dialog-run d)
751 (object-destroy d)))))))
752 (connect-signal (builder-get-object builder "window1") "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
753 (statusbar-push (builder-get-object builder "statusbar1") "times" "0 times")
754 (widget-show (builder-get-object builder "window1")))))
756 (defun read-text-file (file-name)
757 (with-output-to-string (str)
758 (with-open-file (file file-name)
759 (loop
760 for line = (read-line file nil nil)
761 while line
762 do (fresh-line str)
763 do (write-string line str)))))
765 (defun demo-text-editor ()
766 "More advanced example: text editor with ability to evaluate lisp expressions"
767 (within-main-loop
768 (let* ((builder (let ((builder (make-instance 'builder)))
769 (builder-add-from-file builder (namestring (merge-pathnames "demo/text-editor.ui" *src-location*)))
770 builder))
771 (window (builder-get-object builder "window1"))
772 (text-view (builder-get-object builder "textview1"))
773 (statusbar (builder-get-object builder "statusbar1"))
774 (file-name nil)
775 (modified-p t))
776 (statusbar-push statusbar "filename" "Untitled *")
777 (labels ((set-properties ()
778 (statusbar-pop statusbar "filename")
779 (statusbar-push statusbar "filename" (format nil "~A~:[~; *~]" (or file-name "Untitled") modified-p)))
780 (new (&rest args) (declare (ignore args))
781 (setf file-name nil
782 modified-p t
783 (text-buffer-text (text-view-buffer text-view)) "")
784 (set-properties))
785 (cb-open (&rest args) (declare (ignore args))
786 (let ((d (make-instance 'file-chooser-dialog :action :open :title "Open file")))
787 (when file-name (setf (file-chooser-filename d) file-name))
788 (dialog-add-button d "gtk-open" :accept)
789 (dialog-add-button d "gtk-cancel" :cancel)
790 (when (eq :accept (dialog-run d))
791 (setf file-name (file-chooser-filename d)
792 (text-buffer-text (text-view-buffer text-view)) (read-text-file file-name)
793 modified-p nil)
794 (set-properties))
795 (object-destroy d)))
796 (save (&rest args) (declare (ignore args))
797 (if file-name
798 (progn
799 (with-open-file (file file-name :direction :output :if-exists :supersede)
800 (write-string (text-buffer-text (text-view-buffer text-view)) file))
801 (setf modified-p nil)
802 (set-properties))
803 (save-as)))
804 (save-as (&rest args) (declare (ignore args))
805 (let ((d (make-instance 'file-chooser-dialog :action :save :title "Save file")))
806 (when file-name (setf (file-chooser-filename d) file-name))
807 (dialog-add-button d "gtk-save" :accept)
808 (dialog-add-button d "gtk-cancel" :cancel)
809 (if (eq :accept (dialog-run d))
810 (progn
811 (setf file-name (file-chooser-filename d))
812 (object-destroy d)
813 (save))
814 (object-destroy d))))
815 (cut (&rest args) (declare (ignore args))
816 (text-buffer-cut-clipboard (text-view-buffer text-view) (get-clipboard "CLIPBOARD") t))
817 (copy (&rest args) (declare (ignore args))
818 (text-buffer-copy-clipboard (text-view-buffer text-view) (get-clipboard "CLIPBOARD")))
819 (paste (&rest args) (declare (ignore args))
820 (text-buffer-paste-clipboard (text-view-buffer text-view) (get-clipboard "CLIPBOARD")))
821 (cb-delete (&rest args) (declare (ignore args))
822 (let ((buffer (text-view-buffer text-view)))
823 (multiple-value-bind (i1 i2) (text-buffer-get-selection-bounds buffer)
824 (when (and i1 i2)
825 (text-buffer-delete buffer i1 i2)))))
826 (about (&rest args) (declare (ignore args))
827 (let ((d (make-instance 'about-dialog
828 :program-name "Lisp Gtk+ Binding Demo Text Editor"
829 :version (format nil "0.0.0.1 ~A" #\GREEK_SMALL_LETTER_ALPHA)
830 :authors '("Kalyanov Dmitry")
831 :license "Public Domain"
832 :logo-icon-name "accessories-text-editor")))
833 (dialog-run d)
834 (object-destroy d)))
835 (quit (&rest args) (declare (ignore args)) (object-destroy window))
836 (cb-eval (&rest args) (declare (ignore args))
837 (let ((buffer (text-view-buffer text-view)))
838 (multiple-value-bind (i1 i2) (text-buffer-get-selection-bounds buffer)
839 (when (and i1 i2)
840 (with-gtk-message-error-handler
841 (let* ((text (text-buffer-slice buffer i1 i2))
842 (value (eval (read-from-string text)))
843 (value-str (format nil "~A" value))
844 (pos (max (text-iter-offset i1) (text-iter-offset i2))))
845 (text-buffer-insert buffer " => " :position (text-buffer-get-iter-at-offset buffer pos))
846 (incf pos (length " => "))
847 (text-buffer-insert buffer value-str :position (text-buffer-get-iter-at-offset buffer pos)))))))))
848 (builder-connect-signals-simple builder `(("new" ,#'new)
849 ("open" ,#'cb-open)
850 ("save" ,#'save)
851 ("save-as" ,#'save-as)
852 ("cut" ,#'cut)
853 ("copy" ,#'copy)
854 ("paste" ,#'paste)
855 ("delete" ,#'cb-delete)
856 ("about" ,#'about)
857 ("quit" ,#'quit)
858 ("eval" ,#'cb-eval)))
859 (connect-signal window "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
860 (connect-signal (text-view-buffer text-view) "changed" (lambda (b) (declare (ignore b)) (setf modified-p t) (set-properties)))
861 (widget-show window)))))
863 (defun demo-class-browser ()
864 "Show slots of a given class"
865 (let ((output *standard-output*))
866 (within-main-loop
867 (let* ((window (make-instance 'gtk-window
868 :window-position :center
869 :title "Class Browser"
870 :default-width 400
871 :default-height 600))
872 (search-entry (make-instance 'entry))
873 (search-button (make-instance 'button :label "Search"))
874 (scroll (make-instance 'scrolled-window
875 :hscrollbar-policy :automatic
876 :vscrollbar-policy :automatic))
877 (slots-model (make-instance 'array-list-store))
878 (slots-list (make-instance 'tree-view :model slots-model)))
879 (let ((v-box (make-instance 'v-box))
880 (search-box (make-instance 'h-box)))
881 (container-add window v-box)
882 (box-pack-start v-box search-box :expand nil)
883 (box-pack-start search-box search-entry)
884 (box-pack-start search-box search-button :expand nil)
885 (box-pack-start v-box scroll)
886 (container-add scroll slots-list))
887 (store-add-column slots-model "gchararray"
888 (lambda (slot)
889 (format nil "~S" (closer-mop:slot-definition-name slot))))
890 (let ((col (make-instance 'tree-view-column :title "Slot name"))
891 (cr (make-instance 'cell-renderer-text)))
892 (tree-view-column-pack-start col cr)
893 (tree-view-column-add-attribute col cr "text" 0)
894 (tree-view-append-column slots-list col))
895 (labels ((display-class-slots (class)
896 (format output "Displaying ~A~%" class)
897 (loop
898 repeat (store-items-count slots-model)
899 do (store-remove-item slots-model (store-item slots-model 0)))
900 (closer-mop:finalize-inheritance class)
901 (loop
902 for slot in (closer-mop:class-slots class)
903 do (store-add-item slots-model slot)))
904 (on-search-clicked (button)
905 (declare (ignore button))
906 (with-gtk-message-error-handler
907 (let* ((class-name (read-from-string (entry-text search-entry)))
908 (class (find-class class-name)))
909 (display-class-slots class)))))
910 (connect-signal search-button "clicked" #'on-search-clicked))
911 (connect-signal window "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
912 (widget-show window)))))
914 (defun make-tree-from-sexp (l)
915 (setf l (if (listp l) l (list l)))
916 (let ((node (make-tree-node :item (make-tvi :title (format nil "~S" (first l))
917 :value (format nil "~S" (class-of (first l)))))))
918 (iter (for child in (rest l))
919 (tree-node-insert-at node (make-tree-from-sexp child) (length (tree-node-children node))))
920 node))
922 (defun demo-treeview-tree ()
923 "Advanced demo: show s-expression tree structure"
924 (within-main-loop
925 (let* ((window (make-instance 'gtk-window :type :toplevel :title "Treeview (tree)"))
926 (model (make-instance 'tree-lisp-store))
927 (scroll (make-instance 'scrolled-window :hscrollbar-policy :automatic :vscrollbar-policy :automatic))
928 (tree-view (make-instance 'tree-view :headers-visible t :width-request 300 :height-request 400 :rules-hint t))
929 (h-box (make-instance 'h-box))
930 (v-box (make-instance 'v-box))
931 (entry (make-instance 'entry))
932 (button (make-instance 'button :label "Display")))
933 (tree-lisp-store-add-column model "gchararray" #'tvi-title)
934 (tree-lisp-store-add-column model "gchararray" #'tvi-value)
935 (tree-node-insert-at (tree-lisp-store-root model)
936 (make-tree-from-sexp '(lambda (object &rest initargs &key &allow-other-keys)
937 (* 1 2)
938 (- 3 4)))
940 (setf (tree-view-model tree-view) model
941 (tree-view-tooltip-column tree-view) 0)
942 (connect-signal tree-view "row-activated" (lambda (tv path column)
943 (declare (ignore tv column))
944 (show-message (format nil "You clicked on row ~A" (tree-path-indices path)))))
945 (connect-signal button "clicked" (lambda (b)
946 (declare (ignore b))
947 (let ((object (read-from-string (entry-text entry))))
948 (tree-node-remove-at (tree-lisp-store-root model) 0)
949 (tree-node-insert-at (tree-lisp-store-root model)
950 (make-tree-from-sexp object)
951 0))))
952 (container-add window v-box)
953 (box-pack-start v-box h-box :expand nil)
954 (box-pack-start h-box entry)
955 (box-pack-start h-box button :expand nil)
956 (box-pack-start v-box scroll)
957 (container-add scroll tree-view)
958 (let ((column (make-instance 'tree-view-column :title "Value" :sort-column-id 0))
959 (renderer (make-instance 'cell-renderer-text :text "A text")))
960 (tree-view-column-pack-start column renderer)
961 (tree-view-column-add-attribute column renderer "text" 0)
962 (tree-view-append-column tree-view column)
963 (print (tree-view-column-tree-view column))
964 (print (tree-view-column-cell-renderers column)))
965 (let ((column (make-instance 'tree-view-column :title "Type"))
966 (renderer (make-instance 'cell-renderer-text :text "A text")))
967 (tree-view-column-pack-start column renderer)
968 (tree-view-column-add-attribute column renderer "text" 1)
969 (tree-view-append-column tree-view column)
970 (print (tree-view-column-tree-view column))
971 (print (tree-view-column-cell-renderers column)))
972 (connect-signal window "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
973 (widget-show window))))
975 (defclass custom-window (gtk-window)
976 ((label :initform (make-instance 'label :label "A label text") :reader custom-window-label)
977 (button :initform (make-instance 'button :label "Click me!") :reader custom-window-button))
978 (:metaclass gobject-class)
979 (:default-initargs :title "Custom window with default initargs" :default-width 320 :default-height 240))
981 (defun custom-window-label-text (w)
982 (label-label (custom-window-label w)))
984 (defun (setf custom-window-label-text) (new-value w)
985 (setf (label-label (custom-window-label w)) new-value))
987 (defmethod initialize-instance :after ((w custom-window) &key &allow-other-keys)
988 (let ((box (make-instance 'v-box)))
989 (box-pack-start box (custom-window-label w))
990 (box-pack-start box (custom-window-button w) :expand nil)
991 (container-add w box))
992 (connect-signal (custom-window-button w) "clicked" (lambda (b)
993 (declare (ignore b))
994 (custom-window-button-clicked w))))
996 (defun custom-window-button-clicked (w)
997 (setf (custom-window-label-text w)
998 (format nil "Now is: ~A~%" (get-internal-run-time))))
1000 (defun test-custom-window ()
1001 "Simple test of non-GObject subclass of GtkWindow"
1002 (within-main-loop
1003 (let ((w (make-instance 'custom-window)))
1004 (connect-signal w "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
1005 (widget-show w))))
1007 (defun test-assistant ()
1008 "Simple test of GtkAssistant wizard"
1009 (let ((output *standard-output*))
1010 (within-main-loop
1011 (let ((d (make-instance 'assistant :title "Username wizard"))
1012 (p-1 (make-instance 'h-box))
1013 (entry (make-instance 'entry))
1014 (p-2 (make-instance 'label :label "Click Apply to close this wizard")))
1015 (box-pack-start p-1 (make-instance 'label :label "Enter your name:") :expand nil)
1016 (box-pack-start p-1 entry)
1017 (assistant-append-page d p-1)
1018 (assistant-append-page d p-2)
1019 (setf (assistant-child-title d p-1) "Username wizard"
1020 (assistant-child-title d p-2) "Username wizard"
1021 (assistant-child-complete d p-1) nil
1022 (assistant-child-complete d p-2) t
1023 (assistant-child-page-type d p-1) :intro
1024 (assistant-child-page-type d p-2) :confirm
1025 (assistant-forward-page-function d) (lambda (i)
1026 (format output "(assistant-forward-page-function ~A)~%" i)
1027 (ecase i
1028 (0 1)
1029 (1 -1))))
1030 (connect-signal entry "notify::text" (lambda (object pspec)
1031 (declare (ignore object pspec))
1032 (setf (assistant-child-complete d p-1)
1033 (plusp (length (entry-text entry))))))
1034 (let ((w (make-instance 'label :label "A label in action area")))
1035 (widget-show w)
1036 (assistant-add-action-widget d w))
1037 (connect-signal d "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
1038 (connect-signal d "cancel" (lambda (assistant)
1039 (declare (ignore assistant))
1040 (object-destroy d)
1041 (show-message "Canceled")))
1042 (connect-signal d "close" (lambda (assistant)
1043 (declare (ignore assistant))
1044 (object-destroy d)
1045 (show-message (format nil "Thank you, ~A!" (entry-text entry)))))
1046 (connect-signal d "prepare" (lambda (assistant page-widget)
1047 (declare (ignore assistant page-widget))
1048 (format output "Assistant ~A has ~A pages and is on ~Ath page~%"
1049 d (assistant-n-pages d) (assistant-current-page d))))
1050 (widget-show d)))))
1052 (defun test-entry-completion ()
1053 "Not working example of GtkEntryCompletion"
1054 (within-main-loop
1055 (let* ((w (make-instance 'gtk-window))
1056 (model (make-instance 'tree-lisp-store)))
1057 (tree-lisp-store-add-column model "gchararray" #'identity)
1058 (tree-node-insert-at (tree-lisp-store-root model) (make-tree-node :item "Monday") 0)
1059 (tree-node-insert-at (tree-lisp-store-root model) (make-tree-node :item "Tuesday") 0)
1060 (tree-node-insert-at (tree-lisp-store-root model) (make-tree-node :item "Wednesday") 0)
1061 (tree-node-insert-at (tree-lisp-store-root model) (make-tree-node :item "Thursday") 0)
1062 (tree-node-insert-at (tree-lisp-store-root model) (make-tree-node :item "Friday") 0)
1063 (tree-node-insert-at (tree-lisp-store-root model) (make-tree-node :item "Saturday") 0)
1064 (tree-node-insert-at (tree-lisp-store-root model) (make-tree-node :item "Sunday") 0)
1065 (let* ((completion (make-instance 'entry-completion :model model :text-column 0))
1066 (e (make-instance 'entry :completion completion)))
1067 (setf (entry-completion-text-column completion) 0)
1068 (container-add w e))
1069 (connect-signal w "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
1070 (widget-show w))))
1072 (defun test-ui-markup ()
1073 (within-main-loop
1074 (let ((label (make-instance 'label :label "Hello!")))
1075 (let-ui (gtk-window :type :toplevel
1076 :position :center
1077 :title "Hello, world!"
1078 :default-width 300
1079 :default-height 400
1080 :var w
1081 (v-box
1082 (:expr label) :expand nil
1083 (scrolled-window
1084 :hscrollbar-policy :automatic
1085 :vscrollbar-policy :automatic
1086 :shadow-type :etched-in
1087 (text-view :var tv))
1088 (h-box
1089 (label :label "Insert:") :expand nil
1090 (entry :var entry)
1091 (button :label "gtk-ok" :use-stock t :var btn) :expand nil)
1092 :expand nil
1093 (label :label "Table packing")
1094 :expand nil
1095 (table
1096 :n-columns 2
1097 :n-rows 2
1098 (label :label "2 x 1") :left 0 :right 2 :top 0 :bottom 1
1099 (label :label "1 x 1") :left 0 :right 1 :top 1 :bottom 2
1100 (label :label "1 x 1") :left 1 :right 2 :top 1 :bottom 2)))
1101 (connect-signal w "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
1102 (connect-signal btn "clicked"
1103 (lambda (b)
1104 (declare (ignore b))
1105 (text-buffer-insert (text-view-buffer tv)
1106 (entry-text entry))))
1107 (widget-show w)))))
1109 (defun test-list-store ()
1110 "Demonstrates usage of list store"
1111 (within-main-loop
1112 (let-ui (gtk-window
1113 :type :toplevel
1114 :title "GtkListStore"
1115 :default-width 600
1116 :default-height 400
1117 :var w
1118 (v-box
1119 (label :label "A GtkListStore") :expand nil
1120 (scrolled-window
1121 :hscrollbar-policy :automatic
1122 :vscrollbar-policy :automatic
1123 (tree-view :var tv))))
1124 (let ((l (make-instance 'list-store :column-types '("gint" "gchararray"))))
1125 (iter (for i from 0 below 100)
1126 (for n = (random 10000000))
1127 (for s = (format nil "~R" n))
1128 (list-store-insert-with-values l i n s))
1129 (setf (tree-view-model tv) l)
1130 (let ((column (make-instance 'tree-view-column :title "Number" :sort-column-id 0))
1131 (renderer (make-instance 'cell-renderer-text :text "A text")))
1132 (tree-view-column-pack-start column renderer)
1133 (tree-view-column-add-attribute column renderer "text" 0)
1134 (tree-view-append-column tv column))
1135 (let ((column (make-instance 'tree-view-column :title "As string" :sort-column-id 1))
1136 (renderer (make-instance 'cell-renderer-text :text "A text")))
1137 (tree-view-column-pack-start column renderer)
1138 (tree-view-column-add-attribute column renderer "text" 1)
1139 (tree-view-append-column tv column))
1140 (connect-signal tv "row-activated"
1141 (lambda (w path column)
1142 (declare (ignore w column))
1143 (let* ((iter (tree-model-iter-by-path l path))
1144 (n (tree-model-value l iter 0))
1145 (dialog (make-instance 'message-dialog
1146 :title "Clicked"
1147 :text (format nil "Number ~A was clicked" n)
1148 :buttons :ok)))
1149 (dialog-run dialog)
1150 (object-destroy dialog)))))
1151 (connect-signal w "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
1152 (widget-show w))))
1154 (defun test-tree-store ()
1155 "Demonstrates usage of tree store"
1156 (within-main-loop
1157 (let-ui (gtk-window
1158 :type :toplevel
1159 :title "GtkListStore"
1160 :default-width 600
1161 :default-height 400
1162 :var w
1163 (v-box
1164 (label :label "A GtkListStore") :expand nil
1165 (scrolled-window
1166 :hscrollbar-policy :automatic
1167 :vscrollbar-policy :automatic
1168 (tree-view :var tv))))
1169 (let ((l (make-instance 'tree-store :column-types '("gint" "gchararray"))))
1170 (iter (for i from 0 below 100)
1171 (for n = (random 10000000))
1172 (for s = (format nil "~R" n))
1173 (for it = (tree-store-insert-with-values l nil i n s))
1174 (iter (for j from 0 below 10)
1175 (for n2 = (random 10000000))
1176 (for s2 = (format nil "~R" n2))
1177 (tree-store-insert-with-values l it j n2 s2)))
1178 (setf (tree-view-model tv) l)
1179 (let ((column (make-instance 'tree-view-column :title "Number" :sort-column-id 0))
1180 (renderer (make-instance 'cell-renderer-text :text "A text")))
1181 (tree-view-column-pack-start column renderer)
1182 (tree-view-column-add-attribute column renderer "text" 0)
1183 (tree-view-append-column tv column))
1184 (let ((column (make-instance 'tree-view-column :title "As string" :sort-column-id 1))
1185 (renderer (make-instance 'cell-renderer-text :text "A text")))
1186 (tree-view-column-pack-start column renderer)
1187 (tree-view-column-add-attribute column renderer "text" 1)
1188 (tree-view-append-column tv column))
1189 (connect-signal tv "row-activated"
1190 (lambda (w path column)
1191 (declare (ignore w column))
1192 (let* ((iter (tree-model-iter-by-path l path))
1193 (n (tree-model-value l iter 0))
1194 (dialog (make-instance 'message-dialog
1195 :title "Clicked"
1196 :text (format nil "Number ~A was clicked" n)
1197 :buttons :ok)))
1198 (dialog-run dialog)
1199 (object-destroy dialog)))))
1200 (connect-signal w "destroy" (lambda (w) (declare (ignore w)) (leave-gtk-main)))
1201 (widget-show w))))
1203 (defun test-gdk-expose (gdk-window)
1204 (let* ((gc (graphics-context-new gdk-window)))
1205 (multiple-value-bind (w h) (drawable-get-size gdk-window)
1206 (setf (graphics-context-rgb-bg-color gc) (make-color :red 0 :green 0 :blue 0))
1207 (draw-polygon gdk-window gc t (list (make-point :x 0 :y 0)
1208 (make-point :x (truncate w 2) :y 0)
1209 (make-point :x w :y (truncate h 2))
1210 (make-point :x w :y h)
1211 (make-point :x (truncate w 2) :y h)
1212 (make-point :x 0 :y (truncate h 2))))
1213 (setf (graphics-context-rgb-fg-color gc) (make-color :red 65535 :green 0 :blue 0))
1214 (draw-point gdk-window gc 20 10)
1215 (setf (graphics-context-rgb-fg-color gc) (make-color :red 0 :green 65535 :blue 0))
1216 (draw-points gdk-window gc (list (make-point :x 15 :y 20) (make-point :x 35 :y 40)))
1217 (setf (graphics-context-rgb-fg-color gc) (make-color :red 0 :green 0 :blue 65535))
1218 (draw-line gdk-window gc 60 30 40 50)
1219 (setf (graphics-context-rgb-fg-color gc) (make-color :red 65535 :green 65535 :blue 0))
1220 (draw-lines gdk-window gc (list (make-point :x 10 :y 30) (make-point :x 15 :y 40)
1221 (make-point :x 15 :y 50) (make-point :x 10 :y 56)))
1222 (setf (graphics-context-rgb-fg-color gc) (make-color :red 0 :green 65535 :blue 65535))
1223 (draw-segments gdk-window gc (list (make-segment :x1 35 :y1 35 :x2 55 :y2 35)
1224 (make-segment :x1 65 :y1 35 :x2 43 :y2 17)))
1225 (setf (graphics-context-rgb-fg-color gc) (make-color :red 65535 :green 0 :blue 65535)
1226 (graphics-context-rgb-bg-color gc) (make-color :red 32767 :green 0 :blue 32767))
1227 (draw-arc gdk-window gc nil 70 30 75 50 (* 64 75) (* 64 200))
1228 (draw-polygon gdk-window gc nil (list (make-point :x 20 :y 40)
1229 (make-point :x 30 :y 50)
1230 (make-point :x 40 :y 70)
1231 (make-point :x 30 :y 80)
1232 (make-point :x 10 :y 55)))
1233 (setf (graphics-context-rgb-fg-color gc) (make-color :red 16384 :green 16384 :blue 65535))
1234 (draw-trapezoids gdk-window gc (list (make-trapezoid :y1 50.0d0 :y2 70.0d0
1235 :x11 30.0d0 :x12 45.0d0
1236 :x21 70.0d0 :x22 50.0d0))))))
1238 (defun test-gdk ()
1239 "Test various gdk primitives"
1240 (within-main-loop
1241 (let ((window (make-instance 'gtk-window :type :toplevel :app-paintable t)))
1242 (connect-signal window "destroy" (lambda (widget)
1243 (declare (ignore widget))
1244 (leave-gtk-main)))
1245 (connect-signal window "expose-event"
1246 (lambda (widget event)
1247 (declare (ignore widget event))
1248 (test-gdk-expose (widget-window window))))
1249 (connect-signal window "configure-event"
1250 (lambda (widget event)
1251 (declare (ignore widget event))
1252 (widget-queue-draw window)))
1253 (widget-show window)
1254 (push :pointer-motion-mask (gdk-window-events (widget-window window))))))