// Settings paths taken from the example at: // http://msdn2.microsoft.com/en-us/library/system.componentmodel.design.idesigneroptionservice.aspx // public override void Initialize(IComponent component) { base.Initialize(component); this.Control.AllowDrop = true; // Initialize the default values of the Design-Time properties. // _defaultDrawGrid = true; _defaultSnapToGrid = true; _defaultGridSize = new Size(8, 8); // If the parent Control of the designed one has a ParentDesigner then inherit the values // from it's designer. // if (this.Control.Parent != null) { ParentControlDesigner parentDesigner = GetParentControlDesignerOf(Control.Parent); if (parentDesigner != null) { _defaultDrawGrid = (bool)GetValue(parentDesigner.Component, "DrawGrid"); _defaultSnapToGrid = (bool)GetValue(parentDesigner.Component, "SnapToGrid"); _defaultGridSize = (Size)GetValue(parentDesigner.Component, "GridSize"); } } else { // Else retrieve them through the IDesignerOptionService (if available) // IDesignerOptionService options = GetService(typeof(IDesignerOptionService)) as IDesignerOptionService; if (options != null) { object value = null; value = options.GetOptionValue(@"WindowsFormsDesigner\General", "DrawGrid"); if (value is bool) { _defaultDrawGrid = (bool)value; } value = options.GetOptionValue(@"WindowsFormsDesigner\General", "SnapToGrid"); if (value is bool) { _defaultSnapToGrid = (bool)value; } value = options.GetOptionValue(@"WindowsFormsDesigner\General", "GridSize"); if (value is Size) { _defaultGridSize = (Size)value; } } } // At the end set whatever we've managed to get // _drawGrid = _defaultDrawGrid; _snapToGrid = _defaultSnapToGrid; _gridSize = _defaultGridSize; }
// ToolPicked is called when the user double-clicks on a toolbox item. // The document designer should create a component for the specified tool. // Only tools that are enabled in the toolbox will be passed to this method. // // I create the component in the parent container of the primary selection. // If not available I create it in the rootcomponent (this essentially :-) ) // protected virtual void ToolPicked(ToolboxItem tool) { ISelectionService selectionSvc = GetService(typeof(ISelectionService)) as ISelectionService; IDesignerHost host = GetService(typeof(IDesignerHost)) as IDesignerHost; if (selectionSvc != null && host != null) { IDesigner designer = host.GetDesigner((IComponent)selectionSvc.PrimarySelection); if (designer is ParentControlDesigner) { ParentControlDesigner.InvokeCreateTool((ParentControlDesigner)designer, tool); } else { this.CreateTool(tool); } } else { this.CreateTool(tool); } IToolboxService tbServ = this.GetService(typeof(IToolboxService)) as IToolboxService; tbServ.SelectedToolboxItemUsed(); }
// This is the code that is executed when you drop a tool from the Toolbox in the designer. // protected static void InvokeCreateTool(ParentControlDesigner toInvoke, ToolboxItem tool) { if (toInvoke != null) { toInvoke.CreateTool(tool); } }
// Called by the parent ParentControlDesigner when it is populating the grid-related // design-time properties changes // private void OnParentGridPropertiesChanged(ParentControlDesigner parentDesigner) { SetValue(this.Component, "DrawGrid", (bool)GetValue(parentDesigner.Component, "DrawGrid")); SetValue(this.Component, "SnapToGrid", (bool)GetValue(parentDesigner.Component, "SnapToGrid")); SetValue(this.Component, "GridSize", (Size)GetValue(parentDesigner.Component, "GridSize")); // Set also the default values to be those, because we should // match the parent ParentControlDesigner values. // called recursivly, so I will rather go for slower, but no stack-overflowable code // _defaultDrawGrid = (bool)GetValue(parentDesigner.Component, "DrawGrid"); _defaultSnapToGrid = (bool)GetValue(parentDesigner.Component, "SnapToGrid"); _defaultGridSize = (Size)GetValue(parentDesigner.Component, "GridSize"); this.PopulateGridProperties(); }
// Retrieves the ParentControlDesigner of the specified control if available, // else returns null. // private ParentControlDesigner GetParentControlDesignerOf(Control control) { if (control != null) { IDesignerHost designerHost = GetService(typeof(IDesignerHost)) as IDesignerHost; if (designerHost != null) { ParentControlDesigner designer = null; designer = designerHost.GetDesigner(this.Control.Parent) as ParentControlDesigner; if (designer != null) { return(designer); } } } return(null); }
// Informs all children controls' ParentControlDesigners that the grid properties // have changed and passes them // private void PopulateGridProperties() { // Control.Invalidate (true) will redraw the control and it's children // this will cause a WM_PAINT message to be send and the ControlDesigenr will raise // the OnPaintAdornments, where the grid drawing takes place. // // Note that this should be called *after* the grid properties have changed :-) // this.Control.Invalidate(false); if (this.Control != null) { ParentControlDesigner designer = null; foreach (Control control in this.Control.Controls) { designer = this.GetParentControlDesignerOf(control); if (designer != null) { designer.OnParentGridPropertiesChanged(this); } } } }
// Reminder: We set control.Parent so that it gets serialized for Undo/Redo // private void Paste(object sender, EventArgs args) { IDesignerSerializationService stateSerializer = GetService(typeof(IDesignerSerializationService)) as IDesignerSerializationService; ISelectionService selection = GetService(typeof(ISelectionService)) as ISelectionService; IDesignerHost host = GetService(typeof(IDesignerHost)) as IDesignerHost; IComponentChangeService changeService = GetService(typeof(IComponentChangeService)) as IComponentChangeService; if (host == null || stateSerializer == null) { return; } // // TODO: MWF X11 doesn't seem to support custom clipboard formats - bug #357642 // // IDataObject dataObject = Clipboard.GetDataObject (); // byte[] data = dataObject == null ? null : dataObject.GetData (DT_DATA_FORMAT) as byte[]; // if (data != null) { // MemoryStream stream = new MemoryStream (data); // stateSerializer.Deserialize (new BinaryFormatter().Deserialize (stream)); // ..... // } // if (_clipboard == null) { return; } DesignerTransaction transaction = host.CreateTransaction("Paste"); ICollection components = stateSerializer.Deserialize(_clipboard); // Console.WriteLine ("Pasted components: "); // foreach (object c in components) // Console.WriteLine (((IComponent)c).Site.Name); foreach (object component in components) { Control control = component as Control; if (control == null) { continue; // pure Components are added to the ComponentTray by the DocumentDesigner } PropertyDescriptor parentProperty = TypeDescriptor.GetProperties(control)["Parent"]; if (control.Parent != null) { // Already parented during deserialization? // In that case explicitly raise component changing/ed for the Parent property, // so it get's cought by the UndoEngine if (changeService != null) { changeService.OnComponentChanging(control, parentProperty); changeService.OnComponentChanged(control, parentProperty, null, control.Parent); } } else { ParentControlDesigner parentDesigner = null; if (selection != null && selection.PrimarySelection != null) { parentDesigner = host.GetDesigner((IComponent)selection.PrimarySelection) as ParentControlDesigner; } if (parentDesigner == null) { parentDesigner = host.GetDesigner(host.RootComponent) as DocumentDesigner; } if (parentDesigner != null && parentDesigner.CanParent(control)) { parentProperty.SetValue(control, parentDesigner.Control); } } } _clipboard = null; transaction.Commit(); ((IDisposable)transaction).Dispose(); }