-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
RibbonMenuButton.cs
1700 lines (1460 loc) · 69.8 KB
/
RibbonMenuButton.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#if RIBBON_IN_FRAMEWORK
namespace System.Windows.Controls.Ribbon
#else
namespace Microsoft.Windows.Controls.Ribbon
#endif
{
#region Using declarations
using System;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Reflection;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
#if RIBBON_IN_FRAMEWORK
using System.Windows.Controls.Ribbon.Primitives;
using Microsoft.Windows.Controls;
#else
using Microsoft.Windows.Automation.Peers;
using Microsoft.Windows.Controls.Ribbon.Primitives;
#endif
using MS.Internal;
#endregion
/// <summary>
/// RibbonMenuButton is an ItemsControl which on clicking displays a Menu. Its Items could be either RibbonMenuItems, RibbonGallerys or Separators.
/// </summary>
[TemplatePart(Name = RibbonMenuButton.ResizeThumbTemplatePartName, Type = typeof(Thumb))]
[TemplatePart(Name = RibbonMenuButton.ToggleButtonTemplatePartName, Type = typeof(RibbonToggleButton))]
[TemplatePart(Name = RibbonMenuButton.PopupTemplatePartName, Type = typeof(Popup))]
[TemplatePart(Name = RibbonMenuButton.SubMenuScrollViewerTemplatePartName, Type = typeof(ScrollViewer))]
[StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(RibbonMenuItem))]
public class RibbonMenuButton : Menu
{
#region Constructors
/// <summary>
/// Initializes static members of the RibbonMenuButton class. Here we override the default
/// style, a coerce callback, and allow tooltips to be shown for disabled commands.
/// </summary>
static RibbonMenuButton()
{
Type ownerType = typeof(RibbonMenuButton);
DefaultStyleKeyProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(ownerType));
ItemsPanelTemplate template = new ItemsPanelTemplate(new FrameworkElementFactory(typeof(RibbonMenuItemsPanel)));
template.Seal();
ItemsPanelProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(template));
ToolTipProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(null, new CoerceValueCallback(RibbonHelper.CoerceRibbonToolTip)));
ToolTipService.ShowOnDisabledProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(true));
ContextMenuProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(RibbonHelper.OnContextMenuChanged, RibbonHelper.OnCoerceContextMenu));
ContextMenuService.ShowOnDisabledProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(true));
IsMainMenuProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(false));
EventManager.RegisterClassHandler(ownerType, UIElement.LostMouseCaptureEvent, new MouseEventHandler(OnLostMouseCaptureThunk));
EventManager.RegisterClassHandler(ownerType, RibbonControlService.DismissPopupEvent, new RibbonDismissPopupEventHandler(OnDismissPopupThunk));
EventManager.RegisterClassHandler(ownerType, UIElement.GotKeyboardFocusEvent, new KeyboardFocusChangedEventHandler(OnGotKeyboardFocusThunk), true);
EventManager.RegisterClassHandler(ownerType, Mouse.PreviewMouseDownOutsideCapturedElementEvent, new MouseButtonEventHandler(OnClickThroughThunk));
EventManager.RegisterClassHandler(ownerType, Mouse.MouseDownEvent, new MouseButtonEventHandler(OnMouseDownThunk), true);
// pseudo-inherited properties
IsInControlGroupProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(OnPseudoInheritedPropertyChanged), RibbonControlService.IsInControlGroupPropertyKey);
// This should not be a focus scope since Ribbon will be a focus scope.
FocusManager.IsFocusScopeProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(false));
EventManager.RegisterClassHandler(ownerType, RibbonMenuButton.RibbonIsSelectedChangedEvent, new RoutedPropertyChangedEventHandler<bool>(OnRibbonIsSelectedChanged));
KeyboardNavigation.DirectionalNavigationProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(KeyboardNavigationMode.Local));
EventManager.RegisterClassHandler(ownerType, FrameworkElement.ContextMenuOpeningEvent, new ContextMenuEventHandler(OnContextMenuOpeningThunk), true);
EventManager.RegisterClassHandler(ownerType, FrameworkElement.ContextMenuClosingEvent, new ContextMenuEventHandler(OnContextMenuClosingThunk), true);
EventManager.RegisterClassHandler(ownerType, KeyTipService.ActivatingKeyTipEvent, new ActivatingKeyTipEventHandler(OnActivatingKeyTipThunk));
EventManager.RegisterClassHandler(ownerType, KeyTipService.KeyTipAccessedEvent, new KeyTipAccessedEventHandler(OnKeyTipAccessedThunk));
}
#endregion
#region RibbonControlService Properties
/// <summary>
/// DependencyProperty for LargeImageSource property.
/// </summary>
public static readonly DependencyProperty LargeImageSourceProperty =
RibbonControlService.LargeImageSourceProperty.AddOwner(typeof(RibbonMenuButton));
/// <summary>
/// ImageSource property which is normally a 32X32 icon.
/// </summary>
public ImageSource LargeImageSource
{
get { return RibbonControlService.GetLargeImageSource(this); }
set { RibbonControlService.SetLargeImageSource(this, value); }
}
/// <summary>
/// DependencyProperty for SmallImageSource property.
/// </summary>
public static readonly DependencyProperty SmallImageSourceProperty =
RibbonControlService.SmallImageSourceProperty.AddOwner(typeof(RibbonMenuButton));
/// <summary>
/// ImageSource property which is normally a 16X16 icon.
/// </summary>
public ImageSource SmallImageSource
{
get { return RibbonControlService.GetSmallImageSource(this); }
set { RibbonControlService.SetSmallImageSource(this, value); }
}
/// <summary>
/// DependencyProperty for Label property.
/// </summary>
public static readonly DependencyProperty LabelProperty =
RibbonControlService.LabelProperty.AddOwner(typeof(RibbonMenuButton));
/// <summary>
/// Primary label text for the control.
/// </summary>
public string Label
{
get { return RibbonControlService.GetLabel(this); }
set { RibbonControlService.SetLabel(this, value); }
}
/// <summary>
/// DependencyProperty for ToolTipTitle property.
/// </summary>
public static readonly DependencyProperty ToolTipTitleProperty =
RibbonControlService.ToolTipTitleProperty.AddOwner(typeof(RibbonMenuButton), new FrameworkPropertyMetadata(new PropertyChangedCallback(RibbonHelper.OnRibbonToolTipPropertyChanged)));
/// <summary>
/// Title text for the tooltip of the control.
/// </summary>
public string ToolTipTitle
{
get { return RibbonControlService.GetToolTipTitle(this); }
set { RibbonControlService.SetToolTipTitle(this, value); }
}
/// <summary>
/// DependencyProperty for ToolTipDescription property.
/// </summary>
public static readonly DependencyProperty ToolTipDescriptionProperty =
RibbonControlService.ToolTipDescriptionProperty.AddOwner(typeof(RibbonMenuButton), new FrameworkPropertyMetadata(new PropertyChangedCallback(RibbonHelper.OnRibbonToolTipPropertyChanged)));
/// <summary>
/// Description text for the tooltip of the control.
/// </summary>
public string ToolTipDescription
{
get { return RibbonControlService.GetToolTipDescription(this); }
set { RibbonControlService.SetToolTipDescription(this, value); }
}
/// <summary>
/// DependencyProperty for ToolTipImageSource property.
/// </summary>
public static readonly DependencyProperty ToolTipImageSourceProperty =
RibbonControlService.ToolTipImageSourceProperty.AddOwner(typeof(RibbonMenuButton), new FrameworkPropertyMetadata(new PropertyChangedCallback(RibbonHelper.OnRibbonToolTipPropertyChanged)));
/// <summary>
/// Image source for the tooltip of the control.
/// </summary>
public ImageSource ToolTipImageSource
{
get { return RibbonControlService.GetToolTipImageSource(this); }
set { RibbonControlService.SetToolTipImageSource(this, value); }
}
/// <summary>
/// DependencyProperty for ToolTipFooterTitle property.
/// </summary>
public static readonly DependencyProperty ToolTipFooterTitleProperty =
RibbonControlService.ToolTipFooterTitleProperty.AddOwner(typeof(RibbonMenuButton), new FrameworkPropertyMetadata(new PropertyChangedCallback(RibbonHelper.OnRibbonToolTipPropertyChanged)));
/// <summary>
/// Title text for the footer of tooltip of the control.
/// </summary>
public string ToolTipFooterTitle
{
get { return RibbonControlService.GetToolTipFooterTitle(this); }
set { RibbonControlService.SetToolTipFooterTitle(this, value); }
}
/// <summary>
/// DependencyProperty for ToolTipFooterDescription property.
/// </summary>
public static readonly DependencyProperty ToolTipFooterDescriptionProperty =
RibbonControlService.ToolTipFooterDescriptionProperty.AddOwner(typeof(RibbonMenuButton), new FrameworkPropertyMetadata(new PropertyChangedCallback(RibbonHelper.OnRibbonToolTipPropertyChanged)));
/// <summary>
/// Description text for the footer of the tooltip of the control.
/// </summary>
public string ToolTipFooterDescription
{
get { return RibbonControlService.GetToolTipFooterDescription(this); }
set { RibbonControlService.SetToolTipFooterDescription(this, value); }
}
/// <summary>
/// DependencyProperty for ToolTipFooterImageSource property.
/// </summary>
public static readonly DependencyProperty ToolTipFooterImageSourceProperty =
RibbonControlService.ToolTipFooterImageSourceProperty.AddOwner(typeof(RibbonMenuButton), new FrameworkPropertyMetadata(new PropertyChangedCallback(RibbonHelper.OnRibbonToolTipPropertyChanged)));
/// <summary>
/// Image source for the footer of the tooltip of the control.
/// </summary>
public ImageSource ToolTipFooterImageSource
{
get { return RibbonControlService.GetToolTipFooterImageSource(this); }
set { RibbonControlService.SetToolTipFooterImageSource(this, value); }
}
#endregion
#region PseudoInheritedProperties
/// <summary>
/// DependencyProperty for ControlSizeDefinition property.
/// </summary>
public static readonly DependencyProperty ControlSizeDefinitionProperty =
RibbonControlService.ControlSizeDefinitionProperty.AddOwner(typeof(RibbonMenuButton));
/// <summary>
/// Size definition, including image size and visibility of label and image, for this control
/// </summary>
public RibbonControlSizeDefinition ControlSizeDefinition
{
get { return RibbonControlService.GetControlSizeDefinition(this); }
set { RibbonControlService.SetControlSizeDefinition(this, value); }
}
/// <summary>
/// DependencyProperty for IsInControlGroup property.
/// </summary>
public static readonly DependencyProperty IsInControlGroupProperty =
RibbonControlService.IsInControlGroupProperty.AddOwner(typeof(RibbonMenuButton));
/// <summary>
/// This property indicates whether the control is part of a RibbonControlGroup.
/// </summary>
public bool IsInControlGroup
{
get { return RibbonControlService.GetIsInControlGroup(this); }
internal set { RibbonControlService.SetIsInControlGroup(this, value); }
}
/// <summary>
/// DependencyProperty for QuickAccessToolBarControlSizeDefinition property.
/// </summary>
public static readonly DependencyProperty QuickAccessToolBarControlSizeDefinitionProperty =
RibbonControlService.QuickAccessToolBarControlSizeDefinitionProperty.AddOwner(typeof(RibbonMenuButton));
/// <summary>
/// Size definition to apply to this control when it's placed in a QuickAccessToolBar.
/// </summary>
public RibbonControlSizeDefinition QuickAccessToolBarControlSizeDefinition
{
get { return RibbonControlService.GetQuickAccessToolBarControlSizeDefinition(this); }
set { RibbonControlService.SetQuickAccessToolBarControlSizeDefinition(this, value); }
}
/// <summary>
/// DependencyProperty for IsInQuickAccessToolBar property.
/// </summary>
public static readonly DependencyProperty IsInQuickAccessToolBarProperty =
RibbonControlService.IsInQuickAccessToolBarProperty.AddOwner(typeof(RibbonMenuButton));
/// <summary>
/// This property indicates whether the control is part of a QuickAccessToolBar.
/// </summary>
public bool IsInQuickAccessToolBar
{
get { return RibbonControlService.GetIsInQuickAccessToolBar(this); }
internal set { RibbonControlService.SetIsInQuickAccessToolBar(this, value); }
}
private static void OnPseudoInheritedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
RibbonMenuButton menuButton = (RibbonMenuButton)d;
menuButton.TransferPseudoInheritedProperties();
}
internal virtual void TransferPseudoInheritedProperties()
{
if (_partToggleButton != null && RibbonControlService.GetIsInControlGroup(this))
{
RibbonControlService.SetIsInControlGroup(_partToggleButton, true);
}
}
#endregion
#region Visual States
/// <summary>
/// DependencyProperty for Ribbon property.
/// </summary>
public static readonly DependencyProperty RibbonProperty =
RibbonControlService.RibbonProperty.AddOwner(typeof(RibbonMenuButton));
/// <summary>
/// This property is used to access visual style brushes defined on the Ribbon class.
/// </summary>
public Ribbon Ribbon
{
get { return RibbonControlService.GetRibbon(this); }
}
/// <summary>
/// DependencyProperty for MouseOverBorderBrush property.
/// </summary>
public static readonly DependencyProperty MouseOverBorderBrushProperty =
RibbonControlService.MouseOverBorderBrushProperty.AddOwner(typeof(RibbonMenuButton));
/// <summary>
/// Outer border brush used in a "hover" state of the RibbonMenuButton.
/// </summary>
public Brush MouseOverBorderBrush
{
get { return RibbonControlService.GetMouseOverBorderBrush(this); }
set { RibbonControlService.SetMouseOverBorderBrush(this, value); }
}
/// <summary>
/// DependencyProperty for MouseOverBackground property.
/// </summary>
public static readonly DependencyProperty MouseOverBackgroundProperty =
RibbonControlService.MouseOverBackgroundProperty.AddOwner(typeof(RibbonMenuButton));
/// <summary>
/// Control background brush used in a "hover" state of the RibbonMenuButton.
/// </summary>
public Brush MouseOverBackground
{
get { return RibbonControlService.GetMouseOverBackground(this); }
set { RibbonControlService.SetMouseOverBackground(this, value); }
}
/// <summary>
/// DependencyProperty for PressedBorderBrush property.
/// </summary>
public static readonly DependencyProperty PressedBorderBrushProperty =
RibbonControlService.PressedBorderBrushProperty.AddOwner(typeof(RibbonMenuButton));
/// <summary>
/// Outer border brush used in a "pressed" state of the RibbonMenuButton.
/// </summary>
public Brush PressedBorderBrush
{
get { return RibbonControlService.GetPressedBorderBrush(this); }
set { RibbonControlService.SetPressedBorderBrush(this, value); }
}
/// <summary>
/// DependencyProperty for PressedBackground property.
/// </summary>
public static readonly DependencyProperty PressedBackgroundProperty =
RibbonControlService.PressedBackgroundProperty.AddOwner(typeof(RibbonMenuButton));
/// <summary>
/// Control background brush used in a "pressed" state of the RibbonMenuButton.
/// </summary>
public Brush PressedBackground
{
get { return RibbonControlService.GetPressedBackground(this); }
set { RibbonControlService.SetPressedBackground(this, value); }
}
/// <summary>
/// DependencyProperty for FocusedBackground property.
/// </summary>
public static readonly DependencyProperty FocusedBackgroundProperty =
RibbonControlService.FocusedBackgroundProperty.AddOwner(typeof(RibbonMenuButton));
/// <summary>
/// Control background brush used in a "Focused" state of the RibbonMenuButton.
/// </summary>
public Brush FocusedBackground
{
get { return RibbonControlService.GetFocusedBackground(this); }
set { RibbonControlService.SetFocusedBackground(this, value); }
}
/// <summary>
/// DependencyProperty for FocusedBorderBrush property.
/// </summary>
public static readonly DependencyProperty FocusedBorderBrushProperty =
RibbonControlService.FocusedBorderBrushProperty.AddOwner(typeof(RibbonMenuButton));
/// <summary>
/// Control border brush used to paint a "Focused" state of the RibbonMenuButton.
/// </summary>
public Brush FocusedBorderBrush
{
get { return RibbonControlService.GetFocusedBorderBrush(this); }
set { RibbonControlService.SetFocusedBorderBrush(this, value); }
}
#endregion
#region DropDownProperties
/// <summary>
/// DropDown Open event
/// </summary>
public event EventHandler DropDownOpened;
/// <summary>
/// DropDown Close event
/// </summary>
public event EventHandler DropDownClosed;
public static readonly DependencyProperty IsDropDownOpenProperty =
ComboBox.IsDropDownOpenProperty.AddOwner(typeof(RibbonMenuButton),
new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnIsDropDownOpenChanged), new CoerceValueCallback(CoerceIsDropDownOpen)));
public bool IsDropDownOpen
{
get { return (bool)GetValue(IsDropDownOpenProperty); }
set { SetValue(IsDropDownOpenProperty, value); }
}
public static readonly DependencyProperty DropDownHeightProperty = DependencyProperty.Register("DropDownHeight",
typeof(double),
typeof(RibbonMenuButton),
new FrameworkPropertyMetadata(double.NaN,
null,
new CoerceValueCallback(CoerceDropDownHeightProperty)),
new ValidateValueCallback(IsHeightValid));
/// <summary>
/// Gets or Sets the height of the Popup that is displayed when this button is invoked
/// Applicable only when Items collection has atleast one RibbonGallery.
/// </summary>
public double DropDownHeight
{
get { return (double)GetValue(DropDownHeightProperty); }
set { SetValue(DropDownHeightProperty, value); }
}
public static readonly DependencyProperty CanUserResizeVerticallyProperty = DependencyProperty.Register("CanUserResizeVertically",
typeof(bool),
typeof(RibbonMenuButton),
new FrameworkPropertyMetadata(false,
null,
new CoerceValueCallback(CoerceCanUserResizeProperty)));
/// <summary>
/// Gets or sets whether user is allowed to resize this button's dropdown Popup vertically
/// Applicable only when Items collection has atleast one RibbonGallery.
/// </summary>
public bool CanUserResizeVertically
{
get { return (bool)GetValue(CanUserResizeVerticallyProperty); }
set { SetValue(CanUserResizeVerticallyProperty, value); }
}
public static readonly DependencyProperty CanUserResizeHorizontallyProperty = DependencyProperty.Register("CanUserResizeHorizontally",
typeof(bool),
typeof(RibbonMenuButton),
new FrameworkPropertyMetadata(false,
null,
new CoerceValueCallback(CoerceCanUserResizeProperty)));
/// <summary>
/// Gets or sets whether user is allowed to resize this button's dropdown Popup horizontally
/// Applicable only when Items collection has atleast one RibbonGallery
/// and when CanUserResizeVertically is also set to true
/// </summary>
public bool CanUserResizeHorizontally
{
get { return (bool)GetValue(CanUserResizeHorizontallyProperty); }
set { SetValue(CanUserResizeHorizontallyProperty, value); }
}
internal static readonly DependencyPropertyKey HasGalleryPropertyKey = DependencyProperty.RegisterReadOnly("HasGallery",
typeof(bool),
typeof(RibbonMenuButton),
new FrameworkPropertyMetadata(false,
new PropertyChangedCallback(OnHasGalleryChanged)));
public static readonly DependencyProperty HasGalleryProperty = HasGalleryPropertyKey.DependencyProperty;
/// <summary>
/// Indicates that there is atleast one RibbonGallery in Items collection.
/// </summary>
public bool HasGallery
{
get { return (bool)GetValue(HasGalleryProperty); }
private set { SetValue(HasGalleryPropertyKey, value); }
}
private static readonly DependencyPropertyKey IsDropDownPositionedAbovePropertyKey = DependencyProperty.RegisterReadOnly("IsDropDownPositionedAbove",
typeof(bool),
typeof(RibbonMenuButton),
new FrameworkPropertyMetadata(false));
public static readonly DependencyProperty IsDropDownPositionedAboveProperty = IsDropDownPositionedAbovePropertyKey.DependencyProperty;
/// <summary>
/// Indicates whether the Dropdown Popup position has been flipped to top
/// because Popup was near a screen edge.
/// </summary>
public bool IsDropDownPositionedAbove
{
get { return (bool)GetValue(IsDropDownPositionedAboveProperty); }
private set { SetValue(IsDropDownPositionedAbovePropertyKey, value); }
}
#endregion DropDownProperties
#region ContainerGeneration
private object _currentItem;
protected override bool IsItemItsOwnContainerOverride(object item)
{
bool ret = (item is RibbonMenuItem) || (item is RibbonSeparator) || (item is RibbonGallery);
if (!ret)
{
_currentItem = item;
}
return ret;
}
protected override DependencyObject GetContainerForItemOverride()
{
object currentItem = _currentItem;
_currentItem = null;
if (UsesItemContainerTemplate)
{
DataTemplate itemContainerTemplate = ItemContainerTemplateSelector.SelectTemplate(currentItem, this);
if (itemContainerTemplate != null)
{
object itemContainer = itemContainerTemplate.LoadContent();
if (itemContainer is RibbonMenuItem || itemContainer is RibbonGallery || itemContainer is RibbonSeparator)
{
return itemContainer as DependencyObject;
}
else
{
throw new InvalidOperationException(Microsoft.Windows.Controls.SR.Get(Microsoft.Windows.Controls.SRID.InvalidMenuButtonOrItemContainer, this.GetType().Name, itemContainer));
}
}
}
return new RibbonMenuItem();
}
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
if (element is RibbonGallery)
{
HasGallery = (++_galleryCount > 0);
}
else
{
RibbonSeparator separator = element as RibbonSeparator;
if (separator != null)
{
ValueSource vs = DependencyPropertyHelper.GetValueSource(separator, StyleProperty);
if (vs.BaseValueSource <= BaseValueSource.ImplicitStyleReference)
separator.SetResourceReference(StyleProperty, MenuItem.SeparatorStyleKey);
separator.DefaultStyleKeyInternal = MenuItem.SeparatorStyleKey;
}
}
}
protected override void ClearContainerForItemOverride(DependencyObject element, object item)
{
base.ClearContainerForItemOverride(element, item);
// RibbonComboBox containers are pre-generated.
// When dropdown is opened for the first time ever and ItemContainerGenerator
// is hooked up to ItemsPanel, existing containers are cleared, causing _galleryCount to be -ve.
// Hence the check for _galleryCount > 0
if (element is RibbonGallery && _galleryCount > 0)
{
HasGallery = (--_galleryCount > 0);
}
}
protected override bool ShouldApplyItemContainerStyle(DependencyObject container, object item)
{
if (container is RibbonSeparator ||
container is RibbonGallery)
{
return false;
}
else
{
return base.ShouldApplyItemContainerStyle(container, item);
}
}
#if !RIBBON_IN_FRAMEWORK
/// <summary>
/// DependencyProperty for ItemContainerTemplateSelector property.
/// </summary>
public static readonly DependencyProperty ItemContainerTemplateSelectorProperty =
DependencyProperty.Register(
"ItemContainerTemplateSelector",
typeof(ItemContainerTemplateSelector),
typeof(RibbonMenuButton),
new FrameworkPropertyMetadata(new DefaultItemContainerTemplateSelector()));
/// <summary>
/// ItemContainerTemplateSelector property which provides the DataTemplate to be used to create an instance of the ItemContainer.
/// </summary>
public ItemContainerTemplateSelector ItemContainerTemplateSelector
{
get { return (ItemContainerTemplateSelector)GetValue(ItemContainerTemplateSelectorProperty); }
set { SetValue(ItemContainerTemplateSelectorProperty, value); }
}
/// <summary>
/// DependencyProperty for UsesItemContainerTemplateSelector property.
/// </summary>
public static readonly DependencyProperty UsesItemContainerTemplateProperty =
DependencyProperty.Register(
"UsesItemContainerTemplate",
typeof(bool),
typeof(RibbonMenuButton));
/// <summary>
/// UsesItemContainerTemplate property which says whether the ItemContainerTemplateSelector property is to be used.
/// </summary>
public bool UsesItemContainerTemplate
{
get { return (bool)GetValue(UsesItemContainerTemplateProperty); }
set { SetValue(UsesItemContainerTemplateProperty, value); }
}
#endif
#endregion ContainerGeneration
#region Protected Methods
#if RIBBON_IN_FRAMEWORK
protected internal override bool HandlesScrolling
#else
protected override bool HandlesScrolling
#endif
{
get
{
return true;
}
}
public override void OnApplyTemplate()
{
// If a new template has just been generated then
// be sure to clear any stale ItemsHost references
if (InternalItemsHost != null && !this.IsAncestorOf(InternalItemsHost))
{
InternalItemsHost = null;
}
CoerceValue(ControlSizeDefinitionProperty);
base.OnApplyTemplate();
if (_resizeThumb != null)
{
_resizeThumb.DragStarted -= new DragStartedEventHandler(OnPopupResizeStarted);
_resizeThumb.DragDelta -= new DragDeltaEventHandler(OnPopupResize);
}
_resizeThumb = GetTemplateChild(ResizeThumbTemplatePartName) as Thumb;
if (_resizeThumb != null)
{
_resizeThumb.DragStarted += new DragStartedEventHandler(OnPopupResizeStarted);
_resizeThumb.DragDelta += new DragDeltaEventHandler(OnPopupResize);
}
_itemsPresenter = GetTemplateChild(ItemsPresenterTemplatePartName) as ItemsPresenter;
_partToggleButton = GetTemplateChild(ToggleButtonTemplatePartName) as RibbonToggleButton;
_popup = GetTemplateChild(PopupTemplatePartName) as Popup;
_popupRoot = null;
TransferPseudoInheritedProperties();
PropertyHelper.TransferProperty(this, ContextMenuProperty); // Coerce to get a default ContextMenu if none has been specified.
PropertyHelper.TransferProperty(this, RibbonControlService.CanAddToQuickAccessToolBarDirectlyProperty);
TemplateApplied = true;
ItemContainerGenerator.StatusChanged += new EventHandler(OnItemContainerGeneratorStatusChanged);
_submenuScrollViewer = GetTemplateChild(SubMenuScrollViewerTemplatePartName) as ScrollViewer;
if (_submenuScrollViewer != null)
{
KeyTipService.SetCanClipKeyTip(_submenuScrollViewer, false);
}
}
protected override void OnTemplateChanged(ControlTemplate oldTemplate, ControlTemplate newTemplate)
{
TemplateApplied = false;
base.OnTemplateChanged(oldTemplate, newTemplate);
if ((oldTemplate != null) && (_partToggleButton != null))
{
RibbonHelper.ClearPseudoInheritedProperties(_partToggleButton);
_partToggleButton = null;
}
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
HandleSpaceEnterUp = false;
if (e.OriginalSource == _partToggleButton ||
e.OriginalSource == this)
{
if (e.Key == Key.Space ||
e.Key == Key.Enter)
{
// Do not let _partToggleButton handle space and enter.
// This is because it will open dropdown on space/enter
// down where as we need to open on key up.
HandleSpaceEnterUp = true;
e.Handled = true;
}
}
base.OnPreviewKeyDown(e);
}
protected override void OnKeyUp(KeyEventArgs e)
{
if (!IsDropDownOpen && HandleSpaceEnterUp)
{
if (e.OriginalSource == this ||
e.OriginalSource == _partToggleButton)
{
if (e.Key == Key.Space ||
e.Key == Key.Enter)
{
IsDropDownOpen = true;
RibbonHelper.NavigateToNextMenuItemOrGallery(this, -1, BringIndexIntoView);
e.Handled = true;
}
}
}
HandleSpaceEnterUp = false;
base.OnKeyUp(e);
}
protected override void OnKeyDown(KeyEventArgs e)
{
RibbonHelper.HandleDropDownKeyDown(this, e,
delegate { return IsDropDownOpen; },
delegate(bool value) { IsDropDownOpen = value; },
RetainFocusOnEscape ? _partToggleButton : null,
_popup.TryGetChild());
// Do not call base because base's logic interferes
// with that of RibbonMenuButton.
// base.OnKeyDown(e);
if (e.Handled)
return;
OnNavigationKeyDown(e);
}
internal void OnNavigationKeyDown(KeyEventArgs e)
{
int focusedIndex = ItemContainerGenerator.IndexFromContainer(e.OriginalSource as DependencyObject);
bool itemNavigateFromCurrentFocused = true;
if (focusedIndex < 0)
{
UIElement popupChild = _popup.TryGetChild();
if (popupChild != null &&
popupChild.IsKeyboardFocusWithin)
{
// If the popup already has focus within,
// then the focused element is not the item container
// itself, but is inside one such container (eg. filter button
// of gallery). Hence do not navigate in such case, but do default
// keyboard navigation.
itemNavigateFromCurrentFocused = false;
}
}
bool handled = false;
DependencyObject focusedElement = Keyboard.FocusedElement as DependencyObject;
switch (e.Key)
{
case Key.Home:
if (IsDropDownOpen)
{
handled = RibbonHelper.NavigateToNextMenuItemOrGallery(this, -1, BringIndexIntoView);
}
break;
case Key.End:
if (IsDropDownOpen)
{
handled = RibbonHelper.NavigateToPreviousMenuItemOrGallery(this, -1, BringIndexIntoView);
}
break;
case Key.Down:
if (IsDropDownOpen)
{
if (itemNavigateFromCurrentFocused)
{
// event could have bubbled up from MenuItem
// when it could not navigate to the next item (for eg. gallery)
handled = RibbonHelper.NavigateToNextMenuItemOrGallery(this, focusedIndex, BringIndexIntoView);
}
else
{
RibbonHelper.MoveFocus(FocusNavigationDirection.Down);
handled = true;
}
}
break;
case Key.Up:
if (IsDropDownOpen)
{
if (itemNavigateFromCurrentFocused)
{
// event could have bubbled up from MenuItem
// when it could not navigate to the previous item (for eg. gallery)
handled = RibbonHelper.NavigateToPreviousMenuItemOrGallery(this, focusedIndex, BringIndexIntoView);
}
else
{
RibbonHelper.MoveFocus(FocusNavigationDirection.Up);
handled = true;
}
}
break;
case Key.Tab:
if (IsDropDownOpen &&
(IsFocused || (focusedElement != null && TreeHelper.IsVisualAncestorOf(this, focusedElement))))
{
if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
{
handled = RibbonHelper.NavigateToPreviousMenuItemOrGallery(this, Items.Count, BringIndexIntoView);
}
else
{
handled = RibbonHelper.NavigateToNextMenuItemOrGallery(this, -1, BringIndexIntoView);
}
}
break;
}
e.Handled = handled;
}
#endregion
#region Selection
/// <summary>
/// Event raised by either RibbonMenuItem or RibbonGallery to indicate to its parent that it is currently selected.
///
/// We use the Ribbon prefix to disambiguate this property from MenuBase.IsSelectedChangedEvent.
/// </summary>
internal static readonly RoutedEvent RibbonIsSelectedChangedEvent = EventManager.RegisterRoutedEvent(
"RibbonIsSelectedChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<bool>), typeof(RibbonMenuButton));
/// <summary>
/// Called when RibbonIsSelected changed on this element or any descendant.
/// </summary>
private static void OnRibbonIsSelectedChanged(object sender, RoutedPropertyChangedEventArgs<bool> e)
{
FrameworkElement selectionItem = e.OriginalSource as FrameworkElement;
if (selectionItem != null)
{
RibbonMenuButton menu = (RibbonMenuButton)sender;
// If the selected item is a child of ours, make it the current selection.
// If the selection changes from a menu item with its submenu
// open to another then close the old selection's submenu.
if (e.NewValue)
{
ItemsControl parentItemsControl = ItemsControl.ItemsControlFromItemContainer(selectionItem);
if (menu == parentItemsControl)
{
menu.RibbonCurrentSelection = selectionItem;
}
}
else
{
// As in MenuItem.OnRibbonIsSelectedChanged, if the item is deselected
// and it's our current selection, set RibbonCurrentSelection to null.
if (menu.RibbonCurrentSelection == selectionItem)
{
menu.RibbonCurrentSelection = null;
}
}
e.Handled = true;
}
}
/// <summary>
/// Currently selected item in this menu or submenu.
///
/// We use the Ribbon prefix to disambiguate this property from MenuBase.CurrentSelection.
/// </summary>
/// <value></value>
internal FrameworkElement RibbonCurrentSelection
{
get
{
return _ribbonCurrentSelection;
}
set
{
if (_ribbonCurrentSelection != value)
{
RibbonMenuItem selectedMenuItem = _ribbonCurrentSelection as RibbonMenuItem;
if (selectedMenuItem != null)
{
selectedMenuItem.RibbonIsSelected = false;
}
else
{
RibbonGallery selectedGallery = _ribbonCurrentSelection as RibbonGallery;
if (selectedGallery != null)
{
selectedGallery.RibbonIsSelected = false;
}
}
_ribbonCurrentSelection = value;
selectedMenuItem = _ribbonCurrentSelection as RibbonMenuItem;
if (selectedMenuItem != null)
{
selectedMenuItem.RibbonIsSelected = true;
}
else
{
RibbonGallery selectedGallery = _ribbonCurrentSelection as RibbonGallery;
if (selectedGallery != null)
{
selectedGallery.RibbonIsSelected = true;
}
}
}
}
}
#endregion
#region UI Automation
protected override AutomationPeer OnCreateAutomationPeer()
{
return new RibbonMenuButtonAutomationPeer(this);
}
#endregion
#region Dropdown Resizing
void OnPopupResizeStarted(object sender, DragStartedEventArgs e)
{
RibbonDropDownHelper.OnPopupResizeStarted(_itemsPresenter);
// Clear selection and close submenus when resizing.
if (RibbonCurrentSelection != null)
{
RibbonMenuItem selectedMenuItem = RibbonCurrentSelection as RibbonMenuItem;
RibbonCurrentSelection = null;
if (selectedMenuItem != null)
{
selectedMenuItem.IsSubmenuOpen = false;
}
}
e.Handled = true;
}