2009-08-26 Chris Toshok <toshok@ximian.com>
[moon.git] / class / WPF.Toolkit / VSM / System / Windows / VisualStateGroup.cs
blob5bad63180f54f17ac3fde315829c907e01c9f5af
1 // -------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All Rights Reserved.
3 // -------------------------------------------------------------------
5 using System;
6 using System.Collections;
7 using System.Collections.Generic;
8 using System.Collections.ObjectModel;
9 using System.Windows;
10 using System.Windows.Controls;
11 using System.Windows.Markup;
12 using System.Windows.Media.Animation;
14 using Mono;
16 namespace System.Windows
18 /// <summary>
19 /// A group of mutually exclusive visual states.
20 /// </summary>
21 [ContentProperty("States")]
22 public class VisualStateGroup : DependencyObject
24 /// <summary>
25 /// The Name of the VisualStateGroup.
26 /// </summary>
27 public string Name
29 get { return _name; }
30 internal set { _name = value; }
33 /// <summary>
34 /// VisualStates in the group.
35 /// </summary>
36 public IList States
38 get
40 if (_states == null)
42 _states = new Collection<VisualState>();
45 return _states;
49 /// <summary>
50 /// Transitions between VisualStates in the group.
51 /// </summary>
52 public IList Transitions
54 get
56 if (_transitions == null)
58 _transitions = new Collection<VisualTransition>();
61 return _transitions;
65 /// <summary>
66 /// VisualState that is currently applied.
67 /// </summary>
68 internal VisualState CurrentState
70 get; set;
73 internal VisualState GetState(string stateName)
75 for (int stateIndex = 0; stateIndex < States.Count; ++stateIndex)
77 VisualState state = (VisualState)States[stateIndex];
78 if (state.Name == stateName)
80 return state;
84 return null;
87 internal Collection<Storyboard> CurrentStoryboards
89 get
91 if (_currentStoryboards == null)
93 _currentStoryboards = new Collection<Storyboard>();
96 return _currentStoryboards;
100 internal void StartNewThenStopOld(FrameworkElement element, params Storyboard[] newStoryboards)
102 // Start the new Storyboards
103 for (int index = 0; index < newStoryboards.Length; ++index)
105 if (newStoryboards[index] == null)
107 continue;
110 element.Resources.Add (newStoryboards [index].native.ToString (), newStoryboards[index]);
111 try {
112 newStoryboards[index].Begin();
113 } catch {
114 // If an exception is thrown calling begin, clear all the SBs out of the tree before propagating
115 for (int i = 0; i <= index; i++)
116 if (newStoryboards [i] != null)
117 element.Resources.Remove (newStoryboards [i].native.ToString ());
119 throw;
121 // Silverlight had an issue where initially, a checked CheckBox would not show the check mark
122 // until the second frame. They chose to do a Seek(0) at this point, which this line
123 // is supposed to mimic. It does not seem to be equivalent, though, and WPF ends up
124 // with some odd animation behavior. I haven't seen the CheckBox issue on WPF, so
125 // commenting this out for now.
126 // newStoryboards[index].SeekAlignedToLastTick(element, TimeSpan.Zero, TimeSeekOrigin.BeginTime);
129 // Stop the old Storyboards
130 for (int index = 0; index < CurrentStoryboards.Count; ++index)
132 if (CurrentStoryboards[index] == null)
134 continue;
137 element.Resources.Remove (CurrentStoryboards [index].native.ToString ());
138 CurrentStoryboards[index].Stop();
141 // Hold on to the running Storyboards
142 CurrentStoryboards.Clear();
143 for (int index = 0; index < newStoryboards.Length; ++index)
145 CurrentStoryboards.Add(newStoryboards[index]);
149 internal void RaiseCurrentStateChanging(FrameworkElement element, VisualState oldState, VisualState newState, Control control)
151 RaiseCurrentStateChanging (new VisualStateChangedEventArgs(oldState, newState, control));
154 internal void RaiseCurrentStateChanged(FrameworkElement element, VisualState oldState, VisualState newState, Control control)
156 RaiseCurrentStateChanged(new VisualStateChangedEventArgs(oldState, newState, control));
159 internal void RaiseCurrentStateChanging (VisualStateChangedEventArgs e)
161 EventHandler<VisualStateChangedEventArgs> h = (EventHandler<VisualStateChangedEventArgs>) EventList [CurrentStateChangingEvent];
162 if (h != null)
163 h (this, e);
166 internal void RaiseCurrentStateChanged (VisualStateChangedEventArgs e)
168 EventHandler<VisualStateChangedEventArgs> h = (EventHandler<VisualStateChangedEventArgs>) EventList [CurrentStateChangedEvent];
169 if (h != null)
170 h (this, e);
173 static object CurrentStateChangingEvent = new object ();
174 static object CurrentStateChangedEvent = new object ();
176 /// <summary>
177 /// Raised when transition begins
178 /// </summary>
179 public event EventHandler<VisualStateChangedEventArgs> CurrentStateChanging {
180 add {
181 RegisterEvent (CurrentStateChangingEvent, "CurrentStateChanging", Events.current_state_changing, value);
183 remove {
184 UnregisterEvent (CurrentStateChangingEvent, "CurrentStateChanging", Events.current_state_changing, value);
188 /// <summary>
189 /// Raised when transition ends and new state storyboard begins.
190 /// </summary>
191 public event EventHandler<VisualStateChangedEventArgs> CurrentStateChanged {
192 add {
193 RegisterEvent (CurrentStateChangedEvent, "CurrentStateChanged", Events.current_state_changed, value);
195 remove {
196 UnregisterEvent (CurrentStateChangedEvent, "CurrentStateChanged", Events.current_state_changed, value);
201 private Collection<Storyboard> _currentStoryboards;
202 private Collection<VisualState> _states;
203 private Collection<VisualTransition> _transitions;
204 private string _name = String.Empty;