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
436
437
438
439
440
441
442
443
444
445
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Windows.Control.Window;
interface
{$SCOPEDENUMS ON}
uses
Boolean,
Drawing.Canvas,
Drawing.Resource,
Event,
Geometry._2d.Integer32.Rectangle,
Integer._16.Positive,
Integer._32.Positive,
UserInterface.ClipBoard,
UserInterface.Control,
UserInterface.Control.ContextMenu,
UserInterface.Control.Navigation,
UserInterface.Control.Window.Hint,
UserInterface.Control.Window.TaskBar,
Windows.Api.Messages,
Windows.Api.Type_,
Windows.Control;
type
TWindowState = (Normal, Minimized, Maximized);
TWindowClass = class of TWindow;
TWindow = class(TWindowsControl)
private
FShowInTaskbar: TBoolean;
FToolWindow: TBoolean;
FNavigationManager: TControlNavigationManager;
FTaskBar: TWindowTaskBar;
procedure SetShowInTaskbar(const Value: TBoolean);
procedure SetToolWindow(const Value: TBoolean);
procedure SetState(const Value: TWindowState);
procedure SetNavigationManager(const Value: TControlNavigationManager);
procedure AttachToNavigationManager();
procedure DetachFromNavigationManager();
protected
function WMActivateApp(Active: TBoolean; ThreadIdentifier: DWORD): LRESULT; override;
function MsgTaskbarButtonCreated(): LRESULT; override;
function WindowProcedure(Message: TPositiveInteger32; wParam: TWParam; lParam: TLParam): LRESULT; override;
function GetControlStyle(): TPositiveInteger32; override;
function GetExtendedControlStyle(): TPositiveInteger32; override;
procedure ControlChanged(); override;
procedure BoundsChanged(const OldBounds: TInteger32Rectangle; const NewBounds: TInteger32Rectangle); override;
procedure DescendantRequestsFocus(Descendant: TControl); override;
procedure DescendantFiresEvent(Descendant: TControl; Event: TEvent); override;
function CreateDrawingResources(): TDrawingResources; virtual; abstract;
function WMClose(): LRESULT; virtual;
function WMDestroy(): LRESULT; virtual;
function WMActivate(Active: TPositiveInteger16; Minimized: TBoolean; Window: THandle): LRESULT; virtual;
function WMSysCommand(wParam: TWParam): LRESULT; virtual;
function WMMenuSelect(wParam: TWParam): LRESULT; virtual;
public
ContextMenuControl: TContextMenuControl;
HintWindow: THintWindow;
ClipBoard: TClipBoard;
MouseManager: TMouseManager;
FocusManager: TKeyboardFocusManager;
DrawingResources: TDrawingResources;
constructor Create(); override;
destructor Destroy(); override;
procedure PaintAll(Canvas: TCanvas); override;
function IsComposition(): TBoolean; override;
function IsDynamicContainer(): TBoolean; override;
function IsBoundToLayout(): TBoolean; override;
function GetHintWindow(): THintWindow; override;
function GetClipBoard(): TClipBoard; override;
function GetMouseManager(): TMouseManager; override;
function GetFocusManager(): TKeyboardFocusManager; override;
function GetDrawingResources(): TDrawingResources; override;
function HandleEvent(Event: TEvent): TBoolean; override;
property ShowInTaskbar: TBoolean read FShowInTaskbar write SetShowInTaskbar;
property ToolWindow: TBoolean read FToolWindow write SetToolWindow;
property State: TWindowState write SetState;
property NavigationManager: TControlNavigationManager read FNavigationManager write SetNavigationManager;
property TaskBar: TWindowTaskBar read FTaskBar;
end;
TWindowFactory = class(TObject)
public
WindowClass: TWindowClass;
DebugUpdateRegion: TBoolean;
function CreateWindow(): TWindow;
end;
implementation
uses
Exception,
Integer._32,
UserInterface.Control.Event,
Windows.Api.UserInterface,
Windows.Control.Window.Hint,
Windows.TaskBar;
{ TWindow }
procedure TWindow.AttachToNavigationManager();
begin
if NavigationManager <> nil then
NavigationManager.RootControl := Control;
end;
procedure TWindow.BoundsChanged(const OldBounds: TInteger32Rectangle; const NewBounds: TInteger32Rectangle);
var
ControlBounds: TInteger32Rectangle;
begin
inherited BoundsChanged(OldBounds, NewBounds);
if OldBounds.Size <> NewBounds.Size then
begin
ControlBounds := CalculateControlBounds();
NavigationManager.ContainerControl.Size := ControlBounds.Size;
end;
end;
procedure TWindow.ControlChanged();
begin
inherited ControlChanged();
if HasValidHandles then
SetWindowText(Handle, Control.Caption);
if NavigationManager <> nil then
NavigationManager.RootControl := Control;
end;
constructor TWindow.Create();
begin
inherited Create();
ShowInTaskbar := True;
HintWindow := TWindowsHintWindow.Create(Self);
DrawingResources := CreateDrawingResources();
ContextMenuControl := TContextMenuControl.Create();
ContextMenuControl.ParentHandle := 0;
ContextMenuControl.ParentControl := Self;
end;
function TWindow.GetHintWindow(): THintWindow;
begin
Result := HintWindow;
end;
procedure TWindow.DescendantFiresEvent(Descendant: TControl; Event: TEvent);
begin
if Descendant.HasAncestor(ContextMenuControl) and (Event is TControlDefaultActionExecutedEvent) then
ContextMenuControl.Hide()
else if (Event is TControlCaretChangedEvent) or (Event is TControlVisibleRectangleChangedEvent) then
CaretImplementation.Updated();
inherited DescendantFiresEvent(Descendant, Event);
end;
procedure TWindow.DescendantRequestsFocus(Descendant: TControl);
begin
if not Descendant.HasAncestor(ContextMenuControl) then
inherited DescendantRequestsFocus(Descendant);
end;
destructor TWindow.Destroy();
begin
DetachFromNavigationManager();
ContextMenuControl.Free();
HintWindow.Free();
DrawingResources.Free();
inherited Destroy();
end;
procedure TWindow.DetachFromNavigationManager();
begin
if NavigationManager <> nil then
begin
NavigationManager.RootControl := nil;
NavigationManager.ContainerControl.ParentControl := nil;
end;
end;
function TWindow.GetMouseManager(): TMouseManager;
begin
Result := MouseManager;
end;
function TWindow.HandleEvent(Event: TEvent): TBoolean;
begin
if Event is TWillBeFreedEvent then
begin
Result := Event.GetSender() = NavigationManager;
if Result then
NavigationManager := nil;
end
else
Result := False;
end;
function TWindow.IsBoundToLayout(): TBoolean;
begin
Result := True;
end;
function TWindow.IsComposition(): TBoolean;
begin
Result := False;
end;
function TWindow.IsDynamicContainer(): TBoolean;
begin
Result := True;
end;
function TWindow.MsgTaskbarButtonCreated(): LRESULT;
begin
FTaskBar := TWindowsWindowTaskBar.Create(Handle);
Result := inherited MsgTaskbarButtonCreated();
end;
procedure TWindow.PaintAll(Canvas: TCanvas);
begin
inherited PaintAll(Canvas);
Canvas.Transformation.AppendTranslation(Control.Position);
try
NavigationManager.Paint(Canvas);
finally
Canvas.Transformation.RemoveLast();
end;
end;
procedure TWindow.SetNavigationManager(const Value: TControlNavigationManager);
begin
if Value <> NavigationManager then
begin
DetachFromNavigationManager();
FNavigationManager := Value;
AttachToNavigationManager();
end;
end;
procedure TWindow.SetShowInTaskbar(const Value: TBoolean);
begin
if Value <> ShowInTaskbar then
begin
FShowInTaskbar := Value;
if HasValidHandles then
SetWindowLong(Handle, GWL_EXSTYLE, GetExtendedControlStyle());
end;
end;
procedure TWindow.SetState(const Value: TWindowState);
var
Parameter: TInteger32;
begin
case Value of
TWindowState.Normal:
Parameter := SW_SHOWNORMAL;
TWindowState.Minimized:
Parameter := SW_SHOWMINIMIZED;
TWindowState.Maximized:
Parameter := SW_SHOWMAXIMIZED;
else
raise EException.Create('Unknown window state');
end;
ShowWindow(Handle, Parameter);
end;
function TWindow.GetClipBoard(): TClipBoard;
begin
Result := ClipBoard;
end;
function TWindow.GetControlStyle(): TPositiveInteger32;
begin
Result := WS_VISIBLE or WS_OVERLAPPEDWINDOW or WS_CLIPCHILDREN or WS_CLIPSIBLINGS;;
end;
function TWindow.GetDrawingResources(): TDrawingResources;
begin
Result := DrawingResources;
end;
function TWindow.GetExtendedControlStyle(): TPositiveInteger32;
begin
if ShowInTaskbar then
Result := WS_EX_APPWINDOW
else
Result := 0;
if ToolWindow then
Result := Result or WS_EX_TOOLWINDOW;
end;
function TWindow.GetFocusManager(): TKeyboardFocusManager;
begin
Result := FocusManager;
end;
function TWindow.WMClose(): LRESULT;
begin
DestroyWindow();
Result := 0;
end;
function TWindow.WMDestroy(): LRESULT;
begin
FTaskBar.Free();
FTaskBar := nil;
PostQuitMessage(0);
Result := 0;
end;
function TWindow.WMMenuSelect(wParam: TWParam): LRESULT;
begin
if wParam <> $FFFF0000 then
NavigationManager.Stop(False);
Result := 1;
end;
function TWindow.WMSysCommand(wParam: TWParam): LRESULT;
begin
Result := 1;
end;
procedure TWindow.SetToolWindow(const Value: TBoolean);
begin
if Value <> ToolWindow then
begin
FToolWindow := Value;
if HasValidHandles then
SetWindowLong(Handle, GWL_EXSTYLE, GetExtendedControlStyle());
end;
end;
function TWindow.WindowProcedure(Message: TPositiveInteger32; wParam: TWParam; lParam: TLParam): LRESULT;
begin
case Message of
WM_CLOSE:
begin
Result := WMClose();
if Result <> 0 then
Result := Window.CallOriginalWindowProcedure(Message, wParam, lParam);
end;
WM_DESTROY:
begin
Result := WMDestroy();
if Result <> 0 then
Result := Window.CallOriginalWindowProcedure(Message, wParam, lParam);
end;
WM_ACTIVATE:
begin
Result := WMActivate(LowOrderWord(wParam), HighOrderWord(wParam) = 1, lParam);
if Result <> 0 then
Result := Window.CallOriginalWindowProcedure(Message, wParam, lParam);
end;
WM_MENUSELECT:
begin
Result := WMMenuSelect(wParam);
if Result <> 0 then
Result := Window.CallOriginalWindowProcedure(Message, wParam, lParam);
end;
WM_SYSCOMMAND:
begin
Result := WMSysCommand(wParam);
if Result <> 0 then
Result := Window.CallOriginalWindowProcedure(Message, wParam, lParam);
end
else
Result := inherited WindowProcedure(Message, wParam, lParam);
end;
end;
function TWindow.WMActivate(Active: TPositiveInteger16; Minimized: TBoolean; Window: THandle): LRESULT;
begin
if Active in [WA_ACTIVE, WA_CLICKACTIVE] then
SetFocus(Handle);
Result := 0;
end;
function TWindow.WMActivateApp(Active: TBoolean; ThreadIdentifier: DWORD): LRESULT;
begin
NavigationManager.Stop(False);
Result := 0;
end;
{ TWindowFactory }
function TWindowFactory.CreateWindow(): TWindow;
begin
Result := WindowClass.Create();
Result.DebugUpdateRegion := DebugUpdateRegion;
end;
end.
|