G2DPropertyGrid.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. using CommonFroms.Properties;
  2. using CommonLang;
  3. using CommonLang.Xml;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Windows.Forms;
  11. namespace CommonFroms.G2D.DataGrid
  12. {
  13. public class G2DPropertyGrid : System.Windows.Forms.PropertyGrid
  14. {
  15. private int descriptionAreaLineCount = 2;
  16. private int descriptionAreaLineCountMin = 2;
  17. private Control docComment = null;
  18. private Control propertyGridView = null;
  19. private ToolStrip tools;
  20. private Type docCommentType = null;
  21. private PropertyInfo linesProperty;
  22. private bool sizeChangeIsFromUser = true;
  23. private bool inited = false;
  24. private Type last_field_decleard_type = null;
  25. private ToolStripButton btn_copy;
  26. private ToolStripButton btn_paste;
  27. private ToolStripDropDownButton btn_paste_plus;
  28. private ToolStripButton btn_cut;
  29. private ToolStripButton btn_delete;
  30. private ToolStripLabel lbl_copying;
  31. /// <summary>
  32. /// Initializes a new instance of the CustomPropertyGrid class.
  33. /// </summary>
  34. public G2DPropertyGrid()
  35. {
  36. this.ImeMode = ImeMode.On;
  37. this.MinDescriptionAreaLineCount = 5;
  38. foreach (Control control in this.Controls)
  39. {
  40. Type controlType = control.GetType();
  41. if (controlType.Name == "DocComment")
  42. {
  43. this.docCommentType = controlType;
  44. this.docComment = control;
  45. this.linesProperty = this.docCommentType.GetProperty("Lines");
  46. FieldInfo userSizedField = this.docCommentType.BaseType.GetField(
  47. "userSized",
  48. BindingFlags.Instance | BindingFlags.NonPublic);
  49. userSizedField.SetValue(this.docComment, true);
  50. this.docComment.SizeChanged += this.HandleDocCommentSizeChanged;
  51. }
  52. else if (controlType.Name == "PropertyGridView")
  53. {
  54. this.propertyGridView = control;
  55. }
  56. else if (controlType.Name == "ToolStrip")
  57. {
  58. this.tools = control as ToolStrip;
  59. }
  60. }
  61. this.SelectedGridItemChanged += G2DPropertyGrid_SelectedGridItemChanged;
  62. if (tools != null)
  63. {
  64. InitTools();
  65. }
  66. }
  67. public void SetSelectedObject(object value, params IG2DPropertyAdapter[] adds)
  68. {
  69. base.SelectedObject = new G2DPropertyDescriptor(value, adds);
  70. }
  71. public object GetSelectedValue()
  72. {
  73. if (base.SelectedObject is G2DPropertyDescriptor)
  74. {
  75. return (base.SelectedObject as G2DPropertyDescriptor).EditData;
  76. }
  77. return base.SelectedObject;
  78. }
  79. private void G2DPropertyGrid_SelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e)
  80. {
  81. this.OnRefreshHistoryValiable(SelectedGridItem);
  82. }
  83. protected override void OnPropertyValueChanged(PropertyValueChangedEventArgs e)
  84. {
  85. base.OnPropertyValueChanged(e);
  86. this.Refresh();
  87. }
  88. protected override void OnInvalidated(InvalidateEventArgs e)
  89. {
  90. base.OnInvalidated(e);
  91. if (!inited)
  92. {
  93. inited = true;
  94. this.DescriptionAreaLineCount = this.MinDescriptionAreaLineCount;
  95. }
  96. }
  97. //------------------------------------------------------------------------------------------------------
  98. #region _key_events_
  99. protected override bool ProcessCmdKey(ref Message msg, Keys kd)
  100. {
  101. if (Keyboard.IsCtrlDown)
  102. {
  103. Keys keyData = kd ^ Keys.Control;
  104. switch (keyData)
  105. {
  106. case Keys.C:
  107. if (ProcessKeyDown_CtrlC != null)
  108. {
  109. ProcessKeyDown_CtrlC.Invoke(this, new KeyEventArgs(keyData));
  110. }
  111. DoCopy();
  112. return true;
  113. case Keys.V:
  114. if (ProcessKeyDown_CtrlV != null)
  115. {
  116. ProcessKeyDown_CtrlV.Invoke(this, new KeyEventArgs(keyData));
  117. }
  118. DoPaste();
  119. return true;
  120. case Keys.X:
  121. if (ProcessKeyDown_CtrlX != null)
  122. {
  123. ProcessKeyDown_CtrlX.Invoke(this, new KeyEventArgs(keyData));
  124. }
  125. DoCut();
  126. return true;
  127. }
  128. }
  129. switch (kd)
  130. {
  131. case Keys.Delete:
  132. if (ProcessKeyDown_Delete != null)
  133. {
  134. ProcessKeyDown_Delete.Invoke(this, new KeyEventArgs(kd));
  135. }
  136. DoDelete();
  137. return true;
  138. }
  139. return base.ProcessCmdKey(ref msg, kd);
  140. }
  141. public delegate void KeyDownProcessHandler(object sender, KeyEventArgs e);
  142. /// <summary>
  143. /// 键盘Ctrl+C触发
  144. /// </summary>
  145. public event KeyDownProcessHandler ProcessKeyDown_CtrlC;
  146. /// <summary>
  147. /// 键盘Ctrl+V触发
  148. /// </summary>
  149. public event KeyDownProcessHandler ProcessKeyDown_CtrlV;
  150. /// <summary>
  151. /// 键盘Ctrl+X触发
  152. /// </summary>
  153. public event KeyDownProcessHandler ProcessKeyDown_CtrlX;
  154. /// <summary>
  155. /// 键盘Del触发
  156. /// </summary>
  157. public event KeyDownProcessHandler ProcessKeyDown_Delete;
  158. #endregion
  159. //------------------------------------------------------------------------------------------------------
  160. #region _tools_
  161. private void InitTools()
  162. {
  163. tools.CanOverflow = false;
  164. tools.LayoutStyle = ToolStripLayoutStyle.Flow;
  165. // try
  166. // {
  167. // FieldInfo btnViewPropertyPagesField = base.GetType().GetField("btnViewPropertyPages", BindingFlags.Instance);
  168. // ToolStripButton btnViewPropertyPages = btnViewPropertyPagesField.GetValue(this) as ToolStripButton;
  169. // btnViewPropertyPages.Visible = false;
  170. // }
  171. // catch (Exception err) { }
  172. tools.Items.Add(new ToolStripSeparator());
  173. tools.Items.Add(btn_copy = new ToolStripButton("复制", Resources.icons_copy));
  174. tools.Items.Add(btn_paste = new ToolStripButton("粘贴", Resources.icons_paste));
  175. tools.Items.Add(btn_cut = new ToolStripButton("剪切", Resources.icons_cut));
  176. tools.Items.Add(btn_delete = new ToolStripButton("删除", Resources.icons_delete));
  177. tools.Items.Add(new ToolStripSeparator());
  178. tools.Items.Add(btn_paste_plus = new ToolStripDropDownButton("", Resources.icons_paste_plus));
  179. tools.Items.Add(lbl_copying = new ToolStripLabel(""));
  180. btn_paste_plus.ToolTipText = "粘贴自";
  181. btn_paste_plus.ForeColor = System.Drawing.Color.Blue;
  182. btn_paste_plus.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
  183. btn_paste_plus.DropDownItemClicked += Btn_paste_plus_DropDownItemClicked;
  184. btn_paste_plus.DropDownOpening += Btn_paste_plus_DropDownOpening;
  185. btn_paste_plus.AutoToolTip = true;
  186. lbl_copying.ForeColor = System.Drawing.Color.Blue;
  187. lbl_copying.Overflow = ToolStripItemOverflow.Never;
  188. lbl_copying.AutoToolTip = true;
  189. btn_delete.Visible = false;
  190. btn_cut.DisplayStyle = ToolStripItemDisplayStyle.Image;
  191. btn_copy.DisplayStyle = ToolStripItemDisplayStyle.Image;
  192. btn_paste.DisplayStyle = ToolStripItemDisplayStyle.Image;
  193. btn_delete.DisplayStyle = ToolStripItemDisplayStyle.Image;
  194. btn_cut.Click += Btn_cut_Click; ;
  195. btn_copy.Click += Btn_copy_Click;
  196. btn_paste.Click += Btn_paste_Click;
  197. btn_delete.Click += Btn_delete_Click;
  198. }
  199. private void RefreshToolTips(List<CopyHistory.Entry> list)
  200. {
  201. if (list != null && list.Count > 0)
  202. {
  203. CopyHistory.Entry current = list[0];
  204. this.btn_paste_plus.Text = list.Count.ToString();
  205. this.btn_paste_plus.ToolTipText = "粘贴自: " + current.lbl_text;
  206. this.lbl_copying.Text = current.lst_text ;
  207. this.lbl_copying.ToolTipText = current.lbl_text ;
  208. this.btn_paste_plus.Visible = true;
  209. this.lbl_copying.Visible = true;
  210. }
  211. else
  212. {
  213. this.btn_paste_plus.Visible = false;
  214. this.lbl_copying.Visible = false;
  215. }
  216. }
  217. /// <summary>
  218. /// 刷新粘贴可用项目
  219. /// </summary>
  220. /// <param name="item"></param>
  221. private void OnRefreshHistoryValiable(GridItem item)
  222. {
  223. if (item != null && item.PropertyDescriptor is IG2DPropertyDescriptor)
  224. {
  225. Type decleard_type = (item.PropertyDescriptor as IG2DPropertyDescriptor).DecleardFieldType;
  226. if (!decleard_type.Equals(this.last_field_decleard_type))
  227. {
  228. this.last_field_decleard_type = decleard_type;
  229. this.btn_paste_plus.DropDownItems.Clear();
  230. List<CopyHistory.Entry> list = CopyHistory.GetHistoryList(decleard_type);
  231. if (list != null && list.Count > 0)
  232. {
  233. foreach (var h in list)
  234. {
  235. var add = new ToolStripButton(h.lst_text);
  236. add.AutoToolTip = true;
  237. add.ToolTipText = h.lbl_text;
  238. add.DisplayStyle = ToolStripItemDisplayStyle.Text;
  239. add.ForeColor = (add.Enabled) ? System.Drawing.Color.Blue : System.Drawing.Color.Gray;
  240. add.Tag = h;
  241. btn_paste_plus.DropDownItems.Add(add);
  242. }
  243. RefreshToolTips(list);
  244. return;
  245. }
  246. else
  247. {
  248. RefreshToolTips(null);
  249. }
  250. }
  251. }
  252. else
  253. {
  254. this.last_field_decleard_type = null;
  255. RefreshToolTips(null);
  256. }
  257. }
  258. private void OnRefreshHistoryAdded(GridItem item, bool new_item, List<CopyHistory.Entry> list)
  259. {
  260. if (list != null && list.Count > 0)
  261. {
  262. if (new_item)
  263. {
  264. CopyHistory.Entry current = list[0];
  265. var add = new ToolStripButton(current.lst_text);
  266. add.AutoToolTip = true;
  267. add.ToolTipText = current.lbl_text;
  268. add.DisplayStyle = ToolStripItemDisplayStyle.Text;
  269. add.ForeColor = (add.Enabled) ? System.Drawing.Color.Blue : System.Drawing.Color.Gray;
  270. add.Tag = current;
  271. btn_paste_plus.DropDownItems.Insert(0, add);
  272. }
  273. RefreshToolTips(list);
  274. }
  275. else
  276. {
  277. RefreshToolTips(null);
  278. }
  279. }
  280. private void Btn_delete_Click(object sender, EventArgs e)
  281. {
  282. DoDelete();
  283. }
  284. private void Btn_paste_Click(object sender, EventArgs e)
  285. {
  286. DoPaste();
  287. }
  288. private void Btn_copy_Click(object sender, EventArgs e)
  289. {
  290. DoCopy();
  291. }
  292. private void Btn_cut_Click(object sender, EventArgs e)
  293. {
  294. DoCut();
  295. }
  296. private void Btn_paste_plus_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
  297. {
  298. var history = e.ClickedItem.Tag as CopyHistory.Entry;
  299. if (history != null)
  300. {
  301. DoPastePlus(history);
  302. }
  303. }
  304. private void Btn_paste_plus_DropDownOpening(object sender, EventArgs e)
  305. {
  306. }
  307. #endregion
  308. //------------------------------------------------------------------------------------------------------
  309. #region _copying_and_pasting_
  310. public static int CopyHistoryLimit
  311. {
  312. get { return CopyHistory.CopyHistoryLimit; }
  313. set { CopyHistory.CopyHistoryLimit = value; }
  314. }
  315. public static bool TryConvertTo(object src, Type targetType)
  316. {
  317. object target;
  318. return TryConvertTo(src, targetType, out target);
  319. }
  320. public static bool TryConvertTo(object src, Type targetType, out object target)
  321. {
  322. if (targetType.IsInstanceOfType(src))
  323. {
  324. target = src;
  325. return true;
  326. }
  327. if (targetType.IsPrimitive && src.GetType().IsPrimitive)
  328. {
  329. try
  330. {
  331. target = Convert.ChangeType(src, targetType);
  332. if (targetType.IsInstanceOfType(target))
  333. {
  334. return true;
  335. }
  336. }
  337. catch (Exception ) { }
  338. }
  339. target = null;
  340. return false;
  341. }
  342. private static class CopyHistory
  343. {
  344. private static HashMap<Type, List<Entry>> copying_history = new HashMap<Type, List<Entry>>();
  345. private static List<Entry> copying_history_primitive = new List<Entry>();
  346. private static int max_history_count = 22;
  347. public static int CopyHistoryLimit
  348. {
  349. get { return max_history_count; }
  350. set { if (value >= 20) { max_history_count = value; } }
  351. }
  352. public static List<Entry> GetHistoryList(Type decleard_type)
  353. {
  354. if (decleard_type.IsPrimitive && decleard_type.IsValueType && (!decleard_type.Equals(typeof(bool))))
  355. {
  356. return copying_history_primitive;
  357. }
  358. List<Entry> ret = copying_history.Get(decleard_type);
  359. return ret;
  360. }
  361. public static bool Add(object owner, GridItem item, out List<Entry> out_list)
  362. {
  363. if (item != null && item.PropertyDescriptor is IG2DPropertyDescriptor)
  364. {
  365. IG2DPropertyDescriptor g2dpp = item.PropertyDescriptor as IG2DPropertyDescriptor;
  366. if (item.Value != null)
  367. {
  368. var x = XmlUtil.ToString(XmlUtil.ObjectToXml(item.Value, "data", true));
  369. List<Entry> list = GetHistoryList(g2dpp.DecleardFieldType);
  370. if (list != null)
  371. {
  372. foreach (var old in list)
  373. {
  374. if (old.data.Equals(item.Value) || old.EqualsXmlText(x))
  375. {
  376. old.Renew(owner);
  377. list.Sort();
  378. out_list = list;
  379. return false;
  380. }
  381. }
  382. }
  383. else
  384. {
  385. list = new List<Entry>();
  386. copying_history.Put(g2dpp.DecleardFieldType, list);
  387. }
  388. var copying_value = new Entry(owner, x, item);
  389. list.Add(copying_value);
  390. list.Sort();
  391. if (list.Count > max_history_count)
  392. {
  393. list.RemoveRange(max_history_count, list.Count - max_history_count);
  394. }
  395. out_list = list;
  396. return true;
  397. }
  398. }
  399. out_list = null;
  400. return false;
  401. }
  402. public static Entry CurrentAvaliable(Type decleard_type)
  403. {
  404. List<Entry> list = GetHistoryList(decleard_type);
  405. if (list != null && list.Count > 0)
  406. {
  407. return list[0];
  408. }
  409. return null;
  410. }
  411. public static int GetHistoryListCount(Type decleard_type)
  412. {
  413. List<Entry> ret = GetHistoryList(decleard_type);
  414. if (ret != null)
  415. {
  416. return ret.Count;
  417. }
  418. return 0;
  419. }
  420. //----------------------------------------------------------------------------------
  421. public class Entry : IComparable<Entry>
  422. {
  423. public readonly object data;
  424. private readonly string cvt_text;
  425. private readonly string xml_text;
  426. private readonly string lbl_suffix;
  427. public DateTime time { get; private set; }
  428. public string lbl_text { get; private set; }
  429. public string lst_text { get; private set; }
  430. internal Entry(object owner, string x, GridItem item)
  431. {
  432. this.time = DateTime.Now;
  433. this.data = item.Value;
  434. this.xml_text = x;
  435. this.cvt_text = item.Value.ToString();
  436. Type expect_type = item.Value.GetType();
  437. try
  438. {
  439. if (item.PropertyDescriptor.Converter != null)
  440. {
  441. cvt_text = item.PropertyDescriptor.Converter.ConvertToString(item.Value);
  442. }
  443. if (item.PropertyDescriptor is IG2DPropertyDescriptor)
  444. {
  445. IG2DPropertyDescriptor g2dpp = item.PropertyDescriptor as IG2DPropertyDescriptor;
  446. expect_type = g2dpp.DecleardFieldType;
  447. }
  448. }
  449. catch (Exception )
  450. {
  451. this.lst_text = item.Value.ToString();
  452. }
  453. this.lst_text = string.Format("[{0}] {1}", expect_type.Name, cvt_text);
  454. this.lbl_suffix = string.Format("{0} = {1}", item.Label, lst_text);
  455. this.lbl_text = string.Format("{0}:{1}", owner, lbl_suffix);
  456. Clipboard.SetText(cvt_text);
  457. }
  458. public void Renew(object owner)
  459. {
  460. this.time = DateTime.Now;
  461. this.lbl_text = string.Format("{0}:{1}", owner, lbl_suffix);
  462. Clipboard.SetText(cvt_text);
  463. }
  464. public bool CanConvertTo(Type decleard_type)
  465. {
  466. if (TryConvertTo(data, decleard_type))
  467. {
  468. return true;
  469. }
  470. return false;
  471. }
  472. public int CompareTo(Entry other)
  473. {
  474. return -this.time.CompareTo(other.time);
  475. }
  476. public object CloneData(Type decleard_type)
  477. {
  478. object value = XmlUtil.XmlToObject(data.GetType(), XmlUtil.FromString(xml_text), true);
  479. object target;
  480. if (TryConvertTo(value, decleard_type, out target))
  481. {
  482. return target;
  483. }
  484. return value;
  485. }
  486. public bool EqualsXmlText(string x)
  487. {
  488. return this.xml_text.Equals(x);
  489. }
  490. }
  491. }
  492. //----------------------------------------------------------------------------------
  493. private void DoCopy()
  494. {
  495. GridItem item = SelectedGridItem;
  496. if (item != null && item.PropertyDescriptor != null && item.Value != null)
  497. {
  498. List<CopyHistory.Entry> list;
  499. bool new_item = CopyHistory.Add(SelectedObject, item, out list);
  500. this.OnRefreshHistoryAdded(item, new_item, list);
  501. }
  502. }
  503. private void DoPaste()
  504. {
  505. GridItem item = SelectedGridItem;
  506. if (item != null && item.PropertyDescriptor is IG2DPropertyDescriptor)
  507. {
  508. IG2DPropertyDescriptor g2dpp = item.PropertyDescriptor as IG2DPropertyDescriptor;
  509. var current = CopyHistory.CurrentAvaliable(g2dpp.DecleardFieldType);
  510. if (current != null)
  511. {
  512. DoPastePlus(current);
  513. }
  514. }
  515. }
  516. private void DoPastePlus(CopyHistory.Entry copying)
  517. {
  518. GridItem item = SelectedGridItem;
  519. if (item != null && (copying != null) && (item.PropertyDescriptor != null) && (!item.PropertyDescriptor.IsReadOnly))
  520. {
  521. if (item.PropertyDescriptor is IG2DPropertyDescriptor)
  522. {
  523. IG2DPropertyDescriptor g2dpp = item.PropertyDescriptor as IG2DPropertyDescriptor;
  524. try
  525. {
  526. var target = copying.CloneData(g2dpp.DecleardFieldType);
  527. if (target != null)
  528. {
  529. item.PropertyDescriptor.SetValue(g2dpp.ComponentData, target);
  530. this.Refresh();
  531. }
  532. }
  533. catch (Exception err)
  534. {
  535. MessageBox.Show(err.Message);
  536. }
  537. }
  538. }
  539. }
  540. private void DoDelete()
  541. {
  542. GridItem item = SelectedGridItem;
  543. if (item != null && (item.PropertyDescriptor != null) && (!item.PropertyDescriptor.IsReadOnly))
  544. {
  545. if (item.PropertyDescriptor is IG2DPropertyDescriptor)
  546. {
  547. IG2DPropertyDescriptor g2dpp = item.PropertyDescriptor as IG2DPropertyDescriptor;
  548. if (!g2dpp.NotNull)
  549. {
  550. try
  551. {
  552. item.PropertyDescriptor.SetValue(g2dpp.ComponentData, null);
  553. this.Refresh();
  554. }
  555. catch (Exception err)
  556. {
  557. MessageBox.Show(err.Message);
  558. }
  559. }
  560. else if (g2dpp.FieldValue != null)
  561. {
  562. if (g2dpp.DecleardFieldType.IsArray)
  563. {
  564. var array = (Array)g2dpp.FieldValue;
  565. Array.Clear(array, 0, array.Length);
  566. }
  567. else if (g2dpp.DecleardFieldType.GetInterface(typeof(IDictionary).Name) != null)
  568. {
  569. var map = (IDictionary)g2dpp.FieldValue;
  570. map.Clear();
  571. }
  572. else if (g2dpp.DecleardFieldType.GetInterface(typeof(IList).Name) != null)
  573. {
  574. var list = (IList)g2dpp.FieldValue;
  575. list.Clear();
  576. }
  577. try
  578. {
  579. item.PropertyDescriptor.SetValue(g2dpp.ComponentData, g2dpp.FieldValue);
  580. this.Refresh();
  581. }
  582. catch (Exception err)
  583. {
  584. MessageBox.Show(err.Message);
  585. }
  586. }
  587. else
  588. {
  589. MessageBox.Show("字段不可删除!");
  590. }
  591. }
  592. }
  593. }
  594. private void DoCut()
  595. {
  596. DoCopy();
  597. DoDelete();
  598. }
  599. #endregion
  600. //------------------------------------------------------------------------------------------------------
  601. #region _description_
  602. /// <summary>
  603. /// Occurs when the description area size is changed by the user.
  604. /// </summary>
  605. public event EventHandler UserChangedDescriptionAreaSize;
  606. /// <summary>
  607. /// 设置最小默认注释行数量
  608. /// </summary>
  609. public int MinDescriptionAreaLineCount
  610. {
  611. get
  612. {
  613. return this.descriptionAreaLineCountMin;
  614. }
  615. set
  616. {
  617. if (value <= 0)
  618. {
  619. throw new ArgumentException("The value cannot be less than zero.");
  620. }
  621. this.descriptionAreaLineCountMin = value;
  622. }
  623. }
  624. /// <summary>
  625. /// Gets or sets the description area line count.
  626. /// </summary>
  627. /// <value>The description area line count.</value>
  628. /// <exception cref="ArgumentException"> If value is less than zero.</exception>
  629. /// <exception cref="TypeLoadException"> If not of the all objects required to set the field were found.</exception>
  630. public int DescriptionAreaLineCount
  631. {
  632. get
  633. {
  634. return this.descriptionAreaLineCount;
  635. }
  636. set
  637. {
  638. if (!inited)
  639. {
  640. return;
  641. }
  642. if (value < MinDescriptionAreaLineCount)
  643. {
  644. throw new ArgumentException("The value cannot be less than " + MinDescriptionAreaLineCount + ".");
  645. }
  646. if (this.docCommentType == null ||
  647. this.docComment == null ||
  648. this.propertyGridView == null ||
  649. this.linesProperty == null)
  650. {
  651. throw new TypeLoadException("Not all of the objects required to set the field were found.");
  652. }
  653. try
  654. {
  655. int oldDocCommentHeight = this.docComment.Height;
  656. int oldValue = this.DescriptionAreaLineCount;
  657. this.linesProperty.SetValue(this.docComment, value, null);
  658. int difference = this.docComment.Height - oldDocCommentHeight;
  659. if (this.docComment.Top - difference > this.propertyGridView.Top)
  660. {
  661. this.sizeChangeIsFromUser = false;
  662. this.propertyGridView.Height -= difference;
  663. this.docComment.Top -= difference;
  664. this.descriptionAreaLineCount = value;
  665. this.sizeChangeIsFromUser = true;
  666. }
  667. else
  668. {
  669. this.linesProperty.SetValue(this.docComment, oldValue, null);
  670. }
  671. }
  672. catch (TargetInvocationException)
  673. {
  674. }
  675. this.Refresh();
  676. }
  677. }
  678. /// <summary>
  679. /// Gets or sets the height of the description area.
  680. /// </summary>
  681. /// <value>The height of the description area.</value>
  682. public int DescriptionAreaHeight
  683. {
  684. get
  685. {
  686. return this.docComment.Height;
  687. }
  688. set
  689. {
  690. if (!inited)
  691. {
  692. return;
  693. }
  694. int difference = value - this.docComment.Height;
  695. if (this.docComment.Top - difference > this.propertyGridView.Top)
  696. {
  697. this.docComment.Height = value;
  698. this.docComment.Top -= difference;
  699. this.propertyGridView.Height -= difference;
  700. this.Refresh();
  701. }
  702. }
  703. }
  704. /// <summary>
  705. /// Raises the UserChangedDescriptionAreaSize event.
  706. /// </summary>
  707. /// <param name="e">The System.EventArgs instance containing the event data.</param>
  708. protected void OnUserChangedDescriptionAreaSize(EventArgs e)
  709. {
  710. EventHandler handler = this.UserChangedDescriptionAreaSize;
  711. if (handler != null)
  712. {
  713. handler(this, e);
  714. }
  715. }
  716. /// <summary>
  717. /// Handles this.docComment.SizeChanged.
  718. /// </summary>
  719. /// <param name="sender">The sender.</param>
  720. /// <param name="e">The System.EventArgs instance containing the event data.</param>
  721. private void HandleDocCommentSizeChanged(object sender, EventArgs e)
  722. {
  723. if (this.sizeChangeIsFromUser)
  724. {
  725. try
  726. {
  727. this.descriptionAreaLineCount = (int)this.linesProperty.GetValue(this.docComment, null);
  728. this.OnUserChangedDescriptionAreaSize(EventArgs.Empty);
  729. }
  730. catch (TargetInvocationException)
  731. {
  732. }
  733. }
  734. }
  735. #endregion
  736. //------------------------------------------------------------------------------------------------------
  737. }
  738. }