2 (:use :cl :gtk :gdk :gobject :anaphora :iter)
19 #:test-color-selection
24 (in-package :gtk-demo)
27 (let ((window (make-instance 'gtk-window :type :toplevel :app-paintable t))
29 (g-signal-connect window "destroy" (lambda (widget)
32 (g-signal-connect window "motion-notify-event" (lambda (widget event)
34 (setf x (event-motion-x event)
35 y (event-motion-y event))
36 (gtk-widget-queue-draw window)))
37 (g-signal-connect window "expose-event"
38 (lambda (widget event)
39 (declare (ignore event))
42 (using* ((gdk-window (widget-window window))
43 (gc (gdk-gc-new gdk-window))
44 (layout (gtk-widget-create-pango-layout window (format nil "X: ~F~%Y: ~F" x y))))
45 (gdk-draw-layout gdk-window gc 0 0 layout)
46 (gdk-gc-set-rgb-fg-color gc (make-color :red 65535 :green 0 :blue 0))
47 (multiple-value-bind (x y) (drawable-get-size gdk-window)
48 (gdk-draw-line gdk-window gc 0 0 x y)))))
49 (g-signal-connect window "configure-event"
50 (lambda (widget event)
51 (declare (ignore event))
53 (gtk-widget-queue-draw window)))
54 (gtk-widget-show-all window)
55 (push :pointer-motion-mask (gdk-window-events (widget-window window)))
60 (using* ((window (make-instance 'gtk-window :type :toplevel :title "Testing entry" :border-width 10))
61 (box (make-instance 'v-box))
62 (entry (make-instance 'entry))
63 (button (make-instance 'button :label "OK"))
64 (text-buffer (make-instance 'text-buffer))
65 (text-view (make-instance 'text-view :buffer text-buffer))
66 (button-select (make-instance 'button :label "Select"))
67 (button-insert (make-instance 'button :label "Insert")))
68 (box-pack-start box (make-instance 'label :label "Enter <b>anything</b> you wish:" :use-markup t) :expand nil)
69 (box-pack-start box entry :expand nil)
70 (box-pack-start box button :expand nil)
71 (box-pack-start box button-select :expand nil)
72 (box-pack-start box button-insert :expand nil)
73 (using* ((w (make-instance 'scrolled-window)))
74 (box-pack-start box w)
75 (container-add w text-view))
76 (container-add window box)
77 (g-signal-connect window "destroy" (lambda (widget) (release widget) (gtk-main-quit)))
78 (g-signal-connect window "delete-event" (lambda (widget event)
79 (declare (ignore event))
81 (using (dlg (make-instance 'message-dialog :text "Are you sure?" :buttons :yes-no))
82 (let ((response (dialog-run dlg)))
84 (not (eq :yes response))))))
85 (g-signal-connect button "clicked" (lambda (button) (release button)
86 (setf (text-buffer-text text-buffer)
87 (format nil "~A~%~A" (text-buffer-text text-buffer) (entry-text entry))
88 (entry-text entry) "")))
89 (g-signal-connect button-select "clicked" (lambda (button) (release button)
90 (editable-select-region entry 5 10)))
91 (g-signal-connect button-insert "clicked" (lambda (button) (release button)
92 (editable-insert-text entry "hello" 2)))
93 (gtk-widget-show-all window)
96 (defun table-packing ()
97 (using* ((window (make-instance 'gtk-window :type :toplevel :title "Table packing" :border-width 20))
98 (table (make-instance 'table :n-rows 2 :n-columns 2 :homogeneous t))
99 (button-1 (make-instance 'button :label "Button 1"))
100 (button-2 (make-instance 'button :label "Button 2"))
101 (button-q (make-instance 'button :label "Quit")))
102 (container-add window table)
103 (table-attach table button-1 0 1 0 1)
104 (table-attach table button-2 1 2 0 1)
105 (table-attach table button-q 0 2 1 2)
106 (g-signal-connect window "destroy" (lambda (w) (release w) (gtk-main-quit)))
107 (g-signal-connect button-q "clicked" (lambda (b) (release b) (object-destroy window)))
108 (gtk-widget-show-all window)
111 (defun test-pixbuf ()
112 (using* ((window (make-instance 'gtk-window :title "Test pixbuf" :request-width 600 :request-height 240))
113 (vbox (make-instance 'v-box))
114 (eventbox (make-instance 'event-box))
115 (vbox-1 (make-instance 'v-box)))
116 (container-add window vbox)
117 (box-pack-start vbox (make-instance 'label :text "Placing bg image" :font "Times New Roman Italic 10" :color "#00f" :request-height 40))
118 (g-signal-connect window "destroy" (lambda (w) (release w) (gtk-main-quit)))
119 (box-pack-start vbox eventbox)
120 (container-add eventbox vbox-1)
121 (box-pack-start vbox-1 (make-instance 'label :text "This is the eventbox"))
122 (box-pack-start vbox-1 (make-instance 'label :text "The green ball is the bg"))
123 (gtk-widget-show-all window)
127 (using*((window (make-instance 'gtk-window :title "Test images"))
128 (image (make-instance 'image :icon-name "applications-development" :icon-size 6)))
129 (container-add window image)
130 (g-signal-connect window "destroy" (lambda (w) (release w) (gtk-main-quit)))
131 (gtk-widget-show-all window)
134 (defun test-progress-bar ()
135 (using* ((window (make-instance 'gtk-window :title "Test progress bar"))
136 (v-box (make-instance 'v-box))
137 (p-bar (make-instance 'progress-bar :test "A process"))
138 (button-pulse (make-instance 'button :label "Pulse"))
139 (button-set (make-instance 'button :label "Set"))
140 (entry (make-instance 'entry)))
141 (g-signal-connect window "destroy" (lambda (w) (release w) (gtk-main-quit)))
142 (container-add window v-box)
143 (box-pack-start v-box p-bar)
144 (box-pack-start v-box button-pulse)
145 (box-pack-start v-box button-set)
146 (box-pack-start v-box entry)
147 (g-signal-connect button-pulse "clicked" (lambda (w) (release w) (progress-bar-pulse p-bar)))
148 (g-signal-connect button-set "clicked" (lambda (w) (release w)
149 (setf (progress-bar-fraction p-bar)
150 (coerce (read-from-string (entry-text entry)) 'real))))
151 (gtk-widget-show-all window)
154 (defun test-status-bar ()
155 (using* ((window (make-instance 'gtk-window :title "Text status bar"))
156 (v-box (make-instance 'v-box))
157 (h-box (make-instance 'h-box))
158 (label (make-instance 'label :label "Test of status bar" :xalign 0.5 :yalign 0.5))
159 (status-bar (make-instance 'statusbar :has-resize-grip t))
160 (button-push (make-instance 'button :label "Push"))
161 (button-pop (make-instance 'button :label "Pop"))
162 (entry (make-instance 'entry))
163 (icon (make-instance 'status-icon :icon-name "applications-development")))
164 (set-status-icon-tooltip icon "An icon from lisp program")
165 (g-signal-connect window "destroy" (lambda (w) (release w)
166 #+ (or) (setf (status-icon-visible icon) nil)
168 (g-signal-connect button-push "clicked" (lambda (b) (release b) (status-bar-push status-bar "lisp-prog" (entry-text entry))))
169 (g-signal-connect button-pop "clicked" (lambda (b) (release b) (status-bar-pop status-bar "lisp-prog")))
170 (g-signal-connect icon "activate" (lambda (i) (release i)
171 (using (message-dialog (make-instance 'message-dialog :buttons :ok :text "You clicked on icon!"))
172 (dialog-run message-dialog)
173 (object-destroy message-dialog))))
174 (container-add window v-box)
175 (box-pack-start v-box h-box :expand nil)
176 (box-pack-start h-box entry)
177 (box-pack-start h-box button-push :expand nil)
178 (box-pack-start h-box button-pop :expand nil)
179 (box-pack-start v-box label)
180 (box-pack-start v-box status-bar :expand nil)
181 (gtk-widget-show-all window)
182 (setf (status-icon-screen icon) (gtk-window-screen window))
185 (defun test-scale-button ()
186 (using* ((window (make-instance 'gtk-window :type :toplevel :title "Testing scale button"))
187 (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))))
188 (g-signal-connect window "destroy" (lambda (w) (release w) (gtk-main-quit)))
189 (container-add window button)
190 (gtk-widget-show-all window)
193 (defun test-text-view ()
194 (using* ((window (make-instance 'gtk-window :type :toplevel :title "Testing text view" :width-request 400 :height-request 300))
195 (button (make-instance 'button :label "Do"))
196 (bold-btn (make-instance 'button :label "Bold"))
197 (buffer (make-instance 'text-buffer :text "Some text buffer with some text inside"))
198 (v (make-instance 'text-view :buffer buffer :wrap-mode :word))
199 (box (make-instance 'v-box))
200 (scrolled (make-instance 'scrolled-window :hscrollbar-policy :automatic :vscrollbar-policy :automatic)))
201 (g-signal-connect window "destroy" (lambda (w) (release w) (gtk-main-quit)))
202 (g-signal-connect button "clicked" (lambda (b)
204 (using* ((i1 (make-instance 'text-iter))
205 (i2 (make-instance 'text-iter)))
206 (multiple-value-bind (i1 i2) (text-buffer-get-selection-bounds buffer)
208 (using* ((i1 i1) (i2 i2)
209 (dialog (make-instance 'message-dialog :buttons :ok)))
210 (setf (message-dialog-text dialog) (format nil "selection: from (~A,~A) to (~A,~A)"
211 (text-iter-line i1) (text-iter-line-offset i1)
212 (text-iter-line i2) (text-iter-line-offset i2)))
214 (object-destroy dialog)))))))
215 (g-signal-connect bold-btn "clicked" (Lambda (b)
217 (multiple-value-bind (start end) (text-buffer-get-selection-bounds buffer)
218 (when (and start end)
219 (using* ((start start) (end end) (tag (text-tag-table-lookup (text-buffer-tag-table buffer) "bold")))
220 (if (text-iter-has-tag start tag)
221 (text-buffer-remove-tag buffer tag start end)
222 (text-buffer-apply-tag buffer tag start end)))))))
223 (let ((tag (make-instance 'text-tag :name "bold" :weight 700)))
224 (text-tag-table-add (text-buffer-tag-table buffer) tag)
225 (g-signal-connect tag "event"
226 (lambda (tag object event iter)
227 (declare (ignore tag object iter))
228 (when (eq (event-type event) :button-release)
229 (using (dlg (make-instance 'message-dialog :text "You clicked on bold text." :buttons :ok))
231 (object-destroy dlg))))))
232 (container-add window box)
233 (container-add scrolled v)
234 (box-pack-start box button :expand nil)
235 (box-pack-start box bold-btn :expand nil)
236 (box-pack-start box scrolled)
237 (gtk-widget-show-all window)
240 (defun demo-code-editor ()
241 (using* ((window (make-instance 'gtk-window :type :toplevel :title "Code editor" :width-request 400 :height-request 400 :window-position :center))
242 (scrolled (make-instance 'scrolled-window :hscrollbar-policy :automatic :vscrollbar-policy :automatic))
243 (buffer (make-instance 'text-buffer))
244 (view (make-instance 'text-view :buffer buffer)))
245 (g-signal-connect window "destroy" (lambda (w) (release w) (gtk-main-quit)))
246 (container-add window scrolled)
247 (container-add scrolled view)
248 (gtk-widget-show-all window)
249 (g-signal-connect buffer "insert-text" (lambda (buffer location text len)
250 (using* ((buffer buffer) (location location))
251 (format t "~A~%" (list buffer location text len)))))
254 (defstruct tvi title value)
256 (defun test-treeview-list ()
257 (let* ((window (make-instance 'gtk-window :type :toplevel :title "Treeview (list)"))
258 (model (make-instance 'array-list-store))
259 (scroll (make-instance 'scrolled-window :hscrollbar-policy :automatic :vscrollbar-policy :automatic))
260 (tv (make-instance 'tree-view :headers-visible t :width-request 100 :height-request 400 :rules-hint t))
261 (h-box (make-instance 'h-box))
262 (v-box (make-instance 'v-box))
263 (title-entry (make-instance 'entry))
264 (value-entry (make-instance 'entry))
265 (button (make-instance 'button :label "Add")))
266 (store-add-column model "gchararray" #'tvi-title)
267 (store-add-column model "gint" #'tvi-value)
268 (store-add-item model (make-tvi :title "Monday" :value 1))
269 (store-add-item model (make-tvi :title "Tuesday" :value 2))
270 (store-add-item model (make-tvi :title "Wednesday" :value 3))
271 (store-add-item model (make-tvi :title "Thursday" :value 4))
272 (store-add-item model (make-tvi :title "Friday" :value 5))
273 (store-add-item model (make-tvi :title "Saturday" :value 6))
274 (store-add-item model (make-tvi :title "Sunday" :value 7))
275 (setf (tree-view-model tv) model (tree-view-tooltip-column tv) 0)
276 (gobject:g-signal-connect window "destroy" (lambda (w) (gobject:release w) (gtk-main-quit)))
277 (gobject:g-signal-connect button "clicked" (lambda (b) (gobject:release b)
278 (store-add-item model (make-tvi :title (entry-text title-entry)
279 :value (or (parse-integer (entry-text value-entry)
282 (g-signal-connect tv "row-activated" (lambda (tv path column)
283 (release* tv path column)
284 (format t "You clicked on row ~A~%" (tree-path-indices path))))
285 (container-add window v-box)
286 (box-pack-start v-box h-box :expand nil)
287 (box-pack-start h-box title-entry :expand nil)
288 (box-pack-start h-box value-entry :expand nil)
289 (box-pack-start h-box button :expand nil)
290 (box-pack-start v-box scroll)
291 (container-add scroll tv)
292 (let ((column (make-instance 'tree-view-column :title "Title" :sort-column-id 0))
293 (renderer (make-instance 'cell-renderer-text :text "A text")))
294 (tree-view-column-pack-start column renderer)
295 (tree-view-column-add-attribute column renderer "text" 0)
296 (tree-view-append-column tv column)
297 (print (tree-view-column-tree-view column))
298 (print (tree-view-column-cell-renderers column)))
299 (let ((column (make-instance 'tree-view-column :title "Value"))
300 (renderer (make-instance 'cell-renderer-text :text "A text")))
301 (tree-view-column-pack-start column renderer)
302 (tree-view-column-add-attribute column renderer "text" 1)
303 (tree-view-append-column tv column)
304 (print (tree-view-column-tree-view column))
305 (print (tree-view-column-cell-renderers column)))
306 (gtk-widget-show-all window)
309 (defun test-toolbar ()
310 (let* ((window (make-instance 'gtk-window :type :toplevel :title "Toolbar" :width-request 200 :height-request 100 :window-position :center))
311 (v-box (make-instance 'v-box))
312 (toolbar (make-instance 'toolbar :toolbar-style :icons :icon-size :small-toolbar :icon-size-set t))
313 (l (make-instance 'label :label "Toolbar demo")))
314 (gobject:g-signal-connect window "destroy" (lambda (w) (gobject:release w) (gtk-main-quit)))
315 (container-add window v-box)
316 (box-pack-start v-box toolbar :expand nil)
317 (box-pack-start v-box l)
318 (let ((b (make-instance 'tool-button :stock-id "gtk-connect")))
319 (g-signal-connect b "clicked" (lambda (b)
320 (setf (tool-button-stock-id b)
321 (if (string= (tool-button-stock-id b) "gtk-connect")
322 (prog1 "gtk-disconnect" (setf (label-label l) "Disconnected"))
323 (prog1 "gtk-connect" (setf (label-label l) "Connected"))))))
324 (toolbar-insert toolbar b -1))
325 (toolbar-insert toolbar (make-instance 'separator-tool-item) -1)
326 (toolbar-insert toolbar (make-instance 'tool-button :stock-id "gtk-undo" :sensitive nil) -1)
327 (toolbar-insert toolbar (make-instance 'tool-button :stock-id "gtk-redo") -1)
328 (gtk-widget-show-all window)
331 (defun test-color-button ()
332 (let* ((window (make-instance 'gtk-window :type :toplevel :title "Color button" :width-request 200 :height-request 100 :window-position :center))
333 (button (make-instance 'color-button :label "Choose your color" :use-alpha t)))
334 (gobject:g-signal-connect window "destroy" (lambda (w) (gobject:release w) (gtk-main-quit)))
335 (container-add window button)
336 (setf (color-button-color button)
337 (make-color :red (random 65536) :green (random 65536) :blue (random 65536)))
338 (gtk-widget-show-all window)
341 (defun test-ui-manager ()
342 (let* ((window (make-instance 'gtk-window :type :toplevel :title "UI Manager" :default-width 200 :default-height 100 :window-position :center))
343 (ui-manager (make-instance 'ui-manager))
344 (print-confirmation t))
345 (ui-manager-add-ui-from-string ui-manager
348 <toolbar action='toolbar1'>
350 <toolitem name='Left' action='justify-left'/>
351 <toolitem name='Center' action='justify-center'/>
352 <toolitem name='Right' action='justify-right'/>
353 <toolitem name='Zoom in' action='zoom-in' />
354 <toolitem name='print-confirm' action='print-confirm' />
358 (gobject:g-signal-connect window "destroy" (lambda (w) (gobject:release w) (gtk-main-quit)))
359 (iter (with fn = (lambda (action) (when print-confirmation (format t "Action ~A with name ~A activated~%" action (action-name action)))))
360 (with action-group = (make-instance 'action-group :name "Actions"))
361 (finally (let ((a (make-instance 'toggle-action :name "print-confirm" :label "Print" :stock-id "gtk-print-report" :active t)))
362 (g-signal-connect a "toggled" (lambda (action) (setf print-confirmation (toggle-action-active action))))
363 (action-group-add-action action-group a))
364 (ui-manager-insert-action-group ui-manager action-group 0))
365 (for (name stock-id) in '(("justify-left" "gtk-justify-left")
366 ("justify-center" "gtk-justify-center")
367 ("justify-right" "gtk-justify-right")
368 ("zoom-in" "gtk-zoom-in")))
369 (for action = (make-instance 'action :name name :stock-id stock-id))
370 (g-signal-connect action "activate" fn)
371 (action-group-add-action action-group action))
372 (awhen (ui-manager-widget ui-manager "/toolbar1")
373 (container-add window it))
374 (gtk-widget-show-all window)
377 (defun test-color-button ()
378 (let ((window (make-instance 'gtk-window :title "Color button" :type :toplevel :window-position :center :width-request 100 :height-request 100))
379 (button (make-instance 'color-button :title "Color button")))
380 (g-signal-connect window "destroy" (lambda (w) (release w) (gtk-main-quit)))
381 (g-signal-connect button "color-set" (lambda (b) (release b) (format t "Chose color ~A~%" (color-button-color button))))
382 (container-add window button)
383 (gtk-widget-show-all window)
386 (defun test-color-selection ()
387 (let ((window (make-instance 'gtk-window :title "Color selection" :type :toplevel :window-position :center))
388 (selection (make-instance 'color-selection :has-opacity-control t)))
389 (g-signal-connect window "destroy" (lambda (w) (declare (ignore w)) (gtk-main-quit)))
390 (g-signal-connect selection "color-changed" (lambda (s) (declare (ignore s)) (unless (color-selection-adjusting-p selection) (format t "color: ~A~%" (color-selection-current-color selection)))))
391 (container-add window selection)
392 (gtk-widget-show-all window)
395 (defun test-file-chooser ()
396 (let ((window (make-instance 'gtk-window :title "file chooser" :type :toplevel :window-position :center :default-width 100 :default-height 100))
397 (v-box (make-instance 'v-box))
398 (button (make-instance 'file-chooser-button :action :open))
399 (b (make-instance 'button :label "Choose for save" :stock-id "gtk-save")))
400 (g-signal-connect window "destroy" (lambda (w) (declare (ignore w)) (gtk-main-quit)))
401 (g-signal-connect button "file-set" (lambda (b) (declare (ignore b)) (format t "File set: ~A~%" (file-chooser-filename button))))
402 (g-signal-connect b "clicked" (lambda (b)
404 (let ((d (make-instance 'file-chooser-dialog :action :save :title "Choose file to save")))
405 (dialog-add-button d "gtk-save" :accept)
406 (dialog-add-button d "gtk-cancel" :cancel)
407 (when (eq (dialog-run d) :accept)
408 (format t "saved to file ~A~%" (file-chooser-filename d)))
409 (object-destroy d))))
410 (container-add window v-box)
411 (box-pack-start v-box button)
412 (box-pack-start v-box b)
413 (gtk-widget-show-all window)
416 (defun test-font-chooser ()
417 (let ((window (make-instance 'gtk-window :title "fonts" :type :toplevel :window-position :center :default-width 100 :default-height 100))
418 (v-box (make-instance 'v-box))
419 (button (make-instance 'font-button :title "Choose font" :font-name "Sans 10")))
420 (g-signal-connect window "destroy" (lambda (w) (declare (ignore w)) (gtk-main-quit)))
421 (g-signal-connect button "font-set" (lambda (b) (declare (ignore b)) (format t "Chose font ~A~%" (font-button-font-name button))))
422 (container-add window v-box)
423 (box-pack-start v-box button)
424 (gtk-widget-show-all window)
427 (defun test-notebook ()
428 (let ((window (make-instance 'gtk-window :title "Notebook" :type :toplevel :window-position :center :default-width 100 :default-height 100))
429 (expander (make-instance 'expander :expanded t :label "notebook"))
430 (notebook (make-instance 'notebook :enable-popup t)))
431 (g-signal-connect window "destroy" (lambda (w) (declare (ignore w)) (gtk-main-quit)))
432 (iter (for i from 0 to 5)
433 (for page = (make-instance 'label :label (format nil "Label for page ~A" i)))
434 (for tab-label = (make-instance 'label :label (format nil "Tab ~A" i)))
435 (for tab-button = (make-instance 'button :use-stock t :label "gtk-close" :relief :none))
436 (for tab-hbox = (make-instance 'h-box))
437 (box-pack-start tab-hbox tab-label)
438 (box-pack-start tab-hbox tab-button)
439 (gtk-widget-show-all tab-hbox)
440 (notebook-add-page notebook page tab-hbox))
441 (container-add window expander)
442 (container-add expander notebook)
443 (gtk-widget-show-all window)