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 267 268 269 270 271
|
unit MyxBaseForm;
interface
uses
GnuGetText, TntForms, Classes, Grt, TntSysUtils, Forms, AuxFuncs,
SysUtils, MyxOptions;
type
TMyxBaseForm = class(TTntForm)
private
FWindowPositionRestore: Boolean;
FWindowPositionRestoreSize: Boolean;
protected
function GetGrt: TGrt; virtual;
procedure RestoreWindowPosition;
procedure StoreWindowPosition;
public
constructor Create(AOwner: TComponent); overload; override;
constructor Create(AOwner: TComponent; RestoreWindowPos: Boolean; RestoreWindowSize: Boolean); reintroduce; overload;
destructor Destroy; override;
class procedure StoreWindowPositions(FileName: WideString = '');
property Grt: TGrt read GetGrt;
end;
//----------------------------------------------------------------------------------------------------------------------
implementation
uses
Math;
//----------------------------------------------------------------------------------------------------------------------
constructor TMyxBaseForm.Create(AOwner: TComponent);
begin
Create(AOwner, True, True);
end;
//----------------------------------------------------------------------------------------------------------------------
constructor TMyxBaseForm.Create(AOwner: TComponent; RestoreWindowPos: Boolean; RestoreWindowSize: Boolean);
begin
inherited Create(AOwner);
// Make sure GRT is available
if (Grt = nil) then
Abort;
if (AuxFuncs.DefaultFontName = '') then
begin
AuxFuncs.DefaultFontName :=
CommonOptions.OptionString['DefaultFontName'];
AuxFuncs.DefaultFontHeight :=
StrToIntDef(CommonOptions.OptionString['DefaultFontHeight'], 8);
end;
InitForm(self);
FWindowPositionRestore := RestoreWindowPos;
FWindowPositionRestoreSize := RestoreWindowSize;
RestoreWindowPosition;
end;
// -----------------------------------------------------------------------------
destructor TMyxBaseForm.Destroy;
begin
inherited;
end;
// -----------------------------------------------------------------------------
function TMyxBaseForm.GetGrt: TGrt;
begin
Result := RuntimeEnvironment;
end;
// -----------------------------------------------------------------------------
procedure TMyxBaseForm.RestoreWindowPosition;
var
PFormPositions,
PFormPosition: Pointer;
PosLeft,
PosTop,
PosWidth,
PosHeight: Integer;
FileName: WideString;
begin
// make sure the form can be set on the complete desktop
DefaultMonitor := dmDesktop;
PFormPositions := Grt.Global['/app/formPositions'];
PFormPosition := nil;
if (PFormPositions = nil) then
begin
// Make sure /app exists
GlobalAppDict;
FileName := CommonOptions.OptionString['UserDataDir'] +
TntApplication.Title + ' Window Positions.xml';
if (FileExists(FileName)) then
PFormPositions := Grt.ValueLoadFromFile(FileName)
else
PFormPositions := Grt.DictNewTyped(GrtListValue,
'forms.Position');
Grt.Global['/app/formPositions'] := PFormPositions;
end;
if (PFormPositions <> nil) then
PFormPosition := Grt.DictItem[PFormPositions, Name];
if (PFormPosition <> nil) then
begin
PosLeft := Grt.DictInt[PFormPosition, 'left'];
PosTop := Grt.DictInt[PFormPosition, 'top'];
PosWidth := Grt.DictInt[PFormPosition, 'width'];
PosHeight := Grt.DictInt[PFormPosition, 'height'];
if FWindowPositionRestoreSize then
begin
Width := Max(100, PosWidth);
Height := Max(40, PosHeight);
end;
if FWindowPositionRestore then
begin
// Make sure the form is fully visible on the current desktop (or at least most of it).
if PosLeft + Width > Screen.DesktopWidth then
PosLeft := Screen.DesktopWidth - Width;
if PosTop + Height > Screen.DesktopHeight then
PosTop := Screen.DesktopHeight - Height;
Left := Max(0, PosLeft);
Top := Max(0, PosTop);
end;
WindowState := TWindowState(Grt.DictInt[PFormPosition, 'state']);
end
else
begin
if (Screen.Width >= 1024) then
begin
Left := (Screen.Width + Width) div 2 - Width;
Top := (Screen.Height + Height) div 2 - Height;
end
else
begin
Left := (Screen.Width + Width) div 2 - Width;
Top := 0;
WindowState := wsMaximized;
end;
end;
end;
// -----------------------------------------------------------------------------
procedure TMyxBaseForm.StoreWindowPosition;
var
PApp,
PFormPositions,
PFormPosition,
POldFormPos: Pointer;
begin
PApp := Grt.Global['/app'];
if (PApp = nil) then
begin
PApp := Grt.DictNew('');
Grt.Global['/app'] := PApp;
end;
PFormPositions := Grt.DictItem[PApp, 'formPositions'];
if (PFormPositions = nil) then
begin
PFormPositions :=
Grt.DictNewTyped(GrtDictValue, 'forms.Position');
end;
PFormPosition := Grt.DictNew('forms.Position');
Grt.DictInt[PFormPosition, 'state'] := Ord(WindowState);
Grt.DictInt[PFormPosition, 'monitor'] := Monitor.MonitorNum;
if (WindowState <> wsMaximized) then
begin
Grt.DictInt[PFormPosition, 'left'] := Left;
Grt.DictInt[PFormPosition, 'top'] := Top;
Grt.DictInt[PFormPosition, 'width'] := Width;
Grt.DictInt[PFormPosition, 'height'] := Height;
end
else
begin
POldFormPos := Grt.DictItem[PFormPositions, name];
// make sure to keep existing pos + size when
// the form is maximized
if (POldFormPos <> nil) then
begin
Grt.DictInt[PFormPosition, 'left'] :=
Grt.DictInt[POldFormPos, 'left'];
Grt.DictInt[PFormPosition, 'top'] :=
Grt.DictInt[POldFormPos, 'top'];
Grt.DictInt[PFormPosition, 'width'] :=
Grt.DictInt[POldFormPos, 'width'];
Grt.DictInt[PFormPosition, 'height'] :=
Grt.DictInt[POldFormPos, 'height'];
if (Grt.DictInt[PFormPosition, 'left'] <
Screen.Monitors[Monitor.MonitorNum].Left) or
(Grt.DictInt[PFormPosition, 'left'] >=
Screen.Monitors[Monitor.MonitorNum].Left +
Screen.Monitors[Monitor.MonitorNum].Width) then
begin
Grt.DictInt[PFormPosition, 'left'] :=
Screen.Monitors[Monitor.MonitorNum].Left;
Grt.DictInt[PFormPosition, 'top'] :=
Screen.Monitors[Monitor.MonitorNum].Top;
end;
end;
end;
// store position
Grt.DictItem[PFormPositions, Name] := PFormPosition;
end;
// -----------------------------------------------------------------------------
class procedure TMyxBaseForm.StoreWindowPositions(FileName: WideString);
var
Grt: TGrt;
var
I: Integer;
begin
// Close all application forms, if they can be closed
for I := 0 to Screen.FormCount - 1 do
if (Screen.Forms[I] is TMyxBaseForm) then
TMyxBaseForm(Screen.Forms[I]).StoreWindowPosition;
Grt := RuntimeEnvironment;
if (FileName = '') then
FileName := CommonOptions.OptionString['UserDataDir'] +
TntApplication.Title + ' Window Positions.xml';
WideForceDirectories(WideExtractFilePath(FileName));
Grt.ValueSaveToFile(FileName, Grt.Global['/app/formPositions']);
end;
//----------------------------------------------------------------------------------------------------------------------
end.
|