public void OnDeserialize(object context)
        {
            _item.Name = this.Name;
            SortedDictionary <int, List <IDrawDesignControl> > lst = new SortedDictionary <int, List <IDrawDesignControl> >();

            for (int i = 0; i < this.Controls.Count; i++)
            {
                IDrawDesignControl ddc = this.Controls[i] as IDrawDesignControl;
                if (ddc != null)
                {
                    List <IDrawDesignControl> l;
                    if (!lst.TryGetValue(ddc.ZOrder, out l))
                    {
                        l = new List <IDrawDesignControl>();
                        lst.Add(ddc.ZOrder, l);
                    }
                    l.Add(ddc);
                }
            }
            SortedDictionary <int, List <IDrawDesignControl> > .Enumerator en = lst.GetEnumerator();
            while (en.MoveNext())
            {
                foreach (IDrawDesignControl ddc in en.Current.Value)
                {
                    ddc.BringToFront();
                }
            }
        }
 public virtual void OnRemoveItem(IDrawDesignControl ddc)
 {
     if (_items != null)
     {
         if (_items.Contains(ddc.Item))
         {
             _items.Remove(ddc.Item);
         }
         else
         {
             foreach (DrawingItem di in _items)
             {
                 if (string.CompareOrdinal(di.Name, ddc.Item.Name) == 0)
                 {
                     _items.Remove(di);
                     break;
                 }
                 else if (di.LayerId != Guid.Empty && di.LayerId == ddc.Item.LayerId)
                 {
                     _items.Remove(di);
                     break;
                 }
             }
         }
     }
 }
        public int CompareTo(object obj)
        {
            IDrawDesignControl dc = obj as IDrawDesignControl;

            if (dc != null)
            {
                return(ZOrder.CompareTo(dc.ZOrder));
            }
            throw new ArgumentException("object is not a IDrawDesignControl");
        }
        public override void OnLoadedFromXmlFile(DrawingItem obj, IDesignPane designPane, IDrawDesignControl designControl)
        {
            Point p0 = this.Location;

            this.Copy(obj);             //contained objects are cloned in _items
            if (this.Page != null && this.Page.InDesignMode)
            {
                DrawGroupBox dgb = obj as DrawGroupBox;
                if (dgb != null)
                {
                    IList <DrawingItem> lst = dgb.Items;                    //cloned in _items
                    if (lst != null && designPane != null && designControl != null)
                    {
                        //for each object, create a designer control and assign it to the control
                        Control ctrl = designControl as Control;                         //control corresponding to this object
                        foreach (DrawingItem di in lst)
                        {
                            Type     t  = di.GetType();
                            object[] vs = t.GetCustomAttributes(typeof(TypeMappingAttribute), true);
                            if (vs != null && vs.Length > 0)
                            {
                                TypeMappingAttribute tm = vs[0] as TypeMappingAttribute;
                                if (tm != null)
                                {
                                    //create designer control
                                    IComponent         ic  = designPane.CreateComponent(tm.MappedType, di.Name);
                                    IDrawDesignControl idc = ic as IDrawDesignControl;
                                    Control            c   = ic as Control;
                                    DrawingItem        d0  = idc.Item;                             //auto-created object, should be deleted
                                    ctrl.Controls.Add(c);
                                    idc.Item   = GetItemByID(di.DrawingId);                        //use cloned object for the new control
                                    c.Location = di.Location;
                                    if (_items != null)
                                    {
                                        //remove auto-created object d0
                                        for (int i = 0; i < _items.Count; i++)
                                        {
                                            if (d0 == _items[i] || d0.DrawingId == _items[i].DrawingId)
                                            {
                                                _items.RemoveAt(i);
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            this.Location = p0;
        }
        private void mi_copyDrawing(object sender, EventArgs e)
        {
            MenuItem mi = sender as MenuItem;

            if (mi != null)
            {
                IDrawDesignControl drc = mi.Tag as IDrawDesignControl;
                if (drc != null && drc.Item != null)
                {
                    drc.Item.CopyToClipboardAsBitmapImage(0, 0);
                }
            }
        }
        protected override void OnControlAdded(ControlEventArgs e)
        {
            base.OnControlAdded(e);
            IDrawDesignControl ddc = e.Control as IDrawDesignControl;

            if (ddc != null)
            {
                DrawGroupBox dgb = this.Item as DrawGroupBox;
                if (dgb != null)
                {
                }
            }
        }
        public void OnChildZOrderChanged(IDrawDesignControl itemControl)
        {
            IDrawingHolder h = _item as IDrawingHolder;

            if (h != null)
            {
                h.RefreshDrawingOrder();
                DrawingPage.RefreshDrawingDesignControlZOrders(this, null);
                Form f = this.FindForm();
                if (f != null)
                {
                    f.Invalidate();
                }
            }
        }
 public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
 {
     if (context != null && context.Instance != null && provider != null)
     {
         IWindowsFormsEditorService service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
         if (service != null)
         {
             DrawingItem item = context.Instance as DrawingItem;
             if (item == null)
             {
                 IDrawDesignControl dc = context.Instance as IDrawDesignControl;
                 if (dc != null)
                 {
                     item = dc.Item;
                 }
             }
             if (item != null && item.Page != null)
             {
                 ListBox list = new ListBox();
                 foreach (DrawingLayer l in item.Page.DrawingLayers)
                 {
                     list.Items.Add(l);
                 }
                 list.Tag    = service;
                 list.Click += new EventHandler(list_Click);
                 service.DropDownControl(list);
                 if (list.SelectedIndex >= 0)
                 {
                     DrawingLayer ly = list.Items[list.SelectedIndex] as DrawingLayer;
                     item.Page.MoveItemToLayer(item, ly);
                     value = ly.LayerId.ToString();
                     Type         t    = service.GetType();
                     PropertyInfo pif0 = t.GetProperty("OwnerGrid");
                     if (pif0 != null)
                     {
                         PropertyGrid pg = pif0.GetValue(service, null) as PropertyGrid;
                         if (pg != null)
                         {
                             pg.Refresh();
                         }
                     }
                 }
             }
         }
     }
     return(value);
 }
        public void AddControl(IDrawDesignControl dc)
        {
            if (this.Count == 0)
            {
                Add(new DrawingControlLayer());
            }
            DrawingControlLayer l = null;

            for (int i = 1; i < this.Count; i++)
            {
                if (this[i].LayerId == dc.LayerId)
                {
                    l = this[i];
                    break;
                }
            }
            if (l == null)
            {
                l = this[0];
            }
            l.Add(dc);
        }
        private void mi_loadDrawing(object sender, EventArgs e)
        {
            MenuItem mi = sender as MenuItem;

            if (mi != null)
            {
                IDrawDesignControl drc = mi.Tag as IDrawDesignControl;
                if (drc != null && drc.Item != null)
                {
                    IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost));
                    if (host != null)
                    {
                        HostSurface surface = host.GetService(typeof(DesignSurface)) as HostSurface;
                        if (surface != null)
                        {
                            ClassPointer root = surface.Loader.GetRootId();
                            if (root != null)
                            {
                                ILimnorDesignerLoader ldp = root.GetDesignerLoader();
                                if (ldp != null)
                                {
                                    IDesignPane    idp = ldp.DesignPane as IDesignPane;
                                    OpenFileDialog dlg = new OpenFileDialog();
                                    dlg.CheckFileExists = true;
                                    dlg.DefaultExt      = ".xml";
                                    dlg.Filter          = "XML files|*.xml";
                                    dlg.Title           = "Select XML file for loading drawing";
                                    if (dlg.ShowDialog(drc.Item.Page) == DialogResult.OK)
                                    {
                                        drc.Item.LoadDrawingsFromFile(dlg.FileName, idp, drc);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        private void menu_sendToBack(object sender, EventArgs e)
        {
            MenuItem mi = sender as MenuItem;

            if (mi != null)
            {
                IDrawDesignControl ddc = mi.Tag as IDrawDesignControl;
                if (ddc != null)
                {
                    IDrawingHolder h = ddc.Item.Holder;
                    if (h != null)
                    {
                        h.SendObjectToBack(ddc.Item);
                    }
                }
                Control c = mi.Tag as Control;
                if (c != null)
                {
                    c.SendToBack();
                    makeControlChanged(c);
                }
            }
        }
        private void mi_savedrawing(object sender, EventArgs e)
        {
            MenuItem mi = sender as MenuItem;

            if (mi != null)
            {
                IDrawDesignControl drc = mi.Tag as IDrawDesignControl;
                if (drc != null && drc.Item != null)
                {
                    SaveFileDialog dlg = new SaveFileDialog();
                    dlg.DefaultExt = ".xml";
                    dlg.Filter     = "XML files|*.xml";
                    dlg.Title      = "Specify XML file name for saving the drawing";
                    if (dlg.ShowDialog(drc.Item.Page) == DialogResult.OK)
                    {
                        if (drc.Item.SaveToXmlFile(dlg.FileName))
                        {
                            MessageBox.Show(drc.Item.Page, string.Format(CultureInfo.InvariantCulture, "Drawing saved to {0}", dlg.FileName), "Save Drawing", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
            }
        }
        private void showDrawingBoard(object sender, EventArgs e)
        {
            MenuItem mi = sender as MenuItem;

            if (mi != null)
            {
                DrawingPage page = mi.Tag as DrawingPage;
                if (page != null)
                {
                    IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost));
                    if (host != null)
                    {
                        HostSurface surface = host.GetService(typeof(DesignSurface)) as HostSurface;
                        if (surface != null)
                        {
                            ClassPointer           root   = surface.Loader.GetRootId();
                            DrawingLayerCollection layers = page.ShowDrawingsEditor();
                            if (layers != null)
                            {
                                //layers0 is the existing layer collections
                                DrawingLayerCollection layers0 = page.DrawingLayers;
                                //for layers0 if a drawing object's guid does not exist in layers then
                                //the object has been deleted
                                List <DrawingItem> deleted = new List <DrawingItem>();
                                foreach (DrawingLayer l0 in layers0)
                                {
                                    DrawingItem d = null;
                                    for (int i = 0; i < l0.Count; i++)
                                    {
                                        DrawingItem d0 = l0[i];
                                        d = layers.GetDrawingItemById(d0.DrawingId);
                                        if (d != null)
                                        {
                                            d0.Copy(d);                                            //not deleted
                                            //move and resize the IDrawDesignControl
                                            foreach (Control c in page.Controls)
                                            {
                                                IDrawDesignControl dc0 = c as IDrawDesignControl;
                                                if (dc0 != null)
                                                {
                                                    if (dc0.Item == d0)
                                                    {
                                                        c.Location = d0.Location;
                                                        c.Size     = d0.Bounds.Size;
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            deleted.Add(d0);                                             //deleted
                                        }
                                    }
                                }
                                if (deleted.Count > 0)
                                {
                                    //check to see if each deleted item has usages
                                    List <DrawingItem> ls = new List <DrawingItem>();
                                    foreach (DrawingItem d0 in deleted)
                                    {
                                        for (int i = 0; i < page.Controls.Count; i++)
                                        {
                                            IDrawDesignControl dc = page.Controls[i] as IDrawDesignControl;
                                            if (dc != null)
                                            {
                                                if (dc.DrawingId == d0.DrawingId)
                                                {
                                                    uint id = root.ObjectList.GetObjectID(dc);
                                                    if (id == 0)
                                                    {
                                                        DesignUtil.WriteToOutputWindow("id not found on deleting component {0}", dc.Name);
                                                    }
                                                    else
                                                    {
                                                        List <ObjectTextID> list = root.GetComponentUsage(id);
                                                        if (list.Count > 0)
                                                        {
                                                            dlgObjectUsage dlg = new dlgObjectUsage();
                                                            dlg.LoadData("Cannot delete this component. It is being used by the following objects", string.Format("Component - {0}", dc.Name), list);
                                                            dlg.ShowDialog();
                                                            ls.Add(d0);
                                                        }
                                                    }
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    if (ls.Count > 0)
                                    {
                                        foreach (DrawingItem d0 in ls)
                                        {
                                            deleted.Remove(d0);
                                        }
                                    }
                                    foreach (DrawingItem d0 in deleted)
                                    {
                                        for (int i = 0; i < page.Controls.Count; i++)
                                        {
                                            IDrawDesignControl dc = page.Controls[i] as IDrawDesignControl;
                                            if (dc != null)
                                            {
                                                if (dc.DrawingId == d0.DrawingId)
                                                {
                                                    surface.Loader.DeleteComponent(page.Controls[i]);
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                                //for a drawing object in layers if its guid does not exist in layers0 then
                                //it is a new object
                                //reset zorder according to the new layers
                                List <Control> ctrls = new List <Control>();
                                foreach (DrawingLayer l in layers)
                                {
                                    int zOrder = 0;
                                    foreach (DrawingItem d in l)
                                    {
                                        DrawingItem d0 = layers0.GetDrawingItemById(d.DrawingId);
                                        if (d0 == null)
                                        {
                                            //add a new drawing
                                            string name         = d.Name + Guid.NewGuid().GetHashCode().ToString("x");
                                            Type   designerType = TypeMappingAttribute.GetMappedType(d.GetType());
                                            if (designerType == null)
                                            {
                                                throw new DesignerException("Drawing type {0} does not have a designer", d.GetType());
                                            }
                                            Control            dc  = (Control)surface.Loader.CreateComponent(designerType, name);
                                            IDrawDesignControl ddc = (IDrawDesignControl)dc;
                                            ddc.Item    = d;
                                            dc.Location = d.Location;
                                            dc.Size     = d.Bounds.Size;
                                            ctrls.Add(dc);
                                            ddc.ZOrder = zOrder;
                                        }
                                        else
                                        {
                                            for (int k = 0; k < page.Controls.Count; k++)
                                            {
                                                IDrawDesignControl dc = page.Controls[k] as IDrawDesignControl;
                                                if (dc != null)
                                                {
                                                    if (dc.Item == d0)
                                                    {
                                                        dc.ZOrder = zOrder;
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                        zOrder += 10;
                                    }
                                }
                                if (ctrls.Count > 0)
                                {
                                    page.Controls.AddRange(ctrls.ToArray());
                                }
                                page.LoadData(layers, false);
                                page.Refresh();
                                surface.SetModified();
                                surface.Loader.NotifyChanges();
                            }
                        }
                    }
                }
            }
        }
        /// This is called whenever the user right-clicks on a designer. It removes any local verbs
        /// added by a previous, different selection and adds the local verbs for the current (primary)
        /// selection. Then it displays the ContextMenu.
        public override void ShowContextMenu(CommandID menuID, int x, int y)
        {
            ISelectionService ss = this.GetService(typeof(ISelectionService)) as ISelectionService;

            if (ss == null || ss.PrimarySelection == null)
            {
                return;
            }
            IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost));

            //
            if (host != null)
            {
                HostSurface surface = host.GetService(typeof(DesignSurface)) as HostSurface;
                if (surface != null)
                {
                    ContextMenu menu = new ContextMenu();
                    if (host.RootComponent != ss.PrimarySelection && ss.PrimarySelection is Control)
                    {
                        MenuItem mi = new MenuItem("Bring to front", menu_bringToFront);
                        mi.Tag = ss.PrimarySelection;
                        menu.MenuItems.Add(mi);
                        //
                        mi     = new MenuItem("Send to back", menu_sendToBack);
                        mi.Tag = ss.PrimarySelection;
                        menu.MenuItems.Add(mi);
                        //
                        menu.MenuItems.Add("-");
                    }
                    if (host.RootComponent != ss.PrimarySelection)
                    {
                        MenuItem mi = new MenuItem("Copy", menu_cmd);
                        mi.Tag = StandardCommands.Copy;
                        menu.MenuItems.Add(mi);
                        //
                        mi     = new MenuItem("Cut", menu_cmd);
                        mi.Tag = StandardCommands.Cut;
                        menu.MenuItems.Add(mi);
                        //
                        mi     = new MenuItem("Delete", menu_cmd);
                        mi.Tag = StandardCommands.Delete;
                        menu.MenuItems.Add(mi);
                        //
                        if (ss.SelectionCount == 1)
                        {
                            MenuItem mi2 = new MenuItemWithBitmap("Add to toolbar", addToToolbar, Resource1._toolbar.ToBitmap());
                            IXType   ix  = ss.PrimarySelection as IXType;
                            if (ix != null)
                            {
                                mi2.Tag = ix.ValueType;
                            }
                            else
                            {
                                mi2.Tag = ss.PrimarySelection.GetType();
                            }
                            menu.MenuItems.Add(mi2);
                        }
                    }
                    else
                    {
                        MenuItem mi = new MenuItem("Undo", menu_cmd);
                        mi.Tag = StandardCommands.Undo;
                        menu.MenuItems.Add(mi);
                        //
                        mi     = new MenuItem("Redo", menu_cmd);
                        mi.Tag = StandardCommands.Redo;
                        menu.MenuItems.Add(mi);
                        //
                        mi     = new MenuItem("Paste", menu_cmd);
                        mi.Tag = StandardCommands.Paste;
                        menu.MenuItems.Add(mi);
                    }
                    //
                    DrawingPage dp = ss.PrimarySelection as DrawingPage;
                    if (dp != null)
                    {
                        MenuItem mi2 = new MenuItem("-");
                        menu.MenuItems.Add(mi2);
                        //
                        mi2     = new MenuItemWithBitmap("Drawing Board", showDrawingBoard, Resource1._paint.ToBitmap());
                        mi2.Tag = dp;
                        menu.MenuItems.Add(mi2);
                    }
                    else
                    {
                        IDrawDesignControl drc = ss.PrimarySelection as IDrawDesignControl;
                        if (drc != null)
                        {
                            MenuItem midrc = new MenuItemWithBitmap("Copy to Clipboard as bitmap image", mi_copyDrawing, Resource1._copy.ToBitmap());
                            midrc.Tag = drc;
                            menu.MenuItems.Add(midrc);
                            midrc     = new MenuItemWithBitmap("Save to XML file", mi_savedrawing, Resource1._savefile.ToBitmap());
                            midrc.Tag = drc;
                            menu.MenuItems.Add(midrc);
                            midrc     = new MenuItemWithBitmap("Load from XML file", mi_loadDrawing, Resource1._loadfile.ToBitmap());
                            midrc.Tag = drc;
                            menu.MenuItems.Add(midrc);
                        }
                    }
                    MenuItem mi20 = new MenuItem("-");
                    menu.MenuItems.Add(mi20);
                    MenuItem miAddComponent = new MenuItemWithBitmap("Add component", mi_addComponent, Resource1._newIcon.ToBitmap());
                    menu.MenuItems.Add(miAddComponent);
                    //
                    LimnorContextMenuCollection mdata = surface.GetObjectMenuData(ss.PrimarySelection);
                    if (mdata != null)
                    {
                        if (menu.MenuItems.Count > 0)
                        {
                            menu.MenuItems.Add("-");
                        }
                        mdata.CreateContextMenu(menu, new Point(0, 0), surface.Loader.ViewerHolder);
                    }

                    if (menu.MenuItems.Count > 0)
                    {
                        Control ps = surface.View as Control;
                        if (ps != null)
                        {
                            LimnorXmlDesignerLoader2.MenuPoint = new Point(x, y);
                            Point s = ps.PointToClient(LimnorXmlDesignerLoader2.MenuPoint);
                            menu.Show(ps, s);
                        }
                        else
                        {
                            ps = ss.PrimarySelection as Control;
                            if (ps != null)
                            {
                                LimnorXmlDesignerLoader2.MenuPoint = new Point(x, y);
                                Point s = ps.PointToScreen(new Point(0, 0));
                                menu.Show(ps, new Point(x - s.X, y - s.Y));
                            }
                        }
                    }
                }
            }
        }
 public override void OnRemoveItem(IDrawDesignControl ddc)
 {
     base.OnRemoveItem(ddc);
     _bmp = null;
 }