public OfficePart(PackagePart part, XMLParts partType, string relationshipId)
 {
     _part     = part;
     _partType = partType;
     _id       = relationshipId;
     _name     = System.IO.Path.GetFileName(_part.Uri.ToString());
 }
        public void RemoveCustomPart(XMLParts partType)
        {
            Debug.Assert(!_isReadOnly, "File is ReadOnly!");
            if (_isReadOnly)
            {
                return;
            }

            for (int i = _xmlParts.Count - 1; i >= 0; i--)
            {
                if (_xmlParts[i].PartType != partType)
                {
                    continue;
                }

                OfficePart part = _xmlParts[i];
                part.Remove();

                part = null;
                _xmlParts.RemoveAt(i);

                _package.Flush();
                _isDirty = true;
            }
        }
        public void SaveCustomPart(XMLParts partType, string text, bool isCreatingNewPart)
        {
            Debug.Assert(!_isReadOnly, "File is ReadOnly!");
            if (_isReadOnly)
            {
                return;
            }

            OfficePart targetPart = RetrieveCustomPart(partType);

            if (targetPart == null)
            {
                if (isCreatingNewPart)
                {
                    targetPart = this.CreateCustomPart(partType);
                }
                else
                {
                    return;
                }
            }

            Debug.Assert(targetPart != null);
            targetPart.Save(text);
            _isDirty = true;
        }
        private void ShowInsertIconsDialog(XMLParts partType)
        {
            Debug.Assert(this.package != null);
            Debug.Assert(!this.package.ReadOnly);
            if (this.package == null || this.package.ReadOnly)
            {
                return;
            }

            OpenFileDialog insertIconsDialog = new OpenFileDialog();

            insertIconsDialog.Tag = partType;

            Debug.Assert(insertIconsDialog != null);

            #region Initializing Insert Icons Dialog
            insertIconsDialog.Title            = StringsResource.idsInsertIconsDialogTitle;
            insertIconsDialog.Multiselect      = true;
            insertIconsDialog.Filter           = StringsResource.idsFilterAllSupportedImages + "|" + StringsResource.idsFilterAllFiles;
            insertIconsDialog.FilterIndex      = 0;
            insertIconsDialog.RestoreDirectory = false;
            #endregion

            insertIconsDialog.FileOk += new CancelEventHandler(this.insertIcons_FileOk);
            insertIconsDialog.ShowDialog(this);

            insertIconsDialog.Dispose();
        }
        private void insertIcons_FileOk(object sender, CancelEventArgs e)
        {
            XMLParts   partType = (XMLParts)(sender as OpenFileDialog).Tag;
            OfficePart part     = this.package.RetrieveCustomPart(partType);

            Debug.Assert(part != null);

            TreeNode partNode = null;

            foreach (TreeNode node in tvDocument.Nodes[0].Nodes)
            {
                if (node.Text == part.Name)
                {
                    partNode = node;
                    break;
                }
            }
            Debug.Assert(partNode != null);

            tvDocument.SuspendLayout();
            foreach (string fileName in (sender as OpenFileDialog).FileNames)
            {
                try
                {
                    string id                  = XmlConvert.EncodeName(Path.GetFileNameWithoutExtension(fileName));
                    Stream imageStream         = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    System.Drawing.Image image = System.Drawing.Image.FromStream(imageStream, true, true);

                    // The file is a valid image at this point.
                    id = part.AddImage(fileName, id);

                    Debug.Assert(id != null, "Cannot create image part.");
                    if (id == null)
                    {
                        continue;
                    }

                    imageStream.Close();

                    TreeNode imageNode = new TreeNode(id);
                    imageNode.ImageKey         = "_" + id;
                    imageNode.SelectedImageKey = imageNode.ImageKey;
                    imageNode.ContextMenuStrip = imageContextMenu;
                    imageNode.Tag = partType;

                    tvDocument.ImageList.Images.Add(imageNode.ImageKey, image);
                    partNode.Nodes.Add(imageNode);

                    this.package.IsDirty = true;
                }
                catch (Exception ex)
                {
                    ShowError(ex.Message);
                    continue;
                }
            }

            tvDocument.ResumeLayout();
        }
        public OfficePart CreateCustomPart(XMLParts partType)
        {
            string relativePath;
            string relType;

            switch (partType)
            {
            case XMLParts.RibbonX12:
                relativePath = "/customUI/customUI.xml";
                relType      = CustomUIPartRelType;
                break;

            case XMLParts.RibbonX14:
                relativePath = "/customUI/customUI14.xml";
                relType      = CustomUI14PartRelType;
                break;

            case XMLParts.QAT12:
                relativePath = "/customUI/qat.xml";
                relType      = QATPartRelType;
                break;

            default:
                Debug.Assert(false, "Unknown type");
                return(null);
            }

            Uri customUIUri = new Uri(relativePath, UriKind.Relative);
            PackageRelationship relationship = _package.CreateRelationship(customUIUri, TargetMode.Internal, relType);

            OfficePart part = null;

            if (!_package.PartExists(customUIUri))
            {
                part = new OfficePart(_package.CreatePart(customUIUri, "application/xml"), partType, relationship.Id);
            }
            else
            {
                part = new OfficePart(_package.GetPart(customUIUri), partType, relationship.Id);
            }
            Debug.Assert(part != null, "Fail to create custom part.");

            _xmlParts.Add(part);
            _isDirty = true;

            return(part);
        }
        public OfficePart RetrieveCustomPart(XMLParts partType)
        {
            Debug.Assert(_xmlParts != null);
            if (_xmlParts == null || _xmlParts.Count == 0)
            {
                return(null);
            }

            for (int i = 0; i < _xmlParts.Count; i++)
            {
                if (_xmlParts[i].PartType == partType)
                {
                    return(_xmlParts[i]);
                }
            }
            return(null);
        }
        private void tvDocument_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Tag == null || e.Node.Tag is TreeNode)
            {
                return;
            }

            XMLParts partType = (XMLParts)e.Node.Tag;

            if (rtbCustomUI.Tag != null && partType == (XMLParts)rtbCustomUI.Tag)
            {
                return;
            }

            rtbCustomUI.Tag = partType;
            rtbCustomUI.Rtf = rtfContents[(int)partType];

            this.rtbCustomUI.Modified = false;
            this.colorTimer.Stop();
        }
 public void SaveCustomPart(XMLParts partType, string text)
 {
     SaveCustomPart(partType, text, false /*isCreatingNewPart*/);
 }
        private void insertIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            XMLParts partType = (XMLParts)((TreeNode)tvDocument.Tag).Tag;

            ShowInsertIconsDialog(partType);
        }