forked from macoslib/macoslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageTransformExample.rbfrm
361 lines (304 loc) · 10.7 KB
/
ImageTransformExample.rbfrm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#tag Window
Begin Window ImageTransformExample
BackColor = 16777215
Backdrop = ""
CloseButton = True
Composite = True
Frame = 0
FullScreen = False
HasBackColor = False
Height = 293
ImplicitInstance= True
LiveResize = True
MacProcID = 0
MaxHeight = 32000
MaximizeButton = True
MaxWidth = 32000
MenuBar = ""
MenuBarVisible = True
MinHeight = 64
MinimizeButton = True
MinWidth = 64
Placement = 0
Resizeable = True
Title = "Image Transformations"
Visible = True
Width = 521
Begin PushButton PushButton1
AutoDeactivate = True
BehaviorIndex = 0
Bold = ""
Cancel = ""
Caption = "Choose"
Default = True
Enabled = True
Height = 20
HelpTag = ""
Index = 2147483648
InitialParent = ""
Italic = ""
Left = 421
LockBottom = True
LockedInPosition= False
LockLeft = ""
LockRight = True
LockTop = ""
Scope = 0
TabIndex = 0
TabPanelIndex = 0
TabStop = True
TextFont = "System"
TextSize = 0
Top = 253
Underline = ""
Visible = True
Width = 80
End
Begin StaticText StaticText1
AutoDeactivate = True
BehaviorIndex = 1
Bold = ""
DataField = ""
DataSource = ""
Enabled = True
Height = 64
HelpTag = ""
Index = 2147483648
InitialParent = ""
Italic = ""
Left = 20
LockBottom = True
LockedInPosition= False
LockLeft = True
LockRight = True
LockTop = ""
Multiline = True
Scope = 0
TabIndex = 1
TabPanelIndex = 0
TabStop = True
Text = "You should be able to open an image file in any reasonable format, including .icns. This window will display the original image, plus disabled and selected versions of it."
TextAlign = 0
TextColor = &h000000
TextFont = "System"
TextSize = 0
Top = 222
Underline = ""
Visible = True
Width = 389
End
Begin Canvas Canvas1
AcceptFocus = ""
AcceptTabs = ""
AutoDeactivate = False
Backdrop = ""
BehaviorIndex = 2
Enabled = False
EraseBackground = True
Height = 203
HelpTag = ""
Index = 2147483648
InitialParent = ""
Left = 20
LockBottom = True
LockedInPosition= False
LockLeft = True
LockRight = True
LockTop = True
Scope = 0
TabIndex = 2
TabPanelIndex = 0
TabStop = False
Top = 14
UseFocusRing = False
Visible = True
Width = 481
End
End
#tag EndWindow
#tag WindowCode
#tag Event
Sub Open()
'RegisterCarbonEventHandlerForHIView
End Sub
#tag EndEvent
#tag Event
Sub Paint(g As Graphics)
// Drawing it here, with the Button having its Default=true,
// it'll not work right due to repeated calls to this event in a wrong drawing state
// Drawing it from a control's Paint even works, however
'if image <> nil then
'DrawImages g
'end if
#pragma unused g
End Sub
#tag EndEvent
#tag Method, Flags = &h21
Private Function ContentViewRef() As Ptr
'OSStatus HIViewFindByID (
'HIViewRef inStartView,
'HIViewID inID,
'HIViewRef * outControl
');
'
'struct ControlID {
'OSType signature;
'SInt32 id;
'};
'typedef struct ControlID ControlID;
'typedef ControlID HIViewID;
Soft Declare Function CFBundleGetBundleWithIdentifier lib CarbonLib (bundleID as CFStringRef) as Integer
dim carbonBundle as Integer = CFBundleGetBundleWithIdentifier(Carbon.BundleID)
Soft Declare Function CFBundleGetDataPointerForName lib CarbonLib (bundle as Integer, symbolName as CFStringRef) as Ptr
dim contentViewID as Ptr = CFBundleGetDataPointerForName(carbonBundle, "kHIViewWindowContentID")
if contentViewID = nil then
return nil
end if
Soft Declare Function HIViewFindByID lib CarbonLib (inStartView as Ptr, signature as Integer, id as Integer, byRef outControl as Ptr) as Integer
dim theViewRef as Ptr
dim OSError as Integer = HIViewFindByID(me.RootView, contentViewID.Integer(0), contentViewID.Integer(4), theViewRef)
If OSError <> 0 then
break
theViewRef = nil
End if
Return theViewRef
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Sub DrawImages(g as Graphics)
DrawImagesInContext new CGContextGraphicsPort(g)
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Sub DrawImagesInContext(context as CGContext)
if self.Image is nil then
return
end if
dim w as Single = 10 + me.Image.Width
dim rect as CGRect = CGRectMake(10, 10, Image.Width, Image.Height)
context.DrawImage me.Image, CGRectOffset (rect, w * 0, 0)
context.DrawImage me.ImageDisabled, CGRectOffset (rect, w * 1, 0)
context.DrawImage me.ImageSelected, CGRectOffset (rect, w * 2, 0)
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Shared Function ForwardCarbonEventToObject(EventHandlerCallRef as Ptr, EventRef as Ptr, UserData as Integer) As Integer
dim returnValue as Integer = eventNotHandledErr
for i as Integer = 0 to WindowCount - 1
dim w as Window = Window(i)
if w.Handle = UserData and w IsA ImageTransformExample then
returnValue = ImageTransformExample(w).HandleCarbonEvent(EventHandlerCallRef, EventRef)
exit
end if
next
return returnValue
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Function HandleCarbonEvent(EventHandlerCallRef As Ptr, EventRef As Ptr) As Integer
soft declare function GetEventClass lib CarbonLib (inEvent as Ptr) as OSType
soft declare function GetEventKind lib CarbonLib (inEvent as Ptr) as UInt32
soft declare function CallNextEventHandler lib CarbonLib (inCallRef as Ptr, inEvent as Ptr) as Integer
dim eventClass as String = GetEventClass(EventRef)
dim eventKind as UInt32 = GetEventKind(EventRef)
select case eventClass
case kEventClassControl
select case eventKind
case kEventControlDraw
dim f as Integer = CallNextEventHandler(EventHandlerCallRef, EventRef)
#pragma unused f
soft declare function GetEventParameter lib CarbonLib (inEvent as Ptr, inName as OSType, inDesiredType as OSType, outActualType as Ptr, inBufferSize as Integer, outBufferSize as Ptr, ByRef outData as Ptr) as Integer
dim contextRef as Ptr
dim OSError as Integer = GetEventParameter(eventRef, kEventParamCGContextRef, typeCGContextRef, nil, 4, nil, contextRef)
if OSError <> noErr then
return eventNotHandledErr
end if
dim context as new CGContext(contextRef, false)
DrawImagesInContext context
return eventNotHandledErr
else
return eventNotHandledErr
end select
else
return eventNotHandledErr
end select
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Shared Function OSTypeToUInt32(x as OSType) As UInt32
dim d as MemoryBlock = x // Due to bug <feedback://showreport?report_id=16190> we must not use SplitB
d.LittleEndian = false
return d.UInt32Value(0)
End Function
#tag EndMethod
#tag Method, Flags = &h21
Private Sub RegisterCarbonEventHandlerForHIView()
#pragma StackOverflowChecking False
soft declare function InstallEventHandler lib CarbonLib (inTarget as Ptr, inHandler as Ptr, inNumTypes as Integer, inList as Ptr, inUserData as Integer, handlerRef as Ptr) as Integer
soft declare function HIViewGetEventTarget lib CarbonLib (inView as Ptr) as Ptr
dim eventTarget as Ptr = HIViewGetEventTarget(ContentViewRef)
if eventTarget = nil then
return
end if
//inHandler
soft declare function NewEventHandlerUPP lib CarbonLib (userRoutine as Ptr) as Ptr
dim CallbackUPP as Ptr = NewEventHandlerUPP(AddressOf ForwardCarbonEventToObject)
//events
dim eventList(0) as EventTypeSpec
eventList(0).eventClass = OSTypeToUInt32(kEventClassControl)
eventList(0).eventKind = kEventControlDraw
//inUserData
//handlerRef
//I don't want it returned
dim OSError as Integer = InstallEventHandler(eventTarget, CallbackUPP, 1 + UBound(eventList), eventList.CArray, self.Handle, Nil)
// Keep the compiler from complaining
#pragma unused OSError
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Function RootView() As Ptr
soft declare function HIViewGetRoot lib CarbonLib (inWindow as WindowPtr) as Ptr
return HIViewGetRoot(self)
End Function
#tag EndMethod
#tag Note, Name = Notes
You should be able to open an image file in any reasonable format, including .icns.
The window will display the image, plus disabled and selected versions of the image.
#tag EndNote
#tag Property, Flags = &h21
Private Image As CGImage
#tag EndProperty
#tag Property, Flags = &h21
Private ImageDisabled As CGImage
#tag EndProperty
#tag Property, Flags = &h21
Private ImageSelected As CGImage
#tag EndProperty
#tag EndWindowCode
#tag Events PushButton1
#tag Event
Sub Action()
dim dlg as new OpenDialog
dlg.PromptText = "Choose an image file to display."
//probably we should assign some file types.
if dlg.ShowModalWithin(self) is nil then
return
end if
dim source as new CGImageSource(new CFURL(dlg.Result.URLPath))
self.Image = source.Image(0)
self.ImageDisabled = self.Image.TransformDisabled
self.ImageSelected = self.Image.TransformSelected
self.Refresh
End Sub
#tag EndEvent
#tag EndEvents
#tag Events Canvas1
#tag Event
Sub Paint(g As Graphics)
// See note in Window's Paint event
if image <> nil then
DrawImages g
end if
End Sub
#tag EndEvent
#tag EndEvents