///////////////////////////////////////////////////////////////////////////////////////////////////// // NON-PUBLIC PROCEDURES ///////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Dynamically adds a menu item to a context menu. /// </summary> /// <param name="owner">The owner of the context menu.</param> /// <param name="menu">The menu to update.</param> internal static void AddCustomMenuItem(object owner, RibbonControls.Menu menu) { string customButtonLabel = "Programmatically-Added Menu Item"; // Determine if there is a Custom menu item already in the menu bool hasCustomItem = false; foreach (object childObj in menu.Items) { if ((childObj is RibbonControls.Button) && (((RibbonControls.Button)childObj).Label == customButtonLabel)) { hasCustomItem = true; break; } } // If the custom item hasn't been added to this context menu... if (!hasCustomItem) { // Add a separator and a custom button... normally the button would have a command assigned too menu.Items.Add(new RibbonControls.Separator()); RibbonControls.Button newButton = new RibbonControls.Button(); newButton.Label = customButtonLabel; newButton.Click += new EventHandler <RibbonControls.ExecuteRoutedEventArgs>(OnCustomMenuItemClicked); newButton.Tag = new WeakReference(owner); menu.Items.Add(newButton); } }
///////////////////////////////////////////////////////////////////////////////////////////////////// // PUBLIC PROCEDURES ///////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Occurs before a popup is opened. /// </summary> /// <param name="sender">The sender of the event.</param> /// <param name="e">A <see cref="RoutedEventArgs"/> that contains the event data.</param> private void OnPopupButtonPopupOpening(object sender, RoutedEventArgs e) { // Insert random content into the popup RibbonControls.PopupButton popupOwner = (RibbonControls.PopupButton)sender; if (new Random().NextDouble() < 0.65) { // Create a menu RibbonControls.Menu menu = new RibbonControls.Menu(); for (int index = 0; index < 3; index++) { RibbonControls.Button button = new RibbonControls.Button(); button.Label = String.Format("Dynamically created menu item #{0}, created at {1}", index + 1, DateTime.Now); menu.Items.Add(button); } popupOwner.PopupContent = menu; } else { // Create alternate content, the Actipro logo StackPanel panel = new StackPanel(); panel.Children.Add(new TextBlock(new Run("Anything can be placed in a popup"))); panel.Children.Add(new ActiproSoftware.Windows.Controls.ActiproLogo()); popupOwner.PopupContent = panel; } }
/// <summary> /// Updates the spell check context menu items. /// </summary> private void UpdateSpellCheckContextMenuItems() { // Process items for spell-checking if SpellCheck.IsEnabled = true if ((this.ContextMenu != null) && (this.ContextMenu.ItemsSource == null)) { RibbonControls.Menu menu = null; if (this.ContextMenu.Items.Count > 0) { // Get an existing menu menu = this.ContextMenu.Items[0] as RibbonControls.Menu; if ((menu != null) && (!"SpellingErrors".Equals(menu.Tag))) { menu = null; } } if (menu != null) { // Clear the items menu.Items.Clear(); } else { // Create a new menu menu = new RibbonControls.Menu(); menu.Tag = "SpellingErrors"; this.ContextMenu.Items.Insert(0, menu); } // If spell check is enabled... if (this.SpellCheck.IsEnabled) { // Get the spelling error at the caret SpellingError error = this.GetSpellingError(this.CaretPosition); if (error != null) { // Add suggestion items foreach (string suggestion in error.Suggestions) { RibbonControls.Button button = new RibbonControls.Button(); button.Command = EditingCommands.CorrectSpellingError; button.CommandParameter = suggestion; button.Label = suggestion; menu.Items.Add(button); } // Add separator if (menu.Items.Count > 0) { menu.Items.Add(new RibbonControls.Separator()); } } } // Update visibility menu.Visibility = (this.SpellCheck.IsEnabled && (menu.Items.Count > 0) ? Visibility.Visible : Visibility.Collapsed); } }
/// <summary> /// Processes the <see cref="PopupOpeningEvent"/> event. /// </summary> /// <param name="sender">The sender of the event.</param> /// <param name="e">A <c>CancelRoutedEventArgs</c> that contains the event data.</param> private static void OnPopupOpeningEvent(object sender, CancelRoutedEventArgs e) { RibbonControls.PopupButton popupButton = e.OriginalSource as RibbonControls.PopupButton; if (popupButton is RibbonControls.Primitives.QuickAccessToolBarCustomizeButton) { RibbonControls.Menu menu = popupButton.PopupContent as RibbonControls.Menu; if (menu != null) { MainControl.AddCustomMenuItem(popupButton, menu); } } }
/// <summary> /// Initializes an instance of the <c>RichTextBoxExtended</c> class. /// </summary> public RichTextBoxExtended() { // Set appearance this.Background = Brushes.White; this.BorderBrush = Brushes.Black; this.BorderThickness = new Thickness(1); this.Foreground = Brushes.Black; this.Document.Background = Brushes.Transparent; this.Document.Foreground = this.Foreground; ThemeProperties.SetUseBackgroundStates(this, false); ThemeProperties.SetUseBorderStates(this, false); // Assign a custom context menu if (!BrowserInteropHelper.IsBrowserHosted) { RibbonControls.ContextMenu contextMenu = new RibbonControls.ContextMenu(); RibbonControls.Menu menu = new RibbonControls.Menu(); contextMenu.Items.Add(menu); menu.Items.Add(new RibbonControls.Button(System.Windows.Input.ApplicationCommands.Undo) { KeyTipAccessText = "U" }); menu.Items.Add(new RibbonControls.Button(System.Windows.Input.ApplicationCommands.Redo) { KeyTipAccessText = "R" }); menu.Items.Add(new RibbonControls.Separator()); menu.Items.Add(new RibbonControls.Button(System.Windows.Input.ApplicationCommands.Cut) { KeyTipAccessText = "T" }); menu.Items.Add(new RibbonControls.Button(System.Windows.Input.ApplicationCommands.Copy) { KeyTipAccessText = "C" }); menu.Items.Add(new RibbonControls.Button(System.Windows.Input.ApplicationCommands.Paste) { KeyTipAccessText = "P" }); this.ContextMenu = contextMenu; // Attach a mini-toolbar to the context menu contextMenu.MiniToolBar = new ActiproSoftware.ProductSamples.RibbonSamples.Common.RichTextBoxMiniToolBar(); // Attach to the context menu opening event this.ContextMenuOpening += OnContextMenuOpening; } // Add command bindings this.CommandBindings.Add(new CommandBinding(EditingCommands.AlignCenter, null, OnAlignCenterCanExecute)); this.CommandBindings.Add(new CommandBinding(EditingCommands.AlignJustify, null, OnAlignJustifyCanExecute)); this.CommandBindings.Add(new CommandBinding(EditingCommands.AlignLeft, null, OnAlignLeftCanExecute)); this.CommandBindings.Add(new CommandBinding(EditingCommands.AlignRight, null, OnAlignRightCanExecute)); this.CommandBindings.Add(new CommandBinding(ApplicationCommands.ApplyBackground, OnApplyBackgroundExecute, OnApplyBackgroundCanExecute)); this.CommandBindings.Add(new CommandBinding(ApplicationCommands.ApplyDefaultBackground, OnApplyDefaultBackgroundExecute)); this.CommandBindings.Add(new CommandBinding(ApplicationCommands.ApplyDefaultForeground, OnApplyDefaultForegroundExecute)); this.CommandBindings.Add(new CommandBinding(ApplicationCommands.ApplyForeground, OnApplyForegroundExecute, OnApplyForegroundCanExecute)); this.CommandBindings.Add(new CommandBinding(ApplicationCommands.ClearFormatting, OnClearFormattingExecute)); this.CommandBindings.Add(new CommandBinding(ApplicationCommands.FontFamily, OnFontFamilyExecute, OnFontFamilyCanExecute)); this.CommandBindings.Add(new CommandBinding(ApplicationCommands.FontSize, OnFontSizeExecute, OnFontSizeCanExecute)); this.CommandBindings.Add(new CommandBinding(EditingCommands.ToggleBold, null, OnToggleBoldCanExecute)); this.CommandBindings.Add(new CommandBinding(EditingCommands.ToggleItalic, null, OnToggleItalicCanExecute)); this.CommandBindings.Add(new CommandBinding(ApplicationCommands.ToggleStrikethrough, OnToggleStrikethroughExecute, OnToggleStrikethroughCanExecute)); this.CommandBindings.Add(new CommandBinding(EditingCommands.ToggleSubscript, null, OnToggleSubscriptCanExecute)); this.CommandBindings.Add(new CommandBinding(EditingCommands.ToggleSuperscript, null, OnToggleSuperscriptCanExecute)); this.CommandBindings.Add(new CommandBinding(EditingCommands.ToggleUnderline, null, OnToggleUnderlineCanExecute)); }