// parent null denodes parenting to the root component // public Control CreateControl(Type controlType, Control parent) { if (_surface == null) { throw new Exception("Helper designer surface not initialized."); } if (parent == null) { parent = IDesignerHost.RootComponent as Control; } ParentControlDesigner parentDesigner = IDesignerHost.GetDesigner(parent) as ParentControlDesigner; if (parentDesigner == null) { throw new Exception("parent is not handled by a ParentControlDesigner."); } ToolboxItem tbItem = new ToolboxItem(controlType); tbItem.ComponentsCreated += new ToolboxComponentsCreatedEventHandler(OnToolboxComponentCreated); tbItem.CreateComponents(this.IDesignerHost); parent.Controls.Add(_parentedControl); tbItem.ComponentsCreated -= new ToolboxComponentsCreatedEventHandler(OnToolboxComponentCreated); return(_parentedControl); }
// Creates a component from a ToolboxItem, sets its location and size if available and snaps it's // location to the grid. // protected virtual IComponent[] CreateToolCore(ToolboxItem tool, int x, int y, int width, int height, bool hasLocation, bool hasSize) { if (tool == null) { throw new ArgumentNullException("tool"); } IDesignerHost host = GetService(typeof(IDesignerHost)) as IDesignerHost; DesignerTransaction transaction = host.CreateTransaction("Create components in tool '" + tool.DisplayName + "'"); IComponent[] components = tool.CreateComponents(host); foreach (IComponent component in components) { ControlDesigner controlDesigner = host.GetDesigner(component) as ControlDesigner; if (controlDesigner == null) // not a Control, but e.g. a plain Component { continue; } else if (!this.CanParent(controlDesigner)) { host.DestroyComponent(component); continue; } Control control = component as Control; if (control != null) { this.Control.SuspendLayout(); // set parent instead of controls.Add so that it gets serialized for Undo/Redo TypeDescriptor.GetProperties(control)["Parent"].SetValue(control, this.Control); this.Control.SuspendLayout(); if (hasLocation) { base.SetValue(component, "Location", this.SnapPointToGrid(new Point(x, y))); } else { base.SetValue(component, "Location", this.SnapPointToGrid(this.DefaultControlLocation)); } if (hasSize) { base.SetValue(component, "Size", new Size(width, height)); } this.Control.Refresh(); } } ISelectionService selectionServ = this.GetService(typeof(ISelectionService)) as ISelectionService; if (selectionServ != null) { selectionServ.SetSelectedComponents(components, SelectionTypes.Replace); } transaction.Commit(); return(components); }
internal static void AddTab(DetailsTemplatesSurface designSurface) { if (designSurface != null) { IDesignerHost designerHost = designSurface.GetService(typeof(IDesignerHost)) as IDesignerHost; if (designerHost != null) { DesignerTransaction designerTransaction = null; try { designerTransaction = designerHost.CreateTransaction("TabControlAddTabPage" + designSurface.TemplateTab.Site.Name); Hashtable hashtable = new Hashtable(); hashtable["Parent"] = designSurface.TemplateTab; hashtable["TabPageIndex"] = designSurface.TemplateTab.SelectedIndex + 1; ToolboxItem toolboxItem = new ToolboxItem(typeof(CustomTabPage)); toolboxItem.CreateComponents(designerHost, hashtable); } finally { if (designerTransaction != null) { designerTransaction.Commit(); } } } } }
/// <summary> /// Handles the Drag event which raises a DoDragDrop event used by the /// DesignSurface to drag items from the Toolbox to the DesignSurface. /// If the currently selected item is the pointer item then the drag drop /// operation is not started. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnItemDrag(object sender, ItemDragEventArgs e) { if (e.Button == MouseButtons.Left && e.Item.GetType() == typeof(ListViewItem)) { ToolboxItem lItem = (e.Item as ListViewItem).Tag as ToolboxItem; if (lItem.TypeName != NameConsts.Pointer) { mIsDragging = true; IToolboxService lToolboxService = mToolboxService as IToolboxService; object lDataObject = lToolboxService.SerializeToolboxItem(lItem); DrawingTool lTool = lItem.CreateComponents().FirstOrDefault() as DrawingTool; lTool.CreatePersistence(); Rectangle lRect = lTool.SurroundingRect; using (DragImage image = new DragImage(lTool.DefaultImage, lRect.Width / 2, lRect.Height / 2)) { DoDragDrop(lDataObject, DragDropEffects.All); } mIsDragging = false; } } }
private static void EnsureDefaultChildHierarchy(IDesignerHost designerHost) { //When we are adding the root activity we need to make sure that all the child activities which are required by the parent //activity are looked up in the toolboxitem and added appropriately //If the composite activity already has a some child activities but not all then it //means that user has changed the InitializeComponent and hence we do nothing //This is the simple check to get the designer working in case of selecting composite //root activities CompositeActivity rootActivity = designerHost.RootComponent as CompositeActivity; if (rootActivity != null && rootActivity.Activities.Count == 0) { object[] attribs = rootActivity.GetType().GetCustomAttributes(typeof(ToolboxItemAttribute), false); ToolboxItemAttribute toolboxItemAttrib = (attribs != null && attribs.GetLength(0) > 0) ? attribs[0] as ToolboxItemAttribute : null; if (toolboxItemAttrib != null && toolboxItemAttrib.ToolboxItemType != null) { ToolboxItem item = Activator.CreateInstance(toolboxItemAttrib.ToolboxItemType, new object[] { rootActivity.GetType() }) as ToolboxItem; IComponent[] components = item.CreateComponents(); //I am assuming here that there will be always one top level component created. //If there are multiple then there is a bigger problem as we dont know how //to use those CompositeActivity compositeActivity = null; foreach (IComponent component in components) { if (component.GetType() == rootActivity.GetType()) { compositeActivity = component as CompositeActivity; break; } } //Add the children if (compositeActivity != null && compositeActivity.Activities.Count > 0) { IIdentifierCreationService identifierCreationService = designerHost.GetService(typeof(IIdentifierCreationService)) as IIdentifierCreationService; if (identifierCreationService != null) { //We do not go thru the composite designer here as composite activity //might have simple designer Activity[] activities = compositeActivity.Activities.ToArray(); compositeActivity.Activities.Clear(); identifierCreationService.EnsureUniqueIdentifiers(rootActivity, activities); // Work around : Don't called AddRange because it doesn't send the ListChange notifications // to the activity collection. Use multiple Add calls instaead foreach (Activity newActivity in activities) { rootActivity.Activities.Add(newActivity); } foreach (Activity childActivity in activities) { WorkflowDesignerLoader.AddActivityToDesigner(designerHost, childActivity); } } } } } }
} // OnPaint public void InvokeToolboxItem(ToolboxItem tool) { var newComponents = tool.CreateComponents(DesignerHost); _displayString = "Last Component added: " + newComponents[0].GetType().ToString(); UpdateTable(newComponents[0].GetType()); Invalidate(); }
//- Management of the drag and drop of the toolboxItems public void OnDragDrop(object sender, DragEventArgs e) { //- if the user don't drag a ToolboxItem //- then do nothing if (!e.Data.GetDataPresent(typeof(ToolboxItem))) { e.Effect = DragDropEffects.None; return; } //- now retrieve the data node ToolboxItem item = e.Data.GetData(typeof(ToolboxItem)) as ToolboxItem; e.Effect = DragDropEffects.Copy; item.CreateComponents(this.GetIDesignerHost()); }
//Runs when item is clicked. //We should be able to avoid loading actual ToolboxItem instances and their //associated assemblies until they are activated. public override void Activate(object host) { IDesignerHost desHost = host as IDesignerHost; if (desHost == null) { throw new Exception("This ToolboxItem should not have been shown for this host. System.Drawing.Design.ToolboxItem requires a host of type System.ComponentModel.Design.IDesignerHost"); } //web controls have sample HTML that need to be deserialised //TODO: Fix WebControlToolboxItem so we don't have to mess around with type lookups and attributes here if ((item is System.Web.UI.Design.WebControlToolboxItem) && host is AspNetEdit.Editor.ComponentModel.DesignerHost) { AspNetEdit.Editor.ComponentModel.DesignerHost aspDesHost = (AspNetEdit.Editor.ComponentModel.DesignerHost)desHost; if (item.AssemblyName != null && item.TypeName != null) { //look up and register the type ITypeResolutionService typeRes = (ITypeResolutionService)aspDesHost.GetService(typeof(ITypeResolutionService)); typeRes.ReferenceAssembly(item.AssemblyName); Type controlType = typeRes.GetType(item.TypeName, true); //read the WebControlToolboxItem data from the attribute AttributeCollection atts = TypeDescriptor.GetAttributes(controlType); System.Web.UI.ToolboxDataAttribute tda = (System.Web.UI.ToolboxDataAttribute)atts[typeof(System.Web.UI.ToolboxDataAttribute)]; //if it's present if (tda != null && tda.Data.Length > 0) { //look up the tag's prefix and insert it into the data System.Web.UI.Design.IWebFormReferenceManager webRef = aspDesHost.GetService(typeof(System.Web.UI.Design.IWebFormReferenceManager)) as System.Web.UI.Design.IWebFormReferenceManager; if (webRef == null) { throw new Exception("Host does not provide an IWebFormReferenceManager"); } string aspText = String.Format(tda.Data, webRef.GetTagPrefix(controlType)); System.Diagnostics.Trace.WriteLine("Toolbox processing ASP.NET item data: " + aspText); //and add it to the document aspDesHost.RootDocument.DeserializeAndAdd(aspText); return; } } } //No ToolboxDataAttribute? Get the ToolboxItem to create the components itself item.CreateComponents(desHost); }
// Creates a component from a ToolboxItem, sets its location and size if available and snaps it's // location to the grid. // protected virtual IComponent[] CreateToolCore(ToolboxItem tool, int x, int y, int width, int height, bool hasLocation, bool hasSize) { if (tool == null) { throw new ArgumentNullException("tool"); } IDesignerHost host = GetService(typeof(IDesignerHost)) as IDesignerHost; IComponent[] components = tool.CreateComponents(host); foreach (IComponent component in components) { Control control = component as Control; if (control != null) { if (hasLocation) { base.SetValue(component, "Location", this.SnapPointToGrid(new Point(x, y))); } else { base.SetValue(component, "Location", this.SnapPointToGrid(this.DefaultControlLocation)); } if (hasSize) { base.SetValue(component, "Size", new Size(width, height)); } this.Control.SuspendLayout(); this.Control.Controls.Add(control); this.Control.SuspendLayout(); this.Control.Refresh(); } } ISelectionService selectionServ = this.GetService(typeof(ISelectionService)) as ISelectionService; if (selectionServ != null) { selectionServ.SetSelectedComponents(components, SelectionTypes.Replace); } return(components); }
protected override void OnDragDrop(DragEventArgs pArgs) { base.OnDragDrop(pArgs); // drag DrawingTool & drop. if (DraggingPoint != Point.Empty) { string data = pArgs.Data.GetData(typeof(string)) as string; data.Split(',').All(e => { int lIndex = Convert.ToInt32(e); DrawingTool lDrawingTool = DrawingTools[lIndex]; Point lPoint = GetScrollablePoint(PointToClient(new Point(pArgs.X, pArgs.Y))); InvalidateRect(lDrawingTool.Tracker.SurroundingRect); lDrawingTool.DoDrop(new Point(lPoint.X - DraggingPoint.X, lPoint.Y - DraggingPoint.Y)); InvalidateRect(lDrawingTool.Tracker.SurroundingRect); SelectedTool = lDrawingTool; return(true); }); IsDirty = true; } else { // drag ToolboxItem & drop. ToolboxItem lItem = GetService <IToolboxService>().DeserializeToolboxItem(pArgs.Data); DrawingTool lTool = lItem.CreateComponents(DesignerHost).FirstOrDefault() as DrawingTool; if (lTool != null) { lTool.CreatePersistence(); Point lLocation = GetScrollablePoint(PointToClient(new Point(pArgs.X, pArgs.Y))); Rectangle lRect = lTool.SurroundingRect; lLocation.Offset(-lRect.Width / 2, -lRect.Height / 2); lTool.Location = lLocation; AddTool(lTool); SelectedTool = lTool; InvalidateRect(lTool.SurroundingRect); } } }
private void StartDragEvent(string toolName) { if (toolName != null) { ToolboxItem toolboxItem = Toolbox.toolboxItemDictionary[toolName]; if (this.mouseClicks == 1) { DataObject data = this.SerializeToolboxItem(toolboxItem) as DataObject; base.DoDragDrop(data, DragDropEffects.Copy); DetailsTemplatesSurface.SortControls(this.designSurface.TemplateTab.SelectedTab, false); this.mouseClicks = 0; return; } if (this.mouseClicks == 2) { IDesignerHost designerHost = (IDesignerHost)this.designSurface.GetService(typeof(IDesignerHost)); ISelectionService selectionService = (designerHost == null) ? null : (designerHost.GetService(typeof(ISelectionService)) as ISelectionService); if (selectionService != null) { DesignerTransaction designerTransaction = null; try { designerTransaction = designerHost.CreateTransaction(toolboxItem.TypeName + this.designSurface.TemplateTab.Site.Name); Hashtable hashtable = new Hashtable(); hashtable["Parent"] = this.designSurface.TemplateTab.SelectedTab; ICollection collection = toolboxItem.CreateComponents(designerHost, hashtable); if (collection != null && collection.Count > 0) { selectionService.SetSelectedComponents(collection, SelectionTypes.Replace); } } finally { if (designerTransaction != null) { designerTransaction.Commit(); } } DetailsTemplatesSurface.SortControls(this.designSurface.TemplateTab.SelectedTab, false); } this.mouseClicks = 0; } } }
protected override void OnDragDrop(DragEventArgs drgevent) { if (drgevent.Effect == DragDropEffects.Copy) { (designerHost.RootComponent as Control).SuspendLayout(); IToolboxService tbsvc = designerHost.GetService(typeof(IToolboxService)) as IToolboxService; IToolboxUser tbu = designerHost.GetDesigner(designerHost.RootComponent) as IToolboxUser; ISelectionService selsvc = designerHost.GetService(typeof(ISelectionService)) as ISelectionService; DesignerTransaction transaction = designerHost.CreateTransaction("DoDragDrop"); //避免当前有选中控件,会把新创建的控件放到选中控件中 selsvc.SetSelectedComponents(null); ToolboxItem item = drgevent.Data.GetData(typeof(ToolboxItem)) as ToolboxItem; tbu.ToolPicked(item); ICollection components = item.CreateComponents(); tbsvc.SelectedToolboxItemUsed(); Control createdControl = selsvc.PrimarySelection as Control; Point location1 = PointToScreen(this.Location); createdControl.Location = new Point(drgevent.X - location1.X + this.Left, drgevent.Y - location1.Y + this.Top); selsvc.SetSelectedComponents(null); selsvc.SetSelectedComponents(new Control[] { createdControl }); transaction.Commit(); ((IDisposable)transaction).Dispose(); (designerHost.RootComponent as Control).ResumeLayout(); } else { base.OnDragDrop(drgevent); } }
public IComponent[] CreateTool(ToolboxItem tool, Control parent, int x, int y, int width, int height, bool hasLocation, bool hasSize, ToolboxSnapDragDropEventArgs e) { // Services we will need // IToolboxService toolboxSvc = (IToolboxService)GetService(typeof(IToolboxService)); ISelectionService selSvc = (ISelectionService)GetService(typeof(ISelectionService)); IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost)); IComponent[] comps = Array.Empty <IComponent>(); Cursor oldCursor = Cursor.Current; Cursor.Current = Cursors.WaitCursor; DesignerTransaction trans = null; try { try { if (host != null) { trans = host.CreateTransaction(string.Format(SR.DesignerBatchCreateTool, tool.ToString())); } } catch (CheckoutException cxe) { if (cxe == CheckoutException.Canceled) { return(comps); } throw; } try { try { // First check if we are currently in localization mode (i.e., language is non-default). // If so, we should not permit addition of new components. This is an intentional // change from Everett - see VSWhidbey #292249. if (host != null && CurrentlyLocalizing(host.RootComponent)) { IUIService uiService = (IUIService)GetService(typeof(IUIService)); if (uiService != null) { uiService.ShowMessage(SR.LocalizingCannotAdd); } comps = Array.Empty <IComponent>(); return(comps); } // Create a dictionary of default values that the designer can // use to initialize a control with. Hashtable defaultValues = new Hashtable(); if (parent != null) { defaultValues["Parent"] = parent; } // adjust the location if we are in a mirrored parent. That is because the origin // will then be in the upper right rather than upper left. if (parent != null && parent.IsMirrored) { x += width; } if (hasLocation) { defaultValues["Location"] = new Point(x, y); } if (hasSize) { defaultValues["Size"] = new Size(width, height); } //store off extra behavior drag/drop information if (e != null) { defaultValues["ToolboxSnapDragDropEventArgs"] = e; } comps = tool.CreateComponents(host, defaultValues); } catch (CheckoutException checkoutEx) { if (checkoutEx == CheckoutException.Canceled) { comps = Array.Empty <IComponent>(); } else { throw; } } catch (ArgumentException argumentEx) { IUIService uiService = (IUIService)GetService(typeof(IUIService)); if (uiService != null) { uiService.ShowError(argumentEx); } } catch (Exception ex) { IUIService uiService = (IUIService)GetService(typeof(IUIService)); string exceptionMessage = string.Empty; if (ex.InnerException != null) { exceptionMessage = ex.InnerException.ToString(); } if (string.IsNullOrEmpty(exceptionMessage)) { exceptionMessage = ex.ToString(); } if (ex is InvalidOperationException) { exceptionMessage = ex.Message; } if (uiService != null) { uiService.ShowError(ex, string.Format(SR.FailedToCreateComponent, tool.DisplayName, exceptionMessage)); } else { throw; } } if (comps == null) { comps = Array.Empty <IComponent>(); } } finally { if (toolboxSvc != null && tool.Equals(toolboxSvc.GetSelectedToolboxItem(host))) { toolboxSvc.SelectedToolboxItemUsed(); } } } finally { if (trans != null) { trans.Commit(); } Cursor.Current = oldCursor; } // Finally, select the newly created components. // if (selSvc != null && comps.Length > 0) { if (host != null) { host.Activate(); } ArrayList selectComps = new ArrayList(comps); for (int i = 0; i < comps.Length; i++) { if (!TypeDescriptor.GetAttributes(comps[i]).Contains(DesignTimeVisibleAttribute.Yes)) { selectComps.Remove(comps[i]); } } selSvc.SetSelectedComponents(selectComps.ToArray(), SelectionTypes.Replace); } codemarkers.CodeMarker((int)CodeMarkerEvent.perfFXDesignCreateComponentEnd); return(comps); }
public static Activity[] DeserializeActivitiesFromDataObject(IServiceProvider serviceProvider, IDataObject dataObj, bool addReference) { IDesignerHost designerHost = (IDesignerHost)serviceProvider.GetService(typeof(IDesignerHost)); if (designerHost == null) { throw new InvalidOperationException("IDesignerHost is missing."); } if (dataObj == null) { return new Activity[] { } } ; object data = dataObj.GetData(CF_DESIGNER); ICollection activities = null; if (data is Stream) { BinaryFormatter formatter = new BinaryFormatter(); ((Stream)data).Seek(0, SeekOrigin.Begin); object serializationData = formatter.Deserialize((Stream)data); if (serializationData is SerializationStore) { // get component serialization service ComponentSerializationService css = serviceProvider.GetService(typeof(ComponentSerializationService)) as ComponentSerializationService; if (css == null) { throw new Exception("ComponentSerializationService is missing."); } // deserialize data activities = css.Deserialize((SerializationStore)serializationData); } } else { // Now check for a toolbox item. IToolboxService ts = (IToolboxService)serviceProvider.GetService(typeof(IToolboxService)); if (ts != null && ts.IsSupported(dataObj, designerHost)) { ToolboxItem toolBoxItem = ts.DeserializeToolboxItem(dataObj, designerHost); if (toolBoxItem != null) { // this will make sure that we add the assembly reference to project if (addReference && toolBoxItem.AssemblyName != null) { ITypeResolutionService trs = serviceProvider.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService; if (trs != null) { trs.ReferenceAssembly(toolBoxItem.AssemblyName); } } ActivityToolboxItem ActivityToolboxItem = toolBoxItem as ActivityToolboxItem; if (addReference && ActivityToolboxItem != null) { activities = ActivityToolboxItem.CreateComponentsWithUI(designerHost); } else { activities = toolBoxItem.CreateComponents(designerHost); } } } } return((activities != null) ? (Activity[])(new ArrayList(activities).ToArray(typeof(Activity))) : new Activity[] { }); }
protected override void OnMouseMove(MouseEventArgs pArgs) { base.OnMouseMove(pArgs); Point lLocation = GetScrollablePoint(pArgs.Location); switch (pArgs.Button) { case MouseButtons.Right: return; case MouseButtons.None: Cursor lCurrentCursor = Cursors.Hand; if (SelectedToolboxItem == null && PickingTool == null) { DrawingTools.Any(e => { Cursor lCursor = e.Tracker.GetCursor(lLocation); bool lRet = false; if (lCursor != Cursors.Default) { lCurrentCursor = lCursor; lRet = true; } return(lRet); }); } else { lCurrentCursor = Cursors.Cross; } Cursor.Current = lCurrentCursor; break; case MouseButtons.Left: if (FullDragMode) { return; } bool lRunDefault = true; if (SelectedTool != null) { DrawingTracker lTracker = SelectedTool.Tracker; lRunDefault = false; // stretching on tracker. if (lTracker.IsResizing) { InvalidateRect(lTracker.SurroundingRect); lTracker.Resize(lLocation); InvalidateRect(lTracker.SurroundingRect); } else if (DragBoxFromMouseDown != Rectangle.Empty) { // start DragDrop on selected tool object. if (!DragBoxFromMouseDown.Contains(lLocation)) { int dx = GraphicsMapper.Instance.TransformInt(DraggingPoint.X - SelectedTool.SurroundingRect.Left, CoordinateSpace.Device, CoordinateSpace.Page); int dy = GraphicsMapper.Instance.TransformInt(DraggingPoint.Y - SelectedTool.SurroundingRect.Top, CoordinateSpace.Device, CoordinateSpace.Page); using (new DragImage(SelectedTool.GetDraggingImage(LayerWidth, LayerHeight), dx, dy)) { int lIndex = DrawingTools.IndexOf(SelectedTool); DoDragDrop(lIndex.ToString(), DragDropEffects.All); DraggingPoint = Point.Empty; DragBoxFromMouseDown = Rectangle.Empty; } } } else { lRunDefault = true; } } // update current Cursor. if (lRunDefault) { if (PickingTool == null) { ToolboxItem lItem = SelectedToolboxItem; // update cursor by hitTest on each drawing tool. if (lItem != null && DragBoxFromMouseDown != Rectangle.Empty) { // create toolbox item if (!DragBoxFromMouseDown.Contains(lLocation)) { PickingTool = lItem.CreateComponents(DesignerHost).FirstOrDefault() as DrawingTool; PickingTool.CreatePersistence(); PickingTool.StartResize(lLocation); } } } } break; } if (PickingTool != null) { InvalidateRect(PickingTool.SurroundingRect); PickingTool.Resize(lLocation); InvalidateRect(PickingTool.SurroundingRect); } }
internal static IComponent CreateComponent(ToolboxItem currentTool, IDesignerHost host) { return(currentTool.CreateComponents(host)[0]); }