} // OnPaint public void InvokeToolboxItem(System.Drawing.Design.ToolboxItem tool) { IComponent[] newComponents = tool.CreateComponents(DesignerHost); _displayString = "Last Component added: " + newComponents[0].GetType().ToString(); UpdateTable(newComponents[0].GetType()); Invalidate(); }
private void handleToolboxNode(ItemToolboxNode node) { ToolboxItemToolboxNode tiNode = node as ToolboxItemToolboxNode; if (tiNode != null) { //load the type into this process and get the ToolboxItem tiNode.Type.Load(); System.Drawing.Design.ToolboxItem ti = tiNode.GetToolboxItem(); //web controls have sample HTML that need to be deserialised, in a ToolboxDataAttribute //TODO: Fix WebControlToolboxItem and (mono classlib's use of it) so we don't have to mess around with type lookups and attributes here if (ti.AssemblyName != null && ti.TypeName != null) { //look up and register the type ITypeResolutionService typeRes = (ITypeResolutionService)designerHost.GetService(typeof(ITypeResolutionService)); typeRes.ReferenceAssembly(ti.AssemblyName); Type controlType = typeRes.GetType(ti.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 WebFormReferenceManager webRef = designerHost.GetService(typeof(WebFormReferenceManager)) as WebFormReferenceManager; 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 designerHost.RootDocument.InsertFragment(aspText); return; } } //No ToolboxDataAttribute? Get the ToolboxItem to create the components itself ti.CreateComponents(designerHost); } }
private static ICollection GetActivitiesFromToolboxItem(IServiceProvider serviceProvider, bool addAssemblyReference, IDesignerHost designerHost, ICollection activities, ToolboxItem toolBoxItem) { // this will make sure that we add the assembly reference to project if (addAssemblyReference && toolBoxItem.AssemblyName != null) { ITypeResolutionService trs = serviceProvider.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService; if (trs != null) trs.ReferenceAssembly(toolBoxItem.AssemblyName); } ActivityToolboxItem ActivityToolboxItem = toolBoxItem as ActivityToolboxItem; if (addAssemblyReference && ActivityToolboxItem != null) activities = ActivityToolboxItem.CreateComponentsWithUI(designerHost); else activities = toolBoxItem.CreateComponents(designerHost); return activities; }
public IComponent[] CreateTool(ToolboxItem tool, Control parent, int x, int y, int width, int height, bool hasLocation, bool hasSize, ToolboxSnapDragDropEventArgs e) { IToolboxService service = (IToolboxService) this.GetService(typeof(IToolboxService)); ISelectionService service2 = (ISelectionService) this.GetService(typeof(ISelectionService)); IDesignerHost host = (IDesignerHost) this.GetService(typeof(IDesignerHost)); IComponent[] c = new IComponent[0]; Cursor current = Cursor.Current; Cursor.Current = Cursors.WaitCursor; DesignerTransaction transaction = null; try { try { if (host != null) { transaction = host.CreateTransaction(System.Design.SR.GetString("DesignerBatchCreateTool", new object[] { tool.ToString() })); } } catch (CheckoutException exception) { if (exception != CheckoutException.Canceled) { throw exception; } return c; } try { try { if ((host != null) && this.CurrentlyLocalizing(host.RootComponent)) { IUIService service3 = (IUIService) this.GetService(typeof(IUIService)); if (service3 != null) { service3.ShowMessage(System.Design.SR.GetString("LocalizingCannotAdd")); } return new IComponent[0]; } Hashtable defaultValues = new Hashtable(); if (parent != null) { defaultValues["Parent"] = parent; } if ((parent != null) && parent.IsMirrored) { x += width; } if (hasLocation) { defaultValues["Location"] = new Point(x, y); } if (hasSize) { defaultValues["Size"] = new Size(width, height); } if (e != null) { defaultValues["ToolboxSnapDragDropEventArgs"] = e; } c = tool.CreateComponents(host, defaultValues); } catch (CheckoutException exception2) { if (exception2 != CheckoutException.Canceled) { throw; } c = new IComponent[0]; } catch (ArgumentException exception3) { IUIService service4 = (IUIService) this.GetService(typeof(IUIService)); if (service4 != null) { service4.ShowError(exception3); } } catch (Exception exception4) { IUIService service5 = (IUIService) this.GetService(typeof(IUIService)); string message = string.Empty; if (exception4.InnerException != null) { message = exception4.InnerException.ToString(); } if (string.IsNullOrEmpty(message)) { message = exception4.ToString(); } if (exception4 is InvalidOperationException) { message = exception4.Message; } if (service5 == null) { throw; } service5.ShowError(exception4, System.Design.SR.GetString("FailedToCreateComponent", new object[] { tool.DisplayName, message })); } if (c == null) { c = new IComponent[0]; } } finally { if ((service != null) && tool.Equals(service.GetSelectedToolboxItem(host))) { service.SelectedToolboxItemUsed(); } } } finally { if (transaction != null) { transaction.Commit(); } Cursor.Current = current; } if ((service2 != null) && (c.Length > 0)) { if (host != null) { host.Activate(); } ArrayList list = new ArrayList(c); for (int i = 0; i < c.Length; i++) { if (!TypeDescriptor.GetAttributes(c[i]).Contains(DesignTimeVisibleAttribute.Yes)) { list.Remove(c[i]); } } service2.SetSelectedComponents(list.ToArray(), SelectionTypes.Replace); } codemarkers.CodeMarker(0x1d4d); return c; }
private static ICollection GetActivitiesFromToolboxItem(IServiceProvider serviceProvider, bool addAssemblyReference, IDesignerHost designerHost, ICollection activities, ToolboxItem toolBoxItem) { if (addAssemblyReference && (toolBoxItem.AssemblyName != null)) { ITypeResolutionService service = serviceProvider.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService; if (service != null) { service.ReferenceAssembly(toolBoxItem.AssemblyName); } } ActivityToolboxItem item = toolBoxItem as ActivityToolboxItem; if (addAssemblyReference && (item != null)) { activities = item.CreateComponentsWithUI(designerHost); return activities; } activities = toolBoxItem.CreateComponents(designerHost); return activities; }
// 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; }
// 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; }