/// ------------------------------------------------------------------------------------
        /// <summary>
        /// Shows the Format Styles dialog.
        /// </summary>
        /// <param name="paraStyleName">Name of the initially selected paragraph style.</param>
        /// <param name="charStyleName">Name of the initially selected character style.</param>
        /// <param name="setPropsToFactorySettings">Delegate to set style info properties back
        /// to the default facotry settings</param>
        /// <returns>
        /// true if a refresh is needed to reload the cache
        /// </returns>
        /// ------------------------------------------------------------------------------------
        public bool ShowStylesDialog(string paraStyleName, string charStyleName,
                                     Action <StyleInfo> setPropsToFactorySettings)
        {
            StVc        vc             = null;
            IVwRootSite activeViewSite = null;

            if (EditingHelper == null)            // If the calling location doesn't provide this just bring up the dialog with normal selected
            {
                paraStyleName = "Normal";
            }
            if (ActiveView != null)
            {
                vc             = ActiveView.EditingHelper.ViewConstructor as StVc;
                activeViewSite = ActiveView.CastAsIVwRootSite();
            }
            if (paraStyleName == null && charStyleName == null && EditingHelper.CurrentSelection != null &&
                EditingHelper.CurrentSelection.Selection != null)
            {
                // If the caller didn't know the default style, try to figure it out from
                // the selection.
                GetStyleNames(ActiveView as SimpleRootSite, EditingHelper.CurrentSelection.Selection,
                              ref paraStyleName, ref charStyleName);
            }
            using (FwStylesDlg stylesDlg = new FwStylesDlg(activeViewSite,
                                                           Cache, ActiveStyleSheet, (vc == null) ? false : vc.RightToLeft,
                                                           Cache.ServiceLocator.WritingSystems.AllWritingSystems.Any(ws => ws.RightToLeftScript),
                                                           ActiveStyleSheet.GetDefaultBasedOnStyleName(),
                                                           MaxStyleLevelToShow, m_app.MeasurementSystem, paraStyleName, charStyleName,
                                                           HvoAppRootObject, m_app, m_app))
            {
                stylesDlg.SetPropsToFactorySettings         = setPropsToFactorySettings;
                stylesDlg.StylesRenamedOrDeleted           += m_callbacks.OnStylesRenamedOrDeleted;
                stylesDlg.AllowSelectStyleTypes             = true;
                stylesDlg.ShowTEStyleTypes                  = m_callbacks.ShowTEStylesComboInStylesDialog;
                stylesDlg.CanSelectParagraphBackgroundColor = m_callbacks.CanSelectParagraphBackgroundColor;
                return(stylesDlg.ShowDialog(m_callbacks) == DialogResult.OK &&
                       ((stylesDlg.ChangeType & StyleChangeType.DefChanged) > 0 ||
                        (stylesDlg.ChangeType & StyleChangeType.Added) > 0));
            }
        }
        private void GetStyleNames(SimpleRootSite rootsite, IVwSelection sel, ref string paraStyleName,
                                   ref string charStyleName)
        {
            ITsTextProps[]     vttp;
            IVwPropertyStore[] vvps;
            int cttp;

            SelectionHelper.GetSelectionProps(sel, out vttp, out vvps, out cttp);
            bool   fSingleStyle = true;
            string sStyle       = null;

            for (int ittp = 0; ittp < cttp; ++ittp)
            {
                string style = vttp[ittp].Style();
                if (ittp == 0)
                {
                    sStyle = style;
                }
                else if (sStyle != style)
                {
                    fSingleStyle = false;
                }
            }
            if (fSingleStyle && !String.IsNullOrEmpty(sStyle))
            {
                if (ActiveStyleSheet.GetType(sStyle) == (int)StyleType.kstCharacter)
                {
                    if (sel.CanFormatChar)
                    {
                        charStyleName = sStyle;
                    }
                }
                else
                {
                    if (sel.CanFormatPara)
                    {
                        paraStyleName = sStyle;
                    }
                }
            }
            if (paraStyleName == null)
            {
                // Look at the paragraph (if there is one) to get the paragraph style.
                var helper = SelectionHelper.GetSelectionInfo(sel, rootsite);
                var info   = helper.GetLevelInfo(SelectionHelper.SelLimitType.End);
                if (info.Length > 0)
                {
                    var hvo = info[0].hvo;
                    if (hvo != 0)
                    {
                        var cmObjectRepository = m_callbacks.Cache.ServiceLocator.GetInstance <ICmObjectRepository>();
                        if (cmObjectRepository.IsValidObjectId(hvo))                         // perhaps some sort of dummy; we can't get paragraph style.
                        {
                            var cmo = cmObjectRepository.GetObject(hvo);
                            if (cmo is IStPara)
                            {
                                paraStyleName = (cmo as IStPara).StyleName;
                            }
                        }
                    }
                }
            }
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Shows the Format Apply Style dialog. Apply the results to the selection of the active
        /// view if the user clicks OK.
        /// </summary>
        /// <param name="paraStyleName">Name of the para style.</param>
        /// <param name="charStyleName">Name of the char style.</param>
        /// <param name="maxStyleLevel">The maximum style level that will be shown in this
        /// dialog. (apps that do not use style levels in their stylesheets can pass 0)</param>
        /// ------------------------------------------------------------------------------------
        public void ShowApplyStyleDialog(string paraStyleName, string charStyleName, int maxStyleLevel)
        {
            SimpleRootSite rootsite = (ActiveView as SimpleRootSite);

            try
            {
                if (rootsite != null)
                {
                    rootsite.ShowRangeSelAfterLostFocus = true;
                }

                IVwSelection sel = EditingHelper.CurrentSelection.Selection;
                if (paraStyleName == null && charStyleName == null)
                {
                    // If the caller didn't know the default style, try to figure it out from
                    // the selection.
                    GetStyleNames(rootsite, sel, ref paraStyleName, ref charStyleName);
                }
                int hvoRoot, frag;
                IVwViewConstructor vc;
                IVwStylesheet      ss;
                ActiveView.CastAsIVwRootSite().RootBox.GetRootObject(out hvoRoot, out vc, out frag, out ss);
                using (FwApplyStyleDlg applyStyleDlg = new FwApplyStyleDlg(ActiveView.CastAsIVwRootSite(),
                                                                           Cache, ActiveStyleSheet.RootObjectHvo, StyleSheetOwningFlid,
                                                                           ActiveStyleSheet.GetDefaultBasedOnStyleName(), maxStyleLevel,
                                                                           paraStyleName, charStyleName, hvoRoot, m_app, m_app))
                {
                    if (FwEditingHelper != null)
                    {
                        if (FwEditingHelper.ApplicableStyleContexts != null)
                        {
                            applyStyleDlg.ApplicableStyleContexts = FwEditingHelper.ApplicableStyleContexts;
                        }
                    }
                    else if (m_app != null)
                    {
                        // Window doesn't have an editing helper, go with whole-app default
                        if (m_app.DefaultStyleContexts != null)
                        {
                            applyStyleDlg.ApplicableStyleContexts = m_app.DefaultStyleContexts;
                        }
                    }
                    applyStyleDlg.AllowSelectStyleTypes  = m_callbacks.ShowTEStylesComboInStylesDialog;
                    applyStyleDlg.CanApplyCharacterStyle = sel.CanFormatChar;
                    applyStyleDlg.CanApplyParagraphStyle = sel.CanFormatPara;

                    if (applyStyleDlg.ShowDialog(m_callbacks) == DialogResult.OK)
                    {
                        string sUndo, sRedo;
                        ResourceHelper.MakeUndoRedoLabels("kstidUndoApplyStyle", out sUndo, out sRedo);
                        using (UndoTaskHelper helper = new UndoTaskHelper(Cache.ActionHandlerAccessor,
                                                                          ActiveView.CastAsIVwRootSite(), sUndo, sRedo))
                        {
                            EditingHelper.ApplyStyle(applyStyleDlg.StyleChosen);
                            helper.RollBack = false;
                        }
                    }
                }
            }
            finally
            {
                if (rootsite != null)
                {
                    rootsite.ShowRangeSelAfterLostFocus = false;
                }
            }
        }