-
Notifications
You must be signed in to change notification settings - Fork 2
/
wxCalc.lua
500 lines (384 loc) · 13.4 KB
/
wxCalc.lua
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
-- ----------------------------------------------------------------------------
--
-- wxCalc - window for conversion Unicode <-> UTF_8
--
-- ----------------------------------------------------------------------------
local wx = require "wx" -- uses wxWidgets for Lua 5.2
local palette = require "wxPalette" -- common colours definition in wxWidgets
require "extrastr" -- check UTF_8 byte sequence validity
local _floor = math.floor
local _format = string.format
local _utf8lkp = string.utf8lkp
local _insert = table.insert
-- ----------------------------------------------------------------------------
-- reference to the application
--
local m_App = nil
-- ----------------------------------------------------------------------------
-- window's private members
--
local m_Frame =
{
hWindow = nil, -- main frame
edUnicode = nil, -- Unicode edit control
edSequence = nil, -- bytes' sequence control
ckSequence = nil, -- check sequence against Unicode ABNF
txMsg = nil, -- label for message
}
-- ----------------------------------------------------------------------------
-- preallocated GDI objects
--
local m_clrBack = palette.Gray15
local m_clrFore = palette.Honeydew2
local m_clrExtra = palette.OrangeRed
-- ----------------------------------------------------------------------------
-- return the wxWindow handle
--
local function GetHandle()
return m_Frame.hWindow
end
-- ----------------------------------------------------------------------------
-- check the bytes' sequence to be a proper Unicode sequence
-- as described in the RFC 3629 document
-- wants a normalized hex string
--
local function _checkUTF_8(inText)
local sByte = inText:sub(1, 2)
local iByte = tonumber(sByte, 16)
-- trick to check for the second byte
--
if "C0" == sByte or "C1" == sByte then sByte = "C2" end
-- get the scrutiny row using the first byte
--
local tUtf8Row = _utf8lkp(iByte)
if not tUtf8Row then return "UTF8 row no found" end
-- check the number of required bytes
-- (by accident the 2 arrays size must match)
--
if #tUtf8Row > #inText then return "Too few bytes" end
if #tUtf8Row < #inText then return "Too many bytes" end
-- check the remaining bytes against the description
--
for i=3, #tUtf8Row, 2 do
sByte = inText:sub(i, i + 1)
iByte = tonumber(sByte, 16)
-- check if within bounds
--
if tUtf8Row[i] > iByte or iByte > tUtf8Row[i + 1] then
return _format("At: %d offending: %s", i, sByte)
end
end
-- alles good
--
return ""
end
-- ----------------------------------------------------------------------------
-- make a string array represent a valid hexadecimal code
--
local function _norm(inText)
if 0 == inText:len() then return "00" end
inText = inText:gsub(" ", "")
inText = inText:upper()
inText = inText:sub(1, math.min(#inText, 8))
if 1 == (inText:len() % 2) then inText = "0" .. inText end
return inText
end
-- ----------------------------------------------------------------------------
-- convert an ASCII string of hex values into a table of bytes
--
local function _text2hex(inText)
local tBytes = { }
local iHexVal
for i=1, #inText do
iHexVal = (inText:byte(i) - 0x30)
if 0x09 < iHexVal then iHexVal = iHexVal - 0x07 end
_insert(tBytes, iHexVal)
end
return tBytes
end
-- ----------------------------------------------------------------------------
-- split a string into a table of bytes
-- (converts from text hex to number)
-- assume the string evenly aligned
--
local function _text2num(inText)
local iNibbleH, iNibbleL
local tBytes = { }
-- convert from hex text to number
-- byte by byte (2 chars at time)
--
for i=1, #inText, 2 do
iNibbleH = (inText:byte(i) - 0x30)
if 0x09 < iNibbleH then iNibbleH = iNibbleH - 0x07 end
iNibbleH = iNibbleH * 0x10
iNibbleL = (inText:byte(i+1) - 0x30)
if 0x09 < iNibbleL then iNibbleL = iNibbleL - 0x07 end
_insert(tBytes, iNibbleH + iNibbleL)
end
return tBytes
end
-- ----------------------------------------------------------------------------
-- get a Unicode reference number and convert it to its UTF_8 representation
-- (assume a properly formatted hex number was passed as argument)
--
local function _uni2bytes(inText)
local iSumUp = tonumber(inText, 16)
local tBytes = _text2hex(inText)
local sValue = ""
local iByte1 = 0
local iByte2 = 0
local iByte3 = 0
local iByte4 = 0
-- sanity check
--
if not iSumUp then return sValue end
if 0x0080 > iSumUp then
sValue = _format("%02x", iSumUp)
elseif 0x07d0 > iSumUp then
-- c2-df block (2 bytes)
------------------------
iByte4 = 0x80 + (tBytes[4] % 0x10) + 0x10 * (tBytes[3] % 0x04)
iByte3 = 0xc0 + _floor(tBytes[3] / 0x04) + 0x04 * (tBytes[2] % 0x08)
sValue = _format("%02x %02x", iByte3, iByte4)
elseif 0x010000 > iSumUp then
-- e0-ef block (3 bytes)
------------------------
iByte4 = 0x80 + (tBytes[4] % 0x10) + 0x10 * (tBytes[3] % 0x04)
iByte3 = 0x80 + _floor(tBytes[3] / 0x04) + 0x04 * (tBytes[2] % 0x10)
iByte2 = 0xe0 + tBytes[1]
if 0x00 == tBytes[1] then iByte3 = iByte3 + 1 end -- correct the start
sValue = _format("%02x %02x %02x", iByte2, iByte3, iByte4)
else
-- f0-f4 block (4 bytes)
------------------------
iByte4 = 0x80 + (tBytes[6] % 0x10) + 0x10 * (tBytes[5] % 0x04)
iByte3 = 0x80 + _floor(tBytes[5] / 0x04) + 0x04 * (tBytes[4] % 0x10)
iByte2 = 0x80 + (tBytes[3] % 0x10) + 0x10 * (tBytes[2] % 0x04)
iByte1 = 0xf0 + _floor(tBytes[2] / 0x04) + 0x04 * (tBytes[1] % 0x10)
sValue = _format("%02x %02x %02x %02x", iByte1, iByte2, iByte3, iByte4)
end
return sValue
end
-- ----------------------------------------------------------------------------
-- get a byte array and try to convert it to an Unicode reference number
-- expect an array made of 8 characters maximum
-- where each character is a nibble
--
local function _bytes2uni(inText)
local tBytes = _text2num(inText) -- split values
local iRefUTF8 = -1
-- if the code entered is invalid these arithmetics will overflow
--
if 4 == #tBytes then
iRefUTF8 = ((tBytes[1] - 0xf0) * 0x40000) + ((tBytes[2] - 0x80) * 0x1000) + ((tBytes[3] - 0x80) * 0x40) + (tBytes[4] - 0x80)
elseif 3 == #tBytes then
if 0xe0 == tBytes[1] then
iRefUTF8 = ((tBytes[1] - 0xe0) * 0x1000) + ((tBytes[2] - 0x81) * 0x40) + (tBytes[3] - 0x80)
else
iRefUTF8 = ((tBytes[1] - 0xe0) * 0x1000) + ((tBytes[2] - 0x80) * 0x40) + (tBytes[3] - 0x80)
end
elseif 2 == #tBytes then
iRefUTF8 = ((tBytes[1] - 0xc0) * 0x40) + (tBytes[2] - 0x80)
else
-- this is not strictly an error
-- mark with a negative number
--
end
if 0 > iRefUTF8 then return "" end
-- Unicode prints its documents with uppercase letters
-- Unicode uses a 5 nibbles format (not 6) for long codes
--
return _format("%04X", iRefUTF8)
end
-- ----------------------------------------------------------------------------
--
local function OnUnicodeEnter()
-- trace.line("OnUnicodeEnter")
local edUnico = m_Frame.edUnicode
local edBytes = m_Frame.edSequence
local txMsg = m_Frame.txMsg
txMsg:SetLabel("") -- cleanup
edBytes:SetValue("") -- cleanup
local sText = edUnico:GetValue()
if 3 >= sText:len() then return end
sText = _norm(sText) -- normalize
sText = _uni2bytes(sText) -- convert it
edBytes:SetValue(sText) -- show result
txMsg:SetLabel("") -- auto cleanup
end
-- ----------------------------------------------------------------------------
--
local function OnSequenceEnter()
-- trace.line("OnSequenceEnter")
local edUnico = m_Frame.edUnicode
local edBytes = m_Frame.edSequence
local txMsg = m_Frame.txMsg
txMsg:SetLabel("") -- cleanup
edUnico:SetValue("") -- cleanup
local sText = edBytes:GetValue()
if 3 >= sText:len() then return end
sText = _norm(sText) -- normalize
sText = _bytes2uni(sText) -- convert it
edUnico:SetValue(sText) -- show result
txMsg:SetLabel("") -- auto cleanup
-- do the extra check
--
if true == m_Frame.ckSequence:IsChecked() then
local sChecked = _checkUTF_8(_norm(edBytes:GetValue()))
m_Frame.txMsg:SetLabel(sChecked)
end
end
-- ----------------------------------------------------------------------------
-- handle the show window event
--
local function OnShow(event)
-- trace.line("OnShow")
if not m_Frame.hWindow then return end
if event:GetShow() then
m_Frame.edUnicode:SetFocus()
end
end
-- ----------------------------------------------------------------------------
--
local function IsWindowVisible()
local wFrame = m_Frame.hWindow
if not wFrame then return false end
return wFrame:IsShown()
end
-- ----------------------------------------------------------------------------
-- show/hide the window
--
local function DisplayWindow(inShowStatus)
-- trace.line("DisplayWindow")
local wFrame = m_Frame.hWindow
if not wFrame then return end
-- display/hide
--
wFrame:Show(inShowStatus)
end
-- ----------------------------------------------------------------------------
--
local function OnClose()
-- trace.line("OnClose")
local wFrame = m_Frame.hWindow
if not wFrame then return end
-- finally destroy the window
--
wFrame:Destroy(wFrame)
m_Frame.hWindow = nil
end
-- ----------------------------------------------------------------------------
--
local function CloseWindow()
-- trace.line("CloseWindow")
OnClose()
end
-- ----------------------------------------------------------------------------
-- assign back and fore colors to all controls in dialog
--
local function SetupColour(inBack, inFront, inExtra, inFont)
-- trace.line("SetupColour")
m_clrBack = inBack
m_clrFore = inFront
m_clrExtra = inExtra
local wlist = m_Frame.hWindow:GetChildren()
local wNode = wlist:Item(0)
local ctrl
-- cycle all controls in the dialog window
--
while wNode do
ctrl = wNode:GetData():DynamicCast("wxWindow")
ctrl:SetBackgroundColour(m_clrBack)
ctrl:SetForegroundColour(m_clrFore)
-- if a font was supplied then change it
--
if inFont then ctrl:SetFont(inFont) end
wNode = wNode:GetNext()
end
m_Frame.hWindow:SetBackgroundColour(m_clrBack)
m_Frame.txMsg:SetForegroundColour(m_clrExtra)
if IsWindowVisible() then m_Frame.hWindow:Refresh() end
end
-- ----------------------------------------------------------------------------
-- create the main window
--
local function CreateWindow(inApp, inParent, inConfig)
-- trace.line("CreateWindow")
inParent = inParent or wx.NULL
-- flags in use for the frame - note that lacks the resize border
--
local dwFrameFlags = bit32.bor(wx.wxFRAME_TOOL_WINDOW, wx.wxCAPTION)
dwFrameFlags = bit32.bor(dwFrameFlags, wx.wxFRAME_FLOAT_ON_PARENT)
-- create a window
--
local ptLeft = inConfig[1]
local ptTop = inConfig[2]
local siWidth = inConfig[3]
local siHeight = inConfig[4]
local frame = wx.wxFrame(inParent, wx.wxID_ANY, "Calculator",
wx.wxPoint(ptLeft, ptTop),
wx.wxSize(siWidth, siHeight),
dwFrameFlags)
-- standard event handlers
--
frame:Connect(wx.wxEVT_SHOW, OnShow)
frame:Connect(wx.wxEVT_CLOSE_WINDOW, OnClose)
-- validator for the text controls
--
local validator = wx.wxTextValidator(wx.wxFILTER_INCLUDE_CHAR_LIST)
local tFilter = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"a", "b", "c", "d", "e", "f",
"A", "B", "C", "D", "E", "F", " ",
}
validator:SetIncludes(tFilter)
-- edit controls for typing values
--
local edUnico, edBytes, ckSeqnc, txMsg
wx.wxStaticText(frame, wx.wxID_ANY, "Unicode Value: U+",
wx.wxPoint(10, 40), wx.wxSize(200, 50))
edUnico = wx.wxTextCtrl(frame, wx.wxID_ANY, "",
wx.wxPoint(250, 38), wx.wxSize(150, 40),
wx.wxTE_PROCESS_ENTER, validator)
wx.wxStaticText(frame, wx.wxID_ANY, "Hex Sequence:",
wx.wxPoint(10, 100), wx.wxSize(200, 50))
edBytes = wx.wxTextCtrl(frame, wx.wxID_ANY, "",
wx.wxPoint(250, 98), wx.wxSize(230, 40),
wx.wxTE_PROCESS_ENTER, validator)
ckSeqnc = wx.wxCheckBox(frame, wx.wxID_ANY, "Check Sequence",
wx.wxPoint(250, 150), wx.wxSize(230, 40))
txMsg = wx.wxStaticText(frame, wx.wxID_ANY, "",
wx.wxPoint(250, 190), wx.wxSize(230, 40))
-- event handlers
--
edUnico:Connect(wx.wxEVT_COMMAND_TEXT_ENTER, OnUnicodeEnter)
edBytes:Connect(wx.wxEVT_COMMAND_TEXT_ENTER, OnSequenceEnter)
-- set the error check enabled
--
ckSeqnc:SetValue(true)
-- store for later
--
m_Frame.hWindow = frame
m_Frame.edUnicode = edUnico
m_Frame.edSequence = edBytes
m_Frame.ckSequence = ckSeqnc
m_Frame.txMsg = txMsg
m_App = inApp
-- assign colours and font
--
local fnText = wx.wxFont(-17, wx.wxFONTFAMILY_SWISS, wx.wxFONTFLAG_ANTIALIASED,
wx.wxFONTWEIGHT_LIGHT, false, "Lucida Sans Unicode")
SetupColour(m_clrBack, m_clrFore, m_clrExtra, fnText)
end
-- ----------------------------------------------------------------------------
--
return
{
GetHandle = GetHandle,
Create = CreateWindow,
Display = DisplayWindow,
Close = CloseWindow,
IsVisible = IsWindowVisible,
SetupColour = SetupColour,
}
-- ----------------------------------------------------------------------------
-- ----------------------------------------------------------------------------