File: baseform.pas

package info (click to toggle)
transgui 5.0.1-5.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 3,792 kB
  • sloc: pascal: 40,152; makefile: 2,294; cs: 347; perl: 347; lisp: 347; sh: 84
file content (266 lines) | stat: -rw-r--r-- 7,240 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
{*************************************************************************************
  This file is part of Transmission Remote GUI.
  Copyright (c) 2008-2014 by Yury Sidorov.

  Transmission Remote GUI is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  Transmission Remote GUI is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Transmission Remote GUI; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*************************************************************************************}

unit BaseForm;

{$mode objfpc}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics;

type

  { TBaseForm }

  TBaseForm = class(TForm)
  private
    FNeedAutoSize: boolean;
    procedure DoScale(C: TControl);
    procedure InitScale;
  protected
    procedure DoCreate; override;
  public
    constructor Create(TheOwner: TComponent); override;
  end;

procedure AutoSizeForm(Form: TCustomForm);
function ScaleInt(i: integer): integer;

var
  IntfScale: integer = 100;

implementation

uses LCLType, ButtonPanel, VarGrid, ComCtrls, StdCtrls, ExtCtrls, lclversion;

var
  ScaleMul, ScaleDiv: integer;

function ScaleInt(i: integer): integer;
begin
  Result:=i*ScaleMul div ScaleDiv;
end;

type THackControl = class(TWinControl) end;

procedure AutoSizeForm(Form: TCustomForm);
var
  i, ht, w, h: integer;
  C: TControl;
begin
  ht:=0;
  for i:=0 to Form.ControlCount - 1 do begin
    C:=Form.Controls[i];
    if not C.Visible then
      continue;
    with C do begin
      if C is TButtonPanel then begin
        TButtonPanel(C).HandleNeeded;
        w:=0;
        h:=0;
        THackControl(C).CalculatePreferredSize(w, h, True);
      end
      else
        h:=Height;
{$ifdef LCLcarbon}
      if C is TPageControl then
        Inc(h, ScaleInt(10));
{$endif LCLcarbon}
      Inc(ht, h + BorderSpacing.Top + BorderSpacing.Bottom + BorderSpacing.Around*2);
    end;
  end;
  ht:=ht + 2*Form.BorderWidth;

  Form.ClientHeight:=ht;
  if Form.ClientHeight <> ht then begin
    Form.Constraints.MinHeight:=0;
    Form.ClientHeight:=ht;
    Form.Constraints.MinHeight:=Form.Height;
  end;
  if Form.BorderStyle = bsDialog then begin
    Form.Constraints.MinHeight:=Form.Height;
    Form.Constraints.MinWidth:=Form.Width;
  end;
end;

{ TBaseForm }

procedure TBaseForm.DoScale(C: TControl);
var
  i: integer;
  R: TRect;
  w, h: integer;
begin
  with C do begin
{$ifdef darwin}
    if C is TButtonPanel then
      exit;
{$endif darwin}
    if C is TWinControl then
      TWinControl(C).DisableAlign;
    try
      if ScaleMul <> ScaleDiv then begin
        ScaleConstraints(ScaleMul, ScaleDiv);
        R := BaseBounds;
        R.Left := ScaleInt(R.Left);
        R.Top := ScaleInt(R.Top);
        R.Right := ScaleInt(R.Right);
        R.Bottom := ScaleInt(R.Bottom);
        if (Parent <> nil) and (Align = alNone) then begin
          if akRight in Anchors then
            Inc(R.Right, C.Parent.ClientWidth - ScaleInt(C.BaseParentClientSize.cx));
          if akBottom in Anchors then
            Inc(R.Bottom, C.Parent.ClientHeight - ScaleInt(C.BaseParentClientSize.cy));
        end;
        BoundsRect := R;
        with BorderSpacing do begin
          Top:=ScaleInt(Top);
          Left:=ScaleInt(Left);
          Bottom:=ScaleInt(Bottom);
          Right:=ScaleInt(Right);
          Around:=ScaleInt(Around);
          InnerBorder:=ScaleInt(InnerBorder);
        end;

        if C is TWinControl then
          with TWinControl(C).ChildSizing do begin
            HorizontalSpacing:=ScaleInt(HorizontalSpacing);
            VerticalSpacing:=ScaleInt(VerticalSpacing);
            LeftRightSpacing:=ScaleInt(LeftRightSpacing);
            TopBottomSpacing:=ScaleInt(TopBottomSpacing);
          end;

        if C is TButtonPanel then
          TButtonPanel(C).Spacing:=ScaleInt(TButtonPanel(C).Spacing);

        if C is TVarGrid then
          with TVarGrid(C).Columns do
            for i:=0 to Count - 1 do
               Items[i].Width:=ScaleInt(Items[i].Width);
        if C is TStatusBar then
          with TStatusBar(C) do
            for i:=0 to Panels.Count - 1 do
               Panels[i].Width:=ScaleInt(Panels[i].Width);
      end;

      // Runtime fixes

      // Fix right aligned label autosize
      if C.Visible and (C is TCustomLabel) and C.AutoSize and (TLabel(C).Alignment = taLeftJustify) and (C.Anchors*[akLeft, akRight] = [akRight]) then begin
        w:=0;
        h:=0;
        THackControl(C).CalculatePreferredSize(w, h, True);
        C.Width:=w;
      end;
{$ifdef darwin}
      // Always use standard button height on OS X for proper theming
      if C.Visible and (C is TCustomButton) then begin
        w:=0;
        h:=0;
        THackControl(C).CalculatePreferredSize(w, h, True);
        C.Height:=h;
      end;
      // Add extra top spacing for group box
      i:=ScaleInt(6);
      if C.Parent is TCustomGroupBox then
        Top:=Top + i;
      if C is TCustomGroupBox then
        with TCustomGroupBox(C).ChildSizing do
          TopBottomSpacing:=TopBottomSpacing + i;
{$endif darwin}
{$ifdef LCLgtk2}
      // Fix panel color bug on GTK2
      if (C is TCustomPanel) and ParentColor and (Color = clDefault) then
        Color:=clForm;
{$endif LCLgtk2}

      if C is TWinControl then
        with TWinControl(C) do
          for i:=0 to ControlCount - 1 do
            DoScale(Controls[i]);
    finally
      if C is TWinControl then
        TWinControl(C).EnableAlign;
    end;
  end;
end;

constructor TBaseForm.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  FNeedAutoSize:=AutoSize;
  AutoSize:=False;
end;

procedure TBaseForm.DoCreate;
{$ifdef LCLcarbon}
var
  i: integer;
  {$endif LCLcarbon}
begin
  InitScale;
  HandleNeeded;
  Font.Height:=ScaleInt(-11);
  DoScale(Self);
  if FNeedAutoSize then
    AutoSizeForm(Self);
{$ifdef LCLcarbon}
  // Destroy handles of child controls to fix the LCL Carbon bug.
  // Without this hack, it will not be possible to hide form's controls.
  for i:=0 to ControlCount - 1 do
    if Controls[i] is TWinControl then
      THackControl(Controls[i]).DestroyHandle;
{$endif LCLcarbon}
  inherited DoCreate;
end;

procedure TBaseForm.InitScale;
var
  i: integer;
  tm: TLCLTextMetric;
begin
  if ScaleDiv <> 0 then exit;
  ScaleDiv:=11;
  i:=Screen.SystemFont.Height;
  if i = 0 then begin
    if Canvas.GetTextMetrics(tm) then begin
      ScaleMul:=tm.Ascender;
      if ScaleMul < 11 then
        ScaleMul:=11;
    end
    else begin
      ScaleMul:=Canvas.TextHeight('Wy');
      ScaleDiv:=13;
    end;
    if ScaleMul = 0 then
      ScaleMul:=ScaleDiv;
  end
  else
    ScaleMul:=Abs(i);
  ScaleMul:=ScaleMul*IntfScale;
  ScaleDiv:=ScaleDiv*100;
end;

initialization
  {$I baseform.lrs}

end.