-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCCanvas.cls
473 lines (380 loc) · 12.6 KB
/
CCanvas.cls
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "CCanvas"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Compare Database
Option Explicit
Private mvForeColor As Variant
Private mvBackColor As Variant
Private mvPenForeColor As Variant
Private mvPenBackColor As Variant
Private msFontName As String
Private miFontSize As Integer
Private msFilename As String
Private miSelStartRow As Integer
Private miSelStartCol As Integer
Private miSelEndRow As Integer
Private miSelEndCol As Integer
Public Enum eTransparencyMode
eColorTransparency
eAlphaTransparency
End Enum
Private meTransMode As eTransparencyMode
Private mvTransColor As Variant 'Long or null
Private miTransAlphaPct As Integer
Private miLineSpacingTop As Integer
Private miLineSpacingBottom As Integer
Private miLinePaddingTop As Integer
Private miLinePaddingBottom As Integer
Private mfAutoAdjustWidth As Boolean
Private moGrid As CConsoleGrid
'Class error context
Private mlErrNo As Long
Private msErrCtx As String
Private msErrDesc As String
Private Sub ClearErr()
mlErrNo = 0&
msErrCtx = ""
msErrDesc = ""
End Sub
Private Sub SetErr(ByVal psErrCtx As String, ByVal plErrNum As Long, ByVal psErrDesc As String)
mlErrNo = plErrNum
msErrCtx = psErrCtx
msErrDesc = psErrDesc
End Sub
Public Property Get LastErr() As Long
LastErr = mlErrNo
End Property
Public Property Get LastErrDesc() As String
LastErrDesc = msErrDesc
End Property
Private Sub Class_Initialize()
Set moGrid = New CConsoleGrid
End Sub
Private Sub Class_Terminate()
Set moGrid = Nothing
End Sub
Public Property Get ConsoleGrid() As CConsoleGrid
Set ConsoleGrid = moGrid
End Property
Private Function IsValidColorParam(ByRef pvColorParam As Variant) As Boolean
If Not IsNull(pvColorParam) Then
If VarType(pvColorParam) <> vbLong Then
Exit Function
End If
End If
IsValidColorParam = True
End Function
Public Property Get BackColor() As Variant
BackColor = mvBackColor
End Property
Public Property Let BackColor(ByVal pvBackColor As Variant)
If IsValidColorParam(pvBackColor) Then
mvBackColor = pvBackColor
End If
End Property
Public Property Get ForeColor() As Variant
ForeColor = mvForeColor
End Property
Public Property Let ForeColor(ByVal pvForeColor As Variant)
If IsValidColorParam(pvForeColor) Then
mvForeColor = pvForeColor
End If
End Property
Public Property Get PenBackColor() As Variant
PenBackColor = mvPenBackColor
End Property
Public Property Let PenBackColor(ByVal pvPenBackColor As Variant)
If IsValidColorParam(pvPenBackColor) Then
mvPenBackColor = pvPenBackColor
End If
End Property
Public Property Get PenForeColor() As Variant
PenForeColor = mvPenForeColor
End Property
Public Property Let PenForeColor(ByVal pvPenForeColor As Variant)
If IsValidColorParam(pvPenForeColor) Then
mvPenForeColor = pvPenForeColor
End If
End Property
Public Property Get FontName() As String
FontName = msFontName
End Property
Public Property Let FontName(ByVal psFontName As String)
msFontName = psFontName
End Property
Public Property Get FontSize() As Integer
FontSize = miFontSize
End Property
Public Property Let FontSize(ByVal piFontSize As Integer)
miFontSize = piFontSize
End Property
Public Property Get Filename() As String
Filename = msFilename
End Property
Public Property Let Filename(ByVal psFilename As String)
msFilename = psFilename
End Property
Public Property Get SelStartRow() As Integer
SelStartRow = miSelStartRow
End Property
Public Property Let SelStartRow(ByVal piSelStartRow As Integer)
miSelStartRow = piSelStartRow
End Property
Public Property Get SelStartCol() As Integer
SelStartCol = miSelStartCol
End Property
Public Property Let SelStartCol(ByVal piSelStartCol As Integer)
miSelStartCol = piSelStartCol
End Property
Public Property Get SelEndRow() As Integer
SelEndRow = miSelEndRow
End Property
Public Property Let SelEndRow(ByVal piSelEndRow As Integer)
miSelEndRow = piSelEndRow
End Property
Public Property Get SelEndCol() As Integer
SelEndCol = miSelEndCol
End Property
Public Property Let SelEndCol(ByVal piSelEndCol As Integer)
miSelEndCol = piSelEndCol
End Property
Public Property Get TransparentAlphaPct() As Integer
TransparentAlphaPct = miTransAlphaPct
End Property
Public Property Let TransparentAlphaPct(ByVal piPct As Integer)
If (piPct >= 0) And (piPct <= 100) Then
miTransAlphaPct = piPct
End If
End Property
Public Property Get TransparentColor() As Variant
TransparentColor = mvTransColor
End Property
Public Property Let TransparentColor(ByVal pvTransColor As Variant)
If IsValidColorParam(pvTransColor) Then
mvTransColor = pvTransColor
End If
End Property
Public Property Get TransparencyMode() As eTransparencyMode
TransparencyMode = meTransMode
End Property
Public Property Let TransparencyMode(ByVal peTransMode As eTransparencyMode)
meTransMode = peTransMode
End Property
Public Property Get AutoAdjustOnCharWidth() As Boolean
AutoAdjustOnCharWidth = mfAutoAdjustWidth
End Property
Public Property Let AutoAdjustOnCharWidth(ByVal pfAutoAjust As Boolean)
mfAutoAdjustWidth = pfAutoAjust
End Property
Public Property Get LineSpacingTop() As Integer
LineSpacingTop = miLineSpacingTop
End Property
Public Property Let LineSpacingTop(ByVal piPixels As Integer)
miLineSpacingTop = piPixels
End Property
Public Property Get LineSpacingBottom() As Integer
LineSpacingBottom = miLineSpacingBottom
End Property
Public Property Let LineSpacingBottom(ByVal piPixels As Integer)
miLineSpacingBottom = piPixels
End Property
Public Property Get LinePaddingTop() As Integer
LinePaddingTop = miLinePaddingTop
End Property
Public Property Let LinePaddingTop(ByVal piPixels As Integer)
miLinePaddingTop = piPixels
End Property
Public Property Get LinePaddingBottom() As Integer
LinePaddingBottom = miLinePaddingBottom
End Property
Public Property Let LinePaddingBottom(ByVal piPixels As Integer)
miLinePaddingBottom = piPixels
End Property
Public Function LoadFromNativeStream( _
ByVal phStream As Integer, _
ByRef piiProgress As IProgressIndicator, _
Optional ByVal pfAutoResize As Boolean = True _
) As Boolean
Dim fOK As Boolean
Dim iTemp As Integer
Dim fLostBool As Boolean
Dim lLostLong As Long
Dim iLostInt As Integer
Dim sLostString As String
Dim lValue As Long
On Error GoTo LoadFromNativeStream_Err
Const LOCAL_ERR_CTX As String = "LoadFromNativeStream"
piiProgress.SetMax 2
piiProgress.SetValue 1
fOK = moGrid.LoadFromNativeStream(phStream, pfAutoResize:=pfAutoResize)
If Not fOK Then
SetErr LOCAL_ERR_CTX, -1&, moGrid.IIClassError.LastErrDesc
GoTo LoadFromNativeStream_Exit
End If
'load additional info
If Not EOF(phStream) Then
Get #phStream, , lValue
mvForeColor = lValue
Get #phStream, , lValue
mvBackColor = lValue
Get #phStream, , miFontSize
msFontName = FileGetUnicodeString(phStream)
'V02.00.00 load canvas transparency and background picture filename (if any)
piiProgress.SetValue 2
'We test for EOF so we can still import old versions of the file format
If Not EOF(phStream) Then
'V002.00.06:
'Transparency mode is a UI setting that is no more saved.
'Setting was either 0 or 1, corresponding to the chkTransparency value in "Canvas" Form.
'This code was previously in the Form_Canvas code, it's now incorporated in this class.
'To keep compatibility with old versions, we have to read it for nothing.
Get #phStream, , fLostBool 'We do nothing with that
Get #phStream, , iTemp
meTransMode = iTemp
If meTransMode > eAlphaTransparency Then
If Not piiProgress.Console Is Nothing Then
piiProgress.Console.OutputLn LOCAL_ERR_CTX, "invalid value [" & meTransMode & "] at file offset [" & Seek(phStream) & "]"
piiProgress.Console.OutputLn "Transparency mode can be either " & eAlphaTransparency & " (alpha) or " & eColorTransparency & " (color), set to alpha"
End If
meTransMode = eAlphaTransparency
End If
Get #phStream, , lValue
mvTransColor = lValue
Get #phStream, , miTransAlphaPct
If miTransAlphaPct > 100 Then
If Not piiProgress.Console Is Nothing Then
piiProgress.Console.OutputLn LOCAL_ERR_CTX, "invalid value [" & miTransAlphaPct & "] at file offset [" & Seek(phStream) & "]"
piiProgress.Console.OutputLn "Transparency percent must be in [0..100], set to 0 (zero)"
End If
miTransAlphaPct = 100
End If
'Previous version wrongfully saved the background image filename
'msTransImageFilename = FileGetUnicodeString(phStream)
'Read and lose
sLostString = FileGetUnicodeString(phStream)
End If
If Not EOF(phStream) Then
Get #phStream, , iTemp 'line padding top
miLinePaddingTop = iTemp
Get #phStream, , iTemp 'line padding top
miLinePaddingBottom = iTemp
Get #phStream, , iTemp 'line padding top
miLineSpacingTop = iTemp
Get #phStream, , iTemp 'line padding top
miLineSpacingBottom = iTemp
Get #phStream, , fLostBool
mfAutoAdjustWidth = fLostBool
End If
End If
LoadFromNativeStream = fOK
LoadFromNativeStream_Exit:
Exit Function
LoadFromNativeStream_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
Resume LoadFromNativeStream_Exit
Resume
End Function
Public Function LoadNative( _
ByVal psFilename As String, _
ByRef piiProgress As IProgressIndicator, _
Optional ByVal pfAutoResize As Boolean = True _
) As Boolean
Dim hStream As Integer
Dim fIsOpen As Boolean
Dim fOK As Boolean
On Error GoTo LoadNative_Err
Const LOCAL_ERR_CTX As String = "LoadNative"
'Open file for output (overwrite)
hStream = FreeFile
Open psFilename For Binary Access Read Lock Write As #hStream
fIsOpen = True
piiProgress.SetMax 2
piiProgress.SetValue 1
fOK = LoadFromNativeStream(hStream, piiProgress, pfAutoResize)
'Close the file
Close hStream
fIsOpen = False
LoadNative = fOK
LoadNative_Exit:
If fIsOpen Then
Close hStream
End If
Exit Function
LoadNative_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
Resume LoadNative_Exit
Resume
End Function
Public Function SaveToNativeStream(ByVal phStream As Integer, ByRef piiProgress As IProgressIndicator) As Boolean
On Error GoTo SaveToNativeStream_Err
Const LOCAL_ERR_CTX As String = "SaveToNativeStream"
Dim lValue As Long
Dim fLostBool As Boolean
Dim fOK As Boolean
fOK = moGrid.SaveToNativeStream(phStream)
If Not fOK Then
SetErr LOCAL_ERR_CTX, -1&, moGrid.IIClassError.LastErrDesc
GoTo SaveToNativeStream_Exit
End If
'append canvas colors
lValue = CLng(Nz(mvForeColor, 0&))
Put #phStream, , lValue
lValue = CLng(Nz(mvBackColor, 0&))
Put #phStream, , lValue
'append font info
Put #phStream, , miFontSize
FilePutUnicodeString phStream, msFontName
'V02.00.06 This is osbolete; was used to store the app transparency switch
'We have to keep writing it for compatibility.
Put #phStream, , fLostBool
Put #phStream, , (CInt(meTransMode))
lValue = CLng(Nz(mvTransColor, 0&))
Put #phStream, , lValue
Put #phStream, , miTransAlphaPct
'V02.00.06 This is osbolete; was used to store the app bkgnd img filename
'FilePutUnicodeString phStream, msTransImageFilename
FilePutUnicodeString phStream, ""
Put #phStream, , miLinePaddingTop
Put #phStream, , miLinePaddingBottom
Put #phStream, , miLineSpacingTop
Put #phStream, , miLineSpacingBottom
Put #phStream, , mfAutoAdjustWidth
SaveToNativeStream = fOK
SaveToNativeStream_Exit:
Exit Function
SaveToNativeStream_Err:
SetErr LOCAL_ERR_CTX, Err.Number, Err.Description
Resume SaveToNativeStream_Exit
Resume
End Function
Public Function SaveNative(ByVal psFilename As String, ByRef piiProgress As IProgressIndicator) As Boolean
Dim hStream As Integer
Dim fIsOpen As Boolean
Dim fOK As Boolean
Dim fBoolValue As Boolean
Dim iTemp As Integer
On Error GoTo SaveNative_Err
'Open file for output (overwrite)
hStream = FreeFile
Open psFilename For Binary Access Write Lock Read Write As #hStream
fIsOpen = True
fOK = SaveToNativeStream(hStream, piiProgress)
'Close the file
Close hStream
fIsOpen = False
SaveNative = fOK
SaveNative_Exit:
If fIsOpen Then
Close hStream
End If
Exit Function
SaveNative_Err:
ShowUFError "Failed to save file " & psFilename, Err.Description
Resume SaveNative_Exit
End Function