git.fiddlerwoaroof.com
Browse code

testing hooks

fiddlerwoaroof authored on 24/10/2014 19:33:47
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,137 @@
1
+;  Copyright (c) 2014 Edward Langley
2
+;  All rights reserved.
3
+
4
+;  Redistribution and use in source and binary forms, with or without
5
+;  modification, are permitted provided that the following conditions
6
+;  are met:
7
+
8
+;  Redistributions of source code must retain the above copyright notice,
9
+;  this list of conditions and the following disclaimer.
10
+
11
+;  Redistributions in binary form must reproduce the above copyright
12
+;  notice, this list of conditions and the following disclaimer in the
13
+;  documentation and/or other materials provided with the distribution.
14
+
15
+;  Neither the name of the project's author nor the names of its
16
+;  contributors may be used to endorse or promote products derived from
17
+;  this software without specific prior written permission.
18
+
19
+;  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
+;  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
+;  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22
+;  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
+;  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
+;  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
25
+;  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
+;  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
+;  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
+;  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
+;  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+
31
+(asdf:load-system :cl-cffi-gtk)
32
+
33
+(defpackage :gtk-tutorial
34
+  (:use :gtk :gdk :gdk-pixbuf :gobject
35
+        :glib :gio :pango :cairo :common-lisp))
36
+
37
+(in-package :gtk-tutorial)
38
+
39
+;(macroexpand-1 '(on (clicked button) (__)
40
+;                    (declare (ignore __))
41
+;                    (format t "Button 1 was pressed")))
42
+
43
+(defmacro on ((sig target) bind &body body)
44
+  "Bind a signal handler: note that this intentionally shadows whatever the 'bind' argument is"
45
+  `(g-signal-connect ,target ,(string-downcase (symbol-name sig))
46
+                     (lambda ,bind
47
+                       ,@body)))
48
+
49
+(let ((surface nil))
50
+  (defun example-drawing ()
51
+    (within-main-loop
52
+      (let ((window (make-instance 'gtk-window
53
+                                   :type :toplevel
54
+                                   :title "Example Drawing"))
55
+            (frame (make-instance 'gtk-frame
56
+                                  :shadow-type :in))
57
+            (area (make-instance 'gtk-drawing-area
58
+                                 :width-request 250
59
+                                 :height-request 200)))
60
+        (g-signal-connect window "destroy"
61
+                          (lambda (widget)
62
+                            (declare (ignore widget))
63
+                            (leave-gtk-main)))
64
+        ;; Signals used to handle the backing surface
65
+        (g-signal-connect area "draw"
66
+                          (lambda (widget cr)
67
+                            (declare (ignore widget))
68
+                            (let ((cr (pointer cr)))
69
+                              (cairo-set-source-surface cr surface 0.0 0.0)
70
+                              (cairo-paint cr)
71
+                              (cairo-destroy cr)
72
+                              +gdk-event-propagate+)))
73
+        (g-signal-connect area "configure-event"
74
+                          (lambda (widget event)
75
+                            (declare (ignore event))
76
+                            (when surface
77
+                              (cairo-surface-destroy surface))
78
+                            (setf surface
79
+                                  (gdk-window-create-similar-surface
80
+                                    (gtk-widget-window widget)
81
+                                    :color
82
+                                    (gtk-widget-get-allocated-width widget)
83
+                                    (gtk-widget-get-allocated-height widget)))
84
+                            ;; Clear surface
85
+                            (let ((cr (cairo-create surface)))
86
+                              (cairo-set-source-rgb cr 1.0 1.0 1.0)
87
+                              (cairo-paint cr)
88
+                              (cairo-destroy cr))
89
+                            (format t "leave event 'configure-event'~%")
90
+                            +gdk-event-stop+))
91
+        ;; Event signals
92
+        (g-signal-connect area "motion-notify-event"
93
+                          (lambda (widget event)
94
+                            (format t "MOTION-NOTIFY-EVENT ~A~%" event)
95
+                            (when (member :button1-mask (gdk-event-motion-state event))
96
+                              (let ((cr (cairo-create surface))
97
+                                    (x (gdk-event-motion-x event))
98
+                                    (y (gdk-event-motion-y event)))
99
+                                (cairo-rectangle cr (- x 3.0) (- y 3.0) 6.0 6.0)
100
+                                (cairo-fill cr)
101
+                                (cairo-destroy cr)
102
+                                (gtk-widget-queue-draw-area widget
103
+                                                            (truncate (- x 3.0))
104
+                                                            (truncate (- y 3.0))
105
+                                                            6
106
+                                                            6)))
107
+                            ;; We have handled the event, stop processing
108
+                            +gdk-event-stop+))
109
+        (g-signal-connect area "button-press-event"
110
+                          (lambda (widget event)
111
+                            (format t "BUTTON-PRESS-EVENT ~A~%" event)
112
+                            (if (eql 1 (gdk-event-button-button event))
113
+                              (let ((cr (cairo-create surface))
114
+                                    (x (gdk-event-button-x event))
115
+                                    (y (gdk-event-button-y event)))
116
+                                (cairo-rectangle cr (- x 3.0) (- y 3.0) 6.0 6.0)
117
+                                (cairo-fill cr)
118
+                                (cairo-destroy cr)
119
+                                (gtk-widget-queue-draw-area widget
120
+                                                            (truncate (- x 3.0))
121
+                                                            (truncate (- y 3.0))
122
+                                                            6
123
+                                                            6))
124
+                              ;; Clear surface
125
+                              (let ((cr (cairo-create surface)))
126
+                                (cairo-set-source-rgb cr 1.0 1.0 1.0)
127
+                                (cairo-paint cr)
128
+                                (cairo-destroy cr)
129
+                                (gtk-widget-queue-draw widget)))))
130
+        (gtk-widget-add-events area
131
+                               '(:button-press-mask
132
+                                  :pointer-motion-mask))
133
+        (gtk-container-add frame area)
134
+        (gtk-container-add window frame)
135
+        (gtk-widget-show-all window)))))
136
+
137
+(example-drawing)