Beispiel #1
0
        /// <summary>
        /// Add an action (on context menu) on the series grid.
        /// </summary>
        /// <param name="menuItemText">The text of the menu item</param>
        /// <param name="onClick">The event handler to call when menu is selected</param>
        public void AddContextOption(string menuItemText, System.EventHandler onClick, bool active)
        {
            CheckMenuItem item = null;

            foreach (Widget w in Popup)
            {
                CheckMenuItem oldItem = w as CheckMenuItem;
                if (oldItem != null)
                {
                    AccelLabel itemText = oldItem.Child as AccelLabel;
                    if (itemText.Text == menuItemText)
                    {
                        item = oldItem;
                        // Deactivate the "Activate" handler
                        PropertyInfo pi = item.GetType().GetProperty("AfterSignals", BindingFlags.NonPublic | BindingFlags.Instance);
                        if (pi != null)
                        {
                            System.Collections.Hashtable handlers = (System.Collections.Hashtable)pi.GetValue(w);
                            if (handlers != null && handlers.ContainsKey("activate"))
                            {
                                EventHandler handler = (EventHandler)handlers["activate"];
                                item.Activated -= handler;
                            }
                        }
                        break;
                    }
                }
            }
            if (item == null)
            {
                item             = new CheckMenuItem(menuItemText);
                item.DrawAsRadio = false;
                Popup.Append(item);
                Popup.ShowAll();
            }
            // Be sure to set the Active property before attaching the Activated event, since
            // the event handler will call this function again when Active is changed.
            // This can lead to infinite recursion. This is also why we deactivate the handler
            // (done above) when the item is already found in the menu
            item.Active     = active;
            item.Activated += onClick;
        }