Example #1
0
 protected static void RemoveFromEnd(List <RichTag> list, RichTag element)
 {
     for (int i = list.Count - 1; i >= 0; i--)
     {
         if (list[i] == element)
         {
             list.RemoveAt(i);
             return;
         }
     }
 }
Example #2
0
        //public void AddExclusiveCommands<TKey>(object parentKey, IEnumerable<MenuSeed<TKey>> seeds)
        //{
        //    foreach ( var seed in seeds )
        //    {
        //        seed.MutuallyExclusive = true;
        //        seed.ParentKey = parentKey;
        //        AddCommand(seed);
        //    }
        //}

        void CompleteItem <Tkey>(MenuItem item, MenuSeed <Tkey> seed)
        {
            RichTag  rt      = (RichTag)item.Tag;
            ICommand command = seed.MutuallyExclusive ? new ClickCommandExclusive <Tkey>() : new ClickCommand <Tkey>();

            item.Command          = command;
            item.CommandParameter = item.Tag;

            if (seed.DefaultCheck.HasValue)
            {
                mapKeyExclusivity[seed.Key] = seed.MutuallyExclusive;
            }

            string tip = seed.Tooltip ?? rt.LabelDetail;

            //  maybe wrap
            //  Regex rgx = new Regex("(.{50}\\s)"); string s = rgx.Replace(longMessage,"$1\n");

            if (seed.ShortcutKey != Key.None)
            {
                KeyGesture keyg = new KeyGesture(seed.ShortcutKey, seed.ShortcutModifier);
                Application.Current.MainWindow.InputBindings.Add(new InputBinding(item.Command, keyg)
                {
                    CommandParameter = item.CommandParameter
                });

                if (!string.IsNullOrWhiteSpace(seed.Tooltip))
                {
                    tip += " ";
                }

                tip += "(";
                if (seed.ShortcutModifier != ModifierKeys.None)
                {
                    tip += seed.ShortcutModifier + "-";
                }

                tip += seed.ShortcutKey + ")";
            }
            item.ToolTip = tip;

            if (seed.Enabled.HasValue)
            {
                item.IsEnabled = seed.Enabled.Value;
            }

            CompleteParentMenuItem(item);
        }
Example #3
0
            public virtual void Execute(object parameter)
            {
                RichTag  rt   = (RichTag)parameter;
                MenuItem item = (MenuItem)rt.Item;

                if (false == item.IsEnabled)
                {
                    return;
                }

                Tkey key       = (Tkey)rt.Tag;
                bool?isChecked = item.IsCheckable ? (bool?)item.IsChecked : (bool?)null;

                ButtonBus <Tkey> .Handle(key, isChecked); // the whole point

                //e.Handled = true;  // otherwise, Click event tunneled down through MenuItem hierarchy
            }
Example #4
0
            void Descend(ItemCollection items, Func <RichTag, bool> allowUncheck)
            {
                foreach (MenuItem item in items)
                {
                    RichTag rt = item.Tag as RichTag;
                    if (rt != null)
                    {
                        string qualifier = rt.Qualifier;

                        if (item.IsChecked)
                        {
                            CheckedEnums.Add(qualifier);
                        }
                        else if (allowUncheck(rt))
                        {
                            UncheckedEnums.Add(qualifier);
                        }
                    }
                    Descend(item.Items, allowUncheck);
                }
            }
Example #5
0
            public override void Execute(object parameter)
            {
                base.Execute(parameter);

                // turn off sibiling items
                RichTag  rt   = (RichTag)parameter;
                MenuItem item = (MenuItem)rt.Item;

                if (false == item.IsEnabled)
                {
                    return;
                }

                MenuItem parent = (MenuItem)item.Parent;

                foreach (MenuItem otherItem in parent.Items)
                {
                    if (otherItem != item)
                    {
                        otherItem.IsChecked = false;
                    }
                }
            }
Example #6
0
        public override void Markup(TextBlock textBlock, string text)
        {
            textBlock.Text = string.Empty;
            textBlock.Inlines.Clear();

            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            int pos = text.ToLowerInvariant().IndexOf(searchText.ToLowerInvariant());

            List <RichTag> openedTags = new List <RichTag>();

            StringBuilder sb = new StringBuilder();

            void flush()
            {
                Inline inl = new Run(sb.ToString());

                foreach (var t in openedTags)
                {
                    t.Modify(inl);
                }
                sb.Clear();

                textBlock.Inlines.Add(inl);
            }

            for (int i = 0; i < text.Length; i++)
            {
                if (i == pos)
                {
                    flush();
                    openedTags.Add(new RichTag(RichTag.SEARCH_NAME));
                }
                else if (i == pos + searchText.Length)
                {
                    flush();
                    RemoveFromEnd(openedTags, new RichTag(RichTag.SEARCH_NAME));
                }

                char?prev    = i > 0 ? text[i - 1] : (char?)null;
                char current = text[i];

                if (current == '<' && prev != '\\')
                {
                    string tag = text.Substring(i, text.IndexOf('>', i) - i);
                    i += tag.Length;

                    string   clearTag    = tag.Trim('<', '>');
                    int      num         = clearTag.IndexOf('/');
                    string   clearerTag  = clearTag.Trim('/');
                    string[] splittedTag = clearerTag.Split('=');
                    RichTag  t;
                    if (splittedTag.Length == 1)
                    {
                        t = new RichTag(splittedTag[0]);
                    }
                    else
                    {
                        t = new RichTag(splittedTag[0], splittedTag[1]);
                    }
                    if (num != -1) // close
                    {
                        flush();
                        RemoveFromEnd(openedTags, t);
                    }
                    else // open
                    {
                        flush();
                        openedTags.Add(t);
                    }
                }
                else
                {
                    sb.Append(current);
                }
            }

            flush();
        }
Example #7
0
        public static List <string> richWrapMessage(string text)
        {
            IEnumerable <T> Reverse <T>(IList <T> ts)
            {
                for (int i = ts.Count - 1; i >= 0; i--)
                {
                    yield return(ts[i]);
                }
            }

            void RemoveFromEnd(List <RichTag> richTags, RichTag element)
            {
                for (int i = richTags.Count - 1; i >= 0; i--)
                {
                    if (richTags[i] == element)
                    {
                        richTags.RemoveAt(i);
                        return;
                    }
                }
            }

            List <string> result = new List <string>();

            List <RichTag> tags = new List <RichTag>();

            StringBuilder sb = new StringBuilder();

            int clearLength = 0;

            for (int i = 0; i < text.Length; i++)
            {
                char?  prev    = i > 0 ? text[i - 1] : (char?)null;
                char?  next    = i < text.Length - 1 ? text[i + 1] : (char?)null;
                char   current = text[i];
                string cut     = text.Substring(i);

                float lRatio = (sb.Length + tags.Select(d => d.Close).Sum(d => d.Length)) / 255f;

                if (clearLength >= 90 || lRatio >= 0.75f)
                {
                    result.Add($"{sb}{string.Join("", Reverse(tags).Select(d => d.Close))}");
                    sb.Clear();
                    clearLength = 0;
                    sb.Append(string.Join("", tags.Select(d => d.Open)));
                }

                switch (current)
                {
                case '<' when prev != '\\':
                    string tag = cut.Substring(0, cut.IndexOf('>'));
                    i += tag.Length;
                    if (next == '/')
                    {
                        RichTag t = RichTag.FromClose(tag);
                        RemoveFromEnd(tags, t);
                        sb.Append(t.Close);
                    }
                    else
                    {
                        RichTag t = RichTag.FromOpen(tag);
                        tags.Add(t);
                        sb.Append(t.Open);
                    }
                    break;

                default:
                    sb.Append(current);
                    clearLength++;
                    break;
                }
            }

            if (sb.Length > 0)
            {
                result.Add($"{sb}{string.Join("", Reverse(tags).Select(d => d.Close))}");
            }

            return(result);
        }