Menu.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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. MenuItemStyle dynamicMenuItemStyle;
  43. MenuItemStyle dynamicMenuStyle;
  44. MenuItemStyle dynamicSelectedStyle;
  45. MenuItemStyle staticMenuItemStyle;
  46. MenuItemStyle staticMenuStyle;
  47. MenuItemStyle staticSelectedStyle;
  48. Style staticHoverStyle;
  49. Style dynamicHoverStyle;
  50. MenuItemStyleCollection levelMenuItemStyles;
  51. MenuItemStyleCollection levelSelectedStyles;
  52. MenuItemCollection items;
  53. MenuItemBindingCollection dataBindings;
  54. MenuItem selectedItem;
  55. Hashtable bindings;
  56. private static readonly object MenuItemClickEvent = new object();
  57. public event MenuEventHandler MenuItemClick {
  58. add { Events.AddHandler (MenuItemClickEvent, value); }
  59. remove { Events.RemoveHandler (MenuItemClickEvent, value); }
  60. }
  61. protected virtual void OnMenuItemClick (MenuEventArgs e)
  62. {
  63. if (Events != null) {
  64. MenuEventHandler eh = (MenuEventHandler) Events [MenuItemClickEvent];
  65. if (eh != null) eh (this, e);
  66. }
  67. }
  68. [PersistenceMode (PersistenceMode.InnerProperty)]
  69. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  70. [Editor ("System.Web.UI.Design.MenuItemBindingsEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  71. public virtual MenuItemBindingCollection DataBindings {
  72. get {
  73. if (dataBindings == null) {
  74. dataBindings = new MenuItemBindingCollection ();
  75. if (IsTrackingViewState)
  76. ((IStateManager)dataBindings).TrackViewState();
  77. }
  78. return dataBindings;
  79. }
  80. }
  81. [DefaultValue (500)]
  82. public virtual int DisappearAfter {
  83. get {
  84. object o = ViewState ["DisappearAfter"];
  85. if (o != null) return (int)o;
  86. return 500;
  87. }
  88. set {
  89. ViewState["DisappearAfter"] = value;
  90. }
  91. }
  92. [DefaultValue ("")]
  93. [UrlProperty]
  94. [WebCategory ("Appearance")]
  95. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  96. public virtual string DynamicBottomSeparatorImageUrl {
  97. get {
  98. object o = ViewState ["dbsiu"];
  99. if (o != null) return (string)o;
  100. return "";
  101. }
  102. set {
  103. ViewState["dbsiu"] = value;
  104. }
  105. }
  106. [DefaultValue ("")]
  107. [UrlProperty]
  108. [WebCategory ("Appearance")]
  109. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  110. public virtual string DynamicTopSeparatorImageUrl {
  111. get {
  112. object o = ViewState ["dtsiu"];
  113. if (o != null) return (string)o;
  114. return "";
  115. }
  116. set {
  117. ViewState["dtsiu"] = value;
  118. }
  119. }
  120. [DefaultValue ("")]
  121. [UrlProperty]
  122. [WebCategory ("Appearance")]
  123. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  124. public virtual string StaticBottomSeparatorImageUrl {
  125. get {
  126. object o = ViewState ["sbsiu"];
  127. if (o != null) return (string)o;
  128. return "";
  129. }
  130. set {
  131. ViewState["sbsiu"] = value;
  132. }
  133. }
  134. [DefaultValue ("")]
  135. [UrlProperty]
  136. [WebCategory ("Appearance")]
  137. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  138. public virtual string StaticTopSeparatorImageUrl {
  139. get {
  140. object o = ViewState ["stsiu"];
  141. if (o != null) return (string)o;
  142. return "";
  143. }
  144. set {
  145. ViewState["stsiu"] = value;
  146. }
  147. }
  148. [DefaultValue (Orientation.Vertical)]
  149. public virtual Orientation Orientation {
  150. get {
  151. object o = ViewState ["Orientation"];
  152. if (o != null) return (Orientation) o;
  153. return Orientation.Vertical;
  154. }
  155. set {
  156. ViewState["Orientation"] = value;
  157. }
  158. }
  159. [DefaultValue (1)]
  160. public virtual int StaticDisplayLevels {
  161. get {
  162. object o = ViewState ["StaticDisplayLevels"];
  163. if (o != null) return (int)o;
  164. return 1;
  165. }
  166. set {
  167. if (value < 1) throw new ArgumentOutOfRangeException ();
  168. ViewState["StaticDisplayLevels"] = value;
  169. }
  170. }
  171. [DefaultValue ("16px")]
  172. public Unit StaticSubMenuIndent {
  173. get {
  174. object o = ViewState ["StaticSubMenuIndent"];
  175. if (o != null) return (Unit)o;
  176. return new Unit (16);
  177. }
  178. set {
  179. ViewState["StaticSubMenuIndent"] = value;
  180. }
  181. }
  182. [DefaultValue (3)]
  183. public virtual int MaximumDynamicDisplayLevels {
  184. get {
  185. object o = ViewState ["MaximumDynamicDisplayLevels"];
  186. if (o != null) return (int)o;
  187. return 3;
  188. }
  189. set {
  190. if (value < 0) throw new ArgumentOutOfRangeException ();
  191. ViewState["MaximumDynamicDisplayLevels"] = value;
  192. }
  193. }
  194. [DefaultValue (0)]
  195. public virtual int DynamicVerticalOffset {
  196. get {
  197. object o = ViewState ["DynamicVerticalOffset"];
  198. if (o != null) return (int)o;
  199. return 0;
  200. }
  201. set {
  202. ViewState["DynamicVerticalOffset"] = value;
  203. }
  204. }
  205. [DefaultValue (0)]
  206. public virtual int DynamicHorizontalOffset {
  207. get {
  208. object o = ViewState ["DynamicHorizontalOffset"];
  209. if (o != null) return (int)o;
  210. return 0;
  211. }
  212. set {
  213. ViewState["DynamicHorizontalOffset"] = value;
  214. }
  215. }
  216. [DefaultValue (true)]
  217. public virtual bool DynamicEnableDefaultPopOutImage {
  218. get {
  219. object o = ViewState ["dedpoi"];
  220. if (o != null) return (bool)o;
  221. return true;
  222. }
  223. set {
  224. ViewState["dedpoi"] = value;
  225. }
  226. }
  227. [DefaultValue (true)]
  228. public virtual bool StaticEnableDefaultPopOutImage {
  229. get {
  230. object o = ViewState ["sedpoi"];
  231. if (o != null) return (bool)o;
  232. return true;
  233. }
  234. set {
  235. ViewState["sedpoi"] = value;
  236. }
  237. }
  238. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  239. [PersistenceMode (PersistenceMode.InnerProperty)]
  240. [Editor ("System.Web.UI.Design.MenuItemCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  241. public virtual MenuItemCollection Items {
  242. get {
  243. if (items == null) {
  244. items = new MenuItemCollection (this);
  245. if (IsTrackingViewState)
  246. ((IStateManager)items).TrackViewState();
  247. }
  248. return items;
  249. }
  250. }
  251. [DefaultValue ('/')]
  252. public virtual char PathSeparator {
  253. get {
  254. object o = ViewState ["PathSeparator"];
  255. if(o != null) return (char)o;
  256. return '/';
  257. }
  258. set {
  259. ViewState ["PathSeparator"] = value;
  260. }
  261. }
  262. [DefaultValue (false)]
  263. public virtual bool ItemWrap {
  264. get {
  265. object o = ViewState ["ItemWrap"];
  266. if(o != null) return (bool)o;
  267. return false;
  268. }
  269. set {
  270. ViewState ["ItemWrap"] = value;
  271. }
  272. }
  273. [PersistenceMode (PersistenceMode.InnerProperty)]
  274. [NotifyParentProperty (true)]
  275. [DefaultValue (null)]
  276. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  277. public virtual MenuItemStyle DynamicMenuItemStyle {
  278. get {
  279. if (dynamicMenuItemStyle == null) {
  280. dynamicMenuItemStyle = new MenuItemStyle ();
  281. if (IsTrackingViewState)
  282. dynamicMenuItemStyle.TrackViewState();
  283. }
  284. return dynamicMenuItemStyle;
  285. }
  286. }
  287. [PersistenceMode (PersistenceMode.InnerProperty)]
  288. [NotifyParentProperty (true)]
  289. [DefaultValue (null)]
  290. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  291. public virtual MenuItemStyle DynamicSelectedStyle {
  292. get {
  293. if (dynamicSelectedStyle == null) {
  294. dynamicSelectedStyle = new MenuItemStyle ();
  295. if (IsTrackingViewState)
  296. dynamicSelectedStyle.TrackViewState();
  297. }
  298. return dynamicSelectedStyle;
  299. }
  300. }
  301. [PersistenceMode (PersistenceMode.InnerProperty)]
  302. [NotifyParentProperty (true)]
  303. [DefaultValue (null)]
  304. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  305. public virtual MenuItemStyle DynamicMenuStyle {
  306. get {
  307. if (dynamicMenuStyle == null) {
  308. dynamicMenuStyle = new MenuItemStyle ();
  309. if (IsTrackingViewState)
  310. dynamicMenuStyle.TrackViewState();
  311. }
  312. return dynamicMenuStyle;
  313. }
  314. }
  315. [PersistenceMode (PersistenceMode.InnerProperty)]
  316. [NotifyParentProperty (true)]
  317. [DefaultValue (null)]
  318. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  319. public virtual MenuItemStyle StaticMenuItemStyle {
  320. get {
  321. if (staticMenuItemStyle == null) {
  322. staticMenuItemStyle = new MenuItemStyle ();
  323. if (IsTrackingViewState)
  324. staticMenuItemStyle.TrackViewState();
  325. }
  326. return staticMenuItemStyle;
  327. }
  328. }
  329. [PersistenceMode (PersistenceMode.InnerProperty)]
  330. [NotifyParentProperty (true)]
  331. [DefaultValue (null)]
  332. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  333. public virtual MenuItemStyle StaticSelectedStyle {
  334. get {
  335. if (staticSelectedStyle == null) {
  336. staticSelectedStyle = new MenuItemStyle ();
  337. if (IsTrackingViewState)
  338. staticSelectedStyle.TrackViewState();
  339. }
  340. return staticSelectedStyle;
  341. }
  342. }
  343. [PersistenceMode (PersistenceMode.InnerProperty)]
  344. [NotifyParentProperty (true)]
  345. [DefaultValue (null)]
  346. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  347. public virtual MenuItemStyle StaticMenuStyle {
  348. get {
  349. if (staticMenuStyle == null) {
  350. staticMenuStyle = new MenuItemStyle ();
  351. if (IsTrackingViewState)
  352. staticMenuStyle.TrackViewState();
  353. }
  354. return staticMenuStyle;
  355. }
  356. }
  357. [PersistenceMode (PersistenceMode.InnerProperty)]
  358. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  359. public virtual MenuItemStyleCollection LevelMenuItemStyles {
  360. get {
  361. if (levelMenuItemStyles == null) {
  362. levelMenuItemStyles = new MenuItemStyleCollection ();
  363. if (IsTrackingViewState)
  364. ((IStateManager)levelMenuItemStyles).TrackViewState();
  365. }
  366. return levelMenuItemStyles;
  367. }
  368. }
  369. [PersistenceMode (PersistenceMode.InnerProperty)]
  370. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  371. public virtual MenuItemStyleCollection LevelSelectedStyles {
  372. get {
  373. if (levelSelectedStyles == null) {
  374. levelSelectedStyles = new MenuItemStyleCollection ();
  375. if (IsTrackingViewState)
  376. ((IStateManager)levelSelectedStyles).TrackViewState();
  377. }
  378. return levelSelectedStyles;
  379. }
  380. }
  381. [PersistenceMode (PersistenceMode.InnerProperty)]
  382. [NotifyParentProperty (true)]
  383. [DefaultValue (null)]
  384. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  385. public virtual Style DynamicHoverStyle {
  386. get {
  387. if (dynamicHoverStyle == null) {
  388. dynamicHoverStyle = new Style ();
  389. if (IsTrackingViewState)
  390. dynamicHoverStyle.TrackViewState();
  391. }
  392. return dynamicHoverStyle;
  393. }
  394. }
  395. [PersistenceMode (PersistenceMode.InnerProperty)]
  396. [NotifyParentProperty (true)]
  397. [DefaultValue (null)]
  398. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  399. public virtual Style StaticHoverStyle {
  400. get {
  401. if (staticHoverStyle == null) {
  402. staticHoverStyle = new Style ();
  403. if (IsTrackingViewState)
  404. staticHoverStyle.TrackViewState();
  405. }
  406. return staticHoverStyle;
  407. }
  408. }
  409. [Browsable (false)]
  410. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  411. public MenuItem SelectedItem {
  412. get { return selectedItem; }
  413. }
  414. internal void SetSelectedItem (MenuItem item)
  415. {
  416. if (selectedItem == item) return;
  417. if (selectedItem != null)
  418. selectedItem.SelectedFlag = false;
  419. selectedItem = item;
  420. selectedItem.SelectedFlag = true;
  421. }
  422. public MenuItem FindItem (string valuePath)
  423. {
  424. if (valuePath == null) throw new ArgumentNullException ("valuePath");
  425. string[] path = valuePath.Split (PathSeparator);
  426. int n = 0;
  427. MenuItemCollection col = Items;
  428. bool foundBranch = true;
  429. while (col.Count > 0 && foundBranch) {
  430. foundBranch = false;
  431. foreach (MenuItem item in col) {
  432. if (item.Value == path [n]) {
  433. if (++n == path.Length) return item;
  434. col = item.ChildItems;
  435. foundBranch = true;
  436. break;
  437. }
  438. }
  439. }
  440. return null;
  441. }
  442. string GetBindingKey (string dataMember, int depth)
  443. {
  444. return dataMember + " " + depth;
  445. }
  446. internal MenuItemBinding FindBindingForItem (string type, int depth)
  447. {
  448. if (bindings == null) return null;
  449. MenuItemBinding bin = (MenuItemBinding) bindings [GetBindingKey (type, depth)];
  450. if (bin != null) return bin;
  451. bin = (MenuItemBinding) bindings [GetBindingKey (type, -1)];
  452. if (bin != null) return bin;
  453. bin = (MenuItemBinding) bindings [GetBindingKey ("", depth)];
  454. if (bin != null) return bin;
  455. bin = (MenuItemBinding) bindings [GetBindingKey ("", -1)];
  456. return bin;
  457. }
  458. protected internal override void PerformDataBinding ()
  459. {
  460. base.PerformDataBinding ();
  461. HierarchicalDataSourceView data = GetData ("");
  462. IHierarchicalEnumerable e = data.Select ();
  463. foreach (object obj in e) {
  464. IHierarchyData hdata = e.GetHierarchyData (obj);
  465. MenuItem item = new MenuItem ();
  466. item.Bind (hdata);
  467. Items.Add (item);
  468. }
  469. }
  470. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  471. {
  472. MenuItem item = FindItemByPos (eventArgument);
  473. if (item == null) return;
  474. item.Selected = true;
  475. OnMenuItemClick (new MenuEventArgs (item));
  476. }
  477. MenuItem FindItemByPos (string path)
  478. {
  479. string[] indexes = path.Split ('_');
  480. MenuItem item = null;
  481. foreach (string index in indexes) {
  482. int i = int.Parse (index);
  483. if (item == null) {
  484. if (i >= Items.Count) return null;
  485. item = Items [i];
  486. } else {
  487. if (i >= item.ChildItems.Count) return null;
  488. item = item.ChildItems [i];
  489. }
  490. }
  491. return item;
  492. }
  493. protected override void TrackViewState()
  494. {
  495. EnsureDataBound ();
  496. base.TrackViewState();
  497. if (dataBindings != null) {
  498. ((IStateManager)dataBindings).TrackViewState ();
  499. }
  500. if (items != null) {
  501. ((IStateManager)items).TrackViewState();
  502. }
  503. if (dynamicMenuItemStyle != null)
  504. dynamicMenuItemStyle.TrackViewState ();
  505. if (dynamicMenuStyle != null)
  506. dynamicMenuStyle.TrackViewState ();
  507. if (levelMenuItemStyles != null)
  508. ((IStateManager)levelMenuItemStyles).TrackViewState();
  509. if (levelSelectedStyles != null)
  510. ((IStateManager)levelSelectedStyles).TrackViewState();
  511. if (dynamicSelectedStyle != null)
  512. dynamicSelectedStyle.TrackViewState();
  513. if (staticMenuItemStyle != null)
  514. staticMenuItemStyle.TrackViewState ();
  515. if (staticMenuStyle != null)
  516. staticMenuStyle.TrackViewState ();
  517. if (staticSelectedStyle != null)
  518. staticSelectedStyle.TrackViewState();
  519. if (staticHoverStyle != null)
  520. staticHoverStyle.TrackViewState();
  521. if (dynamicHoverStyle != null)
  522. dynamicHoverStyle.TrackViewState();
  523. }
  524. protected override object SaveViewState()
  525. {
  526. object[] states = new object [13];
  527. states[0] = base.SaveViewState ();
  528. states[1] = dataBindings == null ? null : ((IStateManager)dataBindings).SaveViewState();
  529. states[2] = items == null ? null : ((IStateManager)items).SaveViewState();
  530. states[3] = dynamicMenuItemStyle == null ? null : dynamicMenuItemStyle.SaveViewState();
  531. states[4] = dynamicMenuStyle == null ? null : dynamicMenuStyle.SaveViewState();
  532. states[5] = levelMenuItemStyles == null ? null : ((IStateManager)levelMenuItemStyles).SaveViewState();
  533. states[6] = levelSelectedStyles == null ? null : ((IStateManager)levelSelectedStyles).SaveViewState();
  534. states[7] = dynamicSelectedStyle == null ? null : dynamicSelectedStyle.SaveViewState();
  535. states[8] = (staticMenuItemStyle == null ? null : staticMenuItemStyle.SaveViewState());
  536. states[9] = staticMenuStyle == null ? null : staticMenuStyle.SaveViewState();
  537. states[10] = staticSelectedStyle == null ? null : staticSelectedStyle.SaveViewState();
  538. states[11] = staticHoverStyle == null ? null : staticHoverStyle.SaveViewState();
  539. states[12] = dynamicHoverStyle == null ? null : dynamicHoverStyle.SaveViewState();
  540. for (int i = states.Length - 1; i >= 0; i--) {
  541. if (states [i] != null)
  542. return states;
  543. }
  544. return null;
  545. }
  546. protected override void LoadViewState (object savedState)
  547. {
  548. if (savedState == null)
  549. return;
  550. object [] states = (object []) savedState;
  551. base.LoadViewState (states[0]);
  552. if (states[1] != null)
  553. ((IStateManager)dataBindings).LoadViewState(states[1]);
  554. if (states[2] != null)
  555. ((IStateManager)Items).LoadViewState(states[2]);
  556. if (states[3] != null)
  557. dynamicMenuItemStyle.LoadViewState (states[3]);
  558. if (states[4] != null)
  559. dynamicMenuStyle.LoadViewState (states[4]);
  560. if (states[5] != null)
  561. ((IStateManager)levelMenuItemStyles).LoadViewState(states[5]);
  562. if (states[6] != null)
  563. ((IStateManager)levelSelectedStyles).LoadViewState(states[6]);
  564. if (states[7] != null)
  565. dynamicSelectedStyle.LoadViewState (states[7]);
  566. if (states[8] != null)
  567. staticMenuItemStyle.LoadViewState (states[8]);
  568. if (states[9] != null)
  569. staticMenuStyle.LoadViewState (states[9]);
  570. if (states[10] != null)
  571. staticSelectedStyle.LoadViewState (states[10]);
  572. if (states[11] != null)
  573. staticHoverStyle.LoadViewState (states[11]);
  574. if (states[12] != null)
  575. dynamicHoverStyle.LoadViewState (states[12]);
  576. }
  577. protected override void OnPreRender (EventArgs e)
  578. {
  579. base.OnPreRender (e);
  580. if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof(Menu), "Menu.js")) {
  581. string url = Page.GetWebResourceUrl (typeof(Menu), "Menu.js");
  582. Page.ClientScript.RegisterClientScriptInclude (typeof(Menu), "Menu.js", url);
  583. }
  584. string cmenu = ClientID + "_data";
  585. string script = string.Format ("var {0} = new Object ();\n", cmenu);
  586. script += string.Format ("{0}.disappearAfter = {1};\n", cmenu, ClientScriptManager.GetScriptLiteral (DisappearAfter));
  587. script += string.Format ("{0}.vertical = {1};\n", cmenu, ClientScriptManager.GetScriptLiteral (Orientation == Orientation.Vertical));
  588. if (DynamicHorizontalOffset != 0)
  589. script += string.Format ("{0}.dho = {1};\n", cmenu, ClientScriptManager.GetScriptLiteral (DynamicHorizontalOffset));
  590. if (DynamicVerticalOffset != 0)
  591. script += string.Format ("{0}.dvo = {1};\n", cmenu, ClientScriptManager.GetScriptLiteral (DynamicVerticalOffset));
  592. // The order in which styles are defined matters when more than one class
  593. // is assigned to an element
  594. if (dynamicMenuStyle != null)
  595. RegisterItemStyle (dynamicMenuStyle);
  596. if (staticMenuStyle != null)
  597. RegisterItemStyle (staticMenuStyle);
  598. if (staticMenuItemStyle != null)
  599. RegisterItemStyle (staticMenuItemStyle);
  600. if (staticSelectedStyle != null)
  601. RegisterItemStyle (staticSelectedStyle);
  602. if (dynamicMenuItemStyle != null)
  603. RegisterItemStyle (dynamicMenuItemStyle);
  604. if (dynamicSelectedStyle != null)
  605. RegisterItemStyle (dynamicSelectedStyle);
  606. if (levelMenuItemStyles != null)
  607. foreach (Style style in levelMenuItemStyles)
  608. RegisterItemStyle (style);
  609. if (levelSelectedStyles != null)
  610. foreach (Style style in levelSelectedStyles)
  611. RegisterItemStyle (style);
  612. if (dynamicHoverStyle != null)
  613. RegisterItemStyle (dynamicHoverStyle);
  614. if (staticHoverStyle != null)
  615. RegisterItemStyle (staticHoverStyle);
  616. if (staticHoverStyle != null)
  617. script += string.Format ("{0}.staticHover = {1};\n", cmenu, ClientScriptManager.GetScriptLiteral (staticHoverStyle.RegisteredCssClass));
  618. if (dynamicHoverStyle != null)
  619. script += string.Format ("{0}.dynamicHover = {1};\n", cmenu, ClientScriptManager.GetScriptLiteral (dynamicHoverStyle.RegisteredCssClass));
  620. Page.ClientScript.RegisterStartupScript (typeof(Menu), ClientID, script, true);
  621. if (dataBindings != null && dataBindings.Count > 0) {
  622. bindings = new Hashtable ();
  623. foreach (TreeNodeBinding bin in dataBindings) {
  624. string key = GetBindingKey (bin.DataMember, bin.Depth);
  625. bindings [key] = bin;
  626. }
  627. }
  628. else
  629. bindings = null;
  630. }
  631. void RegisterItemStyle (Style baseStyle)
  632. {
  633. Page.Header.StyleSheet.RegisterStyle (baseStyle, this);
  634. Style ts = new Style ();
  635. ts.CopyTextStylesFrom (baseStyle);
  636. Page.Header.StyleSheet.CreateStyleRule (ts, "." + baseStyle.RegisteredCssClass + " A", this);
  637. }
  638. protected override void RenderContents (HtmlTextWriter writer)
  639. {
  640. ArrayList dynamicMenus = new ArrayList ();
  641. RenderMenu (writer, Items, Orientation == Orientation.Vertical, dynamicMenus, false);
  642. for (int n=0; n<dynamicMenus.Count; n++) {
  643. MenuItem item = (MenuItem) dynamicMenus [n];
  644. writer.AddStyleAttribute ("visibility", "hidden");
  645. writer.AddStyleAttribute ("position", "absolute");
  646. writer.AddStyleAttribute ("left", "0px");
  647. writer.AddStyleAttribute ("top", "0px");
  648. writer.AddAttribute ("id", GetItemClientId (item, "s"));
  649. writer.RenderBeginTag (HtmlTextWriterTag.Div);
  650. RenderMenu (writer, item.ChildItems, true, dynamicMenus, true);
  651. writer.RenderEndTag (); // DIV
  652. }
  653. }
  654. void RenderMenu (HtmlTextWriter writer, MenuItemCollection items, bool vertical, ArrayList dynamicMenus, bool dynamic)
  655. {
  656. writer.AddAttribute ("cellpadding", "0");
  657. writer.AddAttribute ("cellspacing", "0");
  658. if (dynamic && dynamicMenuStyle != null)
  659. writer.AddAttribute ("class", dynamicMenuStyle.RegisteredCssClass);
  660. else if (!dynamic && staticMenuStyle != null)
  661. writer.AddAttribute ("class", staticMenuStyle.RegisteredCssClass);
  662. writer.RenderBeginTag (HtmlTextWriterTag.Table);
  663. if (!vertical) writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  664. for (int n=0; n<items.Count; n++) {
  665. MenuItem item = items [n];
  666. if (n > 0) {
  667. int itemSpacing = GetItemSpacing (item, dynamic);
  668. if (itemSpacing != 0) {
  669. if (vertical) {
  670. writer.AddAttribute ("height", itemSpacing + "px");
  671. writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  672. writer.RenderEndTag ();
  673. } else {
  674. writer.AddAttribute ("width", itemSpacing + "px");
  675. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  676. writer.RenderEndTag ();
  677. }
  678. }
  679. }
  680. RenderMenuItem (writer, item, dynamicMenus);
  681. }
  682. if (!vertical) writer.RenderEndTag (); // TR
  683. writer.RenderEndTag (); // TABLE
  684. }
  685. void RenderMenuItem (HtmlTextWriter writer, MenuItem item, ArrayList dynamicMenus)
  686. {
  687. bool displayChildren = (item.Depth + 1 < StaticDisplayLevels + MaximumDynamicDisplayLevels);
  688. bool dynamicChildren = displayChildren && (item.Depth + 1 >= StaticDisplayLevels) && item.ChildItems.Count > 0;
  689. bool isDynamicItem = item.Depth + 1 > StaticDisplayLevels;
  690. bool vertical = (Orientation == Orientation.Vertical) || isDynamicItem;
  691. if (vertical)
  692. writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  693. Style itemStyle = null;
  694. if (levelMenuItemStyles != null && item.Depth < levelMenuItemStyles.Count)
  695. itemStyle = levelMenuItemStyles [item.Depth];
  696. else if (isDynamicItem) {
  697. if (dynamicMenuItemStyle != null)
  698. itemStyle = dynamicMenuItemStyle;
  699. } else {
  700. if (staticMenuItemStyle != null)
  701. itemStyle = staticMenuItemStyle;
  702. }
  703. Style selectedStyle = null;
  704. if (item == SelectedItem) {
  705. if (levelSelectedStyles != null && item.Depth < levelSelectedStyles.Count)
  706. selectedStyle = levelSelectedStyles [item.Depth];
  707. else if (isDynamicItem) {
  708. if (dynamicSelectedStyle != null)
  709. selectedStyle = dynamicSelectedStyle;
  710. } else {
  711. if (staticSelectedStyle != null)
  712. selectedStyle = staticSelectedStyle;
  713. }
  714. }
  715. string cls = "";
  716. if (itemStyle != null) cls += itemStyle.RegisteredCssClass + " ";
  717. if (selectedStyle != null) cls += selectedStyle.RegisteredCssClass + " ";
  718. if (cls != "")
  719. writer.AddAttribute ("class", cls);
  720. string parentId = isDynamicItem ? "'" + item.Parent.Path + "'" : "null";
  721. if (dynamicChildren) {
  722. writer.AddAttribute ("onmouseover", string.Format ("javascript:Menu_OverItem ('{0}','{1}',{2})", ClientID, item.Path, parentId));
  723. writer.AddAttribute ("onmouseout", string.Format ("javascript:Menu_OutItem ('{0}','{1}')", ClientID, item.Path));
  724. } else if (isDynamicItem) {
  725. writer.AddAttribute ("onmouseover", string.Format ("javascript:Menu_OverDynamicLeafItem ('{0}','{1}',{2})", ClientID, item.Path, parentId));
  726. writer.AddAttribute ("onmouseout", string.Format ("javascript:Menu_OutItem ('{0}','{1}',{2})", ClientID, item.Path, parentId));
  727. } else {
  728. writer.AddAttribute ("onmouseover", string.Format ("javascript:Menu_OverStaticLeafItem ('{0}','{1}')", ClientID, item.Path));
  729. writer.AddAttribute ("onmouseout", string.Format ("javascript:Menu_OutItem ('{0}','{1}')", ClientID, item.Path));
  730. }
  731. writer.AddAttribute ("id", GetItemClientId (item, "i"));
  732. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  733. // Bottom separator image
  734. if (isDynamicItem && DynamicTopSeparatorImageUrl != "") {
  735. writer.AddAttribute ("src", DynamicTopSeparatorImageUrl);
  736. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  737. writer.RenderEndTag (); // IMG
  738. } else if (!isDynamicItem && StaticTopSeparatorImageUrl != "") {
  739. writer.AddAttribute ("src", StaticTopSeparatorImageUrl);
  740. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  741. writer.RenderEndTag (); // IMG
  742. }
  743. // Menu item box
  744. writer.AddAttribute ("cellpadding", "0");
  745. writer.AddAttribute ("cellspacing", "0");
  746. writer.AddAttribute ("width", "100%");
  747. writer.RenderBeginTag (HtmlTextWriterTag.Table);
  748. writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  749. if (item.Depth > 0 && !isDynamicItem) {
  750. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  751. writer.AddStyleAttribute ("width", StaticSubMenuIndent.ToString ());
  752. writer.RenderBeginTag (HtmlTextWriterTag.Div);
  753. writer.RenderEndTag (); // DIV
  754. writer.RenderEndTag (); // TD
  755. }
  756. if (item.ImageUrl != "") {
  757. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  758. RenderItemHref (writer, item);
  759. writer.RenderBeginTag (HtmlTextWriterTag.A);
  760. writer.AddAttribute ("src", item.ImageUrl);
  761. writer.AddAttribute ("border", "0");
  762. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  763. writer.RenderEndTag (); // IMG
  764. writer.RenderEndTag (); // A
  765. writer.RenderEndTag (); // TD
  766. }
  767. // Menu item text
  768. writer.AddAttribute ("width", "100%");
  769. if (!ItemWrap)
  770. writer.AddAttribute ("nowrap", "nowrap");
  771. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  772. RenderItemHref (writer, item);
  773. writer.AddStyleAttribute ("text-decoration", "none");
  774. writer.RenderBeginTag (HtmlTextWriterTag.A);
  775. writer.Write (item.Text);
  776. writer.RenderEndTag (); // A
  777. writer.RenderEndTag (); // TD
  778. // Popup image
  779. if (dynamicChildren && ((isDynamicItem && DynamicEnableDefaultPopOutImage) || (!isDynamicItem && StaticEnableDefaultPopOutImage) || item.PopOutImageUrl != ""))
  780. {
  781. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  782. string src;
  783. if (item.PopOutImageUrl != "")
  784. src = item.PopOutImageUrl;
  785. else
  786. src = AssemblyResourceLoader.GetResourceUrl (typeof(Menu), "arrow_plus.gif");
  787. writer.AddAttribute ("src", src);
  788. writer.AddAttribute ("border", "0");
  789. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  790. writer.RenderEndTag (); // IMG
  791. writer.RenderEndTag (); // TD
  792. }
  793. writer.RenderEndTag (); // TR
  794. writer.RenderEndTag (); // TABLE
  795. // Bottom separator image
  796. if (isDynamicItem && DynamicBottomSeparatorImageUrl != "") {
  797. writer.AddAttribute ("src", DynamicBottomSeparatorImageUrl);
  798. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  799. writer.RenderEndTag (); // IMG
  800. } else if (!isDynamicItem && StaticBottomSeparatorImageUrl != "") {
  801. writer.AddAttribute ("src", StaticBottomSeparatorImageUrl);
  802. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  803. writer.RenderEndTag (); // IMG
  804. }
  805. // Submenu
  806. if (vertical) {
  807. if (displayChildren) {
  808. if (dynamicChildren) dynamicMenus.Add (item);
  809. else {
  810. writer.AddAttribute ("width", "100%");
  811. RenderMenu (writer, item.ChildItems, true, dynamicMenus, false);
  812. }
  813. }
  814. writer.RenderEndTag (); // TD
  815. writer.RenderEndTag (); // TR
  816. } else {
  817. writer.RenderEndTag (); // TD
  818. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  819. if (displayChildren) {
  820. if (dynamicChildren) dynamicMenus.Add (item);
  821. else RenderMenu (writer, item.ChildItems, false, dynamicMenus, false);
  822. }
  823. writer.RenderEndTag (); // TD
  824. }
  825. }
  826. int GetItemSpacing (MenuItem item, bool dynamic)
  827. {
  828. int itemSpacing;
  829. if (item.Selected) {
  830. if (levelSelectedStyles != null && item.Depth < levelSelectedStyles.Count) {
  831. itemSpacing = levelSelectedStyles [item.Depth].ItemSpacing;
  832. if (itemSpacing != 0) return itemSpacing;
  833. }
  834. if (dynamic) itemSpacing = DynamicSelectedStyle.ItemSpacing;
  835. else itemSpacing = StaticSelectedStyle.ItemSpacing;
  836. if (itemSpacing != 0) return itemSpacing;
  837. }
  838. if (levelMenuItemStyles != null && item.Depth < levelMenuItemStyles.Count) {
  839. itemSpacing = levelMenuItemStyles [item.Depth].ItemSpacing;
  840. if (itemSpacing != 0) return itemSpacing;
  841. }
  842. if (dynamic) return DynamicMenuItemStyle.ItemSpacing;
  843. else return StaticMenuItemStyle.ItemSpacing;
  844. }
  845. void RenderItemHref (HtmlTextWriter writer, MenuItem item)
  846. {
  847. if (item.NavigateUrl != "") {
  848. writer.AddAttribute ("href", item.NavigateUrl);
  849. if (item.Target != null)
  850. writer.AddAttribute ("target", item.Target);
  851. }
  852. else {
  853. writer.AddAttribute ("href", GetClientEvent (item));
  854. }
  855. }
  856. string GetItemClientId (MenuItem item, string sufix)
  857. {
  858. return ClientID + "_" + item.Path + sufix;
  859. }
  860. string GetClientEvent (MenuItem item)
  861. {
  862. return Page.GetPostBackClientHyperlink (this, item.Path);
  863. }
  864. }
  865. }
  866. #endif