views:

66

answers:

2

I am writing an editor in Delphi (2009) using a TRichEdit component. The editor is append-only, in the sense that the caret must be at the end at all times, while maintaining the ability to copy using the mouse from elsewhere in the component.

The way it is working at the moment is by moving the caret to the end whenever something is written, but is it possible to make the caret not follow the mouse when clicking at other parts of the text?

+1  A: 

No, it is not possible. You have to move the caret to the end when the user types something.

Andreas Rejbrand
A: 

No. The caret must move in order for the user to make selections with the mouse or keyboard. You will have to move the caret to the end each time you insert new text. You should probably keep and restore the user's current caret position during each insertion as well, eg:

procedure TForm.AppendText(const S: String);
var
  OldCharRange, NewCharRange: TCharRange;
begin
  SendMessage(RichEdit1.Handle, EM_EXGETSEL, 0, LParam(@OldCharRange));
  try
    NewCharRange.cpMin := RichEdit1.GetTextLen;
    NewCharRange.cpMax := NewCharRange.cpMin;
    SendMessage(RichEdit1.Handle, EM_EXSETSEL, 0, LParam(@NewCharRange));
    RichEdit1.SelText := S;
  finally
    SendMessage(RichEdit1.Handle, EM_EXSETSEL, 0, LParam(@OldCharRange));
  end;
end;
Remy Lebeau - TeamB
@Remy: Too much work in C++? <g> What's wrong with OldStart := RichEdit1.SelStart; OldLen := RichEdit1.SelLength; try RichEdit1.SelStart := NewStart; RichEdit1.SelText := S; finally RichEdit1.SelStart := OldStart; RichEdit1.SelLength := OldLen; end;
Ken White
Using `EM_EXGETSEL` and `EM_EXSETSEL` directly eliminates several messages being sent to the RichEdit (the SelStart and SelLength property getters and setters re-issue those messages individually), and also the SelLength property setter issues an extra EM_SCROLLCARET message as well.
Remy Lebeau - TeamB