@@ -15,6 +15,9 @@ interface
1515
1616{ $I '..\Include\SciApi.inc'}
1717
18+ const
19+ MULTISELECTION_MASK: 0 ..4 = $4 ;
20+
1821 type
1922 // ////////////////////////////////////////////////////////////////////////////////////////////////
2023 TActiveDocument = class ;
@@ -84,7 +87,7 @@ TSelection = class(TTextRange)
8487 property StartPos: Sci_Position read GetStart write SetStart;
8588 property EndPos: Sci_Position read GetEnd write SetEnd;
8689 property Length: Sci_Position read GetLength write SetLength;
87- property Text: WideString read GetText write SetText ;
90+ property Text: WideString read GetText;
8891 end ;
8992 { -------------------------------------------------------------------------------------------- }
9093 TTextRangeMark = class
@@ -116,9 +119,9 @@ TWindowedObject = class
116119 end ;
117120
118121 { -------------------------------------------------------------------------------------------- }
122+ TSelectionMode = (smStreamSingle = SC_SEL_STREAM, smColumn, smLines, smThin, smStreamMulti);
119123 TActiveDocument = class (TWindowedObject)
120124 private
121-
122125 FSelection: TSelection;
123126
124127 function GetEditor (): TActiveDocument;
@@ -133,26 +136,22 @@ TActiveDocument = class(TWindowedObject)
133136 function GetNextLineStart (): Sci_Position;
134137 function GetLangType (): LangType;
135138 procedure SetLangType (const AValue: LangType);
136-
137139 function GetCurrentPos (): Sci_Position;
138140 procedure SetCurrentPos (const AValue: Sci_Position);
139141 function GetSelection : TSelection;
140142 function GetFirstVisibleLine : Sci_Position;
141143 function GetLinesOnScreen : Sci_Position;
142144 public
143145 destructor Destroy(); override;
144-
145146 function Activate (): TActiveDocument;
146-
147147 procedure Insert (const Text: WideString; const Position: Sci_Position = Sci_Position(High(Cardinal)));
148-
149148 function GetRange (const StartPosition: Sci_Position = 0 ; const LastPosition: Sci_Position = Sci_Position(High(Cardinal))): TTextRange;
150149 function GetLines (const FirstLine: Sci_Position; const Count: Sci_Position = 1 ): TTextRange;
151-
150+ function CharWidth : Byte;
151+ function SelectionMode : TSelectionMode;
152152 procedure Select (const Start: Sci_Position = 0 ; const Length: Sci_Position = Sci_Position(High(Cardinal)));
153- procedure SelectLines (const FirstLine: Sci_Position; const LineCount: Sci_Position = 1 );
154- procedure SelectColumns (const FirstPosition, LastPosition: Sci_Position);
155-
153+ procedure SelectMultiple (const Start: Sci_Position; const ALength: Sci_Position; MatchAll: Boolean = True);
154+ procedure ReplaceSelection (const AValue: WideString);
156155 procedure Find (const AText: WideString; var ATarget: TTextRange; const AOptions: integer = 0 ;
157156 const AStartPos: Sci_Position = -1 ; const AEndPos: Sci_Position = -1 ); overload;
158157 procedure Find (const AText: WideString; var ATarget: TTextRange; const AOptions: integer); overload;
@@ -164,7 +163,6 @@ TActiveDocument = class(TWindowedObject)
164163 property LineCount: Sci_Position read GetLineCount;
165164 property NextLineStartPosition: Sci_Position read GetNextLineStart;
166165 property Language: LangType read GetLangType write SetLangType;
167-
168166 property CurrentPosition:Sci_Position read GetCurrentPos write SetCurrentPos;
169167 property Selection: TSelection read GetSelection;
170168 property TopLine: Sci_Position read GetFirstVisibleLine;
@@ -690,6 +688,14 @@ procedure TActiveDocument.Find(const AText: WideString; var ATarget: TTextRange;
690688 end ;
691689end ;
692690
691+ { ------------------------------------------------------------------------------------------------ }
692+ function TActiveDocument.SelectionMode : TSelectionMode;
693+ begin
694+ Result := TSelectionMode(Self.SendMessage(SCI_GETSELECTIONMODE));
695+ if (Ord(Result) = SC_SEL_STREAM) and (Self.SendMessage(SCI_GETSELECTIONS) > 1 ) then
696+ Result := smStreamMulti;
697+ end ;
698+
693699{ ------------------------------------------------------------------------------------------------ }
694700
695701function TActiveDocument.GetCurrentPos : Sci_Position;
@@ -719,6 +725,15 @@ function TActiveDocument.GetLinesOnScreen: Sci_Position;
719725
720726{ ------------------------------------------------------------------------------------------------ }
721727
728+ function TActiveDocument.CharWidth : Byte;
729+ begin
730+ Result := SizeOf(AnsiChar);
731+ if (SendMessage(SCI_GETCODEPAGE) = SC_CP_UTF8) then
732+ Result := Sizeof(Widechar);
733+ end ;
734+
735+ { ------------------------------------------------------------------------------------------------ }
736+
722737function TActiveDocument.GetLangType : LangType;
723738var
724739 LT: integer;
@@ -743,42 +758,52 @@ function TActiveDocument.GetSelection: TSelection;
743758{ ------------------------------------------------------------------------------------------------ }
744759procedure TActiveDocument.Select (const Start, Length: Sci_Position);
745760var
746- SelMode: cardinal; // TODO: implement this as a property of the editor (or the selection object?)
761+ SelMode: TSelectionMode;
747762begin
748- SelMode := SendMessage(SCI_GETSELECTIONMODE );
749- if SelMode <> SC_SEL_STREAM then
763+ SelMode := TSelectionMode(Ord(SelectionMode) and ( not MULTISELECTION_MASK) );
764+ if SelMode <> smStreamSingle then
750765 SendMessage(SCI_SETSELECTIONMODE, SC_SEL_STREAM);
751766 SendMessage(SCI_SETSEL, Start, Start + Length);
752- if SelMode <> SC_SEL_STREAM then
753- SendMessage(SCI_SETSELECTIONMODE, SelMode);
767+ if SelMode <> smStreamSingle then
768+ SendMessage(SCI_SETSELECTIONMODE, Ord( SelMode))
754769end ;
755770
756771{ ------------------------------------------------------------------------------------------------ }
757772
758- procedure TActiveDocument.SelectColumns (const FirstPosition, LastPosition : Sci_Position);
773+ procedure TActiveDocument.SelectMultiple (const Start: Sci_Position; const ALength : Sci_Position; MatchAll: Boolean );
759774var
760- SelMode : cardinal; // TODO: implement this as a property of the editor (or the selection object?)
775+ SciMsg : cardinal;
761776begin
762- SelMode := SendMessage(SCI_GETSELECTIONMODE);
763- if SelMode <> SC_SEL_RECTANGLE then
764- SendMessage(SCI_SETSELECTIONMODE, SC_SEL_RECTANGLE);
765- SendMessage(SCI_SETSEL, FirstPosition, LastPosition);
766- if SelMode <> SC_SEL_RECTANGLE then
767- SendMessage(SCI_SETSELECTIONMODE, SelMode);
777+ SendMessage(SCI_CANCEL);
778+ FSelection.StartPos := SendMessage(SCI_POSITIONAFTER, (Start - (CharWidth shr 1 )));
779+ FSelection.EndPos := FSelection.StartPos + ALength;
780+ SciMsg := SCI_MULTIPLESELECTADDEACH;
781+ if not MatchAll then
782+ SciMsg := SCI_MULTIPLESELECTADDNEXT;
783+ SendMessage(SciMsg);
768784end ;
769785
770786{ ------------------------------------------------------------------------------------------------ }
771787
772- procedure TActiveDocument.SelectLines (const FirstLine, LineCount: Sci_Position );
788+ procedure TActiveDocument.ReplaceSelection (const AValue: WideString );
773789var
774- SelMode: cardinal; // TODO: implement this as a property of the editor (or the selection object?)
775- begin
776- SelMode := SendMessage(SCI_GETSELECTIONMODE);
777- if SelMode <> SC_SEL_LINES then
778- SendMessage(SCI_SETSELECTIONMODE, SC_SEL_LINES);
779- SendMessage(SCI_SETSEL, SendMessage(SCI_POSITIONFROMLINE, FirstLine), SendMessage(SCI_GETLINEENDPOSITION, FirstLine + LineCount));
780- if SelMode <> SC_SEL_LINES then
781- SendMessage(SCI_SETSELECTIONMODE, SelMode);
790+ Chars: AnsiString;
791+ MultiPasteMode: Cardinal;
792+ begin
793+ case Self.SendMessage(SCI_GETCODEPAGE) of
794+ SC_CP_UTF8:
795+ Chars := UTF8Encode(AValue)
796+ else
797+ Chars := RawByteString(AValue);
798+ end ;
799+ if (SelectionMode = smStreamMulti) then begin
800+ MultiPasteMode := SendMessage(SCI_GETMULTIPASTE);
801+ SendMessage(SCI_SETMULTIPASTE, SC_MULTIPASTE_EACH);
802+ SendMessage(SCI_COPYTEXT, System.Length(Chars), PAnsiChar(Chars));
803+ SendMessage(SCI_PASTE);
804+ SendMessage(SCI_SETMULTIPASTE, MultiPasteMode);
805+ end else
806+ Selection.SetText(AValue);
782807end ;
783808
784809{ ================================================================================================ }
0 commit comments