/
FrameworkElement.cs
2187 lines (1896 loc) · 83.2 KB
/
FrameworkElement.cs
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#region Copyright (C) 2007-2017 Team MediaPortal
/*
Copyright (C) 2007-2017 Team MediaPortal
http://www.team-mediaportal.com
This file is part of MediaPortal 2
MediaPortal 2 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 3 of the License, or
(at your option) any later version.
MediaPortal 2 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 MediaPortal 2. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
// Define DEBUG_LAYOUT to make MP log screen layouting information. That will slow down the layouting process significantly
// but can be used to find layouting bugs. Don't use that switch in release builds.
// Use DEBUG_MORE_LAYOUT to get more information, also for skipped method calls.
//#define DEBUG_LAYOUT
//#define DEBUG_MORE_LAYOUT
using System;
using System.Collections.Generic;
using System.Linq;
using MediaPortal.UI.Control.InputManager;
using MediaPortal.Common.General;
using MediaPortal.UI.SkinEngine.Commands;
using MediaPortal.UI.SkinEngine.ContentManagement;
using MediaPortal.UI.SkinEngine.Controls.Transforms;
using MediaPortal.UI.SkinEngine.Controls.Visuals.Effects;
using MediaPortal.UI.SkinEngine.Fonts;
using MediaPortal.UI.SkinEngine.InputManagement;
using MediaPortal.UI.SkinEngine.MpfElements;
using MediaPortal.UI.SkinEngine.MpfElements.Resources;
using MediaPortal.UI.SkinEngine.Rendering;
using MediaPortal.UI.SkinEngine.ScreenManagement;
using MediaPortal.UI.SkinEngine.Xaml.Interfaces;
using SharpDX;
using SharpDX.Direct3D9;
using MediaPortal.UI.SkinEngine.DirectX;
using MediaPortal.UI.SkinEngine.Controls.Visuals.Styles;
using MediaPortal.Utilities.DeepCopy;
using Effect = MediaPortal.UI.SkinEngine.Controls.Visuals.Effects.Effect;
using Size = SharpDX.Size2;
using SizeF = SharpDX.Size2F;
using PointF = SharpDX.Vector2;
namespace MediaPortal.UI.SkinEngine.Controls.Visuals
{
public enum VerticalAlignmentEnum
{
Top = 0,
Center = 1,
Bottom = 2,
Stretch = 3,
};
public enum HorizontalAlignmentEnum
{
Left = 0,
Center = 1,
Right = 2,
Stretch = 3,
};
public enum MoveFocusDirection
{
Up,
Down,
Left,
Right
}
public class SetElementStateAction : IUIElementAction
{
protected ElementState _state;
public SetElementStateAction(ElementState state)
{
_state = state;
}
public void Execute(UIElement element)
{
element.ElementState = _state;
}
}
public enum ElementState
{
/// <summary>
/// The element is used as template, style resource or during resource creation.
/// </summary>
Available,
/// <summary>
/// The element doesn't participate in the render run yet but is being prepared. This means we can take some shortcuts
/// when setting properties.
/// </summary>
Preparing,
/// <summary>
/// The element participates in the render run. In this state, the render thread is the only thread which may access several
/// properties of the elements in the screens (only some exceptions for properties which can be accessed by the input thread).
/// </summary>
Running,
/// <summary>
/// The element is (being) disposed.
/// </summary>
Disposing
}
public class FrameworkElement : UIElement
{
#region Constants
public const string GOTFOCUS_EVENT = "FrameworkElement.GotFocus";
public const string LOSTFOCUS_EVENT = "FrameworkElement.LostFocus";
public const string MOUSEENTER_EVENT = "FrameworkElement.MouseEnter";
public const string MOUSELEAVE_EVENT = "FrameworkElement.MouseLeave";
public const string TOUCHENTER_EVENT = "FrameworkElement.TouchEnter";
public const string TOUCHLEAVE_EVENT = "FrameworkElement.TouchLeave";
protected const string GLOBAL_RENDER_TEXTURE_ASSET_KEY = "SkinEngine::GlobalRenderTexture";
protected const string GLOBAL_RENDER_SURFACE_ASSET_KEY = "SkinEngine::GlobalRenderSurface";
#endregion
#region Protected fields
protected AbstractProperty _widthProperty;
protected AbstractProperty _heightProperty;
protected AbstractProperty _actualWidthProperty;
protected AbstractProperty _actualHeightProperty;
protected AbstractProperty _minWidthProperty;
protected AbstractProperty _minHeightProperty;
protected AbstractProperty _maxWidthProperty;
protected AbstractProperty _maxHeightProperty;
protected AbstractProperty _horizontalAlignmentProperty;
protected AbstractProperty _verticalAlignmentProperty;
protected AbstractProperty _styleProperty;
protected AbstractProperty _focusableProperty;
protected AbstractProperty _hasFocusProperty;
protected AbstractProperty _isKeyboardFocusWithinProperty;
protected AbstractProperty _isMouseOverProperty;
protected AbstractProperty _fontSizeProperty;
protected AbstractProperty _fontFamilyProperty;
protected AbstractProperty _contextMenuCommandProperty;
protected PrimitiveBuffer _opacityMaskContext;
protected PrimitiveBuffer _effectContext;
protected static Effects.Effect _defaultEffect;
protected bool _updateOpacityMask = false;
protected RectangleF _lastOccupiedTransformedBounds = RectangleF.Empty;
protected bool _styleSet = false;
protected volatile SetFocusPriority _setFocusPrio = SetFocusPriority.None;
protected SizeF? _availableSize = null;
protected RectangleF? _outerRect = null;
protected RectangleF? _renderedBoundingBox = null;
protected SizeF _innerDesiredSize; // Desiredd size in local coords
protected SizeF _desiredSize; // Desired size in parent coordinate system
protected RectangleF _innerRect;
protected volatile bool _isMeasureInvalid = true;
protected volatile bool _isArrangeInvalid = true;
protected Matrix? _inverseFinalTransform = null;
protected Matrix? _finalTransform = null;
protected float _lastZIndex = 0;
#endregion
#region Ctor & maintainance
public FrameworkElement()
{
Init();
Attach();
}
void Init()
{
// Default is not set
_widthProperty = new SProperty(typeof(double), Double.NaN);
_heightProperty = new SProperty(typeof(double), Double.NaN);
// Default is not set
_actualWidthProperty = new SProperty(typeof(double), Double.NaN);
_actualHeightProperty = new SProperty(typeof(double), Double.NaN);
// Min/Max width
_minWidthProperty = new SProperty(typeof(double), 0.0);
_minHeightProperty = new SProperty(typeof(double), 0.0);
_maxWidthProperty = new SProperty(typeof(double), double.MaxValue);
_maxHeightProperty = new SProperty(typeof(double), double.MaxValue);
// Default is not set
_styleProperty = new SProperty(typeof(Style), null);
// Default is stretch
_horizontalAlignmentProperty = new SProperty(typeof(HorizontalAlignmentEnum), HorizontalAlignmentEnum.Stretch);
_verticalAlignmentProperty = new SProperty(typeof(VerticalAlignmentEnum), VerticalAlignmentEnum.Stretch);
// Focus properties
_focusableProperty = new SProperty(typeof(bool), false);
_hasFocusProperty = new SProperty(typeof(bool), false);
_isKeyboardFocusWithinProperty = new SProperty(typeof(bool), false);
_isMouseOverProperty = new SProperty(typeof(bool), false);
// Context menu
_contextMenuCommandProperty = new SProperty(typeof(IExecutableCommand), null);
// Font properties
_fontFamilyProperty = new SProperty(typeof(string), String.Empty);
_fontSizeProperty = new SProperty(typeof(int), -1);
}
void Attach()
{
_widthProperty.Attach(OnMeasureGetsInvalid);
_heightProperty.Attach(OnMeasureGetsInvalid);
_actualHeightProperty.Attach(OnActualBoundsChanged);
_actualWidthProperty.Attach(OnActualBoundsChanged);
_styleProperty.Attach(OnStyleChanged);
LayoutTransformProperty.Attach(OnLayoutTransformPropertyChanged);
MarginProperty.Attach(OnMeasureGetsInvalid);
VisibilityProperty.Attach(OnParentCompleteLayoutGetsInvalid);
OpacityProperty.Attach(OnOpacityChanged);
OpacityMaskProperty.Attach(OnOpacityChanged);
ActualPositionProperty.Attach(OnActualBoundsChanged);
IsEnabledProperty.Attach(OnEnabledChanged);
HasFocusProperty.Attach(OnHasFocusChanged);
}
void Detach()
{
_widthProperty.Detach(OnMeasureGetsInvalid);
_heightProperty.Detach(OnMeasureGetsInvalid);
_actualHeightProperty.Detach(OnActualBoundsChanged);
_actualWidthProperty.Detach(OnActualBoundsChanged);
_styleProperty.Detach(OnStyleChanged);
LayoutTransformProperty.Detach(OnLayoutTransformPropertyChanged);
MarginProperty.Detach(OnMeasureGetsInvalid);
VisibilityProperty.Detach(OnParentCompleteLayoutGetsInvalid);
OpacityProperty.Detach(OnOpacityChanged);
OpacityMaskProperty.Detach(OnOpacityChanged);
ActualPositionProperty.Detach(OnActualBoundsChanged);
IsEnabledProperty.Detach(OnEnabledChanged);
HasFocusProperty.Detach(OnHasFocusChanged);
}
public override void DeepCopy(IDeepCopyable source, ICopyManager copyManager)
{
Detach();
object oldLayoutTransform = LayoutTransform;
base.DeepCopy(source, copyManager);
FrameworkElement fe = (FrameworkElement) source;
Width = fe.Width;
Height = fe.Height;
Style = copyManager.GetCopy(fe.Style);
ActualWidth = fe.ActualWidth;
ActualHeight = fe.ActualHeight;
HorizontalAlignment = fe.HorizontalAlignment;
VerticalAlignment = fe.VerticalAlignment;
Focusable = fe.Focusable;
FontSize = fe.FontSize;
FontFamily = fe.FontFamily;
MinWidth = fe.MinWidth;
MinHeight = fe.MinHeight;
MaxWidth = fe.MaxWidth;
MaxHeight = fe.MaxHeight;
_setFocusPrio = fe.SetFocusPrio;
_renderedBoundingBox = null;
ContextMenuCommand = copyManager.GetCopy(fe.ContextMenuCommand);
// Need to manually call this because we are in a detached state
OnLayoutTransformPropertyChanged(_layoutTransformProperty, oldLayoutTransform);
Attach();
}
public override void Dispose()
{
if (HasFocus || SetFocus)
{
FrameworkElement parent = VisualParent as FrameworkElement;
if (parent != null)
parent.SetFocus = true;
}
MPF.TryCleanupAndDispose(ContextMenuCommand);
base.Dispose();
MPF.TryCleanupAndDispose(Style);
}
#endregion
#region Event handlers
protected override void OnUpdateElementState()
{
base.OnUpdateElementState();
if (PreparingOrRunning && !_styleSet)
UpdateStyle(null);
}
protected void UpdateStyle(Style oldStyle)
{
bool changed = false;
if (oldStyle != null)
{
changed = true;
oldStyle.Reset(this);
MPF.TryCleanupAndDispose(oldStyle);
}
if (Style != null)
{
changed = true;
Style.Set(this);
_styleSet = true;
}
if (changed)
InvalidateLayout(true, true);
}
protected virtual void OnStyleChanged(AbstractProperty property, object oldValue)
{
if (!PreparingOrRunning)
return;
UpdateStyle((Style) oldValue);
}
void OnActualBoundsChanged(AbstractProperty property, object oldValue)
{
_updateOpacityMask = true;
}
void OnEnabledChanged(AbstractProperty property, object oldValue)
{
if (!IsEnabled)
ResetFocus();
}
void OnHasFocusChanged(AbstractProperty property, object oldValue)
{
// propagate up the Visual Tree
var visual = VisualParent;
while (visual != null)
{
var fe = visual as FrameworkElement;
if (fe != null)
fe.IsKeyboardFocusWithin = HasFocus;
visual = visual.VisualParent;
}
}
void OnLayoutTransformChanged(IObservable observable)
{
InvalidateLayout(true, true);
}
void OnLayoutTransformPropertyChanged(AbstractProperty property, object oldValue)
{
if (oldValue is Transform)
((Transform) oldValue).ObjectChanged -= OnLayoutTransformChanged;
if (LayoutTransform != null)
LayoutTransform.ObjectChanged += OnLayoutTransformChanged;
}
void OnOpacityChanged(AbstractProperty property, object oldValue)
{
_updateOpacityMask = true;
}
/// <summary>
/// Called when a property has been changed which makes our arrangement invalid.
/// </summary>
/// <param name="property">The property.</param>
/// <param name="oldValue">The old value of the property.</param>
protected void OnArrangeGetsInvalid(AbstractProperty property, object oldValue)
{
InvalidateLayout(false, true);
}
/// <summary>
/// Called when a property has been changed which makes our measurement invalid.
/// </summary>
/// <param name="property">The property.</param>
/// <param name="oldValue">The old value of the property.</param>
protected void OnMeasureGetsInvalid(AbstractProperty property, object oldValue)
{
InvalidateLayout(true, false);
}
/// <summary>
/// Called when a property has been changed which makes our measurement and our arrangement invalid.
/// </summary>
/// <param name="property">The property.</param>
/// <param name="oldValue">The old value of the property.</param>
protected void OnCompleteLayoutGetsInvalid(AbstractProperty property, object oldValue)
{
InvalidateLayout(true, true);
}
/// <summary>
/// Called when a property has been changed which makes our parent's measurement and its arrangement invalid.
/// </summary>
/// <param name="property">The property.</param>
/// <param name="oldValue">The old value of the property.</param>
protected void OnParentCompleteLayoutGetsInvalid(AbstractProperty property, object oldValue)
{
InvalidateParentLayout(true, true);
}
#endregion
#region Public properties
/// <summary>
/// Returns the desired size this element calculated based on the available size.
/// This value denotes the desired size of this element including its <see cref="UIElement.Margin"/> in the parent's coordinate
/// system, i.e. with the <see cref="UIElement.RenderTransform"/> and <see cref="UIElement.LayoutTransform"/> applied.
/// </summary>
public SizeF DesiredSize
{
get { return _desiredSize; }
}
public AbstractProperty WidthProperty
{
get { return _widthProperty; }
}
public double Width
{
get { return (double) _widthProperty.GetValue(); }
set { _widthProperty.SetValue(value); }
}
public AbstractProperty HeightProperty
{
get { return _heightProperty; }
}
public double Height
{
get { return (double) _heightProperty.GetValue(); }
set { _heightProperty.SetValue(value); }
}
public AbstractProperty ActualWidthProperty
{
get { return _actualWidthProperty; }
}
public double ActualWidth
{
get { return (double) _actualWidthProperty.GetValue(); }
set { _actualWidthProperty.SetValue(value); }
}
public AbstractProperty ActualHeightProperty
{
get { return _actualHeightProperty; }
}
public double ActualHeight
{
get { return (double) _actualHeightProperty.GetValue(); }
set { _actualHeightProperty.SetValue(value); }
}
public AbstractProperty MinWidthProperty
{
get { return _minWidthProperty; }
}
public double MinWidth
{
get { return (double) _minWidthProperty.GetValue(); }
set { _minWidthProperty.SetValue(value); }
}
public AbstractProperty MinHeightProperty
{
get { return _minHeightProperty; }
}
public double MinHeight
{
get { return (double) _minHeightProperty.GetValue(); }
set { _minHeightProperty.SetValue(value); }
}
public AbstractProperty MaxWidthProperty
{
get { return _maxWidthProperty; }
}
public double MaxWidth
{
get { return (double) _maxWidthProperty.GetValue(); }
set { _maxWidthProperty.SetValue(value); }
}
public AbstractProperty MaxHeightProperty
{
get { return _maxHeightProperty; }
}
public double MaxHeight
{
get { return (double) _maxHeightProperty.GetValue(); }
set { _maxHeightProperty.SetValue(value); }
}
/// <summary>
/// Gets this element's bounds in this element's coordinate system.
/// This is a derived property which is calculated by the layout system.
/// </summary>
public RectangleF ActualBounds
{
get { return _innerRect; }
}
/// <summary>
/// Gets the actual bounds in layout plus <see cref="UIElement.Margin"/> plus the space which is needed for our
/// <see cref="UIElement.LayoutTransform"/> in the coordinate system of the parent.
/// Render transformations aren't taken into account here.
/// </summary>
public RectangleF ActualTotalBounds
{
get { return _outerRect ?? RectangleF.Empty; }
}
/// <summary>
/// Gets the box which encloses the position where this control is rendered in the world coordinate system.
/// </summary>
/// <remarks>
/// This property is only set if this element was rendered at its current position. If it was not yet rendered or rearranged
/// after the last rendering, this property is <c>null</c>.
/// </remarks>
public RectangleF? RenderedBoundingBox
{
get { return _renderedBoundingBox; }
}
/// <summary>
/// Gets the best known bounding box of the render area of this element.
/// </summary>
/// <remarks>
/// This property returns the <see cref="RenderedBoundingBox"/>, if present. Else, it will calculate the final transform which would
/// be used by this element if it would have been rendered before.
/// </remarks>
public RectangleF BoundingBox
{
get { return _renderedBoundingBox ?? CalculateBoundingBox(_innerRect, ExtortFinalTransform()); }
}
public AbstractProperty HorizontalAlignmentProperty
{
get { return _horizontalAlignmentProperty; }
}
public HorizontalAlignmentEnum HorizontalAlignment
{
get { return (HorizontalAlignmentEnum) _horizontalAlignmentProperty.GetValue(); }
set { _horizontalAlignmentProperty.SetValue(value); }
}
public AbstractProperty VerticalAlignmentProperty
{
get { return _verticalAlignmentProperty; }
}
public VerticalAlignmentEnum VerticalAlignment
{
get { return (VerticalAlignmentEnum) _verticalAlignmentProperty.GetValue(); }
set { _verticalAlignmentProperty.SetValue(value); }
}
public AbstractProperty StyleProperty
{
get { return _styleProperty; }
}
/// <summary>
/// Style of this element. This property must be set to a sensible value, else, this element cannot be rendered.
/// This property can be guessed by assigning te return value of <see cref="CopyDefaultStyle"/>.
/// </summary>
public Style Style
{
get { return (Style) _styleProperty.GetValue(); }
set { _styleProperty.SetValue(value); }
}
/// <summary>
/// Helper property to make it possible in the screenfiles to set the focus to a framework element (or its first focusable child)
/// when the screen is initialized. Use this property to set the initial focus.
/// </summary>
public SetFocusPriority SetFocusPrio
{
get { return _setFocusPrio; }
set
{
_setFocusPrio = value;
if (value > SetFocusPriority.None)
InvalidateLayout(false, true);
}
}
public bool SetFocus
{
get { return _setFocusPrio > SetFocusPriority.None; }
set { _setFocusPrio = value ? SetFocusPriority.Default : SetFocusPriority.None; }
}
public AbstractProperty HasFocusProperty
{
get { return _hasFocusProperty; }
}
/// <summary>
/// Returns the information whether this element currently has the focus. This element should not be set from the GUI!
/// </summary>
public virtual bool HasFocus
{
get { return (bool) _hasFocusProperty.GetValue(); }
internal set { _hasFocusProperty.SetValue(value); }
}
public AbstractProperty IsKeyboardFocusWithinProperty
{
get { return _isKeyboardFocusWithinProperty; }
}
/// <summary>
/// Gets a value indicating whether keyboard focus is anywhere within the element or its visual tree child elements.
/// </summary>
public virtual bool IsKeyboardFocusWithin
{
get { return (bool)_isKeyboardFocusWithinProperty.GetValue(); }
internal set { _isKeyboardFocusWithinProperty.SetValue(value); }
}
public AbstractProperty FocusableProperty
{
get { return _focusableProperty; }
}
public bool Focusable
{
get { return (bool) _focusableProperty.GetValue(); }
set { _focusableProperty.SetValue(value); }
}
public AbstractProperty IsMouseOverProperty
{
get { return _isMouseOverProperty; }
}
public bool IsMouseOver
{
get { return (bool) _isMouseOverProperty.GetValue(); }
internal set { _isMouseOverProperty.SetValue(value); }
}
public AbstractProperty ContextMenuCommandProperty
{
get { return _contextMenuCommandProperty; }
}
public IExecutableCommand ContextMenuCommand
{
get { return (IExecutableCommand) _contextMenuCommandProperty.GetValue(); }
internal set { _contextMenuCommandProperty.SetValue(value); }
}
public AbstractProperty FontFamilyProperty
{
get { return _fontFamilyProperty; }
}
// FontFamily & FontSize are defined in FrameworkElement because it should also be defined on
// panels, and in MPF, panels inherit from FrameworkElement
public string FontFamily
{
get { return (string) _fontFamilyProperty.GetValue(); }
set { _fontFamilyProperty.SetValue(value); }
}
public AbstractProperty FontSizeProperty
{
get { return _fontSizeProperty; }
}
// FontFamily & FontSize are defined in FrameworkElement because it should also be defined on
// panels, and in MPF, panels inherit from FrameworkElement
public int FontSize
{
get { return (int) _fontSizeProperty.GetValue(); }
set { _fontSizeProperty.SetValue(value); }
}
public bool IsMeasureInvalid
{
get { return _isMeasureInvalid; }
}
public bool IsArrangeInvalid
{
get { return _isArrangeInvalid; }
}
#endregion
#region Font handling
public string GetFontFamilyOrInherited()
{
string result = FontFamily;
Visual current = this;
while (string.IsNullOrEmpty(result) && (current = current.VisualParent) != null)
{
FrameworkElement currentFE = current as FrameworkElement;
if (currentFE != null)
result = currentFE.FontFamily;
}
return string.IsNullOrEmpty(result) ? FontManager.DefaultFontFamily : result;
}
public int GetFontSizeOrInherited()
{
int result = FontSize;
Visual current = this;
while (result == -1 && (current = current.VisualParent) != null)
{
FrameworkElement currentFE = current as FrameworkElement;
if (currentFE != null)
result = currentFE.FontSize;
}
return result == -1 ? FontManager.DefaultFontSize : result;
}
#endregion
#region Keyboard handling
internal override void OnKeyPreview(ref Key key)
{
base.OnKeyPreview(ref key);
if (!HasFocus)
return;
if (key == Key.None) return;
IExecutableCommand cmd = ContextMenuCommand;
if (key == Key.ContextMenu && ContextMenuCommand != null)
{
if (cmd != null)
InputManager.Instance.ExecuteCommand(cmd.Execute);
key = Key.None;
}
}
#endregion
#region hit testing
public override UIElement InputHitTest(PointF point)
{
if (!IsVisible)
return null;
if (IsInArea(point.X, point.Y))
{
// since we know the z-order here, lets use it, everything other is identical to UIElement implementation.
foreach (var uiElement in GetChildren().OrderByDescending(e => (e is FrameworkElement) ? ((FrameworkElement)e)._lastZIndex : 0f))
{
var hitElement = uiElement.InputHitTest(point);
if (hitElement != null)
{
return hitElement;
}
}
if (IsInVisibleArea(point.X, point.Y))
{
return this;
}
}
return null;
}
#endregion
#region Mouse handling
protected bool TransformMouseCoordinates(ref float x, ref float y)
{
Matrix? ift = _inverseFinalTransform;
if (ift.HasValue)
{
ift.Value.Transform(ref x, ref y);
return true;
}
return false;
}
public bool CanHandleMouseMove()
{
return _inverseFinalTransform.HasValue;
}
internal override void OnMouseMove(float x, float y, ICollection<FocusCandidate> focusCandidates)
{
if (IsVisible)
{
if (IsInVisibleArea(x, y))
{
if (!IsMouseOver)
{
IsMouseOver = true;
FireEvent(MOUSEENTER_EVENT, RoutingStrategyEnum.Direct);
}
focusCandidates.Add(new FocusCandidate(this, _lastZIndex));
}
else
{
if (IsMouseOver)
{
IsMouseOver = false;
FireEvent(MOUSELEAVE_EVENT, RoutingStrategyEnum.Direct);
}
}
}
base.OnMouseMove(x, y, focusCandidates);
}
#endregion
#region Focus handling
/// <summary>
/// Checks if this element is focusable. This is the case if the element is visible, enabled and
/// focusable. If this is the case, this method will set the focus to this element.
/// </summary>
/// <param name="checkChildren">If this parameter is set to <c>true</c>, this method will also try to
/// set the focus to any of its child elements.</param>
public bool TrySetFocus(bool checkChildren)
{
if (HasFocus)
return true;
if (IsVisible && IsEnabled && Focusable)
{
Screen screen = Screen;
if (screen == null)
return false;
RectangleF actualBounds = ActualBounds;
BringIntoView(this, actualBounds);
screen.UpdateFocusRect(actualBounds);
screen.FrameworkElementGotFocus(this);
HasFocus = true;
return true;
}
if (checkChildren)
return GetChildren().OfType<FrameworkElement>().Any(fe => fe.TrySetFocus(true));
return false;
}
public void ResetFocus()
{
HasFocus = false;
Screen screen = Screen;
if (screen == null)
return;
screen.FrameworkElementLostFocus(this);
}
protected void UpdateFocus()
{
if (_setFocusPrio == SetFocusPriority.None)
return;
Screen screen = Screen;
if (screen == null)
return;
screen.ScheduleSetFocus(this, _setFocusPrio);
_setFocusPrio = SetFocusPriority.None;
}
/// <summary>
/// Checks if the currently focused control is contained in this virtual keyboard control.
/// </summary>
public bool IsInFocusRootPath()
{
Screen screen = Screen;
Visual focusPath = screen == null ? null : screen.FocusedElement;
while (focusPath != null)
{
if (focusPath == this)
// Focused control is located in our focus scope
return true;
focusPath = focusPath.VisualParent;
}
return false;
}
#endregion
#region Focus & control predicition
#region Focus movement
protected FrameworkElement GetFocusedElementOrChild()
{
Screen screen = Screen;
FrameworkElement result = screen == null ? null : screen.FocusedElement;
if (result == null)
foreach (UIElement child in GetChildren())
{
result = child as FrameworkElement;
if (result != null)
break;
}
return result;
}
/// <summary>
/// Moves the focus from the currently focused element in the screen to the first child element in the given
/// direction.
/// </summary>
/// <param name="direction">Direction to move the focus.</param>
/// <returns><c>true</c>, if the focus could be moved to the desired child, else <c>false</c>.</returns>
protected bool MoveFocus1(MoveFocusDirection direction)
{
FrameworkElement currentElement = GetFocusedElementOrChild();
if (currentElement == null)
return false;
FrameworkElement nextElement = PredictFocus(currentElement.ActualBounds, direction);
if (nextElement == null) return false;
return nextElement.TrySetFocus(true);
}
/// <summary>
/// Moves the focus from the currently focused element in the screen to our last child in the given
/// direction. For example if <c>direction == MoveFocusDirection.Up</c>, this method tries to focus the
/// topmost child.
/// </summary>
/// <param name="direction">Direction to move the focus.</param>
/// <returns><c>true</c>, if the focus could be moved to the desired child, else <c>false</c>.</returns>
protected bool MoveFocusN(MoveFocusDirection direction)
{
FrameworkElement currentElement = GetFocusedElementOrChild();
if (currentElement == null)
return false;
ICollection<FrameworkElement> focusableChildren = GetFEChildren();
if (focusableChildren.Count == 0)
return false;
FrameworkElement nextElement;
while ((nextElement = FindNextFocusElement(focusableChildren, currentElement.ActualBounds, direction)) != null)
currentElement = nextElement;
return currentElement.TrySetFocus(true);
}
#endregion
#region Focus prediction
/// <summary>
/// Predicts the next control located inside this element which is positioned in the specified direction
/// <paramref name="dir"/> to the specified <paramref name="currentFocusRect"/> and
/// which is able to get the focus.
/// This method will search the control tree down starting with this element as root element.
/// This method is only able to find focusable elements which are located at least one element outside the visible
/// range (see <see cref="AddPotentialFocusableElements"/>).
/// </summary>
/// <param name="currentFocusRect">The borders of the currently focused control.</param>
/// <param name="dir">Direction, the result control should be positioned relative to the
/// currently focused control.</param>
/// <returns>Framework element which should get the focus, or <c>null</c>, if this control
/// tree doesn't contain an appropriate control. The returned control will be