/// <summary>
        /// Handle Word's OnEnter event for content controls, to set the selection in the pane (if the option is set).
        /// </summary>
        /// <param name="ccEntered">A ContentControl object specifying the control that was entered.</param>
        private void doc_ContentControlOnEnter(Word.ContentControl ccEntered)
        {
            log.Debug("Document.ContentControlOnEnter fired.");
            if (ccEntered.XMLMapping.IsMapped)
            {
                log.Debug("control mapped to " + ccEntered.XMLMapping.XPath);
                if (ccEntered.XMLMapping.CustomXMLNode != null)
                {
                    m_cmTaskPane.RefreshControls(Controls.ControlMain.ChangeReason.OnEnter, null, null, null, ccEntered.XMLMapping.CustomXMLNode, null);
                }
                else
                {
                    // Not mapped to anything; probably because the part was replaced?
                    log.Debug(".. but XMLMapping.CustomXMLNode is null");
                    m_cmTaskPane.controlTreeView.DeselectNode();
                    m_cmTaskPane.WarnViaProperties(ccEntered.XMLMapping.XPath);
                }
            }
            else if (ccEntered.Tag != null)
            {
                TagData td      = new TagData(ccEntered.Tag);
                string  xpathid = td.getXPathID();
                if (xpathid == null)
                {
                    xpathid = td.getRepeatID();
                }

                if (xpathid == null)
                {
                    // Visually indicate in the task pane that we're no longer in a mapped control
                    m_cmTaskPane.controlTreeView.DeselectNode();
                    m_cmTaskPane.PropertiesClear();
                }
                else
                {
                    log.Debug("control mapped via tag to " + xpathid);

                    // Repeats, escaped XHTML; we don't show anything for conditions
                    XPathsPartEntry      xppe          = new XPathsPartEntry(m_cmTaskPane.model);
                    xpathsXpath          xx            = xppe.getXPathByID(xpathid);
                    Office.CustomXMLNode customXMLNode = null;
                    if (xx != null)
                    {
                        log.Debug(xx.dataBinding.xpath);
                        customXMLNode = m_currentPart.SelectSingleNode(xx.dataBinding.xpath);
                    }
                    if (customXMLNode == null)
                    {
                        // Not mapped to anything; probably because the part was replaced?
                        m_cmTaskPane.controlTreeView.DeselectNode();
                        if (xx == null)
                        {
                            log.Error("Couldn't find xpath for " + xpathid);
                            m_cmTaskPane.WarnViaProperties("Missing");
                        }
                        else
                        {
                            log.Warn("Couldn't find target node for " + xx.dataBinding.xpath);
                            m_cmTaskPane.WarnViaProperties(xx.dataBinding.xpath);
                        }
                    }
                    else
                    {
                        m_cmTaskPane.RefreshControls(Controls.ControlMain.ChangeReason.OnEnter, null, null, null, customXMLNode, null);
                    }
                }
            }
            Ribbon.buttonEditEnabled   = true;
            Ribbon.buttonDeleteEnabled = true;

            Ribbon.myInvalidate();
        }
Пример #2
0
        /// <summary>
        /// Update the state of our button on the Ribbon.
        /// </summary>
        private void UpdateButtonState()
        {
            //check for an MDI<-->SDI switch
            CheckForMdiSdiSwitch();

            try
            {
                //only leave it off in the fishbowl
                if (Globals.ThisAddIn.Application.Documents.Count == 0)
                {
                    m_ribbon.toggleButtonMappingChecked = false;
                    m_ribbon.toggleButtonMappingEnabled = false;

                    DisableSecondaryButtons();

                    Ribbon.myInvalidate();

                    return;
                }
                else
                {
                    m_ribbon.toggleButtonMappingEnabled = true;
                }
            }
            catch (COMException ex)
            {
                Debug.Fail(ex.Source, ex.Message);
                m_ribbon.toggleButtonMappingEnabled = false;
                return;
            }

            //check pressed state
            if (m_fLastSdiMode)
            {
                //get the ctp for this window (or null if there's not one)
                CustomTaskPane ctpPaneForThisWindow = null;
                try
                {
                    Globals.ThisAddIn.TaskPaneList.TryGetValue(Globals.ThisAddIn.Application.ActiveWindow, out ctpPaneForThisWindow);
                }
                catch (COMException ex)
                {
                    if (ex.Message.Contains("no document is open"))
                    {
                        // silently swallow
                        log.Debug("Silently swallowing " + ex.Message);

                        /* To reproduce:
                         * - in a doc1, add content controls
                         * - create a new doc2 (just a plain Word doc)
                         * - close doc1
                         */
                        return;
                    }
                    else
                    {
                        Debug.Fail("Failed to get CTP:" + ex.Message);
                    }
                }

                //if it's not built, don't check
                if (ctpPaneForThisWindow == null)
                {
                    m_ribbon.toggleButtonMappingChecked = false;
                    DisableSecondaryButtons();
                }
                else
                {
                    //if it's visible, down
                    if (ctpPaneForThisWindow.Visible == true)
                    {
                        m_ribbon.toggleButtonMappingChecked = true;
                        EnableSecondaryButtons();
                    }
                    else
                    {
                        m_ribbon.toggleButtonMappingChecked = false;
                        DisableSecondaryButtons();
                    }
                }
            }
            else
            {
                if (Globals.ThisAddIn.CustomTaskPanes.Count > 0)
                {
                    Debug.Assert(Globals.ThisAddIn.CustomTaskPanes.Count == 1, "why are there multiple CTPs?");

                    if (Globals.ThisAddIn.CustomTaskPanes[0].Visible)
                    {
                        m_ribbon.toggleButtonMappingChecked = true;
                        EnableSecondaryButtons();
                    }
                    else
                    {
                        m_ribbon.toggleButtonMappingChecked = false;
                        DisableSecondaryButtons();
                    }
                }
            }
            Ribbon.myInvalidate();
        }