mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-06-23 03:28:28 +02:00
customdrawn: Starts selection support in TCDEdit
git-svn-id: trunk@33209 -
This commit is contained in:
parent
8fa968c191
commit
14c0de80e5
@ -118,6 +118,8 @@ var
|
|||||||
lVisibleText, lTmpText, lControlText: TCaption;
|
lVisibleText, lTmpText, lControlText: TCaption;
|
||||||
lCaretPixelPos: Integer;
|
lCaretPixelPos: Integer;
|
||||||
lHeight: Integer;
|
lHeight: Integer;
|
||||||
|
lSelLeftPos, lSelLeftPixelPos, lSelLength, lSelRightPos: Integer;
|
||||||
|
lTextWidth: Integer;
|
||||||
begin
|
begin
|
||||||
// The background
|
// The background
|
||||||
ADest.Brush.Color := clWhite;
|
ADest.Brush.Color := clWhite;
|
||||||
@ -126,22 +128,55 @@ begin
|
|||||||
ADest.Pen.Style := psSolid;
|
ADest.Pen.Style := psSolid;
|
||||||
ADest.Rectangle(0, 0, CDControl.Width, CDControl.Height);
|
ADest.Rectangle(0, 0, CDControl.Width, CDControl.Height);
|
||||||
|
|
||||||
// The text
|
|
||||||
lControlText := CDEdit.Text;
|
lControlText := CDEdit.Text;
|
||||||
lVisibleText := Copy(lControlText, CDEdit.FVisibleTextStart, Length(lControlText));
|
ADest.Brush.Style := bsClear;
|
||||||
ADest.Font.Assign(CDControl.Font);
|
ADest.Font.Assign(CDControl.Font);
|
||||||
ADest.TextOut(4, 1, lVisibleText);
|
|
||||||
|
|
||||||
// Selection
|
// The text without selection
|
||||||
if CDEdit.FSelLength > 0 then
|
if CDEdit.FSelLength = 0 then
|
||||||
begin
|
begin
|
||||||
//FSelStart, : Integer;
|
lVisibleText := Copy(lControlText, CDEdit.FVisibleTextStart, Length(lControlText));
|
||||||
|
ADest.TextOut(4, 1, lVisibleText);
|
||||||
|
end
|
||||||
|
// Text and Selection
|
||||||
|
else
|
||||||
|
begin
|
||||||
|
lSelLeftPos := CDEdit.FSelStart;
|
||||||
|
if CDEdit.FSelLength < 0 then lSelLeftPos := lSelLeftPos + CDEdit.FSelLength;
|
||||||
|
lSelRightPos := CDEdit.FSelStart;
|
||||||
|
if CDEdit.FSelLength > 0 then lSelRightPos := lSelRightPos + CDEdit.FSelLength;
|
||||||
|
lSelLength := CDEdit.FSelLength;
|
||||||
|
if lSelLength < 0 then lSelLength := lSelLength * -1;
|
||||||
|
|
||||||
|
// Text left of the selection
|
||||||
|
lVisibleText := Copy(lControlText, CDEdit.FVisibleTextStart, lSelLeftPos-CDEdit.FVisibleTextStart);
|
||||||
|
ADest.TextOut(4, 1, lVisibleText);
|
||||||
|
lSelLeftPixelPos := ADest.TextWidth(lVisibleText)+4;
|
||||||
|
|
||||||
|
// The selection background
|
||||||
|
lVisibleText := Copy(lControlText, lSelLeftPos, lSelLength);
|
||||||
|
lTextWidth := ADest.TextWidth(lVisibleText);
|
||||||
|
ADest.Brush.Color := clBlue;
|
||||||
|
ADest.Brush.Style := bsSolid;
|
||||||
|
ADest.Rectangle(lSelLeftPixelPos, 1, lSelLeftPixelPos+lTextWidth, lHeight-1);
|
||||||
|
ADest.Brush.Style := bsClear;
|
||||||
|
|
||||||
|
// The selection text
|
||||||
|
ADest.Font.Color := clWhite;
|
||||||
|
ADest.TextOut(lSelLeftPixelPos, 1, lVisibleText);
|
||||||
|
lSelLeftPixelPos := lSelLeftPixelPos + lTextWidth;
|
||||||
|
|
||||||
|
// Text right of the selection
|
||||||
|
ADest.Brush.Color := clWhite;
|
||||||
|
ADest.Font.Color := CDEdit.Font.Color;
|
||||||
|
lVisibleText := Copy(lControlText, lSelLeftPos+lSelLength+1, Length(lControlText));
|
||||||
|
ADest.TextOut(lSelLeftPixelPos, 1, lVisibleText);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// And the caret
|
// And the caret
|
||||||
if CDEdit.FCaretIsVisible then
|
if CDEdit.FCaretIsVisible then
|
||||||
begin
|
begin
|
||||||
lTmpText := Copy(lControlText, 1, CDEdit.FCaretPos-CDEdit.FVisibleTextStart);
|
lTmpText := Copy(lControlText, 1, CDEdit.FCaretPos-CDEdit.FVisibleTextStart+1);
|
||||||
lCaretPixelPos := ADest.TextWidth(lTmpText) + 3;
|
lCaretPixelPos := ADest.TextWidth(lTmpText) + 3;
|
||||||
lHeight := CDControl.Height;
|
lHeight := CDControl.Height;
|
||||||
ADest.Line(lCaretPixelPos, 2, lCaretPixelPos, lHeight-2);
|
ADest.Line(lCaretPixelPos, 2, lCaretPixelPos, lHeight-2);
|
||||||
|
@ -192,7 +192,8 @@ type
|
|||||||
FDragDropStarted: boolean;
|
FDragDropStarted: boolean;
|
||||||
FCaretIsVisible: Boolean;
|
FCaretIsVisible: Boolean;
|
||||||
FCaretPos: Integer; // zero-based position
|
FCaretPos: Integer; // zero-based position
|
||||||
FSelStart, FSelLength: Integer;
|
FSelStart: Integer; // zero-based position
|
||||||
|
FSelLength: Integer; // zero means no selection. Negative numbers selection to the left from the start and positive ones to the right
|
||||||
FVisibleTextStart: Integer; // 1-based
|
FVisibleTextStart: Integer; // 1-based
|
||||||
constructor Create(AOwner: TComponent); override;
|
constructor Create(AOwner: TComponent); override;
|
||||||
destructor Destroy; override;
|
destructor Destroy; override;
|
||||||
@ -678,7 +679,8 @@ end;
|
|||||||
|
|
||||||
procedure TCDEdit.DoDeleteSelection;
|
procedure TCDEdit.DoDeleteSelection;
|
||||||
begin
|
begin
|
||||||
|
FSelStart := 1;
|
||||||
|
FSelLength := 0;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TCDEdit.PrepareCurrentDrawer;
|
procedure TCDEdit.PrepareCurrentDrawer;
|
||||||
@ -757,8 +759,17 @@ begin
|
|||||||
end;
|
end;
|
||||||
VK_LEFT:
|
VK_LEFT:
|
||||||
begin
|
begin
|
||||||
if FCaretPos > 0 then
|
if (FCaretPos > 0) then
|
||||||
begin
|
begin
|
||||||
|
// Selecting to the left
|
||||||
|
if ssShift in Shift then
|
||||||
|
begin
|
||||||
|
Dec(FSelLength);
|
||||||
|
if FSelStart < 0 then FSelStart := FCaretPos;
|
||||||
|
end
|
||||||
|
// Normal move to the left
|
||||||
|
else FSelLength := 0;
|
||||||
|
|
||||||
Dec(FCaretPos);
|
Dec(FCaretPos);
|
||||||
FCaretIsVisible := True;
|
FCaretIsVisible := True;
|
||||||
Invalidate;
|
Invalidate;
|
||||||
@ -768,6 +779,15 @@ begin
|
|||||||
begin
|
begin
|
||||||
if FCaretPos < Length(lOldText) then
|
if FCaretPos < Length(lOldText) then
|
||||||
begin
|
begin
|
||||||
|
// Selecting to the right
|
||||||
|
if ssShift in Shift then
|
||||||
|
begin
|
||||||
|
Inc(FSelLength);
|
||||||
|
if FSelStart < 0 then FSelStart := FCaretPos;
|
||||||
|
end
|
||||||
|
// Normal move to the right
|
||||||
|
else FSelLength := 0;
|
||||||
|
|
||||||
Inc(FCaretPos);
|
Inc(FCaretPos);
|
||||||
FCaretIsVisible := True;
|
FCaretIsVisible := True;
|
||||||
Invalidate;
|
Invalidate;
|
||||||
@ -792,6 +812,8 @@ begin
|
|||||||
// Don't handle it here because it is already handled in KeyDown
|
// Don't handle it here because it is already handled in KeyDown
|
||||||
if UTF8Key = #8 then Exit;
|
if UTF8Key = #8 then Exit;
|
||||||
|
|
||||||
|
DoDeleteSelection;
|
||||||
|
|
||||||
// Normal characters
|
// Normal characters
|
||||||
lOldText := Text;
|
lOldText := Text;
|
||||||
lLeftText := Copy(lOldText, 1, FCaretPos);
|
lLeftText := Copy(lOldText, 1, FCaretPos);
|
||||||
@ -840,6 +862,7 @@ begin
|
|||||||
|
|
||||||
// State information
|
// State information
|
||||||
FVisibleTextStart := 1;
|
FVisibleTextStart := 1;
|
||||||
|
FSelStart := -1;
|
||||||
|
|
||||||
// Caret code
|
// Caret code
|
||||||
FCaretTimer := TTimer.Create(Self);
|
FCaretTimer := TTimer.Create(Self);
|
||||||
|
Loading…
Reference in New Issue
Block a user