-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwxMain.lua
More file actions
3065 lines (2407 loc) · 82.3 KB
/
wxMain.lua
File metadata and controls
3065 lines (2407 loc) · 82.3 KB
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- ----------------------------------------------------------------------------
-- ----------------------------------------------------------------------------
--
-- wxMain - main window for the application
--
-- ----------------------------------------------------------------------------
local wx = require "wx" -- uses wxWidgets for Lua 5.2
local palette = require "wxPalette" -- common colours definition in wxWidgets
local trace = require "trace" -- shortcut for tracing
local ticktime = require "ticktimer" -- timer object constructor
local random = require "random" -- random number generator
local wxLoupe = require "wxLoupe" -- display magnification of current char
local wxCalc = require "wxCalc" -- dialog for conversion bytes <-> Unicode
local wxFind = require "wxFind" -- find text within the file
require "extrastr" -- extra string processor
local _floor = math.floor
local _max = math.max
local _min = math.min
local _concat = table.concat
local _format = string.format
local _find = string.find
local _strrep = string.rep
local _byte = string.byte
local _utf8sub = string.utf8sub -- extract utf8 bytes (1-4)
-- ----------------------------------------------------------------------------
-- status bar panes width
--
local tStbarWidths =
{
180, -- application
750, -- message
150, -- codepage
120, -- errors
90, -- current line
160, -- cursor position
}
-- ----------------------------------------------------------------------------
--
local tSchemeWhite =
{
["LeftBack"] = palette.White,
["ColourColumn"]= palette.Honeydew,
["LeftText"] = palette.Black,
["LeftCursor"] = palette.Orchid2,
["Linefeed"] = palette.Red,
["Unprintable"] = palette.Purple3,
["MarkStart"] = palette.CadetBlue2,
["HighBits"] = palette.NavajoWhite1,
["RightBack"] = palette.White,
["RightText"] = palette.Black,
["RightCursor"] = palette.Blue1,
["VerticalBar"] = palette.SlateGray2,
["StopRow"] = palette.Wheat2,
["Gutter"] = palette.Gray70,
["SlideActive"] = palette.Black,
["LoupeBack"] = palette.Ivory,
["LoupeFore"] = palette.Black,
["LoupeExtra"] = palette.Goldenrod2,
["DialogsBack"] = palette.GhostWhite,
["DialogsFore"] = palette.Black,
["DialogsExtra"]= palette.Turquoise3,
}
local tSchemeLight =
{
["LeftBack"] = palette.Gray90,
["ColourColumn"]= palette.Gray94,
["LeftText"] = palette.Gray30,
["LeftCursor"] = palette.DarkSeaGreen3,
["Linefeed"] = palette.Gray15,
["Unprintable"] = palette.Magenta,
["MarkStart"] = palette.LightPink2,
["HighBits"] = palette.LightBlue2,
["RightBack"] = palette.Seashell2,
["RightText"] = palette.Gray40,
["RightCursor"] = palette.Black,
["VerticalBar"] = palette.CadetBlue,
["StopRow"] = palette.Burlywood1,
["Gutter"] = palette.Snow3,
["SlideActive"] = palette.Black,
["LoupeBack"] = palette.Seashell2,
["LoupeFore"] = palette.Gray30,
["LoupeExtra"] = palette.SteelBlue3,
["DialogsBack"] = palette.Cornsilk,
["DialogsFore"] = palette.Black,
["DialogsExtra"]= palette.DarkOrchid,
}
local tSchemeDark =
{
["LeftBack"] = palette.SlateBlue4,
["ColourColumn"]= palette.NavyBlue,
["LeftText"] = palette.Gray80,
["LeftCursor"] = palette.VioletRed1,
["Linefeed"] = palette.DeepPink2,
["Unprintable"] = palette.Green,
["MarkStart"] = palette.Sienna,
["HighBits"] = palette.Cyan4,
["RightBack"] = palette.DarkSlateGray,
["RightText"] = palette.Gray70,
["RightCursor"] = palette.Azure1,
["VerticalBar"] = palette.PeachPuff,
["StopRow"] = palette.Salmon4,
["Gutter"] = palette.SteelBlue2,
["SlideActive"] = palette.Yellow,
["LoupeBack"] = palette.MediumPurple4,
["LoupeFore"] = palette.White,
["LoupeExtra"] = palette.PaleGoldenrod,
["DialogsBack"] = palette.NavyBlue,
["DialogsFore"] = palette.White,
["DialogsExtra"]= palette.DeepPink,
}
local tSchemeBlack =
{
["LeftBack"] = palette.Gray15,
["ColourColumn"]= palette.Gray35,
["LeftText"] = palette.Gray90,
["LeftCursor"] = palette.SeaGreen3,
["Linefeed"] = palette.Thistle1,
["Unprintable"] = palette.Magenta,
["MarkStart"] = palette.SlateBlue,
["HighBits"] = palette.DarkGoldenrod,
["RightBack"] = palette.Black,
["RightText"] = palette.Gray80,
["RightCursor"] = palette.LightGoldenrod,
["VerticalBar"] = palette.Wheat3,
["StopRow"] = palette.IndianRed4,
["Gutter"] = palette.MediumSlateBlue,
["SlideActive"] = palette.MediumAquamarine,
["LoupeBack"] = palette.Gray6,
["LoupeFore"] = palette.LightYellow2,
["LoupeExtra"] = palette.OrangeRed,
["DialogsBack"] = palette.Gray15,
["DialogsFore"] = palette.White,
["DialogsExtra"]= palette.Green,
}
-- ----------------------------------------------------------------------------
-- (pre)allocate pens and brushes
--
local m_PenNull = wx.wxPen(palette.Black, 1, wx.wxTRANSPARENT)
local m_BrushNull = wx.wxBrush(palette.Black, wx.wxTRANSPARENT)
-- bytes pane
--
local m_penLF = wx.wxPen(tSchemeWhite.Linefeed, 3, wx.wxSOLID)
local m_penXX = wx.wxPen(tSchemeWhite.Unprintable, 3, wx.wxSOLID)
local m_brMrk = wx.wxBrush(tSchemeWhite.MarkStart, wx.wxSOLID)
local m_brHBt = wx.wxBrush(tSchemeWhite.HighBits, wx.wxSOLID)
local m_brCur = wx.wxBrush(tSchemeWhite.LeftCursor, wx.wxSOLID)
-- text pane
--
local m_penUnd = wx.wxPen(tSchemeWhite.RightCursor, 6, wx.wxSOLID)
local m_brStp = wx.wxBrush(tSchemeWhite.StopRow, wx.wxSOLID)
local m_penBar = wx.wxPen(tSchemeWhite.VerticalBar, 1, wx.wxSOLID)
local m_brBar = wx.wxBrush(tSchemeWhite.VerticalBar, wx.wxSOLID)
-- ----------------------------------------------------------------------------
-- format to use for the bytes pane
-- (the 2nd element is a precalc of the length)
--
local tFormat =
{
["Oct"] = {"%04o ", 5},
["Dec"] = {"%03d ", 4},
["Hex"] = {"%02x ", 3},
}
-- ----------------------------------------------------------------------------
-- ticktimers
--
local tTimers =
{
-- name interval last fired
--
["Display"] = ticktime.new("Display"),
["Garbage"] = ticktime.new("Garbage"),
["Encoded"] = ticktime.new("Encoded"),
}
-- ----------------------------------------------------------------------------
-- reference to the application
--
local m_App = nil
-- ----------------------------------------------------------------------------
-- window's private members
--
local m_Frame =
{
hWindow = nil, -- main frame
hLoupe = nil, -- loupe window
hCalcUni = nil, -- calculator window
hFindText = nil, -- find text window
bVisible = false,-- last visibile status
hStatusBar = nil, -- the status bar
hSlidesDC = nil, -- background for the 2 slides
hMemoryDC = nil, -- device context for the window
hTickTimer = nil, -- timer for messages in the statusbar
iTmInterval = 3000, -- diplay msg on status bar for much time
hFontBytes = nil, -- the font for the bytes (left) pane
hFontText = nil, -- the font for the text (right) pane
tColourScheme = tSchemeLight, -- assign colours' table
iCurrentSlide = 1, -- the slide being selected
rcClientW = 0, -- client rect width
rcClientH = 0, -- client rect height
iOffsetX = 6, -- offset for writing
iOffsetY = 14, -- offset for writing
iRightOffsetX = 0, -- offset for the right pane (precalc)
iRightOffsetY = 0, -- offset for the right pane (precalc)
iLeftMonoWidth = 0, -- pixels for 1 byte display
iLeftSpacerX = 0, -- pixels for formatted string (oct/dec/hex)
iLeftSpacerY = 0, -- pixels for the height
iRightSpacerX = 0, -- not used
iRightSpacerY = 0, -- height in pixels of character
tFormatBytes = nil, -- format string for the bytes display
sTabReplace = "", -- replacement for tab on text slide
sSpaceSub = "", -- replacement for spaces if hide selected _strrep(" ", tFmtBytes[2])
iByteRowCount = 0, -- number of visibles rows
iByteFirstRow = 0, -- first row visible, left rect
iTextRowCount = 0, -- number of visible rows
iTextFirstRow = 0, -- first row visible, right rect
}
-- ----------------------------------------------------------------------------
--
local m_Cursor =
{
iAbsOffset = -1, -- cursor position absolute within the file
iRelOffset = -1, -- cursor position relative to current line
iStopLine = -1, -- current line index within the file
tCurrLine = nil, -- current line {prev bytes count, text}
sCurrChar = "", -- current character (1 to 4 bytes)
}
-- ----------------------------------------------------------------------------
--
local m_Marker =
{
bMouseDown = false, -- the left mouse button is down
bShiftDown = false, -- shift key is down
iPosStart = 1, -- start marker
iPosStop = 1, -- stop marker
}
-- ----------------------------------------------------------------------------
--
local m_Errors =
{
iTotals = 0, -- returned number of errors
iCurrent = 0, -- current error index
tCurrErr = { }, -- copy of current error
sFormat = "Err: %d/%d", -- format string for statustext
}
-- ----------------------------------------------------------------------------
--
local m_Codepage =
{
tUserReq = { }, -- name, id : where name = ISO/OEM/WIN
iGuessCnt = 0, -- number of tests so far
iGuessLmt = 0, -- upper limit for guess
tGuess = { }, -- guess codepage format
bHardCnvtd = false, -- user did a UTF_8 conversion
sDisplay = "", -- string for statustext
randomizer = random.new()
}
-- ----------------------------------------------------------------------------
-- Generate a unique new wxWindowID
--
local iRCEntry = wx.wxID_HIGHEST + 1
local function UniqueID()
iRCEntry = iRCEntry + 1
return iRCEntry
end
-- ----------------------------------------------------------------------------
-- default dialogs' rectangles and visibility
--
local m_WinIni = "winini.lua"
local tWindows = nil
-- ----------------------------------------------------------------------------
-- return the wxWindow handle
--
local function GetHandle()
return m_Frame.hWindow
end
-- ----------------------------------------------------------------------------
-- reset the errors' values
--
local function ErrorsReset()
-- trace.line("ErrorsReset")
m_Errors.iCurrent = 0
m_Errors.iTotals = 0
m_Errors.tCurrErr = { }
end
-- ----------------------------------------------------------------------------
-- reset the marker's values
--
local function MarkerReset()
-- trace.line("MarkerReset")
m_Marker.bMouseDown = false
m_Marker.bShiftDown = false
m_Marker.iPosStart = 1
m_Marker.iPosStop = 1
end
-- ----------------------------------------------------------------------------
-- reset the cursor's values
--
local function CursorReset()
-- trace.line("CursorReset")
m_Frame.iByteFirstRow = 0
m_Frame.iByteRowCount = 0
m_Frame.iTextFirstRow = 0
m_Frame.iTextRowCount = 0
m_Cursor.iAbsOffset = -1
m_Cursor.iRelOffset = -1
m_Cursor.iStopLine = -1
m_Cursor.tCurrLine = nil
m_Cursor.sCurrChar = ""
end
-- ----------------------------------------------------------------------------
-- reset the codepage's values
--
local function CodepageReset()
trace.line("CodepageReset")
m_Codepage.tUserReq = { }
m_Codepage.iGuessCnt = 0
m_Codepage.iGuessLmt = 0
m_Codepage.tGuess = { }
m_Codepage.bHardCnvtd = false
m_Codepage.sDisplay = ""
end
-- ----------------------------------------------------------------------------
-- load configuration for the windows' rectangle and visibility
-- (set visibility with 1 = true)
--
local function LoadDlgRects()
-- trace.line("LoadDlgRects")
local fhSrc = io.open(m_WinIni, "r")
if fhSrc then
fhSrc:close()
tWindows = dofile(m_WinIni)
end
if not tWindows then
tWindows =
{
["Main"] = { 0, 0, 1000, 1500, 1},
["Loupe"] = {1000, 650, 500, 560, 0},
["Calc"] = {1000, 150, 500, 280, 0},
["Find"] = {1000, 430, 500, 250, 0},
}
end
end
-- ----------------------------------------------------------------------------
-- format values for writing to a Lua's syntax file
--
local function DlgRect2Lua(inLabel, inHWindow, inConfig)
if inHWindow then
local iPosX = inHWindow:GetPosition().x
local iPosY = inHWindow:GetPosition().y
local iWidth = inHWindow:GetSize():GetWidth()
local iHeight = inHWindow:GetSize():GetHeight()
local iVisible = 0
if inHWindow:IsShown() then iVisible = 1 end
-- correct negative offsets
--
if 0 > iPosX then iPosX = 0 end
if 0 > iPosY then iPosY = 0 end
-- check minimum sizes
--
if 0 >= iWidth then iWidth = 50 end
if 0 >= iHeight then iHeight = 50 end
-- override default configuration
--
inConfig = {iPosX, iPosY, iWidth, iHeight, iVisible}
end
-- returned line of text
--
local sLine
sLine = "{" .. _concat(inConfig, ", ") .. "},\n"
sLine = "\t[\"" .. inLabel .. "\"]\t= " .. sLine
return sLine, inConfig
end
-- ----------------------------------------------------------------------------
-- save configuration for the windows' position and size
--
local function SaveDlgRects()
-- trace.line("SaveDlgRects")
local fhTgt = io.open(m_WinIni, "w")
if not fhTgt then return false end
local sSep = "-- " .. _strrep("-", 77) .. "\n"
local sLine
fhTgt:write(sSep)
sLine = "-- last used dialogs\' rectangles\n--\n"
fhTgt:write(sLine)
-- start of table's defintion
--
sLine = "local tWindows =\n{\n"
fhTgt:write(sLine)
-- main window
--
sLine, tWindows.Main = DlgRect2Lua("Main", GetHandle(), tWindows.Main)
fhTgt:write(sLine)
-- all dialogs
--
local wCurrent
if m_Frame.hLoupe then wCurrent = m_Frame.hLoupe.GetHandle() else wCurrent = nil end
sLine, tWindows.Loupe = DlgRect2Lua("Loupe", wCurrent, tWindows.Loupe)
fhTgt:write(sLine)
if m_Frame.hCalcUni then wCurrent = m_Frame.hCalcUni.GetHandle() else wCurrent = nil end
sLine, tWindows.Calc = DlgRect2Lua("Calc", wCurrent, tWindows.Calc)
fhTgt:write(sLine)
if m_Frame.hFindText then wCurrent = m_Frame.hFindText.GetHandle() else wCurrent = nil end
sLine, tWindows.Find = DlgRect2Lua("Find", wCurrent, tWindows.Find)
fhTgt:write(sLine)
-- end of table's defintion
--
sLine = "}\n\nreturn tWindows\n\n"
fhTgt:write(sLine)
fhTgt:write(sSep)
fhTgt:write(sSep)
fhTgt:close()
return true
end
-- ----------------------------------------------------------------------------
-- get the correct spacing
--
local function CalcFontSpacing(inDrawDC)
-- trace.line("CalcFontSpacing")
if not inDrawDC then return end
if not m_Frame.hFontBytes then return end
if not m_Frame.hFontText then return end
-- left pane, presume a mono-spaced font used
--
inDrawDC:SetFont(m_Frame.hFontBytes)
local sTest = _format(m_Frame.tFormatBytes[1], 0x00)
m_Frame.iLeftMonoWidth = inDrawDC:GetTextExtent("0")
m_Frame.iLeftSpacerX = inDrawDC:GetTextExtent(sTest)
m_Frame.iLeftSpacerY = inDrawDC:GetCharHeight()
-- right pane
--
inDrawDC:SetFont(m_Frame.hFontText)
m_Frame.iRightSpacerX = 0
m_Frame.iRightSpacerY = inDrawDC:GetCharHeight()
end
-- ----------------------------------------------------------------------------
-- draw a vertical bar of size of 1 page
-- if no text then it will be not shown
--
local function DrawVerticalBar(inDrawDC)
-- trace.line("DrawVerticalBar")
if not inDrawDC then return end
if 0 == m_App.iFileBytes then return end
-- get the correct spacing here
--
local iOffY = m_Frame.iOffsetY
local iHeight = m_Frame.rcClientH - iOffY * 2
local iNumRows = #m_App.tFileLines -- total file's lines
local iVIORows = m_Frame.iTextRowCount -- max visible rows
local iCurPage = (m_Frame.iTextFirstRow / iVIORows) -- current page shown
local iTotPages = (iNumRows / iVIORows) -- pages making the file
local iPageLen = (iHeight / iTotPages) -- hieght of the bar
local iPosY = (iPageLen * iCurPage) -- absolute position
-- just fix it when too small
-- (it affects only the appereance)
--
iPageLen = _max(iPageLen, 25)
-- fix for not rounding divisions
--
if iHeight <= (iPosY + iPageLen) then iPosY = iHeight - iPageLen end
-- draw a rect for the bar itself and a line all through the slide's height
--
inDrawDC:SetPen(m_penBar)
inDrawDC:SetBrush(m_brBar)
inDrawDC:DrawRectangle(m_Frame.rcClientW - 20, iOffY, 2, iHeight - iOffY)
inDrawDC:DrawRoundedRectangle(m_Frame.rcClientW - 30, iPosY, 20, iPageLen, 5)
end
-- ----------------------------------------------------------------------------
-- draw highlight for each even column
--
local function DrawColumns(inDrawDC)
-- trace.line("DrawColumns")
if not inDrawDC then return end
if not m_Frame.hFontBytes then return end
local iCurX = m_Frame.iOffsetX
local iColumns = m_App.tConfig.Columns
local iSpacerX = m_Frame.iLeftSpacerX
local tScheme = m_Frame.tColourScheme
inDrawDC:SetPen(m_PenNull)
inDrawDC:SetBrush(wx.wxBrush(tScheme.ColourColumn, wx.wxSOLID))
-- center the highlight on the number of chars written (hex/dec/oct)
--
iCurX = iCurX - (iSpacerX / (m_Frame.tFormatBytes[2] * 2)) + iSpacerX
while iColumns > 0 do
inDrawDC:DrawRectangle(iCurX, 0, iSpacerX, m_Frame.rcClientH)
iCurX = iCurX + iSpacerX * 2
iColumns = iColumns - 2
end
end
-- ----------------------------------------------------------------------------
-- extract the current char at offset's position within the current line
-- works backward to find the starting index
-- (the character's composition will span from 1 to 4 bytes)
--
local function SetCurrentChar(inBUffer, inOffset)
-- trace.line("SetCurrentChar")
local sUTF_8, iRetCode = _utf8sub(inBUffer, inOffset)
local iSignal = iRetCode
local iStart = inOffset
while (0 > iRetCode) and (0 < iStart) do
iStart = iStart - 1
sUTF_8, iRetCode = _utf8sub(inBUffer, iStart)
end
-- this is an error case, the character is not a valid
-- UTF_8 code and the algorithm felt on a previous char
-- which happened to be valid by chance
--
if ((0 > iSignal) and (1 == sUTF_8:len())) then
m_Cursor.sCurrChar = sUTF_8
return inOffset
end
if 0 < iRetCode then
-- the character is valid
--
m_Cursor.sCurrChar = sUTF_8
return iStart
end
-- try to handle an error case
--
m_Cursor.sCurrChar = inBUffer:sub(inOffset, inOffset)
return inOffset
end
-- ----------------------------------------------------------------------------
-- drawing the left pane
-- will also set the current char selected
--
local function DrawBytes(inDrawDC)
-- trace.line("DrawBytes")
-- quit if no data
--
if 0 >= m_App.iFileBytes then return end
if 0 >= m_Cursor.iAbsOffset then return end
-- here we go
--
local iCursor = m_Cursor.iAbsOffset
local iNumCols = m_App.tConfig.Columns
local tFile = m_App.tFileLines
local iOffX = m_Frame.iOffsetX
local iOffY = m_Frame.iOffsetY
local iCurX = iOffX
local iCurY = iOffY
local tFmtBytes = m_Frame.tFormatBytes -- format table to use (hex/dec/oct)
local bUnderline = m_App.tConfig.Underline -- underline bytes below 0x20
local bUnicode = m_App.tConfig.ColourCodes -- highlight Unicode codes
local tScheme = m_Frame.tColourScheme -- colour scheme in use
-- this is the number of visible rows in the drawing area
-- refresh it now
--
m_Frame.iByteRowCount = 0
-- foreground
--
inDrawDC:SetFont(m_Frame.hFontBytes)
inDrawDC:SetTextForeground(tScheme.LeftText)
-- get the correct spacing here
--
local iSpacerX = m_Frame.iLeftMonoWidth
local iSpacerY = m_Frame.iLeftSpacerY
local iRectH = m_Frame.rcClientH - iOffY * 2 - iSpacerY
------------------------
-- fill tables to flush
--
local tChars = { } -- current set of tokens
local tDraw1 = { } -- underline \n \r
local tDraw2 = { } -- underline unprintable
local tDraw3 = { } -- rect under UTF_8 mark byte
local tDraw4 = { } -- rect under UTF_8 code
local tDraw5 = { } -- cursor
-- start from first visible row
--
local iOffset = iNumCols * m_Frame.iByteFirstRow
local iFileIndex = m_App.FindLine(iOffset)
local tCurrLine = tFile[iFileIndex]
local iCurColumn = 1
local iBufIndex = iOffset - tCurrLine[1] + 1
local bHideSpace = m_App.tConfig.HideSpaces
local ch
while tCurrLine do
while iNumCols >= iCurColumn do
if #tCurrLine[2] < iBufIndex then
-- query next row in table
--
iFileIndex = iFileIndex + 1
tCurrLine = tFile[iFileIndex]
iBufIndex = 1
-- safety check
--
if not tCurrLine then break end
end
ch = tCurrLine[2]:byte(iBufIndex)
-- add string to collection
-- (check for replacing the space byte)
--
if 0x20 == ch and bHideSpace then
tChars[#tChars + 1] = m_Frame.sSpaceSub
else
tChars[#tChars + 1] = _format(tFmtBytes[1], ch)
end
-- highlight chars
--
if (0x20 > ch) or (0x7f < ch) or (tCurrLine[1] + iBufIndex) == iCursor then
local xPos = iCurX + ((iCurColumn - 1) * iSpacerX * tFmtBytes[2])
local yPos = iCurY + iSpacerY - 3
local xLen = iSpacerX * (tFmtBytes[2] - 1)
if bUnderline then
if 0x0a == ch then tDraw1[#tDraw1 + 1] = {xPos, yPos, xPos + xLen, yPos}
elseif 0x20 > ch then tDraw2[#tDraw2 + 1] = {xPos, yPos, xPos + xLen, yPos} end
end
if bUnicode then
if 0xbf < ch then tDraw3[#tDraw3 + 1] = {xPos, iCurY, xLen, iSpacerY}
elseif 0x7f < ch then tDraw4[#tDraw4 + 1] = {xPos, iCurY, xLen, iSpacerY} end
end
-- draw the cursor
-- (make it slightly bigger)
--
if (tCurrLine[1] + iBufIndex) == iCursor then
tDraw5 = {xPos - 2, iCurY - 2, xLen + 4, iSpacerY + 4}
end
end
-- get next
--
iBufIndex = iBufIndex + 1
iCurColumn = iCurColumn + 1
end
-- draw all the cosmetics
--
if 0 < #tDraw1 then
inDrawDC:SetPen(m_penLF)
for _, coords in ipairs(tDraw1) do inDrawDC:DrawLine(coords[1], coords[2], coords[3], coords[4]) end
tDraw1 = { }
end
if 0 < #tDraw2 then
inDrawDC:SetPen(m_penXX)
for _, coords in ipairs(tDraw2) do inDrawDC:DrawLine(coords[1], coords[2], coords[3], coords[4]) end
tDraw2 = { }
end
inDrawDC:SetPen(m_PenNull) -- remove rectangle's bounding box
if 0 < #tDraw3 then
inDrawDC:SetBrush(m_brMrk)
for _, coords in ipairs(tDraw3) do inDrawDC:DrawRectangle(coords[1], coords[2], coords[3], coords[4]) end
tDraw3 = { }
end
if 0 < #tDraw4 then
inDrawDC:SetBrush(m_brHBt)
for _, coords in ipairs(tDraw4) do inDrawDC:DrawRectangle(coords[1], coords[2], coords[3], coords[4]) end
tDraw4 = { }
end
if 0 < #tDraw5 then
inDrawDC:SetBrush(m_brCur)
inDrawDC:DrawRectangle(tDraw5[1], tDraw5[2], tDraw5[3], tDraw5[4])
tDraw5 = { }
end
-- draw all the text line
--
inDrawDC:DrawText(_concat(tChars), iCurX, iCurY)
tChars = { }
-- check if we are writing off the client area
--
iCurY = iCurY + iSpacerY
if iRectH < iCurY then break end
-- restart from first column
--
iCurColumn = 1
-- update the number of visible rows
--
m_Frame.iByteRowCount = m_Frame.iByteRowCount + 1
end
end
-- ----------------------------------------------------------------------------
-- drawing the right pane
--
local function DrawText(inDrawDC)
-- trace.line("DrawText")
if 0 >= m_App.iFileBytes then return end
if 0 >= m_Cursor.iAbsOffset then return end
-- local iCursor = m_Cursor.iAbsOffset
local tScheme = m_Frame.tColourScheme -- colour scheme in use
local sTabRep = m_Frame.sTabReplace -- replace tab with chars
local iOffX = m_Frame.iOffsetX
local iOffY = m_Frame.iOffsetY
local iSpacerY = m_Frame.iRightSpacerY -- height of each line
local iCurX = iOffX + m_Frame.iRightOffsetX -- left offset
local iCurY = iOffY
-- check which line will be the first
--
local tFile = m_App.tFileLines
local iStopLine = m_Cursor.iStopLine
if -1 == iStopLine then return end -- failed to get text
-- get the number of rows * font height and
-- correct the y offset
--
local iLowLimit = m_Frame.rcClientH - (iOffY * 2) - iSpacerY -- drawing height bound
local iNumRows = _floor(iLowLimit / iSpacerY) -- allowed rows
-- update
--
iOffY = math.ceil(((iLowLimit - (iSpacerY * iNumRows)) / 2)) -- vertical offset updated
iLowLimit = m_Frame.rcClientH - iOffY - iSpacerY -- vertical limit
-- align indexes for the line currently highlighted
-- (basically a list view)
--
local iFirstRow = m_Frame.iTextFirstRow
if iFirstRow > iStopLine then iFirstRow = iStopLine end -- passing the top
if iStopLine > (iFirstRow + iNumRows) then iFirstRow = (iStopLine - iNumRows) end -- passing the bottom
if 0 >= iFirstRow then iFirstRow = 1 end
-- store for later use outside the drawing function
--
m_Frame.iRightOffsetY = iOffY
m_Frame.iTextFirstRow = iFirstRow
m_Frame.iTextRowCount = iNumRows
-- set the clipping region
--
inDrawDC:SetClippingRegion(iCurX, iOffY, m_Frame.rcClientW - iCurX - 20, m_Frame.rcClientH - 10)
------------------
-- print all chars
--
iCurX = iCurX + iOffX
iCurY = iOffY
-- highlight the background of the stopline
--
if 0 < iStopLine then
local pStartX = iCurX
local pStartY = iOffY + (iStopLine - iFirstRow) * iSpacerY
local iWidth = m_Frame.rcClientW - pStartX
local iHeight = iSpacerY
inDrawDC:SetPen(m_PenNull)
inDrawDC:SetBrush(m_brStp)
inDrawDC:DrawRectangle(pStartX, pStartY, iWidth, iHeight)
end
-- assign font for drawing text
--
inDrawDC:SetFont(m_Frame.hFontText)
inDrawDC:SetTextForeground(tScheme.RightText)
-- draw the list
--
local sToDraw
local iCurLine = iFirstRow
local iLastLine= iFirstRow + iNumRows + 1
while tFile[iCurLine] and (iLastLine > iCurLine) do
sToDraw = tFile[iCurLine][2]
sToDraw = sToDraw:gsub("\t", sTabRep)
inDrawDC:DrawText(sToDraw, iCurX, iCurY)
iCurY = iCurY + iSpacerY
iCurLine = iCurLine + 1
end
-- underline the current character
--
if 0 < iStopLine then
local iStop = m_Cursor.iRelOffset - 1
-- extract the string before the current char
--
sToDraw = ""
if 0 < iStop then
sToDraw = m_Cursor.tCurrLine[2]:sub(1, iStop)
sToDraw = sToDraw:gsub("\t", sTabRep)
end
-- get both the extents
--
local iStopOffset = inDrawDC:GetTextExtent(sToDraw)
local iStopLength = inDrawDC:GetTextExtent(m_Cursor.sCurrChar)
local pTopY = iOffY + (iStopLine - iFirstRow + 1) * iSpacerY
local pStartX = iCurX + iStopOffset
local pEndX = pStartX + iStopLength
inDrawDC:SetPen(m_penUnd)
inDrawDC:DrawLine(pStartX, pTopY, pEndX, pTopY)
end
-- remove clipping
--
inDrawDC:DestroyClippingRegion()
end
-- ----------------------------------------------------------------------------
--
local function DrawSelectionText(inDrawDC)
-- trace.line("DrawSelectionText")
if 0 >= m_App.iFileBytes then return end
if m_Marker.iPosStart == m_Marker.iPosStop then return end
-- realign the marker's positions
-- (must work on a copy)
--
local iPosStart = m_Marker.iPosStart
local iPosStop = m_Marker.iPosStop
if iPosStart > iPosStop then
iPosStart, iPosStop = iPosStop, iPosStart
end
local iFirstRow = m_Frame.iTextFirstRow
local iLastRow = m_Frame.iTextRowCount + iFirstRow
-- positions
--
local tFile = m_App.tFileLines
local iStartLn = m_App.FindLine(iPosStart - 1)
local iStopLn = m_App.FindLine(iPosStop - 1)
if -1 == iStartLn or -1 == iStopLn then return end
if iStartLn > iLastRow then return end -- selection is not visible
if iStopLn < iFirstRow then return end -- idem
local iOffX = m_Frame.iOffsetX
local iOffY = m_Frame.iRightOffsetY
local iLeftW = m_Frame.iRightOffsetX + iOffX * 2 -- left offset
local iHeight = m_Frame.rcClientH
local sTabRep = m_Frame.sTabReplace -- replace tab with chars
local iMarkStart= iPosStart - tFile[iStartLn][1]
local sPrevText = tFile[iStartLn][2]:sub(1, iMarkStart - 1)
local iMarkStop
local sEndText
-- assume the correct font is already selectec
--
if iStartLn == iStopLn then
iMarkStop = iPosStop - tFile[iStartLn][1]
sEndText = tFile[iStartLn][2]:sub(iMarkStart, iMarkStop)
else
sEndText = tFile[iStartLn][2]:sub(iMarkStart)
end
sPrevText = sPrevText:gsub("\t", sTabRep)
sEndText = sEndText:gsub("\t", sTabRep)
local iExtX = inDrawDC:GetTextExtent(sPrevText)
local iExtY = inDrawDC:GetCharHeight()
local iWidth = inDrawDC:GetTextExtent(sEndText)
local iLeft = iLeftW + iExtX
local iTopY = (iStartLn - iFirstRow) * iExtY + iOffY