Debian Codegraph @

components/KASToolBar/kascdedit.pas

raw
1 {
2 Double Commander
3 -------------------------------------------------------------------------
4 Custom edit control with the look and feel like TLabel
5
6 Copyright (C) 2017-2024 Alexander Koblov ([email protected])
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 }
21
22 unit KASCDEdit;
23
24 {$mode delphi}
25
26 interface
27
28 uses
29 Classes, SysUtils, LResources, Controls, Graphics, Dialogs, Types,
30 Menus, CustomDrawnControls, CustomDrawnDrawers, CustomDrawn_Common;
31
32 type
33
34 { TKASCDEdit }
35
36 TKASCDEdit = class(TCDEdit)
37 private
38 FDragDropStarted: Boolean;
39 FEditMenu: TPopupMenu; static;
40 private
41 procedure CreatePopupMenu;
42 procedure ShowMenu(Data: PtrInt);
43 procedure MenuCopy(Sender: TObject);
44 procedure MenuSelectAll(Sender: TObject);
45 function MousePosToCaretPos(X, Y: Integer): TPoint;
46 protected
47 procedure RealSetText(const Value: TCaption); override;
48 procedure CalculatePreferredSize(var PreferredWidth, PreferredHeight: Integer;
49 WithThemeSpace: Boolean); override;
50 procedure CalculateSize(MaxWidth: Integer; var NeededWidth, NeededHeight: Integer);
51 procedure KeyDown(var Key: word; Shift: TShiftState); override;
52 public
53 constructor Create(AOwner: TComponent); override;
54 procedure MouseMove(Shift: TShiftState; X, Y: integer); override;
55 procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: integer); override;
56 procedure MouseUp(Button: TMouseButton; Shift:TShiftState; X,Y:Integer); override;
57 public
58 procedure SelectAll;
59 procedure CopyToClipboard;
60 published
61 property Color default clDefault;
62 property Cursor default crIBeam;
63 property ReadOnly default True;
64 property OnMouseDown;
65 property OnMouseMove;
66 property OnMouseUp;
67 end;
68
69 { TKASCDDrawer }
70
71 TKASCDDrawer = class(TCDDrawerCommon)
72 public
73 function GetMeasures(AMeasureID: Integer): Integer; override;
74 procedure DrawEditBackground(ADest: TCanvas; ADestPos: TPoint; ASize: TSize;
75 AState: TCDControlState; AStateEx: TCDEditStateEx); override;
76 procedure DrawEdit(ADest: TCanvas; ASize: TSize;
77 AState: TCDControlState; AStateEx: TCDEditStateEx); override;
78 end;
79
80 procedure Register;
81
82 implementation
83
84 uses
85 Math, Forms, Clipbrd, LCLType, LCLIntf, LazUTF8;
86
87 resourcestring
88 rsMnuCopyToClipboard = 'Copy';
89 rsMnuSelectAll = 'Select &All';
90
91 procedure Register;
92 begin
93 RegisterComponents('KASComponents', [TKASCDEdit]);
94 end;
95
96 { TKASCDDrawer }
97
98 function TKASCDDrawer.GetMeasures(AMeasureID: Integer): Integer;
99 begin
100 case AMeasureID of
101 TCDEDIT_LEFT_TEXT_SPACING: Result := 0;
102 TCDEDIT_RIGHT_TEXT_SPACING: Result := 0;
103 else Result:= inherited GetMeasures(AMeasureID);
104 end;
105 end;
106
107 procedure TKASCDDrawer.DrawEditBackground(ADest: TCanvas; ADestPos: TPoint;
108 ASize: TSize; AState: TCDControlState; AStateEx: TCDEditStateEx);
109 begin
110 // None
111 end;
112
113 procedure TKASCDDrawer.DrawEdit(ADest: TCanvas; ASize: TSize;
114 AState: TCDControlState; AStateEx: TCDEditStateEx);
115 var
116 lVisibleText, lControlText: TCaption;
117 lSelLeftPos, lSelLength, lSelRightPos: Integer;
118 lLineHeight, lLineTop: Integer;
119 lControlTextLen: PtrInt;
120 lTextLeftSpacing, lTextTopSpacing, lTextBottomSpacing: Integer;
121 lTextColor: TColor;
122 i, lVisibleLinesCount: Integer;
123 AClipRect: TRect;
124 begin
125 // General text configurations which apply to all lines
126 // Configure the text color
127 if csfEnabled in AState then
128 lTextColor := AStateEx.Font.Color
129 else
130 lTextColor := clGrayText;
131
132 ADest.Brush.Style := bsClear;
133 ADest.Font.Assign(AStateEx.Font);
134 ADest.Font.Color := lTextColor;
135 lTextLeftSpacing := GetMeasures(TCDEDIT_LEFT_TEXT_SPACING);
136 //lTextRightSpacing := GetMeasures(TCDEDIT_RIGHT_TEXT_SPACING);
137 lTextTopSpacing := GetMeasures(TCDEDIT_TOP_TEXT_SPACING);
138 lTextBottomSpacing := GetMeasures(TCDEDIT_BOTTOM_TEXT_SPACING);
139
140 lLineHeight := ADest.TextHeight(cddTestStr)+2;
141 lLineHeight := Min(ASize.cy-lTextBottomSpacing, lLineHeight);
142
143 // Fill this to be used in other parts
144 AStateEx.LineHeight := lLineHeight;
145 AStateEx.FullyVisibleLinesCount := ASize.cy - lTextTopSpacing - lTextBottomSpacing;
146 AStateEx.FullyVisibleLinesCount := AStateEx.FullyVisibleLinesCount div lLineHeight;
147 AStateEx.FullyVisibleLinesCount := Min(AStateEx.FullyVisibleLinesCount, AStateEx.Lines.Count);
148
149 // Calculate how many lines to draw
150 if AStateEx.Multiline then
151 lVisibleLinesCount := AStateEx.FullyVisibleLinesCount + 1
152 else
153 lVisibleLinesCount := 1;
154 lVisibleLinesCount := Min(lVisibleLinesCount, AStateEx.Lines.Count);
155
156 // Now draw each line
157 for i := 0 to lVisibleLinesCount - 1 do
158 begin
159 lControlText := AStateEx.Lines.Strings[AStateEx.VisibleTextStart.Y+i];
160 lControlText := VisibleText(lControlText, AStateEx.PasswordChar);
161 lControlTextLen := UTF8Length(lControlText);
162 lLineTop := lTextTopSpacing + i * lLineHeight;
163
164 // The text
165 ADest.Pen.Style := psClear;
166 ADest.Brush.Style := bsClear;
167 lVisibleText := UTF8Copy(lControlText, AStateEx.VisibleTextStart.X, lControlTextLen);
168 // ToDo: Implement multi-line selection
169 if (AStateEx.SelLength = 0) or (AStateEx.SelStart.Y <> AStateEx.VisibleTextStart.Y+i) then
170 begin
171 ADest.TextOut(lTextLeftSpacing, lLineTop, lVisibleText);
172 end
173 // Text and Selection
174 else
175 begin
176 lSelLeftPos := AStateEx.SelStart.X;
177 if AStateEx.SelLength < 0 then lSelLeftPos := lSelLeftPos + AStateEx.SelLength;
178
179 lSelRightPos := AStateEx.SelStart.X;
180 if AStateEx.SelLength > 0 then lSelRightPos := lSelRightPos + AStateEx.SelLength;
181
182 lSelLength := AStateEx.SelLength;
183 if lSelLength < 0 then lSelLength := lSelLength * -1;
184
185 // Draw a normal text
186 ADest.Font.Color := lTextColor;
187 ADest.TextOut(lTextLeftSpacing, lLineTop, lVisibleText);
188
189 // Draw a selected text
190 ADest.Brush.Color := clHighlight;
191 ADest.Font.Color := clHighlightText;
192 // Calculate a clip rect
193 AClipRect := ADest.ClipRect;
194 AClipRect.Left := ADest.TextWidth(UTF8Copy(lVisibleText, 1, lSelLeftPos));
195 AClipRect.Right := ADest.TextWidth(UTF8Copy(lVisibleText, 1, lSelLeftPos + lSelLength));
196 IntersectClipRect(ADest.Handle, AClipRect.Left, AClipRect.Top, AClipRect.Right, AClipRect.Bottom);
197 ADest.TextOut(lTextLeftSpacing, lLineTop, lVisibleText);
198 end;
199 end;
200
201 // And the caret
202 DrawCaret(ADest, Point(0, 0), ASize, AState, AStateEx);
203 end;
204
205 { TKASCDEdit }
206
207 procedure TKASCDEdit.CreatePopupMenu;
208 var
209 MenuItem: TMenuItem;
210 begin
211 if not Assigned(FEditMenu) then
212 begin
213 FEditMenu:= TPopupMenu.Create(Application);
214 MenuItem:= TMenuItem.Create(FEditMenu);
215 MenuItem.Caption:= rsMnuCopyToClipboard;
216 MenuItem.OnClick:= MenuCopy;
217 FEditMenu.Items.Add(MenuItem);
218 MenuItem:= TMenuItem.Create(FEditMenu);
219 MenuItem.Caption:= '-';
220 FEditMenu.Items.Add(MenuItem);
221 MenuItem:= TMenuItem.Create(FEditMenu);
222 MenuItem.Caption:= rsMnuSelectAll;
223 MenuItem.OnClick:= MenuSelectAll;
224 FEditMenu.Items.Add(MenuItem);
225 end;
226 end;
227
228 procedure TKASCDEdit.ShowMenu(Data: PtrInt);
229 begin
230 FEditMenu.Tag:= Data;
231 FEditMenu.PopUp;
232 end;
233
234 procedure TKASCDEdit.MenuCopy(Sender: TObject);
235 begin
236 TKASCDEdit(TMenuItem(Sender).Owner.Tag).CopyToClipboard;
237 end;
238
239 procedure TKASCDEdit.MenuSelectAll(Sender: TObject);
240 begin
241 TKASCDEdit(TMenuItem(Sender).Owner.Tag).SelectAll;
242 end;
243
244 function TKASCDEdit.MousePosToCaretPos(X, Y: Integer): TPoint;
245 var
246 lStrLen, i: PtrInt;
247 lBeforeStr: String;
248 lTextLeftSpacing: Integer;
249 lVisibleStr, lCurChar: String;
250 lPos: Integer;
251 lBestDiff: Cardinal = $FFFFFFFF;
252 lLastDiff: Cardinal = $FFFFFFFF;
253 lCurDiff, lBestMatch: Integer;
254 begin
255 // Find the best Y position
256 lPos := Y - FDrawer.GetMeasures(TCDEDIT_TOP_TEXT_SPACING);
257 Result.Y := lPos div FEditState.LineHeight;
258 Result.Y := Min(Result.Y, FEditState.FullyVisibleLinesCount);
259 Result.Y := Min(Result.Y, FEditState.Lines.Count-1);
260 if Result.Y < 0 then
261 begin
262 Result.X := 1;
263 Result.Y := 0;
264 Exit;
265 end;
266
267 // Find the best X position
268 Canvas.Font := Font;
269 lVisibleStr := Lines.Strings[Result.Y];
270 lVisibleStr := LazUTF8.UTF8Copy(lVisibleStr, FEditState.VisibleTextStart.X, Length(lVisibleStr));
271 lVisibleStr := TCDDrawer.VisibleText(lVisibleStr, FEditState.PasswordChar);
272 lStrLen := LazUTF8.UTF8Length(lVisibleStr);
273 lTextLeftSpacing := FDrawer.GetMeasures(TCDEDIT_LEFT_TEXT_SPACING);
274 lBestMatch := 0;
275 lBeforeStr := EmptyStr;
276 lPos := lTextLeftSpacing;
277 for i := 0 to lStrLen do
278 begin
279 lCurDiff := X - lPos;
280 if lCurDiff < 0 then lCurDiff := lCurDiff * -1;
281
282 if lCurDiff < lBestDiff then
283 begin
284 lBestDiff := lCurDiff;
285 lBestMatch := i;
286 end;
287
288 // When the diff starts to grow we already found the caret pos, so exit
289 if lCurDiff > lLastDiff then Break
290 else lLastDiff := lCurDiff;
291
292 if i <> lStrLen then
293 begin
294 lCurChar := LazUTF8.UTF8Copy(lVisibleStr, i + 1, 1);
295 lBeforeStr := lBeforeStr + lCurChar;
296 lPos := lTextLeftSpacing + Canvas.TextWidth(lBeforeStr);
297 end;
298 end;
299
300 Result.X := lBestMatch+(FEditState.VisibleTextStart.X-1);
301 Result.X := Min(Result.X, FEditState.VisibleTextStart.X+lStrLen-1);
302 end;
303
304 procedure TKASCDEdit.RealSetText(const Value: TCaption);
305 begin
306 Lines.Text := Value;
307 inherited RealSetText(Value);
308 end;
309
310 procedure TKASCDEdit.CalculatePreferredSize(var PreferredWidth,
311 PreferredHeight: Integer; WithThemeSpace: Boolean);
312 var
313 AWidth: Integer;
314 begin
315 if (Parent = nil) or (not Parent.HandleAllocated) then Exit;
316 AWidth := Constraints.MinMaxWidth(10000);
317 CalculateSize(AWidth, PreferredWidth, PreferredHeight);
318 end;
319
320 procedure TKASCDEdit.CalculateSize(MaxWidth: Integer; var NeededWidth,
321 NeededHeight: Integer);
322 var
323 DC: HDC;
324 R: TRect;
325 Flags: Cardinal;
326 OldFont: HGDIOBJ;
327 LabelText: String;
328 lTextLeftSpacing, lTextTopSpacing,
329 lTextBottomSpacing, lTextRightSpacing: Integer;
330 begin
331 LabelText := Text;
332
333 if LabelText = '' then
334 begin
335 NeededWidth:= 1;
336 NeededHeight:= 1;
337 Exit;
338 end;
339
340 lTextLeftSpacing := FDrawer.GetMeasures(TCDEDIT_LEFT_TEXT_SPACING);
341 lTextTopSpacing := FDrawer.GetMeasures(TCDEDIT_TOP_TEXT_SPACING);
342 lTextRightSpacing := FDrawer.GetMeasures(TCDEDIT_RIGHT_TEXT_SPACING);
343 lTextBottomSpacing := FDrawer.GetMeasures(TCDEDIT_BOTTOM_TEXT_SPACING);
344
345 DC := GetDC(Parent.Handle);
346 try
347 R := Rect(0, 0, MaxWidth, 10000);
348 OldFont := SelectObject(DC, HGDIOBJ(Font.Reference.Handle));
349 Flags := DT_CALCRECT or DT_EXPANDTABS;
350 if not MultiLine then Flags := Flags or DT_SINGLELINE;
351 DrawText(DC, PAnsiChar(LabelText), Length(LabelText), R, Flags);
352 SelectObject(DC, OldFont);
353 NeededWidth := R.Right - R.Left + lTextLeftSpacing + lTextRightSpacing;
354 NeededHeight := R.Bottom - R.Top + lTextTopSpacing + lTextBottomSpacing;
355 finally
356 ReleaseDC(Parent.Handle, DC);
357 end;
358 end;
359
360 procedure TKASCDEdit.KeyDown(var Key: word; Shift: TShiftState);
361 begin
362 if (ssModifier in Shift) then
363 begin
364 case Key of
365 VK_A:
366 begin
367 SelectAll;
368 Key:= 0;
369 end;
370 VK_C:
371 begin
372 CopyToClipboard;
373 Key:= 0;
374 end;
375 end;
376 end;
377 if ReadOnly and (Key in [VK_BACK, VK_DELETE]) then
378 begin
379 Key:= 0;
380 end;
381 inherited KeyDown(Key, Shift);
382 end;
383
384 constructor TKASCDEdit.Create(AOwner: TComponent);
385 begin
386 CreatePopupMenu;
387 inherited Create(AOwner);
388 ReadOnly:= True;
389 Cursor:= crIBeam;
390 Color:= clDefault;
391 DrawStyle:= dsExtra1;
392 ControlStyle:= ControlStyle + [csParentBackground] - [csOpaque];
393 end;
394
395 procedure TKASCDEdit.MouseMove(Shift: TShiftState; X, Y: integer);
396 begin
397 inherited MouseMove(Shift, X, Y);
398
399 // Mouse dragging selection
400 if FDragDropStarted then
401 begin
402 FEditState.CaretPos := MousePosToCaretPos(X, Y);
403 FEditState.SelLength := FEditState.CaretPos.X - FEditState.SelStart.X;
404 Invalidate;
405 end;
406 end;
407
408 procedure TKASCDEdit.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
409 Y: integer);
410 begin
411 if (Button = mbLeft) or (GetSelLength = 0) then
412 begin
413 inherited MouseDown(Button, Shift, X, Y);
414
415 FDragDropStarted := True;
416
417 // Caret positioning
418 FEditState.CaretPos := MousePosToCaretPos(X, Y);
419 FEditState.SelStart.X := FEditState.CaretPos.X;
420 FEditState.SelStart.Y := FEditState.CaretPos.Y;
421 Invalidate;
422 end
423 else if Assigned(OnMouseDown) then begin
424 OnMouseDown(Self, Button, Shift, X, Y);
425 end;
426 end;
427
428 procedure TKASCDEdit.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
429 begin
430 inherited MouseUp(Button, Shift, X, Y);
431
432 FDragDropStarted := False;
433
434 if Button = mbRight then begin
435 Application.QueueAsyncCall(ShowMenu, PtrInt(Self));
436 end;
437 end;
438
439 procedure TKASCDEdit.SelectAll;
440 begin
441 FEditState.SelStart.X:= 0;
442 FEditState.SelLength:= UTF8Length(Text);
443 Invalidate;
444 end;
445
446 procedure TKASCDEdit.CopyToClipboard;
447 begin
448 if (FEditState.SelLength >= 0) then
449 Clipboard.AsText:= UTF8Copy(Text, FEditState.SelStart.X + 1, FEditState.SelLength)
450 else begin
451 Clipboard.AsText:= UTF8Copy(Text, FEditState.SelStart.X + FEditState.SelLength + 1, -FEditState.SelLength);
452 end;
453 end;
454
455 initialization
456 RegisterDrawer(TKASCDDrawer.Create, dsExtra1);
457
458 end.