author | Jan Vrany <jan.vrany@labware.com> |
Mon, 15 Jun 2020 15:01:43 +0100 | |
changeset 88 | 9d51db2ba641 |
parent 36 | 9b680e54aa94 |
permissions | -rw-r--r-- |
88 | 1 |
" |
2 |
stx:goodies/libcairo - Cairo graphics bindings for Smalltalk/X |
|
3 |
||
4 |
Copyright (C) 2008-2019 Jan Vrany |
|
5 |
||
6 |
This code is licensed under Creative Commons Attribution-NonCommercial License. |
|
7 |
For full text of the license, see file LICENSE.txt |
|
8 |
" |
|
15 | 9 |
"{ Package: 'stx:goodies/libcairo' }" |
10 |
||
11 |
"{ NameSpace: Cairo }" |
|
12 |
||
13 |
SimpleView subclass:#ClockView |
|
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
14 |
instanceVariableNames:'updater' |
15 | 15 |
classVariableNames:'' |
16 |
poolDictionaries:'' |
|
17 |
category:'Cairo-Examples' |
|
18 |
! |
|
19 |
||
88 | 20 |
!ClockView class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
stx:goodies/libcairo - Cairo graphics bindings for Smalltalk/X |
|
25 |
||
26 |
Copyright (C) 2008-2019 Jan Vrany |
|
27 |
||
28 |
This code is licensed under Creative Commons Attribution-NonCommercial License. |
|
29 |
For full text of the license, see file LICENSE.txt |
|
30 |
" |
|
31 |
! ! |
|
15 | 32 |
|
17 | 33 |
!ClockView methodsFor:'accessing-dimensions'! |
34 |
||
35 |
preferredExtent |
|
36 |
||
18 | 37 |
^400 @ 400 |
17 | 38 |
|
39 |
"Created: / 17-06-2012 / 22:37:47 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
40 |
! ! |
|
41 |
||
42 |
!ClockView methodsFor:'event handling'! |
|
43 |
||
44 |
destroy |
|
45 |
||
46 |
updater notNil ifTrue:[updater terminate]. |
|
47 |
super destroy. |
|
48 |
||
49 |
"Created: / 17-06-2012 / 22:41:25 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
50 |
! |
|
51 |
||
52 |
mapped |
|
53 |
||
54 |
updater isNil ifTrue:[ |
|
55 |
updater := [ [ Delay waitForSeconds: 1. self invalidate ] loop ] newProcess. |
|
56 |
updater resume. |
|
57 |
]. |
|
58 |
super mapped. |
|
59 |
||
60 |
"Created: / 17-06-2012 / 22:40:41 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
35
395689a88b32
Make Cairo::GraphicsContext to inherit from DeviceGraphicsContext
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
31
diff
changeset
|
61 |
"Modified: / 12-02-2016 / 16:40:06 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
17 | 62 |
! |
63 |
||
64 |
unmapped |
|
65 |
||
66 |
updater notNil ifTrue:[updater terminate]. |
|
67 |
super unmapped. |
|
68 |
||
69 |
"Created: / 17-06-2012 / 22:42:23 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
35
395689a88b32
Make Cairo::GraphicsContext to inherit from DeviceGraphicsContext
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
31
diff
changeset
|
70 |
"Modified: / 12-02-2016 / 16:40:16 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
395689a88b32
Make Cairo::GraphicsContext to inherit from DeviceGraphicsContext
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
31
diff
changeset
|
71 |
! ! |
395689a88b32
Make Cairo::GraphicsContext to inherit from DeviceGraphicsContext
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
31
diff
changeset
|
72 |
|
15 | 73 |
!ClockView methodsFor:'redrawing'! |
74 |
||
75 |
redraw |
|
29
6ba06265e543
Bindinge updated to recent Cairo version.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
28
diff
changeset
|
76 |
self redrawWithCairoBuffered |
15 | 77 |
|
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
78 |
"Created: / 16-06-2012 / 23:25:36 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
29
6ba06265e543
Bindinge updated to recent Cairo version.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
28
diff
changeset
|
79 |
"Modified: / 28-12-2014 / 22:17:02 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
80 |
! |
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
81 |
|
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
82 |
redrawX: x y: y width: w height: h |
29
6ba06265e543
Bindinge updated to recent Cairo version.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
28
diff
changeset
|
83 |
self redrawWithCairoBufferedX: x y: y width: w height: h |
17 | 84 |
|
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
85 |
"Created: / 17-06-2012 / 21:33:48 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
29
6ba06265e543
Bindinge updated to recent Cairo version.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
28
diff
changeset
|
86 |
"Modified: / 28-12-2014 / 22:18:18 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
87 |
! ! |
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
88 |
|
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
89 |
!ClockView methodsFor:'redrawing - cairo'! |
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
90 |
|
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
91 |
redrawWithCairo: cr |
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
92 |
|
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
93 |
| time hours mins secs | |
17 | 94 |
|
95 |
"/ scale to unit square and translate (0, 0) to be (0.5, 0.5), i.e. |
|
96 |
"/ the center of the window |
|
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
97 |
cr save. |
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
98 |
cr scale: self extent. |
36
9b680e54aa94
Take a step back: separate Cairo's GraphicsContext (cairo_t) and Smalltalk/X's graphics context
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
35
diff
changeset
|
99 |
cr translate: (0.5 @ 0.5). |
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
100 |
cr lineWidth: 0.05. |
17 | 101 |
|
36
9b680e54aa94
Take a step back: separate Cairo's GraphicsContext (cairo_t) and Smalltalk/X's graphics context
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
35
diff
changeset
|
102 |
cr sourceR: 0.33 G: 0.61 B: 0.11. |
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
103 |
cr paint. |
17 | 104 |
|
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
105 |
cr arcX: 0 y: 0 radius: 0.42 from: 0 to: (2 * (Float pi)). |
36
9b680e54aa94
Take a step back: separate Cairo's GraphicsContext (cairo_t) and Smalltalk/X's graphics context
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
35
diff
changeset
|
106 |
cr sourceR: 1.0 G: 1.0 B: 1.0. |
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
107 |
cr fillAndPreserve. |
36
9b680e54aa94
Take a step back: separate Cairo's GraphicsContext (cairo_t) and Smalltalk/X's graphics context
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
35
diff
changeset
|
108 |
cr sourceR: 0.0 G: 0.0 B: 0.0. |
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
109 |
cr strokeAndPreserve. |
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
110 |
cr clip. |
17 | 111 |
|
112 |
"Now, clock ticks" |
|
113 |
||
114 |
0 to: 11 do:[:i| |
|
115 |
| inset | |
|
116 |
||
117 |
inset := 0.05. |
|
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
118 |
cr save. |
17 | 119 |
|
29
6ba06265e543
Bindinge updated to recent Cairo version.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
28
diff
changeset
|
120 |
cr lineCap: Cairo::LineCap CAIRO_LINE_CAP_ROUND. |
17 | 121 |
(i \\ 3) ~~ 0 ifTrue:[ |
122 |
inset := inset * 0.8. |
|
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
123 |
cr lineWidth: 0.03. |
17 | 124 |
]. |
125 |
||
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
126 |
cr moveToX: (0.42 - inset) * ( i * (Float pi / 6)) cos |
17 | 127 |
y: (0.42 - inset) * ( i * (Float pi / 6)) sin. |
128 |
||
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
129 |
cr lineToX: (0.42 ) * ( i * (Float pi / 6)) cos |
17 | 130 |
y: (0.42 ) * ( i * (Float pi / 6)) sin. |
16 | 131 |
|
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
132 |
cr stroke. |
17 | 133 |
|
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
134 |
cr restore. |
17 | 135 |
]. |
136 |
||
137 |
"/ Not, the current time" |
|
138 |
||
139 |
time := Time now. |
|
140 |
hours := (time hours > 12 ifTrue:[time hours - 12] ifFalse:[time hours]) |
|
141 |
* (Float pi / 6). |
|
142 |
mins := time minutes * (Float pi / 30). |
|
143 |
secs := time seconds * (Float pi / 30). |
|
144 |
||
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
145 |
cr save. |
29
6ba06265e543
Bindinge updated to recent Cairo version.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
28
diff
changeset
|
146 |
cr lineCap: Cairo::LineCap CAIRO_LINE_CAP_ROUND. |
16 | 147 |
|
17 | 148 |
"/ draw the seconds hand |
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
149 |
cr save. |
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
150 |
cr lineWidth: 0.016. |
36
9b680e54aa94
Take a step back: separate Cairo's GraphicsContext (cairo_t) and Smalltalk/X's graphics context
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
35
diff
changeset
|
151 |
cr source: ((Color red: 70 green: 70 blue: 70) alpha: 0.8). |
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
152 |
cr moveToX: 0.0 y: 0.0. |
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
153 |
cr lineToX: (secs sin * (0.42 * 0.9)) |
17 | 154 |
y: (-1 * (secs cos * (0.42 * 0.9))). |
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
155 |
cr stroke. |
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
156 |
cr restore. |
16 | 157 |
|
17 | 158 |
"/ draw th minutes |
36
9b680e54aa94
Take a step back: separate Cairo's GraphicsContext (cairo_t) and Smalltalk/X's graphics context
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
35
diff
changeset
|
159 |
cr source: ((Color red: 11 green: 33 blue: 61) alpha: 0.7). |
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
160 |
cr moveToX: 0.0 y: 0.0. |
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
161 |
cr lineToX: ((mins + (secs / 60)) sin * (0.42 * 0.8)) |
17 | 162 |
y: (-1 * ((mins + (secs / 60)) cos * (0.42 * 0.8))). |
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
163 |
cr stroke. |
17 | 164 |
|
165 |
"/ draw the hours hand |
|
36
9b680e54aa94
Take a step back: separate Cairo's GraphicsContext (cairo_t) and Smalltalk/X's graphics context
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
35
diff
changeset
|
166 |
cr source: ((Color red: 33 green: 61 blue: 11) alpha: 0.6). |
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
167 |
cr moveToX: 0.0 y: 0.0. |
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
168 |
cr lineToX: ((hours + (mins / 12)) sin * (0.42 * 0.5)) |
17 | 169 |
y: (-1 * ((hours + (mins / 12)) cos * (0.42 * 0.5))). |
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
170 |
cr stroke. |
17 | 171 |
|
172 |
||
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
173 |
cr restore. |
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
174 |
cr arcX: 0 y: 0 radius: 0.01 from: 0 to: (2 * (Float pi)). |
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
175 |
cr fill. |
15 | 176 |
|
16 | 177 |
|
178 |
||
17 | 179 |
|
15 | 180 |
|
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
181 |
cr restore. |
16 | 182 |
|
28
1bd3d147cd77
Added utility methods to SimpleView...
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
26
diff
changeset
|
183 |
"Created: / 27-12-2014 / 00:00:44 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
36
9b680e54aa94
Take a step back: separate Cairo's GraphicsContext (cairo_t) and Smalltalk/X's graphics context
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
35
diff
changeset
|
184 |
"Modified: / 13-02-2016 / 17:01:58 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
15 | 185 |
! ! |
186 |
||
187 |
!ClockView class methodsFor:'documentation'! |
|
188 |
||
23
38ee47dbd976
Added version_HG to make Mercurial happy
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18
diff
changeset
|
189 |
version_HG |
38ee47dbd976
Added version_HG to make Mercurial happy
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18
diff
changeset
|
190 |
^ '$Changeset: <not expanded> $' |
15 | 191 |
! ! |
26
7f07a8c31e6d
Fixed flickring of Cairo::ClockView by rendering into an off-screen image.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
192 |