PropertyGridView.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Jonathan Chambers ([email protected])
  24. //
  25. //
  26. // NOT COMPLETE
  27. using System;
  28. using System.Collections;
  29. using System.Drawing;
  30. using System.Drawing.Design;
  31. using System.ComponentModel;
  32. namespace System.Windows.Forms.PropertyGridInternal
  33. {
  34. public class PropertyGridView : System.Windows.Forms.ScrollableControl
  35. {
  36. #region Private Members
  37. internal const int V_INDENT = 16;
  38. internal const int ROW_HEIGHT = 16;
  39. internal const int RESIZE_WIDTH = 3;
  40. internal const int BUTTON_WIDTH = 25;
  41. private PropertyGridTextBox grid_textbox;
  42. internal PropertyGrid property_grid;
  43. internal bool redraw;
  44. internal bool resizing_grid;
  45. internal int label_column_width;
  46. private bool add_hscroll;
  47. private int open_grid_item_count = -1;
  48. private VScrollBar vbar;
  49. private bool vbar_added;
  50. private int skipped_grid_items;
  51. private Form dropdown_form;
  52. private ListBox listBox;
  53. #endregion
  54. #region Contructors
  55. public PropertyGridView (PropertyGrid propertyGrid)
  56. {
  57. property_grid = propertyGrid;
  58. this.BackColor = Color.Beige;
  59. grid_textbox = new PropertyGridTextBox();
  60. grid_textbox.DropDownButtonClicked +=new EventHandler(grid_textbox_DropDownButtonClicked);
  61. dropdown_form = new Form();
  62. dropdown_form.FormBorderStyle = FormBorderStyle.None;
  63. dropdown_form.Deactivate +=new EventHandler(dropdown_form_Deactivate);
  64. listBox = new ListBox();
  65. listBox.Dock = DockStyle.Fill;
  66. listBox.SelectedIndexChanged +=new EventHandler(listBox_SelectedIndexChanged);
  67. dropdown_form.Controls.Add(listBox);
  68. grid_textbox.Visible = true;
  69. grid_textbox.Font = new Font(this.Font,FontStyle.Bold);
  70. grid_textbox.BackColor = this.BackColor;
  71. // Not working at all, used to??
  72. //grid_textbox.TextBox.Validated += new EventHandler(TextBoxValidated);
  73. label_column_width = 65;
  74. resizing_grid = false;
  75. ForeColorChanged+=new EventHandler(RedrawEvent);
  76. BackColorChanged+=new System.EventHandler(RedrawEvent);
  77. FontChanged+=new EventHandler(RedrawEvent);
  78. SizeChanged+=new EventHandler(RedrawEvent);
  79. SetStyle(ControlStyles.UserPaint, true);
  80. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  81. SetStyle(ControlStyles.ResizeRedraw, true);
  82. SetStyle(ControlStyles.StandardClick | ControlStyles.StandardDoubleClick, false);
  83. }
  84. #endregion
  85. #region Protected Instance Methods
  86. protected override void WndProc (ref Message m)
  87. {
  88. switch ((Msg) m.Msg) {
  89. case Msg.WM_PAINT: {
  90. PaintEventArgs paint_event;
  91. paint_event = XplatUI.PaintEventStart (Handle);
  92. DoPaint (paint_event);
  93. XplatUI.PaintEventEnd (Handle);
  94. return;
  95. }
  96. }
  97. base.WndProc (ref m);
  98. }
  99. protected override void OnMouseMove (MouseEventArgs e)
  100. {
  101. if (resizing_grid) {
  102. label_column_width = Math.Max(e.X - V_INDENT,V_INDENT);
  103. Redraw();
  104. }
  105. else if (e.X > label_column_width+V_INDENT - RESIZE_WIDTH && e.X < label_column_width+V_INDENT + RESIZE_WIDTH) {
  106. this.Cursor = Cursors.VSplit;
  107. }
  108. base.OnMouseMove (e);
  109. }
  110. private GridItem GetSelectedGridItem (GridItemCollection grid_items, int y)
  111. {
  112. foreach (GridItem child_grid_item in grid_items) {
  113. if (y > child_grid_item.Top && y < child_grid_item.Top + ROW_HEIGHT) {
  114. return child_grid_item;
  115. }
  116. GridItem foundItem = GetSelectedGridItem(child_grid_item.GridItems, y);
  117. if (foundItem != null)
  118. return foundItem;
  119. }
  120. return null;
  121. }
  122. protected override void OnMouseDown (MouseEventArgs e)
  123. {
  124. if (e.X > label_column_width+V_INDENT + 2 - RESIZE_WIDTH && e.X < label_column_width+V_INDENT + 2 + RESIZE_WIDTH) {
  125. resizing_grid = true;
  126. }
  127. else {
  128. GridItem foundItem = GetSelectedGridItem(property_grid.grid_items, e.Y);
  129. if (this.property_grid.SelectedGridItem != null) {
  130. PropertyDescriptor desc = property_grid.SelectedGridItem.PropertyDescriptor;
  131. if (desc != null) {
  132. desc.SetValue(property_grid.SelectedObject, desc.Converter.ConvertFromString(grid_textbox.TextBoxText));
  133. }
  134. }
  135. if (foundItem != null)
  136. property_grid.SelectedGridItem = foundItem;
  137. if (property_grid.SelectedGridItem.Expandable) {
  138. if (((CategoryGridEntry)property_grid.SelectedGridItem).PlusMinusBounds.Contains(e.X,e.Y)){
  139. property_grid.SelectedGridItem.Expanded = !property_grid.SelectedGridItem.Expanded;
  140. }
  141. }
  142. Redraw();
  143. base.OnMouseDown (e);
  144. }
  145. }
  146. protected override void OnMouseUp (MouseEventArgs e)
  147. {
  148. resizing_grid = false;
  149. base.OnMouseUp (e);
  150. }
  151. protected override void OnKeyDown (KeyEventArgs e)
  152. {
  153. base.OnKeyDown (e);
  154. }
  155. #endregion
  156. #region Private Helper Methods
  157. private void AddVerticalScrollBar (int total_grid_items, bool count_changed)
  158. {
  159. if (vbar == null) {
  160. vbar = new VScrollBar ();
  161. count_changed = true;
  162. }
  163. vbar.Bounds = new Rectangle (ClientRectangle.Width - vbar.Width,
  164. 0, vbar.Width, Height);
  165. if (count_changed) {
  166. vbar.Maximum = total_grid_items;
  167. int height = ClientRectangle.Height;
  168. vbar.LargeChange = height / ROW_HEIGHT;
  169. }
  170. if (!vbar_added) {
  171. Controls.Add (vbar);
  172. vbar.ValueChanged += new EventHandler (VScrollBarValueChanged);
  173. vbar_added = true;
  174. }
  175. vbar.Visible = true;
  176. }
  177. internal void Redraw ()
  178. {
  179. redraw = true;
  180. Refresh ();
  181. }
  182. private GridItem GetGridItemAt (int y)
  183. {
  184. return null;
  185. }
  186. #region Drawing Code
  187. private void DoPaint (PaintEventArgs pevent)
  188. {
  189. Draw(pevent.ClipRectangle);
  190. pevent.Graphics.DrawImage(this.ImageBuffer, pevent.ClipRectangle, pevent.ClipRectangle, GraphicsUnit.Pixel);
  191. }
  192. [MonoTODO("Do a better job of clipping")]
  193. private void Draw (Rectangle clip)
  194. {
  195. if (redraw) {
  196. // Decide if we need a scrollbar
  197. bool add_vscroll = false;
  198. int old_open_grid_item_count = open_grid_item_count;
  199. // Use same brushes for all grid items
  200. Brush line_brush = ThemeEngine.Current.ResPool.GetSolidBrush(property_grid.LineColor);
  201. Pen line_pen = ThemeEngine.Current.ResPool.GetPen(property_grid.LineColor);
  202. Brush text_brush = ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorWindowText);
  203. Rectangle fill = ClientRectangle;
  204. // draw grid outline
  205. DeviceContext.FillRectangle (new SolidBrush (BackColor), fill);
  206. DeviceContext.DrawRectangle(ThemeEngine.Current.ResPool.GetPen(SystemColors.ControlDark),fill);
  207. int depth = 0;
  208. int item_height = ROW_HEIGHT;
  209. Font font = Font;
  210. int height = ClientRectangle.Height;
  211. GridItemCollection grid_items = this.property_grid.grid_items;
  212. open_grid_item_count = 0;
  213. foreach (GridItem grid_item in grid_items) {
  214. DrawGridItem (grid_item, clip, ref depth, item_height, font, height, line_brush, text_brush, line_pen);
  215. depth = 0;
  216. }
  217. add_vscroll = (open_grid_item_count * ROW_HEIGHT) > ClientRectangle.Height;
  218. if (add_vscroll) {
  219. AddVerticalScrollBar (open_grid_item_count, old_open_grid_item_count != open_grid_item_count);
  220. } else if (vbar != null) {
  221. vbar.Visible = false;
  222. skipped_grid_items = 0;
  223. }
  224. if (property_grid.SelectedGridItem != null && property_grid.SelectedGridItem.GridItemType == GridItemType.Property) {
  225. //if (grid_textbox.Visible == false) {
  226. this.Controls.Add(grid_textbox);
  227. //}
  228. //grid_textbox.Visible = true;
  229. if (!grid_textbox.Focused)
  230. grid_textbox.Focus();
  231. }
  232. else {
  233. this.Controls.Remove(grid_textbox);
  234. //grid_textbox.Visible = false;
  235. }
  236. redraw = false;
  237. }
  238. }
  239. private void DrawGridItem (GridItem grid_item, Rectangle clip, ref int depth, int item_height,
  240. Font font, int max_height, Brush line_brush, Brush text_brush, Pen line_pen)
  241. {
  242. int y = ClientRectangle.Top+1+(ROW_HEIGHT*(open_grid_item_count-skipped_grid_items));
  243. grid_item.Top = y;
  244. Rectangle indentRectangle = new Rectangle(ClientRectangle.Left+1,y,V_INDENT,ROW_HEIGHT);
  245. Rectangle labelRectangle = new Rectangle(indentRectangle.Right,y,label_column_width,ROW_HEIGHT);
  246. Rectangle valueRectangle = new Rectangle(labelRectangle.Right,y,ClientRectangle.Right-labelRectangle.Right,ROW_HEIGHT);
  247. DrawGridItemIndent(grid_item, line_brush, indentRectangle, grid_item.Expandable, grid_item.Expanded);
  248. DrawGridItemLabel(grid_item, labelRectangle, line_pen, text_brush, Font, depth, line_brush);
  249. DrawGridItemValue(grid_item, valueRectangle, line_pen, text_brush, new Font(Font, FontStyle.Bold), line_brush);
  250. if (grid_item == property_grid.SelectedGridItem) {
  251. if (grid_item.Value != null) {
  252. grid_textbox.TextBoxText = grid_item.Value.ToString();
  253. }
  254. else {
  255. grid_textbox.TextBoxText = string.Empty;
  256. }
  257. grid_textbox.Size = new Size(valueRectangle.Size.Width- (vbar == null || !vbar.Visible ? 0 : ThemeEngine.Current.VerticalScrollBarWidth),valueRectangle.Size.Height);
  258. grid_textbox.Location = new Point(valueRectangle.Location.X+4,valueRectangle.Location.Y+1);
  259. }
  260. open_grid_item_count++;
  261. depth++;
  262. if (grid_item.Expanded) {
  263. foreach (GridItem child_item in grid_item.GridItems) {
  264. int tdepth = depth;
  265. DrawGridItem(child_item, clip, ref tdepth, item_height, font, max_height, line_brush, text_brush, line_pen);
  266. }
  267. }
  268. }
  269. private void DrawGridItemIndent (GridItem grid_item, Brush brush, Rectangle rect, bool showPlusMinus, bool expanded)
  270. {
  271. DeviceContext.FillRectangle(brush,rect);
  272. if (showPlusMinus) {
  273. Rectangle plus_minus_rect = new Rectangle(rect.X+3,rect.Y+3,8,8);
  274. grid_item.PlusMinusBounds = plus_minus_rect;
  275. DeviceContext.DrawRectangle (SystemPens.ControlDark, plus_minus_rect);
  276. int middle = (plus_minus_rect.Bottom-plus_minus_rect.Top)/2 + plus_minus_rect.Top;
  277. int x = plus_minus_rect.X;
  278. DeviceContext.DrawLine (SystemPens.ControlDarkDark, x + 2, middle, x + 6, middle);
  279. if (!expanded) {
  280. DeviceContext.DrawLine (SystemPens.ControlDarkDark, x + 4, middle - 2, x + 4, middle + 2);
  281. }
  282. }
  283. }
  284. private void DrawGridItemLabel (GridItem grid_item, Rectangle rect, Pen pen, Brush brush, Font font, int depth, Brush line_brush)
  285. {
  286. Brush backBrush = ThemeEngine.Current.ResPool.GetSolidBrush(BackColor);
  287. Brush foreBrush = brush;
  288. bool selectedItem = false, expandable = false;
  289. if (grid_item == property_grid.SelectedGridItem){
  290. backBrush = ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorHilight);
  291. foreBrush = ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorHilightText);
  292. }
  293. if (grid_item.Expandable){
  294. backBrush = line_brush;
  295. foreBrush = brush;
  296. }
  297. DeviceContext.FillRectangle(backBrush,rect);
  298. DeviceContext.DrawRectangle(pen,rect);
  299. DeviceContext.DrawString(grid_item.Label,font,foreBrush,rect.Left + 5 + depth*V_INDENT,rect.Top+1);
  300. }
  301. [MonoTODO("GetEditor once the TypeDescriptor class is complete")]
  302. private void DrawGridItemValue (GridItem grid_item, Rectangle rect, Pen pen, Brush brush, Font font, Brush line_brush)
  303. {
  304. DeviceContext.FillRectangle((grid_item.GridItemType == GridItemType.Property) ? ThemeEngine.Current.ResPool.GetSolidBrush(BackColor) : line_brush,rect);
  305. DeviceContext.DrawRectangle(pen,rect);
  306. // PDB - added check to prevent crash with test app
  307. if (grid_item.Value != null) {
  308. DeviceContext.DrawString(grid_item.Value.ToString(),font,brush,rect.Left + 2,rect.Top+1);
  309. }
  310. if (grid_item == property_grid.SelectedGridItem) {
  311. grid_textbox.ReadOnly = false;
  312. if (grid_item.PropertyDescriptor != null) {
  313. UITypeEditor editor = null;//(UITypeEditor)grid_item.PropertyDescriptor.GetEditor(typeof(UITypeEditor));
  314. if (editor != null) {
  315. UITypeEditorEditStyle style = editor.GetEditStyle();
  316. if (style == UITypeEditorEditStyle.DropDown) {
  317. grid_textbox.DropDownButtonVisible = true;
  318. grid_textbox.DialogButtonVisible = false;
  319. }
  320. else if (style == UITypeEditorEditStyle.Modal) {
  321. grid_textbox.DropDownButtonVisible = false;
  322. grid_textbox.DialogButtonVisible = true;
  323. }
  324. }
  325. else if (grid_item.PropertyDescriptor.Converter.GetStandardValuesSupported()) {
  326. listBox.Items.Clear();
  327. foreach (object obj in grid_item.PropertyDescriptor.Converter.GetStandardValues())
  328. listBox.Items.Add(obj);
  329. grid_textbox.DropDownButtonVisible = true;
  330. grid_textbox.DialogButtonVisible = false;
  331. grid_textbox.ReadOnly = true;
  332. }
  333. else {
  334. grid_textbox.DropDownButtonVisible = false;
  335. grid_textbox.DialogButtonVisible = false;
  336. }
  337. }
  338. else {
  339. grid_textbox.DropDownButtonVisible = false;
  340. grid_textbox.DialogButtonVisible = false;
  341. }
  342. }
  343. }
  344. #endregion
  345. #region Event Handling
  346. private void RedrawEvent (object sender, System.EventArgs e)
  347. {
  348. Redraw();
  349. }
  350. private void VScrollBarValueChanged (object sender, EventArgs e)
  351. {
  352. int old_skip = skipped_grid_items;
  353. skipped_grid_items = vbar.Value;
  354. int y_move = (old_skip - skipped_grid_items) * ROW_HEIGHT;
  355. // need this to refresh drawing
  356. redraw = true;
  357. XplatUI.ScrollWindow (Handle, ClientRectangle, 0, y_move, true);
  358. }
  359. private void TextBoxValidated (object sender, EventArgs e)
  360. {
  361. if (this.property_grid.SelectedGridItem != null) {
  362. PropertyDescriptor desc = property_grid.SelectedGridItem.PropertyDescriptor;
  363. if (desc != null) {
  364. desc.SetValue(property_grid.SelectedObject, desc.Converter.ConvertFromString(grid_textbox.TextBoxText));
  365. }
  366. }
  367. }
  368. #endregion
  369. #endregion
  370. private void dropdown_form_Deactivate (object sender, EventArgs e)
  371. {
  372. dropdown_form.Hide();
  373. }
  374. private void listBox_SelectedIndexChanged (object sender, EventArgs e)
  375. {
  376. if (this.property_grid.SelectedGridItem != null) {
  377. PropertyDescriptor desc = property_grid.SelectedGridItem.PropertyDescriptor;
  378. if (desc != null) {
  379. desc.SetValue(property_grid.SelectedObject, listBox.SelectedItem);
  380. }
  381. }
  382. dropdown_form.Hide();
  383. Redraw();
  384. }
  385. private void grid_textbox_DropDownButtonClicked (object sender, EventArgs e)
  386. {
  387. dropdown_form.Location = PointToScreen(new Point(grid_textbox.Location.X,grid_textbox.Location.Y+ROW_HEIGHT));
  388. dropdown_form.Width = grid_textbox.Width;
  389. dropdown_form.Show();
  390. }
  391. }
  392. }