From 640f89fe10698ce8f71a3b18acd9cdd6ddb068b0 Mon Sep 17 00:00:00 2001 From: skalogryz Date: Sat, 13 Jun 2009 12:00:11 +0000 Subject: [PATCH] fix in carbonrichedit getting attributes. changes richedit getting atttributes interface. updated simpletest git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@840 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- components/richmemo/carbon/carbonrichmemo.pas | 116 +++----------- components/richmemo/richmemo.pas | 65 ++++++-- components/richmemo/richmemotypes.pas | 12 +- .../richmemo/samples/testsimple/project1.lpi | 142 +++++++++--------- .../richmemo/samples/testsimple/unit1.lfm | 8 +- .../richmemo/samples/testsimple/unit1.lrs | 6 +- .../richmemo/samples/testsimple/unit1.pas | 3 +- components/richmemo/wsrichmemo.pas | 21 ++- 8 files changed, 171 insertions(+), 202 deletions(-) diff --git a/components/richmemo/carbon/carbonrichmemo.pas b/components/richmemo/carbon/carbonrichmemo.pas index 4524ae988..eb30b86c2 100644 --- a/components/richmemo/carbon/carbonrichmemo.pas +++ b/components/richmemo/carbon/carbonrichmemo.pas @@ -40,9 +40,9 @@ type class function GetStyleRange(const AWinControl: TWinControl; TextStart: Integer; var RangeStart, RangeLen: Integer): Boolean; override; class function GetTextAttributes(const AWinControl: TWinControl; TextStart: Integer; - var Params: TFontParams): Boolean; override; + var Params: TIntFontParams): Boolean; override; class procedure SetTextAttributes(const AWinControl: TWinControl; TextStart, TextLen: Integer; - Mask: TTextStyleMask; const Params: TFontParams); override; + {Mask: TTextStyleMask;} const Params: TIntFontParams); override; class procedure SetHideSelection(const AWinControl: TWinControl; AHideSelection: Boolean); override; class function LoadRichText(const AWinControl: TWinControl; Src: TStream): Boolean; override; class function SaveRichText(const AWinControl: TWinControl; Dst: TStream): Boolean; override; @@ -163,32 +163,32 @@ begin Attr.data.dataPtr := astyle; end; -procedure ParamsToTXNAttribs(ParamsMask: TTextStyleMask; const Params: TFontParams; +procedure ParamsToTXNAttribs({ParamsMask: TTextStyleMask;} const Params: TIntFontParams; var Attr: array of TXNTypeAttributes; var AttrCount: Integer; var MacColor: RGBColor); begin AttrCount := 0; //todo: replace QuickDraw style by ATSU style - if tsm_Color in ParamsMask then begin + //if tsm_Color in ParamsMask then begin MacColor := ColorToRGBColor(Params.Color); AttrSetColor(MacColor, Attr[AttrCount] ); inc(AttrCount); - end; + //end; - if tsm_Name in ParamsMask then begin + //if tsm_Name in ParamsMask then begin AttrSetFontName(Params.Name, Attr[AttrCount] ); inc(AttrCount); - end; + //end; - if tsm_Size in ParamsMask then begin + //if tsm_Size in ParamsMask then begin AttrSetSize(Params.Size, Attr[AttrCount] ); inc(AttrCount); - end; + //end; - if tsm_Styles in ParamsMask then begin + //if tsm_Styles in ParamsMask then begin AttrSetStyle(Params.Style, Attr[AttrCount]); inc(AttrCount); - end; + //end; end; { TCarbonWSCustomRichMemo } @@ -203,8 +203,6 @@ class function TCarbonWSCustomRichMemo.GetStyleRange(const AWinControl: TWinCont TextStart: Integer; var RangeStart, RangeLen: Integer): Boolean; var edit : TCarbonRichEdit; - obj : TXNObject; - sst, slen : Integer; st, len : Integer; send : Integer; fndstyle : Boolean; @@ -227,77 +225,12 @@ begin RangeLen := RngEnd - RngStart; end; -{ edit.GetSelStart(sst); - edit.GetSelLength(slen); - - edit.SetSelStart(TextStart); - edit.SetSelLength(1); - - ATSUCreateStyle(astyle); - AttrSetATSUStyle(nil, wattr[0]); - AttrSetColor(macrgb, wattr[1]); - edit.GetContinuousTypeAttributes(flags, 2, wattr[0]); - - GetTextLen(AWinControl, len); - dec(len, TextStart); - st:=TextStart; - - writeln('TextStart = ', TextStart); - writeln('TextLength = ', 2); - edit.SetSelStart(TextStart); - edit.SetSelLength(2);//send-TextStart); - attr := wattr; - edit.GetContinuousTypeAttributes(flags, 2, attr); - - writeln('contflags ',flags); - - Result := true;} - -{ fndstyle := false; - while not fndstyle do begin - edit.SetSelStart(st); - edit.SetSelLength(len); - attr := wattr; - - send := st + len; - repeat - writeln(st,' ', send); - d := (st+send) div 2; {разделить пополам интервал просмотра} - edit.SetSelStart(TextStart); - edit.SetSelLength(send-TextStart); - attr := wattr; - edit.GetContinuousTypeAttributes(flags, 2, attr); - if flags = (kTXNColorContinuousMask or kTXNATSUIStyleContinuousMask) then - st := st+1 - else - send := d-1; - until (st>send);} - -{ while send > st do begin - writeln(st, ' ', send, ' ', send - st, ' ', (send - st) div 2); - attr := wattr; - edit.SetSelStart(st); - edit.SetSelLength(st+len); - edit.GetContinuousTypeAttributes(flags, 2, attr); - writeln(' flags = ', flags); - {d := (send - st) div 2; - if d = 0 then d := 1;} - if flags = (kTXNColorContinuousMask or kTXNATSUIStyleContinuousMask) - then st := send - else dec(send, d); - end; - Result := send - TextStart;} - // end; - -{ edit.SetSelStart(sst); - edit.SetSelLength(slen); - ATSUDisposeStyle(astyle);} end; class function TCarbonWSCustomRichMemo.GetTextAttributes(const AWinControl: TWinControl; - TextStart: Integer; var Params: TFontParams): Boolean; + TextStart: Integer; var Params: TIntFontParams): Boolean; var - memo : TCarbonRichEdit; + edit : TCarbonRichEdit; attr : array [0..1] of TXNTypeAttributes; sstart : Integer; slen : Integer; @@ -307,20 +240,20 @@ var begin Result := false; - memo := GetValidRichEdit(AWinControl); - if not Assigned(memo) then Exit; + edit := GetValidRichEdit(AWinControl); + if not Assigned(edit) then Exit; - memo.GetSelStart(sstart); - memo.GetSelLength(slen); + edit.GetSelStart(sstart); + edit.GetSelLength(slen); - memo.SetSelStart(TextStart); - memo.SetSelLength(1); + edit.SetSelStart(TextStart); + edit.SetSelLength(1); ATSUCreateStyle(astyle); AttrSetATSUStyle(astyle, attr[0]); AttrSetStyle([], attr[1]); - Result := memo.GetContinuousTypeAttributes(flags, 2, attr); + Result := edit.GetContinuousTypeAttributes(flags, 2, attr); Params.Name := GetATSUFontName(astyle); Params.Color := GetATSUFontColor(astyle); Params.Style := GetATSUFontStyles(astyle) + QDStyleToFontStyle(attr[1].data.dataValue); @@ -328,13 +261,12 @@ begin ATSUDisposeStyle(astyle); - memo.SetSelLength(sstart); - memo.SetSelLength(slen); + edit.SetSelStart(sstart); + edit.SetSelLength(slen); end; class procedure TCarbonWSCustomRichMemo.SetTextAttributes(const AWinControl: TWinControl; - TextStart, TextLen: Integer; - Mask: TTextStyleMask; const Params: TFontParams); + TextStart, TextLen: Integer; {Mask: TTextStyleMask; }const Params: TIntFontParams); var memo : TCarbonRichEdit; Attr : array [0..TXNAttributesMax-1] of TXNTypeAttributes; @@ -344,7 +276,7 @@ begin memo := GetValidRichEdit(AWinControl); if not Assigned(memo) then Exit; - ParamsToTXNAttribs(Mask, Params, attr, Count, maccolor); + ParamsToTXNAttribs({Mask,} Params, attr, Count, maccolor); memo.SetTypeAttributes(Count, Attr, TextStart, TextStart+TextLen); end; diff --git a/components/richmemo/richmemo.pas b/components/richmemo/richmemo.pas index 757d01d18..3d7b35f76 100644 --- a/components/richmemo/richmemo.pas +++ b/components/richmemo/richmemo.pas @@ -10,6 +10,17 @@ uses type + TFontParams = TIntFontParams; + {TIntFontParams = record // declared at WSRichMemo + Name : String; + Size : Integer; + Color : TColor; + Style : TFontStyles; + end; } + + + TTextModifyMask = set of (tmm_Color, tmm_Name, tmm_Size, tmm_Styles); + { TCustomRichMemo } TCustomRichMemo = class(TCustomMemo) @@ -21,12 +32,15 @@ type procedure UpdateRichMemo; virtual; procedure SetHideSelection(AValue: Boolean); public - procedure SetTextAttributes(TextStart, TextLen: Integer; SetMask: TTextStyleMask; const TextParams: TFontParams); virtual; + procedure SetTextAttributes(TextStart, TextLen: Integer; const TextParams: TFontParams); virtual; function GetTextAttributes(TextStart: Integer; var TextParams: TFontParams): Boolean; virtual; function GetStyleRange(TextStart: Integer; var RangeStart, RangeLen: Integer): Boolean; virtual; procedure SetTextAttributes(TextStart, TextLen: Integer; AFont: TFont); function GetStyleLength(TextStart: Integer): Integer; + procedure SetRangeColor(TextStart, TextLength: Integer; FontColor: TColor); + procedure SetRangeParams(TextStart, TextLength: Integer; ModifyMask: TTextModifyMask; + const FontName: String; FontSize: Integer; FontColor: TColor; AddFontStyle, RemoveFontStyle: TFontStyles); function LoadRichText(Source: TStream): Boolean; virtual; function SaveRichText(Dest: TStream): Boolean; virtual; @@ -91,9 +105,6 @@ type property WordWrap; end; -const - TextStyleAll : TTextStyleMask = [tsm_Color, tsm_Name, tsm_Size, tsm_Styles]; - function GetFontParams(styles: TFontStyles): TFontParams; overload; function GetFontParams(color: TColor; styles: TFontStyles): TFontParams; overload; function GetFontParams(const Name: String; color: TColor; styles: TFontStyles): TFontParams; overload; @@ -160,14 +171,14 @@ begin params.Color := AFont.Color; params.Size := AFont.Size; params.Style := AFont.Style; - SetTextAttributes(TextStart, TextLen, TextStyleAll, params); + SetTextAttributes(TextStart, TextLen, {TextStyleAll,} params); end; procedure TCustomRichMemo.SetTextAttributes(TextStart, TextLen: Integer; - SetMask: TTextStyleMask; const TextParams: TFontParams); + {SetMask: TTextStyleMask;} const TextParams: TFontParams); begin if HandleAllocated then - TWSCustomRichMemoClass(WidgetSetClass).SetTextAttributes(Self, TextStart, TextLen, SetMask, TextParams); + TWSCustomRichMemoClass(WidgetSetClass).SetTextAttributes(Self, TextStart, TextLen, {SetMask,} TextParams); end; function TCustomRichMemo.GetTextAttributes(TextStart: Integer; var TextParams: TFontParams): Boolean; @@ -194,10 +205,42 @@ function TCustomRichMemo.GetStyleLength(TextStart: Integer): Integer; var ofs, len : Integer; begin - if GetStyleRange(TextStart, ofs, len) then - Result := len - (TextStart-ofs) - else - Result := 0; + if GetStyleRange(TextStart, ofs, len) then Result := len - (TextStart-ofs) + else Result := 1; + if Result = 0 then Result := 1; +end; + +procedure TCustomRichMemo.SetRangeColor(TextStart, TextLength: Integer; FontColor: TColor); +begin + SetRangeParams(TextStart, TextLength, [tmm_Color], '', 0, FontColor, [], []); +end; + +procedure TCustomRichMemo.SetRangeParams(TextStart, TextLength: Integer; ModifyMask: TTextModifyMask; + const FontName: String; FontSize: Integer; FontColor: TColor; AddFontStyle, RemoveFontStyle: TFontStyles); +var + i : Integer; + j : Integer; + l : Integer; + p : TFontParams; +begin + if (ModifyMask = []) or (TextLength = 0) then Exit; + + i := TextStart; + j := TextStart + TextLength; + while i < j do begin + GetTextAttributes(i, p); + + if tmm_Name in ModifyMask then p.Name := FontName; + if tmm_Color in ModifyMask then p.Color := FontColor; + if tmm_Size in ModifyMask then p.Size := FontSize; + if tmm_Styles in ModifyMask then p.Style := p.Style + AddFontStyle - RemoveFontStyle; + + l := GetStyleLength(i); + if i + l > j then l := j - i; + if l = 0 then l := 1; + SetTextAttributes(i, l, p); + inc(i, l); + end; end; diff --git a/components/richmemo/richmemotypes.pas b/components/richmemo/richmemotypes.pas index fc71646c0..ef7faca41 100644 --- a/components/richmemo/richmemotypes.pas +++ b/components/richmemo/richmemotypes.pas @@ -1,4 +1,4 @@ -unit RichMemoTypes; +unit RichMemoTypes; //todo: remove {$mode objfpc}{$H+} @@ -7,16 +7,6 @@ interface uses Graphics; -type - TTextStyleMask = set of (tsm_Color, tsm_Name, tsm_Size, tsm_Styles); - - TFontParams = record - Name : String; - Size : Integer; - Color : TColor; - Style : TFontStyles; - end; - implementation end. diff --git a/components/richmemo/samples/testsimple/project1.lpi b/components/richmemo/samples/testsimple/project1.lpi index 8a70ae38f..3b901f5f3 100644 --- a/components/richmemo/samples/testsimple/project1.lpi +++ b/components/richmemo/samples/testsimple/project1.lpi @@ -40,7 +40,7 @@ - + @@ -49,10 +49,10 @@ - - + + - + @@ -60,84 +60,84 @@ - + - + - + - + - + - + - + - + - + - + - + - + @@ -148,7 +148,7 @@ - + @@ -159,81 +159,81 @@ - + - + - + - + - + - + - + - + - + - + - + - + @@ -241,7 +241,7 @@ - + @@ -250,16 +250,16 @@ - + - + - + @@ -272,26 +272,26 @@ - + - + - + - + @@ -299,13 +299,13 @@ - + - + @@ -313,130 +313,130 @@ - + - - + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/components/richmemo/samples/testsimple/unit1.lfm b/components/richmemo/samples/testsimple/unit1.lfm index b5a2a06e0..7c48c9e5a 100644 --- a/components/richmemo/samples/testsimple/unit1.lfm +++ b/components/richmemo/samples/testsimple/unit1.lfm @@ -1,7 +1,7 @@ object Form1: TForm1 - Left = 333 + Left = 344 Height = 596 - Top = 22 + Top = 51 Width = 751 Align = alNone AllowDropFiles = False @@ -28,9 +28,9 @@ object Form1: TForm1 LCLVersion = '0.9.27' WindowState = wsNormal object RichMemo1: TRichMemo - Left = 40 + Left = 24 Height = 432 - Top = 32 + Top = 24 Width = 704 Align = alNone Alignment = taLeftJustify diff --git a/components/richmemo/samples/testsimple/unit1.lrs b/components/richmemo/samples/testsimple/unit1.lrs index 0e20ab61c..e048cac2d 100644 --- a/components/richmemo/samples/testsimple/unit1.lrs +++ b/components/richmemo/samples/testsimple/unit1.lrs @@ -1,7 +1,7 @@ { This is an automatically generated lazarus resource file } LazarusResources.Add('TForm1','FORMDATA',[ - 'TPF0'#6'TForm1'#5'Form1'#4'Left'#3'M'#1#6'Height'#3'T'#2#3'Top'#2#22#5'Width' + 'TPF0'#6'TForm1'#5'Form1'#4'Left'#3'X'#1#6'Height'#3'T'#2#3'Top'#2'3'#5'Width' +#3#239#2#5'Align'#7#6'alNone'#14'AllowDropFiles'#8#10'AutoScroll'#9#8'AutoSi' +'ze'#8#11'BorderIcons'#11#12'biSystemMenu'#10'biMinimize'#10'biMaximize'#0#11 +'BorderStyle'#7#10'bsSizeable'#11'BorderWidth'#2#0#7'Caption'#6#5'Form1'#12 @@ -10,8 +10,8 @@ LazarusResources.Add('TForm1','FORMDATA',[ +'nabled'#9#9'FormStyle'#7#8'fsNormal'#8'OnCreate'#7#10'FormCreate'#14'Parent' +'BiDiMode'#9#10'ParentFont'#8#8'Position'#7#10'poDesigned'#13'ShowInTaskBar' +#7#9'stDefault'#14'UseDockManager'#8#10'LCLVersion'#6#6'0.9.27'#11'WindowSta' - +'te'#7#8'wsNormal'#0#9'TRichMemo'#9'RichMemo1'#4'Left'#2'('#6'Height'#3#176#1 - +#3'Top'#2' '#5'Width'#3#192#2#5'Align'#7#6'alNone'#9'Alignment'#7#13'taLeftJ' + +'te'#7#8'wsNormal'#0#9'TRichMemo'#9'RichMemo1'#4'Left'#2#24#6'Height'#3#176#1 + +#3'Top'#2#24#5'Width'#3#192#2#5'Align'#7#6'alNone'#9'Alignment'#7#13'taLeftJ' +'ustify'#7'Anchors'#11#5'akTop'#6'akLeft'#7'akRight'#8'akBottom'#0#11'Border' +'Style'#7#8'bsSingle'#5'Color'#7#8'clWindow'#10'DragCursor'#7#6'crDrag'#8'Dr' +'agKind'#7#6'dkDrag'#8'DragMode'#7#8'dmManual'#7'Enabled'#9#13'HideSelection' diff --git a/components/richmemo/samples/testsimple/unit1.pas b/components/richmemo/samples/testsimple/unit1.pas index 62afe1aea..84114c461 100644 --- a/components/richmemo/samples/testsimple/unit1.pas +++ b/components/richmemo/samples/testsimple/unit1.pas @@ -48,8 +48,7 @@ procedure TForm1.Button1Click(Sender: TObject); begin Caption := Format('sel start %d, sel length %d', [RichMemo1.SelStart, RichMemo1.SelLength]); RichMemo1.SetTextAttributes( - RichMemo1.SelStart, RichMemo1.SelLength, - [tsm_Color, tsm_Styles], GetFontParams(clRed, [fsBold]) ); + RichMemo1.SelStart, RichMemo1.SelLength, GetFontParams(clRed, [fsBold]) ); end; procedure TForm1.Button2Click(Sender: TObject); diff --git a/components/richmemo/wsrichmemo.pas b/components/richmemo/wsrichmemo.pas index 49fe6c6ff..90c6ebec4 100644 --- a/components/richmemo/wsrichmemo.pas +++ b/components/richmemo/wsrichmemo.pas @@ -9,28 +9,33 @@ uses Graphics, Controls, - RichMemoTypes, - WSStdCtrls; type + + TIntFontParams = record + Name : String; + Size : Integer; + Color : TColor; + Style : TFontStyles; + end; + + { TWSCustomRichMemo } TWSCustomRichMemo = class(TWSCustomMemo) published class function GetStyleRange(const AWinControl: TWinControl; TextStart: Integer; var RangeStart, RangeLen: Integer): Boolean; virtual; class function GetTextAttributes(const AWinControl: TWinControl; TextStart: Integer; - var Params: TFontParams): Boolean; virtual; + var Params: TIntFontParams): Boolean; virtual; class procedure SetTextAttributes(const AWinControl: TWinControl; TextStart, TextLen: Integer; - Mask: TTextStyleMask; const Params: TFontParams); virtual; + {Mask: TTextStyleMask;} const Params: TIntFontParams); virtual; class procedure SetHideSelection(const AWinControl: TWinControl; AHideSelection: Boolean); virtual; class function LoadRichText(const AWinControl: TWinControl; Source: TStream): Boolean; virtual; class function SaveRichText(const AWinControl: TWinControl; Dest: TStream): Boolean; virtual; end; TWSCustomRichMemoClass = class of TWSCustomRichMemo; - - function WSRegisterCustomRichMemo: Boolean; external name 'WSRegisterCustomRichMemo'; @@ -47,14 +52,14 @@ begin end; class function TWSCustomRichMemo.GetTextAttributes(const AWinControl: TWinControl; - TextStart: Integer; var Params: TFontParams): Boolean; + TextStart: Integer; var Params: TIntFontParams): Boolean; begin Result := false; end; class procedure TWSCustomRichMemo.SetTextAttributes(const AWinControl: TWinControl; TextStart, TextLen: Integer; - Mask: TTextStyleMask; const Params: TFontParams); + {Mask: TTextStyleMask;} const Params: TIntFontParams); begin end;