Menu.cs 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136
  1. //
  2. // System.Web.UI.WebControls.Menu.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. // Igor Zelmanovich ([email protected])
  7. //
  8. // (C) 2004 Novell, Inc (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. #if NET_2_0
  31. using System;
  32. using System.Collections;
  33. using System.Text;
  34. using System.ComponentModel;
  35. using System.Web.UI;
  36. using System.Web.Handlers;
  37. using System.Collections.Specialized;
  38. using System.IO;
  39. using System.Drawing;
  40. using System.Collections.Generic;
  41. namespace System.Web.UI.WebControls
  42. {
  43. [DefaultEvent ("MenuItemClick")]
  44. [ControlValueProperty ("SelectedValue")]
  45. [Designer ("System.Web.UI.Design.WebControls.MenuDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  46. [SupportsEventValidation]
  47. public class Menu : HierarchicalDataBoundControl, IPostBackEventHandler, INamingContainer
  48. {
  49. MenuItemStyle dynamicMenuItemStyle;
  50. SubMenuStyle dynamicMenuStyle;
  51. MenuItemStyle dynamicSelectedStyle;
  52. MenuItemStyle staticMenuItemStyle;
  53. SubMenuStyle staticMenuStyle;
  54. MenuItemStyle staticSelectedStyle;
  55. Style staticHoverStyle;
  56. Style dynamicHoverStyle;
  57. MenuItemStyleCollection levelMenuItemStyles;
  58. MenuItemStyleCollection levelSelectedStyles;
  59. SubMenuStyleCollection levelSubMenuStyles;
  60. ITemplate staticItemTemplate;
  61. ITemplate dynamicItemTemplate;
  62. MenuItemCollection items;
  63. MenuItemBindingCollection dataBindings;
  64. MenuItem selectedItem;
  65. string selectedItemPath;
  66. Hashtable bindings;
  67. Hashtable _menuItemControls;
  68. bool _requiresChildControlsDataBinding;
  69. SiteMapNode _currSiteMapNode;
  70. int registeredStylesCounter = -1;
  71. List<Style> levelSelectedLinkStyles;
  72. List<Style> levelMenuItemLinkStyles;
  73. Style popOutBoxStyle;
  74. Style controlLinkStyle;
  75. Style dynamicMenuItemLinkStyle;
  76. Style staticMenuItemLinkStyle;
  77. Style dynamicSelectedLinkStyle;
  78. Style staticSelectedLinkStyle;
  79. Style dynamicHoverLinkStyle;
  80. Style staticHoverLinkStyle;
  81. static readonly object MenuItemClickEvent = new object();
  82. static readonly object MenuItemDataBoundEvent = new object();
  83. public static readonly string MenuItemClickCommandName = "Click";
  84. public event MenuEventHandler MenuItemClick {
  85. add { Events.AddHandler (MenuItemClickEvent, value); }
  86. remove { Events.RemoveHandler (MenuItemClickEvent, value); }
  87. }
  88. public event MenuEventHandler MenuItemDataBound {
  89. add { Events.AddHandler (MenuItemDataBoundEvent, value); }
  90. remove { Events.RemoveHandler (MenuItemDataBoundEvent, value); }
  91. }
  92. protected virtual void OnMenuItemClick (MenuEventArgs e)
  93. {
  94. if (Events != null) {
  95. MenuEventHandler eh = (MenuEventHandler) Events [MenuItemClickEvent];
  96. if (eh != null) eh (this, e);
  97. }
  98. }
  99. protected virtual void OnMenuItemDataBound (MenuEventArgs e)
  100. {
  101. if (Events != null) {
  102. MenuEventHandler eh = (MenuEventHandler) Events [MenuItemDataBoundEvent];
  103. if (eh != null) eh (this, e);
  104. }
  105. }
  106. [DefaultValueAttribute (null)]
  107. [PersistenceMode (PersistenceMode.InnerProperty)]
  108. [EditorAttribute ("System.Web.UI.Design.WebControls.MenuBindingsEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  109. [MergablePropertyAttribute (false)]
  110. public MenuItemBindingCollection DataBindings {
  111. get {
  112. if (dataBindings == null) {
  113. dataBindings = new MenuItemBindingCollection ();
  114. if (IsTrackingViewState)
  115. ((IStateManager)dataBindings).TrackViewState();
  116. }
  117. return dataBindings;
  118. }
  119. }
  120. [DefaultValue (500)]
  121. [ThemeableAttribute (false)]
  122. public int DisappearAfter {
  123. get {
  124. object o = ViewState ["DisappearAfter"];
  125. if (o != null) return (int)o;
  126. return 500;
  127. }
  128. set {
  129. ViewState["DisappearAfter"] = value;
  130. }
  131. }
  132. [ThemeableAttribute (true)]
  133. [DefaultValue ("")]
  134. [UrlProperty]
  135. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  136. public string DynamicBottomSeparatorImageUrl {
  137. get {
  138. object o = ViewState ["dbsiu"];
  139. if (o != null) return (string)o;
  140. return "";
  141. }
  142. set {
  143. ViewState["dbsiu"] = value;
  144. }
  145. }
  146. [DefaultValueAttribute ("")]
  147. public string DynamicItemFormatString {
  148. get {
  149. object o = ViewState ["DynamicItemFormatString"];
  150. if (o != null) return (string)o;
  151. return "";
  152. }
  153. set {
  154. ViewState["DynamicItemFormatString"] = value;
  155. }
  156. }
  157. [DefaultValue ("")]
  158. [UrlProperty]
  159. [WebCategory ("Appearance")]
  160. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  161. public string DynamicTopSeparatorImageUrl {
  162. get {
  163. object o = ViewState ["dtsiu"];
  164. if (o != null) return (string)o;
  165. return "";
  166. }
  167. set {
  168. ViewState["dtsiu"] = value;
  169. }
  170. }
  171. [DefaultValue ("")]
  172. [UrlProperty]
  173. [WebCategory ("Appearance")]
  174. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  175. public string StaticBottomSeparatorImageUrl {
  176. get {
  177. object o = ViewState ["sbsiu"];
  178. if (o != null) return (string)o;
  179. return "";
  180. }
  181. set {
  182. ViewState["sbsiu"] = value;
  183. }
  184. }
  185. [DefaultValue ("")]
  186. [UrlProperty]
  187. [WebCategory ("Appearance")]
  188. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  189. public string StaticTopSeparatorImageUrl {
  190. get {
  191. object o = ViewState ["stsiu"];
  192. if (o != null) return (string)o;
  193. return "";
  194. }
  195. set {
  196. ViewState["stsiu"] = value;
  197. }
  198. }
  199. [DefaultValue (Orientation.Vertical)]
  200. public Orientation Orientation {
  201. get {
  202. object o = ViewState ["Orientation"];
  203. if (o != null) return (Orientation) o;
  204. return Orientation.Vertical;
  205. }
  206. set {
  207. ViewState["Orientation"] = value;
  208. }
  209. }
  210. [DefaultValue (1)]
  211. [ThemeableAttribute (true)]
  212. public int StaticDisplayLevels {
  213. get {
  214. object o = ViewState ["StaticDisplayLevels"];
  215. if (o != null) return (int)o;
  216. return 1;
  217. }
  218. set {
  219. if (value < 1) throw new ArgumentOutOfRangeException ();
  220. ViewState["StaticDisplayLevels"] = value;
  221. }
  222. }
  223. [DefaultValueAttribute ("")]
  224. public string StaticItemFormatString {
  225. get {
  226. object o = ViewState ["StaticItemFormatString"];
  227. if (o != null) return (string)o;
  228. return "";
  229. }
  230. set {
  231. ViewState["StaticItemFormatString"] = value;
  232. }
  233. }
  234. [DefaultValue (typeof (Unit), "16px")]
  235. [ThemeableAttribute (true)]
  236. public Unit StaticSubMenuIndent {
  237. get {
  238. object o = ViewState ["StaticSubMenuIndent"];
  239. if (o != null)
  240. return (Unit)o;
  241. #if NET_4_0
  242. return Unit.Empty;
  243. #else
  244. return new Unit (16);
  245. #endif
  246. }
  247. set {
  248. ViewState["StaticSubMenuIndent"] = value;
  249. }
  250. }
  251. [ThemeableAttribute (true)]
  252. [DefaultValue (3)]
  253. public int MaximumDynamicDisplayLevels {
  254. get {
  255. object o = ViewState ["MaximumDynamicDisplayLevels"];
  256. if (o != null) return (int)o;
  257. return 3;
  258. }
  259. set {
  260. if (value < 0) throw new ArgumentOutOfRangeException ();
  261. ViewState["MaximumDynamicDisplayLevels"] = value;
  262. }
  263. }
  264. [DefaultValue (0)]
  265. public int DynamicVerticalOffset {
  266. get {
  267. object o = ViewState ["DynamicVerticalOffset"];
  268. if (o != null) return (int)o;
  269. return 0;
  270. }
  271. set {
  272. ViewState["DynamicVerticalOffset"] = value;
  273. }
  274. }
  275. [DefaultValue (0)]
  276. public int DynamicHorizontalOffset {
  277. get {
  278. object o = ViewState ["DynamicHorizontalOffset"];
  279. if (o != null) return (int)o;
  280. return 0;
  281. }
  282. set {
  283. ViewState["DynamicHorizontalOffset"] = value;
  284. }
  285. }
  286. [DefaultValue (true)]
  287. public bool DynamicEnableDefaultPopOutImage {
  288. get {
  289. object o = ViewState ["dedpoi"];
  290. if (o != null) return (bool)o;
  291. return true;
  292. }
  293. set {
  294. ViewState["dedpoi"] = value;
  295. }
  296. }
  297. [DefaultValue (true)]
  298. public bool StaticEnableDefaultPopOutImage {
  299. get {
  300. object o = ViewState ["sedpoi"];
  301. if (o != null) return (bool)o;
  302. return true;
  303. }
  304. set {
  305. ViewState["sedpoi"] = value;
  306. }
  307. }
  308. [DefaultValueAttribute (null)]
  309. [PersistenceMode (PersistenceMode.InnerProperty)]
  310. [Editor ("System.Web.UI.Design.MenuItemCollectionEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  311. [MergablePropertyAttribute (false)]
  312. public MenuItemCollection Items {
  313. get {
  314. if (items == null) {
  315. items = new MenuItemCollection (this);
  316. if (IsTrackingViewState)
  317. ((IStateManager)items).TrackViewState();
  318. }
  319. return items;
  320. }
  321. }
  322. [DefaultValue ('/')]
  323. public char PathSeparator {
  324. get {
  325. object o = ViewState ["PathSeparator"];
  326. if(o != null) return (char)o;
  327. return '/';
  328. }
  329. set {
  330. ViewState ["PathSeparator"] = value;
  331. }
  332. }
  333. [DefaultValue (false)]
  334. public bool ItemWrap {
  335. get {
  336. object o = ViewState ["ItemWrap"];
  337. if(o != null) return (bool)o;
  338. return false;
  339. }
  340. set {
  341. ViewState ["ItemWrap"] = value;
  342. }
  343. }
  344. Style PopOutBoxStyle {
  345. get {
  346. if (popOutBoxStyle == null) {
  347. popOutBoxStyle = new Style ();
  348. popOutBoxStyle.BackColor = Color.White;
  349. }
  350. return popOutBoxStyle;
  351. }
  352. }
  353. Style ControlLinkStyle {
  354. get {
  355. if (controlLinkStyle == null) {
  356. controlLinkStyle = new Style ();
  357. controlLinkStyle.AlwaysRenderTextDecoration = true;
  358. }
  359. return controlLinkStyle;
  360. }
  361. }
  362. Style DynamicMenuItemLinkStyle {
  363. get {
  364. if (dynamicMenuItemLinkStyle == null) {
  365. dynamicMenuItemLinkStyle = new Style ();
  366. }
  367. return dynamicMenuItemLinkStyle;
  368. }
  369. }
  370. Style StaticMenuItemLinkStyle {
  371. get {
  372. if (staticMenuItemLinkStyle == null) {
  373. staticMenuItemLinkStyle = new Style ();
  374. }
  375. return staticMenuItemLinkStyle;
  376. }
  377. }
  378. Style DynamicSelectedLinkStyle {
  379. get {
  380. if (dynamicSelectedLinkStyle == null) {
  381. dynamicSelectedLinkStyle = new Style ();
  382. }
  383. return dynamicSelectedLinkStyle;
  384. }
  385. }
  386. Style StaticSelectedLinkStyle {
  387. get {
  388. if (staticSelectedLinkStyle == null) {
  389. staticSelectedLinkStyle = new Style ();
  390. }
  391. return staticSelectedLinkStyle;
  392. }
  393. }
  394. Style DynamicHoverLinkStyle {
  395. get {
  396. if (dynamicHoverLinkStyle == null) {
  397. dynamicHoverLinkStyle = new Style ();
  398. }
  399. return dynamicHoverLinkStyle;
  400. }
  401. }
  402. Style StaticHoverLinkStyle {
  403. get {
  404. if (staticHoverLinkStyle == null) {
  405. staticHoverLinkStyle = new Style ();
  406. }
  407. return staticHoverLinkStyle;
  408. }
  409. }
  410. [PersistenceMode (PersistenceMode.InnerProperty)]
  411. [NotifyParentProperty (true)]
  412. [DefaultValue (null)]
  413. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  414. public MenuItemStyle DynamicMenuItemStyle {
  415. get {
  416. if (dynamicMenuItemStyle == null) {
  417. dynamicMenuItemStyle = new MenuItemStyle ();
  418. if (IsTrackingViewState)
  419. dynamicMenuItemStyle.TrackViewState();
  420. }
  421. return dynamicMenuItemStyle;
  422. }
  423. }
  424. [PersistenceMode (PersistenceMode.InnerProperty)]
  425. [NotifyParentProperty (true)]
  426. [DefaultValue (null)]
  427. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  428. public MenuItemStyle DynamicSelectedStyle {
  429. get {
  430. if (dynamicSelectedStyle == null) {
  431. dynamicSelectedStyle = new MenuItemStyle ();
  432. if (IsTrackingViewState)
  433. dynamicSelectedStyle.TrackViewState();
  434. }
  435. return dynamicSelectedStyle;
  436. }
  437. }
  438. [PersistenceMode (PersistenceMode.InnerProperty)]
  439. [NotifyParentProperty (true)]
  440. [DefaultValue (null)]
  441. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  442. public SubMenuStyle DynamicMenuStyle {
  443. get {
  444. if (dynamicMenuStyle == null) {
  445. dynamicMenuStyle = new SubMenuStyle ();
  446. if (IsTrackingViewState)
  447. dynamicMenuStyle.TrackViewState();
  448. }
  449. return dynamicMenuStyle;
  450. }
  451. }
  452. [PersistenceMode (PersistenceMode.InnerProperty)]
  453. [NotifyParentProperty (true)]
  454. [DefaultValue (null)]
  455. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  456. public MenuItemStyle StaticMenuItemStyle {
  457. get {
  458. if (staticMenuItemStyle == null) {
  459. staticMenuItemStyle = new MenuItemStyle ();
  460. if (IsTrackingViewState)
  461. staticMenuItemStyle.TrackViewState();
  462. }
  463. return staticMenuItemStyle;
  464. }
  465. }
  466. [PersistenceMode (PersistenceMode.InnerProperty)]
  467. [NotifyParentProperty (true)]
  468. [DefaultValue (null)]
  469. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  470. public MenuItemStyle StaticSelectedStyle {
  471. get {
  472. if (staticSelectedStyle == null) {
  473. staticSelectedStyle = new MenuItemStyle ();
  474. if (IsTrackingViewState)
  475. staticSelectedStyle.TrackViewState();
  476. }
  477. return staticSelectedStyle;
  478. }
  479. }
  480. [PersistenceMode (PersistenceMode.InnerProperty)]
  481. [NotifyParentProperty (true)]
  482. [DefaultValue (null)]
  483. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  484. public SubMenuStyle StaticMenuStyle {
  485. get {
  486. if (staticMenuStyle == null) {
  487. staticMenuStyle = new SubMenuStyle ();
  488. if (IsTrackingViewState)
  489. staticMenuStyle.TrackViewState();
  490. }
  491. return staticMenuStyle;
  492. }
  493. }
  494. [DefaultValue (null)]
  495. [PersistenceMode (PersistenceMode.InnerProperty)]
  496. [Editor ("System.Web.UI.Design.WebControls.MenuItemStyleCollectionEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  497. public MenuItemStyleCollection LevelMenuItemStyles {
  498. get {
  499. if (levelMenuItemStyles == null) {
  500. levelMenuItemStyles = new MenuItemStyleCollection ();
  501. if (IsTrackingViewState)
  502. ((IStateManager)levelMenuItemStyles).TrackViewState();
  503. }
  504. return levelMenuItemStyles;
  505. }
  506. }
  507. [DefaultValue (null)]
  508. [PersistenceMode (PersistenceMode.InnerProperty)]
  509. [Editor ("System.Web.UI.Design.WebControls.MenuItemStyleCollectionEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  510. public MenuItemStyleCollection LevelSelectedStyles {
  511. get {
  512. if (levelSelectedStyles == null) {
  513. levelSelectedStyles = new MenuItemStyleCollection ();
  514. if (IsTrackingViewState)
  515. ((IStateManager)levelSelectedStyles).TrackViewState();
  516. }
  517. return levelSelectedStyles;
  518. }
  519. }
  520. [DefaultValue (null)]
  521. [PersistenceMode (PersistenceMode.InnerProperty)]
  522. [Editor ("System.Web.UI.Design.WebControls.SubMenuStyleCollectionEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  523. public SubMenuStyleCollection LevelSubMenuStyles {
  524. get {
  525. if (levelSubMenuStyles == null) {
  526. levelSubMenuStyles = new SubMenuStyleCollection ();
  527. if (IsTrackingViewState)
  528. ((IStateManager)levelSubMenuStyles).TrackViewState();
  529. }
  530. return levelSubMenuStyles;
  531. }
  532. }
  533. [PersistenceMode (PersistenceMode.InnerProperty)]
  534. [NotifyParentProperty (true)]
  535. [DefaultValue (null)]
  536. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  537. public Style DynamicHoverStyle {
  538. get {
  539. if (dynamicHoverStyle == null) {
  540. dynamicHoverStyle = new Style ();
  541. if (IsTrackingViewState)
  542. dynamicHoverStyle.TrackViewState();
  543. }
  544. return dynamicHoverStyle;
  545. }
  546. }
  547. [PersistenceMode (PersistenceMode.InnerProperty)]
  548. [NotifyParentProperty (true)]
  549. [DefaultValue (null)]
  550. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  551. public Style StaticHoverStyle {
  552. get {
  553. if (staticHoverStyle == null) {
  554. staticHoverStyle = new Style ();
  555. if (IsTrackingViewState)
  556. staticHoverStyle.TrackViewState();
  557. }
  558. return staticHoverStyle;
  559. }
  560. }
  561. [DefaultValue ("")]
  562. [UrlProperty]
  563. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  564. public string ScrollDownImageUrl {
  565. get {
  566. object o = ViewState ["sdiu"];
  567. if (o != null) return (string)o;
  568. return "";
  569. }
  570. set {
  571. ViewState["sdiu"] = value;
  572. }
  573. }
  574. [DefaultValue ("")]
  575. [UrlProperty]
  576. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  577. public string ScrollUpImageUrl {
  578. get {
  579. object o = ViewState ["suiu"];
  580. if (o != null) return (string)o;
  581. return "";
  582. }
  583. set {
  584. ViewState["suiu"] = value;
  585. }
  586. }
  587. [Localizable (true)]
  588. public string ScrollDownText {
  589. get {
  590. object o = ViewState ["ScrollDownText"];
  591. if (o != null) return (string) o;
  592. return Locale.GetText ("Scroll down");
  593. }
  594. set {
  595. ViewState["ScrollDownText"] = value;
  596. }
  597. }
  598. [Localizable (true)]
  599. public string ScrollUpText {
  600. get {
  601. object o = ViewState ["ScrollUpText"];
  602. if (o != null) return (string) o;
  603. return Locale.GetText ("Scroll up");
  604. }
  605. set {
  606. ViewState["ScrollUpText"] = value;
  607. }
  608. }
  609. public string DynamicPopOutImageTextFormatString
  610. {
  611. get
  612. {
  613. object o = ViewState ["dpoitf"];
  614. if (o != null) return (string) o;
  615. return Locale.GetText ("Expand {0}");
  616. }
  617. set
  618. {
  619. ViewState ["dpoitf"] = value;
  620. }
  621. }
  622. [DefaultValue ("")]
  623. [UrlProperty]
  624. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  625. public string DynamicPopOutImageUrl {
  626. get {
  627. object o = ViewState ["dpoiu"];
  628. if (o != null) return (string)o;
  629. return "";
  630. }
  631. set {
  632. ViewState["dpoiu"] = value;
  633. }
  634. }
  635. public string StaticPopOutImageTextFormatString
  636. {
  637. get
  638. {
  639. object o = ViewState ["spoitf"];
  640. if (o != null) return (string) o;
  641. return Locale.GetText ("Expand {0}");
  642. }
  643. set
  644. {
  645. ViewState ["spoitf"] = value;
  646. }
  647. }
  648. [DefaultValue ("")]
  649. [UrlProperty]
  650. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  651. public string StaticPopOutImageUrl {
  652. get {
  653. object o = ViewState ["spoiu"];
  654. if (o != null) return (string)o;
  655. return "";
  656. }
  657. set {
  658. ViewState["spoiu"] = value;
  659. }
  660. }
  661. [DefaultValue ("")]
  662. public string Target {
  663. get {
  664. object o = ViewState ["Target"];
  665. if (o != null) return (string) o;
  666. return "";
  667. }
  668. set {
  669. ViewState["Target"] = value;
  670. }
  671. }
  672. [DefaultValue (null)]
  673. [TemplateContainer (typeof(MenuItemTemplateContainer), BindingDirection.OneWay)]
  674. [PersistenceMode (PersistenceMode.InnerProperty)]
  675. [Browsable (false)]
  676. public ITemplate StaticItemTemplate {
  677. get { return staticItemTemplate; }
  678. set { staticItemTemplate = value; }
  679. }
  680. [DefaultValue (null)]
  681. [TemplateContainer (typeof(MenuItemTemplateContainer), BindingDirection.OneWay)]
  682. [PersistenceMode (PersistenceMode.InnerProperty)]
  683. [Browsable (false)]
  684. public ITemplate DynamicItemTemplate {
  685. get { return dynamicItemTemplate; }
  686. set { dynamicItemTemplate = value; }
  687. }
  688. [Browsable (false)]
  689. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  690. public MenuItem SelectedItem {
  691. get {
  692. if (selectedItem == null && selectedItemPath != null) {
  693. selectedItem = FindItemByPos (selectedItemPath);
  694. }
  695. return selectedItem;
  696. }
  697. }
  698. [Browsable (false)]
  699. [DefaultValue ("")]
  700. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  701. public string SelectedValue {
  702. get { return selectedItem != null ? selectedItem.Value : ""; }
  703. }
  704. [Localizable (true)]
  705. public string SkipLinkText
  706. {
  707. get {
  708. object o = ViewState ["SkipLinkText"];
  709. if (o != null)
  710. return (string) o;
  711. return "Skip Navigation Links";
  712. }
  713. set {
  714. ViewState ["SkipLinkText"] = value;
  715. }
  716. }
  717. internal void SetSelectedItem (MenuItem item)
  718. {
  719. if (selectedItem == item) return;
  720. selectedItem = item;
  721. selectedItemPath = item.Path;
  722. }
  723. public MenuItem FindItem (string valuePath)
  724. {
  725. if (valuePath == null) throw new ArgumentNullException ("valuePath");
  726. string[] path = valuePath.Split (PathSeparator);
  727. int n = 0;
  728. MenuItemCollection col = Items;
  729. bool foundBranch = true;
  730. while (col.Count > 0 && foundBranch) {
  731. foundBranch = false;
  732. foreach (MenuItem item in col) {
  733. if (item.Value == path [n]) {
  734. if (++n == path.Length) return item;
  735. col = item.ChildItems;
  736. foundBranch = true;
  737. break;
  738. }
  739. }
  740. }
  741. return null;
  742. }
  743. string GetBindingKey (string dataMember, int depth)
  744. {
  745. return dataMember + " " + depth;
  746. }
  747. internal MenuItemBinding FindBindingForItem (string type, int depth)
  748. {
  749. if (bindings == null) return null;
  750. MenuItemBinding bin = (MenuItemBinding) bindings [GetBindingKey (type, depth)];
  751. if (bin != null) return bin;
  752. bin = (MenuItemBinding) bindings [GetBindingKey (type, -1)];
  753. if (bin != null) return bin;
  754. bin = (MenuItemBinding) bindings [GetBindingKey ("", depth)];
  755. if (bin != null) return bin;
  756. bin = (MenuItemBinding) bindings [GetBindingKey ("", -1)];
  757. return bin;
  758. }
  759. protected internal override void PerformDataBinding ()
  760. {
  761. base.PerformDataBinding ();
  762. // Do not attempt to bind data if there is no
  763. // data source set.
  764. if (!IsBoundUsingDataSourceID && (DataSource == null)) {
  765. EnsureChildControlsDataBound ();
  766. return;
  767. }
  768. InitializeDataBindings ();
  769. HierarchicalDataSourceView data = GetData ("");
  770. if (data == null) {
  771. throw new InvalidOperationException ("No view returned by data source control.");
  772. }
  773. Items.Clear ();
  774. IHierarchicalEnumerable e = data.Select ();
  775. FillBoundChildrenRecursive (e, Items);
  776. CreateChildControlsForItems ();
  777. ChildControlsCreated = true;
  778. EnsureChildControlsDataBound ();
  779. }
  780. void FillBoundChildrenRecursive (IHierarchicalEnumerable hEnumerable, MenuItemCollection itemCollection)
  781. {
  782. if (hEnumerable == null)
  783. return;
  784. foreach (object obj in hEnumerable) {
  785. IHierarchyData hdata = hEnumerable.GetHierarchyData (obj);
  786. MenuItem item = new MenuItem ();
  787. itemCollection.Add (item);
  788. item.Bind (hdata);
  789. SiteMapNode siteMapNode = hdata as SiteMapNode;
  790. if (siteMapNode != null) {
  791. if (_currSiteMapNode == null)
  792. _currSiteMapNode = siteMapNode.Provider.CurrentNode;
  793. if (siteMapNode == _currSiteMapNode)
  794. item.Selected = true;
  795. }
  796. OnMenuItemDataBound (new MenuEventArgs (item));
  797. if (hdata == null || !hdata.HasChildren)
  798. continue;
  799. IHierarchicalEnumerable e = hdata.GetChildren ();
  800. FillBoundChildrenRecursive (e, item.ChildItems);
  801. }
  802. }
  803. protected void SetItemDataBound (MenuItem node, bool dataBound)
  804. {
  805. node.SetDataBound (dataBound);
  806. }
  807. protected void SetItemDataPath (MenuItem node, string dataPath)
  808. {
  809. node.SetDataPath (dataPath);
  810. }
  811. protected void SetItemDataItem (MenuItem node, object dataItem)
  812. {
  813. node.SetDataItem (dataItem);
  814. }
  815. protected internal virtual void RaisePostBackEvent (string eventArgument)
  816. {
  817. ValidateEvent (UniqueID, eventArgument);
  818. if (!IsEnabled)
  819. return;
  820. EnsureChildControls();
  821. MenuItem item = FindItemByPos (eventArgument);
  822. if (item == null) return;
  823. item.Selected = true;
  824. OnMenuItemClick (new MenuEventArgs (item));
  825. }
  826. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  827. {
  828. RaisePostBackEvent (eventArgument);
  829. }
  830. MenuItem FindItemByPos (string path)
  831. {
  832. string[] indexes = path.Split ('_');
  833. MenuItem item = null;
  834. foreach (string index in indexes) {
  835. int i = int.Parse (index);
  836. if (item == null) {
  837. if (i >= Items.Count) return null;
  838. item = Items [i];
  839. } else {
  840. if (i >= item.ChildItems.Count) return null;
  841. item = item.ChildItems [i];
  842. }
  843. }
  844. return item;
  845. }
  846. protected override HtmlTextWriterTag TagKey {
  847. get { return HtmlTextWriterTag.Table; }
  848. }
  849. protected override void TrackViewState()
  850. {
  851. EnsureDataBound ();
  852. base.TrackViewState();
  853. if (dataBindings != null) {
  854. ((IStateManager)dataBindings).TrackViewState ();
  855. }
  856. if (items != null) {
  857. ((IStateManager)items).TrackViewState();
  858. }
  859. if (dynamicMenuItemStyle != null)
  860. dynamicMenuItemStyle.TrackViewState ();
  861. if (dynamicMenuStyle != null)
  862. dynamicMenuStyle.TrackViewState ();
  863. if (levelMenuItemStyles != null && levelMenuItemStyles.Count > 0)
  864. ((IStateManager)levelMenuItemStyles).TrackViewState();
  865. if (levelSelectedStyles != null && levelMenuItemStyles.Count > 0)
  866. ((IStateManager)levelSelectedStyles).TrackViewState();
  867. if (levelSubMenuStyles != null && levelSubMenuStyles.Count > 0)
  868. ((IStateManager)levelSubMenuStyles).TrackViewState();
  869. if (dynamicSelectedStyle != null)
  870. dynamicSelectedStyle.TrackViewState();
  871. if (staticMenuItemStyle != null)
  872. staticMenuItemStyle.TrackViewState ();
  873. if (staticMenuStyle != null)
  874. staticMenuStyle.TrackViewState ();
  875. if (staticSelectedStyle != null)
  876. staticSelectedStyle.TrackViewState();
  877. if (staticHoverStyle != null)
  878. staticHoverStyle.TrackViewState();
  879. if (dynamicHoverStyle != null)
  880. dynamicHoverStyle.TrackViewState();
  881. }
  882. protected override object SaveViewState()
  883. {
  884. object[] states = new object [14];
  885. states[0] = base.SaveViewState ();
  886. states[1] = dataBindings == null ? null : ((IStateManager)dataBindings).SaveViewState();
  887. states[2] = items == null ? null : ((IStateManager)items).SaveViewState();
  888. states[3] = dynamicMenuItemStyle == null ? null : dynamicMenuItemStyle.SaveViewState();
  889. states[4] = dynamicMenuStyle == null ? null : dynamicMenuStyle.SaveViewState();
  890. states[5] = levelMenuItemStyles == null ? null : ((IStateManager)levelMenuItemStyles).SaveViewState();
  891. states[6] = levelSelectedStyles == null ? null : ((IStateManager)levelSelectedStyles).SaveViewState();
  892. states[7] = dynamicSelectedStyle == null ? null : dynamicSelectedStyle.SaveViewState();
  893. states[8] = (staticMenuItemStyle == null ? null : staticMenuItemStyle.SaveViewState());
  894. states[9] = staticMenuStyle == null ? null : staticMenuStyle.SaveViewState();
  895. states[10] = staticSelectedStyle == null ? null : staticSelectedStyle.SaveViewState();
  896. states[11] = staticHoverStyle == null ? null : staticHoverStyle.SaveViewState();
  897. states[12] = dynamicHoverStyle == null ? null : dynamicHoverStyle.SaveViewState();
  898. states[13] = levelSubMenuStyles == null ? null : ((IStateManager)levelSubMenuStyles).SaveViewState();
  899. for (int i = states.Length - 1; i >= 0; i--) {
  900. if (states [i] != null)
  901. return states;
  902. }
  903. return null;
  904. }
  905. protected override void LoadViewState (object savedState)
  906. {
  907. if (savedState == null)
  908. return;
  909. object [] states = (object []) savedState;
  910. base.LoadViewState (states[0]);
  911. if (states[1] != null)
  912. ((IStateManager)DataBindings).LoadViewState(states[1]);
  913. if (states[2] != null)
  914. ((IStateManager)Items).LoadViewState(states[2]);
  915. if (states[3] != null)
  916. DynamicMenuItemStyle.LoadViewState (states[3]);
  917. if (states[4] != null)
  918. DynamicMenuStyle.LoadViewState (states[4]);
  919. if (states[5] != null)
  920. ((IStateManager)LevelMenuItemStyles).LoadViewState(states[5]);
  921. if (states[6] != null)
  922. ((IStateManager)LevelSelectedStyles).LoadViewState(states[6]);
  923. if (states[7] != null)
  924. DynamicSelectedStyle.LoadViewState (states[7]);
  925. if (states[8] != null)
  926. StaticMenuItemStyle.LoadViewState (states[8]);
  927. if (states[9] != null)
  928. StaticMenuStyle.LoadViewState (states[9]);
  929. if (states[10] != null)
  930. StaticSelectedStyle.LoadViewState (states[10]);
  931. if (states[11] != null)
  932. StaticHoverStyle.LoadViewState (states[11]);
  933. if (states[12] != null)
  934. DynamicHoverStyle.LoadViewState (states[12]);
  935. if (states[13] != null)
  936. ((IStateManager)LevelSubMenuStyles).LoadViewState(states[13]);
  937. }
  938. protected internal override void OnInit (EventArgs e)
  939. {
  940. Page.RegisterRequiresControlState (this);
  941. base.OnInit (e);
  942. }
  943. protected internal override void LoadControlState (object ob)
  944. {
  945. if (ob == null) return;
  946. object[] state = (object[]) ob;
  947. base.LoadControlState (state[0]);
  948. selectedItemPath = state[1] as string;
  949. }
  950. protected internal override object SaveControlState ()
  951. {
  952. object bstate = base.SaveControlState ();
  953. object mstate = selectedItemPath;
  954. if (bstate != null || mstate != null)
  955. return new object[] { bstate, mstate };
  956. else
  957. return null;
  958. }
  959. protected internal override void CreateChildControls ()
  960. {
  961. if (!IsBoundUsingDataSourceID && (DataSource == null)) {
  962. CreateChildControlsForItems ();
  963. }
  964. else {
  965. EnsureDataBound ();
  966. }
  967. }
  968. void CreateChildControlsForItems () {
  969. Controls.Clear ();
  970. // Check for HasChildViewState to avoid unnecessary calls to ClearChildViewState.
  971. if (HasChildViewState)
  972. ClearChildViewState ();
  973. _menuItemControls = new Hashtable ();
  974. CreateChildControlsForItems (Items);
  975. _requiresChildControlsDataBinding = true;
  976. }
  977. void CreateChildControlsForItems (MenuItemCollection items ) {
  978. foreach (MenuItem item in items) {
  979. bool isDynamicItem = IsDynamicItem (item);
  980. if (isDynamicItem && dynamicItemTemplate != null) {
  981. MenuItemTemplateContainer cter = new MenuItemTemplateContainer (item.Index, item);
  982. dynamicItemTemplate.InstantiateIn (cter);
  983. _menuItemControls [item] = cter;
  984. Controls.Add (cter);
  985. }
  986. else if (!isDynamicItem && staticItemTemplate != null) {
  987. MenuItemTemplateContainer cter = new MenuItemTemplateContainer (item.Index, item);
  988. staticItemTemplate.InstantiateIn (cter);
  989. _menuItemControls [item] = cter;
  990. Controls.Add (cter);
  991. }
  992. if (item.HasChildData)
  993. CreateChildControlsForItems (item.ChildItems);
  994. }
  995. }
  996. protected override void EnsureDataBound ()
  997. {
  998. base.EnsureDataBound ();
  999. EnsureChildControlsDataBound ();
  1000. }
  1001. void EnsureChildControlsDataBound () {
  1002. if (!_requiresChildControlsDataBinding)
  1003. return;
  1004. DataBindChildren ();
  1005. _requiresChildControlsDataBinding = false;
  1006. }
  1007. [MonoTODO ("Not implemented")]
  1008. protected override IDictionary GetDesignModeState ()
  1009. {
  1010. throw new NotImplementedException ();
  1011. }
  1012. [MonoTODO ("Not implemented")]
  1013. protected override void SetDesignModeState (IDictionary data)
  1014. {
  1015. throw new NotImplementedException ();
  1016. }
  1017. public override ControlCollection Controls {
  1018. get { return base.Controls; }
  1019. }
  1020. public sealed override void DataBind ()
  1021. {
  1022. base.DataBind ();
  1023. }
  1024. protected override bool OnBubbleEvent (object source, EventArgs args)
  1025. {
  1026. if (!(args is CommandEventArgs))
  1027. return false;
  1028. MenuEventArgs menuArgs = args as MenuEventArgs;
  1029. if (menuArgs != null && string.Equals (menuArgs.CommandName, MenuItemClickCommandName))
  1030. OnMenuItemClick (menuArgs);
  1031. return true;
  1032. }
  1033. protected override void OnDataBinding (EventArgs e)
  1034. {
  1035. EnsureChildControls ();
  1036. base.OnDataBinding (e);
  1037. }
  1038. const string onPreRenderScript = "var {0} = new Object ();\n{0}.webForm = {1};\n{0}.disappearAfter = {2};\n{0}.vertical = {3};";
  1039. protected internal override void OnPreRender (EventArgs e)
  1040. {
  1041. base.OnPreRender (e);
  1042. if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof(Menu), "Menu.js")) {
  1043. string url = Page.ClientScript.GetWebResourceUrl (typeof(Menu), "Menu.js");
  1044. Page.ClientScript.RegisterClientScriptInclude (typeof(Menu), "Menu.js", url);
  1045. }
  1046. string cmenu = ClientID + "_data";
  1047. string script = String.Format (onPreRenderScript,
  1048. cmenu,
  1049. Page.IsMultiForm ? Page.theForm : "window",
  1050. ClientScriptManager.GetScriptLiteral (DisappearAfter),
  1051. ClientScriptManager.GetScriptLiteral (Orientation == Orientation.Vertical));
  1052. if (DynamicHorizontalOffset != 0)
  1053. script += String.Concat (cmenu, ".dho = ", ClientScriptManager.GetScriptLiteral (DynamicHorizontalOffset), ";\n");
  1054. if (DynamicVerticalOffset != 0)
  1055. script += String.Concat (cmenu, ".dvo = ", ClientScriptManager.GetScriptLiteral (DynamicVerticalOffset), ";\n");
  1056. // The order in which styles are defined matters when more than one class
  1057. // is assigned to an element
  1058. RegisterStyle (PopOutBoxStyle);
  1059. RegisterStyle (ControlStyle, ControlLinkStyle);
  1060. if (staticMenuItemStyle != null)
  1061. RegisterStyle (StaticMenuItemStyle, StaticMenuItemLinkStyle);
  1062. if (staticMenuStyle != null)
  1063. RegisterStyle (StaticMenuStyle);
  1064. if (dynamicMenuItemStyle != null)
  1065. RegisterStyle (DynamicMenuItemStyle, DynamicMenuItemLinkStyle);
  1066. if (dynamicMenuStyle != null)
  1067. RegisterStyle (DynamicMenuStyle);
  1068. if (levelMenuItemStyles != null && levelMenuItemStyles.Count > 0) {
  1069. levelMenuItemLinkStyles = new List<Style> (levelMenuItemStyles.Count);
  1070. foreach (Style style in levelMenuItemStyles) {
  1071. Style linkStyle = new Style ();
  1072. levelMenuItemLinkStyles.Add (linkStyle);
  1073. RegisterStyle (style, linkStyle);
  1074. }
  1075. }
  1076. if (levelSubMenuStyles != null)
  1077. foreach (Style style in levelSubMenuStyles)
  1078. RegisterStyle (style);
  1079. if (staticSelectedStyle != null)
  1080. RegisterStyle (staticSelectedStyle, StaticSelectedLinkStyle);
  1081. if (dynamicSelectedStyle != null)
  1082. RegisterStyle (dynamicSelectedStyle, DynamicSelectedLinkStyle);
  1083. if (levelSelectedStyles != null && levelSelectedStyles.Count > 0) {
  1084. levelSelectedLinkStyles = new List<Style> (levelSelectedStyles.Count);
  1085. foreach (Style style in levelSelectedStyles) {
  1086. Style linkStyle = new Style ();
  1087. levelSelectedLinkStyles.Add (linkStyle);
  1088. RegisterStyle (style, linkStyle);
  1089. }
  1090. }
  1091. if (staticHoverStyle != null) {
  1092. if (Page.Header == null)
  1093. throw new InvalidOperationException ("Using Menu.StaticHoverStyle requires Page.Header to be non-null (e.g. <head runat=\"server\" />).");
  1094. RegisterStyle (staticHoverStyle, StaticHoverLinkStyle);
  1095. script += string.Concat (cmenu, ".staticHover = ", ClientScriptManager.GetScriptLiteral (staticHoverStyle.RegisteredCssClass), ";\n");
  1096. script += string.Concat (cmenu, ".staticLinkHover = ", ClientScriptManager.GetScriptLiteral (StaticHoverLinkStyle.RegisteredCssClass), ";\n");
  1097. }
  1098. if (dynamicHoverStyle != null) {
  1099. if (Page.Header == null)
  1100. throw new InvalidOperationException ("Using Menu.DynamicHoverStyle requires Page.Header to be non-null (e.g. <head runat=\"server\" />).");
  1101. RegisterStyle (dynamicHoverStyle, DynamicHoverLinkStyle);
  1102. script += string.Concat (cmenu, ".dynamicHover = ", ClientScriptManager.GetScriptLiteral (dynamicHoverStyle.RegisteredCssClass), ";\n");
  1103. script += string.Concat (cmenu, ".dynamicLinkHover = ", ClientScriptManager.GetScriptLiteral (DynamicHoverLinkStyle.RegisteredCssClass), ";\n");
  1104. }
  1105. Page.ClientScript.RegisterWebFormClientScript ();
  1106. Page.ClientScript.RegisterStartupScript (typeof(Menu), ClientID, script, true);
  1107. }
  1108. void InitializeDataBindings () {
  1109. if (dataBindings != null && dataBindings.Count > 0) {
  1110. bindings = new Hashtable ();
  1111. foreach (MenuItemBinding bin in dataBindings) {
  1112. string key = GetBindingKey (bin.DataMember, bin.Depth);
  1113. bindings [key] = bin;
  1114. }
  1115. }
  1116. else
  1117. bindings = null;
  1118. }
  1119. string IncrementStyleClassName () {
  1120. registeredStylesCounter++;
  1121. return ClientID + "_" + registeredStylesCounter;
  1122. }
  1123. void RegisterStyle (Style baseStyle, Style linkStyle) {
  1124. linkStyle.CopyTextStylesFrom (baseStyle);
  1125. linkStyle.BorderStyle = BorderStyle.None;
  1126. RegisterStyle (linkStyle);
  1127. RegisterStyle (baseStyle);
  1128. }
  1129. void RegisterStyle (Style baseStyle)
  1130. {
  1131. if (Page.Header == null)
  1132. return;
  1133. string className = IncrementStyleClassName ();
  1134. baseStyle.SetRegisteredCssClass (className);
  1135. Page.Header.StyleSheet.CreateStyleRule (baseStyle, this, "." + className);
  1136. }
  1137. protected internal override void Render (HtmlTextWriter writer)
  1138. {
  1139. if (Items.Count > 0)
  1140. base.Render (writer);
  1141. }
  1142. protected override void AddAttributesToRender (HtmlTextWriter writer)
  1143. {
  1144. writer.AddAttribute ("cellpadding", "0", false);
  1145. writer.AddAttribute ("cellspacing", "0", false);
  1146. writer.AddAttribute ("border", "0", false);
  1147. if (Page.Header != null) {
  1148. // styles are registered
  1149. if (staticMenuStyle != null) {
  1150. AddCssClass (ControlStyle, staticMenuStyle.CssClass);
  1151. AddCssClass (ControlStyle, staticMenuStyle.RegisteredCssClass);
  1152. }
  1153. if (levelSubMenuStyles != null && levelSubMenuStyles.Count > 0) {
  1154. AddCssClass (ControlStyle, levelSubMenuStyles [0].CssClass);
  1155. AddCssClass (ControlStyle, levelSubMenuStyles [0].RegisteredCssClass);
  1156. }
  1157. }
  1158. else {
  1159. // styles are not registered
  1160. if (staticMenuStyle != null){
  1161. ControlStyle.CopyFrom (staticMenuStyle);
  1162. }
  1163. if (levelSubMenuStyles != null && levelSubMenuStyles.Count > 0) {
  1164. ControlStyle.CopyFrom (levelSubMenuStyles [0]);
  1165. }
  1166. }
  1167. base.AddAttributesToRender (writer);
  1168. }
  1169. void AddCssClass (Style style, string cssClass) {
  1170. style.AddCssClass (cssClass);
  1171. }
  1172. public override void RenderBeginTag (HtmlTextWriter writer)
  1173. {
  1174. if (SkipLinkText != "") {
  1175. System.Web.UI.HtmlControls.HtmlAnchor anchor = new System.Web.UI.HtmlControls.HtmlAnchor ();
  1176. anchor.HRef = "#" + ClientID + "_SkipLink";
  1177. Image img = new Image ();
  1178. ClientScriptManager csm = new ClientScriptManager (null);
  1179. img.ImageUrl = csm.GetWebResourceUrl (typeof (SiteMapPath), "transparent.gif");
  1180. img.Attributes.Add ("height", "0");
  1181. img.Attributes.Add ("width", "0");
  1182. img.AlternateText = SkipLinkText;
  1183. anchor.Controls.Add (img);
  1184. anchor.Render (writer);
  1185. }
  1186. base.RenderBeginTag (writer);
  1187. }
  1188. public override void RenderEndTag (HtmlTextWriter writer)
  1189. {
  1190. base.RenderEndTag (writer);
  1191. if (StaticDisplayLevels == 1 && MaximumDynamicDisplayLevels > 0)
  1192. RenderDynamicMenu (writer, Items);
  1193. if (SkipLinkText != "") {
  1194. System.Web.UI.HtmlControls.HtmlAnchor anchor = new System.Web.UI.HtmlControls.HtmlAnchor ();
  1195. anchor.ID = ClientID + "_SkipLink";
  1196. anchor.Render (writer);
  1197. }
  1198. }
  1199. protected internal override void RenderContents (HtmlTextWriter writer)
  1200. {
  1201. RenderMenuBody (writer, Items, Orientation == Orientation.Vertical, false, false);
  1202. }
  1203. void RenderDynamicMenu (HtmlTextWriter writer, MenuItemCollection items) {
  1204. for (int n = 0; n < items.Count; n++) {
  1205. if (DisplayChildren (items [n])) {
  1206. RenderDynamicMenu (writer, items [n]);
  1207. RenderDynamicMenu (writer, items [n].ChildItems);
  1208. }
  1209. }
  1210. }
  1211. MenuRenderHtmlTemplate _dynamicTemplate;
  1212. MenuRenderHtmlTemplate GetDynamicMenuTemplate (MenuItem item)
  1213. {
  1214. if (_dynamicTemplate != null)
  1215. return _dynamicTemplate;
  1216. _dynamicTemplate = new MenuRenderHtmlTemplate ();
  1217. HtmlTextWriter writer = _dynamicTemplate.GetMenuTemplateWriter ();
  1218. if (Page.Header != null) {
  1219. writer.AddAttribute (HtmlTextWriterAttribute.Class, MenuRenderHtmlTemplate.GetMarker (0));
  1220. }
  1221. else {
  1222. writer.AddAttribute (HtmlTextWriterAttribute.Style, MenuRenderHtmlTemplate.GetMarker (0));
  1223. }
  1224. writer.AddStyleAttribute ("visibility", "hidden");
  1225. writer.AddStyleAttribute ("position", "absolute");
  1226. writer.AddStyleAttribute ("z-index", "1");
  1227. writer.AddStyleAttribute ("left", "0px");
  1228. writer.AddStyleAttribute ("top", "0px");
  1229. writer.AddAttribute ("id", MenuRenderHtmlTemplate.GetMarker (1));
  1230. writer.RenderBeginTag (HtmlTextWriterTag.Div);
  1231. // Up button
  1232. writer.AddAttribute ("id", MenuRenderHtmlTemplate.GetMarker (2));
  1233. writer.AddStyleAttribute ("display", "block");
  1234. writer.AddStyleAttribute ("text-align", "center");
  1235. writer.AddAttribute ("onmouseover", string.Concat ("Menu_OverScrollBtn ('", ClientID, "','", MenuRenderHtmlTemplate.GetMarker (3), "','u')"));
  1236. writer.AddAttribute ("onmouseout", string.Concat ("Menu_OutScrollBtn ('", ClientID, "','", MenuRenderHtmlTemplate.GetMarker (4), "','u')"));
  1237. writer.RenderBeginTag (HtmlTextWriterTag.Div);
  1238. writer.AddAttribute ("src", MenuRenderHtmlTemplate.GetMarker (5)); //src
  1239. writer.AddAttribute ("alt", MenuRenderHtmlTemplate.GetMarker (6)); //ScrollUpText
  1240. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  1241. writer.RenderEndTag (); // IMG
  1242. writer.RenderEndTag (); // DIV scroll button
  1243. writer.AddAttribute ("id", MenuRenderHtmlTemplate.GetMarker (7));
  1244. writer.RenderBeginTag (HtmlTextWriterTag.Div);
  1245. writer.AddAttribute ("id", MenuRenderHtmlTemplate.GetMarker (8));
  1246. writer.RenderBeginTag (HtmlTextWriterTag.Div);
  1247. // call of RenderMenu
  1248. writer.Write (MenuRenderHtmlTemplate.GetMarker (9));
  1249. writer.RenderEndTag (); // DIV Content
  1250. writer.RenderEndTag (); // DIV Scroll container
  1251. // Down button
  1252. writer.AddAttribute ("id", MenuRenderHtmlTemplate.GetMarker (0));
  1253. writer.AddStyleAttribute ("display", "block");
  1254. writer.AddStyleAttribute ("text-align", "center");
  1255. writer.AddAttribute ("onmouseover", string.Concat ("Menu_OverScrollBtn ('", ClientID, "','", MenuRenderHtmlTemplate.GetMarker (1), "','d')"));
  1256. writer.AddAttribute ("onmouseout", string.Concat ("Menu_OutScrollBtn ('", ClientID, "','", MenuRenderHtmlTemplate.GetMarker (2), "','d')"));
  1257. writer.RenderBeginTag (HtmlTextWriterTag.Div);
  1258. writer.AddAttribute ("src", MenuRenderHtmlTemplate.GetMarker (3)); //src
  1259. writer.AddAttribute ("alt", MenuRenderHtmlTemplate.GetMarker (4)); //ScrollDownText
  1260. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  1261. writer.RenderEndTag (); // IMG
  1262. writer.RenderEndTag (); // DIV scroll button
  1263. writer.RenderEndTag (); // DIV menu
  1264. _dynamicTemplate.Parse ();
  1265. return _dynamicTemplate;
  1266. }
  1267. void RenderDynamicMenu (HtmlTextWriter writer, MenuItem item)
  1268. {
  1269. _dynamicTemplate = GetDynamicMenuTemplate (item);
  1270. string idPrefix = ClientID + "_" + item.Path;
  1271. string [] param = new string [9];
  1272. param [0] = GetCssMenuStyle (true, item.Depth + 1);
  1273. param [1] = idPrefix + "s";
  1274. param [2] = idPrefix + "cu";
  1275. param [3] = item.Path;
  1276. param [4] = item.Path;
  1277. param [5] = ScrollUpImageUrl != "" ? ScrollUpImageUrl : Page.ClientScript.GetWebResourceUrl (typeof (Menu), "arrow_up.gif");
  1278. param [6] = ScrollUpText;
  1279. param [7] = idPrefix + "cb";
  1280. param [8] = idPrefix + "cc";
  1281. _dynamicTemplate.RenderTemplate (writer, param, 0, param.Length);
  1282. RenderMenu (writer, item.ChildItems, true, true, item.Depth + 1, false);
  1283. string [] param2 = new string [5];
  1284. param2 [0] = idPrefix + "cd";
  1285. param2 [1] = item.Path;
  1286. param2 [2] = item.Path;
  1287. param2 [3] = ScrollDownImageUrl != "" ? ScrollDownImageUrl : Page.ClientScript.GetWebResourceUrl (typeof (Menu), "arrow_down.gif");
  1288. param2 [4] = ScrollDownText;
  1289. _dynamicTemplate.RenderTemplate (writer, param2, param.Length + 1, param2.Length);
  1290. }
  1291. string GetCssMenuStyle (bool dynamic, int menuLevel)
  1292. {
  1293. if (Page.Header != null) {
  1294. // styles are registered
  1295. StringBuilder sb = new StringBuilder ();
  1296. if (!dynamic && staticMenuStyle != null) {
  1297. sb.Append (staticMenuStyle.CssClass);
  1298. sb.Append (' ');
  1299. sb.Append (staticMenuStyle.RegisteredCssClass);
  1300. }
  1301. if (dynamic && dynamicMenuStyle != null) {
  1302. sb.Append (PopOutBoxStyle.RegisteredCssClass);
  1303. sb.Append (' ');
  1304. sb.Append (dynamicMenuStyle.CssClass);
  1305. sb.Append (' ');
  1306. sb.Append (dynamicMenuStyle.RegisteredCssClass);
  1307. }
  1308. if (levelSubMenuStyles != null && levelSubMenuStyles.Count > menuLevel) {
  1309. sb.Append (levelSubMenuStyles [menuLevel].CssClass);
  1310. sb.Append (' ');
  1311. sb.Append (levelSubMenuStyles [menuLevel].RegisteredCssClass);
  1312. }
  1313. return sb.ToString ();
  1314. }
  1315. else {
  1316. // styles are not registered
  1317. SubMenuStyle style = new SubMenuStyle ();
  1318. if (!dynamic && staticMenuStyle != null) {
  1319. style.CopyFrom (staticMenuStyle);
  1320. }
  1321. if (dynamic && dynamicMenuStyle != null) {
  1322. style.CopyFrom (PopOutBoxStyle);
  1323. style.CopyFrom (dynamicMenuStyle);
  1324. }
  1325. if (levelSubMenuStyles != null && levelSubMenuStyles.Count > menuLevel) {
  1326. style.CopyFrom (levelSubMenuStyles [menuLevel]);
  1327. }
  1328. return style.GetStyleAttributes (null).Value;
  1329. }
  1330. }
  1331. void RenderMenuBeginTagAttributes (HtmlTextWriter writer, bool dynamic, int menuLevel) {
  1332. writer.AddAttribute ("cellpadding", "0", false);
  1333. writer.AddAttribute ("cellspacing", "0", false);
  1334. writer.AddAttribute ("border", "0", false);
  1335. if (!dynamic) {
  1336. SubMenuStyle style = new SubMenuStyle ();
  1337. FillMenuStyle (dynamic, menuLevel, style);
  1338. style.AddAttributesToRender (writer);
  1339. }
  1340. }
  1341. void FillMenuStyle (bool dynamic, int menuLevel, SubMenuStyle style) {
  1342. if (Page.Header != null) {
  1343. // styles are registered
  1344. if (!dynamic && staticMenuStyle != null) {
  1345. AddCssClass (style, staticMenuStyle.CssClass);
  1346. AddCssClass (style, staticMenuStyle.RegisteredCssClass);
  1347. }
  1348. if (dynamic && dynamicMenuStyle != null) {
  1349. AddCssClass (style, dynamicMenuStyle.CssClass);
  1350. AddCssClass (style, dynamicMenuStyle.RegisteredCssClass);
  1351. }
  1352. if (levelSubMenuStyles != null && levelSubMenuStyles.Count > menuLevel) {
  1353. AddCssClass (style, levelSubMenuStyles [menuLevel].CssClass);
  1354. AddCssClass (style, levelSubMenuStyles [menuLevel].RegisteredCssClass);
  1355. }
  1356. }
  1357. else {
  1358. // styles are not registered
  1359. if (!dynamic && staticMenuStyle != null) {
  1360. style.CopyFrom (staticMenuStyle);
  1361. }
  1362. if (dynamic && dynamicMenuStyle != null) {
  1363. style.CopyFrom (dynamicMenuStyle);
  1364. }
  1365. if (levelSubMenuStyles != null && levelSubMenuStyles.Count > menuLevel) {
  1366. style.CopyFrom (levelSubMenuStyles [menuLevel]);
  1367. }
  1368. }
  1369. }
  1370. void RenderMenu (HtmlTextWriter writer, MenuItemCollection items, bool vertical, bool dynamic, int menuLevel, bool notLast)
  1371. {
  1372. RenderMenuBeginTag (writer, dynamic, menuLevel);
  1373. RenderMenuBody (writer, items, vertical, dynamic, notLast);
  1374. RenderMenuEndTag (writer);
  1375. }
  1376. void RenderMenuBeginTag (HtmlTextWriter writer, bool dynamic, int menuLevel)
  1377. {
  1378. RenderMenuBeginTagAttributes (writer, dynamic, menuLevel);
  1379. writer.RenderBeginTag (HtmlTextWriterTag.Table);
  1380. }
  1381. void RenderMenuEndTag (HtmlTextWriter writer)
  1382. {
  1383. writer.RenderEndTag ();
  1384. }
  1385. void RenderMenuBody (HtmlTextWriter writer, MenuItemCollection items, bool vertical, bool dynamic, bool notLast) {
  1386. if (!vertical)
  1387. writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  1388. int count = items.Count;
  1389. for (int n = 0; n < count; n++) {
  1390. MenuItem item = items [n];
  1391. Adapters.MenuAdapter adapter = Adapter as Adapters.MenuAdapter;
  1392. if (adapter != null)
  1393. adapter.RenderItem (writer, item, n);
  1394. else
  1395. RenderMenuItem (writer, item, (n + 1 == count) ? notLast : true, n == 0);
  1396. }
  1397. if (!vertical)
  1398. writer.RenderEndTag (); // TR
  1399. }
  1400. void RenderMenuItemSpacing (HtmlTextWriter writer, Unit itemSpacing, bool vertical) {
  1401. if (vertical) {
  1402. writer.AddStyleAttribute ("height", itemSpacing.ToString ());
  1403. writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  1404. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  1405. writer.RenderEndTag ();
  1406. writer.RenderEndTag ();
  1407. }
  1408. else {
  1409. writer.AddStyleAttribute ("width", itemSpacing.ToString ());
  1410. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  1411. writer.RenderEndTag ();
  1412. }
  1413. }
  1414. bool IsDynamicItem (MenuItem item) {
  1415. return item.Depth + 1 > StaticDisplayLevels;
  1416. }
  1417. bool DisplayChildren (MenuItem item) {
  1418. return (item.Depth + 1 < StaticDisplayLevels + MaximumDynamicDisplayLevels) && item.ChildItems.Count > 0;
  1419. }
  1420. internal void RenderItem (HtmlTextWriter writer, MenuItem item, int position) {
  1421. // notLast should be true if item or any of its ancestors is not a
  1422. // last child.
  1423. bool notLast = false;
  1424. MenuItem parent;
  1425. MenuItem child = item;
  1426. while (null != (parent = child.Parent)) {
  1427. if (child.Index != parent.ChildItems.Count - 1) {
  1428. notLast = true;
  1429. break;
  1430. }
  1431. child = parent;
  1432. }
  1433. RenderMenuItem (writer, item, notLast, position == 0);
  1434. }
  1435. void RenderMenuItem (HtmlTextWriter writer, MenuItem item, bool notLast, bool isFirst) {
  1436. bool displayChildren = DisplayChildren (item);
  1437. bool dynamicChildren = displayChildren && (item.Depth + 1 >= StaticDisplayLevels);
  1438. bool isDynamicItem = IsDynamicItem (item);
  1439. bool vertical = (Orientation == Orientation.Vertical) || isDynamicItem;
  1440. Unit itemSpacing = GetItemSpacing (item, isDynamicItem);
  1441. if (itemSpacing != Unit.Empty && (item.Depth > 0 || !isFirst))
  1442. RenderMenuItemSpacing (writer, itemSpacing, vertical);
  1443. if(!String.IsNullOrEmpty(item.ToolTip))
  1444. writer.AddAttribute (HtmlTextWriterAttribute.Title, item.ToolTip);
  1445. if (vertical)
  1446. writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  1447. string parentId = isDynamicItem ? "'" + item.Parent.Path + "'" : "null";
  1448. if (dynamicChildren) {
  1449. writer.AddAttribute ("onmouseover",
  1450. "javascript:Menu_OverItem ('" + ClientID + "','" + item.Path + "'," + parentId + ")");
  1451. writer.AddAttribute ("onmouseout",
  1452. "javascript:Menu_OutItem ('" + ClientID + "','" + item.Path + "')");
  1453. } else if (isDynamicItem) {
  1454. writer.AddAttribute ("onmouseover",
  1455. "javascript:Menu_OverDynamicLeafItem ('" + ClientID + "','" + item.Path + "'," + parentId + ")");
  1456. writer.AddAttribute ("onmouseout",
  1457. "javascript:Menu_OutItem ('" + ClientID + "','" + item.Path + "'," + parentId + ")");
  1458. } else {
  1459. writer.AddAttribute ("onmouseover",
  1460. "javascript:Menu_OverStaticLeafItem ('" + ClientID + "','" + item.Path + "')");
  1461. writer.AddAttribute ("onmouseout",
  1462. "javascript:Menu_OutItem ('" + ClientID + "','" + item.Path + "')");
  1463. }
  1464. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  1465. // Top separator image
  1466. if (isDynamicItem && DynamicTopSeparatorImageUrl != "") {
  1467. writer.AddAttribute ("src", ResolveClientUrl (DynamicTopSeparatorImageUrl));
  1468. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  1469. writer.RenderEndTag (); // IMG
  1470. }
  1471. else if (!isDynamicItem && StaticTopSeparatorImageUrl != "") {
  1472. writer.AddAttribute ("src", ResolveClientUrl (StaticTopSeparatorImageUrl));
  1473. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  1474. writer.RenderEndTag (); // IMG
  1475. }
  1476. // Menu item box
  1477. MenuItemStyle style = new MenuItemStyle ();
  1478. if (Page.Header != null) {
  1479. // styles are registered
  1480. if (!isDynamicItem && staticMenuItemStyle != null) {
  1481. AddCssClass (style, staticMenuItemStyle.CssClass);
  1482. AddCssClass (style, staticMenuItemStyle.RegisteredCssClass);
  1483. }
  1484. if (isDynamicItem && dynamicMenuItemStyle != null) {
  1485. AddCssClass (style, dynamicMenuItemStyle.CssClass);
  1486. AddCssClass (style, dynamicMenuItemStyle.RegisteredCssClass);
  1487. }
  1488. if (levelMenuItemStyles != null && levelMenuItemStyles.Count > item.Depth) {
  1489. AddCssClass (style, levelMenuItemStyles [item.Depth].CssClass);
  1490. AddCssClass (style, levelMenuItemStyles [item.Depth].RegisteredCssClass);
  1491. }
  1492. if (item == SelectedItem) {
  1493. if (!isDynamicItem && staticSelectedStyle != null) {
  1494. AddCssClass (style, staticSelectedStyle.CssClass);
  1495. AddCssClass (style, staticSelectedStyle.RegisteredCssClass);
  1496. }
  1497. if (isDynamicItem && dynamicSelectedStyle != null) {
  1498. AddCssClass (style, dynamicSelectedStyle.CssClass);
  1499. AddCssClass (style, dynamicSelectedStyle.RegisteredCssClass);
  1500. }
  1501. if (levelSelectedStyles != null && levelSelectedStyles.Count > item.Depth) {
  1502. AddCssClass (style, levelSelectedStyles [item.Depth].CssClass);
  1503. AddCssClass (style, levelSelectedStyles [item.Depth].RegisteredCssClass);
  1504. }
  1505. }
  1506. }
  1507. else {
  1508. // styles are not registered
  1509. if (!isDynamicItem && staticMenuItemStyle != null) {
  1510. style.CopyFrom (staticMenuItemStyle);
  1511. }
  1512. if (isDynamicItem && dynamicMenuItemStyle != null) {
  1513. style.CopyFrom (dynamicMenuItemStyle);
  1514. }
  1515. if (levelMenuItemStyles != null && levelMenuItemStyles.Count > item.Depth) {
  1516. style.CopyFrom (levelMenuItemStyles [item.Depth]);
  1517. }
  1518. if (item == SelectedItem) {
  1519. if (!isDynamicItem && staticSelectedStyle != null) {
  1520. style.CopyFrom (staticSelectedStyle);
  1521. }
  1522. if (isDynamicItem && dynamicSelectedStyle != null) {
  1523. style.CopyFrom (dynamicSelectedStyle);
  1524. }
  1525. if (levelSelectedStyles != null && levelSelectedStyles.Count > item.Depth) {
  1526. style.CopyFrom (levelSelectedStyles [item.Depth]);
  1527. }
  1528. }
  1529. }
  1530. style.AddAttributesToRender (writer);
  1531. writer.AddAttribute ("id", GetItemClientId (item, "i"));
  1532. writer.AddAttribute ("cellpadding", "0", false);
  1533. writer.AddAttribute ("cellspacing", "0", false);
  1534. writer.AddAttribute ("border", "0", false);
  1535. writer.AddAttribute ("width", "100%", false);
  1536. writer.RenderBeginTag (HtmlTextWriterTag.Table);
  1537. writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  1538. // Menu item text
  1539. if (vertical)
  1540. writer.AddStyleAttribute (HtmlTextWriterStyle.Width, "100%");
  1541. if (!ItemWrap)
  1542. writer.AddStyleAttribute ("white-space", "nowrap");
  1543. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  1544. RenderItemHref (writer, item);
  1545. Style linkStyle = new Style ();
  1546. if (Page.Header != null) {
  1547. // styles are registered
  1548. AddCssClass (linkStyle, ControlLinkStyle.RegisteredCssClass);
  1549. if (!isDynamicItem && staticMenuItemStyle != null) {
  1550. AddCssClass (linkStyle, staticMenuItemStyle.CssClass);
  1551. AddCssClass (linkStyle, staticMenuItemLinkStyle.RegisteredCssClass);
  1552. }
  1553. if (isDynamicItem && dynamicMenuItemStyle != null) {
  1554. AddCssClass (linkStyle, dynamicMenuItemStyle.CssClass);
  1555. AddCssClass (linkStyle, dynamicMenuItemLinkStyle.RegisteredCssClass);
  1556. }
  1557. if (levelMenuItemStyles != null && levelMenuItemStyles.Count > item.Depth) {
  1558. AddCssClass (linkStyle, levelMenuItemStyles [item.Depth].CssClass);
  1559. AddCssClass (linkStyle, levelMenuItemLinkStyles [item.Depth].RegisteredCssClass);
  1560. }
  1561. if (item == SelectedItem) {
  1562. if (!isDynamicItem && staticSelectedStyle != null) {
  1563. AddCssClass (linkStyle, staticSelectedStyle.CssClass);
  1564. AddCssClass (linkStyle, staticSelectedLinkStyle.RegisteredCssClass);
  1565. }
  1566. if (isDynamicItem && dynamicSelectedStyle != null) {
  1567. AddCssClass (linkStyle, dynamicSelectedStyle.CssClass);
  1568. AddCssClass (linkStyle, dynamicSelectedLinkStyle.RegisteredCssClass);
  1569. }
  1570. if (levelSelectedStyles != null && levelSelectedStyles.Count > item.Depth) {
  1571. AddCssClass (linkStyle, levelSelectedStyles [item.Depth].CssClass);
  1572. AddCssClass (linkStyle, levelSelectedLinkStyles [item.Depth].RegisteredCssClass);
  1573. }
  1574. }
  1575. }
  1576. else {
  1577. // styles are not registered
  1578. linkStyle.CopyFrom (ControlLinkStyle);
  1579. if (!isDynamicItem && staticMenuItemStyle != null) {
  1580. linkStyle.CopyFrom (staticMenuItemLinkStyle);
  1581. }
  1582. if (isDynamicItem && dynamicMenuItemStyle != null) {
  1583. linkStyle.CopyFrom (dynamicMenuItemLinkStyle);
  1584. }
  1585. if (levelMenuItemStyles != null && levelMenuItemStyles.Count > item.Depth) {
  1586. linkStyle.CopyFrom (levelMenuItemLinkStyles [item.Depth]);
  1587. }
  1588. if (item == SelectedItem) {
  1589. if (!isDynamicItem && staticSelectedStyle != null) {
  1590. linkStyle.CopyFrom (staticSelectedLinkStyle);
  1591. }
  1592. if (isDynamicItem && dynamicSelectedStyle != null) {
  1593. linkStyle.CopyFrom (dynamicSelectedLinkStyle);
  1594. }
  1595. if (levelSelectedStyles != null && levelSelectedStyles.Count > item.Depth) {
  1596. linkStyle.CopyFrom (levelSelectedLinkStyles [item.Depth]);
  1597. }
  1598. }
  1599. linkStyle.AlwaysRenderTextDecoration = true;
  1600. }
  1601. linkStyle.AddAttributesToRender (writer);
  1602. writer.AddAttribute ("id", GetItemClientId (item, "l"));
  1603. if (item.Depth > 0 && !isDynamicItem) {
  1604. Unit indent = new Unit (StaticSubMenuIndent.Value * item.Depth, StaticSubMenuIndent.Type);
  1605. writer.AddStyleAttribute ("margin-left", indent.ToString ());
  1606. }
  1607. writer.RenderBeginTag (HtmlTextWriterTag.A);
  1608. RenderItemContent (writer, item, isDynamicItem);
  1609. writer.RenderEndTag (); // A
  1610. writer.RenderEndTag (); // TD
  1611. // Popup image
  1612. if (dynamicChildren) {
  1613. string popOutImage = GetPopOutImage (item, isDynamicItem);
  1614. if (popOutImage != null) {
  1615. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  1616. writer.AddAttribute ("src", ResolveClientUrl (popOutImage));
  1617. writer.AddAttribute ("border", "0");
  1618. string toolTip = String.Format (isDynamicItem ? DynamicPopOutImageTextFormatString : StaticPopOutImageTextFormatString, item.Text);
  1619. writer.AddAttribute (HtmlTextWriterAttribute.Alt, toolTip);
  1620. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  1621. writer.RenderEndTag (); // IMG
  1622. writer.RenderEndTag (); // TD
  1623. }
  1624. }
  1625. writer.RenderEndTag (); // TR
  1626. writer.RenderEndTag (); // TABLE
  1627. writer.RenderEndTag (); // TD
  1628. if (!vertical && itemSpacing == Unit.Empty && (notLast || (displayChildren && !dynamicChildren))) {
  1629. writer.AddStyleAttribute ("width", "3px");
  1630. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  1631. writer.RenderEndTag ();
  1632. }
  1633. // Bottom separator image
  1634. string separatorImg = item.SeparatorImageUrl;
  1635. if (separatorImg.Length == 0) {
  1636. if (isDynamicItem)
  1637. separatorImg = DynamicBottomSeparatorImageUrl;
  1638. else
  1639. separatorImg = StaticBottomSeparatorImageUrl;
  1640. }
  1641. if (separatorImg.Length > 0) {
  1642. if (!vertical)
  1643. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  1644. writer.AddAttribute ("src", ResolveClientUrl (separatorImg));
  1645. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  1646. writer.RenderEndTag (); // IMG
  1647. if (!vertical)
  1648. writer.RenderEndTag (); // TD
  1649. }
  1650. if (vertical)
  1651. writer.RenderEndTag (); // TR
  1652. if (itemSpacing != Unit.Empty)
  1653. RenderMenuItemSpacing (writer, itemSpacing, vertical);
  1654. // Submenu
  1655. if (displayChildren && !dynamicChildren) {
  1656. if (vertical)
  1657. writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  1658. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  1659. writer.AddAttribute ("width", "100%");
  1660. RenderMenu (writer, item.ChildItems, Orientation == Orientation.Vertical, false, item.Depth + 1, notLast);
  1661. if (item.Depth + 2 == StaticDisplayLevels)
  1662. RenderDynamicMenu (writer, item.ChildItems);
  1663. writer.RenderEndTag (); // TD
  1664. if (vertical)
  1665. writer.RenderEndTag (); // TR
  1666. }
  1667. }
  1668. void RenderItemContent (HtmlTextWriter writer, MenuItem item, bool isDynamicItem) {
  1669. if (_menuItemControls!=null && _menuItemControls [item] != null) {
  1670. ((Control) _menuItemControls [item]).Render (writer);
  1671. }
  1672. else {
  1673. if (!String.IsNullOrEmpty (item.ImageUrl)) {
  1674. writer.AddAttribute (HtmlTextWriterAttribute.Src, ResolveClientUrl (item.ImageUrl));
  1675. writer.AddAttribute (HtmlTextWriterAttribute.Alt, item.ToolTip);
  1676. writer.AddStyleAttribute (HtmlTextWriterStyle.BorderStyle, "none");
  1677. writer.AddStyleAttribute (HtmlTextWriterStyle.VerticalAlign, "middle");
  1678. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  1679. writer.RenderEndTag (); // IMG
  1680. }
  1681. if (isDynamicItem && DynamicItemFormatString.Length > 0) {
  1682. writer.Write (String.Format (DynamicItemFormatString, item.Text));
  1683. }
  1684. else if (!isDynamicItem && StaticItemFormatString.Length > 0) {
  1685. writer.Write (String.Format (StaticItemFormatString, item.Text));
  1686. }
  1687. else {
  1688. writer.Write (item.Text);
  1689. }
  1690. }
  1691. }
  1692. Unit GetItemSpacing (MenuItem item, bool dynamic)
  1693. {
  1694. Unit itemSpacing = Unit.Empty;
  1695. if (item.Selected) {
  1696. if (levelSelectedStyles != null && item.Depth < levelSelectedStyles.Count) {
  1697. itemSpacing = levelSelectedStyles [item.Depth].ItemSpacing;
  1698. if (itemSpacing != Unit.Empty) return itemSpacing;
  1699. }
  1700. if (dynamic && dynamicSelectedStyle != null)
  1701. itemSpacing = dynamicSelectedStyle.ItemSpacing;
  1702. else if (!dynamic && staticSelectedStyle != null)
  1703. itemSpacing = staticSelectedStyle.ItemSpacing;
  1704. if (itemSpacing != Unit.Empty)
  1705. return itemSpacing;
  1706. }
  1707. if (levelMenuItemStyles != null && item.Depth < levelMenuItemStyles.Count) {
  1708. itemSpacing = levelMenuItemStyles [item.Depth].ItemSpacing;
  1709. if (itemSpacing != Unit.Empty) return itemSpacing;
  1710. }
  1711. if (dynamic && dynamicMenuItemStyle != null)
  1712. return dynamicMenuItemStyle.ItemSpacing;
  1713. else if (!dynamic && staticMenuItemStyle != null)
  1714. return staticMenuItemStyle.ItemSpacing;
  1715. else
  1716. return Unit.Empty;
  1717. }
  1718. string GetPopOutImage (MenuItem item, bool isDynamicItem)
  1719. {
  1720. if (item.PopOutImageUrl != "")
  1721. return item.PopOutImageUrl;
  1722. if (isDynamicItem) {
  1723. if (DynamicPopOutImageUrl != "")
  1724. return DynamicPopOutImageUrl;
  1725. if (DynamicEnableDefaultPopOutImage)
  1726. return Page.ClientScript.GetWebResourceUrl (typeof (Menu), "arrow_plus.gif");
  1727. } else {
  1728. if (StaticPopOutImageUrl != "")
  1729. return StaticPopOutImageUrl;
  1730. if (StaticEnableDefaultPopOutImage)
  1731. return Page.ClientScript.GetWebResourceUrl (typeof (Menu), "arrow_plus.gif");
  1732. }
  1733. return null;
  1734. }
  1735. void RenderItemHref (HtmlTextWriter writer, MenuItem item)
  1736. {
  1737. if (!item.BranchEnabled) {
  1738. writer.AddAttribute ("disabled", "true", false);
  1739. }
  1740. else if (!item.Selectable) {
  1741. writer.AddAttribute ("href", "#", false);
  1742. writer.AddStyleAttribute ("cursor", "text");
  1743. }
  1744. else if (item.NavigateUrl != "") {
  1745. string target = item.Target != "" ? item.Target : Target;
  1746. #if TARGET_J2EE
  1747. string navUrl = ResolveClientUrl (item.NavigateUrl, String.Compare (target, "_blank", StringComparison.InvariantCultureIgnoreCase) != 0);
  1748. #else
  1749. string navUrl = ResolveClientUrl (item.NavigateUrl);
  1750. #endif
  1751. writer.AddAttribute ("href", navUrl);
  1752. if (target != "")
  1753. writer.AddAttribute ("target", target);
  1754. }
  1755. else {
  1756. writer.AddAttribute ("href", GetClientEvent (item));
  1757. }
  1758. }
  1759. string GetItemClientId (MenuItem item, string sufix)
  1760. {
  1761. return ClientID + "_" + item.Path + sufix;
  1762. }
  1763. string GetClientEvent (MenuItem item)
  1764. {
  1765. return Page.ClientScript.GetPostBackClientHyperlink (this, item.Path, true);
  1766. }
  1767. class MenuTemplateWriter : TextWriter
  1768. {
  1769. char [] _buffer;
  1770. int _ptr = 0;
  1771. public MenuTemplateWriter (char [] buffer)
  1772. {
  1773. _buffer = buffer;
  1774. }
  1775. public override Encoding Encoding
  1776. {
  1777. get { return Encoding.Unicode; }
  1778. }
  1779. public override void Write (char value)
  1780. {
  1781. if (_ptr == _buffer.Length)
  1782. EnsureCapacity ();
  1783. _buffer [_ptr++] = value;
  1784. }
  1785. public override void Write (string value)
  1786. {
  1787. if (value == null)
  1788. return;
  1789. if (_ptr + value.Length >= _buffer.Length)
  1790. EnsureCapacity ();
  1791. for (int i = 0; i < value.Length; i++)
  1792. _buffer [_ptr++] = value [i];
  1793. }
  1794. void EnsureCapacity ()
  1795. {
  1796. char [] tmpBuffer = new char [_buffer.Length * 2];
  1797. Array.Copy (_buffer, tmpBuffer, _buffer.Length);
  1798. _buffer = tmpBuffer;
  1799. }
  1800. }
  1801. class MenuRenderHtmlTemplate
  1802. {
  1803. public const string Marker = "\u093a\u093b\u0971";
  1804. char [] _templateHtml;
  1805. MenuTemplateWriter _templateWriter;
  1806. ArrayList idxs = new ArrayList (32);
  1807. public MenuRenderHtmlTemplate ()
  1808. {
  1809. _templateHtml = new char [1024];
  1810. _templateWriter = new MenuTemplateWriter (_templateHtml);
  1811. }
  1812. public static string GetMarker (int num)
  1813. {
  1814. char charNum = (char) ((int) '\u0971' + num);
  1815. return string.Concat (Marker, charNum);
  1816. }
  1817. public HtmlTextWriter GetMenuTemplateWriter()
  1818. {
  1819. return new HtmlTextWriter (_templateWriter);
  1820. }
  1821. public void Parse ()
  1822. {
  1823. int mpos = 0;
  1824. for (int i = 0; i < _templateHtml.Length; i++) {
  1825. if (_templateHtml [i] == '\0') {
  1826. idxs.Add (i);
  1827. break;
  1828. }
  1829. if (_templateHtml [i] != Marker [mpos]) {
  1830. mpos = 0;
  1831. continue;
  1832. }
  1833. mpos++;
  1834. if (mpos == Marker.Length) {
  1835. mpos = 0;
  1836. idxs.Add (i - Marker.Length + 1);
  1837. }
  1838. }
  1839. }
  1840. public void RenderTemplate (HtmlTextWriter writer, string [] dynamicParts, int start, int count)
  1841. {
  1842. if (idxs.Count == 0)
  1843. return;
  1844. int partStart = 0;
  1845. int partEnd = (start == 0) ? -Marker.Length - 1 : (int) idxs [start - 1];
  1846. int di = 0;
  1847. int i = start;
  1848. int total = start + count;
  1849. for (; i < total; i++) {
  1850. partStart = partEnd + Marker.Length + 1;
  1851. partEnd = (int) idxs [i];
  1852. // write static part
  1853. writer.Write (_templateHtml, partStart, partEnd - partStart);
  1854. // write synamic part
  1855. di = (int) _templateHtml [partEnd + Marker.Length] - 0x971;
  1856. writer.Write (dynamicParts [di]);
  1857. }
  1858. partStart = partEnd + Marker.Length + 1;
  1859. partEnd = (int) idxs [i];
  1860. writer.Write (_templateHtml, partStart, partEnd - partStart);
  1861. }
  1862. }
  1863. }
  1864. }
  1865. #endif