~ubuntu-branches/ubuntu/karmic/moon/karmic

« back to all changes in this revision

Viewing changes to class/Microsoft.SilverlightControls/Controls/Extended/Src/Calendar/MonthButton.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-14 12:01:08 UTC
  • Revision ID: james.westby@ubuntu.com-20090214120108-06539vb25vhbd8bn
Tags: upstream-1.0
Import upstream version 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright © Microsoft Corporation. 
 
2
// This source is subject to the Microsoft Source License for Silverlight Controls (March 2008 Release).
 
3
// Please see http://go.microsoft.com/fwlink/?LinkID=111693 for details.
 
4
// All other rights reserved. 
 
5
 
 
6
using System.Diagnostics;
 
7
using System.Windows.Media.Animation; 
 
8
 
 
9
namespace System.Windows.Controlsb1
 
10
 
11
    /// <summary>
 
12
    /// Represents a button control used in Calendar Control, which reacts to the Click event.
 
13
    /// </summary> 
 
14
    [TemplatePart(Name = MONTHBUTTON_elementRootName, Type = typeof(FrameworkElement))]
 
15
    [TemplatePart(Name = MONTHBUTTON_elementFocusVisualName, Type = typeof(UIElement))]
 
16
    [TemplatePart(Name = MONTHBUTTON_stateNormalName, Type = typeof(Storyboard))] 
 
17
    [TemplatePart(Name = MONTHBUTTON_statePressName, Type = typeof(Storyboard))] 
 
18
    [TemplatePart(Name = MONTHBUTTON_statePressSelectedName, Type = typeof(Storyboard))]
 
19
    [TemplatePart(Name = MONTHBUTTON_stateSelectedName, Type = typeof(Storyboard))] 
 
20
    [TemplatePart(Name = MONTHBUTTON_stateMouseOverName, Type = typeof(Storyboard))]
 
21
    [TemplatePart(Name = MONTHBUTTON_MouseOverSelectedName, Type = typeof(Storyboard))]
 
22
 
 
23
    public sealed class MonthButton : CalendarButtonBase
 
24
    {
 
25
        #region Constants 
 
26
 
 
27
        private const string MONTHBUTTON_elementFocusVisualName = "FocusVisualElement";
 
28
        private const string MONTHBUTTON_elementRootName = "RootElement"; 
 
29
        private const string MONTHBUTTON_stateMouseOverName = "MouseOver State";
 
30
        private const string MONTHBUTTON_MouseOverSelectedName = "MouseOver Selected State";
 
31
        private const string MONTHBUTTON_stateNormalName = "Normal State"; 
 
32
        private const string MONTHBUTTON_statePressName = "Pressed State";
 
33
        private const string MONTHBUTTON_statePressSelectedName = "Pressed Selected State";
 
34
        private const string MONTHBUTTON_stateSelectedName = "Normal Selected State"; 
 
35
 
 
36
        #endregion Constants
 
37
 
 
38
        #region Data
 
39
 
 
40
        private UIElement _elementFocusVisual; 
 
41
        private FrameworkElement _elementRoot;
 
42
        private bool _isFocusedOverride;
 
43
        private bool _isSelected; 
 
44
        private Storyboard _stateNormal; 
 
45
        private Storyboard _stateSelected;
 
46
        private Storyboard _stateMouseOver; 
 
47
        private Storyboard _stateMouseOverSelected;
 
48
        private Storyboard _statePressed;
 
49
        private Storyboard _statePressedSelected; 
 
50
 
 
51
        #endregion Data
 
52
 
 
53
        internal MonthButton(): base() 
 
54
        {
 
55
            this.IsTabStop = true; 
 
56
        }
 
57
 
 
58
        #region Internal Properties 
 
59
 
 
60
        internal bool IsFocusedOverride
 
61
        { 
 
62
            get 
 
63
            {
 
64
                return this._isFocusedOverride; 
 
65
            }
 
66
            set
 
67
            { 
 
68
                this._isFocusedOverride = value;
 
69
                ChangeVisualState();
 
70
            } 
 
71
        } 
 
72
 
 
73
        internal bool IsSelected 
 
74
        {
 
75
            get
 
76
            { 
 
77
                return this._isSelected;
 
78
            }
 
79
            set 
 
80
            { 
 
81
                this._isSelected = value;
 
82
                ChangeVisualState(); 
 
83
            }
 
84
        }
 
85
 
 
86
        #endregion Internal Properties
 
87
 
 
88
        #region Protected methods 
 
89
        /// <summary> 
 
90
        /// Invoked whenever application code or an internal process,
 
91
        /// such as a rebuilding layout pass, calls the ApplyTemplate method. 
 
92
        /// </summary>
 
93
        public override void OnApplyTemplate()
 
94
        { 
 
95
            base.OnApplyTemplate();
 
96
 
 
97
            // Get the elements 
 
98
            object root = GetTemplateChild(MONTHBUTTON_elementRootName); 
 
99
            Debug.Assert(typeof(FrameworkElement).IsInstanceOfType(root) || (root == null));
 
100
            _elementRoot = root as FrameworkElement; 
 
101
 
 
102
            object focusVisual = GetTemplateChild(MONTHBUTTON_elementFocusVisualName);
 
103
            Debug.Assert(typeof(UIElement).IsInstanceOfType(focusVisual) || (focusVisual == null)); 
 
104
            _elementFocusVisual = focusVisual as UIElement;
 
105
 
 
106
            // Get the states 
 
107
            if (_elementRoot != null) 
 
108
            {
 
109
                DependencyObject normal = _elementRoot.Resources[MONTHBUTTON_stateNormalName] as DependencyObject; 
 
110
                Debug.Assert(typeof(Storyboard).IsInstanceOfType(normal) || (normal == null));
 
111
                _stateNormal = normal as Storyboard;
 
112
 
 
113
                DependencyObject selected = _elementRoot.Resources[MONTHBUTTON_stateSelectedName] as DependencyObject;
 
114
                Debug.Assert(typeof(Storyboard).IsInstanceOfType(selected) || (selected == null));
 
115
                _stateSelected = selected as Storyboard; 
 
116
 
 
117
                DependencyObject mouseOver = _elementRoot.Resources[MONTHBUTTON_stateMouseOverName] as DependencyObject;
 
118
                Debug.Assert(typeof(Storyboard).IsInstanceOfType(mouseOver) || (mouseOver == null)); 
 
119
                _stateMouseOver = mouseOver as Storyboard;
 
120
 
 
121
                DependencyObject mouseOverSelected = _elementRoot.Resources[MONTHBUTTON_MouseOverSelectedName] as DependencyObject; 
 
122
                Debug.Assert(typeof(Storyboard).IsInstanceOfType(mouseOverSelected) || (mouseOverSelected == null));
 
123
                _stateMouseOverSelected = mouseOverSelected as Storyboard;
 
124
 
 
125
                DependencyObject pressed = _elementRoot.Resources[MONTHBUTTON_statePressName] as DependencyObject; 
 
126
                Debug.Assert(typeof(Storyboard).IsInstanceOfType(pressed) || (pressed == null));
 
127
                _statePressed = pressed as Storyboard; 
 
128
 
 
129
                DependencyObject pressedSelected = _elementRoot.Resources[MONTHBUTTON_statePressSelectedName] as DependencyObject;
 
130
                Debug.Assert(typeof(Storyboard).IsInstanceOfType(pressedSelected) || (pressedSelected == null)); 
 
131
                _statePressedSelected = pressedSelected as Storyboard;
 
132
            }
 
133
 
 
134
            // Sync the logical and visual states of the control 
 
135
            ChangeVisualState();
 
136
        } 
 
137
 
 
138
        #endregion Protected Methods
 
139
 
 
140
        #region Internal Methods
 
141
 
 
142
        internal override void ChangeVisualState() 
 
143
        { 
 
144
            if (IsPressed)
 
145
            { 
 
146
                if (_isSelected)
 
147
                {
 
148
                    ChangeVisualState(_statePressedSelected); 
 
149
                }
 
150
                else
 
151
                { 
 
152
                    ChangeVisualState(_statePressed); 
 
153
                }
 
154
            } 
 
155
            else
 
156
            {
 
157
                if (IsMouseOver) 
 
158
                {
 
159
                    if (_isSelected)
 
160
                    { 
 
161
                        ChangeVisualState(_stateMouseOverSelected); 
 
162
                    }
 
163
                    else 
 
164
                    {
 
165
                        ChangeVisualState(_stateMouseOver);
 
166
                    } 
 
167
                }
 
168
                else
 
169
                { 
 
170
                    if (_isSelected) 
 
171
                    {
 
172
                        ChangeVisualState(_stateSelected); 
 
173
                    }
 
174
                    else
 
175
                    { 
 
176
                        ChangeVisualState(_stateNormal);
 
177
                    }
 
178
                } 
 
179
            } 
 
180
 
 
181
            if (_elementFocusVisual != null) 
 
182
            {
 
183
                _elementFocusVisual.Visibility = (_isFocusedOverride) ?
 
184
                    Visibility.Visible : 
 
185
                    Visibility.Collapsed;
 
186
            }
 
187
        } 
 
188
 
 
189
        #endregion Internal Methods
 
190
    } 
 
191
}