summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrakeWPF/Controls/SplitButton/SplitMenuButton.cs
blob: ce7a57bb94c0507d2c93fd252641e9a66cbbf57a (plain)
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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SplitMenuButton.cs" company="HandBrake Project (http://handbrake.fr)">
//   This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
// </copyright>
// <summary>
//   A Split Menu Button Control
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace HandBrakeWPF.Controls.SplitButton
{
    using System;
    using System.Collections.ObjectModel;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;

    /// <summary>
    ///  A Split Menu Button Control
    /// </summary>
    [TemplatePart(Name = SplitElementName, Type = typeof(UIElement))]
    public class SplitMenuButton : Button
    {
        #region Fields and Constants

        /// <summary>
        /// The split element name.
        /// </summary>
        private const string SplitElementName = "SplitElement";

        /// <summary>
        /// The item source for the context menu
        /// </summary>
        private readonly ObservableCollection<object> itemSource = new ObservableCollection<object>();

        /// <summary>
        /// The is mouse over split element.
        /// </summary>
        private bool IsMouseOverSplitElement;

        /// <summary>
        /// The context menu.
        /// </summary>
        private ContextMenu contextMenu;

        /// <summary>
        /// The context menu initial offset.
        /// </summary>
        private Point contextMenuInitialOffset;

        /// <summary>
        /// The logical child.
        /// </summary>
        private DependencyObject logicalChild;

        /// <summary>
        /// The split element.
        /// </summary>
        private UIElement splitElement;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        ///     Initializes a new instance of the <see cref="SplitMenuButton" /> class.
        /// </summary>
        public SplitMenuButton()
        {
            this.DefaultStyleKey = typeof(SplitMenuButton);
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///  Gets the ItemSource for the Context Menu
        /// </summary>
        public Collection<object> ItemSource
        {
            get
            {
                return this.itemSource;
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///     Called when the template is changed.
        /// </summary>
        public override void OnApplyTemplate()
        {
            // Unhook existing handlers
            if (this.splitElement != null)
            {
                this.splitElement.MouseEnter -= this.SplitElement_MouseEnter;
                this.splitElement.MouseLeave -= this.SplitElement_MouseLeave;
                this.splitElement = null;
            }
            if (this.contextMenu != null)
            {
                this.contextMenu.Opened -= this.ContextMenu_Opened;
                this.contextMenu.Closed -= this.ContextMenu_Closed;
                this.contextMenu = null;
            }

            if (this.logicalChild != null)
            {
                this.RemoveLogicalChild(this.logicalChild);
                this.logicalChild = null;
            }

            // Apply new template
            base.OnApplyTemplate();

            // Hook new event handlers
            this.splitElement = this.GetTemplateChild(SplitElementName) as UIElement;
            if (this.splitElement != null)
            {
                this.splitElement.MouseEnter += this.SplitElement_MouseEnter;
                this.splitElement.MouseLeave += this.SplitElement_MouseLeave;

                this.contextMenu = ContextMenuService.GetContextMenu(this.splitElement);
                if (this.contextMenu != null)
                {
                    // Add the ContextMenu as a logical child (for DataContext and RoutedCommands)
                    this.contextMenu.Visibility = Visibility.Collapsed;
                    this.contextMenu.IsOpen = true;
                    DependencyObject current = this.contextMenu;
                    do
                    {
                        this.logicalChild = current;
                        current = LogicalTreeHelper.GetParent(current);
                    }
                    while (current != null);

                    this.contextMenu.IsOpen = false;     
                    this.AddLogicalChild(this.logicalChild);

                    this.contextMenu.Opened += this.ContextMenu_Opened;
                    this.contextMenu.Closed += this.ContextMenu_Closed;              
                }
            }
        }

        #endregion

        #region Methods

        /// <summary>
        ///     Called when the Button is clicked.
        /// </summary>
        protected override void OnClick()
        {
            if (this.IsMouseOverSplitElement)
            {
                this.OpenButtonMenu();
            }
            else
            {
                base.OnClick();
            }
        }

        /// <summary>
        /// Called when a key is pressed.
        /// </summary>
        /// <param name="e">
        /// The e.
        /// </param>
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }

            if (Key.Down == e.Key || Key.Up == e.Key)
            {
                this.Dispatcher.BeginInvoke((Action)this.OpenButtonMenu);
            }
            else
            {
                base.OnKeyDown(e);
            }
        }

        /// <summary>
        /// The open button menu.
        /// </summary>
        protected void OpenButtonMenu()
        {
            if ((this.ItemSource.Count < 0) && (this.contextMenu != null))
            {
                this.contextMenu.HorizontalOffset = 0;
                this.contextMenu.VerticalOffset = 0;
                this.contextMenu.Visibility = Visibility.Visible;
                this.contextMenu.IsOpen = true;
            }
        }

        /// <summary>
        /// The context menu closed.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The RoutedEventArgs.
        /// </param>
        private void ContextMenu_Closed(object sender, RoutedEventArgs e)
        {
            this.LayoutUpdated -= this.SplitButton_LayoutUpdated;
            this.Focus();
        }

        /// <summary>
        /// The context menu opened.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The RoutedEventArgs.
        /// </param>
        private void ContextMenu_Opened(object sender, RoutedEventArgs e)
        {
            this.contextMenuInitialOffset = this.TranslatePoint(new Point(0, this.ActualHeight), this.contextMenu);
            this.UpdateContextMenuOffsets();
            this.LayoutUpdated += this.SplitButton_LayoutUpdated;
        }

        /// <summary>
        /// The split button layout updated.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The EventArgs.
        /// </param>
        private void SplitButton_LayoutUpdated(object sender, EventArgs e)
        {
            this.UpdateContextMenuOffsets();
        }

        /// <summary>
        /// The split element_ mouse enter.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The MouseEventArgs.
        /// </param>
        private void SplitElement_MouseEnter(object sender, MouseEventArgs e)
        {
            this.IsMouseOverSplitElement = true;
        }

        /// <summary>
        /// The split element mouse leave.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The MouseEventArgs
        /// </param>
        private void SplitElement_MouseLeave(object sender, MouseEventArgs e)
        {
            this.IsMouseOverSplitElement = false;
        }

        /// <summary>
        /// The update context menu offsets.
        /// </summary>
        private void UpdateContextMenuOffsets()
        {
            var currentOffset = new Point();
            Point desiredOffset = this.contextMenuInitialOffset;

            this.contextMenu.HorizontalOffset = desiredOffset.X - currentOffset.X;
            this.contextMenu.VerticalOffset = desiredOffset.Y - currentOffset.Y;
            if (FlowDirection.RightToLeft == this.FlowDirection)
            {
                this.contextMenu.HorizontalOffset *= -1;
            }
        }

        #endregion
    }
}