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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
| ///////////////////////////////////////////////////////////////////////////
// //
// 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 UserInterface.Control.Navigation;
interface
uses
Boolean,
Drawing.Canvas,
Event,
Integer._32,
Object_.Base,
UserInterface.Control,
UserInterface.Control.Navigation.Action,
UserInterface.Control.Navigation.OverlayControl,
UserInterface.Control.WeakReference,
UserInterface.DragAndDrop.Manager,
UserInterface.Keyboard.Event,
UserInterface.Keyboard.Key,
UserInterface.Mouse.Event,
UserInterface.Navigation,
UserInterface.User.Level;
type
TControlNavigationInfo = record
public
PreviousControl: TControl;
Direction: TNavigationDirection;
end;
TControlNavigationStack = class(TObject)
private
Elements: array of TControlNavigationInfo;
public
function Peek(): TControlNavigationInfo;
function Pop(): TControlNavigationInfo;
procedure Push(Direction: TNavigationDirection; PreviousControl: TControl);
function Count(): TInteger32;
procedure Clear();
end;
TControlNavigationContainerControl = class(TControl)
end;
TControlNavigationManager = class(TBaseObject)
private
FRootControl: TControl;
CurrentReference: TControlWeakReference<TControl>;
OverlayControl: TNavigationOverlayControl;
procedure SetCurrent(const Value: TControl);
function GetCurrent(): TControl;
procedure SetRootControl(const Value: TControl);
protected
function KeyToNavigationAction(Key: TKey): TControlNavigationAction;
function IsInverseNavigation(ControlNavigationInfo: TControlNavigationInfo; Direction: TNavigationDirection): TBoolean;
function NextNavigationControl(CurrentControl: TControl; Direction: TNavigationDirection): TControl;
procedure LayoutControl(NavigationAction: TControlNavigationAction);
procedure HandleNavigationAction(Action: TControlNavigationAction);
procedure DragControl();
procedure PrintScreen();
procedure CloseCurrent();
procedure SetUserLevel(Level: TUserLevel);
procedure NavigateControl(Direction: TNavigationDirection);
function HandleStartEvent(Event: TEvent): TBoolean;
function HandleKeyboardEvent(Event: TKeyboardEvent): TBoolean;
function HandleMouseWheelEvent(Event: TMouseWheelEvent): TBoolean;
procedure UpdateOverlayBounds();
public
ContainerControl: TControlNavigationContainerControl;
FocusManager: TKeyboardFocusManager;
DragAndDropManager: TDragAndDropManager;
IsActive: TBoolean;
History: TControlNavigationStack;
constructor Create(); override;
destructor Destroy(); override;
procedure Start();
procedure Stop(Successful: TBoolean);
function ImmediateNavigateControl(Direction: TNavigationDirection): TBoolean;
function HandleEvent(Event: TEvent): TBoolean; override;
procedure Paint(Canvas: TCanvas);
property Current: TControl read GetCurrent write SetCurrent;
property RootControl: TControl read FRootControl write SetRootControl;
end;
implementation
uses
DateTime,
Exception,
File_,
Geometry._2d.Integer32.Rectangle,
Geometry._2d.Integer32.Size,
Geometry._2d.Side,
Image.Raster,
Image.Raster.Png.Writer,
Stream.Output,
UserInterface.Control.Event,
UserInterface.Control.Navigation.Action.Panel,
UserInterface.Control.Panel.Split,
UserInterface.Control.Panel.Split.SingleDimension.Horizontal,
UserInterface.Control.Panel.Split.SingleDimension.Vertical,
UserInterface.Keyboard.Modifiers,
Windows.Gdi.Bitmap,
Windows.Gdi.Canvas;
{ TControlNavigationManager }
procedure TControlNavigationManager.CloseCurrent();
var
ContainerControl: TControl;
begin
ContainerControl := Current.ParentControl;
ContainerControl.CloseControl(Current);
FocusManager.FocusedControl := ContainerControl.DefaultControlToFocus;
Current := ContainerControl.DefaultControlToNavigate;
end;
constructor TControlNavigationManager.Create();
begin
inherited Create();
CurrentReference := TControlWeakReference<TControl>.Create(Self);
History := TControlNavigationStack.Create();
OverlayControl := TNavigationOverlayControl.Create();
ContainerControl := TControlNavigationContainerControl.Create();
ContainerControl.ChildControls.Add(OverlayControl);
OverlayControl.Listeners.Add(Self);
OverlayControl.ActionPanel.Listeners.Add(Self);
end;
destructor TControlNavigationManager.Destroy();
begin
OverlayControl.ActionPanel.Listeners.Remove(Self);
OverlayControl.Listeners.Remove(Self);
History.Free();
ContainerControl.Free();
CurrentReference.Free();
inherited Destroy();
end;
procedure TControlNavigationManager.DragControl();
var
DragControl: TControl;
begin
DragControl := Current;
Stop(False);
DragAndDropManager.StartDrag(DragControl);
end;
function TControlNavigationManager.GetCurrent(): TControl;
begin
Result := CurrentReference.Value;
end;
function TControlNavigationManager.HandleEvent(Event: TEvent): TBoolean;
begin
Result := IsActive;
if Result then
begin
if (Event is TControlBoundsChangedEvent) and (Event.GetSender() = Current) then
UpdateOverlayBounds()
else if (Event is TControlNavigationActionExecuted) then
HandleNavigationAction((Event as TControlNavigationActionExecuted).Action)
else if Event is TKeyboardEvent then
Result := HandleKeyboardEvent(Event as TKeyboardEvent)
else
begin
if Event is TMouseWheelEvent then
HandleMouseWheelEvent(Event as TMouseWheelEvent)
else if Event is TMouseEvent then
ContainerControl.GetMouseManager().SendEventToControl(ContainerControl, Event as TMouseEvent);
end;
end
else
Result := HandleStartEvent(Event);
end;
function TControlNavigationManager.HandleKeyboardEvent(Event: TKeyboardEvent): TBoolean;
var
StateEvent: TKeyStateEvent;
begin
Result := False;
if Event is TKeyStateEvent then
begin
StateEvent := Event as TKeyStateEvent;
if (StateEvent is TKeyDownEvent) and (StateEvent.Modifiers = [TKeyModifier.Alt]) then
begin
Result := True;
case StateEvent.Key of
TKey.C:
CloseCurrent();
TKey.D:
SetUserLevel(ulDeveloper);
TKey.U:
SetUserLevel(ulEndUser);
TKey.P:
PrintScreen();
TKey.T, TKey.H, TKey.V:
LayoutControl(KeyToNavigationAction(StateEvent.Key));
TKey.Left, TKey.Right, TKey.Up, TKey.Down, TKey.OpenBracket, TKey.CloseBracket:
NavigateControl(KeyToNavigation(StateEvent.Key, []));
TKey.ESCAPE:
Stop(False);
else
Result := False;
end;
end
else if (StateEvent is TKeyUpEvent) and (StateEvent.Key = TKey.Alt) then
Stop(True);
end;
end;
function TControlNavigationManager.HandleMouseWheelEvent(Event: TMouseWheelEvent): TBoolean;
begin
Result := True;
if Event.Delta > 0 then
NavigateControl(TNavigationDirection.Parent)
else
NavigateControl(TNavigationDirection.Child);
end;
procedure TControlNavigationManager.HandleNavigationAction(Action: TControlNavigationAction);
begin
case Action of
cnaClose:
CloseCurrent();
cnaTab, cnaSplitVertical, cnaSplitHorizontal, cnaToggleCaption:
LayoutControl(Action);
cnaDrag:
DragControl();
else
raise EException.Create('Unknown navigation action');
end;
end;
function TControlNavigationManager.HandleStartEvent(Event: TEvent): TBoolean;
begin
Result := (Event is TKeyDownEvent) and ((Event as TKeyDownEvent).Key = TKey.Alt);
if Result then
Start();
end;
function TControlNavigationManager.ImmediateNavigateControl(Direction: TNavigationDirection): TBoolean;
var
FocusedControl: TControl;
NextControl: TControl;
NavigationDimension: TNavigationDimension;
begin
Result := False;
if Direction in [TNavigationDirection.Up, TNavigationDirection.Down, TNavigationDirection.Left, TNavigationDirection.Right] then
begin
NavigationDimension := NavigationDirectionToDimension(Direction);
FocusedControl := FocusManager.FocusedControl;
if (FocusedControl <> nil) and (NavigationDimension <> TNavigationDimension.None) and not FocusedControl.HandlesNavigationDimension(NavigationDimension) then
begin
NextControl := NextNavigationControl(FocusedControl, Direction);
Result := (NextControl <> nil) and not NextControl.HandlesNavigationDimension(NavigationDimension);
if Result then
NextControl.Enter()
else if (History.Count > 0) and (History.Peek().PreviousControl = FocusedControl) then
History.Pop();
end;
end;
end;
function TControlNavigationManager.IsInverseNavigation(ControlNavigationInfo: TControlNavigationInfo; Direction: TNavigationDirection): TBoolean;
begin
Result :=
(InverseNavigationDirection(ControlNavigationInfo.Direction) = Direction) and
ControlNavigationInfo.PreviousControl.IsVisible;
end;
procedure TControlNavigationManager.LayoutControl(NavigationAction: TControlNavigationAction);
var
Control: TControl;
SplitPanel: TSplitPanel;
PanelIndex: TInteger32;
begin
Control := Current;
if Control.ParentControl is TSplitPanel then
begin
SplitPanel := Control.ParentControl as TSplitPanel;
PanelIndex := SplitPanel.PanelIndexOf(Control);
case NavigationAction of
cnaSplitHorizontal:
Control := SplitPanel.Split(PanelIndex, THorizontalSplitPanel);
cnaSplitVertical:
Control := SplitPanel.Split(PanelIndex, TVerticalSplitPanel);
cnaTab:
Control := SplitPanel.ToggleTabPanel(PanelIndex);
cnaToggleCaption:
Control := SplitPanel.ToggleCaption(PanelIndex);
else
raise EException.Create('Unknown control navigation action');
end;
Current := Control;
end;
end;
function TControlNavigationManager.KeyToNavigationAction(Key: TKey): TControlNavigationAction;
begin
case Key of
TKey.T: Result := cnaTab;
TKey.H: Result := cnaSplitHorizontal;
TKey.V: Result := cnaSplitVertical;
else
raise EException.Create('Unknown key for navigation action');
end;
end;
procedure TControlNavigationManager.NavigateControl(Direction: TNavigationDirection);
var
NextControl: TControl;
begin
if Current <> nil then
begin
NextControl := NextNavigationControl(Current, Direction);
if NextControl <> nil then
Current := NextControl;
end;
end;
function TControlNavigationManager.NextNavigationControl(CurrentControl: TControl; Direction: TNavigationDirection): TControl;
var
AncestorControl: TControl;
Side: TSide;
begin
if (History.Count > 0) and IsInverseNavigation(History.Peek(), Direction) then
Result := History.Pop().PreviousControl
else
begin
case Direction of
TNavigationDirection.Parent:
begin
Result := CurrentControl.ParentControl;
if Result <> nil then
Result := Result.GetChildOfDynamicContainer();
end;
TNavigationDirection.Child:
begin
// TODO: Implement
Result := nil;
end;
else
begin
Side := NavigationDirectionToSide(Direction);
AncestorControl := CurrentControl.AncestorControlWithControlsAtSide(Side);
if AncestorControl <> nil then
Result := CurrentControl.ControlAtSide(AncestorControl.ChildControls, Side)
else
Result := nil;
if Result <> nil then
Result := CurrentControl.LeafControlAtSide(Result, Side);
end;
end;
if Result <> nil then
History.Push(Direction, CurrentControl);
end;
end;
procedure TControlNavigationManager.Paint(Canvas: TCanvas);
begin
if IsActive then
ContainerControl.PaintAll(Canvas);
end;
procedure TControlNavigationManager.PrintScreen();
var
Canvas: TGdiCanvas;
Bitmap: TGdiBitmap;
Image: TRasterImage;
Size: TInteger32Size;
File_: TFile;
Stream: TOutputStream;
Writer: TPortableNetworkGraphicsWriter;
begin
Size := Current.Size;
Bitmap := TGdiBitmap.Create(Size);
Canvas := TGdiCanvas.Create();
Canvas.DeviceContext := Bitmap.DeviceContext;
IsActive := False;
Current.PaintAll(Canvas);
IsActive := True;
Canvas.Free();
Image := Bitmap.ConvertToRasterImage();
Image.AddReference();
File_ := TFile.Create('screenshot.' + DateTimeToTimeStampText(Now_Local()) + '.png');
Stream := File_.CreateRewriteStream();
try
Writer := TPortableNetworkGraphicsWriter.Create(Stream);
Writer.Write(Image);
Writer.Free();
finally
Stream.Free();
end;
File_.Free();
Bitmap.Free();
Image.ReleaseReference();
end;
procedure TControlNavigationManager.SetCurrent(const Value: TControl);
begin
if Value <> Current then
begin
if Current <> nil then
Current.Invalidate();
CurrentReference.Value := Value;
if Current <> nil then
begin
CurrentReference.Value := Current.RootComposition;
Current.MakeVisible();
UpdateOverlayBounds();
end;
end;
end;
procedure TControlNavigationManager.SetRootControl(const Value: TControl);
begin
if Value <> RootControl then
begin
FRootControl := Value;
ContainerControl.ParentControl := RootControl;
end;
end;
procedure TControlNavigationManager.SetUserLevel(Level: TUserLevel);
begin
if Current <> nil then
Current.GetControlFactory().UserLevel := Level;
end;
procedure TControlNavigationManager.Start();
begin
IsActive := True;
Current := FocusManager.FocusedControl.RootComposition;
if Current <> nil then
Current.Invalidate();
end;
procedure TControlNavigationManager.Stop(Successful: TBoolean);
begin
if IsActive then
begin
IsActive := False;
if Current <> nil then
begin
Current.Invalidate();
if Successful then
Current.Enter();
Current := nil;
end;
History.Clear();
OverlayControl.ActionsVisible := False;
end;
end;
procedure TControlNavigationManager.UpdateOverlayBounds();
var
Rectangle: TInteger32Rectangle;
begin
Rectangle := Current.GetVisibleRectangle();
if Current.HasAncestor(RootControl) then
Rectangle := Rectangle.Offset(Current.PositionRelativeTo(RootControl));
OverlayControl.Bounds := Rectangle;
end;
{ TControlNavigationStack }
procedure TControlNavigationStack.Clear();
begin
SetLength(Elements, 0);
end;
function TControlNavigationStack.Count(): TInteger32;
begin
Result := Length(Elements);
end;
function TControlNavigationStack.Peek(): TControlNavigationInfo;
begin
Result := Elements[High(Elements)];
end;
function TControlNavigationStack.Pop(): TControlNavigationInfo;
begin
Result := Peek();
SetLength(Elements, Length(Elements) - 1);
end;
procedure TControlNavigationStack.Push(Direction: TNavigationDirection; PreviousControl: TControl);
var
ControlNavigationInfo: TControlNavigationInfo;
begin
ControlNavigationInfo.PreviousControl := PreviousControl;
ControlNavigationInfo.Direction := Direction;
SetLength(Elements, Length(Elements) + 1);
Elements[High(Elements)] := ControlNavigationInfo;
end;
end.
|