diff options
author | graemeg <graemeg@ae50a9b5-8222-0410-bf8d-8a13f76226bf> | 2008-02-22 07:19:55 +0000 |
---|---|---|
committer | graemeg <graemeg@ae50a9b5-8222-0410-bf8d-8a13f76226bf> | 2008-02-22 07:19:55 +0000 |
commit | 02cee74ef7b81934fc66a176e15517f99f42344c (patch) | |
tree | 07bb545dec5b4fefe41ecdb1116a452a3480cfa1 | |
parent | 17ccab892612ec12ff7950b5ade495c053a344a2 (diff) | |
download | fpGUI-02cee74ef7b81934fc66a176e15517f99f42344c.tar.xz |
* Applied Label patch from Jean-Marc which adds WrapText and Alignment properties to the label component.
-rw-r--r-- | src/gui/gui_combobox.pas | 2 | ||||
-rw-r--r-- | src/gui/gui_label.pas | 146 |
2 files changed, 132 insertions, 16 deletions
diff --git a/src/gui/gui_combobox.pas b/src/gui/gui_combobox.pas index 4c4ca9d5..e98e49e1 100644 --- a/src/gui/gui_combobox.pas +++ b/src/gui/gui_combobox.pas @@ -211,7 +211,7 @@ begin Result.Focusable := True; Result.Height := 23; // replace this with font height + margins - {$Note We still need to handle the AList param as well.} + { TODO : We still need to handle the AList param as well.} end; { TfpgAbstractComboBox } diff --git a/src/gui/gui_label.pas b/src/gui/gui_label.pas index 3dde3a32..e50e3ca9 100644 --- a/src/gui/gui_label.pas +++ b/src/gui/gui_label.pas @@ -26,6 +26,7 @@ uses SysUtils, gfxbase, fpgfx, + gfx_UTF8utils, gfx_widget; type @@ -33,6 +34,12 @@ type TfpgCustomLabel = class(TfpgWidget) private FAutoSize: boolean; + FAlignment: TAlignment; + FWrapText: boolean; + FWrappedText: TStringList; + procedure Wrap(MaxLength: integer; AText: string); + procedure SetWrapText(const AValue: boolean); + procedure SetAlignment(const AValue: TAlignment); function GetFontDesc: string; procedure SetAutoSize(const AValue: boolean); procedure SetFontDesc(const AValue: string); @@ -42,6 +49,9 @@ type FText: string; FFont: TfpgFont; procedure HandlePaint; override; + property WrapText: boolean read FWrapText write SetWrapText default False; + property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify; + property Length: integer read FWidth write SetWidth; property AutoSize: boolean read FAutoSize write SetAutoSize default False; property FontDesc: string read GetFontDesc write SetFontDesc; property Text: string read FText write SetText; @@ -54,11 +64,14 @@ type TfpgLabel = class(TfpgCustomLabel) published + property Alignment; property AutoSize; property BackgroundColor; property FontDesc; + property Length; property Text; property TextColor; + property WrapText; property OnClick; property OnDoubleClick; property OnMouseDown; @@ -70,7 +83,8 @@ type // A convenience function to create a TfpgLabel instance -function CreateLabel(AOwner: TComponent; x, y: TfpgCoord; AText: string): TfpgLabel; +function CreateLabel(AOwner: TComponent; x, y: TfpgCoord; AText: string): TfpgLabel; overload; +function CreateLabel(AOwner: TComponent; x, y, w: TfpgCoord; AText: string): TfpgLabel; overload; implementation @@ -85,9 +99,71 @@ begin Result.Width := Result.Font.TextWidth(Result.Text) + 5; // 5 is some extra spacing end; +function CreateLabel(AOwner: TComponent; x, y, w: TfpgCoord; AText: string): TfpgLabel; +begin + Result := TfpgLabel.Create(AOwner); + Result.Left := x; + Result.Top := y; + Result.Text := AText; + if w = 0 then + begin + Result.Width := Result.Font.TextWidth(Result.Text) + 5; // 5 is some extra spacing + Result.FAutoSize := True; + end + else + begin + Result.Width := w; + Result.WrapText := True; + end; +end; + { TfpgCustomLabel } +procedure TfpgCustomLabel.Wrap(MaxLength: integer; AText: string); +begin + FWrappedText.Clear; + repeat + if UTF8Pos(' ', AText) > 0 then + if Font.TextWidth(UTF8Copy(AText, 1, UTF8Pos(' ', AText))) < MaxLength then + begin + if FWrappedText.Count > 0 then + if (Font.TextWidth(FWrappedText[Pred(FWrappedText.Count)] + ' ' + + UTF8Copy(AText, 1, UTF8Pos(' ', AText)))) < MaxLength then + FWrappedText[Pred(FWrappedText.Count)] := FWrappedText[Pred(FWrappedText.Count)] + ' ' + + UTF8Copy(AText, 1, Pred(UTF8Pos(' ', AText))) + else + FWrappedText.Add(UTF8Copy(AText, 1, Pred(UTF8Pos(' ', AText)))) + else + FWrappedText.Add(UTF8Copy(AText, 1, Pred(UTF8Pos(' ', AText)))); + AText := UTF8Copy(AText, Succ(UTF8Pos(' ', AText)), UTF8Length(AText) - Pred(UTF8Pos(' ', AText))); + end; + until UTF8Pos(' ', AText) = 0; + if (Font.TextWidth(FWrappedText[Pred(FWrappedText.Count)] + ' ' + AText)) < MaxLength then + FWrappedText[Pred(FWrappedText.Count)] := FWrappedText[Pred(FWrappedText.Count)] + ' ' + AText + else + FWrappedText.Add(AText); + Height := FWrappedText.Count * (Font.Height + 2); +end; + +procedure TfpgCustomLabel.SetWrapText(const AValue: boolean); +begin + FWrapText := AValue; + if FWrapText then + Wrap(Width, FText) + else + Height := FFont.Height; +end; + +procedure TfpgCustomLabel.SetAlignment(const AValue: TAlignment); +begin + if FAlignment = AValue then + Exit; + FAlignment := AValue; + if FAlignment <> taLeftJustify then + FAutoSize := False; +end; + function TfpgCustomLabel.GetFontDesc: string; begin Result := FFont.FontDesc; @@ -99,19 +175,15 @@ begin Exit; //==> FAutoSize := AValue; if FAutoSize then - begin - ResizeLabel; - RePaint; - end; + FAlignment := taLeftJustify; + ResizeLabel; end; procedure TfpgCustomLabel.SetFontDesc(const AValue: string); begin FFont.Free; FFont := fpgGetFont(AValue); - if FAutoSize then - ResizeLabel; - RePaint; + ResizeLabel; end; procedure TfpgCustomLabel.SetText(const AValue: string); @@ -119,16 +191,23 @@ begin if FText = AValue then Exit; //==> FText := AValue; - if FAutoSize then - ResizeLabel; - RePaint; + ResizeLabel; end; procedure TfpgCustomLabel.ResizeLabel; begin - Width := FFont.TextWidth(FText); - Height := FFont.Height; - SetPosition(Left, Top, Width, Height); + if FAutoSize then + begin + Width := FFont.TextWidth(FText); + Height := FFont.Height; + SetPosition(Left, Top, Width, Height); + end + else + if FWrapText then + Wrap(Width, FText) + else + Height := FFont.Height; + RePaint; end; constructor TfpgCustomLabel.Create(AOwner: TComponent); @@ -141,23 +220,60 @@ begin FTextColor := Parent.TextColor; FBackgroundColor := Parent.BackgroundColor; FAutoSize := False; + + FAlignment := taLeftJustify; + FWrapText := False; + FWrappedText := TStringList.Create; end; destructor TfpgCustomLabel.Destroy; begin FText := ''; FFont.Free; + FWrappedText.Free; inherited Destroy; end; procedure TfpgCustomLabel.HandlePaint; +var + i: integer; + r: TfpgRect; begin Canvas.BeginDraw; inherited; + Canvas.ClearClipRect; + r.SetRect(0, 0, Width, Height); Canvas.Clear(FBackgroundColor); Canvas.SetFont(Font); Canvas.SetTextColor(FTextColor); - fpgStyle.DrawString(Canvas, 0, 0, FText, Enabled); + if WrapText then + begin + if FWrappedText.Count> 0 then + for i:= 0 to Pred(FWrappedText.Count) do + case FAlignment of + taLeftJustify: + fpgStyle.DrawString(Canvas, 0, (Font.Height + 2) * i, FWrappedText[i], Enabled); + + taRightJustify: + fpgStyle.DrawString(Canvas, Width - Font.TextWidth(FWrappedText[i]), (Font.Height + 2) * i, + FWrappedText[i], Enabled); + + taCenter: + fpgStyle.DrawString(Canvas, (Width - Font.TextWidth(FWrappedText[i])) div 2, (Font.Height + 2) * i, + FWrappedText[i], Enabled); + end; + end + else + case FAlignment of + taLeftJustify: + fpgStyle.DrawString(Canvas, 0, 0, FText, Enabled); + + taRightJustify: + fpgStyle.DrawString(Canvas, Width - Font.TextWidth(FText), 0, FText, Enabled); + + taCenter: + fpgStyle.DrawString(Canvas, (Width - Font.TextWidth(FText)) div 2, 0, FText, Enabled); + end; Canvas.EndDraw; end; |