* Improved painting

* Added fonts support (for now system-type only)
 * Removed default caption value

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@2667 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
loesje_
2013-02-09 14:29:21 +00:00
parent 244b987e9b
commit ca816f94e0

View File

@ -58,6 +58,7 @@ type
bvButtonType, bvButtonType,
bvLineBreak, bvLineBreak,
bvPlaceHolder, bvPlaceHolder,
bvFont,
bvEnabled); bvEnabled);
TXIBProperty = record TXIBProperty = record
@ -73,7 +74,7 @@ const
(APropertyName: 'IBUIOpaque' ; ADefaultValue: 'YES'), (APropertyName: 'IBUIOpaque' ; ADefaultValue: 'YES'),
(APropertyName: 'IBUIHighlighted'; ADefaultValue: 'NO'), (APropertyName: 'IBUIHighlighted'; ADefaultValue: 'NO'),
(APropertyName: 'IBUIAlpha' ; ADefaultValue: '1'), (APropertyName: 'IBUIAlpha' ; ADefaultValue: '1'),
(APropertyName: 'IBUIText' ; ADefaultValue: 'Label'), (APropertyName: 'IBUIText' ; ADefaultValue: ''),
(APropertyName: 'IBUITextColor' ; ADefaultValue: ''), (APropertyName: 'IBUITextColor' ; ADefaultValue: ''),
(APropertyName: 'IBUITextAlignment' ; ADefaultValue: '0'), (APropertyName: 'IBUITextAlignment' ; ADefaultValue: '0'),
(APropertyName: 'IBUINormalTitle' ; ADefaultValue: ''), (APropertyName: 'IBUINormalTitle' ; ADefaultValue: ''),
@ -87,6 +88,7 @@ const
(APropertyName: 'IBUIButtonType' ; ADefaultValue: '0'), (APropertyName: 'IBUIButtonType' ; ADefaultValue: '0'),
(APropertyName: 'IBUILineBreakMode' ; ADefaultValue: '4'), (APropertyName: 'IBUILineBreakMode' ; ADefaultValue: '4'),
(APropertyName: 'IBUIPlaceholder' ; ADefaultValue: ''), (APropertyName: 'IBUIPlaceholder' ; ADefaultValue: ''),
(APropertyName: 'IBUIFontDescription' ; ADefaultValue: ''),
(APropertyName: 'IBUIEnabled' ; ADefaultValue: 'YES')); (APropertyName: 'IBUIEnabled' ; ADefaultValue: 'YES'));
EventNames : array[1..1] of string = ( EventNames : array[1..1] of string = (
@ -102,19 +104,24 @@ type
DisableAutoSize: boolean): TComponent; DisableAutoSize: boolean): TComponent;
end; end;
TiOSFakeFontType = (ftNotSet, ftSystem, ftSystemBold, ftSystemItalic); TiOSFakeFontType = (ftNotSet, ftSystem=1, ftSystemBold=2, ftSystemItalic=3, ftCustom=4);
{ TiOSFakeFontDescription } { TiOSFakeFontDescription }
TiOSFakeFontDescription = class(TPersistent) TiOSFakeFontDescription = class(TPersistent)
private private
FFontType: TiOSFakeFontType; FXIBObjectElement: TDOMElement;
FpointSize: double; procedure SetFont(AFontType: TiOSFakeFontType; APointSize: double);
function GetFontType: TiOSFakeFontType;
function GetpointSize: double;
procedure SetFontType(AValue: TiOSFakeFontType);
procedure SetpointSize(AValue: double);
public public
constructor Create; constructor Create;
procedure ApplyToLCLFont(AFont: TFont);
published published
property FontType: TiOSFakeFontType read FFontType write FFontType; property FontType: TiOSFakeFontType read GetFontType write SetFontType;
property pointSize: double read FpointSize write FpointSize; property pointSize: double read GetpointSize write SetpointSize;
end; end;
{ tiOSFakeComponent } { tiOSFakeComponent }
@ -135,6 +142,7 @@ type
FLeft: integer; FLeft: integer;
FXIBObjectElement: TDOMElement; FXIBObjectElement: TDOMElement;
FParent: tiOSFakeComponent; FParent: tiOSFakeComponent;
FFont: TiOSFakeFontDescription;
// iOS // iOS
procedure AddChildToDom(const AValue: tiOSFakeComponent); procedure AddChildToDom(const AValue: tiOSFakeComponent);
procedure RemoveChildFromDom(const AValue: tiOSFakeComponent); procedure RemoveChildFromDom(const AValue: tiOSFakeComponent);
@ -197,11 +205,12 @@ type
procedure SetXIBColor(index: TXIBProperties; AValue: TColor); procedure SetXIBColor(index: TXIBProperties; AValue: TColor);
function GetXIBEvent(index: integer): TCocoaEvent; function GetXIBEvent(index: integer): TCocoaEvent;
procedure SetXIBEvent(Index: integer; AValue: TCocoaEvent); procedure SetXIBEvent(Index: integer; AValue: TCocoaEvent);
function GetXIBFont(index: TXIBProperties): TiOSFakeFontDescription;
function GetNSObject: NSObject; virtual; function GetNSObject: NSObject; virtual;
// Streaming // Streaming
procedure DefineProperties(Filer: TFiler); override; procedure DefineProperties(Filer: TFiler); override;
function GetKeyNode(AParentNode: TDOMNode; NodeName, Key: string; AClass: string =''): TDOMElement; property Font: TiOSFakeFontDescription index bvFont read GetXIBFont; // write SetXIBFont;
public public
procedure InitializeDefaults; virtual; procedure InitializeDefaults; virtual;
constructor Create(AOwner: TComponent); override; constructor Create(AOwner: TComponent); override;
@ -405,6 +414,7 @@ type
procedure InitializeDefaults; override; procedure InitializeDefaults; override;
constructor Create(AOwner: TComponent); override; constructor Create(AOwner: TComponent); override;
class function GetIBClassName: string; override; class function GetIBClassName: string; override;
procedure paint(ACanvas: TCanvas); override;
property NSNextKeyView: UIView read FNSNextKeyView write FNSNextKeyView; property NSNextKeyView: UIView read FNSNextKeyView write FNSNextKeyView;
published published
@ -412,6 +422,7 @@ type
property NormalTitleColor: TColor index bvNormalTitleColor read GetXIBColor write SetXIBColor; property NormalTitleColor: TColor index bvNormalTitleColor read GetXIBColor write SetXIBColor;
property ButtonType: TiOSFakeButtonType read GetButtonType write SetButtonType; property ButtonType: TiOSFakeButtonType read GetButtonType write SetButtonType;
property onTouchDown: TCocoaEvent index 1 read GetXIBEvent write SetXIBEvent; property onTouchDown: TCocoaEvent index 1 read GetXIBEvent write SetXIBEvent;
property Font;
end; end;
{ UILabel } { UILabel }
@ -420,22 +431,19 @@ type
UILabel = class(UIView) UILabel = class(UIView)
private private
FFont: TiOSFakeFontDescription;
function GetLineBreaks: TLineBreaks; function GetLineBreaks: TLineBreaks;
procedure SetFont(AValue: TiOSFakeFontDescription);
procedure SetLineBreaks(AValue: TLineBreaks); procedure SetLineBreaks(AValue: TLineBreaks);
function GetTextAlignment: TiOSFakeAlignment; function GetTextAlignment: TiOSFakeAlignment;
procedure SetTextAlignment(AValue: TiOSFakeAlignment); procedure SetTextAlignment(AValue: TiOSFakeAlignment);
public public
constructor Create(AOwner: TComponent); override; constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
class function GetIBClassName: string; override; class function GetIBClassName: string; override;
procedure paint(ACanvas: TCanvas); override; procedure paint(ACanvas: TCanvas); override;
published published
property Lines: integer index bvLines read GetXIBInteger write SetXIBInteger; property Lines: integer index bvLines read GetXIBInteger write SetXIBInteger;
property TextAlignment: TiOSFakeAlignment read GetTextAlignment write SetTextAlignment; property TextAlignment: TiOSFakeAlignment read GetTextAlignment write SetTextAlignment;
property TextColor: TColor index bvTextColor read GetXIBColor write SetXIBColor; property TextColor: TColor index bvTextColor read GetXIBColor write SetXIBColor;
property Font: TiOSFakeFontDescription read FFont write SetFont; property Font;
property Enabled: boolean index bvEnabled read GetXIBBoolean write SetXIBBoolean; property Enabled: boolean index bvEnabled read GetXIBBoolean write SetXIBBoolean;
property Highlighted: boolean index bvHighlighted read GetXIBBoolean write SetXIBBoolean; property Highlighted: boolean index bvHighlighted read GetXIBBoolean write SetXIBBoolean;
property LineBreaks: TLineBreaks read GetLineBreaks write SetLineBreaks; property LineBreaks: TLineBreaks read GetLineBreaks write SetLineBreaks;
@ -451,10 +459,13 @@ type
public public
constructor Create(AOwner: TComponent); override; constructor Create(AOwner: TComponent); override;
class function GetIBClassName: string; override; class function GetIBClassName: string; override;
procedure paint(ACanvas: TCanvas); override;
published published
property Placeholder: string index bvPlaceHolder read GetXIBString write SetXIBString; property Placeholder: string index bvPlaceHolder read GetXIBString write SetXIBString;
property TextAlignment: TiOSFakeAlignment read GetTextAlignment write SetTextAlignment; property TextAlignment: TiOSFakeAlignment read GetTextAlignment write SetTextAlignment;
property TextColor: TColor index bvTextColor read GetXIBColor write SetXIBColor;
property caption; property caption;
property font;
end; end;
TiOSFakeSeparatorStyle = (ssNone,ssSingleLine,ssSingleLineEtched); TiOSFakeSeparatorStyle = (ssNone,ssSingleLine,ssSingleLineEtched);
@ -627,6 +638,8 @@ procedure ObtainBaseObjectInfoFromXIB(AStream: TStream; out AXMLDocument: TXMLDo
out AFilesOwnerID: int64; out AFilesOwnerID: int64;
out AXIBUsesObjectsForArrays: boolean); out AXIBUsesObjectsForArrays: boolean);
function FindKeyNode(AParentNode: TDOMNode; NodeName, Key: string): TDOMElement; function FindKeyNode(AParentNode: TDOMNode; NodeName, Key: string): TDOMElement;
function GetKeyNode(AParentNode: TDOMNode; NodeName, Key: string; AClass: string =''): TDOMElement;
function AddElement(ADomNode: TDOMElement; AName: string): TDOMElement;
implementation implementation
@ -1215,7 +1228,7 @@ begin
begin begin
FakeComp := tiOSFakeComponent(Component); FakeComp := tiOSFakeComponent(Component);
AnElement := FakeComp.GetXIBFlattenedProperties; AnElement := FakeComp.GetXIBFlattenedProperties;
AnElement := FakeComp.GetKeyNode(AnElement,'string', inttostr(FakeComp.ObjectID) +'.CustomClassName'); AnElement := GetKeyNode(AnElement,'string', inttostr(FakeComp.ObjectID) +'.CustomClassName');
AnElement.TextContent:=FakeComp.ClassName; AnElement.TextContent:=FakeComp.ClassName;
WriteXML(NSObject(Component).FNIBDocument, FStream); WriteXML(NSObject(Component).FNIBDocument, FStream);
FIsWritten:=true; FIsWritten:=true;
@ -2554,6 +2567,12 @@ begin
end; end;
function tiOSFakeComponent.GetXIBFont(index: TXIBProperties): TiOSFakeFontDescription;
begin
FFont.FXIBObjectElement := XIBObjectElement;
result := FFont;
end;
function tiOSFakeComponent.GetNSObject: NSObject; function tiOSFakeComponent.GetNSObject: NSObject;
begin begin
if assigned(Parent) then if assigned(Parent) then
@ -2626,7 +2645,7 @@ begin
end; end;
end; end;
function tiOSFakeComponent.GetKeyNode(AParentNode: TDOMNode; NodeName, Key: string; AClass: string): TDOMElement; function GetKeyNode(AParentNode: TDOMNode; NodeName, Key: string; AClass: string): TDOMElement;
begin begin
result := FindKeyNode(AParentNode, NodeName, Key); result := FindKeyNode(AParentNode, NodeName, Key);
if not assigned(result) then if not assigned(result) then
@ -2638,6 +2657,12 @@ begin
end; end;
end; end;
function AddElement(ADomNode: TDOMElement; AName: string): TDOMElement;
begin
result := ADomNode.OwnerDocument.CreateElement(AName);
ADomNode.AppendChild(result);
end;
procedure tiOSFakeComponent.SetParentComponent(Value: TComponent); procedure tiOSFakeComponent.SetParentComponent(Value: TComponent);
begin begin
if Value is tiOSFakeComponent then if Value is tiOSFakeComponent then
@ -2766,6 +2791,7 @@ constructor tiOSFakeComponent.Create(AOwner: TComponent);
begin begin
inherited Create(AOwner); inherited Create(AOwner);
FChilds:=TFPList.Create; FChilds:=TFPList.Create;
FFont := TiOSFakeFontDescription.Create;
FStoredEvents := TStringList.Create; FStoredEvents := TStringList.Create;
FStoredEvents.Sorted:=true; FStoredEvents.Sorted:=true;
FStoredEvents.Duplicates:=dupIgnore; FStoredEvents.Duplicates:=dupIgnore;
@ -2777,6 +2803,7 @@ begin
Parent:=nil; Parent:=nil;
FreeAndNil(FChilds); FreeAndNil(FChilds);
FreeAndNil(FStoredEvents); FreeAndNil(FStoredEvents);
FFont.Free;
inherited Destroy; inherited Destroy;
end; end;
@ -3097,21 +3124,146 @@ begin
Result:='IBUITextField'; Result:='IBUITextField';
end; end;
procedure UITextField.paint(ACanvas: TCanvas);
begin
inherited;
ACanvas.Font.Color:=TextColor;
Font.ApplyToLCLFont(ACanvas.Font);
ACanvas.TextOut(5,2,Caption);
end;
{ TiOSFakeFontDescription } { TiOSFakeFontDescription }
procedure TiOSFakeFontDescription.SetFont(AFontType: TiOSFakeFontType; APointSize: double);
var
ADescNode: TDOMNode;
AFontNode: TDOMNode;
ANode: TDOMNode;
begin
if Assigned(FXIBObjectElement) then
begin
ADescNode := FindKeyNode(FXIBObjectElement, 'object', 'IBUIFontDescription');
AFontNode := FindKeyNode(FXIBObjectElement, 'object', 'NSFont');
if AFontType = ftNotSet then
begin
if Assigned(ADescNode) then
ADescNode.ParentNode.RemoveChild(ADescNode);
if Assigned(AFontNode) then
AFontNode.ParentNode.RemoveChild(AFontNode);
end
else
begin
if not assigned(ADescNode) then
ADescNode := GetKeyNode(FXIBObjectElement, 'object', 'IBUIFontDescription', 'IBUIFontDescription');
if not assigned(AFontNode) then
AFontNode := GetKeyNode(FXIBObjectElement, 'object', 'IBUIFont', 'NSFont');
ANode := GetKeyNode(ADescNode,'double','pointSize');
ANode.TextContent:=FloatToStr(ApointSize);
ANode := GetKeyNode(AFontNode,'double','NSSize');
ANode.TextContent:=FloatToStr(ApointSize);
ANode := GetKeyNode(ADescNode, 'int', 'type');
ANode.TextContent:=IntToStr(ord(AFontType));
ANode := GetKeyNode(AFontNode, 'int', 'NSfFlags');
ANode.TextContent:='16';
ANode := GetKeyNode(AFontNode, 'string', 'NSName');
case AFontType of
ftSystem: ANode.TextContent:='Helvetica';
ftSystemBold: ANode.TextContent:='Helvetica-Bold';
ftSystemItalic: ANode.TextContent:='Helvetica-Oblique';
end;
end;
end;
end;
function TiOSFakeFontDescription.GetFontType: TiOSFakeFontType;
var
ANode: TDOMElement;
begin
Result := ftNotSet;
if Assigned(FXIBObjectElement) then
begin
ANode := FindKeyNode(FXIBObjectElement, 'object', 'IBUIFontDescription');
if assigned(ANode) then
begin
ANode := FindKeyNode(ANode,'int','type');
if assigned(ANode) then
Result:=TiOSFakeFontType(StrToInt(ANode.TextContent))
else
Result:=ftCustom;
end;
end
end;
function TiOSFakeFontDescription.GetpointSize: double;
var
ANode: TDOMElement;
begin
Result := -1;
if Assigned(FXIBObjectElement) then
begin
ANode := FindKeyNode(FXIBObjectElement, 'object', 'IBUIFontDescription');
if assigned(ANode) then
begin
ANode := FindKeyNode(ANode,'double','pointSize');
if assigned(ANode) then
Result:=StrToFloat(ANode.TextContent)
else
Result:=-1;
end;
end
end;
procedure TiOSFakeFontDescription.SetFontType(AValue: TiOSFakeFontType);
begin
SetFont(AValue, pointSize);
end;
procedure TiOSFakeFontDescription.SetpointSize(AValue: double);
begin
SetFont(FontType, AValue);
end;
constructor TiOSFakeFontDescription.Create; constructor TiOSFakeFontDescription.Create;
begin begin
pointSize:=12; pointSize:=12;
end; end;
{ TMyLabelButton } procedure TiOSFakeFontDescription.ApplyToLCLFont(AFont: TFont);
procedure UILabel.SetFont(AValue: TiOSFakeFontDescription);
begin begin
if FFont=AValue then Exit; if pointSize>0 then
FFont.Assign(AValue); AFont.Size:=round(pointSize)
else
AFont.Size:=12;
AFont.Name:='Helvetica';
case FontType of
ftSystemBold: begin
AFont.Bold:=true;
AFont.Italic:=False;
end;
ftSystem: begin
AFont.Bold:=false;
AFont.Italic:=False;
end;
ftSystemItalic: begin
AFont.Italic:=true;
AFont.Bold:=false;
end
else
begin
AFont.Italic:=false;
AFont.Bold:=false;
end;
end;
end; end;
{ TMyLabelButton }
function UILabel.GetLineBreaks: TLineBreaks; function UILabel.GetLineBreaks: TLineBreaks;
begin begin
result := TLineBreaks(GetXIBInteger(bvLineBreak)); result := TLineBreaks(GetXIBInteger(bvLineBreak));
@ -3153,14 +3305,6 @@ constructor UILabel.Create(AOwner: TComponent);
begin begin
inherited Create(AOwner); inherited Create(AOwner);
FAcceptChildsAtDesignTime:=false; FAcceptChildsAtDesignTime:=false;
FFont := TiOSFakeFontDescription.Create;
Enabled := true;
end;
destructor UILabel.Destroy;
begin
FFont.Free;
inherited Destroy;
end; end;
class function UILabel.GetIBClassName: string; class function UILabel.GetIBClassName: string;
@ -3170,34 +3314,8 @@ end;
procedure UILabel.paint(ACanvas: TCanvas); procedure UILabel.paint(ACanvas: TCanvas);
begin begin
Font.ApplyToLCLFont(ACanvas.Font);
ACanvas.Font.Color:=TextColor; ACanvas.Font.Color:=TextColor;
case Font.FontType of
ftSystemBold: begin
ACanvas.font.Name:='Helvetica';
ACanvas.Font.Size:=round(Font.FpointSize);
ACanvas.Font.Bold:=true;
ACanvas.Font.Italic:=False;
end;
ftSystem: begin
ACanvas.font.Name:='Helvetica';
ACanvas.Font.Size:=round(Font.FpointSize);
ACanvas.Font.Bold:=false;
ACanvas.Font.Italic:=False;
end;
ftSystemItalic: begin
ACanvas.font.Name:='Helvetica';
ACanvas.Font.Size:=round(Font.FpointSize);
ACanvas.Font.Italic:=true;
ACanvas.Font.Bold:=false;
end
else
begin
ACanvas.font.Name:='Helvetica';
ACanvas.Font.Size:=12;
ACanvas.Font.Italic:=false;
ACanvas.Font.Bold:=false;
end;
end;
ACanvas.TextOut(5,2,Caption); ACanvas.TextOut(5,2,Caption);
end; end;
@ -3411,18 +3529,21 @@ procedure UIView.paint(ACanvas: TCanvas);
begin begin
with ACanvas do with ACanvas do
begin begin
Brush.Style:=bsSolid;
Brush.Color:=BackgroundColor; Brush.Color:=BackgroundColor;
// outer frame Pen.Color:=BackgroundColor;
Pen.Color:=clRed; if (BackgroundColor = clDefault) then
begin
Brush.Style:=bsClear;
pen.Style:=psClear;
end
else
begin
Brush.Style:=bsSolid;
pen.Style:=psSolid;
end;
// Background
Rectangle(0,0,self.Width,self.Height); Rectangle(0,0,self.Width,self.Height);
// caption
Font.Color:=clBlack;
Font.Name:='Helvetica';
Font.Size:=12;
Font.Italic:=false;
Font.Bold:=false;
TextOut(5,2,GetPaintText);
end; end;
end; end;
@ -3474,5 +3595,28 @@ begin
result := 'IBUIButton'; result := 'IBUIButton';
end; end;
procedure UIButton.paint(ACanvas: TCanvas);
var
ARadius: integer;
begin
inherited;
with ACanvas do
begin
brush.Color:=clWhite;
pen.Color:=clGray;
pen.Style := psSolid;
ARadius := min(self.Width,self.Height);
ARadius := min(ARadius, 22);
RoundRect(0,0,self.Width,self.Height,ARadius, ARadius);
// caption
Font.Color:=NormalTitleColor;
self.Font.ApplyToLCLFont(Font);
TextOut(5,2,GetPaintText);
end;
end;
end. end.