git.fiddlerwoaroof.com
Browse code

added main.lisp

fiddlerwoaroof authored on 24/10/2014 19:31:37
Showing 4 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,29 @@
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.
2 32
new file mode 100644
... ...
@@ -0,0 +1,130 @@
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
+
61
+        (on (destroy window) (__)
62
+          (declare (ignore __))
63
+          (leave-gtk-main))
64
+
65
+        (on (draw area) (widget cr)
66
+          (declare (ignore widget))
67
+          (let ((cr (pointer cr)))
68
+            (cairo-set-source-surface cr surface 0.0 0.0)
69
+            (cairo-paint cr)
70
+            (cairo-destroy cr)
71
+            +gdk-event-propagate+))
72
+
73
+        (on (configure-event area) (widget event)
74
+          (declare (ignore widget))
75
+          (when surface
76
+            (format t "surface is not nil~%")
77
+            (cairo-surface-destroy surface))
78
+          (setf surface (gdk-window-create-similar-surface
79
+                          (gtk-widget-window widget)
80
+                          :color
81
+                          (gtk-widget-get-allocated-width widget)
82
+                          (gtk-widget-get-allocated-height widget)))
83
+          (let ((cr (cairo-create surface)))
84
+            (cairo-set-source-rgb cr 1.0 1.0 1.0)
85
+            (cairo-paint cr)
86
+            (cairo-destroy cr)))
87
+
88
+        (on (motion-notify-event area) (widget event)
89
+          (format t "MOTION-NOTIFY-EVENT")
90
+          (when (member :button1-mask (gdk-event-motion-state event))
91
+            (let ((cr (cairo-create surface))
92
+                  (x (gdk-event-motion-x event))
93
+                  (y (gdk-event-motion-y event)))
94
+              (cairo-rectangle cr (- x 3.0) (- y 3.0) 6.0 6.0)
95
+              (cairo-fill cr)
96
+              (cairo-destroy cr)
97
+              (gtk-widget-queue-draw-area widget
98
+                                          (truncate (- x 3.0))
99
+                                          (truncate (- y 3.0))
100
+                                          6
101
+                                          6)))
102
+          +gdk-event-stop+)
103
+
104
+        (on (button-press-event area) (widget event)
105
+          (format t "BUTTON-PRESS-EVENT ~A~%" event)
106
+          (if (eql 1 (gdk-event-button-button event))
107
+            (let ((cr (cairo-create surface))
108
+                  (x (gdk-event-button-x event))
109
+                  (y (gdk-event-button-y event)))
110
+              (cairo-rectangle cr (- x 3.0) (- y 3.0) 6.0 6.0)
111
+              (cairo-fill cr)
112
+              (cairo-destroy cr)
113
+              (gtk-widget-queue-draw-area widget
114
+                                          (truncate (- x 3.0))
115
+                                          (truncate (- y 3.0))
116
+                                          6
117
+                                          6))
118
+            (let ((cr (cairo-create surface)))
119
+              (cairo-set-source-rgb cr 1.0 1.0 1.0)
120
+              (cairo-paint cr)
121
+              (cairo-destroy cr)
122
+              (gtk-widget-queue-draw widget))))
123
+
124
+            (gtk-widget-add-events area '(:button-press-mask :pointer-motion-mask))
125
+            (gtk-container-add frame area)
126
+            (gtk-container-add window frame)
127
+            (gtk-widget-show-all window)))))
128
+
129
+(example-drawing)
130
+