Example #1
0
        /// <summary>
        /// Round <paramref name="clr" /> to the nearest supported color.
        /// </summary>
        public static Color RoundColor(Color clr)
        {
            Debug.Assert(!UIUtil.ColorsEqual(clr, Color.Empty));
            if ((clr.R == clr.B) && (clr.G == clr.B))
            {
                return(g_clrMain);                // Gray => default
            }
            Color[] v = AppIcons.Colors;

            int c = clr.ToArgb();

            for (int i = 0; i < v.Length; ++i)
            {
                if (v[i].ToArgb() == c)
                {
                    return(clr);
                }
            }

            int iMin = 0, dMin = int.MaxValue;

            for (int i = 0; i < v.Length; ++i)
            {
                int d = ColorDist(clr, v[i]);
                if (d < dMin)
                {
                    iMin = i;
                    dMin = d;
                }
            }
            return(v[iMin]);
        }
Example #2
0
        private void OnFormLoad(object sender, EventArgs e)
        {
            GlobalWindowManager.AddWindow(this);

            this.Icon = Properties.Resources.KeePass;
            this.Text = m_strTitle;

            m_nIconDim = m_tvFolders.ItemHeight;

            if (UIUtil.VistaStyleListsSupported)
            {
                m_tvFolders.ShowLines = false;

                UIUtil.SetExplorerTheme(m_tvFolders, true);
                UIUtil.SetExplorerTheme(m_lvFiles, true);
            }

            m_btnOK.Text   = (m_bSaveMode ? KPRes.SaveCmd : KPRes.OpenCmd);
            m_lblHint.Text = m_strHint;

            if (UIUtil.ColorsEqual(m_lblHint.ForeColor, Color.Black))
            {
                m_lblHint.ForeColor = Color.FromArgb(96, 96, 96);
            }

            int nWidth = m_lvFiles.ClientRectangle.Width - UIUtil.GetVScrollBarWidth();

            m_lvFiles.Columns.Add(KPRes.Name, (nWidth * 3) / 4);
            m_lvFiles.Columns.Add(KPRes.Size, nWidth / 4, HorizontalAlignment.Right);

            InitialPopulateFolders();
            BrowseToFolder(Environment.CurrentDirectory);

            EnableControlsEx();
        }
Example #3
0
        private void OnFormLoad(object sender, EventArgs e)
        {
            Debug.Assert(!m_bSaveMode);             // Saving is not fully supported

            GlobalWindowManager.AddWindow(this);

            this.Icon = AppIcons.Default;
            this.Text = m_strTitle;

            m_nIconDim = m_tvFolders.ItemHeight;

            if (UIUtil.VistaStyleListsSupported)
            {
                UIUtil.SetExplorerTheme(m_tvFolders, true);
                UIUtil.SetExplorerTheme(m_lvFiles, true);
            }

            m_btnOK.Text = (m_bSaveMode ? KPRes.SaveCmd : KPRes.OpenCmd);
            Debug.Assert(!m_lblHint.AutoSize);             // For RTL support
            m_lblHint.Text = m_strHint;

            if (UIUtil.ColorsEqual(m_lblHint.ForeColor, Color.Black))
            {
                m_lblHint.ForeColor = Color.FromArgb(96, 96, 96);
            }

            int nWidth = m_lvFiles.ClientSize.Width - UIUtil.GetVScrollBarWidth();

            m_lvFiles.Columns.Add(KPRes.Name, (nWidth * 3) / 4);
            m_lvFiles.Columns.Add(KPRes.Size, nWidth / 4, HorizontalAlignment.Right);

            InitialPopulateFolders();

            string strWorkDir = Program.Config.Application.GetWorkingDirectory(m_strContext);

            if (string.IsNullOrEmpty(strWorkDir))
            {
                strWorkDir = WinUtil.GetHomeDirectory();
            }
            BrowseToFolder(strWorkDir);

            EnableControlsEx();
        }
Example #4
0
        internal static void ConfigureText(ListViewItem lvi, int iSubItem)
        {
            if (lvi == null)
            {
                Debug.Assert(false); return;
            }
            if ((iSubItem < 0) || (iSubItem >= lvi.SubItems.Count))
            {
                Debug.Assert(false); return;
            }

            ListViewItem.ListViewSubItem lvsi = lvi.SubItems[iSubItem];
            if (lvsi.Text == MultipleValuesEx.CueString)
            {
                Color clrNormal = lvi.ForeColor;
                Color clrMulti  = UIUtil.ColorTowards(clrNormal, (UIUtil.IsDarkColor(
                                                                      clrNormal) ? Color.White : Color.Black), 0.5);
                Debug.Assert(UIUtil.ColorsEqual(clrNormal, SystemColors.ControlText));

                Debug.Assert(lvi.UseItemStyleForSubItems);                 // Caller uses colors already?
                lvi.UseItemStyleForSubItems = false;
                lvsi.ForeColor = clrMulti;
            }
        }
Example #5
0
        public static Icon Get(AppIconType t, Size sz, Color clr)
        {
            int w = Math.Min(Math.Max(sz.Width, 0), 256);
            int h = Math.Min(Math.Max(sz.Height, 0), 256);

            if ((w == 0) || (h == 0))
            {
                Size szDefault = UIUtil.GetIconSize();
                w = szDefault.Width;
                h = szDefault.Height;
            }

            Color c = clr;

            if (!UIUtil.ColorsEqual(c, Color.Empty))
            {
                c = RoundColor(c);
            }

            NumberFormatInfo nf    = NumberFormatInfo.InvariantInfo;
            string           strID = ((long)t).ToString(nf) + ":" + w.ToString(nf) + ":" +
                                     h.ToString(nf) + ":" + c.ToArgb().ToString(nf);

            Icon ico = null;

            lock (g_oCacheSync)
            {
                if (g_dCache.TryGetValue(strID, out ico))
                {
                    return(ico);
                }
            }

            if (t == AppIconType.Main)
            {
                ico = Properties.Resources.KeePass;
            }
            else if (t == AppIconType.QuadNormal)
            {
                ico = Properties.Resources.QuadNormal;
            }
            else if (t == AppIconType.QuadLocked)
            {
                ico = Properties.Resources.QuadLocked;

                Debug.Assert(UIUtil.ColorsEqual(c, Color.Empty));
                c = Color.Empty;                 // This icon should not be recolored
            }
            else
            {
                Debug.Assert(false);
            }

            if ((ico != null) && !UIUtil.ColorsEqual(c, Color.Empty))
            {
                ico = IconColorizer.Recolor(ico, c);
            }

            // Select requested resolution
            if (ico != null)
            {
                ico = new Icon(ico, w, h);                         // Preserves icon data
            }
            Debug.Assert(ico != null);
            lock (g_oCacheSync) { g_dCache[strID] = ico; }
            return(ico);
        }
Example #6
0
            // Ported from KeePass Mainform_functions SetListEntry
            internal static ListViewItem InsertListEntryOLD(PwEntry pe, int iIndex)
            {
                // Adapted variables
                DateTime      m_dtCachedNow       = DateTime.Now;
                Font          m_fontExpired       = FontUtil.CreateFont(m_lvEntries.Font, FontStyle.Strikeout);
                bool          m_bEntryGrouping    = m_lvEntries.ShowGroups;
                bool          m_bShowTanIndices   = Program.Config.MainWindow.TanView.ShowIndices;
                bool          bSubEntries         = Program.Config.MainWindow.ShowEntriesOfSubGroups;
                ListViewGroup m_lvgLastEntryGroup = null;

                foreach (ListViewGroup lvg in m_lvEntries.Groups)
                {
                    if ((lvg.Tag as PwGroup) == pe.ParentGroup)
                    {
                        m_lvgLastEntryGroup = lvg;
                    }
                }
                PwGroup pg = (Program.MainForm.GetSelectedGroup());
                PwObjectList <PwEntry> pwlSource = ((pg != null) ? pg.GetEntries(bSubEntries) : new PwObjectList <PwEntry>());
                bool m_bOnlyTans = ListContainsOnlyTans(pwlSource);

                ListViewItem lviTarget = null;
                Color        m_clrAlternateItemBgColor = UIUtil.GetAlternateColor(m_lvEntries.BackColor);

                if (pe == null)
                {
                    Debug.Assert(false); return(null);
                }

                ListViewItem lvi = (lviTarget ?? new ListViewItem());
                PwListItem   pli = new PwListItem(pe);

                lvi.Tag = pli;

                //lvi.BeginUpdate();

                if (pe.Expires && (pe.ExpiryTime <= m_dtCachedNow))
                {
                    lvi.ImageIndex = (int)PwIcon.Expired;
                    if (m_fontExpired != null)
                    {
                        lvi.Font = m_fontExpired;
                    }
                }
                else // Not expired
                {
                    // Reset font, if item was expired previously (i.e. has expired font)
                    if ((lviTarget != null) && (lvi.ImageIndex == (int)PwIcon.Expired))
                    {
                        lvi.Font = m_lvEntries.Font;
                    }

                    if (pe.CustomIconUuid.EqualsValue(PwUuid.Zero))
                    {
                        lvi.ImageIndex = (int)pe.IconId;
                    }
                    else
                    {
                        lvi.ImageIndex = (int)PwIcon.Count +
                                         m_host.MainWindow.DocumentManager.ActiveDatabase.GetCustomIconIndex(pe.CustomIconUuid);
                    }
                }

                if (m_bEntryGrouping && (lviTarget == null))
                {
                    PwGroup pgContainer = pe.ParentGroup;
                    PwGroup pgLast      = ((m_lvgLastEntryGroup != null) ?
                                           (PwGroup)m_lvgLastEntryGroup.Tag : null);

                    Debug.Assert(pgContainer != null);
                    if (pgContainer != null)
                    {
                        if (pgContainer != pgLast)
                        {
                            m_lvgLastEntryGroup = new ListViewGroup(
                                pgContainer.GetFullPath());
                            m_lvgLastEntryGroup.Tag = pgContainer;

                            m_lvEntries.Groups.Add(m_lvgLastEntryGroup);
                        }

                        lvi.Group = m_lvgLastEntryGroup;
                    }
                }

                if (!pe.ForegroundColor.IsEmpty)
                {
                    lvi.ForeColor = pe.ForegroundColor;
                }
                else if (lviTarget != null)
                {
                    lvi.ForeColor = m_lvEntries.ForeColor;
                }
                else
                {
                    Debug.Assert(UIUtil.ColorsEqual(lvi.ForeColor, m_lvEntries.ForeColor));
                }

                if (!pe.BackgroundColor.IsEmpty)
                {
                    lvi.BackColor = pe.BackgroundColor;
                }
                // else if(Program.Config.MainWindow.EntryListAlternatingBgColors &&
                //	((m_lvEntries.Items.Count & 1) == 1))
                //	lvi.BackColor = m_clrAlternateItemBgColor;
                else if (lviTarget != null)
                {
                    lvi.BackColor = m_lvEntries.BackColor;
                }
                else
                {
                    Debug.Assert(UIUtil.ColorsEqual(lvi.BackColor, m_lvEntries.BackColor));
                }

                // m_bOnlyTans &= PwDefs.IsTanEntry(pe);
                if (m_bShowTanIndices && m_bOnlyTans)
                {
                    string strIndex = pe.Strings.ReadSafe(PwDefs.TanIndexField);

                    if (strIndex.Length > 0)
                    {
                        lvi.Text = strIndex;
                    }
                    else
                    {
                        lvi.Text = PwDefs.TanTitle;
                    }
                }
                else
                {
                    lvi.Text = GetEntryFieldEx(pe, 0, true);
                }

                int nColumns = m_lvEntries.Columns.Count;

                if (lviTarget == null)
                {
                    for (int iColumn = 1; iColumn < nColumns; ++iColumn)
                    {
                        lvi.SubItems.Add(GetEntryFieldEx(pe, iColumn, true));
                    }
                }
                else
                {
                    int nSubItems = lvi.SubItems.Count;
                    for (int iColumn = 1; iColumn < nColumns; ++iColumn)
                    {
                        string strSub = GetEntryFieldEx(pe, iColumn, true);
                        if (iColumn < nSubItems)
                        {
                            lvi.SubItems[iColumn].Text = strSub;
                        }
                        else
                        {
                            lvi.SubItems.Add(strSub);
                        }
                    }

                    Debug.Assert(lvi.SubItems.Count == nColumns);
                }

                //if (lviTarget == null) m_lvEntries.Items.Add(lvi);
                if (lviTarget == null)
                {
                    m_lvEntries.Items.Insert(iIndex, lvi);
                }

                UIUtil.SetAlternatingBgColors(m_lvEntries, m_clrAlternateItemBgColor,
                                              Program.Config.MainWindow.EntryListAlternatingBgColors);

                //lvi.EndUpdate();

                return(lvi);
            }
Example #7
0
        private void MultiApplyProperties()
        {
            string strCue = MultipleValuesEx.CueString;

            bool bExpM = m_peM.Expires;

            bool bSetTags  = ((m_peM.Tags.Count != 1) || (m_peM.Tags[0] != strCue));
            bool bSetOvUrl = (m_peM.OverrideUrl != strCue);
            bool bSetAtSeq = (m_peM.AutoType.DefaultSequence != strCue);

            for (int i = 0; i < m_v.Length; ++i)
            {
                PwEntry pe = m_v[i];

                if (!this.MultiExpiry && ((pe.Expires != bExpM) ||
                                          (bExpM && (pe.ExpiryTime != m_peM.ExpiryTime))))
                {
                    PrepareMod(i);
                    pe.Expires = bExpM;

                    if (bExpM)
                    {
                        pe.ExpiryTime = m_peM.ExpiryTime;
                    }
                }

                if (!this.MultiFgColor && !UIUtil.ColorsEqual(pe.ForegroundColor,
                                                              m_peM.ForegroundColor))
                {
                    PrepareMod(i);
                    pe.ForegroundColor = m_peM.ForegroundColor;
                }

                if (!this.MultiBgColor && !UIUtil.ColorsEqual(pe.BackgroundColor,
                                                              m_peM.BackgroundColor))
                {
                    PrepareMod(i);
                    pe.BackgroundColor = m_peM.BackgroundColor;
                }

                if (bSetTags && !MemUtil.ListsEqual <string>(pe.Tags, m_peM.Tags))
                {
                    PrepareMod(i);
                    pe.Tags = new List <string>(m_peM.Tags);
                }

                if (bSetOvUrl && (pe.OverrideUrl != m_peM.OverrideUrl))
                {
                    PrepareMod(i);
                    pe.OverrideUrl = m_peM.OverrideUrl;
                }

                if (!this.MultiAutoTypeEnabled && (pe.AutoType.Enabled !=
                                                   m_peM.AutoType.Enabled))
                {
                    PrepareMod(i);
                    pe.AutoType.Enabled = m_peM.AutoType.Enabled;
                }

                if (bSetAtSeq && (pe.AutoType.DefaultSequence !=
                                  m_peM.AutoType.DefaultSequence))
                {
                    PrepareMod(i);
                    pe.AutoType.DefaultSequence = m_peM.AutoType.DefaultSequence;
                }

                if (!this.MultiAutoTypeObf && (pe.AutoType.ObfuscationOptions !=
                                               m_peM.AutoType.ObfuscationOptions))
                {
                    PrepareMod(i);
                    pe.AutoType.ObfuscationOptions = m_peM.AutoType.ObfuscationOptions;
                }
            }
        }
Example #8
0
        private void MultiInitProperties()
        {
            string strCue = MultipleValuesEx.CueString;

            bool bExpM = m_v[0].Expires;

            m_peM.Expires    = bExpM;
            m_peM.ExpiryTime = m_v[0].ExpiryTime;

            m_peM.ForegroundColor = m_v[0].ForegroundColor;
            m_peM.BackgroundColor = m_v[0].BackgroundColor;

            bool bTagsEq = true;

            m_peM.Tags = new List <string>(m_v[0].Tags);

            m_peM.OverrideUrl = m_v[0].OverrideUrl;

            m_peM.AutoType.Enabled            = m_v[0].AutoType.Enabled;
            m_peM.AutoType.DefaultSequence    = m_v[0].AutoType.DefaultSequence;
            m_peM.AutoType.ObfuscationOptions = m_v[0].AutoType.ObfuscationOptions;

            for (int i = 1; i < m_v.Length; ++i)
            {
                PwEntry pe = m_v[i];

                if ((pe.Expires != bExpM) || (bExpM && (pe.ExpiryTime != m_peM.ExpiryTime)))
                {
                    this.MultiExpiry = true;
                    m_peM.Expires    = false;
                    bExpM            = false;
                }

                if (!UIUtil.ColorsEqual(pe.ForegroundColor, m_peM.ForegroundColor))
                {
                    this.MultiFgColor     = true;
                    m_peM.ForegroundColor = Color.Empty;
                }

                if (!UIUtil.ColorsEqual(pe.BackgroundColor, m_peM.BackgroundColor))
                {
                    this.MultiBgColor     = true;
                    m_peM.BackgroundColor = Color.Empty;
                }

                if (bTagsEq && !MemUtil.ListsEqual <string>(pe.Tags, m_peM.Tags))
                {
                    m_peM.Tags.Clear();                     // We own it, see above
                    m_peM.Tags.Add(strCue);
                    bTagsEq = false;
                }

                if (pe.OverrideUrl != m_peM.OverrideUrl)
                {
                    m_peM.OverrideUrl = strCue;
                }

                if (pe.AutoType.Enabled != m_peM.AutoType.Enabled)
                {
                    this.MultiAutoTypeEnabled = true;
                    m_peM.AutoType.Enabled    = true;
                }

                if (pe.AutoType.DefaultSequence != m_peM.AutoType.DefaultSequence)
                {
                    m_peM.AutoType.DefaultSequence = strCue;
                }

                if (pe.AutoType.ObfuscationOptions != m_peM.AutoType.ObfuscationOptions)
                {
                    this.MultiAutoTypeObf             = true;
                    m_peM.AutoType.ObfuscationOptions = AutoTypeObfuscationOptions.None;
                }
            }
        }
Example #9
0
        private void OnFormLoad(object sender, EventArgs e)
        {
            Debug.Assert(m_pwDatabase != null); if (m_pwDatabase == null)
            {
                throw new InvalidOperationException();
            }

            m_bInitializing = true;

            GlobalWindowManager.AddWindow(this);

            IOConnectionInfo ioc     = m_pwDatabase.IOConnectionInfo;
            string           strDisp = ioc.GetDisplayName();

            string strDesc = KPRes.DatabaseSettingsDesc;

            if (!string.IsNullOrEmpty(strDisp))
            {
                strDesc = strDisp;
            }

            BannerFactory.CreateBannerEx(this, m_bannerImage,
                                         Properties.Resources.B48x48_Ark, KPRes.DatabaseSettings, strDesc);
            this.Icon = AppIcons.Default;

            FontUtil.AssignDefaultItalic(m_lblHeaderCpAlgo);
            FontUtil.AssignDefaultItalic(m_lblHeaderCp);
            FontUtil.AssignDefaultItalic(m_lblHeaderPerf);

            FontUtil.AssignDefaultBold(m_rbNone);
            FontUtil.AssignDefaultBold(m_rbGZip);

            UIUtil.ConfigureToolTip(m_ttRect);
            m_ttRect.SetToolTip(m_btnKdf1Sec, KPRes.KdfParams1Sec);

            m_tbDbName.PromptText = KPRes.DatabaseNamePrompt;
            m_tbDbDesc.PromptText = KPRes.DatabaseDescPrompt;

            if (m_bCreatingNew)
            {
                this.Text = KPRes.ConfigureOnNewDatabase2;
            }
            else
            {
                this.Text = KPRes.DatabaseSettings;
            }

            m_tbDbName.Text = m_pwDatabase.Name;
            UIUtil.SetMultilineText(m_tbDbDesc, m_pwDatabase.Description);
            m_tbDefaultUser.Text = m_pwDatabase.DefaultUserName;

            m_clr = m_pwDatabase.Color;
            bool bClr = !UIUtil.ColorsEqual(m_clr, Color.Empty);

            if (bClr)
            {
                m_clr = AppIcons.RoundColor(m_clr);
                UIUtil.OverwriteButtonImage(m_btnColor, ref m_imgColor,
                                            UIUtil.CreateColorBitmap24(m_btnColor, m_clr));
            }
            m_cbColor.Checked = bClr;

            for (int inx = 0; inx < CipherPool.GlobalPool.EngineCount; ++inx)
            {
                m_cmbEncAlgo.Items.Add(CipherPool.GlobalPool[inx].DisplayName);
            }

            if (m_cmbEncAlgo.Items.Count > 0)
            {
                int nIndex = CipherPool.GlobalPool.GetCipherIndex(m_pwDatabase.DataCipherUuid);
                m_cmbEncAlgo.SelectedIndex = ((nIndex >= 0) ? nIndex : 0);
            }

            Debug.Assert(m_cmbKdf.Items.Count == 0);
            foreach (KdfEngine kdf in KdfPool.Engines)
            {
                m_cmbKdf.Items.Add(kdf.Name);
            }

            m_numKdfIt.Minimum = ulong.MinValue;
            m_numKdfIt.Maximum = ulong.MaxValue;

            m_numKdfMem.Minimum = ulong.MinValue;
            m_numKdfMem.Maximum = ulong.MaxValue;

            Debug.Assert(m_cmbKdfMem.Items.Count == 0);
            Debug.Assert(!m_cmbKdfMem.Sorted);
            m_cmbKdfMem.Items.Add("B");
            m_cmbKdfMem.Items.Add("KB");
            m_cmbKdfMem.Items.Add("MB");
            m_cmbKdfMem.Items.Add("GB");

            m_numKdfPar.Minimum = uint.MinValue;
            m_numKdfPar.Maximum = uint.MaxValue;

            SetKdfParameters(m_pwDatabase.KdfParameters);

            // m_lbMemProt.Items.Add(KPRes.Title, m_pwDatabase.MemoryProtection.ProtectTitle);
            // m_lbMemProt.Items.Add(KPRes.UserName, m_pwDatabase.MemoryProtection.ProtectUserName);
            // m_lbMemProt.Items.Add(KPRes.Password, m_pwDatabase.MemoryProtection.ProtectPassword);
            // m_lbMemProt.Items.Add(KPRes.Url, m_pwDatabase.MemoryProtection.ProtectUrl);
            // m_lbMemProt.Items.Add(KPRes.Notes, m_pwDatabase.MemoryProtection.ProtectNotes);

            // m_cbAutoEnableHiding.Checked = m_pwDatabase.MemoryProtection.AutoEnableVisualHiding;
            // m_cbAutoEnableHiding.Checked = false;

            if (m_pwDatabase.Compression == PwCompressionAlgorithm.None)
            {
                m_rbNone.Checked = true;
            }
            else if (m_pwDatabase.Compression == PwCompressionAlgorithm.GZip)
            {
                m_rbGZip.Checked = true;
            }
            else
            {
                Debug.Assert(false);
            }

            InitRecycleBinTab();
            InitAdvancedTab();

            m_bInitializing = false;
            EnableControlsEx();
            UIUtil.SetFocus(m_tbDbName, this);
        }