File: sparta_fakeformbackground.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (435 lines) | stat: -rw-r--r-- 12,380 bytes parent folder | download | duplicates (3)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
{
 *****************************************************************************
  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************

  Author: Maciej Izak

  DaThoX 2004-2015
  FreeSparta.com
}

unit sparta_FakeFormBackground;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil,
  // BGRAButton,
  // BGRAImageButton,
  Forms, Controls, StdCtrls, ExtCtrls, Menus, sparta_DesignedForm,
  LCLType, LMessages, PropEdits, Graphics, sparta_InterfacesMDI;

type

  { TfrFakeFormBackground }

  TfrFakeFormBackground = class(TFrame, IDesignedFormBackground)
    bBackground: TButton;
    bTop: TButton;
    bFormCaption: TButton;
    bOther: TImage;
    bResize: TImage;
    bIcon: TImage;
    bSystem: TImage;
    bMaximalize: TImage;
    bMinimalize: TImage;
    bHelp: TImage;
    eFormCaption: TEdit;
    lRight: TLabel;
    miNone: TMenuItem;
    miSingle: TMenuItem;
    miSizeable: TMenuItem;
    miDialog: TMenuItem;
    miToolWindow: TMenuItem;
    miSizeToolWin: TMenuItem;
    miAddMinimize: TMenuItem;
    miAddSystemMenu: TMenuItem;
    miAddMaximize: TMenuItem;
    miAddHelp: TMenuItem;
    miLine: TMenuItem;
    miRemove: TMenuItem;
    pmFormStyle: TPopupMenu;
    pmBorderIcons: TPopupMenu;
    procedure bFormCaptionClick(Sender: TObject);
    procedure bIconClick(Sender: TObject);
    procedure bOtherClick(Sender: TObject);
    procedure bResizeClick(Sender: TObject);
    procedure eFormCaptionExit(Sender: TObject);
    procedure eFormCaptionKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure miAddHelpClick(Sender: TObject);
    procedure miNoneClick(Sender: TObject);
    procedure miRemoveClick(Sender: TObject);
    procedure pmBorderIconsPopup(Sender: TObject);
  private
    FDesignedForm: IDesignedForm;
    FDesignedFakeForm: IDesignedFakeForm;

    function RootIsSelected: Boolean;

    function GetMargin(const AIndex: Integer): Integer;
    procedure OnUserInputHandler(Sender: TObject; Msg: Cardinal);
    procedure OnDesignRefreshPropertyValues;
  protected
    function GetParent: TWinControl; virtual;
    procedure SetParent(AParent: TWinControl); override;

    function GetDesignedForm: IDesignedForm;
    function GetResizeFrame: IResizeFrame;
    procedure SetResizeFrame(AValue: IResizeFrame);
  public
    { public declarations }
    constructor Create(const ADesignedForm: IDesignedForm; const ADesignedFakeForm: IDesignedFakeForm); virtual; reintroduce;
    destructor Destroy; override;

    procedure RefreshValues;

    procedure UpdateBorderIcons;
    procedure UpdateCaption;
  end;

implementation

{$R *.lfm}
{$R *.res}

uses
  sparta_MainIDE;

var
  Frames: TList;

{ TfrFakeFormBackground }

procedure TfrFakeFormBackground.miAddHelpClick(Sender: TObject);
begin
  if Sender = miAddHelp then
    FDesignedFakeForm.BorderIcons:=FDesignedFakeForm.BorderIcons + [biHelp]
  else if Sender = miAddMaximize then
    FDesignedFakeForm.BorderIcons:=FDesignedFakeForm.BorderIcons + [biMaximize]
  else if Sender = miAddMinimize then
    FDesignedFakeForm.BorderIcons:=FDesignedFakeForm.BorderIcons + [biMinimize]
  else if Sender = miAddSystemMenu then
    FDesignedFakeForm.BorderIcons:=FDesignedFakeForm.BorderIcons + [biSystemMenu];

  GlobalDesignHook.Modified(Self);
  if not RootIsSelected then
    RefreshValues
  else
    GlobalDesignHook.RefreshPropertyValues;
end;

procedure TfrFakeFormBackground.miNoneClick(Sender: TObject);
begin
  if Sender = miNone then
    FDesignedFakeForm.BorderStyle := bsNone
  else if Sender = miSingle then
    FDesignedFakeForm.BorderStyle := bsSingle
  else if Sender = miSizeable then
    FDesignedFakeForm.BorderStyle := bsSizeable
  else if Sender = miDialog then
    FDesignedFakeForm.BorderStyle := bsDialog
  else if Sender = miToolWindow then
    FDesignedFakeForm.BorderStyle := bsToolWindow
  else if Sender = miSizeToolWin then
    FDesignedFakeForm.BorderStyle := bsSizeToolWin
  ;

  GlobalDesignHook.Modified(Self);
  if not RootIsSelected then
    RefreshValues
  else
    GlobalDesignHook.RefreshPropertyValues;
end;

procedure TfrFakeFormBackground.miRemoveClick(Sender: TObject);
begin
  if pmBorderIcons.Tag = PtrInt(bHelp) then
    FDesignedFakeForm.BorderIcons:=FDesignedFakeForm.BorderIcons - [biHelp]
  else if pmBorderIcons.Tag = PtrInt(bMaximalize) then
    FDesignedFakeForm.BorderIcons:=FDesignedFakeForm.BorderIcons - [biMaximize]
  else if pmBorderIcons.Tag = PtrInt(bMinimalize) then
    FDesignedFakeForm.BorderIcons:=FDesignedFakeForm.BorderIcons - [biMinimize]
  else if pmBorderIcons.Tag = PtrInt(bSystem) then
    FDesignedFakeForm.BorderIcons:=FDesignedFakeForm.BorderIcons - [biSystemMenu];

  GlobalDesignHook.Modified(Self);
  if not RootIsSelected then
    RefreshValues
  else
    GlobalDesignHook.RefreshPropertyValues;
end;

procedure TfrFakeFormBackground.pmBorderIconsPopup(Sender: TObject);
begin
  miRemove.Visible := pmBorderIcons.Tag <> PtrInt(bOther);
  miLine.Visible := (pmBorderIcons.Tag <> PtrInt(bOther)) and (not bHelp.Visible or not bMinimalize.Visible or
    not bMaximalize.Visible or not bSystem.Visible);
  miAddHelp.Visible := not bHelp.Visible;
  miAddMinimize.Visible := not bMinimalize.Visible;
  miAddMaximize.Visible := not bMaximalize.Visible;
  miAddSystemMenu.Visible := not bSystem.Visible;
end;

function TfrFakeFormBackground.RootIsSelected: Boolean;
var
  LSelection: TPersistentSelectionList;
  i: integer;
begin
  Result := False;
  LSelection := TPersistentSelectionList.Create;
  GlobalDesignHook.GetSelection(LSelection);
  for i := 0 to LSelection.Count - 1 do
    if LSelection.Items[i] = FDesignedForm.Form then
    begin
      Result := True;
      Break;
    end;
  LSelection.Free;
end;

procedure TfrFakeFormBackground.bIconClick(Sender: TObject);
begin
  pmFormStyle.PopUp(Mouse.CursorPos.x, Mouse.CursorPos.y);
end;

procedure TfrFakeFormBackground.bFormCaptionClick(Sender: TObject);
begin
  eFormCaption.Visible := True;
  eFormCaption.SetFocus;
end;

procedure TfrFakeFormBackground.bOtherClick(Sender: TObject);
begin
  pmBorderIcons.Tag := PtrInt(Sender);
  pmBorderIcons.PopUp(Mouse.CursorPos.x, Mouse.CursorPos.y);
end;

procedure TfrFakeFormBackground.bResizeClick(Sender: TObject);
begin
  case FDesignedFakeForm.BorderStyle of
    bsSizeable: miNoneClick(miSingle);
    bsSingle: miNoneClick(miSizeable);
    bsToolWindow: miNoneClick(miSizeToolWin);
    bsSizeToolWin: miNoneClick(miToolWindow);
  end;
end;

procedure TfrFakeFormBackground.eFormCaptionExit(Sender: TObject);
begin
  bFormCaption.Caption := eFormCaption.Text;
  eFormCaption.Visible := False;
  FDesignedFakeForm.Caption := eFormCaption.Text;
  GlobalDesignHook.Modified(Self);
  GlobalDesignHook.RefreshPropertyValues;
end;

procedure TfrFakeFormBackground.eFormCaptionKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  if Key = VK_RETURN then
    eFormCaptionExit(eFormCaption);
end;

function TfrFakeFormBackground.GetMargin(const AIndex: Integer): Integer;
begin
  case AIndex of
    0: // left
      Result := 5;
    1: // Top
      Result := 30;
    2: // Right
      Result := 5;
    3: // Bottom
      Result := 5;
  end;
end;

procedure TfrFakeFormBackground.OnUserInputHandler(Sender: TObject; Msg: Cardinal);
var
  LCtrl: TControl;
  LIDE: IDesignedFormIDE;
begin
  LIDE := FDesignedForm as IDesignedFormIDE;
  if LIDE.LastActiveSourceWindow = nil then
    Exit;

  if FindModulePageControl(LIDE.LastActiveSourceWindow).PageIndex <> 1 then
    Exit;

  LCtrl := FindDragTarget(Mouse.CursorPos, True);
  if eFormCaption.Visible and (LCtrl <> eFormCaption)  then
    eFormCaptionExit(eFormCaption);
end;

procedure TfrFakeFormBackground.OnDesignRefreshPropertyValues;
begin
  if RootIsSelected then
    RefreshValues;
end;

function TfrFakeFormBackground.GetParent: TWinControl;
begin
  Result := inherited Parent;
end;

procedure TfrFakeFormBackground.SetParent(AParent: TWinControl);
begin
  inherited SetParent(AParent);
  UpdateBorderIcons;
  UpdateCaption;
end;

function TfrFakeFormBackground.GetDesignedForm: IDesignedForm;
begin
  Result := FDesignedForm as IDesignedForm;
end;

function TfrFakeFormBackground.GetResizeFrame: IResizeFrame;
begin
  Result := nil;
end;

procedure TfrFakeFormBackground.SetResizeFrame(AValue: IResizeFrame);
begin
end;

constructor TfrFakeFormBackground.Create(const ADesignedForm: IDesignedForm;
  const ADesignedFakeForm: IDesignedFakeForm);
begin
  inherited Create(nil);
  FDesignedForm := ADesignedForm;
  FDesignedFakeForm := ADesignedFakeForm;
  Frames.Add(Self);

  GlobalDesignHook.AddHandlerRefreshPropertyValues(@OnDesignRefreshPropertyValues);
  RefreshValues;
end;

destructor TfrFakeFormBackground.Destroy;
begin
  Pointer(FDesignedForm) := nil;
  Pointer(FDesignedFakeForm) := nil;
  Frames.Remove(Self);
  GlobalDesignHook.RemoveHandlerRefreshPropertyValues(@OnDesignRefreshPropertyValues);
  inherited Destroy;
end;

procedure TfrFakeFormBackground.RefreshValues;

  procedure SetBorderStyle({ABorderStyle: TBGRABorderStyle});
  begin
    {bBackground.BorderStyle.BottomLeft:=ABorderStyle;
    bBackground.BorderStyle.BottomRight:=ABorderStyle;
    bBackground.BorderStyle.TopLeft:=ABorderStyle;
    bBackground.BorderStyle.TopRight:=ABorderStyle; }
  end;

  procedure LoadPng(ABitmap: TCustomBitmap; AName: string);
  var
    LPng: TPortableNetworkGraphic;
  begin
    LPng := TPortableNetworkGraphic.Create;
    LPng.LoadFromResourceName(HINSTANCE, AName);
    ABitmap.Assign(LPng);
    LPng.Free;
  end;

  procedure SelectFormStyle(AMenuItem: TMenuItem);
  begin
    miNone.Checked:=False;
    miSingle.Checked:=False;
    miSizeable.Checked:=False;
    miDialog.Checked:=False;
    miToolWindow.Checked:=False;
    miSizeToolWin.Checked:=False;
    AMenuItem.Checked:=True;
  end;

begin
  UpdateBorderIcons;
  UpdateCaption;

  if FDesignedFakeForm.BorderStyle in [bsSizeable, bsSizeToolWin] then
    LoadPng(bResize.Picture.Bitmap, 'form_bg_resize')
  else
    LoadPng(bResize.Picture.Bitmap, 'form_bg_noresize');

  case FDesignedFakeForm.BorderStyle of
    bsToolWindow, bsSizeToolWin: SetBorderStyle({bsSquare});
    bsSingle, bsSizeable, bsNone: SetBorderStyle({bsRound});
    bsDialog: SetBorderStyle({bsBevel});
  end;

  case FDesignedFakeForm.BorderStyle of
    bsToolWindow: SelectFormStyle(miToolWindow);
    bsSizeToolWin: SelectFormStyle(miSizeToolWin);
    bsSingle: SelectFormStyle(miSingle);
    bsSizeable:SelectFormStyle(miSizeable);
    bsNone: SelectFormStyle(miNone);
    bsDialog: SelectFormStyle(miDialog);
  end;

  bTop.Visible := FDesignedFakeForm.BorderStyle = bsNone;
  bBackground.Visible := FDesignedFakeForm.BorderStyle <> bsNone;
end;

procedure TfrFakeFormBackground.UpdateBorderIcons;
begin
  if FDesignedFakeForm = nil then
    Exit;

  bOther.Visible := (FDesignedFakeForm.BorderIcons * [biSystemMenu, biMinimize, biMaximize, biHelp]) = [];
  bHelp.Visible := biHelp in FDesignedFakeForm.BorderIcons;
  bMinimalize.Visible := biMinimize in FDesignedFakeForm.BorderIcons;
  bMaximalize.Visible := biMaximize in FDesignedFakeForm.BorderIcons;
  bSystem.Visible := biSystemMenu in FDesignedFakeForm.BorderIcons;
end;

procedure TfrFakeFormBackground.UpdateCaption;
begin
  if FDesignedFakeForm = nil then
    Exit;

  bFormCaption.Caption := FDesignedFakeForm.Caption;
  eFormCaption.Caption := FDesignedFakeForm.Caption;
end;

type

  { OnUserInputHandler }

  TOnUserInputHandler = class
  public
    class procedure OnUserInputHandler(Sender: TObject; Msg: Cardinal);
  end;

{ OnUserInputHandler }

class procedure TOnUserInputHandler.OnUserInputHandler(Sender: TObject; Msg: Cardinal);
var
  p: pointer;
  frame: TfrFakeFormBackground absolute p;
begin
  case Msg of
    LM_LBUTTONDOWN, LM_RBUTTONDOWN, LM_MBUTTONDOWN, LM_XBUTTONDOWN:
      for p in Frames do
        if frame.Parent <> nil then // jesli robilismy popupparent framesy sie tworzyly i byl przypisywany zly caption
          frame.OnUserInputHandler(Sender, Msg);
  end;
end;

var
  OnUserInputHandler: TOnUserInputHandler;
initialization
  Frames := TList.Create;
  Application.AddOnUserInputHandler(@OnUserInputHandler.OnUserInputHandler);
finalization
  Application.RemoveOnUserInputHandler(@OnUserInputHandler.OnUserInputHandler);
  Frames.Free;
end.