Menu.cs 64 KB

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