/Silverlight.Navigation.Controls/NavigationToolbar.xaml.cs

# · C# · 335 lines · 190 code · 49 blank · 96 comment · 34 complexity · a2dc36b5961ba66d166d45fb81833dd2 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Shapes;
  12. using Silverlight.Navigation.Favorites;
  13. using Silverlight.Navigation.History;
  14. using Silverlight.Navigation.Mapping;
  15. namespace Silverlight.Navigation.Controls
  16. {
  17. public partial class NavigationToolbar : UserControl
  18. {
  19. #region Events
  20. public event EventHandler<NavigationEventArgs> Go;
  21. public event EventHandler GoBack;
  22. public event EventHandler GoForward;
  23. public event EventHandler ClearHistory;
  24. #endregion
  25. #region Fields
  26. private HistoryManager _historyManager;
  27. #endregion
  28. #region Constructors
  29. /// <summary>
  30. /// Initializes a new instance of the NavigationToolbar class.
  31. /// </summary>
  32. public NavigationToolbar()
  33. {
  34. InitializeComponent();
  35. }
  36. /// <summary>
  37. /// Initializes a new instance of the NavigationToolbar class.
  38. /// </summary>
  39. /// <param name="historyManager">The history manager for the application.</param>
  40. /// <param name="favoritesManager">The favorites manager for the application.</param>
  41. public NavigationToolbar(HistoryManager historyManager, FavoritesManager favoritesManager)
  42. : this()
  43. {
  44. if (historyManager == null)
  45. throw new ArgumentNullException("historyManager");
  46. if (favoritesManager == null)
  47. throw new ArgumentNullException("favoritesManager");
  48. HistoryManager = historyManager;
  49. FavoritesManager = favoritesManager;
  50. }
  51. #endregion
  52. #region Properties
  53. /// <summary>
  54. /// Whether or not the back button is enabled.
  55. /// </summary>
  56. public bool CanGoBack
  57. {
  58. get { return this.BackButton.IsEnabled; }
  59. set { this.BackButton.IsEnabled = value; }
  60. }
  61. /// <summary>
  62. /// Whether or not the forward button is enabled.
  63. /// </summary>
  64. public bool CanGoForward
  65. {
  66. get { return this.ForwardButton.IsEnabled; }
  67. set { this.ForwardButton.IsEnabled = value; }
  68. }
  69. /// <summary>
  70. /// The history manager for the application.
  71. /// </summary>
  72. public HistoryManager HistoryManager
  73. {
  74. get { return _historyManager; }
  75. set
  76. {
  77. _historyManager = value;
  78. this.AddressBox.SelectionChanged -= AddressBox_SelectionChanged;
  79. this.AddressBox.ItemsSource = _historyManager.RecentUris;
  80. this.AddressBox.SelectionChanged += AddressBox_SelectionChanged;
  81. }
  82. }
  83. /// <summary>
  84. /// The favorites manager for the application.
  85. /// </summary>
  86. public FavoritesManager FavoritesManager { get; set; }
  87. /// <summary>
  88. /// Whether or not the toolbar is expanded.
  89. /// </summary>
  90. public bool IsExpanded
  91. {
  92. get { return this.NavExpander.IsExpanded; }
  93. set { this.NavExpander.IsExpanded = value; }
  94. }
  95. /// <summary>
  96. /// The current uri.
  97. /// </summary>
  98. public string CurrentUri
  99. {
  100. get
  101. {
  102. if (this.AddressBox.SelectedItem != null && this.AddressBox.SelectedItem is string)
  103. return (string)this.AddressBox.SelectedItem;
  104. else
  105. return string.Empty;
  106. }
  107. set
  108. {
  109. this.AddressBox.SelectionChanged -= AddressBox_SelectionChanged;
  110. this.AddressBox.SelectedItem = value;
  111. this.AddressBox.SelectionChanged += AddressBox_SelectionChanged;
  112. }
  113. }
  114. #endregion
  115. #region Methods
  116. #endregion
  117. #region Event Handlers
  118. /// <summary>
  119. /// Handles the selection changed event for the address box.
  120. /// </summary>
  121. /// <param name="sender"></param>
  122. /// <param name="e"></param>
  123. private void AddressBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  124. {
  125. if (this.AddressBox.SelectedItem != null && Go != null)
  126. Go(this, new NavigationEventArgs(this.CurrentUri));
  127. }
  128. /// <summary>
  129. /// Handles the click event for the back button.
  130. /// </summary>
  131. /// <param name="sender"></param>
  132. /// <param name="e"></param>
  133. private void BackButton_Click(object sender, RoutedEventArgs e)
  134. {
  135. if (GoBack != null)
  136. GoBack(this, new EventArgs());
  137. }
  138. /// <summary>
  139. /// Handles the click event for the forward button.
  140. /// </summary>
  141. /// <param name="sender"></param>
  142. /// <param name="e"></param>
  143. private void ForwardButton_Click(object sender, RoutedEventArgs e)
  144. {
  145. if (GoForward != null)
  146. GoForward(this, new EventArgs());
  147. }
  148. /// <summary>
  149. /// Handles the click event for the history button.
  150. /// </summary>
  151. /// <param name="sender"></param>
  152. /// <param name="e"></param>
  153. private void HistoryButton_Click(object sender, RoutedEventArgs e)
  154. {
  155. History history = new History();
  156. history.HistoryManager = this.HistoryManager;
  157. history.Title = "History";
  158. history.Closed += new EventHandler(History_Closed);
  159. history.ClearHistory += new EventHandler(History_ClearHistory);
  160. history.Show();
  161. }
  162. /// <summary>
  163. /// Handles the clear history event of the history dialog.
  164. /// </summary>
  165. /// <param name="sender"></param>
  166. /// <param name="e"></param>
  167. private void History_ClearHistory(object sender, EventArgs e)
  168. {
  169. this.HistoryManager.Clear();
  170. if (ClearHistory != null)
  171. ClearHistory(this, new EventArgs());
  172. }
  173. /// <summary>
  174. /// Handles the closed event of the History dialog box.
  175. /// </summary>
  176. /// <param name="sender"></param>
  177. /// <param name="e"></param>
  178. private void History_Closed(object sender, EventArgs e)
  179. {
  180. if (sender is History)
  181. {
  182. History history = (History)sender;
  183. if (history.DialogResult.HasValue && history.DialogResult.Value && !(string.IsNullOrEmpty(history.SelectedUri)))
  184. {
  185. if (this.Go != null)
  186. Go(this, new NavigationEventArgs(history.SelectedUri));
  187. }
  188. }
  189. }
  190. /// <summary>
  191. /// Handles the click event for the favorites button.
  192. /// </summary>
  193. /// <param name="sender"></param>
  194. /// <param name="e"></param>
  195. private void FavoritesButton_Click(object sender, RoutedEventArgs e)
  196. {
  197. Favorites favorites = new Favorites(this.FavoritesManager);
  198. favorites.Closed += new EventHandler(Favorites_Closed);
  199. favorites.Show();
  200. }
  201. /// <summary>
  202. /// Handles the closed event of the favorites control.
  203. /// </summary>
  204. /// <param name="sender"></param>
  205. /// <param name="e"></param>
  206. private void Favorites_Closed(object sender, EventArgs e)
  207. {
  208. if (sender is Favorites)
  209. {
  210. Favorites favorites = (Favorites)sender;
  211. if (favorites.DialogResult.HasValue && favorites.DialogResult.Value && favorites.SelectedFavoriteItem != null)
  212. {
  213. if (this.Go != null)
  214. Go(this, new NavigationEventArgs(favorites.SelectedFavoriteItem.Uri));
  215. }
  216. }
  217. }
  218. /// <summary>
  219. /// Handles the click event for the add to to favorites button.
  220. /// </summary>
  221. /// <param name="sender"></param>
  222. /// <param name="e"></param>
  223. private void AddToFavoritesButton_Click(object sender, RoutedEventArgs e)
  224. {
  225. AddToFavorites addToFavorites = new AddToFavorites(this.CurrentUri);
  226. addToFavorites.Closed += new EventHandler(AddToFavorites_Closed);
  227. addToFavorites.Show();
  228. }
  229. /// <summary>
  230. /// Handles the closed event of the add to favorites control.
  231. /// </summary>
  232. /// <param name="sender"></param>
  233. /// <param name="e"></param>
  234. private void AddToFavorites_Closed(object sender, EventArgs e)
  235. {
  236. if (sender is AddToFavorites)
  237. {
  238. AddToFavorites addToFavorites = (AddToFavorites)sender;
  239. if (addToFavorites.DialogResult.HasValue && addToFavorites.DialogResult.Value &&
  240. addToFavorites.FavoriteItem != null)
  241. this.FavoritesManager.Add(addToFavorites.FavoriteItem);
  242. }
  243. }
  244. /// <summary>
  245. /// Handles the click event for the set as home page button.
  246. /// </summary>
  247. /// <param name="sender"></param>
  248. /// <param name="e"></param>
  249. private void SetAsHomePageButton_Click(object sender, RoutedEventArgs e)
  250. {
  251. this.FavoritesManager.HomePage = new FavoriteItem { Name = "Home Page", Uri = this.CurrentUri };
  252. }
  253. /// <summary>
  254. /// Handles the click event for the use default home page button.
  255. /// </summary>
  256. /// <param name="sender"></param>
  257. /// <param name="e"></param>
  258. private void UseDefaultHomePage_Click(object sender, RoutedEventArgs e)
  259. {
  260. this.FavoritesManager.HomePage = null;
  261. }
  262. #endregion
  263. }
  264. /// <summary>
  265. /// Event args for navigation events.
  266. /// </summary>
  267. public class NavigationEventArgs : EventArgs
  268. {
  269. #region Constructors
  270. /// <summary>
  271. /// Instantiates a new instance of the NavigationEventArgs class.
  272. /// </summary>
  273. /// <param name="uri"></param>
  274. public NavigationEventArgs(string uri)
  275. {
  276. Uri = uri;
  277. }
  278. #endregion
  279. #region Properties
  280. /// <summary>
  281. /// The uri to navigate to.
  282. /// </summary>
  283. public string Uri { get; set; }
  284. #endregion
  285. }
  286. }