Lazarus Hacks: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Lazarus Hacks
| Old | New | Line | |
|---|---|---|---|
| 1 | + | Add here your hacks/modifications to the Lazarus IDE or LCL widgets. If a good idea arises and is good enough it may become part of the official distribution; if not it can be used by other people that need more handy functionality than standards. | |
| 2 | + | ||
| 3 | + | > **Note:** Please post a patch to the bugtracker to get improvements into the Lazarus code. Just posting them here will not work. | |
| 4 | + | ||
| 5 | + | ## Exposing/Extending TCustomEdit | |
| 6 | + | ||
| 7 | + | In some applications we need to constantly convert the Text property of TCustomEdit descendants from or to numeric values: | |
| 8 | + | ||
| 9 | + | ``` pascal | |
| 10 | + | TCustomEditFilters = (cefNone, cefInteger, cefUnsigned, cefCurrency, cefFloat); | |
| 11 | + | ||
| 12 | + | { TCustomEdit } | |
| 13 | + | ||
| 14 | + | TCustomEdit = class(TWinControl) | |
| 15 | + | private | |
| 16 | + | ... | |
| 17 | + | FTmpModified : Boolean; | |
| 18 | + | FceFilter : TCustomEditFilters; | |
| 19 | + | procedure SetceFilter(aCeFilter: TCustomEditFilters); | |
| 20 | + | function GetAsInteger:Integer; | |
| 21 | + | procedure SetAsInteger(aVal: Integer); | |
| 22 | + | function GetAsFloat:Extended; | |
| 23 | + | procedure SetAsFloat(aVal: Extended); | |
| 24 | + | function GetAsCurrency:Currency; | |
| 25 | + | procedure SetAsCurrency(aVal: Currency); | |
| 26 | + | ||
| 27 | + | public | |
| 28 | + | ... | |
| 29 | + | property TmpModified: Boolean read FTmpModified write FTmpModified; | |
| 30 | + | property AsInteger: Integer read getAsInteger write setAsInteger; | |
| 31 | + | property AsFloat: Extended read getAsFloat write setAsFloat; | |
| 32 | + | property AsCurrency: Currency read getAsCurrency write setAsCurrency; | |
| 33 | + | published | |
| 34 | + | property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify; | |
| 35 | + | property ceFilter : TCustomEditFilters read FceFilter write SetceFilter default cefNone; | |
| 36 | + | end; | |
| 37 | + | ||
| 38 | + | //On the implementation part tcustomedit.inc: | |
| 39 | + | ||
| 40 | + | resourcestring | |
| 41 | + | rsInvalidNumbe = 'Invalid number !'; | |
| 42 | + | ||
| 43 | + | procedure TCustomEdit.Change; | |
| 44 | + | begin | |
| 45 | + | FTmpModified := True; | |
| 46 | + | if Assigned(FOnChange) then FOnChange(Self); | |
| 47 | + | end; | |
| 48 | + | ||
| 49 | + | procedure TCustomEdit.DoExit; | |
| 50 | + | begin | |
| 51 | + | if FCeFilter <> cefNone then | |
| 52 | + | begin | |
| 53 | + | case FCeFilter of | |
| 54 | + | cefInteger, cefUnsigned: AsInteger; | |
| 55 | + | cefFloat: AsFloat; | |
| 56 | + | cefCurrency: AsCurrency; | |
| 57 | + | end; | |
| 58 | + | end; | |
| 59 | + | FAutoSelected := False; | |
| 60 | + | inherited DoExit; | |
| 61 | + | end; | |
| 62 | + | ||
| 63 | + | procedure TCustomEdit.SetceFilter(aCeFilter: TCustomEditFilters); | |
| 64 | + | begin | |
| 65 | + | FCeFilter := aCeFilter; | |
| 66 | + | if FCeFilter = cefNone then Alignment := taLeftJustify | |
| 67 | + | else Alignment := taRightJustify; | |
| 68 | + | end; | |
| 69 | + | ||
| 70 | + | function TCustomEdit.GetAsInteger:Integer; | |
| 71 | + | begin | |
| 72 | + | TryStrToInt(Text, result); | |
| 73 | + | end; | |
| 74 | + | ||
| 75 | + | procedure TCustomEdit.SetAsInteger(aVal: Integer); | |
| 76 | + | begin | |
| 77 | + | Text := IntToStr(aVal); | |
| 78 | + | end; | |
| 79 | + | ||
| 80 | + | function TCustomEdit.GetAsFloat:Extended; | |
| 81 | + | begin | |
| 82 | + | if Text = '' then result := 0 | |
| 83 | + | else if not TryStrToFloat(Text, result) then | |
| 84 | + | Raise Exception.Create(rsInvalidNumbe); | |
| 85 | + | end; | |
| 86 | + | ||
| 87 | + | procedure TCustomEdit.SetAsFloat(aVal: Extended); | |
| 88 | + | begin | |
| 89 | + | Text := FloatToStr(aVal); | |
| 90 | + | end; | |
| 91 | + | ||
| 92 | + | function TCustomEdit.GetAsCurrency:Currency; | |
| 93 | + | begin | |
| 94 | + | if Text = '' then result := 0 | |
| 95 | + | else if not TryStrToCurr(Text, result) then | |
| 96 | + | raise Exception.Create(rsInvalidNumbe); | |
| 97 | + | end; | |
| 98 | + | ||
| 99 | + | procedure TCustomEdit.SetAsCurrency(aVal: Currency); | |
| 100 | + | begin | |
| 101 | + | Text := CurrToStr(aVal); | |
| 102 | + | end; | |
| 103 | + | ``` | |
| 104 | + | ||
| 105 | + | ## Replacing VK_DECIMAL keypad dot by comma | |
| 106 | + | ||
| 107 | + | When writing Lazarus applications to non-US locales there is a problem with the keypad "." (dot) when the locale uses "," (comma) as decimal separator. I've been looking for a solution and ended up with this: | |
| 108 | + | ||
| 109 | + | Form.KeyPreview := True; Form.OnKeyDown := @FormKeyDown; Form.OnKeyPress := @FormKeyPress; | |
| 110 | + | ||
| 111 | + | ``` pascal | |
| 112 | + | { TFormBase } | |
| 113 | + | ||
| 114 | + | TFormBase = class(TForm) | |
| 115 | + | ... | |
| 116 | + | protected | |
| 117 | + | { protected declarations } | |
| 118 | + | fVK_Decimal : Boolean; | |
| 119 | + | ... | |
| 120 | + | end; | |
| 121 | + | ||
| 122 | + | procedure TFormBase.FormKeyPress(Sender: TObject; var Key: char); | |
| 123 | + | begin | |
| 124 | + | if fVK_Decimal and (key = '.') then key := DecimalSeparator; | |
| 125 | + | end; | |
| 126 | + | ||
| 127 | + | procedure TFormBase.FormKeyDown(Sender: TObject; var Key: word; Shift: TShiftState); | |
| 128 | + | begin | |
| 129 | + | if (Shift = []) and (key = VK_DECIMAL) then | |
| 130 | + | fVK_Decimal := True; | |
| 131 | + | end; | |
| 132 | + | ``` | |
| 133 | + | ||
| 134 | + | ## See also | |
| 135 | + | ||
| 136 | + | - [[Creating A Patch]] - How to get your modifications into the Lazarus codebase | |
| 137 | + | ||
| 138 | + | --- | |
| 139 | + | *From the Free Pascal wiki, page "Lazarus Hacks", as it stood on 2020-03-09 after a change by Minesadorada, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 140 | + | Category: Code Snippets | |
| 141 | + | Category: Lazarus |
