Skip to content

Commit

Permalink
Add support for Addins to AddMenuItem() before or after an existing m…
Browse files Browse the repository at this point in the history
…enu item.
  • Loading branch information
RickStrahl committed Aug 24, 2017
1 parent 67c27af commit 6d3d254
Showing 1 changed file with 92 additions and 15 deletions.
107 changes: 92 additions & 15 deletions MarkdownMonster/_Classes/AddInInterfaces/MarkdownMonsterAddin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Media;
using Westwind.Utilities;
Expand Down Expand Up @@ -282,7 +284,7 @@ public virtual void OnUninstall()
/// a number of methods for getting access to the editor document
/// </summary>
/// <returns></returns>
protected MarkdownDocumentEditor GetMarkdownEditor()
public MarkdownDocumentEditor GetMarkdownEditor()
{
return Model.Window.GetActiveMarkdownEditor();
}
Expand All @@ -296,7 +298,7 @@ protected MarkdownDocumentEditor GetMarkdownEditor()
/// the edtor using the GetSelectedText() and SetSelectedText().
/// </summary>
/// <returns></returns>
protected internal MarkdownDocument GetMarkdownDocument()
public MarkdownDocument GetMarkdownDocument()
{
var editor = Model.Window.GetActiveMarkdownEditor();
return editor?.MarkdownDocument;
Expand All @@ -306,7 +308,7 @@ protected internal MarkdownDocument GetMarkdownDocument()
/// Returns the active live markdown text from the editor
/// </summary>
/// <returns></returns>
protected string GetMarkdown()
public string GetMarkdown()
{
var editor = Model.Window.GetActiveMarkdownEditor();
return editor?.GetMarkdown();
Expand All @@ -317,7 +319,7 @@ protected string GetMarkdown()
/// Sets all the text in the markdown editor
/// </summary>
/// <param name="markdownText"></param>
protected void SetMarkdown(string markdownText)
public void SetMarkdown(string markdownText)
{
var editor = Model.Window.GetActiveMarkdownEditor();
editor?.SetMarkdown(markdownText);
Expand All @@ -328,7 +330,7 @@ protected void SetMarkdown(string markdownText)
/// Gets the active selection from the editor
/// </summary>
/// <returns></returns>
protected string GetSelection()
public string GetSelection()
{
return Model.ActiveEditor?.AceEditor.getselection(false) ?? string.Empty;
}
Expand All @@ -337,7 +339,7 @@ protected string GetSelection()
/// Sets the active selection from the editor
/// </summary>
/// <param name="text"></param>
protected void SetSelection(string text)
public void SetSelection(string text)
{
var editor = Model.Window.GetActiveMarkdownEditor();
if (editor == null)
Expand All @@ -357,7 +359,7 @@ protected void SetSelection(string text)
/// <summary>
/// Brings the editor to focus
/// </summary>
protected void SetEditorFocus()
public void SetEditorFocus()
{
Model.Window.Activate();
Model.ActiveEditor?.SetEditorFocus();
Expand All @@ -368,7 +370,7 @@ protected void SetEditorFocus()
/// Refreshes the Html Preview Window if active
/// </summary>
/// <param name="keepScrollPosition"></param>
protected void RefreshPreview(bool keepScrollPosition=true)
public void RefreshPreview(bool keepScrollPosition=true)
{
Model.Window.PreviewMarkdownAsync(keepScrollPosition: keepScrollPosition);
}
Expand All @@ -378,7 +380,7 @@ protected void RefreshPreview(bool keepScrollPosition=true)
/// against the editor.
/// </summary>
/// <param name="action">Name of the Editor action to perform</param>
protected void ExecuteEditCommand(string action)
public void ExecuteEditCommand(string action)
{
var editor = Model.Window.GetActiveMarkdownEditor();
editor?.ProcessEditorUpdateCommand(action);
Expand All @@ -390,7 +392,7 @@ protected void ExecuteEditCommand(string action)
/// </summary>
/// <param name="filename">File to open</param>
/// <returns>The TabItem instance representing the opened tab</returns>
protected TabItem OpenTab(string filename)
public TabItem OpenTab(string filename)
{
return Model.Window.OpenTab(filename);
}
Expand All @@ -401,7 +403,7 @@ protected TabItem OpenTab(string filename)
/// the tab collection via Model.Window.TabControl.
/// </summary>
/// <param name="tab"></param>
protected void CloseTab(TabItem tab)
public void CloseTab(TabItem tab)
{
Model.Window.CloseTab(tab);
}
Expand All @@ -411,7 +413,7 @@ protected void CloseTab(TabItem tab)
/// filename
/// </summary>
/// <param name="filename"></param>
protected void CloseTab(string filename)
public void CloseTab(string filename)
{
Model.Window.CloseTab(filename);
}
Expand All @@ -422,7 +424,7 @@ protected void CloseTab(string filename)
/// </summary>
/// <param name="message">Message to display</param>
/// <param name="timeoutMs">optional timeout in milliseconds</param>
protected void ShowStatus(string message, int timeoutMs = 0)
public void ShowStatus(string message, int timeoutMs = 0)
{
Model.Window.ShowStatus(message, timeoutMs);
}
Expand All @@ -434,14 +436,88 @@ protected void ShowStatus(string message, int timeoutMs = 0)
/// <param name="icon"></param>
/// <param name="color"></param>
/// <param name="spin"></param>
protected void SetStatusIcon(FontAwesome.WPF.FontAwesomeIcon icon, Color color,bool spin = false)
public void SetStatusIcon(FontAwesome.WPF.FontAwesomeIcon icon, Color color,bool spin = false)
{
Model.Window.SetStatusIcon(icon, color,spin);


}
#endregion

#region UI Shell Operations

/// <summary>
/// Allows insertion of a menu item
/// </summary>
/// <param name="mitem">The menu item to insert</param>
/// <param name="menuItemName">Name of the menuitem element (optional - use either menuItemName or menuItemText)</param>
/// <param name="menuItemText">Text of the menuitem element (optional - use either menuItemName or menuItemText)</param>
/// <param name="mode">0 - insert after, 1 - insert before, 2 - replace</param>
public bool AddMenuItem(MenuItem mitem, string menuItemName = null, string menuItemText = null, int mode = 0)
{
// find the menu item to in
var menuItem = GetChildMenuItem(Model.Window.MainMenu, menuItemName, menuItemText);
if (menuItem == null)
return false;

ItemsControl parent = menuItem.Parent as ItemsControl;
if (parent == null)
return false;

int idx;
if (mode == 0)
{
idx = parent.Items.IndexOf(menuItem);
idx++;
}
else
{
idx = parent.Items.IndexOf(menuItem);
}

parent.Items.Insert(idx, mitem);

return true;
}

/// <summary>
/// Use this to find a menu item either by control name or by
/// caption text.
///
/// Pass either menuItemName OR menuItemText parameter. If both are
/// passed menuItemName takes precendence.
/// </summary>
/// <param name="mitem"></param>
/// <param name="menuItemName"></param>
/// <param name="menuItemText"></param>
/// <returns></returns>
public MenuItem GetChildMenuItem(ItemsControl mitem, string menuItemName = null, string menuItemText = null)
{
foreach (var control in mitem.Items)
{
var menuItem = control as MenuItem;
if (menuItem == null)
continue;


if (!string.IsNullOrEmpty(menuItemName) && menuItemName == menuItem.Name)
return menuItem;

if (!string.IsNullOrEmpty(menuItemText) && menuItemName == menuItem.Header?.ToString())
return menuItem;

if (menuItem.Items != null)
{
menuItem = GetChildMenuItem(menuItem, menuItemName, menuItemText);
if (menuItem != null)
return menuItem;
}
}

return null;
}
#endregion

/// <summary>
/// Customized to display the Addin Id or Name
/// </summary>
Expand All @@ -451,6 +527,7 @@ public override string ToString()
return Id ?? Name ?? "no name";
}




}
}

0 comments on commit 6d3d254

Please sign in to comment.