Skip to content

Commit

Permalink
V2.16.0 Step2
Browse files Browse the repository at this point in the history
  • Loading branch information
harborsiem committed Jun 12, 2024
1 parent 1d50939 commit 0a189b3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
#### Changed (Ribbon)

- New functions for Gallery controls (InvalidateItemsSource() InvalidateCategories()).
- New function in the Ribbon class (IRibbonControl GetRibbonControlById(uint commandId)) to get an IRibbonControl by a CommandId.
- Remove deprecated .NET7 assembly

#### Changed (RibbonTools)
Expand Down
36 changes: 29 additions & 7 deletions Ribbon/Ribbon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using System.Windows.Forms;
using System.ComponentModel;
using MethodInvoker = System.Windows.Forms.MethodInvoker;
using RibbonLib.Controls;

namespace RibbonLib
{
Expand All @@ -36,8 +37,7 @@ public class Ribbon : Control, IUICommandHandler
private IUIImageFromBitmap _imageFromBitmap;
private UIImage _uiImage;
private RibbonUIApplication _application;
private Dictionary<uint, IRibbonControl> _mapRibbonControls = new Dictionary<uint, IRibbonControl>();
internal Dictionary<uint, IRibbonControl> MapRibbonControls { get { return _mapRibbonControls; } }
private Dictionary<uint, BaseRibbonControl> _mapRibbonControls = new Dictionary<uint, BaseRibbonControl>();
private IntPtr _loadedDllHandle = IntPtr.Zero;

private const string DefaultResourceName = "APPLICATION_RIBBON";
Expand Down Expand Up @@ -1122,7 +1122,7 @@ private static IUIImageFromBitmap CreateImageFromBitmapFactory()
/// Adds a ribbon control to the internal map
/// </summary>
/// <param name="ribbonControl">ribbon control to add</param>
internal void AddRibbonControl(IRibbonControl ribbonControl)
internal void AddRibbonControl(BaseRibbonControl ribbonControl)
{
_mapRibbonControls.Add(ribbonControl.CommandID, ribbonControl);
}
Expand Down Expand Up @@ -1172,9 +1172,10 @@ HRESULT IUICommandHandler.Execute(uint commandID, ExecutionVerb verb, PropertyKe
Debug.WriteLine(string.Format("Execute verb: {0} for command {1}", verb, commandID));
#endif

if (_mapRibbonControls.ContainsKey(commandID))
if (TryGetRibbonControlById(commandID, out BaseRibbonControl control))
{
return _mapRibbonControls[commandID].Execute(verb, key, currentValue, commandExecutionProperties);
IRibbonControl item = control as IRibbonControl;
return item.Execute(verb, key, currentValue, commandExecutionProperties);
}

return HRESULT.S_OK;
Expand All @@ -1196,9 +1197,10 @@ HRESULT IUICommandHandler.UpdateProperty(uint commandID, ref PropertyKey key, Pr
Debug.WriteLine(string.Format("UpdateProperty key: {0} for command {1}", RibbonProperties.GetPropertyKeyName(ref key), commandID));
#endif

if (_mapRibbonControls.ContainsKey(commandID))
if (TryGetRibbonControlById(commandID, out BaseRibbonControl control))
{
return _mapRibbonControls[commandID].UpdateProperty(ref key, currentValue, ref newValue);
IRibbonControl item = control as IRibbonControl;
return item.UpdateProperty(ref key, currentValue, ref newValue);
}

return HRESULT.S_OK;
Expand Down Expand Up @@ -1285,5 +1287,25 @@ public IntPtr MarkupHandle
return _loadedDllHandle;
}
}

/// <summary>
/// Get the control by commandId
/// </summary>
/// <param name="commandId"></param>
/// <returns>IRibbonControl</returns>
/// <exception cref="ArgumentException"></exception>
public IRibbonControl GetRibbonControlById(uint commandId)
{
bool result = _mapRibbonControls.TryGetValue(commandId, out BaseRibbonControl item);
if (result)
return item;
throw new ArgumentException("Not found", nameof(commandId));
}

internal bool TryGetRibbonControlById(uint commandId, out BaseRibbonControl item)
{
bool result = _mapRibbonControls.TryGetValue(commandId, out item);
return result;
}
}
}
3 changes: 1 addition & 2 deletions Ribbon/RibbonUIApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ public HRESULT OnViewChanged(uint viewId, ViewType typeID, object view, ViewVerb
/// <returns>Returns S_OK if successful, or an error value otherwise.</returns>
public HRESULT OnCreateUICommand(uint commandId, CommandType typeID, out IUICommandHandler commandHandler)
{
if (_ribbonControl.MapRibbonControls.ContainsKey(commandId))
if (_ribbonControl.TryGetRibbonControlById(commandId, out BaseRibbonControl control))
{
BaseRibbonControl control = _ribbonControl.MapRibbonControls[commandId] as BaseRibbonControl;
if (control != null)
control.CommandType = typeID;
}
Expand Down
6 changes: 3 additions & 3 deletions Ribbon/UICollection'1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public bool MoveNext()
if (gSet != null && gSet.RibbonCtrl == null)
{
uint cmdId = gSet.CommandID;
if (!_ribbon.MapRibbonControls.TryGetValue(cmdId, out IRibbonControl ctrl))
if (!_ribbon.TryGetRibbonControlById(cmdId, out BaseRibbonControl ctrl))
ctrl = null;
gSet.RibbonCtrl = ctrl;
}
Expand All @@ -433,7 +433,7 @@ internal T FromPropertySet(IUISimplePropertySet uiItem)

hr = uiItem.GetValue(ref RibbonProperties.CommandID, out variant);
uint cmdId = PropVariant.UnsafeNativeMethods.PropVariantToUInt32WithDefault(ref variant, 0);
if (!_ribbon.MapRibbonControls.TryGetValue(cmdId, out IRibbonControl ctrl))
if (!_ribbon.TryGetRibbonControlById(cmdId, out BaseRibbonControl ctrl))
ctrl = null;
QatCommandPropertySet result = new QatCommandPropertySet()
{
Expand All @@ -452,7 +452,7 @@ internal T FromPropertySet(IUISimplePropertySet uiItem)
PropVariant.UnsafeNativeMethods.PropVariantClear(ref variant);
hr = uiItem.GetValue(ref RibbonProperties.CommandType, out variant);
CommandType cType = (CommandType)PropVariant.UnsafeNativeMethods.PropVariantToUInt32WithDefault(ref variant, 0);
if (!_ribbon.MapRibbonControls.TryGetValue(cmdId, out IRibbonControl ctrl))
if (!_ribbon.TryGetRibbonControlById(cmdId, out BaseRibbonControl ctrl))
ctrl = null;
GalleryCommandPropertySet result = new GalleryCommandPropertySet()
{
Expand Down

0 comments on commit 0a189b3

Please sign in to comment.