|
| 1 | +#region Copyright © 2015 Couchcoding |
| 2 | + |
| 3 | +// File: ColorDropDown.cs |
| 4 | +// Package: GuiLibrary |
| 5 | +// Project: Logbert |
| 6 | +// |
| 7 | +// The MIT License (MIT) |
| 8 | +// |
| 9 | +// Copyright (c) 2015 Couchcoding |
| 10 | +// |
| 11 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | +// of this software and associated documentation files (the "Software"), to deal |
| 13 | +// in the Software without restriction, including without limitation the rights |
| 14 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | +// copies of the Software, and to permit persons to whom the Software is |
| 16 | +// furnished to do so, subject to the following conditions: |
| 17 | +// |
| 18 | +// The above copyright notice and this permission notice shall be included in |
| 19 | +// all copies or substantial portions of the Software. |
| 20 | +// |
| 21 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | +// THE SOFTWARE. |
| 28 | + |
| 29 | +#endregion |
| 30 | + |
| 31 | +using System; |
| 32 | +using System.Drawing; |
| 33 | +using System.Windows.Forms; |
| 34 | + |
| 35 | +namespace Com.Couchcoding.GuiLibrary.Controls |
| 36 | +{ |
| 37 | + /// <summary> |
| 38 | + /// Implements a <see cref="ComboBox"/> <see cref="Control"/> to select a <see cref="Color"/>. |
| 39 | + /// </summary> |
| 40 | + public class ColorDropDown : ComboBox |
| 41 | + { |
| 42 | + #region Public Types |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Simple object to map <see cref="Color"/>s and strings. |
| 46 | + /// </summary> |
| 47 | + public class ColorInfo |
| 48 | + { |
| 49 | + #region Public Properties |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Gets or sets the text of the <see cref="ColorInfo"/> object. |
| 53 | + /// </summary> |
| 54 | + public string Text |
| 55 | + { |
| 56 | + get; |
| 57 | + set; |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Gets or sets the <see cref=" Color"/> the <see cref="ColorInfo"/> object represents. |
| 62 | + /// </summary> |
| 63 | + public Color Color |
| 64 | + { |
| 65 | + get; |
| 66 | + set; |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Determines whether the <see cref="ColorInfo"/> is a custom <see cref="Color"/>, or not. |
| 71 | + /// </summary> |
| 72 | + public bool IsCustomColor |
| 73 | + { |
| 74 | + get; |
| 75 | + private set; |
| 76 | + } |
| 77 | + |
| 78 | + #endregion |
| 79 | + |
| 80 | + #region Public Methods |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Get a new <see cref="ColorInfo"/> instance initialized by the given <paramref name="color"/>. |
| 84 | + /// </summary> |
| 85 | + /// <param name="color">The <see cref="Color"/> to initialize the <see cref="ColorInfo"/> from.</param> |
| 86 | + /// <param name="text">The optional text of the new <see cref="ColorInfo"/>.</param> |
| 87 | + /// <param name="isCustomColor">Determines whether the <see cref="ColorInfo"/> is a custom <see cref="Color"/>, or not.</param> |
| 88 | + /// <returns>A new <see cref="ColorInfo"/> instance initialized by the given <paramref name="color"/>.</returns> |
| 89 | + public static ColorInfo FromColor(Color color, string text = "", bool isCustomColor = false) |
| 90 | + { |
| 91 | + return new ColorInfo(string.IsNullOrEmpty(text) |
| 92 | + ? color.Name |
| 93 | + : text, color, isCustomColor); |
| 94 | + } |
| 95 | + |
| 96 | + #endregion |
| 97 | + |
| 98 | + #region Constructor |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Creates a new instance of the <see cref="ColorInfo"/> object. |
| 102 | + /// </summary> |
| 103 | + /// <param name="text">The text of the new <see cref="ColorInfo"/> instance.</param> |
| 104 | + /// <param name="color">The <see cref="Color"/> of the new <see cref=" ColorInfo"/> instance.</param> |
| 105 | + /// <param name="isCustomColor">Determines whether the <see cref="ColorInfo"/> is a custom <see cref="Color"/>, or not.</param> |
| 106 | + private ColorInfo(string text, Color color, bool isCustomColor = false) |
| 107 | + { |
| 108 | + Text = text; |
| 109 | + Color = color; |
| 110 | + IsCustomColor = isCustomColor; |
| 111 | + } |
| 112 | + |
| 113 | + #endregion |
| 114 | + } |
| 115 | + |
| 116 | + #endregion |
| 117 | + |
| 118 | + #region Public Properties |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Gets or sets the currently selected item. |
| 122 | + /// </summary> |
| 123 | + public new ColorInfo SelectedItem |
| 124 | + { |
| 125 | + get |
| 126 | + { |
| 127 | + return (ColorInfo)base.SelectedItem; |
| 128 | + } |
| 129 | + set |
| 130 | + { |
| 131 | + base.SelectedItem = value; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /// <summary> |
| 136 | + /// Gets the text of the selected item, or sets the selection to |
| 137 | + /// the item with the specified text. |
| 138 | + /// </summary> |
| 139 | + public new string SelectedText |
| 140 | + { |
| 141 | + get |
| 142 | + { |
| 143 | + return SelectedIndex >= 0 |
| 144 | + ? SelectedItem.Text |
| 145 | + : String.Empty; |
| 146 | + } |
| 147 | + set |
| 148 | + { |
| 149 | + for (int i = 0; i < Items.Count; i++) |
| 150 | + { |
| 151 | + if (((ColorInfo)Items[i]).Text == value) |
| 152 | + { |
| 153 | + SelectedIndex = i; |
| 154 | + break; |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /// <summary> |
| 161 | + /// Gets the value of the selected item, or sets the selection to |
| 162 | + /// the item with the specified value. |
| 163 | + /// </summary> |
| 164 | + public new Color SelectedValue |
| 165 | + { |
| 166 | + get |
| 167 | + { |
| 168 | + return SelectedIndex >= 0 |
| 169 | + ? SelectedItem.Color |
| 170 | + : Color.White; |
| 171 | + } |
| 172 | + set |
| 173 | + { |
| 174 | + for (int i = 0; i < Items.Count; ++i) |
| 175 | + { |
| 176 | + if (((ColorInfo)Items[i]).Color == value) |
| 177 | + { |
| 178 | + if (!((ColorInfo)Items[i]).IsCustomColor) |
| 179 | + { |
| 180 | + // Reset the custom color value |
| 181 | + SetCustomColor(Color.Transparent); |
| 182 | + } |
| 183 | + |
| 184 | + SelectedIndex = i; |
| 185 | + return; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + // Update the custom color value. |
| 190 | + SetCustomColor(value); |
| 191 | + |
| 192 | + // Set the custom color selected. |
| 193 | + SelectedIndex = Items.Count - 1; |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + #endregion |
| 198 | + |
| 199 | + #region Public Methods |
| 200 | + |
| 201 | + /// <summary> |
| 202 | + /// Populate control with standard <see cref="Color"/>s. |
| 203 | + /// </summary> |
| 204 | + public void AddStandardColors(ColorInfo customColor = null) |
| 205 | + { |
| 206 | + Items.Clear(); |
| 207 | + |
| 208 | + Items.Add(ColorInfo.FromColor(Color.Black)); |
| 209 | + Items.Add(ColorInfo.FromColor(Color.Blue)); |
| 210 | + Items.Add(ColorInfo.FromColor(Color.Lime)); |
| 211 | + Items.Add(ColorInfo.FromColor(Color.Cyan)); |
| 212 | + Items.Add(ColorInfo.FromColor(Color.Red)); |
| 213 | + Items.Add(ColorInfo.FromColor(Color.Fuchsia)); |
| 214 | + Items.Add(ColorInfo.FromColor(Color.Yellow)); |
| 215 | + Items.Add(ColorInfo.FromColor(Color.White)); |
| 216 | + Items.Add(ColorInfo.FromColor(Color.Navy)); |
| 217 | + Items.Add(ColorInfo.FromColor(Color.Green)); |
| 218 | + Items.Add(ColorInfo.FromColor(Color.Teal)); |
| 219 | + Items.Add(ColorInfo.FromColor(Color.Maroon)); |
| 220 | + Items.Add(ColorInfo.FromColor(Color.Purple)); |
| 221 | + Items.Add(ColorInfo.FromColor(Color.Olive)); |
| 222 | + Items.Add(ColorInfo.FromColor(Color.Gray)); |
| 223 | + |
| 224 | + if (customColor != null) |
| 225 | + { |
| 226 | + Items.Add(customColor); |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + /// <summary> |
| 231 | + /// Sets the <see cref="Color"/> of the custom <see cref="Color"/> item. |
| 232 | + /// </summary> |
| 233 | + /// <param name="color">The custom <see cref="Color"/> to set.</param> |
| 234 | + public void SetCustomColor(Color color) |
| 235 | + { |
| 236 | + if (Items.Count > 0 && ((ColorInfo)Items[Items.Count - 1]).IsCustomColor) |
| 237 | + { |
| 238 | + ((ColorInfo)Items[Items.Count - 1]).Color = color; |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + #endregion |
| 243 | + |
| 244 | + #region Private Methods |
| 245 | + |
| 246 | + /// <summary> |
| 247 | + /// Raises the <see cref="E:System.Windows.Forms.ComboBox.DrawItem"/> event. |
| 248 | + /// </summary> |
| 249 | + /// <param name="e">A <see cref="T:System.Windows.Forms.DrawItemEventArgs"/> that contains the event data. </param> |
| 250 | + protected override void OnDrawItem(DrawItemEventArgs e) |
| 251 | + { |
| 252 | + if (e.Index >= 0) |
| 253 | + { |
| 254 | + ColorInfo color = (ColorInfo)Items[e.Index]; |
| 255 | + |
| 256 | + e.DrawBackground(); |
| 257 | + |
| 258 | + // Draw the color box |
| 259 | + Rectangle rect = new Rectangle(); |
| 260 | + rect.X = e.Bounds.X + 2; |
| 261 | + rect.Y = e.Bounds.Y + 2; |
| 262 | + rect.Width = 18; |
| 263 | + rect.Height = e.Bounds.Height - 5; |
| 264 | + |
| 265 | + using (SolidBrush cbBrush = new SolidBrush(color.Color)) |
| 266 | + { |
| 267 | + e.Graphics.FillRectangle(cbBrush, rect); |
| 268 | + } |
| 269 | + |
| 270 | + e.Graphics.DrawRectangle( |
| 271 | + SystemPens.WindowText |
| 272 | + , rect); |
| 273 | + |
| 274 | + // Enable smooth drawing. |
| 275 | + e.Graphics.TextRenderingHint = System.Drawing. |
| 276 | + Text.TextRenderingHint.ClearTypeGridFit; |
| 277 | + |
| 278 | + // Write the color name |
| 279 | + Brush brush = (e.State & DrawItemState.Selected) != DrawItemState.None |
| 280 | + ? SystemBrushes.HighlightText |
| 281 | + : SystemBrushes.WindowText; |
| 282 | + |
| 283 | + e.Graphics.DrawString(color.Text, Font, brush, |
| 284 | + e.Bounds.X + rect.X + rect.Width + 2, |
| 285 | + e.Bounds.Y + ((e.Bounds.Height - Font.Height) >> 1)); |
| 286 | + |
| 287 | + // Draw the focus rectangle if appropriate |
| 288 | + if ((e.State & DrawItemState.NoFocusRect) == DrawItemState.None) |
| 289 | + { |
| 290 | + e.DrawFocusRectangle(); |
| 291 | + } |
| 292 | + } |
| 293 | + } |
| 294 | + |
| 295 | + #endregion |
| 296 | + |
| 297 | + #region Constructor |
| 298 | + |
| 299 | + /// <summary> |
| 300 | + /// Creates a new instance of the <see cref="ColorDropDown"/> <see cref="Control"/>. |
| 301 | + /// </summary> |
| 302 | + public ColorDropDown() |
| 303 | + { |
| 304 | + DropDownStyle = ComboBoxStyle.DropDownList; |
| 305 | + DrawMode = DrawMode.OwnerDrawFixed; |
| 306 | + } |
| 307 | + |
| 308 | + #endregion |
| 309 | + } |
| 310 | +} |
0 commit comments