Skip to content

Commit

Permalink
Added base classes for better toolbar items.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiehighfield committed May 19, 2022
1 parent 322ed3b commit 917990b
Show file tree
Hide file tree
Showing 2 changed files with 322 additions and 0 deletions.
227 changes: 227 additions & 0 deletions src/BetterControls/BetterToolbar/Items/BetterToolbarItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
/* COPYRIGHT NOTICE
MIT License
Copyright (c) 2022 SharpVNC Limited
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

using BetterControls.ComponentModel;
using System.ComponentModel;
using System.Drawing;

namespace BetterControls
{
/// <summary>
/// Extend this class to create a toolbar item.
/// </summary>
[ToolboxItem(false)]
[DesignTimeVisible(false)]
[Designer("BetterToolbarItemDesigner")]
public abstract partial class BetterToolbarItem : BetterToolbarItemBase
{
/// <summary>
/// Initialize a new instance of <see cref="BetterToolbarItem"/>.
/// </summary>
private protected BetterToolbarItem() { }

/// <summary>
/// Initialize a new instance of <see cref="BetterToolbarItem"/>.
/// </summary>
/// <param name="ownerToolbar">The parent toolbar as an instance of <see cref="BetterToolbar"/>.</param>
private protected BetterToolbarItem(BetterToolbar ownerToolbar)
: base(ownerToolbar)
{ }

private bool _autoSize = true;
private bool _visible = true;
private int _width;
private int _uniqueIdentifier = -1;

/// <summary>
/// Gets or sets a <see cref="bool"/> value indicating whether or not the button should be auto-sized.
/// </summary>
[Category(Categories.Behavior)]
[Description("Value indicating whether or not the button should be auto-sized.")]
[DefaultValue(true)]
[Localizable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public virtual bool AutoSize
{
get => _autoSize;
set
{
if (AutoSize != value)
{
_autoSize = value;

PerformItemChanged(CollectionElementItemChangedFlags.None);
}
}
}

/// <summary>
/// Gets or sets a <see cref="bool"/> value indicating whether or not the toolbar item is visible.
/// </summary>
[Category(Categories.Behavior)]
[Localizable(true)]
[DefaultValue(true)]
[Description("Value indicating whether or not the toobar item is visible.")]
public bool Visible
{
get => _visible;
set
{
if (Visible != value)
{
_visible = value;

PerformItemChanged(CollectionElementItemChangedFlags.None);
}
}
}

/// <summary>
/// Gets the bounds of the toolbar item.
/// </summary>
[Browsable(false)]
public Rectangle Rectangle
{
get
{
if (IsOwnerHandleCreated)
{
NativeMethods.RECT rc = new NativeMethods.RECT();
UnsafeNativeMethods.SendMessage(GetHandleRef(), NativeMethods.TB_GETRECT, UniqueIdentifier, ref rc);

return Rectangle.FromLTRB(rc.left, rc.top, rc.right, rc.bottom);
}

return Rectangle.Empty;
}
}

/// <summary>
/// Gets the computed width of this item.
/// </summary>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public virtual int ComputedWidth
{
get
{
if (AutoSize)
return ComputeAutoSizeWidth();

return Width;
}
}

/// <summary>
/// Gets or sets the width of the toolbar item.
/// </summary>
[Category(Categories.Appearance)]
[Description("The width of the toolbar item.")]
[DefaultValue(0)]
[Localizable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public virtual int Width
{
get => _width;
set
{
if (Width != value)
{
_width = value;

PerformItemChanged(CollectionElementItemChangedFlags.None);
}
}
}

/// <summary>
/// Gets the unique identifier of this toolbar item, relative to the parent toolbar.
/// </summary>
[Browsable(false)]
public int UniqueIdentifier => _uniqueIdentifier;

/// <summary>
/// Resets the unique identifier of this item.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
internal void ResetUniqueIdentifier() => _uniqueIdentifier = -1;

/// <summary>
/// Sets the unique identifier of this item.
/// </summary>
/// <param name="uniqueIdentifier">The unique identifier of this item.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
internal void SetUniqueIdentifier(int uniqueIdentifier) => _uniqueIdentifier = uniqueIdentifier;

/// <summary>
/// Computes the auto-size width of the toolbar button.
/// </summary>
/// <returns>The computed auto-size width of the toolbar button.</returns>
protected abstract int ComputeAutoSizeWidth();

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="flags"><inheritdoc/></param>
protected override void PerformItemChanged(CollectionElementItemChangedFlags flags)
{
if (IsOwnerHandleCreated)
{
NativeMethods.TBBUTTONINFO structure = ComputeTbButtonInfo();

UnsafeNativeMethods.SendMessage(GetHandleRef(), NativeMethods.TB_SETBUTTONINFO, ItemIndex, ref structure);

// Reflect this change to the parent control.
OwnerToolbar.PerformItemsChanged(flags, new BetterToolbarItem[]
{
this
});
}
}

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <returns><inheritdoc/></returns>
public override string ToString() => nameof(BetterToolbarItem);

/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="disposing"><inheritdoc/></param>
protected override void Dispose(bool disposing)
{
// Disposed items cannot exist in a collection.
Remove();

// If in design mode, also remove this item from the site as well.
if (Site != null && Site.Container != null)
Site.Container.Remove(this);

base.Dispose(disposing);
}
}
}
95 changes: 95 additions & 0 deletions src/BetterControls/BetterToolbar/Items/BetterToolbarItemBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/* COPYRIGHT NOTICE
MIT License
Copyright (c) 2022 SharpVNC Limited
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

using System.ComponentModel;
using System.Runtime.InteropServices;

namespace BetterControls
{
/// <summary>
/// Extend this class to create a toolbar item.
/// </summary>
public abstract class BetterToolbarItemBase : ComponentCollectionElement
{
/// <summary>
/// Initialize a new instance of <see cref="BetterToolbarItemBase"/>.
/// </summary>
private protected BetterToolbarItemBase() { }

/// <summary>
/// Initialize a new instance of <see cref="BetterToolbarItemBase"/>.
/// </summary>
/// <param name="ownerToolbar">The owner control as an instance of <see cref="BetterControl"/>.</param>
private protected BetterToolbarItemBase(BetterToolbar ownerToolbar)
: base(ownerToolbar)
{ }

/// <summary>
/// Gets the owner toolbar as an instance of <see cref="BetterToolbar"/>.
/// </summary>
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public BetterToolbar OwnerToolbar
{
get
{
if (OwnerElement != null)
return (BetterToolbar)OwnerElement;

return null;
}
}

/// <summary>
/// Gets a <see cref="bool"/> value indicating whether or not the handle for this owner toolbar has been created.
/// </summary>
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Advanced)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool IsOwnerHandleCreated {

get
{
if (OwnerToolbar != null)
return OwnerToolbar.IsHandleCreated;

return false;
}
}

/// <summary>
/// Gets a <see cref="HandleRef"/> for the current component.
/// </summary>
/// <returns>An instance of <see cref="HandleRef"/>.</returns>
protected virtual HandleRef GetHandleRef()
{
if (!IsOwnerHandleCreated)
return default;

return new HandleRef(OwnerToolbar, OwnerToolbar.Handle);
}
}
}

0 comments on commit 917990b

Please sign in to comment.