예제 #1
0
        internal void Attach(Action a, Component o, bool designMode)
        {
            Debug.Assert(o != null && a != null);
            component   = o;
            action      = a;
            _designMode = designMode;
            Debug.Assert(action.Parent != null);
            // Text
            textWrapper.Attach(component, "Text");
            Text = action.Text;

            // Enabled
            enabledWrapper.Attach(component, "Enabled");
            Enabled = action.Enabled;

            // Checked
            // special case of a toolbarButton
            if (component is ToolBarButton)
            {
                checkedWrapper.Attach(component, "Pushed");
            }
            else
            {
                checkedWrapper.Attach(component, "Checked");
            }
            Checked = action.Checked;
            // Visible
            visibleWrapper.Attach(component, "Visible");
            Visible = action.Visible;

            // Shortcut
            _shortcut = o.GetType().GetProperty("Shortcut");
            if (_shortcut != null && (!_shortcut.CanRead || !_shortcut.CanWrite) && (_shortcut.PropertyType == typeof(Keys)))
            {
                // we must be able to read and write a shortcut property
                _shortcut = null;
            }
            Shortcut = action.Shortcut;


            _shortcutkeys = o.GetType().GetProperty("ShortcutKeys");
            if (_shortcutkeys != null && (!_shortcutkeys.CanRead || !_shortcutkeys.CanWrite) && (_shortcutkeys.PropertyType == typeof(Keys)))
            {
                // we must be able to read and write a shortcut property
                _shortcutkeys = null;
            }
            ShortcutKeys = action.Shortcut;


            _shortcuts = o.GetType().GetProperty("Shortcuts");
            if (_shortcuts != null && (!_shortcuts.CanRead || !_shortcuts.CanWrite) && (_shortcuts.PropertyType == typeof(ShortcutKeysCollection)))
            {
                // we must be able to read and write a shortcut property
                _shortcuts = null;
            }
            Shortcuts = action.Shortcuts;

            _shortcutkeydisplaystring = o.GetType().GetProperty("ShortcutKeyDisplayString");
            if (_shortcutkeydisplaystring != null && (!_shortcutkeydisplaystring.CanRead || !_shortcutkeydisplaystring.CanWrite) && (_shortcutkeydisplaystring.PropertyType == typeof(string)))
            {
                // we must be able to read and write a shortcut property
                _shortcutkeydisplaystring = null;
            }
            UpdateShortcutKeyDisplayString();


            // ImageList
            // don't handle toolbarButtons here
            if (!(component is ToolBarButton))
            {
                _imageList = o.GetType().GetProperty("ImageList");
                if (_imageList != null && (!_imageList.CanRead || !_imageList.CanWrite) && (_imageList.PropertyType == typeof(ImageList)))
                {
                    // we must be able to read and write an ImageList property
                    _imageList = null;
                }
            }
            ImageList = action.Parent.ImageList;
            // ImageIndex
            _imageIndex = o.GetType().GetProperty("ImageIndex");
            if (_imageIndex != null && (!_imageIndex.CanRead || !_imageIndex.CanWrite) && (_imageIndex.PropertyType == typeof(int)))
            {
                // we must be able to read and write an integer property
                _imageIndex = null;
            }
            ImageIndex = action.ImageIndex;

            //image
            if (_imageIndex == null || o is ToolStripItem)
            {
                _image = o.GetType().GetProperty("Image");
                _imageTransparentColor = o.GetType().GetProperty("ImageTransparentColor");
                if (_image != null &&
                    (!_image.CanRead || !_image.CanWrite) &&
                    (_image.PropertyType == typeof(Image)) &&
                    _imageTransparentColor != null &&
                    (!_imageTransparentColor.CanRead || !_imageTransparentColor.CanWrite) &&
                    (_imageTransparentColor.PropertyType == typeof(Color))
                    )
                {
                    _image = null;
                    _imageTransparentColor = null;
                }
                UpdateImage();
            }

            // Hint
            Hint = action.Hint;
            // click
            if (!designMode)
            {
                // special case of a toolbarButton
                if (component is ToolBarButton)
                {
                    ToolBar tb = ((ToolBarButton)component).Parent;
                    if (tb != null)
                    {
                        tb.ButtonClick += new ToolBarButtonClickEventHandler(OnToolbarClick);
                        _click          = true;
                    }
                }
                else
                {
                    EventInfo e = o.GetType().GetEvent("Click");
                    if (e != null && e.EventHandlerType == typeof(EventHandler))
                    {
                        e.AddEventHandler(component, new EventHandler(action.OnExecute));
                        _click = true;
                    }

                    if (component is System.Windows.Forms.MenuItem)
                    {
                        OwnerDrawMenus = action.Parent.OwnerDrawMenus;
                    }
                }
            }
            // Dispose
            Debug.Assert(action.Parent != null);
            component.Disposed += new EventHandler(action.Parent.OnComponentDisposed);
        }