Fix scaling of categories dropdown and options dialog (#450)

* Scale height of category checkbox list

* Fix bitmap layout

* Clean up code
This commit is contained in:
David Gardiner 2017-03-12 17:37:12 +10:30 committed by Jon Galloway
parent 9a53062fc9
commit 7f4d51d204
2 changed files with 41 additions and 116 deletions

View File

@ -4,6 +4,7 @@
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using System.Drawing; using System.Drawing;
using System.Security.Permissions;
using System.Windows.Forms; using System.Windows.Forms;
using OpenLiveWriter.CoreServices; using OpenLiveWriter.CoreServices;
using OpenLiveWriter.CoreServices.UI; using OpenLiveWriter.CoreServices.UI;
@ -128,13 +129,13 @@ namespace OpenLiveWriter.Controls
/// A value which indicates how the BitmapButton should automcatically determine its /// A value which indicates how the BitmapButton should automcatically determine its
/// width. /// width.
/// </summary> /// </summary>
private bool autoSizeWidth = false; private bool autoSizeWidth;
/// <summary> /// <summary>
/// A value which indicates how the BitmapButton should automcatically determine its /// A value which indicates how the BitmapButton should automcatically determine its
/// height. /// height.
/// </summary> /// </summary>
private bool autoSizeHeight = false; private bool autoSizeHeight;
/// <summary> /// <summary>
/// A value which how the button is drawn by the control. /// A value which how the button is drawn by the control.
@ -146,26 +147,11 @@ namespace OpenLiveWriter.Controls
/// </summary> /// </summary>
private bool clickSetsFocus; private bool clickSetsFocus;
/// <summary>
/// The button bitmap for the disabled state.
/// </summary>
private Bitmap bitmapDisabled;
/// <summary> /// <summary>
/// The button bitmap for the enabled state. /// The button bitmap for the enabled state.
/// </summary> /// </summary>
private Bitmap bitmapEnabled; private Bitmap bitmapEnabled;
/// <summary>
/// The bitmap for the selected state.
/// </summary>
private Bitmap bitmapSelected;
/// <summary>
/// The bitmap for the pushed state.
/// </summary>
private Bitmap bitmapPushed;
/// <summary> /// <summary>
/// The text alignment for the button. /// The text alignment for the button.
/// </summary> /// </summary>
@ -194,36 +180,31 @@ namespace OpenLiveWriter.Controls
/// <summary> /// <summary>
/// A value indicating whether the button is latched. /// A value indicating whether the button is latched.
/// </summary> /// </summary>
private bool latched = false; private bool latched;
/// <summary> /// <summary>
/// A value indicating whether a key press has pushed the button. /// A value indicating whether a key press has pushed the button.
/// </summary> /// </summary>
private bool pushedByKeystroke = false; private bool pushedByKeystroke;
/// <summary> /// <summary>
/// A value indicating whether the mouse is inside the control. /// A value indicating whether the mouse is inside the control.
/// </summary> /// </summary>
private bool mouseInside = false; private bool mouseInside;
/// <summary> /// <summary>
/// A value indicating whether the left mouse button is down. /// A value indicating whether the left mouse button is down.
/// </summary> /// </summary>
private bool leftMouseDown = false; private bool leftMouseDown;
/// <summary> /// <summary>
/// A value indicating whether the right mouse button is down. /// A value indicating whether the right mouse button is down.
/// </summary> /// </summary>
private bool rightMouseDown = false; private bool rightMouseDown;
/// <summary> private bool useVirtualTransparency;
/// A value indicating whether focus is being shown.
/// </summary>
private bool showFocus = true;
private bool useVirtualTransparency = false; private bool allowMirroring;
private bool allowMirroring = false;
#endregion Private Member Variables & Declarations #endregion Private Member Variables & Declarations
@ -244,9 +225,7 @@ namespace OpenLiveWriter.Controls
/// <param name="container"></param> /// <param name="container"></param>
public BitmapButton(IContainer container) public BitmapButton(IContainer container)
{ {
/// <summary> // Required for Windows.Forms Class Composition Designer support
/// Required for Windows.Forms Class Composition Designer support
/// </summary>
container.Add(this); container.Add(this);
InitializeComponent(); InitializeComponent();
@ -259,9 +238,7 @@ namespace OpenLiveWriter.Controls
/// </summary> /// </summary>
public BitmapButton() public BitmapButton()
{ {
/// <summary> // Required for Windows.Forms Class Composition Designer support
/// Required for Windows.Forms Class Composition Designer support
/// </summary>
InitializeComponent(); InitializeComponent();
// Do common initialization. // Do common initialization.
@ -294,10 +271,7 @@ namespace OpenLiveWriter.Controls
{ {
if (disposing) if (disposing)
{ {
if (components != null) components?.Dispose();
{
components.Dispose();
}
} }
base.Dispose(disposing); base.Dispose(disposing);
} }
@ -412,18 +386,7 @@ namespace OpenLiveWriter.Controls
DefaultValue(null), DefaultValue(null),
Description("Specifies the button bitmap for the disabled state.") Description("Specifies the button bitmap for the disabled state.")
] ]
public Bitmap BitmapDisabled public Bitmap BitmapDisabled { get; set; }
{
get
{
return bitmapDisabled;
}
set
{
bitmapDisabled = value;
}
}
/// <summary> /// <summary>
/// Gets or sets the bitmap for the enabled state. /// Gets or sets the bitmap for the enabled state.
@ -456,18 +419,7 @@ namespace OpenLiveWriter.Controls
DefaultValue(null), DefaultValue(null),
Description("Specifies the bitmap for the selected state.") Description("Specifies the bitmap for the selected state.")
] ]
public Bitmap BitmapSelected public Bitmap BitmapSelected { get; set; }
{
get
{
return bitmapSelected;
}
set
{
bitmapSelected = value;
}
}
/// <summary> /// <summary>
/// Gets or sets the bitmap for the pushed state. /// Gets or sets the bitmap for the pushed state.
@ -478,18 +430,8 @@ namespace OpenLiveWriter.Controls
DefaultValue(null), DefaultValue(null),
Description("Specifies the bitmap for the pushed state. Used only when ButtonStyle.Bitmap is specified.") Description("Specifies the bitmap for the pushed state. Used only when ButtonStyle.Bitmap is specified.")
] ]
public Bitmap BitmapPushed
{
get
{
return bitmapPushed;
}
set public Bitmap BitmapPushed { get; set; }
{
bitmapPushed = value;
}
}
/// <summary> /// <summary>
/// Gets or sets the text alignment for the button. /// Gets or sets the text alignment for the button.
@ -555,9 +497,7 @@ namespace OpenLiveWriter.Controls
{ {
get get
{ {
if (toolTip == null) return toolTip?.GetToolTip(this);
return null;
return toolTip.GetToolTip(this);
} }
set set
@ -567,7 +507,7 @@ namespace OpenLiveWriter.Controls
// we need to instantiate this tooltip lazily because it // we need to instantiate this tooltip lazily because it
// causes memory leaks if the form it lives on is not collected, // causes memory leaks if the form it lives on is not collected,
// which is the case for the property shelf. // which is the case for the property shelf.
toolTip = new ToolTip2(this.components); toolTip = new ToolTip2(components);
} }
toolTip.SetToolTip(this, value); toolTip.SetToolTip(this, value);
@ -673,7 +613,6 @@ namespace OpenLiveWriter.Controls
// Track whether focus is being shown. // Track whether focus is being shown.
if (e.ChangeFocus) if (e.ChangeFocus)
{ {
showFocus = e.ShowFocus;
Invalidate(); Invalidate();
} }
@ -971,7 +910,8 @@ namespace OpenLiveWriter.Controls
// drawBitmapRectangle.Offset(ScaleX(PUSHED_OFFSET), ScaleY(PUSHED_OFFSET)); // drawBitmapRectangle.Offset(ScaleX(PUSHED_OFFSET), ScaleY(PUSHED_OFFSET));
// Draw the bitmap. // Draw the bitmap.
g.DrawImage(AllowMirroring, buttonBitmap, new Rectangle(ScaleX(drawBitmapRectangle.X), ScaleY(drawBitmapRectangle.Y), ScaleX(buttonBitmap.Width), ScaleY(buttonBitmap.Height))); var destRect = new Rectangle(drawBitmapRectangle.X, drawBitmapRectangle.Y, ScaleX(buttonBitmap.Width), ScaleY(buttonBitmap.Height));
g.DrawImage(AllowMirroring, buttonBitmap, destRect);
} }
// If focus is being shown, and we're focused, draw the focus rectangle. // If focus is being shown, and we're focused, draw the focus rectangle.
@ -1030,7 +970,7 @@ namespace OpenLiveWriter.Controls
/// <param name="msg">A Message, passed by reference, that represents the window message to process.</param> /// <param name="msg">A Message, passed by reference, that represents the window message to process.</param>
/// <param name="keyData">One of the Keys values that represents the key to process.</param> /// <param name="keyData">One of the Keys values that represents the key to process.</param>
/// <returns>true if the character was processed by the control; otherwise, false.</returns> /// <returns>true if the character was processed by the control; otherwise, false.</returns>
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")] [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{ {
Keys key = keyData & ~(Keys.Control | Keys.Shift); Keys key = keyData & ~(Keys.Control | Keys.Shift);
@ -1039,10 +979,7 @@ namespace OpenLiveWriter.Controls
PushByKeystroke(); PushByKeystroke();
return true; return true;
} }
else return base.ProcessCmdKey(ref msg, keyData);
{
return base.ProcessCmdKey(ref msg, keyData);
}
} }
protected virtual bool IsPushKey(Keys key) protected virtual bool IsPushKey(Keys key)

View File

@ -4,27 +4,22 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.Data;
using System.Globalization; using System.Globalization;
using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
using OpenLiveWriter.CoreServices; using OpenLiveWriter.CoreServices;
using OpenLiveWriter.Extensibility.BlogClient; using OpenLiveWriter.Extensibility.BlogClient;
using OpenLiveWriter.HtmlParser.Parser; using OpenLiveWriter.HtmlParser.Parser;
using OpenLiveWriter.Interop.Windows; using OpenLiveWriter.Interop.Windows;
using OpenLiveWriter.Localization;
namespace OpenLiveWriter.PostEditor.PostPropertyEditing.CategoryControl namespace OpenLiveWriter.PostEditor.PostPropertyEditing.CategoryControl
{ {
internal partial class TreeCategorySelector : UserControl, ICategorySelector internal partial class TreeCategorySelector : UserControl, ICategorySelector
{ {
private readonly CategoryContext ctx; private readonly CategoryContext _ctx;
private TreeNode[] nodes = new TreeNode[0]; private string _lastQuery = "";
private string lastQuery = ""; private bool _initMode;
private bool initMode = false;
internal class DoubleClicklessTreeView : TreeView internal class DoubleClicklessTreeView : TreeView
{ {
@ -52,7 +47,7 @@ namespace OpenLiveWriter.PostEditor.PostPropertyEditing.CategoryControl
public TreeCategorySelector(CategoryContext ctx) public TreeCategorySelector(CategoryContext ctx)
{ {
this.ctx = ctx; _ctx = ctx;
InitializeComponent(); InitializeComponent();
// TODO: Whoops, missed UI Freeze... add this later // TODO: Whoops, missed UI Freeze... add this later
@ -71,6 +66,8 @@ namespace OpenLiveWriter.PostEditor.PostPropertyEditing.CategoryControl
treeView.BeforeCollapse += delegate (object sender, TreeViewCancelEventArgs e) { e.Cancel = true; }; treeView.BeforeCollapse += delegate (object sender, TreeViewCancelEventArgs e) { e.Cancel = true; };
treeView.AfterCheck += treeView1_AfterCheck; treeView.AfterCheck += treeView1_AfterCheck;
treeView.ItemHeight = (int) DisplayHelper.ScaleY(treeView.ItemHeight);
treeView.LostFocus += delegate { treeView.Invalidate(); }; treeView.LostFocus += delegate { treeView.Invalidate(); };
} }
@ -101,10 +98,10 @@ namespace OpenLiveWriter.PostEditor.PostPropertyEditing.CategoryControl
void treeView1_AfterCheck(object sender, TreeViewEventArgs e) void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{ {
if (initMode) if (_initMode)
return; return;
List<BlogPostCategory> categories = new List<BlogPostCategory>(ctx.SelectedCategories); List<BlogPostCategory> categories = new List<BlogPostCategory>(_ctx.SelectedCategories);
TreeNode realTreeNode = (TreeNode)e.Node.Tag; TreeNode realTreeNode = (TreeNode)e.Node.Tag;
realTreeNode.Checked = e.Node.Checked; realTreeNode.Checked = e.Node.Checked;
BlogPostCategory category = (BlogPostCategory)(realTreeNode.Tag); BlogPostCategory category = (BlogPostCategory)(realTreeNode.Tag);
@ -117,7 +114,7 @@ namespace OpenLiveWriter.PostEditor.PostPropertyEditing.CategoryControl
} }
else else
categories.Remove(category); categories.Remove(category);
ctx.SelectedCategories = categories.ToArray(); _ctx.SelectedCategories = categories.ToArray();
} }
public static TreeNode[] CategoriesToNodes(BlogPostCategory[] categories) public static TreeNode[] CategoriesToNodes(BlogPostCategory[] categories)
@ -154,13 +151,7 @@ namespace OpenLiveWriter.PostEditor.PostPropertyEditing.CategoryControl
return (TreeNode[])ArrayHelper.Compact(allNodes); return (TreeNode[])ArrayHelper.Compact(allNodes);
} }
private TreeNode[] RealNodes private TreeNode[] RealNodes { get; set; } = new TreeNode[0];
{
get
{
return nodes;
}
}
private static TreeNode[] FilteredNodes(IEnumerable nodes, Predicate<TreeNode> predicate) private static TreeNode[] FilteredNodes(IEnumerable nodes, Predicate<TreeNode> predicate)
{ {
@ -181,8 +172,7 @@ namespace OpenLiveWriter.PostEditor.PostPropertyEditing.CategoryControl
if (results == null) if (results == null)
return new TreeNode[0]; return new TreeNode[0];
else return results.ToArray();
return results.ToArray();
} }
private TreeNode FindFirstMatch(TreeNodeCollection nodes, Predicate<TreeNode> predicate) private TreeNode FindFirstMatch(TreeNodeCollection nodes, Predicate<TreeNode> predicate)
@ -224,14 +214,14 @@ namespace OpenLiveWriter.PostEditor.PostPropertyEditing.CategoryControl
public void LoadCategories() public void LoadCategories()
{ {
initMode = true; _initMode = true;
try try
{ {
nodes = CategoriesToNodes(ctx.Categories); RealNodes = CategoriesToNodes(_ctx.Categories);
treeView.Nodes.Clear(); treeView.Nodes.Clear();
treeView.Nodes.AddRange(FilteredNodes(RealNodes, delegate { return true; })); treeView.Nodes.AddRange(FilteredNodes(RealNodes, delegate { return true; }));
HashSet selectedCategories = new HashSet(); HashSet selectedCategories = new HashSet();
selectedCategories.AddAll(ctx.SelectedCategories); selectedCategories.AddAll(_ctx.SelectedCategories);
if (selectedCategories.Count > 0) if (selectedCategories.Count > 0)
WalkNodes(treeView.Nodes, delegate (TreeNode n) WalkNodes(treeView.Nodes, delegate (TreeNode n)
{ {
@ -241,7 +231,7 @@ namespace OpenLiveWriter.PostEditor.PostPropertyEditing.CategoryControl
} }
finally finally
{ {
initMode = false; _initMode = false;
} }
} }
@ -255,7 +245,7 @@ namespace OpenLiveWriter.PostEditor.PostPropertyEditing.CategoryControl
return node.Text.ToLower(CultureInfo.CurrentCulture).IndexOf(criteria, StringComparison.CurrentCultureIgnoreCase) >= 0; return node.Text.ToLower(CultureInfo.CurrentCulture).IndexOf(criteria, StringComparison.CurrentCultureIgnoreCase) >= 0;
}; };
if (criteria.Length > 0 && criteria.StartsWith(lastQuery)) if (criteria.Length > 0 && criteria.StartsWith(_lastQuery))
{ {
KeepNodes(treeView.Nodes, prefixPredicate); KeepNodes(treeView.Nodes, prefixPredicate);
} }
@ -292,7 +282,7 @@ namespace OpenLiveWriter.PostEditor.PostPropertyEditing.CategoryControl
treeView.EndUpdate(); treeView.EndUpdate();
} }
lastQuery = criteria; _lastQuery = criteria;
} }
@ -327,8 +317,7 @@ namespace OpenLiveWriter.PostEditor.PostPropertyEditing.CategoryControl
treeView.SelectedNode = nextNode; treeView.SelectedNode = nextNode;
} }
if (treeView.SelectedNode != null) treeView.SelectedNode?.EnsureVisible();
treeView.SelectedNode.EnsureVisible();
treeView.Focus(); treeView.Focus();
} }
@ -350,8 +339,7 @@ namespace OpenLiveWriter.PostEditor.PostPropertyEditing.CategoryControl
treeView.SelectedNode = nextNode; treeView.SelectedNode = nextNode;
} }
if (treeView.SelectedNode != null) treeView.SelectedNode?.EnsureVisible();
treeView.SelectedNode.EnsureVisible();
treeView.Focus(); treeView.Focus();
} }