customdrawn: Starts selection support in TCDEdit

git-svn-id: trunk@33209 -
This commit is contained in:
sekelsenmat 2011-11-01 21:37:35 +00:00
parent 8fa968c191
commit 14c0de80e5
2 changed files with 68 additions and 10 deletions

View File

@ -118,6 +118,8 @@ var
lVisibleText, lTmpText, lControlText: TCaption;
lCaretPixelPos: Integer;
lHeight: Integer;
lSelLeftPos, lSelLeftPixelPos, lSelLength, lSelRightPos: Integer;
lTextWidth: Integer;
begin
// The background
ADest.Brush.Color := clWhite;
@ -126,22 +128,55 @@ begin
ADest.Pen.Style := psSolid;
ADest.Rectangle(0, 0, CDControl.Width, CDControl.Height);
// The text
lControlText := CDEdit.Text;
lVisibleText := Copy(lControlText, CDEdit.FVisibleTextStart, Length(lControlText));
ADest.Brush.Style := bsClear;
ADest.Font.Assign(CDControl.Font);
ADest.TextOut(4, 1, lVisibleText);
// Selection
if CDEdit.FSelLength > 0 then
// The text without selection
if CDEdit.FSelLength = 0 then
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;
// And the caret
if CDEdit.FCaretIsVisible then
begin
lTmpText := Copy(lControlText, 1, CDEdit.FCaretPos-CDEdit.FVisibleTextStart);
lTmpText := Copy(lControlText, 1, CDEdit.FCaretPos-CDEdit.FVisibleTextStart+1);
lCaretPixelPos := ADest.TextWidth(lTmpText) + 3;
lHeight := CDControl.Height;
ADest.Line(lCaretPixelPos, 2, lCaretPixelPos, lHeight-2);

View File

@ -192,7 +192,8 @@ type
FDragDropStarted: boolean;
FCaretIsVisible: Boolean;
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
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
@ -678,7 +679,8 @@ end;
procedure TCDEdit.DoDeleteSelection;
begin
FSelStart := 1;
FSelLength := 0;
end;
procedure TCDEdit.PrepareCurrentDrawer;
@ -757,8 +759,17 @@ begin
end;
VK_LEFT:
begin
if FCaretPos > 0 then
if (FCaretPos > 0) then
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);
FCaretIsVisible := True;
Invalidate;
@ -768,6 +779,15 @@ begin
begin
if FCaretPos < Length(lOldText) then
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);
FCaretIsVisible := True;
Invalidate;
@ -792,6 +812,8 @@ begin
// Don't handle it here because it is already handled in KeyDown
if UTF8Key = #8 then Exit;
DoDeleteSelection;
// Normal characters
lOldText := Text;
lLeftText := Copy(lOldText, 1, FCaretPos);
@ -840,6 +862,7 @@ begin
// State information
FVisibleTextStart := 1;
FSelStart := -1;
// Caret code
FCaretTimer := TTimer.Create(Self);