PageRenderTime 73ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/GenericDesigner.cs

#
C# | 778 lines | 643 code | 126 blank | 9 comment | 126 complexity | 8b0372b3e1c3a15a914b8003289bd243 MD5 | raw file
  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using System.ComponentModel.Design;
  4. using System.Windows.Forms.Design;
  5. using System.Windows.Forms.Design.Behavior;
  6. namespace System.ComponentModel.Design
  7. {
  8. internal static class ComponentDesignerExtension
  9. {
  10. public const System.Reflection.BindingFlags AllInstBind = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance;
  11. public static object EditValue(this ComponentDesigner designer, object objectToChange, string propName)
  12. {
  13. PropertyDescriptor prop = TypeDescriptor.GetProperties(objectToChange)[propName];
  14. EditorServiceContext context = new EditorServiceContext(designer, prop);
  15. var editor = prop.GetEditor(typeof(System.Drawing.Design.UITypeEditor)) as System.Drawing.Design.UITypeEditor;
  16. object curVal = prop.GetValue(objectToChange);
  17. object newVal = editor.EditValue(context, context, curVal);
  18. if (newVal != curVal)
  19. try { prop.SetValue(objectToChange, newVal); }
  20. catch (CheckoutException) { }
  21. return newVal;
  22. }
  23. public static List<DesignerActionItem> GetAllAttributedActionItems(this DesignerActionList actionList)
  24. {
  25. var fullAIList = new List<DesignerActionItem>();
  26. foreach (var mbr in actionList.GetType().GetMethods(AllInstBind))
  27. {
  28. foreach (IActionGetItem attr in mbr.GetCustomAttributes(typeof(DesignerActionMethodAttribute), false))
  29. {
  30. if (mbr.ReturnType == typeof(void) && mbr.GetParameters().Length == 0)
  31. fullAIList.Add(attr.GetItem(actionList, mbr));
  32. else
  33. throw new FormatException("DesignerActionMethodAttribute must be applied to a method returning void and having no parameters.");
  34. }
  35. }
  36. foreach (var mbr in actionList.GetType().GetProperties(AllInstBind))
  37. {
  38. foreach (IActionGetItem attr in mbr.GetCustomAttributes(typeof(DesignerActionPropertyAttribute), false))
  39. fullAIList.Add(attr.GetItem(actionList, mbr));
  40. }
  41. fullAIList.Sort(CompareItems);
  42. return fullAIList;
  43. }
  44. public static DesignerVerbCollection GetAttributedVerbs(this ComponentDesigner designer)
  45. {
  46. var verbs = new DesignerVerbCollection();
  47. foreach (var m in designer.GetType().GetMethods(AllInstBind))
  48. {
  49. foreach (DesignerVerbAttribute attr in m.GetCustomAttributes(typeof(DesignerVerbAttribute), true))
  50. {
  51. verbs.Add(attr.GetDesignerVerb(designer, m));
  52. }
  53. }
  54. return verbs;
  55. }
  56. public static DesignerActionItemCollection GetFilteredActionItems(this DesignerActionList actionList, List<DesignerActionItem> fullAIList)
  57. {
  58. DesignerActionItemCollection col = new DesignerActionItemCollection();
  59. fullAIList.ForEach(ai => { if (CheckCondition(actionList, ai)) { col.Add(ai); } });
  60. // Add header items for displayed items
  61. int i = 0; string cat = null;
  62. while (i < col.Count)
  63. {
  64. string curCat = col[i].Category;
  65. if (string.Compare(curCat, cat, true, Globalization.CultureInfo.CurrentCulture) != 0)
  66. {
  67. col.Insert(i++, new DesignerActionHeaderItem(curCat));
  68. cat = curCat;
  69. }
  70. i++;
  71. }
  72. return col;
  73. }
  74. public static IDictionary<string, List<Attribute>> GetRedirectedProperties(this ComponentDesigner d)
  75. {
  76. var ret = new Dictionary<string, List<Attribute>>();
  77. foreach (var prop in d.GetType().GetProperties(AllInstBind))
  78. {
  79. foreach (RedirectedDesignerPropertyAttribute attr in prop.GetCustomAttributes(typeof(RedirectedDesignerPropertyAttribute), false))
  80. {
  81. List<Attribute> attributes;
  82. if (attr.ApplyOtherAttributes)
  83. {
  84. attributes = new List<Attribute>(Array.ConvertAll<object, Attribute>(prop.GetCustomAttributes(false), o => o as System.Attribute));
  85. attributes.RemoveAll(a => a is RedirectedDesignerPropertyAttribute);
  86. }
  87. else
  88. attributes = new List<Attribute>();
  89. ret.Add(prop.Name, attributes);
  90. }
  91. }
  92. return ret;
  93. }
  94. public static void RedirectRegisteredProperties(this ComponentDesigner d, System.Collections.IDictionary properties, IDictionary<string, List<Attribute>> redirectedProps)
  95. {
  96. foreach (var propName in redirectedProps.Keys)
  97. {
  98. PropertyDescriptor oldPropertyDescriptor = (PropertyDescriptor)properties[propName];
  99. if (oldPropertyDescriptor != null)
  100. {
  101. List<Attribute> attributes = redirectedProps[propName];
  102. properties[propName] = TypeDescriptor.CreateProperty(d.GetType(), oldPropertyDescriptor, attributes.ToArray());
  103. }
  104. }
  105. }
  106. public static void RemoveProperties(this ComponentDesigner d, System.Collections.IDictionary properties, IEnumerable<string> propertiesToRemove)
  107. {
  108. foreach (string p in propertiesToRemove)
  109. if (properties.Contains(p))
  110. properties.Remove(p);
  111. }
  112. public static void SetComponentProperty<T>(this ComponentDesigner d, string propName, T value)
  113. {
  114. PropertyDescriptor propDesc = TypeDescriptor.GetProperties(d.Component)[propName];
  115. if (propDesc != null && propDesc.PropertyType == typeof(T) && !propDesc.IsReadOnly && propDesc.IsBrowsable)
  116. propDesc.SetValue(d.Component, value);
  117. }
  118. public static System.Windows.Forms.DialogResult ShowDialog(this ComponentDesigner designer, System.Windows.Forms.Form dialog)
  119. {
  120. EditorServiceContext context = new EditorServiceContext(designer);
  121. return context.ShowDialog(dialog);
  122. }
  123. private static bool CheckCondition(DesignerActionList actionList, DesignerActionItem ai)
  124. {
  125. if (ai.Properties["Condition"] != null)
  126. {
  127. var p = actionList.GetType().GetProperty((string)ai.Properties["Condition"], AllInstBind, null, typeof(bool), Type.EmptyTypes, null);
  128. if (p != null)
  129. return (bool)p.GetValue(actionList, null);
  130. }
  131. return true;
  132. }
  133. private static int CompareItems(DesignerActionItem a, DesignerActionItem b)
  134. {
  135. int c = string.Compare(a.Category ?? string.Empty, b.Category ?? string.Empty, true, Globalization.CultureInfo.CurrentCulture);
  136. if (c != 0)
  137. return c;
  138. c = (int)a.Properties["Order"] - (int)b.Properties["Order"];
  139. if (c != 0)
  140. return c;
  141. return string.Compare(a.DisplayName, b.DisplayName, true, Globalization.CultureInfo.CurrentCulture);
  142. }
  143. }
  144. [System.AttributeUsage(System.AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
  145. internal sealed class RedirectedDesignerPropertyAttribute : System.Attribute
  146. {
  147. public RedirectedDesignerPropertyAttribute() { ApplyOtherAttributes = true; }
  148. public bool ApplyOtherAttributes { get; set; }
  149. }
  150. }
  151. namespace System.Windows.Forms.Design
  152. {
  153. internal interface IActionGetItem
  154. {
  155. string Category { get; }
  156. DesignerActionItem GetItem(DesignerActionList actions, Reflection.MemberInfo mbr);
  157. }
  158. [System.AttributeUsage(System.AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
  159. internal sealed class DesignerActionMethodAttribute : System.Attribute, IActionGetItem
  160. {
  161. public DesignerActionMethodAttribute(string displayName, int displayOrder = 0)
  162. {
  163. DisplayName = displayName;
  164. DisplayOrder = displayOrder;
  165. }
  166. public bool AllowAssociate { get; set; }
  167. public string Category { get; set; }
  168. public string Condition { get; set; }
  169. public string Description { get; set; }
  170. public string DisplayName { get; }
  171. public int DisplayOrder { get; }
  172. public bool IncludeAsDesignerVerb { get; set; }
  173. DesignerActionItem IActionGetItem.GetItem(DesignerActionList actions, Reflection.MemberInfo mbr)
  174. {
  175. var ret = new DesignerActionMethodItem(actions, mbr.Name, DisplayName, Category, Description, IncludeAsDesignerVerb)
  176. { AllowAssociate = AllowAssociate };
  177. if (!string.IsNullOrEmpty(Condition))
  178. ret.Properties.Add("Condition", Condition);
  179. ret.Properties.Add("Order", DisplayOrder);
  180. return ret;
  181. }
  182. }
  183. [System.AttributeUsage(System.AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
  184. internal sealed class DesignerActionPropertyAttribute : System.Attribute, IActionGetItem
  185. {
  186. public DesignerActionPropertyAttribute(string displayName, int displayOrder = 0)
  187. {
  188. DisplayName = displayName;
  189. DisplayOrder = displayOrder;
  190. }
  191. public bool AllowAssociate { get; set; }
  192. public string Category { get; set; }
  193. public string Condition { get; set; }
  194. public string Description { get; set; }
  195. public string DisplayName { get; }
  196. public int DisplayOrder { get; }
  197. DesignerActionItem IActionGetItem.GetItem(DesignerActionList actions, Reflection.MemberInfo mbr)
  198. {
  199. var ret = new DesignerActionPropertyItem(mbr.Name, DisplayName, Category, Description)
  200. { AllowAssociate = AllowAssociate };
  201. if (!string.IsNullOrEmpty(Condition))
  202. ret.Properties.Add("Condition", Condition);
  203. ret.Properties.Add("Order", DisplayOrder);
  204. return ret;
  205. }
  206. }
  207. [System.AttributeUsage(System.AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
  208. internal sealed class DesignerVerbAttribute : System.Attribute
  209. {
  210. private CommandID cmdId;
  211. private string menuText;
  212. public DesignerVerbAttribute(string menuText)
  213. {
  214. this.menuText = menuText;
  215. }
  216. public DesignerVerbAttribute(string menuText, Guid commandMenuGroup, int commandId)
  217. {
  218. this.menuText = menuText;
  219. cmdId = new CommandID(commandMenuGroup, commandId);
  220. }
  221. internal DesignerVerb GetDesignerVerb(object obj, Reflection.MethodInfo mi)
  222. {
  223. EventHandler handler = (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), obj, mi);
  224. if (cmdId != null)
  225. return new DesignerVerb(menuText, handler, cmdId);
  226. return new DesignerVerb(menuText, handler);
  227. }
  228. }
  229. internal class EditorServiceContext : IWindowsFormsEditorService, ITypeDescriptorContext, IServiceProvider
  230. {
  231. private IComponentChangeService _componentChangeSvc;
  232. private ComponentDesigner _designer;
  233. private PropertyDescriptor _targetProperty;
  234. internal EditorServiceContext(ComponentDesigner designer)
  235. {
  236. _designer = designer;
  237. }
  238. internal EditorServiceContext(ComponentDesigner designer, PropertyDescriptor prop)
  239. {
  240. _designer = designer;
  241. _targetProperty = prop;
  242. if (prop == null)
  243. {
  244. prop = TypeDescriptor.GetDefaultProperty(designer.Component);
  245. if ((prop != null) && typeof(System.Collections.ICollection).IsAssignableFrom(prop.PropertyType))
  246. _targetProperty = prop;
  247. }
  248. }
  249. internal EditorServiceContext(ComponentDesigner designer, PropertyDescriptor prop, string newVerbText)
  250. : this(designer, prop)
  251. {
  252. _designer.Verbs.Add(new DesignerVerb(newVerbText, new EventHandler(OnEditItems)));
  253. }
  254. private T GetService<T>() => (T)((IServiceProvider)this).GetService(typeof(T));
  255. private void OnEditItems(object sender, EventArgs e)
  256. {
  257. object component = _targetProperty.GetValue(_designer.Component);
  258. if (component != null)
  259. {
  260. CollectionEditor editor = TypeDescriptor.GetEditor(component, typeof(System.Drawing.Design.UITypeEditor)) as CollectionEditor;
  261. if (editor != null)
  262. editor.EditValue(this, this, component);
  263. }
  264. }
  265. void ITypeDescriptorContext.OnComponentChanged()
  266. {
  267. ChangeService.OnComponentChanged(_designer.Component, _targetProperty, null, null);
  268. }
  269. bool ITypeDescriptorContext.OnComponentChanging()
  270. {
  271. try
  272. {
  273. ChangeService.OnComponentChanging(_designer.Component, _targetProperty);
  274. }
  275. catch (CheckoutException exception)
  276. {
  277. if (exception != CheckoutException.Canceled)
  278. throw;
  279. return false;
  280. }
  281. return true;
  282. }
  283. object IServiceProvider.GetService(Type serviceType)
  284. {
  285. if ((serviceType == typeof(ITypeDescriptorContext)) || (serviceType == typeof(IWindowsFormsEditorService)))
  286. return this;
  287. if ((_designer.Component != null) && (_designer.Component.Site != null))
  288. return _designer.Component.Site.GetService(serviceType);
  289. return null;
  290. }
  291. void IWindowsFormsEditorService.CloseDropDown()
  292. {
  293. }
  294. void IWindowsFormsEditorService.DropDownControl(Control control)
  295. {
  296. }
  297. public DialogResult ShowDialog(Form dialog)
  298. {
  299. if (dialog == null)
  300. throw new ArgumentNullException(nameof(dialog));
  301. IUIService service = GetService<IUIService>();
  302. if (service != null)
  303. return service.ShowDialog(dialog);
  304. return dialog.ShowDialog(_designer.Component as IWin32Window);
  305. }
  306. private IComponentChangeService ChangeService
  307. {
  308. get
  309. {
  310. if (_componentChangeSvc == null)
  311. _componentChangeSvc = GetService<IComponentChangeService>();
  312. return _componentChangeSvc;
  313. }
  314. }
  315. IContainer ITypeDescriptorContext.Container
  316. {
  317. get
  318. {
  319. if (_designer.Component.Site != null)
  320. return _designer.Component.Site.Container;
  321. return null;
  322. }
  323. }
  324. object ITypeDescriptorContext.Instance => _designer.Component;
  325. PropertyDescriptor ITypeDescriptorContext.PropertyDescriptor => _targetProperty;
  326. }
  327. internal abstract class RichBehavior<D> : Behavior.Behavior where D : ControlDesigner
  328. {
  329. public RichBehavior(D designer)
  330. {
  331. Designer = designer;
  332. }
  333. public D Designer { get; }
  334. }
  335. internal class RichComponentDesigner<C, A> : ComponentDesigner
  336. where C : Component
  337. where A : _BaseDesignerActionList
  338. {
  339. private A actions;
  340. private Adorner adorner;
  341. private IDictionary<string, List<Attribute>> redirectedProps;
  342. private DesignerVerbCollection verbs;
  343. public override DesignerActionListCollection ActionLists
  344. {
  345. get
  346. {
  347. if (actions == null)
  348. actions = Activator.CreateInstance(typeof(A), this, Component) as A;
  349. return new DesignerActionListCollection(new DesignerActionList[] { actions });
  350. }
  351. }
  352. public BehaviorService BehaviorService { get; private set; }
  353. public IComponentChangeService ComponentChangeService { get; private set; }
  354. public new C Component => (C)base.Component;
  355. public virtual GlyphCollection Glyphs => Adorner.Glyphs;
  356. public ISelectionService SelectionService { get; private set; }
  357. public override DesignerVerbCollection Verbs
  358. {
  359. get
  360. {
  361. if (verbs == null)
  362. verbs = this.GetAttributedVerbs();
  363. return verbs;
  364. }
  365. }
  366. internal Adorner Adorner
  367. {
  368. get
  369. {
  370. if (adorner == null)
  371. {
  372. adorner = new Adorner();
  373. BehaviorService.Adorners.Add(adorner);
  374. }
  375. return adorner;
  376. }
  377. }
  378. protected virtual IEnumerable<string> PropertiesToRemove => new string[0];
  379. public override void Initialize(IComponent component)
  380. {
  381. base.Initialize(component);
  382. BehaviorService = GetService<BehaviorService>();
  383. SelectionService = GetService<ISelectionService>();
  384. if (SelectionService != null)
  385. SelectionService.SelectionChanged += OnSelectionChanged;
  386. ComponentChangeService = GetService<IComponentChangeService>();
  387. if (ComponentChangeService != null)
  388. ComponentChangeService.ComponentChanged += OnComponentChanged;
  389. }
  390. protected override void Dispose(bool disposing)
  391. {
  392. if (disposing)
  393. {
  394. if (BehaviorService != null & adorner != null)
  395. BehaviorService.Adorners.Remove(adorner);
  396. ISelectionService ss = SelectionService;
  397. if (ss != null)
  398. ss.SelectionChanged -= OnSelectionChanged;
  399. IComponentChangeService cs = ComponentChangeService;
  400. if (cs != null)
  401. cs.ComponentChanged -= OnComponentChanged;
  402. }
  403. base.Dispose(disposing);
  404. }
  405. protected virtual S GetService<S>() where S : class => (S)GetService(typeof(S));
  406. protected virtual void OnComponentChanged(object sender, ComponentChangedEventArgs e)
  407. {
  408. }
  409. protected virtual void OnSelectionChanged(object sender, EventArgs e)
  410. {
  411. }
  412. protected override void PreFilterProperties(System.Collections.IDictionary properties)
  413. {
  414. base.PreFilterProperties(properties);
  415. // RedirectRegisteredProperties
  416. if (redirectedProps == null)
  417. redirectedProps = this.GetRedirectedProperties();
  418. this.RedirectRegisteredProperties(properties, redirectedProps);
  419. // Remove properties
  420. this.RemoveProperties(properties, PropertiesToRemove);
  421. }
  422. }
  423. internal class RichControlDesigner<C, A> : ControlDesigner
  424. where C : Control
  425. where A : _BaseDesignerActionList
  426. {
  427. private A actions;
  428. private Adorner adorner;
  429. private IDictionary<string, List<Attribute>> redirectedProps;
  430. private DesignerVerbCollection verbs;
  431. public override DesignerActionListCollection ActionLists
  432. {
  433. get
  434. {
  435. if (actions == null)
  436. actions = Activator.CreateInstance(typeof(A), this, Component) as A;
  437. return new DesignerActionListCollection(new DesignerActionList[] { actions });
  438. }
  439. }
  440. public new BehaviorService BehaviorService => base.BehaviorService;
  441. public IComponentChangeService ComponentChangeService { get; private set; }
  442. public new C Control => (C)base.Control;
  443. public virtual GlyphCollection Glyphs => Adorner.Glyphs;
  444. public ISelectionService SelectionService { get; private set; }
  445. public override DesignerVerbCollection Verbs
  446. {
  447. get
  448. {
  449. if (verbs == null)
  450. verbs = this.GetAttributedVerbs();
  451. return verbs;
  452. }
  453. }
  454. internal Adorner Adorner
  455. {
  456. get
  457. {
  458. if (adorner == null)
  459. {
  460. adorner = new Adorner();
  461. BehaviorService.Adorners.Add(adorner);
  462. }
  463. return adorner;
  464. }
  465. }
  466. protected virtual IEnumerable<string> PropertiesToRemove => new string[0];
  467. public override void Initialize(IComponent component)
  468. {
  469. base.Initialize(component);
  470. SelectionService = GetService<ISelectionService>();
  471. if (SelectionService != null)
  472. SelectionService.SelectionChanged += OnSelectionChanged;
  473. ComponentChangeService = GetService<IComponentChangeService>();
  474. if (ComponentChangeService != null)
  475. ComponentChangeService.ComponentChanged += OnComponentChanged;
  476. }
  477. protected override void Dispose(bool disposing)
  478. {
  479. if (disposing)
  480. {
  481. if (BehaviorService != null && adorner != null)
  482. BehaviorService.Adorners.Remove(adorner);
  483. ISelectionService ss = SelectionService;
  484. if (ss != null)
  485. ss.SelectionChanged -= OnSelectionChanged;
  486. IComponentChangeService cs = ComponentChangeService;
  487. if (cs != null)
  488. cs.ComponentChanged -= OnComponentChanged;
  489. }
  490. base.Dispose(disposing);
  491. }
  492. protected virtual S GetService<S>() where S : class => (S)GetService(typeof(S));
  493. protected virtual void OnComponentChanged(object sender, ComponentChangedEventArgs e)
  494. {
  495. }
  496. protected virtual void OnSelectionChanged(object sender, EventArgs e)
  497. {
  498. }
  499. protected override void PreFilterProperties(System.Collections.IDictionary properties)
  500. {
  501. base.PreFilterProperties(properties);
  502. // RedirectRegisteredProperties
  503. if (redirectedProps == null)
  504. redirectedProps = this.GetRedirectedProperties();
  505. this.RedirectRegisteredProperties(properties, redirectedProps);
  506. // Remove properties
  507. this.RemoveProperties(properties, PropertiesToRemove);
  508. }
  509. }
  510. internal abstract class _BaseDesignerActionList : DesignerActionList
  511. {
  512. private List<DesignerActionItem> fullAIList;
  513. public _BaseDesignerActionList(ComponentDesigner designer, IComponent component)
  514. : base(component)
  515. {
  516. base.AutoShow = true;
  517. ParentDesigner = designer;
  518. }
  519. public ComponentDesigner ParentDesigner { get; }
  520. public override DesignerActionItemCollection GetSortedActionItems()
  521. {
  522. // Retrieve all attributed methods and properties
  523. if (fullAIList == null)
  524. fullAIList = this.GetAllAttributedActionItems();
  525. // Filter for conditions and load
  526. return this.GetFilteredActionItems(fullAIList);
  527. }
  528. protected T GetComponentProperty<T>(string propName)
  529. {
  530. var p = ComponentProp(propName, typeof(T));
  531. if (p != null)
  532. return (T)p.GetValue(Component, null);
  533. return default(T);
  534. }
  535. protected void SetComponentProperty<T>(string propName, T value)
  536. {
  537. var p = ComponentProp(propName, typeof(T));
  538. if (p != null)
  539. p.SetValue(Component, value, null);
  540. }
  541. private Reflection.PropertyInfo ComponentProp(string propName, Type retType) => Component.GetType().GetProperty(propName, ComponentDesignerExtension.AllInstBind, null, retType, Type.EmptyTypes, null);
  542. }
  543. internal abstract class RichDesignerActionList<D, C> : _BaseDesignerActionList where D : ComponentDesigner where C : Component
  544. {
  545. public RichDesignerActionList(D designer, C component) : base(designer, component)
  546. {
  547. ParentDesigner = designer;
  548. }
  549. public new C Component => (C)base.Component;
  550. public new D ParentDesigner { get; }
  551. }
  552. internal abstract class RichGlyph<D> : Glyph, IDisposable where D : ControlDesigner
  553. {
  554. public RichGlyph(D designer, Behavior.Behavior behavior)
  555. : base(behavior)
  556. {
  557. Designer = designer;
  558. }
  559. public D Designer { get; }
  560. public virtual void Dispose()
  561. {
  562. }
  563. public void SetBehavior(RichBehavior<D> b) { base.SetBehavior(b); }
  564. }
  565. internal class RichParentControlDesigner<C, A> : ParentControlDesigner
  566. where C : Control
  567. where A : _BaseDesignerActionList
  568. {
  569. private A actions;
  570. private Adorner adorner;
  571. private IDictionary<string, List<Attribute>> redirectedProps;
  572. private DesignerVerbCollection verbs;
  573. public override DesignerActionListCollection ActionLists
  574. {
  575. get
  576. {
  577. if (actions == null)
  578. actions = Activator.CreateInstance(typeof(A), this, Component) as A;
  579. return new DesignerActionListCollection(new DesignerActionList[] { actions });
  580. }
  581. }
  582. public new BehaviorService BehaviorService => base.BehaviorService;
  583. public IComponentChangeService ComponentChangeService { get; private set; }
  584. public new C Control => (C)base.Control;
  585. public virtual GlyphCollection Glyphs => Adorner.Glyphs;
  586. public ISelectionService SelectionService { get; private set; }
  587. public override DesignerVerbCollection Verbs
  588. {
  589. get
  590. {
  591. if (verbs == null)
  592. verbs = this.GetAttributedVerbs();
  593. return verbs;
  594. }
  595. }
  596. internal Adorner Adorner
  597. {
  598. get
  599. {
  600. if (adorner == null)
  601. {
  602. adorner = new Adorner();
  603. BehaviorService.Adorners.Add(adorner);
  604. }
  605. return adorner;
  606. }
  607. }
  608. protected virtual IEnumerable<string> PropertiesToRemove => new string[0];
  609. public override void Initialize(IComponent component)
  610. {
  611. base.Initialize(component);
  612. SelectionService = GetService<ISelectionService>();
  613. if (SelectionService != null)
  614. SelectionService.SelectionChanged += OnSelectionChanged;
  615. ComponentChangeService = GetService<IComponentChangeService>();
  616. if (ComponentChangeService != null)
  617. ComponentChangeService.ComponentChanged += OnComponentChanged;
  618. }
  619. protected override void Dispose(bool disposing)
  620. {
  621. if (disposing)
  622. {
  623. if (BehaviorService != null & adorner != null)
  624. BehaviorService.Adorners.Remove(adorner);
  625. ISelectionService ss = SelectionService;
  626. if (ss != null)
  627. ss.SelectionChanged -= OnSelectionChanged;
  628. IComponentChangeService cs = ComponentChangeService;
  629. if (cs != null)
  630. cs.ComponentChanged -= OnComponentChanged;
  631. }
  632. base.Dispose(disposing);
  633. }
  634. protected virtual S GetService<S>() where S : class => (S)GetService(typeof(S));
  635. protected virtual void OnComponentChanged(object sender, ComponentChangedEventArgs e)
  636. {
  637. }
  638. protected virtual void OnSelectionChanged(object sender, EventArgs e)
  639. {
  640. }
  641. protected override void PreFilterProperties(System.Collections.IDictionary properties)
  642. {
  643. base.PreFilterProperties(properties);
  644. // RedirectRegisteredProperties
  645. if (redirectedProps == null)
  646. redirectedProps = this.GetRedirectedProperties();
  647. this.RedirectRegisteredProperties(properties, redirectedProps);
  648. // Remove properties
  649. this.RemoveProperties(properties, PropertiesToRemove);
  650. }
  651. }
  652. }