procedure TRichEditStrings.LoadFromStream(Stream: TStream{$IFDEF RX_D12}; Encoding: TEncoding{$ENDIF});
var
EditStream: TEditStream;
Position: Longint;
TextType: Longint;
StreamInfo: TRichEditStreamInfo;
Converter: TConversion;
{$IFDEF RX_D12}
PlainText: Boolean;
{$ENDIF}
begin
{$IFDEF RX_D12}
if Encoding = nil then
begin
// Find the appropraite encoding
if ContainsPreamble(Stream, TEncoding.Unicode.GetPreamble) then
Encoding := TEncoding.Unicode
else
if ContainsPreamble(Stream, TEncoding.BigEndianUnicode.GetPreamble) then
Encoding := TEncoding.BigEndianUnicode
else
if ContainsPreamble(Stream, TEncoding.UTF8.GetPreamble) then
Encoding := TEncoding.UTF8
else
Encoding := TEncoding.Default;
end;
{$ENDIF}
StreamInfo.Stream := Stream;
if FConverter <> nil then
Converter := FConverter
else
Converter := RichEdit.DefaultConverter.Create;
StreamInfo.Converter := Converter;
{$IFDEF RX_D12}
PlainText:= (FFormat = sfPlainText) or
((FFormat = sfDefault) and (RichEdit.PlainText = True));
StreamInfo.PlainText:= PlainText;
StreamInfo.Encoding := Encoding;
{$ENDIF}
try
with EditStream do
begin
{$IFDEF RX_D12}
dwCookie := DWORD_PTR(@StreamInfo);
pfnCallBack := StreamLoad;
{$ELSE}
dwCookie := Longint(Pointer(@StreamInfo));
pfnCallBack := @StreamLoad;
{$ENDIF}
dwError := 0;
end;
Position := Stream.Position;
case FFormat of
sfDefault:
if RichEdit.PlainText then
TextType := SF_TEXT
else
TextType := SF_RTF;
sfRichText: TextType := SF_RTF;
else {sfPlainText}
TextType := SF_TEXT;
end;
if TextType = SF_RTF then
begin
if smPlainRtf in Mode then
TextType := TextType or SFF_PLAINRTF;
end
else
if TextType = SF_TEXT then
begin
{$IFNDEF RX_D12}
if (smUnicode in Mode) and (RichEditVersion > 1) then
{$ENDIF}
TextType := TextType or SF_UNICODE;
end;
if smSelection in Mode then
TextType := TextType or SFF_SELECTION;
SendMessage(RichEdit.Handle, EM_STREAMIN, TextType, Longint(@EditStream));
if (EditStream.dwError <> 0) then
begin
Stream.Position := Position;
if (TextType and SF_RTF = SF_RTF) then
TextType := SF_TEXT{$IFNDEF RX_D12} or SF_UNICODE{$ENDIF}
else
TextType := SF_RTF;
{$IFDEF RX_D12}
StreamInfo.PlainText := not PlainText;
{$ENDIF}
SendMessage(RichEdit.Handle, EM_STREAMIN, TextType, Longint(@EditStream));
if EditStream.dwError <> 0 then
raise EOutOfResources.Create(ResStr(sRichEditLoadFail));
end;
RichEdit.SetSelection(0, 0, True);
finally
if FConverter = nil then Converter.Free;
end;
end;