Пример #1
0
        /// <summary>
        /// Get template of each attribute type
        /// it is used in TextRangeFindDialog.
        /// </summary>
        /// <returns></returns>
        public static IList <TemplateData> GetTemplate()
        {
            var boolList = new List <KeyValuePair <bool, string> >()
            {
                new KeyValuePair <bool, string>(false, "False"), new KeyValuePair <bool, string>(true, "True")
            };

            return(new List <TemplateData>
            {
                CreateTemplateData <int>(UIA_AnimationStyleAttributeId, AnimationStyle.GetInstance()),
                CreateTemplateData <int>(UIA_BackgroundColorAttributeId),
                CreateTemplateData <int>(UIA_BulletStyleAttributeId, BulletStyle.GetInstance()),
                CreateTemplateData <int>(UIA_CapStyleAttributeId, CapStyle.GetInstance()),
                CreateTemplateData <int>(UIA_CultureAttributeId, CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures).Select(c => new KeyValuePair <int, string>(c.LCID, c.EnglishName)).ToList()),
                CreateTemplateData <string>(UIA_FontNameAttributeId),
                CreateTemplateData <double>(UIA_FontSizeAttributeId),
                CreateTemplateData <int>(UIA_FontWeightAttributeId, FontWeight.GetInstance()),
                CreateTemplateData <int>(UIA_ForegroundColorAttributeId),
                CreateTemplateData <int>(UIA_HorizontalTextAlignmentAttributeId, HorizontalTextAlignment.GetInstance()),
                CreateTemplateData <int>(UIA_IndentationFirstLineAttributeId),
                CreateTemplateData <int>(UIA_IndentationLeadingAttributeId),
                CreateTemplateData <int>(UIA_IndentationTrailingAttributeId),
                CreateTemplateData <bool>(UIA_IsHiddenAttributeId, boolList),
                CreateTemplateData <bool>(UIA_IsItalicAttributeId, boolList),
                CreateTemplateData <bool>(UIA_IsReadOnlyAttributeId, boolList),
                CreateTemplateData <bool>(UIA_IsSubscriptAttributeId, boolList),
                CreateTemplateData <bool>(UIA_IsSuperscriptAttributeId, boolList),
                CreateTemplateData <int>(UIA_MarginBottomAttributeId),
                CreateTemplateData <int>(UIA_MarginLeadingAttributeId),
                CreateTemplateData <int>(UIA_MarginTopAttributeId),
                CreateTemplateData <int>(UIA_MarginTrailingAttributeId),
                CreateTemplateData <int>(UIA_OutlineStylesAttributeId, OutlineStyle.GetInstance()),
                CreateTemplateData <int>(UIA_OverlineColorAttributeId),
                CreateTemplateData <int>(UIA_OverlineStyleAttributeId, TextDecorationLineStyle.GetInstance()),
                CreateTemplateData <int>(UIA_StrikethroughColorAttributeId),
                CreateTemplateData <int>(UIA_StrikethroughStyleAttributeId, TextDecorationLineStyle.GetInstance()),
                CreateTemplateData <int>(UIA_TabsAttributeId),
                CreateTemplateData <int>(UIA_TextFlowDirectionsAttributeId, FlowDirection.GetInstance()),
                CreateTemplateData <int>(UIA_UnderlineColorAttributeId),
                CreateTemplateData <int>(UIA_UnderlineStyleAttributeId, TextDecorationLineStyle.GetInstance()),
                CreateTemplateData <int>(UIA_AnnotationTypesAttributeId, AnnotationType.GetInstance()),
                //CreateTemplateData<int>(UIA_AnnotationObjectsAttributeId, AnimationStyles.GetInstance()),
                CreateTemplateData <string>(UIA_StyleNameAttributeId),
                CreateTemplateData <int>(UIA_StyleIdAttributeId, StyleId.GetInstance()),
                //CreateTemplateData<int>(UIA_LinkAttributeId, AnimationStyles.GetInstance()),
                CreateTemplateData <bool>(UIA_IsActiveAttributeId, boolList),
                CreateTemplateData <int>(UIA_SelectionActiveEndAttributeId, ActiveEnd.GetInstance()),
                CreateTemplateData <int>(UIA_CaretPositionAttributeId, CaretPosition.GetInstance()),
                CreateTemplateData <int>(UIA_CaretBidiModeAttributeId, CaretBidiMode.GetInstance()),
                CreateTemplateData <string>(UIA_LineSpacingAttributeId),
                CreateTemplateData <double>(UIA_BeforeParagraphSpacingAttributeId),
                CreateTemplateData <double>(UIA_AfterParagraphSpacingAttributeId),
                CreateTemplateData <int>(UIA_SayAsInterpretAsAttributeId, SayAsInterpretAs.GetInstance()),
            });
        }
        private List <TextAttributeViewModel> GetTextRangeAttributeKeyValuePair(TextRange tr, KeyValuePair <int, string> kv, bool collapse)
        {
            List <TextAttributeViewModel> list = new List <TextAttributeViewModel>();

            dynamic value = tr.GetAttributeValue(kv.Key);

            switch (kv.Key)
            {
            case TextAttributeType.UIA_AnimationStyleAttributeId:
                if (value is int)
                {
                    list.Add(new TextAttributeViewModel(kv.Key, kv.Value, AnimationStyle.GetInstance().GetNameById(value)));
                }
                break;

            case TextAttributeType.UIA_BackgroundColorAttributeId:
            case TextAttributeType.UIA_ForegroundColorAttributeId:
            case TextAttributeType.UIA_OverlineColorAttributeId:
            case TextAttributeType.UIA_StrikethroughColorAttributeId:
            case TextAttributeType.UIA_UnderlineColorAttributeId:
                if (value is int)
                {
                    list.Add(new TextAttributeViewModel(kv.Key, kv.Value, string.Format("#{0:X8}", value)));
                }
                break;

            case TextAttributeType.UIA_BulletStyleAttributeId:
                if (value is int)
                {
                    list.Add(new TextAttributeViewModel(kv.Key, kv.Value, BulletStyle.GetInstance().GetNameById(value)));
                }
                break;

            case TextAttributeType.UIA_CapStyleAttributeId:
                if (value is int)
                {
                    list.Add(new TextAttributeViewModel(kv.Key, kv.Value, CapStyle.GetInstance().GetNameById(value)));
                }
                break;

            case TextAttributeType.UIA_CultureAttributeId:
                if (value is int culture)
                {
                    list.Add(new TextAttributeViewModel(kv.Key, kv.Value, Invariant($"{CultureInfo.GetCultureInfo(culture).EnglishName} ({culture})")));
                }
                break;

            case TextAttributeType.UIA_StyleIdAttributeId:
            case TextAttributeType.UIA_SayAsInterpretAsAttributeId:
                // VT_I4
                if (value is int)
                {
                    list.Add(new TextAttributeViewModel(kv.Key, kv.Value, SayAsInterpretAs.GetInstance().GetNameById(value)));
                }
                break;

            case TextAttributeType.UIA_FontNameAttributeId:
            case TextAttributeType.UIA_StyleNameAttributeId:
            case TextAttributeType.UIA_LineSpacingAttributeId:
                // VT_BSTR
                if (value is string)
                {
                    list.Add(new TextAttributeViewModel(kv.Key, kv.Value, value));
                }
                break;

            case TextAttributeType.UIA_FontSizeAttributeId:
            case TextAttributeType.UIA_IndentationFirstLineAttributeId:
            case TextAttributeType.UIA_IndentationLeadingAttributeId:
            case TextAttributeType.UIA_IndentationTrailingAttributeId:
            case TextAttributeType.UIA_MarginBottomAttributeId:
            case TextAttributeType.UIA_MarginLeadingAttributeId:
            case TextAttributeType.UIA_MarginTopAttributeId:
            case TextAttributeType.UIA_MarginTrailingAttributeId:
            case TextAttributeType.UIA_BeforeParagraphSpacingAttributeId:
            case TextAttributeType.UIA_AfterParagraphSpacingAttributeId:
                // VT_R8
                if (value is double || value is long)
                {
                    list.Add(new TextAttributeViewModel(kv.Key, kv.Value, value.ToString()));
                }
                break;

            case TextAttributeType.UIA_FontWeightAttributeId:
                if (value is int)
                {
                    list.Add(new TextAttributeViewModel(kv.Key, kv.Value, Axe.Windows.Desktop.Styles.FontWeight.GetInstance().GetNameById(value)));
                }
                break;

            case TextAttributeType.UIA_HorizontalTextAlignmentAttributeId:
                if (value is int)
                {
                    list.Add(new TextAttributeViewModel(kv.Key, kv.Value, Axe.Windows.Desktop.Styles.FontWeight.GetInstance().GetNameById(value)));
                }
                break;

            case TextAttributeType.UIA_IsHiddenAttributeId:
            case TextAttributeType.UIA_IsItalicAttributeId:
            case TextAttributeType.UIA_IsReadOnlyAttributeId:
            case TextAttributeType.UIA_IsSubscriptAttributeId:
            case TextAttributeType.UIA_IsSuperscriptAttributeId:
            case TextAttributeType.UIA_IsActiveAttributeId:
                if (value is bool)
                {
                    list.Add(new TextAttributeViewModel(kv.Key, kv.Value, value.ToString()));
                }
                break;

            case TextAttributeType.UIA_OutlineStylesAttributeId:
                if (value is int)
                {
                    list.Add(new TextAttributeViewModel(kv.Key, kv.Value, OutlineStyle.GetInstance().GetNameById(value)));
                }
                break;

            case TextAttributeType.UIA_OverlineStyleAttributeId:
            case TextAttributeType.UIA_StrikethroughStyleAttributeId:
            case TextAttributeType.UIA_UnderlineStyleAttributeId:
                if (value is int)
                {
                    list.Add(new TextAttributeViewModel(kv.Key, kv.Value, TextDecorationLineStyle.GetInstance().GetNameById(value)));
                }
                break;

            case TextAttributeType.UIA_TabsAttributeId:
                var txt = ConvertArrayToString(value);

                if (txt != null)
                {
                    list.Add(new TextAttributeViewModel(kv.Key, kv.Value, txt));
                }
                break;

            case TextAttributeType.UIA_TextFlowDirectionsAttributeId:
                if (value is int)
                {
                    list.Add(new TextAttributeViewModel(kv.Key, kv.Value, Axe.Windows.Desktop.Styles.FlowDirection.GetInstance().GetNameById(value)));
                }
                break;

            case TextAttributeType.UIA_AnnotationTypesAttributeId:
                StringBuilder sb = new StringBuilder();
                if (value is double)
                {
                    list.Add(new TextAttributeViewModel(kv.Key, kv.Value, AnnotationType.GetInstance().GetNameById((int)value)));
                }
                else if (value is Array arr)
                {
                    if (collapse && arr.Length > 0)     // collapse the array into a single row
                    {
                        var count = new Dictionary <string, int>();
                        foreach (var val in arr)
                        {
                            var key = AnnotationType.GetInstance().GetNameById((int)val);
                            if (count.ContainsKey(key))
                            {
                                count[key]++;
                            }
                            else
                            {
                                count[key] = 1;
                            }
                        }

                        StringBuilder strBuild = new StringBuilder();
                        foreach (var item in count)
                        {
                            strBuild.Append(Invariant($"{item.Key}: {item.Value}, "));
                        }
                        strBuild.Length -= 2;   //remove final , and <space>
                        list.Add(new TextAttributeViewModel(kv.Key, kv.Value, strBuild.ToString()));
                    }
                    else     // create a row for each array value
                    {
                        if (arr.Length > 0)
                        {
                            for (int i = 0; i < arr.Length; i++)
                            {
                                list.Add(new TextAttributeViewModel(kv.Key, string.Format(CultureInfo.InvariantCulture, "{0}[{1}]", kv.Value, i), AnnotationType.GetInstance().GetNameById((int)arr.GetValue(i))));
                            }
                        }
                    }
                }
                break;

            case TextAttributeType.UIA_SelectionActiveEndAttributeId:
                if (value is int)
                {
                    list.Add(new TextAttributeViewModel(kv.Key, kv.Value, ActiveEnd.GetInstance().GetNameById(value)));
                }
                break;

            case TextAttributeType.UIA_CaretPositionAttributeId:
                if (value is int)
                {
                    list.Add(new TextAttributeViewModel(kv.Key, kv.Value, CaretPosition.GetInstance().GetNameById(value)));
                }
                break;

            case TextAttributeType.UIA_CaretBidiModeAttributeId:
                if (value is int)
                {
                    list.Add(new TextAttributeViewModel(kv.Key, kv.Value, CaretBidiMode.GetInstance().GetNameById(value)));
                }
                break;

            case TextAttributeType.UIA_AnnotationObjectsAttributeId:
                if (value is IUIAutomationElementArray)
                {
                    IUIAutomationElementArray arr = value;
                    if (arr.Length > 0)
                    {
                        for (int i = 0; i < arr.Length; i++)
                        {
                            list.Add(new TextAttributeViewModel(kv.Key, string.Format(CultureInfo.InvariantCulture, Resources.TextRangeViewModel_GetTextRangeAttributeKeyValuePair_AnnotationObjects_0, i), new DesktopElement((IUIAutomationElement)arr.GetElement(i))));
                        }
                    }
                }
                break;

            case TextAttributeType.UIA_LinkAttributeId:
                // do nothing for now until it is shown as necessary information.
                //try
                //{
                //    IUIAutomationTextRange lnk = Marshal.GetObjectForIUnknown(value) as IUIAutomationTextRange;
                //    list.Add(new TextAttributeViewModel(kv.Value, new TextRangeViewModel(new TextRange(lnk))));
                //}
                //catch (Exception e)
                //{
                //    e.ReportException();
                //}
                break;

            default:
                // need to make a decision for these Attributes since it return Object.
                if (value.GetType().Name != "__ComObject")
                {
                    list.Add(new TextAttributeViewModel(kv.Key, kv.Value, value));
                }
                break;
            }

            return(list);
        }