Example #1
0
        public static void MakeTagsButton(Button btn, TextBox tb, ToolTip tt,
                                          PwGroup pgParent, PwGroup pgTagsSource)
        {
            if ((btn == null) || (tb == null))
            {
                Debug.Assert(false); return;
            }
            Debug.Assert(tt != null);

            Image img = UIUtil.CreateDropDownImage(Properties.Resources.B16x16_KNotes);

            UIUtil.SetButtonImage(btn, img, true);

            UIUtil.SetToolTip(tt, btn, KPRes.TagsAddRemove, true);

            CustomContextMenuStripEx ctx = null;
            Font fItalic = null;

            btn.Click += delegate(object sender, EventArgs e)
            {
                if (ctx == null)
                {
                    ctx     = new CustomContextMenuStripEx();
                    fItalic = FontUtil.CreateFont(ctx.Font, FontStyle.Italic);
                }

                List <string> lCur = StrUtil.StringToTags(tb.Text);

                Dictionary <string, bool> dCur = new Dictionary <string, bool>();
                foreach (string strTag in lCur)
                {
                    dCur[strTag] = true;
                }

                List <string> lParent = ((pgParent != null) ?
                                         pgParent.GetTagsInherited(true) : new List <string>());
                Dictionary <string, bool> dParent = new Dictionary <string, bool>();
                foreach (string strTag in lParent)
                {
                    dParent[strTag] = true;
                }

                List <string> lAll = new List <string>(lCur);
                if (pgTagsSource != null)
                {
                    lAll.AddRange(pgTagsSource.BuildEntryTagsList(false, true));
                    StrUtil.NormalizeTags(lAll);
                }

                List <ToolStripItem> lMenuItems = new List <ToolStripItem>();

                if (lAll.Count == 0)
                {
                    ToolStripMenuItem tsmi = new ToolStripMenuItem(
                        StrUtil.EncodeMenuText("(" + KPRes.TagsNotFound + ")"));
                    tsmi.Enabled = false;

                    lMenuItems.Add(tsmi);
                }
                else
                {
                    for (int i = 0; i < lAll.Count; ++i)
                    {
                        string strTag  = lAll[i];                        // Used in Click handler
                        bool   bHasTag = dCur.ContainsKey(strTag);
                        bool   bInh    = dParent.ContainsKey(strTag);

                        string strSuffix = string.Empty;
                        if (bInh)
                        {
                            strSuffix = " (" + KPRes.Inherited + ")";
                        }

                        ToolStripMenuItem tsmi = new ToolStripMenuItem(
                            StrUtil.EncodeMenuText(strTag + strSuffix));
                        UIUtil.SetChecked(tsmi, bHasTag);

                        if (bInh)
                        {
                            tsmi.Font = fItalic;
                        }

                        tsmi.Click += delegate(object senderT, EventArgs eT)
                        {
                            if (bHasTag)
                            {
                                lCur.Remove(strTag);
                            }
                            else
                            {
                                lCur.Add(strTag);
                                StrUtil.NormalizeTags(lCur);
                            }

                            tb.Text = StrUtil.TagsToString(lCur, true);
                        };

                        lMenuItems.Add(tsmi);
                    }
                }

                ctx.Items.Clear();
                ctx.Items.AddRange(lMenuItems.ToArray());

                ctx.ShowEx(btn);
            };
        }