1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2012 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using KeePass.Resources;
namespace KeePass.UI
{
public delegate void SortCommandHandler(bool bEnableSorting, int nColumn,
SortOrder? soForce, bool bUpdateEntryList);
public sealed class ListViewSortMenu
{
private ToolStripMenuItem m_tsmiMenu;
private ListView m_lv;
private SortCommandHandler m_h;
private int m_iCurSortColumn = -1;
private bool m_bCurSortAsc = true;
private ToolStripMenuItem m_tsmiInitDummy = null;
private ToolStripMenuItem m_tsmiNoSort = null;
private ToolStripSeparator m_tssSep0 = null;
private List<ToolStripMenuItem> m_vColumns = null;
private ToolStripSeparator m_tssSep1 = null;
private ToolStripMenuItem m_tsmiAsc = null;
private ToolStripMenuItem m_tsmiDesc = null;
public ListViewSortMenu(ToolStripMenuItem tsmiContainer, ListView lvTarget,
SortCommandHandler h)
{
if(tsmiContainer == null) throw new ArgumentNullException("tsmiContainer");
if(lvTarget == null) throw new ArgumentNullException("lvTarget");
if(h == null) throw new ArgumentNullException("h");
m_tsmiMenu = tsmiContainer;
m_lv = lvTarget;
m_h = h;
m_tsmiInitDummy = new ToolStripMenuItem(KPRes.NoSort);
m_tsmiMenu.DropDownItems.Add(m_tsmiInitDummy);
m_tsmiMenu.DropDownOpening += this.UpdateMenu;
}
public void Release()
{
if(m_tsmiMenu != null)
{
DeleteMenuItems();
m_tsmiMenu.DropDownOpening -= this.UpdateMenu;
m_tsmiMenu = null;
m_lv = null;
m_h = null;
}
}
private void DeleteMenuItems()
{
if(m_tsmiInitDummy != null)
{
m_tsmiMenu.DropDownItems.Remove(m_tsmiInitDummy);
m_tsmiInitDummy = null;
}
if(m_tsmiNoSort == null) return;
m_tsmiNoSort.Click -= this.OnNoSort;
foreach(ToolStripMenuItem tsmiCol in m_vColumns)
tsmiCol.Click -= this.OnSortColumn;
m_tsmiAsc.Click -= this.OnSortAscDesc;
m_tsmiDesc.Click -= this.OnSortAscDesc;
m_tsmiMenu.DropDownItems.Clear();
m_tsmiNoSort = null;
m_tssSep0 = null;
m_vColumns = null;
m_tssSep1 = null;
m_tsmiAsc = null;
m_tsmiDesc = null;
}
private void UpdateMenu(object sender, EventArgs e)
{
if(m_lv == null) { Debug.Assert(false); return; }
DeleteMenuItems();
IComparer icSorter = m_lv.ListViewItemSorter;
ListSorter ls = ((icSorter != null) ? (icSorter as ListSorter) : null);
if(ls != null)
{
m_iCurSortColumn = ls.Column;
m_bCurSortAsc = (ls.Order != SortOrder.Descending);
if((ls.Order == SortOrder.None) || (m_iCurSortColumn >= m_lv.Columns.Count))
m_iCurSortColumn = -1;
}
else m_iCurSortColumn = -1;
m_tsmiNoSort = new ToolStripMenuItem(KPRes.NoSort);
if(m_iCurSortColumn < 0) SetRadioChecked(m_tsmiNoSort);
m_tsmiNoSort.Click += this.OnNoSort;
m_tsmiMenu.DropDownItems.Add(m_tsmiNoSort);
m_tssSep0 = new ToolStripSeparator();
m_tsmiMenu.DropDownItems.Add(m_tssSep0);
m_vColumns = new List<ToolStripMenuItem>();
foreach(ColumnHeader ch in m_lv.Columns)
{
string strText = (ch.Text ?? string.Empty);
strText = strText.Replace(@"&", @"&&");
ToolStripMenuItem tsmi = new ToolStripMenuItem(strText);
if(ch.Index == m_iCurSortColumn) SetRadioChecked(tsmi);
tsmi.Click += this.OnSortColumn;
m_vColumns.Add(tsmi);
m_tsmiMenu.DropDownItems.Add(tsmi);
}
m_tssSep1 = new ToolStripSeparator();
m_tsmiMenu.DropDownItems.Add(m_tssSep1);
m_tsmiAsc = new ToolStripMenuItem(KPRes.Ascending);
if((m_iCurSortColumn >= 0) && m_bCurSortAsc) SetRadioChecked(m_tsmiAsc);
m_tsmiAsc.Click += this.OnSortAscDesc;
if(m_iCurSortColumn < 0) m_tsmiAsc.Enabled = false;
m_tsmiMenu.DropDownItems.Add(m_tsmiAsc);
m_tsmiDesc = new ToolStripMenuItem(KPRes.Descending);
if((m_iCurSortColumn >= 0) && !m_bCurSortAsc) SetRadioChecked(m_tsmiDesc);
m_tsmiDesc.Click += this.OnSortAscDesc;
if(m_iCurSortColumn < 0) m_tsmiDesc.Enabled = false;
m_tsmiMenu.DropDownItems.Add(m_tsmiDesc);
}
private void OnNoSort(object sender, EventArgs e)
{
if(m_h == null) { Debug.Assert(false); return; }
m_h(false, 0, null, true);
}
private void OnSortColumn(object sender, EventArgs e)
{
if(m_h == null) { Debug.Assert(false); return; }
ToolStripMenuItem tsmi = (sender as ToolStripMenuItem);
if(tsmi == null) { Debug.Assert(false); return; }
for(int i = 0; i < m_vColumns.Count; ++i)
{
if(m_vColumns[i] == tsmi)
{
bool bAsc = m_bCurSortAsc;
if(i == m_iCurSortColumn) bAsc = !bAsc; // Toggle
m_h(true, i, bAsc ? SortOrder.Ascending : SortOrder.Descending, true);
break;
}
}
}
private void OnSortAscDesc(object sender, EventArgs e)
{
if(m_h == null) { Debug.Assert(false); return; }
if(m_iCurSortColumn < 0) { Debug.Assert(false); return; }
ToolStripMenuItem tsmi = (sender as ToolStripMenuItem);
if(tsmi == null) { Debug.Assert(false); return; }
if((tsmi == m_tsmiAsc) && !m_bCurSortAsc)
m_h(true, m_iCurSortColumn, SortOrder.Ascending, true);
else if((tsmi == m_tsmiDesc) && m_bCurSortAsc)
m_h(true, m_iCurSortColumn, SortOrder.Descending, true);
}
private static void SetRadioChecked(ToolStripMenuItem tsmi)
{
tsmi.Image = Properties.Resources.B16x16_MenuRadio;
tsmi.CheckState = CheckState.Checked;
}
}
}
|