-
Notifications
You must be signed in to change notification settings - Fork 78
/
TDataEditor.cpp
1707 lines (1430 loc) · 60.4 KB
/
TDataEditor.cpp
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
/*****************************************************************************/
/* TDataEditor.h Copyright (c) Ladislav Zezula 2014 */
/*---------------------------------------------------------------------------*/
/* Description : */
/*---------------------------------------------------------------------------*/
/* Date Ver Who Comment */
/* -------- ---- --- ------- */
/* 18.03.14 1.00 Lad The first version of TDataEditor.cpp */
/*****************************************************************************/
#include "FileTest.h"
//-----------------------------------------------------------------------------
// Local defines
#define DATAEDIT_SPACES_BEFORE_HEXA 2
#define DATAEDIT_SPACES_BEFORE_TEXT 2
#define DATAEDIT_END_OF_LINE 0x7FFFFFFF
//-----------------------------------------------------------------------------
// Data for DataEditor
// Selection mode for MoveCaretTo
enum TSelectMode
{
SmUnchanged = 0,
SmStartSelection,
SmContinueSelection
};
// Internal data structure
struct TEditorData
{
ULONGLONG BaseAddress; // Base address of the data
LPBYTE pbEditorDataBegin; // Pointer to the editor data
LPBYTE pbEditorDataEnd; // Pointer to the editor data
LPTSTR szLineBuffer; // Buffer for formatting single line
HFONT hFont; // Currently used font
HWND hWndParent; // Parent window
HWND hWnd; // Window handle
DWORD dwStyles; // Window styles
DWORD dwId; // Control ID
ULONG ExceptionCode; // Last exception code
SIZE_T cbBytesPerLine; // Number of bytes per line
size_t nTopIndex; // Index of the top line
size_t nLines; // Number of lines
TPointerFormat PointerFormat; // Format of the pointer
int nPointerSize; // Width of the address, in chars
int nAddressWidth; // Width of the address, in chars
int nBeginHexaValues; // Start of the hexa values in the line
int nEndHexaValues; // End of the hexa values in the line
int nBeginTextValues; // Start of the text values in the line
int nEndTextValues; // End of the hexa values in the line
int nHeight; // Height of the client area
int nWidth; // Width of the client area
int nLeftOrg; // Origin of the left border (0 = default) in pixels
int nCharHeight; // Height of one character, in screen pixels
int nAveCharWidth; // Average width of one character, in screen pixels
int nTabSize; // Size of one tab character, in logical units
int nFullVisibleLines; // Number of fully visible lines
int nVisibleLines; // Number of visible lines (including the last one, partially visible)
size_t nStartSelLine; // Start selection
int nStartSelCol; // Start selection
size_t nEndSelLine; // End selection
int nEndSelCol; // End selection
size_t nCaretLine; // Line of the caret
int nCaretCol; // Column of the caret
int nCaretX; // X-Position of the caret
int nCaretY; // Y-Position of the caret
bool bWriteException;
bool bReadException;
bool bHasCapture; // If true if selecting text with the mouse
bool bFixedFont; // If true, it means that we use fixed font size
bool bHasCaret;
};
#define GetEditorData(hWnd) ((TEditorData *)(LONG_PTR)GetWindowLongPtr(hWnd, GWLP_USERDATA))
//-----------------------------------------------------------------------------
// Local variables
// ASCII to printable characters
BYTE AsciiToPrintableTable[256] =
{
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E,
0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E,
0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E,
0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E,
0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E,
0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E,
0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E,
0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E
};
const LPTSTR szDataEditClassName = _T("DataEditor");
//-----------------------------------------------------------------------------
// Data access functions
static void SendExceptionNotification(
TEditorData * pData,
PVOID ExceptionAddress,
ULONG ExceptionCode,
BOOL WriteOperation)
{
DTE_EXCEPTION_DATA Data;
// Notify the parent about the exception
Data.hdr.hwndFrom = pData->hWnd;
Data.hdr.idFrom = pData->dwId;
Data.hdr.code = DEN_EXCEPTION;
Data.ExceptionAddress = ExceptionAddress;
Data.ExceptionCode = ExceptionCode;
Data.WriteOperation = WriteOperation;
SendMessage(pData->hWndParent, WM_NOTIFY, pData->dwId, (LPARAM)&Data);
}
static BYTE GuardedReadByte(TEditorData * pData, size_t cbByteOffset)
{
BYTE OneByte = 0;
__try
{
// Only attempt to read the exception when read exception was not there yet
if(pData->bReadException == false)
{
assert((pData->pbEditorDataBegin + cbByteOffset) < pData->pbEditorDataEnd);
OneByte = pData->pbEditorDataBegin[cbByteOffset];
}
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
// Notify parent about the exception
SendExceptionNotification(pData, pData->pbEditorDataBegin + cbByteOffset, GetExceptionCode(), FALSE);
// Remember that we has a read exception
pData->ExceptionCode = GetExceptionCode();
pData->bReadException = true;
}
return OneByte;
}
static void GuardedWriteBuffer(TEditorData * pData, size_t cbByteOffset, LPCVOID pvBuffer, size_t cbLength)
{
__try
{
// Only attempt to read the exception when read exception was not there yet
if(pData->bWriteException == false)
{
assert((pData->pbEditorDataBegin + cbByteOffset) < pData->pbEditorDataEnd);
memcpy(pData->pbEditorDataBegin + cbByteOffset, pvBuffer, cbLength);
}
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
// Notify parent about the exception
SendExceptionNotification(pData, pData->pbEditorDataBegin + cbByteOffset, GetExceptionCode(), TRUE);
// Remember that we has a write exception
pData->ExceptionCode = GetExceptionCode();
pData->bWriteException = true;
}
}
static void PasteTextInternal(TEditorData * pData, LPCSTR szPasteText, size_t PasteOffset, size_t PasteLength)
{
LPBYTE pbPastePosition = pData->pbEditorDataBegin + PasteOffset;
// If the paste position is before the end of data, do it
if(pbPastePosition < pData->pbEditorDataEnd)
{
// Check the length of the data
if((pbPastePosition + PasteLength) > pData->pbEditorDataEnd)
PasteLength = (size_t)(pData->pbEditorDataEnd - pbPastePosition);
// Perform the guarded memcopy
GuardedWriteBuffer(pData, PasteOffset, szPasteText, PasteLength);
}
}
//-----------------------------------------------------------------------------
// Local functions
// The caller needs to free the buffer using HeapFree(GetProcessHeap(), 0, szText)
static LPSTR QueryClipboardText(HWND hWnd, size_t * pcchTextLength)
{
HANDLE hGlobal;
LPCSTR szClipboardText;
LPSTR szText = NULL;
size_t cchText = 0;
// Retrieve data from the clipboard
if(IsClipboardFormatAvailable(CF_TEXT))
{
if(OpenClipboard(hWnd))
{
hGlobal = GetClipboardData(CF_TEXT);
if(hGlobal != NULL)
{
szClipboardText = (LPCSTR)GlobalLock(hGlobal);
if(szClipboardText != NULL)
{
cchText = strlen(szClipboardText);
szText = (LPSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cchText + 1);
if(szText != NULL)
memcpy(szText, szClipboardText, cchText);
GlobalUnlock(hGlobal);
}
}
CloseClipboard();
}
}
// Give the result to the caller
if(pcchTextLength != NULL)
*pcchTextLength = cchText;
return szText;
}
static LPTSTR FormatPointer(TEditorData * pData, LPTSTR Buffer, ULONGLONG LineAddress)
{
ULONG PointerHigh;
ULONG PointerLow = (ULONG)(LineAddress);
// Put the high DWORD on 64-bit pointer
if(pData->nPointerSize == 8)
{
// Get the high DWORD pointer
PointerHigh = (ULONG)(LineAddress >> 32);
// Format the upper DWORD
*Buffer++ = HexaAlphabetUpper[(PointerHigh >> 0x1C) & 0x0F];
*Buffer++ = HexaAlphabetUpper[(PointerHigh >> 0x18) & 0x0F];
*Buffer++ = HexaAlphabetUpper[(PointerHigh >> 0x14) & 0x0F];
*Buffer++ = HexaAlphabetUpper[(PointerHigh >> 0x10) & 0x0F];
*Buffer++ = HexaAlphabetUpper[(PointerHigh >> 0x0C) & 0x0F];
*Buffer++ = HexaAlphabetUpper[(PointerHigh >> 0x08) & 0x0F];
*Buffer++ = HexaAlphabetUpper[(PointerHigh >> 0x04) & 0x0F];
*Buffer++ = HexaAlphabetUpper[(PointerHigh >> 0x00) & 0x0F];
// Put the separator
*Buffer++ = _T('\'');
}
// Format the upper DWORD
*Buffer++ = HexaAlphabetUpper[(PointerLow >> 0x1C) & 0x0F];
*Buffer++ = HexaAlphabetUpper[(PointerLow >> 0x18) & 0x0F];
*Buffer++ = HexaAlphabetUpper[(PointerLow >> 0x14) & 0x0F];
*Buffer++ = HexaAlphabetUpper[(PointerLow >> 0x10) & 0x0F];
*Buffer++ = HexaAlphabetUpper[(PointerLow >> 0x0C) & 0x0F];
*Buffer++ = HexaAlphabetUpper[(PointerLow >> 0x08) & 0x0F];
*Buffer++ = HexaAlphabetUpper[(PointerLow >> 0x04) & 0x0F];
*Buffer++ = HexaAlphabetUpper[(PointerLow >> 0x00) & 0x0F];
// Put the end of string
*Buffer = 0;
return Buffer;
}
static int FormatOneLine(TEditorData * pData, size_t nLineIndex)
{
ULONGLONG LineAddress;
LPBYTE pbDataBegin;
LPBYTE pbDataPtr;
LPBYTE pbDataEnd;
LPTSTR szLineBuffer = pData->szLineBuffer;
LPTSTR szLineEnd = szLineBuffer + pData->nEndTextValues;
size_t DataOffset = (nLineIndex * pData->cbBytesPerLine);
// Get the single line
if(nLineIndex <= pData->nLines)
{
// Copy the address
LineAddress = pData->BaseAddress + DataOffset;
// Format the pointer
szLineBuffer = FormatPointer(pData, szLineBuffer, LineAddress);
// Put the spaces
*szLineBuffer++ = _T(' ');
*szLineBuffer++ = _T(' ');
// Put the space between address and hexa values
pbDataBegin = pData->pbEditorDataBegin + DataOffset;
pbDataEnd = pbDataBegin + pData->cbBytesPerLine;
// If there was an exception before, display the exception text
pData->bReadException = false;
GuardedReadByte(pData, DataOffset);
if(pData->bReadException)
{
StringCchPrintf(szLineBuffer, (szLineEnd - szLineBuffer), _T("Exception %08X when reading address %p"), pData->ExceptionCode, pbDataBegin);
szLineBuffer += _tcslen(szLineBuffer);
return (int)(szLineBuffer - pData->szLineBuffer);
}
// Format all data as hexa values
for(pbDataPtr = pbDataBegin; pbDataPtr < pbDataEnd; pbDataPtr++)
{
if(pbDataPtr < pData->pbEditorDataEnd)
{
*szLineBuffer++ = HexaAlphabetUpper[*pbDataPtr >> 0x04];
*szLineBuffer++ = HexaAlphabetUpper[*pbDataPtr & 0x0F];
*szLineBuffer++ = _T(' ');
}
else
{
*szLineBuffer++ = _T(' ');
*szLineBuffer++ = _T(' ');
*szLineBuffer++ = _T(' ');
}
}
// Put the space between hexa values and text values
*szLineBuffer++ = _T(' ');
// Put the values as they are
for(pbDataPtr = pbDataBegin; pbDataPtr < pbDataEnd; pbDataPtr++)
{
if(pbDataPtr < pData->pbEditorDataEnd)
{
*szLineBuffer++ = AsciiToPrintableTable[pbDataPtr[0]];
}
else
{
*szLineBuffer++ = _T(' ');
}
}
}
// Finish the line with zero
*szLineBuffer = 0;
return (int)(szLineBuffer - pData->szLineBuffer);
}
static int FindPreviousWord(TEditorData * pData, size_t nLine, int nCol)
{
int nBeginHexaByte;
int nCurrentByte;
UNREFERENCED_PARAMETER(nLine);
// If the caret position is greater than text view, move it to the begin of the text view
if(nCol > pData->nBeginTextValues)
return pData->nBeginTextValues;
// If the caret position is within the hexa view, move it to the hexa byte
if(nCol > pData->nBeginHexaValues)
{
// Get the current byte we are pointing at
nCurrentByte = (nCol - pData->nBeginHexaValues) / 3;
nBeginHexaByte = pData->nBeginHexaValues + (nCurrentByte * 3);
if(nCol > nBeginHexaByte)
return nBeginHexaByte;
// Move to the previous byte
return pData->nBeginHexaValues + (nCurrentByte - 1) * 3;
}
// We assume that we are before the hexa bytes --> go to position 0
return 0;
}
static int FindNextWord(TEditorData * pData, size_t nLine, int nCol)
{
int nCurrentByte;
UNREFERENCED_PARAMETER(nLine);
// If the caret position is greater than text view, move it to the end
if(nCol >= pData->nBeginTextValues)
return pData->nEndTextValues;
// If the caret position is at the last byte, move it to the text area
if(nCol >= pData->nEndHexaValues - 2)
return pData->nBeginTextValues;
// If the current position is at the hexa values, move to the previous hexa string
if(nCol >= pData->nBeginHexaValues)
{
// Get the current byte we are pointing at
nCurrentByte = (nCol - pData->nBeginHexaValues) / 3;
return pData->nBeginHexaValues + (nCurrentByte + 1) * 3;
}
// We assume we are past the begin of the text values
return pData->nBeginHexaValues;
}
static void CreateTextFont(TEditorData * pData)
{
LOGFONT LogFont;
// Create font for all windows
ZeroMemory(&LogFont, sizeof(LOGFONT));
LogFont.lfWeight = FW_NORMAL;
LogFont.lfCharSet = DEFAULT_CHARSET;
LogFont.lfQuality = DEFAULT_QUALITY;
LogFont.lfPitchAndFamily = FIXED_PITCH;
LogFont.lfHeight = -12;
StringCchCopy(LogFont.lfFaceName, _countof(LogFont.lfFaceName), _T("Courier New"));
pData->hFont = CreateFontIndirect(&LogFont);
// If the font couldn't be created, create default one
if(pData->hFont == NULL)
{
LogFont.lfHeight = -15;
StringCchCopy(LogFont.lfFaceName, _countof(LogFont.lfFaceName), _T("Courier"));
pData->hFont = CreateFontIndirect(&LogFont);
// If even that failed, get the default fixed width font
if(pData->hFont == NULL)
{
pData->hFont = (HFONT)GetStockObject(ANSI_FIXED_FONT);
}
}
// Apply the font to the window
SendMessage(pData->hWnd, WM_SETFONT, (WPARAM)pData->hFont, 0);
}
// Recalculate the view based on new font
static void QueryFontDimensions(TEditorData * pData)
{
TEXTMETRIC TextMetrics;
HFONT hOldFont;
HDC hdc;
// Get the height of one line
hdc = GetDC(pData->hWnd);
if(hdc != NULL)
{
hOldFont = (HFONT)SelectObject(hdc, pData->hFont);
GetTextMetrics(hdc, &TextMetrics);
// Rememember the height of one line
pData->nAveCharWidth = TextMetrics.tmAveCharWidth;
pData->nCharHeight = TextMetrics.tmHeight;
pData->nTabSize = pData->nAveCharWidth * 4;
assert(pData->nCharHeight != 0);
// Remember if we are using variable pich font.
// Note that the TMPF_FIXED_PITCH bit is actually the opposite of what its meaning says
pData->bFixedFont = (TextMetrics.tmPitchAndFamily & TMPF_FIXED_PITCH) ? false : true;
// Select the old font and
SelectObject(hdc, hOldFont);
ReleaseDC(pData->hWnd, hdc);
}
}
static void RecalculateView(TEditorData * pData)
{
SCROLLINFO si;
DWORD dwOldStyles;
DWORD dwNewStyles;
DWORD dwAddStyles;
RECT rect;
size_t cbEditorData;
// The data must already have been created
assert(pData != NULL);
// Update the client size
GetClientRect(pData->hWnd, &rect);
pData->nHeight = rect.bottom;
pData->nWidth = rect.right;
pData->nLines = 0;
// Update the total number of lines
cbEditorData = (size_t)(pData->pbEditorDataEnd - pData->pbEditorDataBegin);
if(cbEditorData > 0)
pData->nLines = ((cbEditorData - 1) / pData->cbBytesPerLine) + 1;
// Update the number of lines, based on new size
pData->nFullVisibleLines = (pData->nHeight / pData->nCharHeight);
pData->nVisibleLines = ((pData->nHeight - 1) / pData->nCharHeight) + 1;
// Now check if we need to show or remove vertical scroll bar
dwAddStyles = ((pData->dwStyles & DES_VSCROLL) && (pData->nLines > (size_t)pData->nFullVisibleLines)) ? WS_VSCROLL : 0;
dwOldStyles = (DWORD)GetWindowLongPtr(pData->hWnd, GWL_STYLE);
dwNewStyles = (dwOldStyles & ~WS_VSCROLL) | dwAddStyles;
// If the styles changed, apply them
if(dwNewStyles != dwOldStyles)
{
SetWindowLongPtr(pData->hWnd, GWL_STYLE, dwNewStyles);
SetWindowPos(pData->hWnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
}
// Do we have vertical scrollbar ?
if(dwNewStyles & WS_VSCROLL)
{
// Update scroll bar. Note that this also calls WM_SIZE,
// which in turn calls this function.
ZeroMemory(&si, sizeof(SCROLLINFO));
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
si.nPos = (int)pData->nTopIndex;
si.nPage = (pData->nFullVisibleLines > 1) ? (pData->nFullVisibleLines - 1) : 1;
si.nMax = (int)((pData->nLines > 1) ? (pData->nLines - 1) : 1);
SetScrollInfo(pData->hWnd, SB_VERT, &si, FALSE);
}
}
static void SetBytesPerLine(TEditorData * pData, SIZE_T cbBytesPerLine)
{
// Don't accept zero number of bytes per line
if(cbBytesPerLine <= 0)
cbBytesPerLine = 0x10;
// Set the width of the pointer
switch (pData->PointerFormat)
{
case PtrPlatformSpecific:
pData->nAddressWidth = (pData->nPointerSize == 8) ? 17 : 8;
pData->nPointerSize = sizeof(void *);
break;
case PtrPointer64Bit:
pData->nAddressWidth = 17;
pData->nPointerSize = 8;
break;
case PtrPointer32Bit:
pData->nAddressWidth = 8;
pData->nPointerSize = 4;
break;
}
// Calculate the sizes
pData->nBeginHexaValues = (int)(pData->nAddressWidth + DATAEDIT_SPACES_BEFORE_HEXA);
pData->nEndHexaValues = (int)(pData->nBeginHexaValues + (cbBytesPerLine * 3) - 1);
pData->nBeginTextValues = (int)(pData->nEndHexaValues + DATAEDIT_SPACES_BEFORE_TEXT);
pData->nEndTextValues = (INT)(pData->nBeginTextValues + cbBytesPerLine);
// Remember the number of bytes per line
pData->cbBytesPerLine = cbBytesPerLine;
// Reallocate the line buffer
if(pData->szLineBuffer != NULL)
HeapFree(GetProcessHeap(), 0, pData->szLineBuffer);
pData->szLineBuffer = (LPTSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (pData->nEndTextValues + 1) * sizeof(TCHAR));
// Force the view to be recalculated
RecalculateView(pData);
}
// Calculates the X and Y coordinates of the caret
// - Does NOT set the caret position
// - Does NOT redraw the window
// - Returns TRUE if the caret's position actually changed
static bool RecalcCaretXY(TEditorData * pData, bool * bNeedRedraw)
{
HFONT hOldFont;
DWORD TextSize = 0;
HDC hdc;
int nSaveCaretX = pData->nCaretX;
int nSaveCaretY = pData->nCaretY;
// Calculate the Y-position of the caret
pData->nCaretY = 0;
if(pData->nLines > 0 && pData->nCaretLine > pData->nTopIndex)
pData->nCaretY = ((int)(pData->nCaretLine - pData->nTopIndex) * pData->nCharHeight);
// Calculate the X-position of the caret
pData->nCaretX = 0;
if(pData->nLines > 0 && pData->nCaretCol > 0)
{
hdc = GetDC(pData->hWnd);
if(hdc != NULL)
{
// Retrieve the n-th line
FormatOneLine(pData, pData->nCaretLine);
// Calculate the width of the text from the beginning up to the char at the caret pos
hOldFont = (HFONT)SelectObject(hdc, pData->hFont);
TextSize = GetTabbedTextExtent(hdc, pData->szLineBuffer, pData->nCaretCol, 1, &pData->nTabSize);
SelectObject(hdc, hOldFont);
ReleaseDC(pData->hWnd, hdc);
}
// Increment the cursor x-pos by the char position
pData->nCaretX += LOWORD(TextSize);
}
// If the X position od the caret got out of the screen, we have to move the org
if(pData->nCaretX > (pData->nLeftOrg + pData->nWidth))
{
pData->nLeftOrg = pData->nCaretX - (pData->nWidth - 10);
*bNeedRedraw = true;
}
// If the caret went out of the view
if(pData->nCaretX < pData->nLeftOrg)
{
pData->nLeftOrg = pData->nCaretX;
*bNeedRedraw = true;
}
// Add the indent constant
pData->nCaretX = (pData->nCaretX - pData->nLeftOrg) + DATAEDIT_TEXT_INDENT;
// Return whether the caret's position has been changed or not
return (bool)(pData->nCaretX != nSaveCaretX || pData->nCaretY != nSaveCaretY);
}
//
// Recalculates the mouse position to caret line and column
//
static void MousePosToCaretPos(TEditorData * pData,
int nMouseX,
int nMouseY,
size_t * piMouseLine,
int * piMouseCol)
{
LPTSTR szLineText;
HFONT hOldFont;
DWORD TextSize;
size_t nLineLength = 0;
size_t nCharIndex1;
size_t nCharIndex2;
size_t nCharHalfX;
HDC hdc;
size_t nCaretLine;
int nNextCharTreshold = (pData->nAveCharWidth / 2);
int nCaretCol;
int nRealWidth;
// Adjust the text indent value
if(nMouseX < DATAEDIT_TEXT_INDENT)
nMouseX = DATAEDIT_TEXT_INDENT;
nMouseX = nMouseX + pData->nLeftOrg - DATAEDIT_TEXT_INDENT;
// Calculate Y character position
if(nMouseY < 0)
nCaretLine = pData->nTopIndex - (-nMouseY / pData->nCharHeight) - 1;
else
nCaretLine = pData->nTopIndex + (nMouseY / pData->nCharHeight);
// By default, use average char width to calculate cursor X coordinate
nCaretCol = (nMouseX / pData->nAveCharWidth);
// Get line text where the caret is
if(nCaretLine < pData->nLines)
{
// Get the text of the line
nLineLength = FormatOneLine(pData, nCaretLine);
if(nLineLength != 0)
{
if(pData->bFixedFont == false)
{
// Set the font into the HDC of the window
hdc = GetDC(pData->hWnd);
hOldFont = (HFONT)SelectObject(hdc, pData->hFont);
// Initialize the search range
nCharIndex1 = 0;
nCharIndex2 = nLineLength + 1;
szLineText = pData->szLineBuffer;
// Perform binary search
while(nCharIndex1 < (nCharIndex2 - 1))
{
// Get character in half of the current string range
nCharHalfX = nCharIndex1 + max((nCharIndex2 - nCharIndex1) / 2, 1);
// Get size of it
TextSize = GetTabbedTextExtent(hdc, szLineText, (int)nCharHalfX, 1, &pData->nTabSize);
nRealWidth = LOWORD(TextSize) - nNextCharTreshold;
// Choose one of the halves
if(nRealWidth < nMouseX)
nCharIndex1 = nCharHalfX;
else
nCharIndex2 = nCharHalfX;
}
// Get the column
nCaretCol = (int)nCharIndex1;
// Release the HDC of the window
SelectObject(hdc, hOldFont);
ReleaseDC(pData->hWnd, hdc);
}
else
{
// On a fixed-pitch font, it's simple ...
nCaretCol = (nMouseX + nNextCharTreshold) / pData->nAveCharWidth;
if(nCaretCol > (int)(nLineLength + 1))
nCaretCol = (int)(nLineLength + 1);
}
}
}
*piMouseLine = nCaretLine;
*piMouseCol = nCaretCol;
}
//-----------------------------------------------------------------------------
// Caret movement functions
inline bool HasSelection(TEditorData * pData)
{
return (bool)(pData->nStartSelLine != pData->nEndSelLine || pData->nStartSelCol != pData->nEndSelCol);
}
inline bool IsReversedSelection(TEditorData * pData)
{
return ((pData->nEndSelLine < pData->nStartSelLine) || (pData->nStartSelLine == pData->nEndSelLine && pData->nEndSelCol < pData->nStartSelCol));
}
// This method sets the current selection.
// - Does NOT change position of the cursor
// - Does NOT redraw the view
static bool SetSelection(
TEditorData * pData,
size_t nStartSelLine,
int nStartSelCol,
size_t nEndSelLine,
int nEndSelCol)
{
bool bSelectionChanged = false;
// Only do selection if there are some lines
if(pData->nLines == 0)
{
nStartSelLine = 0;
nStartSelCol = 0;
nEndSelLine = 0;
nEndSelCol = 0;
}
// Determine if the selection has actually changed
if(nStartSelLine != pData->nStartSelLine || nStartSelCol != pData->nStartSelCol)
bSelectionChanged = true;
if(nEndSelLine != pData->nEndSelLine || nEndSelCol != pData->nEndSelCol)
bSelectionChanged = true;
// Set the selection to the lines
pData->nStartSelLine = nStartSelLine;
pData->nStartSelCol = nStartSelCol;
pData->nEndSelLine = nEndSelLine;
pData->nEndSelCol = nEndSelCol;
return bSelectionChanged;
}
// Moves the view and the caret
// Returns true if the entire view needs to be redrawn.
static void MoveTopIndexAndCaretTo(
TEditorData * pData,
size_t nNewTopIndex,
size_t nNewCaretLine,
int nNewCaretCol,
TSelectMode SelectionMode)
{
SCROLLINFO si;
size_t nNewStartSelLine;
size_t nMaxTopIndex;
bool bNeedRedraw = false;
int nNewStartSelCol;
int nPageSize;
// We can not do anything if there are no lines
if(pData->nLines != 0)
{
// The page size is the number of fully visible lines
// If less than two lines are fully visible, the page size is one line
nPageSize = (pData->nFullVisibleLines >= 2) ? pData->nFullVisibleLines : 1;
// Check for moving the top index out of bounds
nMaxTopIndex = pData->nLines - nPageSize;
nNewTopIndex = (nNewTopIndex > nMaxTopIndex) ? nMaxTopIndex : nNewTopIndex;
// Check if we are actually changing the top index
if(nNewTopIndex != pData->nTopIndex)
{
pData->nTopIndex = nNewTopIndex;
bNeedRedraw = true;
}
// Check for moving the caret line out of bounds
nNewCaretLine = (nNewCaretLine > (pData->nLines - 1)) ? (pData->nLines - 1) : nNewCaretLine;
// Check for moving the caret column out of bounds
nNewCaretCol = (nNewCaretCol < 0) ? 0 : nNewCaretCol;
nNewCaretCol = (nNewCaretCol > (int)pData->nEndTextValues) ? (int)pData->nEndTextValues : nNewCaretCol;
// Set the new caret position
pData->nCaretLine = nNewCaretLine;
pData->nCaretCol = nNewCaretCol;
// Update the selection
nNewStartSelLine = (SelectionMode == SmStartSelection) ? nNewCaretLine : pData->nStartSelLine;
nNewStartSelCol = (SelectionMode == SmStartSelection) ? nNewCaretCol : pData->nStartSelCol;
if(SetSelection(pData, nNewStartSelLine, nNewStartSelCol, nNewCaretLine, nNewCaretCol))
bNeedRedraw = true;
// If we need to redraw the view, do it
if(bNeedRedraw)
{
// Redraw the view
InvalidateRect(pData->hWnd, NULL, FALSE);
// Do we have vertical scrollbar ?
if(pData->dwStyles & DES_VSCROLL)
{
ZeroMemory(&si, sizeof(SCROLLINFO));
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_PAGE | SIF_POS;
si.nPos = (int)pData->nTopIndex;
si.nPage = nPageSize;
SetScrollInfo(pData->hWnd, SB_VERT, &si, TRUE);
}
}
// Recalculate the caret's X and Y position and move the caret.
// Move the caret position, if it changed
if(pData->bHasCaret && RecalcCaretXY(pData, &bNeedRedraw))
SetCaretPos(pData->nCaretX, pData->nCaretY);
}
}
// Changes the top index, keeps the caret position
static void MoveTopIndexTo(TEditorData * pData, size_t nNewTopIndex)
{
MoveTopIndexAndCaretTo(pData, nNewTopIndex, pData->nCaretLine, pData->nCaretCol, SmContinueSelection);
}
// Moves a caret to new position. If the caret moves out of the current view,
// the view is scrolled to ensure that the caret is visible
static void MoveCaret_ScrollView(
TEditorData * pData,
size_t nNewCaretLine,
int nNewCaretCol,
TSelectMode SelectionMode)
{
size_t nNewTopIndex = pData->nTopIndex;
int nPageSize = (pData->nFullVisibleLines >= 2) ? pData->nFullVisibleLines : 1;
// If the caret goes before the top index, move the top index as well
if(nNewCaretLine < nNewTopIndex)
nNewTopIndex = nNewCaretLine;
// If the new caret line is below the last visible line, move top index down
if((nNewTopIndex + nPageSize - 1) < nNewCaretLine && nNewCaretLine <= pData->nLines)
nNewTopIndex = (nNewCaretLine - nPageSize + 1);
MoveTopIndexAndCaretTo(pData, nNewTopIndex, nNewCaretLine, nNewCaretCol, SelectionMode);
}
//-----------------------------------------------------------------------------
// WM_ message handlers
static void OnCreate(HWND hWnd, LPCREATESTRUCT pCreateParams)
{
TEditorData * pData = new TEditorData;
// Pre-init the editor data
ZeroMemory(pData, sizeof(TEditorData));
pData->hWndParent = pCreateParams->hwndParent;
pData->dwStyles = pCreateParams->style;
pData->hWnd = hWnd;
pData->dwId = (DWORD)(DWORD_PTR)pCreateParams->hMenu;
// Remove the VS_VISIBLE style, as we want it to show the real state
pData->dwStyles &= ~WS_VISIBLE;
// Remember the editor data
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)pData);
// We need to recalculate view for new font
CreateTextFont(pData);
QueryFontDimensions(pData);
// Set the default number of bytes per line
SetBytesPerLine(pData, 0x10);
}
static void OnShowWindow(HWND hWnd, WPARAM wParam)
{
TEditorData * pData = GetEditorData(hWnd);
pData->dwStyles = (pData->dwStyles & ~WS_VISIBLE) | (wParam ? WS_VISIBLE : 0);
}
static void OnSize(HWND hWnd, LPARAM lParam)
{
TEditorData * pData = GetEditorData(hWnd);
pData->nHeight = GET_Y_LPARAM(lParam);
pData->nWidth = GET_X_LPARAM(lParam);
}
static void OnSetFocus(HWND hWnd)
{
TEditorData * pData = GetEditorData(hWnd);
// Create the text caret
if(CreateCaret(hWnd, NULL, 1, pData->nCharHeight))
{
// Force recalculating the caret position, otherwise
// the position remains unchanged from previously focused window
SetCaretPos(pData->nCaretX, pData->nCaretY);
ShowCaret(hWnd);
pData->bHasCaret = true;
}
// Notify the parent that we received focus
SendMessage(pData->hWndParent, WM_COMMAND, MAKEWPARAM(pData->dwId, EN_SETFOCUS), (LPARAM)pData->hWnd);
}
static void OnKillFocus(HWND hWnd)
{
TEditorData * pData = GetEditorData(hWnd);
// If we have capture active, we have to release it
if(pData->bHasCapture)
{
ReleaseCapture();
pData->bHasCapture = false;
}
// If we have caret, we have to get rid of it
if(pData->bHasCaret)
{
HideCaret(hWnd);
DestroyCaret();
pData->bHasCaret = false;
}
}
static LRESULT OnKeyDown(HWND hWnd, WPARAM wParam, LPARAM /* lParam */)
{
TEditorData * pData = GetEditorData(hWnd);
TSelectMode SelectionMode;
UINT uKeyCode = (UINT)wParam;
size_t nNewCaretLine;
size_t nNewTopIndex;
int nNewCaretCol = pData->nCaretCol;
bool bIsShiftPressed = false;
bool bIsCtrlPressed = false;
// Do nothing if there are no lines
if(pData->nLines == 0)
return TRUE;
// Get the state of the Shift key
if(GetAsyncKeyState(VK_SHIFT) & 0x8000)
bIsShiftPressed = true;
if(GetAsyncKeyState(VK_CONTROL) & 0x8000)
bIsCtrlPressed = true;
// Determine the selection mode
SelectionMode = bIsShiftPressed ? SmContinueSelection : SmStartSelection;
// Perform key-specific action
switch(uKeyCode)
{
case VK_SHIFT:
case VK_CONTROL:
case VK_MENU:
break;
case VK_HOME:
// Ctrl+Shift+Home: Moves the caret to the first char of the document with selection
// Ctrl+Home: Moves the caret to the first char of the document
// Shift+Home: Moves the caret to the first char with selection
// Home: Moves the caret to the first char
nNewTopIndex = bIsCtrlPressed ? 0 : pData->nTopIndex;
nNewCaretLine = bIsCtrlPressed ? 0 : pData->nCaretLine;
MoveTopIndexAndCaretTo(pData, nNewTopIndex, nNewCaretLine, 0, SelectionMode);
break;
case VK_PRIOR:
// Ctrl+Shift+PageUp: Moves the caret to the top line of the page with selection
// Ctrl+PageUp: Moves the caret to the top line of the page
// Shift+PageUp: Moves the caret one page up with selection
// PageUp: Moves the top index one page up, keeps relative caret position
nNewTopIndex = (pData->nTopIndex > (size_t)pData->nFullVisibleLines) ? (pData->nTopIndex - pData->nFullVisibleLines) : 0;
nNewTopIndex = (bIsCtrlPressed == false) ? nNewTopIndex : pData->nTopIndex;
nNewCaretLine = (pData->nCaretLine > (size_t)pData->nFullVisibleLines) ? (pData->nCaretLine - pData->nFullVisibleLines) : 0;