PopupManager.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Controls.Primitives;
  5. using System.Windows.Input;
  6. namespace MuchInfo.Chart.Infrastructure.Helpers
  7. {
  8. public class PopupManager
  9. {
  10. #region Fields
  11. //public static Panel PopupRoot { get; set; }
  12. #endregion Fields
  13. #region Enumerations
  14. public enum RelativePositionHoriz
  15. {
  16. Left,
  17. FlushRights,
  18. FlushLefts,
  19. Right
  20. }
  21. public enum RelativePositionVert
  22. {
  23. Above,
  24. FlushBottoms,
  25. FlushTops,
  26. Below
  27. }
  28. #endregion Enumerations
  29. #region Methods
  30. private static Popup CreatePopup()
  31. {
  32. return new Popup
  33. {
  34. AllowsTransparency = true,
  35. Placement = PlacementMode.Relative,
  36. };
  37. }
  38. #region Public Static Methods
  39. public static Popup CreateAppOverlay(Panel popupRoot)
  40. {
  41. var popup = CreatePopup();
  42. var popupManager = new PopupManager.AppOverlayPopup();
  43. popupManager.BuildPopup(popup, popupRoot);
  44. return popup;
  45. }
  46. public static Popup CreateAppOverlay(MouseEventArgs position, Panel popupRoot)
  47. {
  48. var popup = CreatePopup();
  49. var popupManager = new PopupManager.AppOverlayPopup();
  50. popupManager.BuildPopup(popup, popupRoot);
  51. bool flag = position != null;
  52. if (flag)
  53. {
  54. var p1 = position.GetPosition(popupRoot);
  55. popup.HorizontalOffset = (p1.X);
  56. popup.VerticalOffset = (p1.Y);
  57. }
  58. return popup;
  59. }
  60. public static Popup CreateOverlay(Panel popupRoot)
  61. {
  62. var popup = CreatePopup();
  63. var popupManager = new PopupManager.OverlayPopup();
  64. popupManager.BuildPopup(popup, popupRoot);
  65. return popup;
  66. }
  67. public static Popup CreateOverlay(MouseEventArgs position, Panel popupRoot)
  68. {
  69. var popup = CreatePopup();
  70. var popupManager = new PopupManager.OverlayPopup();
  71. popupManager.BuildPopup(popup, popupRoot);
  72. bool flag = position != null;
  73. if (flag)
  74. {
  75. var p1 = position.GetPosition(popupRoot);
  76. popup.HorizontalOffset = (p1.X);
  77. popup.VerticalOffset = (p1.Y);
  78. }
  79. return popup;
  80. }
  81. //public static Popup CreatePopUp(MouseEventArgs position, Panel popupRoot)
  82. //{
  83. // var popup = new Popup();
  84. // bool flag = position != null;
  85. // if (flag)
  86. // {
  87. // var currentPosition = position.GetPosition(popupRoot);
  88. // popup.HorizontalOffset = (currentPosition.X);
  89. // popup.VerticalOffset = (currentPosition.Y);
  90. // }
  91. // popup.Closed += (new EventHandler(PopupManager.PopUpManager_2090));
  92. // popupRoot.Children.Add(popup);
  93. // return popup;
  94. //}
  95. //public static Popup CreatePopUp(Panel popupRoot)
  96. //{
  97. // var popup = new Popup();
  98. // popup.Closed += (new EventHandler(PopupManager.PopUpManager_2090));
  99. // popupRoot.Children.Add(popup);
  100. // return popup;
  101. //}
  102. public static void MovePopup(Popup aPop, FrameworkElement relativeTo, PopupManager.RelativePositionHoriz positionLeft, PopupManager.RelativePositionVert positionTop, Panel popupRoot)
  103. {
  104. aPop.UpdateLayout();
  105. bool flag = aPop.Child != null;
  106. if (flag)
  107. {
  108. aPop.Child.UpdateLayout();
  109. }
  110. Point point = GeometryHelper.PositionOfControlRelative(relativeTo, popupRoot);
  111. switch (positionLeft)
  112. {
  113. case PopupManager.RelativePositionHoriz.Left:
  114. point.X = (point.X - aPop.ActualWidth);
  115. break;
  116. case PopupManager.RelativePositionHoriz.FlushRights:
  117. point.X = (point.X - (aPop.ActualWidth - relativeTo.ActualWidth));
  118. break;
  119. case PopupManager.RelativePositionHoriz.Right:
  120. point.X = (point.X + relativeTo.ActualWidth);
  121. break;
  122. }
  123. switch (positionTop)
  124. {
  125. case PopupManager.RelativePositionVert.Above:
  126. point.Y = (point.Y - aPop.ActualHeight);
  127. break;
  128. case PopupManager.RelativePositionVert.FlushBottoms:
  129. point.Y = (point.Y - (aPop.ActualHeight - relativeTo.ActualHeight));
  130. break;
  131. case PopupManager.RelativePositionVert.Below:
  132. point.Y = (point.Y + relativeTo.ActualHeight);
  133. break;
  134. }
  135. aPop.HorizontalOffset = (point.X);
  136. aPop.VerticalOffset = (point.Y);
  137. }
  138. //public static void SetPopUpRoot(UIElement root)
  139. //{
  140. // bool flag = root is Panel;
  141. // if (flag)
  142. // {
  143. // PopupManager.PopupRoot = (Panel)root;
  144. // return;
  145. // }
  146. // throw new Exception("Root is not a panel");
  147. //}
  148. #endregion Public Static Methods
  149. #region Private Static Methods
  150. //private static void PopUpManager_2090(object sender, System.EventArgs e)
  151. //{
  152. // bool flag = sender is Popup;
  153. // if (flag)
  154. // {
  155. // var popup = (Popup)sender;
  156. // try
  157. // {
  158. // popup.Child = (null);
  159. // popup.Closed -= (new EventHandler(PopupManager.PopUpManager_2090));
  160. // PopupManager.PopupRoot.Children.Remove(popup);
  161. // }
  162. // catch (Exception expr_59)
  163. // {
  164. // ProjectData.SetProjectError(expr_59);
  165. // ProjectData.ClearProjectError();
  166. // }
  167. // }
  168. //}
  169. #endregion Private Static Methods
  170. #endregion Methods
  171. #region Nested Types
  172. private sealed class AppOverlayPopup
  173. {
  174. #region Fields
  175. private Popup _popup;
  176. private Panel _panel;
  177. #endregion Fields
  178. #region Properties
  179. #region Public Properties
  180. private Popup Popup
  181. {
  182. get
  183. {
  184. return this._popup;
  185. }
  186. set
  187. {
  188. var sizeChangedEventHandler = new SizeChangedEventHandler(this.Popup_SizeChanged);
  189. var eventHandler = new EventHandler(this.Popup_Closed);
  190. bool flag = this._popup != null;
  191. if (flag)
  192. {
  193. this._popup.SizeChanged -= (sizeChangedEventHandler);
  194. this._popup.Closed -= (eventHandler);
  195. }
  196. this._popup = value;
  197. flag = (this._popup != null);
  198. if (flag)
  199. {
  200. this._popup.SizeChanged += (sizeChangedEventHandler);
  201. this._popup.Closed += (eventHandler);
  202. }
  203. }
  204. }
  205. private Panel Root
  206. {
  207. get
  208. {
  209. return this._panel;
  210. }
  211. set
  212. {
  213. var mouseButtonEventHandler = new MouseButtonEventHandler(this.Panel_MouseLeftButtonDown);
  214. var routedEventHandler = new RoutedEventHandler(this.Panel_GotFocus);
  215. bool flag = this._panel != null;
  216. if (flag)
  217. {
  218. this._panel.MouseLeftButtonDown -= (mouseButtonEventHandler);
  219. this._panel.GotFocus -= (routedEventHandler);
  220. }
  221. this._panel = value;
  222. flag = (this._panel != null);
  223. if (flag)
  224. {
  225. this._panel.MouseLeftButtonDown += (mouseButtonEventHandler);
  226. this._panel.GotFocus += (routedEventHandler);
  227. }
  228. }
  229. }
  230. #endregion Public Properties
  231. #endregion Properties
  232. #region Methods
  233. #region Internal Methods
  234. internal void BuildPopup(Popup aPop, Panel root)
  235. {
  236. this.Root = root;
  237. this.Popup = aPop;
  238. root.Children.Add(this.Popup);
  239. }
  240. #endregion Internal Methods
  241. #region Private Methods
  242. private void Popup_Closed(object sender, System.EventArgs e)
  243. {
  244. this.Root.Children.Remove(this.Popup);
  245. this.Popup.Child = (null);
  246. this.Root = null;
  247. this.Popup = null;
  248. }
  249. private void Panel_GotFocus(object sender, RoutedEventArgs e)
  250. {
  251. this.Popup.IsOpen = false;
  252. }
  253. private void Panel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  254. {
  255. this.Popup.IsOpen = false;
  256. }
  257. private void Popup_SizeChanged(object sender, SizeChangedEventArgs e)
  258. {
  259. GeometryHelper.EnsureControlInApplication(this.Popup, this.Root);
  260. }
  261. #endregion Private Methods
  262. #endregion Methods
  263. }
  264. private sealed class OverlayPopup
  265. {
  266. #region Fields
  267. private Popup _popup;
  268. private Panel _panel;
  269. #endregion Fields
  270. #region Constructors
  271. public OverlayPopup()
  272. {
  273. //this.MainGrid = new Grid();
  274. }
  275. #endregion Constructors
  276. #region Properties
  277. #region Public Properties
  278. //private Grid MainGrid
  279. //{
  280. // get
  281. // {
  282. // return this._mainGrid;
  283. // }
  284. // set
  285. // {
  286. // var mouseButtonEventHandler = new MouseButtonEventHandler(MainGrid_MouseLeftButtonDown);
  287. // bool flag = this._mainGrid != null;
  288. // if (flag)
  289. // {
  290. // this._mainGrid.MouseLeftButtonDown -= (mouseButtonEventHandler);
  291. // }
  292. // this._mainGrid = value;
  293. // flag = (this._mainGrid != null);
  294. // if (flag)
  295. // {
  296. // this._mainGrid.MouseLeftButtonDown += (mouseButtonEventHandler);
  297. // }
  298. // }
  299. //}
  300. //private Popup BasePopup { get; set; }
  301. private Popup Popup
  302. {
  303. get
  304. {
  305. return this._popup;
  306. }
  307. set
  308. {
  309. var eventHandler = new EventHandler(this.Popup_Closed);
  310. bool flag = this._popup != null;
  311. if (flag)
  312. {
  313. this._popup.Closed -= (eventHandler);
  314. }
  315. this._popup = value;
  316. flag = (this._popup != null);
  317. if (flag)
  318. {
  319. this._popup.Closed += (eventHandler);
  320. }
  321. }
  322. }
  323. private Panel Root
  324. {
  325. get
  326. {
  327. return this._panel;
  328. }
  329. set
  330. {
  331. //var sizeChangedEventHandler = new SizeChangedEventHandler(this.Panel_SizeChanged);
  332. //bool flag = this._panel != null;
  333. //if (flag)
  334. //{
  335. // this._panel.SizeChanged -= (sizeChangedEventHandler);
  336. //}
  337. this._panel = value;
  338. //flag = (this._panel != null);
  339. //if (flag)
  340. //{
  341. // this._panel.SizeChanged += (sizeChangedEventHandler);
  342. //}
  343. }
  344. }
  345. #endregion Public Properties
  346. #endregion Properties
  347. #region Methods
  348. #region Internal Methods
  349. internal void BuildPopup(Popup popup, Panel root)
  350. {
  351. this.Root = root;
  352. this.Popup = popup;
  353. root.Children.Add(this.Popup);
  354. //this.BasePopup = new Popup()
  355. //{
  356. // AllowsTransparency = true,
  357. // Placement = PlacementMode.Relative
  358. //};
  359. //root.Children.Add(this.BasePopup);
  360. //this.BasePopup.IsOpen = true;
  361. //this.MainGrid.Background = new SolidColorBrush(Colors.Transparent);
  362. //this.MainGrid.Width = root.ActualWidth;
  363. //this.MainGrid.Height = root.ActualHeight;
  364. //this.BasePopup.Child = this.MainGrid;
  365. //父容器左击其它区域退出菜单
  366. this.Root.MouseLeftButtonDown -= Root_MouseLeftButtonDown;
  367. this.Root.MouseLeftButtonDown += Root_MouseLeftButtonDown;
  368. //父容器右击其它区域退出菜单(后面添加的)
  369. this.Root.MouseRightButtonDown -= Root_MouseLeftButtonDown;
  370. this.Root.MouseRightButtonDown += Root_MouseLeftButtonDown;
  371. }
  372. void Root_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  373. {
  374. //popup不能弹出看这里:在popup父容器MouseLeftButtonDown set handled = true
  375. if (Popup != null) Popup.IsOpen = false;
  376. }
  377. #endregion Internal Methods
  378. #region Private Static Methods
  379. #endregion Private Static Methods
  380. #region Private Methods
  381. //private void MainGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  382. //{
  383. // this.Popup.IsOpen = false;
  384. //}
  385. private void Popup_Closed(object sender, System.EventArgs e)
  386. {
  387. //this.BasePopup.IsOpen = false;
  388. this.Root.Children.Remove(this.Popup);
  389. //this.Root.Children.Remove(this.BasePopup);
  390. this.Root.MouseLeftButtonDown -= Root_MouseLeftButtonDown;
  391. this.Root.MouseRightButtonDown -= Root_MouseLeftButtonDown;
  392. this.Popup.Child = null;
  393. //this.BasePopup = null;
  394. this.Root = null;
  395. //this.MainGrid = null;
  396. this.Popup = null;
  397. }
  398. //private void Panel_SizeChanged(object sender, SizeChangedEventArgs e)
  399. //{
  400. // this.MainGrid.Width = this.Root.ActualWidth;
  401. // this.MainGrid.Height = this.Root.ActualHeight;
  402. //}
  403. #endregion Private Methods
  404. #endregion Methods
  405. }
  406. #endregion Nested Types
  407. }
  408. }