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