Skip to content

Commit 0a189b3

Browse files
committed
V2.16.0 Step2
1 parent 1d50939 commit 0a189b3

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
77
#### Changed (Ribbon)
88

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

1213
#### Changed (RibbonTools)

Ribbon/Ribbon.cs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using System.Windows.Forms;
2424
using System.ComponentModel;
2525
using MethodInvoker = System.Windows.Forms.MethodInvoker;
26+
using RibbonLib.Controls;
2627

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

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

1175-
if (_mapRibbonControls.ContainsKey(commandID))
1175+
if (TryGetRibbonControlById(commandID, out BaseRibbonControl control))
11761176
{
1177-
return _mapRibbonControls[commandID].Execute(verb, key, currentValue, commandExecutionProperties);
1177+
IRibbonControl item = control as IRibbonControl;
1178+
return item.Execute(verb, key, currentValue, commandExecutionProperties);
11781179
}
11791180

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

1199-
if (_mapRibbonControls.ContainsKey(commandID))
1200+
if (TryGetRibbonControlById(commandID, out BaseRibbonControl control))
12001201
{
1201-
return _mapRibbonControls[commandID].UpdateProperty(ref key, currentValue, ref newValue);
1202+
IRibbonControl item = control as IRibbonControl;
1203+
return item.UpdateProperty(ref key, currentValue, ref newValue);
12021204
}
12031205

12041206
return HRESULT.S_OK;
@@ -1285,5 +1287,25 @@ public IntPtr MarkupHandle
12851287
return _loadedDllHandle;
12861288
}
12871289
}
1290+
1291+
/// <summary>
1292+
/// Get the control by commandId
1293+
/// </summary>
1294+
/// <param name="commandId"></param>
1295+
/// <returns>IRibbonControl</returns>
1296+
/// <exception cref="ArgumentException"></exception>
1297+
public IRibbonControl GetRibbonControlById(uint commandId)
1298+
{
1299+
bool result = _mapRibbonControls.TryGetValue(commandId, out BaseRibbonControl item);
1300+
if (result)
1301+
return item;
1302+
throw new ArgumentException("Not found", nameof(commandId));
1303+
}
1304+
1305+
internal bool TryGetRibbonControlById(uint commandId, out BaseRibbonControl item)
1306+
{
1307+
bool result = _mapRibbonControls.TryGetValue(commandId, out item);
1308+
return result;
1309+
}
12881310
}
12891311
}

Ribbon/RibbonUIApplication.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,8 @@ public HRESULT OnViewChanged(uint viewId, ViewType typeID, object view, ViewVerb
114114
/// <returns>Returns S_OK if successful, or an error value otherwise.</returns>
115115
public HRESULT OnCreateUICommand(uint commandId, CommandType typeID, out IUICommandHandler commandHandler)
116116
{
117-
if (_ribbonControl.MapRibbonControls.ContainsKey(commandId))
117+
if (_ribbonControl.TryGetRibbonControlById(commandId, out BaseRibbonControl control))
118118
{
119-
BaseRibbonControl control = _ribbonControl.MapRibbonControls[commandId] as BaseRibbonControl;
120119
if (control != null)
121120
control.CommandType = typeID;
122121
}

Ribbon/UICollection'1.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ public bool MoveNext()
407407
if (gSet != null && gSet.RibbonCtrl == null)
408408
{
409409
uint cmdId = gSet.CommandID;
410-
if (!_ribbon.MapRibbonControls.TryGetValue(cmdId, out IRibbonControl ctrl))
410+
if (!_ribbon.TryGetRibbonControlById(cmdId, out BaseRibbonControl ctrl))
411411
ctrl = null;
412412
gSet.RibbonCtrl = ctrl;
413413
}
@@ -433,7 +433,7 @@ internal T FromPropertySet(IUISimplePropertySet uiItem)
433433

434434
hr = uiItem.GetValue(ref RibbonProperties.CommandID, out variant);
435435
uint cmdId = PropVariant.UnsafeNativeMethods.PropVariantToUInt32WithDefault(ref variant, 0);
436-
if (!_ribbon.MapRibbonControls.TryGetValue(cmdId, out IRibbonControl ctrl))
436+
if (!_ribbon.TryGetRibbonControlById(cmdId, out BaseRibbonControl ctrl))
437437
ctrl = null;
438438
QatCommandPropertySet result = new QatCommandPropertySet()
439439
{
@@ -452,7 +452,7 @@ internal T FromPropertySet(IUISimplePropertySet uiItem)
452452
PropVariant.UnsafeNativeMethods.PropVariantClear(ref variant);
453453
hr = uiItem.GetValue(ref RibbonProperties.CommandType, out variant);
454454
CommandType cType = (CommandType)PropVariant.UnsafeNativeMethods.PropVariantToUInt32WithDefault(ref variant, 0);
455-
if (!_ribbon.MapRibbonControls.TryGetValue(cmdId, out IRibbonControl ctrl))
455+
if (!_ribbon.TryGetRibbonControlById(cmdId, out BaseRibbonControl ctrl))
456456
ctrl = null;
457457
GalleryCommandPropertySet result = new GalleryCommandPropertySet()
458458
{

0 commit comments

Comments
 (0)