-
Notifications
You must be signed in to change notification settings - Fork 52
/
KryptonColorButton.cs
1546 lines (1343 loc) · 58.4 KB
/
KryptonColorButton.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 BSD License
/*
*
* Original BSD 3-Clause License (https://github.com/ComponentFactory/Krypton/blob/master/LICENSE)
* © Component Factory Pty Ltd, 2006 - 2016, (Version 4.5.0.0) All rights reserved.
*
* New BSD 3-Clause License (https://github.com/Krypton-Suite/Standard-Toolkit/blob/master/LICENSE)
* Modifications by Peter Wagner(aka Wagnerp) & Simon Coghlan(aka Smurf-IV), et al. 2017 - 2023. All rights reserved.
*
*/
#endregion
using System.Drawing;
namespace Krypton.Toolkit
{
/// <summary>
/// Combines color button functionality with the styling features of the Krypton Toolkit.
/// </summary>
[ToolboxItem(true)]
[ToolboxBitmap(typeof(KryptonColorButton), "ToolboxBitmaps.KryptonColorButton.bmp")]
[DefaultEvent(@"SelectedColorChanged")]
[DefaultProperty(@"SelectedColor")]
[Designer(@"Krypton.Toolkit.KryptonColorButtonDesigner, Krypton.Toolkit")]
[DesignerCategory(@"code")]
[Description(@"Raises an event when the user clicks it.")]
public class KryptonColorButton : VisualSimpleBase, IButtonControl, IContentValues
{
#region Instance Fields
private readonly ViewDrawButton _drawButton;
private ButtonStyle _style;
private readonly ButtonController _buttonController;
private readonly PaletteRedirectDropDownButton _paletteDropDownButtonImages;
private readonly PaletteTripleOverride _overrideFocus;
private readonly PaletteTripleOverride _overrideNormal;
private readonly PaletteTripleOverride _overrideTracking;
private readonly PaletteTripleOverride _overridePressed;
private KryptonCommand _command;
private Rectangle _selectedRect;
private Color _selectedColor;
private Color _emptyBorderColor;
private readonly List<Color> _recentColors;
private Image _wasImage;
private bool _wasEnabled;
private bool _isDefault;
private bool _useMnemonic;
private bool _allowFullOpen;
private bool _clickOverriden;
private KryptonColorButtonCustomColorPreviewShape _customColorPreviewShape;
// Context menu items
private readonly KryptonContextMenu _kryptonContextMenu;
private readonly KryptonContextMenuSeparator _separatorTheme;
private readonly KryptonContextMenuSeparator _separatorStandard;
private readonly KryptonContextMenuSeparator _separatorRecent;
private readonly KryptonContextMenuHeading _headingTheme;
private readonly KryptonContextMenuHeading _headingStandard;
private readonly KryptonContextMenuHeading _headingRecent;
private readonly KryptonContextMenuColorColumns _colorsTheme;
private readonly KryptonContextMenuColorColumns _colorsStandard;
private readonly KryptonContextMenuColorColumns _colorsRecent;
private readonly KryptonContextMenuSeparator _separatorNoColor;
private readonly KryptonContextMenuItems _itemsNoColor;
private readonly KryptonContextMenuItem _itemNoColor;
private readonly KryptonContextMenuSeparator _separatorMoreColors;
private readonly KryptonContextMenuItems _itemsMoreColors;
private readonly KryptonContextMenuItem _itemMoreColors;
#endregion
#region Events
/// <summary>
/// Occurs when the drop down portion of the color button is pressed.
/// </summary>
[Category(@"Action")]
[Description(@"Occurs when the drop down portion of the color button is pressed.")]
public event EventHandler<ContextPositionMenuArgs> DropDown;
/// <summary>
/// Occurs when the value of the KryptonCommand property changes.
/// </summary>
[Category(@"Property Changed")]
[Description(@"Occurs when the value of the KryptonCommand property changes.")]
public event EventHandler KryptonCommandChanged;
/// <summary>
/// Occurs when the SelectedColor property changes value.
/// </summary>
[Category(@"Property Changed")]
[Description(@"Occurs when the SelectedColor property changes value.")]
public event EventHandler<ColorEventArgs> SelectedColorChanged;
/// <summary>
/// Occurs when the user is tracking over a color.
/// </summary>
[Category(@"Action")]
[Description(@"Occurs when user is tracking over a color.")]
public event EventHandler<ColorEventArgs> TrackingColor;
/// <summary>
/// Occurs when the user selects the more colors option.
/// </summary>
[Category(@"Action")]
[Description(@"Occurs when user selects the more colors option.")]
public event CancelEventHandler MoreColors;
#endregion
#region Identity
/// <summary>
/// Initialize a new instance of the KryptonColorButton class.
/// </summary>
public KryptonColorButton()
{
// We generate click events manually, suppress default
// production of them by the base Control class
SetStyle(ControlStyles.StandardClick |
ControlStyles.StandardDoubleClick, false);
// Set default color button properties
_style = ButtonStyle.Standalone;
VisibleThemes = true;
VisibleStandard = true;
VisibleRecent = true;
VisibleNoColor = true;
VisibleMoreColors = true;
AutoRecentColors = true;
SchemeThemes = ColorScheme.OfficeThemes;
SchemeStandard = ColorScheme.OfficeStandard;
_selectedRect = new Rectangle(0, 12, 16, 4);
_selectedColor = Color.Red;
_emptyBorderColor = Color.DarkGray;
DialogResult = DialogResult.None;
_useMnemonic = true;
MaxRecentColors = 10;
_recentColors = new List<Color>();
_allowFullOpen = true;
// Create the context menu items
_kryptonContextMenu = new KryptonContextMenu();
_separatorTheme = new KryptonContextMenuSeparator();
_headingTheme = new KryptonContextMenuHeading(@"Theme Colors");
_colorsTheme = new KryptonContextMenuColorColumns(ColorScheme.OfficeThemes);
_separatorStandard = new KryptonContextMenuSeparator();
_headingStandard = new KryptonContextMenuHeading(@"Standard Colors");
_colorsStandard = new KryptonContextMenuColorColumns(ColorScheme.OfficeStandard);
_separatorRecent = new KryptonContextMenuSeparator();
_headingRecent = new KryptonContextMenuHeading(@"Recent Colors");
_colorsRecent = new KryptonContextMenuColorColumns(ColorScheme.None);
_separatorNoColor = new KryptonContextMenuSeparator();
_itemNoColor = new KryptonContextMenuItem(@"&No Color", Resources.GenericImageResources.ButtonNoColor, OnClickNoColor);
_itemsNoColor = new KryptonContextMenuItems();
_itemsNoColor.Items.Add(_itemNoColor);
_separatorMoreColors = new KryptonContextMenuSeparator();
_itemMoreColors = new KryptonContextMenuItem(@"&More Colors...", OnClickMoreColors);
_itemsMoreColors = new KryptonContextMenuItems();
_itemsMoreColors.Items.Add(_itemMoreColors);
_kryptonContextMenu.Items.AddRange(new KryptonContextMenuItemBase[] { _separatorTheme, _headingTheme, _colorsTheme,
_separatorStandard, _headingStandard, _colorsStandard,
_separatorRecent, _headingRecent, _colorsRecent,
_separatorNoColor, _itemsNoColor,
_separatorMoreColors, _itemsMoreColors});
// Create content storage
Values = CreateButtonValues(NeedPaintDelegate);
Values.TextChanged += OnButtonTextChanged;
Images = new DropDownButtonImages(NeedPaintDelegate);
// Image need an extra redirector to check the local images first
_paletteDropDownButtonImages = new PaletteRedirectDropDownButton(Redirector, Images);
// Create the palette storage
Strings = new PaletteColorButtonStrings();
StateCommon = new PaletteTripleRedirect(Redirector, PaletteBackStyle.ButtonStandalone, PaletteBorderStyle.ButtonStandalone, PaletteContentStyle.ButtonStandalone, NeedPaintDelegate);
StateDisabled = new PaletteTriple(StateCommon, NeedPaintDelegate);
StateNormal = new PaletteTriple(StateCommon, NeedPaintDelegate);
StateTracking = new PaletteTriple(StateCommon, NeedPaintDelegate);
StatePressed = new PaletteTriple(StateCommon, NeedPaintDelegate);
OverrideDefault = new PaletteTripleRedirect(Redirector, PaletteBackStyle.ButtonStandalone, PaletteBorderStyle.ButtonStandalone, PaletteContentStyle.ButtonStandalone, NeedPaintDelegate);
OverrideFocus = new PaletteTripleRedirect(Redirector, PaletteBackStyle.ButtonStandalone, PaletteBorderStyle.ButtonStandalone, PaletteContentStyle.ButtonStandalone, NeedPaintDelegate);
// Create the override handling classes
_overrideFocus = new PaletteTripleOverride(OverrideFocus, StateNormal, PaletteState.FocusOverride);
_overrideNormal = new PaletteTripleOverride(OverrideDefault, _overrideFocus, PaletteState.NormalDefaultOverride);
_overrideTracking = new PaletteTripleOverride(OverrideFocus, StateTracking, PaletteState.FocusOverride);
_overridePressed = new PaletteTripleOverride(OverrideFocus, StatePressed, PaletteState.FocusOverride);
// Create the view color button instance
_drawButton = new ViewDrawButton(StateDisabled,
_overrideNormal,
_overrideTracking,
_overridePressed,
new PaletteMetricRedirect(Redirector),
this,
VisualOrientation.Top,
UseMnemonic)
{
// Set default color button state
DropDown = true,
Splitter = true,
TestForFocusCues = true,
DropDownPalette = _paletteDropDownButtonImages
};
// Create a color button controller to handle button style behaviour
_buttonController = new ButtonController(_drawButton, NeedPaintDelegate)
{
BecomesFixed = true
};
// Assign the controller to the view element to treat as a button
_drawButton.MouseController = _buttonController;
_drawButton.KeyController = _buttonController;
_drawButton.SourceController = _buttonController;
// Need to know when user clicks the button view or mouse selects it
_buttonController.Click += OnButtonClick;
_buttonController.MouseSelect += OnButtonSelect;
// Create the view manager instance
ViewManager = new ViewManager(this, _drawButton);
CustomColorPreviewShape = KryptonColorButtonCustomColorPreviewShape.None;
}
#endregion
#region Public
/// <summary>
/// Gets and sets the automatic resize of the control to fit contents.
/// </summary>
[Browsable(true)]
[Localizable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[RefreshProperties(RefreshProperties.All)]
public override bool AutoSize
{
get => base.AutoSize;
set => base.AutoSize = value;
}
/// <summary>
/// Gets and sets the internal padding space.
/// </summary>
[Browsable(false)]
[Localizable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new Padding Padding
{
get => base.Padding;
set => base.Padding = value;
}
/// <summary>
/// Allows the developer to add their action
/// </summary>
[Category(@"Action")]
[Description(@"Override to allow your click event")]
public new event EventHandler Click
{
add
{
_clickOverriden = true;
_drawButton.DropDown = false;
Splitter = false;
base.Click += value;
}
remove
{
_clickOverriden = false;
base.Click -= value;
}
}
/// <summary>
/// Gets or sets the text associated with this control.
/// </summary>
[Editor(@"System.ComponentModel.Design.MultilineStringEditor", typeof(UITypeEditor))]
public override string Text
{
get => Values.Text;
set => Values.Text = value;
}
private bool ShouldSerializeText() =>
// Never serialize, let the color button values serialize instead
false;
/// <summary>
/// Resets the Text property to its default value.
/// </summary>
public override void ResetText() =>
// Map onto the color button property from the values
Values.ResetText();
/// <summary>
/// Gets or sets the ContextMenuStrip associated with this control.
/// </summary>
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public override ContextMenuStrip ContextMenuStrip
{
get => null;
set { }
}
/// <summary>
/// Gets and sets the KryptonContextMenu to show when right clicked.
/// </summary>
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public override KryptonContextMenu KryptonContextMenu
{
get => null;
set { }
}
/// <summary>
/// Gets and sets the maximum number of recent colors to store and display.
/// </summary>
[Category(@"Behavior")]
[Description(@"Determine the maximum number of recent colors to store and display.")]
[DefaultValue(10)]
public int MaxRecentColors { get; set; }
/// <summary>
/// Gets and sets the visible state of the themes color set.
/// </summary>
[Category(@"Behavior")]
[Description(@"Determine the visible state of the themes color set.")]
[DefaultValue(true)]
public bool VisibleThemes { get; set; }
/// <summary>
/// Gets and sets the visible state of the standard color set.
/// </summary>
[Category(@"Behavior")]
[Description(@"Determine the visible state of the standard color set.")]
[DefaultValue(true)]
public bool VisibleStandard { get; set; }
/// <summary>
/// Gets and sets the visible state of the recent color set.
/// </summary>
[Category(@"Behavior")]
[Description(@"Determine the visible state of the recent color set.")]
[DefaultValue(true)]
public bool VisibleRecent { get; set; }
/// <summary>
/// Gets and sets the visible state of the no color menu item.
/// </summary>
[Category(@"Behavior")]
[Description(@"Determine if the 'No Color' menu item is used.")]
[DefaultValue(true)]
public bool VisibleNoColor { get; set; }
/// <summary>
/// Gets and sets the visible state of the more colors menu item.
/// </summary>
[Category(@"Behavior")]
[Description(@"Determine if the 'More Colors...' menu item is used.")]
[DefaultValue(true)]
public bool VisibleMoreColors { get; set; }
/// <summary>
/// Gets and sets if the recent colors should be automatically updated.
/// </summary>
[Category(@"Behavior")]
[Description(@"Should recent colors be automatically updated.")]
[DefaultValue(true)]
public bool AutoRecentColors { get; set; }
/// <summary>
/// Gets and sets the color scheme for the themes color set.
/// </summary>
[Category(@"Behavior")]
[Description(@"Color scheme to use for the themes color set.")]
[DefaultValue(typeof(ColorScheme), "OfficeThemes")]
public ColorScheme SchemeThemes { get; set; }
/// <summary>
/// Gets and sets the color scheme for the standard color set.
/// </summary>
[Category(@"Behavior")]
[Description(@"Color scheme to use for the standard color set.")]
[DefaultValue(typeof(ColorScheme), "OfficeStandard")]
public ColorScheme SchemeStandard { get; set; }
/// <summary>
/// Gets and sets the selected color.
/// </summary>
[Bindable(true)]
[Category(@"Appearance")]
[Description(@"Selected color.")]
[DefaultValue(typeof(Color), "Red")]
public Color SelectedColor
{
get => _selectedColor;
set
{
if (value != _selectedColor)
{
_selectedColor = value;
Values.SelectedColor = value;
UpdateRecentColors(_selectedColor);
OnSelectedColorChanged(_selectedColor);
PerformNeedPaint(true);
}
}
}
/// <summary>
/// Gets and sets the selected color block when selected color is empty.
/// </summary>
[Bindable(true)]
[Category(@"Appearance")]
[Description(@"Border color of selected block when selected color is empty.")]
[DefaultValue(typeof(Color), "DarkGray")]
public Color EmptyBorderColor
{
get => _emptyBorderColor;
set
{
if (value != _emptyBorderColor)
{
_emptyBorderColor = value;
Values.EmptyBorderColor = value;
PerformNeedPaint(true);
}
}
}
/// <summary>
/// Gets and sets the selected color drawing rectangle.
/// </summary>
[Bindable(true)]
[Category(@"Appearance")]
[Description(@"Selected color drawing rectangle.")]
[DefaultValue(typeof(Rectangle), "0,12,16,4")]
public Rectangle SelectedRect
{
get => _selectedRect;
set
{
_selectedRect = value;
Values.SelectedRect = value;
PerformNeedPaint(true);
}
}
/// <summary>
/// Gets and sets the visual orientation of the control.
/// </summary>
[Category(@"Visuals")]
[Description(@"Visual orientation of the control.")]
[DefaultValue(typeof(VisualOrientation), "Top")]
public virtual VisualOrientation ButtonOrientation
{
get => _drawButton.Orientation;
set
{
if (_drawButton.Orientation != value)
{
_drawButton.Orientation = value;
PerformNeedPaint(true);
}
}
}
/// <summary>
/// Gets and sets the position of the drop arrow within the color button.
/// </summary>
[Category(@"Visuals")]
[Description(@"Position of the drop arrow within the color button.")]
[DefaultValue(typeof(VisualOrientation), "Right")]
public virtual VisualOrientation DropDownPosition
{
get => _drawButton.DropDownPosition;
set
{
if (_drawButton.DropDownPosition != value)
{
_drawButton.DropDownPosition = value;
PerformNeedPaint(true);
}
}
}
/// <summary>
/// Gets and sets the orientation of the drop arrow within the color button.
/// </summary>
[Category(@"Visuals")]
[Description(@"Orientation of the drop arrow within the color button.")]
[DefaultValue(typeof(VisualOrientation), "Bottom")]
public virtual VisualOrientation DropDownOrientation
{
get
{
return _drawButton.DropDownOrientation switch
{
VisualOrientation.Top => VisualOrientation.Bottom,
VisualOrientation.Bottom => VisualOrientation.Top,
VisualOrientation.Left => VisualOrientation.Right,
VisualOrientation.Right => VisualOrientation.Left,
_ => VisualOrientation.Bottom
};
}
set
{
VisualOrientation converted = value switch
{
VisualOrientation.Bottom => VisualOrientation.Top,
VisualOrientation.Top => VisualOrientation.Bottom,
VisualOrientation.Right => VisualOrientation.Left,
VisualOrientation.Left => VisualOrientation.Right,
_ => VisualOrientation.Top
};
if (_drawButton.DropDownOrientation != converted)
{
_drawButton.DropDownOrientation = converted;
PerformNeedPaint(true);
}
}
}
/// <summary>
/// Gets and sets if the color button works as a splitter or as a drop down.
/// </summary>
[Category(@"Visuals")]
[Description(@"Determine if color button acts as a splitter or just a drop down.")]
[DefaultValue(true)]
public virtual bool Splitter
{
get => _drawButton.Splitter;
set
{
if (_drawButton.Splitter != value)
{
_drawButton.Splitter = value;
PerformNeedPaint(true);
}
}
}
/// <summary>
/// Gets and sets the color button style.
/// </summary>
[Category(@"Visuals")]
[Description(@"Color button style.")]
public ButtonStyle ButtonStyle
{
get => _style;
set
{
if (_style != value)
{
_style = value;
SetStyles(_style);
PerformNeedPaint(true);
}
}
}
private bool ShouldSerializeButtonStyle() => ButtonStyle != ButtonStyle.Standalone;
private void ResetButtonStyle()
{
ButtonStyle = ButtonStyle.Standalone;
}
/// <summary>
/// Gets access to the color button content.
/// </summary>
[Category(@"Visuals")]
[Description(@"Color button values")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ColorButtonValues Values { get; }
private bool ShouldSerializeValues() => !Values.IsDefault;
/// <summary>
/// Gets access to the image value overrides.
/// </summary>
[Category(@"Visuals")]
[Description(@"Image value overrides.")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public DropDownButtonImages Images { get; }
private bool ShouldSerializeImages() => !Images.IsDefault;
/// <summary>
/// Gets access to the context menu display strings.
/// </summary>
[Category(@"Visuals")]
[Description(@"Context menu display strings.")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public PaletteColorButtonStrings Strings { get; }
/// <summary>
/// Gets access to the common color button appearance that other states can override.
/// </summary>
[Category(@"Visuals")]
[Description(@"Overrides for defining common color button appearance that other states can override.")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public PaletteTripleRedirect StateCommon { get; }
private bool ShouldSerializeStateCommon() => !StateCommon.IsDefault;
/// <summary>
/// Gets access to the disabled color button appearance entries.
/// </summary>
[Category(@"Visuals")]
[Description(@"Overrides for defining disabled color button appearance.")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public PaletteTriple StateDisabled { get; }
private bool ShouldSerializeStateDisabled() => !StateDisabled.IsDefault;
/// <summary>
/// Gets access to the normal color button appearance entries.
/// </summary>
[Category(@"Visuals")]
[Description(@"Overrides for defining normal color button appearance.")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public PaletteTriple StateNormal { get; }
private bool ShouldSerializeStateNormal() => !StateNormal.IsDefault;
/// <summary>
/// Gets access to the hot tracking color button appearance entries.
/// </summary>
[Category(@"Visuals")]
[Description(@"Overrides for defining hot tracking color button appearance.")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public PaletteTriple StateTracking { get; }
private bool ShouldSerializeStateTracking() => !StateTracking.IsDefault;
/// <summary>
/// Gets access to the pressed color button appearance entries.
/// </summary>
[Category(@"Visuals")]
[Description(@"Overrides for defining pressed color button appearance.")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public PaletteTriple StatePressed { get; }
private bool ShouldSerializeStatePressed() => !StatePressed.IsDefault;
/// <summary>
/// Gets access to the normal color button appearance when default.
/// </summary>
[Category(@"Visuals")]
[Description(@"Overrides for defining normal color button appearance when default.")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public PaletteTripleRedirect OverrideDefault { get; }
private bool ShouldSerializeOverrideDefault() => !OverrideDefault.IsDefault;
/// <summary>
/// Gets access to the color button appearance when it has focus.
/// </summary>
[Category(@"Visuals")]
[Description(@"Overrides for defining color button appearance when it has focus.")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public PaletteTripleRedirect OverrideFocus { get; }
private bool ShouldSerializeOverrideFocus() => !OverrideFocus.IsDefault;
/// <summary>
/// Gets or sets the value returned to the parent form when the color button is clicked.
/// </summary>
[Category(@"Behavior")]
[Description(@"The dialog-box result produced in a modal form by clicking the color button.")]
[DefaultValue(typeof(DialogResult), "None")]
public DialogResult DialogResult { get; set; }
/// <summary>
/// Gets and sets the associated KryptonCommand.
/// </summary>
[Category(@"Behavior")]
[Description(@"Command associated with the color button.")]
[DefaultValue(null)]
public virtual KryptonCommand KryptonCommand
{
get => _command;
set
{
if (_command != value)
{
if (_command != null)
{
_command.PropertyChanged -= OnCommandPropertyChanged;
}
else
{
_wasEnabled = Enabled;
_wasImage = Values.Image;
}
_command = value;
OnKryptonCommandChanged(EventArgs.Empty);
if (_command != null)
{
_command.PropertyChanged += OnCommandPropertyChanged;
}
else
{
Enabled = _wasEnabled;
Values.Image = _wasImage;
}
}
}
}
/// <summary>
/// Notifies a control that it is the default color button so that its appearance and behavior is adjusted accordingly.
/// </summary>
/// <param name="value">true if the control should behave as a default color button; otherwise false.</param>
public void NotifyDefault(bool value)
{
if (!ViewDrawButton.IsFixed && (_isDefault != value))
{
// Remember new default status
_isDefault = value;
// Decide if the default overrides should be applied
_overrideNormal.Apply = value;
// Change in deault state requires a layout and repaint
PerformNeedPaint(true);
}
}
/// <summary>
/// Generates a Click event for the control.
/// </summary>
public void PerformClick()
{
if (CanSelect)
{
OnClick(EventArgs.Empty);
}
}
/// <summary>
/// Generates a DropDown event for the control.
/// </summary>
public void PerformDropDown()
{
if (CanSelect)
{
ShowDropDown();
}
}
/// <summary>
/// Gets or sets a value indicating whether an ampersand is included in the text of the control.
/// </summary>
[Category(@"Appearance")]
[Description(@"When true the first character after an ampersand will be used as a mnemonic.")]
[DefaultValue(true)]
public bool UseMnemonic
{
get => _useMnemonic;
set
{
if (_useMnemonic != value)
{
_useMnemonic = value;
_drawButton.UseMnemonic = value;
PerformNeedPaint(true);
}
}
}
/// <summary>
/// Fix the control to a particular palette state.
/// </summary>
/// <param name="state">Palette state to fix.</param>
public virtual void SetFixedState(PaletteState state)
{
if (state == PaletteState.NormalDefaultOverride)
{
// Setup the overrides correctly to match state
_overrideFocus.Apply = true;
_overrideNormal.Apply = true;
// Must pass a proper drawing state to the view
state = PaletteState.Normal;
}
// Request fixed state from the view
_drawButton.FixedState = state;
}
/// <summary>
/// Determines the IME status of the object when selected.
/// </summary>
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public new ImeMode ImeMode
{
get => base.ImeMode;
set => base.ImeMode = value;
}
/// <summary>Full color dialog.</summary>
[DefaultValue(true), Description(@"Full color dialog.")]
public bool AllowFullOpen
{
get => _allowFullOpen;
set => _allowFullOpen = value;
}
/// <summary>
/// Allows the configuration of a custom colour preview shape.
/// </summary>
[DefaultValue(typeof(KryptonColorButtonCustomColorPreviewShape), "KryptonColorButtonCustomColorPreviewShape.None"), Description(@"Allows the configuration of a custom colour preview shape.")]
public KryptonColorButtonCustomColorPreviewShape CustomColorPreviewShape
{
get => _customColorPreviewShape;
set
{
if (_customColorPreviewShape != value)
{
_customColorPreviewShape = value;
SetCustomColorPreviewShape(value);
PerformNeedPaint(true);
}
}
}
#endregion
#region IContentValues
/// <summary>
/// Gets the content short text.
/// </summary>
/// <returns>String value.</returns>
public string GetShortText() => KryptonCommand?.Text ?? Values.GetShortText();
/// <summary>
/// Gets the content long text.
/// </summary>
/// <returns>String value.</returns>
public string GetLongText() => KryptonCommand?.ExtraText ?? Values.GetLongText();
/// <summary>
/// Gets the content image.
/// </summary>
/// <param name="state">The state for which the image is needed.</param>
/// <returns>Image value.</returns>
public Image GetImage(PaletteState state) => Values.GetImage(state);
/// <summary>
/// Gets the image color that should be transparent.
/// </summary>
/// <param name="state">The state for which the image is needed.</param>
/// <returns>Color value.</returns>
public Color GetImageTransparentColor(PaletteState state) =>
KryptonCommand?.ImageTransparentColor ?? Values.GetImageTransparentColor(state);
#endregion
#region Protected Overrides
/// <summary>
/// Gets the default size of the control.
/// </summary>
protected override Size DefaultSize => new(90, 25);
/// <summary>
/// Gets the default Input Method Editor (IME) mode supported by this control.
/// </summary>
protected override ImeMode DefaultImeMode => ImeMode.Disable;
/// <summary>
/// Raises the EnabledChanged event.
/// </summary>
/// <param name="e">An EventArgs that contains the event data.</param>
protected override void OnEnabledChanged(EventArgs e)
{
// Must inform the color button view which itself tells the embedded elements
_drawButton.Enabled = Enabled;
// Change in enabled state requires a layout and repaint
PerformNeedPaint(true);
// Let base class fire standard event
base.OnEnabledChanged(e);
}
/// <summary>
/// Raises the GotFocus event.
/// </summary>
/// <param name="e">An EventArgs that contains the event data.</param>
protected override void OnGotFocus(EventArgs e)
{
if (!ViewDrawButton.IsFixed)
{
// Apply the focus overrides
_overrideFocus.Apply = true;
_overrideTracking.Apply = true;
_overridePressed.Apply = true;
// Change in focus requires a repaint
PerformNeedPaint(false);
}
// Let base class fire standard event
base.OnGotFocus(e);
}
/// <summary>
/// Raises the LostFocus event.
/// </summary>
/// <param name="e">An EventArgs that contains the event data.</param>
protected override void OnLostFocus(EventArgs e)
{
if (!ViewDrawButton.IsFixed)
{
// Apply the focus overrides
_overrideFocus.Apply = false;
_overrideTracking.Apply = false;
_overridePressed.Apply = false;
// Change in focus requires a repaint
PerformNeedPaint(false);
}
// Let base class fire standard event
base.OnLostFocus(e);
}
/// <summary>
/// Raises the Click event.
/// </summary>
/// <param name="e">An EventArgs that contains the event data.</param>
protected override void OnClick(EventArgs e)
{
// Find the form this color button is on
Form owner = FindForm();
// If we find a valid owner
if (owner != null)
{
// Update owner with our dialog result setting
owner.DialogResult = DialogResult;
}
// Let base class fire standard event
base.OnClick(e);
// If we have an attached command then execute it
KryptonCommand?.PerformExecute();
}
/// <summary>
/// Processes a mnemonic character.
/// </summary>
/// <param name="charCode">The mnemonic character entered.</param>
/// <returns>true if the mnemonic was processed; otherwise, false.</returns>
protected override bool ProcessMnemonic(char charCode)
{
// Are we allowed to process mnemonics?
if (UseMnemonic && CanProcessMnemonic())
{
// Does the color button primary text contain the mnemonic?
if (IsMnemonic(charCode, Values.Text))
{
if (!Splitter)
{
PerformDropDown();
}
else
{
PerformClick();
}
return true;
}