From f892042ca9b48f56dcafe02dc59a1c552b1300d2 Mon Sep 17 00:00:00 2001 From: skalogryz Date: Tue, 17 Mar 2015 01:52:51 +0000 Subject: [PATCH] richmemo: renamed tab stop alignment to left and right, implemented for cocoa git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4041 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- components/richmemo/cocoa/cocoarichmemo.pas | 106 +++++++++++++++++++- components/richmemo/richmemo.pas | 2 +- 2 files changed, 106 insertions(+), 2 deletions(-) diff --git a/components/richmemo/cocoa/cocoarichmemo.pas b/components/richmemo/cocoa/cocoarichmemo.pas index 9bcd0485c..e0b90c428 100644 --- a/components/richmemo/cocoa/cocoarichmemo.pas +++ b/components/richmemo/cocoa/cocoarichmemo.pas @@ -24,6 +24,13 @@ type const AAlign: TIntParaAlignment); override; class function GetParaAlignment(const AWinControl: TWinControl; TextStart: Integer; var AAlign: TIntParaAlignment): Boolean; override; + + class procedure SetParaTabs(const AWinControl: TWinControl; TextStart, TextLen: Integer; + const AStopList: TTabStopList); override; + class function GetParaTabs(const AWinControl: TWinControl; TextStart: integer; + var AStopList: TTabStopList): Boolean; override; + + class procedure InDelText(const AWinControl: TWinControl; const TextUTF8: String; DstStart, DstLen: Integer); override; class function LoadRichText(const AWinControl: TWinControl; Source: TStream): Boolean; override; @@ -31,7 +38,6 @@ type implementation - function MemoTextView(AWinControl: TWinControl): TCocoaTextView; begin if not Assigned(AWinControl) or (AWinControl.Handle=0) then @@ -40,6 +46,21 @@ begin Result := TCocoaTextView(NSScrollView(AWinControl.Handle).documentView); end; +function ParaRange(txt: NSString; textOffset, TextLen: Integer): NSRange; overload; +begin + Result.location:=textOffset; + if textOffset+TextLen>txt.length then + Result.length:=txt.length-textOffset + else + Result.length:=TextLen; + Result:=txt.paragraphRangeForRange(Result); +end; + +function ParaRange(txt: NSTextStorage; textOffset, TextLen: Integer): NSRange; inline; overload; +begin + Result:=ParaRange(txt.string_, textOffset, textLen); +end; + { TCocoaWSCustomRichMemo } class procedure TCocoaWSCustomRichMemo.SetParaAlignment( @@ -105,6 +126,87 @@ begin Result:=true; end; +class procedure TCocoaWSCustomRichMemo.SetParaTabs( + const AWinControl: TWinControl; TextStart, TextLen: Integer; + const AStopList: TTabStopList); +var + view : TCocoaTextView; + txt : NSTextStorage; + par : NSMutableParagraphStyle; + tabs : NSMutableArray; + tab : NSTextTab; + dict : NSDictionary; + i : Integer; + rng : NSRange; +const + TabAlignMap : array [TTabAlignment] of NSTextTabType = ( + NSLeftTabStopType, // taLeft, + NSCenterTabStopType, // taCenter, + NSRightTabStopType, // taRight, + NSDecimalTabStopType, // taDecimal + NSLeftTabStopType // taWordBar - not supported + ); +begin + view:=MemoTextView(AWinControl); + if not Assigned(view) then Exit; + + par := NSParagraphStyle.defaultParagraphStyle.mutableCopyWithZone(nil); + if AStopList.Count>0 then begin + tabs := NSMutableArray.alloc.init; + for i:=0 to AStopList.Count-1 do begin + tab := NSTextTab.alloc.initWithType_location( TabAlignMap[AStopList.Tabs[i].Align], AStopList.Tabs[i].Offset ); + tabs.addObject( tab ); + tab.release; + end; + end; + par.setTabStops(tabs); + txt := view.textStorage; + txt.addAttribute_value_range( NSParagraphStyleAttributeName, par, ParaRange(txt, TextStart, textLen)); + par.release; +end; + +class function TCocoaWSCustomRichMemo.GetParaTabs( + const AWinControl: TWinControl; TextStart: integer; + var AStopList: TTabStopList): Boolean; +var + view : TCocoaTextView; + txt : NSTextStorage; + par : NSParagraphStyle; + tabs : NSArray; + dict : NSDictionary; + tab : NSTextTab; + i : Integer; +begin + InitTabStopList(AStopList); + view:=MemoTextView(AWinControl); + Result:=false; + if not Assigned(view) then Exit; + + txt:=view.textStorage; + dict := txt.attributesAtIndex_effectiveRange(textStart, nil); + if not Assigned(dict) then Exit; + + par:=NSParagraphStyle( dict.objectForKey(NSParagraphStyleAttributeName) ); + if not Assigned(par) then + par:=NSParagraphStyle.defaultParagraphStyle; + + tabs:=par.tabStops; + if not Assigned(par) then Exit; + AStopList.Count:=tabs.count; + SetLength(AStopList.Tabs, AStopList.Count); + for i:=0 to tabs.count-1 do begin + tab:=NSTextTab(tabs.objectAtIndex(i)); + AStopList.Tabs[i].Offset:=tab.location; + case tab.tabStopType of + NSCenterTabStopType: AStopList.Tabs[i].Align:= taCenter; + NSRightTabStopType: AStopList.Tabs[i].Align:= taRight; + NSDecimalTabStopType: AStopList.Tabs[i].Align:= taDecimal; + else + AStopList.Tabs[i].Align:=taLeft; + end; + end; +end; + class procedure TCocoaWSCustomRichMemo.InDelText( const AWinControl: TWinControl; const TextUTF8: String; DstStart, DstLen: Integer); @@ -128,6 +230,7 @@ var txt : TCocoaTextView; begin //todo: avoid copying data. + Result:=false; if not Assigned(Source) or not Assigned(AWinControl) or (AWinControl.Handle=0) then Exit; txt:=MemoTextView(AWinControl); @@ -139,6 +242,7 @@ begin txt.replaceCharactersInRange_withRTF(rng, data); data.release; end; + Result:=true; end; end. diff --git a/components/richmemo/richmemo.pas b/components/richmemo/richmemo.pas index 137d53e1d..ba146c64f 100644 --- a/components/richmemo/richmemo.pas +++ b/components/richmemo/richmemo.pas @@ -91,7 +91,7 @@ type end; type - TTabAlignment = (taHead, taCenter, taTail, taDecimal, taWordBar); + TTabAlignment = (taLeft, taCenter, taRight, taDecimal, taWordBar); TTabStop = record Offset : Double;