| 1 |
{ |
| 2 |
Component ViewerControl (Free Pascal) |
| 3 |
show file in text (wraped or not) or bin or hex mode |
| 4 |
|
| 5 |
This is part of Seksi Commander |
| 6 |
|
| 7 |
To searching use uFindMmap, |
| 8 |
to movement call Upxxxx, Downxxxx, or set Position |
| 9 |
|
| 10 |
Realised under GNU GPL 2 |
| 11 |
author Radek Cervinka ([email protected]) |
| 12 |
|
| 13 |
changes: |
| 14 |
5.8 |
| 15 |
- customizable view area of transformed text(transformed Values per Line) |
| 16 |
- customizable separators |
| 17 |
- added view as decimal |
| 18 |
- added class TCharToCustomValueTransformProc to handle parameters of customizable modes(at this time - Hex and Dec) |
| 19 |
- Hex and Dec mode handled by the same mechanism(potentially can be added any additionsl custom mode) |
| 20 |
so added properties FHex and FDec |
| 21 |
- added property FCustom which is nil if current mode - not custom, or is equal to current customizable mode |
| 22 |
- Added CopyToClipboardF which copy transformed text to clipboard |
| 23 |
|
| 24 |
5.7. (RC) |
| 25 |
- selecting text with mouse |
| 26 |
- CopyToclipBoard, SelectAll |
| 27 |
?.6. - LoadFromStdIn and loading first 64Kb of files with size=0 :) (/proc fs ..) |
| 28 |
17.6. (RC) |
| 29 |
- mapfile (in error set FMappedFile=nil) |
| 30 |
- writetext TABs fixed (tab is replaced by 9 spaces) |
| 31 |
- set correct position for modes hex, bin (SetPosition) |
| 32 |
21.7 |
| 33 |
- wrap text on 80 character lines works better now (by Radek Polak) |
| 34 |
- problems with function UpLine for specific lines: |
| 35 |
(lines of 80(=cTextWidth) character ended with ENTER (=#10) |
| 36 |
6.2. (RC) |
| 37 |
- ported to fpc for linux (CustomControl and gtk) |
| 38 |
7.2. (RC) |
| 39 |
- use temp to new implementation of LoadFromStdIn (and mmap temp file) |
| 40 |
- faster drawing of text (I hope) |
| 41 |
|
| 42 |
contributors: |
| 43 |
|
| 44 |
Copyright (C) 2006-2023 Alexander Koblov ([email protected]) |
| 45 |
|
| 46 |
|
| 47 |
TODO: |
| 48 |
a) File mapping blocks writing into file by other processes. |
| 49 |
Either: |
| 50 |
+ Open small text files by reading them all into memory (done). |
| 51 |
- Add optional custom loading/caching portions of file in memory |
| 52 |
and only reading from file when neccessary. |
| 53 |
|
| 54 |
b) Searching in Unicode encodings and case-insensitive searching. |
| 55 |
|
| 56 |
c) Selecting text does not work well with composed Unicode characters |
| 57 |
(characters that are composed of multiple Unicode characters). |
| 58 |
|
| 59 |
d) Drawing/selecting text does not work correctly with RTL (right to left) text. |
| 60 |
|
| 61 |
e) FTextHeight is unreliable with complex unicode characters. It should be |
| 62 |
calculated based on currently displayed text (get max from each line's height). |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
unit ViewerControl; |
| 67 |
|
| 68 |
{$mode objfpc}{$H+} |
| 69 |
|
| 70 |
interface |
| 71 |
|
| 72 |
uses |
| 73 |
SysUtils, Classes, Controls, StdCtrls, LCLVersion, LMessages, fgl; |
| 74 |
|
| 75 |
const |
| 76 |
MaxMemSize = $400000; // 4 Mb |
| 77 |
|
| 78 |
type |
| 79 |
TViewerControlMode = (vcmBin, vcmHex, vcmText, vcmWrap, vcmBook, vcmDec); |
| 80 |
TDataAccess = (dtMmap, dtNothing); |
| 81 |
TCharSide = (csBefore, csLeft, csRight, csAfter); |
| 82 |
|
| 83 |
TPtrIntList = specialize TFPGList<PtrInt>; |
| 84 |
|
| 85 |
TGuessEncodingEvent = function(const s: string): string; |
| 86 |
TFileOpenEvent = function(const FileName: String; Mode: LongWord): System.THandle; |
| 87 |
|
| 88 |
TCustomCharsPresentation = class; |
| 89 |
TCharToCustomValueTransformProc = function(AChar:AnsiChar;AMaxDigitsCount:integer):AnsiString of object; |
| 90 |
|
| 91 |
{ TCustomCharsPresentation } |
| 92 |
|
| 93 |
{ |
| 94 |
Presentation one char is called Value |
| 95 |
Function for convert char to Value is ChrToValueProc |
| 96 |
} |
| 97 |
|
| 98 |
TCustomCharsPresentation = class |
| 99 |
public |
| 100 |
ValuesPerLine :integer; // = 16 for Hex by default |
| 101 |
MaxValueDigits :integer; // the max width of present char (255) - 3 symbols |
| 102 |
|
| 103 |
MaxAddrDigits :integer; // = 8; |
| 104 |
StartOfs :integer; // = OffsetWidth + 2; // ': ' |
| 105 |
EndOfs :integer; // = StartOfs + (ValuesPerLine * (ValueMaxDigits+SpaceCount)); |
| 106 |
StartAscii :integer; // = StartOfs + (ValuesPerLine * (ValueMaxDigits+SpaceCount)) + 2; // ' ' |
| 107 |
|
| 108 |
SpaceCount :integer; // = 1 - one spacebar between Values |
| 109 |
SeparatorSpace :AnsiString; // spacebar * SpaceCount |
| 110 |
|
| 111 |
SeparatorChar :AnsiChar; // '|' |
| 112 |
CountSeperate :integer; // insert SeparatorChar after every CountSeperate values |
| 113 |
|
| 114 |
ChrToValueProc :TCharToCustomValueTransformProc; // procedure which return presentation of one char |
| 115 |
|
| 116 |
constructor Create(APresentValuesPerLine,ACharMaxPresentWidth,AOffsetWidth,ACountSeparate:integer;AChrToValueProc:TCharToCustomValueTransformProc); |
| 117 |
destructor Destroy();override; |
| 118 |
end; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
type |
| 124 |
// If additional encodings are added they should be also supported by: |
| 125 |
// - GetNextCharAsAscii |
| 126 |
// - GetPrevCharAsAscii |
| 127 |
// - GetNextCharAsUtf8 |
| 128 |
// - ConvertToUTF8 |
| 129 |
// - UpdateSelection |
| 130 |
TViewerEncoding = (veAutoDetect, |
| 131 |
veUtf8, |
| 132 |
veUtf8bom, |
| 133 |
veAnsi, |
| 134 |
veOem, |
| 135 |
veCp1250, |
| 136 |
veCp1251, |
| 137 |
veCp1252, |
| 138 |
veCp1253, |
| 139 |
veCp1254, |
| 140 |
veCp1255, |
| 141 |
veCp1256, |
| 142 |
veCp1257, |
| 143 |
veCp1258, |
| 144 |
veCp437, |
| 145 |
veCp850, |
| 146 |
veCp852, |
| 147 |
veCp866, |
| 148 |
veCp874, |
| 149 |
veCp932, |
| 150 |
veCp936, |
| 151 |
veCp949, |
| 152 |
veCp950, |
| 153 |
veIso88591, |
| 154 |
veIso88592, |
| 155 |
veKoi8r, |
| 156 |
veKoi8u, |
| 157 |
veKoi8ru, |
| 158 |
veUcs2le, |
| 159 |
veUcs2be, |
| 160 |
veUtf16le, |
| 161 |
veUtf16be, |
| 162 |
veUtf32le, // = ucs4le |
| 163 |
veUtf32be); // = ucs4be |
| 164 |
|
| 165 |
TViewerEncodings = set of TViewerEncoding; |
| 166 |
|
| 167 |
const |
| 168 |
ViewerEncodingsNames: array [TViewerEncoding] of string = |
| 169 |
('Auto-detect', |
| 170 |
'UTF-8', |
| 171 |
'UTF-8BOM', |
| 172 |
'ANSI', |
| 173 |
'OEM', |
| 174 |
'CP1250', |
| 175 |
'CP1251', |
| 176 |
'CP1252', |
| 177 |
'CP1253', |
| 178 |
'CP1254', |
| 179 |
'CP1255', |
| 180 |
'CP1256', |
| 181 |
'CP1257', |
| 182 |
'CP1258', |
| 183 |
'CP437', |
| 184 |
'CP850', |
| 185 |
'CP852', |
| 186 |
'CP866', |
| 187 |
'CP874', |
| 188 |
'CP932', |
| 189 |
'CP936', |
| 190 |
'CP949', |
| 191 |
'CP950', |
| 192 |
'ISO-8859-1', |
| 193 |
'ISO-8859-2', |
| 194 |
'KOI8-R', |
| 195 |
'KOI8-U', |
| 196 |
'KOI8-RU', |
| 197 |
'UCS-2LE', |
| 198 |
'UCS-2BE', |
| 199 |
'UTF-16LE', |
| 200 |
'UTF-16BE', |
| 201 |
'UTF-32LE', |
| 202 |
'UTF-32BE'); |
| 203 |
|
| 204 |
const |
| 205 |
ViewerEncodingMultiByte: TViewerEncodings = [ |
| 206 |
veUtf8, veUtf8bom, veUcs2le, veUcs2be, |
| 207 |
veUtf16le, veUtf16be, veUtf32le, veUtf32be]; |
| 208 |
|
| 209 |
ViewerEncodingDoubleByte: TViewerEncodings = [ |
| 210 |
veUcs2le, veUcs2be, veUtf16le, veUtf16be ]; |
| 211 |
|
| 212 |
type |
| 213 |
|
| 214 |
|
| 215 |
{ TViewerControl } |
| 216 |
|
| 217 |
TViewerControl = class(TCustomControl) |
| 218 |
protected |
| 219 |
FEncoding: TViewerEncoding; |
| 220 |
FViewerControlMode: TViewerControlMode; |
| 221 |
FFileName: String; |
| 222 |
FFileHandle: THandle; |
| 223 |
FFileSize: Int64; |
| 224 |
FMappingHandle: THandle; |
| 225 |
FMappedFile: Pointer; |
| 226 |
FPosition: PtrInt; |
| 227 |
FHPosition: Integer; // Tab for text during horizontal scroll |
| 228 |
FHLowEnd: Integer; // End for HPosition (string with max char) |
| 229 |
FVisibleOffset: PtrInt; // Offset in symbols for current line (see IsVisible and MakeVisible) |
| 230 |
FLowLimit: PtrInt; // Lowest possible value for Position |
| 231 |
FHighLimit: PtrInt; // Position cannot reach this value |
| 232 |
FBOMLength: Integer; |
| 233 |
FLineList: TPtrIntList; |
| 234 |
FBlockBeg: PtrInt; |
| 235 |
FBlockEnd: PtrInt; |
| 236 |
FCaretPos: PtrInt; |
| 237 |
FCaretPoint: TPoint; |
| 238 |
FMouseBlockBeg: PtrInt; |
| 239 |
FMouseBlockSide: TCharSide; |
| 240 |
FSelecting: Boolean; |
| 241 |
FTextWidth: Integer; // max char count or width in window |
| 242 |
FTextHeight: Integer; // measured values of font, rec calc at font changed |
| 243 |
FScrollBarVert: TScrollBar; |
| 244 |
FScrollBarHorz: TScrollBar; |
| 245 |
FOnPositionChanged: TNotifyEvent; |
| 246 |
FUpdateScrollBarPos: Boolean; // used to block updating of scrollbar |
| 247 |
FScrollBarPosition: Integer; // for updating vertical scrollbar based on Position |
| 248 |
FHScrollBarPosition: Integer; // for updating horizontal scrollbar based on HPosition |
| 249 |
FColCount: Integer; |
| 250 |
FTabSpaces: Integer; // tab width in spaces |
| 251 |
FMaxTextWidth: Integer; // maximum of chars on one line unwrapped text (max 16384) |
| 252 |
FExtraLineSpacing: Integer; |
| 253 |
FLeftMargin: Integer; |
| 254 |
FOnGuessEncoding: TGuessEncodingEvent; |
| 255 |
FOnFileOpen: TFileOpenEvent; |
| 256 |
FCaretVisible: Boolean; |
| 257 |
FShowCaret: Boolean; |
| 258 |
FAutoCopy: Boolean; |
| 259 |
FLastError: String; |
| 260 |
FText: String; |
| 261 |
|
| 262 |
FHex:TCustomCharsPresentation; |
| 263 |
FDec:TCustomCharsPresentation; |
| 264 |
FCustom:TCustomCharsPresentation; |
| 265 |
|
| 266 |
function GetPercent: Integer; |
| 267 |
procedure SetPercent(const AValue: Integer); |
| 268 |
procedure SetBlockBegin(const AValue: PtrInt); |
| 269 |
procedure SetBlockEnd(const AValue: PtrInt); |
| 270 |
procedure SetPosition(Value: PtrInt); virtual; |
| 271 |
procedure SetHPosition(Value: Integer); |
| 272 |
procedure SetPosition(Value: PtrInt; Force: Boolean); overload; |
| 273 |
procedure SetHPosition(Value: Integer; Force: Boolean); overload; |
| 274 |
procedure SetEncoding(AEncoding: TViewerEncoding); |
| 275 |
function GetEncodingName: string; |
| 276 |
procedure SetEncodingName(AEncodingName: string); |
| 277 |
procedure SetViewerMode(Value: TViewerControlMode); |
| 278 |
procedure SetColCount(const AValue: Integer); |
| 279 |
procedure SetMaxTextWidth(const AValue: Integer); |
| 280 |
procedure SetTabSpaces(const AValue: Integer); |
| 281 |
procedure SetShowCaret(AValue: Boolean); |
| 282 |
procedure SetCaretPos(AValue: PtrInt); |
| 283 |
|
| 284 |
{en |
| 285 |
Returns how many lines (given current FTextHeight) will fit into the window. |
| 286 |
} |
| 287 |
function GetClientHeightInLines(Whole: Boolean = True): Integer; inline; |
| 288 |
|
| 289 |
{en |
| 290 |
Calculates how many lines can be displayed from given position. |
| 291 |
param(FromPosition |
| 292 |
Position from which to check. It should point to a start of a line.) |
| 293 |
@param(LastLineReached |
| 294 |
If it is set to @true when the function returns, then the last |
| 295 |
line of text was reached when scanning. |
| 296 |
This means that there are no more lines to be displayed other than |
| 297 |
the ones scanned from FromPosition. In other words: |
| 298 |
SetPosition(GetStartOfNextLine(FromPosition)) will be one line |
| 299 |
too many and will be scrolled back.) |
| 300 |
} |
| 301 |
function GetLinesTillEnd(FromPosition: PtrInt; out LastLineReached: Boolean): Integer; |
| 302 |
|
| 303 |
function GetBomLength: Integer; |
| 304 |
|
| 305 |
procedure UpdateLimits; |
| 306 |
|
| 307 |
{en |
| 308 |
@param(iStartPos |
| 309 |
Should point to start of a line. |
| 310 |
It is increased by the amount of parsed data (with line endings).) |
| 311 |
@param(aLimit |
| 312 |
Position which cannot be reached while reading from file.) |
| 313 |
@param(DataLength |
| 314 |
It is length in bytes of parsed data without any line endings. |
| 315 |
iStartPos is moved beyond the line endings though.) |
| 316 |
} |
| 317 |
function CalcTextLineLength(var iStartPos: PtrInt; const aLimit: Int64; out DataLength: PtrInt): Integer; |
| 318 |
|
| 319 |
function GetStartOfLine(aPosition: PtrInt): PtrInt; |
| 320 |
function GetEndOfLine(aPosition: PtrInt): PtrInt; |
| 321 |
|
| 322 |
function GetStartOfPrevLine(aPosition: PtrInt): PtrInt; |
| 323 |
function GetStartOfNextLine(aPosition: PtrInt): PtrInt; |
| 324 |
|
| 325 |
{en |
| 326 |
Changes the value of aPosition to X lines back or forward. |
| 327 |
@param(aPosition |
| 328 |
File position to change.) |
| 329 |
@param(iLines |
| 330 |
Nr of lines to scroll. |
| 331 |
If positive the position is increased by iLines lines, |
| 332 |
if negative the position is decreased by -iLines lines.) |
| 333 |
} |
| 334 |
function ScrollPosition(var aPosition: PtrInt; iLines: Integer): Boolean; |
| 335 |
|
| 336 |
{en |
| 337 |
Calculates (x,y) cursor position to a position within file. |
| 338 |
@param(x |
| 339 |
Client X coordinate of mouse cursor.) |
| 340 |
@param(y |
| 341 |
Client Y coordinate of mouse cursor.) |
| 342 |
@param(CharSide |
| 343 |
To which side of a character at returned position the (x,y) points to. |
| 344 |
Only valid if returned position is not -1.) |
| 345 |
@returns(Position in file to which (x,y) points to, based on what is |
| 346 |
currently displayed. |
| 347 |
Returns -1 if (x,y) doesn't point to any position (outside of |
| 348 |
the text for example).) |
| 349 |
} |
| 350 |
function XYPos2Adr(x, y: Integer; out CharSide: TCharSide): PtrInt; |
| 351 |
|
| 352 |
procedure OutText(x, y: Integer; const sText: String; StartPos: PtrInt; DataLength: Integer); |
| 353 |
procedure OutBin(x, y: Integer; const sText: String; StartPos: PtrInt; DataLength: Integer); |
| 354 |
|
| 355 |
procedure OutCustom(x, y: Integer; const sText: string;StartPos: PtrInt; DataLength: Integer); // render one line |
| 356 |
function TransformCustom(var APosition: PtrInt; ALimit: PtrInt;AWithAdditionalData:boolean=True): AnsiString; |
| 357 |
function TransformCustomBlock(var APosition: PtrInt; DataLength: integer ; ASeparatorsOn, AAlignData:boolean; out AChars:AnsiString): AnsiString; |
| 358 |
|
| 359 |
function HexToValueProc(AChar:AnsiChar;AMaxDigitsCount:integer):AnsiString; |
| 360 |
function DecToValueProc(AChar:AnsiChar;AMaxDigitsCount:integer):AnsiString; |
| 361 |
|
| 362 |
procedure WriteBin; |
| 363 |
procedure WriteText; |
| 364 |
procedure WriteCustom; virtual; |
| 365 |
function TransformText(const sText: String; const Xoffset: Integer): String; |
| 366 |
function TransformBin(var aPosition: PtrInt; aLimit: PtrInt): AnsiString; |
| 367 |
function TransformHex(var aPosition: PtrInt; aLimit: PtrInt): AnsiString;virtual; |
| 368 |
|
| 369 |
procedure AddLineOffset(const iOffset: PtrInt); inline; |
| 370 |
|
| 371 |
procedure DrawLastError; |
| 372 |
|
| 373 |
function MapFile(const sFileName: String): Boolean; |
| 374 |
procedure UnMapFile; |
| 375 |
procedure SetFileName(const sFileName: String); |
| 376 |
|
| 377 |
procedure UpdateScrollbars; |
| 378 |
|
| 379 |
procedure ViewerResize(Sender: TObject); |
| 380 |
|
| 381 |
{en |
| 382 |
Returns next unicode character from the file, depending on Encoding. |
| 383 |
It is a faster version, which does as little conversion as possible, |
| 384 |
but only Ascii values are guaranteed to be valid (0-127). |
| 385 |
Other unicode values may/may not be valid, so shouldn't be tested. |
| 386 |
This function is used for reading pure ascii characters such as |
| 387 |
line endings, tabs, white spaces, etc. |
| 388 |
} |
| 389 |
function GetNextCharAsAscii(const iPosition: PtrInt; out CharLenInBytes: Integer): Cardinal; |
| 390 |
function GetPrevCharAsAscii(const iPosition: PtrInt; out CharLenInBytes: Integer): Cardinal; |
| 391 |
|
| 392 |
{en |
| 393 |
Retrieve next character from the file depending on encoding and |
| 394 |
automatically convert it to UTF-8. |
| 395 |
If CharLenInBytes is greater than 0 but the result is an empty string |
| 396 |
then it's possible there was no appropriate UTF-8 character for the |
| 397 |
next character of the current encoding. |
| 398 |
} |
| 399 |
function GetNextCharAsUtf8(const iPosition: PtrInt; out CharLenInBytes: Integer): String; |
| 400 |
|
| 401 |
procedure ReReadFile; |
| 402 |
|
| 403 |
{en |
| 404 |
Searches for an ASCII character. |
| 405 |
@param(aPosition |
| 406 |
Position from where the search starts.) |
| 407 |
@param(aMaxBytes |
| 408 |
How many bytes are available for reading.) |
| 409 |
@param(AsciiChars |
| 410 |
The function searches for any character that this string contains.) |
| 411 |
@param(bFindNotIn |
| 412 |
If @true searches for first character not included in AsciiChars. |
| 413 |
If @false searches for first character included in AsciiChars.) |
| 414 |
} |
| 415 |
function FindAsciiSetForward(aPosition, aMaxBytes: PtrInt; |
| 416 |
const AsciiChars: String; |
| 417 |
bFindNotIn: Boolean): PtrInt; |
| 418 |
|
| 419 |
{en |
| 420 |
Same as FindForward but it searches backwards from pAdr. |
| 421 |
aMaxBytes must be number of available bytes for reading backwards from pAdr. |
| 422 |
} |
| 423 |
function FindAsciiSetBackward(aPosition, aMaxBytes: PtrInt; |
| 424 |
const AsciiChars: String; |
| 425 |
bFindNotIn: Boolean): PtrInt; |
| 426 |
|
| 427 |
{en |
| 428 |
Checks if current selection is still valid given current viewer mode and encoding. |
| 429 |
For example checks if selection is not in the middle of a unicode character. |
| 430 |
} |
| 431 |
procedure UpdateSelection; |
| 432 |
function GetViewerRect: TRect; |
| 433 |
|
| 434 |
procedure ScrollBarVertScroll(Sender: TObject; ScrollCode: TScrollCode; |
| 435 |
var ScrollPos: Integer); |
| 436 |
procedure ScrollBarHorzScroll(Sender: TObject; ScrollCode: TScrollCode; |
| 437 |
var ScrollPos: Integer); |
| 438 |
|
| 439 |
function GetText(const StartPos, Len: PtrInt; const Xoffset: Integer): string; |
| 440 |
|
| 441 |
procedure SetText(const AValue: String); |
| 442 |
|
| 443 |
protected |
| 444 |
procedure WMSetFocus(var Message: TLMSetFocus); message LM_SETFOCUS; |
| 445 |
procedure WMKillFocus(var Message: TLMKillFocus); message LM_KILLFOCUS; |
| 446 |
procedure FontChanged(Sender: TObject); override; |
| 447 |
procedure KeyDown(var Key: word; Shift: TShiftState); override; |
| 448 |
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override; |
| 449 |
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override; |
| 450 |
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override; |
| 451 |
function DoMouseWheelDown(Shift: TShiftState; MousePos: TPoint): Boolean; override; |
| 452 |
function DoMouseWheelUp(Shift: TShiftState; MousePos: TPoint): Boolean; override; |
| 453 |
function DoMouseWheelLeft(Shift: TShiftState; MousePos: TPoint): Boolean; override; |
| 454 |
function DoMouseWheelRight(Shift: TShiftState; MousePos: TPoint): Boolean; override; |
| 455 |
procedure DoAutoAdjustLayout(const AMode: TLayoutAdjustmentPolicy; const AXProportion, AYProportion: Double); override; |
| 456 |
|
| 457 |
public |
| 458 |
constructor Create(AOwner: TComponent); override; |
| 459 |
destructor Destroy; override; |
| 460 |
|
| 461 |
procedure Paint; override; |
| 462 |
|
| 463 |
{en |
| 464 |
Scrolls the displayed text in the window. |
| 465 |
|
| 466 |
@param(iLines |
| 467 |
Nr of lines to scroll. |
| 468 |
If positive the text is scrolled downwards, |
| 469 |
if negative the text is scrolled upwards.) |
| 470 |
@returns(@true if the text was scrolled.) |
| 471 |
} |
| 472 |
function Scroll(iLines: Integer): Boolean; |
| 473 |
function HScroll(iSymbols: Integer): Boolean; |
| 474 |
|
| 475 |
procedure PageUp; |
| 476 |
procedure PageDown; |
| 477 |
procedure GoHome; |
| 478 |
procedure GoEnd; |
| 479 |
|
| 480 |
procedure HPageUp; |
| 481 |
procedure HPageDown; |
| 482 |
procedure HGoHome; |
| 483 |
procedure HGoEnd; |
| 484 |
|
| 485 |
procedure CaretGoHome; |
| 486 |
procedure CaretGoEnd; |
| 487 |
|
| 488 |
function GetDataAdr: Pointer; |
| 489 |
|
| 490 |
procedure SelectAll; |
| 491 |
procedure SelectText(AStart, AEnd: PtrInt); |
| 492 |
procedure CopyToClipboard; |
| 493 |
procedure CopyToClipboardF; |
| 494 |
function Selection: String; |
| 495 |
|
| 496 |
function IsVisible(const aPosition: PtrInt): Boolean; overload; |
| 497 |
procedure MakeVisible(const aPosition: PtrInt); |
| 498 |
|
| 499 |
function ConvertToUTF8(const sText: AnsiString): String; |
| 500 |
function ConvertFromUTF8(const sText: String): AnsiString; |
| 501 |
function FindUtf8Text(iStartPos: PtrInt; const sSearchText: String; |
| 502 |
bCaseSensitive: Boolean; bSearchBackwards: Boolean): PtrInt; |
| 503 |
|
| 504 |
procedure ResetEncoding; |
| 505 |
function IsFileOpen: Boolean; inline; |
| 506 |
function DetectEncoding: TViewerEncoding; |
| 507 |
procedure GetSupportedEncodings(List: TStrings); |
| 508 |
|
| 509 |
property Text: String read FText write SetText; |
| 510 |
property Percent: Integer Read GetPercent Write SetPercent; |
| 511 |
property Position: PtrInt Read FPosition Write SetPosition; |
| 512 |
property FileSize: Int64 Read FFileSize; |
| 513 |
property FileHandle: THandle read FFileHandle; |
| 514 |
property CaretPos: PtrInt Read FCaretPos Write SetCaretPos; |
| 515 |
property SelectionStart: PtrInt Read FBlockBeg Write SetBlockBegin; |
| 516 |
property SelectionEnd: PtrInt Read FBlockEnd Write SetBlockEnd; |
| 517 |
property EncodingName: string Read GetEncodingName Write SetEncodingName; |
| 518 |
property ColCount: Integer Read FColCount Write SetColCount; |
| 519 |
property MaxTextWidth: Integer read FMaxTextWidth write SetMaxTextWidth; |
| 520 |
property TabSpaces: Integer read FTabSpaces write SetTabSpaces; |
| 521 |
property LeftMargin: Integer read FLeftMargin write FLeftMargin; |
| 522 |
property ExtraLineSpacing: Integer read FExtraLineSpacing write FExtraLineSpacing; |
| 523 |
property AutoCopy: Boolean read FAutoCopy write FAutoCopy; |
| 524 |
property OnGuessEncoding: TGuessEncodingEvent Read FOnGuessEncoding Write FOnGuessEncoding; |
| 525 |
property OnFileOpen: TFileOpenEvent read FOnFileOpen write FOnFileOpen; |
| 526 |
|
| 527 |
published |
| 528 |
property Mode: TViewerControlMode Read FViewerControlMode Write SetViewerMode default vcmWrap; |
| 529 |
property FileName: String Read FFileName Write SetFileName; |
| 530 |
property Encoding: TViewerEncoding Read FEncoding Write SetEncoding default veAutoDetect; |
| 531 |
property OnPositionChanged: TNotifyEvent Read FOnPositionChanged Write FOnPositionChanged; |
| 532 |
property ShowCaret: Boolean read FShowCaret write SetShowCaret; |
| 533 |
|
| 534 |
property OnClick; |
| 535 |
property OnMouseDown; |
| 536 |
property OnMouseMove; |
| 537 |
property OnMouseUp; |
| 538 |
|
| 539 |
property OnMouseWheelUp; |
| 540 |
property OnMouseWheelDown; |
| 541 |
|
| 542 |
property Align; |
| 543 |
property Color; |
| 544 |
property Cursor default crIBeam; |
| 545 |
property Font; |
| 546 |
property ParentColor default False; |
| 547 |
property TabStop default True; |
| 548 |
end; |
| 549 |
|
| 550 |
procedure Register; |
| 551 |
|
| 552 |
implementation |
| 553 |
|
| 554 |
uses |
| 555 |
Math, LCLType, Graphics, Forms, LCLProc, Clipbrd, LConvEncoding, |
| 556 |
DCUnicodeUtils, LCLIntf, LazUTF8, DCOSUtils , DCConvertEncoding |
| 557 |
{$IF LCL_FULLVERSION >= 4990000} |
| 558 |
, LazUTF16 |
| 559 |
{$ENDIF} |
| 560 |
{$IF DEFINED(UNIX)} |
| 561 |
, BaseUnix, Unix, DCUnix |
| 562 |
{$ELSEIF DEFINED(WINDOWS)} |
| 563 |
, Windows, DCWindows |
| 564 |
{$ENDIF}; |
| 565 |
|
| 566 |
|
| 567 |
const |
| 568 |
cBinWidth = 80; |
| 569 |
|
| 570 |
// These strings must be Ascii only. |
| 571 |
sNonCharacter: string = ' !"#$%&''()*+,-./:;<=>?@[\]^`{|}~'#13#10#9; |
| 572 |
sWhiteSpace : string = ' '#13#10#9#8; |
| 573 |
|
| 574 |
{ TCustomCharsPresentation } |
| 575 |
|
| 576 |
constructor TCustomCharsPresentation.Create(APresentValuesPerLine, |
| 577 |
ACharMaxPresentWidth, AOffsetWidth, ACountSeparate: integer;AChrToValueProc:TCharToCustomValueTransformProc); |
| 578 |
begin |
| 579 |
SpaceCount:=1; // count of spacebars between values, =1 |
| 580 |
ValuesPerLine := APresentValuesPerLine; // default for hex: 16 values |
| 581 |
MaxAddrDigits := AOffsetWidth; // = 8 , count of symbols for display caret offset |
| 582 |
StartOfs := AOffsetWidth + 2; // ': ' |
| 583 |
MaxValueDigits := ACharMaxPresentWidth; // hex char (FF) - 2 symbols, dec char (255) - 3 symbols |
| 584 |
EndOfs := StartOfs + (ValuesPerLine * (MaxValueDigits+SpaceCount)); // +1 - take in spacebar |
| 585 |
StartAscii := StartOfs + (ValuesPerLine * (MaxValueDigits+SpaceCount)) + 2; // ' ' |
| 586 |
|
| 587 |
SeparatorChar:='|'; |
| 588 |
CountSeperate:=ACountSeparate; |
| 589 |
|
| 590 |
SeparatorSpace:=' '; |
| 591 |
|
| 592 |
ChrToValueProc:=AChrToValueProc; // method for convert char to Value |
| 593 |
end; |
| 594 |
|
| 595 |
destructor TCustomCharsPresentation.Destroy; |
| 596 |
begin |
| 597 |
inherited; |
| 598 |
end; |
| 599 |
|
| 600 |
// ---------------------------------------------------------------------------- |
| 601 |
|
| 602 |
constructor TViewerControl.Create(AOwner: TComponent); |
| 603 |
begin |
| 604 |
inherited Create(AOwner); |
| 605 |
Cursor := crIBeam; |
| 606 |
ParentColor := False; |
| 607 |
DoubleBuffered := True; |
| 608 |
ControlStyle := ControlStyle + [csTripleClicks, csOpaque]; |
| 609 |
TabStop := True; // so that it can get keyboard focus |
| 610 |
|
| 611 |
FEncoding := veAutoDetect; |
| 612 |
FViewerControlMode := vcmText; |
| 613 |
FCustom := nil; |
| 614 |
FFileName := ''; |
| 615 |
FMappedFile := nil; |
| 616 |
FFileHandle := 0; |
| 617 |
FMappingHandle := 0; |
| 618 |
FPosition := 0; |
| 619 |
FHPosition := 0; |
| 620 |
FHLowEnd := 0; |
| 621 |
FLowLimit := 0; |
| 622 |
FHighLimit := 0; |
| 623 |
FBOMLength := 0; |
| 624 |
FTextHeight:= 14; // dummy value |
| 625 |
FColCount := 1; |
| 626 |
FTabSpaces := 8; |
| 627 |
FLeftMargin := 4; |
| 628 |
FMaxTextWidth := 1024; |
| 629 |
FAutoCopy := True; |
| 630 |
|
| 631 |
FLineList := TPtrIntList.Create; |
| 632 |
|
| 633 |
FScrollBarVert := TScrollBar.Create(Self); |
| 634 |
FScrollBarVert.Parent := Self; |
| 635 |
FScrollBarVert.Kind := sbVertical; |
| 636 |
FScrollBarVert.Align := alRight; |
| 637 |
FScrollBarVert.OnScroll := @ScrollBarVertScroll; |
| 638 |
FScrollBarVert.TabStop := False; |
| 639 |
FScrollBarVert.PageSize := 0; |
| 640 |
|
| 641 |
FScrollBarHorz := TScrollBar.Create(Self); |
| 642 |
FScrollBarHorz.Parent := Self; |
| 643 |
FScrollBarHorz.Kind := sbHorizontal; |
| 644 |
FScrollBarHorz.Align := alBottom; |
| 645 |
FScrollBarHorz.OnScroll := @ScrollBarHorzScroll; |
| 646 |
FScrollBarHorz.TabStop := False; |
| 647 |
FScrollBarHorz.PageSize := 0; |
| 648 |
|
| 649 |
FUpdateScrollBarPos := True; |
| 650 |
FScrollBarPosition := 0; |
| 651 |
FHScrollBarPosition := 0; |
| 652 |
|
| 653 |
FOnPositionChanged := nil; |
| 654 |
FOnGuessEncoding := nil; |
| 655 |
|
| 656 |
OnResize := @ViewerResize; |
| 657 |
|
| 658 |
FHex:=TCustomCharsPresentation.Create(16,2,8,8,@HexToValueProc); |
| 659 |
FDec:=TCustomCharsPresentation.Create(15,3,8,5,@DecToValueProc); // for set bigger ValuePerLine need to improve method GetEndOfLine |
| 660 |
|
| 661 |
end; |
| 662 |
|
| 663 |
destructor TViewerControl.Destroy; |
| 664 |
begin |
| 665 |
FHex.Free; |
| 666 |
FDec.Free; |
| 667 |
|
| 668 |
FHex:=nil; |
| 669 |
FDec:=nil; |
| 670 |
FCustom:=nil; |
| 671 |
|
| 672 |
UnMapFile; |
| 673 |
|
| 674 |
if Assigned(FLineList) then |
| 675 |
FreeAndNil(FLineList); |
| 676 |
|
| 677 |
inherited Destroy; |
| 678 |
end; |
| 679 |
|
| 680 |
procedure TViewerControl.DrawLastError; |
| 681 |
var |
| 682 |
AStyle: TTextStyle; |
| 683 |
begin |
| 684 |
AStyle:= Canvas.TextStyle; |
| 685 |
AStyle.Alignment:= taCenter; |
| 686 |
AStyle.Layout:= tlCenter; |
| 687 |
Canvas.Pen.Color := Canvas.Font.Color; |
| 688 |
Canvas.Line(0, 0, ClientWidth - 1, ClientHeight - 1); |
| 689 |
Canvas.Line(0, ClientHeight - 1, ClientWidth - 1, 0); |
| 690 |
Canvas.TextRect(GetViewerRect, 0, 0, FLastError, AStyle); |
| 691 |
end; |
| 692 |
|
| 693 |
procedure TViewerControl.Paint; |
| 694 |
var |
| 695 |
AText: String; |
| 696 |
begin |
| 697 |
if not IsFileOpen then |
| 698 |
begin |
| 699 |
DrawLastError; |
| 700 |
Exit; |
| 701 |
end; |
| 702 |
|
| 703 |
if FShowCaret and FCaretVisible then |
| 704 |
begin |
| 705 |
FCaretPoint.X := -1; |
| 706 |
FCaretVisible := not LCLIntf.HideCaret(Handle); |
| 707 |
end; |
| 708 |
|
| 709 |
Canvas.Font := Self.Font; |
| 710 |
Canvas.Brush.Color := Self.Color; |
| 711 |
{$IF DEFINED(LCLQT) and (LCL_FULLVERSION < 093100)} |
| 712 |
Canvas.Brush.Style := bsSolid; |
| 713 |
Canvas.FillRect(ClientRect); |
| 714 |
{$ENDIF} |
| 715 |
Canvas.Brush.Style := bsClear; |
| 716 |
FTextHeight := Canvas.TextHeight('Wg') + FExtraLineSpacing; |
| 717 |
|
| 718 |
if FViewerControlMode = vcmBook then |
| 719 |
FTextWidth := ((ClientWidth - (Canvas.TextWidth('W') * FColCount)) div FColCount) |
| 720 |
else begin |
| 721 |
AText := StringOfChar('W', FMaxTextWidth); |
| 722 |
FTextWidth := Canvas.TextFitInfo(AText, GetViewerRect.Width - FLeftMargin); |
| 723 |
end; |
| 724 |
|
| 725 |
FLineList.Clear; |
| 726 |
|
| 727 |
case FViewerControlMode of |
| 728 |
vcmBin : WriteBin; |
| 729 |
vcmText: WriteText; |
| 730 |
vcmWrap: WriteText; |
| 731 |
vcmBook: WriteText; |
| 732 |
vcmDec,vcmHex : WriteCustom; |
| 733 |
end; |
| 734 |
|
| 735 |
if FShowCaret and (FCaretPoint.X > -1) then |
| 736 |
begin |
| 737 |
LCLIntf.SetCaretPos(FCaretPoint.X, FCaretPoint.Y); |
| 738 |
if not FCaretVisible then FCaretVisible:= LCLIntf.ShowCaret(Handle); |
| 739 |
end; |
| 740 |
end; |
| 741 |
|
| 742 |
procedure TViewerControl.SetViewerMode(Value: TViewerControlMode); |
| 743 |
begin |
| 744 |
if not (csDesigning in ComponentState) then |
| 745 |
begin |
| 746 |
FLineList.Clear; // do not use cache from previous mode |
| 747 |
|
| 748 |
FViewerControlMode := Value; |
| 749 |
case FViewerControlMode of |
| 750 |
vcmHex: FCustom := FHex; |
| 751 |
vcmDec: FCustom := FDec; |
| 752 |
else |
| 753 |
FCustom := nil; |
| 754 |
end; |
| 755 |
|
| 756 |
if not IsFileOpen then |
| 757 |
Exit; |
| 758 |
|
| 759 |
// Take limits into account for selection. |
| 760 |
FBlockBeg := FBlockBeg + (GetDataAdr - FMappedFile); |
| 761 |
FBlockEnd := FBlockEnd + (GetDataAdr - FMappedFile); |
| 762 |
|
| 763 |
FHPosition := 0; |
| 764 |
FBOMLength := GetBomLength; |
| 765 |
UpdateLimits; |
| 766 |
|
| 767 |
// Take limits into account for selection. |
| 768 |
FBlockBeg := FBlockBeg - (GetDataAdr - FMappedFile); |
| 769 |
FBlockEnd := FBlockEnd - (GetDataAdr - FMappedFile); |
| 770 |
|
| 771 |
UpdateSelection; |
| 772 |
|
| 773 |
// Force recalculating position. |
| 774 |
SetPosition(FPosition, True); |
| 775 |
SetHPosition(FHPosition, True); |
| 776 |
|
| 777 |
UpdateScrollbars; |
| 778 |
Invalidate; |
| 779 |
end |
| 780 |
else |
| 781 |
FViewerControlMode := Value; |
| 782 |
end; |
| 783 |
|
| 784 |
procedure TViewerControl.SetColCount(const AValue: Integer); |
| 785 |
begin |
| 786 |
if AValue > 0 then FColCount := AValue |
| 787 |
else FColCount := 1; |
| 788 |
end; |
| 789 |
|
| 790 |
procedure TViewerControl.SetMaxTextWidth(const AValue: Integer); |
| 791 |
begin |
| 792 |
if AValue < 80 then |
| 793 |
FMaxTextWidth := 80 |
| 794 |
else if AValue > 16384 then |
| 795 |
FMaxTextWidth := 16384 |
| 796 |
else |
| 797 |
FMaxTextWidth:= AValue; |
| 798 |
end; |
| 799 |
|
| 800 |
procedure TViewerControl.SetTabSpaces(const AValue: Integer); |
| 801 |
begin |
| 802 |
if AValue < 1 then |
| 803 |
FTabSpaces := 1 |
| 804 |
else if AValue > 32 then |
| 805 |
FTabSpaces := 32 |
| 806 |
else |
| 807 |
FTabSpaces := AValue; |
| 808 |
end; |
| 809 |
|
| 810 |
function TViewerControl.ScrollPosition(var aPosition: PtrInt; iLines: Integer): Boolean; |
| 811 |
var |
| 812 |
i: Integer; |
| 813 |
NewPos: PtrInt; |
| 814 |
begin |
| 815 |
Result := False; |
| 816 |
NewPos := aPosition; |
| 817 |
if iLines < 0 then |
| 818 |
for i := 1 to -iLines do |
| 819 |
NewPos := GetStartOfPrevLine(NewPos) |
| 820 |
else |
| 821 |
for i := 1 to iLines do |
| 822 |
NewPos := GetStartOfNextLine(NewPos); |
| 823 |
|
| 824 |
Result := aPosition <> NewPos; |
| 825 |
aPosition := NewPos; |
| 826 |
end; |
| 827 |
|
| 828 |
function TViewerControl.Scroll(iLines: Integer): Boolean; |
| 829 |
var |
| 830 |
aPosition: PtrInt; |
| 831 |
begin |
| 832 |
if not IsFileOpen then |
| 833 |
Exit(False); |
| 834 |
aPosition := FPosition; |
| 835 |
Result := ScrollPosition(aPosition, iLines); |
| 836 |
if aPosition <> FPosition then |
| 837 |
SetPosition(aPosition); |
| 838 |
end; |
| 839 |
|
| 840 |
function TViewerControl.HScroll(iSymbols: Integer): Boolean; |
| 841 |
var |
| 842 |
newPos: Integer; |
| 843 |
begin |
| 844 |
if not IsFileOpen then |
| 845 |
Exit(False); |
| 846 |
newPos := FHPosition + iSymbols; |
| 847 |
if newPos < 0 then |
| 848 |
newPos := 0 |
| 849 |
else if (newPos > FHLowEnd - FTextWidth) and (FHLowEnd - FTextWidth > 0) then |
| 850 |
newPos := FHLowEnd - FTextWidth; |
| 851 |
if newPos <> FHPosition then |
| 852 |
SetHPosition(newPos); |
| 853 |
Result:= True; |
| 854 |
end; |
| 855 |
|
| 856 |
function TViewerControl.GetText(const StartPos, Len: PtrInt; const Xoffset: Integer): string; |
| 857 |
begin |
| 858 |
SetString(Result, GetDataAdr + StartPos, Len); |
| 859 |
Result := TransformText(ConvertToUTF8(Result), Xoffset); |
| 860 |
end; |
| 861 |
|
| 862 |
procedure TViewerControl.SetText(const AValue: String); |
| 863 |
begin |
| 864 |
UnMapFile; |
| 865 |
FText:= AValue; |
| 866 |
FileName:= EmptyStr; |
| 867 |
FFileSize:= Length(FText); |
| 868 |
FMappedFile:= Pointer(FText); |
| 869 |
end; |
| 870 |
|
| 871 |
function TViewerControl.GetViewerRect: TRect; |
| 872 |
begin |
| 873 |
Result:= GetClientRect; |
| 874 |
if Assigned(FScrollBarHorz) and FScrollBarHorz.Visible then |
| 875 |
Dec(Result.Bottom, FScrollBarHorz.Height); |
| 876 |
if Assigned(FScrollBarVert) and FScrollBarVert.Visible then |
| 877 |
Dec(Result.Right, FScrollBarVert.Width); |
| 878 |
end; |
| 879 |
|
| 880 |
procedure TViewerControl.WMSetFocus(var Message: TLMSetFocus); |
| 881 |
begin |
| 882 |
if FShowCaret then |
| 883 |
begin |
| 884 |
LCLIntf.CreateCaret(Handle, 0, 2, FTextHeight); |
| 885 |
LCLIntf.ShowCaret(Handle); |
| 886 |
FCaretVisible:= True; |
| 887 |
end; |
| 888 |
end; |
| 889 |
|
| 890 |
procedure TViewerControl.WMKillFocus(var Message: TLMKillFocus); |
| 891 |
begin |
| 892 |
if FShowCaret then |
| 893 |
begin |
| 894 |
FCaretVisible:= False; |
| 895 |
LCLIntf.DestroyCaret(Handle); |
| 896 |
end; |
| 897 |
end; |
| 898 |
|
| 899 |
procedure TViewerControl.FontChanged(Sender: TObject); |
| 900 |
begin |
| 901 |
inherited FontChanged(Sender); |
| 902 |
|
| 903 |
if HandleAllocated then |
| 904 |
begin |
| 905 |
FTextHeight := Canvas.TextHeight('Wg') + FExtraLineSpacing; |
| 906 |
if FShowCaret then LCLIntf.CreateCaret(Handle, 0, 2, FTextHeight); |
| 907 |
end; |
| 908 |
end; |
| 909 |
|
| 910 |
function TViewerControl.CalcTextLineLength(var iStartPos: PtrInt; const aLimit: Int64; out DataLength: PtrInt): Integer; |
| 911 |
var |
| 912 |
MaxLineLength: Boolean; |
| 913 |
CharLenInBytes: Integer; |
| 914 |
OldPos, LastSpacePos: PtrInt; |
| 915 |
LastSpaceResult: Integer; |
| 916 |
begin |
| 917 |
Result := 0; |
| 918 |
DataLength := 0; |
| 919 |
LastSpacePos := -1; |
| 920 |
MaxLineLength := True; |
| 921 |
OldPos := iStartPos; |
| 922 |
|
| 923 |
while MaxLineLength and (iStartPos < aLimit) do |
| 924 |
begin |
| 925 |
case GetNextCharAsAscii(iStartPos, CharLenInBytes) of |
| 926 |
9: // tab |
| 927 |
Inc(Result, FTabSpaces - Result mod FTabSpaces); |
| 928 |
10: // stroka |
| 929 |
begin |
| 930 |
DataLength := iStartPos - OldPos; |
| 931 |
iStartPos := iStartPos + CharLenInBytes; |
| 932 |
Exit; |
| 933 |
end; |
| 934 |
13: // karetka |
| 935 |
begin |
| 936 |
DataLength := iStartPos - OldPos; |
| 937 |
iStartPos := iStartPos + CharLenInBytes; |
| 938 |
// Move after possible #10. |
| 939 |
if (iStartPos < aLimit) and (GetNextCharAsAscii(iStartPos, CharLenInBytes) = 10) then |
| 940 |
Inc(iStartPos, CharLenInBytes); |
| 941 |
Exit; |
| 942 |
end; |
| 943 |
32, 33, 40, 41, 44, 45, 46, 47, 92, 58, 59, 63, 91, 93: //probel |
| 944 |
begin |
| 945 |
Inc(Result, 1); |
| 946 |
LastSpacePos := iStartPos + CharLenInBytes; |
| 947 |
LastSpaceResult := Result; |
| 948 |
end; |
| 949 |
else |
| 950 |
Inc(Result, 1); |
| 951 |
end; |
| 952 |
|
| 953 |
if CharLenInBytes = 0 then // End of data or invalid character. |
| 954 |
break; |
| 955 |
|
| 956 |
iStartPos := iStartPos + CharLenInBytes; |
| 957 |
DataLength := iStartPos - OldPos; |
| 958 |
case FViewerControlMode of |
| 959 |
vcmText: MaxLineLength := Result < FMaxTextWidth; |
| 960 |
vcmWrap: MaxLineLength := Result < FTextWidth; |
| 961 |
vcmBook: MaxLineLength := Canvas.TextWidth(GetText(OldPos, DataLength, 0)) < FTextWidth; |
| 962 |
else |
| 963 |
Exit; |
| 964 |
end; |
| 965 |
end; |
| 966 |
|
| 967 |
if (not MaxLineLength) and (LastSpacePos <> -1) then |
| 968 |
begin |
| 969 |
iStartPos := LastSpacePos; |
| 970 |
Result := LastSpaceResult; |
| 971 |
DataLength := iStartPos - OldPos; |
| 972 |
end; |
| 973 |
end; |
| 974 |
|
| 975 |
function TViewerControl.TransformText(const sText: String; const Xoffset: Integer): String; |
| 976 |
var |
| 977 |
c: AnsiChar; |
| 978 |
i: Integer; |
| 979 |
begin |
| 980 |
Result := ''; |
| 981 |
for i := 1 to Length(sText) do |
| 982 |
begin |
| 983 |
c := sText[i]; |
| 984 |
// Parse only ASCII chars. |
| 985 |
case c of |
| 986 |
#9: |
| 987 |
Result := Result + StringOfChar(' ', |
| 988 |
FTabSpaces - (UTF8Length(Result) + Xoffset) mod FTabSpaces); |
| 989 |
else |
| 990 |
begin |
| 991 |
if c < ' ' then |
| 992 |
Result := Result + ' ' |
| 993 |
else |
| 994 |
Result := Result + c; |
| 995 |
end; |
| 996 |
end; |
| 997 |
end; |
| 998 |
end; |
| 999 |
|
| 1000 |
|
| 1001 |
function TViewerControl.TransformBin(var aPosition: PtrInt; aLimit: PtrInt): AnsiString; |
| 1002 |
var |
| 1003 |
c: AnsiChar; |
| 1004 |
i: Integer; |
| 1005 |
begin |
| 1006 |
Result := ''; |
| 1007 |
for i := 0 to cBinWidth - 1 do |
| 1008 |
begin |
| 1009 |
if aPosition >= aLimit then |
| 1010 |
Break; |
| 1011 |
c := PAnsiChar(GetDataAdr)[aPosition]; |
| 1012 |
if c < ' ' then |
| 1013 |
Result := Result + '.' |
| 1014 |
else if c > #127 then |
| 1015 |
Result := Result + '.' |
| 1016 |
else |
| 1017 |
Result := Result + c; |
| 1018 |
|
| 1019 |
Inc(aPosition); |
| 1020 |
end; |
| 1021 |
end; |
| 1022 |
|
| 1023 |
function TViewerControl.TransformHex(var aPosition: PtrInt; aLimit: PtrInt): AnsiString; |
| 1024 |
begin |
| 1025 |
Result:=TransformCustom(aPosition,aLimit); |
| 1026 |
end; |
| 1027 |
|
| 1028 |
|
| 1029 |
function TViewerControl.TransformCustom(var APosition: PtrInt; ALimit: PtrInt; |
| 1030 |
AWithAdditionalData: boolean): AnsiString; |
| 1031 |
var |
| 1032 |
sAscii: string = ''; |
| 1033 |
sRez : string = ''; |
| 1034 |
tPos : integer; |
| 1035 |
begin |
| 1036 |
|
| 1037 |
tPos:=APosition; |
| 1038 |
sRez:=TransformCustomBlock(APosition,FCustom.ValuesPerLine,True,True,sAscii); |
| 1039 |
|
| 1040 |
// Result := LineFormat(sRez, sStr, aStartOffset) else |
| 1041 |
|
| 1042 |
if AWithAdditionalData then |
| 1043 |
begin |
| 1044 |
sRez := Format('%s: %s', [IntToHex(tPos, FCustom.MaxAddrDigits), sRez]); |
| 1045 |
|
| 1046 |
if Length(sRez) < FCustom.ValuesPerLine * (FCustom.SpaceCount+FCustom.MaxValueDigits) then |
| 1047 |
sRez := sRez + StringOfChar(' ', FCustom.ValuesPerLine * (FCustom.SpaceCount+FCustom.MaxValueDigits) - Length(sRez)); |
| 1048 |
sRez := sRez + ' '; |
| 1049 |
sRez := sRez + sAscii; |
| 1050 |
end; |
| 1051 |
|
| 1052 |
Result:=sRez; |
| 1053 |
end; |
| 1054 |
|
| 1055 |
function TViewerControl.TransformCustomBlock(var APosition: PtrInt; DataLength: integer ; ASeparatorsOn, AAlignData:boolean; out AChars:AnsiString): AnsiString; |
| 1056 |
var |
| 1057 |
c: AnsiChar; |
| 1058 |
i: Integer; |
| 1059 |
iSep,Len :integer; |
| 1060 |
sStr: string = ''; |
| 1061 |
sRez: string = ''; |
| 1062 |
sEmpty:string; |
| 1063 |
aStartOffset: PtrInt; |
| 1064 |
begin |
| 1065 |
if (APosition+DataLength)>FHighLimit then Len:=FHighLimit-APosition else |
| 1066 |
Len:=DataLength; |
| 1067 |
|
| 1068 |
iSep:=1; // counter for set separator |
| 1069 |
aStartOffset := APosition; |
| 1070 |
for i := 0 to Len - 1 do |
| 1071 |
begin |
| 1072 |
c := PAnsiChar(GetDataAdr)[aPosition]; |
| 1073 |
if c < ' ' then |
| 1074 |
AChars := AChars + '.' |
| 1075 |
else if c > #127 then |
| 1076 |
AChars := AChars + '.' |
| 1077 |
else |
| 1078 |
AChars := AChars + c; |
| 1079 |
|
| 1080 |
sRez := sRez + FCustom.ChrToValueProc(c, FCustom.MaxValueDigits); |
| 1081 |
if ( iSep = FCustom.CountSeperate) and ASeparatorsOn and |
| 1082 |
( i < (FCustom.ValuesPerLine - 1))then |
| 1083 |
begin |
| 1084 |
sRez := sRez + FCustom.SeparatorChar; |
| 1085 |
iSep:=0; |
| 1086 |
end else |
| 1087 |
begin |
| 1088 |
sRez := sRez + FCustom.SeparatorSpace; |
| 1089 |
end; |
| 1090 |
|
| 1091 |
Inc(aPosition); |
| 1092 |
inc(iSep); |
| 1093 |
end; |
| 1094 |
|
| 1095 |
if AAlignData then |
| 1096 |
begin |
| 1097 |
setlength(sEmpty,FCustom.MaxValueDigits); |
| 1098 |
FillChar(sEmpty[1],FCustom.MaxValueDigits,chr(VK_SPACE)); |
| 1099 |
|
| 1100 |
while (i<FCustom.ValuesPerLine-1) do |
| 1101 |
begin |
| 1102 |
sRez := sRez + sEmpty + FCustom.SeparatorSpace; |
| 1103 |
inc(i); |
| 1104 |
end; |
| 1105 |
|
| 1106 |
setlength(sEmpty,0); |
| 1107 |
end; |
| 1108 |
|
| 1109 |
Result:=sRez; |
| 1110 |
end; |
| 1111 |
|
| 1112 |
|
| 1113 |
function TViewerControl.DecToValueProc(AChar:AnsiChar;AMaxDigitsCount:integer):AnsiString; |
| 1114 |
begin |
| 1115 |
Result:= IntToStr(Ord(AChar)); |
| 1116 |
while Length(Result) < AMaxDigitsCount do |
| 1117 |
Result:= '0' + Result; |
| 1118 |
end; |
| 1119 |
|
| 1120 |
function TViewerControl.HexToValueProc(AChar:AnsiChar;AMaxDigitsCount:integer):AnsiString; |
| 1121 |
begin |
| 1122 |
Result:=IntToHex(Ord(AChar), AMaxDigitsCount); |
| 1123 |
while length(Result)<AMaxDigitsCount do |
| 1124 |
Result:=' '+Result; |
| 1125 |
end; |
| 1126 |
|
| 1127 |
function TViewerControl.GetStartOfLine(aPosition: PtrInt): PtrInt; |
| 1128 |
|
| 1129 |
function GetStartOfLineText: PtrInt; |
| 1130 |
var |
| 1131 |
tmpPos, LineStartPos: PtrInt; |
| 1132 |
DataLength: PtrInt; |
| 1133 |
prevChar: Cardinal; |
| 1134 |
MaxLineLength: Boolean; |
| 1135 |
CharLenInBytes: Integer; |
| 1136 |
begin |
| 1137 |
prevChar := GetPrevCharAsAscii(aPosition, CharLenInBytes); |
| 1138 |
if CharLenInBytes = 0 then |
| 1139 |
Exit(aPosition); |
| 1140 |
|
| 1141 |
// Check if this already is not a start of line (if previous char is #10). |
| 1142 |
if prevChar = 10 then |
| 1143 |
Exit(aPosition); |
| 1144 |
|
| 1145 |
tmpPos := aPosition - CharLenInBytes; |
| 1146 |
|
| 1147 |
if tmpPos <= FLowLimit then |
| 1148 |
Exit(FLowLimit); |
| 1149 |
|
| 1150 |
// Check if we're not in the middle of line ending |
| 1151 |
// (previous char is #13, current char is #10). |
| 1152 |
if (prevChar = 13) and |
| 1153 |
(GetNextCharAsAscii(aPosition, CharLenInBytes) = 10) then |
| 1154 |
begin |
| 1155 |
prevChar := GetPrevCharAsAscii(tmpPos, CharLenInBytes); |
| 1156 |
if CharLenInBytes = 0 then |
| 1157 |
Exit(aPosition); |
| 1158 |
Dec(tmpPos, CharLenInBytes); |
| 1159 |
end; |
| 1160 |
|
| 1161 |
if tmpPos <= FLowLimit then |
| 1162 |
Exit(FLowLimit); |
| 1163 |
|
| 1164 |
DataLength:= 0; |
| 1165 |
// Search for real start of line. |
| 1166 |
while (not (prevChar in [10, 13])) and (tmpPos > FLowLimit) do |
| 1167 |
begin |
| 1168 |
prevChar := GetPrevCharAsAscii(tmpPos, CharLenInBytes); |
| 1169 |
if CharLenInBytes = 0 then |
| 1170 |
Break; |
| 1171 |
Dec(tmpPos, CharLenInBytes); |
| 1172 |
|
| 1173 |
case prevChar of |
| 1174 |
9: |
| 1175 |
Inc(DataLength, FTabSpaces - DataLength mod FTabSpaces); |
| 1176 |
else |
| 1177 |
Inc(DataLength, 1); |
| 1178 |
end; |
| 1179 |
|
| 1180 |
case FViewerControlMode of |
| 1181 |
vcmText: MaxLineLength := DataLength < FMaxTextWidth; |
| 1182 |
vcmWrap: MaxLineLength := DataLength < FTextWidth; |
| 1183 |
end; |
| 1184 |
if not MaxLineLength then Exit(tmpPos); |
| 1185 |
end; |
| 1186 |
|
| 1187 |
// Previous end of line not found and there are no more data to check. |
| 1188 |
if (not (prevChar in [10, 13])) and (tmpPos <= FLowLimit) then |
| 1189 |
Exit(FLowLimit); |
| 1190 |
|
| 1191 |
// Move forward to first non-line ending character. |
| 1192 |
Inc(tmpPos, CharLenInBytes); |
| 1193 |
|
| 1194 |
// Search for start of real line or wrapped line. |
| 1195 |
while True do |
| 1196 |
begin |
| 1197 |
LineStartPos := tmpPos; |
| 1198 |
CalcTextLineLength(tmpPos, FHighLimit, DataLength); |
| 1199 |
|
| 1200 |
if tmpPos = aPosition then |
| 1201 |
begin |
| 1202 |
if aPosition < FHighLimit then |
| 1203 |
Exit(aPosition) // aPosition is already at start of a line |
| 1204 |
else |
| 1205 |
Exit(LineStartPos); // aPosition points to end of file so return start of this line |
| 1206 |
end |
| 1207 |
else if tmpPos > aPosition then |
| 1208 |
Exit(LineStartPos); // Found start of line |
| 1209 |
end; |
| 1210 |
end; |
| 1211 |
|
| 1212 |
function GetStartOfLineFixed(aFixedWidth: Integer): PtrInt; |
| 1213 |
begin |
| 1214 |
Result := aPosition - (aPosition mod aFixedWidth); |
| 1215 |
end; |
| 1216 |
|
| 1217 |
var |
| 1218 |
i: Integer; |
| 1219 |
begin |
| 1220 |
if aPosition <= FLowLimit then |
| 1221 |
Exit(FLowLimit) |
| 1222 |
else if aPosition >= FHighLimit then |
| 1223 |
aPosition := FHighLimit; // search from the end of the file |
| 1224 |
|
| 1225 |
// Speedup for currently displayed positions. |
| 1226 |
if (FLineList.Count > 0) and |
| 1227 |
(aPosition >= FLineList.Items[0]) and |
| 1228 |
(aPosition <= FLineList.Items[FLineList.Count - 1]) then |
| 1229 |
begin |
| 1230 |
for i := FLineList.Count - 1 downto 0 do |
| 1231 |
if FLineList.Items[i] <= aPosition then |
| 1232 |
Exit(FLineList.Items[i]); |
| 1233 |
end; |
| 1234 |
|
| 1235 |
case FViewerControlMode of |
| 1236 |
vcmBin: |
| 1237 |
Result := GetStartOfLineFixed(cBinWidth); |
| 1238 |
vcmHex, vcmDec: |
| 1239 |
Result := GetStartOfLineFixed(FCustom.ValuesPerLine); |
| 1240 |
|
| 1241 |
vcmText, vcmWrap, vcmBook: |
| 1242 |
Result := GetStartOfLineText; |
| 1243 |
else |
| 1244 |
Result := aPosition; |
| 1245 |
end; |
| 1246 |
end; |
| 1247 |
|
| 1248 |
function TViewerControl.GetEndOfLine(aPosition: PtrInt): PtrInt; |
| 1249 |
|
| 1250 |
function GetEndOfLineText: PtrInt; |
| 1251 |
var |
| 1252 |
tmpPos: PtrInt; |
| 1253 |
DataLength: PtrInt; |
| 1254 |
begin |
| 1255 |
Result := GetStartOfLine(aPosition); |
| 1256 |
tmpPos := Result; |
| 1257 |
CalcTextLineLength(tmpPos, FHighLimit, DataLength); |
| 1258 |
Result := Result + DataLength; |
| 1259 |
if Result < aPosition then |
| 1260 |
Result := aPosition; |
| 1261 |
end; |
| 1262 |
|
| 1263 |
function GetEndOfLineFixed(aFixedWidth: Integer): PtrInt; |
| 1264 |
begin |
| 1265 |
Result := aPosition - (aPosition mod aFixedWidth) + aFixedWidth; |
| 1266 |
end; |
| 1267 |
|
| 1268 |
begin |
| 1269 |
case FViewerControlMode of |
| 1270 |
vcmBin: |
| 1271 |
Result := GetEndOfLineFixed(cBinWidth); |
| 1272 |
vcmHex,vcmDec: |
| 1273 |
Result := GetEndOfLineFixed(FCustom.ValuesPerLine); |
| 1274 |
vcmText, vcmWrap, vcmBook: |
| 1275 |
Result := GetEndOfLineText; |
| 1276 |
else |
| 1277 |
Result := aPosition; |
| 1278 |
end; |
| 1279 |
end; |
| 1280 |
|
| 1281 |
function TViewerControl.GetStartOfPrevLine(aPosition: PtrInt): PtrInt; |
| 1282 |
|
| 1283 |
function GetPrevLineText: PtrInt; |
| 1284 |
var |
| 1285 |
tmpPos, LineStartPos: PtrInt; |
| 1286 |
DataLength: PtrInt; |
| 1287 |
prevChar: Cardinal; |
| 1288 |
MaxLineLength: Boolean; |
| 1289 |
CharLenInBytes: Integer; |
| 1290 |
begin |
| 1291 |
prevChar := GetPrevCharAsAscii(aPosition, CharLenInBytes); |
| 1292 |
if CharLenInBytes = 0 then |
| 1293 |
Exit(aPosition); |
| 1294 |
|
| 1295 |
tmpPos := aPosition - CharLenInBytes; // start search from previous character |
| 1296 |
|
| 1297 |
if tmpPos <= FLowLimit then |
| 1298 |
Exit(FLowLimit); |
| 1299 |
|
| 1300 |
// Check if we're not in the middle of line ending |
| 1301 |
// (previous char is #13, current char is #10). |
| 1302 |
if (prevChar = 13) and |
| 1303 |
(GetNextCharAsAscii(aPosition, CharLenInBytes) = 10) then |
| 1304 |
begin |
| 1305 |
prevChar := GetPrevCharAsAscii(tmpPos, CharLenInBytes); |
| 1306 |
if CharLenInBytes = 0 then |
| 1307 |
Exit(aPosition); |
| 1308 |
Dec(tmpPos, CharLenInBytes); |
| 1309 |
end |
| 1310 |
else |
| 1311 |
begin |
| 1312 |
// Bypass possible end of previous line. |
| 1313 |
if prevChar = 10 then |
| 1314 |
begin |
| 1315 |
prevChar := GetPrevCharAsAscii(tmpPos, CharLenInBytes); |
| 1316 |
if CharLenInBytes = 0 then |
| 1317 |
Exit(aPosition); |
| 1318 |
Dec(tmpPos, CharLenInBytes); |
| 1319 |
end; |
| 1320 |
if prevChar = 13 then |
| 1321 |
begin |
| 1322 |
prevChar := GetPrevCharAsAscii(tmpPos, CharLenInBytes); |
| 1323 |
if CharLenInBytes = 0 then |
| 1324 |
Exit(aPosition); |
| 1325 |
Dec(tmpPos, CharLenInBytes); |
| 1326 |
end; |
| 1327 |
end; |
| 1328 |
|
| 1329 |
if tmpPos <= FLowLimit then |
| 1330 |
Exit(FLowLimit); |
| 1331 |
|
| 1332 |
DataLength:= 0; |
| 1333 |
// Search for real start of line. |
| 1334 |
while (not (prevChar in [10, 13])) and (tmpPos > FLowLimit) do |
| 1335 |
begin |
| 1336 |
prevChar := GetPrevCharAsAscii(tmpPos, CharLenInBytes); |
| 1337 |
if CharLenInBytes = 0 then |
| 1338 |
Break; |
| 1339 |
Dec(tmpPos, CharLenInBytes); |
| 1340 |
|
| 1341 |
case prevChar of |
| 1342 |
9: |
| 1343 |
Inc(DataLength, FTabSpaces - DataLength mod FTabSpaces); |
| 1344 |
else |
| 1345 |
Inc(DataLength, 1); |
| 1346 |
end; |
| 1347 |
|
| 1348 |
case FViewerControlMode of |
| 1349 |
vcmText: MaxLineLength := DataLength < FMaxTextWidth; |
| 1350 |
vcmWrap: MaxLineLength := DataLength < FTextWidth; |
| 1351 |
end; |
| 1352 |
if not MaxLineLength then Exit(tmpPos); |
| 1353 |
end; |
| 1354 |
|
| 1355 |
// Move forward to first non-line ending character. |
| 1356 |
Inc(tmpPos, CharLenInBytes); |
| 1357 |
|
| 1358 |
// Search for start of real line or wrapped line. |
| 1359 |
while True do |
| 1360 |
begin |
| 1361 |
LineStartPos := tmpPos; |
| 1362 |
CalcTextLineLength(tmpPos, aPosition, DataLength); |
| 1363 |
|
| 1364 |
if tmpPos >= aPosition then |
| 1365 |
Exit(LineStartPos); // Found start of line |
| 1366 |
end; |
| 1367 |
end; |
| 1368 |
|
| 1369 |
function GetPrevLineFixed(aFixedWidth: Integer): PtrInt; |
| 1370 |
begin |
| 1371 |
Result := aPosition - (aPosition mod aFixedWidth); |
| 1372 |
if Result >= aFixedWidth then |
| 1373 |
Result := Result - aFixedWidth; |
| 1374 |
end; |
| 1375 |
|
| 1376 |
var |
| 1377 |
i: Integer; |
| 1378 |
begin |
| 1379 |
if aPosition <= FLowLimit then |
| 1380 |
Exit(FLowLimit) |
| 1381 |
else if aPosition >= FHighLimit then |
| 1382 |
aPosition := FHighLimit; // search from the end of the file |
| 1383 |
|
| 1384 |
// Speedup for currently displayed positions. |
| 1385 |
if (FLineList.Count > 0) and |
| 1386 |
(aPosition >= FLineList.Items[0]) and |
| 1387 |
(aPosition <= FLineList.Items[FLineList.Count - 1]) then |
| 1388 |
begin |
| 1389 |
for i := FLineList.Count - 1 downto 0 do |
| 1390 |
if FLineList.Items[i] < aPosition then |
| 1391 |
Exit(FLineList.Items[i]); |
| 1392 |
end; |
| 1393 |
|
| 1394 |
case FViewerControlMode of |
| 1395 |
vcmBin: |
| 1396 |
Result := GetPrevLineFixed(cBinWidth); |
| 1397 |
vcmHex,vcmDec: |
| 1398 |
Result := GetPrevLineFixed(FCustom.ValuesPerLine); |
| 1399 |
vcmText, vcmWrap, vcmBook: |
| 1400 |
Result := GetPrevLineText; |
| 1401 |
else |
| 1402 |
Result := aPosition; |
| 1403 |
end; |
| 1404 |
end; |
| 1405 |
|
| 1406 |
function TViewerControl.GetStartOfNextLine(aPosition: PtrInt): PtrInt; |
| 1407 |
|
| 1408 |
function GetNextLineText: PtrInt; |
| 1409 |
var |
| 1410 |
tmpPos: PtrInt; |
| 1411 |
DataLength: PtrInt; |
| 1412 |
prevChar: Cardinal; |
| 1413 |
CharLenInBytes: Integer; |
| 1414 |
begin |
| 1415 |
tmpPos := aPosition; |
| 1416 |
|
| 1417 |
// This might not be a real start of line (it may be start of wrapped line). |
| 1418 |
// Search for start of line. |
| 1419 |
while (tmpPos > FLowLimit) do |
| 1420 |
begin |
| 1421 |
prevChar := GetPrevCharAsAscii(tmpPos, CharLenInBytes); |
| 1422 |
if CharLenInBytes = 0 then |
| 1423 |
Break; |
| 1424 |
if (prevChar in [10, 13]) then |
| 1425 |
Break |
| 1426 |
else |
| 1427 |
Dec(tmpPos, CharLenInBytes); |
| 1428 |
end; |
| 1429 |
|
| 1430 |
// Now we know we are at the start of a line, search the start of next line. |
| 1431 |
while True do |
| 1432 |
begin |
| 1433 |
CalcTextLineLength(tmpPos, FHighLimit, DataLength); |
| 1434 |
|
| 1435 |
if tmpPos >= aPosition then |
| 1436 |
Exit(tmpPos); // Found start of line |
| 1437 |
end; |
| 1438 |
end; |
| 1439 |
|
| 1440 |
function GetNextLineFixed(aFixedWidth: Integer): PtrInt; |
| 1441 |
begin |
| 1442 |
Result := aPosition - (aPosition mod aFixedWidth); |
| 1443 |
if Result + aFixedWidth < FHighLimit then |
| 1444 |
Result := Result + aFixedWidth; |
| 1445 |
end; |
| 1446 |
|
| 1447 |
var |
| 1448 |
i: Integer; |
| 1449 |
begin |
| 1450 |
if aPosition < FLowLimit then |
| 1451 |
aPosition := FLowLimit // search from the start of the file |
| 1452 |
else if aPosition >= FHighLimit then |
| 1453 |
aPosition := FHighLimit; // search from the end of the file |
| 1454 |
|
| 1455 |
// Speedup for currently displayed positions. |
| 1456 |
if (FLineList.Count > 0) and |
| 1457 |
(aPosition >= FLineList.Items[0]) and |
| 1458 |
(aPosition <= FLineList.Items[FLineList.Count - 1]) then |
| 1459 |
begin |
| 1460 |
for i := 0 to FLineList.Count - 1 do |
| 1461 |
if FLineList.Items[i] > aPosition then |
| 1462 |
Exit(FLineList.Items[i]); |
| 1463 |
end; |
| 1464 |
|
| 1465 |
case FViewerControlMode of |
| 1466 |
vcmBin: |
| 1467 |
Result := GetNextLineFixed(cBinWidth); |
| 1468 |
vcmHex,vcmDec: |
| 1469 |
Result := GetNextLineFixed(FCustom.ValuesPerLine); |
| 1470 |
vcmText, vcmWrap, vcmBook: |
| 1471 |
Result := GetNextLineText; |
| 1472 |
else |
| 1473 |
Result := aPosition; |
| 1474 |
end; |
| 1475 |
end; |
| 1476 |
|
| 1477 |
procedure TViewerControl.PageUp; |
| 1478 |
var |
| 1479 |
H: Integer; |
| 1480 |
begin |
| 1481 |
H := GetClientHeightInLines * FColCount - 1; |
| 1482 |
if H <= 0 then |
| 1483 |
H := 1; |
| 1484 |
Scroll(-H); |
| 1485 |
end; |
| 1486 |
|
| 1487 |
procedure TViewerControl.HPageUp; |
| 1488 |
var |
| 1489 |
H: Integer; |
| 1490 |
begin |
| 1491 |
H := FHPosition - FTextWidth; |
| 1492 |
if H <= 0 then |
| 1493 |
H := FHPosition else H:= FTextWidth; |
| 1494 |
HScroll(-H); |
| 1495 |
end; |
| 1496 |
|
| 1497 |
procedure TViewerControl.PageDown; |
| 1498 |
var |
| 1499 |
H: Integer; |
| 1500 |
begin |
| 1501 |
H := GetClientHeightInLines * FColCount - 1; |
| 1502 |
if H <= 0 then |
| 1503 |
H := 1; |
| 1504 |
Scroll(H); |
| 1505 |
end; |
| 1506 |
|
| 1507 |
procedure TViewerControl.HPageDown; |
| 1508 |
var |
| 1509 |
H: Integer; |
| 1510 |
begin |
| 1511 |
H := FHLowEnd - FHPosition; |
| 1512 |
if H > FTextWidth then H := FTextWidth ; |
| 1513 |
HScroll(H); |
| 1514 |
end; |
| 1515 |
|
| 1516 |
procedure TViewerControl.GoHome; |
| 1517 |
begin |
| 1518 |
Position := FLowLimit; |
| 1519 |
end; |
| 1520 |
|
| 1521 |
procedure TViewerControl.GoEnd; |
| 1522 |
begin |
| 1523 |
Position := FHighLimit; |
| 1524 |
end; |
| 1525 |
|
| 1526 |
procedure TViewerControl.HGoHome; |
| 1527 |
begin |
| 1528 |
HScroll (-FHPosition); |
| 1529 |
end; |
| 1530 |
|
| 1531 |
procedure TViewerControl.HGoEnd; |
| 1532 |
begin |
| 1533 |
HScroll (FHLowEnd-FHPosition); |
| 1534 |
end; |
| 1535 |
|
| 1536 |
procedure TViewerControl.CaretGoHome; |
| 1537 |
begin |
| 1538 |
HScroll (-FHPosition); |
| 1539 |
CaretPos := GetStartOfLine(CaretPos); |
| 1540 |
end; |
| 1541 |
|
| 1542 |
procedure TViewerControl.CaretGoEnd; |
| 1543 |
begin |
| 1544 |
if FViewerControlMode in [vcmBin, vcmHex, vcmDec] then |
| 1545 |
CaretPos := GetEndOfLine(CaretPos) - 1 |
| 1546 |
else begin |
| 1547 |
CaretPos := GetEndOfLine(CaretPos); |
| 1548 |
end; |
| 1549 |
|
| 1550 |
if FViewerControlMode = vcmText then |
| 1551 |
begin |
| 1552 |
if not IsVisible(CaretPos) then |
| 1553 |
begin |
| 1554 |
if (FVisibleOffset < FHPosition) or |
| 1555 |
(FVisibleOffset > FHPosition + FTextWidth) then |
| 1556 |
begin |
| 1557 |
SetHPosition(FVisibleOffset); |
| 1558 |
HScroll(-1); |
| 1559 |
end; |
| 1560 |
end; |
| 1561 |
end; |
| 1562 |
end; |
| 1563 |
|
| 1564 |
procedure TViewerControl.SetFileName(const sFileName: String); |
| 1565 |
begin |
| 1566 |
if not (csDesigning in ComponentState) then |
| 1567 |
begin |
| 1568 |
UnMapFile; |
| 1569 |
|
| 1570 |
if sFileName <> '' then |
| 1571 |
begin |
| 1572 |
if MapFile(sFileName) then |
| 1573 |
begin |
| 1574 |
FFileName := sFileName; |
| 1575 |
|
| 1576 |
// Detect encoding if needed. |
| 1577 |
if FEncoding = veAutoDetect then |
| 1578 |
FEncoding := DetectEncoding; |
| 1579 |
|
| 1580 |
ReReadFile; |
| 1581 |
|
| 1582 |
CaretPos := FLowLimit; |
| 1583 |
end; |
| 1584 |
end; |
| 1585 |
end |
| 1586 |
else |
| 1587 |
FFileName := sFileName; |
| 1588 |
end; |
| 1589 |
|
| 1590 |
function TViewerControl.MapFile(const sFileName: String): Boolean; |
| 1591 |
|
| 1592 |
function ReadFile: Boolean; inline; |
| 1593 |
begin |
| 1594 |
FMappedFile := GetMem(FFileSize); |
| 1595 |
Result := (FileRead(FFileHandle, FMappedFile^, FFileSize) = FFileSize); |
| 1596 |
if not Result then |
| 1597 |
begin |
| 1598 |
FLastError := mbSysErrorMessage; |
| 1599 |
FreeMemAndNil(FMappedFile); |
| 1600 |
end; |
| 1601 |
FileClose(FFileHandle); |
| 1602 |
FFileHandle := 0; |
| 1603 |
end; |
| 1604 |
|
| 1605 |
{$IFDEF LINUX} |
| 1606 |
var |
| 1607 |
Sbfs: TStatFS; |
| 1608 |
{$ENDIF} |
| 1609 |
begin |
| 1610 |
Result := False; |
| 1611 |
FLastError := EmptyStr; |
| 1612 |
|
| 1613 |
if Assigned(FMappedFile) then |
| 1614 |
UnMapFile; // if needed |
| 1615 |
|
| 1616 |
if Assigned(FOnFileOpen) then |
| 1617 |
FFileHandle := FOnFileOpen(sFileName, fmOpenRead or fmShareDenyNone) |
| 1618 |
else begin |
| 1619 |
FFileHandle := mbFileOpen(sFileName, fmOpenRead or fmShareDenyNone); |
| 1620 |
end; |
| 1621 |
|
| 1622 |
if FFileHandle = feInvalidHandle then |
| 1623 |
begin |
| 1624 |
FLastError := mbSysErrorMessage; |
| 1625 |
FFileHandle := 0; |
| 1626 |
Exit; |
| 1627 |
end; |
| 1628 |
|
| 1629 |
FFileSize := FileGetSize(FFileHandle); |
| 1630 |
|
| 1631 |
if (FFileSize < 0) then |
| 1632 |
begin |
| 1633 |
FLastError := mbSysErrorMessage; |
| 1634 |
FileClose(FFileHandle); |
| 1635 |
FFileHandle := 0; |
| 1636 |
Exit; |
| 1637 |
end; |
| 1638 |
|
| 1639 |
{$IFDEF LINUX} |
| 1640 |
if (fpFStatFS(FFileHandle, @Sbfs) = 0) then |
| 1641 |
begin |
| 1642 |
// Special case for PROC_FS and SYS_FS |
| 1643 |
if (sbfs.fstype = PROC_SUPER_MAGIC) or (sbfs.fstype = SYSFS_MAGIC) then |
| 1644 |
begin |
| 1645 |
FMappedFile := GetMem(MaxMemSize - 1); |
| 1646 |
FFileSize := FileRead(FFileHandle, FMappedFile^, MaxMemSize - 1); |
| 1647 |
Result := (FFileSize >= 0); |
| 1648 |
if not Result then |
| 1649 |
begin |
| 1650 |
FLastError := mbSysErrorMessage; |
| 1651 |
FreeMemAndNil(FMappedFile); |
| 1652 |
end; |
| 1653 |
FileClose(FFileHandle); |
| 1654 |
FFileHandle := 0; |
| 1655 |
Exit; |
| 1656 |
end; |
| 1657 |
end; |
| 1658 |
{$ENDIF} |
| 1659 |
|
| 1660 |
if (FFileSize < MaxMemSize) then |
| 1661 |
begin |
| 1662 |
Result := ReadFile; |
| 1663 |
Exit; |
| 1664 |
end; |
| 1665 |
|
| 1666 |
{$IFDEF MSWINDOWS} |
| 1667 |
FMappingHandle := CreateFileMapping(FFileHandle, nil, PAGE_READONLY, 0, 0, nil); |
| 1668 |
if FMappingHandle = 0 then |
| 1669 |
begin |
| 1670 |
FLastError := mbSysErrorMessage; |
| 1671 |
FMappedFile := nil; |
| 1672 |
UnMapFile; |
| 1673 |
end |
| 1674 |
else begin |
| 1675 |
FMappedFile := MapViewOfFile(FMappingHandle, FILE_MAP_READ, 0, 0, 0); |
| 1676 |
if (FMappedFile = nil) then |
| 1677 |
begin |
| 1678 |
FLastError := mbSysErrorMessage; |
| 1679 |
UnMapFile; |
| 1680 |
end; |
| 1681 |
end; |
| 1682 |
{$ELSE} |
| 1683 |
FMappedFile := fpmmap(nil, FFileSize, PROT_READ, MAP_PRIVATE{SHARED}, FFileHandle, 0); |
| 1684 |
if FMappedFile = MAP_FAILED then |
| 1685 |
begin |
| 1686 |
FLastError := mbSysErrorMessage; |
| 1687 |
FMappedFile:= nil; |
| 1688 |
FileClose(FFileHandle); |
| 1689 |
FFileHandle := 0; |
| 1690 |
Exit; |
| 1691 |
end; |
| 1692 |
{$ENDIF} |
| 1693 |
Result := Assigned(FMappedFile); |
| 1694 |
end; |
| 1695 |
|
| 1696 |
procedure TViewerControl.UnMapFile; |
| 1697 |
begin |
| 1698 |
if FMappedFile = Pointer(FText) then |
| 1699 |
begin |
| 1700 |
FMappedFile:= nil; |
| 1701 |
FText:= EmptyStr; |
| 1702 |
end; |
| 1703 |
|
| 1704 |
if (FFileSize < MaxMemSize) then |
| 1705 |
begin |
| 1706 |
if Assigned(FMappedFile) then |
| 1707 |
begin |
| 1708 |
FreeMem(FMappedFile); |
| 1709 |
FMappedFile := nil; |
| 1710 |
end; |
| 1711 |
end; |
| 1712 |
|
| 1713 |
{$IFDEF MSWINDOWS} |
| 1714 |
|
| 1715 |
if Assigned(FMappedFile) then |
| 1716 |
begin |
| 1717 |
UnmapViewOfFile(FMappedFile); |
| 1718 |
FMappedFile := nil; |
| 1719 |
end; |
| 1720 |
|
| 1721 |
if FMappingHandle <> 0 then |
| 1722 |
begin |
| 1723 |
CloseHandle(FMappingHandle); |
| 1724 |
FMappingHandle := 0; |
| 1725 |
end; |
| 1726 |
|
| 1727 |
{$ELSE} |
| 1728 |
|
| 1729 |
if Assigned(FMappedFile) then |
| 1730 |
begin |
| 1731 |
if fpmunmap(FMappedFile, FFileSize) = -1 then |
| 1732 |
DebugLn('Error unmapping file: ', SysErrorMessage(fpgeterrno)); |
| 1733 |
FMappedFile := nil; |
| 1734 |
end; |
| 1735 |
|
| 1736 |
{$ENDIF} |
| 1737 |
|
| 1738 |
if FFileHandle <> 0 then |
| 1739 |
begin |
| 1740 |
FileClose(FFileHandle); |
| 1741 |
FFileHandle := 0; |
| 1742 |
end; |
| 1743 |
|
| 1744 |
FFileName := ''; |
| 1745 |
FFileSize := 0; |
| 1746 |
Position := 0; |
| 1747 |
FLowLimit := 0; |
| 1748 |
FHighLimit := 0; |
| 1749 |
FBOMLength := 0; |
| 1750 |
FBlockBeg := 0; |
| 1751 |
FBlockEnd := 0; |
| 1752 |
end; |
| 1753 |
|
| 1754 |
procedure TViewerControl.WriteText; |
| 1755 |
var |
| 1756 |
yIndex, xIndex, w, i: Integer; |
| 1757 |
LineStart, iPos: PtrInt; |
| 1758 |
CharLenInBytes: Integer; |
| 1759 |
DataLength: PtrInt; |
| 1760 |
sText: String; |
| 1761 |
|
| 1762 |
procedure DrawCaret(X, Y: Integer; LinePos: PtrInt); |
| 1763 |
begin |
| 1764 |
if FShowCaret and (FCaretPos = LinePos) then |
| 1765 |
begin |
| 1766 |
FCaretPoint.X:= X; |
| 1767 |
FCaretPoint.Y:= Y; |
| 1768 |
end; |
| 1769 |
end; |
| 1770 |
|
| 1771 |
begin |
| 1772 |
iPos := FPosition; |
| 1773 |
if Mode = vcmBook then |
| 1774 |
w := Width div FColCount |
| 1775 |
else begin |
| 1776 |
w := 0; |
| 1777 |
end; |
| 1778 |
|
| 1779 |
for xIndex := 0 to FColCount-1 do |
| 1780 |
begin |
| 1781 |
for yIndex := 0 to GetClientHeightInLines(False) - 1 do |
| 1782 |
begin |
| 1783 |
if iPos > FHighLimit then |
| 1784 |
Break; |
| 1785 |
|
| 1786 |
if iPos = FHighLimit then |
| 1787 |
begin |
| 1788 |
if GetPrevCharAsAscii(iPos, CharLenInBytes) = 10 then |
| 1789 |
begin |
| 1790 |
DrawCaret(0, yIndex * FTextHeight, iPos); |
| 1791 |
end; |
| 1792 |
Break; |
| 1793 |
end; |
| 1794 |
|
| 1795 |
AddLineOffset(iPos); |
| 1796 |
LineStart := iPos; |
| 1797 |
|
| 1798 |
i := CalcTextLineLength(iPos, FHighLimit, DataLength); |
| 1799 |
|
| 1800 |
if i > FHLowEnd then FHLowEnd:= i; |
| 1801 |
|
| 1802 |
if DataLength = 0 then |
| 1803 |
DrawCaret(0, yIndex * FTextHeight, LineStart) |
| 1804 |
else begin |
| 1805 |
if (Mode = vcmText) and (FHPosition > 0) then |
| 1806 |
begin |
| 1807 |
for i:= 1 to FHPosition do |
| 1808 |
begin |
| 1809 |
GetNextCharAsAscii(LineStart, CharLenInBytes); |
| 1810 |
DataLength -= CharLenInBytes; |
| 1811 |
LineStart += CharLenInBytes; |
| 1812 |
end; |
| 1813 |
if (DataLength <= 0) then Continue; |
| 1814 |
end; |
| 1815 |
sText := GetText(LineStart, DataLength, 0); |
| 1816 |
|
| 1817 |
OutText(FLeftMargin + xIndex * w, yIndex * FTextHeight, sText, LineStart, DataLength); |
| 1818 |
end; |
| 1819 |
end; |
| 1820 |
end; |
| 1821 |
end; |
| 1822 |
|
| 1823 |
procedure TViewerControl.WriteCustom; |
| 1824 |
// this method render visible page of text |
| 1825 |
var |
| 1826 |
yIndex: Integer; |
| 1827 |
iPos, LineStart: PtrInt; |
| 1828 |
s: string; |
| 1829 |
begin |
| 1830 |
iPos := FPosition; |
| 1831 |
for yIndex := 0 to GetClientHeightInLines(False) - 1 do |
| 1832 |
begin |
| 1833 |
if iPos >= FHighLimit then |
| 1834 |
Break; |
| 1835 |
LineStart := iPos; |
| 1836 |
AddLineOffset(iPos); |
| 1837 |
s := TransformCustom(iPos, FHighLimit); // get line text for render |
| 1838 |
|
| 1839 |
if s <> '' then |
| 1840 |
OutCustom(FLeftMargin, yIndex * FTextHeight, s, LineStart, iPos - LineStart); // render line to canvas |
| 1841 |
end; |
| 1842 |
end; |
| 1843 |
|
| 1844 |
procedure TViewerControl.WriteBin; |
| 1845 |
var |
| 1846 |
yIndex: Integer; |
| 1847 |
iPos, LineStart: PtrInt; |
| 1848 |
s: string; |
| 1849 |
begin |
| 1850 |
iPos := FPosition; |
| 1851 |
for yIndex := 0 to GetClientHeightInLines(False) - 1 do |
| 1852 |
begin |
| 1853 |
if iPos >= FHighLimit then |
| 1854 |
Break; |
| 1855 |
LineStart := iPos; |
| 1856 |
AddLineOffset(iPos); |
| 1857 |
s := TransformBin(iPos, FHighLimit); |
| 1858 |
if s <> '' then |
| 1859 |
OutBin(FLeftMargin, yIndex * FTextHeight, s, LineStart, iPos - LineStart); |
| 1860 |
end; |
| 1861 |
end; |
| 1862 |
|
| 1863 |
function TViewerControl.GetDataAdr: Pointer; |
| 1864 |
begin |
| 1865 |
case FViewerControlMode of |
| 1866 |
vcmText, vcmWrap, vcmBook: |
| 1867 |
Result := FMappedFile + FBOMLength; |
| 1868 |
else |
| 1869 |
Result := FMappedFile; |
| 1870 |
end; |
| 1871 |
end; |
| 1872 |
|
| 1873 |
procedure TViewerControl.SetPosition(Value: PtrInt); |
| 1874 |
begin |
| 1875 |
SetPosition(Value, False); |
| 1876 |
end; |
| 1877 |
|
| 1878 |
procedure TViewerControl.SetHPosition(Value: Integer); |
| 1879 |
begin |
| 1880 |
SetHPosition(Value, False); |
| 1881 |
end; |
| 1882 |
|
| 1883 |
procedure TViewerControl.SetHPosition(Value: Integer; Force: Boolean); |
| 1884 |
begin |
| 1885 |
if not IsFileOpen then |
| 1886 |
Exit; |
| 1887 |
|
| 1888 |
FHPosition := Value; |
| 1889 |
// Set new scroll position. |
| 1890 |
if (FHPosition > 0) and (FHLowEnd - FTextWidth > 0) then |
| 1891 |
FHScrollBarPosition := FHPosition * 100 div (FHLowEnd - FTextWidth) |
| 1892 |
else |
| 1893 |
FHScrollBarPosition := 0; |
| 1894 |
// Update scrollbar position. |
| 1895 |
if FUpdateScrollBarPos then |
| 1896 |
begin |
| 1897 |
if FScrollBarHorz.Position <> FHScrollBarPosition then |
| 1898 |
begin |
| 1899 |
// Workaround for bug: http://bugs.freepascal.org/view.php?id=23815 |
| 1900 |
{$IF DEFINED(LCLQT) and (LCL_FULLVERSION < 1010000)} |
| 1901 |
FScrollBarHorz.OnScroll := nil; |
| 1902 |
FScrollBarHorz.Position := FHScrollBarPosition; |
| 1903 |
Application.ProcessMessages; // Skip message |
| 1904 |
FScrollBarHorz.OnScroll := @ScrollBarHorzScroll; |
| 1905 |
{$ELSE} |
| 1906 |
FScrollBarHorz.Position := FHScrollBarPosition; |
| 1907 |
{$ENDIF} |
| 1908 |
end; |
| 1909 |
end; |
| 1910 |
// else the scrollbar position will be updated in ScrollBarVertScroll |
| 1911 |
Invalidate; |
| 1912 |
end; |
| 1913 |
|
| 1914 |
procedure TViewerControl.SetPosition(Value: PtrInt; Force: Boolean); |
| 1915 |
var |
| 1916 |
LinesTooMany: Integer; |
| 1917 |
LastLineReached: Boolean; |
| 1918 |
begin |
| 1919 |
if not IsFileOpen then |
| 1920 |
Exit; |
| 1921 |
|
| 1922 |
// Double byte text can have only even position |
| 1923 |
if (Encoding in ViewerEncodingDoubleByte) and Odd(Value) then |
| 1924 |
begin |
| 1925 |
Value := Value - 1; |
| 1926 |
end; |
| 1927 |
|
| 1928 |
// Speedup if total nr of lines is less then nr of lines that can be displayed. |
| 1929 |
if (FPosition = FLowLimit) and // only if already at the top |
| 1930 |
(FLineList.Count > 0) and (FLineList.Count < GetClientHeightInLines) |
| 1931 |
then |
| 1932 |
Value := FLowLimit |
| 1933 |
else |
| 1934 |
// Boundary checks are done in GetStartOfLine. |
| 1935 |
Value := GetStartOfLine(Value); |
| 1936 |
|
| 1937 |
if (Value <> FPosition) or Force then |
| 1938 |
begin |
| 1939 |
// Don't allow empty lines at the bottom of the control. |
| 1940 |
LinesTooMany := GetClientHeightInLines - GetLinesTillEnd(Value, LastLineReached); |
| 1941 |
if LinesTooMany > 0 then |
| 1942 |
begin |
| 1943 |
// scroll back upwards |
| 1944 |
ScrollPosition(Value, -LinesTooMany); |
| 1945 |
end; |
| 1946 |
|
| 1947 |
FPosition := Value; |
| 1948 |
if Assigned(FOnPositionChanged) then |
| 1949 |
FOnPositionChanged(Self); |
| 1950 |
Invalidate; |
| 1951 |
|
| 1952 |
// Set new scroll position. |
| 1953 |
if LastLineReached and (Value > 0) then |
| 1954 |
FScrollBarPosition := 100 |
| 1955 |
else |
| 1956 |
FScrollBarPosition := Percent; |
| 1957 |
end; |
| 1958 |
|
| 1959 |
// Update scrollbar position. |
| 1960 |
if FUpdateScrollBarPos then |
| 1961 |
begin |
| 1962 |
if FScrollBarVert.Position <> FScrollBarPosition then |
| 1963 |
begin |
| 1964 |
// Workaround for bug: http://bugs.freepascal.org/view.php?id=23815 |
| 1965 |
{$IF DEFINED(LCLQT) and (LCL_FULLVERSION < 1010000)} |
| 1966 |
FScrollBarVert.OnScroll := nil; |
| 1967 |
FScrollBarVert.Position := FScrollBarPosition; |
| 1968 |
Application.ProcessMessages; // Skip message |
| 1969 |
FScrollBarVert.OnScroll := @ScrollBarVertScroll; |
| 1970 |
{$ELSE} |
| 1971 |
FScrollBarVert.Position := FScrollBarPosition; |
| 1972 |
{$ENDIF} |
| 1973 |
end; |
| 1974 |
end; |
| 1975 |
// else the scrollbar position will be updated in ScrollBarVertScroll |
| 1976 |
end; |
| 1977 |
|
| 1978 |
procedure TViewerControl.SetEncoding(AEncoding: TViewerEncoding); |
| 1979 |
begin |
| 1980 |
if not (csDesigning in ComponentState) then |
| 1981 |
begin |
| 1982 |
if AEncoding = veAutoDetect then |
| 1983 |
FEncoding := DetectEncoding |
| 1984 |
else |
| 1985 |
FEncoding := AEncoding; |
| 1986 |
|
| 1987 |
ReReadFile; |
| 1988 |
end |
| 1989 |
else |
| 1990 |
FEncoding := AEncoding; |
| 1991 |
end; |
| 1992 |
|
| 1993 |
function TViewerControl.GetEncodingName: string; |
| 1994 |
begin |
| 1995 |
Result := ViewerEncodingsNames[FEncoding]; |
| 1996 |
end; |
| 1997 |
|
| 1998 |
procedure TViewerControl.SetEncodingName(AEncodingName: string); |
| 1999 |
var |
| 2000 |
i: TViewerEncoding; |
| 2001 |
begin |
| 2002 |
for i := Low(TViewerEncoding) to High(TViewerEncoding) do |
| 2003 |
if NormalizeEncoding(ViewerEncodingsNames[i]) = NormalizeEncoding(AEncodingName) then |
| 2004 |
begin |
| 2005 |
SetEncoding(i); |
| 2006 |
break; |
| 2007 |
end; |
| 2008 |
end; |
| 2009 |
|
| 2010 |
function TViewerControl.GetClientHeightInLines(Whole: Boolean): Integer; |
| 2011 |
begin |
| 2012 |
if FTextHeight > 0 then |
| 2013 |
begin |
| 2014 |
if Whole then |
| 2015 |
Result := GetViewerRect.Height div FTextHeight |
| 2016 |
else |
| 2017 |
Result := Ceil(GetViewerRect.Height / FTextHeight); |
| 2018 |
end |
| 2019 |
else |
| 2020 |
Result := 0; |
| 2021 |
end; |
| 2022 |
|
| 2023 |
function TViewerControl.GetLinesTillEnd(FromPosition: PtrInt; |
| 2024 |
out LastLineReached: Boolean): Integer; |
| 2025 |
var |
| 2026 |
iPos: PtrInt; |
| 2027 |
yIndex: Integer; |
| 2028 |
DataLength: PtrInt; |
| 2029 |
CharLenInBytes: Integer; |
| 2030 |
begin |
| 2031 |
Result := 0; |
| 2032 |
iPos := FromPosition; |
| 2033 |
for yIndex := 0 to GetClientHeightInLines - 1 do |
| 2034 |
begin |
| 2035 |
if iPos >= FHighLimit then |
| 2036 |
Break; |
| 2037 |
Inc(Result, 1); |
| 2038 |
case Mode of |
| 2039 |
vcmBin: |
| 2040 |
iPos := iPos + cBinWidth; |
| 2041 |
vcmHex,vcmDec: |
| 2042 |
iPos := iPos + FCustom.ValuesPerLine; |
| 2043 |
vcmText, vcmWrap, vcmBook: |
| 2044 |
CalcTextLineLength(iPos, FHighLimit, DataLength); |
| 2045 |
end; |
| 2046 |
end; |
| 2047 |
LastLineReached := (iPos >= FHighLimit); |
| 2048 |
if LastLineReached and (FViewerControlMode in [vcmText, vcmWrap, vcmBook]) then |
| 2049 |
begin |
| 2050 |
if (GetPrevCharAsAscii(FHighLimit, CharLenInBytes) = 10) then |
| 2051 |
Inc(Result); |
| 2052 |
end; |
| 2053 |
end; |
| 2054 |
|
| 2055 |
procedure TViewerControl.SetShowCaret(AValue: Boolean); |
| 2056 |
begin |
| 2057 |
if FShowCaret <> AValue then |
| 2058 |
begin |
| 2059 |
FShowCaret:= AValue; |
| 2060 |
if FShowCaret then |
| 2061 |
begin |
| 2062 |
LCLIntf.CreateCaret(Handle, 0, 2, FTextHeight); |
| 2063 |
LCLIntf.ShowCaret(Handle); |
| 2064 |
FCaretVisible:= True; |
| 2065 |
Invalidate; |
| 2066 |
end |
| 2067 |
else begin |
| 2068 |
FCaretVisible:= False; |
| 2069 |
LCLIntf.HideCaret(Handle); |
| 2070 |
LCLIntf.DestroyCaret(Handle); |
| 2071 |
end; |
| 2072 |
end; |
| 2073 |
end; |
| 2074 |
|
| 2075 |
procedure TViewerControl.SetCaretPos(AValue: PtrInt); |
| 2076 |
begin |
| 2077 |
if FCaretPos <> AValue then |
| 2078 |
begin |
| 2079 |
FCaretPos := AValue; |
| 2080 |
if FShowCaret then Invalidate; |
| 2081 |
end; |
| 2082 |
end; |
| 2083 |
|
| 2084 |
function TViewerControl.GetPercent: Integer; |
| 2085 |
begin |
| 2086 |
if FHighLimit - FLowLimit > 0 then |
| 2087 |
Result := (Int64(FPosition - FLowLimit) * 100) div Int64(FHighLimit - FLowLimit) |
| 2088 |
else |
| 2089 |
Result := 0; |
| 2090 |
end; |
| 2091 |
|
| 2092 |
procedure TViewerControl.SetPercent(const AValue: Integer); |
| 2093 |
begin |
| 2094 |
if FHighLimit - FLowLimit > 0 then |
| 2095 |
Position := Int64(AValue) * (Int64(FHighLimit - FLowLimit) div 100) + FLowLimit |
| 2096 |
else |
| 2097 |
Position := 0; |
| 2098 |
end; |
| 2099 |
|
| 2100 |
procedure TViewerControl.SetBlockBegin(const AValue: PtrInt); |
| 2101 |
begin |
| 2102 |
if (AValue >= FLowLimit) and (AValue < FHighLimit) then |
| 2103 |
begin |
| 2104 |
if FBlockEnd < AValue then |
| 2105 |
FBlockEnd := AValue; |
| 2106 |
FBlockBeg := AValue; |
| 2107 |
Invalidate; |
| 2108 |
end; |
| 2109 |
end; |
| 2110 |
|
| 2111 |
procedure TViewerControl.SetBlockEnd(const AValue: PtrInt); |
| 2112 |
begin |
| 2113 |
if (AValue >= FLowLimit) and (AValue < FHighLimit) then |
| 2114 |
begin |
| 2115 |
if FBlockBeg > AValue then |
| 2116 |
FBlockBeg := AValue; |
| 2117 |
FBlockEnd := AValue; |
| 2118 |
Invalidate; |
| 2119 |
end; |
| 2120 |
end; |
| 2121 |
|
| 2122 |
procedure TViewerControl.OutText(x, y: Integer; const sText: String; |
| 2123 |
StartPos: PtrInt; DataLength: Integer); |
| 2124 |
var |
| 2125 |
pBegLine, pEndLine: PtrInt; |
| 2126 |
iBegDrawIndex, iEndDrawIndex: PtrInt; |
| 2127 |
begin |
| 2128 |
pBegLine := StartPos; |
| 2129 |
pEndLine := pBegLine + DataLength; |
| 2130 |
|
| 2131 |
Canvas.Font.Color := Font.Color; |
| 2132 |
|
| 2133 |
if FShowCaret and (FCaretPos >= pBegLine) and (FCaretPos <= pEndLine) then |
| 2134 |
begin |
| 2135 |
FCaretPoint.Y:= Y; |
| 2136 |
FCaretPoint.X:= X + Canvas.TextWidth(GetText(StartPos, FCaretPos - pBegLine, 0)); |
| 2137 |
end; |
| 2138 |
|
| 2139 |
// Out of selection, draw normal |
| 2140 |
if ((FBlockEnd - FBlockBeg) = 0) or ((FBlockBeg < pBegLine) and (FBlockEnd < pBegLine)) or // before |
| 2141 |
((FBlockBeg > pEndLine) and (FBlockEnd > pEndLine)) then // after |
| 2142 |
begin |
| 2143 |
Canvas.TextOut(x, y, sText); |
| 2144 |
Exit; |
| 2145 |
end; |
| 2146 |
|
| 2147 |
// Get selection start |
| 2148 |
if (FBlockBeg <= pBegLine) then |
| 2149 |
iBegDrawIndex := pBegLine |
| 2150 |
else |
| 2151 |
iBegDrawIndex := FBlockBeg; |
| 2152 |
// Get selection end |
| 2153 |
if (FBlockEnd < pEndLine) then |
| 2154 |
iEndDrawIndex := FBlockEnd |
| 2155 |
else |
| 2156 |
iEndDrawIndex := pEndLine; |
| 2157 |
|
| 2158 |
// Text after selection. |
| 2159 |
if pEndLine - iEndDrawIndex > 0 then |
| 2160 |
Canvas.TextOut(x, y, sText); |
| 2161 |
|
| 2162 |
// Text before selection + selected text |
| 2163 |
Canvas.Brush.Color := clHighlight; |
| 2164 |
Canvas.Font.Color := clHighlightText; |
| 2165 |
|
| 2166 |
Canvas.TextOut(X, Y, GetText(StartPos, iEndDrawIndex - pBegLine, 0)); |
| 2167 |
|
| 2168 |
// Restore previous canvas settings |
| 2169 |
Canvas.Brush.Color := Color; |
| 2170 |
Canvas.Font.Color := Font.Color; |
| 2171 |
|
| 2172 |
// Text before selection |
| 2173 |
if iBegDrawIndex - pBegLine > 0 then |
| 2174 |
Canvas.TextOut(X, Y, GetText(StartPos, iBegDrawIndex - pBegLine, 0)); |
| 2175 |
end; |
| 2176 |
|
| 2177 |
procedure TViewerControl.OutCustom(x, y: Integer; const sText: string; |
| 2178 |
StartPos: PtrInt; DataLength: Integer); |
| 2179 |
var |
| 2180 |
sTmpText: String; |
| 2181 |
pBegLine, pEndLine: PtrInt; |
| 2182 |
iBegDrawIndex, iEndDrawIndex: PtrInt; |
| 2183 |
begin |
| 2184 |
pBegLine := StartPos; |
| 2185 |
pEndLine := pBegLine + DataLength; |
| 2186 |
|
| 2187 |
Canvas.Font.Color := Font.Color; |
| 2188 |
|
| 2189 |
if FShowCaret and (FCaretPos >= pBegLine) and (FCaretPos <= pEndLine) then |
| 2190 |
begin |
| 2191 |
FCaretPoint.Y:= Y; |
| 2192 |
FCaretPoint.X:= X + Canvas.TextWidth(Copy(sText, 1, FCustom.StartAscii + (FCaretPos - pBegLine))); |
| 2193 |
end; |
| 2194 |
|
| 2195 |
// Out of selection, draw normal |
| 2196 |
if ((FBlockEnd - FBlockBeg) = 0) or ((FBlockBeg < pBegLine) and (FBlockEnd <= pBegLine)) or // before |
| 2197 |
((FBlockBeg > pEndLine) and (FBlockEnd > pEndLine)) then // after |
| 2198 |
begin |
| 2199 |
// Offset + hex part + space between hex and ascii |
| 2200 |
sTmpText:= Copy(sText, 1, FCustom.EndOfs) + ' '; |
| 2201 |
Canvas.TextOut(x, y, sTmpText); |
| 2202 |
x := x + Canvas.TextWidth(sTmpText); |
| 2203 |
// Ascii part |
| 2204 |
sTmpText := Copy(sText, 1 + FCustom.StartAscii, MaxInt); |
| 2205 |
Canvas.TextOut(x, y, sTmpText); |
| 2206 |
Exit; |
| 2207 |
end; |
| 2208 |
|
| 2209 |
// Get selection start |
| 2210 |
if (FBlockBeg <= pBegLine) then |
| 2211 |
iBegDrawIndex := pBegLine |
| 2212 |
else |
| 2213 |
iBegDrawIndex := FBlockBeg; |
| 2214 |
// Get selection end |
| 2215 |
if (FBlockEnd < pEndLine) then |
| 2216 |
iEndDrawIndex := FBlockEnd |
| 2217 |
else |
| 2218 |
iEndDrawIndex := pEndLine; |
| 2219 |
|
| 2220 |
// Text after selection (hex part) |
| 2221 |
if pEndLine - iEndDrawIndex > 0 then |
| 2222 |
begin |
| 2223 |
sTmpText := Copy(sText, 1, FCustom.StartOfs + (pEndLine - pBegLine) * (FCustom.MaxValueDigits + FCustom.SpaceCount)); |
| 2224 |
Canvas.TextOut(x, y, sTmpText); |
| 2225 |
end; |
| 2226 |
|
| 2227 |
// Text before selection + selected text (hex part) |
| 2228 |
sTmpText := Copy(sText, 1, FCustom.StartOfs + (iEndDrawIndex - pBegLine) * (FCustom.MaxValueDigits + FCustom.SpaceCount) - 1); |
| 2229 |
Canvas.Brush.Color := clHighlight; |
| 2230 |
Canvas.Font.Color := clHighlightText; |
| 2231 |
Canvas.TextOut(x, y, sTmpText); |
| 2232 |
|
| 2233 |
// Restore previous canvas settings |
| 2234 |
Canvas.Brush.Color := Color; |
| 2235 |
Canvas.Font.Color := Font.Color; |
| 2236 |
|
| 2237 |
// Offset + text before selection (hex part) |
| 2238 |
sTmpText := Copy(sText, 1, FCustom.StartOfs + (iBegDrawIndex - pBegLine) * (FCustom.MaxValueDigits + FCustom.SpaceCount)); |
| 2239 |
Canvas.TextOut(x, y, sTmpText); |
| 2240 |
|
| 2241 |
// Offset + hex part + space between hex and ascii |
| 2242 |
sTmpText:= Copy(sText, 1, FCustom.EndOfs) + ' '; |
| 2243 |
x := x + Canvas.TextWidth(sTmpText); |
| 2244 |
|
| 2245 |
// Text after selection (ascii part) |
| 2246 |
if pEndLine - iEndDrawIndex > 0 then |
| 2247 |
begin |
| 2248 |
sTmpText := Copy(sText, FCustom.StartAscii + 1, MaxInt); |
| 2249 |
Canvas.TextOut(x, y, sTmpText); |
| 2250 |
end; |
| 2251 |
|
| 2252 |
// Text before selection + selected text (ascii part) |
| 2253 |
sTmpText := Copy(sText, 1 + FCustom.StartAscii, iEndDrawIndex - pBegLine); |
| 2254 |
Canvas.Brush.Color := clHighlight; |
| 2255 |
Canvas.Font.Color := clHighlightText; |
| 2256 |
Canvas.TextOut(x, y, sTmpText); |
| 2257 |
|
| 2258 |
// Restore background color |
| 2259 |
Canvas.Brush.Color := Color; |
| 2260 |
Canvas.Font.Color := Font.Color; |
| 2261 |
|
| 2262 |
// Text before selection (ascii part) |
| 2263 |
if iBegDrawIndex - pBegLine > 0 then |
| 2264 |
begin |
| 2265 |
sTmpText := Copy(sText, 1 + FCustom.StartAscii, iBegDrawIndex - pBegLine); |
| 2266 |
Canvas.TextOut(x, y, sTmpText); |
| 2267 |
end; |
| 2268 |
end; |
| 2269 |
|
| 2270 |
procedure TViewerControl.OutBin(x, y: Integer; const sText: String; |
| 2271 |
StartPos: PtrInt; DataLength: Integer); |
| 2272 |
var |
| 2273 |
pBegLine, pEndLine: PtrInt; |
| 2274 |
iBegDrawIndex, iEndDrawIndex: PtrInt; |
| 2275 |
begin |
| 2276 |
pBegLine := StartPos; |
| 2277 |
pEndLine := pBegLine + DataLength; |
| 2278 |
|
| 2279 |
Canvas.Font.Color := Font.Color; |
| 2280 |
|
| 2281 |
if FShowCaret and (FCaretPos >= pBegLine) and (FCaretPos <= pEndLine) then |
| 2282 |
begin |
| 2283 |
FCaretPoint.Y:= Y; |
| 2284 |
FCaretPoint.X:= X + Canvas.TextWidth(Copy(sText, 1, FCaretPos - pBegLine)); |
| 2285 |
end; |
| 2286 |
|
| 2287 |
// Out of selection, draw normal |
| 2288 |
if ((FBlockEnd - FBlockBeg) = 0) or ((FBlockBeg < pBegLine) and (FBlockEnd < pBegLine)) or // before |
| 2289 |
((FBlockBeg > pEndLine) and (FBlockEnd > pEndLine)) then //after |
| 2290 |
begin |
| 2291 |
Canvas.TextOut(x, y, sText); |
| 2292 |
Exit; |
| 2293 |
end; |
| 2294 |
|
| 2295 |
// Get selection start/end. |
| 2296 |
if (FBlockBeg <= pBegLine) then |
| 2297 |
iBegDrawIndex := pBegLine |
| 2298 |
else |
| 2299 |
iBegDrawIndex := FBlockBeg; |
| 2300 |
if (FBlockEnd < pEndLine) then |
| 2301 |
iEndDrawIndex := FBlockEnd |
| 2302 |
else |
| 2303 |
iEndDrawIndex := pEndLine; |
| 2304 |
|
| 2305 |
// Text after selection. |
| 2306 |
if pEndLine - iEndDrawIndex > 0 then |
| 2307 |
Canvas.TextOut(x, y, sText); |
| 2308 |
|
| 2309 |
// Text before selection + selected text |
| 2310 |
Canvas.Brush.Color := clHighlight; |
| 2311 |
Canvas.Font.Color := clHighlightText; |
| 2312 |
|
| 2313 |
Canvas.TextOut(X, Y, Copy(sText, 1, iEndDrawIndex - pBegLine)); |
| 2314 |
|
| 2315 |
// Restore previous canvas settings |
| 2316 |
Canvas.Brush.Color := Color; |
| 2317 |
Canvas.Font.Color := Font.Color; |
| 2318 |
|
| 2319 |
// Text before selection |
| 2320 |
if iBegDrawIndex - pBegLine > 0 then |
| 2321 |
Canvas.TextOut(X, Y, Copy(sText, 1, iBegDrawIndex - pBegLine)); |
| 2322 |
end; |
| 2323 |
|
| 2324 |
procedure TViewerControl.AddLineOffset(const iOffset: PtrInt); |
| 2325 |
begin |
| 2326 |
FLineList.Add(iOffset); |
| 2327 |
end; |
| 2328 |
|
| 2329 |
procedure TViewerControl.KeyDown(var Key: word; Shift: TShiftState); |
| 2330 |
var |
| 2331 |
CharLenInBytes: Integer; |
| 2332 |
begin |
| 2333 |
if Shift = [] then |
| 2334 |
begin |
| 2335 |
case Key of |
| 2336 |
VK_DOWN: |
| 2337 |
begin |
| 2338 |
Key := 0; |
| 2339 |
Scroll(1); |
| 2340 |
end; |
| 2341 |
VK_UP: |
| 2342 |
begin |
| 2343 |
Key := 0; |
| 2344 |
Scroll(-1); |
| 2345 |
end; |
| 2346 |
VK_RIGHT: |
| 2347 |
begin |
| 2348 |
Key := 0; |
| 2349 |
HScroll(1); |
| 2350 |
end; |
| 2351 |
VK_LEFT: |
| 2352 |
begin |
| 2353 |
Key := 0; |
| 2354 |
HScroll(-1); |
| 2355 |
end; |
| 2356 |
VK_HOME: |
| 2357 |
begin |
| 2358 |
Key := 0; |
| 2359 |
CaretGoHome; |
| 2360 |
end; |
| 2361 |
VK_END: |
| 2362 |
begin |
| 2363 |
Key := 0; |
| 2364 |
CaretGoEnd; |
| 2365 |
end; |
| 2366 |
VK_PRIOR: |
| 2367 |
begin |
| 2368 |
Key := 0; |
| 2369 |
PageUp; |
| 2370 |
end; |
| 2371 |
VK_NEXT: |
| 2372 |
begin |
| 2373 |
Key := 0; |
| 2374 |
PageDown; |
| 2375 |
end; |
| 2376 |
else |
| 2377 |
inherited KeyDown(Key, Shift); |
| 2378 |
end; |
| 2379 |
end |
| 2380 |
else if Shift = [ssCtrl] then |
| 2381 |
begin |
| 2382 |
case Key of |
| 2383 |
VK_HOME: |
| 2384 |
begin |
| 2385 |
Key := 0; |
| 2386 |
CaretPos := FLowLimit; |
| 2387 |
MakeVisible(FCaretPos) |
| 2388 |
end; |
| 2389 |
VK_END: |
| 2390 |
begin |
| 2391 |
Key := 0; |
| 2392 |
CaretPos := FHighLimit; |
| 2393 |
MakeVisible(FCaretPos); |
| 2394 |
end; |
| 2395 |
else |
| 2396 |
inherited KeyDown(Key, Shift); |
| 2397 |
end; |
| 2398 |
end |
| 2399 |
else |
| 2400 |
inherited KeyDown(Key, Shift); |
| 2401 |
end; |
| 2402 |
|
| 2403 |
function TViewerControl.FindAsciiSetForward(aPosition, aMaxBytes: PtrInt; |
| 2404 |
const AsciiChars: String; |
| 2405 |
bFindNotIn: Boolean): PtrInt; |
| 2406 |
var |
| 2407 |
i: Integer; |
| 2408 |
found: Boolean; |
| 2409 |
u: Cardinal; |
| 2410 |
CharLenInBytes: Integer; |
| 2411 |
begin |
| 2412 |
Result := -1; |
| 2413 |
while aMaxBytes > 0 do |
| 2414 |
begin |
| 2415 |
u := GetNextCharAsAscii(aPosition, CharLenInBytes); |
| 2416 |
if CharLenInBytes = 0 then |
| 2417 |
Exit; |
| 2418 |
|
| 2419 |
if not bFindNotIn then |
| 2420 |
begin |
| 2421 |
for i := 1 to Length(AsciiChars) do |
| 2422 |
if u = ord(AsciiChars[i]) then |
| 2423 |
Exit(aPosition); |
| 2424 |
end |
| 2425 |
else |
| 2426 |
begin |
| 2427 |
found := False; |
| 2428 |
for i := 1 to Length(AsciiChars) do |
| 2429 |
if u = ord(AsciiChars[i]) then |
| 2430 |
begin |
| 2431 |
found := True; |
| 2432 |
break; |
| 2433 |
end; |
| 2434 |
|
| 2435 |
if not found then |
| 2436 |
Exit(aPosition); |
| 2437 |
end; |
| 2438 |
|
| 2439 |
Inc(aPosition, CharLenInBytes); |
| 2440 |
Dec(aMaxBytes, CharLenInBytes); |
| 2441 |
end; |
| 2442 |
end; |
| 2443 |
|
| 2444 |
function TViewerControl.FindAsciiSetBackward(aPosition, aMaxBytes: PtrInt; |
| 2445 |
const AsciiChars: String; |
| 2446 |
bFindNotIn: Boolean): PtrInt; |
| 2447 |
var |
| 2448 |
i: Integer; |
| 2449 |
found: Boolean; |
| 2450 |
u: Cardinal; |
| 2451 |
CharLenInBytes: Integer; |
| 2452 |
begin |
| 2453 |
Result := -1; |
| 2454 |
while aMaxBytes > 0 do |
| 2455 |
begin |
| 2456 |
u := GetPrevCharAsAscii(aPosition, CharLenInBytes); |
| 2457 |
if CharLenInBytes = 0 then |
| 2458 |
Exit; |
| 2459 |
|
| 2460 |
if not bFindNotIn then |
| 2461 |
begin |
| 2462 |
for i := 1 to Length(AsciiChars) do |
| 2463 |
if u = ord(AsciiChars[i]) then |
| 2464 |
Exit(aPosition); |
| 2465 |
end |
| 2466 |
else |
| 2467 |
begin |
| 2468 |
found := False; |
| 2469 |
for i := 1 to Length(AsciiChars) do |
| 2470 |
if u = ord(AsciiChars[i]) then |
| 2471 |
begin |
| 2472 |
found := True; |
| 2473 |
break; |
| 2474 |
end; |
| 2475 |
|
| 2476 |
if not found then |
| 2477 |
Exit(aPosition); |
| 2478 |
end; |
| 2479 |
|
| 2480 |
Dec(aPosition, CharLenInBytes); |
| 2481 |
Dec(aMaxBytes, CharLenInBytes); |
| 2482 |
end; |
| 2483 |
end; |
| 2484 |
|
| 2485 |
procedure TViewerControl.MouseDown(Button: TMouseButton; Shift: TShiftState; |
| 2486 |
X, Y: Integer); |
| 2487 |
var |
| 2488 |
LineBegin, LineEnd: PtrInt; |
| 2489 |
ClickPos: PtrInt; |
| 2490 |
CharSide: TCharSide; |
| 2491 |
CharLenInBytes: Integer; |
| 2492 |
begin |
| 2493 |
inherited; |
| 2494 |
|
| 2495 |
SetFocus; |
| 2496 |
|
| 2497 |
if not IsFileOpen then |
| 2498 |
Exit; |
| 2499 |
|
| 2500 |
case Button of |
| 2501 |
mbLeft: |
| 2502 |
begin |
| 2503 |
if Shift * [ssDouble, ssTriple] = [] then |
| 2504 |
begin |
| 2505 |
// Single click. |
| 2506 |
ClickPos := XYPos2Adr(x, y, CharSide); |
| 2507 |
if ClickPos <> -1 then |
| 2508 |
begin |
| 2509 |
FBlockBeg := ClickPos; |
| 2510 |
FBlockEnd := ClickPos; |
| 2511 |
FCaretPos := ClickPos; |
| 2512 |
FMouseBlockBeg := ClickPos; |
| 2513 |
FMouseBlockSide := CharSide; |
| 2514 |
FSelecting := True; |
| 2515 |
|
| 2516 |
if CharSide in [csRight, csAfter] then |
| 2517 |
begin |
| 2518 |
GetNextCharAsAscii(FCaretPos, CharLenInBytes); |
| 2519 |
FCaretPos := FCaretPos + CharLenInBytes; |
| 2520 |
end; |
| 2521 |
|
| 2522 |
Invalidate; |
| 2523 |
end |
| 2524 |
else |
| 2525 |
FSelecting := False; |
| 2526 |
end |
| 2527 |
else // if double click or triple click |
| 2528 |
begin |
| 2529 |
FSelecting := False; |
| 2530 |
LineBegin := GetStartOfLine(FMouseBlockBeg); |
| 2531 |
LineEnd := GetEndOfLine(FMouseBlockBeg); |
| 2532 |
|
| 2533 |
if ssDouble in Shift then |
| 2534 |
begin |
| 2535 |
// Select word with double-click. |
| 2536 |
FBlockBeg := FindAsciiSetBackward(FMouseBlockBeg, |
| 2537 |
FMouseBlockBeg - LineBegin, sNonCharacter, False); |
| 2538 |
|
| 2539 |
FBlockEnd := FindAsciiSetForward(FMouseBlockBeg, |
| 2540 |
LineEnd - FMouseBlockBeg, sNonCharacter, False); |
| 2541 |
end |
| 2542 |
else if ssTriple in Shift then |
| 2543 |
begin |
| 2544 |
// Select line with triple-click. |
| 2545 |
FBlockBeg := FindAsciiSetForward(LineBegin, |
| 2546 |
LineEnd - LineBegin, sWhiteSpace, True); |
| 2547 |
|
| 2548 |
FBlockEnd := FindAsciiSetBackward(LineEnd, |
| 2549 |
LineEnd - LineBegin, sWhiteSpace, True); |
| 2550 |
end; |
| 2551 |
|
| 2552 |
if FBlockBeg = -1 then |
| 2553 |
FBlockBeg := LineBegin; |
| 2554 |
if FBlockEnd = -1 then |
| 2555 |
FBlockEnd := LineEnd; |
| 2556 |
|
| 2557 |
if FBlockBeg > FBlockEnd then |
| 2558 |
FBlockEnd := FBlockBeg; |
| 2559 |
|
| 2560 |
if FAutoCopy then |
| 2561 |
CopyToClipboard; |
| 2562 |
Invalidate; |
| 2563 |
end; |
| 2564 |
end; // mbLeft |
| 2565 |
end; // case |
| 2566 |
end; |
| 2567 |
|
| 2568 |
procedure TViewerControl.MouseMove(Shift: TShiftState; X, Y: Integer); |
| 2569 |
|
| 2570 |
procedure MoveOneChar(var aPosition: PtrInt); |
| 2571 |
var |
| 2572 |
CharLenInBytes: Integer; |
| 2573 |
begin |
| 2574 |
GetNextCharAsAscii(aPosition, CharLenInBytes); |
| 2575 |
aPosition := aPosition + CharLenInBytes; |
| 2576 |
end; |
| 2577 |
|
| 2578 |
procedure MoveOneCharByMouseSide(var aPosition: PtrInt); |
| 2579 |
begin |
| 2580 |
if FMouseBlockSide in [csRight, csAfter] then |
| 2581 |
MoveOneChar(aPosition); |
| 2582 |
end; |
| 2583 |
|
| 2584 |
var |
| 2585 |
ClickPos: PtrInt; |
| 2586 |
CharSide: TCharSide; |
| 2587 |
begin |
| 2588 |
inherited; |
| 2589 |
|
| 2590 |
if FSelecting then |
| 2591 |
begin |
| 2592 |
if y < FTextHeight then |
| 2593 |
Scroll(-3) |
| 2594 |
else if y > ClientHeight - FTextHeight then |
| 2595 |
Scroll(3); |
| 2596 |
|
| 2597 |
ClickPos := XYPos2Adr(x, y, CharSide); |
| 2598 |
if ClickPos <> -1 then |
| 2599 |
begin |
| 2600 |
if ClickPos < FMouseBlockBeg then |
| 2601 |
begin |
| 2602 |
// Got a new beginning. |
| 2603 |
FBlockBeg := ClickPos; |
| 2604 |
FBlockEnd := FMouseBlockBeg; |
| 2605 |
|
| 2606 |
// Move end beyond last character. |
| 2607 |
MoveOneCharByMouseSide(FBlockEnd); |
| 2608 |
|
| 2609 |
// When selecting from right to left, the current selected side must be |
| 2610 |
// either csLeft or csBefore, otherwise current position is not included. |
| 2611 |
if not (CharSide in [csLeft, csBefore]) then |
| 2612 |
begin |
| 2613 |
// Current position should not be included in selection. |
| 2614 |
// Move beginning after first character. |
| 2615 |
MoveOneChar(FBlockBeg); |
| 2616 |
end; |
| 2617 |
FCaretPos:= FBlockBeg; |
| 2618 |
end |
| 2619 |
else if ClickPos > FMouseBlockBeg then |
| 2620 |
begin |
| 2621 |
// Got a new end. |
| 2622 |
FBlockBeg := FMouseBlockBeg; |
| 2623 |
FBlockEnd := ClickPos; |
| 2624 |
|
| 2625 |
// Move beginning after first character. |
| 2626 |
MoveOneCharByMouseSide(FBlockBeg); |
| 2627 |
|
| 2628 |
// When selecting from left to right, the current selected side must be |
| 2629 |
// either csRight or csAfter, otherwise current position is not included. |
| 2630 |
if CharSide in [csRight, csAfter] then |
| 2631 |
begin |
| 2632 |
// Current position should be included in selection. |
| 2633 |
// Move end beyond last character. |
| 2634 |
MoveOneChar(FBlockEnd); |
| 2635 |
end; |
| 2636 |
FCaretPos:= FBlockEnd; |
| 2637 |
end |
| 2638 |
else if FMouseBlockSide <> CharSide then |
| 2639 |
begin |
| 2640 |
// Same position but changed side of the character. |
| 2641 |
FBlockBeg := FMouseBlockBeg; |
| 2642 |
FBlockEnd := FMouseBlockBeg; |
| 2643 |
|
| 2644 |
if ((FMouseBlockSide in [csBefore, csLeft]) and |
| 2645 |
(CharSide in [csRight, csAfter])) or |
| 2646 |
((FMouseBlockSide in [csRight, csAfter]) and |
| 2647 |
(CharSide in [csBefore, csLeft])) then |
| 2648 |
begin |
| 2649 |
// Move end beyond last character. |
| 2650 |
MoveOneChar(FBlockEnd); |
| 2651 |
end; |
| 2652 |
FCaretPos:= FBlockEnd; |
| 2653 |
end |
| 2654 |
else |
| 2655 |
begin |
| 2656 |
FBlockBeg := FMouseBlockBeg; |
| 2657 |
FBlockEnd := FMouseBlockBeg; |
| 2658 |
end; |
| 2659 |
|
| 2660 |
Invalidate; |
| 2661 |
end; |
| 2662 |
end; |
| 2663 |
end; |
| 2664 |
|
| 2665 |
procedure TViewerControl.MouseUp(Button: TMouseButton; Shift: TShiftState; |
| 2666 |
X, Y: Integer); |
| 2667 |
begin |
| 2668 |
inherited; |
| 2669 |
|
| 2670 |
if FSelecting and (Button = mbLeft) and (Shift * [ssDouble, ssTriple] = []) then |
| 2671 |
begin |
| 2672 |
if FAutoCopy then |
| 2673 |
CopyToClipboard; |
| 2674 |
FSelecting := False; |
| 2675 |
end; |
| 2676 |
end; |
| 2677 |
|
| 2678 |
function TViewerControl.DoMouseWheelDown(Shift: TShiftState; MousePos: TPoint): Boolean; |
| 2679 |
begin |
| 2680 |
Result := inherited; |
| 2681 |
if not Result then |
| 2682 |
Result := Scroll(Mouse.WheelScrollLines); |
| 2683 |
end; |
| 2684 |
|
| 2685 |
function TViewerControl.DoMouseWheelUp(Shift: TShiftState; MousePos: TPoint): Boolean; |
| 2686 |
begin |
| 2687 |
Result := inherited; |
| 2688 |
if not Result then |
| 2689 |
Result := Scroll(-Mouse.WheelScrollLines); |
| 2690 |
end; |
| 2691 |
|
| 2692 |
function TViewerControl.DoMouseWheelLeft(Shift: TShiftState; MousePos: TPoint): Boolean; |
| 2693 |
begin |
| 2694 |
Result:= inherited DoMouseWheelLeft(Shift, MousePos); |
| 2695 |
if not Result then |
| 2696 |
Result := HScroll(-Mouse.WheelScrollLines); |
| 2697 |
end; |
| 2698 |
|
| 2699 |
function TViewerControl.DoMouseWheelRight(Shift: TShiftState; MousePos: TPoint): Boolean; |
| 2700 |
begin |
| 2701 |
Result:= inherited DoMouseWheelRight(Shift, MousePos); |
| 2702 |
if not Result then |
| 2703 |
Result := HScroll(Mouse.WheelScrollLines); |
| 2704 |
end; |
| 2705 |
|
| 2706 |
procedure TViewerControl.DoAutoAdjustLayout(const AMode: TLayoutAdjustmentPolicy; const AXProportion, AYProportion: Double); |
| 2707 |
begin |
| 2708 |
FScrollBarVert.Width := LCLIntf.GetSystemMetrics(SM_CYVSCROLL); |
| 2709 |
FScrollBarHorz.Height := LCLIntf.GetSystemMetrics(SM_CYHSCROLL); |
| 2710 |
inherited DoAutoAdjustLayout(AMode, AXProportion, AYProportion); |
| 2711 |
end; |
| 2712 |
|
| 2713 |
function TViewerControl.XYPos2Adr(x, y: Integer; out CharSide: TCharSide): PtrInt; |
| 2714 |
var |
| 2715 |
yIndex: Integer; |
| 2716 |
StartLine, EndLine: PtrInt; |
| 2717 |
|
| 2718 |
function XYPos2AdrBin: PtrInt; |
| 2719 |
var |
| 2720 |
I: Integer; |
| 2721 |
charWidth: Integer; |
| 2722 |
textWidth: Integer; |
| 2723 |
tmpPosition: PtrInt; |
| 2724 |
s, ss, sText: String; |
| 2725 |
begin |
| 2726 |
ss := EmptyStr; |
| 2727 |
tmpPosition := StartLine; |
| 2728 |
sText := TransformBin(tmpPosition, EndLine); |
| 2729 |
for I := 1 to Length(sText) do |
| 2730 |
begin |
| 2731 |
s:= sText[I]; |
| 2732 |
ss := ss + s; |
| 2733 |
textWidth := Canvas.TextWidth(ss); |
| 2734 |
if textWidth > x then |
| 2735 |
begin |
| 2736 |
charWidth := Canvas.TextWidth(s); |
| 2737 |
if textWidth - charWidth div 2 > x then |
| 2738 |
CharSide := csLeft |
| 2739 |
else |
| 2740 |
CharSide := csRight; |
| 2741 |
|
| 2742 |
Exit(StartLine + I - 1); // -1 because we count from 1 |
| 2743 |
end; |
| 2744 |
end; |
| 2745 |
CharSide := csBefore; |
| 2746 |
Result := EndLine; |
| 2747 |
end; |
| 2748 |
|
| 2749 |
|
| 2750 |
function XYPos2AdrCustom: PtrInt; |
| 2751 |
// | offset part | custom part | native part | |
| 2752 |
// | 0000AAAA: | FF AA CC AE | djfjks | |
| 2753 |
|
| 2754 |
var |
| 2755 |
i: Integer; |
| 2756 |
charWidth: Integer; |
| 2757 |
textWidth: Integer; |
| 2758 |
tmpPosition: PtrInt; |
| 2759 |
ss, sText, sPartialText: String; |
| 2760 |
begin |
| 2761 |
tmpPosition := StartLine; |
| 2762 |
sText := TransformCustom(tmpPosition, EndLine); |
| 2763 |
if sText = '' then Exit; |
| 2764 |
|
| 2765 |
// Clicked on offset part |
| 2766 |
ss := Copy(sText, 1, FCustom.StartOfs); |
| 2767 |
textWidth := Canvas.TextWidth(ss); |
| 2768 |
if textWidth > x then |
| 2769 |
begin |
| 2770 |
CharSide := csBefore; |
| 2771 |
Exit(StartLine); |
| 2772 |
end; |
| 2773 |
|
| 2774 |
// Clicked on custom part |
| 2775 |
for i := 0 to FCustom.ValuesPerLine - 1 do |
| 2776 |
begin |
| 2777 |
sPartialText := Copy(sText, 1 + FCustom.StartOfs + i * (FCustom.MaxValueDigits + FCustom.SpaceCount), FCustom.MaxValueDigits); |
| 2778 |
ss := ss + sPartialText; |
| 2779 |
textWidth := Canvas.TextWidth(ss); |
| 2780 |
if textWidth > x then |
| 2781 |
begin |
| 2782 |
// Check if we're not after end of data. |
| 2783 |
if StartLine + i >= EndLine then |
| 2784 |
begin |
| 2785 |
CharSide := csBefore; |
| 2786 |
Exit(EndLine); |
| 2787 |
end; |
| 2788 |
|
| 2789 |
charWidth := Canvas.TextWidth(sPartialText); |
| 2790 |
|
| 2791 |
if textWidth - charWidth div 2 > x then |
| 2792 |
CharSide := csLeft |
| 2793 |
else |
| 2794 |
CharSide := csRight; |
| 2795 |
|
| 2796 |
Exit(StartLine + i); |
| 2797 |
end; |
| 2798 |
|
| 2799 |
// Space after hex number. |
| 2800 |
ss := ss + string(sText[1 + FCustom.StartOfs + i * (FCustom.MaxValueDigits + 1) + FCustom.MaxValueDigits]); |
| 2801 |
textWidth := Canvas.TextWidth(ss); |
| 2802 |
|
| 2803 |
if textWidth > x then |
| 2804 |
begin |
| 2805 |
CharSide := csAfter; |
| 2806 |
Exit(StartLine + i); |
| 2807 |
end; |
| 2808 |
end; |
| 2809 |
|
| 2810 |
// Clicked between hex and ascii. |
| 2811 |
sPartialText := Copy(sText, 1 + FCustom.StartOfs, FCustom.StartAscii - FCustom.EndOfs); |
| 2812 |
ss := ss + sPartialText; |
| 2813 |
textWidth := Canvas.TextWidth(ss); |
| 2814 |
if textWidth > x then |
| 2815 |
begin |
| 2816 |
Exit(-1); // No position. |
| 2817 |
end; |
| 2818 |
|
| 2819 |
// Clicked on ascii part. |
| 2820 |
for i := 0 to FCustom.ValuesPerLine - 1 do |
| 2821 |
begin |
| 2822 |
sPartialText := string(sText[1 + FCustom.StartAscii + i]); |
| 2823 |
ss := ss + sPartialText; |
| 2824 |
textWidth := Canvas.TextWidth(ss); |
| 2825 |
if textWidth > x then |
| 2826 |
begin |
| 2827 |
// Check if we're not after end of data. |
| 2828 |
if StartLine + i >= EndLine then |
| 2829 |
begin |
| 2830 |
CharSide := csBefore; |
| 2831 |
Exit(EndLine); |
| 2832 |
end; |
| 2833 |
|
| 2834 |
charWidth := Canvas.TextWidth(sPartialText); |
| 2835 |
|
| 2836 |
if textWidth - charWidth div 2 > x then |
| 2837 |
CharSide := csLeft |
| 2838 |
else |
| 2839 |
CharSide := csRight; |
| 2840 |
|
| 2841 |
Exit(StartLine + i); |
| 2842 |
end; |
| 2843 |
end; |
| 2844 |
|
| 2845 |
CharSide := csBefore; |
| 2846 |
Result := EndLine; |
| 2847 |
end; |
| 2848 |
|
| 2849 |
function XYPos2AdrText: PtrInt; |
| 2850 |
var |
| 2851 |
i: PtrInt; |
| 2852 |
charWidth: Integer; |
| 2853 |
textWidth: Integer; |
| 2854 |
len: Integer = 0; |
| 2855 |
CharLenInBytes: Integer; |
| 2856 |
s: String; |
| 2857 |
ss: String; |
| 2858 |
begin |
| 2859 |
ss := ''; |
| 2860 |
i := StartLine; |
| 2861 |
while i < EndLine do |
| 2862 |
begin |
| 2863 |
s := GetNextCharAsUtf8(i, CharLenInBytes); |
| 2864 |
if CharLenInBytes = 0 then |
| 2865 |
Break; |
| 2866 |
|
| 2867 |
// Check if the conversion to UTF-8 was successful. |
| 2868 |
if Length(s) > 0 then |
| 2869 |
begin |
| 2870 |
if s = #9 then |
| 2871 |
begin |
| 2872 |
s := StringOfChar(' ', FTabSpaces - len mod FTabSpaces); |
| 2873 |
len := len + (FTabSpaces - len mod FTabSpaces); |
| 2874 |
end |
| 2875 |
else |
| 2876 |
Inc(len); // Assume there is one character after conversion |
| 2877 |
// (otherwise use Inc(len, UTF8Length(s))). |
| 2878 |
|
| 2879 |
if (Mode = vcmText) and (len <= FHPosition) then |
| 2880 |
begin |
| 2881 |
i := i + CharLenInBytes; |
| 2882 |
Continue; |
| 2883 |
end; |
| 2884 |
|
| 2885 |
if (CharLenInBytes = 1) and (s[1] < ' ') then |
| 2886 |
begin |
| 2887 |
s := ' '; |
| 2888 |
end; |
| 2889 |
|
| 2890 |
ss := ss + s; |
| 2891 |
textWidth := Canvas.TextWidth(ss); |
| 2892 |
if textWidth > x then |
| 2893 |
begin |
| 2894 |
charWidth := Canvas.TextWidth(s); |
| 2895 |
if textWidth - charWidth div 2 > x then |
| 2896 |
CharSide := csLeft |
| 2897 |
else |
| 2898 |
CharSide := csRight; |
| 2899 |
|
| 2900 |
Exit(i); |
| 2901 |
end; |
| 2902 |
end; |
| 2903 |
|
| 2904 |
i := i + CharLenInBytes; |
| 2905 |
end; |
| 2906 |
|
| 2907 |
CharSide := csBefore; |
| 2908 |
Result := EndLine; |
| 2909 |
end; |
| 2910 |
|
| 2911 |
begin |
| 2912 |
if FLineList.Count = 0 then |
| 2913 |
Exit(-1); |
| 2914 |
|
| 2915 |
if (x < FLeftMargin) then |
| 2916 |
x := 0 |
| 2917 |
else begin |
| 2918 |
x := x - FLeftMargin; |
| 2919 |
end; |
| 2920 |
|
| 2921 |
yIndex := y div FTextHeight; |
| 2922 |
if yIndex >= FLineList.Count then |
| 2923 |
yIndex := FLineList.Count - 1; |
| 2924 |
if yIndex < 0 then |
| 2925 |
yIndex := 0; |
| 2926 |
|
| 2927 |
// Get position of first character of the line. |
| 2928 |
StartLine := FLineList.Items[yIndex]; |
| 2929 |
// Get position of last character of the line. |
| 2930 |
EndLine := GetEndOfLine(StartLine); |
| 2931 |
|
| 2932 |
if (x = 0) and ((Mode <> vcmText) or (FHPosition = 0)) then |
| 2933 |
begin |
| 2934 |
CharSide := csBefore; |
| 2935 |
Exit(StartLine); |
| 2936 |
end; |
| 2937 |
|
| 2938 |
case Mode of |
| 2939 |
vcmBin: |
| 2940 |
Result := XYPos2AdrBin; |
| 2941 |
vcmHex,vcmDec: |
| 2942 |
Result := XYPos2AdrCustom; // XYPos2AdrHex; |
| 2943 |
vcmText, vcmWrap, vcmBook: |
| 2944 |
Result := XYPos2AdrText; |
| 2945 |
else |
| 2946 |
raise Exception.Create('Invalid viewer mode'); |
| 2947 |
end; |
| 2948 |
|
| 2949 |
end; |
| 2950 |
|
| 2951 |
procedure TViewerControl.SelectAll; |
| 2952 |
begin |
| 2953 |
SelectText(FLowLimit, FHighLimit); |
| 2954 |
end; |
| 2955 |
|
| 2956 |
procedure TViewerControl.SelectText(AStart, AEnd: PtrInt); |
| 2957 |
begin |
| 2958 |
if AStart < FLowLimit then |
| 2959 |
AStart := FLowLimit; |
| 2960 |
if AEnd > FHighLimit then |
| 2961 |
AEnd := FHighLimit; |
| 2962 |
|
| 2963 |
if AStart <= AEnd then |
| 2964 |
begin |
| 2965 |
FBlockBeg := AStart; |
| 2966 |
FBlockEnd := AEnd; |
| 2967 |
Invalidate; |
| 2968 |
end; |
| 2969 |
end; |
| 2970 |
|
| 2971 |
procedure TViewerControl.CopyToClipboard; |
| 2972 |
var |
| 2973 |
sText, utf8Text: string; |
| 2974 |
begin |
| 2975 |
if (FBlockEnd - FBlockBeg) <= 0 then |
| 2976 |
Exit; |
| 2977 |
if (FBlockEnd - FBlockBeg) > 1024 * 1024 then // Max 1 MB to clipboard |
| 2978 |
Exit; |
| 2979 |
SetString(sText, GetDataAdr + FBlockBeg, FBlockEnd - FBlockBeg); |
| 2980 |
utf8Text := ConvertToUTF8(sText); |
| 2981 |
{$IFDEF LCLGTK2} |
| 2982 |
// Workaround for Lazarus bug #0021453. LCL adds trailing zero to clipboard in Clipboard.AsText. |
| 2983 |
Clipboard.Clear; |
| 2984 |
Clipboard.AddFormat(PredefinedClipboardFormat(pcfText), utf8Text[1], Length(utf8Text)); |
| 2985 |
{$ELSE} |
| 2986 |
Clipboard.AsText := utf8Text; |
| 2987 |
{$ENDIF} |
| 2988 |
end; |
| 2989 |
|
| 2990 |
procedure TViewerControl.CopyToClipboardF; |
| 2991 |
var |
| 2992 |
s,sText, utf8Text: string; |
| 2993 |
len: Integer; |
| 2994 |
begin |
| 2995 |
len:=FBlockEnd-FBlockBeg; |
| 2996 |
if len=0 then exit; |
| 2997 |
sText:=TransformCustomBlock(FBlockBeg,len,False,False,s); |
| 2998 |
|
| 2999 |
utf8Text := ConvertToUTF8(sText); |
| 3000 |
{$IFDEF LCLGTK2} |
| 3001 |
// Workaround for Lazarus bug #0021453. LCL adds trailing zero to clipboard in Clipboard.AsText. |
| 3002 |
Clipboard.Clear; |
| 3003 |
Clipboard.AddFormat(PredefinedClipboardFormat(pcfText), utf8Text[1], Length(utf8Text)); |
| 3004 |
{$ELSE} |
| 3005 |
Clipboard.AsText := utf8Text; |
| 3006 |
{$ENDIF} |
| 3007 |
end; |
| 3008 |
|
| 3009 |
function TViewerControl.Selection: String; |
| 3010 |
const |
| 3011 |
MAX_LEN = 512; |
| 3012 |
var |
| 3013 |
sText: String; |
| 3014 |
AIndex: PtrInt; |
| 3015 |
ALength: PtrInt; |
| 3016 |
CharLenInBytes: Integer; |
| 3017 |
begin |
| 3018 |
if (FBlockEnd - FBlockBeg) <= 0 then |
| 3019 |
Exit(EmptyStr); |
| 3020 |
ALength:= FBlockEnd - FBlockBeg; |
| 3021 |
if ALength <= MAX_LEN then |
| 3022 |
begin |
| 3023 |
SetString(sText, GetDataAdr + FBlockBeg, ALength); |
| 3024 |
Result := ConvertToUTF8(sText); |
| 3025 |
end |
| 3026 |
else begin |
| 3027 |
Result:= EmptyStr; |
| 3028 |
AIndex:= FBlockBeg; |
| 3029 |
ALength:= AIndex + MAX_LEN; |
| 3030 |
while AIndex < ALength do |
| 3031 |
begin |
| 3032 |
sText := GetNextCharAsUtf8(AIndex, CharLenInBytes); |
| 3033 |
if CharLenInBytes = 0 then |
| 3034 |
Break; |
| 3035 |
Result:= Result + sText; |
| 3036 |
AIndex:= AIndex + CharLenInBytes; |
| 3037 |
end; |
| 3038 |
end; |
| 3039 |
end; |
| 3040 |
|
| 3041 |
function TViewerControl.GetNextCharAsAscii(const iPosition: PtrInt; out CharLenInBytes: Integer): Cardinal; |
| 3042 |
var |
| 3043 |
u1, u2: Word; |
| 3044 |
InvalidCharLen: Integer; |
| 3045 |
begin |
| 3046 |
Result := 0; |
| 3047 |
case FEncoding of |
| 3048 |
veUtf8, veUtf8bom: |
| 3049 |
begin |
| 3050 |
if iPosition < FHighLimit then |
| 3051 |
begin |
| 3052 |
CharLenInBytes := SafeUTF8NextCharLen(GetDataAdr + iPosition, |
| 3053 |
FHighLimit - iPosition, |
| 3054 |
InvalidCharLen); |
| 3055 |
|
| 3056 |
// It's enough to only return Ascii. |
| 3057 |
if CharLenInBytes = 1 then |
| 3058 |
Result := PByte(GetDataAdr)[iPosition]; |
| 3059 |
|
| 3060 |
// Full conversion: |
| 3061 |
// Result := UTF8CodepointToUnicode(PAnsiChar(GetDataAdr + iPosition), CharLenInBytes); |
| 3062 |
end |
| 3063 |
else |
| 3064 |
CharLenInBytes := 0; |
| 3065 |
end; |
| 3066 |
|
| 3067 |
veAnsi, veOem, |
| 3068 |
veCp1250..veCp874, |
| 3069 |
veIso88591, |
| 3070 |
veIso88592, |
| 3071 |
veKoi8r, |
| 3072 |
veKoi8u, |
| 3073 |
veKoi8ru: |
| 3074 |
if iPosition < FHighLimit then |
| 3075 |
begin |
| 3076 |
Result := PByte(GetDataAdr)[iPosition]; |
| 3077 |
CharLenInBytes := 1; |
| 3078 |
end |
| 3079 |
else |
| 3080 |
CharLenInBytes := 0; |
| 3081 |
|
| 3082 |
veUcs2be: |
| 3083 |
if iPosition + SizeOf(Word) - 1 < FHighLimit then |
| 3084 |
begin |
| 3085 |
Result := BEtoN(PWord(GetDataAdr + iPosition)[0]); |
| 3086 |
CharLenInBytes := SizeOf(Word); |
| 3087 |
end |
| 3088 |
else |
| 3089 |
CharLenInBytes := 0; |
| 3090 |
|
| 3091 |
veUcs2le: |
| 3092 |
if iPosition + SizeOf(Word) - 1 < FHighLimit then |
| 3093 |
begin |
| 3094 |
Result := LEtoN(PWord(GetDataAdr + iPosition)[0]); |
| 3095 |
CharLenInBytes := SizeOf(Word); |
| 3096 |
end |
| 3097 |
else |
| 3098 |
CharLenInBytes := 0; |
| 3099 |
|
| 3100 |
veUtf16be: |
| 3101 |
if iPosition + SizeOf(Word) - 1 < FHighLimit then |
| 3102 |
begin |
| 3103 |
u1 := BEtoN(PWord(GetDataAdr + iPosition)[0]); |
| 3104 |
CharLenInBytes := UTF16CharacterLength(@u1); |
| 3105 |
if CharLenInBytes = 1 then |
| 3106 |
begin |
| 3107 |
Result := u1; |
| 3108 |
end |
| 3109 |
else if iPosition + SizeOf(Word) * CharLenInBytes - 1 < FHighLimit then |
| 3110 |
begin |
| 3111 |
u2 := BEtoN(PWord(GetDataAdr + iPosition)[1]); |
| 3112 |
Result := utf16PairToUnicode(u1, u2); |
| 3113 |
end; |
| 3114 |
|
| 3115 |
CharLenInBytes := CharLenInBytes * SizeOf(Word); |
| 3116 |
end |
| 3117 |
else |
| 3118 |
CharLenInBytes := 0; |
| 3119 |
|
| 3120 |
veUtf16le: |
| 3121 |
if iPosition + SizeOf(Word) - 1 < FHighLimit then |
| 3122 |
begin |
| 3123 |
u1 := LEtoN(PWord(GetDataAdr + iPosition)[0]); |
| 3124 |
CharLenInBytes := UTF16CharacterLength(@u1); |
| 3125 |
if CharLenInBytes = 1 then |
| 3126 |
begin |
| 3127 |
Result := u1; |
| 3128 |
end |
| 3129 |
else if iPosition + SizeOf(Word) * CharLenInBytes - 1 < FHighLimit then |
| 3130 |
begin |
| 3131 |
u2 := LEtoN(PWord(GetDataAdr + iPosition)[1]); |
| 3132 |
Result := utf16PairToUnicode(u1, u2); |
| 3133 |
end |
| 3134 |
else |
| 3135 |
CharLenInBytes := 0; |
| 3136 |
|
| 3137 |
CharLenInBytes := CharLenInBytes * SizeOf(Word); |
| 3138 |
end |
| 3139 |
else |
| 3140 |
CharLenInBytes := 0; |
| 3141 |
|
| 3142 |
veUtf32be: |
| 3143 |
if iPosition + SizeOf(LongWord) - 1 < FHighLimit then |
| 3144 |
begin |
| 3145 |
Result := BEtoN(PLongWord(GetDataAdr + iPosition)[0]); |
| 3146 |
CharLenInBytes := SizeOf(LongWord); |
| 3147 |
end |
| 3148 |
else |
| 3149 |
CharLenInBytes := 0; |
| 3150 |
|
| 3151 |
veUtf32le: |
| 3152 |
if iPosition + SizeOf(LongWord) - 1 < FHighLimit then |
| 3153 |
begin |
| 3154 |
Result := LEtoN(PLongWord(GetDataAdr + iPosition)[0]); |
| 3155 |
CharLenInBytes := SizeOf(LongWord); |
| 3156 |
end |
| 3157 |
else |
| 3158 |
CharLenInBytes := 0; |
| 3159 |
|
| 3160 |
veCp932, // Unsupported variable-width encodings |
| 3161 |
veCp936, // TODO: Add cp932, cp936, cp949, cp950 encoding support |
| 3162 |
veCp949, |
| 3163 |
veCp950: |
| 3164 |
if iPosition < FHighLimit then |
| 3165 |
begin |
| 3166 |
Result := PByte(GetDataAdr)[iPosition]; |
| 3167 |
CharLenInBytes := 1; |
| 3168 |
end |
| 3169 |
else |
| 3170 |
CharLenInBytes := 0; |
| 3171 |
|
| 3172 |
else |
| 3173 |
raise Exception.Create('Unsupported viewer encoding'); |
| 3174 |
end; |
| 3175 |
end; |
| 3176 |
|
| 3177 |
function TViewerControl.GetPrevCharAsAscii(const iPosition: PtrInt; out CharLenInBytes: Integer): Cardinal; |
| 3178 |
var |
| 3179 |
u1, u2: Word; |
| 3180 |
InvalidCharLen: Integer; |
| 3181 |
begin |
| 3182 |
Result := 0; |
| 3183 |
case FEncoding of |
| 3184 |
veUtf8, veUtf8bom: |
| 3185 |
begin |
| 3186 |
if iPosition > FLowLimit then |
| 3187 |
begin |
| 3188 |
CharLenInBytes := SafeUTF8PrevCharLen(GetDataAdr + iPosition, |
| 3189 |
iPosition - FLowLimit, |
| 3190 |
InvalidCharLen); |
| 3191 |
|
| 3192 |
// It's enough to only return Ascii. |
| 3193 |
if CharLenInBytes = 1 then |
| 3194 |
Result := PByte(GetDataAdr)[iPosition - 1]; |
| 3195 |
|
| 3196 |
// Full conversion: |
| 3197 |
// Result := UTF8CodepointToUnicode(PAnsiChar(GetDataAdr + iPosition - CharLenInBytes), CharLenInBytes); |
| 3198 |
end |
| 3199 |
else |
| 3200 |
CharLenInBytes := 0; |
| 3201 |
end; |
| 3202 |
|
| 3203 |
veAnsi, veOem, |
| 3204 |
veCp1250..veCp874, |
| 3205 |
veIso88591, |
| 3206 |
veIso88592, |
| 3207 |
veKoi8r, |
| 3208 |
veKoi8u, |
| 3209 |
veKoi8ru: |
| 3210 |
if iPosition > FLowLimit then |
| 3211 |
begin |
| 3212 |
Result := PByte(GetDataAdr + iPosition)[-1]; |
| 3213 |
CharLenInBytes := 1; |
| 3214 |
end |
| 3215 |
else |
| 3216 |
CharLenInBytes := 0; |
| 3217 |
|
| 3218 |
veUcs2be: |
| 3219 |
if iPosition >= FLowLimit + SizeOf(Word) then |
| 3220 |
begin |
| 3221 |
Result := BEtoN(PWord(GetDataAdr + iPosition)[-1]); |
| 3222 |
CharLenInBytes := SizeOf(Word); |
| 3223 |
end |
| 3224 |
else |
| 3225 |
CharLenInBytes := 0; |
| 3226 |
|
| 3227 |
veUcs2le: |
| 3228 |
if iPosition >= FLowLimit + SizeOf(Word) then |
| 3229 |
begin |
| 3230 |
Result := LEtoN(PWord(GetDataAdr + iPosition)[-1]); |
| 3231 |
CharLenInBytes := SizeOf(Word); |
| 3232 |
end |
| 3233 |
else |
| 3234 |
CharLenInBytes := 0; |
| 3235 |
|
| 3236 |
veUtf16be: |
| 3237 |
if iPosition >= FLowLimit + SizeOf(Word) then |
| 3238 |
begin |
| 3239 |
u1 := BEtoN(PWord(GetDataAdr + iPosition)[-1]); |
| 3240 |
CharLenInBytes := UTF16CharacterLength(@u1); |
| 3241 |
if CharLenInBytes = 1 then |
| 3242 |
begin |
| 3243 |
Result := u1; |
| 3244 |
end |
| 3245 |
else if iPosition >= FLowLimit + SizeOf(Word) * CharLenInBytes then |
| 3246 |
begin |
| 3247 |
u2 := BEtoN(PWord(GetDataAdr + iPosition)[-2]); |
| 3248 |
// u2 is the first, u1 is the second value of the pair |
| 3249 |
Result := utf16PairToUnicode(u2, u1); |
| 3250 |
end; |
| 3251 |
|
| 3252 |
CharLenInBytes := CharLenInBytes * SizeOf(Word); |
| 3253 |
end |
| 3254 |
else |
| 3255 |
CharLenInBytes := 0; |
| 3256 |
|
| 3257 |
veUtf16le: |
| 3258 |
if iPosition >= FLowLimit + SizeOf(Word) then |
| 3259 |
begin |
| 3260 |
u1 := LEtoN(PWord(GetDataAdr + iPosition)[-1]); |
| 3261 |
CharLenInBytes := UTF16CharacterLength(@u1); |
| 3262 |
if CharLenInBytes = 1 then |
| 3263 |
begin |
| 3264 |
Result := u1; |
| 3265 |
end |
| 3266 |
else if iPosition >= FLowLimit + SizeOf(Word) * CharLenInBytes then |
| 3267 |
begin |
| 3268 |
u2 := LEtoN(PWord(GetDataAdr + iPosition)[-2]); |
| 3269 |
// u2 is the first, u1 is the second value of the pair |
| 3270 |
Result := utf16PairToUnicode(u2, u1); |
| 3271 |
end; |
| 3272 |
|
| 3273 |
CharLenInBytes := CharLenInBytes * SizeOf(Word); |
| 3274 |
end |
| 3275 |
else |
| 3276 |
CharLenInBytes := 0; |
| 3277 |
|
| 3278 |
veUtf32be: |
| 3279 |
if iPosition >= FLowLimit + SizeOf(LongWord) then |
| 3280 |
begin |
| 3281 |
Result := BEtoN(PLongWord(GetDataAdr + iPosition)[-1]); |
| 3282 |
CharLenInBytes := SizeOf(LongWord); |
| 3283 |
end |
| 3284 |
else |
| 3285 |
CharLenInBytes := 0; |
| 3286 |
|
| 3287 |
veUtf32le: |
| 3288 |
if iPosition >= FLowLimit + SizeOf(LongWord) then |
| 3289 |
begin |
| 3290 |
Result := LEtoN(PLongWord(GetDataAdr + iPosition)[-1]); |
| 3291 |
CharLenInBytes := SizeOf(LongWord); |
| 3292 |
end |
| 3293 |
else |
| 3294 |
CharLenInBytes := 0; |
| 3295 |
|
| 3296 |
veCp932, // Unsupported variable-width encodings |
| 3297 |
veCp936, // TODO: Add cp932, cp936, cp949, cp950 encoding support |
| 3298 |
veCp949, |
| 3299 |
veCp950: |
| 3300 |
if iPosition > FLowLimit then |
| 3301 |
begin |
| 3302 |
Result := PByte(GetDataAdr + iPosition)[-1]; |
| 3303 |
CharLenInBytes := 1; |
| 3304 |
end |
| 3305 |
else |
| 3306 |
CharLenInBytes := 0; |
| 3307 |
|
| 3308 |
else |
| 3309 |
raise Exception.Create('Unsupported viewer encoding'); |
| 3310 |
end; |
| 3311 |
end; |
| 3312 |
|
| 3313 |
function TViewerControl.GetNextCharAsUtf8(const iPosition: PtrInt; out CharLenInBytes: Integer): String; |
| 3314 |
var |
| 3315 |
u1: Word; |
| 3316 |
s: string; |
| 3317 |
InvalidCharLen: Integer; |
| 3318 |
begin |
| 3319 |
Result := ''; |
| 3320 |
case FEncoding of |
| 3321 |
veUtf8, veUtf8bom: |
| 3322 |
CharLenInBytes := SafeUTF8NextCharLen(GetDataAdr + iPosition, |
| 3323 |
FHighLimit - iPosition, |
| 3324 |
InvalidCharLen); |
| 3325 |
veAnsi, veOem, |
| 3326 |
veCp1250..veCp874, |
| 3327 |
veIso88591, |
| 3328 |
veIso88592, |
| 3329 |
veKoi8r, |
| 3330 |
veKoi8u, |
| 3331 |
veKoi8ru: |
| 3332 |
CharLenInBytes := 1; |
| 3333 |
veUcs2be, veUcs2le: |
| 3334 |
CharLenInBytes := 2; |
| 3335 |
veUtf16be: |
| 3336 |
if iPosition + SizeOf(Word) - 1 < FHighLimit then |
| 3337 |
begin |
| 3338 |
u1 := BEtoN(PWord(GetDataAdr + iPosition)[0]); |
| 3339 |
CharLenInBytes := UTF16CharacterLength(@u1) * SizeOf(Word); |
| 3340 |
end |
| 3341 |
else |
| 3342 |
CharLenInBytes := 0; |
| 3343 |
veUtf16le: |
| 3344 |
if iPosition + SizeOf(Word) - 1 < FHighLimit then |
| 3345 |
begin |
| 3346 |
u1 := LEtoN(PWord(GetDataAdr + iPosition)[0]); |
| 3347 |
CharLenInBytes := UTF16CharacterLength(@u1) * SizeOf(Word); |
| 3348 |
end |
| 3349 |
else |
| 3350 |
CharLenInBytes := 0; |
| 3351 |
veUtf32be, veUtf32le: |
| 3352 |
CharLenInBytes := 4; |
| 3353 |
|
| 3354 |
veCp932, // Unsupported variable-width encodings |
| 3355 |
veCp936, // TODO: Add cp932, cp936, cp949, cp950 encoding support |
| 3356 |
veCp949, |
| 3357 |
veCp950: |
| 3358 |
CharLenInBytes := 1; |
| 3359 |
|
| 3360 |
else |
| 3361 |
raise Exception.Create('Unsupported viewer encoding'); |
| 3362 |
end; |
| 3363 |
|
| 3364 |
if (CharLenInBytes > 0) and (iPosition + CharLenInBytes - 1 < FHighLimit) then |
| 3365 |
begin |
| 3366 |
SetString(s, GetDataAdr + iPosition, CharLenInBytes); |
| 3367 |
Result := ConvertToUTF8(s); |
| 3368 |
end |
| 3369 |
else |
| 3370 |
Result := ''; |
| 3371 |
end; |
| 3372 |
|
| 3373 |
function TViewerControl.ConvertToUTF8(const sText: AnsiString): String; |
| 3374 |
begin |
| 3375 |
if FEncoding = veAutoDetect then |
| 3376 |
FEncoding := DetectEncoding; // Force detect encoding. |
| 3377 |
|
| 3378 |
case FEncoding of |
| 3379 |
veAutoDetect: ; |
| 3380 |
veAnsi: |
| 3381 |
Result := CeAnsiToUtf8(sText); |
| 3382 |
veOem: |
| 3383 |
Result := CeOemToUtf8(sText); |
| 3384 |
veUtf8, veUtf8bom: |
| 3385 |
Result := Utf8ReplaceBroken(sText); |
| 3386 |
veUtf16be: |
| 3387 |
Result := Utf16BEToUtf8(sText); |
| 3388 |
veUtf16le: |
| 3389 |
Result := Utf16LEToUtf8(sText); |
| 3390 |
veUtf32be: |
| 3391 |
Result := Utf32BEToUtf8(sText); |
| 3392 |
veUtf32le: |
| 3393 |
Result := Utf32LEToUtf8(sText); |
| 3394 |
else |
| 3395 |
Result := LConvEncoding.ConvertEncoding(sText, |
| 3396 |
ViewerEncodingsNames[FEncoding], EncodingUTF8); |
| 3397 |
end; |
| 3398 |
end; |
| 3399 |
|
| 3400 |
function TViewerControl.ConvertFromUTF8(const sText: String): AnsiString; |
| 3401 |
begin |
| 3402 |
if FEncoding = veAutoDetect then |
| 3403 |
FEncoding := DetectEncoding; // Force detect encoding. |
| 3404 |
|
| 3405 |
case FEncoding of |
| 3406 |
veAutoDetect: ; |
| 3407 |
veAnsi: |
| 3408 |
Result := CeUtf8ToAnsi(sText); |
| 3409 |
veOem: |
| 3410 |
Result := CeUtf8ToOem(sText); |
| 3411 |
veUtf8, veUtf8bom: |
| 3412 |
Result := sText; |
| 3413 |
veUtf16be: |
| 3414 |
Result := Utf8ToUtf16BE(sText); |
| 3415 |
veUtf16le: |
| 3416 |
Result := Utf8ToUtf16LE(sText); |
| 3417 |
veUtf32be: |
| 3418 |
Result := '';//Utf8ToUtf32BE(sText); |
| 3419 |
veUtf32le: |
| 3420 |
Result := '';//Utf8ToUtf32LE(sText); |
| 3421 |
else |
| 3422 |
Result := LConvEncoding.ConvertEncoding(sText, |
| 3423 |
EncodingUTF8, ViewerEncodingsNames[FEncoding]); |
| 3424 |
end; |
| 3425 |
end; |
| 3426 |
|
| 3427 |
function TViewerControl.IsVisible(const aPosition: PtrInt): Boolean; |
| 3428 |
var |
| 3429 |
StartPos: PtrInt; |
| 3430 |
CharLenInBytes: Integer; |
| 3431 |
begin |
| 3432 |
if IsFileOpen and (FLineList.Count > 0) then |
| 3433 |
begin |
| 3434 |
FVisibleOffset:= 0; |
| 3435 |
StartPos:= GetStartOfLine(aPosition); |
| 3436 |
// Calculate horizontal offset in symbols |
| 3437 |
while (StartPos < aPosition) do |
| 3438 |
begin |
| 3439 |
GetNextCharAsAscii(StartPos, CharLenInBytes); |
| 3440 |
Inc(StartPos, CharLenInBytes); |
| 3441 |
Inc(FVisibleOffset); |
| 3442 |
end; |
| 3443 |
|
| 3444 |
Result := (aPosition >= FLineList.Items[0]) and |
| 3445 |
(aPosition <= FLineList.Items[FLineList.Count - 1]) and |
| 3446 |
(FVisibleOffset >= FHPosition) and |
| 3447 |
(FVisibleOffset <= FHPosition + FTextWidth); |
| 3448 |
end |
| 3449 |
else |
| 3450 |
Result := False; |
| 3451 |
end; |
| 3452 |
|
| 3453 |
procedure TViewerControl.MakeVisible(const aPosition: PtrInt); |
| 3454 |
var |
| 3455 |
Offset: Integer; |
| 3456 |
LastLine: Boolean; |
| 3457 |
begin |
| 3458 |
if not IsVisible(aPosition) then |
| 3459 |
begin |
| 3460 |
SetPosition(aPosition); |
| 3461 |
Offset:= GetLinesTillEnd(aPosition, LastLine); |
| 3462 |
if (Offset > 4) and (LastLine = False) then Scroll(-4); |
| 3463 |
|
| 3464 |
Update; |
| 3465 |
|
| 3466 |
if FViewerControlMode = vcmText then |
| 3467 |
begin |
| 3468 |
if (FVisibleOffset < FHPosition) or |
| 3469 |
(FVisibleOffset > FHPosition + FTextWidth) then |
| 3470 |
begin |
| 3471 |
SetHPosition(FVisibleOffset); |
| 3472 |
HScroll(-1); |
| 3473 |
end; |
| 3474 |
end; |
| 3475 |
end; |
| 3476 |
end; |
| 3477 |
|
| 3478 |
procedure TViewerControl.ScrollBarVertScroll(Sender: TObject; |
| 3479 |
ScrollCode: TScrollCode; var ScrollPos: Integer); |
| 3480 |
begin |
| 3481 |
FUpdateScrollBarPos := False; |
| 3482 |
case ScrollCode of |
| 3483 |
scLineUp: Scroll(-1); |
| 3484 |
scLineDown: Scroll(1); |
| 3485 |
scPageUp: PageUp; |
| 3486 |
scPageDown: PageDown; |
| 3487 |
scTop: GoHome; |
| 3488 |
scBottom: GoEnd; |
| 3489 |
scTrack, |
| 3490 |
scPosition: |
| 3491 |
begin |
| 3492 |
// This check helps avoiding loops if changing ScrollPos below |
| 3493 |
// triggers another scPosition message. |
| 3494 |
if (ScrollCode = scTrack) or (ScrollPos <> FScrollBarPosition) then |
| 3495 |
begin |
| 3496 |
if ScrollPos = 0 then |
| 3497 |
GoHome |
| 3498 |
else if ScrollPos = 100 then |
| 3499 |
GoEnd |
| 3500 |
else |
| 3501 |
Percent := ScrollPos; |
| 3502 |
end; |
| 3503 |
end; |
| 3504 |
scEndScroll: |
| 3505 |
begin |
| 3506 |
end; |
| 3507 |
end; |
| 3508 |
|
| 3509 |
ScrollPos := FScrollBarPosition; |
| 3510 |
FUpdateScrollBarPos := True; |
| 3511 |
end; |
| 3512 |
|
| 3513 |
procedure TViewerControl.ScrollBarHorzScroll(Sender: TObject; |
| 3514 |
ScrollCode: TScrollCode; var ScrollPos: Integer); |
| 3515 |
begin |
| 3516 |
FUpdateScrollBarPos := False; |
| 3517 |
case ScrollCode of |
| 3518 |
scLineUp: HScroll(-1); |
| 3519 |
scLineDown: HScroll(1); |
| 3520 |
scPageUp: HPageUp; |
| 3521 |
scPageDown: HPageDown; |
| 3522 |
scTop: HGoHome; |
| 3523 |
scBottom: HGoEnd; |
| 3524 |
scTrack, |
| 3525 |
scPosition: |
| 3526 |
begin |
| 3527 |
// This check helps avoiding loops if changing ScrollPos below |
| 3528 |
// triggers another scPosition message. |
| 3529 |
if (ScrollCode = scTrack) or (ScrollPos <> FHScrollBarPosition) then |
| 3530 |
begin |
| 3531 |
if ScrollPos = 0 then |
| 3532 |
HGoHome |
| 3533 |
else if ScrollPos = 100 then |
| 3534 |
HGoEnd |
| 3535 |
else |
| 3536 |
HScroll((FHLowEnd - FTextWidth) * ScrollPos div 100 - FHPosition); |
| 3537 |
end; |
| 3538 |
end; |
| 3539 |
scEndScroll: |
| 3540 |
begin |
| 3541 |
end; |
| 3542 |
end; |
| 3543 |
ScrollPos := FHScrollBarPosition; |
| 3544 |
FUpdateScrollBarPos := True; |
| 3545 |
end; |
| 3546 |
|
| 3547 |
procedure TViewerControl.UpdateScrollbars; |
| 3548 |
begin |
| 3549 |
FScrollBarVert.LargeChange := GetClientHeightInLines - 1; |
| 3550 |
case Mode of |
| 3551 |
vcmBin, vcmHex: |
| 3552 |
begin |
| 3553 |
//FScrollBarVert.PageSize := |
| 3554 |
// ((FHighLimit div cHexWidth - GetClientHeightInLines) div 100); |
| 3555 |
end |
| 3556 |
else |
| 3557 |
FScrollBarVert.PageSize := 1; |
| 3558 |
end; |
| 3559 |
FScrollBarHorz.Visible:= (FViewerControlMode = vcmText); |
| 3560 |
end; |
| 3561 |
|
| 3562 |
procedure TViewerControl.ViewerResize(Sender: TObject); |
| 3563 |
begin |
| 3564 |
UpdateScrollbars; |
| 3565 |
// Force recalculating position. |
| 3566 |
SetPosition(FPosition); |
| 3567 |
SetHPosition(FHPosition); |
| 3568 |
end; |
| 3569 |
|
| 3570 |
procedure TViewerControl.ReReadFile; |
| 3571 |
begin |
| 3572 |
FBlockBeg := 0; |
| 3573 |
FBlockEnd := 0; |
| 3574 |
|
| 3575 |
FBOMLength := GetBomLength; |
| 3576 |
UpdateLimits; |
| 3577 |
|
| 3578 |
UpdateScrollbars; |
| 3579 |
Invalidate; |
| 3580 |
end; |
| 3581 |
|
| 3582 |
function TViewerControl.IsFileOpen: Boolean; |
| 3583 |
begin |
| 3584 |
Result := Assigned(FMappedFile); |
| 3585 |
end; |
| 3586 |
|
| 3587 |
function TViewerControl.DetectEncoding: TViewerEncoding; |
| 3588 |
var |
| 3589 |
DetectStringLength: Integer = 4096; // take first 4kB of the file to detect encoding |
| 3590 |
DetectString: String; |
| 3591 |
DetectedEncodingName: String; |
| 3592 |
Enc: TViewerEncoding; |
| 3593 |
begin |
| 3594 |
if IsFileOpen then |
| 3595 |
begin |
| 3596 |
// Default to Ansi in case encoding cannot be detected or is unsupported. |
| 3597 |
Result := veAnsi; |
| 3598 |
|
| 3599 |
if FFileSize < DetectStringLength then |
| 3600 |
DetectStringLength := FFileSize; |
| 3601 |
|
| 3602 |
SetString(DetectString, PAnsiChar(FMappedFile), DetectStringLength); |
| 3603 |
|
| 3604 |
if Assigned(FOnGuessEncoding) then |
| 3605 |
DetectedEncodingName := FOnGuessEncoding(DetectString) |
| 3606 |
else |
| 3607 |
DetectedEncodingName := LConvEncoding.GuessEncoding(DetectString); |
| 3608 |
|
| 3609 |
if DetectedEncodingName <> '' then |
| 3610 |
begin |
| 3611 |
DetectedEncodingName := NormalizeEncoding(DetectedEncodingName); |
| 3612 |
|
| 3613 |
// Map UCS-2 to UTF-16. |
| 3614 |
if DetectedEncodingName = 'ucs2le' then |
| 3615 |
DetectedEncodingName := 'utf16le' |
| 3616 |
else if DetectedEncodingName = 'ucs2be' then |
| 3617 |
DetectedEncodingName := 'utf16be'; |
| 3618 |
|
| 3619 |
for Enc := Low(TViewerEncoding) to High(TViewerEncoding) do |
| 3620 |
begin |
| 3621 |
if NormalizeEncoding(ViewerEncodingsNames[Enc]) = DetectedEncodingName then |
| 3622 |
begin |
| 3623 |
Result := Enc; |
| 3624 |
break; |
| 3625 |
end; |
| 3626 |
end; |
| 3627 |
end; |
| 3628 |
end |
| 3629 |
else |
| 3630 |
Result := veAutoDetect; |
| 3631 |
end; |
| 3632 |
|
| 3633 |
procedure TViewerControl.GetSupportedEncodings(List: TStrings); |
| 3634 |
var |
| 3635 |
Enc: TViewerEncoding; |
| 3636 |
begin |
| 3637 |
for Enc := Low(TViewerEncoding) to High(TViewerEncoding) do |
| 3638 |
List.Add(ViewerEncodingsNames[Enc]); |
| 3639 |
end; |
| 3640 |
|
| 3641 |
function TViewerControl.GetBomLength: Integer; |
| 3642 |
begin |
| 3643 |
Result := 0; |
| 3644 |
|
| 3645 |
case FEncoding of |
| 3646 |
veUtf8, veUtf8bom: |
| 3647 |
if (FFileSize >= 3) and |
| 3648 |
(PByte(FMappedFile)[0] = $EF) and |
| 3649 |
(PByte(FMappedFile)[1] = $BB) and |
| 3650 |
(PByte(FMappedFile)[2] = $BF) then |
| 3651 |
begin |
| 3652 |
Result := 3; |
| 3653 |
end; |
| 3654 |
|
| 3655 |
veUcs2be, veUtf16be: |
| 3656 |
if (FFileSize >= 2) and |
| 3657 |
(PByte(FMappedFile)[0] = $FE) and |
| 3658 |
(PByte(FMappedFile)[1] = $FF) then |
| 3659 |
begin |
| 3660 |
Result := 2; |
| 3661 |
end; |
| 3662 |
|
| 3663 |
veUcs2le, veUtf16le: |
| 3664 |
if (FFileSize >= 2) and |
| 3665 |
(PByte(FMappedFile)[0] = $FF) and |
| 3666 |
(PByte(FMappedFile)[1] = $FE) then |
| 3667 |
begin |
| 3668 |
Result := 2; |
| 3669 |
end; |
| 3670 |
|
| 3671 |
veUtf32be: |
| 3672 |
if (FFileSize >= 4) and |
| 3673 |
(PByte(FMappedFile)[0] = $00) and |
| 3674 |
(PByte(FMappedFile)[1] = $00) and |
| 3675 |
(PByte(FMappedFile)[2] = $FE) and |
| 3676 |
(PByte(FMappedFile)[3] = $FF) then |
| 3677 |
begin |
| 3678 |
Result := 4; |
| 3679 |
end; |
| 3680 |
|
| 3681 |
veUtf32le: |
| 3682 |
if (FFileSize >= 4) and |
| 3683 |
(PByte(FMappedFile)[0] = $00) and |
| 3684 |
(PByte(FMappedFile)[1] = $00) and |
| 3685 |
(PByte(FMappedFile)[2] = $FF) and |
| 3686 |
(PByte(FMappedFile)[3] = $FE) then |
| 3687 |
begin |
| 3688 |
Result := 4; |
| 3689 |
end; |
| 3690 |
end; |
| 3691 |
end; |
| 3692 |
|
| 3693 |
procedure TViewerControl.UpdateLimits; |
| 3694 |
begin |
| 3695 |
if FEncoding = veAutoDetect then |
| 3696 |
FEncoding := DetectEncoding; |
| 3697 |
|
| 3698 |
FBOMLength := GetBomLength; |
| 3699 |
|
| 3700 |
case FViewerControlMode of |
| 3701 |
vcmText, vcmWrap, vcmBook: |
| 3702 |
begin |
| 3703 |
FLowLimit := 0; |
| 3704 |
FHighLimit := FFileSize - FBOMLength; |
| 3705 |
end; |
| 3706 |
else |
| 3707 |
begin |
| 3708 |
FLowLimit := 0; |
| 3709 |
FHighLimit := FFileSize; |
| 3710 |
end; |
| 3711 |
end; |
| 3712 |
end; |
| 3713 |
|
| 3714 |
procedure TViewerControl.UpdateSelection; |
| 3715 |
|
| 3716 |
procedure Check(var aPosition: PtrInt; Backwards: Boolean); |
| 3717 |
var |
| 3718 |
CharStart: Pointer; |
| 3719 |
begin |
| 3720 |
case FEncoding of |
| 3721 |
veUtf8, veUtf8bom: |
| 3722 |
begin |
| 3723 |
if not Backwards then |
| 3724 |
begin |
| 3725 |
CharStart := SafeUTF8NextCharStart(GetDataAdr + aPosition, |
| 3726 |
FHighLimit - aPosition); |
| 3727 |
if Assigned(CharStart) then |
| 3728 |
aPosition := CharStart - GetDataAdr |
| 3729 |
else |
| 3730 |
aPosition := 0; |
| 3731 |
end |
| 3732 |
else |
| 3733 |
begin |
| 3734 |
CharStart := SafeUTF8PrevCharEnd(GetDataAdr + aPosition, |
| 3735 |
aPosition - FLowLimit); |
| 3736 |
if Assigned(CharStart) then |
| 3737 |
aPosition := CharStart - GetDataAdr |
| 3738 |
else |
| 3739 |
aPosition := 0; |
| 3740 |
end; |
| 3741 |
end; |
| 3742 |
|
| 3743 |
veAnsi, veOem, |
| 3744 |
veCp1250..veCp874, |
| 3745 |
veIso88591, |
| 3746 |
veIso88592, |
| 3747 |
veKoi8r, |
| 3748 |
veKoi8u, |
| 3749 |
veKoi8ru: |
| 3750 |
; // any position allowed |
| 3751 |
|
| 3752 |
veUcs2be, veUcs2le: |
| 3753 |
aPosition := ((aPosition - FLowLimit) and not 1) + FLowLimit; |
| 3754 |
veUtf16be, veUtf16le: |
| 3755 |
// todo: check if not in the middle of utf-16 character |
| 3756 |
aPosition := ((aPosition - FLowLimit) and not 1) + FLowLimit; |
| 3757 |
veUtf32be, veUtf32le: |
| 3758 |
aPosition := ((aPosition - FLowLimit) and not 3) + FLowLimit; |
| 3759 |
|
| 3760 |
veCp932, // Unsupported variable-width encodings |
| 3761 |
veCp936, // TODO: Add cp932, cp936, cp949, cp950 encoding support |
| 3762 |
veCp949, |
| 3763 |
veCp950: |
| 3764 |
; |
| 3765 |
|
| 3766 |
else |
| 3767 |
raise Exception.Create('Unsupported viewer encoding'); |
| 3768 |
end; |
| 3769 |
end; |
| 3770 |
|
| 3771 |
begin |
| 3772 |
if (FBlockBeg < FLowLimit) or (FBlockBeg >= FHighLimit) or |
| 3773 |
(FBlockEnd < FLowLimit) or (FBlockEnd >= FHighLimit) then |
| 3774 |
begin |
| 3775 |
FBlockBeg := FLowLimit; |
| 3776 |
FBlockEnd := FLowLimit; |
| 3777 |
end |
| 3778 |
else |
| 3779 |
begin |
| 3780 |
case FViewerControlMode of |
| 3781 |
vcmText, vcmWrap, vcmBook: |
| 3782 |
begin |
| 3783 |
Check(FBlockBeg, False); |
| 3784 |
Check(FBlockEnd, True); |
| 3785 |
|
| 3786 |
if (FBlockBeg < FLowLimit) or (FBlockBeg >= FHighLimit) or |
| 3787 |
(FBlockEnd < FLowLimit) or (FBlockEnd >= FHighLimit) or |
| 3788 |
(FBlockEnd < FBlockBeg) then |
| 3789 |
begin |
| 3790 |
FBlockBeg := FLowLimit; |
| 3791 |
FBlockEnd := FLowLimit; |
| 3792 |
end; |
| 3793 |
end; |
| 3794 |
// In non-text modes any selection is valid. |
| 3795 |
end; |
| 3796 |
end; |
| 3797 |
end; |
| 3798 |
|
| 3799 |
function TViewerControl.FindUtf8Text(iStartPos: PtrInt; const sSearchText: String; |
| 3800 |
bCaseSensitive: Boolean; bSearchBackwards: Boolean): PtrInt; |
| 3801 |
var |
| 3802 |
SearchTextLength: Integer; |
| 3803 |
sSearchChars: array of String; |
| 3804 |
pCurrentAddr, pEndAddr: PtrInt; |
| 3805 |
i, charLen: Integer; |
| 3806 |
|
| 3807 |
function sPos2(pAdr: PtrInt):Boolean; |
| 3808 |
var |
| 3809 |
curChr:String; |
| 3810 |
i, charLen: Integer; |
| 3811 |
begin |
| 3812 |
Result := False; |
| 3813 |
for i := 0 to SearchTextLength-1 do |
| 3814 |
begin |
| 3815 |
curChr:=GetNextCharAsUtf8(pAdr,charLen); |
| 3816 |
case bCaseSensitive of |
| 3817 |
False: if UTF8UpperCase(curChr) <> UTF8UpperCase(sSearchChars[i]) then Exit; |
| 3818 |
True : if curChr <> sSearchChars[i] then Exit; |
| 3819 |
end; |
| 3820 |
if charLen>0 then |
| 3821 |
pAdr:=pAdr+charLen |
| 3822 |
else |
| 3823 |
Inc(pAdr); |
| 3824 |
end; |
| 3825 |
Result:=True; |
| 3826 |
end; |
| 3827 |
|
| 3828 |
begin |
| 3829 |
Result := PtrInt(-1); |
| 3830 |
SearchTextLength := UTF8Length(sSearchText); |
| 3831 |
if (SearchTextLength <= 0) then |
| 3832 |
Exit; |
| 3833 |
|
| 3834 |
setLength(sSearchChars,SearchTextLength); |
| 3835 |
for i:=1 to SearchTextLength do |
| 3836 |
sSearchChars[i-1]:=UTF8Copy(sSearchText,i,1); |
| 3837 |
|
| 3838 |
|
| 3839 |
pCurrentAddr := iStartPos; |
| 3840 |
pEndAddr := FHighLimit - Length(ConvertFromUTF8(sSearchText)); |
| 3841 |
|
| 3842 |
if bSearchBackwards and (pCurrentAddr > pEndAddr) then |
| 3843 |
// Move to the first possible position for searching backwards. |
| 3844 |
pCurrentAddr := pEndAddr; |
| 3845 |
|
| 3846 |
if (pEndAddr < 0) or (pCurrentAddr < 0) or (pCurrentAddr > pEndAddr) then |
| 3847 |
Exit; |
| 3848 |
|
| 3849 |
while True do |
| 3850 |
begin |
| 3851 |
if (pCurrentAddr > pEndAddr) or (pCurrentAddr < 0) then |
| 3852 |
Exit; |
| 3853 |
|
| 3854 |
if sPos2(pCurrentAddr) then |
| 3855 |
begin |
| 3856 |
Result := pCurrentAddr; |
| 3857 |
Exit; |
| 3858 |
end; |
| 3859 |
|
| 3860 |
case bSearchBackwards of |
| 3861 |
False: |
| 3862 |
begin |
| 3863 |
GetNextCharAsUtf8(pCurrentAddr,charLen); |
| 3864 |
if charLen>0 then |
| 3865 |
pCurrentAddr:=pCurrentAddr+charLen |
| 3866 |
else |
| 3867 |
Inc(pCurrentAddr); |
| 3868 |
end; |
| 3869 |
True : Dec(pCurrentAddr); |
| 3870 |
end; |
| 3871 |
end; |
| 3872 |
end; |
| 3873 |
|
| 3874 |
procedure TViewerControl.ResetEncoding; |
| 3875 |
begin |
| 3876 |
FEncoding:= veAutoDetect; |
| 3877 |
end; |
| 3878 |
|
| 3879 |
procedure Register; |
| 3880 |
begin |
| 3881 |
RegisterComponents('SeksiCmd', [TViewerControl]); |
| 3882 |
end; |
| 3883 |
|
| 3884 |
end. |
| 3885 |
|