1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
|
unit LazNumEdit;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Math, StdCtrls, LCLType, Controls, Clipbrd, StrUtils;
(* Since generics can not override anythning, the specialized class must do this
and redirect the calls
*)
type
TLazIntegerEditBaseChangeEvent = procedure(ASender: TObject; ACurrentBase: Integer; var ANewBase: integer; var APrefix) of object;
{ TLazIntegerEditGen }
generic TLazIntegerEditGen<T: TCustomEdit> = class(T)
protected const
Min_Limit = low(Int64);
Max_Limit = High(Int64);
private
FAllowMinus: Boolean;
FAllowPlus: Boolean;
FOnBaseChange: TLazIntegerEditBaseChangeEvent;
FDisplayBase: integer;
FBinIndicator: String;
FHexIndicator: String;
FOctIndicator: String;
FSetBinKeys: String;
FSetDecimalKeys: String;
FSetHexKeys: String;
FSetOctKeys: String;
FToggleBinKeys: String;
FToggleHexKeys: String;
FToggleOctKeys: String;
FValue: Int64;
FMinValue, FMaxValue: Int64;
FCurrentBasePrefix: string;
FCurrentPrefix: string;
FLastDecodeWasEmpty: Boolean;
function GetCurrentValue: Int64;
function GetValid: Boolean;
procedure SetBinIndicator(AValue: String);
procedure SetDisplayBase(AValue: integer);
procedure SetHexIndicator(AValue: String);
procedure SetMaxValue(AValue: Int64);
procedure SetMinValue(AValue: Int64);
procedure SetOctIndicator(AValue: String);
procedure SetSetDecimalKeys(AValue: String);
procedure SetValue(AValue: Int64);
procedure UpdateText(ANewText: string; AnAdjustPos: Integer = 0; AnAdjustOffs: Integer = 0; AWasEmpty: boolean = False);
function ReEncodeText(ACheckLimit: boolean; ANewBase: integer=-1; ANewPrefix: String=''): Int64;
function DecodeText(out APrefix: String; out AVal: Int64; ACheckLimit: boolean): Boolean;
function EncodeText(APrefix: String; AVal: Int64; APrefixOnly: Boolean = False): String;
protected
procedure _KeyDown(var Key: Word; Shift: TShiftState); //override;
procedure _KeyPress(var Key: Char); //override;
procedure _Utf8KeyPress(var UTF8Key: TUTF8Char); //override;
procedure _InitializeWnd; //override;
function _RealGetText: TCaption; //override;
procedure _FinalizeWnd; //override;
procedure _DoExit; //override;
procedure _EditingDone; //override;
procedure _Init; //create; override;
public
property Value: Int64 read FValue write SetValue;
property CurrentValue: Int64 read GetCurrentValue; // while editing // before EditingDone or focus shift
property Valid: Boolean read GetValid; // CurrentValue is valid
property MinValue: Int64 read FMinValue write SetMinValue;
property MaxValue: Int64 read FMaxValue write SetMaxValue;
property DisplayBase: integer read FDisplayBase write SetDisplayBase default 10;
property SetDecimalKeys: String read FSetDecimalKeys write SetSetDecimalKeys;
property HexIndicator: String read FHexIndicator write SetHexIndicator;
property SetHexKeys: String read FSetHexKeys write FSetHexKeys;
property ToggleHexKeys: String read FToggleHexKeys write FToggleHexKeys; // between hex/dec
property OctIndicator: String read FOctIndicator write SetOctIndicator;
property SetOctKeys: String read FSetOctKeys write FSetOctKeys;
property ToggleOctKeys: String read FToggleOctKeys write FToggleOctKeys;
property BinIndicator: String read FBinIndicator write SetBinIndicator;
property SetBinKeys: String read FSetBinKeys write FSetBinKeys;
property ToggleBinKeys: String read FToggleBinKeys write FToggleBinKeys;
property OnBaseChange: TLazIntegerEditBaseChangeEvent read FOnBaseChange write FOnBaseChange;
property AllowMinus: Boolean read FAllowMinus write FAllowMinus default True;
property AllowPlus: Boolean read FAllowPlus write FAllowPlus default True;
//property DisplayQWord: boolean;
end;
{ TLazIntegerEdit }
TLazIntegerEdit = class(specialize TLazIntegerEditGen<TCustomEdit>)
protected
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyPress(var Key: Char); override;
procedure Utf8KeyPress(var UTF8Key: TUTF8Char); override;
function RealGetText: TCaption; override;
procedure InitializeWnd; override;
procedure FinalizeWnd; override;
procedure DoExit; override;
procedure EditingDone; override;
//procedure DoEnter; override;
public
constructor Create(AOwner: TComponent); override;
published
property Value;
property MinValue;
property MaxValue;
property DisplayBase;
property SetDecimalKeys;
property HexIndicator;
property SetHexKeys;
property ToggleHexKeys;
property OctIndicator;
property SetOctKeys;
property ToggleOctKeys;
property BinIndicator;
property SetBinKeys;
property ToggleBinKeys;
property OnBaseChange;
property AllowMinus;
property AllowPlus;
public
property AutoSelected;
published
property Align;
property Alignment;
property Anchors;
property AutoSize;
property AutoSelect;
property BidiMode;
property BorderSpacing;
property BorderStyle;
property CharCase;
property Color;
property Constraints;
property DoubleBuffered;
property DragCursor;
property DragKind;
property DragMode;
property EchoMode;
property Enabled;
property Font;
property HideSelection;
property MaxLength;
property NumbersOnly;
property ParentBidiMode;
property ParentColor;
property ParentDoubleBuffered;
property ParentFont;
property ParentShowHint;
property PasswordChar;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabStop;
property TabOrder;
property Text;
property TextHint;
property Visible;
property OnChange;
property OnChangeBounds;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEditingDone;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnMouseWheel;
property OnMouseWheelDown;
property OnMouseWheelUp;
property OnResize;
property OnStartDrag;
property OnUTF8KeyPress;
end;
function Str2QWord(S: string; Base: Byte): QWord;
function QWord2Str(N: QWord; Base: Byte): string;
implementation
// from StrUtils.Dec2Numb / Numb2Dec
function QWord2Str(N: QWord; Base: Byte): string;
var
C: Integer;
Number: QWord;
begin
if N=0 then
Result:='0'
else
begin
Number:=N;
Result:='';
while Number>0 do
begin
C:=Number mod Base;
if C>9 then
C:=C+55
else
C:=C+48;
Result:=Chr(C)+Result;
Number:=Number div Base;
end;
end;
end;
{$PUSH}{$R+}{$Q+}
function Str2QWord(S: string; Base: Byte): QWord;
var
i, P: sizeint;
begin
i:=Length(S);
Result:=0;
S:=UpperCase(S);
P:=1;
while (i>=1) do
begin
if (S[i]>'@') then
Result:=Result+(Ord(S[i])-55)*P
else
Result:=Result+(Ord(S[i])-48)*P;
Dec(i);
P:=P*Base;
end;
end;
{$POP}
{ TLazIntegerEditGen }
function TLazIntegerEditGen.GetCurrentValue: Int64;
var
p: String;
begin
if HandleAllocated then begin
DecodeText(p, Result, False);
end
else
Result := FValue;
end;
function TLazIntegerEditGen.GetValid: Boolean;
var
p: String;
v: Int64;
begin
Result := DecodeText(p, v, False);
end;
procedure TLazIntegerEditGen.SetBinIndicator(AValue: String);
begin
if FBinIndicator = AValue then Exit;
FBinIndicator := AValue;
if FDisplayBase = 2 then
ReEncodeText(False, FDisplayBase, AValue);
end;
procedure TLazIntegerEditGen.SetDisplayBase(AValue: integer);
var
np: String;
begin
if FDisplayBase = AValue then
exit;
if (AValue < 2) then AValue := 2;
if (AValue > 35) then AValue := 35;
case AValue of
2: np := FBinIndicator;
8: np := FOctIndicator;
16: np := FHexIndicator;
else np := '';
end;
if FOnBaseChange <> nil then
FOnBaseChange(Self, FDisplayBase, AValue, np);
if (AValue = FDisplayBase) and (np = FCurrentBasePrefix) then
exit;
ReEncodeText(False, AValue, np);
end;
procedure TLazIntegerEditGen.SetHexIndicator(AValue: String);
begin
if FHexIndicator = AValue then Exit;
FHexIndicator := AValue;
if FDisplayBase = 16 then
ReEncodeText(False, FDisplayBase, AValue);
end;
procedure TLazIntegerEditGen.SetMaxValue(AValue: Int64);
begin
if FMaxValue = AValue then
exit;
FMaxValue := AValue;
FValue := ReEncodeText(True);
end;
procedure TLazIntegerEditGen.SetMinValue(AValue: Int64);
begin
if FMinValue = AValue then
exit;
FMinValue := AValue;
FValue := ReEncodeText(True);
end;
procedure TLazIntegerEditGen.SetOctIndicator(AValue: String);
begin
if FOctIndicator = AValue then Exit;
FOctIndicator := AValue;
if FDisplayBase = 86 then
ReEncodeText(False, FDisplayBase, AValue);
end;
procedure TLazIntegerEditGen.SetSetDecimalKeys(AValue: String);
begin
if FSetDecimalKeys = AValue then Exit;
FSetDecimalKeys := AValue;
end;
procedure TLazIntegerEditGen.SetValue(AValue: Int64);
begin
if AValue < FMinValue then
AValue := FMinValue
else
if AValue > FMaxValue then
AValue := FMaxValue;
if FValue = AValue then
exit;
FValue := AValue;
Text := EncodeText(FCurrentBasePrefix, FValue);
end;
procedure TLazIntegerEditGen.UpdateText(ANewText: string; AnAdjustPos: Integer;
AnAdjustOffs: Integer; AWasEmpty: boolean);
var
sb, se, m: Integer;
ToEnd, NoSel: Boolean;
begin
sb := SelStart;
se := sb+SelLength;
NoSel := se=sb;
ToEnd := se = Length(Text);
Text := ANewText;
if AnAdjustOffs <> 0 then begin
m := AnAdjustPos;
if AnAdjustOffs < 0 then m := m + AnAdjustOffs;
if (sb >= AnAdjustPos) and
not((sb=0) and ToEnd)
then
sb := Max(m, sb + AnAdjustOffs);
if se >= AnAdjustPos then
se := Max(m, se + AnAdjustOffs);
end;
if AWasEmpty or ToEnd then
se := Length(ANewText);
if NoSel then
sb := se;
SelStart := sb;
SelLength := se-sb;
end;
function TLazIntegerEditGen.ReEncodeText(ACheckLimit: boolean; ANewBase: integer;
ANewPrefix: String): Int64;
var
p: String;
x, o: Integer;
begin
DecodeText(p, Result, ACheckLimit);
x := 0;
o := 0;
if ANewBase > 1 then begin
x := Length(FCurrentBasePrefix);
o := Length(ANewPrefix) - x;
if x < 0 then inc(x);
FDisplayBase := ANewBase;
FCurrentBasePrefix := ANewPrefix;
p := ANewPrefix;
end;
UpdateText(EncodeText(p, Result), x, o, FLastDecodeWasEmpty);
end;
function TLazIntegerEditGen.DecodeText(out APrefix: String; out AVal: Int64; ACheckLimit: boolean
): Boolean;
var
s: String;
n: Boolean;
begin
Result := True;
APrefix := '';
AVal := 0;
s := RealGetText;
n := (s<>'') and (s[1]='-');
if (s<>'') and (s[1] in ['-', '+']) then
delete(s, 1, 1);
if (Length(s) >= Length(FCurrentBasePrefix)) and
(strlicomp(PChar(s), PChar(FCurrentBasePrefix), Length(FCurrentBasePrefix)) = 0)
then begin
APrefix := copy(s, 1, Length(FCurrentBasePrefix));
delete(s, 1, Length(FCurrentBasePrefix));
end;
FLastDecodeWasEmpty := s = '';
try
{$PUSH}{$R+}{$Q+}
if s <> '' then
AVal := Str2QWord(s, FDisplayBase);
if n then
AVal := -AVal;
{$POP}
except
Result := False;
if n then
AVal := Min_Limit
else
AVal := Max_Limit;
end;
if ACheckLimit then begin
if AVal < FMinValue then
AVal := FMinValue
else
if AVal > FMaxValue then
AVal := FMaxValue;
end;
end;
function TLazIntegerEditGen.EncodeText(APrefix: String; AVal: Int64; APrefixOnly: Boolean): String;
begin
Result := APrefix + QWord2Str(abs(AVal), FDisplayBase);
if APrefixOnly then
exit;
if AVal < 0 then
Result := '-' + Result;
end;
procedure TLazIntegerEditGen._KeyDown(var Key: Word; Shift: TShiftState);
var
s: TCaption;
PreB, PreE, SelB, SelE, l: Integer;
begin
inherited KeyDown(Key, Shift);
case Key of
VK_BACK: begin
s := Text;
PreB := 0;
if (s <> '') and (s[1] = '-') then PreB := 1;
PreE := PreB + Length(FCurrentBasePrefix);
SelB := SelStart;
l := SelLength;
SelE := SelB+l;
if ((l = 0) and (SelB <= PreE)) or
(SelE <= PreE)
then begin
if (SelB <= PreE) then
Key := 0;
end
else begin
if (SelB <= PreE) then begin
SelStart := PreE;
SelLength := SelE-PreE;
end;
end;
end;
VK_DELETE: begin
s := Text;
PreB := 0;
if (s <> '') and (s[1] = '-') then PreB := 1;
PreE := PreB + Length(FCurrentBasePrefix);
SelB := SelStart;
l := SelLength;
SelE := SelB+l;
if ((l = 0) and (SelB < PreE)) or
((l > 0) and (SelE <= PreE))
then begin
Key := 0;
end
else begin
if (SelB <= PreE) then begin
SelStart := PreE;
SelLength := SelE-PreE;
end;
end;
end;
end;
end;
procedure TLazIntegerEditGen._KeyPress(var Key: Char);
function CheckChar(c: Char): boolean;
begin
Result :=
(c >= '0') and
( (c <= chr(ord('0')+Min(10, FDisplayBase-1))) or
( (FDisplayBase > 10) and
( ((ord(c) and $DF) >= 65) and
((ord(c) and $DF) < 55 + FDisplayBase)
) ) );
end;
const
AllowedControlChars = [#8,#9,^A,^C,^Z];
var
Utf8Key: TUtf8Char;
p: String;
v: Int64;
e: integer;
s: TCaption;
sgn, SelS, SelE: Integer;
k: Char;
begin
inherited KeyPress(Key);
if ReadOnly then exit;
if key in AllowedControlChars then
exit;
s := Text;
if (s = '') or (s = '-') then begin
s := s + FCurrentBasePrefix;
if s <> '' then begin
Text := s;
SelStart := Length(s);
end;
end;
sgn := 0;
if (s <> '') and (s[1] = '-') then sgn := 1;
SelS := SelStart;
SelE := SelS + SelLength;
if (key = ^X) then begin
Clipboard.AsText := SelText;
if SelS < sgn + Length(FCurrentBasePrefix) then begin
SelStart := 0;
SelLength := SelE;
end;
SelText := '';
end;
if (key = ^V) then begin
if (SelS = 0) and (SelE = Length(s)) then begin
val(Clipboard.AsText, v, e);
if e = 0 then begin
FValue := v;
s := EncodeText(FCurrentBasePrefix, FValue);
Text := s;
SelStart := Length(s);
SelLength := 0;
end;
end
else
if (SelS >= sgn + Length(FCurrentBasePrefix))then begin
for k in Clipboard.AsText do
if not CheckChar(k) then begin
Key := #0;
exit;
end;
exit; // handle key
end;
Key := #0;
exit;
end;
if ( (SelS = 0) and (SelE = Length(s)) and CheckChar(Key) ) or
( (SelS = sgn) and (SelE = Length(s)-sgn) and CheckChar(Key) )
then begin
FDisplayBase := 10;
FCurrentBasePrefix := '';
exit;
end;
if (SelS >= sgn + Length(FCurrentBasePrefix)) and CheckChar(Key) then
exit;
if pos(Key, FToggleBinKeys) > 0 then begin
if DisplayBase = 2
then DisplayBase := 10
else DisplayBase := 2;
end
else
if pos(Key, FToggleOctKeys) > 0 then begin
if DisplayBase = 8
then DisplayBase := 10
else DisplayBase := 8;
end
else
if pos(Key, FToggleHexKeys) > 0 then begin
if DisplayBase = 16
then DisplayBase := 10
else DisplayBase := 16;
end
else if pos(Key, FSetDecimalKeys) > 0 then DisplayBase := 10
else if pos(Key, FSetBinKeys) > 0 then DisplayBase := 2
else if pos(Key, FSetOctKeys) > 0 then DisplayBase := 8
else if pos(Key, FSetHexKeys) > 0 then DisplayBase := 16
else
if FAllowMinus and (key = '-') then begin
DecodeText(p, v, False);
if v > 0 then
UpdateText(EncodeText(p, -v, FLastDecodeWasEmpty), 0, 1)
else
if v < 0 then
UpdateText(EncodeText(p, -v, FLastDecodeWasEmpty), 1, -1);
end
else
if FAllowPlus and (key = '+') then begin
DecodeText(p, v, False);
if v < 0 then
UpdateText(EncodeText(p, -v, FLastDecodeWasEmpty), 1, -1);
end;
Key := #0;
end;
procedure TLazIntegerEditGen._Utf8KeyPress(var UTF8Key: TUTF8Char);
begin
inherited Utf8KeyPress(UTF8Key);
if (Length(Utf8Key) > 1) then
UTF8Key := '';
end;
procedure TLazIntegerEditGen._InitializeWnd;
begin
inherited InitializeWnd;
Text := EncodeText(FCurrentBasePrefix, FValue);
end;
function TLazIntegerEditGen._RealGetText: TCaption;
begin
if HandleAllocated then
Result := inherited RealGetText
else
Result := EncodeText(FCurrentBasePrefix, FValue);
end;
procedure TLazIntegerEditGen._FinalizeWnd;
var
p: String;
begin
DecodeText(p, FValue, True);
inherited FinalizeWnd;
end;
procedure TLazIntegerEditGen._DoExit;
begin
FValue := ReEncodeText(True);
inherited DoExit;
end;
procedure TLazIntegerEditGen._EditingDone;
begin
FValue := ReEncodeText(True);
inherited EditingDone;
end;
procedure TLazIntegerEditGen._Init;
begin
FMinValue := Min_Limit;
FMaxValue := Max_Limit;
FDisplayBase := 10;
FBinIndicator := '%';
FHexIndicator := '$';
FOctIndicator := '&';
FSetDecimalKeys := '#';
FToggleBinKeys := '%';
FToggleHexKeys := '$x';
FToggleOctKeys := '&';
FAllowMinus := True;
FAllowPlus := True;
end;
{ TLazIntegerEdit }
procedure TLazIntegerEdit.KeyDown(var Key: Word; Shift: TShiftState);
begin
_KeyDown(Key, Shift);
end;
procedure TLazIntegerEdit.KeyPress(var Key: Char);
begin
_KeyPress(Key);
end;
procedure TLazIntegerEdit.Utf8KeyPress(var UTF8Key: TUTF8Char);
begin
_Utf8KeyPress(UTF8Key);
end;
function TLazIntegerEdit.RealGetText: TCaption;
begin
Result := _RealGetText;
end;
procedure TLazIntegerEdit.InitializeWnd;
begin
_InitializeWnd;
end;
procedure TLazIntegerEdit.FinalizeWnd;
begin
_FinalizeWnd;
end;
procedure TLazIntegerEdit.DoExit;
begin
_DoExit;
end;
procedure TLazIntegerEdit.EditingDone;
begin
_EditingDone;
end;
constructor TLazIntegerEdit.Create(AOwner: TComponent);
begin
_Init;
inherited Create(AOwner);
end;
end.
|