Menu.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. //
  2. // System.Web.UI.WebControls.Menu.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2004 Novell, Inc (http://www.novell.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.Collections;
  32. using System.Text;
  33. using System.ComponentModel;
  34. using System.Web.UI;
  35. using System.Web.Handlers;
  36. using System.Collections.Specialized;
  37. using System.IO;
  38. namespace System.Web.UI.WebControls
  39. {
  40. public class Menu : HierarchicalDataBoundControl, IPostBackEventHandler, INamingContainer
  41. {
  42. MenuItemCollection items;
  43. MenuItemBindingCollection dataBindings;
  44. MenuItem selectedItem;
  45. Hashtable bindings;
  46. [PersistenceMode (PersistenceMode.InnerProperty)]
  47. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  48. [Editor ("System.Web.UI.Design.MenuItemBindingsEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  49. public virtual MenuItemBindingCollection DataBindings {
  50. get {
  51. if (dataBindings == null) {
  52. dataBindings = new MenuItemBindingCollection ();
  53. if (IsTrackingViewState)
  54. ((IStateManager)dataBindings).TrackViewState();
  55. }
  56. return dataBindings;
  57. }
  58. }
  59. [DefaultValue (500)]
  60. public virtual int DisappearAfter {
  61. get {
  62. object o = ViewState ["DisappearAfter"];
  63. if (o != null) return (int)o;
  64. return 500;
  65. }
  66. set {
  67. ViewState["DisappearAfter"] = value;
  68. }
  69. }
  70. [DefaultValue ("")]
  71. [UrlProperty]
  72. [WebCategory ("Appearance")]
  73. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  74. public virtual string DynamicBottomSeparatorImageUrl {
  75. get {
  76. object o = ViewState ["dbsiu"];
  77. if (o != null) return (string)o;
  78. return "";
  79. }
  80. set {
  81. ViewState["dbsiu"] = value;
  82. }
  83. }
  84. /* [DefaultValue (true)]
  85. public virtual bool DynamicEnableDefaultPopOutImage {
  86. get {
  87. object o = ViewState ["dedpoi"];
  88. if (o != null) return (bool)o;
  89. return true;
  90. }
  91. set {
  92. ViewState["dedpoi"] = value;
  93. }
  94. }
  95. */
  96. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  97. [PersistenceMode (PersistenceMode.InnerProperty)]
  98. [Editor ("System.Web.UI.Design.MenuItemCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  99. public virtual MenuItemCollection Items {
  100. get {
  101. if (items == null) {
  102. items = new MenuItemCollection (this);
  103. if (IsTrackingViewState)
  104. ((IStateManager)items).TrackViewState();
  105. }
  106. return items;
  107. }
  108. }
  109. [DefaultValue ('/')]
  110. public virtual char PathSeparator {
  111. get {
  112. object o = ViewState ["PathSeparator"];
  113. if(o != null) return (char)o;
  114. return '/';
  115. }
  116. set {
  117. ViewState ["PathSeparator"] = value;
  118. }
  119. }
  120. [Browsable (false)]
  121. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  122. public MenuItem SelectedItem {
  123. get { return selectedItem; }
  124. }
  125. internal void SetSelectedItem (MenuItem item)
  126. {
  127. if (selectedItem == item) return;
  128. if (selectedItem != null)
  129. selectedItem.SelectedFlag = false;
  130. selectedItem = item;
  131. selectedItem.SelectedFlag = true;
  132. // OnSelectedItemChanged (new MenuItemEventArgs (selectedItem));
  133. }
  134. public MenuItem FindItem (string valuePath)
  135. {
  136. if (valuePath == null) throw new ArgumentNullException ("valuePath");
  137. string[] path = valuePath.Split (PathSeparator);
  138. int n = 0;
  139. MenuItemCollection col = Items;
  140. bool foundBranch = true;
  141. while (col.Count > 0 && foundBranch) {
  142. foundBranch = false;
  143. foreach (MenuItem item in col) {
  144. if (item.Value == path [n]) {
  145. if (++n == path.Length) return item;
  146. col = item.ChildItems;
  147. foundBranch = true;
  148. break;
  149. }
  150. }
  151. }
  152. return null;
  153. }
  154. string GetBindingKey (string dataMember, int depth)
  155. {
  156. return dataMember + " " + depth;
  157. }
  158. internal MenuItemBinding FindBindingForItem (string type, int depth)
  159. {
  160. if (bindings == null) return null;
  161. MenuItemBinding bin = (MenuItemBinding) bindings [GetBindingKey (type, depth)];
  162. if (bin != null) return bin;
  163. bin = (MenuItemBinding) bindings [GetBindingKey (type, -1)];
  164. if (bin != null) return bin;
  165. bin = (MenuItemBinding) bindings [GetBindingKey ("", depth)];
  166. if (bin != null) return bin;
  167. bin = (MenuItemBinding) bindings [GetBindingKey ("", -1)];
  168. return bin;
  169. }
  170. protected internal override void PerformDataBinding ()
  171. {
  172. base.PerformDataBinding ();
  173. HierarchicalDataSourceView data = GetData ("");
  174. IHierarchicalEnumerable e = data.Select ();
  175. foreach (object obj in e) {
  176. IHierarchyData hdata = e.GetHierarchyData (obj);
  177. MenuItem item = new MenuItem ();
  178. item.Bind (hdata);
  179. Items.Add (item);
  180. }
  181. }
  182. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  183. {
  184. }
  185. protected override void TrackViewState()
  186. {
  187. EnsureDataBound ();
  188. base.TrackViewState();
  189. if (dataBindings != null) {
  190. ((IStateManager)dataBindings).TrackViewState ();
  191. }
  192. if (items != null) {
  193. ((IStateManager)items).TrackViewState();;
  194. }
  195. }
  196. protected override object SaveViewState()
  197. {
  198. object[] states = new object [2];
  199. states[0] = base.SaveViewState();
  200. states[1] = (dataBindings == null ? null : ((IStateManager)dataBindings).SaveViewState());
  201. states[2] = (items == null ? null : ((IStateManager)items).SaveViewState());
  202. for (int i = states.Length - 1; i >= 0; i--) {
  203. if (states [i] != null)
  204. return states;
  205. }
  206. return null;
  207. }
  208. protected override void LoadViewState (object savedState)
  209. {
  210. if (savedState == null)
  211. return;
  212. object [] states = (object []) savedState;
  213. base.LoadViewState (states[0]);
  214. if (states[1] != null)
  215. ((IStateManager)dataBindings).LoadViewState(states[8]);
  216. if (states[2] != null)
  217. ((IStateManager)Items).LoadViewState(states[9]);
  218. }
  219. protected override void RenderContents (HtmlTextWriter writer)
  220. {
  221. writer.WriteLine ("This is a menu");
  222. }
  223. }
  224. }
  225. #endif