PageRenderTime 63ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/src/ToolConfigStrip.cs

https://bitbucket.org/tuldok89/openpdn
C# | 2979 lines | 2335 code | 419 blank | 225 comment | 220 complexity | b71bda30139507e48234f6a58848dd64 MD5 | raw file
  1. /////////////////////////////////////////////////////////////////////////////////
  2. // Paint.NET //
  3. // Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
  4. // Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
  5. // See src/Resources/Files/License.txt for full licensing and attribution //
  6. // details. //
  7. // . //
  8. /////////////////////////////////////////////////////////////////////////////////
  9. using System.Linq;
  10. using PaintDotNet.Base;
  11. using PaintDotNet.SystemLayer;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.ComponentModel;
  15. using System.Drawing;
  16. using System.Drawing.Drawing2D;
  17. using System.Threading;
  18. using System.Windows.Forms;
  19. namespace PaintDotNet
  20. {
  21. // TODO: for 4.0, refactor into smaller ToolConfigStrip "Sections"
  22. // better yet, use IndirectUI
  23. internal class ToolConfigStrip
  24. : ToolStripEx,
  25. IBrushConfig,
  26. IShapeTypeConfig,
  27. IPenConfig,
  28. IAntiAliasingConfig,
  29. IAlphaBlendingConfig,
  30. ITextConfig,
  31. IToleranceConfig,
  32. IColorPickerConfig,
  33. IGradientConfig,
  34. IResamplingConfig,
  35. ISelectionCombineModeConfig,
  36. IFloodModeConfig,
  37. ISelectionDrawModeConfig
  38. {
  39. private ToolBarConfigItems _toolBarConfigItems = ToolBarConfigItems.None;
  40. private readonly EnumLocalizer _hatchStyleNames = EnumLocalizer.Create(typeof(HatchStyle));
  41. private readonly string _solidBrushText;
  42. private readonly ImageResource _shapeOutlineImage = PdnResources.GetImageResource("Icons.ShapeOutlineIcon.png");
  43. private readonly ImageResource _shapeInteriorImage = PdnResources.GetImageResource("Icons.ShapeInteriorIcon.png");
  44. private readonly ImageResource _shapeBothImage = PdnResources.GetImageResource("Icons.ShapeBothIcon.png");
  45. private ToolStripSeparator _brushSeparator;
  46. private ToolStripLabel _brushStyleLabel;
  47. private ToolStripComboBox _brushStyleComboBox;
  48. private ToolStripSeparator _shapeSeparator;
  49. private ToolStripSplitButton _shapeButton;
  50. private ToolStripSeparator _penSeparator;
  51. private ToolStripLabel _penSizeLabel;
  52. private ToolStripButton _penSizeDecButton;
  53. private ToolStripComboBox _penSizeComboBox;
  54. private ToolStripButton _penSizeIncButton;
  55. private ToolStripLabel _penStyleLabel;
  56. private ToolStripSplitButton _penStartCapSplitButton; // Tag property is used to store chosen LineCap value
  57. private ToolStripSplitButton _penDashStyleSplitButton; // Tag property is used to store chosen DashStyle value
  58. private ToolStripSplitButton _penEndCapSplitButton; // Tag property is used to store chosen LineCap value
  59. private readonly EnumLocalizer _lineCapLocalizer = EnumLocalizer.Create(typeof(LineCap2));
  60. private readonly EnumLocalizer _dashStyleLocalizer = EnumLocalizer.Create(typeof(DashStyle));
  61. private ToolStripSeparator _blendingSeparator;
  62. private ToolStripSplitButton _alphaBlendingSplitButton;
  63. private bool _alphaBlendingEnabled = true;
  64. private readonly ImageResource _alphaBlendingEnabledImage;
  65. private readonly ImageResource _alphaBlendingOverwriteImage;
  66. private ToolStripSplitButton _antiAliasingSplitButton;
  67. private bool _antiAliasingEnabled = true;
  68. private readonly ImageResource _antiAliasingEnabledImage;
  69. private readonly ImageResource _antiAliasingDisabledImage;
  70. private readonly EnumLocalizer _resamplingAlgorithmNames = EnumLocalizer.Create(typeof(ResamplingAlgorithm));
  71. private ToolStripSeparator _resamplingSeparator;
  72. private ToolStripLabel _resamplingLabel;
  73. private ToolStripComboBox _resamplingComboBox;
  74. private readonly EnumLocalizer _colorPickerBehaviorNames = EnumLocalizer.Create(typeof(ColorPickerClickBehavior));
  75. private ToolStripSeparator _colorPickerSeparator;
  76. private ToolStripLabel _colorPickerLabel;
  77. private ToolStripComboBox _colorPickerComboBox;
  78. private ToolStripSeparator _toleranceSeparator;
  79. private ToolStripLabel _toleranceLabel;
  80. private ToolStripControlHost _toleranceSliderStrip;
  81. private ToleranceSliderControl _toleranceSlider;
  82. private readonly LineCap2[] _lineCaps =
  83. new[]
  84. {
  85. LineCap2.Flat,
  86. LineCap2.Arrow,
  87. LineCap2.ArrowFilled,
  88. LineCap2.Rounded
  89. };
  90. private readonly DashStyle[] _dashStyles =
  91. new[]
  92. {
  93. DashStyle.Solid,
  94. DashStyle.Dash,
  95. DashStyle.DashDot,
  96. DashStyle.DashDotDot,
  97. DashStyle.Dot
  98. };
  99. private const float MinPenSize = 1.0f;
  100. private const float MaxPenSize = 500.0f;
  101. private readonly int[] _brushSizes =
  102. new[]
  103. {
  104. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  105. 11, 12, 13, 14, 15, 20, 25, 30,
  106. 35, 40, 45, 50, 55, 60, 65, 70,
  107. 75, 80, 85, 90, 95, 100, 125,
  108. 150, 175, 200, 225, 250, 275, 300,
  109. 325, 350, 375, 400, 425, 450, 475,
  110. 500
  111. };
  112. private ShapeDrawType _shapeDrawType;
  113. private readonly EnumLocalizer _gradientTypeNames = EnumLocalizer.Create(typeof(GradientType));
  114. private GradientInfo _gradientInfo = new GradientInfo(GradientType.LinearClamped, false);
  115. private ToolStripSeparator _gradientSeparator1;
  116. private ToolStripButton _gradientLinearClampedButton;
  117. private ToolStripButton _gradientLinearReflectedButton;
  118. private ToolStripButton _gradientLinearDiamondButton;
  119. private ToolStripButton _gradientRadialButton;
  120. private ToolStripButton _gradientConicalButton;
  121. private ToolStripSeparator _gradientSeparator2;
  122. private ToolStripSplitButton _gradientChannelsSplitButton;
  123. private readonly ImageResource _gradientAllColorChannelsImage;
  124. private readonly ImageResource _gradientAlphaChannelOnlyImage;
  125. private readonly EnumLocalizer _fontSmoothingLocalizer = EnumLocalizer.Create(typeof(FontSmoothing));
  126. private const int MaxFontSize = 2000;
  127. private const int MinFontSize = 1;
  128. private const int InitialFontSize = 12;
  129. private readonly FontFamily _arialFontFamily;
  130. private FontStyle _fontStyle;
  131. private TextAlignment _alignment;
  132. private float _oldSizeValue;
  133. private readonly Brush _highlightBrush;
  134. private readonly Brush _highlightTextBrush;
  135. private readonly Brush _windowBrush;
  136. private readonly Brush _windowTextBrush;
  137. private readonly Font _arialFontBase;
  138. private const string ArialName = "Arial";
  139. private static readonly ManualResetEvent StaticFontNamesPopulatedEvent = new ManualResetEvent(false);
  140. private static List<string> _staticFontNames;
  141. private bool _fontsComboBoxPopulated;
  142. private ToolStripSeparator _fontSeparator;
  143. private ToolStripLabel _fontLabel;
  144. private ToolStripComboBox _fontFamilyComboBox;
  145. private ToolStripComboBox _fontSizeComboBox;
  146. private ToolStripComboBox _fontSmoothingComboBox;
  147. private ToolStripSeparator _fontStyleSeparator;
  148. private ToolStripButton _fontBoldButton;
  149. private ToolStripButton _fontItalicsButton;
  150. private ToolStripButton _fontUnderlineButton;
  151. private ToolStripSeparator _fontAlignSeparator;
  152. private ToolStripButton _fontAlignLeftButton;
  153. private ToolStripButton _fontAlignCenterButton;
  154. private ToolStripButton _fontAlignRightButton;
  155. private readonly int[] _defaultFontSizes =
  156. new[]
  157. {
  158. 8, 9, 10, 11, 12, 14, 16, 18, 20,
  159. 22, 24, 26, 28, 36, 48, 72, 84, 96,
  160. 108, 144, 192, 216, 288
  161. };
  162. private ToolStripSeparator _selectionCombineModeSeparator;
  163. private ToolStripLabel _selectionCombineModeLabel;
  164. private ToolStripSplitButton _selectionCombineModeSplitButton;
  165. private ToolStripSeparator _floodModeSeparator;
  166. private ToolStripLabel _floodModeLabel;
  167. private ToolStripSplitButton _floodModeSplitButton;
  168. private SelectionDrawModeInfo _selectionDrawModeInfo;
  169. private ToolStripSeparator _selectionDrawModeSeparator;
  170. private ToolStripLabel _selectionDrawModeModeLabel;
  171. private ToolStripSplitButton _selectionDrawModeSplitButton;
  172. private ToolStripLabel _selectionDrawModeWidthLabel;
  173. private ToolStripTextBox _selectionDrawModeWidthTextBox;
  174. private ToolStripButton _selectionDrawModeSwapButton;
  175. private ToolStripLabel _selectionDrawModeHeightLabel;
  176. private ToolStripTextBox _selectionDrawModeHeightTextBox;
  177. private UnitsComboBoxStrip _selectionDrawModeUnits;
  178. public event EventHandler SelectionDrawModeUnitsChanging;
  179. protected void OnSelectionDrawModeUnitsChanging()
  180. {
  181. if (SelectionDrawModeUnitsChanging != null)
  182. {
  183. SelectionDrawModeUnitsChanging(this, EventArgs.Empty);
  184. }
  185. }
  186. public event EventHandler SelectionDrawModeUnitsChanged;
  187. protected void OnSelectionDrawModeUnitsChanged()
  188. {
  189. if (SelectionDrawModeUnitsChanged != null)
  190. {
  191. SelectionDrawModeUnitsChanged(this, EventArgs.Empty);
  192. }
  193. }
  194. public void LoadFromAppEnvironment(AppEnvironment appEnvironment)
  195. {
  196. AlphaBlending = appEnvironment.AlphaBlending;
  197. AntiAliasing = appEnvironment.AntiAliasing;
  198. BrushInfo = appEnvironment.BrushInfo;
  199. ColorPickerClickBehavior = appEnvironment.ColorPickerClickBehavior;
  200. GradientInfo = appEnvironment.GradientInfo;
  201. PenInfo = appEnvironment.PenInfo;
  202. ResamplingAlgorithm = appEnvironment.ResamplingAlgorithm;
  203. ShapeDrawType = appEnvironment.ShapeDrawType;
  204. FontInfo = appEnvironment.FontInfo;
  205. FontSmoothing = appEnvironment.FontSmoothing;
  206. FontAlignment = appEnvironment.TextAlignment;
  207. Tolerance = appEnvironment.Tolerance;
  208. SelectionCombineMode = appEnvironment.SelectionCombineMode;
  209. FloodMode = appEnvironment.FloodMode;
  210. SelectionDrawModeInfo = appEnvironment.SelectionDrawModeInfo;
  211. }
  212. public event EventHandler BrushInfoChanged;
  213. protected virtual void OnBrushChanged()
  214. {
  215. if (BrushInfoChanged != null)
  216. {
  217. BrushInfoChanged(this, EventArgs.Empty);
  218. }
  219. }
  220. public BrushInfo BrushInfo
  221. {
  222. get
  223. {
  224. if (_brushStyleComboBox.SelectedItem.ToString() == _solidBrushText)
  225. {
  226. return new BrushInfo(BrushType.Solid, HatchStyle.BackwardDiagonal);
  227. }
  228. if (_brushStyleComboBox.SelectedIndex == -1)
  229. {
  230. return new BrushInfo(BrushType.Solid, HatchStyle.BackwardDiagonal);
  231. }
  232. return new BrushInfo(
  233. BrushType.Hatch,
  234. (HatchStyle)_hatchStyleNames.LocalizedNameToEnumValue(_brushStyleComboBox.SelectedItem.ToString()));
  235. }
  236. set {
  237. _brushStyleComboBox.SelectedItem = value.BrushType == BrushType.Solid ? _solidBrushText : _hatchStyleNames.EnumValueToLocalizedName(value.HatchStyle);
  238. }
  239. }
  240. public event EventHandler GradientInfoChanged;
  241. protected virtual void OnGradientInfoChanged()
  242. {
  243. if (GradientInfoChanged != null)
  244. {
  245. GradientInfoChanged(this, EventArgs.Empty);
  246. }
  247. }
  248. public void PerformGradientInfoChanged()
  249. {
  250. OnGradientInfoChanged();
  251. }
  252. public GradientInfo GradientInfo
  253. {
  254. get
  255. {
  256. return _gradientInfo;
  257. }
  258. set
  259. {
  260. if (value == null)
  261. {
  262. throw new ArgumentNullException();
  263. }
  264. _gradientInfo = value;
  265. OnGradientInfoChanged();
  266. SyncGradientInfo();
  267. }
  268. }
  269. private void SyncGradientInfo()
  270. {
  271. _gradientConicalButton.Checked = false;
  272. _gradientRadialButton.Checked = false;
  273. _gradientLinearClampedButton.Checked = false;
  274. _gradientLinearReflectedButton.Checked = false;
  275. _gradientLinearDiamondButton.Checked = false;
  276. switch (_gradientInfo.GradientType)
  277. {
  278. case GradientType.Conical:
  279. _gradientConicalButton.Checked = true;
  280. break;
  281. case GradientType.LinearClamped:
  282. _gradientLinearClampedButton.Checked = true;
  283. break;
  284. case GradientType.LinearReflected:
  285. _gradientLinearReflectedButton.Checked = true;
  286. break;
  287. case GradientType.LinearDiamond:
  288. _gradientLinearDiamondButton.Checked = true;
  289. break;
  290. case GradientType.Radial:
  291. _gradientRadialButton.Checked = true;
  292. break;
  293. default:
  294. throw new InvalidEnumArgumentException();
  295. }
  296. _gradientChannelsSplitButton.Image = _gradientInfo.AlphaOnly ? _gradientAlphaChannelOnlyImage.Reference : _gradientAllColorChannelsImage.Reference;
  297. }
  298. private void ShapeButtonDropDownOpening(object sender, EventArgs e)
  299. {
  300. var outlineMI = new ToolStripMenuItem
  301. {
  302. Text = PdnResources.GetString("ShapeDrawTypeConfigWidget.OutlineButton.ToolTipText"),
  303. Image = _shapeOutlineImage.Reference,
  304. Tag = ShapeDrawType.Outline
  305. };
  306. outlineMI.Click += ShapeMIClick;
  307. var interiorMI = new ToolStripMenuItem
  308. {
  309. Text =
  310. PdnResources.GetString("ShapeDrawTypeConfigWidget.InteriorButton.ToolTipText"),
  311. Image = _shapeInteriorImage.Reference,
  312. Tag = ShapeDrawType.Interior
  313. };
  314. interiorMI.Click += ShapeMIClick;
  315. var bothMI = new ToolStripMenuItem
  316. {
  317. Text = PdnResources.GetString("ShapeDrawTypeConfigWidget.BothButton.ToolTipText"),
  318. Image = _shapeBothImage.Reference,
  319. Tag = ShapeDrawType.Both
  320. };
  321. bothMI.Click += ShapeMIClick;
  322. switch (_shapeDrawType)
  323. {
  324. case ShapeDrawType.Outline:
  325. outlineMI.Checked = true;
  326. break;
  327. case ShapeDrawType.Interior:
  328. interiorMI.Checked = true;
  329. break;
  330. case ShapeDrawType.Both:
  331. bothMI.Checked = true;
  332. break;
  333. default:
  334. throw new InvalidEnumArgumentException();
  335. }
  336. _shapeButton.DropDownItems.AddRange(
  337. new ToolStripItem[]
  338. {
  339. outlineMI,
  340. interiorMI,
  341. bothMI
  342. });
  343. }
  344. private void ShapeMIClick(object sender, EventArgs e)
  345. {
  346. var sdt = (ShapeDrawType)((ToolStripMenuItem)sender).Tag;
  347. Tracing.LogFeature("ToolConfigStrip(" + sdt + ")");
  348. ShapeDrawType = sdt;
  349. }
  350. public ToolConfigStrip()
  351. {
  352. SuspendLayout();
  353. InitializeComponent();
  354. _solidBrushText = PdnResources.GetString("BrushConfigWidget.SolidBrush.Text"); // "Solid Brush"
  355. _brushStyleComboBox.Items.Add(_solidBrushText);
  356. string[] styleNames = _hatchStyleNames.GetLocalizedNames();
  357. Array.Sort(styleNames);
  358. foreach (string styleName in styleNames)
  359. {
  360. _brushStyleComboBox.Items.Add(styleName);
  361. }
  362. _brushStyleComboBox.SelectedIndex = 0;
  363. _brushStyleLabel.Text = PdnResources.GetString("BrushConfigWidget.FillStyleLabel.Text");
  364. _shapeDrawType = ShapeDrawType.Outline;
  365. _shapeButton.Image = _shapeOutlineImage.Reference;
  366. _penSizeLabel.Text = PdnResources.GetString("PenConfigWidget.BrushWidthLabel");
  367. _penSizeComboBox.ComboBox.SuspendLayout();
  368. for (int i = 0; i < _brushSizes.Length; ++i)
  369. {
  370. _penSizeComboBox.Items.Add(_brushSizes[i].ToString());
  371. }
  372. _penSizeComboBox.ComboBox.ResumeLayout(false);
  373. _penSizeComboBox.SelectedIndex = 1; // default to brush size of 2
  374. _penSizeDecButton.ToolTipText = PdnResources.GetString("ToolConfigStrip.PenSizeDecButton.ToolTipText");
  375. _penSizeDecButton.Image = PdnResources.GetImageResource("Icons.MinusButtonIcon.png").Reference;
  376. _penSizeIncButton.ToolTipText = PdnResources.GetString("ToolConfigStrip.PenSizeIncButton.ToolTipText");
  377. _penSizeIncButton.Image = PdnResources.GetImageResource("Icons.PlusButtonIcon.png").Reference;
  378. _penStyleLabel.Text = PdnResources.GetString("ToolConfigStrip.PenStyleLabel.Text");
  379. _penStartCapSplitButton.Tag = PenInfo.DefaultLineCap;
  380. _penStartCapSplitButton.Image = GetLineCapImage(PenInfo.DefaultLineCap, true).Reference;
  381. _penStartCapSplitButton.ToolTipText = PdnResources.GetString("ToolConfigStrip.PenStartCapSplitButton.ToolTipText");
  382. _penDashStyleSplitButton.Tag = PenInfo.DefaultDashStyle;
  383. _penDashStyleSplitButton.Image = GetDashStyleImage(PenInfo.DefaultDashStyle);
  384. _penDashStyleSplitButton.ToolTipText = PdnResources.GetString("ToolConfigStrip.PenDashStyleSplitButton.ToolTipText");
  385. _penEndCapSplitButton.Tag = PenInfo.DefaultLineCap;
  386. _penEndCapSplitButton.Image = GetLineCapImage(PenInfo.DefaultLineCap, false).Reference;
  387. _penEndCapSplitButton.ToolTipText = PdnResources.GetString("ToolConfigStrip.PenEndCapSplitButton.ToolTipText");
  388. _gradientLinearClampedButton.ToolTipText = _gradientTypeNames.EnumValueToLocalizedName(GradientType.LinearClamped);
  389. _gradientLinearClampedButton.Image = PdnResources.GetImageResource("Icons.LinearClampedGradientIcon.png").Reference;
  390. _gradientLinearReflectedButton.ToolTipText = _gradientTypeNames.EnumValueToLocalizedName(GradientType.LinearReflected);
  391. _gradientLinearReflectedButton.Image = PdnResources.GetImageResource("Icons.LinearReflectedGradientIcon.png").Reference;
  392. _gradientLinearDiamondButton.ToolTipText = _gradientTypeNames.EnumValueToLocalizedName(GradientType.LinearDiamond);
  393. _gradientLinearDiamondButton.Image = PdnResources.GetImageResource("Icons.LinearDiamondGradientIcon.png").Reference;
  394. _gradientRadialButton.ToolTipText = _gradientTypeNames.EnumValueToLocalizedName(GradientType.Radial);
  395. _gradientRadialButton.Image = PdnResources.GetImageResource("Icons.RadialGradientIcon.png").Reference;
  396. _gradientConicalButton.ToolTipText = _gradientTypeNames.EnumValueToLocalizedName(GradientType.Conical);
  397. _gradientConicalButton.Image = PdnResources.GetImageResource("Icons.ConicalGradientIcon.png").Reference;
  398. _gradientAllColorChannelsImage = PdnResources.GetImageResource("Icons.AllColorChannelsIcon.png");
  399. _gradientAlphaChannelOnlyImage = PdnResources.GetImageResource("Icons.AlphaChannelOnlyIcon.png");
  400. _gradientChannelsSplitButton.Image = _gradientAllColorChannelsImage.Reference;
  401. _antiAliasingEnabledImage = PdnResources.GetImageResource("Icons.AntiAliasingEnabledIcon.png");
  402. _antiAliasingDisabledImage = PdnResources.GetImageResource("Icons.AntiAliasingDisabledIcon.png");
  403. _antiAliasingSplitButton.Image = _antiAliasingEnabledImage.Reference;
  404. _alphaBlendingEnabledImage = PdnResources.GetImageResource("Icons.BlendingEnabledIcon.png");
  405. _alphaBlendingOverwriteImage = PdnResources.GetImageResource("Icons.BlendingOverwriteIcon.png");
  406. _alphaBlendingSplitButton.Image = _alphaBlendingEnabledImage.Reference;
  407. _penSizeComboBox.Size = new Size(UI.ScaleWidth(_penSizeComboBox.Width), _penSizeComboBox.Height);
  408. _brushStyleComboBox.Size = new Size(UI.ScaleWidth(_brushStyleComboBox.Width), _brushStyleComboBox.Height);
  409. _brushStyleComboBox.DropDownWidth = UI.ScaleWidth(_brushStyleComboBox.DropDownWidth);
  410. _brushStyleComboBox.DropDownHeight = UI.ScaleHeight(_brushStyleComboBox.DropDownHeight);
  411. _toleranceLabel.Text = PdnResources.GetString("ToleranceConfig.ToleranceLabel.Text");
  412. _toleranceSlider.Tolerance = 0.5f;
  413. if (_fontSizeComboBox.ComboBox != null) _fontSizeComboBox.ComboBox.SuspendLayout();
  414. for (int i = 0; i < _defaultFontSizes.Length; ++i)
  415. {
  416. _fontSizeComboBox.Items.Add(_defaultFontSizes[i].ToString());
  417. }
  418. if (_fontSizeComboBox.ComboBox != null) _fontSizeComboBox.ComboBox.ResumeLayout(false);
  419. _fontSmoothingComboBox.Items.AddRange(
  420. new object[]
  421. {
  422. _fontSmoothingLocalizer.EnumValueToLocalizedName(FontSmoothing.Smooth),
  423. _fontSmoothingLocalizer.EnumValueToLocalizedName(FontSmoothing.Sharp)
  424. });
  425. _fontSmoothingComboBox.SelectedIndex = 0;
  426. _fontLabel.Text = PdnResources.GetString("TextConfigWidget.FontLabel.Text");
  427. try
  428. {
  429. _arialFontFamily = new FontFamily(ArialName);
  430. }
  431. catch (Exception)
  432. {
  433. _arialFontFamily = new FontFamily(System.Drawing.Text.GenericFontFamilies.SansSerif);
  434. }
  435. try
  436. {
  437. _arialFontBase = new Font(_arialFontFamily, InitialFontSize, FontStyle.Regular);
  438. }
  439. catch (Exception)
  440. {
  441. _arialFontBase = new Font(FontFamily.GenericSansSerif, InitialFontSize, FontStyle.Regular);
  442. }
  443. _fontFamilyComboBox.ComboBox.DropDownHeight = 600;
  444. _alignment = TextAlignment.Left;
  445. _fontAlignLeftButton.Checked = true;
  446. _oldSizeValue = InitialFontSize;
  447. _highlightBrush = new SolidBrush(SystemColors.Highlight);
  448. _highlightTextBrush = new SolidBrush(SystemColors.HighlightText);
  449. _windowBrush = new SolidBrush(SystemColors.Window);
  450. _windowTextBrush = new SolidBrush(SystemColors.WindowText);
  451. // These buttons need a color key to maintain consistency with v2.5 language packs
  452. _fontBoldButton.ImageTransparentColor = Utility.TransparentKey;
  453. _fontItalicsButton.ImageTransparentColor = Utility.TransparentKey;
  454. _fontUnderlineButton.ImageTransparentColor = Utility.TransparentKey;
  455. _fontBoldButton.Image = PdnResources.GetImageBmpOrPng("Icons.FontBoldIcon");
  456. _fontItalicsButton.Image = PdnResources.GetImageBmpOrPng("Icons.FontItalicIcon");
  457. _fontUnderlineButton.Image = PdnResources.GetImageBmpOrPng("Icons.FontUnderlineIcon");
  458. _fontAlignLeftButton.Image = PdnResources.GetImageResource("Icons.TextAlignLeftIcon.png").Reference;
  459. _fontAlignCenterButton.Image = PdnResources.GetImageResource("Icons.TextAlignCenterIcon.png").Reference;
  460. _fontAlignRightButton.Image = PdnResources.GetImageResource("Icons.TextAlignRightIcon.png").Reference;
  461. _fontBoldButton.ToolTipText = PdnResources.GetString("TextConfigWidget.BoldButton.ToolTipText");
  462. _fontItalicsButton.ToolTipText = PdnResources.GetString("TextConfigWidget.ItalicButton.ToolTipText");
  463. _fontUnderlineButton.ToolTipText = PdnResources.GetString("TextConfigWidget.UnderlineButton.ToolTipText");
  464. _fontAlignLeftButton.ToolTipText = PdnResources.GetString("TextConfigWidget.AlignLeftButton.ToolTipText");
  465. _fontAlignCenterButton.ToolTipText = PdnResources.GetString("TextConfigWidget.AlignCenterButton.ToolTipText");
  466. _fontAlignRightButton.ToolTipText = PdnResources.GetString("TextConfigWidget.AlignRightButton.ToolTipText");
  467. _fontFamilyComboBox.Size = new Size(UI.ScaleWidth(_fontFamilyComboBox.Width), _fontFamilyComboBox.Height);
  468. _fontFamilyComboBox.DropDownWidth = UI.ScaleWidth(_fontFamilyComboBox.DropDownWidth);
  469. _fontSizeComboBox.Size = new Size(UI.ScaleWidth(_fontSizeComboBox.Width), _fontSizeComboBox.Height);
  470. _fontSmoothingComboBox.Size = new Size(UI.ScaleWidth(_fontSmoothingComboBox.Width), _fontSmoothingComboBox.Height);
  471. _fontSmoothingComboBox.DropDownWidth = UI.ScaleWidth(_fontSmoothingComboBox.DropDownWidth);
  472. _resamplingLabel.Text = PdnResources.GetString("ToolConfigStrip.ResamplingLabel.Text");
  473. _resamplingComboBox.BeginUpdate();
  474. _resamplingComboBox.Items.Add(_resamplingAlgorithmNames.EnumValueToLocalizedName(ResamplingAlgorithm.Bilinear));
  475. _resamplingComboBox.Items.Add(_resamplingAlgorithmNames.EnumValueToLocalizedName(ResamplingAlgorithm.NearestNeighbor));
  476. _resamplingComboBox.EndUpdate();
  477. _resamplingComboBox.SelectedIndex = 0; // bilinear
  478. _resamplingComboBox.Size = new Size(UI.ScaleWidth(_resamplingComboBox.Width), _resamplingComboBox.Height);
  479. _resamplingComboBox.DropDownWidth = UI.ScaleWidth(_resamplingComboBox.DropDownWidth);
  480. _colorPickerLabel.Text = PdnResources.GetString("ToolConfigStrip.ColorPickerLabel.Text");
  481. string[] colorPickerBehaviorNames = _colorPickerBehaviorNames.GetLocalizedNames();
  482. // Make sure these items are sorted to be in the order specified by the enumeration
  483. Array.Sort(
  484. colorPickerBehaviorNames,
  485. delegate(string lhs, string rhs)
  486. {
  487. var lhsE = (ColorPickerClickBehavior)_colorPickerBehaviorNames.LocalizedNameToEnumValue(lhs);
  488. var rhsE = (ColorPickerClickBehavior)_colorPickerBehaviorNames.LocalizedNameToEnumValue(rhs);
  489. if ((int)lhsE < (int)rhsE)
  490. {
  491. return -1;
  492. }
  493. return (int) lhsE > (int) rhsE ? +1 : 0;
  494. });
  495. _colorPickerComboBox.Items.AddRange(colorPickerBehaviorNames);
  496. _colorPickerComboBox.SelectedIndex = 0;
  497. _colorPickerComboBox.Size = new Size(UI.ScaleWidth(_colorPickerComboBox.Width), _colorPickerComboBox.Height);
  498. _colorPickerComboBox.DropDownWidth = UI.ScaleWidth(_colorPickerComboBox.DropDownWidth);
  499. _toleranceSlider.Size = UI.ScaleSize(_toleranceSlider.Size);
  500. _selectionCombineModeLabel.Text = PdnResources.GetString("ToolConfigStrip.SelectionCombineModeLabel.Text");
  501. _floodModeLabel.Text = PdnResources.GetString("ToolConfigStrip.FloodModeLabel.Text");
  502. _selectionDrawModeModeLabel.Text = PdnResources.GetString("ToolConfigStrip.SelectionDrawModeLabel.Text");
  503. _selectionDrawModeWidthLabel.Text = PdnResources.GetString("ToolConfigStrip.SelectionDrawModeWidthLabel.Text");
  504. _selectionDrawModeHeightLabel.Text = PdnResources.GetString("ToolConfigStrip.SelectionDrawModeHeightLabel.Text");
  505. _selectionDrawModeSwapButton.Image = PdnResources.GetImageResource("Icons.ToolConfigStrip.SelectionDrawModeSwapButton.png").Reference;
  506. _selectionDrawModeWidthTextBox.Size = new Size(UI.ScaleWidth(_selectionDrawModeWidthTextBox.Width), _selectionDrawModeWidthTextBox.Height);
  507. _selectionDrawModeHeightTextBox.Size = new Size(UI.ScaleWidth(_selectionDrawModeHeightTextBox.Width), _selectionDrawModeHeightTextBox.Height);
  508. _selectionDrawModeUnits.Size = new Size(UI.ScaleWidth(_selectionDrawModeUnits.Width), _selectionDrawModeUnits.Height);
  509. ToolBarConfigItems = ToolBarConfigItems.None;
  510. ResumeLayout(false);
  511. }
  512. private void AsyncInitFontNames()
  513. {
  514. if (!IsHandleCreated)
  515. {
  516. CreateControl();
  517. }
  518. if (_fontFamilyComboBox.ComboBox != null)
  519. if (!_fontFamilyComboBox.ComboBox.IsHandleCreated)
  520. {
  521. _fontFamilyComboBox.ComboBox.CreateControl();
  522. }
  523. if (_staticFontNames == null)
  524. {
  525. ThreadPool.QueueUserWorkItem(PopulateFontsBackgroundThread, null);
  526. }
  527. }
  528. protected override void OnHandleCreated(EventArgs e)
  529. {
  530. if ((_toolBarConfigItems & ToolBarConfigItems.Text) == ToolBarConfigItems.Text)
  531. {
  532. AsyncInitFontNames();
  533. }
  534. base.OnHandleCreated(e);
  535. }
  536. private void InitializeComponent()
  537. {
  538. _brushSeparator = new ToolStripSeparator();
  539. _brushStyleLabel = new ToolStripLabel();
  540. _brushStyleComboBox = new ToolStripComboBox();
  541. _shapeSeparator = new ToolStripSeparator();
  542. _shapeButton = new ToolStripSplitButton();
  543. _gradientSeparator1 = new ToolStripSeparator();
  544. _gradientLinearClampedButton = new ToolStripButton();
  545. _gradientLinearReflectedButton = new ToolStripButton();
  546. _gradientLinearDiamondButton = new ToolStripButton();
  547. _gradientRadialButton = new ToolStripButton();
  548. _gradientConicalButton = new ToolStripButton();
  549. _gradientSeparator2 = new ToolStripSeparator();
  550. _gradientChannelsSplitButton = new ToolStripSplitButton();
  551. _penSeparator = new ToolStripSeparator();
  552. _penSizeLabel = new ToolStripLabel();
  553. _penSizeDecButton = new ToolStripButton();
  554. _penSizeComboBox = new ToolStripComboBox();
  555. _penSizeIncButton = new ToolStripButton();
  556. _penStyleLabel = new ToolStripLabel();
  557. _penStartCapSplitButton = new ToolStripSplitButton();
  558. _penDashStyleSplitButton = new ToolStripSplitButton();
  559. _penEndCapSplitButton = new ToolStripSplitButton();
  560. _blendingSeparator = new ToolStripSeparator();
  561. _antiAliasingSplitButton = new ToolStripSplitButton();
  562. _alphaBlendingSplitButton = new ToolStripSplitButton();
  563. _toleranceSeparator = new ToolStripSeparator();
  564. _toleranceLabel = new ToolStripLabel();
  565. _toleranceSlider = new ToleranceSliderControl();
  566. _toleranceSliderStrip = new ToolStripControlHost(_toleranceSlider);
  567. _fontSeparator = new ToolStripSeparator();
  568. _fontLabel = new ToolStripLabel();
  569. _fontFamilyComboBox = new ToolStripComboBox();
  570. _fontSizeComboBox = new ToolStripComboBox();
  571. _fontSmoothingComboBox = new ToolStripComboBox();
  572. _fontStyleSeparator = new ToolStripSeparator();
  573. _fontBoldButton = new ToolStripButton();
  574. _fontItalicsButton = new ToolStripButton();
  575. _fontUnderlineButton = new ToolStripButton();
  576. _fontAlignSeparator = new ToolStripSeparator();
  577. _fontAlignLeftButton = new ToolStripButton();
  578. _fontAlignCenterButton = new ToolStripButton();
  579. _fontAlignRightButton = new ToolStripButton();
  580. _resamplingSeparator = new ToolStripSeparator();
  581. _resamplingLabel = new ToolStripLabel();
  582. _resamplingComboBox = new ToolStripComboBox();
  583. _colorPickerSeparator = new ToolStripSeparator();
  584. _colorPickerLabel = new ToolStripLabel();
  585. _colorPickerComboBox = new ToolStripComboBox();
  586. _selectionCombineModeSeparator = new ToolStripSeparator();
  587. _selectionCombineModeLabel = new ToolStripLabel();
  588. _selectionCombineModeSplitButton = new ToolStripSplitButton();
  589. _floodModeSeparator = new ToolStripSeparator();
  590. _floodModeLabel = new ToolStripLabel();
  591. _floodModeSplitButton = new ToolStripSplitButton();
  592. _selectionDrawModeSeparator = new ToolStripSeparator();
  593. _selectionDrawModeModeLabel = new ToolStripLabel();
  594. _selectionDrawModeSplitButton = new ToolStripSplitButton();
  595. _selectionDrawModeWidthLabel = new ToolStripLabel();
  596. _selectionDrawModeWidthTextBox = new ToolStripTextBox {TextBox = {Width = 50}};
  597. _selectionDrawModeSwapButton = new ToolStripButton();
  598. _selectionDrawModeHeightLabel = new ToolStripLabel();
  599. _selectionDrawModeHeightTextBox = new ToolStripTextBox {TextBox = {Width = 50}};
  600. _selectionDrawModeUnits = new UnitsComboBoxStrip();
  601. SuspendLayout();
  602. //
  603. // brushStyleLabel
  604. //
  605. _brushStyleLabel.Name = "fillStyleLabel";
  606. //
  607. // brushStyleComboBox
  608. //
  609. _brushStyleComboBox.Name = "styleComboBox";
  610. _brushStyleComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
  611. _brushStyleComboBox.DropDownWidth = 234;
  612. _brushStyleComboBox.AutoSize = true;
  613. //
  614. // brushStyleComboBox.ComboBox
  615. //
  616. if (_brushStyleComboBox.ComboBox != null)
  617. {
  618. _brushStyleComboBox.ComboBox.DrawMode = DrawMode.OwnerDrawVariable;
  619. _brushStyleComboBox.ComboBox.MeasureItem += ComboBoxStyleMeasureItem;
  620. _brushStyleComboBox.ComboBox.SelectedValueChanged += ComboBoxStyleSelectedValueChanged;
  621. _brushStyleComboBox.ComboBox.DrawItem += ComboBoxStyleDrawItem;
  622. }
  623. //
  624. // shapeButton
  625. //
  626. _shapeButton.Name = "shapeButton";
  627. _shapeButton.DropDownOpening += ShapeButtonDropDownOpening;
  628. _shapeButton.DropDownClosed +=
  629. (sender, e) => _shapeButton.DropDownItems.Clear();
  630. _shapeButton.ButtonClick +=
  631. delegate
  632. {
  633. Tracing.LogFeature("ToolConfigStrip(shapeButton)");
  634. switch (ShapeDrawType)
  635. {
  636. case ShapeDrawType.Outline:
  637. ShapeDrawType = ShapeDrawType.Interior;
  638. break;
  639. case ShapeDrawType.Interior:
  640. ShapeDrawType = ShapeDrawType.Both;
  641. break;
  642. case ShapeDrawType.Both:
  643. ShapeDrawType = ShapeDrawType.Outline;
  644. break;
  645. default:
  646. throw new InvalidEnumArgumentException();
  647. }
  648. };
  649. //
  650. // gradientSeparator
  651. //
  652. _gradientSeparator1.Name = "gradientSeparator";
  653. //
  654. // gradientLinearClampedButton
  655. //
  656. _gradientLinearClampedButton.Name = "gradientLinearClampedButton";
  657. _gradientLinearClampedButton.Click += GradientTypeButtonClicked;
  658. _gradientLinearClampedButton.Tag = GradientType.LinearClamped;
  659. //
  660. // gradientLinearReflectedButton
  661. //
  662. _gradientLinearReflectedButton.Name = "gradientLinearReflectedButton";
  663. _gradientLinearReflectedButton.Click += GradientTypeButtonClicked;
  664. _gradientLinearReflectedButton.Tag = GradientType.LinearReflected;
  665. //
  666. // gradientLinearDiamondButton
  667. //
  668. _gradientLinearDiamondButton.Name = "gradientLinearDiamondButton";
  669. _gradientLinearDiamondButton.Click += GradientTypeButtonClicked;
  670. _gradientLinearDiamondButton.Tag = GradientType.LinearDiamond;
  671. //
  672. // gradientRadialButton
  673. //
  674. _gradientRadialButton.Name = "gradientRadialButton";
  675. _gradientRadialButton.Click += GradientTypeButtonClicked;
  676. _gradientRadialButton.Tag = GradientType.Radial;
  677. //
  678. // gradientConicalButton
  679. //
  680. _gradientConicalButton.Name = "gradientConicalButton";
  681. _gradientConicalButton.Click += GradientTypeButtonClicked;
  682. _gradientConicalButton.Tag = GradientType.Conical;
  683. //
  684. // gradientSeparator2
  685. //
  686. _gradientSeparator2.Name = "gradientSeparator2";
  687. //
  688. // gradientChannelsSplitButton
  689. //
  690. _gradientChannelsSplitButton.Name = "gradientChannelsSplitButton";
  691. _gradientChannelsSplitButton.DropDownOpening += GradientChannelsSplitButtonDropDownOpening;
  692. _gradientChannelsSplitButton.DropDownClosed +=
  693. (sender, e) => _gradientChannelsSplitButton.DropDownItems.Clear();
  694. _gradientChannelsSplitButton.ButtonClick +=
  695. delegate
  696. {
  697. Tracing.LogFeature("ToolConfigStrip(gradientChannelsSplitButton)");
  698. GradientInfo = new GradientInfo(GradientInfo.GradientType, !GradientInfo.AlphaOnly);
  699. };
  700. //
  701. // penSeparator
  702. //
  703. _penSeparator.Name = "penSeparator";
  704. //
  705. // penSizeLabel
  706. //
  707. _penSizeLabel.Name = "brushSizeLabel";
  708. //
  709. // penSizeDecButton
  710. //
  711. _penSizeDecButton.Name = "penSizeDecButton";
  712. _penSizeDecButton.Click +=
  713. delegate
  714. {
  715. Tracing.LogFeature("ToolConfigStrip(penSizeDecButton)");
  716. float amount = -1.0f;
  717. if ((ModifierKeys & Keys.Control) != 0)
  718. {
  719. amount *= 5.0f;
  720. }
  721. AddToPenSize(amount);
  722. };
  723. //
  724. // penSizeComboBox
  725. //
  726. _penSizeComboBox.Name = "penSizeComboBox";
  727. _penSizeComboBox.Validating += BrushSizeComboBoxValidating;
  728. _penSizeComboBox.TextChanged += SizeComboBoxTextChanged;
  729. _penSizeComboBox.AutoSize = false;
  730. _penSizeComboBox.Width = 44;
  731. //
  732. // penSizeIncButton
  733. //
  734. _penSizeIncButton.Name = "penSizeIncButton";
  735. _penSizeIncButton.Click +=
  736. delegate
  737. {
  738. Tracing.LogFeature("ToolConfigStrip(penSizeIncButton)");
  739. float amount = 1.0f;
  740. if ((ModifierKeys & Keys.Control) != 0)
  741. {
  742. amount *= 5.0f;
  743. }
  744. AddToPenSize(amount);
  745. };
  746. //
  747. // penStartCapLabel
  748. //
  749. _penStyleLabel.Name = "penStartCapLabel";
  750. //
  751. // penStartCapSplitButton
  752. //
  753. _penStartCapSplitButton.Name = "penStartCapSplitButton";
  754. _penStartCapSplitButton.DropDownOpening += PenCapSplitButtonDropDownOpening;
  755. _penStartCapSplitButton.DropDownClosed +=
  756. (sender, e) => _penStartCapSplitButton.DropDownItems.Clear();
  757. _penStartCapSplitButton.ButtonClick +=
  758. delegate
  759. {
  760. Tracing.LogFeature("ToolConfigStrip(penStartCapSplitButton)");
  761. CyclePenStartCap();
  762. };
  763. //
  764. // penDashStyleSplitButton
  765. //
  766. _penDashStyleSplitButton.Name = "penDashStyleSplitButton";
  767. _penDashStyleSplitButton.ImageScaling = ToolStripItemImageScaling.None;
  768. _penDashStyleSplitButton.DropDownOpening += PenDashStyleButtonDropDownOpening;
  769. _penDashStyleSplitButton.DropDownClosed +=
  770. (sender, e) => _penDashStyleSplitButton.DropDownItems.Clear();
  771. _penDashStyleSplitButton.ButtonClick +=
  772. delegate
  773. {
  774. Tracing.LogFeature("ToolConfigStrip(penDashStyleSplitButton)");
  775. CyclePenDashStyle();
  776. };
  777. //
  778. // penEndCapSplitButton
  779. //
  780. _penEndCapSplitButton.Name = "penEndCapSplitButton";
  781. _penEndCapSplitButton.DropDownOpening += PenCapSplitButtonDropDownOpening;
  782. _penEndCapSplitButton.DropDownClosed +=
  783. (sender, e) => _penEndCapSplitButton.DropDownItems.Clear();
  784. _penEndCapSplitButton.ButtonClick +=
  785. delegate
  786. {
  787. Tracing.LogFeature("ToolConfigStrip(penEndCapSplitButton)");
  788. CyclePenEndCap();
  789. };
  790. //
  791. // antiAliasingSplitButton
  792. //
  793. _antiAliasingSplitButton.Name = "antiAliasingSplitButton";
  794. _antiAliasingSplitButton.DisplayStyle = ToolStripItemDisplayStyle.Image;
  795. _antiAliasingSplitButton.DropDownOpening += AntiAliasingSplitButtonDropDownOpening;
  796. _antiAliasingSplitButton.DropDownClosed +=
  797. (sender, e) => _antiAliasingSplitButton.DropDownItems.Clear();
  798. _antiAliasingSplitButton.ButtonClick +=
  799. delegate
  800. {
  801. Tracing.LogFeature("ToolConfigStrip(antiAliasingSplitButton)");
  802. AntiAliasing = !AntiAliasing;
  803. };
  804. //
  805. // alphaBlendingSplitButton
  806. //
  807. _alphaBlendingSplitButton.Name = "alphaBlendingSplitButton";
  808. _alphaBlendingSplitButton.DisplayStyle = ToolStripItemDisplayStyle.Image;
  809. _alphaBlendingSplitButton.DropDownOpening += AlphaBlendingSplitButtonDropDownOpening;
  810. _alphaBlendingSplitButton.DropDownClosed +=
  811. (sender, e) => _alphaBlendingSplitButton.DropDownItems.Clear();
  812. _alphaBlendingSplitButton.ButtonClick +=
  813. delegate
  814. {
  815. Tracing.LogFeature("ToolConfigStrip(alphaBlendingSplitButton)");
  816. AlphaBlending = !AlphaBlending;
  817. };
  818. //
  819. // toleranceLabel
  820. //
  821. _toleranceLabel.Name = "toleranceLabel";
  822. //
  823. // toleranceSlider
  824. //
  825. _toleranceSlider.Name = "_toleranceSlider";
  826. _toleranceSlider.ToleranceChanged += ToleranceSliderToleranceChanged;
  827. _toleranceSlider.Size = new Size(150, 16);
  828. //
  829. // toleranceSliderStrip
  830. //
  831. _toleranceSliderStrip.Name = "toleranceSliderStrip";
  832. _toleranceSliderStrip.AutoSize = false;
  833. //
  834. // fontLabel
  835. //
  836. _fontLabel.Name = "fontLabel";
  837. //
  838. // fontFamilyComboBox
  839. //
  840. _fontFamilyComboBox.Name = "fontComboBox";
  841. _fontFamilyComboBox.DropDownWidth = 240;
  842. _fontFamilyComboBox.MaxDropDownItems = 12;
  843. _fontFamilyComboBox.Sorted = true;
  844. _fontFamilyComboBox.GotFocus += FontFamilyComboBoxGotFocus;
  845. _fontFamilyComboBox.Items.Add(ArialName);
  846. _fontFamilyComboBox.SelectedItem = ArialName;
  847. _fontFamilyComboBox.SelectedIndexChanged += FontFamilyComboBoxSelectedIndexChanged;
  848. _fontFamilyComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
  849. //
  850. // fontFamilyComboBox.ComboBox
  851. //
  852. if (_fontFamilyComboBox.ComboBox != null)
  853. {
  854. _fontFamilyComboBox.ComboBox.DrawMode = DrawMode.OwnerDrawFixed;
  855. _fontFamilyComboBox.ComboBox.MeasureItem += FontFamilyComboBoxMeasureItem;
  856. _fontFamilyComboBox.ComboBox.DrawItem += FontFamilyComboBoxDrawItem;
  857. }
  858. //
  859. // fontSizeComboBox
  860. //
  861. _fontSizeComboBox.Name = "fontSizeComboBox";
  862. _fontSizeComboBox.AutoSize = false;
  863. _fontSizeComboBox.TextChanged += FontSizeComboBoxTextChanged;
  864. _fontSizeComboBox.Validating += FontSizeComboBoxValidating;
  865. _fontSizeComboBox.Text = InitialFontSize.ToString();
  866. _fontSizeComboBox.Width = 44;
  867. //
  868. // fontSmoothingComboBox
  869. //
  870. _fontSmoothingComboBox.Name = "smoothingComboBOx";
  871. _fontSmoothingComboBox.AutoSize = false;
  872. _fontSmoothingComboBox.Sorted = false;
  873. _fontSmoothingComboBox.Width = 70;
  874. _fontSmoothingComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
  875. _fontSmoothingComboBox.SelectedIndexChanged += SmoothingComboBoxSelectedIndexChanged;
  876. //
  877. // fontBoldButton
  878. //
  879. _fontBoldButton.Name = "boldButton";
  880. //
  881. // fontItalicsButton
  882. //
  883. _fontItalicsButton.Name = "italicsButton";
  884. //
  885. // fontUnderlineButton
  886. //
  887. _fontUnderlineButton.Name = "underlineButton";
  888. //
  889. // fontAlignLeftButton
  890. //
  891. _fontAlignLeftButton.Name = "alignLeftButton";
  892. //
  893. // fontAlignCenterButton
  894. //
  895. _fontAlignCenterButton.Name = "alignCenterButton";
  896. //
  897. // fontAlignRightButton
  898. //
  899. _fontAlignRightButton.Name = "alignRightButton";
  900. //
  901. // resamplingSeparator
  902. //
  903. _resamplingSeparator.Name = "resamplingSeparator";
  904. //
  905. // resamplingLabel
  906. //
  907. _resamplingLabel.Name = "resamplingLabel";
  908. //
  909. // resamplingComboBox
  910. //
  911. _resamplingComboBox.Name = "resamplingComboBox";
  912. _resamplingComboBox.AutoSize = true;
  913. _resamplingComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
  914. _resamplingComboBox.Sorted = false;
  915. _resamplingComboBox.Width = 100;
  916. _resamplingComboBox.DropDownWidth = 100;
  917. _resamplingComboBox.SelectedIndexChanged += ResamplingComboBoxSelectedIndexChanged;
  918. //
  919. // colorPickerSeparator
  920. //
  921. _colorPickerSeparator.Name = "colorPickerSeparator";
  922. //
  923. // colorPickerLabel
  924. //
  925. _colorPickerLabel.Name = "colorPickerLabel";
  926. //
  927. // colorPickerComboBox
  928. //
  929. _colorPickerComboBox.Name = "colorPickerComboBox";
  930. _colorPickerComboBox.AutoSize = true;
  931. _colorPickerComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
  932. _colorPickerComboBox.Width = 200;
  933. _colorPickerComboBox.DropDownWidth = 200;
  934. _colorPickerComboBox.Sorted = false;
  935. _colorPickerComboBox.SelectedIndexChanged += ColorPickerComboBoxSelectedIndexChanged;
  936. //
  937. // selectionCombineModeSeparator
  938. //
  939. _selectionCombineModeSeparator.Name = "selectionCombineModeSeparator";
  940. //
  941. // selectionCombineModeLabel
  942. //
  943. _selectionCombineModeLabel.Name = "selectionCombineModeLabel";
  944. //
  945. // selectionCombineModeSplitButton
  946. //
  947. _selectionCombineModeSplitButton.Name = "selectionCombineModeSplitButton";
  948. _selectionCombineModeSplitButton.DisplayStyle = ToolStripItemDisplayStyle.Image;
  949. _selectionCombineModeSplitButton.DropDownOpening += SelectionCombineModeSplitButtonDropDownOpening;
  950. _selectionCombineModeSplitButton.DropDownClosed +=
  951. (sender, e) => _selectionCombineModeSplitButton.DropDownItems.Clear();
  952. _selectionCombineModeSplitButton.ButtonClick +=
  953. delegate
  954. {
  955. Tracing.LogFeature("ToolConfigStrip(selectionCombineModeSplitButton)");
  956. SelectionCombineMode = CycleSelectionCombineMode(SelectionCombineMode);
  957. };
  958. //
  959. // floodModeSeparator
  960. //
  961. _floodModeSeparator.Name = "floodModeSeparator";
  962. //
  963. // floodModeLabel
  964. //
  965. _floodModeLabel.Name = "floodModeLabel";
  966. //
  967. // floodModeSplitButton
  968. //
  969. _floodModeSplitButton.Name = "floodModeSplitButton";
  970. _floodModeSplitButton.DisplayStyle = ToolStripItemDisplayStyle.Image;
  971. _floodModeSplitButton.DropDownOpening += FloodModeSplitButtonDropDownOpening;
  972. _floodModeSplitButton.DropDownClosed +=
  973. (sender, e) => _floodModeSplitButton.DropDownItems.Clear();
  974. _floodModeSplitButton.ButtonClick +=
  975. delegate
  976. {
  977. Tracing.LogFeature("ToolConfigStrip(floodModeSplitButton)");
  978. FloodMode = CycleFloodMode(FloodMode);
  979. };
  980. //
  981. // selectionDrawModeSeparator
  982. //
  983. _selectionDrawModeSeparator.Name = "selectionDrawModeSeparator";
  984. //
  985. // selectionDrawModeModeLabel
  986. //
  987. _selectionDrawModeModeLabel.Name = "selectionDrawModeModeLabel";
  988. //
  989. // selectionDrawModeSplitButton
  990. //
  991. _selectionDrawModeSplitButton.Name = "selectionDrawModeSplitButton";
  992. _selectionDrawModeSplitButton.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
  993. _selectionDrawModeSplitButton.DropDownOpening += SelectionDrawModeSplitButtonDropDownOpening;
  994. _selectionDrawModeSplitButton.DropDownClosed +=
  995. (sender, e) => _selectionDrawModeSplitButton.DropDownItems.Clear();
  996. _selectionDrawModeSplitButton.ButtonClick +=
  997. delegate
  998. {
  999. Tracing.LogFeature("ToolConfigStrip(selectionDrawModeSplitButton)");
  1000. SelectionDrawMode newSDM = CycleSelectionDrawMode(SelectionDrawModeInfo.DrawMode);
  1001. SelectionDrawModeInfo = SelectionDrawModeInfo.CloneWithNewDrawMode(newSDM);
  1002. };
  1003. //
  1004. // selectionDrawModeWidthLabel
  1005. //
  1006. _selectionDrawModeWidthLabel.Name = "selectionDrawModeWidthLabel";
  1007. //
  1008. // selectionDrawModeWidthTextBox
  1009. //
  1010. _selectionDrawModeWidthTextBox.Name = "selectionDrawModeWidthTextBox";
  1011. _selectionDrawModeWidthTextBox.TextBoxTextAlign = HorizontalAlignment.Right;
  1012. _selectionDrawModeWidthTextBox.Enter +=
  1013. (sender, e) =>
  1014. _selectionDrawModeWidthTextBox.TextBox.Select(0,
  1015. _selectionDrawModeWidthTextBox.TextBox.Text.
  1016. Length);
  1017. _selectionDrawModeWidthTextBox.Leave +=
  1018. delegate
  1019. {
  1020. Tracing.LogFeature("ToolConfigStrip(selectionDrawModeWidthTextBox.Leave)");
  1021. double newWidth;
  1022. if (double.TryParse(_selectionDrawModeWidthTextBox.Text, out newWidth))
  1023. {
  1024. SelectionDrawModeInfo = _selectionDrawModeInfo.CloneWithNewWidth(newWidth);
  1025. }
  1026. else
  1027. {
  1028. _selectionDrawModeWidthTextBox.Text = _selectionDrawModeInfo.Width.ToString();
  1029. }
  1030. };
  1031. //
  1032. // selectionDrawModeSwapButton
  1033. //
  1034. _selectionDrawModeSwapButton.Name = "selectionDrawModeSwapButton";
  1035. _selectionDrawModeSwapButton.Click +=
  1036. delegate
  1037. {
  1038. Tracing.LogFeature("ToolConfigStrip(selectionDrawModeSwapButton)");
  1039. SelectionDrawModeInfo oldSDMI = SelectionDrawModeInfo;
  1040. var newSDMI = new SelectionDrawModeInfo(oldSDMI.DrawMode, oldSDMI.Height, oldSDMI.Width, oldSDMI.Units);
  1041. SelectionDrawModeInfo = newSDMI;
  1042. };
  1043. //
  1044. // selectionDrawModeHeightLabel
  1045. //
  1046. _selectionDrawModeHeightLabel.Name = "selectionDrawModeHeightLabel";
  1047. //
  1048. // selectionDrawModeHeightTextBox
  1049. //
  1050. _selectionDrawModeHeightTextBox.Name = "selectionDrawModeHeightTextBox";
  1051. _selectionDrawModeHeightTextBox.TextBoxTextAlign = HorizontalAlignment.Right;
  1052. _selectionDrawModeHeightTextBox.Enter +=
  1053. (sender, e) =>
  1054. _selectionDrawModeHeightTextBox.TextBox.Select(0,
  1055. _selectionDrawModeHeightTextBox.TextBox.Text.
  1056. Length);
  1057. _selectionDrawModeHeightTextBox.Leave +=
  1058. delegate
  1059. {
  1060. Tracing.LogFeature("ToolConfigStrip(selectionDrawModeHeightTextBox.Leave)");
  1061. double newHeight;
  1062. if (double.TryParse(_selectionDrawModeHeightTextBox.Text, out newHeight))
  1063. {
  1064. SelectionDrawModeInfo = _selectionDrawModeInfo.CloneWithNewHeight(newHeight);
  1065. }
  1066. else
  1067. {
  1068. _selectionDrawModeHeightTextBox.Text = _selectionDrawModeInfo.Height.ToString();
  1069. }
  1070. };
  1071. //
  1072. // selectionDrawModeUnits
  1073. //
  1074. _selectionDrawModeUnits.Name = "selectionDrawModeUnits";
  1075. _selectionDrawModeUnits.UnitsDisplayType = UnitsDisplayType.Plural;
  1076. _selectionDrawModeUnits.LowercaseStrings = true;
  1077. _selectionDrawModeUnits.Size = new Size(90, _selectionDrawModeUnits.Height);
  1078. //
  1079. // DrawConfigStrip
  1080. //
  1081. AutoSize = true;
  1082. Items.AddRange(
  1083. new ToolStripItem[]
  1084. {
  1085. _selectionCombineModeSeparator,
  1086. _selectionCombineModeLabel,
  1087. _selectionCombineModeSplitButton,
  1088. _selectionDrawModeSeparator,
  1089. _selectionDrawModeModeLabel,
  1090. _selectionDrawModeSplitButton,
  1091. _selectionDrawModeWidthLabel,
  1092. _selectionDrawModeWidthTextBox,
  1093. _selectionDrawModeSwapButton,
  1094. _selectionDrawModeHeightLabel,
  1095. _selectionDrawModeHeightTextBox,
  1096. _selectionDrawModeUnits,
  1097. _floodModeSeparator,
  1098. _floodModeLabel,
  1099. _floodModeSplitButton,
  1100. _resamplingSeparator,
  1101. _resamplingLabel,
  1102. _resamplingComboBox,
  1103. _colorPickerSeparator,
  1104. _colorPickerLabel,
  1105. _colorPickerComboBox,
  1106. _fontSeparator,
  1107. _fontLabel,
  1108. _fontFamilyComboBox,
  1109. _fontSizeComboBox,
  1110. _fontSmoothingComboBox,
  1111. _fontStyleSeparator,
  1112. _fontBoldButton,
  1113. _fontItalicsButton,
  1114. _fontUnderlineButton,
  1115. _fontAlignSeparator,
  1116. _fontAlignLeftButton,
  1117. _fontAlignCenterButton,
  1118. _fontAlignRightButton,
  1119. _shapeSeparator,
  1120. _shapeButton,
  1121. _gradientSeparator1,
  1122. _gradientLinearClampedButton,
  1123. _gradientLinearReflectedButton,
  1124. _gradientLinearDiamondButton,
  1125. _gradientRadialButton,
  1126. _gradientConicalButton,
  1127. _gradientSeparator2,
  1128. _gradientChannelsSplitButton,
  1129. _penSeparator,
  1130. _penSizeLabel,
  1131. _penSizeDecButton,
  1132. _penSizeComboBox,
  1133. _penSizeIncButton,
  1134. _penStyleLabel,
  1135. _penStartCapSplitButton,
  1136. _penDashStyleSplitButton,
  1137. _penEndCapSplitButton,
  1138. _brushSeparator,
  1139. _brushStyleLabel,
  1140. _brushStyleComboBox,
  1141. _toleranceSeparator,
  1142. _toleranceLabel,
  1143. _toleranceSliderStrip,
  1144. _blendingSeparator,
  1145. _antiAliasingSplitButton,
  1146. _alphaBlendingSplitButton
  1147. });
  1148. ResumeLayout(false);
  1149. }
  1150. public void CyclePenEndCap()
  1151. {
  1152. PenInfo newPenInfo = PenInfo.Clone();
  1153. newPenInfo.EndCap = NextLineCap(newPenInfo.EndCap);
  1154. PenInfo = newPenInfo;
  1155. }
  1156. public void CyclePenStartCap()
  1157. {
  1158. PenInfo newPenInfo = PenInfo.Clone();
  1159. newPenInfo.StartCap = NextLineCap(newPenInfo.StartCap);
  1160. PenInfo = newPenInfo;
  1161. }
  1162. public void CyclePenDashStyle()
  1163. {
  1164. PenInfo newPenInfo = PenInfo.Clone();
  1165. newPenInfo.DashStyle = NextDashStyle(newPenInfo.DashStyle);
  1166. PenInfo = newPenInfo;
  1167. }
  1168. private DashStyle NextDashStyle(DashStyle oldDash)
  1169. {
  1170. int dashIndex = Array.IndexOf(_dashStyles, oldDash);
  1171. int newDashIndex = (dashIndex + 1) % _dashStyles.Length;
  1172. return _dashStyles[newDashIndex];
  1173. }
  1174. private LineCap2 NextLineCap(LineCap2 oldCap)
  1175. {
  1176. int capIndex = Array.IndexOf(_lineCaps, oldCap);
  1177. int newCapIndex = (capIndex + 1) % _lineCaps.Length;
  1178. return _lineCaps[newCapIndex];
  1179. }
  1180. private void GradientTypeButtonClicked(object sender, EventArgs e)
  1181. {
  1182. Tracing.LogFeature("ToolConfigStrip(GradientTypeButtonClicked)");
  1183. var newType = (GradientType)((ToolStripButton)sender).Tag;
  1184. GradientInfo = new GradientInfo(newType, GradientInfo.AlphaOnly);
  1185. }
  1186. private void ComboBoxStyleSelectedValueChanged(object sender, EventArgs e)
  1187. {
  1188. OnBrushChanged();
  1189. }
  1190. public void PerformBrushChanged()
  1191. {
  1192. OnBrushChanged();
  1193. }
  1194. private void ComboBoxStyleDrawItem(object sender, DrawItemEventArgs e)
  1195. {
  1196. e.DrawBackground();
  1197. Rectangle r = e.Bounds;
  1198. if (e.Index == -1) return;
  1199. var itemName = (string)_brushStyleComboBox.Items[e.Index];
  1200. if (itemName != _solidBrushText)
  1201. {
  1202. Rectangle rd = r;
  1203. rd.Width = rd.Left + 25;
  1204. Rectangle rt = r;
  1205. r.X = rd.Right;
  1206. string displayText = _brushStyleComboBox.Items[e.Index].ToString();
  1207. var hs = (HatchStyle)_hatchStyleNames.LocalizedNameToEnumValue(displayText);
  1208. using (var b = new HatchBrush(hs, e.ForeColor, e.BackColor))
  1209. {
  1210. e.Graphics.FillRectangle(b, rd);
  1211. }
  1212. var sf = new StringFormat {Alignment = StringAlignment.Near};
  1213. using (var sb = new SolidBrush(Color.White))
  1214. {
  1215. if ((e.State & DrawItemState.Focus) == 0)
  1216. {
  1217. sb.Color = SystemColors.Window;
  1218. e.Graphics.FillRectangle(sb, r);
  1219. sb.Color = SystemColors.WindowText;
  1220. e.Graphics.DrawString(displayText, Font, sb, r, sf);
  1221. }
  1222. else
  1223. {
  1224. sb.Color = SystemColors.Highlight;
  1225. e.Graphics.FillRectangle(sb, r);
  1226. sb.Color = SystemColors.HighlightText;
  1227. e.Graphics.DrawString(displayText, Font, sb, r, sf);
  1228. }
  1229. }
  1230. sf.Dispose();
  1231. sf = null;
  1232. }
  1233. else
  1234. {
  1235. // Solid Brush
  1236. using (var sb = new SolidBrush(Color.White))
  1237. {
  1238. if ((e.State & DrawItemState.Focus) == 0)
  1239. {
  1240. sb.Color = SystemColors.Window;
  1241. e.Graphics.FillRectangle(sb, e.Bounds);
  1242. string displayText = _brushStyleComboBox.Items[e.Index].ToString();
  1243. sb.Color = SystemColors.WindowText;
  1244. e.Graphics.DrawString(displayText, Font, sb, e.Bounds);
  1245. }
  1246. else
  1247. {
  1248. sb.Color = SystemColors.Highlight;
  1249. e.Graphics.FillRectangle(sb, e.Bounds);
  1250. string displayText = _brushStyleComboBox.Items[e.Index].ToString();
  1251. sb.Color = SystemColors.HighlightText;
  1252. e.Graphics.DrawString(displayText, Font, sb, e.Bounds);
  1253. }
  1254. }
  1255. }
  1256. e.DrawFocusRectangle();
  1257. }
  1258. private void ComboBoxStyleMeasureItem(object sender, MeasureItemEventArgs e)
  1259. {
  1260. // Work out what the text will be
  1261. string displayText = _brushStyleComboBox.Items[e.Index].ToString();
  1262. // Get width & height of string
  1263. SizeF stringSize = e.Graphics.MeasureString(displayText, Font);
  1264. // set height to text height
  1265. e.ItemHeight = (int)stringSize.Height;
  1266. // set width to text width
  1267. e.ItemWidth = (int)stringSize.Width;
  1268. }
  1269. public event EventHandler ShapeDrawTypeChanged;
  1270. protected virtual void OnShapeDrawTypeChanged()
  1271. {
  1272. if (ShapeDrawTypeChanged != null)
  1273. {
  1274. ShapeDrawTypeChanged(this, EventArgs.Empty);
  1275. }
  1276. }
  1277. public void PerformShapeDrawTypeChanged()
  1278. {
  1279. OnShapeDrawTypeChanged();
  1280. }
  1281. public ShapeDrawType ShapeDrawType
  1282. {
  1283. get
  1284. {
  1285. return _shapeDrawType;
  1286. }
  1287. set
  1288. {
  1289. if (_shapeDrawType == value) return;
  1290. _shapeDrawType = value;
  1291. // if the user sets the shape the buttons must be updated
  1292. switch (_shapeDrawType)
  1293. {
  1294. case ShapeDrawType.Outline:
  1295. _shapeButton.Image = _shapeOutlineImage.Reference;
  1296. break;
  1297. case ShapeDrawType.Both:
  1298. _shapeButton.Image = _shapeBothImage.Reference;
  1299. break;
  1300. case ShapeDrawType.Interior:
  1301. _shapeButton.Image = _shapeInteriorImage.Reference;
  1302. break;
  1303. default:
  1304. throw new InvalidOperationException("Shape draw type is invalid");
  1305. }
  1306. OnShapeDrawTypeChanged();
  1307. }
  1308. }
  1309. private void SetFontStyleButtons(FontStyle style)
  1310. {
  1311. bool bold = ((style & FontStyle.Bold) != 0);
  1312. bool italic = ((style & FontStyle.Italic) != 0);
  1313. bool underline = ((style & FontStyle.Underline) != 0);
  1314. _fontBoldButton.Checked = bold;
  1315. _fontItalicsButton.Checked = italic;
  1316. _fontUnderlineButton.Checked = underline;
  1317. }
  1318. protected override void OnItemClicked(ToolStripItemClickedEventArgs e)
  1319. {
  1320. if (e.ClickedItem == _fontBoldButton)
  1321. {
  1322. Tracing.LogFeature("ToolConfigStrip(fontBoldButton)");
  1323. _fontStyle ^= FontStyle.Bold;
  1324. SetFontStyleButtons(_fontStyle);
  1325. OnFontInfoChanged();
  1326. }
  1327. else if (e.ClickedItem == _fontItalicsButton)
  1328. {
  1329. Tracing.LogFeature("ToolConfigStrip(fontItalicsButton)");
  1330. _fontStyle ^= FontStyle.Italic;
  1331. SetFontStyleButtons(_fontStyle);
  1332. OnFontInfoChanged();
  1333. }
  1334. else if (e.ClickedItem == _fontUnderlineButton)
  1335. {
  1336. Tracing.LogFeature("ToolConfigStrip(fontUnderlineButton)");
  1337. _fontStyle ^= FontStyle.Underline;
  1338. SetFontStyleButtons(_fontStyle);
  1339. OnFontInfoChanged();
  1340. }
  1341. else if (e.ClickedItem == _fontAlignLeftButton)
  1342. {
  1343. Tracing.LogFeature("ToolConfigStrip(fontAlignLeftButton)");
  1344. FontAlignment = TextAlignment.Left;
  1345. }
  1346. else if (e.ClickedItem == _fontAlignCenterButton)
  1347. {
  1348. Tracing.LogFeature("ToolConfigStrip(fontAlignCenterButton)");
  1349. FontAlignment = TextAlignment.Center;
  1350. }
  1351. else if (e.ClickedItem == _fontAlignRightButton)
  1352. {
  1353. Tracing.LogFeature("ToolConfigStrip(fontAlignRightButton)");
  1354. FontAlignment = TextAlignment.Right;
  1355. }
  1356. base.OnItemClicked(e);
  1357. }
  1358. public event EventHandler PenInfoChanged;
  1359. protected virtual void OnPenChanged()
  1360. {
  1361. if (PenInfoChanged != null)
  1362. {
  1363. PenInfoChanged(this, EventArgs.Empty);
  1364. }
  1365. }
  1366. public void PerformPenChanged()
  1367. {
  1368. OnPenChanged();
  1369. }
  1370. public void AddToPenSize(float delta)
  1371. {
  1372. if ((_toolBarConfigItems & ToolBarConfigItems.Pen) != ToolBarConfigItems.Pen) return;
  1373. float newWidth = Utility.Clamp(PenInfo.Width + delta, MinPenSize, MaxPenSize);
  1374. PenInfo newPenInfo = PenInfo.Clone();
  1375. newPenInfo.Width += delta;
  1376. newPenInfo.Width = Utility.Clamp(newPenInfo.Width, MinPenSize, MaxPenSize);
  1377. PenInfo = newPenInfo;
  1378. }
  1379. public PenInfo PenInfo
  1380. {
  1381. get
  1382. {
  1383. float width;
  1384. try
  1385. {
  1386. width = float.Parse(_penSizeComboBox.Text);
  1387. }
  1388. catch (FormatException)
  1389. {
  1390. // TODO: would much rather grab the value from AppEnvironment
  1391. // or in some way that we keep track of the "last good value"
  1392. width = 2;
  1393. }
  1394. LineCap2 startCap;
  1395. try
  1396. {
  1397. startCap = (LineCap2)_penStartCapSplitButton.Tag;
  1398. }
  1399. catch (Exception)
  1400. {
  1401. startCap = PenInfo.DefaultLineCap;
  1402. }
  1403. LineCap2 endCap;
  1404. try
  1405. {
  1406. endCap = (LineCap2)_penEndCapSplitButton.Tag;
  1407. }
  1408. catch (Exception)
  1409. {
  1410. endCap = PenInfo.DefaultLineCap;
  1411. }
  1412. var dashStyle = (DashStyle)_penDashStyleSplitButton.Tag;
  1413. return new PenInfo(dashStyle, width, startCap, endCap, PenInfo.DefaultCapScale);
  1414. }
  1415. set
  1416. {
  1417. if (PenInfo == value) return;
  1418. _penSizeComboBox.Text = value.Width.ToString();
  1419. _penStartCapSplitButton.Tag = value.StartCap;
  1420. _penStartCapSplitButton.Image = GetLineCapImage(value.StartCap, true).Reference;
  1421. _penDashStyleSplitButton.Tag = value.DashStyle;
  1422. _penDashStyleSplitButton.Image = GetDashStyleImage(value.DashStyle);
  1423. _penEndCapSplitButton.Tag = value.EndCap;
  1424. _penEndCapSplitButton.Image = GetLineCapImage(value.EndCap, false).Reference;
  1425. OnPenChanged();
  1426. }
  1427. }
  1428. private void SizeComboBoxTextChanged(object sender, EventArgs e)
  1429. {
  1430. BrushSizeComboBoxValidating(this, new CancelEventArgs());
  1431. }
  1432. private void BrushSizeComboBoxValidating(object sender, CancelEventArgs e)
  1433. {
  1434. float penSize;
  1435. bool valid = float.TryParse(_penSizeComboBox.Text, out penSize);
  1436. if (!valid)
  1437. {
  1438. _penSizeComboBox.BackColor = Color.Red;
  1439. _penSizeComboBox.ToolTipText = PdnResources.GetString("PenConfigWidget.Error.InvalidNumber");
  1440. }
  1441. else
  1442. {
  1443. if (penSize < MinPenSize)
  1444. {
  1445. // Set the error if the size is too small.
  1446. _penSizeComboBox.BackColor = Color.Red;
  1447. string tooSmallFormat = PdnResources.GetString("PenConfigWidget.Error.TooSmall.Format");
  1448. string tooSmall = string.Format(tooSmallFormat, MinPenSize);
  1449. _penSizeComboBox.ToolTipText = tooSmall;
  1450. }
  1451. else if (penSize > MaxPenSize)
  1452. {
  1453. // Set the error if the size is too large.
  1454. _penSizeComboBox.BackColor = Color.Red;
  1455. string tooLargeFormat = PdnResources.GetString("PenConfigWidget.Error.TooLarge.Format");
  1456. string tooLarge = string.Format(tooLargeFormat, MaxPenSize);
  1457. _penSizeComboBox.ToolTipText = tooLarge;
  1458. }
  1459. else
  1460. {
  1461. // Clear the error, if any
  1462. _penSizeComboBox.BackColor = SystemColors.Window;
  1463. _penSizeComboBox.ToolTipText = string.Empty;
  1464. OnPenChanged();
  1465. }
  1466. }
  1467. }
  1468. public event EventHandler AlphaBlendingChanged;
  1469. protected virtual void OnAlphaBlendingChanged()
  1470. {
  1471. if (AlphaBlendingChanged != null)
  1472. {
  1473. AlphaBlendingChanged(this, EventArgs.Empty);
  1474. }
  1475. }
  1476. public void PerformAlphaBlendingChanged()
  1477. {
  1478. OnAlphaBlendingChanged();
  1479. }
  1480. private void GradientChannelsSplitButtonDropDownOpening(object sender, EventArgs e)
  1481. {
  1482. var allChannels = new ToolStripMenuItem(
  1483. PdnResources.GetString("GradientChannels.AllColorChannels.Text"),
  1484. _gradientAllColorChannelsImage.Reference,
  1485. delegate {
  1486. GradientInfo = new GradientInfo(GradientInfo.GradientType, false);
  1487. });
  1488. var alphaOnly = new ToolStripMenuItem(
  1489. PdnResources.GetString("GradientChannels.AlphaChannelOnly.Text"),
  1490. _gradientAlphaChannelOnlyImage.Reference,
  1491. delegate {
  1492. GradientInfo = new GradientInfo(GradientInfo.GradientType, true);
  1493. });
  1494. allChannels.Checked = !GradientInfo.AlphaOnly;
  1495. alphaOnly.Checked = GradientInfo.AlphaOnly;
  1496. _gradientChannelsSplitButton.DropDownItems.Clear();
  1497. _gradientChannelsSplitButton.DropDownItems.AddRange(
  1498. new ToolStripItem[]
  1499. {
  1500. allChannels,
  1501. alphaOnly
  1502. });
  1503. }
  1504. private void AlphaBlendingSplitButtonDropDownOpening(object sender, EventArgs e)
  1505. {
  1506. var abEnabled = new ToolStripMenuItem(
  1507. PdnResources.GetString("AlphaBlendingSplitButton.BlendingEnabled.Text"),
  1508. _alphaBlendingEnabledImage.Reference,
  1509. delegate {
  1510. AlphaBlending = true;
  1511. });
  1512. var abOverwrite = new ToolStripMenuItem(
  1513. PdnResources.GetString("AlphaBlendingSplitButton.BlendingOverwrite.Text"),
  1514. _alphaBlendingOverwriteImage.Reference,
  1515. delegate {
  1516. AlphaBlending = false;
  1517. });
  1518. abEnabled.Checked = AlphaBlending;
  1519. abOverwrite.Checked = !AlphaBlending;
  1520. _alphaBlendingSplitButton.DropDownItems.Clear();
  1521. _alphaBlendingSplitButton.DropDownItems.AddRange(
  1522. new ToolStripItem[]
  1523. {
  1524. abEnabled,
  1525. abOverwrite
  1526. });
  1527. }
  1528. public bool AlphaBlending
  1529. {
  1530. get
  1531. {
  1532. return _alphaBlendingEnabled;
  1533. }
  1534. set
  1535. {
  1536. if (value == _alphaBlendingEnabled) return;
  1537. _alphaBlendingEnabled = value;
  1538. _alphaBlendingSplitButton.Image = value ? _alphaBlendingEnabledImage.Reference : _alphaBlendingOverwriteImage.Reference;
  1539. OnAlphaBlendingChanged();
  1540. }
  1541. }
  1542. private void AlphaBlendingComboBox_SelectedIndexChanged(object sender, EventArgs e)
  1543. {
  1544. OnAlphaBlendingChanged();
  1545. }
  1546. public event EventHandler AntiAliasingChanged;
  1547. protected virtual void OnAntiAliasingChanged()
  1548. {
  1549. if (AntiAliasingChanged != null)
  1550. {
  1551. AntiAliasingChanged(this, EventArgs.Empty);
  1552. }
  1553. }
  1554. public void PerformAntiAliasingChanged()
  1555. {
  1556. OnAntiAliasingChanged();
  1557. }
  1558. public bool AntiAliasing
  1559. {
  1560. get
  1561. {
  1562. return _antiAliasingEnabled;
  1563. }
  1564. set
  1565. {
  1566. if (_antiAliasingEnabled == value) return;
  1567. _antiAliasingSplitButton.Image = value ? _antiAliasingEnabledImage.Reference : _antiAliasingDisabledImage.Reference;
  1568. _antiAliasingEnabled = value;
  1569. OnAntiAliasingChanged();
  1570. }
  1571. }
  1572. private static Image GetDashStyleImage(DashStyle dashStyle)
  1573. {
  1574. const string nameFormat = "Images.DashStyleButton.{0}.png";
  1575. string name = string.Format(nameFormat, dashStyle);
  1576. ImageResource imageResource = PdnResources.GetImageResource(name);
  1577. Image returnImage;
  1578. if (UI.GetXScaleFactor() == 1.0f && UI.GetYScaleFactor() == 1.0f)
  1579. {
  1580. returnImage = imageResource.Reference;
  1581. }
  1582. else
  1583. {
  1584. returnImage = new Bitmap(imageResource.Reference, UI.ScaleSize(imageResource.Reference.Size));
  1585. }
  1586. return returnImage;
  1587. }
  1588. private static ImageResource GetLineCapImage(LineCap2 lineCap, bool isStartCap)
  1589. {
  1590. const string nameFormat = "Images.LineCapButton.{0}.{1}.png";
  1591. string name = string.Format(nameFormat, lineCap, isStartCap ? "Start" : "End");
  1592. ImageResource imageResource = PdnResources.GetImageResource(name);
  1593. return imageResource;
  1594. }
  1595. private void PenDashStyleButtonDropDownOpening(object sender, EventArgs e)
  1596. {
  1597. var menuItems = new List<ToolStripMenuItem>();
  1598. foreach (DashStyle dashStyle in _dashStyles)
  1599. {
  1600. var mi = new ToolStripMenuItem(
  1601. _dashStyleLocalizer.EnumValueToLocalizedName(dashStyle),
  1602. GetDashStyleImage(dashStyle),
  1603. delegate(object sender2, EventArgs e2)
  1604. {
  1605. var tsmi = (ToolStripMenuItem)sender2;
  1606. var newDashStyle = (DashStyle)tsmi.Tag;
  1607. PenInfo newPenInfo = PenInfo.Clone();
  1608. newPenInfo.DashStyle = newDashStyle;
  1609. PenInfo = newPenInfo;
  1610. }) {ImageScaling = ToolStripItemImageScaling.None};
  1611. if (dashStyle == PenInfo.DashStyle)
  1612. {
  1613. mi.Checked = true;
  1614. }
  1615. mi.Tag = dashStyle;
  1616. menuItems.Add(mi);
  1617. }
  1618. _penDashStyleSplitButton.DropDownItems.Clear();
  1619. _penDashStyleSplitButton.DropDownItems.AddRange(menuItems.ToArray());
  1620. }
  1621. private void PenCapSplitButtonDropDownOpening(object sender, EventArgs e)
  1622. {
  1623. var splitButton = (ToolStripSplitButton)sender;
  1624. var menuItems = new List<ToolStripMenuItem>();
  1625. LineCap2 currentLineCap = PenInfo.DefaultLineCap;
  1626. bool isStartCap;
  1627. if (ReferenceEquals(splitButton, _penStartCapSplitButton))
  1628. {
  1629. isStartCap = true;
  1630. currentLineCap = PenInfo.StartCap;
  1631. }
  1632. else if (ReferenceEquals(splitButton, _penEndCapSplitButton))
  1633. {
  1634. isStartCap = false;
  1635. currentLineCap = PenInfo.EndCap;
  1636. }
  1637. else
  1638. {
  1639. throw new InvalidOperationException();
  1640. }
  1641. foreach (LineCap2 lineCap in _lineCaps)
  1642. {
  1643. var mi = new ToolStripMenuItem(
  1644. _lineCapLocalizer.EnumValueToLocalizedName(lineCap),
  1645. GetLineCapImage(lineCap, isStartCap).Reference,
  1646. delegate(object sender2, EventArgs e2)
  1647. {
  1648. var tsmi = (ToolStripMenuItem)sender2;
  1649. var data = (Pair<ToolStripSplitButton, LineCap2>)tsmi.Tag;
  1650. PenInfo newPenInfo = PenInfo.Clone();
  1651. if (ReferenceEquals(data.First, _penStartCapSplitButton))
  1652. {
  1653. newPenInfo.StartCap = data.Second;
  1654. }
  1655. else if (ReferenceEquals(data.First, _penEndCapSplitButton))
  1656. {
  1657. newPenInfo.EndCap = data.Second;
  1658. }
  1659. PenInfo = newPenInfo;
  1660. }) {Tag = Pair.Create(splitButton, lineCap)};
  1661. if (lineCap == currentLineCap)
  1662. {
  1663. mi.Checked = true;
  1664. }
  1665. menuItems.Add(mi);
  1666. }
  1667. splitButton.DropDownItems.Clear();
  1668. splitButton.DropDownItems.AddRange(menuItems.ToArray());
  1669. }
  1670. private void AntiAliasingSplitButtonDropDownOpening(object sender, EventArgs e)
  1671. {
  1672. var aaEnabled = new ToolStripMenuItem(
  1673. PdnResources.GetString("AntiAliasingSplitButton.Enabled.Text"),
  1674. _antiAliasingEnabledImage.Reference,
  1675. delegate {
  1676. AntiAliasing = true;
  1677. });
  1678. var aaDisabled = new ToolStripMenuItem(
  1679. PdnResources.GetString("AntiAliasingSplitButton.Disabled.Text"),
  1680. _antiAliasingDisabledImage.Reference,
  1681. delegate {
  1682. AntiAliasing = false;
  1683. });
  1684. aaEnabled.Checked = AntiAliasing;
  1685. aaDisabled.Checked = !AntiAliasing;
  1686. _antiAliasingSplitButton.DropDownItems.Clear();
  1687. _antiAliasingSplitButton.DropDownItems.AddRange(
  1688. new ToolStripItem[]
  1689. {
  1690. aaEnabled,
  1691. aaDisabled
  1692. });
  1693. }
  1694. private void SmoothingComboBoxSelectedIndexChanged(object sender, EventArgs e)
  1695. {
  1696. OnFontSmoothingChanged();
  1697. }
  1698. private void PopulateFontsBackgroundThread(object ignored)
  1699. {
  1700. PopulateFontsBackgroundThread();
  1701. }
  1702. private void PopulateFontsBackgroundThread()
  1703. {
  1704. try
  1705. {
  1706. using (new ThreadBackground(ThreadBackgroundFlags.Cpu))
  1707. {
  1708. PopulateFonts();
  1709. }
  1710. }
  1711. finally
  1712. {
  1713. StaticFontNamesPopulatedEvent.Set();
  1714. }
  1715. }
  1716. /*NOTE: CHECK THIS OVER*/
  1717. private void PopulateFonts()
  1718. {
  1719. var fontNames = new List<string>();
  1720. using (Graphics g = CreateGraphics())
  1721. {
  1722. FontFamily[] families = FontFamily.Families;
  1723. foreach (FontFamily family in FontFamily.Families)
  1724. {
  1725. using (var fi = new FontInfo(family, 10, FontStyle.Regular))
  1726. {
  1727. if (!fontNames.Contains(family.Name) && fi.CanCreateFont())
  1728. {
  1729. fontNames.Add(family.Name);
  1730. }
  1731. }
  1732. }
  1733. }
  1734. _staticFontNames = fontNames;
  1735. Tracing.LogFeature("PopulateFonts()");
  1736. }
  1737. public event EventHandler FontInfoChanged;
  1738. protected virtual void OnFontInfoChanged()
  1739. {
  1740. if (FontInfoChanged != null)
  1741. {
  1742. FontInfoChanged(this, EventArgs.Empty);
  1743. }
  1744. }
  1745. public event EventHandler FontAlignmentChanged;
  1746. protected virtual void OnTextAlignmentChanged()
  1747. {
  1748. if (FontAlignmentChanged != null)
  1749. {
  1750. FontAlignmentChanged(this, EventArgs.Empty);
  1751. }
  1752. }
  1753. public event EventHandler FontSmoothingChanged;
  1754. protected virtual void OnFontSmoothingChanged()
  1755. {
  1756. if (FontSmoothingChanged != null)
  1757. {
  1758. FontSmoothingChanged(this, EventArgs.Empty);
  1759. }
  1760. }
  1761. public FontSmoothing FontSmoothing
  1762. {
  1763. get
  1764. {
  1765. return (FontSmoothing)_fontSmoothingLocalizer.LocalizedNameToEnumValue((string)_fontSmoothingComboBox.SelectedItem);
  1766. }
  1767. set
  1768. {
  1769. if (value != FontSmoothing)
  1770. {
  1771. _fontSmoothingComboBox.SelectedItem = _fontSmoothingLocalizer.EnumValueToLocalizedName(value);
  1772. }
  1773. }
  1774. }
  1775. public FontInfo FontInfo
  1776. {
  1777. get
  1778. {
  1779. return new FontInfo(FontFamily, FontSize, FontStyle);
  1780. }
  1781. set
  1782. {
  1783. FontFamily = value.FontFamily;
  1784. FontSize = value.Size;
  1785. FontStyle = value.FontStyle;
  1786. }
  1787. }
  1788. /// <summary>
  1789. /// Gets or sets the font style i.e. bold, italic, and underline
  1790. /// </summary>
  1791. public FontStyle FontStyle
  1792. {
  1793. get
  1794. {
  1795. return _fontStyle;
  1796. }
  1797. set
  1798. {
  1799. if (_fontStyle == value) return;
  1800. _fontStyle = value;
  1801. SetFontStyleButtons(FontStyle);
  1802. OnFontInfoChanged();
  1803. }
  1804. }
  1805. /// <summary>
  1806. /// Gets or sets the size of the font.
  1807. /// </summary>
  1808. public float FontSize
  1809. {
  1810. get
  1811. {
  1812. bool invalid = false;
  1813. float number = _oldSizeValue;
  1814. try
  1815. {
  1816. number = float.Parse(_fontSizeComboBox.Text);
  1817. }
  1818. catch (FormatException)
  1819. {
  1820. invalid = true;
  1821. }
  1822. catch (OverflowException)
  1823. {
  1824. invalid = true;
  1825. }
  1826. // if the size is valid update the new size else return the last known good size.
  1827. if (!invalid)
  1828. {
  1829. _oldSizeValue = number;
  1830. }
  1831. return _oldSizeValue;
  1832. }
  1833. set
  1834. {
  1835. bool invalid = false;
  1836. float number = _oldSizeValue;
  1837. try
  1838. {
  1839. number = float.Parse(_fontSizeComboBox.Text);
  1840. }
  1841. catch (FormatException)
  1842. {
  1843. invalid = true;
  1844. }
  1845. catch (OverflowException)
  1846. {
  1847. invalid = true;
  1848. }
  1849. // if the size is valid update the new size else return the last known good size.
  1850. if (invalid) return;
  1851. if (float.Parse(_fontSizeComboBox.Text) == value) return;
  1852. _fontSizeComboBox.Text = value.ToString();
  1853. OnFontInfoChanged();
  1854. }
  1855. }
  1856. private FontFamily IndexToFontFamily(int i)
  1857. {
  1858. try
  1859. {
  1860. return new FontFamily((string)_fontFamilyComboBox.Items[i]);
  1861. }
  1862. catch (Exception)
  1863. {
  1864. return FontFamily.GenericSansSerif;
  1865. }
  1866. }
  1867. private Font IndexToFont(int i, float size, FontStyle style)
  1868. {
  1869. using (FontFamily ff = IndexToFontFamily(i))
  1870. {
  1871. return new FontInfo(ff, size, style).CreateFont();
  1872. }
  1873. }
  1874. /// <summary>
  1875. /// Gets or sets the font family.
  1876. /// </summary>
  1877. public FontFamily FontFamily
  1878. {
  1879. get
  1880. {
  1881. try
  1882. {
  1883. return new FontFamily((string)_fontFamilyComboBox.SelectedItem);
  1884. }
  1885. catch (ArgumentException)
  1886. {
  1887. return FontFamily.GenericSansSerif;
  1888. }
  1889. }
  1890. set
  1891. {
  1892. var current = (string)_fontFamilyComboBox.SelectedItem;
  1893. if (current == value.Name)
  1894. {
  1895. }
  1896. else
  1897. {
  1898. int index = _fontFamilyComboBox.Items.IndexOf(value.Name);
  1899. if (index != -1)
  1900. {
  1901. _fontFamilyComboBox.SelectedIndex = index;
  1902. OnFontInfoChanged();
  1903. }
  1904. else
  1905. {
  1906. // Maybe they're just specifying a font we didn't add to the list yet? aka pre-list initialization
  1907. FontInfo oldFI = FontInfo;
  1908. FontFamily newFF;
  1909. try
  1910. {
  1911. newFF = new FontFamily(value.Name);
  1912. }
  1913. catch (Exception)
  1914. {
  1915. newFF = new FontFamily(System.Drawing.Text.GenericFontFamilies.SansSerif);
  1916. }
  1917. var newFI = new FontInfo(newFF, oldFI.Size, oldFI.FontStyle);
  1918. if (newFI.CanCreateFont())
  1919. {
  1920. _fontFamilyComboBox.Items.Add(value.Name);
  1921. _fontFamilyComboBox.SelectedItem = value.Name;
  1922. }
  1923. else
  1924. {
  1925. throw new InvalidOperationException("FontFamily is not valid");
  1926. }
  1927. }
  1928. }
  1929. }
  1930. }
  1931. public TextAlignment FontAlignment
  1932. {
  1933. get
  1934. {
  1935. return _alignment;
  1936. }
  1937. set
  1938. {
  1939. if (_alignment == value) return;
  1940. _alignment = value;
  1941. // if the user sets the text alignment the buttons must be updated
  1942. if (_alignment == TextAlignment.Left)
  1943. {
  1944. _fontAlignLeftButton.Checked = true;
  1945. _fontAlignCenterButton.Checked = false;
  1946. _fontAlignRightButton.Checked = false;
  1947. }
  1948. else if (_alignment == TextAlignment.Center)
  1949. {
  1950. _fontAlignLeftButton.Checked = false;
  1951. _fontAlignCenterButton.Checked = true;
  1952. _fontAlignRightButton.Checked = false;
  1953. }
  1954. else if (_alignment == TextAlignment.Right)
  1955. {
  1956. _fontAlignLeftButton.Checked = false;
  1957. _fontAlignCenterButton.Checked = false;
  1958. _fontAlignRightButton.Checked = true;
  1959. }
  1960. else
  1961. {
  1962. throw new InvalidOperationException("Text alignment type is invalid");
  1963. }
  1964. OnTextAlignmentChanged();
  1965. }
  1966. }
  1967. private void FontFamilyComboBoxDrawItem(object sender, DrawItemEventArgs e)
  1968. {
  1969. if (e.Index != -1)
  1970. {
  1971. var displayText = (string)_fontFamilyComboBox.Items[e.Index];
  1972. SizeF stringSize = e.Graphics.MeasureString(displayText, _arialFontBase);
  1973. var size = (int)stringSize.Width;
  1974. // set up two areas to draw
  1975. const int leftMargin = 3;
  1976. Rectangle bounds = e.Bounds;
  1977. bounds.X += leftMargin;
  1978. bounds.Width -= leftMargin;
  1979. Rectangle r = bounds;
  1980. Rectangle rd = r;
  1981. rd.Width = rd.Left + size;
  1982. Rectangle rt = r;
  1983. r.X = rd.Right;
  1984. using (Font myFont = IndexToFont(e.Index, 10, FontStyle.Regular))
  1985. {
  1986. var sf = (StringFormat)StringFormat.GenericTypographic.Clone();
  1987. sf.LineAlignment = StringAlignment.Center;
  1988. sf.FormatFlags &= ~StringFormatFlags.LineLimit;
  1989. sf.FormatFlags |= StringFormatFlags.NoWrap;
  1990. bool isSymbol = Fonts.IsSymbolFont(myFont);
  1991. bool isSelected = ((e.State & DrawItemState.Selected) != 0);
  1992. Brush fillBrush;
  1993. Brush textBrush;
  1994. if (isSelected)
  1995. {
  1996. fillBrush = _highlightBrush;
  1997. textBrush = _highlightTextBrush;
  1998. }
  1999. else
  2000. {
  2001. fillBrush = _windowBrush;
  2002. textBrush = _windowTextBrush;
  2003. }
  2004. e.Graphics.FillRectangle(fillBrush, e.Bounds);
  2005. if (isSymbol)
  2006. {
  2007. e.Graphics.DrawString(displayText, _arialFontBase, textBrush, bounds, sf);
  2008. e.Graphics.DrawString(displayText, myFont, textBrush, r, sf);
  2009. }
  2010. else
  2011. {
  2012. e.Graphics.DrawString(displayText, myFont, textBrush, bounds, sf);
  2013. }
  2014. sf.Dispose();
  2015. sf = null;
  2016. }
  2017. }
  2018. e.DrawFocusRectangle();
  2019. }
  2020. private void FontFamilyComboBoxMeasureItem(object sender, MeasureItemEventArgs e)
  2021. {
  2022. // Work out what the text will be
  2023. var displayText = (string)_fontFamilyComboBox.Items[e.Index];
  2024. // Get width & height of string
  2025. SizeF stringSize;
  2026. using (Font font = IndexToFont(e.Index, 10, FontStyle.Regular))
  2027. {
  2028. stringSize = e.Graphics.MeasureString(displayText, font);
  2029. }
  2030. // Account for top margin
  2031. stringSize.Height += UI.ScaleHeight(6);
  2032. // set hight to text height
  2033. e.ItemHeight = (int)stringSize.Height;
  2034. int maxHeight = UI.ScaleHeight(20);
  2035. if (e.ItemHeight > maxHeight)
  2036. {
  2037. e.ItemHeight = maxHeight;
  2038. }
  2039. // set width to text width
  2040. e.ItemWidth = (int)stringSize.Width;
  2041. }
  2042. private void FontSizeComboBoxValidating(object sender, CancelEventArgs e)
  2043. {
  2044. try
  2045. {
  2046. bool invalid = false;
  2047. try
  2048. {
  2049. float number = float.Parse(_fontSizeComboBox.Text);
  2050. }
  2051. catch (FormatException)
  2052. {
  2053. invalid = true;
  2054. }
  2055. catch (OverflowException)
  2056. {
  2057. invalid = true;
  2058. }
  2059. if (invalid)
  2060. {
  2061. _fontSizeComboBox.BackColor = Color.Red;
  2062. _fontSizeComboBox.ToolTipText = PdnResources.GetString("TextConfigWidget.Error.InvalidNumber");
  2063. }
  2064. else
  2065. {
  2066. if ((float.Parse(_fontSizeComboBox.Text) < MinFontSize))
  2067. {
  2068. // Set the error if the size is too small.
  2069. _fontSizeComboBox.BackColor = Color.Red;
  2070. string format = PdnResources.GetString("TextConfigWidget.Error.TooSmall.Format");
  2071. string text = string.Format(format, MinFontSize);
  2072. _fontSizeComboBox.ToolTipText = text;
  2073. }
  2074. else if ((float.Parse(_fontSizeComboBox.Text) > MaxFontSize))
  2075. {
  2076. // Set the error if the size is too large.
  2077. _fontSizeComboBox.BackColor = Color.Red;
  2078. string format = PdnResources.GetString("TextConfigWidget.Error.TooLarge.Format");
  2079. string text = string.Format(format, MaxFontSize);
  2080. _fontSizeComboBox.ToolTipText = text;
  2081. }
  2082. else
  2083. {
  2084. // Clear the error, if any
  2085. _fontSizeComboBox.ToolTipText = string.Empty;
  2086. _fontSizeComboBox.BackColor = SystemColors.Window;
  2087. OnFontInfoChanged();
  2088. }
  2089. }
  2090. }
  2091. catch (FormatException)
  2092. {
  2093. e.Cancel = true;
  2094. }
  2095. }
  2096. private void FontSizeComboBoxTextChanged(object sender, EventArgs e)
  2097. {
  2098. FontSizeComboBoxValidating(sender, new CancelEventArgs());
  2099. }
  2100. private void FontFamilyComboBoxSelectedIndexChanged(object sender, EventArgs e)
  2101. {
  2102. OnFontInfoChanged();
  2103. }
  2104. private void FontFamilyComboBoxGotFocus(object sender, EventArgs e)
  2105. {
  2106. if (_fontsComboBoxPopulated) return;
  2107. AsyncInitFontNames();
  2108. using (new WaitCursorChanger(this))
  2109. {
  2110. StaticFontNamesPopulatedEvent.WaitOne();
  2111. if (_fontFamilyComboBox.ComboBox != null) _fontFamilyComboBox.ComboBox.BeginUpdate();
  2112. List<string> fontNames = _staticFontNames;
  2113. foreach (string name in fontNames.Where(name => !_fontFamilyComboBox.Items.Contains(name)))
  2114. {
  2115. _fontFamilyComboBox.Items.Add(name);
  2116. }
  2117. if (_fontFamilyComboBox.ComboBox != null) _fontFamilyComboBox.ComboBox.EndUpdate();
  2118. }
  2119. _fontsComboBoxPopulated = true;
  2120. }
  2121. public ToolBarConfigItems ToolBarConfigItems
  2122. {
  2123. get
  2124. {
  2125. return _toolBarConfigItems;
  2126. }
  2127. set
  2128. {
  2129. _toolBarConfigItems = value;
  2130. SuspendLayout();
  2131. bool showPen = ((value & ToolBarConfigItems.Pen) != ToolBarConfigItems.None);
  2132. _penSeparator.Visible = showPen;
  2133. _penSizeLabel.Visible = showPen;
  2134. _penSizeDecButton.Visible = showPen;
  2135. _penSizeComboBox.Visible = showPen;
  2136. _penSizeIncButton.Visible = showPen;
  2137. bool showPenCaps = ((value & ToolBarConfigItems.PenCaps) != ToolBarConfigItems.None);
  2138. _penStyleLabel.Visible = showPenCaps;
  2139. _penStartCapSplitButton.Visible = showPenCaps;
  2140. _penDashStyleSplitButton.Visible = showPenCaps;
  2141. _penEndCapSplitButton.Visible = showPenCaps;
  2142. bool showBrush = ((value & ToolBarConfigItems.Brush) != ToolBarConfigItems.None);
  2143. _brushSeparator.Visible = showBrush;
  2144. _brushStyleLabel.Visible = showBrush;
  2145. _brushStyleComboBox.Visible = showBrush;
  2146. bool showShape = ((value & ToolBarConfigItems.ShapeType) != ToolBarConfigItems.None);
  2147. _shapeSeparator.Visible = showShape;
  2148. _shapeButton.Visible = showShape;
  2149. bool showGradient = ((value & ToolBarConfigItems.Gradient) != ToolBarConfigItems.None);
  2150. _gradientSeparator1.Visible = showGradient;
  2151. _gradientLinearClampedButton.Visible = showGradient;
  2152. _gradientLinearReflectedButton.Visible = showGradient;
  2153. _gradientLinearDiamondButton.Visible = showGradient;
  2154. _gradientRadialButton.Visible = showGradient;
  2155. _gradientConicalButton.Visible = showGradient;
  2156. _gradientSeparator2.Visible = showGradient;
  2157. _gradientChannelsSplitButton.Visible = showGradient;
  2158. bool showAA = ((value & ToolBarConfigItems.Antialiasing) != ToolBarConfigItems.None);
  2159. _antiAliasingSplitButton.Visible = showAA;
  2160. bool showAB = ((value & ToolBarConfigItems.AlphaBlending) != ToolBarConfigItems.None);
  2161. _alphaBlendingSplitButton.Visible = showAB;
  2162. bool showBlendingSep = ((value & (ToolBarConfigItems.AlphaBlending | ToolBarConfigItems.Antialiasing)) != ToolBarConfigItems.None);
  2163. _blendingSeparator.Visible = showBlendingSep;
  2164. bool showTolerance = ((value & ToolBarConfigItems.Tolerance) != ToolBarConfigItems.None);
  2165. _toleranceSeparator.Visible = showTolerance;
  2166. _toleranceLabel.Visible = showTolerance;
  2167. _toleranceSliderStrip.Visible = showTolerance;
  2168. bool showText = ((value & ToolBarConfigItems.Text) != ToolBarConfigItems.None);
  2169. _fontSeparator.Visible = showText;
  2170. _fontLabel.Visible = showText;
  2171. _fontFamilyComboBox.Visible = showText;
  2172. _fontSizeComboBox.Visible = showText;
  2173. _fontSmoothingComboBox.Visible = showText;
  2174. _fontStyleSeparator.Visible = showText;
  2175. _fontBoldButton.Visible = showText;
  2176. _fontItalicsButton.Visible = showText;
  2177. _fontUnderlineButton.Visible = showText;
  2178. _fontAlignSeparator.Visible = showText;
  2179. _fontAlignLeftButton.Visible = showText;
  2180. _fontAlignCenterButton.Visible = showText;
  2181. _fontAlignRightButton.Visible = showText;
  2182. if (showText && IsHandleCreated)
  2183. {
  2184. AsyncInitFontNames();
  2185. }
  2186. bool showResampling = ((value & ToolBarConfigItems.Resampling) != ToolBarConfigItems.None);
  2187. _resamplingSeparator.Visible = showResampling;
  2188. _resamplingLabel.Visible = showResampling;
  2189. _resamplingComboBox.Visible = showResampling;
  2190. bool showColorPicker = ((value & ToolBarConfigItems.ColorPickerBehavior) != ToolBarConfigItems.None);
  2191. _colorPickerSeparator.Visible = showColorPicker;
  2192. _colorPickerLabel.Visible = showColorPicker;
  2193. _colorPickerComboBox.Visible = showColorPicker;
  2194. bool showSelectionCombineMode = ((value & ToolBarConfigItems.SelectionCombineMode) != ToolBarConfigItems.None);
  2195. _selectionCombineModeSeparator.Visible = showSelectionCombineMode;
  2196. _selectionCombineModeLabel.Visible = showSelectionCombineMode;
  2197. _selectionCombineModeSplitButton.Visible = showSelectionCombineMode;
  2198. bool showFloodMode = ((value & ToolBarConfigItems.FloodMode) != ToolBarConfigItems.None);
  2199. _floodModeSeparator.Visible = showFloodMode;
  2200. _floodModeLabel.Visible = showFloodMode;
  2201. _floodModeSplitButton.Visible = showFloodMode;
  2202. bool showSelectionDrawMode = ((value & ToolBarConfigItems.SelectionDrawMode) != ToolBarConfigItems.None);
  2203. _selectionDrawModeSeparator.Visible = showSelectionDrawMode;
  2204. _selectionDrawModeModeLabel.Visible = showSelectionDrawMode;
  2205. _selectionDrawModeSplitButton.Visible = showSelectionDrawMode;
  2206. _selectionDrawModeWidthLabel.Visible = showSelectionDrawMode;
  2207. _selectionDrawModeSwapButton.Visible = showSelectionDrawMode;
  2208. _selectionDrawModeHeightLabel.Visible = showSelectionDrawMode;
  2209. RefreshSelectionDrawModeInfoVisibilities();
  2210. Visible = value != ToolBarConfigItems.None;
  2211. ResumeLayout();
  2212. PerformLayout();
  2213. }
  2214. }
  2215. private void ToleranceSliderToleranceChanged(object sender, EventArgs e)
  2216. {
  2217. OnToleranceChanged();
  2218. }
  2219. public void PerformToleranceChanged()
  2220. {
  2221. OnToleranceChanged();
  2222. }
  2223. public float Tolerance
  2224. {
  2225. get
  2226. {
  2227. return _toleranceSlider.Tolerance;
  2228. }
  2229. set
  2230. {
  2231. if (value != _toleranceSlider.Tolerance)
  2232. {
  2233. _toleranceSlider.Tolerance = value;
  2234. }
  2235. }
  2236. }
  2237. public event EventHandler ToleranceChanged;
  2238. protected void OnToleranceChanged()
  2239. {
  2240. if (ToleranceChanged != null)
  2241. {
  2242. ToleranceChanged(this, EventArgs.Empty);
  2243. }
  2244. }
  2245. public event EventHandler ColorPickerClickBehaviorChanged;
  2246. protected void OnColorPickerClickBehaviorChanged()
  2247. {
  2248. if (ColorPickerClickBehaviorChanged != null)
  2249. {
  2250. ColorPickerClickBehaviorChanged(this, EventArgs.Empty);
  2251. }
  2252. }
  2253. public ColorPickerClickBehavior ColorPickerClickBehavior
  2254. {
  2255. get
  2256. {
  2257. var selectedItem = (string)_colorPickerComboBox.SelectedItem;
  2258. var cpcb = (ColorPickerClickBehavior)_colorPickerBehaviorNames.LocalizedNameToEnumValue(selectedItem);
  2259. return cpcb;
  2260. }
  2261. set
  2262. {
  2263. if (value != ColorPickerClickBehavior)
  2264. {
  2265. _colorPickerComboBox.SelectedItem = _colorPickerBehaviorNames.EnumValueToLocalizedName(value);
  2266. }
  2267. }
  2268. }
  2269. public void PerformColorPickerClickBehaviorChanged()
  2270. {
  2271. OnColorPickerClickBehaviorChanged();
  2272. }
  2273. private void ColorPickerComboBoxSelectedIndexChanged(object sender, EventArgs e)
  2274. {
  2275. OnColorPickerClickBehaviorChanged();
  2276. }
  2277. private void ResamplingComboBoxSelectedIndexChanged(object sender, EventArgs e)
  2278. {
  2279. OnResamplingAlgorithmChanged();
  2280. }
  2281. public event EventHandler ResamplingAlgorithmChanged;
  2282. protected void OnResamplingAlgorithmChanged()
  2283. {
  2284. if (ResamplingAlgorithmChanged != null)
  2285. {
  2286. ResamplingAlgorithmChanged(this, EventArgs.Empty);
  2287. }
  2288. }
  2289. public ResamplingAlgorithm ResamplingAlgorithm
  2290. {
  2291. get
  2292. {
  2293. var selectedItem = (string)_resamplingComboBox.SelectedItem;
  2294. var ra = (ResamplingAlgorithm)_resamplingAlgorithmNames.LocalizedNameToEnumValue(selectedItem);
  2295. return ra;
  2296. }
  2297. set
  2298. {
  2299. if (value == ResamplingAlgorithm) return;
  2300. if (value != ResamplingAlgorithm.NearestNeighbor && value != ResamplingAlgorithm.Bilinear)
  2301. {
  2302. throw new InvalidEnumArgumentException();
  2303. }
  2304. _resamplingComboBox.SelectedItem = _resamplingAlgorithmNames.EnumValueToLocalizedName(value);
  2305. }
  2306. }
  2307. public void PerformResamplingAlgorithmChanged()
  2308. {
  2309. OnResamplingAlgorithmChanged();
  2310. }
  2311. public event EventHandler SelectionCombineModeChanged;
  2312. protected void OnSelectionCombineModeChanged()
  2313. {
  2314. if (SelectionCombineModeChanged != null)
  2315. {
  2316. SelectionCombineModeChanged(this, EventArgs.Empty);
  2317. }
  2318. }
  2319. public CombineMode SelectionCombineMode
  2320. {
  2321. get
  2322. {
  2323. object tag = _selectionCombineModeSplitButton.Tag;
  2324. CombineMode cm = (tag == null) ? CombineMode.Replace : (CombineMode)tag;
  2325. return cm;
  2326. }
  2327. set
  2328. {
  2329. object tag = _selectionCombineModeSplitButton.Tag;
  2330. CombineMode oldCm = (tag == null) ? CombineMode.Replace : (CombineMode)tag;
  2331. if (tag != null && (value == oldCm)) return;
  2332. _selectionCombineModeSplitButton.Tag = value;
  2333. UI.SuspendControlPainting(this);
  2334. _selectionCombineModeSplitButton.Image = GetSelectionCombineModeImage(value).Reference;
  2335. UI.ResumeControlPainting(this);
  2336. OnSelectionCombineModeChanged();
  2337. Invalidate(true);
  2338. }
  2339. }
  2340. public void PerformSelectionCombineModeChanged()
  2341. {
  2342. OnSelectionCombineModeChanged();
  2343. }
  2344. private static ImageResource GetSelectionCombineModeImage(CombineMode cm)
  2345. {
  2346. return PdnResources.GetImageResource("Icons.ToolConfigStrip.SelectionCombineMode." + cm + ".png");
  2347. }
  2348. private void SelectionCombineModeSplitButtonDropDownOpening(object sender, EventArgs e)
  2349. {
  2350. _selectionCombineModeSplitButton.DropDownItems.Clear();
  2351. foreach (var cmMI in new[]
  2352. {
  2353. CombineMode.Replace, CombineMode.Union, CombineMode.Exclude, CombineMode.Intersect, CombineMode.Xor
  2354. }.Select(cm => new ToolStripMenuItem(PdnResources.GetString("ToolConfigStrip.SelectionCombineModeSplitButton." + cm + ".Text"), GetSelectionCombineModeImage(cm).Reference, delegate(object sender2, EventArgs e2)
  2355. {
  2356. var asTSMI = (ToolStripMenuItem) sender2;
  2357. var newCM = (CombineMode) asTSMI.Tag;
  2358. SelectionCombineMode = newCM;
  2359. }) {Tag = cm, Checked = (cm == SelectionCombineMode)}))
  2360. {
  2361. _selectionCombineModeSplitButton.DropDownItems.Add(cmMI);
  2362. }
  2363. }
  2364. private static CombineMode CycleSelectionCombineMode(CombineMode mode)
  2365. {
  2366. CombineMode newMode;
  2367. // Replace -> Union -> Exclude -> Intersect -> Xor -> Replace
  2368. switch (mode)
  2369. {
  2370. default:
  2371. throw new InvalidEnumArgumentException();
  2372. case CombineMode.Exclude:
  2373. newMode = CombineMode.Intersect;
  2374. break;
  2375. case CombineMode.Intersect:
  2376. newMode = CombineMode.Xor;
  2377. break;
  2378. case CombineMode.Replace:
  2379. newMode = CombineMode.Union;
  2380. break;
  2381. case CombineMode.Union:
  2382. newMode = CombineMode.Exclude;
  2383. break;
  2384. case CombineMode.Xor:
  2385. newMode = CombineMode.Replace;
  2386. break;
  2387. }
  2388. return newMode;
  2389. }
  2390. public event EventHandler FloodModeChanged;
  2391. protected void OnFloodModeChanged()
  2392. {
  2393. if (FloodModeChanged != null)
  2394. {
  2395. FloodModeChanged(this, EventArgs.Empty);
  2396. }
  2397. }
  2398. public FloodMode FloodMode
  2399. {
  2400. get
  2401. {
  2402. object tag = _floodModeSplitButton.Tag;
  2403. FloodMode fm = (tag == null) ? FloodMode.Local : (FloodMode)tag;
  2404. return fm;
  2405. }
  2406. set
  2407. {
  2408. object tag = _floodModeSplitButton.Tag;
  2409. FloodMode oldFm = (tag == null) ? FloodMode.Local : (FloodMode)tag;
  2410. if (tag != null && (value == oldFm)) return;
  2411. _floodModeSplitButton.Tag = value;
  2412. _floodModeSplitButton.Image = GetFloodModeImage(value).Reference;
  2413. OnFloodModeChanged();
  2414. }
  2415. }
  2416. public void PerformFloodModeChanged()
  2417. {
  2418. OnFloodModeChanged();
  2419. }
  2420. private static ImageResource GetFloodModeImage(FloodMode cm)
  2421. {
  2422. return PdnResources.GetImageResource("Icons.ToolConfigStrip.FloodMode." + cm + ".png");
  2423. }
  2424. private void FloodModeSplitButtonDropDownOpening(object sender, EventArgs e)
  2425. {
  2426. _floodModeSplitButton.DropDownItems.Clear();
  2427. foreach (var fmMI in new[]
  2428. {
  2429. FloodMode.Local, FloodMode.Global
  2430. }.Select(fm => new ToolStripMenuItem(PdnResources.GetString("ToolConfigStrip.FloodModeSplitButton." + fm + ".Text"), GetFloodModeImage(fm).Reference, delegate(object sender2, EventArgs e2)
  2431. {
  2432. var asTSMI = (ToolStripMenuItem) sender2;
  2433. var newFM = (FloodMode) asTSMI.Tag;
  2434. FloodMode = newFM;
  2435. }) {Tag = fm, Checked = (fm == FloodMode)}))
  2436. {
  2437. _floodModeSplitButton.DropDownItems.Add(fmMI);
  2438. }
  2439. }
  2440. private static FloodMode CycleFloodMode(FloodMode mode)
  2441. {
  2442. FloodMode newMode;
  2443. switch (mode)
  2444. {
  2445. default:
  2446. throw new InvalidEnumArgumentException();
  2447. case FloodMode.Global:
  2448. newMode = FloodMode.Local;
  2449. break;
  2450. case FloodMode.Local:
  2451. newMode = FloodMode.Global;
  2452. break;
  2453. }
  2454. return newMode;
  2455. }
  2456. public event EventHandler SelectionDrawModeInfoChanged;
  2457. protected void OnSelectionDrawModeInfoChanged()
  2458. {
  2459. if (SelectionDrawModeInfoChanged != null)
  2460. {
  2461. SelectionDrawModeInfoChanged(this, EventArgs.Empty);
  2462. }
  2463. }
  2464. // syncs SelectionDrawModeInfo into the controls
  2465. private void SyncSelectionDrawModeInfoUI()
  2466. {
  2467. _selectionDrawModeSplitButton.Text = GetSelectionDrawModeString(_selectionDrawModeInfo.DrawMode);
  2468. _selectionDrawModeSplitButton.Image = GetSelectionDrawModeImage(_selectionDrawModeInfo.DrawMode);
  2469. _selectionDrawModeWidthTextBox.Text = _selectionDrawModeInfo.Width.ToString();
  2470. _selectionDrawModeHeightTextBox.Text = _selectionDrawModeInfo.Height.ToString();
  2471. _selectionDrawModeUnits.UnitsChanged -= SelectionDrawModeUnitsUnitsChanged;
  2472. _selectionDrawModeUnits.Units = _selectionDrawModeInfo.Units;
  2473. _selectionDrawModeUnits.UnitsChanged += SelectionDrawModeUnitsUnitsChanged;
  2474. RefreshSelectionDrawModeInfoVisibilities();
  2475. }
  2476. private void RefreshSelectionDrawModeInfoVisibilities()
  2477. {
  2478. if (_selectionDrawModeInfo == null) return;
  2479. SuspendLayout();
  2480. bool anyVisible = (ToolBarConfigItems & ToolBarConfigItems.SelectionDrawMode) != ToolBarConfigItems.None;
  2481. _selectionDrawModeModeLabel.Visible = false;
  2482. bool showWidthHeight = anyVisible & (_selectionDrawModeInfo.DrawMode != SelectionDrawMode.Normal);
  2483. _selectionDrawModeWidthTextBox.Visible = showWidthHeight;
  2484. _selectionDrawModeHeightTextBox.Visible = showWidthHeight;
  2485. _selectionDrawModeWidthLabel.Visible = showWidthHeight;
  2486. _selectionDrawModeHeightLabel.Visible = showWidthHeight;
  2487. _selectionDrawModeSwapButton.Visible = showWidthHeight;
  2488. _selectionDrawModeUnits.Visible = anyVisible & (_selectionDrawModeInfo.DrawMode == SelectionDrawMode.FixedSize);
  2489. ResumeLayout(false);
  2490. PerformLayout();
  2491. }
  2492. public SelectionDrawModeInfo SelectionDrawModeInfo
  2493. {
  2494. get
  2495. {
  2496. return (_selectionDrawModeInfo ?? SelectionDrawModeInfo.CreateDefault()).Clone();
  2497. }
  2498. set
  2499. {
  2500. if (_selectionDrawModeInfo != null && _selectionDrawModeInfo.Equals(value)) return;
  2501. _selectionDrawModeInfo = value.Clone();
  2502. OnSelectionDrawModeInfoChanged();
  2503. SyncSelectionDrawModeInfoUI();
  2504. }
  2505. }
  2506. public void PerformSelectionDrawModeInfoChanged()
  2507. {
  2508. OnSelectionDrawModeInfoChanged();
  2509. }
  2510. private static string GetSelectionDrawModeString(SelectionDrawMode drawMode)
  2511. {
  2512. return PdnResources.GetString("ToolConfigStrip.SelectionDrawModeSplitButton." + drawMode + ".Text");
  2513. }
  2514. private static Image GetSelectionDrawModeImage(SelectionDrawMode drawMode)
  2515. {
  2516. return PdnResources.GetImageResource("Icons.ToolConfigStrip.SelectionDrawModeSplitButton." + drawMode + ".png").Reference;
  2517. }
  2518. private void SelectionDrawModeSplitButtonDropDownOpening(object sender, EventArgs e)
  2519. {
  2520. _selectionDrawModeSplitButton.DropDownItems.Clear();
  2521. foreach (var sdmTSMI in new[]
  2522. {
  2523. SelectionDrawMode.Normal, SelectionDrawMode.FixedRatio, SelectionDrawMode.FixedSize
  2524. }.Select(sdm => new ToolStripMenuItem(GetSelectionDrawModeString(sdm), GetSelectionDrawModeImage(sdm), delegate(object sender2, EventArgs e2)
  2525. {
  2526. var asTSMI = (ToolStripMenuItem) sender2;
  2527. var newSDM = (SelectionDrawMode) asTSMI.Tag;
  2528. SelectionDrawModeInfo = SelectionDrawModeInfo.CloneWithNewDrawMode(newSDM);
  2529. }) {Tag = sdm, Checked = (sdm == SelectionDrawModeInfo.DrawMode)}))
  2530. {
  2531. _selectionDrawModeSplitButton.DropDownItems.Add(sdmTSMI);
  2532. }
  2533. }
  2534. private static SelectionDrawMode CycleSelectionDrawMode(SelectionDrawMode drawMode)
  2535. {
  2536. SelectionDrawMode newSDM;
  2537. switch (drawMode)
  2538. {
  2539. case SelectionDrawMode.Normal:
  2540. newSDM = SelectionDrawMode.FixedRatio;
  2541. break;
  2542. case SelectionDrawMode.FixedRatio:
  2543. newSDM = SelectionDrawMode.FixedSize;
  2544. break;
  2545. case SelectionDrawMode.FixedSize:
  2546. newSDM = SelectionDrawMode.Normal;
  2547. break;
  2548. default:
  2549. throw new InvalidEnumArgumentException();
  2550. }
  2551. return newSDM;
  2552. }
  2553. private void SelectionDrawModeUnitsUnitsChanged(object sender, EventArgs e)
  2554. {
  2555. OnSelectionDrawModeUnitsChanging();
  2556. SelectionDrawModeInfo = _selectionDrawModeInfo.CloneWithNewUnits(_selectionDrawModeUnits.Units);
  2557. OnSelectionDrawModeUnitsChanged();
  2558. }
  2559. }
  2560. }