Ignore:
Timestamp:
Dec 30, 2018, 1:01:14 AM (5 years ago)
Author:
chronos
Message:
  • Added: Close button to window title bar.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/overos/UControls.pas

    r21 r22  
    1414  TControl = class
    1515  private
     16    FOnClick: TNotifyEvent;
    1617    FParentControl: TControl;
     18    FRectangle: TRectangle;
    1719    FVisible: Boolean;
     20    function GetPosition: TPosition;
     21    function GetSize: TSize;
    1822    procedure SetParentControl(AValue: TControl);
    19     procedure SetVisible(AValue: Boolean);
     23    procedure SetPosition(AValue: TPosition);
     24    procedure SetSize(AValue: TSize);
     25  protected
     26    procedure SetRectangle(AValue: TRectangle); virtual;
     27    procedure SetVisible(AValue: Boolean); virtual;
    2028  public
    2129    Canvas: TCanvas;
    22     Rectangle: TRectangle;
    2330    Controls: TFPGObjectList<TControl>;
    2431    procedure MouseButtonDown(Pos: TPosition; Button: TMouseButton); virtual;
     
    3037    property ParentControl: TControl read FParentControl write SetParentControl;
    3138    property Visible: Boolean read FVisible write SetVisible;
     39    property Position: TPosition read GetPosition write SetPosition;
     40    property Size: TSize read GetSize write SetSize;
     41    property Rectangle: TRectangle read FRectangle write SetRectangle;
     42    property OnClick: TNotifyEvent read FOnClick write FOnClick;
    3243  end;
    3344
     
    3950    procedure DrawArea(Rect: TRectangle; Color: TColor); override;
    4051    procedure DrawText(P: TPosition; Color: TColor; Text: string); override;
     52    function GetTextSize(Text: string): TSize; override;
    4153  end;
    4254
     
    4759    FClicked: Boolean;
    4860    FTitle: string;
    49     procedure MouseButtonDown(Pos: TPosition; Button: TMouseButton); override;
    50     procedure MouseButtonUp(Pos: TPosition; Button: TMouseButton); override;
    5161    procedure SetClicked(AValue: Boolean);
    5262    procedure SetTitle(AValue: string);
    5363  public
     64    procedure MouseButtonDown(Pos: TPosition; Button: TMouseButton); override;
     65    procedure MouseButtonUp(Pos: TPosition; Button: TMouseButton); override;
    5466    procedure Paint; override;
    5567    property Clicked: Boolean read FClicked write SetClicked;
     
    8597procedure TCanvasControl.DrawLine(P1, P2: TPosition; Color: TColor);
    8698begin
    87   Control.ParentControl.Canvas.DrawLine(P1 + Control.Rectangle.Position, P2 + Control.Rectangle.Position, Color);
     99  if Assigned(Control) and Assigned(Control.ParentControl) then
     100    Control.ParentControl.Canvas.DrawLine(P1 + Control.Rectangle.Position, P2 + Control.Rectangle.Position, Color);
    88101end;
    89102
    90103procedure TCanvasControl.DrawArea(Rect: TRectangle; Color: TColor);
    91104begin
    92   Control.ParentControl.Canvas.DrawArea(TRectangle.Create(Rect.Position + Control.Rectangle.Position,
    93     Rect.Size), Color);
     105  if Assigned(Control) and Assigned(Control.ParentControl) then
     106    Control.ParentControl.Canvas.DrawArea(TRectangle.Create(Rect.Position + Control.Rectangle.Position,
     107      Rect.Size), Color);
    94108end;
    95109
    96110procedure TCanvasControl.DrawText(P: TPosition; Color: TColor; Text: string);
    97111begin
    98   Control.ParentControl.Canvas.DrawText(P + Control.Rectangle.Position, Color, Text);
     112  if Assigned(Control) and Assigned(Control.ParentControl) then
     113    Control.ParentControl.Canvas.DrawText(P + Control.Rectangle.Position, Color, Text);
     114end;
     115
     116function TCanvasControl.GetTextSize(Text: string): TSize;
     117begin
     118  if Assigned(Control) and Assigned(Control.ParentControl) then
     119    Result := Control.ParentControl.Canvas.GetTextSize(Text);
    99120end;
    100121
     
    105126  if FVisible = AValue then Exit;
    106127  FVisible := AValue;
    107   Paint;
     128  if not FVisible then begin
     129    if Assigned(ParentControl) then ParentControl.Paint;
     130  end else Paint;
    108131end;
    109132
     
    128151      Break;
    129152    end;
     153  if Assigned(FOnClick) then
     154    FOnClick(Self);
    130155end;
    131156
     
    151176end;
    152177
     178function TControl.GetPosition: TPosition;
     179begin
     180  Result := FRectangle.Position;
     181end;
     182
     183function TControl.GetSize: TSize;
     184begin
     185  Result := FRectangle.Size;
     186end;
     187
     188procedure TControl.SetPosition(AValue: TPosition);
     189begin
     190  Rectangle := TRectangle.Create(AValue, FRectangle.Size);
     191end;
     192
     193procedure TControl.SetSize(AValue: TSize);
     194begin
     195  Rectangle := TRectangle.Create(FRectangle.Position, AValue);
     196end;
     197
     198procedure TControl.SetRectangle(AValue: TRectangle);
     199begin
     200  if FRectangle = AValue then Exit;
     201  FRectangle := AValue;
     202end;
     203
    153204procedure TControl.Paint;
    154205var
    155206  I: Integer;
    156207begin
    157   for I := 0 to Controls.Count - 1 do
    158     Controls[I].Paint;
     208  if FVisible then begin
     209    for I := 0 to Controls.Count - 1 do
     210      Controls[I].Paint;
     211  end;
    159212end;
    160213
     
    168221
    169222destructor TControl.Destroy;
    170 begin
     223var
     224  I: Integer;
     225begin
     226  for I := 0 to Controls.Count - 1 do
     227    Controls[I].Free;
     228  ParentControl := nil;
    171229  Canvas.Free;
    172230  Controls.Free;
     
    185243procedure TEdit.Paint;
    186244begin
    187   Canvas.DrawArea(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), clBlack);
    188   Canvas.DrawFrame(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), clWhite);
    189   Canvas.DrawText(TPosition.Create(4, 4), clWhite, FText);
     245  if FVisible then begin
     246    Canvas.DrawArea(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), clBlack);
     247    Canvas.DrawFrame(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), clWhite);
     248    Canvas.DrawText(TPosition.Create(4, 4), clWhite, FText);
     249  end;
    190250  inherited;
    191251end;
     
    202262procedure TLabel.Paint;
    203263begin
    204   Canvas.DrawText(TPosition.Create(0, 0), clWhite, FTitle);
     264  if FVisible then begin
     265    Canvas.DrawText(TPosition.Create(0, 0), clWhite, FTitle);
     266  end;
    205267  inherited;
    206268end;
     
    237299var
    238300  Color: TColor;
    239 begin
    240   if Clicked then Color := clBlack
    241     else Color := clGray;
    242   Canvas.DrawArea(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), Color);
    243   Canvas.DrawFrame(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), clWhite);
    244   Canvas.DrawText(TPosition.Create(8, 8), clWhite, FTitle);
     301  TextSize: TSize;
     302begin
     303  if FVisible then begin
     304    if Clicked then Color := clBlack
     305      else Color := clGray;
     306    Canvas.DrawArea(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), Color);
     307    Canvas.DrawFrame(TRectangle.Create(TPosition.Create(0, 0), Rectangle.Size), clWhite);
     308    TextSize := Canvas.GetTextSize(FTitle);
     309    Canvas.DrawText(TPosition.Create((Size.Width - TextSize.Width) div 2,
     310      (Size.Height - TextSize.Height) div 2), clWhite, FTitle);
     311  end;
    245312  inherited;
    246313end;
Note: See TracChangeset for help on using the changeset viewer.