Imported existing sources

This commit is contained in:
Daniel Brunner
2017-02-04 13:47:58 +01:00
parent 14a5be61cc
commit 7c97b9e539
86 changed files with 49665 additions and 0 deletions

63
.gitattributes vendored Normal file
View File

@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto
###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp
###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary
###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary
###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain

View File

@ -0,0 +1,520 @@
/*
* CellEditKeyEngine - A engine that allows the behaviour of arbitrary keys to be configured
*
* Author: Phillip Piper
* Date: 3-March-2011 10:53 pm
*
* Change log:
* v2.8
* 2014-05-30 JPP - When a row is disabled, skip over it when looking for another cell to edit
* v2.5
* 2012-04-14 JPP - Fixed bug where, on a OLV with only a single editable column, tabbing
* to change rows would edit the cell above rather than the cell below
* the cell being edited.
* 2.5
* 2011-03-03 JPP - First version
*
* Copyright (C) 2011-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using BrightIdeasSoftware;
namespace BrightIdeasSoftware {
/// <summary>
/// Indicates the behavior of a key when a cell "on the edge" is being edited.
/// and the normal behavior of that key would exceed the edge. For example,
/// for a key that normally moves one column to the left, the "edge" would be
/// the left most column, since the normal action of the key cannot be taken
/// (since there are no more columns to the left).
/// </summary>
public enum CellEditAtEdgeBehaviour {
/// <summary>
/// The key press will be ignored
/// </summary>
Ignore,
/// <summary>
/// The key press will result in the cell editing wrapping to the
/// cell on the opposite edge.
/// </summary>
Wrap,
/// <summary>
/// The key press will wrap, but the column will be changed to the
/// appropiate adjacent column. This only makes sense for keys where
/// the normal action is ChangeRow.
/// </summary>
ChangeColumn,
/// <summary>
/// The key press will wrap, but the row will be changed to the
/// appropiate adjacent row. This only makes sense for keys where
/// the normal action is ChangeColumn.
/// </summary>
ChangeRow,
/// <summary>
/// The key will result in the current edit operation being ended.
/// </summary>
EndEdit
};
/// <summary>
/// Indicates the normal behaviour of a key when used during a cell edit
/// operation.
/// </summary>
public enum CellEditCharacterBehaviour {
/// <summary>
/// The key press will be ignored
/// </summary>
Ignore,
/// <summary>
/// The key press will end the current edit and begin an edit
/// operation on the next editable cell to the left.
/// </summary>
ChangeColumnLeft,
/// <summary>
/// The key press will end the current edit and begin an edit
/// operation on the next editable cell to the right.
/// </summary>
ChangeColumnRight,
/// <summary>
/// The key press will end the current edit and begin an edit
/// operation on the row above.
/// </summary>
ChangeRowUp,
/// <summary>
/// The key press will end the current edit and begin an edit
/// operation on the row below
/// </summary>
ChangeRowDown,
/// <summary>
/// The key press will cancel the current edit
/// </summary>
CancelEdit,
/// <summary>
/// The key press will finish the current edit operation
/// </summary>
EndEdit,
/// <summary>
/// Custom verb that can be used for specialized actions.
/// </summary>
CustomVerb1,
/// <summary>
/// Custom verb that can be used for specialized actions.
/// </summary>
CustomVerb2,
/// <summary>
/// Custom verb that can be used for specialized actions.
/// </summary>
CustomVerb3,
/// <summary>
/// Custom verb that can be used for specialized actions.
/// </summary>
CustomVerb4,
/// <summary>
/// Custom verb that can be used for specialized actions.
/// </summary>
CustomVerb5,
/// <summary>
/// Custom verb that can be used for specialized actions.
/// </summary>
CustomVerb6,
/// <summary>
/// Custom verb that can be used for specialized actions.
/// </summary>
CustomVerb7,
/// <summary>
/// Custom verb that can be used for specialized actions.
/// </summary>
CustomVerb8,
/// <summary>
/// Custom verb that can be used for specialized actions.
/// </summary>
CustomVerb9,
/// <summary>
/// Custom verb that can be used for specialized actions.
/// </summary>
CustomVerb10,
};
/// <summary>
/// Instances of this class handle key presses during a cell edit operation.
/// </summary>
public class CellEditKeyEngine {
#region Public interface
/// <summary>
/// Sets the behaviour of a given key
/// </summary>
/// <param name="key"></param>
/// <param name="normalBehaviour"></param>
/// <param name="atEdgeBehaviour"></param>
public virtual void SetKeyBehaviour(Keys key, CellEditCharacterBehaviour normalBehaviour, CellEditAtEdgeBehaviour atEdgeBehaviour) {
this.CellEditKeyMap[key] = normalBehaviour;
this.CellEditKeyAtEdgeBehaviourMap[key] = atEdgeBehaviour;
}
/// <summary>
/// Handle a key press
/// </summary>
/// <param name="olv"></param>
/// <param name="keyData"></param>
/// <returns>True if the key was completely handled.</returns>
public virtual bool HandleKey(ObjectListView olv, Keys keyData) {
if (olv == null) throw new ArgumentNullException("olv");
CellEditCharacterBehaviour behaviour;
if (!CellEditKeyMap.TryGetValue(keyData, out behaviour))
return false;
this.ListView = olv;
switch (behaviour) {
case CellEditCharacterBehaviour.Ignore:
break;
case CellEditCharacterBehaviour.CancelEdit:
this.HandleCancelEdit();
break;
case CellEditCharacterBehaviour.EndEdit:
this.HandleEndEdit();
break;
case CellEditCharacterBehaviour.ChangeColumnLeft:
case CellEditCharacterBehaviour.ChangeColumnRight:
this.HandleColumnChange(keyData, behaviour);
break;
case CellEditCharacterBehaviour.ChangeRowDown:
case CellEditCharacterBehaviour.ChangeRowUp:
this.HandleRowChange(keyData, behaviour);
break;
default:
return this.HandleCustomVerb(keyData, behaviour);
};
return true;
}
#endregion
#region Implementation properties
/// <summary>
/// Gets or sets the ObjectListView on which the current key is being handled.
/// This cannot be null.
/// </summary>
protected ObjectListView ListView {
get { return listView; }
set { listView = value; }
}
private ObjectListView listView;
/// <summary>
/// Gets the row of the cell that is currently being edited
/// </summary>
protected OLVListItem ItemBeingEdited {
get {
return this.ListView.CellEditEventArgs.ListViewItem;
}
}
/// <summary>
/// Gets the index of the column of the cell that is being edited
/// </summary>
protected int SubItemIndexBeingEdited {
get {
return this.ListView.CellEditEventArgs.SubItemIndex;
}
}
/// <summary>
/// Gets or sets the map that remembers the normal behaviour of keys
/// </summary>
protected IDictionary<Keys, CellEditCharacterBehaviour> CellEditKeyMap {
get {
if (cellEditKeyMap == null)
this.InitializeCellEditKeyMaps();
return cellEditKeyMap;
}
set {
cellEditKeyMap = value;
}
}
private IDictionary<Keys, CellEditCharacterBehaviour> cellEditKeyMap;
/// <summary>
/// Gets or sets the map that remembers the desired behaviour of keys
/// on edge cases.
/// </summary>
protected IDictionary<Keys, CellEditAtEdgeBehaviour> CellEditKeyAtEdgeBehaviourMap {
get {
if (cellEditKeyAtEdgeBehaviourMap == null)
this.InitializeCellEditKeyMaps();
return cellEditKeyAtEdgeBehaviourMap;
}
set {
cellEditKeyAtEdgeBehaviourMap = value;
}
}
private IDictionary<Keys, CellEditAtEdgeBehaviour> cellEditKeyAtEdgeBehaviourMap;
#endregion
#region Initialization
/// <summary>
/// Setup the default key mapping
/// </summary>
protected virtual void InitializeCellEditKeyMaps() {
this.cellEditKeyMap = new Dictionary<Keys, CellEditCharacterBehaviour>();
this.cellEditKeyMap[Keys.Escape] = CellEditCharacterBehaviour.CancelEdit;
this.cellEditKeyMap[Keys.Return] = CellEditCharacterBehaviour.EndEdit;
this.cellEditKeyMap[Keys.Enter] = CellEditCharacterBehaviour.EndEdit;
this.cellEditKeyMap[Keys.Tab] = CellEditCharacterBehaviour.ChangeColumnRight;
this.cellEditKeyMap[Keys.Tab | Keys.Shift] = CellEditCharacterBehaviour.ChangeColumnLeft;
this.cellEditKeyMap[Keys.Left | Keys.Alt] = CellEditCharacterBehaviour.ChangeColumnLeft;
this.cellEditKeyMap[Keys.Right | Keys.Alt] = CellEditCharacterBehaviour.ChangeColumnRight;
this.cellEditKeyMap[Keys.Up | Keys.Alt] = CellEditCharacterBehaviour.ChangeRowUp;
this.cellEditKeyMap[Keys.Down | Keys.Alt] = CellEditCharacterBehaviour.ChangeRowDown;
this.cellEditKeyAtEdgeBehaviourMap = new Dictionary<Keys, CellEditAtEdgeBehaviour>();
this.cellEditKeyAtEdgeBehaviourMap[Keys.Tab] = CellEditAtEdgeBehaviour.Wrap;
this.cellEditKeyAtEdgeBehaviourMap[Keys.Tab | Keys.Shift] = CellEditAtEdgeBehaviour.Wrap;
this.cellEditKeyAtEdgeBehaviourMap[Keys.Left | Keys.Alt] = CellEditAtEdgeBehaviour.Wrap;
this.cellEditKeyAtEdgeBehaviourMap[Keys.Right | Keys.Alt] = CellEditAtEdgeBehaviour.Wrap;
this.cellEditKeyAtEdgeBehaviourMap[Keys.Up | Keys.Alt] = CellEditAtEdgeBehaviour.ChangeColumn;
this.cellEditKeyAtEdgeBehaviourMap[Keys.Down | Keys.Alt] = CellEditAtEdgeBehaviour.ChangeColumn;
}
#endregion
#region Command handling
/// <summary>
/// Handle the end edit command
/// </summary>
protected virtual void HandleEndEdit() {
this.ListView.PossibleFinishCellEditing();
}
/// <summary>
/// Handle the cancel edit command
/// </summary>
protected virtual void HandleCancelEdit() {
this.ListView.CancelCellEdit();
}
/// <summary>
/// Placeholder that subclasses can override to handle any custom verbs
/// </summary>
/// <param name="keyData"></param>
/// <param name="behaviour"></param>
/// <returns></returns>
protected virtual bool HandleCustomVerb(Keys keyData, CellEditCharacterBehaviour behaviour) {
return false;
}
/// <summary>
/// Handle a change row command
/// </summary>
/// <param name="keyData"></param>
/// <param name="behaviour"></param>
protected virtual void HandleRowChange(Keys keyData, CellEditCharacterBehaviour behaviour) {
// If we couldn't finish editing the current cell, don't try to move it
if (!this.ListView.PossibleFinishCellEditing())
return;
OLVListItem olvi = this.ItemBeingEdited;
int subItemIndex = this.SubItemIndexBeingEdited;
bool isGoingUp = behaviour == CellEditCharacterBehaviour.ChangeRowUp;
// Try to find a row above (or below) the currently edited cell
// If we find one, start editing it and we're done.
OLVListItem adjacentOlvi = this.GetAdjacentItemOrNull(olvi, isGoingUp);
if (adjacentOlvi != null) {
this.StartCellEditIfDifferent(adjacentOlvi, subItemIndex);
return;
}
// There is no adjacent row in the direction we want, so we must be on an edge.
CellEditAtEdgeBehaviour atEdgeBehaviour;
if (!this.CellEditKeyAtEdgeBehaviourMap.TryGetValue(keyData, out atEdgeBehaviour))
atEdgeBehaviour = CellEditAtEdgeBehaviour.Wrap;
switch (atEdgeBehaviour) {
case CellEditAtEdgeBehaviour.Ignore:
break;
case CellEditAtEdgeBehaviour.EndEdit:
this.ListView.PossibleFinishCellEditing();
break;
case CellEditAtEdgeBehaviour.Wrap:
adjacentOlvi = this.GetAdjacentItemOrNull(null, isGoingUp);
this.StartCellEditIfDifferent(adjacentOlvi, subItemIndex);
break;
case CellEditAtEdgeBehaviour.ChangeColumn:
// Figure out the next editable column
List<OLVColumn> editableColumnsInDisplayOrder = this.EditableColumnsInDisplayOrder;
int displayIndex = Math.Max(0, editableColumnsInDisplayOrder.IndexOf(this.ListView.GetColumn(subItemIndex)));
if (isGoingUp)
displayIndex = (editableColumnsInDisplayOrder.Count + displayIndex - 1) % editableColumnsInDisplayOrder.Count;
else
displayIndex = (displayIndex + 1) % editableColumnsInDisplayOrder.Count;
subItemIndex = editableColumnsInDisplayOrder[displayIndex].Index;
// Wrap to the next row and start the cell edit
adjacentOlvi = this.GetAdjacentItemOrNull(null, isGoingUp);
this.StartCellEditIfDifferent(adjacentOlvi, subItemIndex);
break;
}
}
/// <summary>
/// Handle a change column command
/// </summary>
/// <param name="keyData"></param>
/// <param name="behaviour"></param>
protected virtual void HandleColumnChange(Keys keyData, CellEditCharacterBehaviour behaviour)
{
// If we couldn't finish editing the current cell, don't try to move it
if (!this.ListView.PossibleFinishCellEditing())
return;
// Changing columns only works in details mode
if (this.ListView.View != View.Details)
return;
List<OLVColumn> editableColumns = this.EditableColumnsInDisplayOrder;
OLVListItem olvi = this.ItemBeingEdited;
int displayIndex = Math.Max(0,
editableColumns.IndexOf(this.ListView.GetColumn(this.SubItemIndexBeingEdited)));
bool isGoingLeft = behaviour == CellEditCharacterBehaviour.ChangeColumnLeft;
// Are we trying to continue past one of the edges?
if ((isGoingLeft && displayIndex == 0) ||
(!isGoingLeft && displayIndex == editableColumns.Count - 1))
{
// Yes, so figure out our at edge behaviour
CellEditAtEdgeBehaviour atEdgeBehaviour;
if (!this.CellEditKeyAtEdgeBehaviourMap.TryGetValue(keyData, out atEdgeBehaviour))
atEdgeBehaviour = CellEditAtEdgeBehaviour.Wrap;
switch (atEdgeBehaviour)
{
case CellEditAtEdgeBehaviour.Ignore:
return;
case CellEditAtEdgeBehaviour.EndEdit:
this.HandleEndEdit();
return;
case CellEditAtEdgeBehaviour.ChangeRow:
case CellEditAtEdgeBehaviour.Wrap:
if (atEdgeBehaviour == CellEditAtEdgeBehaviour.ChangeRow)
olvi = GetAdjacentItem(olvi, isGoingLeft && displayIndex == 0);
if (isGoingLeft)
displayIndex = editableColumns.Count - 1;
else
displayIndex = 0;
break;
}
}
else
{
if (isGoingLeft)
displayIndex -= 1;
else
displayIndex += 1;
}
int subItemIndex = editableColumns[displayIndex].Index;
this.StartCellEditIfDifferent(olvi, subItemIndex);
}
#endregion
#region Utilities
/// <summary>
/// Start editing the indicated cell if that cell is not already being edited
/// </summary>
/// <param name="olvi">The row to edit</param>
/// <param name="subItemIndex">The cell within that row to edit</param>
protected void StartCellEditIfDifferent(OLVListItem olvi, int subItemIndex) {
if (this.ItemBeingEdited == olvi && this.SubItemIndexBeingEdited == subItemIndex)
return;
this.ListView.EnsureVisible(olvi.Index);
this.ListView.StartCellEdit(olvi, subItemIndex);
}
/// <summary>
/// Gets the adjacent item to the given item in the given direction.
/// If that item is disabled, continue in that direction until an enabled item is found.
/// </summary>
/// <param name="olvi">The row whose neighbour is sought</param>
/// <param name="up">The direction of the adjacentness</param>
/// <returns>An OLVListView adjacent to the given item, or null if there are no more enabled items in that direction.</returns>
protected OLVListItem GetAdjacentItemOrNull(OLVListItem olvi, bool up) {
OLVListItem item = up ? this.ListView.GetPreviousItem(olvi) : this.ListView.GetNextItem(olvi);
while (item != null && !item.Enabled)
item = up ? this.ListView.GetPreviousItem(item) : this.ListView.GetNextItem(item);
return item;
}
/// <summary>
/// Gets the adjacent item to the given item in the given direction, wrapping if needed.
/// </summary>
/// <param name="olvi">The row whose neighbour is sought</param>
/// <param name="up">The direction of the adjacentness</param>
/// <returns>An OLVListView adjacent to the given item, or null if there are no more items in that direction.</returns>
protected OLVListItem GetAdjacentItem(OLVListItem olvi, bool up) {
return this.GetAdjacentItemOrNull(olvi, up) ?? this.GetAdjacentItemOrNull(null, up);
}
/// <summary>
/// Gets a collection of columns that are editable in the order they are shown to the user
/// </summary>
protected List<OLVColumn> EditableColumnsInDisplayOrder {
get {
List<OLVColumn> editableColumnsInDisplayOrder = new List<OLVColumn>();
foreach (OLVColumn x in this.ListView.ColumnsInDisplayOrder)
if (x.IsEditable)
editableColumnsInDisplayOrder.Add(x);
return editableColumnsInDisplayOrder;
}
}
#endregion
}
}

288
CellEditing/CellEditors.cs Normal file
View File

@ -0,0 +1,288 @@
/*
* CellEditors - Several slightly modified controls that are used as celleditors within ObjectListView.
*
* Author: Phillip Piper
* Date: 20/10/2008 5:15 PM
*
* Change log:
* v2.6
* 2012-08-02 JPP - Make most editors public so they can be reused/subclassed
* v2.3
* 2009-08-13 JPP - Standardized code formatting
* v2.2.1
* 2008-01-18 JPP - Added special handling for enums
* 2008-01-16 JPP - Added EditorRegistry
* v2.0.1
* 2008-10-20 JPP - Separated from ObjectListView.cs
*
* Copyright (C) 2006-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
namespace BrightIdeasSoftware
{
/// <summary>
/// These items allow combo boxes to remember a value and its description.
/// </summary>
public class ComboBoxItem
{
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <param name="description"></param>
public ComboBoxItem(Object key, String description) {
this.key = key;
this.description = description;
}
private readonly String description;
/// <summary>
///
/// </summary>
public Object Key {
get { return key; }
}
private readonly Object key;
/// <summary>
/// Returns a string that represents the current object.
/// </summary>
/// <returns>
/// A string that represents the current object.
/// </returns>
/// <filterpriority>2</filterpriority>
public override string ToString() {
return this.description;
}
}
//-----------------------------------------------------------------------
// Cell editors
// These classes are simple cell editors that make it easier to get and set
// the value that the control is showing.
// In many cases, you can intercept the CellEditStarting event to
// change the characteristics of the editor. For example, changing
// the acceptable range for a numeric editor or changing the strings
// that respresent true and false values for a boolean editor.
/// <summary>
/// This editor shows and auto completes values from the given listview column.
/// </summary>
[ToolboxItem(false)]
public class AutoCompleteCellEditor : ComboBox
{
/// <summary>
/// Create an AutoCompleteCellEditor
/// </summary>
/// <param name="lv"></param>
/// <param name="column"></param>
public AutoCompleteCellEditor(ObjectListView lv, OLVColumn column) {
this.DropDownStyle = ComboBoxStyle.DropDown;
Dictionary<String, bool> alreadySeen = new Dictionary<string, bool>();
for (int i = 0; i < Math.Min(lv.GetItemCount(), 1000); i++) {
String str = column.GetStringValue(lv.GetModelObject(i));
if (!alreadySeen.ContainsKey(str)) {
this.Items.Add(str);
alreadySeen[str] = true;
}
}
this.Sorted = true;
this.AutoCompleteSource = AutoCompleteSource.ListItems;
this.AutoCompleteMode = AutoCompleteMode.Append;
}
}
/// <summary>
/// This combo box is specialised to allow editing of an enum.
/// </summary>
[ToolboxItem(false)]
public class EnumCellEditor : ComboBox
{
/// <summary>
///
/// </summary>
/// <param name="type"></param>
public EnumCellEditor(Type type) {
this.DropDownStyle = ComboBoxStyle.DropDownList;
this.ValueMember = "Key";
ArrayList values = new ArrayList();
foreach (object value in Enum.GetValues(type))
values.Add(new ComboBoxItem(value, Enum.GetName(type, value)));
this.DataSource = values;
}
}
/// <summary>
/// This editor simply shows and edits integer values.
/// </summary>
[ToolboxItem(false)]
public class IntUpDown : NumericUpDown
{
/// <summary>
///
/// </summary>
public IntUpDown() {
this.DecimalPlaces = 0;
this.Minimum = -9999999;
this.Maximum = 9999999;
}
/// <summary>
/// Gets or sets the value shown by this editor
/// </summary>
new public int Value {
get { return Decimal.ToInt32(base.Value); }
set { base.Value = new Decimal(value); }
}
}
/// <summary>
/// This editor simply shows and edits unsigned integer values.
/// </summary>
/// <remarks>This class can't be made public because unsigned int is not a
/// CLS-compliant type. If you want to use, just copy the code to this class
/// into your project and use it from there.</remarks>
[ToolboxItem(false)]
internal class UintUpDown : NumericUpDown
{
public UintUpDown() {
this.DecimalPlaces = 0;
this.Minimum = 0;
this.Maximum = 9999999;
}
new public uint Value {
get { return Decimal.ToUInt32(base.Value); }
set { base.Value = new Decimal(value); }
}
}
/// <summary>
/// This editor simply shows and edits boolean values.
/// </summary>
[ToolboxItem(false)]
public class BooleanCellEditor : ComboBox
{
/// <summary>
///
/// </summary>
public BooleanCellEditor() {
this.DropDownStyle = ComboBoxStyle.DropDownList;
this.ValueMember = "Key";
ArrayList values = new ArrayList();
values.Add(new ComboBoxItem(false, "False"));
values.Add(new ComboBoxItem(true, "True"));
this.DataSource = values;
}
}
/// <summary>
/// This editor simply shows and edits boolean values using a checkbox
/// </summary>
[ToolboxItem(false)]
public class BooleanCellEditor2 : CheckBox
{
/// <summary>
/// Gets or sets the value shown by this editor
/// </summary>
public bool? Value {
get {
switch (this.CheckState) {
case CheckState.Checked: return true;
case CheckState.Indeterminate: return null;
case CheckState.Unchecked:
default: return false;
}
}
set {
if (value.HasValue)
this.CheckState = value.Value ? CheckState.Checked : CheckState.Unchecked;
else
this.CheckState = CheckState.Indeterminate;
}
}
/// <summary>
/// Gets or sets how the checkbox will be aligned
/// </summary>
public new HorizontalAlignment TextAlign {
get {
switch (this.CheckAlign) {
case ContentAlignment.MiddleRight: return HorizontalAlignment.Right;
case ContentAlignment.MiddleCenter: return HorizontalAlignment.Center;
case ContentAlignment.MiddleLeft:
default: return HorizontalAlignment.Left;
}
}
set {
switch (value) {
case HorizontalAlignment.Left:
this.CheckAlign = ContentAlignment.MiddleLeft;
break;
case HorizontalAlignment.Center:
this.CheckAlign = ContentAlignment.MiddleCenter;
break;
case HorizontalAlignment.Right:
this.CheckAlign = ContentAlignment.MiddleRight;
break;
}
}
}
}
/// <summary>
/// This editor simply shows and edits floating point values.
/// </summary>
/// <remarks>You can intercept the CellEditStarting event if you want
/// to change the characteristics of the editor. For example, by increasing
/// the number of decimal places.</remarks>
[ToolboxItem(false)]
public class FloatCellEditor : NumericUpDown
{
/// <summary>
///
/// </summary>
public FloatCellEditor() {
this.DecimalPlaces = 2;
this.Minimum = -9999999;
this.Maximum = 9999999;
}
/// <summary>
/// Gets or sets the value shown by this editor
/// </summary>
new public double Value {
get { return Convert.ToDouble(base.Value); }
set { base.Value = Convert.ToDecimal(value); }
}
}
}

View File

@ -0,0 +1,203 @@
/*
* EditorRegistry - A registry mapping types to cell editors.
*
* Author: Phillip Piper
* Date: 6-March-2011 7:53 am
*
* Change log:
* 2011-03-31 JPP - Use OLVColumn.DataType if the value to be edited is null
* 2011-03-06 JPP - Separated from CellEditors.cs
*
* Copyright (C) 2011-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace BrightIdeasSoftware {
/// <summary>
/// A delegate that creates an editor for the given value
/// </summary>
/// <param name="model">The model from which that value came</param>
/// <param name="column">The column for which the editor is being created</param>
/// <param name="value">A representative value of the type to be edited. This value may not be the exact
/// value for the column/model combination. It could be simply representative of
/// the appropriate type of value.</param>
/// <returns>A control which can edit the given value</returns>
public delegate Control EditorCreatorDelegate(Object model, OLVColumn column, Object value);
/// <summary>
/// An editor registry gives a way to decide what cell editor should be used to edit
/// the value of a cell. Programmers can register non-standard types and the control that
/// should be used to edit instances of that type.
/// </summary>
/// <remarks>
/// <para>All ObjectListViews share the same editor registry.</para>
/// </remarks>
public class EditorRegistry {
#region Initializing
/// <summary>
/// Create an EditorRegistry
/// </summary>
public EditorRegistry() {
this.InitializeStandardTypes();
}
private void InitializeStandardTypes() {
this.Register(typeof(Boolean), typeof(BooleanCellEditor));
this.Register(typeof(Int16), typeof(IntUpDown));
this.Register(typeof(Int32), typeof(IntUpDown));
this.Register(typeof(Int64), typeof(IntUpDown));
this.Register(typeof(UInt16), typeof(UintUpDown));
this.Register(typeof(UInt32), typeof(UintUpDown));
this.Register(typeof(UInt64), typeof(UintUpDown));
this.Register(typeof(Single), typeof(FloatCellEditor));
this.Register(typeof(Double), typeof(FloatCellEditor));
this.Register(typeof(DateTime), delegate(Object model, OLVColumn column, Object value) {
DateTimePicker c = new DateTimePicker();
c.Format = DateTimePickerFormat.Short;
return c;
});
this.Register(typeof(Boolean), delegate(Object model, OLVColumn column, Object value) {
CheckBox c = new BooleanCellEditor2();
c.ThreeState = column.TriStateCheckBoxes;
return c;
});
}
#endregion
#region Registering
/// <summary>
/// Register that values of 'type' should be edited by instances of 'controlType'.
/// </summary>
/// <param name="type">The type of value to be edited</param>
/// <param name="controlType">The type of the Control that will edit values of 'type'</param>
/// <example>
/// ObjectListView.EditorRegistry.Register(typeof(Color), typeof(MySpecialColorEditor));
/// </example>
public void Register(Type type, Type controlType) {
this.Register(type, delegate(Object model, OLVColumn column, Object value) {
return controlType.InvokeMember("", BindingFlags.CreateInstance, null, null, null) as Control;
});
}
/// <summary>
/// Register the given delegate so that it is called to create editors
/// for values of the given type
/// </summary>
/// <param name="type">The type of value to be edited</param>
/// <param name="creator">The delegate that will create a control that can edit values of 'type'</param>
/// <example>
/// ObjectListView.EditorRegistry.Register(typeof(Color), CreateColorEditor);
/// ...
/// public Control CreateColorEditor(Object model, OLVColumn column, Object value)
/// {
/// return new MySpecialColorEditor();
/// }
/// </example>
public void Register(Type type, EditorCreatorDelegate creator) {
this.creatorMap[type] = creator;
}
/// <summary>
/// Register a delegate that will be called to create an editor for values
/// that have not been handled.
/// </summary>
/// <param name="creator">The delegate that will create a editor for all other types</param>
public void RegisterDefault(EditorCreatorDelegate creator) {
this.defaultCreator = creator;
}
/// <summary>
/// Register a delegate that will be given a chance to create a control
/// before any other option is considered.
/// </summary>
/// <param name="creator">The delegate that will create a control</param>
public void RegisterFirstChance(EditorCreatorDelegate creator) {
this.firstChanceCreator = creator;
}
#endregion
#region Accessing
/// <summary>
/// Create and return an editor that is appropriate for the given value.
/// Return null if no appropriate editor can be found.
/// </summary>
/// <param name="model">The model involved</param>
/// <param name="column">The column to be edited</param>
/// <param name="value">The value to be edited. This value may not be the exact
/// value for the column/model combination. It could be simply representative of
/// the appropriate type of value.</param>
/// <returns>A Control that can edit the given type of values</returns>
public Control GetEditor(Object model, OLVColumn column, Object value) {
Control editor;
// Give the first chance delegate a chance to decide
if (this.firstChanceCreator != null) {
editor = this.firstChanceCreator(model, column, value);
if (editor != null)
return editor;
}
// Try to find a creator based on the type of the value (or the column)
Type type = value == null ? column.DataType : value.GetType();
if (type != null && this.creatorMap.ContainsKey(type)) {
editor = this.creatorMap[type](model, column, value);
if (editor != null)
return editor;
}
// Enums without other processing get a special editor
if (value != null && value.GetType().IsEnum)
return this.CreateEnumEditor(value.GetType());
// Give any default creator a final chance
if (this.defaultCreator != null)
return this.defaultCreator(model, column, value);
return null;
}
/// <summary>
/// Create and return an editor that will edit values of the given type
/// </summary>
/// <param name="type">A enum type</param>
protected Control CreateEnumEditor(Type type) {
return new EnumCellEditor(type);
}
#endregion
#region Private variables
private EditorCreatorDelegate firstChanceCreator;
private EditorCreatorDelegate defaultCreator;
private Dictionary<Type, EditorCreatorDelegate> creatorMap = new Dictionary<Type, EditorCreatorDelegate>();
#endregion
}
}

46
CustomDictionary.xml Normal file
View File

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<Dictionary>
<Words>
<Recognized>
<Word>br</Word>
<Word>Canceled</Word>
<Word>Center</Word>
<Word>Color</Word>
<Word>Colors</Word>
<Word>f</Word>
<Word>fmt</Word>
<Word>g</Word>
<Word>gdi</Word>
<Word>hti</Word>
<Word>i</Word>
<Word>lightbox</Word>
<Word>lv</Word>
<Word>lvi</Word>
<Word>lvsi</Word>
<Word>m</Word>
<Word>multi</Word>
<Word>Munger</Word>
<Word>n</Word>
<Word>olv</Word>
<Word>olvi</Word>
<Word>p</Word>
<Word>parms</Word>
<Word>r</Word>
<Word>Renderer</Word>
<Word>s</Word>
<Word>SubItem</Word>
<Word>Unapply</Word>
<Word>Unpause</Word>
<Word>x</Word>
<Word>y</Word>
</Recognized>
<Deprecated>
<Term PreferredAlternate="EnterpriseServices">ComPlus</Term>
</Deprecated>
</Words>
<Acronyms>
<CasingExceptions>
<Acronym>OLV</Acronym>
</CasingExceptions>
</Acronyms>
</Dictionary>

199
DataListView.cs Normal file
View File

@ -0,0 +1,199 @@
/*
* DataListView - A data-bindable listview
*
* Author: Phillip Piper
* Date: 27/09/2008 9:15 AM
*
* Change log:
* 2011-02-27 JPP - Moved most of the logic to DataSourceAdapter (where it
* can be used by FastDataListView too)
* v2.3
* 2009-01-18 JPP - Boolean columns are now handled as checkboxes
* - Auto-generated columns would fail if the data source was
* reseated, even to the same data source
* v2.0.1
* 2009-01-07 JPP - Made all public and protected methods virtual
* 2008-10-03 JPP - Separated from ObjectListView.cs
*
* Copyright (C) 2006-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing.Design;
using System.Windows.Forms;
namespace BrightIdeasSoftware
{
/// <summary>
/// A DataListView is a ListView that can be bound to a datasource (which would normally be a DataTable or DataView).
/// </summary>
/// <remarks>
/// <para>This listview keeps itself in sync with its source datatable by listening for change events.</para>
/// <para>The DataListView will automatically create columns to show all of the data source's columns/properties, if there is not already
/// a column showing that property. This allows you to define one or two columns in the designer and then have the others generated automatically.
/// If you don't want any column to be auto generated, set <see cref="AutoGenerateColumns"/> to false.
/// These generated columns will be only the simplest view of the world, and would look more interesting with a few delegates installed.</para>
/// <para>This listview will also automatically generate missing aspect getters to fetch the values from the data view.</para>
/// <para>Changing data sources is possible, but error prone. Before changing data sources, the programmer is responsible for modifying/resetting
/// the column collection to be valid for the new data source.</para>
/// <para>Internally, a CurrencyManager controls keeping the data source in-sync with other users of the data source (as per normal .NET
/// behavior). This means that the model objects in the DataListView are DataRowView objects. If you write your own AspectGetters/Setters,
/// they will be given DataRowView objects.</para>
/// </remarks>
public class DataListView : ObjectListView
{
/// <summary>
/// Make a DataListView
/// </summary>
public DataListView()
{
this.Adapter = new DataSourceAdapter(this);
}
#region Public Properties
/// <summary>
/// Gets or sets whether or not columns will be automatically generated to show the
/// columns when the DataSource is set.
/// </summary>
/// <remarks>This must be set before the DataSource is set. It has no effect afterwards.</remarks>
[Category("Data"),
Description("Should the control automatically generate columns from the DataSource"),
DefaultValue(true)]
public bool AutoGenerateColumns {
get { return this.Adapter.AutoGenerateColumns; }
set { this.Adapter.AutoGenerateColumns = value; }
}
/// <summary>
/// Get or set the DataSource that will be displayed in this list view.
/// </summary>
/// <remarks>The DataSource should implement either <see cref="IList"/>, <see cref="IBindingList"/>,
/// or <see cref="IListSource"/>. Some common examples are the following types of objects:
/// <list type="unordered">
/// <item><description><see cref="DataView"/></description></item>
/// <item><description><see cref="DataTable"/></description></item>
/// <item><description><see cref="DataSet"/></description></item>
/// <item><description><see cref="DataViewManager"/></description></item>
/// <item><description><see cref="BindingSource"/></description></item>
/// </list>
/// <para>When binding to a list container (i.e. one that implements the
/// <see cref="IListSource"/> interface, such as <see cref="DataSet"/>)
/// you must also set the <see cref="DataMember"/> property in order
/// to identify which particular list you would like to display. You
/// may also set the <see cref="DataMember"/> property even when
/// DataSource refers to a list, since <see cref="DataMember"/> can
/// also be used to navigate relations between lists.</para>
/// <para>When a DataSource is set, the control will create OLVColumns to show any
/// data source columns that are not already shown.</para>
/// <para>If the DataSource is changed, you will have to remove any previously
/// created columns, since they will be configured for the previous DataSource.
/// <see cref="ObjectListView.Reset()"/>.</para>
/// </remarks>
[Category("Data"),
TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design")]
public virtual Object DataSource
{
get { return this.Adapter.DataSource; }
set { this.Adapter.DataSource = value; }
}
/// <summary>
/// Gets or sets the name of the list or table in the data source for which the DataListView is displaying data.
/// </summary>
/// <remarks>If the data source is not a DataSet or DataViewManager, this property has no effect</remarks>
[Category("Data"),
Editor("System.Windows.Forms.Design.DataMemberListEditor, System.Design", typeof(UITypeEditor)),
DefaultValue("")]
public virtual string DataMember
{
get { return this.Adapter.DataMember; }
set { this.Adapter.DataMember = value; }
}
#endregion
#region Implementation properties
/// <summary>
/// Gets or sets the DataSourceAdaptor that does the bulk of the work needed
/// for data binding.
/// </summary>
protected DataSourceAdapter Adapter {
get {
Debug.Assert(adapter != null, "Data adapter should not be null");
return adapter;
}
set { adapter = value; }
}
private DataSourceAdapter adapter;
#endregion
#region Object manipulations
/// <summary>
/// Add the given collection of model objects to this control.
/// </summary>
/// <param name="modelObjects">A collection of model objects</param>
/// <remarks>This is a no-op for data lists, since the data
/// is controlled by the VirtualListDataSource. Manipulate the data source
/// rather than this view of the data source.</remarks>
public override void AddObjects(ICollection modelObjects)
{
}
/// <summary>
/// Remove the given collection of model objects from this control.
/// </summary>
/// <remarks>This is a no-op for data lists, since the data
/// is controlled by the VirtualListDataSource. Manipulate the data source
/// rather than this view of the data source.</remarks>
public override void RemoveObjects(ICollection modelObjects)
{
}
#endregion
#region Event Handlers
/// <summary>
/// Handles parent binding context changes
/// </summary>
/// <param name="e">Unused EventArgs.</param>
protected override void OnParentBindingContextChanged(EventArgs e)
{
base.OnParentBindingContextChanged(e);
// BindingContext is an ambient property - by default it simply picks
// up the parent control's context (unless something has explicitly
// given us our own). So we must respond to changes in our parent's
// binding context in the same way we would changes to our own
// binding context.
// THINK: Do we need to forward this to the adapter?
}
#endregion
}
}

226
DataTreeListView.cs Normal file
View File

@ -0,0 +1,226 @@
/*
* DataTreeListView - A data bindable TreeListView
*
* Author: Phillip Piper
* Date: 05/05/2012 3:26 PM
*
* Change log:
* 2012-05-05 JPP Initial version
*
* TO DO:
*
* Copyright (C) 2012 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing.Design;
using System.Windows.Forms;
namespace BrightIdeasSoftware
{
/// <summary>
/// A DataTreeListView is a TreeListView that calculates its hierarchy based on
/// information in the data source.
/// </summary>
/// <remarks>
/// <para>Like a <see cref="DataListView"/>, a DataTreeListView sources all its information
/// from a combination of <see cref="DataSource"/> and <see cref="DataMember"/>.
/// <see cref="DataSource"/> can be a DataTable, DataSet,
/// or anything that implements <see cref="IList"/>.
/// </para>
/// <para>
/// To function properly, the DataTreeListView requires:
/// <list type="bullet">
/// <item>the table to have a column which holds a unique for the row. The name of this column must be set in <see cref="KeyAspectName"/>.</item>
/// <item>the table to have a column which holds id of the hierarchical parent of the row. The name of this column must be set in <see cref="ParentKeyAspectName"/>.</item>
/// <item>a value which identifies which rows are the roots of the tree (<see cref="RootKeyValue"/>).</item>
/// </list>
/// The hierarchy structure is determined finding all the rows where the parent key is equal to <see cref="RootKeyValue"/>. These rows
/// become the root objects of the hierarchy.
/// </para>
/// <para>Like a TreeListView, the hierarchy must not contain cycles. Bad things will happen if the data is cyclic.</para>
/// </remarks>
public partial class DataTreeListView : TreeListView
{
#region Public Properties
/// <summary>
/// Get or set the DataSource that will be displayed in this list view.
/// </summary>
/// <remarks>The DataSource should implement either <see cref="IList"/>, <see cref="IBindingList"/>,
/// or <see cref="IListSource"/>. Some common examples are the following types of objects:
/// <list type="unordered">
/// <item><description><see cref="DataView"/></description></item>
/// <item><description><see cref="DataTable"/></description></item>
/// <item><description><see cref="DataSet"/></description></item>
/// <item><description><see cref="DataViewManager"/></description></item>
/// <item><description><see cref="BindingSource"/></description></item>
/// </list>
/// <para>When binding to a list container (i.e. one that implements the
/// <see cref="IListSource"/> interface, such as <see cref="DataSet"/>)
/// you must also set the <see cref="DataMember"/> property in order
/// to identify which particular list you would like to display. You
/// may also set the <see cref="DataMember"/> property even when
/// DataSource refers to a list, since <see cref="DataMember"/> can
/// also be used to navigate relations between lists.</para>
/// </remarks>
[Category("Data"),
TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design")]
public virtual Object DataSource {
get { return this.Adapter.DataSource; }
set { this.Adapter.DataSource = value; }
}
/// <summary>
/// Gets or sets the name of the list or table in the data source for which the DataListView is displaying data.
/// </summary>
/// <remarks>If the data source is not a DataSet or DataViewManager, this property has no effect</remarks>
[Category("Data"),
Editor("System.Windows.Forms.Design.DataMemberListEditor, System.Design", typeof(UITypeEditor)),
DefaultValue("")]
public virtual string DataMember {
get { return this.Adapter.DataMember; }
set { this.Adapter.DataMember = value; }
}
/// <summary>
/// Gets or sets the name of the property/column that uniquely identifies each row.
/// </summary>
/// <remarks>
/// <para>
/// The value contained by this column must be unique across all rows
/// in the data source. Odd and unpredictable things will happen if two
/// rows have the same id.
/// </para>
/// <para>Null cannot be a valid key value.</para>
/// </remarks>
[Category("Data"),
Description("The name of the property/column that holds the key of a row"),
DefaultValue(null)]
public virtual string KeyAspectName {
get { return this.Adapter.KeyAspectName; }
set { this.Adapter.KeyAspectName = value; }
}
/// <summary>
/// Gets or sets the name of the property/column that contains the key of
/// the parent of a row.
/// </summary>
/// <remarks>
/// <para>
/// The test condition for deciding if one row is the parent of another is functionally
/// equivilent to this:
/// <code>
/// Object.Equals(candidateParentRow[this.KeyAspectName], row[this.ParentKeyAspectName])
/// </code>
/// </para>
/// <para>Unlike key value, parent keys can be null but a null parent key can only be used
/// to identify root objects.</para>
/// </remarks>
[Category("Data"),
Description("The name of the property/column that holds the key of the parent of a row"),
DefaultValue(null)]
public virtual string ParentKeyAspectName {
get { return this.Adapter.ParentKeyAspectName; }
set { this.Adapter.ParentKeyAspectName = value; }
}
/// <summary>
/// Gets or sets the value that identifies a row as a root object.
/// When the ParentKey of a row equals the RootKeyValue, that row will
/// be treated as root of the TreeListView.
/// </summary>
/// <remarks>
/// <para>
/// The test condition for deciding a root object is functionally
/// equivilent to this:
/// <code>
/// Object.Equals(candidateRow[this.ParentKeyAspectName], this.RootKeyValue)
/// </code>
/// </para>
/// <para>The RootKeyValue can be null. Actually, it can be any value that can
/// be compared for equality against a basic type.</para>
/// <para>If this is set to the wrong value (i.e. to a value that no row
/// has in the parent id column), the list will be empty.</para>
/// </remarks>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public virtual object RootKeyValue {
get { return this.Adapter.RootKeyValue; }
set { this.Adapter.RootKeyValue = value; }
}
/// <summary>
/// Gets or sets the value that identifies a row as a root object.
/// <see cref="RootKeyValue"/>. The RootKeyValue can be of any type,
/// but the IDE cannot sensibly represent a value of any type,
/// so this is a typed wrapper around that property.
/// </summary>
/// <remarks>
/// If you want the root value to be something other than a string,
/// you will have set it yourself.
/// </remarks>
[Category("Data"),
Description("The parent id value that identifies a row as a root object"),
DefaultValue(null)]
public virtual string RootKeyValueString {
get { return Convert.ToString(this.Adapter.RootKeyValue); }
set { this.Adapter.RootKeyValue = value; }
}
/// <summary>
/// Gets or sets whether or not the key columns (id and parent id) should
/// be shown to the user.
/// </summary>
/// <remarks>This must be set before the DataSource is set. It has no effect
/// afterwards.</remarks>
[Category("Data"),
Description("Should the keys columns (id and parent id) be shown to the user?"),
DefaultValue(true)]
public virtual bool ShowKeyColumns {
get { return this.Adapter.ShowKeyColumns; }
set { this.Adapter.ShowKeyColumns = value; }
}
#endregion
#region Implementation properties
/// <summary>
/// Gets or sets the DataSourceAdaptor that does the bulk of the work needed
/// for data binding.
/// </summary>
protected TreeDataSourceAdapter Adapter {
get {
if (this.adapter == null)
this.adapter = new TreeDataSourceAdapter(this);
return adapter;
}
set { adapter = value; }
}
private TreeDataSourceAdapter adapter;
#endregion
}
}

219
DragDrop/DragSource.cs Normal file
View File

@ -0,0 +1,219 @@
/*
* DragSource.cs - Add drag source functionality to an ObjectListView
*
* Author: Phillip Piper
* Date: 2009-03-17 5:15 PM
*
* Change log:
* 2011-03-29 JPP - Separate OLVDataObject.cs
* v2.3
* 2009-07-06 JPP - Make sure Link is acceptable as an drop effect by default
* (since MS didn't make it part of the 'All' value)
* v2.2
* 2009-04-15 JPP - Separated DragSource.cs into DropSink.cs
* 2009-03-17 JPP - Initial version
*
* Copyright (C) 2009-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace BrightIdeasSoftware
{
/// <summary>
/// An IDragSource controls how drag out from the ObjectListView will behave
/// </summary>
public interface IDragSource
{
/// <summary>
/// A drag operation is beginning. Return the data object that will be used
/// for data transfer. Return null to prevent the drag from starting. The data
/// object will normally include all the selected objects.
/// </summary>
/// <remarks>
/// The returned object is later passed to the GetAllowedEffect() and EndDrag()
/// methods.
/// </remarks>
/// <param name="olv">What ObjectListView is being dragged from.</param>
/// <param name="button">Which mouse button is down?</param>
/// <param name="item">What item was directly dragged by the user? There may be more than just this
/// item selected.</param>
/// <returns>The data object that will be used for data transfer. This will often be a subclass
/// of DataObject, but does not need to be.</returns>
Object StartDrag(ObjectListView olv, MouseButtons button, OLVListItem item);
/// <summary>
/// What operations are possible for this drag? This controls the icon shown during the drag
/// </summary>
/// <param name="dragObject">The data object returned by StartDrag()</param>
/// <returns>A combination of DragDropEffects flags</returns>
DragDropEffects GetAllowedEffects(Object dragObject);
/// <summary>
/// The drag operation is complete. Do whatever is necessary to complete the action.
/// </summary>
/// <param name="dragObject">The data object returned by StartDrag()</param>
/// <param name="effect">The value returned from GetAllowedEffects()</param>
void EndDrag(Object dragObject, DragDropEffects effect);
}
/// <summary>
/// A do-nothing implementation of IDragSource that can be safely subclassed.
/// </summary>
public class AbstractDragSource : IDragSource
{
#region IDragSource Members
/// <summary>
/// See IDragSource documentation
/// </summary>
/// <param name="olv"></param>
/// <param name="button"></param>
/// <param name="item"></param>
/// <returns></returns>
public virtual Object StartDrag(ObjectListView olv, MouseButtons button, OLVListItem item) {
return null;
}
/// <summary>
/// See IDragSource documentation
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public virtual DragDropEffects GetAllowedEffects(Object data) {
return DragDropEffects.None;
}
/// <summary>
/// See IDragSource documentation
/// </summary>
/// <param name="dragObject"></param>
/// <param name="effect"></param>
public virtual void EndDrag(Object dragObject, DragDropEffects effect) {
}
#endregion
}
/// <summary>
/// A reasonable implementation of IDragSource that provides normal
/// drag source functionality. It creates a data object that supports
/// inter-application dragging of text and HTML representation of
/// the dragged rows. It can optionally force a refresh of all dragged
/// rows when the drag is complete.
/// </summary>
/// <remarks>Subclasses can override GetDataObject() to add new
/// data formats to the data transfer object.</remarks>
public class SimpleDragSource : IDragSource
{
#region Constructors
/// <summary>
/// Construct a SimpleDragSource
/// </summary>
public SimpleDragSource() {
}
/// <summary>
/// Construct a SimpleDragSource that refreshes the dragged rows when
/// the drag is complete
/// </summary>
/// <param name="refreshAfterDrop"></param>
public SimpleDragSource(bool refreshAfterDrop) {
this.RefreshAfterDrop = refreshAfterDrop;
}
#endregion
#region Public properties
/// <summary>
/// Gets or sets whether the dragged rows should be refreshed when the
/// drag operation is complete.
/// </summary>
public bool RefreshAfterDrop {
get { return refreshAfterDrop; }
set { refreshAfterDrop = value; }
}
private bool refreshAfterDrop;
#endregion
#region IDragSource Members
/// <summary>
/// Create a DataObject when the user does a left mouse drag operation.
/// See IDragSource for further information.
/// </summary>
/// <param name="olv"></param>
/// <param name="button"></param>
/// <param name="item"></param>
/// <returns></returns>
public virtual Object StartDrag(ObjectListView olv, MouseButtons button, OLVListItem item) {
// We only drag on left mouse
if (button != MouseButtons.Left)
return null;
return this.CreateDataObject(olv);
}
/// <summary>
/// Which operations are allowed in the operation? By default, all operations are supported.
/// </summary>
/// <param name="data"></param>
/// <returns>All opertions are supported</returns>
public virtual DragDropEffects GetAllowedEffects(Object data) {
return DragDropEffects.All | DragDropEffects.Link; // why didn't MS include 'Link' in 'All'??
}
/// <summary>
/// The drag operation is finished. Refreshe the dragged rows if so configured.
/// </summary>
/// <param name="dragObject"></param>
/// <param name="effect"></param>
public virtual void EndDrag(Object dragObject, DragDropEffects effect) {
OLVDataObject data = dragObject as OLVDataObject;
if (data == null)
return;
if (this.RefreshAfterDrop)
data.ListView.RefreshObjects(data.ModelObjects);
}
/// <summary>
/// Create a data object that will be used to as the data object
/// for the drag operation.
/// </summary>
/// <remarks>
/// Subclasses can override this method add new formats to the data object.
/// </remarks>
/// <param name="olv">The ObjectListView that is the source of the drag</param>
/// <returns>A data object for the drag</returns>
protected virtual object CreateDataObject(ObjectListView olv) {
return new OLVDataObject(olv);
}
#endregion
}
}

1435
DragDrop/DropSink.cs Normal file

File diff suppressed because it is too large Load Diff

185
DragDrop/OLVDataObject.cs Normal file
View File

@ -0,0 +1,185 @@
/*
* OLVDataObject.cs - An OLE DataObject that knows how to convert rows of an OLV to text and HTML
*
* Author: Phillip Piper
* Date: 2011-03-29 3:34PM
*
* Change log:
* v2.8
* 2014-05-02 JPP - When the listview is completely empty, don't try to set CSV text in the clipboard.
* v2.6
* 2012-08-08 JPP - Changed to use OLVExporter.
* - Added CSV to formats exported to Clipboard
* v2.4
* 2011-03-29 JPP - Initial version
*
* Copyright (C) 2011-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.Windows.Forms;
namespace BrightIdeasSoftware {
/// <summary>
/// A data transfer object that knows how to transform a list of model
/// objects into a text and HTML representation.
/// </summary>
public class OLVDataObject : DataObject {
#region Life and death
/// <summary>
/// Create a data object from the selected objects in the given ObjectListView
/// </summary>
/// <param name="olv">The source of the data object</param>
public OLVDataObject(ObjectListView olv)
: this(olv, olv.SelectedObjects) {
}
/// <summary>
/// Create a data object which operates on the given model objects
/// in the given ObjectListView
/// </summary>
/// <param name="olv">The source of the data object</param>
/// <param name="modelObjects">The model objects to be put into the data object</param>
public OLVDataObject(ObjectListView olv, IList modelObjects) {
this.objectListView = olv;
this.modelObjects = modelObjects;
this.includeHiddenColumns = olv.IncludeHiddenColumnsInDataTransfer;
this.includeColumnHeaders = olv.IncludeColumnHeadersInCopy;
this.CreateTextFormats();
}
#endregion
#region Properties
/// <summary>
/// Gets or sets whether hidden columns will also be included in the text
/// and HTML representation. If this is false, only visible columns will
/// be included.
/// </summary>
public bool IncludeHiddenColumns {
get { return includeHiddenColumns; }
}
private readonly bool includeHiddenColumns;
/// <summary>
/// Gets or sets whether column headers will also be included in the text
/// and HTML representation.
/// </summary>
public bool IncludeColumnHeaders {
get { return includeColumnHeaders; }
}
private readonly bool includeColumnHeaders;
/// <summary>
/// Gets the ObjectListView that is being used as the source of the data
/// </summary>
public ObjectListView ListView {
get { return objectListView; }
}
private readonly ObjectListView objectListView;
/// <summary>
/// Gets the model objects that are to be placed in the data object
/// </summary>
public IList ModelObjects {
get { return modelObjects; }
}
private readonly IList modelObjects;
#endregion
/// <summary>
/// Put a text and HTML representation of our model objects
/// into the data object.
/// </summary>
public void CreateTextFormats() {
OLVExporter exporter = this.CreateExporter();
// Put both the text and html versions onto the clipboard.
// For some reason, SetText() with UnicodeText doesn't set the basic CF_TEXT format,
// but using SetData() does.
//this.SetText(sbText.ToString(), TextDataFormat.UnicodeText);
this.SetData(exporter.ExportTo(OLVExporter.ExportFormat.TabSeparated));
string exportTo = exporter.ExportTo(OLVExporter.ExportFormat.CSV);
if (!String.IsNullOrEmpty(exportTo))
this.SetText(exportTo, TextDataFormat.CommaSeparatedValue);
this.SetText(ConvertToHtmlFragment(exporter.ExportTo(OLVExporter.ExportFormat.HTML)), TextDataFormat.Html);
}
/// <summary>
/// Create an exporter for the data contained in this object
/// </summary>
/// <returns></returns>
protected OLVExporter CreateExporter() {
OLVExporter exporter = new OLVExporter(this.ListView);
exporter.IncludeColumnHeaders = this.IncludeColumnHeaders;
exporter.IncludeHiddenColumns = this.IncludeHiddenColumns;
exporter.ModelObjects = this.ModelObjects;
return exporter;
}
/// <summary>
/// Make a HTML representation of our model objects
/// </summary>
[Obsolete("Use OLVExporter directly instead", false)]
public string CreateHtml() {
OLVExporter exporter = this.CreateExporter();
return exporter.ExportTo(OLVExporter.ExportFormat.HTML);
}
/// <summary>
/// Convert the fragment of HTML into the Clipboards HTML format.
/// </summary>
/// <remarks>The HTML format is found here http://msdn2.microsoft.com/en-us/library/aa767917.aspx
/// </remarks>
/// <param name="fragment">The HTML to put onto the clipboard. It must be valid HTML!</param>
/// <returns>A string that can be put onto the clipboard and will be recognized as HTML</returns>
private string ConvertToHtmlFragment(string fragment) {
// Minimal implementation of HTML clipboard format
const string SOURCE = "http://www.codeproject.com/Articles/16009/A-Much-Easier-to-Use-ListView";
const String MARKER_BLOCK =
"Version:1.0\r\n" +
"StartHTML:{0,8}\r\n" +
"EndHTML:{1,8}\r\n" +
"StartFragment:{2,8}\r\n" +
"EndFragment:{3,8}\r\n" +
"StartSelection:{2,8}\r\n" +
"EndSelection:{3,8}\r\n" +
"SourceURL:{4}\r\n" +
"{5}";
int prefixLength = String.Format(MARKER_BLOCK, 0, 0, 0, 0, SOURCE, "").Length;
const String DEFAULT_HTML_BODY =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">" +
"<HTML><HEAD></HEAD><BODY><!--StartFragment-->{0}<!--EndFragment--></BODY></HTML>";
string html = String.Format(DEFAULT_HTML_BODY, fragment);
int startFragment = prefixLength + html.IndexOf(fragment, StringComparison.Ordinal);
int endFragment = startFragment + fragment.Length;
return String.Format(MARKER_BLOCK, prefixLength, prefixLength + html.Length, startFragment, endFragment, SOURCE, html);
}
}
}

140
FastDataListView.cs Normal file
View File

@ -0,0 +1,140 @@
/*
* FastDataListView - A data bindable listview that has the speed of a virtual list
*
* Author: Phillip Piper
* Date: 22/09/2010 8:11 AM
*
* Change log:
* 2010-09-22 JPP - Initial version
*
* Copyright (C) 2006-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Design;
namespace BrightIdeasSoftware
{
/// <summary>
/// A FastDataListView virtualizes the display of data from a DataSource. It operates on
/// DataSets and DataTables in the same way as a DataListView, but does so much more efficiently.
/// </summary>
/// <remarks>
/// <para>
/// A FastDataListView still has to load all its data from the DataSource. If you have SQL statement
/// that returns 1 million rows, all 1 million rows will still need to read from the database.
/// However, once the rows are loaded, the FastDataListView will only build rows as they are displayed.
/// </para>
/// </remarks>
public class FastDataListView : FastObjectListView
{
#region Public Properties
/// <summary>
/// Gets or sets whether or not columns will be automatically generated to show the
/// columns when the DataSource is set.
/// </summary>
/// <remarks>This must be set before the DataSource is set. It has no effect afterwards.</remarks>
[Category("Data"),
Description("Should the control automatically generate columns from the DataSource"),
DefaultValue(true)]
public bool AutoGenerateColumns
{
get { return this.Adapter.AutoGenerateColumns; }
set { this.Adapter.AutoGenerateColumns = value; }
}
/// <summary>
/// Get or set the VirtualListDataSource that will be displayed in this list view.
/// </summary>
/// <remarks>The VirtualListDataSource should implement either <see cref="IList"/>, <see cref="IBindingList"/>,
/// or <see cref="IListSource"/>. Some common examples are the following types of objects:
/// <list type="unordered">
/// <item><description><see cref="DataView"/></description></item>
/// <item><description><see cref="DataTable"/></description></item>
/// <item><description><see cref="DataSet"/></description></item>
/// <item><description><see cref="DataViewManager"/></description></item>
/// <item><description><see cref="BindingSource"/></description></item>
/// </list>
/// <para>When binding to a list container (i.e. one that implements the
/// <see cref="IListSource"/> interface, such as <see cref="DataSet"/>)
/// you must also set the <see cref="DataMember"/> property in order
/// to identify which particular list you would like to display. You
/// may also set the <see cref="DataMember"/> property even when
/// VirtualListDataSource refers to a list, since <see cref="DataMember"/> can
/// also be used to navigate relations between lists.</para>
/// </remarks>
[Category("Data"),
TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design")]
public virtual Object DataSource {
get { return this.Adapter.DataSource; }
set { this.Adapter.DataSource = value; }
}
/// <summary>
/// Gets or sets the name of the list or table in the data source for which the DataListView is displaying data.
/// </summary>
/// <remarks>If the data source is not a DataSet or DataViewManager, this property has no effect</remarks>
[Category("Data"),
Editor("System.Windows.Forms.Design.DataMemberListEditor, System.Design", typeof(UITypeEditor)),
DefaultValue("")]
public virtual string DataMember {
get { return this.Adapter.DataMember; }
set { this.Adapter.DataMember = value; }
}
#endregion
#region Implementation properties
/// <summary>
/// Gets or sets the DataSourceAdaptor that does the bulk of the work needed
/// for data binding.
/// </summary>
protected DataSourceAdapter Adapter {
get {
if (adapter == null)
adapter = this.CreateDataSourceAdapter();
return adapter;
}
set { adapter = value; }
}
private DataSourceAdapter adapter;
#endregion
#region Implementation
/// <summary>
/// Create the DataSourceAdapter that this control will use.
/// </summary>
/// <returns>A DataSourceAdapter configured for this list</returns>
/// <remarks>Subclasses should overrride this to create their
/// own specialized adapters</remarks>
protected virtual DataSourceAdapter CreateDataSourceAdapter() {
return new DataSourceAdapter(this);
}
#endregion
}
}

360
FastObjectListView.cs Normal file
View File

@ -0,0 +1,360 @@
/*
* FastObjectListView - A listview that behaves like an ObjectListView but has the speed of a virtual list
*
* Author: Phillip Piper
* Date: 27/09/2008 9:15 AM
*
* Change log:
* 2012-06-11 JPP - Added more efficient version of FilteredObjects
* v2.5.1
* 2011-04-25 JPP - Fixed problem with removing objects from filtered or sorted list
* v2.4
* 2010-04-05 JPP - Added filtering
* v2.3
* 2009-08-27 JPP - Added GroupingStrategy
* - Added optimized Objects property
* v2.2.1
* 2009-01-07 JPP - Made all public and protected methods virtual
* 2008-09-27 JPP - Separated from ObjectListView.cs
*
* Copyright (C) 2006-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
namespace BrightIdeasSoftware
{
/// <summary>
/// A FastObjectListView trades function for speed.
/// </summary>
/// <remarks>
/// <para>On my mid-range laptop, this view builds a list of 10,000 objects in 0.1 seconds,
/// as opposed to a normal ObjectListView which takes 10-15 seconds. Lists of up to 50,000 items should be
/// able to be handled with sub-second response times even on low end machines.</para>
/// <para>
/// A FastObjectListView is implemented as a virtual list with many of the virtual modes limits (e.g. no sorting)
/// fixed through coding. There are some functions that simply cannot be provided. Specifically, a FastObjectListView cannot:
/// <list type="bullet">
/// <item><description>use Tile view</description></item>
/// <item><description>show groups on XP</description></item>
/// </list>
/// </para>
/// </remarks>
public class FastObjectListView : VirtualObjectListView
{
/// <summary>
/// Make a FastObjectListView
/// </summary>
public FastObjectListView() {
this.VirtualListDataSource = new FastObjectListDataSource(this);
this.GroupingStrategy = new FastListGroupingStrategy();
}
/// <summary>
/// Gets the collection of objects that survive any filtering that may be in place.
/// </summary>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public override IEnumerable FilteredObjects {
get {
// This is much faster than the base method
return ((FastObjectListDataSource)this.VirtualListDataSource).FilteredObjectList;
}
}
/// <summary>
/// Get/set the collection of objects that this list will show
/// </summary>
/// <remarks>
/// <para>
/// The contents of the control will be updated immediately after setting this property.
/// </para>
/// <para>This method preserves selection, if possible. Use SetObjects() if
/// you do not want to preserve the selection. Preserving selection is the slowest part of this
/// code and performance is O(n) where n is the number of selected rows.</para>
/// <para>This method is not thread safe.</para>
/// </remarks>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public override IEnumerable Objects {
get {
// This is much faster than the base method
return ((FastObjectListDataSource)this.VirtualListDataSource).ObjectList;
}
set { base.Objects = value; }
}
/// <summary>
/// Remove any sorting and revert to the given order of the model objects
/// </summary>
/// <remarks>To be really honest, Unsort() doesn't work on FastObjectListViews since
/// the original ordering of model objects is lost when Sort() is called. So this method
/// effectively just turns off sorting.</remarks>
public override void Unsort() {
this.ShowGroups = false;
this.PrimarySortColumn = null;
this.PrimarySortOrder = SortOrder.None;
this.SetObjects(this.Objects);
}
}
/// <summary>
/// Provide a data source for a FastObjectListView
/// </summary>
/// <remarks>
/// This class isn't intended to be used directly, but it is left as a public
/// class just in case someone wants to subclass it.
/// </remarks>
public class FastObjectListDataSource : AbstractVirtualListDataSource
{
/// <summary>
/// Create a FastObjectListDataSource
/// </summary>
/// <param name="listView"></param>
public FastObjectListDataSource(FastObjectListView listView)
: base(listView) {
}
#region IVirtualListDataSource Members
/// <summary>
/// Get n'th object
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
public override object GetNthObject(int n) {
if (n >= 0 && n < this.filteredObjectList.Count)
return this.filteredObjectList[n];
return null;
}
/// <summary>
/// How many items are in the data source
/// </summary>
/// <returns></returns>
public override int GetObjectCount() {
return this.filteredObjectList.Count;
}
/// <summary>
/// Get the index of the given model
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public override int GetObjectIndex(object model) {
int index;
if (model != null && this.objectsToIndexMap.TryGetValue(model, out index))
return index;
return -1;
}
/// <summary>
///
/// </summary>
/// <param name="text"></param>
/// <param name="first"></param>
/// <param name="last"></param>
/// <param name="column"></param>
/// <returns></returns>
public override int SearchText(string text, int first, int last, OLVColumn column) {
if (first <= last) {
for (int i = first; i <= last; i++) {
string data = column.GetStringValue(this.listView.GetNthItemInDisplayOrder(i).RowObject);
if (data.StartsWith(text, StringComparison.CurrentCultureIgnoreCase))
return i;
}
} else {
for (int i = first; i >= last; i--) {
string data = column.GetStringValue(this.listView.GetNthItemInDisplayOrder(i).RowObject);
if (data.StartsWith(text, StringComparison.CurrentCultureIgnoreCase))
return i;
}
}
return -1;
}
/// <summary>
///
/// </summary>
/// <param name="column"></param>
/// <param name="sortOrder"></param>
public override void Sort(OLVColumn column, SortOrder sortOrder) {
if (sortOrder != SortOrder.None) {
ModelObjectComparer comparer = new ModelObjectComparer(column, sortOrder, this.listView.SecondarySortColumn, this.listView.SecondarySortOrder);
this.fullObjectList.Sort(comparer);
this.filteredObjectList.Sort(comparer);
}
this.RebuildIndexMap();
}
/// <summary>
///
/// </summary>
/// <param name="modelObjects"></param>
public override void AddObjects(ICollection modelObjects) {
foreach (object modelObject in modelObjects) {
if (modelObject != null)
this.fullObjectList.Add(modelObject);
}
this.FilterObjects();
this.RebuildIndexMap();
}
/// <summary>
/// Remove the given collection of models from this source.
/// </summary>
/// <param name="modelObjects"></param>
public override void RemoveObjects(ICollection modelObjects) {
List<int> indicesToRemove = new List<int>();
foreach (object modelObject in modelObjects) {
int i = this.GetObjectIndex(modelObject);
if (i >= 0)
indicesToRemove.Add(i);
// Remove the objects from the unfiltered list
this.fullObjectList.Remove(modelObject);
}
// Sort the indices from highest to lowest so that we
// remove latter ones before earlier ones. In this way, the
// indices of the rows doesn't change after the deletes.
indicesToRemove.Sort();
indicesToRemove.Reverse();
foreach (int i in indicesToRemove)
this.listView.SelectedIndices.Remove(i);
this.FilterObjects();
this.RebuildIndexMap();
}
/// <summary>
///
/// </summary>
/// <param name="collection"></param>
public override void SetObjects(IEnumerable collection) {
ArrayList newObjects = ObjectListView.EnumerableToArray(collection, true);
this.fullObjectList = newObjects;
this.FilterObjects();
this.RebuildIndexMap();
}
/// <summary>
/// Update/replace the nth object with the given object
/// </summary>
/// <param name="index"></param>
/// <param name="modelObject"></param>
public override void UpdateObject(int index, object modelObject) {
if (index < 0 || index >= this.filteredObjectList.Count)
return;
int i = this.fullObjectList.IndexOf(this.filteredObjectList[index]);
if (i < 0)
return;
this.fullObjectList[i] = modelObject;
this.filteredObjectList[index] = modelObject;
this.objectsToIndexMap[modelObject] = index;
}
private ArrayList fullObjectList = new ArrayList();
private ArrayList filteredObjectList = new ArrayList();
private IModelFilter modelFilter;
private IListFilter listFilter;
#endregion
#region IFilterableDataSource Members
/// <summary>
/// Apply the given filters to this data source. One or both may be null.
/// </summary>
/// <param name="iModelFilter"></param>
/// <param name="iListFilter"></param>
public override void ApplyFilters(IModelFilter iModelFilter, IListFilter iListFilter) {
this.modelFilter = iModelFilter;
this.listFilter = iListFilter;
this.SetObjects(this.fullObjectList);
}
#endregion
#region Implementation
/// <summary>
/// Gets the full list of objects being used for this fast list.
/// This list is unfiltered.
/// </summary>
public ArrayList ObjectList {
get { return fullObjectList; }
}
/// <summary>
/// Gets the list of objects from ObjectList which survive any installed filters.
/// </summary>
public ArrayList FilteredObjectList {
get { return filteredObjectList; }
}
/// <summary>
/// Rebuild the map that remembers which model object is displayed at which line
/// </summary>
protected void RebuildIndexMap() {
this.objectsToIndexMap.Clear();
for (int i = 0; i < this.filteredObjectList.Count; i++)
this.objectsToIndexMap[this.filteredObjectList[i]] = i;
}
readonly Dictionary<Object, int> objectsToIndexMap = new Dictionary<Object, int>();
/// <summary>
/// Build our filtered list from our full list.
/// </summary>
protected void FilterObjects() {
if (!this.listView.UseFiltering || (this.modelFilter == null && this.listFilter == null)) {
this.filteredObjectList = new ArrayList(this.fullObjectList);
return;
}
IEnumerable objects = (this.listFilter == null) ?
this.fullObjectList : this.listFilter.Filter(this.fullObjectList);
// Apply the object filter if there is one
if (this.modelFilter == null) {
this.filteredObjectList = ObjectListView.EnumerableToArray(objects, false);
} else {
this.filteredObjectList = new ArrayList();
foreach (object model in objects) {
if (this.modelFilter.Filter(model))
this.filteredObjectList.Add(model);
}
}
}
#endregion
}
}

125
Filtering/Cluster.cs Normal file
View File

@ -0,0 +1,125 @@
/*
* Cluster - Implements a simple cluster
*
* Author: Phillip Piper
* Date: 3-March-2011 10:53 pm
*
* Change log:
* 2011-03-03 JPP - First version
*
* Copyright (C) 2011-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace BrightIdeasSoftware {
/// <summary>
/// Concrete implementation of the ICluster interface.
/// </summary>
public class Cluster : ICluster {
#region Life and death
/// <summary>
/// Create a cluster
/// </summary>
/// <param name="key">The key for the cluster</param>
public Cluster(object key) {
this.Count = 1;
this.ClusterKey = key;
}
#endregion
#region Public overrides
/// <summary>
/// Return a string representation of this cluster
/// </summary>
/// <returns></returns>
public override string ToString() {
return this.DisplayLabel ?? "[empty]";
}
#endregion
#region Implementation of ICluster
/// <summary>
/// Gets or sets how many items belong to this cluster
/// </summary>
public int Count {
get { return count; }
set { count = value; }
}
private int count;
/// <summary>
/// Gets or sets the label that will be shown to the user to represent
/// this cluster
/// </summary>
public string DisplayLabel {
get { return displayLabel; }
set { displayLabel = value; }
}
private string displayLabel;
/// <summary>
/// Gets or sets the actual data object that all members of this cluster
/// have commonly returned.
/// </summary>
public object ClusterKey {
get { return clusterKey; }
set { clusterKey = value; }
}
private object clusterKey;
#endregion
#region Implementation of IComparable
/// <summary>
/// Return an indication of the ordering between this object and the given one
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public int CompareTo(object other) {
if (other == null || other == System.DBNull.Value)
return 1;
ICluster otherCluster = other as ICluster;
if (otherCluster == null)
return 1;
string keyAsString = this.ClusterKey as string;
if (keyAsString != null)
return String.Compare(keyAsString, otherCluster.ClusterKey as string, StringComparison.CurrentCultureIgnoreCase);
IComparable keyAsComparable = this.ClusterKey as IComparable;
if (keyAsComparable != null)
return keyAsComparable.CompareTo(otherCluster.ClusterKey);
return -1;
}
#endregion
}
}

View File

@ -0,0 +1,189 @@
/*
* ClusteringStrategy - Implements a simple clustering strategy
*
* Author: Phillip Piper
* Date: 3-March-2011 10:53 pm
*
* Change log:
* 2011-03-03 JPP - First version
*
* Copyright (C) 2011-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.Text;
namespace BrightIdeasSoftware {
/// <summary>
/// This class provides a useful base implemention of a clustering
/// strategy where the clusters are grouped around the value of a given column.
/// </summary>
public class ClusteringStrategy : IClusteringStrategy {
#region Static properties
/// <summary>
/// This field is the text that will be shown to the user when a cluster
/// key is null. It is exposed so it can be localized.
/// </summary>
static public string NULL_LABEL = "[null]";
/// <summary>
/// This field is the text that will be shown to the user when a cluster
/// key is empty (i.e. a string of zero length). It is exposed so it can be localized.
/// </summary>
static public string EMPTY_LABEL = "[empty]";
/// <summary>
/// Gets or sets the format that will be used by default for clusters that only
/// contain 1 item. The format string must accept two placeholders:
/// - {0} is the cluster key converted to a string
/// - {1} is the number of items in the cluster (always 1 in this case)
/// </summary>
static public string DefaultDisplayLabelFormatSingular {
get { return defaultDisplayLabelFormatSingular; }
set { defaultDisplayLabelFormatSingular = value; }
}
static private string defaultDisplayLabelFormatSingular = "{0} ({1} item)";
/// <summary>
/// Gets or sets the format that will be used by default for clusters that
/// contain 0 or two or more items. The format string must accept two placeholders:
/// - {0} is the cluster key converted to a string
/// - {1} is the number of items in the cluster
/// </summary>
static public string DefaultDisplayLabelFormatPlural {
get { return defaultDisplayLabelFormatPural; }
set { defaultDisplayLabelFormatPural = value; }
}
static private string defaultDisplayLabelFormatPural = "{0} ({1} items)";
#endregion
#region Life and death
/// <summary>
/// Create a clustering strategy
/// </summary>
public ClusteringStrategy() {
this.DisplayLabelFormatSingular = DefaultDisplayLabelFormatSingular;
this.DisplayLabelFormatPlural = DefaultDisplayLabelFormatPlural;
}
#endregion
#region Public properties
/// <summary>
/// Gets or sets the column upon which this strategy is operating
/// </summary>
public OLVColumn Column {
get { return column; }
set { column = value; }
}
private OLVColumn column;
/// <summary>
/// Gets or sets the format that will be used when the cluster
/// contains only 1 item. The format string must accept two placeholders:
/// - {0} is the cluster key converted to a string
/// - {1} is the number of items in the cluster (always 1 in this case)
/// </summary>
/// <remarks>If this is not set, the value from
/// ClusteringStrategy.DefaultDisplayLabelFormatSingular will be used</remarks>
public string DisplayLabelFormatSingular {
get { return displayLabelFormatSingular; }
set { displayLabelFormatSingular = value; }
}
private string displayLabelFormatSingular;
/// <summary>
/// Gets or sets the format that will be used when the cluster
/// contains 0 or two or more items. The format string must accept two placeholders:
/// - {0} is the cluster key converted to a string
/// - {1} is the number of items in the cluster
/// </summary>
/// <remarks>If this is not set, the value from
/// ClusteringStrategy.DefaultDisplayLabelFormatPlural will be used</remarks>
public string DisplayLabelFormatPlural {
get { return displayLabelFormatPural; }
set { displayLabelFormatPural = value; }
}
private string displayLabelFormatPural;
#endregion
#region ICluster implementation
/// <summary>
/// Get the cluster key by which the given model will be partitioned by this strategy
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
virtual public object GetClusterKey(object model) {
return this.Column.GetValue(model);
}
/// <summary>
/// Create a cluster to hold the given cluster key
/// </summary>
/// <param name="clusterKey"></param>
/// <returns></returns>
virtual public ICluster CreateCluster(object clusterKey) {
return new Cluster(clusterKey);
}
/// <summary>
/// Gets the display label that the given cluster should use
/// </summary>
/// <param name="cluster"></param>
/// <returns></returns>
virtual public string GetClusterDisplayLabel(ICluster cluster) {
string s = this.Column.ValueToString(cluster.ClusterKey) ?? NULL_LABEL;
if (String.IsNullOrEmpty(s))
s = EMPTY_LABEL;
return this.ApplyDisplayFormat(cluster, s);
}
/// <summary>
/// Create a filter that will include only model objects that
/// match one or more of the given values.
/// </summary>
/// <param name="valuesChosenForFiltering"></param>
/// <returns></returns>
virtual public IModelFilter CreateFilter(IList valuesChosenForFiltering) {
return new OneOfFilter(this.GetClusterKey, valuesChosenForFiltering);
}
/// <summary>
/// Create a label that combines the string representation of the cluster
/// key with a format string that holds an "X [N items in cluster]" type layout.
/// </summary>
/// <param name="cluster"></param>
/// <param name="s"></param>
/// <returns></returns>
virtual protected string ApplyDisplayFormat(ICluster cluster, string s) {
string format = (cluster.Count == 1) ? this.DisplayLabelFormatSingular : this.DisplayLabelFormatPlural;
return String.IsNullOrEmpty(format) ? s : String.Format(format, s, cluster.Count);
}
#endregion
}
}

View File

@ -0,0 +1,70 @@
/*
* ClusteringStrategy - Implements a simple clustering strategy
*
* Author: Phillip Piper
* Date: 1-April-2011 8:12am
*
* Change log:
* 2011-04-01 JPP - First version
*
* Copyright (C) 2011-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace BrightIdeasSoftware {
/// <summary>
/// This class calculates clusters from the groups that the column uses.
/// </summary>
/// <remarks>
/// <para>
/// This is the default strategy for all non-date, filterable columns.
/// </para>
/// <para>
/// This class does not strictly mimic the groups created by the given column.
/// In particular, if the programmer changes the default grouping technique
/// by listening for grouping events, this class will not mimic that behaviour.
/// </para>
/// </remarks>
public class ClustersFromGroupsStrategy : ClusteringStrategy {
/// <summary>
/// Get the cluster key by which the given model will be partitioned by this strategy
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public override object GetClusterKey(object model) {
return this.Column.GetGroupKey(model);
}
/// <summary>
/// Gets the display label that the given cluster should use
/// </summary>
/// <param name="cluster"></param>
/// <returns></returns>
public override string GetClusterDisplayLabel(ICluster cluster) {
string s = this.Column.ConvertGroupKeyToTitle(cluster.ClusterKey);
if (String.IsNullOrEmpty(s))
s = EMPTY_LABEL;
return this.ApplyDisplayFormat(cluster, s);
}
}
}

View File

@ -0,0 +1,187 @@
/*
* DateTimeClusteringStrategy - A strategy to cluster objects by a date time
*
* Author: Phillip Piper
* Date: 30-March-2011 9:40am
*
* Change log:
* 2011-03-30 JPP - First version
*
* Copyright (C) 2011-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Globalization;
namespace BrightIdeasSoftware {
/// <summary>
/// This enum is used to indicate various portions of a datetime
/// </summary>
[Flags]
public enum DateTimePortion {
/// <summary>
/// Year
/// </summary>
Year = 0x01,
/// <summary>
/// Month
/// </summary>
Month = 0x02,
/// <summary>
/// Day of the month
/// </summary>
Day = 0x04,
/// <summary>
/// Hour
/// </summary>
Hour = 0x08,
/// <summary>
/// Minute
/// </summary>
Minute = 0x10,
/// <summary>
/// Second
/// </summary>
Second = 0x20
}
/// <summary>
/// This class implements a strategy where the model objects are clustered
/// according to some portion of the datetime value in the configured column.
/// </summary>
/// <remarks>To create a strategy that grouped people who were born in
/// the same month, you would create a strategy that extracted just
/// the month, and formatted it to show just the month's name. Like this:
/// </remarks>
/// <example>
/// someColumn.ClusteringStrategy = new DateTimeClusteringStrategy(DateTimePortion.Month, "MMMM");
/// </example>
public class DateTimeClusteringStrategy : ClusteringStrategy {
#region Life and death
/// <summary>
/// Create a strategy that clusters by month/year
/// </summary>
public DateTimeClusteringStrategy()
: this(DateTimePortion.Year | DateTimePortion.Month, "MMMM yyyy") {
}
/// <summary>
/// Create a strategy that clusters around the given parts
/// </summary>
/// <param name="portions"></param>
/// <param name="format"></param>
public DateTimeClusteringStrategy(DateTimePortion portions, string format) {
this.Portions = portions;
this.Format = format;
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the format string will will be used to create a user-presentable
/// version of the cluster key.
/// </summary>
/// <remarks>The format should use the date/time format strings, as documented
/// in the Windows SDK. Both standard formats and custom format will work.</remarks>
/// <example>"D" - long date pattern</example>
/// <example>"MMMM, yyyy" - "January, 1999"</example>
public string Format {
get { return format; }
set { format = value; }
}
private string format;
/// <summary>
/// Gets or sets the parts of the DateTime that will be extracted when
/// determining the clustering key for an object.
/// </summary>
public DateTimePortion Portions {
get { return portions; }
set { portions = value; }
}
private DateTimePortion portions = DateTimePortion.Year | DateTimePortion.Month;
#endregion
#region IClusterStrategy implementation
/// <summary>
/// Get the cluster key by which the given model will be partitioned by this strategy
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public override object GetClusterKey(object model) {
// Get the data attribute we want from the given model
// Make sure the returned value is a DateTime
DateTime? dateTime = this.Column.GetValue(model) as DateTime?;
if (!dateTime.HasValue)
return null;
// Extract the parts of the datetime that we are intereted in.
// Even if we aren't interested in a particular portion, we still have to give it a reasonable default
// otherwise we won't be able to build a DateTime object for it
int year = ((this.Portions & DateTimePortion.Year) == DateTimePortion.Year) ? dateTime.Value.Year : 1;
int month = ((this.Portions & DateTimePortion.Month) == DateTimePortion.Month) ? dateTime.Value.Month : 1;
int day = ((this.Portions & DateTimePortion.Day) == DateTimePortion.Day) ? dateTime.Value.Day : 1;
int hour = ((this.Portions & DateTimePortion.Hour) == DateTimePortion.Hour) ? dateTime.Value.Hour : 0;
int minute = ((this.Portions & DateTimePortion.Minute) == DateTimePortion.Minute) ? dateTime.Value.Minute : 0;
int second = ((this.Portions & DateTimePortion.Second) == DateTimePortion.Second) ? dateTime.Value.Second : 0;
return new DateTime(year, month, day, hour, minute, second);
}
/// <summary>
/// Gets the display label that the given cluster should use
/// </summary>
/// <param name="cluster"></param>
/// <returns></returns>
public override string GetClusterDisplayLabel(ICluster cluster) {
DateTime? dateTime = cluster.ClusterKey as DateTime?;
return this.ApplyDisplayFormat(cluster, dateTime.HasValue ? this.DateToString(dateTime.Value) : NULL_LABEL);
}
/// <summary>
/// Convert the given date into a user presentable string
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
protected virtual string DateToString(DateTime dateTime) {
if (String.IsNullOrEmpty(this.Format))
return dateTime.ToString(CultureInfo.CurrentUICulture);
try {
return dateTime.ToString(this.Format);
}
catch (FormatException) {
return String.Format("Bad format string '{0}' for value '{1}'", this.Format, dateTime);
}
}
#endregion
}
}

View File

@ -0,0 +1,369 @@
/*
* FilterMenuBuilder - Responsible for creating a Filter menu
*
* Author: Phillip Piper
* Date: 4-March-2011 11:59 pm
*
* Change log:
* 2012-05-20 JPP - Allow the same model object to be in multiple clusters
* Useful for xor'ed flag fields, and multi-value strings
* (e.g. hobbies that are stored as comma separated values).
* v2.5.1
* 2012-04-14 JPP - Fixed rare bug with clustering an empty list (SF #3445118)
* v2.5
* 2011-04-12 JPP - Added some images to menu
* 2011-03-04 JPP - First version
*
* Copyright (C) 2011-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
namespace BrightIdeasSoftware {
/// <summary>
/// Instances of this class know how to build a Filter menu.
/// It is responsible for clustering the values in the target column,
/// build a menu that shows those clusters, and then constructing
/// a filter that will enact the users choices.
/// </summary>
/// <remarks>
/// Almost all of the methods in this class are declared as "virtual protected"
/// so that subclasses can provide alternative behaviours.
/// </remarks>
public class FilterMenuBuilder {
#region Static properties
/// <summary>
/// Gets or sets the string that labels the Apply button.
/// Exposed so it can be localized.
/// </summary>
static public string APPLY_LABEL = "Apply";
/// <summary>
/// Gets or sets the string that labels the Clear All menu item.
/// Exposed so it can be localized.
/// </summary>
static public string CLEAR_ALL_FILTERS_LABEL = "Clear All Filters";
/// <summary>
/// Gets or sets the string that labels the Filtering menu as a whole..
/// Exposed so it can be localized.
/// </summary>
static public string FILTERING_LABEL = "Filtering";
/// <summary>
/// Gets or sets the string that represents Select All values.
/// If this is set to null or empty, no Select All option will be included.
/// Exposed so it can be localized.
/// </summary>
static public string SELECT_ALL_LABEL = "Select All";
/// <summary>
/// Gets or sets the image that will be placed next to the Clear Filtering menu item
/// </summary>
static public Bitmap ClearFilteringImage = BrightIdeasSoftware.Properties.Resources.ClearFiltering;
/// <summary>
/// Gets or sets the image that will be placed next to all "Apply" menu items on the filtering menu
/// </summary>
static public Bitmap FilteringImage = BrightIdeasSoftware.Properties.Resources.Filtering;
#endregion
#region Public properties
/// <summary>
/// Gets or sets whether null should be considered as a valid data value.
/// If this is true (the default), then a cluster will null as a key will be allow.
/// If this is false, object that return a cluster key of null will ignored.
/// </summary>
public bool TreatNullAsDataValue {
get { return treatNullAsDataValue; }
set { treatNullAsDataValue = value; }
}
private bool treatNullAsDataValue = true;
/// <summary>
/// Gets or sets the maximum number of objects that the clustering strategy
/// will consider. This should be large enough to collect all unique clusters,
/// but small enough to finish in a reasonable time.
/// </summary>
/// <remarks>The default value is 10,000. This should be perfectly
/// acceptable for almost all lists.</remarks>
public int MaxObjectsToConsider {
get { return maxObjectsToConsider; }
set { maxObjectsToConsider = value; }
}
private int maxObjectsToConsider = 10000;
#endregion
/// <summary>
/// Create a Filter menu on the given tool tip for the given column in the given ObjectListView.
/// </summary>
/// <remarks>This is the main entry point into this class.</remarks>
/// <param name="strip"></param>
/// <param name="listView"></param>
/// <param name="column"></param>
/// <returns>The strip that should be shown to the user</returns>
virtual public ToolStripDropDown MakeFilterMenu(ToolStripDropDown strip, ObjectListView listView, OLVColumn column) {
if (strip == null) throw new ArgumentNullException("strip");
if (listView == null) throw new ArgumentNullException("listView");
if (column == null) throw new ArgumentNullException("column");
if (!column.UseFiltering || column.ClusteringStrategy == null || listView.Objects == null)
return strip;
List<ICluster> clusters = this.Cluster(column.ClusteringStrategy, listView, column);
if (clusters.Count > 0) {
this.SortClusters(column.ClusteringStrategy, clusters);
strip.Items.Add(this.CreateFilteringMenuItem(column, clusters));
}
return strip;
}
/// <summary>
/// Create a collection of clusters that should be presented to the user
/// </summary>
/// <param name="strategy"></param>
/// <param name="listView"></param>
/// <param name="column"></param>
/// <returns></returns>
virtual protected List<ICluster> Cluster(IClusteringStrategy strategy, ObjectListView listView, OLVColumn column) {
// Build a map that correlates cluster key to clusters
NullableDictionary<object, ICluster> map = new NullableDictionary<object, ICluster>();
int count = 0;
foreach (object model in listView.ObjectsForClustering) {
this.ClusterOneModel(strategy, map, model);
if (count++ > this.MaxObjectsToConsider)
break;
}
// Now that we know exactly how many items are in each cluster, create a label for it
foreach (ICluster cluster in map.Values)
cluster.DisplayLabel = strategy.GetClusterDisplayLabel(cluster);
return new List<ICluster>(map.Values);
}
private void ClusterOneModel(IClusteringStrategy strategy, NullableDictionary<object, ICluster> map, object model) {
object clusterKey = strategy.GetClusterKey(model);
// If the returned value is an IEnumerable, that means the given model can belong to more than one cluster
IEnumerable keyEnumerable = clusterKey as IEnumerable;
if (clusterKey is string || keyEnumerable == null)
keyEnumerable = new object[] {clusterKey};
// Deal with nulls and DBNulls
ArrayList nullCorrected = new ArrayList();
foreach (object key in keyEnumerable) {
if (key == null || key == System.DBNull.Value) {
if (this.TreatNullAsDataValue)
nullCorrected.Add(null);
} else nullCorrected.Add(key);
}
// Group by key
foreach (object key in nullCorrected) {
if (map.ContainsKey(key))
map[key].Count += 1;
else
map[key] = strategy.CreateCluster(key);
}
}
/// <summary>
/// Order the given list of clusters in the manner in which they should be presented to the user.
/// </summary>
/// <param name="strategy"></param>
/// <param name="clusters"></param>
virtual protected void SortClusters(IClusteringStrategy strategy, List<ICluster> clusters) {
clusters.Sort();
}
/// <summary>
/// Do the work of making a menu that shows the clusters to the users
/// </summary>
/// <param name="column"></param>
/// <param name="clusters"></param>
/// <returns></returns>
virtual protected ToolStripMenuItem CreateFilteringMenuItem(OLVColumn column, List<ICluster> clusters) {
ToolStripCheckedListBox checkedList = new ToolStripCheckedListBox();
checkedList.Tag = column;
foreach (ICluster cluster in clusters)
checkedList.AddItem(cluster, column.ValuesChosenForFiltering.Contains(cluster.ClusterKey));
if (!String.IsNullOrEmpty(SELECT_ALL_LABEL)) {
int checkedCount = checkedList.CheckedItems.Count;
if (checkedCount == 0)
checkedList.AddItem(SELECT_ALL_LABEL, CheckState.Unchecked);
else
checkedList.AddItem(SELECT_ALL_LABEL, checkedCount == clusters.Count ? CheckState.Checked : CheckState.Indeterminate);
}
checkedList.ItemCheck += new ItemCheckEventHandler(HandleItemCheckedWrapped);
ToolStripMenuItem clearAll = new ToolStripMenuItem(CLEAR_ALL_FILTERS_LABEL, ClearFilteringImage, delegate(object sender, EventArgs args) {
this.ClearAllFilters(column);
});
ToolStripMenuItem apply = new ToolStripMenuItem(APPLY_LABEL, FilteringImage, delegate(object sender, EventArgs args) {
this.EnactFilter(checkedList, column);
});
ToolStripMenuItem subMenu = new ToolStripMenuItem(FILTERING_LABEL, null, new ToolStripItem[] {
clearAll, new ToolStripSeparator(), checkedList, apply });
return subMenu;
}
/// <summary>
/// Wrap a protected section around the real HandleItemChecked method, so that if
/// that method tries to change a "checkedness" of an item, we don't get a recursive
/// stack error. Effectively, this ensure that HandleItemChecked is only called
/// in response to a user action.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void HandleItemCheckedWrapped(object sender, ItemCheckEventArgs e) {
if (alreadyInHandleItemChecked)
return;
try {
alreadyInHandleItemChecked = true;
this.HandleItemChecked(sender, e);
}
finally {
alreadyInHandleItemChecked = false;
}
}
bool alreadyInHandleItemChecked = false;
/// <summary>
/// Handle a user-generated ItemCheck event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
virtual protected void HandleItemChecked(object sender, ItemCheckEventArgs e) {
ToolStripCheckedListBox checkedList = sender as ToolStripCheckedListBox;
if (checkedList == null) return;
OLVColumn column = checkedList.Tag as OLVColumn;
if (column == null) return;
ObjectListView listView = column.ListView as ObjectListView;
if (listView == null) return;
// Deal with the "Select All" item if there is one
int selectAllIndex = checkedList.Items.IndexOf(SELECT_ALL_LABEL);
if (selectAllIndex >= 0)
HandleSelectAllItem(e, checkedList, selectAllIndex);
}
/// <summary>
/// Handle any checking/unchecking of the Select All option, and keep
/// its checkedness in sync with everything else that is checked.
/// </summary>
/// <param name="e"></param>
/// <param name="checkedList"></param>
/// <param name="selectAllIndex"></param>
virtual protected void HandleSelectAllItem(ItemCheckEventArgs e, ToolStripCheckedListBox checkedList, int selectAllIndex) {
// Did they check/uncheck the "Select All"?
if (e.Index == selectAllIndex) {
if (e.NewValue == CheckState.Checked)
checkedList.CheckAll();
if (e.NewValue == CheckState.Unchecked)
checkedList.UncheckAll();
return;
}
// OK. The user didn't check/uncheck SelectAll. Now we have to update it's
// checkedness to reflect the state of everything else
// If all clusters are checked, we check the Select All.
// If no clusters are checked, the uncheck the Select All.
// For everything else, Select All is set to indeterminate.
// How many items are currenty checked?
int count = checkedList.CheckedItems.Count;
// First complication.
// The value of the Select All itself doesn't count
if (checkedList.GetItemCheckState(selectAllIndex) != CheckState.Unchecked)
count -= 1;
// Another complication.
// CheckedItems does not yet know about the item the user has just
// clicked, so we have to adjust the count of checked items to what
// it is going to be
if (e.NewValue != e.CurrentValue) {
if (e.NewValue == CheckState.Checked)
count += 1;
else
count -= 1;
}
// Update the state of the Select All item
if (count == 0)
checkedList.SetItemState(selectAllIndex, CheckState.Unchecked);
else if (count == checkedList.Items.Count - 1)
checkedList.SetItemState(selectAllIndex, CheckState.Checked);
else
checkedList.SetItemState(selectAllIndex, CheckState.Indeterminate);
}
/// <summary>
/// Clear all the filters that are applied to the given column
/// </summary>
/// <param name="column">The column from which filters are to be removed</param>
virtual protected void ClearAllFilters(OLVColumn column) {
ObjectListView olv = column.ListView as ObjectListView;
if (olv == null || olv.IsDisposed)
return;
olv.ResetColumnFiltering();
}
/// <summary>
/// Apply the selected values from the given list as a filter on the given column
/// </summary>
/// <param name="checkedList">A list in which the checked items should be used as filters</param>
/// <param name="column">The column for which a filter should be generated</param>
virtual protected void EnactFilter(ToolStripCheckedListBox checkedList, OLVColumn column) {
ObjectListView olv = column.ListView as ObjectListView;
if (olv == null || olv.IsDisposed)
return;
// Collect all the checked values
ArrayList chosenValues = new ArrayList();
foreach (object x in checkedList.CheckedItems) {
ICluster cluster = x as ICluster;
if (cluster != null) {
chosenValues.Add(cluster.ClusterKey);
}
}
column.ValuesChosenForFiltering = chosenValues;
olv.UpdateColumnFiltering();
}
}
}

476
Filtering/Filters.cs Normal file
View File

@ -0,0 +1,476 @@
/*
* Filters - Filtering on ObjectListViews
*
* Author: Phillip Piper
* Date: 03/03/2010 17:00
*
* Change log:
* 2011-03-01 JPP Added CompositeAllFilter, CompositeAnyFilter and OneOfFilter
* v2.4.1
* 2010-06-23 JPP Extended TextMatchFilter to handle regular expressions and string prefix matching.
* v2.4
* 2010-03-03 JPP Initial version
*
* TO DO:
*
* Copyright (C) 2010-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Drawing;
namespace BrightIdeasSoftware
{
/// <summary>
/// Interface for model-by-model filtering
/// </summary>
public interface IModelFilter
{
/// <summary>
/// Should the given model be included when this filter is installed
/// </summary>
/// <param name="modelObject">The model object to consider</param>
/// <returns>Returns true if the model will be included by the filter</returns>
bool Filter(object modelObject);
}
/// <summary>
/// Interface for whole list filtering
/// </summary>
public interface IListFilter
{
/// <summary>
/// Return a subset of the given list of model objects as the new
/// contents of the ObjectListView
/// </summary>
/// <param name="modelObjects">The collection of model objects that the list will possibly display</param>
/// <returns>The filtered collection that holds the model objects that will be displayed.</returns>
IEnumerable Filter(IEnumerable modelObjects);
}
/// <summary>
/// Base class for model-by-model filters
/// </summary>
public class AbstractModelFilter : IModelFilter
{
/// <summary>
/// Should the given model be included when this filter is installed
/// </summary>
/// <param name="modelObject">The model object to consider</param>
/// <returns>Returns true if the model will be included by the filter</returns>
virtual public bool Filter(object modelObject) {
return true;
}
}
/// <summary>
/// This filter calls a given Predicate to decide if a model object should be included
/// </summary>
public class ModelFilter : IModelFilter
{
/// <summary>
/// Create a filter based on the given predicate
/// </summary>
/// <param name="predicate">The function that will filter objects</param>
public ModelFilter(Predicate<object> predicate) {
this.Predicate = predicate;
}
/// <summary>
/// Gets or sets the predicate used to filter model objects
/// </summary>
protected Predicate<object> Predicate {
get { return predicate; }
set { predicate = value; }
}
private Predicate<object> predicate;
/// <summary>
/// Should the given model object be included?
/// </summary>
/// <param name="modelObject"></param>
/// <returns></returns>
virtual public bool Filter(object modelObject) {
return this.Predicate == null ? true : this.Predicate(modelObject);
}
}
/// <summary>
/// A CompositeFilter joins several other filters together.
/// If there are no filters, all model objects are included
/// </summary>
abstract public class CompositeFilter : IModelFilter {
/// <summary>
/// Create an empty filter
/// </summary>
public CompositeFilter() {
}
/// <summary>
/// Create a composite filter from the given list of filters
/// </summary>
/// <param name="filters">A list of filters</param>
public CompositeFilter(IEnumerable<IModelFilter> filters) {
foreach (IModelFilter filter in filters) {
if (filter != null)
Filters.Add(filter);
}
}
/// <summary>
/// Gets or sets the filters used by this composite
/// </summary>
public IList<IModelFilter> Filters {
get { return filters; }
set { filters = value; }
}
private IList<IModelFilter> filters = new List<IModelFilter>();
/// <summary>
/// Decide whether or not the given model should be included by the filter
/// </summary>
/// <param name="modelObject"></param>
/// <returns>True if the object is included by the filter</returns>
virtual public bool Filter(object modelObject) {
if (this.Filters == null || this.Filters.Count == 0)
return true;
return this.FilterObject(modelObject);
}
/// <summary>
/// Decide whether or not the given model should be included by the filter
/// </summary>
/// <remarks>Filters is guaranteed to be non-empty when this method is called</remarks>
/// <param name="modelObject">The model object under consideration</param>
/// <returns>True if the object is included by the filter</returns>
abstract public bool FilterObject(object modelObject);
}
/// <summary>
/// A CompositeAllFilter joins several other filters together.
/// A model object must satisfy all filters to be included.
/// If there are no filters, all model objects are included
/// </summary>
public class CompositeAllFilter : CompositeFilter {
/// <summary>
/// Create a filter
/// </summary>
/// <param name="filters"></param>
public CompositeAllFilter(List<IModelFilter> filters)
: base(filters) {
}
/// <summary>
/// Decide whether or not the given model should be included by the filter
/// </summary>
/// <remarks>Filters is guaranteed to be non-empty when this method is called</remarks>
/// <param name="modelObject">The model object under consideration</param>
/// <returns>True if the object is included by the filter</returns>
override public bool FilterObject(object modelObject) {
foreach (IModelFilter filter in this.Filters)
if (!filter.Filter(modelObject))
return false;
return true;
}
}
/// <summary>
/// A CompositeAllFilter joins several other filters together.
/// A model object must only satisfy one of the filters to be included.
/// If there are no filters, all model objects are included
/// </summary>
public class CompositeAnyFilter : CompositeFilter {
/// <summary>
/// Create a filter from the given filters
/// </summary>
/// <param name="filters"></param>
public CompositeAnyFilter(List<IModelFilter> filters)
: base(filters) {
}
/// <summary>
/// Decide whether or not the given model should be included by the filter
/// </summary>
/// <remarks>Filters is guaranteed to be non-empty when this method is called</remarks>
/// <param name="modelObject">The model object under consideration</param>
/// <returns>True if the object is included by the filter</returns>
override public bool FilterObject(object modelObject) {
foreach (IModelFilter filter in this.Filters)
if (filter.Filter(modelObject))
return true;
return false;
}
}
/// <summary>
/// Instances of this class extract a value from the model object
/// and compare that value to a list of fixed values. The model
/// object is included if the extracted value is in the list
/// </summary>
/// <remarks>If there is no delegate installed or there are
/// no values to match, no model objects will be matched</remarks>
public class OneOfFilter : IModelFilter {
/// <summary>
/// Create a filter that will use the given delegate to extract values
/// </summary>
/// <param name="valueGetter"></param>
public OneOfFilter(AspectGetterDelegate valueGetter) :
this(valueGetter, new ArrayList()) {
}
/// <summary>
/// Create a filter that will extract values using the given delegate
/// and compare them to the values in the given list.
/// </summary>
/// <param name="valueGetter"></param>
/// <param name="possibleValues"></param>
public OneOfFilter(AspectGetterDelegate valueGetter, ICollection possibleValues) {
this.ValueGetter = valueGetter;
this.PossibleValues = new ArrayList(possibleValues);
}
/// <summary>
/// Gets or sets the delegate that will be used to extract values
/// from model objects
/// </summary>
virtual public AspectGetterDelegate ValueGetter {
get { return valueGetter; }
set { valueGetter = value; }
}
private AspectGetterDelegate valueGetter;
/// <summary>
/// Gets or sets the list of values that the value extracted from
/// the model object must match in order to be included.
/// </summary>
virtual public IList PossibleValues {
get { return possibleValues; }
set { possibleValues = value; }
}
private IList possibleValues;
/// <summary>
/// Should the given model object be included?
/// </summary>
/// <param name="modelObject"></param>
/// <returns></returns>
public virtual bool Filter(object modelObject) {
if (this.ValueGetter == null || this.PossibleValues == null || this.PossibleValues.Count == 0)
return false;
object result = this.ValueGetter(modelObject);
IEnumerable enumerable = result as IEnumerable;
if (result is string || enumerable == null)
return this.DoesValueMatch(result);
foreach (object x in enumerable) {
if (this.DoesValueMatch(x))
return true;
}
return false;
}
/// <summary>
/// Decides if the given property is a match for the values in the PossibleValues collection
/// </summary>
/// <param name="result"></param>
/// <returns></returns>
protected virtual bool DoesValueMatch(object result) {
return this.PossibleValues.Contains(result);
}
}
/// <summary>
/// Instances of this class match a property of a model objects against
/// a list of bit flags. The property should be an xor-ed collection
/// of bits flags.
/// </summary>
/// <remarks>Both the property compared and the list of possible values
/// must be convertible to ulongs.</remarks>
public class FlagBitSetFilter : OneOfFilter {
/// <summary>
/// Create an instance
/// </summary>
/// <param name="valueGetter"></param>
/// <param name="possibleValues"></param>
public FlagBitSetFilter(AspectGetterDelegate valueGetter, ICollection possibleValues) : base(valueGetter, possibleValues) {
this.ConvertPossibleValues();
}
/// <summary>
/// Gets or sets the collection of values that will be matched.
/// These must be ulongs (or convertible to ulongs).
/// </summary>
public override IList PossibleValues {
get { return base.PossibleValues; }
set {
base.PossibleValues = value;
this.ConvertPossibleValues();
}
}
private void ConvertPossibleValues() {
this.possibleValuesAsUlongs = new List<UInt64>();
foreach (object x in this.PossibleValues)
this.possibleValuesAsUlongs.Add(Convert.ToUInt64(x));
}
/// <summary>
/// Decides if the given property is a match for the values in the PossibleValues collection
/// </summary>
/// <param name="result"></param>
/// <returns></returns>
protected override bool DoesValueMatch(object result) {
try {
UInt64 value = Convert.ToUInt64(result);
foreach (ulong flag in this.possibleValuesAsUlongs) {
if ((value & flag) == flag)
return true;
}
return false;
}
catch (InvalidCastException) {
return false;
}
catch (FormatException) {
return false;
}
}
private List<UInt64> possibleValuesAsUlongs = new List<UInt64>();
}
/// <summary>
/// Base class for whole list filters
/// </summary>
public class AbstractListFilter : IListFilter
{
/// <summary>
/// Return a subset of the given list of model objects as the new
/// contents of the ObjectListView
/// </summary>
/// <param name="modelObjects">The collection of model objects that the list will possibly display</param>
/// <returns>The filtered collection that holds the model objects that will be displayed.</returns>
virtual public IEnumerable Filter(IEnumerable modelObjects) {
return modelObjects;
}
}
/// <summary>
/// Instance of this class implement delegate based whole list filtering
/// </summary>
public class ListFilter : AbstractListFilter
{
/// <summary>
/// A delegate that filters on a whole list
/// </summary>
/// <param name="rowObjects"></param>
/// <returns></returns>
public delegate IEnumerable ListFilterDelegate(IEnumerable rowObjects);
/// <summary>
/// Create a ListFilter
/// </summary>
/// <param name="function"></param>
public ListFilter(ListFilterDelegate function) {
this.Function = function;
}
/// <summary>
/// Gets or sets the delegate that will filter the list
/// </summary>
public ListFilterDelegate Function {
get { return function; }
set { function = value; }
}
private ListFilterDelegate function;
/// <summary>
/// Do the actual work of filtering
/// </summary>
/// <param name="modelObjects"></param>
/// <returns></returns>
public override IEnumerable Filter(IEnumerable modelObjects) {
if (this.Function == null)
return modelObjects;
return this.Function(modelObjects);
}
}
/// <summary>
/// Filter the list so only the last N entries are displayed
/// </summary>
public class TailFilter : AbstractListFilter
{
/// <summary>
/// Create a no-op tail filter
/// </summary>
public TailFilter() {
}
/// <summary>
/// Create a filter that includes on the last N model objects
/// </summary>
/// <param name="numberOfObjects"></param>
public TailFilter(int numberOfObjects) {
this.Count = numberOfObjects;
}
/// <summary>
/// Gets or sets the number of model objects that will be
/// returned from the tail of the list
/// </summary>
public int Count {
get { return count; }
set { count = value; }
}
private int count;
/// <summary>
/// Return the last N subset of the model objects
/// </summary>
/// <param name="modelObjects"></param>
/// <returns></returns>
public override IEnumerable Filter(IEnumerable modelObjects) {
if (this.Count <= 0)
return modelObjects;
ArrayList list = ObjectListView.EnumerableToArray(modelObjects, false);
if (this.Count > list.Count)
return list;
object[] tail = new object[this.Count];
list.CopyTo(list.Count - this.Count, tail, 0, this.Count);
return new ArrayList(tail);
}
}
}

View File

@ -0,0 +1,160 @@
/*
* FlagClusteringStrategy - Implements a clustering strategy for a field which is a single integer
* containing an XOR'ed collection of bit flags
*
* Author: Phillip Piper
* Date: 23-March-2012 8:33 am
*
* Change log:
* 2012-03-23 JPP - First version
*
* Copyright (C) 2012 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
namespace BrightIdeasSoftware {
/// <summary>
/// Instances of this class cluster model objects on the basis of a
/// property that holds an xor-ed collection of bit flags.
/// </summary>
public class FlagClusteringStrategy : ClusteringStrategy
{
#region Life and death
/// <summary>
/// Create a clustering strategy that operates on the flags of the given enum
/// </summary>
/// <param name="enumType"></param>
public FlagClusteringStrategy(Type enumType) {
if (enumType == null) throw new ArgumentNullException("enumType");
if (!enumType.IsEnum) throw new ArgumentException("Type must be enum", "enumType");
if (enumType.GetCustomAttributes(typeof(FlagsAttribute), false) == null) throw new ArgumentException("Type must have [Flags] attribute", "enumType");
List<long> flags = new List<long>();
foreach (object x in Enum.GetValues(enumType))
flags.Add(Convert.ToInt64(x));
List<string> flagLabels = new List<string>();
foreach (string x in Enum.GetNames(enumType))
flagLabels.Add(x);
this.SetValues(flags.ToArray(), flagLabels.ToArray());
}
/// <summary>
/// Create a clustering strategy around the given collections of flags and their display labels.
/// There must be the same number of elements in both collections.
/// </summary>
/// <param name="values">The list of flags. </param>
/// <param name="labels"></param>
public FlagClusteringStrategy(long[] values, string[] labels) {
this.SetValues(values, labels);
}
#endregion
#region Implementation
/// <summary>
/// Gets the value that will be xor-ed to test for the presence of a particular value.
/// </summary>
public long[] Values {
get { return this.values; }
private set { this.values = value; }
}
private long[] values;
/// <summary>
/// Gets the labels that will be used when the corresponding Value is XOR present in the data.
/// </summary>
public string[] Labels {
get { return this.labels; }
private set { this.labels = value; }
}
private string[] labels;
private void SetValues(long[] flags, string[] flagLabels) {
if (flags == null || flags.Length == 0) throw new ArgumentNullException("flags");
if (flagLabels == null || flagLabels.Length == 0) throw new ArgumentNullException("flagLabels");
if (flags.Length != flagLabels.Length) throw new ArgumentException("values and labels must have the same number of entries", "flags");
this.Values = flags;
this.Labels = flagLabels;
}
#endregion
#region Implementation of IClusteringStrategy
/// <summary>
/// Get the cluster key by which the given model will be partitioned by this strategy
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public override object GetClusterKey(object model) {
List<long> flags = new List<long>();
try {
long modelValue = Convert.ToInt64(this.Column.GetValue(model));
foreach (long x in this.Values) {
if ((x & modelValue) == x)
flags.Add(x);
}
return flags;
}
catch (InvalidCastException ex) {
System.Diagnostics.Debug.Write(ex);
return flags;
}
catch (FormatException ex) {
System.Diagnostics.Debug.Write(ex);
return flags;
}
}
/// <summary>
/// Gets the display label that the given cluster should use
/// </summary>
/// <param name="cluster"></param>
/// <returns></returns>
public override string GetClusterDisplayLabel(ICluster cluster) {
long clusterKeyAsUlong = Convert.ToInt64(cluster.ClusterKey);
for (int i = 0; i < this.Values.Length; i++ ) {
if (clusterKeyAsUlong == this.Values[i])
return this.ApplyDisplayFormat(cluster, this.Labels[i]);
}
return this.ApplyDisplayFormat(cluster, clusterKeyAsUlong.ToString(CultureInfo.CurrentUICulture));
}
/// <summary>
/// Create a filter that will include only model objects that
/// match one or more of the given values.
/// </summary>
/// <param name="valuesChosenForFiltering"></param>
/// <returns></returns>
public override IModelFilter CreateFilter(IList valuesChosenForFiltering) {
return new FlagBitSetFilter(this.GetClusterKey, valuesChosenForFiltering);
}
#endregion
}
}

56
Filtering/ICluster.cs Normal file
View File

@ -0,0 +1,56 @@
/*
* ICluster - A cluster is a group of objects that can be included or excluded as a whole
*
* Author: Phillip Piper
* Date: 4-March-2011 11:59 pm
*
* Change log:
* 2011-03-04 JPP - First version
*
* Copyright (C) 2011-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace BrightIdeasSoftware {
/// <summary>
/// A cluster is a like collection of objects that can be usefully filtered
/// as whole using the filtering UI provided by the ObjectListView.
/// </summary>
public interface ICluster : IComparable {
/// <summary>
/// Gets or sets how many items belong to this cluster
/// </summary>
int Count { get; set; }
/// <summary>
/// Gets or sets the label that will be shown to the user to represent
/// this cluster
/// </summary>
string DisplayLabel { get; set; }
/// <summary>
/// Gets or sets the actual data object that all members of this cluster
/// have commonly returned.
/// </summary>
object ClusterKey { get; set; }
}
}

View File

@ -0,0 +1,80 @@
/*
* IClusterStrategy - Encapsulates the ability to create a list of clusters from an ObjectListView
*
* Author: Phillip Piper
* Date: 4-March-2011 11:59 pm
*
* Change log:
* 2012-05-23 JPP - Added CreateFilter() method to interface to allow the strategy
* to control the actual model filter that is created.
* v2.5
* 2011-03-04 JPP - First version
*
* Copyright (C) 2011-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace BrightIdeasSoftware{
/// <summary>
/// Implementation of this interface control the selecting of cluster keys
/// and how those clusters will be presented to the user
/// </summary>
public interface IClusteringStrategy {
/// <summary>
/// Gets or sets the column upon which this strategy will operate
/// </summary>
OLVColumn Column { get; set; }
/// <summary>
/// Get the cluster key by which the given model will be partitioned by this strategy
/// </summary>
/// <remarks>If the returned value is an IEnumerable, the given model is considered
/// to belong to multiple clusters</remarks>
/// <param name="model"></param>
/// <returns></returns>
object GetClusterKey(object model);
/// <summary>
/// Create a cluster to hold the given cluster key
/// </summary>
/// <param name="clusterKey"></param>
/// <returns></returns>
ICluster CreateCluster(object clusterKey);
/// <summary>
/// Gets the display label that the given cluster should use
/// </summary>
/// <param name="cluster"></param>
/// <returns></returns>
string GetClusterDisplayLabel(ICluster cluster);
/// <summary>
/// Create a filter that will include only model objects that
/// match one or more of the given values.
/// </summary>
/// <param name="valuesChosenForFiltering"></param>
/// <returns></returns>
IModelFilter CreateFilter(IList valuesChosenForFiltering);
}
}

View File

@ -0,0 +1,623 @@
/*
* TextMatchFilter - Text based filtering on ObjectListViews
*
* Author: Phillip Piper
* Date: 31/05/2011 7:45am
*
* Change log:
* v2.6
* 2012-10-13 JPP Allow filtering to consider additional columns
* v2.5.1
* 2011-06-22 JPP Handle searching for empty strings
* v2.5.0
* 2011-05-31 JPP Initial version
*
* TO DO:
*
* Copyright (C) 2011-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
namespace BrightIdeasSoftware {
/// <summary>
/// Instances of this class include only those rows of the listview
/// that match one or more given strings.
/// </summary>
/// <remarks>This class can match strings by prefix, regex, or simple containment.
/// There are factory methods for each of these matching strategies.</remarks>
public class TextMatchFilter : AbstractModelFilter {
#region Life and death
/// <summary>
/// Create a text filter that will include rows where any cell matches
/// any of the given regex expressions.
/// </summary>
/// <param name="olv"></param>
/// <param name="texts"></param>
/// <returns></returns>
/// <remarks>Any string that is not a valid regex expression will be ignored.</remarks>
public static TextMatchFilter Regex(ObjectListView olv, params string[] texts) {
TextMatchFilter filter = new TextMatchFilter(olv);
filter.RegexStrings = texts;
return filter;
}
/// <summary>
/// Create a text filter that includes rows where any cell begins with one of the given strings
/// </summary>
/// <param name="olv"></param>
/// <param name="texts"></param>
/// <returns></returns>
public static TextMatchFilter Prefix(ObjectListView olv, params string[] texts) {
TextMatchFilter filter = new TextMatchFilter(olv);
filter.PrefixStrings = texts;
return filter;
}
/// <summary>
/// Create a text filter that includes rows where any cell contains any of the given strings.
/// </summary>
/// <param name="olv"></param>
/// <param name="texts"></param>
/// <returns></returns>
public static TextMatchFilter Contains(ObjectListView olv, params string[] texts) {
TextMatchFilter filter = new TextMatchFilter(olv);
filter.ContainsStrings = texts;
return filter;
}
/// <summary>
/// Create a TextFilter
/// </summary>
/// <param name="olv"></param>
public TextMatchFilter(ObjectListView olv) {
this.ListView = olv;
}
/// <summary>
/// Create a TextFilter that finds the given string
/// </summary>
/// <param name="olv"></param>
/// <param name="text"></param>
public TextMatchFilter(ObjectListView olv, string text) {
this.ListView = olv;
this.ContainsStrings = new string[] { text };
}
/// <summary>
/// Create a TextFilter that finds the given string using the given comparison
/// </summary>
/// <param name="olv"></param>
/// <param name="text"></param>
/// <param name="comparison"></param>
public TextMatchFilter(ObjectListView olv, string text, StringComparison comparison) {
this.ListView = olv;
this.ContainsStrings = new string[] { text };
this.StringComparison = comparison;
}
#endregion
#region Public properties
/// <summary>
/// Gets or sets which columns will be used for the comparisons? If this is null, all columns will be used
/// </summary>
public OLVColumn[] Columns {
get { return columns; }
set { columns = value; }
}
private OLVColumn[] columns;
/// <summary>
/// Gets or sets additional columns which will be used in the comparison. These will be used
/// in addition to either the Columns property or to all columns taken from the control.
/// </summary>
public OLVColumn[] AdditionalColumns {
get { return additionalColumns; }
set { additionalColumns = value; }
}
private OLVColumn[] additionalColumns;
/// <summary>
/// Gets or sets the collection of strings that will be used for
/// contains matching. Setting this replaces all previous texts
/// of any kind.
/// </summary>
public IEnumerable<string> ContainsStrings {
get {
foreach (TextMatchingStrategy component in this.MatchingStrategies)
yield return component.Text;
}
set {
this.MatchingStrategies = new List<TextMatchingStrategy>();
if (value != null) {
foreach (string text in value)
this.MatchingStrategies.Add(new TextContainsMatchingStrategy(this, text));
}
}
}
/// <summary>
/// Gets whether or not this filter has any search criteria
/// </summary>
public bool HasComponents {
get {
return this.MatchingStrategies.Count > 0;
}
}
/// <summary>
/// Gets or set the ObjectListView upon which this filter will work
/// </summary>
/// <remarks>
/// You cannot really rebase a filter after it is created, so do not change this value.
/// It is included so that it can be set in an object initializer.
/// </remarks>
public ObjectListView ListView {
get { return listView; }
set { listView = value; }
}
private ObjectListView listView;
/// <summary>
/// Gets or sets the collection of strings that will be used for
/// prefix matching. Setting this replaces all previous texts
/// of any kind.
/// </summary>
public IEnumerable<string> PrefixStrings {
get {
foreach (TextMatchingStrategy component in this.MatchingStrategies)
yield return component.Text;
}
set {
this.MatchingStrategies = new List<TextMatchingStrategy>();
if (value != null) {
foreach (string text in value)
this.MatchingStrategies.Add(new TextBeginsMatchingStrategy(this, text));
}
}
}
/// <summary>
/// Gets or sets the options that will be used when compiling the regular expression.
/// </summary>
/// <remarks>
/// This is only used when doing Regex matching (obviously).
/// If this is not set specifically, the appropriate options are chosen to match the
/// StringComparison setting (culture invariant, case sensitive).
/// </remarks>
public RegexOptions RegexOptions {
get {
if (!regexOptions.HasValue) {
switch (this.StringComparison) {
case StringComparison.CurrentCulture:
regexOptions = RegexOptions.None;
break;
case StringComparison.CurrentCultureIgnoreCase:
regexOptions = RegexOptions.IgnoreCase;
break;
case StringComparison.Ordinal:
case StringComparison.InvariantCulture:
regexOptions = RegexOptions.CultureInvariant;
break;
case StringComparison.OrdinalIgnoreCase:
case StringComparison.InvariantCultureIgnoreCase:
regexOptions = RegexOptions.CultureInvariant | RegexOptions.IgnoreCase;
break;
default:
regexOptions = RegexOptions.None;
break;
}
}
return regexOptions.Value;
}
set {
regexOptions = value;
}
}
private RegexOptions? regexOptions;
/// <summary>
/// Gets or sets the collection of strings that will be used for
/// regex pattern matching. Setting this replaces all previous texts
/// of any kind.
/// </summary>
public IEnumerable<string> RegexStrings {
get {
foreach (TextMatchingStrategy component in this.MatchingStrategies)
yield return component.Text;
}
set {
this.MatchingStrategies = new List<TextMatchingStrategy>();
if (value != null) {
foreach (string text in value)
this.MatchingStrategies.Add(new TextRegexMatchingStrategy(this, text));
}
}
}
/// <summary>
/// Gets or sets how the filter will match text
/// </summary>
public StringComparison StringComparison {
get { return this.stringComparison; }
set { this.stringComparison = value; }
}
private StringComparison stringComparison = StringComparison.InvariantCultureIgnoreCase;
#endregion
#region Implementation
/// <summary>
/// Loop over the columns that are being considering by the filter
/// </summary>
/// <returns></returns>
protected virtual IEnumerable<OLVColumn> IterateColumns() {
if (this.Columns == null) {
foreach (OLVColumn column in this.ListView.Columns)
yield return column;
} else {
foreach (OLVColumn column in this.Columns)
yield return column;
}
if (this.AdditionalColumns != null) {
foreach (OLVColumn column in this.AdditionalColumns)
yield return column;
}
}
#endregion
#region Public interface
/// <summary>
/// Do the actual work of filtering
/// </summary>
/// <param name="modelObject"></param>
/// <returns></returns>
public override bool Filter(object modelObject) {
if (this.ListView == null || !this.HasComponents)
return true;
foreach (OLVColumn column in this.IterateColumns()) {
if (column.IsVisible && column.Searchable) {
string cellText = column.GetStringValue(modelObject);
foreach (TextMatchingStrategy filter in this.MatchingStrategies) {
if (String.IsNullOrEmpty(filter.Text) || filter.MatchesText(cellText))
return true;
}
}
}
return false;
}
/// <summary>
/// Find all the ways in which this filter matches the given string.
/// </summary>
/// <remarks>This is used by the renderer to decide which bits of
/// the string should be highlighted</remarks>
/// <param name="cellText"></param>
/// <returns>A list of character ranges indicating the matched substrings</returns>
public IEnumerable<CharacterRange> FindAllMatchedRanges(string cellText) {
List<CharacterRange> ranges = new List<CharacterRange>();
foreach (TextMatchingStrategy filter in this.MatchingStrategies) {
if (!String.IsNullOrEmpty(filter.Text))
ranges.AddRange(filter.FindAllMatchedRanges(cellText));
}
return ranges;
}
/// <summary>
/// Is the given column one of the columns being used by this filter?
/// </summary>
/// <param name="column"></param>
/// <returns></returns>
public bool IsIncluded(OLVColumn column) {
if (this.Columns == null) {
return column.ListView == this.ListView;
}
foreach (OLVColumn x in this.Columns) {
if (x == column)
return true;
}
return false;
}
#endregion
#region Implementation members
private List<TextMatchingStrategy> MatchingStrategies = new List<TextMatchingStrategy>();
#endregion
#region Components
/// <summary>
/// Base class for the various types of string matching that TextMatchFilter provides
/// </summary>
abstract protected class TextMatchingStrategy {
/// <summary>
/// Gets how the filter will match text
/// </summary>
public StringComparison StringComparison {
get { return this.TextFilter.StringComparison; }
}
/// <summary>
/// Gets the text filter to which this component belongs
/// </summary>
public TextMatchFilter TextFilter {
get { return textFilter; }
set { textFilter = value; }
}
private TextMatchFilter textFilter;
/// <summary>
/// Gets or sets the text that will be matched
/// </summary>
public string Text {
get { return this.text; }
set { this.text = value; }
}
private string text;
/// <summary>
/// Find all the ways in which this filter matches the given string.
/// </summary>
/// <remarks>
/// <para>
/// This is used by the renderer to decide which bits of
/// the string should be highlighted.
/// </para>
/// <para>this.Text will not be null or empty when this is called.</para>
/// </remarks>
/// <param name="cellText">The text of the cell we want to search</param>
/// <returns>A list of character ranges indicating the matched substrings</returns>
abstract public IEnumerable<CharacterRange> FindAllMatchedRanges(string cellText);
/// <summary>
/// Does the given text match the filter
/// </summary>
/// <remarks>
/// <para>this.Text will not be null or empty when this is called.</para>
/// </remarks>
/// <param name="cellText">The text of the cell we want to search</param>
/// <returns>Return true if the given cellText matches our strategy</returns>
abstract public bool MatchesText(string cellText);
}
/// <summary>
/// This component provides text contains matching strategy.
/// </summary>
protected class TextContainsMatchingStrategy : TextMatchingStrategy {
/// <summary>
/// Create a text contains strategy
/// </summary>
/// <param name="filter"></param>
/// <param name="text"></param>
public TextContainsMatchingStrategy(TextMatchFilter filter, string text) {
this.TextFilter = filter;
this.Text = text;
}
/// <summary>
/// Does the given text match the filter
/// </summary>
/// <remarks>
/// <para>this.Text will not be null or empty when this is called.</para>
/// </remarks>
/// <param name="cellText">The text of the cell we want to search</param>
/// <returns>Return true if the given cellText matches our strategy</returns>
override public bool MatchesText(string cellText) {
return cellText.IndexOf(this.Text, this.StringComparison) != -1;
}
/// <summary>
/// Find all the ways in which this filter matches the given string.
/// </summary>
/// <remarks>
/// <para>
/// This is used by the renderer to decide which bits of
/// the string should be highlighted.
/// </para>
/// <para>this.Text will not be null or empty when this is called.</para>
/// </remarks>
/// <param name="cellText">The text of the cell we want to search</param>
/// <returns>A list of character ranges indicating the matched substrings</returns>
override public IEnumerable<CharacterRange> FindAllMatchedRanges(string cellText) {
List<CharacterRange> ranges = new List<CharacterRange>();
int matchIndex = cellText.IndexOf(this.Text, this.StringComparison);
while (matchIndex != -1) {
ranges.Add(new CharacterRange(matchIndex, this.Text.Length));
matchIndex = cellText.IndexOf(this.Text, matchIndex + this.Text.Length, this.StringComparison);
}
return ranges;
}
}
/// <summary>
/// This component provides text begins with matching strategy.
/// </summary>
protected class TextBeginsMatchingStrategy : TextMatchingStrategy {
/// <summary>
/// Create a text begins strategy
/// </summary>
/// <param name="filter"></param>
/// <param name="text"></param>
public TextBeginsMatchingStrategy(TextMatchFilter filter, string text) {
this.TextFilter = filter;
this.Text = text;
}
/// <summary>
/// Does the given text match the filter
/// </summary>
/// <remarks>
/// <para>this.Text will not be null or empty when this is called.</para>
/// </remarks>
/// <param name="cellText">The text of the cell we want to search</param>
/// <returns>Return true if the given cellText matches our strategy</returns>
override public bool MatchesText(string cellText) {
return cellText.StartsWith(this.Text, this.StringComparison);
}
/// <summary>
/// Find all the ways in which this filter matches the given string.
/// </summary>
/// <remarks>
/// <para>
/// This is used by the renderer to decide which bits of
/// the string should be highlighted.
/// </para>
/// <para>this.Text will not be null or empty when this is called.</para>
/// </remarks>
/// <param name="cellText">The text of the cell we want to search</param>
/// <returns>A list of character ranges indicating the matched substrings</returns>
override public IEnumerable<CharacterRange> FindAllMatchedRanges(string cellText) {
List<CharacterRange> ranges = new List<CharacterRange>();
if (cellText.StartsWith(this.Text, this.StringComparison))
ranges.Add(new CharacterRange(0, this.Text.Length));
return ranges;
}
}
/// <summary>
/// This component provides regex matching strategy.
/// </summary>
protected class TextRegexMatchingStrategy : TextMatchingStrategy {
/// <summary>
/// Creates a regex strategy
/// </summary>
/// <param name="filter"></param>
/// <param name="text"></param>
public TextRegexMatchingStrategy(TextMatchFilter filter, string text) {
this.TextFilter = filter;
this.Text = text;
}
/// <summary>
/// Gets or sets the options that will be used when compiling the regular expression.
/// </summary>
public RegexOptions RegexOptions {
get {
return this.TextFilter.RegexOptions;
}
}
/// <summary>
/// Gets or sets a compilex regular expression, based on our current Text and RegexOptions.
/// </summary>
/// <remarks>
/// If Text fails to compile as a regular expression, this will return a Regex object
/// that will match all strings.
/// </remarks>
protected Regex Regex {
get {
if (this.regex == null) {
try {
this.regex = new Regex(this.Text, this.RegexOptions);
}
catch (ArgumentException) {
this.regex = TextRegexMatchingStrategy.InvalidRegexMarker;
}
}
return this.regex;
}
set {
this.regex = value;
}
}
private Regex regex;
/// <summary>
/// Gets whether or not our current regular expression is a valid regex
/// </summary>
protected bool IsRegexInvalid {
get {
return this.Regex == TextRegexMatchingStrategy.InvalidRegexMarker;
}
}
static private Regex InvalidRegexMarker = new Regex(".*");
/// <summary>
/// Does the given text match the filter
/// </summary>
/// <remarks>
/// <para>this.Text will not be null or empty when this is called.</para>
/// </remarks>
/// <param name="cellText">The text of the cell we want to search</param>
/// <returns>Return true if the given cellText matches our strategy</returns>
public override bool MatchesText(string cellText) {
if (this.IsRegexInvalid)
return true;
return this.Regex.Match(cellText).Success;
}
/// <summary>
/// Find all the ways in which this filter matches the given string.
/// </summary>
/// <remarks>
/// <para>
/// This is used by the renderer to decide which bits of
/// the string should be highlighted.
/// </para>
/// <para>this.Text will not be null or empty when this is called.</para>
/// </remarks>
/// <param name="cellText">The text of the cell we want to search</param>
/// <returns>A list of character ranges indicating the matched substrings</returns>
override public IEnumerable<CharacterRange> FindAllMatchedRanges(string cellText) {
List<CharacterRange> ranges = new List<CharacterRange>();
if (!this.IsRegexInvalid) {
foreach (Match match in this.Regex.Matches(cellText)) {
if (match.Length > 0)
ranges.Add(new CharacterRange(match.Index, match.Length));
}
}
return ranges;
}
}
#endregion
}
}

1261
FullClassDiagram.cd Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,335 @@
/*
* Attributes - Attributes that can be attached to properties of models to allow columns to be
* built from them directly
*
* Author: Phillip Piper
* Date: 15/08/2009 22:01
*
* Change log:
* v2.6
* 2012-08-16 JPP - Added [OLVChildren] and [OLVIgnore]
* - OLV attributes can now only be set on properties
* v2.4
* 2010-04-14 JPP - Allow Name property to be set
*
* v2.3
* 2009-08-15 JPP - Initial version
*
* To do:
*
* Copyright (C) 2009-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace BrightIdeasSoftware
{
/// <summary>
/// This attribute is used to mark a property of a model
/// class that should be noticed by Generator class.
/// </summary>
/// <remarks>
/// All the attributes of this class match their equivilent properties on OLVColumn.
/// </remarks>
[AttributeUsage(AttributeTargets.Property)]
public class OLVColumnAttribute : Attribute
{
#region Constructor
// There are several property where we actually want nullable value (bool?, int?),
// but it seems attribute properties can't be nullable types.
// So we explicitly track if those properties have been set.
/// <summary>
/// Create a new OLVColumnAttribute
/// </summary>
public OLVColumnAttribute() {
}
/// <summary>
/// Create a new OLVColumnAttribute with the given title
/// </summary>
/// <param name="title">The title of the column</param>
public OLVColumnAttribute(string title) {
this.Title = title;
}
#endregion
#region Public properties
/// <summary>
///
/// </summary>
public string AspectToStringFormat {
get { return aspectToStringFormat; }
set { aspectToStringFormat = value; }
}
private string aspectToStringFormat;
/// <summary>
///
/// </summary>
public bool CheckBoxes {
get { return checkBoxes; }
set {
checkBoxes = value;
this.IsCheckBoxesSet = true;
}
}
private bool checkBoxes;
internal bool IsCheckBoxesSet = false;
/// <summary>
///
/// </summary>
public int DisplayIndex {
get { return displayIndex; }
set { displayIndex = value; }
}
private int displayIndex = -1;
/// <summary>
///
/// </summary>
public bool FillsFreeSpace {
get { return fillsFreeSpace; }
set { fillsFreeSpace = value; }
}
private bool fillsFreeSpace;
/// <summary>
///
/// </summary>
public int FreeSpaceProportion {
get { return freeSpaceProportion; }
set {
freeSpaceProportion = value;
IsFreeSpaceProportionSet = true;
}
}
private int freeSpaceProportion;
internal bool IsFreeSpaceProportionSet = false;
/// <summary>
/// An array of IComparables that mark the cutoff points for values when
/// grouping on this column.
/// </summary>
public object[] GroupCutoffs {
get { return groupCutoffs; }
set { groupCutoffs = value; }
}
private object[] groupCutoffs;
/// <summary>
///
/// </summary>
public string[] GroupDescriptions {
get { return groupDescriptions; }
set { groupDescriptions = value; }
}
private string[] groupDescriptions;
/// <summary>
///
/// </summary>
public string GroupWithItemCountFormat {
get { return groupWithItemCountFormat; }
set { groupWithItemCountFormat = value; }
}
private string groupWithItemCountFormat;
/// <summary>
///
/// </summary>
public string GroupWithItemCountSingularFormat {
get { return groupWithItemCountSingularFormat; }
set { groupWithItemCountSingularFormat = value; }
}
private string groupWithItemCountSingularFormat;
/// <summary>
///
/// </summary>
public bool Hyperlink {
get { return hyperlink; }
set { hyperlink = value; }
}
private bool hyperlink;
/// <summary>
///
/// </summary>
public string ImageAspectName {
get { return imageAspectName; }
set { imageAspectName = value; }
}
private string imageAspectName;
/// <summary>
///
/// </summary>
public bool IsEditable {
get { return isEditable; }
set {
isEditable = value;
this.IsEditableSet = true;
}
}
private bool isEditable = true;
internal bool IsEditableSet = false;
/// <summary>
///
/// </summary>
public bool IsVisible {
get { return isVisible; }
set { isVisible = value; }
}
private bool isVisible = true;
/// <summary>
///
/// </summary>
public bool IsTileViewColumn {
get { return isTileViewColumn; }
set { isTileViewColumn = value; }
}
private bool isTileViewColumn;
/// <summary>
///
/// </summary>
public int MaximumWidth {
get { return maximumWidth; }
set { maximumWidth = value; }
}
private int maximumWidth = -1;
/// <summary>
///
/// </summary>
public int MinimumWidth {
get { return minimumWidth; }
set { minimumWidth = value; }
}
private int minimumWidth = -1;
/// <summary>
///
/// </summary>
public String Name {
get { return name; }
set { name = value; }
}
private String name;
/// <summary>
///
/// </summary>
public HorizontalAlignment TextAlign {
get { return this.textAlign; }
set {
this.textAlign = value;
IsTextAlignSet = true;
}
}
private HorizontalAlignment textAlign = HorizontalAlignment.Left;
internal bool IsTextAlignSet = false;
/// <summary>
///
/// </summary>
public String Tag {
get { return tag; }
set { tag = value; }
}
private String tag;
/// <summary>
///
/// </summary>
public String Title {
get { return title; }
set { title = value; }
}
private String title;
/// <summary>
///
/// </summary>
public String ToolTipText {
get { return toolTipText; }
set { toolTipText = value; }
}
private String toolTipText;
/// <summary>
///
/// </summary>
public bool TriStateCheckBoxes {
get { return triStateCheckBoxes; }
set {
triStateCheckBoxes = value;
this.IsTriStateCheckBoxesSet = true;
}
}
private bool triStateCheckBoxes;
internal bool IsTriStateCheckBoxesSet = false;
/// <summary>
///
/// </summary>
public bool UseInitialLetterForGroup {
get { return useInitialLetterForGroup; }
set { useInitialLetterForGroup = value; }
}
private bool useInitialLetterForGroup;
/// <summary>
///
/// </summary>
public int Width {
get { return width; }
set { width = value; }
}
private int width = 150;
#endregion
}
/// <summary>
/// Properties marked with [OLVChildren] will be used as the children source in a TreeListView.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class OLVChildrenAttribute : Attribute
{
}
/// <summary>
/// Properties marked with [OLVIgnore] will not have columns generated for them.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class OLVIgnoreAttribute : Attribute
{
}
}

291
Implementation/Comparers.cs Normal file
View File

@ -0,0 +1,291 @@
/*
* Comparers - Various Comparer classes used within ObjectListView
*
* Author: Phillip Piper
* Date: 25/11/2008 17:15
*
* Change log:
* v2.3
* 2009-08-24 JPP - Added OLVGroupComparer
* 2009-06-01 JPP - ModelObjectComparer would crash if secondary sort column was null.
* 2008-12-20 JPP - Fixed bug with group comparisons when a group key was null (SF#2445761)
* 2008-11-25 JPP Initial version
*
* TO DO:
*
* Copyright (C) 2006-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
namespace BrightIdeasSoftware
{
/// <summary>
/// ColumnComparer is the workhorse for all comparison between two values of a particular column.
/// If the column has a specific comparer, use that to compare the values. Otherwise, do
/// a case insensitive string compare of the string representations of the values.
/// </summary>
/// <remarks><para>This class inherits from both IComparer and its generic counterpart
/// so that it can be used on untyped and typed collections.</para></remarks>
public class ColumnComparer : IComparer, IComparer<OLVListItem>
{
/// <summary>
/// Create a ColumnComparer that will order the rows in a list view according
/// to the values in a given column
/// </summary>
/// <param name="col">The column whose values will be compared</param>
/// <param name="order">The ordering for column values</param>
public ColumnComparer(OLVColumn col, SortOrder order)
{
this.column = col;
this.sortOrder = order;
}
/// <summary>
/// Create a ColumnComparer that will order the rows in a list view according
/// to the values in a given column, and by a secondary column if the primary
/// column is equal.
/// </summary>
/// <param name="col">The column whose values will be compared</param>
/// <param name="order">The ordering for column values</param>
/// <param name="col2">The column whose values will be compared for secondary sorting</param>
/// <param name="order2">The ordering for secondary column values</param>
public ColumnComparer(OLVColumn col, SortOrder order, OLVColumn col2, SortOrder order2)
: this(col, order)
{
// There is no point in secondary sorting on the same column
if (col != col2)
this.secondComparer = new ColumnComparer(col2, order2);
}
/// <summary>
/// Compare two rows
/// </summary>
/// <param name="x">row1</param>
/// <param name="y">row2</param>
/// <returns>An ordering indication: -1, 0, 1</returns>
public int Compare(object x, object y)
{
return this.Compare((OLVListItem)x, (OLVListItem)y);
}
/// <summary>
/// Compare two rows
/// </summary>
/// <param name="x">row1</param>
/// <param name="y">row2</param>
/// <returns>An ordering indication: -1, 0, 1</returns>
public int Compare(OLVListItem x, OLVListItem y)
{
if (this.sortOrder == SortOrder.None)
return 0;
int result = 0;
object x1 = this.column.GetValue(x.RowObject);
object y1 = this.column.GetValue(y.RowObject);
// Handle nulls. Null values come last
bool xIsNull = (x1 == null || x1 == System.DBNull.Value);
bool yIsNull = (y1 == null || y1 == System.DBNull.Value);
if (xIsNull || yIsNull) {
if (xIsNull && yIsNull)
result = 0;
else
result = (xIsNull ? -1 : 1);
} else {
result = this.CompareValues(x1, y1);
}
if (this.sortOrder == SortOrder.Descending)
result = 0 - result;
// If the result was equality, use the secondary comparer to resolve it
if (result == 0 && this.secondComparer != null)
result = this.secondComparer.Compare(x, y);
return result;
}
/// <summary>
/// Compare the actual values to be used for sorting
/// </summary>
/// <param name="x">The aspect extracted from the first row</param>
/// <param name="y">The aspect extracted from the second row</param>
/// <returns>An ordering indication: -1, 0, 1</returns>
public int CompareValues(object x, object y)
{
// Force case insensitive compares on strings
String xAsString = x as String;
if (xAsString != null)
return String.Compare(xAsString, (String)y, StringComparison.CurrentCultureIgnoreCase);
else {
IComparable comparable = x as IComparable;
if (comparable != null)
return comparable.CompareTo(y);
else
return 0;
}
}
private OLVColumn column;
private SortOrder sortOrder;
private ColumnComparer secondComparer;
}
/// <summary>
/// This comparer sort list view groups. OLVGroups have a "SortValue" property,
/// which is used if present. Otherwise, the titles of the groups will be compared.
/// </summary>
public class OLVGroupComparer : IComparer<OLVGroup>
{
/// <summary>
/// Create a group comparer
/// </summary>
/// <param name="order">The ordering for column values</param>
public OLVGroupComparer(SortOrder order) {
this.sortOrder = order;
}
/// <summary>
/// Compare the two groups. OLVGroups have a "SortValue" property,
/// which is used if present. Otherwise, the titles of the groups will be compared.
/// </summary>
/// <param name="x">group1</param>
/// <param name="y">group2</param>
/// <returns>An ordering indication: -1, 0, 1</returns>
public int Compare(OLVGroup x, OLVGroup y) {
// If we can compare the sort values, do that.
// Otherwise do a case insensitive compare on the group header.
int result;
if (x.SortValue != null && y.SortValue != null)
result = x.SortValue.CompareTo(y.SortValue);
else
result = String.Compare(x.Header, y.Header, StringComparison.CurrentCultureIgnoreCase);
if (this.sortOrder == SortOrder.Descending)
result = 0 - result;
return result;
}
private SortOrder sortOrder;
}
/// <summary>
/// This comparer can be used to sort a collection of model objects by a given column
/// </summary>
public class ModelObjectComparer : IComparer, IComparer<object>
{
/// <summary>
/// Create a model object comparer
/// </summary>
/// <param name="col"></param>
/// <param name="order"></param>
public ModelObjectComparer(OLVColumn col, SortOrder order)
{
this.column = col;
this.sortOrder = order;
}
/// <summary>
/// Create a model object comparer with a secondary sorting column
/// </summary>
/// <param name="col"></param>
/// <param name="order"></param>
/// <param name="col2"></param>
/// <param name="order2"></param>
public ModelObjectComparer(OLVColumn col, SortOrder order, OLVColumn col2, SortOrder order2)
: this(col, order)
{
// There is no point in secondary sorting on the same column
if (col != col2 && col2 != null && order2 != SortOrder.None)
this.secondComparer = new ModelObjectComparer(col2, order2);
}
/// <summary>
/// Compare the two model objects
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public int Compare(object x, object y)
{
int result = 0;
object x1 = this.column.GetValue(x);
object y1 = this.column.GetValue(y);
if (this.sortOrder == SortOrder.None)
return 0;
// Handle nulls. Null values come last
bool xIsNull = (x1 == null || x1 == System.DBNull.Value);
bool yIsNull = (y1 == null || y1 == System.DBNull.Value);
if (xIsNull || yIsNull) {
if (xIsNull && yIsNull)
result = 0;
else
result = (xIsNull ? -1 : 1);
} else {
result = this.CompareValues(x1, y1);
}
if (this.sortOrder == SortOrder.Descending)
result = 0 - result;
// If the result was equality, use the secondary comparer to resolve it
if (result == 0 && this.secondComparer != null)
result = this.secondComparer.Compare(x, y);
return result;
}
/// <summary>
/// Compare the actual values
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public int CompareValues(object x, object y)
{
// Force case insensitive compares on strings
String xStr = x as String;
if (xStr != null)
return String.Compare(xStr, (String)y, StringComparison.CurrentCultureIgnoreCase);
else {
IComparable comparable = x as IComparable;
if (comparable != null)
return comparable.CompareTo(y);
else
return 0;
}
}
private OLVColumn column;
private SortOrder sortOrder;
private ModelObjectComparer secondComparer;
#region IComparer<object> Members
#endregion
}
}

View File

@ -0,0 +1,613 @@
/*
* DataSourceAdapter - A helper class that translates DataSource events for an ObjectListView
*
* Author: Phillip Piper
* Date: 20/09/2010 7:42 AM
*
* Change log:
* v2.6
* 2012-08-16 JPP - Unify common column creation functionality with Generator when possible
*
* 2010-09-20 JPP - Initial version
*
* Copyright (C) 2010-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing.Design;
using System.Globalization;
using System.Windows.Forms;
using System.Diagnostics;
namespace BrightIdeasSoftware
{
/// <summary>
/// A helper class that translates DataSource events for an ObjectListView
/// </summary>
public class DataSourceAdapter : IDisposable
{
#region Life and death
/// <summary>
/// Make a DataSourceAdapter
/// </summary>
public DataSourceAdapter(ObjectListView olv) {
if (olv == null) throw new ArgumentNullException("olv");
this.ListView = olv;
this.BindListView(this.ListView);
}
/// <summary>
/// Finalize this object
/// </summary>
~DataSourceAdapter() {
this.Dispose(false);
}
/// <summary>
/// Release all the resources used by this instance
/// </summary>
public void Dispose() {
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Release all the resources used by this instance
/// </summary>
public virtual void Dispose(bool fromUser) {
this.UnbindListView(this.ListView);
this.UnbindDataSource();
}
#endregion
#region Public Properties
/// <summary>
/// Gets or sets whether or not columns will be automatically generated to show the
/// columns when the DataSource is set.
/// </summary>
/// <remarks>This must be set before the DataSource is set. It has no effect afterwards.</remarks>
public bool AutoGenerateColumns {
get { return this.autoGenerateColumns; }
set { this.autoGenerateColumns = value; }
}
private bool autoGenerateColumns = true;
/// <summary>
/// Get or set the DataSource that will be displayed in this list view.
/// </summary>
public virtual Object DataSource {
get { return dataSource; }
set {
dataSource = value;
this.RebindDataSource(true);
}
}
private Object dataSource;
/// <summary>
/// Gets or sets the name of the list or table in the data source for which the DataListView is displaying data.
/// </summary>
/// <remarks>If the data source is not a DataSet or DataViewManager, this property has no effect</remarks>
public virtual string DataMember {
get { return dataMember; }
set {
if (dataMember != value) {
dataMember = value;
RebindDataSource();
}
}
}
private string dataMember = "";
/// <summary>
/// Gets the ObjectListView upon which this adaptor will operate
/// </summary>
public ObjectListView ListView {
get { return listView; }
internal set { listView = value; }
}
private ObjectListView listView;
#endregion
#region Implementation properties
/// <summary>
/// Gets or sets the currency manager which is handling our binding context
/// </summary>
protected CurrencyManager CurrencyManager {
get { return currencyManager; }
set { currencyManager = value; }
}
private CurrencyManager currencyManager = null;
#endregion
#region Binding and unbinding
/// <summary>
///
/// </summary>
/// <param name="olv"></param>
protected virtual void BindListView(ObjectListView olv) {
if (olv == null)
return;
olv.Freezing += new EventHandler<FreezeEventArgs>(HandleListViewFreezing);
olv.SelectedIndexChanged += new EventHandler(HandleListViewSelectedIndexChanged);
olv.BindingContextChanged += new EventHandler(HandleListViewBindingContextChanged);
}
/// <summary>
///
/// </summary>
/// <param name="olv"></param>
protected virtual void UnbindListView(ObjectListView olv) {
if (olv == null)
return;
olv.Freezing -= new EventHandler<FreezeEventArgs>(HandleListViewFreezing);
olv.SelectedIndexChanged -= new EventHandler(HandleListViewSelectedIndexChanged);
olv.BindingContextChanged -= new EventHandler(HandleListViewBindingContextChanged);
}
/// <summary>
///
/// </summary>
protected virtual void BindDataSource() {
if (this.CurrencyManager == null)
return;
this.CurrencyManager.MetaDataChanged += new EventHandler(HandleCurrencyManagerMetaDataChanged);
this.CurrencyManager.PositionChanged += new EventHandler(HandleCurrencyManagerPositionChanged);
this.CurrencyManager.ListChanged += new ListChangedEventHandler(CurrencyManagerListChanged);
}
/// <summary>
///
/// </summary>
protected virtual void UnbindDataSource() {
if (this.CurrencyManager == null)
return;
this.CurrencyManager.MetaDataChanged -= new EventHandler(HandleCurrencyManagerMetaDataChanged);
this.CurrencyManager.PositionChanged -= new EventHandler(HandleCurrencyManagerPositionChanged);
this.CurrencyManager.ListChanged -= new ListChangedEventHandler(CurrencyManagerListChanged);
}
#endregion
#region Initialization
/// <summary>
/// Our data source has changed. Figure out how to handle the new source
/// </summary>
protected virtual void RebindDataSource() {
RebindDataSource(false);
}
/// <summary>
/// Our data source has changed. Figure out how to handle the new source
/// </summary>
protected virtual void RebindDataSource(bool forceDataInitialization) {
CurrencyManager tempCurrencyManager = null;
if (this.ListView != null && this.ListView.BindingContext != null && this.DataSource != null) {
tempCurrencyManager = this.ListView.BindingContext[this.DataSource, this.DataMember] as CurrencyManager;
}
// Has our currency manager changed?
if (this.CurrencyManager != tempCurrencyManager) {
this.UnbindDataSource();
this.CurrencyManager = tempCurrencyManager;
this.BindDataSource();
// Our currency manager has changed so we have to initialize a new data source
forceDataInitialization = true;
}
if (forceDataInitialization)
InitializeDataSource();
}
/// <summary>
/// The data source for this control has changed. Reconfigure the control for the new source
/// </summary>
protected virtual void InitializeDataSource() {
if (this.ListView.Frozen || this.CurrencyManager == null)
return;
this.CreateColumnsFromSource();
this.CreateMissingAspectGettersAndPutters();
this.SetListContents();
this.ListView.AutoSizeColumns();
}
/// <summary>
/// Take the contents of the currently bound list and put them into the control
/// </summary>
protected virtual void SetListContents() {
this.ListView.Objects = this.CurrencyManager.List;
}
/// <summary>
/// Create columns for the listview based on what properties are available in the data source
/// </summary>
/// <remarks>
/// <para>This method will create columns if there is not already a column displaying that property.</para>
/// </remarks>
protected virtual void CreateColumnsFromSource() {
if (this.CurrencyManager == null)
return;
// Don't generate any columns in design mode. If we do, the user will see them,
// but the Designer won't know about them and won't persist them, which is very confusing
if (this.ListView.IsDesignMode)
return;
// Don't create columns if we've been told not to
if (!this.AutoGenerateColumns)
return;
// Use a Generator to create columns
Generator generator = Generator.Instance as Generator ?? new Generator();
PropertyDescriptorCollection properties = this.CurrencyManager.GetItemProperties();
if (properties.Count == 0)
return;
foreach (PropertyDescriptor property in properties) {
if (!this.ShouldCreateColumn(property))
continue;
// Create a column
OLVColumn column = generator.MakeColumnFromPropertyDescriptor(property);
this.ConfigureColumn(column, property);
// Add it to our list
this.ListView.AllColumns.Add(column);
}
generator.PostCreateColumns(this.ListView);
}
/// <summary>
/// Decide if a new column should be added to the control to display
/// the given property
/// </summary>
/// <param name="property"></param>
/// <returns></returns>
protected virtual bool ShouldCreateColumn(PropertyDescriptor property) {
// Is there a column that already shows this property? If so, we don't show it again
if (this.ListView.AllColumns.Exists(delegate(OLVColumn x) { return x.AspectName == property.Name; }))
return false;
// Relationships to other tables turn up as IBindibleLists. Don't make columns to show them.
// CHECK: Is this always true? What other things could be here? Constraints? Triggers?
if (property.PropertyType == typeof(IBindingList))
return false;
// Ignore anything marked with [OLVIgnore]
return property.Attributes[typeof(OLVIgnoreAttribute)] == null;
}
/// <summary>
/// Configure the given column to show the given property.
/// The title and aspect name of the column are already filled in.
/// </summary>
/// <param name="column"></param>
/// <param name="property"></param>
protected virtual void ConfigureColumn(OLVColumn column, PropertyDescriptor property) {
column.LastDisplayIndex = this.ListView.AllColumns.Count;
// If our column is a BLOB, it could be an image, so assign a renderer to draw it.
// CONSIDER: Is this a common enough case to warrant this code?
if (property.PropertyType == typeof(System.Byte[]))
column.Renderer = new ImageRenderer();
}
/// <summary>
/// Generate aspect getters and putters for any columns that are missing them (and for which we have
/// enough information to actually generate a getter)
/// </summary>
protected virtual void CreateMissingAspectGettersAndPutters() {
foreach (OLVColumn x in this.ListView.AllColumns) {
OLVColumn column = x; // stack based variable accessible from closures
if (column.AspectGetter == null && !String.IsNullOrEmpty(column.AspectName)) {
column.AspectGetter = delegate(object row) {
// In most cases, rows will be DataRowView objects
DataRowView drv = row as DataRowView;
if (drv == null)
return column.GetAspectByName(row);
return (drv.Row.RowState == DataRowState.Detached) ? null : drv[column.AspectName];
};
}
if (column.IsEditable && column.AspectPutter == null && !String.IsNullOrEmpty(column.AspectName)) {
column.AspectPutter = delegate(object row, object newValue) {
// In most cases, rows will be DataRowView objects
DataRowView drv = row as DataRowView;
if (drv == null)
column.PutAspectByName(row, newValue);
else {
if (drv.Row.RowState != DataRowState.Detached)
drv[column.AspectName] = newValue;
}
};
}
}
}
#endregion
#region Event Handlers
/// <summary>
/// CurrencyManager ListChanged event handler.
/// Deals with fine-grained changes to list items.
/// </summary>
/// <remarks>
/// It's actually difficult to deal with these changes in a fine-grained manner.
/// If our listview is grouped, then any change may make a new group appear or
/// an old group disappear. It is rarely enough to simply update the affected row.
/// </remarks>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void CurrencyManagerListChanged(object sender, ListChangedEventArgs e) {
Debug.Assert(sender == this.CurrencyManager);
// Ignore changes make while frozen, since we will do a complete rebuild when we unfreeze
if (this.ListView.Frozen)
return;
//System.Diagnostics.Debug.WriteLine(e.ListChangedType);
//Stopwatch sw = new Stopwatch();
//sw.Start();
switch (e.ListChangedType) {
case ListChangedType.Reset:
this.HandleListChangedReset(e);
break;
case ListChangedType.ItemChanged:
this.HandleListChangedItemChanged(e);
break;
case ListChangedType.ItemAdded:
this.HandleListChangedItemAdded(e);
break;
// An item has gone away.
case ListChangedType.ItemDeleted:
this.HandleListChangedItemDeleted(e);
break;
// An item has changed its index.
case ListChangedType.ItemMoved:
this.HandleListChangedItemMoved(e);
break;
// Something has changed in the metadata.
// CHECK: When are these events actually fired?
case ListChangedType.PropertyDescriptorAdded:
case ListChangedType.PropertyDescriptorChanged:
case ListChangedType.PropertyDescriptorDeleted:
this.HandleListChangedMetadataChanged(e);
break;
}
//sw.Stop();
//System.Diagnostics.Debug.WriteLine(String.Format("Processing {0} event on {1} rows took {2}ms", e.ListChangedType, this.ListView.GetItemCount(), sw.ElapsedMilliseconds));
}
/// <summary>
/// Handle PropertyDescriptor* events
/// </summary>
/// <param name="e"></param>
protected virtual void HandleListChangedMetadataChanged(ListChangedEventArgs e) {
this.InitializeDataSource();
}
/// <summary>
/// Handle ItemMoved event
/// </summary>
/// <param name="e"></param>
protected virtual void HandleListChangedItemMoved(ListChangedEventArgs e) {
// When is this actually triggered?
this.InitializeDataSource();
}
/// <summary>
/// Handle the ItemDeleted event
/// </summary>
/// <param name="e"></param>
protected virtual void HandleListChangedItemDeleted(ListChangedEventArgs e) {
this.InitializeDataSource();
}
/// <summary>
/// Handle an ItemAdded event.
/// </summary>
/// <param name="e"></param>
protected virtual void HandleListChangedItemAdded(ListChangedEventArgs e) {
// We get this event twice if certain grid controls are used to add a new row to a
// datatable: once when the editing of a new row begins, and once again when that
// editing commits. (If the user cancels the creation of the new row, we never see
// the second creation.) We detect this by seeing if this is a view on a row in a
// DataTable, and if it is, testing to see if it's a new row under creation.
Object newRow = this.CurrencyManager.List[e.NewIndex];
DataRowView drv = newRow as DataRowView;
if (drv == null || !drv.IsNew) {
// Either we're not dealing with a view on a data table, or this is the commit
// notification. Either way, this is the final notification, so we want to
// handle the new row now!
this.InitializeDataSource();
}
}
/// <summary>
/// Handle the Reset event
/// </summary>
/// <param name="e"></param>
protected virtual void HandleListChangedReset(ListChangedEventArgs e) {
// The whole list has changed utterly, so reload it.
this.InitializeDataSource();
}
/// <summary>
/// Handle ItemChanged event. This is triggered when a single item
/// has changed, so just refresh that one item.
/// </summary>
/// <param name="e"></param>
/// <remarks>Even in this simple case, we should probably rebuild the list.
/// For example, the change could put the item into its own new group.</remarks>
protected virtual void HandleListChangedItemChanged(ListChangedEventArgs e) {
// A single item has changed, so just refresh that.
//System.Diagnostics.Debug.WriteLine(String.Format("HandleListChangedItemChanged: {0}, {1}", e.NewIndex, e.PropertyDescriptor.Name));
Object changedRow = this.CurrencyManager.List[e.NewIndex];
this.ListView.RefreshObject(changedRow);
}
/// <summary>
/// The CurrencyManager calls this if the data source looks
/// different. We just reload everything.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <remarks>
/// CHECK: Do we need this if we are handle ListChanged metadata events?
/// </remarks>
protected virtual void HandleCurrencyManagerMetaDataChanged(object sender, EventArgs e) {
this.InitializeDataSource();
}
/// <summary>
/// Called by the CurrencyManager when the currently selected item
/// changes. We update the ListView selection so that we stay in sync
/// with any other controls bound to the same source.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void HandleCurrencyManagerPositionChanged(object sender, EventArgs e) {
int index = this.CurrencyManager.Position;
// Make sure the index is sane (-1 pops up from time to time)
if (index < 0 || index >= this.ListView.GetItemCount())
return;
// Avoid recursion. If we are currently changing the index, don't
// start the process again.
if (this.isChangingIndex)
return;
try {
this.isChangingIndex = true;
this.ChangePosition(index);
}
finally {
this.isChangingIndex = false;
}
}
private bool isChangingIndex = false;
/// <summary>
/// Change the control's position (which is it's currently selected row)
/// to the nth row in the dataset
/// </summary>
/// <param name="index">The index of the row to be selected</param>
protected virtual void ChangePosition(int index) {
// We can't use the index directly, since our listview may be sorted
this.ListView.SelectedObject = this.CurrencyManager.List[index];
// THINK: Do we always want to bring it into view?
if (this.ListView.SelectedIndices.Count > 0)
this.ListView.EnsureVisible(this.ListView.SelectedIndices[0]);
}
#endregion
#region ObjectListView event handlers
/// <summary>
/// Handle the selection changing in our ListView.
/// We need to tell our currency manager about the new position.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void HandleListViewSelectedIndexChanged(object sender, EventArgs e) {
// Prevent recursion
if (this.isChangingIndex)
return;
// If we are bound to a data source, and only one item is selected,
// tell the currency manager which item is selected.
if (this.ListView.SelectedIndices.Count == 1 && this.CurrencyManager != null) {
try {
this.isChangingIndex = true;
// We can't use the selectedIndex directly, since our listview may be sorted.
// So we have to find the index of the selected object within the original list.
this.CurrencyManager.Position = this.CurrencyManager.List.IndexOf(this.ListView.SelectedObject);
} finally {
this.isChangingIndex = false;
}
}
}
/// <summary>
/// Handle the frozenness of our ListView changing.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void HandleListViewFreezing(object sender, FreezeEventArgs e) {
if (!alreadyFreezing && e.FreezeLevel == 0) {
try {
alreadyFreezing = true;
this.RebindDataSource(true);
} finally {
alreadyFreezing = false;
}
}
}
private bool alreadyFreezing = false;
/// <summary>
/// Handle a change to the BindingContext of our ListView.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void HandleListViewBindingContextChanged(object sender, EventArgs e) {
this.RebindDataSource(false);
}
#endregion
}
}

151
Implementation/Delegates.cs Normal file
View File

@ -0,0 +1,151 @@
/*
* Delegates - All delegate definitions used in ObjectListView
*
* Author: Phillip Piper
* Date: 31-March-2011 5:53 pm
*
* Change log:
* 2011-03-31 JPP - Split into its own file
*
* Copyright (C) 2011-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace BrightIdeasSoftware {
#region Delegate declarations
/// <summary>
/// These delegates are used to extract an aspect from a row object
/// </summary>
public delegate Object AspectGetterDelegate(Object rowObject);
/// <summary>
/// These delegates are used to put a changed value back into a model object
/// </summary>
public delegate void AspectPutterDelegate(Object rowObject, Object newValue);
/// <summary>
/// These delegates can be used to convert an aspect value to a display string,
/// instead of using the default ToString()
/// </summary>
public delegate string AspectToStringConverterDelegate(Object value);
/// <summary>
/// These delegates are used to get the tooltip for a cell
/// </summary>
public delegate String CellToolTipGetterDelegate(OLVColumn column, Object modelObject);
/// <summary>
/// These delegates are used to the state of the checkbox for a row object.
/// </summary>
/// <remarks><para>
/// For reasons known only to someone in Microsoft, we can only set
/// a boolean on the ListViewItem to indicate it's "checked-ness", but when
/// we receive update events, we have to use a tristate CheckState. So we can
/// be told about an indeterminate state, but we can't set it ourselves.
/// </para>
/// <para>As of version 2.0, we can now return indeterminate state.</para>
/// </remarks>
public delegate CheckState CheckStateGetterDelegate(Object rowObject);
/// <summary>
/// These delegates are used to get the state of the checkbox for a row object.
/// </summary>
/// <param name="rowObject"></param>
/// <returns></returns>
public delegate bool BooleanCheckStateGetterDelegate(Object rowObject);
/// <summary>
/// These delegates are used to put a changed check state back into a model object
/// </summary>
public delegate CheckState CheckStatePutterDelegate(Object rowObject, CheckState newValue);
/// <summary>
/// These delegates are used to put a changed check state back into a model object
/// </summary>
/// <param name="rowObject"></param>
/// <param name="newValue"></param>
/// <returns></returns>
public delegate bool BooleanCheckStatePutterDelegate(Object rowObject, bool newValue);
/// <summary>
/// The callbacks for RightColumnClick events
/// </summary>
public delegate void ColumnRightClickEventHandler(object sender, ColumnClickEventArgs e);
/// <summary>
/// This delegate will be used to own draw header column.
/// </summary>
public delegate bool HeaderDrawingDelegate(Graphics g, Rectangle r, int columnIndex, OLVColumn column, bool isPressed, HeaderStateStyle stateStyle);
/// <summary>
/// This delegate is called when a group has been created but not yet made
/// into a real ListViewGroup. The user can take this opportunity to fill
/// in lots of other details about the group.
/// </summary>
public delegate void GroupFormatterDelegate(OLVGroup group, GroupingParameters parms);
/// <summary>
/// These delegates are used to retrieve the object that is the key of the group to which the given row belongs.
/// </summary>
public delegate Object GroupKeyGetterDelegate(Object rowObject);
/// <summary>
/// These delegates are used to convert a group key into a title for the group
/// </summary>
public delegate string GroupKeyToTitleConverterDelegate(Object groupKey);
/// <summary>
/// These delegates are used to get the tooltip for a column header
/// </summary>
public delegate String HeaderToolTipGetterDelegate(OLVColumn column);
/// <summary>
/// These delegates are used to fetch the image selector that should be used
/// to choose an image for this column.
/// </summary>
public delegate Object ImageGetterDelegate(Object rowObject);
/// <summary>
/// These delegates are used to draw a cell
/// </summary>
public delegate bool RenderDelegate(EventArgs e, Graphics g, Rectangle r, Object rowObject);
/// <summary>
/// These delegates are used to fetch a row object for virtual lists
/// </summary>
public delegate Object RowGetterDelegate(int rowIndex);
/// <summary>
/// These delegates are used to format a listviewitem before it is added to the control.
/// </summary>
public delegate void RowFormatterDelegate(OLVListItem olvItem);
/// <summary>
/// These delegates are used to sort the listview in some custom fashion
/// </summary>
public delegate void SortDelegate(OLVColumn column, SortOrder sortOrder);
#endregion
}

View File

@ -0,0 +1,407 @@
/*
* DragSource.cs - Add drag source functionality to an ObjectListView
*
* UNFINISHED
*
* Author: Phillip Piper
* Date: 2009-03-17 5:15 PM
*
* Change log:
* v2.3
* 2009-07-06 JPP - Make sure Link is acceptable as an drop effect by default
* (since MS didn't make it part of the 'All' value)
* v2.2
* 2009-04-15 JPP - Separated DragSource.cs into DropSink.cs
* 2009-03-17 JPP - Initial version
*
* Copyright (C) 2009 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace BrightIdeasSoftware
{
/// <summary>
/// An IDragSource controls how drag out from the ObjectListView will behave
/// </summary>
public interface IDragSource
{
/// <summary>
/// A drag operation is beginning. Return the data object that will be used
/// for data transfer. Return null to prevent the drag from starting.
/// </summary>
/// <remarks>
/// The returned object is later passed to the GetAllowedEffect() and EndDrag()
/// methods.
/// </remarks>
/// <param name="olv">What ObjectListView is being dragged from.</param>
/// <param name="button">Which mouse button is down?</param>
/// <param name="item">What item was directly dragged by the user? There may be more than just this
/// item selected.</param>
/// <returns>The data object that will be used for data transfer. This will often be a subclass
/// of DataObject, but does not need to be.</returns>
Object StartDrag(ObjectListView olv, MouseButtons button, OLVListItem item);
/// <summary>
/// What operations are possible for this drag? This controls the icon shown during the drag
/// </summary>
/// <param name="dragObject">The data object returned by StartDrag()</param>
/// <returns>A combination of DragDropEffects flags</returns>
DragDropEffects GetAllowedEffects(Object dragObject);
/// <summary>
/// The drag operation is complete. Do whatever is necessary to complete the action.
/// </summary>
/// <param name="dragObject">The data object returned by StartDrag()</param>
/// <param name="effect">The value returned from GetAllowedEffects()</param>
void EndDrag(Object dragObject, DragDropEffects effect);
}
/// <summary>
/// A do-nothing implementation of IDragSource that can be safely subclassed.
/// </summary>
public class AbstractDragSource : IDragSource
{
#region IDragSource Members
/// <summary>
/// See IDragSource documentation
/// </summary>
/// <param name="olv"></param>
/// <param name="button"></param>
/// <param name="item"></param>
/// <returns></returns>
public virtual Object StartDrag(ObjectListView olv, MouseButtons button, OLVListItem item) {
return null;
}
/// <summary>
/// See IDragSource documentation
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public virtual DragDropEffects GetAllowedEffects(Object data) {
return DragDropEffects.None;
}
/// <summary>
/// See IDragSource documentation
/// </summary>
/// <param name="dragObject"></param>
/// <param name="effect"></param>
public virtual void EndDrag(Object dragObject, DragDropEffects effect) {
}
#endregion
}
/// <summary>
/// A reasonable implementation of IDragSource that provides normal
/// drag source functionality. It creates a data object that supports
/// inter-application dragging of text and HTML representation of
/// the dragged rows. It can optionally force a refresh of all dragged
/// rows when the drag is complete.
/// </summary>
/// <remarks>Subclasses can override GetDataObject() to add new
/// data formats to the data transfer object.</remarks>
public class SimpleDragSource : IDragSource
{
#region Constructors
/// <summary>
/// Construct a SimpleDragSource
/// </summary>
public SimpleDragSource() {
}
/// <summary>
/// Construct a SimpleDragSource that refreshes the dragged rows when
/// the drag is complete
/// </summary>
/// <param name="refreshAfterDrop"></param>
public SimpleDragSource(bool refreshAfterDrop) {
this.RefreshAfterDrop = refreshAfterDrop;
}
#endregion
#region Public properties
/// <summary>
/// Gets or sets whether the dragged rows should be refreshed when the
/// drag operation is complete.
/// </summary>
public bool RefreshAfterDrop {
get { return refreshAfterDrop; }
set { refreshAfterDrop = value; }
}
private bool refreshAfterDrop;
#endregion
#region IDragSource Members
/// <summary>
/// Create a DataObject when the user does a left mouse drag operation.
/// See IDragSource for further information.
/// </summary>
/// <param name="olv"></param>
/// <param name="button"></param>
/// <param name="item"></param>
/// <returns></returns>
public virtual Object StartDrag(ObjectListView olv, MouseButtons button, OLVListItem item) {
// We only drag on left mouse
if (button != MouseButtons.Left)
return null;
return this.CreateDataObject(olv);
}
/// <summary>
/// Which operations are allowed in the operation? By default, all operations are supported.
/// </summary>
/// <param name="data"></param>
/// <returns>All opertions are supported</returns>
public virtual DragDropEffects GetAllowedEffects(Object data) {
return DragDropEffects.All | DragDropEffects.Link; // why didn't MS include 'Link' in 'All'??
}
/// <summary>
/// The drag operation is finished. Refreshe the dragged rows if so configured.
/// </summary>
/// <param name="dragObject"></param>
/// <param name="effect"></param>
public virtual void EndDrag(Object dragObject, DragDropEffects effect) {
OLVDataObject data = dragObject as OLVDataObject;
if (data == null)
return;
if (this.RefreshAfterDrop)
data.ListView.RefreshObjects(data.ModelObjects);
}
/// <summary>
/// Create a data object that will be used to as the data object
/// for the drag operation.
/// </summary>
/// <remarks>
/// Subclasses can override this method add new formats to the data object.
/// </remarks>
/// <param name="olv">The ObjectListView that is the source of the drag</param>
/// <returns>A data object for the drag</returns>
protected virtual object CreateDataObject(ObjectListView olv) {
OLVDataObject data = new OLVDataObject(olv);
data.CreateTextFormats();
return data;
}
#endregion
}
/// <summary>
/// A data transfer object that knows how to transform a list of model
/// objects into a text and HTML representation.
/// </summary>
public class OLVDataObject : DataObject
{
#region Life and death
/// <summary>
/// Create a data object from the selected objects in the given ObjectListView
/// </summary>
/// <param name="olv">The source of the data object</param>
public OLVDataObject(ObjectListView olv) : this(olv, olv.SelectedObjects) {
}
/// <summary>
/// Create a data object which operates on the given model objects
/// in the given ObjectListView
/// </summary>
/// <param name="olv">The source of the data object</param>
/// <param name="modelObjects">The model objects to be put into the data object</param>
public OLVDataObject(ObjectListView olv, IList modelObjects) {
this.objectListView = olv;
this.modelObjects = modelObjects;
this.includeHiddenColumns = olv.IncludeHiddenColumnsInDataTransfer;
this.includeColumnHeaders = olv.IncludeColumnHeadersInCopy;
}
#endregion
#region Properties
/// <summary>
/// Gets or sets whether hidden columns will also be included in the text
/// and HTML representation. If this is false, only visible columns will
/// be included.
/// </summary>
public bool IncludeHiddenColumns {
get { return includeHiddenColumns; }
}
private bool includeHiddenColumns;
/// <summary>
/// Gets or sets whether column headers will also be included in the text
/// and HTML representation.
/// </summary>
public bool IncludeColumnHeaders
{
get { return includeColumnHeaders; }
}
private bool includeColumnHeaders;
/// <summary>
/// Gets the ObjectListView that is being used as the source of the data
/// </summary>
public ObjectListView ListView {
get { return objectListView; }
}
private ObjectListView objectListView;
/// <summary>
/// Gets the model objects that are to be placed in the data object
/// </summary>
public IList ModelObjects {
get { return modelObjects; }
}
private IList modelObjects = new ArrayList();
#endregion
/// <summary>
/// Put a text and HTML representation of our model objects
/// into the data object.
/// </summary>
public void CreateTextFormats() {
IList<OLVColumn> columns = this.IncludeHiddenColumns ? this.ListView.AllColumns : this.ListView.ColumnsInDisplayOrder;
// Build text and html versions of the selection
StringBuilder sbText = new StringBuilder();
StringBuilder sbHtml = new StringBuilder("<table>");
// Include column headers
if (includeColumnHeaders)
{
sbHtml.Append("<tr><td>");
foreach (OLVColumn col in columns)
{
if (col != columns[0])
{
sbText.Append("\t");
sbHtml.Append("</td><td>");
}
string strValue = col.Text;
sbText.Append(strValue);
sbHtml.Append(strValue); //TODO: Should encode the string value
}
sbText.AppendLine();
sbHtml.AppendLine("</td></tr>");
}
foreach (object modelObject in this.ModelObjects)
{
sbHtml.Append("<tr><td>");
foreach (OLVColumn col in columns) {
if (col != columns[0]) {
sbText.Append("\t");
sbHtml.Append("</td><td>");
}
string strValue = col.GetStringValue(modelObject);
sbText.Append(strValue);
sbHtml.Append(strValue); //TODO: Should encode the string value
}
sbText.AppendLine();
sbHtml.AppendLine("</td></tr>");
}
sbHtml.AppendLine("</table>");
// Put both the text and html versions onto the clipboard.
// For some reason, SetText() with UnicodeText doesn't set the basic CF_TEXT format,
// but using SetData() does.
//this.SetText(sbText.ToString(), TextDataFormat.UnicodeText);
this.SetData(sbText.ToString());
this.SetText(ConvertToHtmlFragment(sbHtml.ToString()), TextDataFormat.Html);
}
/// <summary>
/// Make a HTML representation of our model objects
/// </summary>
public string CreateHtml() {
IList<OLVColumn> columns = this.ListView.ColumnsInDisplayOrder;
// Build html version of the selection
StringBuilder sbHtml = new StringBuilder("<table>");
foreach (object modelObject in this.ModelObjects) {
sbHtml.Append("<tr><td>");
foreach (OLVColumn col in columns) {
if (col != columns[0]) {
sbHtml.Append("</td><td>");
}
string strValue = col.GetStringValue(modelObject);
sbHtml.Append(strValue); //TODO: Should encode the string value
}
sbHtml.AppendLine("</td></tr>");
}
sbHtml.AppendLine("</table>");
return sbHtml.ToString();
}
/// <summary>
/// Convert the fragment of HTML into the Clipboards HTML format.
/// </summary>
/// <remarks>The HTML format is found here http://msdn2.microsoft.com/en-us/library/aa767917.aspx
/// </remarks>
/// <param name="fragment">The HTML to put onto the clipboard. It must be valid HTML!</param>
/// <returns>A string that can be put onto the clipboard and will be recognized as HTML</returns>
private string ConvertToHtmlFragment(string fragment) {
// Minimal implementation of HTML clipboard format
string source = "http://www.codeproject.com/KB/list/ObjectListView.aspx";
const String MARKER_BLOCK =
"Version:1.0\r\n" +
"StartHTML:{0,8}\r\n" +
"EndHTML:{1,8}\r\n" +
"StartFragment:{2,8}\r\n" +
"EndFragment:{3,8}\r\n" +
"StartSelection:{2,8}\r\n" +
"EndSelection:{3,8}\r\n" +
"SourceURL:{4}\r\n" +
"{5}";
int prefixLength = String.Format(MARKER_BLOCK, 0, 0, 0, 0, source, "").Length;
const String DEFAULT_HTML_BODY =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">" +
"<HTML><HEAD></HEAD><BODY><!--StartFragment-->{0}<!--EndFragment--></BODY></HTML>";
string html = String.Format(DEFAULT_HTML_BODY, fragment);
int startFragment = prefixLength + html.IndexOf(fragment);
int endFragment = startFragment + fragment.Length;
return String.Format(MARKER_BLOCK, prefixLength, prefixLength + html.Length, startFragment, endFragment, source, html);
}
}
}

1402
Implementation/DropSink.cs Normal file

File diff suppressed because it is too large Load Diff

99
Implementation/Enums.cs Normal file
View File

@ -0,0 +1,99 @@
/*
* Enums - All enum definitions used in ObjectListView
*
* Author: Phillip Piper
* Date: 31-March-2011 5:53 pm
*
* Change log:
* 2011-03-31 JPP - Split into its own file
*
* Copyright (C) 2011-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace BrightIdeasSoftware {
public partial class ObjectListView {
/// <summary>
/// How does a user indicate that they want to edit cells?
/// </summary>
public enum CellEditActivateMode {
/// <summary>
/// This list cannot be edited. F2 does nothing.
/// </summary>
None = 0,
/// <summary>
/// A single click on a <strong>subitem</strong> will edit the value. Single clicking the primary column,
/// selects the row just like normal. The user must press F2 to edit the primary column.
/// </summary>
SingleClick = 1,
/// <summary>
/// Double clicking a subitem or the primary column will edit that cell.
/// F2 will edit the primary column.
/// </summary>
DoubleClick = 2,
/// <summary>
/// Pressing F2 is the only way to edit the cells. Once the primary column is being edited,
/// the other cells in the row can be edited by pressing Tab.
/// </summary>
F2Only = 3
}
/// <summary>
/// These values specify how column selection will be presented to the user
/// </summary>
public enum ColumnSelectBehaviour {
/// <summary>
/// No column selection will be presented
/// </summary>
None,
/// <summary>
/// The columns will be show in the main menu
/// </summary>
InlineMenu,
/// <summary>
/// The columns will be shown in a submenu
/// </summary>
Submenu,
/// <summary>
/// A model dialog will be presented to allow the user to choose columns
/// </summary>
ModelDialog,
/*
* NonModelDialog is just a little bit tricky since the OLV can change views while the dialog is showing
* So, just comment this out for the time being.
/// <summary>
/// A non-model dialog will be presented to allow the user to choose columns
/// </summary>
NonModelDialog
*
*/
}
}
}

2422
Implementation/Events.cs Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,175 @@
/*
* GroupingParameters - All the data that is used to create groups in an ObjectListView
*
* Author: Phillip Piper
* Date: 31-March-2011 5:53 pm
*
* Change log:
* 2011-03-31 JPP - Split into its own file
*
* Copyright (C) 2011-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace BrightIdeasSoftware {
/// <summary>
/// This class contains all the settings used when groups are created
/// </summary>
public class GroupingParameters {
/// <summary>
/// Create a GroupingParameters
/// </summary>
/// <param name="olv"></param>
/// <param name="groupByColumn"></param>
/// <param name="groupByOrder"></param>
/// <param name="column"></param>
/// <param name="order"></param>
/// <param name="secondaryColumn"></param>
/// <param name="secondaryOrder"></param>
/// <param name="titleFormat"></param>
/// <param name="titleSingularFormat"></param>
/// <param name="sortItemsByPrimaryColumn"></param>
public GroupingParameters(ObjectListView olv, OLVColumn groupByColumn, SortOrder groupByOrder,
OLVColumn column, SortOrder order, OLVColumn secondaryColumn, SortOrder secondaryOrder,
string titleFormat, string titleSingularFormat, bool sortItemsByPrimaryColumn) {
this.ListView = olv;
this.GroupByColumn = groupByColumn;
this.GroupByOrder = groupByOrder;
this.PrimarySort = column;
this.PrimarySortOrder = order;
this.SecondarySort = secondaryColumn;
this.SecondarySortOrder = secondaryOrder;
this.SortItemsByPrimaryColumn = sortItemsByPrimaryColumn;
this.TitleFormat = titleFormat;
this.TitleSingularFormat = titleSingularFormat;
}
/// <summary>
/// Gets or sets the ObjectListView being grouped
/// </summary>
public ObjectListView ListView {
get { return this.listView; }
set { this.listView = value; }
}
private ObjectListView listView;
/// <summary>
/// Gets or sets the column used to create groups
/// </summary>
public OLVColumn GroupByColumn {
get { return this.groupByColumn; }
set { this.groupByColumn = value; }
}
private OLVColumn groupByColumn;
/// <summary>
/// In what order will the groups themselves be sorted?
/// </summary>
public SortOrder GroupByOrder {
get { return this.groupByOrder; }
set { this.groupByOrder = value; }
}
private SortOrder groupByOrder;
/// <summary>
/// If this is set, this comparer will be used to order the groups
/// </summary>
public IComparer<OLVGroup> GroupComparer {
get { return this.groupComparer; }
set { this.groupComparer = value; }
}
private IComparer<OLVGroup> groupComparer;
/// <summary>
/// If this is set, this comparer will be used to order items within each group
/// </summary>
public IComparer<OLVListItem> ItemComparer {
get { return this.itemComparer; }
set { this.itemComparer = value; }
}
private IComparer<OLVListItem> itemComparer;
/// <summary>
/// Gets or sets the column that will be the primary sort
/// </summary>
public OLVColumn PrimarySort {
get { return this.primarySort; }
set { this.primarySort = value; }
}
private OLVColumn primarySort;
/// <summary>
/// Gets or sets the ordering for the primary sort
/// </summary>
public SortOrder PrimarySortOrder {
get { return this.primarySortOrder; }
set { this.primarySortOrder = value; }
}
private SortOrder primarySortOrder;
/// <summary>
/// Gets or sets the column used for secondary sorting
/// </summary>
public OLVColumn SecondarySort {
get { return this.secondarySort; }
set { this.secondarySort = value; }
}
private OLVColumn secondarySort;
/// <summary>
/// Gets or sets the ordering for the secondary sort
/// </summary>
public SortOrder SecondarySortOrder {
get { return this.secondarySortOrder; }
set { this.secondarySortOrder = value; }
}
private SortOrder secondarySortOrder;
/// <summary>
/// Gets or sets the title format used for groups with zero or more than one element
/// </summary>
public string TitleFormat {
get { return this.titleFormat; }
set { this.titleFormat = value; }
}
private string titleFormat;
/// <summary>
/// Gets or sets the title format used for groups with only one element
/// </summary>
public string TitleSingularFormat {
get { return this.titleSingularFormat; }
set { this.titleSingularFormat = value; }
}
private string titleSingularFormat;
/// <summary>
/// Gets or sets whether the items should be sorted by the primary column
/// </summary>
public bool SortItemsByPrimaryColumn {
get { return this.sortItemsByPrimaryColumn; }
set { this.sortItemsByPrimaryColumn = value; }
}
private bool sortItemsByPrimaryColumn;
}
}

747
Implementation/Groups.cs Normal file
View File

@ -0,0 +1,747 @@
/*
* Groups - Enhancements to the normal ListViewGroup
*
* Author: Phillip Piper
* Date: 22/08/2009 6:03PM
*
* Change log:
* v2.3
* 2009-09-09 JPP - Added Collapsed and Collapsible properties
* 2009-09-01 JPP - Cleaned up code, added more docs
* - Works under VS2005 again
* 2009-08-22 JPP - Initial version
*
* To do:
* - Implement subseting
* - Implement footer items
*
* Copyright (C) 2009-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace BrightIdeasSoftware
{
/// <summary>
/// These values indicate what is the state of the group. These values
/// are taken directly from the SDK and many are not used by ObjectListView.
/// </summary>
[Flags]
public enum GroupState
{
/// <summary>
/// Normal
/// </summary>
LVGS_NORMAL = 0x0,
/// <summary>
/// Collapsed
/// </summary>
LVGS_COLLAPSED = 0x1,
/// <summary>
/// Hidden
/// </summary>
LVGS_HIDDEN = 0x2,
/// <summary>
/// NoHeader
/// </summary>
LVGS_NOHEADER = 0x4,
/// <summary>
/// Can be collapsed
/// </summary>
LVGS_COLLAPSIBLE = 0x8,
/// <summary>
/// Has focus
/// </summary>
LVGS_FOCUSED = 0x10,
/// <summary>
/// Is Selected
/// </summary>
LVGS_SELECTED = 0x20,
/// <summary>
/// Is subsetted
/// </summary>
LVGS_SUBSETED = 0x40,
/// <summary>
/// Subset link has focus
/// </summary>
LVGS_SUBSETLINKFOCUSED = 0x80,
/// <summary>
/// All styles
/// </summary>
LVGS_ALL = 0xFFFF
}
/// <summary>
/// This mask indicates which members of a LVGROUP have valid data. These values
/// are taken directly from the SDK and many are not used by ObjectListView.
/// </summary>
[Flags]
public enum GroupMask
{
/// <summary>
/// No mask
/// </summary>
LVGF_NONE = 0,
/// <summary>
/// Group has header
/// </summary>
LVGF_HEADER = 1,
/// <summary>
/// Group has footer
/// </summary>
LVGF_FOOTER = 2,
/// <summary>
/// Group has state
/// </summary>
LVGF_STATE = 4,
/// <summary>
///
/// </summary>
LVGF_ALIGN = 8,
/// <summary>
///
/// </summary>
LVGF_GROUPID = 0x10,
/// <summary>
/// pszSubtitle is valid
/// </summary>
LVGF_SUBTITLE = 0x00100,
/// <summary>
/// pszTask is valid
/// </summary>
LVGF_TASK = 0x00200,
/// <summary>
/// pszDescriptionTop is valid
/// </summary>
LVGF_DESCRIPTIONTOP = 0x00400,
/// <summary>
/// pszDescriptionBottom is valid
/// </summary>
LVGF_DESCRIPTIONBOTTOM = 0x00800,
/// <summary>
/// iTitleImage is valid
/// </summary>
LVGF_TITLEIMAGE = 0x01000,
/// <summary>
/// iExtendedImage is valid
/// </summary>
LVGF_EXTENDEDIMAGE = 0x02000,
/// <summary>
/// iFirstItem and cItems are valid
/// </summary>
LVGF_ITEMS = 0x04000,
/// <summary>
/// pszSubsetTitle is valid
/// </summary>
LVGF_SUBSET = 0x08000,
/// <summary>
/// readonly, cItems holds count of items in visible subset, iFirstItem is valid
/// </summary>
LVGF_SUBSETITEMS = 0x10000
}
/// <summary>
/// This mask indicates which members of a GROUPMETRICS structure are valid
/// </summary>
[Flags]
public enum GroupMetricsMask
{
/// <summary>
///
/// </summary>
LVGMF_NONE = 0,
/// <summary>
///
/// </summary>
LVGMF_BORDERSIZE = 1,
/// <summary>
///
/// </summary>
LVGMF_BORDERCOLOR = 2,
/// <summary>
///
/// </summary>
LVGMF_TEXTCOLOR = 4
}
/// <summary>
/// Instances of this class enhance the capabilities of a normal ListViewGroup,
/// enabling the functionality that was released in v6 of the common controls.
/// </summary>
/// <remarks>
/// <para>
/// In this implementation (2009-09), these objects are essentially passive.
/// Setting properties does not automatically change the associated group in
/// the listview. Collapsed and Collapsible are two exceptions to this and
/// give immediate results.
/// </para>
/// <para>
/// This really should be a subclass of ListViewGroup, but that class is
/// sealed (why is that?). So this class provides the same interface as a
/// ListViewGroup, plus many other new properties.
/// </para>
/// </remarks>
public class OLVGroup
{
#region Creation
/// <summary>
/// Create an OLVGroup
/// </summary>
public OLVGroup() : this("Default group header") {
}
/// <summary>
/// Create a group with the given title
/// </summary>
/// <param name="header">Title of the group</param>
public OLVGroup(string header) {
this.Header = header;
this.Id = OLVGroup.nextId++;
this.TitleImage = -1;
this.ExtendedImage = -1;
}
private static int nextId;
#endregion
#region Public properties
/// <summary>
/// Gets or sets the bottom description of the group
/// </summary>
/// <remarks>
/// Descriptions only appear when group is centered and there is a title image
/// </remarks>
public string BottomDescription {
get { return this.bottomDescription; }
set { this.bottomDescription = value; }
}
private string bottomDescription;
/// <summary>
/// Gets or sets whether or not this group is collapsed
/// </summary>
public bool Collapsed {
get { return this.GetOneState(GroupState.LVGS_COLLAPSED); }
set { this.SetOneState(value, GroupState.LVGS_COLLAPSED); }
}
/// <summary>
/// Gets or sets whether or not this group can be collapsed
/// </summary>
public bool Collapsible {
get { return this.GetOneState(GroupState.LVGS_COLLAPSIBLE); }
set { this.SetOneState(value, GroupState.LVGS_COLLAPSIBLE); }
}
/// <summary>
/// Gets or sets some representation of the contents of this group
/// </summary>
/// <remarks>This is user defined (like Tag)</remarks>
public IList Contents {
get { return this.contents; }
set { this.contents = value; }
}
private IList contents;
/// <summary>
/// Gets whether this group has been created.
/// </summary>
public bool Created {
get { return this.ListView != null; }
}
/// <summary>
/// Gets or sets the int or string that will select the extended image to be shown against the title
/// </summary>
public object ExtendedImage {
get { return this.extendedImage; }
set { this.extendedImage = value; }
}
private object extendedImage;
/// <summary>
/// Gets or sets the footer of the group
/// </summary>
public string Footer {
get { return this.footer; }
set { this.footer = value; }
}
private string footer;
/// <summary>
/// Gets the internal id of our associated ListViewGroup.
/// </summary>
public int GroupId {
get {
if (this.ListViewGroup == null)
return this.Id;
// Use reflection to get around the access control on the ID property
if (OLVGroup.groupIdPropInfo == null) {
OLVGroup.groupIdPropInfo = typeof(ListViewGroup).GetProperty("ID",
BindingFlags.NonPublic | BindingFlags.Instance);
System.Diagnostics.Debug.Assert(OLVGroup.groupIdPropInfo != null);
}
int? groupId = OLVGroup.groupIdPropInfo.GetValue(this.ListViewGroup, null) as int?;
return groupId.HasValue ? groupId.Value : -1;
}
}
private static PropertyInfo groupIdPropInfo;
/// <summary>
/// Gets or sets the header of the group
/// </summary>
public string Header {
get { return this.header; }
set { this.header = value; }
}
private string header;
/// <summary>
/// Gets or sets the horizontal alignment of the group header
/// </summary>
public HorizontalAlignment HeaderAlignment {
get { return this.headerAlignment; }
set { this.headerAlignment = value; }
}
private HorizontalAlignment headerAlignment;
/// <summary>
/// Gets or sets the internally created id of the group
/// </summary>
public int Id {
get { return this.id; }
set { this.id = value; }
}
private int id;
/// <summary>
/// Gets or sets ListViewItems that are members of this group
/// </summary>
/// <remarks>Listener of the BeforeCreatingGroups event can populate this collection.
/// It is only used on non-virtual lists.</remarks>
public IList<OLVListItem> Items {
get { return this.items; }
set { this.items = value; }
}
private IList<OLVListItem> items = new List<OLVListItem>();
/// <summary>
/// Gets or sets the key that was used to partition objects into this group
/// </summary>
/// <remarks>This is user defined (like Tag)</remarks>
public object Key {
get { return this.key; }
set { this.key = value; }
}
private object key;
/// <summary>
/// Gets the ObjectListView that this group belongs to
/// </summary>
/// <remarks>If this is null, the group has not yet been created.</remarks>
public ObjectListView ListView {
get { return this.listView; }
protected set { this.listView = value; }
}
private ObjectListView listView;
/// <summary>
/// Gets or sets the name of the group
/// </summary>
/// <remarks>As of 2009-09-01, this property is not used.</remarks>
public string Name {
get { return this.name; }
set { this.name = value; }
}
private string name;
/// <summary>
/// Gets or sets whether this group is focused
/// </summary>
public bool Focused
{
get { return this.GetOneState(GroupState.LVGS_FOCUSED); }
set { this.SetOneState(value, GroupState.LVGS_FOCUSED); }
}
/// <summary>
/// Gets or sets whether this group is selected
/// </summary>
public bool Selected
{
get { return this.GetOneState(GroupState.LVGS_SELECTED); }
set { this.SetOneState(value, GroupState.LVGS_SELECTED); }
}
/// <summary>
/// Gets or sets the text that will show that this group is subsetted
/// </summary>
/// <remarks>
/// As of WinSDK v7.0, subsetting of group is officially unimplemented.
/// We can get around this using undocumented interfaces and may do so.
/// </remarks>
public string SubsetTitle {
get { return this.subsetTitle; }
set { this.subsetTitle = value; }
}
private string subsetTitle;
/// <summary>
/// Gets or set the subtitleof the task
/// </summary>
public string Subtitle {
get { return this.subtitle; }
set { this.subtitle = value; }
}
private string subtitle;
/// <summary>
/// Gets or sets the value by which this group will be sorted.
/// </summary>
public IComparable SortValue {
get { return this.sortValue; }
set { this.sortValue = value; }
}
private IComparable sortValue;
/// <summary>
/// Gets or sets the state of the group
/// </summary>
public GroupState State {
get { return this.state; }
set { this.state = value; }
}
private GroupState state;
/// <summary>
/// Gets or sets which bits of State are valid
/// </summary>
public GroupState StateMask {
get { return this.stateMask; }
set { this.stateMask = value; }
}
private GroupState stateMask;
/// <summary>
/// Gets or sets whether this group is showing only a subset of its elements
/// </summary>
/// <remarks>
/// As of WinSDK v7.0, this property officially does nothing.
/// </remarks>
public bool Subseted {
get { return this.GetOneState(GroupState.LVGS_SUBSETED); }
set { this.SetOneState(value, GroupState.LVGS_SUBSETED); }
}
/// <summary>
/// Gets or sets the user-defined data attached to this group
/// </summary>
public object Tag {
get { return this.tag; }
set { this.tag = value; }
}
private object tag;
/// <summary>
/// Gets or sets the task of this group
/// </summary>
/// <remarks>This task is the clickable text that appears on the right margin
/// of the group header.</remarks>
public string Task {
get { return this.task; }
set { this.task = value; }
}
private string task;
/// <summary>
/// Gets or sets the int or string that will select the image to be shown against the title
/// </summary>
public object TitleImage {
get { return this.titleImage; }
set { this.titleImage = value; }
}
private object titleImage;
/// <summary>
/// Gets or sets the top description of the group
/// </summary>
/// <remarks>
/// Descriptions only appear when group is centered and there is a title image
/// </remarks>
public string TopDescription {
get { return this.topDescription; }
set { this.topDescription = value; }
}
private string topDescription;
/// <summary>
/// Gets or sets the number of items that are within this group.
/// </summary>
/// <remarks>This should only be used for virtual groups.</remarks>
public int VirtualItemCount {
get { return this.virtualItemCount; }
set { this.virtualItemCount = value; }
}
private int virtualItemCount;
#endregion
#region Protected properties
/// <summary>
/// Gets or sets the ListViewGroup that is shadowed by this group.
/// </summary>
/// <remarks>For virtual groups, this will always be null.</remarks>
protected ListViewGroup ListViewGroup {
get { return this.listViewGroup; }
set { this.listViewGroup = value; }
}
private ListViewGroup listViewGroup;
#endregion
#region Calculations/Conversions
/// <summary>
/// Calculate the index into the group image list of the given image selector
/// </summary>
/// <param name="imageSelector"></param>
/// <returns></returns>
public int GetImageIndex(object imageSelector) {
if (imageSelector == null || this.ListView == null || this.ListView.GroupImageList == null)
return -1;
if (imageSelector is Int32)
return (int)imageSelector;
String imageSelectorAsString = imageSelector as String;
if (imageSelectorAsString != null)
return this.ListView.GroupImageList.Images.IndexOfKey(imageSelectorAsString);
return -1;
}
/// <summary>
/// Convert this object to a string representation
/// </summary>
/// <returns></returns>
public override string ToString() {
return this.Header;
}
#endregion
#region Commands
/// <summary>
/// Insert a native group into the underlying Windows control,
/// *without* using a ListViewGroup
/// </summary>
/// <param name="olv"></param>
/// <remarks>This is used when creating virtual groups</remarks>
public void InsertGroupNewStyle(ObjectListView olv) {
this.ListView = olv;
NativeMethods.InsertGroup(olv, this.AsNativeGroup(true));
}
/// <summary>
/// Insert a native group into the underlying control via a ListViewGroup
/// </summary>
/// <param name="olv"></param>
public void InsertGroupOldStyle(ObjectListView olv) {
this.ListView = olv;
// Create/update the associated ListViewGroup
if (this.ListViewGroup == null)
this.ListViewGroup = new ListViewGroup();
this.ListViewGroup.Header = this.Header;
this.ListViewGroup.HeaderAlignment = this.HeaderAlignment;
this.ListViewGroup.Name = this.Name;
// Remember which OLVGroup created the ListViewGroup
this.ListViewGroup.Tag = this;
// Add the group to the control
olv.Groups.Add(this.ListViewGroup);
// Add any extra information
NativeMethods.SetGroupInfo(olv, this.GroupId, this.AsNativeGroup(false));
}
/// <summary>
/// Change the members of the group to match the current contents of Items,
/// using a ListViewGroup
/// </summary>
public void SetItemsOldStyle() {
List<OLVListItem> list = this.Items as List<OLVListItem>;
if (list == null) {
foreach (OLVListItem item in this.Items) {
this.ListViewGroup.Items.Add(item);
}
} else {
this.ListViewGroup.Items.AddRange(list.ToArray());
}
}
#endregion
#region Implementation
/// <summary>
/// Create a native LVGROUP structure that matches this group
/// </summary>
internal NativeMethods.LVGROUP2 AsNativeGroup(bool withId) {
NativeMethods.LVGROUP2 group = new NativeMethods.LVGROUP2();
group.cbSize = (uint)Marshal.SizeOf(typeof(NativeMethods.LVGROUP2));
group.mask = (uint)(GroupMask.LVGF_HEADER ^ GroupMask.LVGF_ALIGN ^ GroupMask.LVGF_STATE);
group.pszHeader = this.Header;
group.uAlign = (uint)this.HeaderAlignment;
group.stateMask = (uint)this.StateMask;
group.state = (uint)this.State;
if (withId) {
group.iGroupId = this.GroupId;
group.mask ^= (uint)GroupMask.LVGF_GROUPID;
}
if (!String.IsNullOrEmpty(this.Footer)) {
group.pszFooter = this.Footer;
group.mask ^= (uint)GroupMask.LVGF_FOOTER;
}
if (!String.IsNullOrEmpty(this.Subtitle)) {
group.pszSubtitle = this.Subtitle;
group.mask ^= (uint)GroupMask.LVGF_SUBTITLE;
}
if (!String.IsNullOrEmpty(this.Task)) {
group.pszTask = this.Task;
group.mask ^= (uint)GroupMask.LVGF_TASK;
}
if (!String.IsNullOrEmpty(this.TopDescription)) {
group.pszDescriptionTop = this.TopDescription;
group.mask ^= (uint)GroupMask.LVGF_DESCRIPTIONTOP;
}
if (!String.IsNullOrEmpty(this.BottomDescription)) {
group.pszDescriptionBottom = this.BottomDescription;
group.mask ^= (uint)GroupMask.LVGF_DESCRIPTIONBOTTOM;
}
int imageIndex = this.GetImageIndex(this.TitleImage);
if (imageIndex >= 0) {
group.iTitleImage = imageIndex;
group.mask ^= (uint)GroupMask.LVGF_TITLEIMAGE;
}
imageIndex = this.GetImageIndex(this.ExtendedImage);
if (imageIndex >= 0) {
group.iExtendedImage = imageIndex;
group.mask ^= (uint)GroupMask.LVGF_EXTENDEDIMAGE;
}
if (!String.IsNullOrEmpty(this.SubsetTitle)) {
group.pszSubsetTitle = this.SubsetTitle;
group.mask ^= (uint)GroupMask.LVGF_SUBSET;
}
if (this.VirtualItemCount > 0) {
group.cItems = this.VirtualItemCount;
group.mask ^= (uint)GroupMask.LVGF_ITEMS;
}
return group;
}
private bool GetOneState(GroupState mask) {
if (this.Created)
this.State = this.GetState();
return (this.State & mask) == mask;
}
/// <summary>
/// Get the current state of this group from the underlying control
/// </summary>
protected GroupState GetState() {
return NativeMethods.GetGroupState(this.ListView, this.GroupId, GroupState.LVGS_ALL);
}
/// <summary>
/// Get the current state of this group from the underlying control
/// </summary>
protected int SetState(GroupState newState, GroupState mask) {
NativeMethods.LVGROUP2 group = new NativeMethods.LVGROUP2();
group.cbSize = ((uint)Marshal.SizeOf(typeof(NativeMethods.LVGROUP2)));
group.mask = (uint)GroupMask.LVGF_STATE;
group.state = (uint)newState;
group.stateMask = (uint)mask;
return NativeMethods.SetGroupInfo(this.ListView, this.GroupId, group);
}
private void SetOneState(bool value, GroupState mask)
{
this.StateMask ^= mask;
if (value)
this.State ^= mask;
else
this.State &= ~mask;
if (this.Created)
this.SetState(this.State, mask);
}
#endregion
}
}

568
Implementation/Munger.cs Normal file
View File

@ -0,0 +1,568 @@
/*
* Munger - An Interface pattern on getting and setting values from object through Reflection
*
* Author: Phillip Piper
* Date: 28/11/2008 17:15
*
* Change log:
* v2.5.1
* 2012-05-01 JPP - Added IgnoreMissingAspects property
* v2.5
* 2011-05-20 JPP - Accessing through an indexer when the target had both a integer and
* a string indexer didn't work reliably.
* v2.4.1
* 2010-08-10 JPP - Refactored into Munger/SimpleMunger. 3x faster!
* v2.3
* 2009-02-15 JPP - Made Munger a public class
* 2009-01-20 JPP - Made the Munger capable of handling indexed access.
* Incidentally, this removed the ugliness that the last change introduced.
* 2009-01-18 JPP - Handle target objects from a DataListView (normally DataRowViews)
* v2.0
* 2008-11-28 JPP Initial version
*
* TO DO:
*
* Copyright (C) 2006-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Reflection;
namespace BrightIdeasSoftware
{
/// <summary>
/// An instance of Munger gets a value from or puts a value into a target object. The property
/// to be peeked (or poked) is determined from a string. The peeking or poking is done using reflection.
/// </summary>
/// <remarks>
/// Name of the aspect to be peeked can be a field, property or parameterless method. The name of an
/// aspect to poke can be a field, writable property or single parameter method.
/// <para>
/// Aspect names can be dotted to chain a series of references.
/// </para>
/// <example>Order.Customer.HomeAddress.State</example>
/// </remarks>
public class Munger
{
#region Life and death
/// <summary>
/// Create a do nothing Munger
/// </summary>
public Munger()
{
}
/// <summary>
/// Create a Munger that works on the given aspect name
/// </summary>
/// <param name="aspectName">The name of the </param>
public Munger(String aspectName)
{
this.AspectName = aspectName;
}
#endregion
#region Static utility methods
/// <summary>
/// A helper method to put the given value into the given aspect of the given object.
/// </summary>
/// <remarks>This method catches and silently ignores any errors that occur
/// while modifying the target object</remarks>
/// <param name="target">The object to be modified</param>
/// <param name="propertyName">The name of the property/field to be modified</param>
/// <param name="value">The value to be assigned</param>
/// <returns>Did the modification work?</returns>
public static bool PutProperty(object target, string propertyName, object value) {
try {
Munger munger = new Munger(propertyName);
return munger.PutValue(target, value);
}
catch (MungerException) {
// Not a lot we can do about this. Something went wrong in the bowels
// of the property. Let's take the ostrich approach and just ignore it :-)
// Normally, we would never just silently ignore an exception.
// However, in this case, this is a utility method that explicitly
// contracts to catch and ignore errors. If this is not acceptible,
// the programmer should not use this method.
}
return false;
}
/// <summary>
/// Gets or sets whether Mungers will silently ignore missing aspect errors.
/// </summary>
/// <remarks>
/// <para>
/// By default, if a Munger is asked to fetch a field/property/method
/// that does not exist from a model, it returns an error message, since that
/// condition is normally a programming error. There are some use cases where
/// this is not an error, and the munger should simply keep quiet.
/// </para>
/// <para>By default this is true during release builds.</para>
/// </remarks>
public static bool IgnoreMissingAspects {
get { return ignoreMissingAspects; }
set { ignoreMissingAspects = value; }
}
private static bool ignoreMissingAspects
#if !DEBUG
= true
#endif
;
#endregion
#region Public properties
/// <summary>
/// The name of the aspect that is to be peeked or poked.
/// </summary>
/// <remarks>
/// <para>
/// This name can be a field, property or parameter-less method.
/// </para>
/// <para>
/// The name can be dotted, which chains references. If any link in the chain returns
/// null, the entire chain is considered to return null.
/// </para>
/// </remarks>
/// <example>"DateOfBirth"</example>
/// <example>"Owner.HomeAddress.Postcode"</example>
public string AspectName
{
get { return aspectName; }
set {
aspectName = value;
// Clear any cache
aspectParts = null;
}
}
private string aspectName;
#endregion
#region Public interface
/// <summary>
/// Extract the value indicated by our AspectName from the given target.
/// </summary>
/// <remarks>If the aspect name is null or empty, this will return null.</remarks>
/// <param name="target">The object that will be peeked</param>
/// <returns>The value read from the target</returns>
public Object GetValue(Object target) {
if (this.Parts.Count == 0)
return null;
try {
return this.EvaluateParts(target, this.Parts);
} catch (MungerException ex) {
if (Munger.IgnoreMissingAspects)
return null;
return String.Format("'{0}' is not a parameter-less method, property or field of type '{1}'",
ex.Munger.AspectName, ex.Target.GetType());
}
}
/// <summary>
/// Extract the value indicated by our AspectName from the given target, raising exceptions
/// if the munger fails.
/// </summary>
/// <remarks>If the aspect name is null or empty, this will return null.</remarks>
/// <param name="target">The object that will be peeked</param>
/// <returns>The value read from the target</returns>
public Object GetValueEx(Object target) {
if (this.Parts.Count == 0)
return null;
return this.EvaluateParts(target, this.Parts);
}
/// <summary>
/// Poke the given value into the given target indicated by our AspectName.
/// </summary>
/// <remarks>
/// <para>
/// If the AspectName is a dotted path, all the selectors bar the last
/// are used to find the object that should be updated, and the last
/// selector is used as the property to update on that object.
/// </para>
/// <para>
/// So, if 'target' is a Person and the AspectName is "HomeAddress.Postcode",
/// this method will first fetch "HomeAddress" property, and then try to set the
/// "Postcode" property on the home address object.
/// </para>
/// </remarks>
/// <param name="target">The object that will be poked</param>
/// <param name="value">The value that will be poked into the target</param>
/// <returns>bool indicating whether the put worked</returns>
public bool PutValue(Object target, Object value)
{
if (this.Parts.Count == 0)
return false;
SimpleMunger lastPart = this.Parts[this.Parts.Count - 1];
if (this.Parts.Count > 1) {
List<SimpleMunger> parts = new List<SimpleMunger>(this.Parts);
parts.RemoveAt(parts.Count - 1);
try {
target = this.EvaluateParts(target, parts);
} catch (MungerException ex) {
this.ReportPutValueException(ex);
return false;
}
}
if (target != null) {
try {
return lastPart.PutValue(target, value);
} catch (MungerException ex) {
this.ReportPutValueException(ex);
}
}
return false;
}
#endregion
#region Implementation
/// <summary>
/// Gets the list of SimpleMungers that match our AspectName
/// </summary>
private IList<SimpleMunger> Parts {
get {
if (aspectParts == null)
aspectParts = BuildParts(this.AspectName);
return aspectParts;
}
}
private IList<SimpleMunger> aspectParts;
/// <summary>
/// Convert a possibly dotted AspectName into a list of SimpleMungers
/// </summary>
/// <param name="aspect"></param>
/// <returns></returns>
private IList<SimpleMunger> BuildParts(string aspect) {
List<SimpleMunger> parts = new List<SimpleMunger>();
if (!String.IsNullOrEmpty(aspect)) {
foreach (string part in aspect.Split('.')) {
parts.Add(new SimpleMunger(part.Trim()));
}
}
return parts;
}
/// <summary>
/// Evaluate the given chain of SimpleMungers against an initial target.
/// </summary>
/// <param name="target"></param>
/// <param name="parts"></param>
/// <returns></returns>
private object EvaluateParts(object target, IList<SimpleMunger> parts) {
foreach (SimpleMunger part in parts) {
if (target == null)
break;
target = part.GetValue(target);
}
return target;
}
private void ReportPutValueException(MungerException ex) {
//TODO: How should we report this error?
System.Diagnostics.Debug.WriteLine("PutValue failed");
System.Diagnostics.Debug.WriteLine(String.Format("- Culprit aspect: {0}", ex.Munger.AspectName));
System.Diagnostics.Debug.WriteLine(String.Format("- Target: {0} of type {1}", ex.Target, ex.Target.GetType()));
System.Diagnostics.Debug.WriteLine(String.Format("- Inner exception: {0}", ex.InnerException));
}
#endregion
}
/// <summary>
/// A SimpleMunger deals with a single property/field/method on its target.
/// </summary>
/// <remarks>
/// Munger uses a chain of these resolve a dotted aspect name.
/// </remarks>
public class SimpleMunger
{
#region Life and death
/// <summary>
/// Create a SimpleMunger
/// </summary>
/// <param name="aspectName"></param>
public SimpleMunger(String aspectName)
{
this.aspectName = aspectName;
}
#endregion
#region Public properties
/// <summary>
/// The name of the aspect that is to be peeked or poked.
/// </summary>
/// <remarks>
/// <para>
/// This name can be a field, property or method.
/// When using a method to get a value, the method must be parameter-less.
/// When using a method to set a value, the method must accept 1 parameter.
/// </para>
/// <para>
/// It cannot be a dotted name.
/// </para>
/// </remarks>
public string AspectName {
get { return aspectName; }
}
private readonly string aspectName;
#endregion
#region Public interface
/// <summary>
/// Get a value from the given target
/// </summary>
/// <param name="target"></param>
/// <returns></returns>
public Object GetValue(Object target) {
if (target == null)
return null;
this.ResolveName(target, this.AspectName, 0);
try {
if (this.resolvedPropertyInfo != null)
return this.resolvedPropertyInfo.GetValue(target, null);
if (this.resolvedMethodInfo != null)
return this.resolvedMethodInfo.Invoke(target, null);
if (this.resolvedFieldInfo != null)
return this.resolvedFieldInfo.GetValue(target);
// If that didn't work, try to use the indexer property.
// This covers things like dictionaries and DataRows.
if (this.indexerPropertyInfo != null)
return this.indexerPropertyInfo.GetValue(target, new object[] { this.AspectName });
} catch (Exception ex) {
// Lots of things can do wrong in these invocations
throw new MungerException(this, target, ex);
}
// If we get to here, we couldn't find a match for the aspect
throw new MungerException(this, target, new MissingMethodException());
}
/// <summary>
/// Poke the given value into the given target indicated by our AspectName.
/// </summary>
/// <param name="target">The object that will be poked</param>
/// <param name="value">The value that will be poked into the target</param>
/// <returns>bool indicating if the put worked</returns>
public bool PutValue(object target, object value) {
if (target == null)
return false;
this.ResolveName(target, this.AspectName, 1);
try {
if (this.resolvedPropertyInfo != null) {
this.resolvedPropertyInfo.SetValue(target, value, null);
return true;
}
if (this.resolvedMethodInfo != null) {
this.resolvedMethodInfo.Invoke(target, new object[] { value });
return true;
}
if (this.resolvedFieldInfo != null) {
this.resolvedFieldInfo.SetValue(target, value);
return true;
}
// If that didn't work, try to use the indexer property.
// This covers things like dictionaries and DataRows.
if (this.indexerPropertyInfo != null) {
this.indexerPropertyInfo.SetValue(target, value, new object[] { this.AspectName });
return true;
}
} catch (Exception ex) {
// Lots of things can do wrong in these invocations
throw new MungerException(this, target, ex);
}
return false;
}
#endregion
#region Implementation
private void ResolveName(object target, string name, int numberMethodParameters) {
if (cachedTargetType == target.GetType() && cachedName == name && cachedNumberParameters == numberMethodParameters)
return;
cachedTargetType = target.GetType();
cachedName = name;
cachedNumberParameters = numberMethodParameters;
resolvedFieldInfo = null;
resolvedPropertyInfo = null;
resolvedMethodInfo = null;
indexerPropertyInfo = null;
const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance /*| BindingFlags.NonPublic*/;
foreach (PropertyInfo pinfo in target.GetType().GetProperties(flags)) {
if (pinfo.Name == name) {
resolvedPropertyInfo = pinfo;
return;
}
// See if we can find an string indexer property while we are here.
// We also need to allow for old style <object> keyed collections.
if (indexerPropertyInfo == null && pinfo.Name == "Item") {
ParameterInfo[] par = pinfo.GetGetMethod().GetParameters();
if (par.Length > 0) {
Type parameterType = par[0].ParameterType;
if (parameterType == typeof(string) || parameterType == typeof(object))
indexerPropertyInfo = pinfo;
}
}
}
foreach (FieldInfo info in target.GetType().GetFields(flags)) {
if (info.Name == name) {
resolvedFieldInfo = info;
return;
}
}
foreach (MethodInfo info in target.GetType().GetMethods(flags)) {
if (info.Name == name && info.GetParameters().Length == numberMethodParameters) {
resolvedMethodInfo = info;
return;
}
}
}
private Type cachedTargetType;
private string cachedName;
private int cachedNumberParameters;
private FieldInfo resolvedFieldInfo;
private PropertyInfo resolvedPropertyInfo;
private MethodInfo resolvedMethodInfo;
private PropertyInfo indexerPropertyInfo;
#endregion
}
/// <summary>
/// These exceptions are raised when a munger finds something it cannot process
/// </summary>
public class MungerException : ApplicationException
{
/// <summary>
/// Create a MungerException
/// </summary>
/// <param name="munger"></param>
/// <param name="target"></param>
/// <param name="ex"></param>
public MungerException(SimpleMunger munger, object target, Exception ex)
: base("Munger failed", ex) {
this.munger = munger;
this.target = target;
}
/// <summary>
/// Get the munger that raised the exception
/// </summary>
public SimpleMunger Munger {
get { return munger; }
}
private readonly SimpleMunger munger;
/// <summary>
/// Gets the target that threw the exception
/// </summary>
public object Target {
get { return target; }
}
private readonly object target;
}
/*
* We don't currently need this
* 2010-08-06
*
internal class SimpleBinder : Binder
{
public override FieldInfo BindToField(BindingFlags bindingAttr, FieldInfo[] match, object value, System.Globalization.CultureInfo culture) {
//return Type.DefaultBinder.BindToField(
throw new NotImplementedException();
}
public override object ChangeType(object value, Type type, System.Globalization.CultureInfo culture) {
throw new NotImplementedException();
}
public override MethodBase BindToMethod(BindingFlags bindingAttr, MethodBase[] match, ref object[] args, ParameterModifier[] modifiers, System.Globalization.CultureInfo culture, string[] names, out object state) {
throw new NotImplementedException();
}
public override void ReorderArgumentArray(ref object[] args, object state) {
throw new NotImplementedException();
}
public override MethodBase SelectMethod(BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers) {
throw new NotImplementedException();
}
public override PropertyInfo SelectProperty(BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers) {
if (match == null)
throw new ArgumentNullException("match");
if (match.Length == 0)
return null;
return match[0];
}
}
*/
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,87 @@
/*
* NullableDictionary - A simple Dictionary that can handle null as a key
*
* Author: Phillip Piper
* Date: 31-March-2011 5:53 pm
*
* Change log:
* 2011-03-31 JPP - Split into its own file
*
* Copyright (C) 2011-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace BrightIdeasSoftware {
/// <summary>
/// A simple-minded implementation of a Dictionary that can handle null as a key.
/// </summary>
/// <typeparam name="TKey">The type of the dictionary key</typeparam>
/// <typeparam name="TValue">The type of the values to be stored</typeparam>
/// <remarks>This is not a full implementation and is only meant to handle
/// collecting groups by their keys, since groups can have null as a key value.</remarks>
internal class NullableDictionary<TKey, TValue> : Dictionary<TKey, TValue> {
private bool hasNullKey;
private TValue nullValue;
new public TValue this[TKey key] {
get {
if (key != null)
return base[key];
if (this.hasNullKey)
return this.nullValue;
throw new KeyNotFoundException();
}
set {
if (key == null) {
this.hasNullKey = true;
this.nullValue = value;
} else
base[key] = value;
}
}
new public bool ContainsKey(TKey key) {
return key == null ? this.hasNullKey : base.ContainsKey(key);
}
new public IList Keys {
get {
ArrayList list = new ArrayList(base.Keys);
if (this.hasNullKey)
list.Add(null);
return list;
}
}
new public IList<TValue> Values {
get {
List<TValue> list = new List<TValue>(base.Values);
if (this.hasNullKey)
list.Add(this.nullValue);
return list;
}
}
}
}

View File

@ -0,0 +1,272 @@
/*
* OLVListItem - A row in an ObjectListView
*
* Author: Phillip Piper
* Date: 31-March-2011 5:53 pm
*
* Change log:
* v2.8
* 2014-09-27 JPP - Remove faulty caching of CheckState
* 2014-05-06 JPP - Added OLVListItem.Enabled flag
* vOld
* 2011-03-31 JPP - Split into its own file
*
* Copyright (C) 2011-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace BrightIdeasSoftware {
/// <summary>
/// OLVListItems are specialized ListViewItems that know which row object they came from,
/// and the row index at which they are displayed, even when in group view mode. They
/// also know the image they should draw against themselves
/// </summary>
public class OLVListItem : ListViewItem {
#region Constructors
/// <summary>
/// Create a OLVListItem for the given row object
/// </summary>
public OLVListItem(object rowObject) {
this.rowObject = rowObject;
}
/// <summary>
/// Create a OLVListItem for the given row object, represented by the given string and image
/// </summary>
public OLVListItem(object rowObject, string text, Object image)
: base(text, -1) {
this.rowObject = rowObject;
this.imageSelector = image;
}
#endregion
#region Properties
/// <summary>
/// Gets the bounding rectangle of the item, including all subitems
/// </summary>
new public Rectangle Bounds {
get {
try {
return base.Bounds;
}
catch (System.ArgumentException) {
// If the item is part of a collapsed group, Bounds will throw an exception
return Rectangle.Empty;
}
}
}
/// <summary>
/// Gets or sets how many pixels will be left blank around each cell of this item
/// </summary>
/// <remarks>This setting only takes effect when the control is owner drawn.</remarks>
public Rectangle? CellPadding {
get { return this.cellPadding; }
set { this.cellPadding = value; }
}
private Rectangle? cellPadding;
/// <summary>
/// Gets or sets how the cells of this item will be vertically aligned
/// </summary>
/// <remarks>This setting only takes effect when the control is owner drawn.</remarks>
public StringAlignment? CellVerticalAlignment {
get { return this.cellVerticalAlignment; }
set { this.cellVerticalAlignment = value; }
}
private StringAlignment? cellVerticalAlignment;
/// <summary>
/// Gets or sets the checkedness of this item.
/// </summary>
/// <remarks>
/// Virtual lists don't handle checkboxes well, so we have to intercept attempts to change them
/// through the items, and change them into something that will work.
/// Unfortuneately, this won't work if this property is set through the base class, since
/// the property is not declared as virtual.
/// </remarks>
new public bool Checked {
get {
return base.Checked;
}
set {
if (this.Checked != value) {
if (value)
((ObjectListView)this.ListView).CheckObject(this.RowObject);
else
((ObjectListView)this.ListView).UncheckObject(this.RowObject);
}
}
}
/// <summary>
/// Enable tri-state checkbox.
/// </summary>
/// <remarks>.NET's Checked property was not built to handle tri-state checkboxes,
/// and will return True for both Checked and Indeterminate states.</remarks>
public CheckState CheckState {
get {
switch (this.StateImageIndex) {
case 0:
return System.Windows.Forms.CheckState.Unchecked;
case 1:
return System.Windows.Forms.CheckState.Checked;
case 2:
return System.Windows.Forms.CheckState.Indeterminate;
default:
return System.Windows.Forms.CheckState.Unchecked;
}
}
set {
switch (value) {
case System.Windows.Forms.CheckState.Unchecked:
this.StateImageIndex = 0;
break;
case System.Windows.Forms.CheckState.Checked:
this.StateImageIndex = 1;
break;
case System.Windows.Forms.CheckState.Indeterminate:
this.StateImageIndex = 2;
break;
}
}
}
/// <summary>
/// Gets if this item has any decorations set for it.
/// </summary>
public bool HasDecoration {
get {
return this.decorations != null && this.decorations.Count > 0;
}
}
/// <summary>
/// Gets or sets the decoration that will be drawn over this item
/// </summary>
/// <remarks>Setting this replaces all other decorations</remarks>
public IDecoration Decoration {
get {
if (this.HasDecoration)
return this.Decorations[0];
else
return null;
}
set {
this.Decorations.Clear();
if (value != null)
this.Decorations.Add(value);
}
}
/// <summary>
/// Gets the collection of decorations that will be drawn over this item
/// </summary>
public IList<IDecoration> Decorations {
get {
if (this.decorations == null)
this.decorations = new List<IDecoration>();
return this.decorations;
}
}
private IList<IDecoration> decorations;
/// <summary>
/// Gets whether or not this row can be selected and activated
/// </summary>
public bool Enabled
{
get { return this.enabled; }
internal set { this.enabled = value; }
}
private bool enabled;
/// <summary>
/// Get or set the image that should be shown against this item
/// </summary>
/// <remarks><para>This can be an Image, a string or an int. A string or an int will
/// be used as an index into the small image list.</para></remarks>
public Object ImageSelector {
get { return imageSelector; }
set {
imageSelector = value;
if (value is Int32)
this.ImageIndex = (Int32)value;
else if (value is String)
this.ImageKey = (String)value;
else
this.ImageIndex = -1;
}
}
private Object imageSelector;
/// <summary>
/// Gets or sets the the model object that is source of the data for this list item.
/// </summary>
public object RowObject {
get { return rowObject; }
set { rowObject = value; }
}
private object rowObject;
#endregion
#region Accessing
/// <summary>
/// Return the sub item at the given index
/// </summary>
/// <param name="index">Index of the subitem to be returned</param>
/// <returns>An OLVListSubItem</returns>
public virtual OLVListSubItem GetSubItem(int index) {
if (index >= 0 && index < this.SubItems.Count)
return (OLVListSubItem)this.SubItems[index];
return null;
}
/// <summary>
/// Return bounds of the given subitem
/// </summary>
/// <remarks>This correctly calculates the bounds even for column 0.</remarks>
public virtual Rectangle GetSubItemBounds(int subItemIndex) {
if (subItemIndex == 0) {
Rectangle r = this.Bounds;
Point sides = NativeMethods.GetScrolledColumnSides(this.ListView, subItemIndex);
r.X = sides.X + 1;
r.Width = sides.Y - sides.X;
return r;
}
OLVListSubItem subItem = this.GetSubItem(subItemIndex);
return subItem == null ? new Rectangle() : subItem.Bounds;
}
#endregion
}
}

View File

@ -0,0 +1,162 @@
/*
* OLVListSubItem - A single cell in an ObjectListView
*
* Author: Phillip Piper
* Date: 31-March-2011 5:53 pm
*
* Change log:
* 2011-03-31 JPP - Split into its own file
*
* Copyright (C) 2011-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
namespace BrightIdeasSoftware {
/// <summary>
/// A ListViewSubItem that knows which image should be drawn against it.
/// </summary>
[Browsable(false)]
public class OLVListSubItem : ListViewItem.ListViewSubItem {
#region Constructors
/// <summary>
/// Create a OLVListSubItem
/// </summary>
public OLVListSubItem() {
}
/// <summary>
/// Create a OLVListSubItem that shows the given string and image
/// </summary>
public OLVListSubItem(object modelValue, string text, Object image) {
this.ModelValue = modelValue;
this.Text = text;
this.ImageSelector = image;
}
#endregion
#region Properties
/// <summary>
/// Gets or sets how many pixels will be left blank around this cell
/// </summary>
/// <remarks>This setting only takes effect when the control is owner drawn.</remarks>
public Rectangle? CellPadding {
get { return this.cellPadding; }
set { this.cellPadding = value; }
}
private Rectangle? cellPadding;
/// <summary>
/// Gets or sets how this cell will be vertically aligned
/// </summary>
/// <remarks>This setting only takes effect when the control is owner drawn.</remarks>
public StringAlignment? CellVerticalAlignment {
get { return this.cellVerticalAlignment; }
set { this.cellVerticalAlignment = value; }
}
private StringAlignment? cellVerticalAlignment;
/// <summary>
/// Gets or sets the model value is being displayed by this subitem.
/// </summary>
public object ModelValue
{
get { return modelValue; }
private set { modelValue = value; }
}
private object modelValue;
/// <summary>
/// Gets if this subitem has any decorations set for it.
/// </summary>
public bool HasDecoration {
get {
return this.decorations != null && this.decorations.Count > 0;
}
}
/// <summary>
/// Gets or sets the decoration that will be drawn over this item
/// </summary>
/// <remarks>Setting this replaces all other decorations</remarks>
public IDecoration Decoration {
get {
return this.HasDecoration ? this.Decorations[0] : null;
}
set {
this.Decorations.Clear();
if (value != null)
this.Decorations.Add(value);
}
}
/// <summary>
/// Gets the collection of decorations that will be drawn over this item
/// </summary>
public IList<IDecoration> Decorations {
get {
if (this.decorations == null)
this.decorations = new List<IDecoration>();
return this.decorations;
}
}
private IList<IDecoration> decorations;
/// <summary>
/// Get or set the image that should be shown against this item
/// </summary>
/// <remarks><para>This can be an Image, a string or an int. A string or an int will
/// be used as an index into the small image list.</para></remarks>
public Object ImageSelector {
get { return imageSelector; }
set { imageSelector = value; }
}
private Object imageSelector;
/// <summary>
/// Gets or sets the url that should be invoked when this subitem is clicked
/// </summary>
public string Url {
get { return this.url; }
set { this.url = value; }
}
private string url;
#endregion
#region Implementation Properties
/// <summary>
/// Return the state of the animatation of the image on this subitem.
/// Null means there is either no image, or it is not an animation
/// </summary>
internal ImageRenderer.AnimationState AnimationState;
#endregion
}
}

View File

@ -0,0 +1,383 @@
/*
* OlvListViewHitTestInfo - All information gathered during a OlvHitTest() operation
*
* Author: Phillip Piper
* Date: 31-March-2011 5:53 pm
*
* Change log:
* 2011-03-31 JPP - Split into its own file
*
* Copyright (C) 2011-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace BrightIdeasSoftware {
/// <summary>
/// An indication of where a hit was within ObjectListView cell
/// </summary>
public enum HitTestLocation {
/// <summary>
/// Nowhere
/// </summary>
Nothing,
/// <summary>
/// On the text
/// </summary>
Text,
/// <summary>
/// On the image
/// </summary>
Image,
/// <summary>
/// On the checkbox
/// </summary>
CheckBox,
/// <summary>
/// On the expand button (TreeListView)
/// </summary>
ExpandButton,
/// <summary>
/// in the cell but not in any more specific location
/// </summary>
InCell,
/// <summary>
/// UserDefined location1 (used for custom renderers)
/// </summary>
UserDefined,
/// <summary>
/// On the expand/collapse widget of the group
/// </summary>
GroupExpander,
/// <summary>
/// Somewhere on a group
/// </summary>
Group,
/// <summary>
/// Somewhere in a column header
/// </summary>
Header,
/// <summary>
/// Somewhere in a column header checkbox
/// </summary>
HeaderCheckBox,
/// <summary>
/// Somewhere in a header divider
/// </summary>
HeaderDivider,
}
/// <summary>
/// A collection of ListViewHitTest constants
/// </summary>
[Flags]
public enum HitTestLocationEx {
/// <summary>
///
/// </summary>
LVHT_NOWHERE = 0x00000001,
/// <summary>
///
/// </summary>
LVHT_ONITEMICON = 0x00000002,
/// <summary>
///
/// </summary>
LVHT_ONITEMLABEL = 0x00000004,
/// <summary>
///
/// </summary>
LVHT_ONITEMSTATEICON = 0x00000008,
/// <summary>
///
/// </summary>
LVHT_ONITEM = (LVHT_ONITEMICON | LVHT_ONITEMLABEL | LVHT_ONITEMSTATEICON),
/// <summary>
///
/// </summary>
LVHT_ABOVE = 0x00000008,
/// <summary>
///
/// </summary>
LVHT_BELOW = 0x00000010,
/// <summary>
///
/// </summary>
LVHT_TORIGHT = 0x00000020,
/// <summary>
///
/// </summary>
LVHT_TOLEFT = 0x00000040,
/// <summary>
///
/// </summary>
LVHT_EX_GROUP_HEADER = 0x10000000,
/// <summary>
///
/// </summary>
LVHT_EX_GROUP_FOOTER = 0x20000000,
/// <summary>
///
/// </summary>
LVHT_EX_GROUP_COLLAPSE = 0x40000000,
/// <summary>
///
/// </summary>
LVHT_EX_GROUP_BACKGROUND = -2147483648, // 0x80000000
/// <summary>
///
/// </summary>
LVHT_EX_GROUP_STATEICON = 0x01000000,
/// <summary>
///
/// </summary>
LVHT_EX_GROUP_SUBSETLINK = 0x02000000,
/// <summary>
///
/// </summary>
LVHT_EX_GROUP = (LVHT_EX_GROUP_BACKGROUND | LVHT_EX_GROUP_COLLAPSE | LVHT_EX_GROUP_FOOTER | LVHT_EX_GROUP_HEADER | LVHT_EX_GROUP_STATEICON | LVHT_EX_GROUP_SUBSETLINK),
/// <summary>
///
/// </summary>
LVHT_EX_GROUP_MINUS_FOOTER_AND_BKGRD = (LVHT_EX_GROUP_COLLAPSE | LVHT_EX_GROUP_HEADER | LVHT_EX_GROUP_STATEICON | LVHT_EX_GROUP_SUBSETLINK),
/// <summary>
///
/// </summary>
LVHT_EX_ONCONTENTS = 0x04000000, // On item AND not on the background
/// <summary>
///
/// </summary>
LVHT_EX_FOOTER = 0x08000000,
}
/// <summary>
/// Instances of this class encapsulate the information gathered during a OlvHitTest()
/// operation.
/// </summary>
/// <remarks>Custom renderers can use HitTestLocation.UserDefined and the UserData
/// object to store more specific locations for use during event handlers.</remarks>
public class OlvListViewHitTestInfo {
/// <summary>
/// Create a OlvListViewHitTestInfo
/// </summary>
public OlvListViewHitTestInfo(OLVListItem olvListItem, OLVListSubItem subItem, int flags, OLVGroup group, int iColumn)
{
this.item = olvListItem;
this.subItem = subItem;
this.location = ConvertNativeFlagsToDotNetLocation(olvListItem, flags);
this.HitTestLocationEx = (HitTestLocationEx)flags;
this.Group = group;
this.ColumnIndex = iColumn;
this.ListView = olvListItem == null ? null : (ObjectListView)olvListItem.ListView;
switch (location) {
case ListViewHitTestLocations.StateImage:
this.HitTestLocation = HitTestLocation.CheckBox;
break;
case ListViewHitTestLocations.Image:
this.HitTestLocation = HitTestLocation.Image;
break;
case ListViewHitTestLocations.Label:
this.HitTestLocation = HitTestLocation.Text;
break;
default:
if ((this.HitTestLocationEx & HitTestLocationEx.LVHT_EX_GROUP_COLLAPSE) == HitTestLocationEx.LVHT_EX_GROUP_COLLAPSE)
this.HitTestLocation = HitTestLocation.GroupExpander;
else if ((this.HitTestLocationEx & HitTestLocationEx.LVHT_EX_GROUP_MINUS_FOOTER_AND_BKGRD) != 0)
this.HitTestLocation = HitTestLocation.Group;
else
this.HitTestLocation = HitTestLocation.Nothing;
break;
}
}
/// <summary>
/// Create a OlvListViewHitTestInfo when the header was hit
/// </summary>
public OlvListViewHitTestInfo(ObjectListView olv, int iColumn, bool isOverCheckBox, int iDivider) {
this.ListView = olv;
this.ColumnIndex = iColumn;
this.HeaderDividerIndex = iDivider;
this.HitTestLocation = isOverCheckBox ? HitTestLocation.HeaderCheckBox : (iDivider < 0 ? HitTestLocation.Header : HitTestLocation.HeaderDivider);
}
private static ListViewHitTestLocations ConvertNativeFlagsToDotNetLocation(OLVListItem hitItem, int flags)
{
// Untangle base .NET behaviour.
// In Windows SDK, the value 8 can have two meanings here: LVHT_ONITEMSTATEICON or LVHT_ABOVE.
// .NET changes these to be:
// - LVHT_ABOVE becomes ListViewHitTestLocations.AboveClientArea (which is 0x100).
// - LVHT_ONITEMSTATEICON becomes ListViewHitTestLocations.StateImage (which is 0x200).
// So, if we see the 8 bit set in flags, we change that to either a state image hit
// (if we hit an item) or to AboveClientAream if nothing was hit.
if ((8 & flags) == 8)
return (ListViewHitTestLocations)(0xf7 & flags | (hitItem == null ? 0x100 : 0x200));
// Mask off the LVHT_EX_XXXX values since ListViewHitTestLocations doesn't have them
return (ListViewHitTestLocations)(flags & 0xffff);
}
#region Public fields
/// <summary>
/// Where is the hit location?
/// </summary>
public HitTestLocation HitTestLocation;
/// <summary>
/// Where is the hit location?
/// </summary>
public HitTestLocationEx HitTestLocationEx;
/// <summary>
/// Which group was hit?
/// </summary>
public OLVGroup Group;
/// <summary>
/// Custom renderers can use this information to supply more details about the hit location
/// </summary>
public Object UserData;
#endregion
#region Public read-only properties
/// <summary>
/// Gets the item that was hit
/// </summary>
public OLVListItem Item {
get { return item; }
internal set { item = value; }
}
private OLVListItem item;
/// <summary>
/// Gets the subitem that was hit
/// </summary>
public OLVListSubItem SubItem {
get { return subItem; }
internal set { subItem = value; }
}
private OLVListSubItem subItem;
/// <summary>
/// Gets the part of the subitem that was hit
/// </summary>
public ListViewHitTestLocations Location {
get { return location; }
internal set { location = value; }
}
private ListViewHitTestLocations location;
/// <summary>
/// Gets the ObjectListView that was tested
/// </summary>
public ObjectListView ListView {
get { return listView; }
internal set { listView = value; }
}
private ObjectListView listView;
/// <summary>
/// Gets the model object that was hit
/// </summary>
public Object RowObject {
get {
return this.Item == null ? null : this.Item.RowObject;
}
}
/// <summary>
/// Gets the index of the row under the hit point or -1
/// </summary>
public int RowIndex {
get { return this.Item == null ? -1 : this.Item.Index; }
}
/// <summary>
/// Gets the index of the column under the hit point
/// </summary>
public int ColumnIndex {
get { return columnIndex; }
internal set { columnIndex = value; }
}
private int columnIndex;
/// <summary>
/// Gets the index of the header divider
/// </summary>
public int HeaderDividerIndex {
get { return headerDividerIndex; }
internal set { headerDividerIndex = value; }
}
private int headerDividerIndex = -1;
/// <summary>
/// Gets the column that was hit
/// </summary>
public OLVColumn Column {
get {
int index = this.ColumnIndex;
return index < 0 || this.ListView == null ? null : this.ListView.GetColumn(index);
}
}
#endregion
/// <summary>
/// Returns a string that represents the current object.
/// </summary>
/// <returns>
/// A string that represents the current object.
/// </returns>
/// <filterpriority>2</filterpriority>
public override string ToString()
{
return string.Format("HitTestLocation: {0}, HitTestLocationEx: {1}, Item: {2}, SubItem: {3}, Location: {4}, Group: {5}, ColumnIndex: {6}",
this.HitTestLocation, this.HitTestLocationEx, this.item, this.subItem, this.location, this.Group, this.ColumnIndex);
}
internal class HeaderHitTestInfo
{
public int ColumnIndex;
public bool IsOverCheckBox;
public int OverDividerIndex;
}
}
}

View File

@ -0,0 +1,262 @@
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
namespace BrightIdeasSoftware
{
/// <summary>
/// A TreeDataSourceAdapter knows how to build a tree structure from a binding list.
/// </summary>
/// <remarks>To build a tree</remarks>
public class TreeDataSourceAdapter : DataSourceAdapter
{
#region Life and death
/// <summary>
/// Create a data source adaptor that knows how to build a tree structure
/// </summary>
/// <param name="tlv"></param>
public TreeDataSourceAdapter(DataTreeListView tlv)
: base(tlv) {
this.treeListView = tlv;
this.treeListView.CanExpandGetter = delegate(object model) { return this.CalculateHasChildren(model); };
this.treeListView.ChildrenGetter = delegate(object model) { return this.CalculateChildren(model); };
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the name of the property/column that uniquely identifies each row.
/// </summary>
/// <remarks>
/// <para>
/// The value contained by this column must be unique across all rows
/// in the data source. Odd and unpredictable things will happen if two
/// rows have the same id.
/// </para>
/// <para>Null cannot be a valid key value.</para>
/// </remarks>
public virtual string KeyAspectName {
get { return keyAspectName; }
set {
if (keyAspectName == value)
return;
keyAspectName = value;
this.keyMunger = new Munger(this.KeyAspectName);
this.InitializeDataSource();
}
}
private string keyAspectName;
/// <summary>
/// Gets or sets the name of the property/column that contains the key of
/// the parent of a row.
/// </summary>
/// <remarks>
/// <para>
/// The test condition for deciding if one row is the parent of another is functionally
/// equivilent to this:
/// <code>
/// Object.Equals(candidateParentRow[this.KeyAspectName], row[this.ParentKeyAspectName])
/// </code>
/// </para>
/// <para>Unlike key value, parent keys can be null but a null parent key can only be used
/// to identify root objects.</para>
/// </remarks>
public virtual string ParentKeyAspectName {
get { return parentKeyAspectName; }
set {
if (parentKeyAspectName == value)
return;
parentKeyAspectName = value;
this.parentKeyMunger = new Munger(this.ParentKeyAspectName);
this.InitializeDataSource();
}
}
private string parentKeyAspectName;
/// <summary>
/// Gets or sets the value that identifies a row as a root object.
/// When the ParentKey of a row equals the RootKeyValue, that row will
/// be treated as root of the TreeListView.
/// </summary>
/// <remarks>
/// <para>
/// The test condition for deciding a root object is functionally
/// equivilent to this:
/// <code>
/// Object.Equals(candidateRow[this.ParentKeyAspectName], this.RootKeyValue)
/// </code>
/// </para>
/// <para>The RootKeyValue can be null.</para>
/// </remarks>
public virtual object RootKeyValue {
get { return rootKeyValue; }
set {
if (Equals(rootKeyValue, value))
return;
rootKeyValue = value;
this.InitializeDataSource();
}
}
private object rootKeyValue;
/// <summary>
/// Gets or sets whether or not the key columns (id and parent id) should
/// be shown to the user.
/// </summary>
/// <remarks>This must be set before the DataSource is set. It has no effect
/// afterwards.</remarks>
public virtual bool ShowKeyColumns {
get { return showKeyColumns; }
set { showKeyColumns = value; }
}
private bool showKeyColumns = true;
#endregion
#region Implementation properties
/// <summary>
/// Gets the DataTreeListView that is being managed
/// </summary>
protected DataTreeListView TreeListView {
get { return treeListView; }
}
private readonly DataTreeListView treeListView;
#endregion
#region Implementation
/// <summary>
///
/// </summary>
protected override void InitializeDataSource() {
base.InitializeDataSource();
this.TreeListView.RebuildAll(true);
}
/// <summary>
///
/// </summary>
protected override void SetListContents() {
this.TreeListView.Roots = this.CalculateRoots();
}
/// <summary>
///
/// </summary>
/// <param name="property"></param>
/// <returns></returns>
protected override bool ShouldCreateColumn(PropertyDescriptor property) {
// If the property is a key column, and we aren't supposed to show keys, don't show it
if (!this.ShowKeyColumns && (property.Name == this.KeyAspectName || property.Name == this.ParentKeyAspectName))
return false;
return base.ShouldCreateColumn(property);
}
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected override void HandleListChangedItemChanged(System.ComponentModel.ListChangedEventArgs e) {
// If the id or the parent id of a row changes, we just rebuild everything.
// We can't do anything more specific. We don't know what the previous values, so we can't
// tell the previous parent to refresh itself. If the id itself has changed, things that used
// to be children will no longer be children. Just rebuild everything.
// It seems PropertyDescriptor is only filled in .NET 4 :(
if (e.PropertyDescriptor != null &&
(e.PropertyDescriptor.Name == this.KeyAspectName ||
e.PropertyDescriptor.Name == this.ParentKeyAspectName))
this.InitializeDataSource();
else
base.HandleListChangedItemChanged(e);
}
/// <summary>
///
/// </summary>
/// <param name="index"></param>
protected override void ChangePosition(int index) {
// We can't use our base method directly, since the normal position management
// doesn't know about our tree structure. They treat our dataset as a flat list
// but we have a collapsable structure. This means that the 5'th row to them
// may not even be visible to us
// To display the n'th row, we have to make sure that all its ancestors
// are expanded. Then we will be able to select it.
object model = this.CurrencyManager.List[index];
object parent = this.CalculateParent(model);
while (parent != null && !this.TreeListView.IsExpanded(parent)) {
this.TreeListView.Expand(parent);
parent = this.CalculateParent(parent);
}
base.ChangePosition(index);
}
private IEnumerable CalculateRoots() {
foreach (object x in this.CurrencyManager.List) {
object parentKey = this.GetParentValue(x);
if (Object.Equals(this.RootKeyValue, parentKey))
yield return x;
}
}
private bool CalculateHasChildren(object model) {
object keyValue = this.GetKeyValue(model);
if (keyValue == null)
return false;
foreach (object x in this.CurrencyManager.List) {
object parentKey = this.GetParentValue(x);
if (Object.Equals(keyValue, parentKey))
return true;
}
return false;
}
private IEnumerable CalculateChildren(object model) {
object keyValue = this.GetKeyValue(model);
if (keyValue != null) {
foreach (object x in this.CurrencyManager.List) {
object parentKey = this.GetParentValue(x);
if (Object.Equals(keyValue, parentKey))
yield return x;
}
}
}
private object CalculateParent(object model) {
object parentValue = this.GetParentValue(model);
if (parentValue == null)
return null;
foreach (object x in this.CurrencyManager.List) {
object key = this.GetKeyValue(x);
if (Object.Equals(parentValue, key))
return x;
}
return null;
}
private object GetKeyValue(object model) {
return this.keyMunger == null ? null : this.keyMunger.GetValue(model);
}
private object GetParentValue(object model) {
return this.parentKeyMunger == null ? null : this.parentKeyMunger.GetValue(model);
}
#endregion
private Munger keyMunger;
private Munger parentKeyMunger;
}
}

View File

@ -0,0 +1,355 @@
/*
* Virtual groups - Classes and interfaces needed to implement virtual groups
*
* Author: Phillip Piper
* Date: 28/08/2009 11:10am
*
* Change log:
* 2011-02-21 JPP - Correctly honor group comparer and collapsible groups settings
* v2.3
* 2009-08-28 JPP - Initial version
*
* To do:
*
* Copyright (C) 2009-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace BrightIdeasSoftware
{
/// <summary>
/// A IVirtualGroups is the interface that a virtual list must implement to support virtual groups
/// </summary>
public interface IVirtualGroups
{
/// <summary>
/// Return the list of groups that should be shown according to the given parameters
/// </summary>
/// <param name="parameters"></param>
/// <returns></returns>
IList<OLVGroup> GetGroups(GroupingParameters parameters);
/// <summary>
/// Return the index of the item that appears at the given position within the given group.
/// </summary>
/// <param name="group"></param>
/// <param name="indexWithinGroup"></param>
/// <returns></returns>
int GetGroupMember(OLVGroup group, int indexWithinGroup);
/// <summary>
/// Return the index of the group to which the given item belongs
/// </summary>
/// <param name="itemIndex"></param>
/// <returns></returns>
int GetGroup(int itemIndex);
/// <summary>
/// Return the index at which the given item is shown in the given group
/// </summary>
/// <param name="group"></param>
/// <param name="itemIndex"></param>
/// <returns></returns>
int GetIndexWithinGroup(OLVGroup group, int itemIndex);
/// <summary>
/// A hint that the given range of items are going to be required
/// </summary>
/// <param name="fromGroupIndex"></param>
/// <param name="fromIndex"></param>
/// <param name="toGroupIndex"></param>
/// <param name="toIndex"></param>
void CacheHint(int fromGroupIndex, int fromIndex, int toGroupIndex, int toIndex);
}
/// <summary>
/// This is a safe, do nothing implementation of a grouping strategy
/// </summary>
public class AbstractVirtualGroups : IVirtualGroups
{
/// <summary>
/// Return the list of groups that should be shown according to the given parameters
/// </summary>
/// <param name="parameters"></param>
/// <returns></returns>
public virtual IList<OLVGroup> GetGroups(GroupingParameters parameters) {
return new List<OLVGroup>();
}
/// <summary>
/// Return the index of the item that appears at the given position within the given group.
/// </summary>
/// <param name="group"></param>
/// <param name="indexWithinGroup"></param>
/// <returns></returns>
public virtual int GetGroupMember(OLVGroup group, int indexWithinGroup) {
return -1;
}
/// <summary>
/// Return the index of the group to which the given item belongs
/// </summary>
/// <param name="itemIndex"></param>
/// <returns></returns>
public virtual int GetGroup(int itemIndex) {
return -1;
}
/// <summary>
/// Return the index at which the given item is shown in the given group
/// </summary>
/// <param name="group"></param>
/// <param name="itemIndex"></param>
/// <returns></returns>
public virtual int GetIndexWithinGroup(OLVGroup group, int itemIndex) {
return -1;
}
/// <summary>
/// A hint that the given range of items are going to be required
/// </summary>
/// <param name="fromGroupIndex"></param>
/// <param name="fromIndex"></param>
/// <param name="toGroupIndex"></param>
/// <param name="toIndex"></param>
public virtual void CacheHint(int fromGroupIndex, int fromIndex, int toGroupIndex, int toIndex) {
}
}
/// <summary>
/// Provides grouping functionality to a FastObjectListView
/// </summary>
public class FastListGroupingStrategy : AbstractVirtualGroups
{
/// <summary>
/// Create groups for FastListView
/// </summary>
/// <param name="parmameters"></param>
/// <returns></returns>
public override IList<OLVGroup> GetGroups(GroupingParameters parmameters) {
// There is a lot of overlap between this method and ObjectListView.MakeGroups()
// Any changes made here may need to be reflected there
// This strategy can only be used on FastObjectListViews
FastObjectListView folv = (FastObjectListView)parmameters.ListView;
// Separate the list view items into groups, using the group key as the descrimanent
int objectCount = 0;
NullableDictionary<object, List<object>> map = new NullableDictionary<object, List<object>>();
foreach (object model in folv.FilteredObjects) {
object key = parmameters.GroupByColumn.GetGroupKey(model);
if (!map.ContainsKey(key))
map[key] = new List<object>();
map[key].Add(model);
objectCount++;
}
// Sort the items within each group
// TODO: Give parameters a ModelComparer property
OLVColumn primarySortColumn = parmameters.SortItemsByPrimaryColumn ? parmameters.ListView.GetColumn(0) : parmameters.PrimarySort;
ModelObjectComparer sorter = new ModelObjectComparer(primarySortColumn, parmameters.PrimarySortOrder,
parmameters.SecondarySort, parmameters.SecondarySortOrder);
foreach (object key in map.Keys) {
map[key].Sort(sorter);
}
// Make a list of the required groups
List<OLVGroup> groups = new List<OLVGroup>();
foreach (object key in map.Keys) {
string title = parmameters.GroupByColumn.ConvertGroupKeyToTitle(key);
if (!String.IsNullOrEmpty(parmameters.TitleFormat)) {
int count = map[key].Count;
string format = (count == 1 ? parmameters.TitleSingularFormat : parmameters.TitleFormat);
try {
title = String.Format(format, title, count);
} catch (FormatException) {
title = "Invalid group format: " + format;
}
}
OLVGroup lvg = new OLVGroup(title);
lvg.Collapsible = folv.HasCollapsibleGroups;
lvg.Key = key;
lvg.SortValue = key as IComparable;
lvg.Contents = map[key].ConvertAll<int>(delegate(object x) { return folv.IndexOf(x); });
lvg.VirtualItemCount = map[key].Count;
if (parmameters.GroupByColumn.GroupFormatter != null)
parmameters.GroupByColumn.GroupFormatter(lvg, parmameters);
groups.Add(lvg);
}
// Sort the groups
if (parmameters.GroupByOrder != SortOrder.None)
groups.Sort(parmameters.GroupComparer ?? new OLVGroupComparer(parmameters.GroupByOrder));
// Build an array that remembers which group each item belongs to.
this.indexToGroupMap = new List<int>(objectCount);
this.indexToGroupMap.AddRange(new int[objectCount]);
for (int i = 0; i < groups.Count; i++) {
OLVGroup group = groups[i];
List<int> members = (List<int>)group.Contents;
foreach (int j in members)
this.indexToGroupMap[j] = i;
}
return groups;
}
private List<int> indexToGroupMap;
/// <summary>
///
/// </summary>
/// <param name="group"></param>
/// <param name="indexWithinGroup"></param>
/// <returns></returns>
public override int GetGroupMember(OLVGroup group, int indexWithinGroup) {
return (int)group.Contents[indexWithinGroup];
}
/// <summary>
///
/// </summary>
/// <param name="itemIndex"></param>
/// <returns></returns>
public override int GetGroup(int itemIndex) {
return this.indexToGroupMap[itemIndex];
}
/// <summary>
///
/// </summary>
/// <param name="group"></param>
/// <param name="itemIndex"></param>
/// <returns></returns>
public override int GetIndexWithinGroup(OLVGroup group, int itemIndex) {
return group.Contents.IndexOf(itemIndex);
}
}
/// <summary>
/// This is the COM interface that a ListView must be given in order for groups in virtual lists to work.
/// </summary>
/// <remarks>
/// This interface is NOT documented by MS. It was found on Greg Chapell's site. This means that there is
/// no guarantee that it will work on future versions of Windows, nor continue to work on current ones.
/// </remarks>
[ComImport(),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("44C09D56-8D3B-419D-A462-7B956B105B47")]
internal interface IOwnerDataCallback
{
/// <summary>
/// Not sure what this does
/// </summary>
/// <param name="i"></param>
/// <param name="pt"></param>
void GetItemPosition(int i, out NativeMethods.POINT pt);
/// <summary>
/// Not sure what this does
/// </summary>
/// <param name="t"></param>
/// <param name="pt"></param>
void SetItemPosition(int t, NativeMethods.POINT pt);
/// <summary>
/// Get the index of the item that occurs at the n'th position of the indicated group.
/// </summary>
/// <param name="groupIndex">Index of the group</param>
/// <param name="n">Index within the group</param>
/// <param name="itemIndex">Index of the item within the whole list</param>
void GetItemInGroup(int groupIndex, int n, out int itemIndex);
/// <summary>
/// Get the index of the group to which the given item belongs
/// </summary>
/// <param name="itemIndex">Index of the item within the whole list</param>
/// <param name="occurrenceCount">Which occurences of the item is wanted</param>
/// <param name="groupIndex">Index of the group</param>
void GetItemGroup(int itemIndex, int occurrenceCount, out int groupIndex);
/// <summary>
/// Get the number of groups that contain the given item
/// </summary>
/// <param name="itemIndex">Index of the item within the whole list</param>
/// <param name="occurrenceCount">How many groups does it occur within</param>
void GetItemGroupCount(int itemIndex, out int occurrenceCount);
/// <summary>
/// A hint to prepare any cache for the given range of requests
/// </summary>
/// <param name="i"></param>
/// <param name="j"></param>
void OnCacheHint(NativeMethods.LVITEMINDEX i, NativeMethods.LVITEMINDEX j);
}
/// <summary>
/// A default implementation of the IOwnerDataCallback interface
/// </summary>
[Guid("6FC61F50-80E8-49b4-B200-3F38D3865ABD")]
internal class OwnerDataCallbackImpl : IOwnerDataCallback
{
public OwnerDataCallbackImpl(VirtualObjectListView olv) {
this.olv = olv;
}
VirtualObjectListView olv;
#region IOwnerDataCallback Members
public void GetItemPosition(int i, out NativeMethods.POINT pt) {
//System.Diagnostics.Debug.WriteLine("GetItemPosition");
throw new NotSupportedException();
}
public void SetItemPosition(int t, NativeMethods.POINT pt) {
//System.Diagnostics.Debug.WriteLine("SetItemPosition");
throw new NotSupportedException();
}
public void GetItemInGroup(int groupIndex, int n, out int itemIndex) {
//System.Diagnostics.Debug.WriteLine(String.Format("-> GetItemInGroup({0}, {1})", groupIndex, n));
itemIndex = this.olv.GroupingStrategy.GetGroupMember(this.olv.OLVGroups[groupIndex], n);
//System.Diagnostics.Debug.WriteLine(String.Format("<- {0}", itemIndex));
}
public void GetItemGroup(int itemIndex, int occurrenceCount, out int groupIndex) {
//System.Diagnostics.Debug.WriteLine(String.Format("GetItemGroup({0}, {1})", itemIndex, occurrenceCount));
groupIndex = this.olv.GroupingStrategy.GetGroup(itemIndex);
//System.Diagnostics.Debug.WriteLine(String.Format("<- {0}", groupIndex));
}
public void GetItemGroupCount(int itemIndex, out int occurrenceCount) {
//System.Diagnostics.Debug.WriteLine(String.Format("GetItemGroupCount({0})", itemIndex));
occurrenceCount = 1;
}
public void OnCacheHint(NativeMethods.LVITEMINDEX from, NativeMethods.LVITEMINDEX to) {
//System.Diagnostics.Debug.WriteLine(String.Format("OnCacheHint({0}, {1}, {2}, {3})", from.iGroup, from.iItem, to.iGroup, to.iItem));
this.olv.GroupingStrategy.CacheHint(from.iGroup, from.iItem, to.iGroup, to.iItem);
}
#endregion
}
}

View File

@ -0,0 +1,334 @@
/*
* VirtualListDataSource - Encapsulate how data is provided to a virtual list
*
* Author: Phillip Piper
* Date: 28/08/2009 11:10am
*
* Change log:
* v2.4
* 2010-04-01 JPP - Added IFilterableDataSource
* v2.3
* 2009-08-28 JPP - Initial version (Separated from VirtualObjectListView.cs)
*
* To do:
*
* Copyright (C) 2009-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.Windows.Forms;
namespace BrightIdeasSoftware
{
/// <summary>
/// A VirtualListDataSource is a complete manner to provide functionality to a virtual list.
/// An object that implements this interface provides a VirtualObjectListView with all the
/// information it needs to be fully functional.
/// </summary>
/// <remarks>Implementors must provide functioning implementations of at least GetObjectCount()
/// and GetNthObject(), otherwise nothing will appear in the list.</remarks>
public interface IVirtualListDataSource
{
/// <summary>
/// Return the object that should be displayed at the n'th row.
/// </summary>
/// <param name="n">The index of the row whose object is to be returned.</param>
/// <returns>The model object at the n'th row, or null if the fetching was unsuccessful.</returns>
Object GetNthObject(int n);
/// <summary>
/// Return the number of rows that should be visible in the virtual list
/// </summary>
/// <returns>The number of rows the list view should have.</returns>
int GetObjectCount();
/// <summary>
/// Get the index of the row that is showing the given model object
/// </summary>
/// <param name="model">The model object sought</param>
/// <returns>The index of the row showing the model, or -1 if the object could not be found.</returns>
int GetObjectIndex(Object model);
/// <summary>
/// The ListView is about to request the given range of items. Do
/// whatever caching seems appropriate.
/// </summary>
/// <param name="first"></param>
/// <param name="last"></param>
void PrepareCache(int first, int last);
/// <summary>
/// Find the first row that "matches" the given text in the given range.
/// </summary>
/// <param name="value">The text typed by the user</param>
/// <param name="first">Start searching from this index. This may be greater than the 'to' parameter,
/// in which case the search should descend</param>
/// <param name="last">Do not search beyond this index. This may be less than the 'from' parameter.</param>
/// <param name="column">The column that should be considered when looking for a match.</param>
/// <returns>Return the index of row that was matched, or -1 if no match was found</returns>
int SearchText(string value, int first, int last, OLVColumn column);
/// <summary>
/// Sort the model objects in the data source.
/// </summary>
/// <param name="column"></param>
/// <param name="order"></param>
void Sort(OLVColumn column, SortOrder order);
//-----------------------------------------------------------------------------------
// Modification commands
// THINK: Should we split these four into a separate interface?
/// <summary>
/// Add the given collection of model objects to this control.
/// </summary>
/// <param name="modelObjects">A collection of model objects</param>
void AddObjects(ICollection modelObjects);
/// <summary>
/// Remove all of the given objects from the control
/// </summary>
/// <param name="modelObjects">Collection of objects to be removed</param>
void RemoveObjects(ICollection modelObjects);
/// <summary>
/// Set the collection of objects that this control will show.
/// </summary>
/// <param name="collection"></param>
void SetObjects(IEnumerable collection);
/// <summary>
/// Update/replace the nth object with the given object
/// </summary>
/// <param name="index"></param>
/// <param name="modelObject"></param>
void UpdateObject(int index, object modelObject);
}
/// <summary>
/// This extension allow virtual lists to filter their contents
/// </summary>
public interface IFilterableDataSource
{
/// <summary>
/// All subsequent retrievals on this data source should be filtered
/// through the given filters. null means no filtering of that kind.
/// </summary>
/// <param name="modelFilter"></param>
/// <param name="listFilter"></param>
void ApplyFilters(IModelFilter modelFilter, IListFilter listFilter);
}
/// <summary>
/// A do-nothing implementation of the VirtualListDataSource interface.
/// </summary>
public class AbstractVirtualListDataSource : IVirtualListDataSource, IFilterableDataSource
{
/// <summary>
/// Creates an AbstractVirtualListDataSource
/// </summary>
/// <param name="listView"></param>
public AbstractVirtualListDataSource(VirtualObjectListView listView) {
this.listView = listView;
}
/// <summary>
/// The list view that this data source is giving information to.
/// </summary>
protected VirtualObjectListView listView;
/// <summary>
///
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
public virtual object GetNthObject(int n) {
return null;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public virtual int GetObjectCount() {
return -1;
}
/// <summary>
///
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public virtual int GetObjectIndex(object model) {
return -1;
}
/// <summary>
///
/// </summary>
/// <param name="from"></param>
/// <param name="to"></param>
public virtual void PrepareCache(int from, int to) {
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <param name="first"></param>
/// <param name="last"></param>
/// <param name="column"></param>
/// <returns></returns>
public virtual int SearchText(string value, int first, int last, OLVColumn column) {
return -1;
}
/// <summary>
///
/// </summary>
/// <param name="column"></param>
/// <param name="order"></param>
public virtual void Sort(OLVColumn column, SortOrder order) {
}
/// <summary>
///
/// </summary>
/// <param name="modelObjects"></param>
public virtual void AddObjects(ICollection modelObjects) {
}
/// <summary>
///
/// </summary>
/// <param name="modelObjects"></param>
public virtual void RemoveObjects(ICollection modelObjects) {
}
/// <summary>
///
/// </summary>
/// <param name="collection"></param>
public virtual void SetObjects(IEnumerable collection) {
}
/// <summary>
/// Update/replace the nth object with the given object
/// </summary>
/// <param name="index"></param>
/// <param name="modelObject"></param>
public virtual void UpdateObject(int index, object modelObject) {
}
/// <summary>
/// This is a useful default implementation of SearchText method, intended to be called
/// by implementors of IVirtualListDataSource.
/// </summary>
/// <param name="value"></param>
/// <param name="first"></param>
/// <param name="last"></param>
/// <param name="column"></param>
/// <param name="source"></param>
/// <returns></returns>
static public int DefaultSearchText(string value, int first, int last, OLVColumn column, IVirtualListDataSource source) {
if (first <= last) {
for (int i = first; i <= last; i++) {
string data = column.GetStringValue(source.GetNthObject(i));
if (data.StartsWith(value, StringComparison.CurrentCultureIgnoreCase))
return i;
}
} else {
for (int i = first; i >= last; i--) {
string data = column.GetStringValue(source.GetNthObject(i));
if (data.StartsWith(value, StringComparison.CurrentCultureIgnoreCase))
return i;
}
}
return -1;
}
#region IFilterableDataSource Members
/// <summary>
///
/// </summary>
/// <param name="modelFilter"></param>
/// <param name="listFilter"></param>
virtual public void ApplyFilters(IModelFilter modelFilter, IListFilter listFilter) {
}
#endregion
}
/// <summary>
/// This class mimics the behavior of VirtualObjectListView v1.x.
/// </summary>
public class VirtualListVersion1DataSource : AbstractVirtualListDataSource
{
/// <summary>
/// Creates a VirtualListVersion1DataSource
/// </summary>
/// <param name="listView"></param>
public VirtualListVersion1DataSource(VirtualObjectListView listView)
: base(listView) {
}
#region Public properties
/// <summary>
/// How will the n'th object of the data source be fetched?
/// </summary>
public RowGetterDelegate RowGetter {
get { return rowGetter; }
set { rowGetter = value; }
}
private RowGetterDelegate rowGetter;
#endregion
#region IVirtualListDataSource implementation
/// <summary>
///
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
public override object GetNthObject(int n) {
if (this.RowGetter == null)
return null;
else
return this.RowGetter(n);
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <param name="first"></param>
/// <param name="last"></param>
/// <param name="column"></param>
/// <returns></returns>
public override int SearchText(string value, int first, int last, OLVColumn column) {
return DefaultSearchText(value, first, last, column, this);
}
#endregion
}
}

674
LICENSE Normal file
View File

@ -0,0 +1,674 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
{one line to give the program's name and a brief idea of what it does.}
Copyright (C) {year} {name of author}
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 3 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, see <http://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
{project} Copyright (C) {year} {fullname}
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<http://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<http://www.gnu.org/philosophy/why-not-lgpl.html>.

1672
OLVColumn.cs Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,550 @@
/*
* DesignSupport - Design time support for the various classes within ObjectListView
*
* Author: Phillip Piper
* Date: 12/08/2009 8:36 PM
*
* Change log:
* 2012-08-27 JPP - Fall back to more specific type name for the ListViewDesigner if
* the first GetType() fails.
* v2.5.1
* 2012-04-26 JPP - Filter group events from TreeListView since it can't have groups
* 2011-06-06 JPP - Vastly improved ObjectListViewDesigner, based off information in
* "'Inheriting' from an Internal WinForms Designer" on CodeProject.
* v2.3
* 2009-08-12 JPP - Initial version
*
* To do:
*
* Copyright (C) 2009-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace BrightIdeasSoftware.Design
{
/// <summary>
/// Designer for <see cref="ObjectListView"/> and its subclasses.
/// </summary>
/// <remarks>
/// <para>
/// This designer removes properties and events that are available on ListView but that are not
/// useful on ObjectListView.
/// </para>
/// <para>
/// We can't inherit from System.Windows.Forms.Design.ListViewDesigner, since it is marked internal.
/// So, this class uses reflection to create a ListViewDesigner and then forwards messages to that designer.
/// </para>
/// </remarks>
public class ObjectListViewDesigner : ControlDesigner
{
#region Initialize & Dispose
/// <summary>
/// Initializes the designer with the specified component.
/// </summary>
/// <param name="component">The <see cref="T:System.ComponentModel.IComponent"/> to associate the designer with. This component must always be an instance of, or derive from, <see cref="T:System.Windows.Forms.Control"/>. </param>
public override void Initialize(IComponent component) {
// Debug.WriteLine("ObjectListViewDesigner.Initialize");
// Use reflection to bypass the "internal" marker on ListViewDesigner
// If we can't get the unversioned designer, look specifically for .NET 4.0 version of it.
Type tListViewDesigner = Type.GetType("System.Windows.Forms.Design.ListViewDesigner, System.Design") ??
Type.GetType("System.Windows.Forms.Design.ListViewDesigner, System.Design, " +
"Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
if (tListViewDesigner == null) throw new ArgumentException("Could not load ListViewDesigner");
this.listViewDesigner = (ControlDesigner)Activator.CreateInstance(tListViewDesigner, BindingFlags.Instance | BindingFlags.Public, null, null, null);
this.designerFilter = this.listViewDesigner;
// Fetch the methods from the ListViewDesigner that we know we want to use
this.listViewDesignGetHitTest = tListViewDesigner.GetMethod("GetHitTest", BindingFlags.Instance | BindingFlags.NonPublic);
this.listViewDesignWndProc = tListViewDesigner.GetMethod("WndProc", BindingFlags.Instance | BindingFlags.NonPublic);
Debug.Assert(this.listViewDesignGetHitTest != null, "Required method (GetHitTest) not found on ListViewDesigner");
Debug.Assert(this.listViewDesignWndProc != null, "Required method (WndProc) not found on ListViewDesigner");
// Tell the Designer to use properties of default designer as well as the properties of this class (do before base.Initialize)
TypeDescriptor.CreateAssociation(component, this.listViewDesigner);
IServiceContainer site = (IServiceContainer)component.Site;
if (site != null && GetService(typeof(DesignerCommandSet)) == null) {
site.AddService(typeof(DesignerCommandSet), new CDDesignerCommandSet(this));
} else {
Debug.Fail("site != null && GetService(typeof (DesignerCommandSet)) == null");
}
this.listViewDesigner.Initialize(component);
base.Initialize(component);
RemoveDuplicateDockingActionList();
}
/// <summary>
/// Initializes a newly created component.
/// </summary>
/// <param name="defaultValues">A name/value dictionary of default values to apply to properties. May be null if no default values are specified.</param>
public override void InitializeNewComponent(IDictionary defaultValues) {
// Debug.WriteLine("ObjectListViewDesigner.InitializeNewComponent");
base.InitializeNewComponent(defaultValues);
this.listViewDesigner.InitializeNewComponent(defaultValues);
}
/// <summary>
/// Releases the unmanaged resources used by the <see cref="T:System.Windows.Forms.Design.ControlDesigner"/> and optionally releases the managed resources.
/// </summary>
/// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources. </param>
protected override void Dispose(bool disposing) {
// Debug.WriteLine("ObjectListViewDesigner.Dispose");
if (disposing) {
if (this.listViewDesigner != null) {
this.listViewDesigner.Dispose();
// Normally we would now null out the designer, but this designer
// still has methods called AFTER it is disposed.
}
}
base.Dispose(disposing);
}
/// <summary>
/// Removes the duplicate DockingActionList added by this designer to the <see cref="DesignerActionService"/>.
/// </summary>
/// <remarks>
/// <see cref="ControlDesigner.Initialize"/> adds an internal DockingActionList : 'Dock/Undock in Parent Container'.
/// But the default designer has already added that action list. So we need to remove one.
/// </remarks>
private void RemoveDuplicateDockingActionList() {
// This is a true hack -- in a class that is basically a huge hack itself.
// Reach into the bowel of our base class, get a private field, and use that fields value to
// remove an action from the designer.
// In ControlDesigner, there is "private DockingActionList dockingAction;"
// Don't you just love Reflector?!
FieldInfo fi = typeof(ControlDesigner).GetField("dockingAction", BindingFlags.Instance | BindingFlags.NonPublic);
if (fi != null) {
DesignerActionList dockingAction = (DesignerActionList)fi.GetValue(this);
if (dockingAction != null) {
DesignerActionService service = (DesignerActionService)GetService(typeof(DesignerActionService));
if (service != null) {
service.Remove(this.Control, dockingAction);
}
}
}
}
#endregion
#region IDesignerFilter overrides
/// <summary>
/// Adjusts the set of properties the component exposes through a <see cref="T:System.ComponentModel.TypeDescriptor"/>.
/// </summary>
/// <param name="properties">An <see cref="T:System.Collections.IDictionary"/> containing the properties for the class of the component. </param>
protected override void PreFilterProperties(IDictionary properties) {
// Debug.WriteLine("ObjectListViewDesigner.PreFilterProperties");
// Always call the base PreFilterProperties implementation
// before you modify the properties collection.
base.PreFilterProperties(properties);
// Give the listviewdesigner a chance to filter the properties
// (though we already know it's not going to do anything)
this.designerFilter.PreFilterProperties(properties);
// I'd like to just remove the redundant properties, but that would
// break backward compatibility. The deserialiser that handles the XXX.Designer.cs file
// works off the designer, so even if the property exists in the class, the deserialiser will
// throw an error if the associated designer actually removes that property.
// So we shadow the unwanted properties, and give the replacement properties
// non-browsable attributes so that they are hidden from the user
List<string> unwantedProperties = new List<string>(new string[] {
"BackgroundImage", "BackgroundImageTiled", "HotTracking", "HoverSelection",
"LabelEdit", "VirtualListSize", "VirtualMode" });
// Also hid Tooltip properties, since giving a tooltip to the control through the IDE
// messes up the tooltip handling
foreach (string propertyName in properties.Keys) {
if (propertyName.StartsWith("ToolTip")) {
unwantedProperties.Add(propertyName);
}
}
// If we are looking at a TreeListView, remove group related properties
// since TreeListViews can't show groups
if (this.Control is TreeListView) {
unwantedProperties.AddRange(new string[] {
"GroupImageList", "GroupWithItemCountFormat", "GroupWithItemCountSingularFormat", "HasCollapsibleGroups",
"SpaceBetweenGroups", "ShowGroups", "SortGroupItemsByPrimaryColumn", "ShowItemCountOnGroups"
});
}
// Shadow the unwanted properties, and give the replacement properties
// non-browsable attributes so that they are hidden from the user
foreach (string unwantedProperty in unwantedProperties) {
PropertyDescriptor propertyDesc = TypeDescriptor.CreateProperty(
typeof(ObjectListView),
(PropertyDescriptor)properties[unwantedProperty],
new BrowsableAttribute(false));
properties[unwantedProperty] = propertyDesc;
}
}
/// <summary>
/// Allows a designer to add to the set of events that it exposes through a <see cref="T:System.ComponentModel.TypeDescriptor"/>.
/// </summary>
/// <param name="events">The events for the class of the component. </param>
protected override void PreFilterEvents(IDictionary events) {
// Debug.WriteLine("ObjectListViewDesigner.PreFilterEvents");
base.PreFilterEvents(events);
this.designerFilter.PreFilterEvents(events);
// Remove the events that don't make sense for an ObjectListView.
// See PreFilterProperties() for why we do this dance rather than just remove the event.
List<string> unwanted = new List<string>(new string[] {
"AfterLabelEdit",
"BeforeLabelEdit",
"DrawColumnHeader",
"DrawItem",
"DrawSubItem",
"RetrieveVirtualItem",
"SearchForVirtualItem",
"VirtualItemsSelectionRangeChanged"
});
// If we are looking at a TreeListView, remove group related events
// since TreeListViews can't show groups
if (this.Control is TreeListView) {
unwanted.AddRange(new string[] {
"AboutToCreateGroups",
"AfterCreatingGroups",
"BeforeCreatingGroups",
"GroupTaskClicked",
"GroupExpandingCollapsing",
"GroupStateChanged"
});
}
foreach (string unwantedEvent in unwanted) {
EventDescriptor eventDesc = TypeDescriptor.CreateEvent(
typeof(ObjectListView),
(EventDescriptor)events[unwantedEvent],
new BrowsableAttribute(false));
events[unwantedEvent] = eventDesc;
}
}
/// <summary>
/// Allows a designer to change or remove items from the set of attributes that it exposes through a <see cref="T:System.ComponentModel.TypeDescriptor"/>.
/// </summary>
/// <param name="attributes">The attributes for the class of the component. </param>
protected override void PostFilterAttributes(IDictionary attributes) {
// Debug.WriteLine("ObjectListViewDesigner.PostFilterAttributes");
this.designerFilter.PostFilterAttributes(attributes);
base.PostFilterAttributes(attributes);
}
/// <summary>
/// Allows a designer to change or remove items from the set of events that it exposes through a <see cref="T:System.ComponentModel.TypeDescriptor"/>.
/// </summary>
/// <param name="events">The events for the class of the component. </param>
protected override void PostFilterEvents(IDictionary events) {
// Debug.WriteLine("ObjectListViewDesigner.PostFilterEvents");
this.designerFilter.PostFilterEvents(events);
base.PostFilterEvents(events);
}
#endregion
#region Overrides
/// <summary>
/// Gets the design-time action lists supported by the component associated with the designer.
/// </summary>
/// <returns>
/// The design-time action lists supported by the component associated with the designer.
/// </returns>
public override DesignerActionListCollection ActionLists {
get {
// We want to change the first action list so it only has the commands we want
DesignerActionListCollection actionLists = this.listViewDesigner.ActionLists;
if (actionLists.Count > 0 && !(actionLists[0] is ListViewActionListAdapter)) {
actionLists[0] = new ListViewActionListAdapter(this, actionLists[0]);
}
return actionLists;
}
}
/// <summary>
/// Gets the collection of components associated with the component managed by the designer.
/// </summary>
/// <returns>
/// The components that are associated with the component managed by the designer.
/// </returns>
public override ICollection AssociatedComponents {
get {
ArrayList components = new ArrayList(base.AssociatedComponents);
components.AddRange(this.listViewDesigner.AssociatedComponents);
return components;
}
}
/// <summary>
/// Indicates whether a mouse click at the specified point should be handled by the control.
/// </summary>
/// <returns>
/// true if a click at the specified point is to be handled by the control; otherwise, false.
/// </returns>
/// <param name="point">A <see cref="T:System.Drawing.Point"/> indicating the position at which the mouse was clicked, in screen coordinates. </param>
protected override bool GetHitTest(Point point) {
// The ListViewDesigner wants to allow column dividers to be resized
return (bool)this.listViewDesignGetHitTest.Invoke(listViewDesigner, new object[] { point });
}
/// <summary>
/// Processes Windows messages and optionally routes them to the control.
/// </summary>
/// <param name="m">The <see cref="T:System.Windows.Forms.Message"/> to process. </param>
protected override void WndProc(ref Message m) {
switch (m.Msg) {
case 0x4e:
case 0x204e:
// The listview designer is interested in HDN_ENDTRACK notifications
this.listViewDesignWndProc.Invoke(listViewDesigner, new object[] { m });
break;
default:
base.WndProc(ref m);
break;
}
}
#endregion
#region Implementation variables
private ControlDesigner listViewDesigner;
private IDesignerFilter designerFilter;
private MethodInfo listViewDesignGetHitTest;
private MethodInfo listViewDesignWndProc;
#endregion
#region Custom action list
/// <summary>
/// This class modifies a ListViewActionList, by removing the "Edit Items" and "Edit Groups" actions.
/// </summary>
/// <remarks>
/// <para>
/// That class is internal, so we cannot simply subclass it, which would be simplier.
/// </para>
/// <para>
/// Action lists use reflection to determine if that action can be executed, so we not
/// only have to modify the returned collection of actions, but we have to implement
/// the properties and commands that the returned actions use. </para>
/// </remarks>
private class ListViewActionListAdapter : DesignerActionList
{
public ListViewActionListAdapter(ObjectListViewDesigner designer, DesignerActionList wrappedList)
: base(wrappedList.Component) {
this.designer = designer;
this.wrappedList = wrappedList;
}
public override DesignerActionItemCollection GetSortedActionItems() {
DesignerActionItemCollection items = wrappedList.GetSortedActionItems();
items.RemoveAt(2); // remove Edit Groups
items.RemoveAt(0); // remove Edit Items
return items;
}
private void EditValue(ComponentDesigner componentDesigner, IComponent iComponent, string propertyName) {
// One more complication. The ListViewActionList classes uses an internal class, EditorServiceContext, to
// edit the items/columns/groups collections. So, we use reflection to bypass the data hiding.
Type tEditorServiceContext = Type.GetType("System.Windows.Forms.Design.EditorServiceContext, System.Design");
tEditorServiceContext.InvokeMember("EditValue", BindingFlags.InvokeMethod | BindingFlags.Static, null, null, new object[] { componentDesigner, iComponent, propertyName });
}
private void SetValue(object target, string propertyName, object value) {
TypeDescriptor.GetProperties(target)[propertyName].SetValue(target, value);
}
public void InvokeColumnsDialog() {
EditValue(this.designer, base.Component, "Columns");
}
// Don't need these since we removed their corresponding actions from the list.
// Keep the methods just in case.
//public void InvokeGroupsDialog() {
// EditValue(this.designer, base.Component, "Groups");
//}
//public void InvokeItemsDialog() {
// EditValue(this.designer, base.Component, "Items");
//}
public ImageList LargeImageList {
get { return ((ListView)base.Component).LargeImageList; }
set { SetValue(base.Component, "LargeImageList", value); }
}
public ImageList SmallImageList {
get { return ((ListView)base.Component).SmallImageList; }
set { SetValue(base.Component, "SmallImageList", value); }
}
public View View {
get { return ((ListView)base.Component).View; }
set { SetValue(base.Component, "View", value); }
}
ObjectListViewDesigner designer;
DesignerActionList wrappedList;
}
#endregion
#region DesignerCommandSet
private class CDDesignerCommandSet : DesignerCommandSet
{
public CDDesignerCommandSet(ComponentDesigner componentDesigner) {
this.componentDesigner = componentDesigner;
}
public override ICollection GetCommands(string name) {
// Debug.WriteLine("CDDesignerCommandSet.GetCommands:" + name);
if (componentDesigner != null) {
if (name.Equals("Verbs")) {
return componentDesigner.Verbs;
}
if (name.Equals("ActionLists")) {
return componentDesigner.ActionLists;
}
}
return base.GetCommands(name);
}
private readonly ComponentDesigner componentDesigner;
}
#endregion
}
/// <summary>
/// This class works in conjunction with the OLVColumns property to allow OLVColumns
/// to be added to the ObjectListView.
/// </summary>
public class OLVColumnCollectionEditor : System.ComponentModel.Design.CollectionEditor
{
/// <summary>
/// Create a OLVColumnCollectionEditor
/// </summary>
/// <param name="t"></param>
public OLVColumnCollectionEditor(Type t)
: base(t) {
}
/// <summary>
/// What type of object does this editor create?
/// </summary>
/// <returns></returns>
protected override Type CreateCollectionItemType() {
return typeof(OLVColumn);
}
/// <summary>
/// Edit a given value
/// </summary>
/// <param name="context"></param>
/// <param name="provider"></param>
/// <param name="value"></param>
/// <returns></returns>
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) {
if (context == null)
throw new ArgumentNullException("context");
if (provider == null)
throw new ArgumentNullException("provider");
// Figure out which ObjectListView we are working on. This should be the Instance of the context.
ObjectListView olv = context.Instance as ObjectListView;
Debug.Assert(olv != null, "Instance must be an ObjectListView");
// Edit all the columns, not just the ones that are visible
base.EditValue(context, provider, olv.AllColumns);
// Set the columns on the ListView to just the visible columns
List<OLVColumn> newColumns = olv.GetFilteredColumns(View.Details);
olv.Columns.Clear();
olv.Columns.AddRange(newColumns.ToArray());
return olv.Columns;
}
/// <summary>
/// What text should be shown in the list for the given object?
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
protected override string GetDisplayText(object value) {
OLVColumn col = value as OLVColumn;
if (col == null || String.IsNullOrEmpty(col.AspectName))
return base.GetDisplayText(value);
return String.Format("{0} ({1})", base.GetDisplayText(value), col.AspectName);
}
}
/// <summary>
/// Control how the overlay is presented in the IDE
/// </summary>
internal class OverlayConverter : ExpandableObjectConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
return destinationType == typeof(string) || base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
if (destinationType == typeof(string)) {
ImageOverlay imageOverlay = value as ImageOverlay;
if (imageOverlay != null) {
return imageOverlay.Image == null ? "(none)" : "(set)";
}
TextOverlay textOverlay = value as TextOverlay;
if (textOverlay != null) {
return String.IsNullOrEmpty(textOverlay.Text) ? "(none)" : "(set)";
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
}

3521
ObjectListView.FxCop Normal file

File diff suppressed because it is too large Load Diff

10482
ObjectListView.cs Normal file

File diff suppressed because it is too large Load Diff

47
ObjectListView.shfb Normal file
View File

@ -0,0 +1,47 @@
<project schemaVersion="1.6.0.2">
<assemblies>
<assembly assemblyPath=".\bin\Debug\ObjectListViewDemo.exe" xmlCommentsPath=".\bin\Debug\ObjectListViewDemo.XML" commentsOnly="False" />
</assemblies>
<namespaceSummaries>
<namespaceSummaryItem name="" isDocumented="False" />
<namespaceSummaryItem name="BrightIdeasSoftware" isDocumented="True">All ObjectListView appears in this namespace</namespaceSummaryItem>
<namespaceSummaryItem name="ObjectListViewDemo" isDocumented="False" />
</namespaceSummaries>
<ProjectSummary>ObjectListViewDemo demonstrates helpful techniques when using an ObjectListView</ProjectSummary>
<MissingTags>Summary, Parameter, Returns, AutoDocumentCtors, Namespace</MissingTags>
<VisibleItems>InheritedMembers, Protected, SealedProtected</VisibleItems>
<HtmlHelp1xCompilerPath path="" />
<HtmlHelp2xCompilerPath path="" />
<OutputPath>.\Help\</OutputPath>
<SandcastlePath path="" />
<WorkingPath path="" />
<CleanIntermediates>True</CleanIntermediates>
<KeepLogFile>True</KeepLogFile>
<HelpFileFormat>HtmlHelp1x</HelpFileFormat>
<PurgeDuplicateTopics>True</PurgeDuplicateTopics>
<CppCommentsFixup>False</CppCommentsFixup>
<FrameworkVersion>2.0.50727</FrameworkVersion>
<BinaryTOC>True</BinaryTOC>
<IncludeFavorites>False</IncludeFavorites>
<Preliminary>True</Preliminary>
<RootNamespaceContainer>False</RootNamespaceContainer>
<RootNamespaceTitle />
<HelpTitle>ObjectListView Reference</HelpTitle>
<HtmlHelpName>Documentation</HtmlHelpName>
<Language>en-US</Language>
<CopyrightHref />
<CopyrightText>(c) Copyright 2006-2008 Phillip Piper All Rights Reserved</CopyrightText>
<FeedbackEMailAddress>phillip_piper@bigfoot.com</FeedbackEMailAddress>
<HeaderText />
<FooterText />
<ProjectLinkType>Local</ProjectLinkType>
<SdkLinkType>Msdn</SdkLinkType>
<SdkLinkTarget>Blank</SdkLinkTarget>
<PresentationStyle>Prototype</PresentationStyle>
<NamingMethod>Guid</NamingMethod>
<SyntaxFilters>CSharp</SyntaxFilters>
<ShowFeedbackControl>False</ShowFeedbackControl>
<ContentPlacement>AboveNamespaces</ContentPlacement>
<ContentSiteMap path="" />
<TopicFileTransform path="" />
</project>

178
ObjectListView2005.csproj Normal file
View File

@ -0,0 +1,178 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{18FEDA0C-D147-4286-B39A-01204808106A}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>BrightIdeasSoftware</RootNamespace>
<AssemblyName>ObjectListView</AssemblyName>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>olv-keyfile.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="Accessibility" />
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Design" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DataTreeListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="DragDrop\OLVDataObject.cs" />
<Compile Include="Filtering\ClustersFromGroupsStrategy.cs" />
<Compile Include="Filtering\DateTimeClusteringStrategy.cs" />
<Compile Include="Filtering\FlagClusteringStrategy.cs" />
<Compile Include="Filtering\TextMatchFilter.cs" />
<Compile Include="Implementation\Delegates.cs" />
<Compile Include="Implementation\Enums.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Implementation\GroupingParameters.cs" />
<Compile Include="Implementation\NullableDictionary.cs" />
<Compile Include="Implementation\OLVListItem.cs" />
<Compile Include="Implementation\OLVListSubItem.cs" />
<Compile Include="Implementation\OlvListViewHitTestInfo.cs" />
<Compile Include="Implementation\TreeDataSourceAdapter.cs" />
<Compile Include="OLVColumn.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Rendering\Adornments.cs" />
<Compile Include="Implementation\Attributes.cs" />
<Compile Include="CellEditing\CellEditors.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="CellEditing\EditorRegistry.cs" />
<Compile Include="DataListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Implementation\DataSourceAdapter.cs" />
<Compile Include="Rendering\Decorations.cs" />
<Compile Include="DragDrop\DragSource.cs" />
<Compile Include="DragDrop\DropSink.cs" />
<Compile Include="Implementation\Events.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="FastDataListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Filtering\ClusteringStrategy.cs" />
<Compile Include="Filtering\Cluster.cs" />
<Compile Include="FastObjectListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Filtering\FilterMenuBuilder.cs" />
<Compile Include="Filtering\Filters.cs" />
<Compile Include="Filtering\ICluster.cs" />
<Compile Include="Filtering\IClusteringStrategy.cs" />
<Compile Include="Rendering\TreeRenderer.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="SubControls\ToolStripCheckedListBox.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Utilities\ColumnSelectionForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Utilities\ColumnSelectionForm.Designer.cs">
<DependentUpon>ColumnSelectionForm.cs</DependentUpon>
</Compile>
<Compile Include="Utilities\Generator.cs" />
<Compile Include="SubControls\GlassPanelForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Implementation\Groups.cs" />
<Compile Include="SubControls\HeaderControl.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="CellEditing\CellEditKeyEngine.cs" />
<Compile Include="Implementation\Munger.cs" />
<Compile Include="Implementation\NativeMethods.cs" />
<Compile Include="Implementation\Comparers.cs">
</Compile>
<Compile Include="ObjectListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="ObjectListView.DesignTime.cs" />
<Compile Include="Rendering\Overlays.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Rendering\Renderers.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Rendering\Styles.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="SubControls\ToolTipControl.cs" />
<Compile Include="TreeListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Utilities\OLVExporter.cs" />
<Compile Include="Utilities\TypedObjectListView.cs" />
<Compile Include="Implementation\VirtualGroups.cs" />
<Compile Include="Implementation\VirtualListDataSource.cs" />
<Compile Include="VirtualObjectListView.cs">
<SubType>Component</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<Content Include="CustomDictionary.xml" />
<None Include="Resources\sort-descending.png" />
<None Include="Resources\sort-ascending.png" />
<None Include="Resources\filter.png" />
<None Include="Resources\clear-filter.png" />
<None Include="Resources\filter-icons3.png" />
</ItemGroup>
<ItemGroup>
<None Include="olv-keyfile.snk" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="Utilities\ColumnSelectionForm.resx">
<DependentUpon>ColumnSelectionForm.cs</DependentUpon>
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

188
ObjectListView2008.csproj Normal file
View File

@ -0,0 +1,188 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{18FEDA0C-D147-4286-B39A-01204808106A}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>BrightIdeasSoftware</RootNamespace>
<AssemblyName>ObjectListView</AssemblyName>
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>2.0</OldToolsVersion>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>olv-keyfile.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
<DocumentationFile>
</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
<DocumentationFile>
</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Accessibility" />
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Design" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DataTreeListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="DragDrop\OLVDataObject.cs" />
<Compile Include="Filtering\ClustersFromGroupsStrategy.cs" />
<Compile Include="Filtering\DateTimeClusteringStrategy.cs" />
<Compile Include="Filtering\FlagClusteringStrategy.cs" />
<Compile Include="Filtering\TextMatchFilter.cs" />
<Compile Include="Implementation\Delegates.cs" />
<Compile Include="Implementation\Enums.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Implementation\GroupingParameters.cs" />
<Compile Include="Implementation\NullableDictionary.cs" />
<Compile Include="Implementation\OLVListItem.cs" />
<Compile Include="Implementation\OLVListSubItem.cs" />
<Compile Include="Implementation\OlvListViewHitTestInfo.cs" />
<Compile Include="Implementation\TreeDataSourceAdapter.cs" />
<Compile Include="OLVColumn.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Rendering\Adornments.cs" />
<Compile Include="Implementation\Attributes.cs" />
<Compile Include="CellEditing\CellEditors.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="CellEditing\EditorRegistry.cs" />
<Compile Include="DataListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Implementation\DataSourceAdapter.cs" />
<Compile Include="Rendering\Decorations.cs" />
<Compile Include="DragDrop\DragSource.cs" />
<Compile Include="DragDrop\DropSink.cs" />
<Compile Include="Implementation\Events.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="FastDataListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Filtering\ClusteringStrategy.cs" />
<Compile Include="Filtering\Cluster.cs" />
<Compile Include="FastObjectListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Filtering\FilterMenuBuilder.cs" />
<Compile Include="Filtering\Filters.cs" />
<Compile Include="Filtering\ICluster.cs" />
<Compile Include="Filtering\IClusteringStrategy.cs" />
<Compile Include="Rendering\TreeRenderer.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="SubControls\ToolStripCheckedListBox.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Utilities\ColumnSelectionForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Utilities\ColumnSelectionForm.Designer.cs">
<DependentUpon>ColumnSelectionForm.cs</DependentUpon>
</Compile>
<Compile Include="Utilities\Generator.cs" />
<Compile Include="SubControls\GlassPanelForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Implementation\Groups.cs" />
<Compile Include="SubControls\HeaderControl.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="CellEditing\CellEditKeyEngine.cs" />
<Compile Include="Implementation\Munger.cs" />
<Compile Include="Implementation\NativeMethods.cs" />
<Compile Include="Implementation\Comparers.cs">
</Compile>
<Compile Include="ObjectListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="ObjectListView.DesignTime.cs" />
<Compile Include="Rendering\Overlays.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Rendering\Renderers.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Rendering\Styles.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="SubControls\ToolTipControl.cs" />
<Compile Include="TreeListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Utilities\OLVExporter.cs" />
<Compile Include="Utilities\TypedObjectListView.cs" />
<Compile Include="Implementation\VirtualGroups.cs" />
<Compile Include="Implementation\VirtualListDataSource.cs" />
<Compile Include="VirtualObjectListView.cs">
<SubType>Component</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<Content Include="CustomDictionary.xml" />
<None Include="Resources\sort-descending.png" />
<None Include="Resources\sort-ascending.png" />
<None Include="Resources\filter.png" />
<None Include="Resources\clear-filter.png" />
<None Include="Resources\filter-icons3.png" />
</ItemGroup>
<ItemGroup>
<None Include="olv-keyfile.snk" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="Utilities\ColumnSelectionForm.resx">
<DependentUpon>ColumnSelectionForm.cs</DependentUpon>
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@ -0,0 +1,16 @@
<ProjectConfiguration>
<CopyReferencedAssembliesToWorkspace>false</CopyReferencedAssembliesToWorkspace>
<ConsiderInconclusiveTestsAsPassing>false</ConsiderInconclusiveTestsAsPassing>
<IgnoreThisComponentCompletely>false</IgnoreThisComponentCompletely>
<RunPreBuildEvents>false</RunPreBuildEvents>
<RunPostBuildEvents>false</RunPostBuildEvents>
<PreviouslyBuiltSuccessfully>true</PreviouslyBuiltSuccessfully>
<InstrumentAssembly>true</InstrumentAssembly>
<PreventSigningOfAssembly>false</PreventSigningOfAssembly>
<AnalyseExecutionTimes>true</AnalyseExecutionTimes>
<IncludeStaticReferencesInWorkspace>true</IncludeStaticReferencesInWorkspace>
<DefaultTestTimeout>60000</DefaultTestTimeout>
<UseBuildConfiguration />
<ProxyProcessPath />
<UseCPUArchitecture>AutoDetect</UseCPUArchitecture>
</ProjectConfiguration>

188
ObjectListView2010.csproj Normal file
View File

@ -0,0 +1,188 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{18FEDA0C-D147-4286-B39A-01204808106A}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>BrightIdeasSoftware</RootNamespace>
<AssemblyName>ObjectListView</AssemblyName>
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>3.5</OldToolsVersion>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>olv-keyfile.snk</AssemblyOriginatorKeyFile>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
<DocumentationFile>bin\Debug\ObjectListView.XML</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="Accessibility" />
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Design" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DataTreeListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="DragDrop\OLVDataObject.cs" />
<Compile Include="Filtering\DateTimeClusteringStrategy.cs" />
<Compile Include="Filtering\FlagClusteringStrategy.cs" />
<Compile Include="Filtering\TextMatchFilter.cs" />
<Compile Include="Implementation\Delegates.cs" />
<Compile Include="Implementation\Enums.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Implementation\GroupingParameters.cs" />
<Compile Include="Implementation\NullableDictionary.cs" />
<Compile Include="Implementation\OLVListItem.cs" />
<Compile Include="Implementation\OLVListSubItem.cs" />
<Compile Include="Implementation\OlvListViewHitTestInfo.cs" />
<Compile Include="Implementation\TreeDataSourceAdapter.cs" />
<Compile Include="OLVColumn.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Rendering\Adornments.cs" />
<Compile Include="Implementation\Attributes.cs" />
<Compile Include="CellEditing\CellEditors.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="CellEditing\EditorRegistry.cs" />
<Compile Include="DataListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Implementation\DataSourceAdapter.cs" />
<Compile Include="Rendering\Decorations.cs" />
<Compile Include="DragDrop\DragSource.cs" />
<Compile Include="DragDrop\DropSink.cs" />
<Compile Include="Implementation\Events.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="FastDataListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Filtering\ClusteringStrategy.cs" />
<Compile Include="Filtering\Cluster.cs" />
<Compile Include="Filtering\ClustersFromGroupsStrategy.cs" />
<Compile Include="FastObjectListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Filtering\FilterMenuBuilder.cs" />
<Compile Include="Filtering\Filters.cs" />
<Compile Include="Filtering\ICluster.cs" />
<Compile Include="Filtering\IClusteringStrategy.cs" />
<Compile Include="Rendering\TreeRenderer.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="SubControls\ToolStripCheckedListBox.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Utilities\ColumnSelectionForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Utilities\ColumnSelectionForm.Designer.cs">
<DependentUpon>ColumnSelectionForm.cs</DependentUpon>
</Compile>
<Compile Include="Utilities\Generator.cs" />
<Compile Include="SubControls\GlassPanelForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Implementation\Groups.cs" />
<Compile Include="SubControls\HeaderControl.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="CellEditing\CellEditKeyEngine.cs" />
<Compile Include="Implementation\Munger.cs" />
<Compile Include="Implementation\NativeMethods.cs" />
<Compile Include="Implementation\Comparers.cs">
</Compile>
<Compile Include="ObjectListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="ObjectListView.DesignTime.cs" />
<Compile Include="Rendering\Overlays.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Rendering\Renderers.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Rendering\Styles.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="SubControls\ToolTipControl.cs" />
<Compile Include="TreeListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Utilities\OLVExporter.cs" />
<Compile Include="Utilities\TypedObjectListView.cs" />
<Compile Include="Implementation\VirtualGroups.cs" />
<Compile Include="Implementation\VirtualListDataSource.cs" />
<Compile Include="VirtualObjectListView.cs">
<SubType>Component</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<Content Include="CustomDictionary.xml" />
<None Include="FullClassDiagram.cd" />
<None Include="Resources\sort-descending.png" />
<None Include="Resources\sort-ascending.png" />
<None Include="Resources\filter.png" />
<None Include="Resources\clear-filter.png" />
<None Include="Resources\filter-icons3.png" />
</ItemGroup>
<ItemGroup>
<None Include="olv-keyfile.snk" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="Utilities\ColumnSelectionForm.resx">
<DependentUpon>ColumnSelectionForm.cs</DependentUpon>
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@ -0,0 +1,27 @@
<ProjectConfiguration>
<CopyReferencedAssembliesToWorkspace>false</CopyReferencedAssembliesToWorkspace>
<ConsiderInconclusiveTestsAsPassing>false</ConsiderInconclusiveTestsAsPassing>
<PreloadReferencedAssemblies>false</PreloadReferencedAssemblies>
<AllowDynamicCodeContractChecking>true</AllowDynamicCodeContractChecking>
<AllowStaticCodeContractChecking>false</AllowStaticCodeContractChecking>
<IgnoreThisComponentCompletely>false</IgnoreThisComponentCompletely>
<RunPreBuildEvents>false</RunPreBuildEvents>
<RunPostBuildEvents>false</RunPostBuildEvents>
<PreviouslyBuiltSuccessfully>true</PreviouslyBuiltSuccessfully>
<InstrumentAssembly>true</InstrumentAssembly>
<PreventSigningOfAssembly>false</PreventSigningOfAssembly>
<AnalyseExecutionTimes>true</AnalyseExecutionTimes>
<IncludeStaticReferencesInWorkspace>true</IncludeStaticReferencesInWorkspace>
<DefaultTestTimeout>60000</DefaultTestTimeout>
<UseBuildConfiguration></UseBuildConfiguration>
<UseBuildPlatform />
<ProxyProcessPath></ProxyProcessPath>
<UseCPUArchitecture>AutoDetect</UseCPUArchitecture>
<MSTestThreadApartmentState>STA</MSTestThreadApartmentState>
<BuildProcessArchitecture>x86</BuildProcessArchitecture>
<IgnoredTests>
<RegexTestSelector>
<RegularExpression>.*</RegularExpression>
</RegexTestSelector>
</IgnoredTests>
</ProjectConfiguration>

198
ObjectListView2012.csproj Normal file
View File

@ -0,0 +1,198 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{18FEDA0C-D147-4286-B39A-01204808106A}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>BrightIdeasSoftware</RootNamespace>
<AssemblyName>ObjectListView</AssemblyName>
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>3.5</OldToolsVersion>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>olv-keyfile.snk</AssemblyOriginatorKeyFile>
<TargetFrameworkProfile />
<SccProjectName>
</SccProjectName>
<SccLocalPath>
</SccLocalPath>
<SccAuxPath>
</SccAuxPath>
<SccProvider>
</SccProvider>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
<DocumentationFile>bin\Debug\ObjectListView.XML</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
<DocumentationFile>bin\Release\ObjectListView.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Accessibility" />
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Design" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DataTreeListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="DragDrop\OLVDataObject.cs" />
<Compile Include="Filtering\DateTimeClusteringStrategy.cs" />
<Compile Include="Filtering\FlagClusteringStrategy.cs" />
<Compile Include="Filtering\TextMatchFilter.cs" />
<Compile Include="Implementation\Delegates.cs" />
<Compile Include="Implementation\Enums.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Implementation\GroupingParameters.cs" />
<Compile Include="Implementation\NullableDictionary.cs" />
<Compile Include="Implementation\OLVListItem.cs" />
<Compile Include="Implementation\OLVListSubItem.cs" />
<Compile Include="Implementation\OlvListViewHitTestInfo.cs" />
<Compile Include="Implementation\TreeDataSourceAdapter.cs" />
<Compile Include="OLVColumn.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Rendering\Adornments.cs" />
<Compile Include="Implementation\Attributes.cs" />
<Compile Include="CellEditing\CellEditors.cs" />
<Compile Include="CellEditing\EditorRegistry.cs" />
<Compile Include="DataListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Implementation\DataSourceAdapter.cs" />
<Compile Include="Rendering\Decorations.cs" />
<Compile Include="DragDrop\DragSource.cs" />
<Compile Include="DragDrop\DropSink.cs" />
<Compile Include="Implementation\Events.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="FastDataListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Filtering\ClusteringStrategy.cs" />
<Compile Include="Filtering\Cluster.cs" />
<Compile Include="Filtering\ClustersFromGroupsStrategy.cs" />
<Compile Include="FastObjectListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Filtering\FilterMenuBuilder.cs" />
<Compile Include="Filtering\Filters.cs" />
<Compile Include="Filtering\ICluster.cs" />
<Compile Include="Filtering\IClusteringStrategy.cs" />
<Compile Include="Rendering\TreeRenderer.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="SubControls\ToolStripCheckedListBox.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Utilities\ColumnSelectionForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Utilities\ColumnSelectionForm.Designer.cs">
<DependentUpon>ColumnSelectionForm.cs</DependentUpon>
</Compile>
<Compile Include="Utilities\Generator.cs" />
<Compile Include="SubControls\GlassPanelForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Implementation\Groups.cs" />
<Compile Include="SubControls\HeaderControl.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="CellEditing\CellEditKeyEngine.cs" />
<Compile Include="Implementation\Munger.cs" />
<Compile Include="Implementation\NativeMethods.cs" />
<Compile Include="Implementation\Comparers.cs">
</Compile>
<Compile Include="ObjectListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="ObjectListView.DesignTime.cs" />
<Compile Include="Rendering\Overlays.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Rendering\Renderers.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Rendering\Styles.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="SubControls\ToolTipControl.cs" />
<Compile Include="TreeListView.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Utilities\OLVExporter.cs" />
<Compile Include="Utilities\TypedObjectListView.cs" />
<Compile Include="Implementation\VirtualGroups.cs" />
<Compile Include="Implementation\VirtualListDataSource.cs" />
<Compile Include="VirtualObjectListView.cs">
<SubType>Component</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="CustomDictionary.xml" />
<None Include="FullClassDiagram.cd" />
<None Include="ObjectListView2012.nuspec">
<SubType>Designer</SubType>
</None>
<None Include="Resources\sort-descending.png" />
<None Include="Resources\sort-ascending.png" />
<None Include="Resources\filter.png" />
<None Include="Resources\clear-filter.png" />
<None Include="Resources\filter-icons3.png" />
</ItemGroup>
<ItemGroup>
<None Include="olv-keyfile.snk" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="Utilities\ColumnSelectionForm.resx">
<DependentUpon>ColumnSelectionForm.cs</DependentUpon>
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@ -0,0 +1,22 @@
<ProjectConfiguration>
<CopyReferencedAssembliesToWorkspace>false</CopyReferencedAssembliesToWorkspace>
<ConsiderInconclusiveTestsAsPassing>false</ConsiderInconclusiveTestsAsPassing>
<PreloadReferencedAssemblies>false</PreloadReferencedAssemblies>
<AllowDynamicCodeContractChecking>true</AllowDynamicCodeContractChecking>
<AllowStaticCodeContractChecking>false</AllowStaticCodeContractChecking>
<IgnoreThisComponentCompletely>false</IgnoreThisComponentCompletely>
<RunPreBuildEvents>false</RunPreBuildEvents>
<RunPostBuildEvents>false</RunPostBuildEvents>
<PreviouslyBuiltSuccessfully>true</PreviouslyBuiltSuccessfully>
<InstrumentAssembly>true</InstrumentAssembly>
<PreventSigningOfAssembly>false</PreventSigningOfAssembly>
<AnalyseExecutionTimes>true</AnalyseExecutionTimes>
<IncludeStaticReferencesInWorkspace>true</IncludeStaticReferencesInWorkspace>
<DefaultTestTimeout>60000</DefaultTestTimeout>
<UseBuildConfiguration />
<UseBuildPlatform />
<ProxyProcessPath />
<UseCPUArchitecture>AutoDetect</UseCPUArchitecture>
<MSTestThreadApartmentState>STA</MSTestThreadApartmentState>
<BuildProcessArchitecture>x86</BuildProcessArchitecture>
</ProjectConfiguration>

19
ObjectListView2012.nuspec Normal file
View File

@ -0,0 +1,19 @@
<?xml version="1.0"?>
<package >
<metadata>
<id>ObjectListView.Official</id>
<title>ObjectListView (Official)</title>
<version>2.8.0</version>
<authors>Phillip Piper</authors>
<owners>Phillip Piper</owners>
<licenseUrl>http://www.gnu.org/licenses/gpl.html</licenseUrl>
<projectUrl>http://objectlistview.sourceforge.net</projectUrl>
<iconUrl>http://objectlistview.sourceforge.net/cs/_static/index-icon.png</iconUrl>
<requireLicenseAcceptance>true</requireLicenseAcceptance>
<summary>ObjectListView is a .NET ListView wired on caffeine, guarana and steroids.</summary>
<description>ObjectListView is a .NET ListView wired on caffeine, guarana and steroids. More calmly, it is a C# wrapper around a .NET ListView, which makes the ListView much easier to use and teaches it lots of neat new tricks.</description>
<releaseNotes>V2.8 added the ability to disable rows, and to have checkboxes in column headers</releaseNotes>
<copyright>Copyright 2006-2014 Bright Ideas Software</copyright>
<tags>.Net WinForms Net20 Net40 ListView Controls</tags>
</metadata>
</package>

View File

@ -0,0 +1,22 @@
<ProjectConfiguration>
<CopyReferencedAssembliesToWorkspace>false</CopyReferencedAssembliesToWorkspace>
<ConsiderInconclusiveTestsAsPassing>false</ConsiderInconclusiveTestsAsPassing>
<PreloadReferencedAssemblies>false</PreloadReferencedAssemblies>
<AllowDynamicCodeContractChecking>true</AllowDynamicCodeContractChecking>
<AllowStaticCodeContractChecking>false</AllowStaticCodeContractChecking>
<IgnoreThisComponentCompletely>false</IgnoreThisComponentCompletely>
<RunPreBuildEvents>false</RunPreBuildEvents>
<RunPostBuildEvents>false</RunPostBuildEvents>
<PreviouslyBuiltSuccessfully>true</PreviouslyBuiltSuccessfully>
<InstrumentAssembly>true</InstrumentAssembly>
<PreventSigningOfAssembly>false</PreventSigningOfAssembly>
<AnalyseExecutionTimes>true</AnalyseExecutionTimes>
<IncludeStaticReferencesInWorkspace>true</IncludeStaticReferencesInWorkspace>
<DefaultTestTimeout>60000</DefaultTestTimeout>
<UseBuildConfiguration />
<UseBuildPlatform />
<ProxyProcessPath />
<UseCPUArchitecture>AutoDetect</UseCPUArchitecture>
<MSTestThreadApartmentState>STA</MSTestThreadApartmentState>
<BuildProcessArchitecture>x86</BuildProcessArchitecture>
</ProjectConfiguration>

View File

@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ObjectListView")]
[assembly: AssemblyDescription("A much easier to use ListView and friends")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Bright Ideas Software")]
[assembly: AssemblyProduct("ObjectListView")]
[assembly: AssemblyCopyright("Copyright © 2006-2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("ef28c7a8-77ae-442d-abc3-bb023fa31e57")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("2.8.0.*")]
[assembly: AssemblyFileVersion("2.8.0.0")]
[assembly: System.CLSCompliant(true)]

98
Properties/Resources.Designer.cs generated Normal file
View File

@ -0,0 +1,98 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.1
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace BrightIdeasSoftware.Properties {
using System;
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources {
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources() {
}
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("BrightIdeasSoftware.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
internal static System.Drawing.Bitmap ClearFiltering {
get {
object obj = ResourceManager.GetObject("ClearFiltering", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
internal static System.Drawing.Bitmap ColumnFilterIndicator {
get {
object obj = ResourceManager.GetObject("ColumnFilterIndicator", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
internal static System.Drawing.Bitmap Filtering {
get {
object obj = ResourceManager.GetObject("Filtering", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
internal static System.Drawing.Bitmap SortAscending {
get {
object obj = ResourceManager.GetObject("SortAscending", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
internal static System.Drawing.Bitmap SortDescending {
get {
object obj = ResourceManager.GetObject("SortDescending", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
}
}

137
Properties/Resources.resx Normal file
View File

@ -0,0 +1,137 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="ClearFiltering" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\clear-filter.png;System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="ColumnFilterIndicator" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\filter-icons3.png;System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="Filtering" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\filter.png;System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="SortAscending" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\sort-ascending.png;System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="SortDescending" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\sort-descending.png;System.Drawing.Bitmap, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>

View File

@ -1,2 +1,4 @@
# ObjectListView
A mirror of the ObjectListView library
Original can be found [on the homepage](https://www.codeproject.com/kb/list/objectlistview.aspx).

743
Rendering/Adornments.cs Normal file
View File

@ -0,0 +1,743 @@
/*
* Adornments - Adornments are the basis for overlays and decorations -- things that can be rendered over the top of a ListView
*
* Author: Phillip Piper
* Date: 16/08/2009 1:02 AM
*
* Change log:
* v2.6
* 2012-08-18 JPP - Correctly dispose of brush and pen resources
* v2.3
* 2009-09-22 JPP - Added Wrap property to TextAdornment, to allow text wrapping to be disabled
* - Added ShrinkToWidth property to ImageAdornment
* 2009-08-17 JPP - Initial version
*
* To do:
* - Use IPointLocator rather than Corners
* - Add RotationCenter property ratherr than always using middle center
*
* Copyright (C) 2009-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace BrightIdeasSoftware
{
/// <summary>
/// An adorment is the common base for overlays and decorations.
/// </summary>
public class GraphicAdornment
{
#region Public properties
/// <summary>
/// Gets or sets the corner of the adornment that will be positioned at the reference corner
/// </summary>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public System.Drawing.ContentAlignment AdornmentCorner {
get { return this.adornmentCorner; }
set { this.adornmentCorner = value; }
}
private System.Drawing.ContentAlignment adornmentCorner = System.Drawing.ContentAlignment.MiddleCenter;
/// <summary>
/// Gets or sets location within the reference rectange where the adornment will be drawn
/// </summary>
/// <remarks>This is a simplied interface to ReferenceCorner and AdornmentCorner </remarks>
[Category("ObjectListView"),
Description("How will the adornment be aligned"),
DefaultValue(System.Drawing.ContentAlignment.BottomRight),
NotifyParentProperty(true)]
public System.Drawing.ContentAlignment Alignment {
get { return this.alignment; }
set {
this.alignment = value;
this.ReferenceCorner = value;
this.AdornmentCorner = value;
}
}
private System.Drawing.ContentAlignment alignment = System.Drawing.ContentAlignment.BottomRight;
/// <summary>
/// Gets or sets the offset by which the position of the adornment will be adjusted
/// </summary>
[Category("ObjectListView"),
Description("The offset by which the position of the adornment will be adjusted"),
DefaultValue(typeof(Size), "0,0")]
public Size Offset {
get { return this.offset; }
set { this.offset = value; }
}
private Size offset = new Size();
/// <summary>
/// Gets or sets the point of the reference rectangle to which the adornment will be aligned.
/// </summary>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public System.Drawing.ContentAlignment ReferenceCorner {
get { return this.referenceCorner; }
set { this.referenceCorner = value; }
}
private System.Drawing.ContentAlignment referenceCorner = System.Drawing.ContentAlignment.MiddleCenter;
/// <summary>
/// Gets or sets the degree of rotation by which the adornment will be transformed.
/// The centre of rotation will be the center point of the adornment.
/// </summary>
[Category("ObjectListView"),
Description("The degree of rotation that will be applied to the adornment."),
DefaultValue(0),
NotifyParentProperty(true)]
public int Rotation {
get { return this.rotation; }
set { this.rotation = value; }
}
private int rotation;
/// <summary>
/// Gets or sets the transparency of the overlay.
/// 0 is completely transparent, 255 is completely opaque.
/// </summary>
[Category("ObjectListView"),
Description("The transparency of this adornment. 0 is completely transparent, 255 is completely opaque."),
DefaultValue(128)]
public int Transparency {
get { return this.transparency; }
set { this.transparency = Math.Min(255, Math.Max(0, value)); }
}
private int transparency = 128;
#endregion
#region Calculations
/// <summary>
/// Calculate the location of rectangle of the given size,
/// so that it's indicated corner would be at the given point.
/// </summary>
/// <param name="pt">The point</param>
/// <param name="size"></param>
/// <param name="corner">Which corner will be positioned at the reference point</param>
/// <returns></returns>
/// <example>CalculateAlignedPosition(new Point(50, 100), new Size(10, 20), System.Drawing.ContentAlignment.TopLeft) -> Point(50, 100)</example>
/// <example>CalculateAlignedPosition(new Point(50, 100), new Size(10, 20), System.Drawing.ContentAlignment.MiddleCenter) -> Point(45, 90)</example>
/// <example>CalculateAlignedPosition(new Point(50, 100), new Size(10, 20), System.Drawing.ContentAlignment.BottomRight) -> Point(40, 80)</example>
public virtual Point CalculateAlignedPosition(Point pt, Size size, System.Drawing.ContentAlignment corner) {
switch (corner) {
case System.Drawing.ContentAlignment.TopLeft:
return pt;
case System.Drawing.ContentAlignment.TopCenter:
return new Point(pt.X - (size.Width / 2), pt.Y);
case System.Drawing.ContentAlignment.TopRight:
return new Point(pt.X - size.Width, pt.Y);
case System.Drawing.ContentAlignment.MiddleLeft:
return new Point(pt.X, pt.Y - (size.Height / 2));
case System.Drawing.ContentAlignment.MiddleCenter:
return new Point(pt.X - (size.Width / 2), pt.Y - (size.Height / 2));
case System.Drawing.ContentAlignment.MiddleRight:
return new Point(pt.X - size.Width, pt.Y - (size.Height / 2));
case System.Drawing.ContentAlignment.BottomLeft:
return new Point(pt.X, pt.Y - size.Height);
case System.Drawing.ContentAlignment.BottomCenter:
return new Point(pt.X - (size.Width / 2), pt.Y - size.Height);
case System.Drawing.ContentAlignment.BottomRight:
return new Point(pt.X - size.Width, pt.Y - size.Height);
}
// Should never reach here
return pt;
}
/// <summary>
/// Calculate a rectangle that has the given size which is positioned so that
/// its alignment point is at the reference location of the given rect.
/// </summary>
/// <param name="r"></param>
/// <param name="sz"></param>
/// <returns></returns>
public virtual Rectangle CreateAlignedRectangle(Rectangle r, Size sz) {
return this.CreateAlignedRectangle(r, sz, this.ReferenceCorner, this.AdornmentCorner, this.Offset);
}
/// <summary>
/// Create a rectangle of the given size which is positioned so that
/// its indicated corner is at the indicated corner of the reference rect.
/// </summary>
/// <param name="r"></param>
/// <param name="sz"></param>
/// <param name="corner"></param>
/// <param name="referenceCorner"></param>
/// <param name="offset"></param>
/// <returns></returns>
/// <remarks>
/// <para>Creates a rectangle so that its bottom left is at the centre of the reference:
/// corner=BottomLeft, referenceCorner=MiddleCenter</para>
/// <para>This is a powerful concept that takes some getting used to, but is
/// very neat once you understand it.</para>
/// </remarks>
public virtual Rectangle CreateAlignedRectangle(Rectangle r, Size sz,
System.Drawing.ContentAlignment corner, System.Drawing.ContentAlignment referenceCorner, Size offset) {
Point referencePt = this.CalculateCorner(r, referenceCorner);
Point topLeft = this.CalculateAlignedPosition(referencePt, sz, corner);
return new Rectangle(topLeft + offset, sz);
}
/// <summary>
/// Return the point at the indicated corner of the given rectangle (it doesn't
/// have to be a corner, but a named location)
/// </summary>
/// <param name="r">The reference rectangle</param>
/// <param name="corner">Which point of the rectangle should be returned?</param>
/// <returns>A point</returns>
/// <example>CalculateReferenceLocation(new Rectangle(0, 0, 50, 100), System.Drawing.ContentAlignment.TopLeft) -> Point(0, 0)</example>
/// <example>CalculateReferenceLocation(new Rectangle(0, 0, 50, 100), System.Drawing.ContentAlignment.MiddleCenter) -> Point(25, 50)</example>
/// <example>CalculateReferenceLocation(new Rectangle(0, 0, 50, 100), System.Drawing.ContentAlignment.BottomRight) -> Point(50, 100)</example>
public virtual Point CalculateCorner(Rectangle r, System.Drawing.ContentAlignment corner) {
switch (corner) {
case System.Drawing.ContentAlignment.TopLeft:
return new Point(r.Left, r.Top);
case System.Drawing.ContentAlignment.TopCenter:
return new Point(r.X + (r.Width / 2), r.Top);
case System.Drawing.ContentAlignment.TopRight:
return new Point(r.Right, r.Top);
case System.Drawing.ContentAlignment.MiddleLeft:
return new Point(r.Left, r.Top + (r.Height / 2));
case System.Drawing.ContentAlignment.MiddleCenter:
return new Point(r.X + (r.Width / 2), r.Top + (r.Height / 2));
case System.Drawing.ContentAlignment.MiddleRight:
return new Point(r.Right, r.Top + (r.Height / 2));
case System.Drawing.ContentAlignment.BottomLeft:
return new Point(r.Left, r.Bottom);
case System.Drawing.ContentAlignment.BottomCenter:
return new Point(r.X + (r.Width / 2), r.Bottom);
case System.Drawing.ContentAlignment.BottomRight:
return new Point(r.Right, r.Bottom);
}
// Should never reach here
return r.Location;
}
/// <summary>
/// Given the item and the subitem, calculate its bounds.
/// </summary>
/// <param name="item"></param>
/// <param name="subItem"></param>
/// <returns></returns>
public virtual Rectangle CalculateItemBounds(OLVListItem item, OLVListSubItem subItem) {
if (item == null)
return Rectangle.Empty;
if (subItem == null)
return item.Bounds;
return item.GetSubItemBounds(item.SubItems.IndexOf(subItem));
}
#endregion
#region Commands
/// <summary>
/// Apply any specified rotation to the Graphic content.
/// </summary>
/// <param name="g">The Graphics to be transformed</param>
/// <param name="r">The rotation will be around the centre of this rect</param>
protected virtual void ApplyRotation(Graphics g, Rectangle r) {
if (this.Rotation == 0)
return;
// THINK: Do we want to reset the transform? I think we want to push a new transform
g.ResetTransform();
Matrix m = new Matrix();
m.RotateAt(this.Rotation, new Point(r.Left + r.Width / 2, r.Top + r.Height / 2));
g.Transform = m;
}
/// <summary>
/// Reverse the rotation created by ApplyRotation()
/// </summary>
/// <param name="g"></param>
protected virtual void UnapplyRotation(Graphics g) {
if (this.Rotation != 0)
g.ResetTransform();
}
#endregion
}
/// <summary>
/// An overlay that will draw an image over the top of the ObjectListView
/// </summary>
public class ImageAdornment : GraphicAdornment
{
#region Public properties
/// <summary>
/// Gets or sets the image that will be drawn
/// </summary>
[Category("ObjectListView"),
Description("The image that will be drawn"),
DefaultValue(null),
NotifyParentProperty(true)]
public Image Image {
get { return this.image; }
set { this.image = value; }
}
private Image image;
/// <summary>
/// Gets or sets if the image will be shrunk to fit with its horizontal bounds
/// </summary>
[Category("ObjectListView"),
Description("Will the image be shrunk to fit within its width?"),
DefaultValue(false)]
public bool ShrinkToWidth {
get { return this.shrinkToWidth; }
set { this.shrinkToWidth = value; }
}
private bool shrinkToWidth;
#endregion
#region Commands
/// <summary>
/// Draw the image in its specified location
/// </summary>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The bounds of the rendering</param>
public virtual void DrawImage(Graphics g, Rectangle r) {
if (this.ShrinkToWidth)
this.DrawScaledImage(g, r, this.Image, this.Transparency);
else
this.DrawImage(g, r, this.Image, this.Transparency);
}
/// <summary>
/// Draw the image in its specified location
/// </summary>
/// <param name="image">The image to be drawn</param>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The bounds of the rendering</param>
/// <param name="transparency">How transparent should the image be (0 is completely transparent, 255 is opaque)</param>
public virtual void DrawImage(Graphics g, Rectangle r, Image image, int transparency) {
if (image != null)
this.DrawImage(g, r, image, image.Size, transparency);
}
/// <summary>
/// Draw the image in its specified location
/// </summary>
/// <param name="image">The image to be drawn</param>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The bounds of the rendering</param>
/// <param name="sz">How big should the image be?</param>
/// <param name="transparency">How transparent should the image be (0 is completely transparent, 255 is opaque)</param>
public virtual void DrawImage(Graphics g, Rectangle r, Image image, Size sz, int transparency) {
if (image == null)
return;
Rectangle adornmentBounds = this.CreateAlignedRectangle(r, sz);
try {
this.ApplyRotation(g, adornmentBounds);
this.DrawTransparentBitmap(g, adornmentBounds, image, transparency);
}
finally {
this.UnapplyRotation(g);
}
}
/// <summary>
/// Draw the image in its specified location, scaled so that it is not wider
/// than the given rectangle. Height is scaled proportional to the width.
/// </summary>
/// <param name="image">The image to be drawn</param>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The bounds of the rendering</param>
/// <param name="transparency">How transparent should the image be (0 is completely transparent, 255 is opaque)</param>
public virtual void DrawScaledImage(Graphics g, Rectangle r, Image image, int transparency) {
if (image == null)
return;
// If the image is too wide to be drawn in the space provided, proportionally scale it down.
// Too tall images are not scaled.
Size size = image.Size;
if (image.Width > r.Width) {
float scaleRatio = (float)r.Width / (float)image.Width;
size.Height = (int)((float)image.Height * scaleRatio);
size.Width = r.Width - 1;
}
this.DrawImage(g, r, image, size, transparency);
}
/// <summary>
/// Utility to draw a bitmap transparenly.
/// </summary>
/// <param name="g"></param>
/// <param name="r"></param>
/// <param name="image"></param>
/// <param name="transparency"></param>
protected virtual void DrawTransparentBitmap(Graphics g, Rectangle r, Image image, int transparency) {
ImageAttributes imageAttributes = null;
if (transparency != 255) {
imageAttributes = new ImageAttributes();
float a = (float)transparency / 255.0f;
float[][] colorMatrixElements = {
new float[] {1, 0, 0, 0, 0},
new float[] {0, 1, 0, 0, 0},
new float[] {0, 0, 1, 0, 0},
new float[] {0, 0, 0, a, 0},
new float[] {0, 0, 0, 0, 1}};
imageAttributes.SetColorMatrix(new ColorMatrix(colorMatrixElements));
}
g.DrawImage(image,
r, // destination rectangle
0, 0, image.Size.Width, image.Size.Height, // source rectangle
GraphicsUnit.Pixel,
imageAttributes);
}
#endregion
}
/// <summary>
/// An adornment that will draw text
/// </summary>
public class TextAdornment : GraphicAdornment
{
#region Public properties
/// <summary>
/// Gets or sets the background color of the text
/// Set this to Color.Empty to not draw a background
/// </summary>
[Category("ObjectListView"),
Description("The background color of the text"),
DefaultValue(typeof(Color), "")]
public Color BackColor {
get { return this.backColor; }
set { this.backColor = value; }
}
private Color backColor = Color.Empty;
/// <summary>
/// Gets the brush that will be used to paint the text
/// </summary>
[Browsable(false)]
public Brush BackgroundBrush {
get {
return new SolidBrush(Color.FromArgb(this.workingTransparency, this.BackColor));
}
}
/// <summary>
/// Gets or sets the color of the border around the billboard.
/// Set this to Color.Empty to remove the border
/// </summary>
[Category("ObjectListView"),
Description("The color of the border around the text"),
DefaultValue(typeof(Color), "")]
public Color BorderColor {
get { return this.borderColor; }
set { this.borderColor = value; }
}
private Color borderColor = Color.Empty;
/// <summary>
/// Gets the brush that will be used to paint the text
/// </summary>
[Browsable(false)]
public Pen BorderPen {
get {
return new Pen(Color.FromArgb(this.workingTransparency, this.BorderColor), this.BorderWidth);
}
}
/// <summary>
/// Gets or sets the width of the border around the text
/// </summary>
[Category("ObjectListView"),
Description("The width of the border around the text"),
DefaultValue(0.0f)]
public float BorderWidth {
get { return this.borderWidth; }
set { this.borderWidth = value; }
}
private float borderWidth;
/// <summary>
/// How rounded should the corners of the border be? 0 means no rounding.
/// </summary>
/// <remarks>If this value is too large, the edges of the border will appear odd.</remarks>
[Category("ObjectListView"),
Description("How rounded should the corners of the border be? 0 means no rounding."),
DefaultValue(16.0f),
NotifyParentProperty(true)]
public float CornerRounding {
get { return this.cornerRounding; }
set { this.cornerRounding = value; }
}
private float cornerRounding = 16.0f;
/// <summary>
/// Gets or sets the font that will be used to draw the text
/// </summary>
[Category("ObjectListView"),
Description("The font that will be used to draw the text"),
DefaultValue(null),
NotifyParentProperty(true)]
public Font Font {
get { return this.font; }
set { this.font = value; }
}
private Font font;
/// <summary>
/// Gets the font that will be used to draw the text or a reasonable default
/// </summary>
[Browsable(false)]
public Font FontOrDefault {
get {
return this.Font ?? new Font("Tahoma", 16);
}
}
/// <summary>
/// Does this text have a background?
/// </summary>
[Browsable(false)]
public bool HasBackground {
get {
return this.BackColor != Color.Empty;
}
}
/// <summary>
/// Does this overlay have a border?
/// </summary>
[Browsable(false)]
public bool HasBorder {
get {
return this.BorderColor != Color.Empty && this.BorderWidth > 0;
}
}
/// <summary>
/// Gets or sets the maximum width of the text. Text longer than this will wrap.
/// 0 means no maximum.
/// </summary>
[Category("ObjectListView"),
Description("The maximum width the text (0 means no maximum). Text longer than this will wrap"),
DefaultValue(0)]
public int MaximumTextWidth {
get { return this.maximumTextWidth; }
set { this.maximumTextWidth = value; }
}
private int maximumTextWidth;
/// <summary>
/// Gets or sets the formatting that should be used on the text
/// </summary>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public virtual StringFormat StringFormat {
get {
if (this.stringFormat == null) {
this.stringFormat = new StringFormat();
this.stringFormat.Alignment = StringAlignment.Center;
this.stringFormat.LineAlignment = StringAlignment.Center;
this.stringFormat.Trimming = StringTrimming.EllipsisCharacter;
if (!this.Wrap)
this.stringFormat.FormatFlags = StringFormatFlags.NoWrap;
}
return this.stringFormat;
}
set { this.stringFormat = value; }
}
private StringFormat stringFormat;
/// <summary>
/// Gets or sets the text that will be drawn
/// </summary>
[Category("ObjectListView"),
Description("The text that will be drawn over the top of the ListView"),
DefaultValue(null),
NotifyParentProperty(true),
Localizable(true)]
public string Text {
get { return this.text; }
set { this.text = value; }
}
private string text;
/// <summary>
/// Gets the brush that will be used to paint the text
/// </summary>
[Browsable(false)]
public Brush TextBrush {
get {
return new SolidBrush(Color.FromArgb(this.workingTransparency, this.TextColor));
}
}
/// <summary>
/// Gets or sets the color of the text
/// </summary>
[Category("ObjectListView"),
Description("The color of the text"),
DefaultValue(typeof(Color), "DarkBlue"),
NotifyParentProperty(true)]
public Color TextColor {
get { return this.textColor; }
set { this.textColor = value; }
}
private Color textColor = Color.DarkBlue;
/// <summary>
/// Gets or sets whether the text will wrap when it exceeds its bounds
/// </summary>
[Category("ObjectListView"),
Description("Will the text wrap?"),
DefaultValue(true)]
public bool Wrap {
get { return this.wrap; }
set { this.wrap = value; }
}
private bool wrap = true;
#endregion
#region Implementation
/// <summary>
/// Draw our text with our stored configuration in relation to the given
/// reference rectangle
/// </summary>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The reference rectangle in relation to which the text will be drawn</param>
public virtual void DrawText(Graphics g, Rectangle r) {
this.DrawText(g, r, this.Text, this.Transparency);
}
/// <summary>
/// Draw the given text with our stored configuration
/// </summary>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The reference rectangle in relation to which the text will be drawn</param>
/// <param name="s">The text to draw</param>
/// <param name="transparency">How opaque should be text be</param>
public virtual void DrawText(Graphics g, Rectangle r, string s, int transparency) {
if (String.IsNullOrEmpty(s))
return;
Rectangle textRect = this.CalculateTextBounds(g, r, s);
this.DrawBorderedText(g, textRect, s, transparency);
}
/// <summary>
/// Draw the text with a border
/// </summary>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="textRect">The bounds within which the text should be drawn</param>
/// <param name="text">The text to draw</param>
/// <param name="transparency">How opaque should be text be</param>
protected virtual void DrawBorderedText(Graphics g, Rectangle textRect, string text, int transparency) {
Rectangle borderRect = textRect;
borderRect.Inflate((int)this.BorderWidth / 2, (int)this.BorderWidth / 2);
borderRect.Y -= 1; // Looker better a little higher
try {
this.ApplyRotation(g, textRect);
using (GraphicsPath path = this.GetRoundedRect(borderRect, this.CornerRounding)) {
this.workingTransparency = transparency;
if (this.HasBackground) {
using (Brush b = this.BackgroundBrush)
g.FillPath(b, path);
}
using (Brush b = this.TextBrush)
g.DrawString(text, this.FontOrDefault, b, textRect, this.StringFormat);
if (this.HasBorder) {
using (Pen p = this.BorderPen)
g.DrawPath(p, path);
}
}
}
finally {
this.UnapplyRotation(g);
}
}
/// <summary>
/// Return the rectangle that will be the precise bounds of the displayed text
/// </summary>
/// <param name="g"></param>
/// <param name="r"></param>
/// <param name="s"></param>
/// <returns>The bounds of the text</returns>
protected virtual Rectangle CalculateTextBounds(Graphics g, Rectangle r, string s) {
int maxWidth = this.MaximumTextWidth <= 0 ? r.Width : this.MaximumTextWidth;
SizeF sizeF = g.MeasureString(s, this.FontOrDefault, maxWidth, this.StringFormat);
Size size = new Size(1 + (int)sizeF.Width, 1 + (int)sizeF.Height);
return this.CreateAlignedRectangle(r, size);
}
/// <summary>
/// Return a GraphicPath that is a round cornered rectangle
/// </summary>
/// <param name="rect">The rectangle</param>
/// <param name="diameter">The diameter of the corners</param>
/// <returns>A round cornered rectagle path</returns>
/// <remarks>If I could rely on people using C# 3.0+, this should be
/// an extension method of GraphicsPath.</remarks>
protected virtual GraphicsPath GetRoundedRect(Rectangle rect, float diameter) {
GraphicsPath path = new GraphicsPath();
if (diameter > 0) {
RectangleF arc = new RectangleF(rect.X, rect.Y, diameter, diameter);
path.AddArc(arc, 180, 90);
arc.X = rect.Right - diameter;
path.AddArc(arc, 270, 90);
arc.Y = rect.Bottom - diameter;
path.AddArc(arc, 0, 90);
arc.X = rect.Left;
path.AddArc(arc, 90, 90);
path.CloseFigure();
} else {
path.AddRectangle(rect);
}
return path;
}
#endregion
private int workingTransparency;
}
}

820
Rendering/Decorations.cs Normal file
View File

@ -0,0 +1,820 @@
/*
* Decorations - Images, text or other things that can be rendered onto an ObjectListView
*
* Author: Phillip Piper
* Date: 19/08/2009 10:56 PM
*
* Change log:
* 2011-04-04 JPP - Added ability to have a gradient background on BorderDecoration
* v2.4
* 2010-04-15 JPP - Tweaked LightBoxDecoration a little
* v2.3
* 2009-09-23 JPP - Added LeftColumn and RightColumn to RowBorderDecoration
* 2009-08-23 JPP - Added LightBoxDecoration
* 2009-08-19 JPP - Initial version. Separated from Overlays.cs
*
* To do:
*
* Copyright (C) 2009-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace BrightIdeasSoftware
{
/// <summary>
/// A decoration is an overlay that draws itself in relation to a given row or cell.
/// Decorations scroll when the listview scrolls.
/// </summary>
public interface IDecoration : IOverlay
{
/// <summary>
/// Gets or sets the row that is to be decorated
/// </summary>
OLVListItem ListItem { get; set; }
/// <summary>
/// Gets or sets the subitem that is to be decorated
/// </summary>
OLVListSubItem SubItem { get; set; }
}
/// <summary>
/// An AbstractDecoration is a safe do-nothing implementation of the IDecoration interface
/// </summary>
public class AbstractDecoration : IDecoration
{
#region IDecoration Members
/// <summary>
/// Gets or sets the row that is to be decorated
/// </summary>
public OLVListItem ListItem {
get { return listItem; }
set { listItem = value; }
}
private OLVListItem listItem;
/// <summary>
/// Gets or sets the subitem that is to be decorated
/// </summary>
public OLVListSubItem SubItem {
get { return subItem; }
set { subItem = value; }
}
private OLVListSubItem subItem;
#endregion
#region Public properties
/// <summary>
/// Gets the bounds of the decorations row
/// </summary>
public Rectangle RowBounds {
get {
if (this.ListItem == null)
return Rectangle.Empty;
else
return this.ListItem.Bounds;
}
}
/// <summary>
/// Get the bounds of the decorations cell
/// </summary>
public Rectangle CellBounds {
get {
if (this.ListItem == null || this.SubItem == null)
return Rectangle.Empty;
else
return this.ListItem.GetSubItemBounds(this.ListItem.SubItems.IndexOf(this.SubItem));
}
}
#endregion
#region IOverlay Members
/// <summary>
/// Draw the decoration
/// </summary>
/// <param name="olv"></param>
/// <param name="g"></param>
/// <param name="r"></param>
public virtual void Draw(ObjectListView olv, Graphics g, Rectangle r) {
}
#endregion
}
/// <summary>
/// This decoration draws a slight tint over a column of the
/// owning listview. If no column is explicitly set, the selected
/// column in the listview will be used.
/// The selected column is normally the sort column, but does not have to be.
/// </summary>
public class TintedColumnDecoration : AbstractDecoration
{
#region Constructors
/// <summary>
/// Create a TintedColumnDecoration
/// </summary>
public TintedColumnDecoration() {
this.Tint = Color.FromArgb(15, Color.Blue);
}
/// <summary>
/// Create a TintedColumnDecoration
/// </summary>
/// <param name="column"></param>
public TintedColumnDecoration(OLVColumn column)
: this() {
this.ColumnToTint = column;
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the column that will be tinted
/// </summary>
public OLVColumn ColumnToTint {
get { return this.columnToTint; }
set { this.columnToTint = value; }
}
private OLVColumn columnToTint;
/// <summary>
/// Gets or sets the color that will be 'tinted' over the selected column
/// </summary>
public Color Tint {
get { return this.tint; }
set {
if (this.tint == value)
return;
if (this.tintBrush != null) {
this.tintBrush.Dispose();
this.tintBrush = null;
}
this.tint = value;
this.tintBrush = new SolidBrush(this.tint);
}
}
private Color tint;
private SolidBrush tintBrush;
#endregion
#region IOverlay Members
/// <summary>
/// Draw a slight colouring over our tinted column
/// </summary>
/// <remarks>
/// This overlay only works when:
/// - the list is in Details view
/// - there is at least one row
/// - there is a selected column (or a specified tint column)
/// </remarks>
/// <param name="olv"></param>
/// <param name="g"></param>
/// <param name="r"></param>
public override void Draw(ObjectListView olv, Graphics g, Rectangle r) {
if (olv.View != System.Windows.Forms.View.Details)
return;
if (olv.GetItemCount() == 0)
return;
OLVColumn column = this.ColumnToTint ?? olv.SelectedColumn;
if (column == null)
return;
Point sides = NativeMethods.GetScrolledColumnSides(olv, column.Index);
if (sides.X == -1)
return;
Rectangle columnBounds = new Rectangle(sides.X, r.Top, sides.Y - sides.X, r.Bottom);
// Find the bottom of the last item. The tinting should extend only to there.
OLVListItem lastItem = olv.GetLastItemInDisplayOrder();
if (lastItem != null) {
Rectangle lastItemBounds = lastItem.Bounds;
if (!lastItemBounds.IsEmpty && lastItemBounds.Bottom < columnBounds.Bottom)
columnBounds.Height = lastItemBounds.Bottom - columnBounds.Top;
}
g.FillRectangle(this.tintBrush, columnBounds);
}
#endregion
}
/// <summary>
/// This decoration draws an optionally filled border around a rectangle.
/// Subclasses must override CalculateBounds().
/// </summary>
public class BorderDecoration : AbstractDecoration
{
#region Constructors
/// <summary>
/// Create a BorderDecoration
/// </summary>
public BorderDecoration()
: this(new Pen(Color.FromArgb(64, Color.Blue), 1)) {
}
/// <summary>
/// Create a BorderDecoration
/// </summary>
/// <param name="borderPen">The pen used to draw the border</param>
public BorderDecoration(Pen borderPen) {
this.BorderPen = borderPen;
}
/// <summary>
/// Create a BorderDecoration
/// </summary>
/// <param name="borderPen">The pen used to draw the border</param>
/// <param name="fill">The brush used to fill the rectangle</param>
public BorderDecoration(Pen borderPen, Brush fill) {
this.BorderPen = borderPen;
this.FillBrush = fill;
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the pen that will be used to draw the border
/// </summary>
public Pen BorderPen {
get { return this.borderPen; }
set { this.borderPen = value; }
}
private Pen borderPen;
/// <summary>
/// Gets or sets the padding that will be added to the bounds of the item
/// before drawing the border and fill.
/// </summary>
public Size BoundsPadding {
get { return this.boundsPadding; }
set { this.boundsPadding = value; }
}
private Size boundsPadding = new Size(-1, 2);
/// <summary>
/// How rounded should the corners of the border be? 0 means no rounding.
/// </summary>
/// <remarks>If this value is too large, the edges of the border will appear odd.</remarks>
public float CornerRounding {
get { return this.cornerRounding; }
set { this.cornerRounding = value; }
}
private float cornerRounding = 16.0f;
/// <summary>
/// Gets or sets the brush that will be used to fill the border
/// </summary>
/// <remarks>This value is ignored when using gradient brush</remarks>
public Brush FillBrush {
get { return this.fillBrush; }
set { this.fillBrush = value; }
}
private Brush fillBrush = new SolidBrush(Color.FromArgb(64, Color.Blue));
/// <summary>
/// Gets or sets the color that will be used as the start of a gradient fill.
/// </summary>
/// <remarks>This and FillGradientTo must be given value to show a gradient</remarks>
public Color? FillGradientFrom {
get { return this.fillGradientFrom; }
set { this.fillGradientFrom = value; }
}
private Color? fillGradientFrom;
/// <summary>
/// Gets or sets the color that will be used as the end of a gradient fill.
/// </summary>
/// <remarks>This and FillGradientFrom must be given value to show a gradient</remarks>
public Color? FillGradientTo {
get { return this.fillGradientTo; }
set { this.fillGradientTo = value; }
}
private Color? fillGradientTo;
/// <summary>
/// Gets or sets the fill mode that will be used for the gradient.
/// </summary>
public LinearGradientMode FillGradientMode {
get { return this.fillGradientMode; }
set { this.fillGradientMode = value; }
}
private LinearGradientMode fillGradientMode = LinearGradientMode.Vertical;
#endregion
#region IOverlay Members
/// <summary>
/// Draw a filled border
/// </summary>
/// <param name="olv"></param>
/// <param name="g"></param>
/// <param name="r"></param>
public override void Draw(ObjectListView olv, Graphics g, Rectangle r) {
Rectangle bounds = this.CalculateBounds();
if (!bounds.IsEmpty)
this.DrawFilledBorder(g, bounds);
}
#endregion
#region Subclass responsibility
/// <summary>
/// Subclasses should override this to say where the border should be drawn
/// </summary>
/// <returns></returns>
protected virtual Rectangle CalculateBounds() {
return Rectangle.Empty;
}
#endregion
#region Implementation utlities
/// <summary>
/// Do the actual work of drawing the filled border
/// </summary>
/// <param name="g"></param>
/// <param name="bounds"></param>
protected void DrawFilledBorder(Graphics g, Rectangle bounds) {
bounds.Inflate(this.BoundsPadding);
GraphicsPath path = this.GetRoundedRect(bounds, this.CornerRounding);
if (this.FillGradientFrom != null && this.FillGradientTo != null) {
if (this.FillBrush != null)
this.FillBrush.Dispose();
this.FillBrush = new LinearGradientBrush(bounds, this.FillGradientFrom.Value, this.FillGradientTo.Value, this.FillGradientMode);
}
if (this.FillBrush != null)
g.FillPath(this.FillBrush, path);
if (this.BorderPen != null)
g.DrawPath(this.BorderPen, path);
}
/// <summary>
/// Create a GraphicsPath that represents a round cornered rectangle.
/// </summary>
/// <param name="rect"></param>
/// <param name="diameter">If this is 0 or less, the rectangle will not be rounded.</param>
/// <returns></returns>
protected GraphicsPath GetRoundedRect(RectangleF rect, float diameter) {
GraphicsPath path = new GraphicsPath();
if (diameter <= 0.0f) {
path.AddRectangle(rect);
} else {
RectangleF arc = new RectangleF(rect.X, rect.Y, diameter, diameter);
path.AddArc(arc, 180, 90);
arc.X = rect.Right - diameter;
path.AddArc(arc, 270, 90);
arc.Y = rect.Bottom - diameter;
path.AddArc(arc, 0, 90);
arc.X = rect.Left;
path.AddArc(arc, 90, 90);
path.CloseFigure();
}
return path;
}
#endregion
}
/// <summary>
/// Instances of this class draw a border around the decorated row
/// </summary>
public class RowBorderDecoration : BorderDecoration
{
/// <summary>
/// Gets or sets the index of the left most column to be used for the border
/// </summary>
public int LeftColumn {
get { return leftColumn; }
set { leftColumn = value; }
}
private int leftColumn = -1;
/// <summary>
/// Gets or sets the index of the right most column to be used for the border
/// </summary>
public int RightColumn {
get { return rightColumn; }
set { rightColumn = value; }
}
private int rightColumn = -1;
/// <summary>
/// Calculate the boundaries of the border
/// </summary>
/// <returns></returns>
protected override Rectangle CalculateBounds() {
Rectangle bounds = this.RowBounds;
if (this.ListItem == null)
return bounds;
if (this.LeftColumn >= 0) {
Rectangle leftCellBounds = this.ListItem.GetSubItemBounds(this.LeftColumn);
if (!leftCellBounds.IsEmpty) {
bounds.Width = bounds.Right - leftCellBounds.Left;
bounds.X = leftCellBounds.Left;
}
}
if (this.RightColumn >= 0) {
Rectangle rightCellBounds = this.ListItem.GetSubItemBounds(this.RightColumn);
if (!rightCellBounds.IsEmpty) {
bounds.Width = rightCellBounds.Right - bounds.Left;
}
}
return bounds;
}
}
/// <summary>
/// Instances of this class draw a border around the decorated subitem.
/// </summary>
public class CellBorderDecoration : BorderDecoration
{
/// <summary>
/// Calculate the boundaries of the border
/// </summary>
/// <returns></returns>
protected override Rectangle CalculateBounds() {
return this.CellBounds;
}
}
/// <summary>
/// This decoration puts a border around the cell being edited and
/// optionally "lightboxes" the cell (makes the rest of the control dark).
/// </summary>
public class EditingCellBorderDecoration : BorderDecoration
{
#region Life and death
/// <summary>
/// Create a EditingCellBorderDecoration
/// </summary>
public EditingCellBorderDecoration() {
this.FillBrush = null;
this.BorderPen = new Pen(Color.DarkBlue, 2);
this.CornerRounding = 8;
this.BoundsPadding = new Size(10, 8);
}
/// <summary>
/// Create a EditingCellBorderDecoration
/// </summary>
/// <param name="useLightBox">Should the decoration use a lighbox display style?</param>
public EditingCellBorderDecoration(bool useLightBox) : this()
{
this.UseLightbox = useLightbox;
}
#endregion
#region Configuration properties
/// <summary>
/// Gets or set whether the decoration should make the rest of
/// the control dark when a cell is being edited
/// </summary>
/// <remarks>If this is true, FillBrush is used to overpaint
/// the control.</remarks>
public bool UseLightbox {
get { return this.useLightbox; }
set {
if (this.useLightbox == value)
return;
this.useLightbox = value;
if (this.useLightbox) {
if (this.FillBrush == null)
this.FillBrush = new SolidBrush(Color.FromArgb(64, Color.Black));
}
}
}
private bool useLightbox;
#endregion
#region Implementation
/// <summary>
/// Draw the decoration
/// </summary>
/// <param name="olv"></param>
/// <param name="g"></param>
/// <param name="r"></param>
public override void Draw(ObjectListView olv, Graphics g, Rectangle r) {
if (!olv.IsCellEditing)
return;
Rectangle bounds = olv.CellEditor.Bounds;
if (bounds.IsEmpty)
return;
bounds.Inflate(this.BoundsPadding);
GraphicsPath path = this.GetRoundedRect(bounds, this.CornerRounding);
if (this.FillBrush != null) {
if (this.UseLightbox) {
using (Region newClip = new Region(r)) {
newClip.Exclude(path);
Region originalClip = g.Clip;
g.Clip = newClip;
g.FillRectangle(this.FillBrush, r);
g.Clip = originalClip;
}
} else {
g.FillPath(this.FillBrush, path);
}
}
if (this.BorderPen != null)
g.DrawPath(this.BorderPen, path);
}
#endregion
}
/// <summary>
/// This decoration causes everything *except* the row under the mouse to be overpainted
/// with a tint, making the row under the mouse stand out in comparison.
/// The darker and more opaque the fill color, the more obvious the
/// decorated row becomes.
/// </summary>
public class LightBoxDecoration : BorderDecoration
{
/// <summary>
/// Create a LightBoxDecoration
/// </summary>
public LightBoxDecoration() {
this.BoundsPadding = new Size(-1, 4);
this.CornerRounding = 8.0f;
this.FillBrush = new SolidBrush(Color.FromArgb(72, Color.Black));
}
/// <summary>
/// Draw a tint over everything in the ObjectListView except the
/// row under the mouse.
/// </summary>
/// <param name="olv"></param>
/// <param name="g"></param>
/// <param name="r"></param>
public override void Draw(ObjectListView olv, Graphics g, Rectangle r) {
if (!r.Contains(olv.PointToClient(Cursor.Position)))
return;
Rectangle bounds = this.RowBounds;
if (bounds.IsEmpty) {
if (olv.View == View.Tile)
g.FillRectangle(this.FillBrush, r);
return;
}
using (Region newClip = new Region(r)) {
bounds.Inflate(this.BoundsPadding);
newClip.Exclude(this.GetRoundedRect(bounds, this.CornerRounding));
Region originalClip = g.Clip;
g.Clip = newClip;
g.FillRectangle(this.FillBrush, r);
g.Clip = originalClip;
}
}
}
/// <summary>
/// Instances of this class put an Image over the row/cell that it is decorating
/// </summary>
public class ImageDecoration : ImageAdornment, IDecoration
{
#region Constructors
/// <summary>
/// Create an image decoration
/// </summary>
public ImageDecoration() {
this.Alignment = ContentAlignment.MiddleRight;
}
/// <summary>
/// Create an image decoration
/// </summary>
/// <param name="image"></param>
public ImageDecoration(Image image)
: this() {
this.Image = image;
}
/// <summary>
/// Create an image decoration
/// </summary>
/// <param name="image"></param>
/// <param name="transparency"></param>
public ImageDecoration(Image image, int transparency)
: this() {
this.Image = image;
this.Transparency = transparency;
}
/// <summary>
/// Create an image decoration
/// </summary>
/// <param name="image"></param>
/// <param name="alignment"></param>
public ImageDecoration(Image image, ContentAlignment alignment)
: this() {
this.Image = image;
this.Alignment = alignment;
}
/// <summary>
/// Create an image decoration
/// </summary>
/// <param name="image"></param>
/// <param name="transparency"></param>
/// <param name="alignment"></param>
public ImageDecoration(Image image, int transparency, ContentAlignment alignment)
: this() {
this.Image = image;
this.Transparency = transparency;
this.Alignment = alignment;
}
#endregion
#region IDecoration Members
/// <summary>
/// Gets or sets the item being decorated
/// </summary>
public OLVListItem ListItem {
get { return listItem; }
set { listItem = value; }
}
private OLVListItem listItem;
/// <summary>
/// Gets or sets the sub item being decorated
/// </summary>
public OLVListSubItem SubItem {
get { return subItem; }
set { subItem = value; }
}
private OLVListSubItem subItem;
#endregion
#region Commands
/// <summary>
/// Draw this decoration
/// </summary>
/// <param name="olv">The ObjectListView being decorated</param>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The bounds of the rendering</param>
public virtual void Draw(ObjectListView olv, Graphics g, Rectangle r) {
this.DrawImage(g, this.CalculateItemBounds(this.ListItem, this.SubItem));
}
#endregion
}
/// <summary>
/// Instances of this class draw some text over the row/cell that they are decorating
/// </summary>
public class TextDecoration : TextAdornment, IDecoration
{
#region Constructors
/// <summary>
/// Create a TextDecoration
/// </summary>
public TextDecoration() {
this.Alignment = ContentAlignment.MiddleRight;
}
/// <summary>
/// Create a TextDecoration
/// </summary>
/// <param name="text"></param>
public TextDecoration(string text)
: this() {
this.Text = text;
}
/// <summary>
/// Create a TextDecoration
/// </summary>
/// <param name="text"></param>
/// <param name="transparency"></param>
public TextDecoration(string text, int transparency)
: this() {
this.Text = text;
this.Transparency = transparency;
}
/// <summary>
/// Create a TextDecoration
/// </summary>
/// <param name="text"></param>
/// <param name="alignment"></param>
public TextDecoration(string text, ContentAlignment alignment)
: this() {
this.Text = text;
this.Alignment = alignment;
}
/// <summary>
/// Create a TextDecoration
/// </summary>
/// <param name="text"></param>
/// <param name="transparency"></param>
/// <param name="alignment"></param>
public TextDecoration(string text, int transparency, ContentAlignment alignment)
: this() {
this.Text = text;
this.Transparency = transparency;
this.Alignment = alignment;
}
#endregion
#region IDecoration Members
/// <summary>
/// Gets or sets the item being decorated
/// </summary>
public OLVListItem ListItem {
get { return listItem; }
set { listItem = value; }
}
private OLVListItem listItem;
/// <summary>
/// Gets or sets the sub item being decorated
/// </summary>
public OLVListSubItem SubItem {
get { return subItem; }
set { subItem = value; }
}
private OLVListSubItem subItem;
#endregion
#region Commands
/// <summary>
/// Draw this decoration
/// </summary>
/// <param name="olv">The ObjectListView being decorated</param>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The bounds of the rendering</param>
public virtual void Draw(ObjectListView olv, Graphics g, Rectangle r) {
this.DrawText(g, this.CalculateItemBounds(this.ListItem, this.SubItem));
}
#endregion
}
}

302
Rendering/Overlays.cs Normal file
View File

@ -0,0 +1,302 @@
/*
* Overlays - Images, text or other things that can be rendered over the top of a ListView
*
* Author: Phillip Piper
* Date: 14/04/2009 4:36 PM
*
* Change log:
* v2.3
* 2009-08-17 JPP - Overlays now use Adornments
* - Added ITransparentOverlay interface. Overlays can now have separate transparency levels
* 2009-08-10 JPP - Moved decoration related code to new file
* v2.2.1
* 200-07-24 JPP - TintedColumnDecoration now works when last item is a member of a collapsed
* group (well, it no longer crashes).
* v2.2
* 2009-06-01 JPP - Make sure that TintedColumnDecoration reaches to the last item in group view
* 2009-05-05 JPP - Unified BillboardOverlay text rendering with that of TextOverlay
* 2009-04-30 JPP - Added TintedColumnDecoration
* 2009-04-14 JPP - Initial version
*
* To do:
*
* Copyright (C) 2009-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace BrightIdeasSoftware
{
/// <summary>
/// The interface for an object which can draw itself over the top of
/// an ObjectListView.
/// </summary>
public interface IOverlay
{
/// <summary>
/// Draw this overlay
/// </summary>
/// <param name="olv">The ObjectListView that is being overlaid</param>
/// <param name="g">The Graphics onto the given OLV</param>
/// <param name="r">The content area of the OLV</param>
void Draw(ObjectListView olv, Graphics g, Rectangle r);
}
/// <summary>
/// An interface for an overlay that supports variable levels of transparency
/// </summary>
public interface ITransparentOverlay : IOverlay
{
/// <summary>
/// Gets or sets the transparency of the overlay.
/// 0 is completely transparent, 255 is completely opaque.
/// </summary>
int Transparency { get; set; }
}
/// <summary>
/// A null implementation of the IOverlay interface
/// </summary>
public class AbstractOverlay : ITransparentOverlay
{
#region IOverlay Members
/// <summary>
/// Draw this overlay
/// </summary>
/// <param name="olv">The ObjectListView that is being overlaid</param>
/// <param name="g">The Graphics onto the given OLV</param>
/// <param name="r">The content area of the OLV</param>
public virtual void Draw(ObjectListView olv, Graphics g, Rectangle r) {
}
#endregion
#region ITransparentOverlay Members
/// <summary>
/// How transparent should this overlay be?
/// </summary>
[Category("ObjectListView"),
Description("How transparent should this overlay be"),
DefaultValue(128),
NotifyParentProperty(true)]
public int Transparency {
get { return this.transparency; }
set { this.transparency = Math.Min(255, Math.Max(0, value)); }
}
private int transparency = 128;
#endregion
}
/// <summary>
/// An overlay that will draw an image over the top of the ObjectListView
/// </summary>
[TypeConverter("BrightIdeasSoftware.Design.OverlayConverter")]
public class ImageOverlay : ImageAdornment, ITransparentOverlay
{
/// <summary>
/// Create an ImageOverlay
/// </summary>
public ImageOverlay() {
this.Alignment = System.Drawing.ContentAlignment.BottomRight;
}
#region Public properties
/// <summary>
/// Gets or sets the horizontal inset by which the position of the overlay will be adjusted
/// </summary>
[Category("ObjectListView"),
Description("The horizontal inset by which the position of the overlay will be adjusted"),
DefaultValue(20),
NotifyParentProperty(true)]
public int InsetX {
get { return this.insetX; }
set { this.insetX = Math.Max(0, value); }
}
private int insetX = 20;
/// <summary>
/// Gets or sets the vertical inset by which the position of the overlay will be adjusted
/// </summary>
[Category("ObjectListView"),
Description("Gets or sets the vertical inset by which the position of the overlay will be adjusted"),
DefaultValue(20),
NotifyParentProperty(true)]
public int InsetY {
get { return this.insetY; }
set { this.insetY = Math.Max(0, value); }
}
private int insetY = 20;
#endregion
#region Commands
/// <summary>
/// Draw this overlay
/// </summary>
/// <param name="olv">The ObjectListView being decorated</param>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The bounds of the rendering</param>
public virtual void Draw(ObjectListView olv, Graphics g, Rectangle r) {
Rectangle insetRect = r;
insetRect.Inflate(-this.InsetX, -this.InsetY);
// We hard code a transparency of 255 here since transparency is handled by the glass panel
this.DrawImage(g, insetRect, this.Image, 255);
}
#endregion
}
/// <summary>
/// An overlay that will draw text over the top of the ObjectListView
/// </summary>
[TypeConverter("BrightIdeasSoftware.Design.OverlayConverter")]
public class TextOverlay : TextAdornment, ITransparentOverlay
{
/// <summary>
/// Create a TextOverlay
/// </summary>
public TextOverlay() {
this.Alignment = System.Drawing.ContentAlignment.BottomRight;
}
#region Public properties
/// <summary>
/// Gets or sets the horizontal inset by which the position of the overlay will be adjusted
/// </summary>
[Category("ObjectListView"),
Description("The horizontal inset by which the position of the overlay will be adjusted"),
DefaultValue(20),
NotifyParentProperty(true)]
public int InsetX {
get { return this.insetX; }
set { this.insetX = Math.Max(0, value); }
}
private int insetX = 20;
/// <summary>
/// Gets or sets the vertical inset by which the position of the overlay will be adjusted
/// </summary>
[Category("ObjectListView"),
Description("Gets or sets the vertical inset by which the position of the overlay will be adjusted"),
DefaultValue(20),
NotifyParentProperty(true)]
public int InsetY {
get { return this.insetY; }
set { this.insetY = Math.Max(0, value); }
}
private int insetY = 20;
/// <summary>
/// Gets or sets whether the border will be drawn with rounded corners
/// </summary>
[Browsable(false),
Obsolete("Use CornerRounding instead", false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool RoundCorneredBorder {
get { return this.CornerRounding > 0; }
set {
if (value)
this.CornerRounding = 16.0f;
else
this.CornerRounding = 0.0f;
}
}
#endregion
#region Commands
/// <summary>
/// Draw this overlay
/// </summary>
/// <param name="olv">The ObjectListView being decorated</param>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The bounds of the rendering</param>
public virtual void Draw(ObjectListView olv, Graphics g, Rectangle r) {
if (String.IsNullOrEmpty(this.Text))
return;
Rectangle insetRect = r;
insetRect.Inflate(-this.InsetX, -this.InsetY);
// We hard code a transparency of 255 here since transparency is handled by the glass panel
this.DrawText(g, insetRect, this.Text, 255);
}
#endregion
}
/// <summary>
/// A Billboard overlay is a TextOverlay positioned at an absolute point
/// </summary>
public class BillboardOverlay : TextOverlay
{
/// <summary>
/// Create a BillboardOverlay
/// </summary>
public BillboardOverlay() {
this.Transparency = 255;
this.BackColor = Color.PeachPuff;
this.TextColor = Color.Black;
this.BorderColor = Color.Empty;
this.Font = new Font("Tahoma", 10);
}
/// <summary>
/// Gets or sets where should the top left of the billboard be placed
/// </summary>
public Point Location {
get { return this.location; }
set { this.location = value; }
}
private Point location;
/// <summary>
/// Draw this overlay
/// </summary>
/// <param name="olv">The ObjectListView being decorated</param>
/// <param name="g">The Graphics used for drawing</param>
/// <param name="r">The bounds of the rendering</param>
public override void Draw(ObjectListView olv, Graphics g, Rectangle r) {
if (String.IsNullOrEmpty(this.Text))
return;
// Calculate the bounds of the text, and then move it to where it should be
Rectangle textRect = this.CalculateTextBounds(g, r, this.Text);
textRect.Location = this.Location;
// Make sure the billboard is within the bounds of the List, as far as is possible
if (textRect.Right > r.Width)
textRect.X = Math.Max(r.Left, r.Width - textRect.Width);
if (textRect.Bottom > r.Height)
textRect.Y = Math.Max(r.Top, r.Height - textRect.Height);
this.DrawBorderedText(g, textRect, this.Text, 255);
}
}
}

3413
Rendering/Renderers.cs Normal file

File diff suppressed because it is too large Load Diff

400
Rendering/Styles.cs Normal file
View File

@ -0,0 +1,400 @@
/*
* Styles - A style is a group of formatting attributes that can be applied to a row or a cell
*
* Author: Phillip Piper
* Date: 29/07/2009 23:09
*
* Change log:
* v2.4
* 2010-03-23 JPP - Added HeaderFormatStyle and support
* v2.3
* 2009-08-15 JPP - Added Decoration and Overlay properties to HotItemStyle
* 2009-07-29 JPP - Initial version
*
* To do:
* - These should be more generally available. It should be possible to do something like this:
* this.olv.GetItem(i).Style = new ItemStyle();
* this.olv.GetItem(i).GetSubItem(j).Style = new CellStyle();
*
* Copyright (C) 2009-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace BrightIdeasSoftware
{
/// <summary>
/// The common interface supported by all style objects
/// </summary>
public interface IItemStyle
{
/// <summary>
/// Gets or set the font that will be used by this style
/// </summary>
Font Font { get; set; }
/// <summary>
/// Gets or set the font style
/// </summary>
FontStyle FontStyle { get; set; }
/// <summary>
/// Gets or sets the ForeColor
/// </summary>
Color ForeColor { get; set; }
/// <summary>
/// Gets or sets the BackColor
/// </summary>
Color BackColor { get; set; }
}
/// <summary>
/// Basic implementation of IItemStyle
/// </summary>
public class SimpleItemStyle : System.ComponentModel.Component, IItemStyle
{
/// <summary>
/// Gets or sets the font that will be applied by this style
/// </summary>
[DefaultValue(null)]
public Font Font
{
get { return this.font; }
set { this.font = value; }
}
private Font font;
/// <summary>
/// Gets or sets the style of font that will be applied by this style
/// </summary>
[DefaultValue(FontStyle.Regular)]
public FontStyle FontStyle
{
get { return this.fontStyle; }
set { this.fontStyle = value; }
}
private FontStyle fontStyle;
/// <summary>
/// Gets or sets the color of the text that will be applied by this style
/// </summary>
[DefaultValue(typeof (Color), "")]
public Color ForeColor
{
get { return this.foreColor; }
set { this.foreColor = value; }
}
private Color foreColor;
/// <summary>
/// Gets or sets the background color that will be applied by this style
/// </summary>
[DefaultValue(typeof (Color), "")]
public Color BackColor
{
get { return this.backColor; }
set { this.backColor = value; }
}
private Color backColor;
}
/// <summary>
/// Instances of this class specify how should "hot items" (non-selected
/// rows under the cursor) be renderered.
/// </summary>
public class HotItemStyle : SimpleItemStyle
{
/// <summary>
/// Gets or sets the overlay that should be drawn as part of the hot item
/// </summary>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public IOverlay Overlay {
get { return this.overlay; }
set { this.overlay = value; }
}
private IOverlay overlay;
/// <summary>
/// Gets or sets the decoration that should be drawn as part of the hot item
/// </summary>
/// <remarks>A decoration is different from an overlay in that an decoration
/// scrolls with the listview contents, whilst an overlay does not.</remarks>
[Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public IDecoration Decoration {
get { return this.decoration; }
set { this.decoration = value; }
}
private IDecoration decoration;
}
/// <summary>
/// This class defines how a cell should be formatted
/// </summary>
[TypeConverter(typeof(ExpandableObjectConverter))]
public class CellStyle : IItemStyle
{
/// <summary>
/// Gets or sets the font that will be applied by this style
/// </summary>
public Font Font {
get { return this.font; }
set { this.font = value; }
}
private Font font;
/// <summary>
/// Gets or sets the style of font that will be applied by this style
/// </summary>
[DefaultValue(FontStyle.Regular)]
public FontStyle FontStyle {
get { return this.fontStyle; }
set { this.fontStyle = value; }
}
private FontStyle fontStyle;
/// <summary>
/// Gets or sets the color of the text that will be applied by this style
/// </summary>
[DefaultValue(typeof(Color), "")]
public Color ForeColor {
get { return this.foreColor; }
set { this.foreColor = value; }
}
private Color foreColor;
/// <summary>
/// Gets or sets the background color that will be applied by this style
/// </summary>
[DefaultValue(typeof(Color), "")]
public Color BackColor {
get { return this.backColor; }
set { this.backColor = value; }
}
private Color backColor;
}
/// <summary>
/// Instances of this class describe how hyperlinks will appear
/// </summary>
public class HyperlinkStyle : System.ComponentModel.Component
{
/// <summary>
/// Create a HyperlinkStyle
/// </summary>
public HyperlinkStyle() {
this.Normal = new CellStyle();
this.Normal.ForeColor = Color.Blue;
this.Over = new CellStyle();
this.Over.FontStyle = FontStyle.Underline;
this.Visited = new CellStyle();
this.Visited.ForeColor = Color.Purple;
this.OverCursor = Cursors.Hand;
}
/// <summary>
/// What sort of formatting should be applied to hyperlinks in their normal state?
/// </summary>
[Category("Appearance"),
Description("How should hyperlinks be drawn")]
public CellStyle Normal {
get { return this.normalStyle; }
set { this.normalStyle = value; }
}
private CellStyle normalStyle;
/// <summary>
/// What sort of formatting should be applied to hyperlinks when the mouse is over them?
/// </summary>
[Category("Appearance"),
Description("How should hyperlinks be drawn when the mouse is over them?")]
public CellStyle Over {
get { return this.overStyle; }
set { this.overStyle = value; }
}
private CellStyle overStyle;
/// <summary>
/// What sort of formatting should be applied to hyperlinks after they have been clicked?
/// </summary>
[Category("Appearance"),
Description("How should hyperlinks be drawn after they have been clicked")]
public CellStyle Visited {
get { return this.visitedStyle; }
set { this.visitedStyle = value; }
}
private CellStyle visitedStyle;
/// <summary>
/// Gets or sets the cursor that should be shown when the mouse is over a hyperlink.
/// </summary>
[Category("Appearance"),
Description("What cursor should be shown when the mouse is over a link?")]
public Cursor OverCursor {
get { return this.overCursor; }
set { this.overCursor = value; }
}
private Cursor overCursor;
}
/// <summary>
/// Instances of this class control one the styling of one particular state
/// (normal, hot, pressed) of a header control
/// </summary>
[TypeConverter(typeof(ExpandableObjectConverter))]
public class HeaderStateStyle
{
/// <summary>
/// Gets or sets the font that will be applied by this style
/// </summary>
[DefaultValue(null)]
public Font Font {
get { return this.font; }
set { this.font = value; }
}
private Font font;
/// <summary>
/// Gets or sets the color of the text that will be applied by this style
/// </summary>
[DefaultValue(typeof(Color), "")]
public Color ForeColor {
get { return this.foreColor; }
set { this.foreColor = value; }
}
private Color foreColor;
/// <summary>
/// Gets or sets the background color that will be applied by this style
/// </summary>
[DefaultValue(typeof(Color), "")]
public Color BackColor {
get { return this.backColor; }
set { this.backColor = value; }
}
private Color backColor;
/// <summary>
/// Gets or sets the color in which a frame will be drawn around the header for this column
/// </summary>
[DefaultValue(typeof(Color), "")]
public Color FrameColor {
get { return this.frameColor; }
set { this.frameColor = value; }
}
private Color frameColor;
/// <summary>
/// Gets or sets the width of the frame that will be drawn around the header for this column
/// </summary>
[DefaultValue(0.0f)]
public float FrameWidth {
get { return this.frameWidth; }
set { this.frameWidth = value; }
}
private float frameWidth;
}
/// <summary>
/// This class defines how a header should be formatted in its various states.
/// </summary>
public class HeaderFormatStyle : System.ComponentModel.Component
{
/// <summary>
/// Create a new HeaderFormatStyle
/// </summary>
public HeaderFormatStyle() {
this.Hot = new HeaderStateStyle();
this.Normal = new HeaderStateStyle();
this.Pressed = new HeaderStateStyle();
}
/// <summary>
/// What sort of formatting should be applied to a column header when the mouse is over it?
/// </summary>
[Category("Appearance"),
Description("How should the header be drawn when the mouse is over it?")]
public HeaderStateStyle Hot {
get { return this.hotStyle; }
set { this.hotStyle = value; }
}
private HeaderStateStyle hotStyle;
/// <summary>
/// What sort of formatting should be applied to a column header in its normal state?
/// </summary>
[Category("Appearance"),
Description("How should a column header normally be drawn")]
public HeaderStateStyle Normal {
get { return this.normalStyle; }
set { this.normalStyle = value; }
}
private HeaderStateStyle normalStyle;
/// <summary>
/// What sort of formatting should be applied to a column header when pressed?
/// </summary>
[Category("Appearance"),
Description("How should a column header be drawn when it is pressed")]
public HeaderStateStyle Pressed {
get { return this.pressedStyle; }
set { this.pressedStyle = value; }
}
private HeaderStateStyle pressedStyle;
/// <summary>
/// Set the font for all three states
/// </summary>
/// <param name="font"></param>
public void SetFont(Font font) {
this.Normal.Font = font;
this.Hot.Font = font;
this.Pressed.Font = font;
}
/// <summary>
/// Set the fore color for all three states
/// </summary>
/// <param name="color"></param>
public void SetForeColor(Color color) {
this.Normal.ForeColor = color;
this.Hot.ForeColor = color;
this.Pressed.ForeColor = color;
}
/// <summary>
/// Set the back color for all three states
/// </summary>
/// <param name="color"></param>
public void SetBackColor(Color color) {
this.Normal.BackColor = color;
this.Hot.BackColor = color;
this.Pressed.BackColor = color;
}
}
}

248
Rendering/TreeRenderer.cs Normal file
View File

@ -0,0 +1,248 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Drawing.Drawing2D;
namespace BrightIdeasSoftware {
public partial class TreeListView {
/// <summary>
/// This class handles drawing the tree structure of the primary column.
/// </summary>
public class TreeRenderer : HighlightTextRenderer {
/// <summary>
/// Create a TreeRenderer
/// </summary>
public TreeRenderer() {
this.LinePen = new Pen(Color.Blue, 1.0f);
this.LinePen.DashStyle = DashStyle.Dot;
}
/// <summary>
/// Return the branch that the renderer is currently drawing.
/// </summary>
private Branch Branch {
get {
return this.TreeListView.TreeModel.GetBranch(this.RowObject);
}
}
/// <summary>
/// Return the pen that will be used to draw the lines between branches
/// </summary>
public Pen LinePen {
get { return linePen; }
set { linePen = value; }
}
private Pen linePen;
/// <summary>
/// Return the TreeListView for which the renderer is being used.
/// </summary>
public TreeListView TreeListView {
get {
return (TreeListView)this.ListView;
}
}
/// <summary>
/// Should the renderer draw lines connecting siblings?
/// </summary>
public bool IsShowLines {
get { return isShowLines; }
set { isShowLines = value; }
}
private bool isShowLines = true;
/// <summary>
/// How many pixels will be reserved for each level of indentation?
/// </summary>
public static int PIXELS_PER_LEVEL = 16 + 1;
/// <summary>
/// The real work of drawing the tree is done in this method
/// </summary>
/// <param name="g"></param>
/// <param name="r"></param>
public override void Render(System.Drawing.Graphics g, System.Drawing.Rectangle r) {
this.DrawBackground(g, r);
Branch br = this.Branch;
Rectangle paddedRectangle = this.ApplyCellPadding(r);
Rectangle expandGlyphRectangle = paddedRectangle;
expandGlyphRectangle.Offset((br.Level - 1) * PIXELS_PER_LEVEL, 0);
expandGlyphRectangle.Width = PIXELS_PER_LEVEL;
expandGlyphRectangle.Height = PIXELS_PER_LEVEL;
expandGlyphRectangle.Y = this.AlignVertically(paddedRectangle, expandGlyphRectangle);
int expandGlyphRectangleMidVertical = expandGlyphRectangle.Y + (expandGlyphRectangle.Height/2);
if (this.IsShowLines)
this.DrawLines(g, r, this.LinePen, br, expandGlyphRectangleMidVertical);
if (br.CanExpand)
this.DrawExpansionGlyph(g, expandGlyphRectangle, br.IsExpanded);
int indent = br.Level * PIXELS_PER_LEVEL;
paddedRectangle.Offset(indent, 0);
paddedRectangle.Width -= indent;
this.DrawImageAndText(g, paddedRectangle);
}
/// <summary>
/// Draw the expansion indicator
/// </summary>
/// <param name="g"></param>
/// <param name="r"></param>
/// <param name="isExpanded"></param>
protected virtual void DrawExpansionGlyph(Graphics g, Rectangle r, bool isExpanded) {
if (this.UseStyles) {
this.DrawExpansionGlyphStyled(g, r, isExpanded);
} else {
this.DrawExpansionGlyphManual(g, r, isExpanded);
}
}
/// <summary>
/// Gets whether or not we should render using styles
/// </summary>
protected virtual bool UseStyles {
get {
return !this.IsPrinting && Application.RenderWithVisualStyles;
}
}
/// <summary>
/// Draw the expansion indicator using styles
/// </summary>
/// <param name="g"></param>
/// <param name="r"></param>
/// <param name="isExpanded"></param>
protected virtual void DrawExpansionGlyphStyled(Graphics g, Rectangle r, bool isExpanded) {
VisualStyleElement element = VisualStyleElement.TreeView.Glyph.Closed;
if (isExpanded)
element = VisualStyleElement.TreeView.Glyph.Opened;
VisualStyleRenderer renderer = new VisualStyleRenderer(element);
renderer.DrawBackground(g, r);
}
/// <summary>
/// Draw the expansion indicator without using styles
/// </summary>
/// <param name="g"></param>
/// <param name="r"></param>
/// <param name="isExpanded"></param>
protected virtual void DrawExpansionGlyphManual(Graphics g, Rectangle r, bool isExpanded) {
int h = 8;
int w = 8;
int x = r.X + 4;
int y = r.Y + (r.Height / 2) - 4;
g.DrawRectangle(new Pen(SystemBrushes.ControlDark), x, y, w, h);
g.FillRectangle(Brushes.White, x + 1, y + 1, w - 1, h - 1);
g.DrawLine(Pens.Black, x + 2, y + 4, x + w - 2, y + 4);
if (!isExpanded)
g.DrawLine(Pens.Black, x + 4, y + 2, x + 4, y + h - 2);
}
/// <summary>
/// Draw the lines of the tree
/// </summary>
/// <param name="g"></param>
/// <param name="r"></param>
/// <param name="p"></param>
/// <param name="br"></param>
/// <param name="glyphMidVertical"> </param>
protected virtual void DrawLines(Graphics g, Rectangle r, Pen p, Branch br, int glyphMidVertical) {
Rectangle r2 = r;
r2.Width = PIXELS_PER_LEVEL;
// Vertical lines have to start on even points, otherwise the dotted line looks wrong.
// This is only needed if pen is dotted.
int top = r2.Top;
//if (p.DashStyle == DashStyle.Dot && (top & 1) == 0)
// top += 1;
// Draw lines for ancestors
int midX;
IList<Branch> ancestors = br.Ancestors;
foreach (Branch ancestor in ancestors) {
if (!ancestor.IsLastChild && !ancestor.IsOnlyBranch) {
midX = r2.Left + r2.Width / 2;
g.DrawLine(p, midX, top, midX, r2.Bottom);
}
r2.Offset(PIXELS_PER_LEVEL, 0);
}
// Draw lines for this branch
midX = r2.Left + r2.Width / 2;
// Horizontal line first
g.DrawLine(p, midX, glyphMidVertical, r2.Right, glyphMidVertical);
// Vertical line second
if (br.IsFirstBranch) {
if (!br.IsLastChild && !br.IsOnlyBranch)
g.DrawLine(p, midX, glyphMidVertical, midX, r2.Bottom);
} else {
if (br.IsLastChild)
g.DrawLine(p, midX, top, midX, glyphMidVertical);
else
g.DrawLine(p, midX, top, midX, r2.Bottom);
}
}
/// <summary>
/// Do the hit test
/// </summary>
/// <param name="g"></param>
/// <param name="hti"></param>
/// <param name="x"></param>
/// <param name="y"></param>
protected override void HandleHitTest(Graphics g, OlvListViewHitTestInfo hti, int x, int y) {
Branch br = this.Branch;
Rectangle r = this.Bounds;
if (br.CanExpand) {
r.Offset((br.Level - 1) * PIXELS_PER_LEVEL, 0);
r.Width = PIXELS_PER_LEVEL;
if (r.Contains(x, y)) {
hti.HitTestLocation = HitTestLocation.ExpandButton;
return;
}
}
r = this.Bounds;
int indent = br.Level * PIXELS_PER_LEVEL;
r.X += indent;
r.Width -= indent;
// Ignore events in the indent zone
if (x < r.Left) {
hti.HitTestLocation = HitTestLocation.Nothing;
} else {
this.StandardHitTest(g, hti, r, x, y);
}
}
/// <summary>
/// Calculate the edit rect
/// </summary>
/// <param name="g"></param>
/// <param name="cellBounds"></param>
/// <param name="item"></param>
/// <param name="subItemIndex"></param>
/// <param name="preferredSize"> </param>
/// <returns></returns>
protected override Rectangle HandleGetEditRectangle(Graphics g, Rectangle cellBounds, OLVListItem item, int subItemIndex, Size preferredSize) {
return this.StandardGetEditRectangle(g, cellBounds, preferredSize);
}
}
}
}

BIN
Resources/clear-filter.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
Resources/coffee.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

BIN
Resources/filter-icons3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
Resources/filter.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -0,0 +1,459 @@
/*
* GlassPanelForm - A transparent form that is placed over an ObjectListView
* to allow flicker-free overlay images during scrolling.
*
* Author: Phillip Piper
* Date: 14/04/2009 4:36 PM
*
* Change log:
* 2010-08-18 JPP - Added WS_EX_TOOLWINDOW style so that the form won't appear in Alt-Tab list.
* v2.4
* 2010-03-11 JPP - Work correctly in MDI applications -- more or less. Actually, less than more.
* They don't crash but they don't correctly handle overlapping MDI children.
* Overlays from one control are shown on top of other other windows.
* 2010-03-09 JPP - Correctly Unbind() when the related ObjectListView is disposed.
* 2009-10-28 JPP - Use FindForm() rather than TopMostControl, since the latter doesn't work
* as I expected when the OLV is part of an MDI child window. Thanks to
* wvd_vegt who tracked this down.
* v2.3
* 2009-08-19 JPP - Only hide the glass pane on resize, not on move
* - Each glass panel now only draws one overlays
* v2.2
* 2009-06-05 JPP - Handle when owning window is a topmost window
* 2009-04-14 JPP - Initial version
*
* To do:
*
* Copyright (C) 2009-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace BrightIdeasSoftware
{
/// <summary>
/// A GlassPanelForm sits transparently over an ObjectListView to show overlays.
/// </summary>
internal partial class GlassPanelForm : Form
{
public GlassPanelForm() {
this.Name = "GlassPanelForm";
this.Text = "GlassPanelForm";
ClientSize = new System.Drawing.Size(0, 0);
ControlBox = false;
FormBorderStyle = FormBorderStyle.None;
SizeGripStyle = SizeGripStyle.Hide;
StartPosition = FormStartPosition.Manual;
MaximizeBox = false;
MinimizeBox = false;
ShowIcon = false;
ShowInTaskbar = false;
FormBorderStyle = FormBorderStyle.None;
SetStyle(ControlStyles.Selectable, false);
this.Opacity = 0.5f;
this.BackColor = Color.FromArgb(255, 254, 254, 254);
this.TransparencyKey = this.BackColor;
this.HideGlass();
NativeMethods.ShowWithoutActivate(this);
}
protected override void Dispose(bool disposing) {
if (disposing)
this.Unbind();
base.Dispose(disposing);
}
#region Properties
/// <summary>
/// Get the low-level windows flag that will be given to CreateWindow.
/// </summary>
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20; // WS_EX_TRANSPARENT
cp.ExStyle |= 0x80; // WS_EX_TOOLWINDOW
return cp;
}
}
#endregion
#region Commands
/// <summary>
/// Attach this form to the given ObjectListView
/// </summary>
public void Bind(ObjectListView olv, IOverlay overlay) {
if (this.objectListView != null)
this.Unbind();
this.objectListView = olv;
this.Overlay = overlay;
this.mdiClient = null;
this.mdiOwner = null;
if (this.objectListView == null)
return;
// NOTE: If you listen to any events here, you *must* stop listening in Unbind()
this.objectListView.Disposed += new EventHandler(objectListView_Disposed);
this.objectListView.LocationChanged += new EventHandler(objectListView_LocationChanged);
this.objectListView.SizeChanged += new EventHandler(objectListView_SizeChanged);
this.objectListView.VisibleChanged += new EventHandler(objectListView_VisibleChanged);
this.objectListView.ParentChanged += new EventHandler(objectListView_ParentChanged);
// Collect our ancestors in the widget hierachy
if (this.ancestors == null)
this.ancestors = new List<Control>();
Control parent = this.objectListView.Parent;
while (parent != null) {
this.ancestors.Add(parent);
parent = parent.Parent;
}
// Listen for changes in the hierachy
foreach (Control ancestor in this.ancestors) {
ancestor.ParentChanged += new EventHandler(objectListView_ParentChanged);
TabControl tabControl = ancestor as TabControl;
if (tabControl != null) {
tabControl.Selected += new TabControlEventHandler(tabControl_Selected);
}
}
// Listen for changes in our owning form
this.Owner = this.objectListView.FindForm();
this.myOwner = this.Owner;
if (this.Owner != null) {
this.Owner.LocationChanged += new EventHandler(Owner_LocationChanged);
this.Owner.SizeChanged += new EventHandler(Owner_SizeChanged);
this.Owner.ResizeBegin += new EventHandler(Owner_ResizeBegin);
this.Owner.ResizeEnd += new EventHandler(Owner_ResizeEnd);
if (this.Owner.TopMost) {
// We can't do this.TopMost = true; since that will activate the panel,
// taking focus away from the owner of the listview
NativeMethods.MakeTopMost(this);
}
// We need special code to handle MDI
this.mdiOwner = this.Owner.MdiParent;
if (this.mdiOwner != null) {
this.mdiOwner.LocationChanged += new EventHandler(Owner_LocationChanged);
this.mdiOwner.SizeChanged += new EventHandler(Owner_SizeChanged);
this.mdiOwner.ResizeBegin += new EventHandler(Owner_ResizeBegin);
this.mdiOwner.ResizeEnd += new EventHandler(Owner_ResizeEnd);
// Find the MDIClient control, which houses all MDI children
foreach (Control c in this.mdiOwner.Controls) {
this.mdiClient = c as MdiClient;
if (this.mdiClient != null) {
break;
}
}
if (this.mdiClient != null) {
this.mdiClient.ClientSizeChanged += new EventHandler(myMdiClient_ClientSizeChanged);
}
}
}
this.UpdateTransparency();
}
void myMdiClient_ClientSizeChanged(object sender, EventArgs e) {
this.RecalculateBounds();
this.Invalidate();
}
/// <summary>
/// Made the overlay panel invisible
/// </summary>
public void HideGlass() {
if (!this.isGlassShown)
return;
this.isGlassShown = false;
this.Bounds = new Rectangle(-10000, -10000, 1, 1);
}
/// <summary>
/// Show the overlay panel in its correctly location
/// </summary>
/// <remarks>
/// If the panel is always shown, this method does nothing.
/// If the panel is being resized, this method also does nothing.
/// </remarks>
public void ShowGlass() {
if (this.isGlassShown || this.isDuringResizeSequence)
return;
this.isGlassShown = true;
this.RecalculateBounds();
}
/// <summary>
/// Detach this glass panel from its previous ObjectListView
/// </summary>
/// <remarks>
/// You should unbind the overlay panel before making any changes to the
/// widget hierarchy.
/// </remarks>
public void Unbind() {
if (this.objectListView != null) {
this.objectListView.Disposed -= new EventHandler(objectListView_Disposed);
this.objectListView.LocationChanged -= new EventHandler(objectListView_LocationChanged);
this.objectListView.SizeChanged -= new EventHandler(objectListView_SizeChanged);
this.objectListView.VisibleChanged -= new EventHandler(objectListView_VisibleChanged);
this.objectListView.ParentChanged -= new EventHandler(objectListView_ParentChanged);
this.objectListView = null;
}
if (this.ancestors != null) {
foreach (Control parent in this.ancestors) {
parent.ParentChanged -= new EventHandler(objectListView_ParentChanged);
TabControl tabControl = parent as TabControl;
if (tabControl != null) {
tabControl.Selected -= new TabControlEventHandler(tabControl_Selected);
}
}
this.ancestors = null;
}
if (this.myOwner != null) {
this.myOwner.LocationChanged -= new EventHandler(Owner_LocationChanged);
this.myOwner.SizeChanged -= new EventHandler(Owner_SizeChanged);
this.myOwner.ResizeBegin -= new EventHandler(Owner_ResizeBegin);
this.myOwner.ResizeEnd -= new EventHandler(Owner_ResizeEnd);
this.myOwner = null;
}
if (this.mdiOwner != null) {
this.mdiOwner.LocationChanged -= new EventHandler(Owner_LocationChanged);
this.mdiOwner.SizeChanged -= new EventHandler(Owner_SizeChanged);
this.mdiOwner.ResizeBegin -= new EventHandler(Owner_ResizeBegin);
this.mdiOwner.ResizeEnd -= new EventHandler(Owner_ResizeEnd);
this.mdiOwner = null;
}
if (this.mdiClient != null) {
this.mdiClient.ClientSizeChanged -= new EventHandler(myMdiClient_ClientSizeChanged);
this.mdiClient = null;
}
}
#endregion
#region Event Handlers
void objectListView_Disposed(object sender, EventArgs e) {
this.Unbind();
}
/// <summary>
/// Handle when the form that owns the ObjectListView begins to be resized
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void Owner_ResizeBegin(object sender, EventArgs e) {
// When the top level window is being resized, we just want to hide
// the overlay window. When the resizing finishes, we want to show
// the overlay window, if it was shown before the resize started.
this.isDuringResizeSequence = true;
this.wasGlassShownBeforeResize = this.isGlassShown;
}
/// <summary>
/// Handle when the form that owns the ObjectListView finished to be resized
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void Owner_ResizeEnd(object sender, EventArgs e) {
this.isDuringResizeSequence = false;
if (this.wasGlassShownBeforeResize)
this.ShowGlass();
}
/// <summary>
/// The owning form has moved. Move the overlay panel too.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void Owner_LocationChanged(object sender, EventArgs e) {
if (this.mdiOwner != null)
this.HideGlass();
else
this.RecalculateBounds();
}
/// <summary>
/// The owning form is resizing. Hide our overlay panel until the resizing stops
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void Owner_SizeChanged(object sender, EventArgs e) {
this.HideGlass();
}
/// <summary>
/// Handle when the bound OLV changes its location. The overlay panel must
/// be moved too, IFF it is currently visible.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void objectListView_LocationChanged(object sender, EventArgs e) {
if (this.isGlassShown) {
this.RecalculateBounds();
}
}
/// <summary>
/// Handle when the bound OLV changes size. The overlay panel must
/// resize too, IFF it is currently visible.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void objectListView_SizeChanged(object sender, EventArgs e) {
// This event is triggered in all sorts of places, and not always when the size changes.
//if (this.isGlassShown) {
// this.Size = this.objectListView.ClientSize;
//}
}
/// <summary>
/// Handle when the bound OLV is part of a TabControl and that
/// TabControl changes tabs. The overlay panel is hidden. The
/// first time the bound OLV is redrawn, the overlay panel will
/// be shown again.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void tabControl_Selected(object sender, TabControlEventArgs e) {
this.HideGlass();
}
/// <summary>
/// Somewhere the parent of the bound OLV has changed. Update
/// our events.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void objectListView_ParentChanged(object sender, EventArgs e) {
ObjectListView olv = this.objectListView;
IOverlay overlay = this.Overlay;
this.Unbind();
this.Bind(olv, overlay);
}
/// <summary>
/// Handle when the bound OLV changes its visibility.
/// The overlay panel should match the OLV's visibility.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void objectListView_VisibleChanged(object sender, EventArgs e) {
if (this.objectListView.Visible)
this.ShowGlass();
else
this.HideGlass();
}
#endregion
#region Implementation
protected override void OnPaint(PaintEventArgs e) {
if (this.objectListView == null || this.Overlay == null)
return;
Graphics g = e.Graphics;
g.TextRenderingHint = ObjectListView.TextRenderingHint;
g.SmoothingMode = ObjectListView.SmoothingMode;
//g.DrawRectangle(new Pen(Color.Green, 4.0f), this.ClientRectangle);
// If we are part of an MDI app, make sure we don't draw outside the bounds
if (this.mdiClient != null) {
Rectangle r = mdiClient.RectangleToScreen(mdiClient.ClientRectangle);
Rectangle r2 = this.objectListView.RectangleToClient(r);
g.SetClip(r2, System.Drawing.Drawing2D.CombineMode.Intersect);
}
this.Overlay.Draw(this.objectListView, g, this.objectListView.ClientRectangle);
}
protected void RecalculateBounds() {
if (!this.isGlassShown)
return;
Rectangle rect = this.objectListView.ClientRectangle;
rect.X = 0;
rect.Y = 0;
this.Bounds = this.objectListView.RectangleToScreen(rect);
}
internal void UpdateTransparency() {
ITransparentOverlay transparentOverlay = this.Overlay as ITransparentOverlay;
if (transparentOverlay == null)
this.Opacity = this.objectListView.OverlayTransparency / 255.0f;
else
this.Opacity = transparentOverlay.Transparency / 255.0f;
}
protected override void WndProc(ref Message m) {
const int WM_NCHITTEST = 132;
const int HTTRANSPARENT = -1;
switch (m.Msg) {
// Ignore all mouse interactions
case WM_NCHITTEST:
m.Result = (IntPtr)HTTRANSPARENT;
break;
}
base.WndProc(ref m);
}
#endregion
#region Implementation variables
internal IOverlay Overlay;
#endregion
#region Private variables
private ObjectListView objectListView;
private bool isDuringResizeSequence;
private bool isGlassShown;
private bool wasGlassShownBeforeResize;
// Cache these so we can unsubscribe from events even when the OLV has been disposed.
private Form myOwner;
private Form mdiOwner;
private List<Control> ancestors;
MdiClient mdiClient;
#endregion
}
}

1218
SubControls/HeaderControl.cs Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,189 @@
/*
* ToolStripCheckedListBox - Puts a CheckedListBox into a tool strip menu item
*
* Author: Phillip Piper
* Date: 4-March-2011 11:59 pm
*
* Change log:
* 2011-03-04 JPP - First version
*
* Copyright (C) 2011-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace BrightIdeasSoftware {
/// <summary>
/// Instances of this class put a CheckedListBox into a tool strip menu item.
/// </summary>
public class ToolStripCheckedListBox : ToolStripControlHost {
/// <summary>
/// Create a ToolStripCheckedListBox
/// </summary>
public ToolStripCheckedListBox()
: base(new CheckedListBox()) {
this.CheckedListBoxControl.MaximumSize = new Size(400, 700);
this.CheckedListBoxControl.ThreeDCheckBoxes = true;
this.CheckedListBoxControl.CheckOnClick = true;
this.CheckedListBoxControl.SelectionMode = SelectionMode.One;
}
/// <summary>
/// Gets the control embedded in the menu
/// </summary>
public CheckedListBox CheckedListBoxControl {
get {
return Control as CheckedListBox;
}
}
/// <summary>
/// Gets the items shown in the checkedlistbox
/// </summary>
public CheckedListBox.ObjectCollection Items {
get {
return this.CheckedListBoxControl.Items;
}
}
/// <summary>
/// Gets or sets whether an item should be checked when it is clicked
/// </summary>
public bool CheckedOnClick {
get {
return this.CheckedListBoxControl.CheckOnClick;
}
set {
this.CheckedListBoxControl.CheckOnClick = value;
}
}
/// <summary>
/// Gets a collection of the checked items
/// </summary>
public CheckedListBox.CheckedItemCollection CheckedItems {
get {
return this.CheckedListBoxControl.CheckedItems;
}
}
/// <summary>
/// Add a possibly checked item to the control
/// </summary>
/// <param name="item"></param>
/// <param name="isChecked"></param>
public void AddItem(object item, bool isChecked) {
this.Items.Add(item);
if (isChecked)
this.CheckedListBoxControl.SetItemChecked(this.Items.Count - 1, true);
}
/// <summary>
/// Add an item with the given state to the control
/// </summary>
/// <param name="item"></param>
/// <param name="state"></param>
public void AddItem(object item, CheckState state) {
this.Items.Add(item);
this.CheckedListBoxControl.SetItemCheckState(this.Items.Count - 1, state);
}
/// <summary>
/// Gets the checkedness of the i'th item
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public CheckState GetItemCheckState(int i) {
return this.CheckedListBoxControl.GetItemCheckState(i);
}
/// <summary>
/// Set the checkedness of the i'th item
/// </summary>
/// <param name="i"></param>
/// <param name="checkState"></param>
public void SetItemState(int i, CheckState checkState) {
if (i >= 0 && i < this.Items.Count)
this.CheckedListBoxControl.SetItemCheckState(i, checkState);
}
/// <summary>
/// Check all the items in the control
/// </summary>
public void CheckAll() {
for (int i = 0; i < this.Items.Count; i++)
this.CheckedListBoxControl.SetItemChecked(i, true);
}
/// <summary>
/// Unchecked all the items in the control
/// </summary>
public void UncheckAll() {
for (int i = 0; i < this.Items.Count; i++)
this.CheckedListBoxControl.SetItemChecked(i, false);
}
#region Events
/// <summary>
/// Listen for events on the underlying control
/// </summary>
/// <param name="c"></param>
protected override void OnSubscribeControlEvents(Control c) {
base.OnSubscribeControlEvents(c);
CheckedListBox control = (CheckedListBox)c;
control.ItemCheck += new ItemCheckEventHandler(OnItemCheck);
}
/// <summary>
/// Stop listening for events on the underlying control
/// </summary>
/// <param name="c"></param>
protected override void OnUnsubscribeControlEvents(Control c) {
base.OnUnsubscribeControlEvents(c);
CheckedListBox control = (CheckedListBox)c;
control.ItemCheck -= new ItemCheckEventHandler(OnItemCheck);
}
/// <summary>
/// Tell the world that an item was checked
/// </summary>
public event ItemCheckEventHandler ItemCheck;
/// <summary>
/// Trigger the ItemCheck event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnItemCheck(object sender, ItemCheckEventArgs e) {
if (ItemCheck != null) {
ItemCheck(this, e);
}
}
#endregion
}
}

View File

@ -0,0 +1,699 @@
/*
* ToolTipControl - A limited wrapper around a Windows tooltip control
*
* For some reason, the ToolTip class in the .NET framework is implemented in a significantly
* different manner to other controls. For our purposes, the worst of these problems
* is that we cannot get the Handle, so we cannot send Windows level messages to the control.
*
* Author: Phillip Piper
* Date: 2009-05-17 7:22PM
*
* Change log:
* v2.3
* 2009-06-13 JPP - Moved ToolTipShowingEventArgs to Events.cs
* v2.2
* 2009-06-06 JPP - Fixed some Vista specific problems
* 2009-05-17 JPP - Initial version
*
* TO DO:
*
* Copyright (C) 2006-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Security.Permissions;
namespace BrightIdeasSoftware
{
/// <summary>
/// A limited wrapper around a Windows tooltip window.
/// </summary>
public class ToolTipControl : NativeWindow
{
#region Constants
/// <summary>
/// These are the standard icons that a tooltip can display.
/// </summary>
public enum StandardIcons
{
/// <summary>
/// No icon
/// </summary>
None = 0,
/// <summary>
/// Info
/// </summary>
Info = 1,
/// <summary>
/// Warning
/// </summary>
Warning = 2,
/// <summary>
/// Error
/// </summary>
Error = 3,
/// <summary>
/// Large info (Vista and later only)
/// </summary>
InfoLarge = 4,
/// <summary>
/// Large warning (Vista and later only)
/// </summary>
WarningLarge = 5,
/// <summary>
/// Large error (Vista and later only)
/// </summary>
ErrorLarge = 6
}
const int GWL_STYLE = -16;
const int WM_GETFONT = 0x31;
const int WM_SETFONT = 0x30;
const int WS_BORDER = 0x800000;
const int WS_EX_TOPMOST = 8;
const int TTM_ADDTOOL = 0x432;
const int TTM_ADJUSTRECT = 0x400 + 31;
const int TTM_DELTOOL = 0x433;
const int TTM_GETBUBBLESIZE = 0x400 + 30;
const int TTM_GETCURRENTTOOL = 0x400 + 59;
const int TTM_GETTIPBKCOLOR = 0x400 + 22;
const int TTM_GETTIPTEXTCOLOR = 0x400 + 23;
const int TTM_GETDELAYTIME = 0x400 + 21;
const int TTM_NEWTOOLRECT = 0x400 + 52;
const int TTM_POP = 0x41c;
const int TTM_SETDELAYTIME = 0x400 + 3;
const int TTM_SETMAXTIPWIDTH = 0x400 + 24;
const int TTM_SETTIPBKCOLOR = 0x400 + 19;
const int TTM_SETTIPTEXTCOLOR = 0x400 + 20;
const int TTM_SETTITLE = 0x400 + 33;
const int TTM_SETTOOLINFO = 0x400 + 54;
const int TTF_IDISHWND = 1;
//const int TTF_ABSOLUTE = 0x80;
const int TTF_CENTERTIP = 2;
const int TTF_RTLREADING = 4;
const int TTF_SUBCLASS = 0x10;
//const int TTF_TRACK = 0x20;
//const int TTF_TRANSPARENT = 0x100;
const int TTF_PARSELINKS = 0x1000;
const int TTS_NOPREFIX = 2;
const int TTS_BALLOON = 0x40;
const int TTS_USEVISUALSTYLE = 0x100;
const int TTN_FIRST = -520;
/// <summary>
///
/// </summary>
public const int TTN_SHOW = (TTN_FIRST - 1);
/// <summary>
///
/// </summary>
public const int TTN_POP = (TTN_FIRST - 2);
/// <summary>
///
/// </summary>
public const int TTN_LINKCLICK = (TTN_FIRST - 3);
/// <summary>
///
/// </summary>
public const int TTN_GETDISPINFO = (TTN_FIRST - 10);
const int TTDT_AUTOMATIC = 0;
const int TTDT_RESHOW = 1;
const int TTDT_AUTOPOP = 2;
const int TTDT_INITIAL = 3;
#endregion
#region Properties
/// <summary>
/// Get or set if the style of the tooltip control
/// </summary>
internal int WindowStyle {
get {
return (int)NativeMethods.GetWindowLong(this.Handle, GWL_STYLE);
}
set {
NativeMethods.SetWindowLong(this.Handle, GWL_STYLE, value);
}
}
/// <summary>
/// Get or set if the tooltip should be shown as a ballon
/// </summary>
public bool IsBalloon {
get {
return (this.WindowStyle & TTS_BALLOON) == TTS_BALLOON;
}
set {
if (this.IsBalloon == value)
return;
int windowStyle = this.WindowStyle;
if (value) {
windowStyle |= (TTS_BALLOON | TTS_USEVISUALSTYLE);
// On XP, a border makes the ballon look wrong
if (!ObjectListView.IsVistaOrLater)
windowStyle &= ~WS_BORDER;
} else {
windowStyle &= ~(TTS_BALLOON | TTS_USEVISUALSTYLE);
if (!ObjectListView.IsVistaOrLater) {
if (this.hasBorder)
windowStyle |= WS_BORDER;
else
windowStyle &= ~WS_BORDER;
}
}
this.WindowStyle = windowStyle;
}
}
/// <summary>
/// Get or set if the tooltip should be shown as a ballon
/// </summary>
public bool HasBorder {
get {
return this.hasBorder;
}
set {
if (this.hasBorder == value)
return;
if (value) {
this.WindowStyle |= WS_BORDER;
} else {
this.WindowStyle &= ~WS_BORDER;
}
}
}
private bool hasBorder = true;
/// <summary>
/// Get or set the background color of the tooltip
/// </summary>
public Color BackColor {
get {
int color = (int)NativeMethods.SendMessage(this.Handle, TTM_GETTIPBKCOLOR, 0, 0);
return ColorTranslator.FromWin32(color);
}
set {
// For some reason, setting the color fails on Vista and messes up later ops.
// So we don't even try to set it.
if (!ObjectListView.IsVistaOrLater) {
int color = ColorTranslator.ToWin32(value);
NativeMethods.SendMessage(this.Handle, TTM_SETTIPBKCOLOR, color, 0);
//int x2 = Marshal.GetLastWin32Error();
}
}
}
/// <summary>
/// Get or set the color of the text and border on the tooltip.
/// </summary>
public Color ForeColor {
get {
int color = (int)NativeMethods.SendMessage(this.Handle, TTM_GETTIPTEXTCOLOR, 0, 0);
return ColorTranslator.FromWin32(color);
}
set {
// For some reason, setting the color fails on Vista and messes up later ops.
// So we don't even try to set it.
if (!ObjectListView.IsVistaOrLater) {
int color = ColorTranslator.ToWin32(value);
NativeMethods.SendMessage(this.Handle, TTM_SETTIPTEXTCOLOR, color, 0);
}
}
}
/// <summary>
/// Get or set the title that will be shown on the tooltip.
/// </summary>
public string Title {
get {
return this.title;
}
set {
if (String.IsNullOrEmpty(value))
this.title = String.Empty;
else
if (value.Length >= 100)
this.title = value.Substring(0, 99);
else
this.title = value;
NativeMethods.SendMessageString(this.Handle, TTM_SETTITLE, (int)this.standardIcon, this.title);
}
}
private string title;
/// <summary>
/// Get or set the icon that will be shown on the tooltip.
/// </summary>
public StandardIcons StandardIcon {
get {
return this.standardIcon;
}
set {
this.standardIcon = value;
NativeMethods.SendMessageString(this.Handle, TTM_SETTITLE, (int)this.standardIcon, this.title);
}
}
private StandardIcons standardIcon;
/// <summary>
/// Gets or sets the font that will be used to draw this control.
/// is still.
/// </summary>
/// <remarks>Setting this to null reverts to the default font.</remarks>
public Font Font {
get {
IntPtr hfont = NativeMethods.SendMessage(this.Handle, WM_GETFONT, 0, 0);
if (hfont == IntPtr.Zero)
return Control.DefaultFont;
else
return Font.FromHfont(hfont);
}
set {
Font newFont = value ?? Control.DefaultFont;
if (newFont == this.font)
return;
this.font = newFont;
IntPtr hfont = this.font.ToHfont(); // THINK: When should we delete this hfont?
NativeMethods.SendMessage(this.Handle, WM_SETFONT, hfont, 0);
}
}
private Font font;
/// <summary>
/// Gets or sets how many milliseconds the tooltip will remain visible while the mouse
/// is still.
/// </summary>
public int AutoPopDelay {
get { return this.GetDelayTime(TTDT_AUTOPOP); }
set { this.SetDelayTime(TTDT_AUTOPOP, value); }
}
/// <summary>
/// Gets or sets how many milliseconds the mouse must be still before the tooltip is shown.
/// </summary>
public int InitialDelay {
get { return this.GetDelayTime(TTDT_INITIAL); }
set { this.SetDelayTime(TTDT_INITIAL, value); }
}
/// <summary>
/// Gets or sets how many milliseconds the mouse must be still before the tooltip is shown again.
/// </summary>
public int ReshowDelay {
get { return this.GetDelayTime(TTDT_RESHOW); }
set { this.SetDelayTime(TTDT_RESHOW, value); }
}
private int GetDelayTime(int which) {
return (int)NativeMethods.SendMessage(this.Handle, TTM_GETDELAYTIME, which, 0);
}
private void SetDelayTime(int which, int value) {
NativeMethods.SendMessage(this.Handle, TTM_SETDELAYTIME, which, value);
}
#endregion
#region Commands
/// <summary>
/// Create the underlying control.
/// </summary>
/// <param name="parentHandle">The parent of the tooltip</param>
/// <remarks>This does nothing if the control has already been created</remarks>
public void Create(IntPtr parentHandle) {
if (this.Handle != IntPtr.Zero)
return;
CreateParams cp = new CreateParams();
cp.ClassName = "tooltips_class32";
cp.Style = TTS_NOPREFIX;
cp.ExStyle = WS_EX_TOPMOST;
cp.Parent = parentHandle;
this.CreateHandle(cp);
// Ensure that multiline tooltips work correctly
this.SetMaxWidth();
}
/// <summary>
/// Take a copy of the current settings and restore them when the
/// tooltip is poppped.
/// </summary>
/// <remarks>
/// This call cannot be nested. Subsequent calls to this method will be ignored
/// until PopSettings() is called.
/// </remarks>
public void PushSettings() {
// Ignore any nested calls
if (this.settings != null)
return;
this.settings = new Hashtable();
this.settings["IsBalloon"] = this.IsBalloon;
this.settings["HasBorder"] = this.HasBorder;
this.settings["BackColor"] = this.BackColor;
this.settings["ForeColor"] = this.ForeColor;
this.settings["Title"] = this.Title;
this.settings["StandardIcon"] = this.StandardIcon;
this.settings["AutoPopDelay"] = this.AutoPopDelay;
this.settings["InitialDelay"] = this.InitialDelay;
this.settings["ReshowDelay"] = this.ReshowDelay;
this.settings["Font"] = this.Font;
}
private Hashtable settings;
/// <summary>
/// Restore the settings of the tooltip as they were when PushSettings()
/// was last called.
/// </summary>
public void PopSettings() {
if (this.settings == null)
return;
this.IsBalloon = (bool)this.settings["IsBalloon"];
this.HasBorder = (bool)this.settings["HasBorder"];
this.BackColor = (Color)this.settings["BackColor"];
this.ForeColor = (Color)this.settings["ForeColor"];
this.Title = (string)this.settings["Title"];
this.StandardIcon = (StandardIcons)this.settings["StandardIcon"];
this.AutoPopDelay = (int)this.settings["AutoPopDelay"];
this.InitialDelay = (int)this.settings["InitialDelay"];
this.ReshowDelay = (int)this.settings["ReshowDelay"];
this.Font = (Font)this.settings["Font"];
this.settings = null;
}
/// <summary>
/// Add the given window to those for whom this tooltip will show tips
/// </summary>
/// <param name="window">The window</param>
public void AddTool(IWin32Window window) {
NativeMethods.TOOLINFO lParam = this.MakeToolInfoStruct(window);
NativeMethods.SendMessageTOOLINFO(this.Handle, TTM_ADDTOOL, 0, lParam);
}
/// <summary>
/// Hide any currently visible tooltip
/// </summary>
/// <param name="window"></param>
public void PopToolTip(IWin32Window window) {
NativeMethods.SendMessage(this.Handle, TTM_POP, 0, 0);
}
//public void Munge() {
// NativeMethods.TOOLINFO tool = new NativeMethods.TOOLINFO();
// IntPtr result = NativeMethods.SendMessageTOOLINFO(this.Handle, TTM_GETCURRENTTOOL, 0, tool);
// System.Diagnostics.Trace.WriteLine("-");
// System.Diagnostics.Trace.WriteLine(result);
// result = NativeMethods.SendMessageTOOLINFO(this.Handle, TTM_GETBUBBLESIZE, 0, tool);
// System.Diagnostics.Trace.WriteLine(String.Format("{0} {1}", result.ToInt32() >> 16, result.ToInt32() & 0xFFFF));
// NativeMethods.ChangeSize(this, result.ToInt32() & 0xFFFF, result.ToInt32() >> 16);
// //NativeMethods.RECT r = new NativeMethods.RECT();
// //r.right
// //IntPtr x = NativeMethods.SendMessageRECT(this.Handle, TTM_ADJUSTRECT, true, ref r);
// //System.Diagnostics.Trace.WriteLine(String.Format("{0} {1} {2} {3}", r.left, r.top, r.right, r.bottom));
//}
/// <summary>
/// Remove the given window from those managed by this tooltip
/// </summary>
/// <param name="window"></param>
public void RemoveToolTip(IWin32Window window) {
NativeMethods.TOOLINFO lParam = this.MakeToolInfoStruct(window);
NativeMethods.SendMessageTOOLINFO(this.Handle, TTM_DELTOOL, 0, lParam);
}
/// <summary>
/// Set the maximum width of a tooltip string.
/// </summary>
public void SetMaxWidth() {
this.SetMaxWidth(SystemInformation.MaxWindowTrackSize.Width);
}
/// <summary>
/// Set the maximum width of a tooltip string.
/// </summary>
/// <remarks>Setting this ensures that line breaks in the tooltip are honoured.</remarks>
public void SetMaxWidth(int maxWidth) {
NativeMethods.SendMessage(this.Handle, TTM_SETMAXTIPWIDTH, 0, maxWidth);
}
#endregion
#region Implementation
/// <summary>
/// Make a TOOLINFO structure for the given window
/// </summary>
/// <param name="window"></param>
/// <returns>A filled in TOOLINFO</returns>
private NativeMethods.TOOLINFO MakeToolInfoStruct(IWin32Window window) {
NativeMethods.TOOLINFO toolinfo_tooltip = new NativeMethods.TOOLINFO();
toolinfo_tooltip.hwnd = window.Handle;
toolinfo_tooltip.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
toolinfo_tooltip.uId = window.Handle;
toolinfo_tooltip.lpszText = (IntPtr)(-1); // LPSTR_TEXTCALLBACK
return toolinfo_tooltip;
}
/// <summary>
/// Handle a WmNotify message
/// </summary>
/// <param name="msg">The msg</param>
/// <returns>True if the message has been handled</returns>
protected virtual bool HandleNotify(ref Message msg) {
//THINK: What do we have to do here? Nothing it seems :)
//NativeMethods.NMHEADER nmheader = (NativeMethods.NMHEADER)msg.GetLParam(typeof(NativeMethods.NMHEADER));
//System.Diagnostics.Trace.WriteLine("HandleNotify");
//System.Diagnostics.Trace.WriteLine(nmheader.nhdr.code);
//switch (nmheader.nhdr.code) {
//}
return false;
}
/// <summary>
/// Handle a get display info message
/// </summary>
/// <param name="msg">The msg</param>
/// <returns>True if the message has been handled</returns>
public virtual bool HandleGetDispInfo(ref Message msg) {
//System.Diagnostics.Trace.WriteLine("HandleGetDispInfo");
this.SetMaxWidth();
ToolTipShowingEventArgs args = new ToolTipShowingEventArgs();
args.ToolTipControl = this;
this.OnShowing(args);
if (String.IsNullOrEmpty(args.Text))
return false;
this.ApplyEventFormatting(args);
NativeMethods.NMTTDISPINFO dispInfo = (NativeMethods.NMTTDISPINFO)msg.GetLParam(typeof(NativeMethods.NMTTDISPINFO));
dispInfo.lpszText = args.Text;
dispInfo.hinst = IntPtr.Zero;
if (args.RightToLeft == RightToLeft.Yes)
dispInfo.uFlags |= TTF_RTLREADING;
Marshal.StructureToPtr(dispInfo, msg.LParam, false);
return true;
}
private void ApplyEventFormatting(ToolTipShowingEventArgs args) {
if (!args.IsBalloon.HasValue &&
!args.BackColor.HasValue &&
!args.ForeColor.HasValue &&
args.Title == null &&
!args.StandardIcon.HasValue &&
!args.AutoPopDelay.HasValue &&
args.Font == null)
return;
this.PushSettings();
if (args.IsBalloon.HasValue)
this.IsBalloon = args.IsBalloon.Value;
if (args.BackColor.HasValue)
this.BackColor = args.BackColor.Value;
if (args.ForeColor.HasValue)
this.ForeColor = args.ForeColor.Value;
if (args.StandardIcon.HasValue)
this.StandardIcon = args.StandardIcon.Value;
if (args.AutoPopDelay.HasValue)
this.AutoPopDelay = args.AutoPopDelay.Value;
if (args.Font != null)
this.Font = args.Font;
if (args.Title != null)
this.Title = args.Title;
}
/// <summary>
/// Handle a TTN_LINKCLICK message
/// </summary>
/// <param name="msg">The msg</param>
/// <returns>True if the message has been handled</returns>
/// <remarks>This cannot call base.WndProc() since the msg may have come from another control.</remarks>
public virtual bool HandleLinkClick(ref Message msg) {
//System.Diagnostics.Trace.WriteLine("HandleLinkClick");
return false;
}
/// <summary>
/// Handle a TTN_POP message
/// </summary>
/// <param name="msg">The msg</param>
/// <returns>True if the message has been handled</returns>
/// <remarks>This cannot call base.WndProc() since the msg may have come from another control.</remarks>
public virtual bool HandlePop(ref Message msg) {
//System.Diagnostics.Trace.WriteLine("HandlePop");
this.PopSettings();
return true;
}
/// <summary>
/// Handle a TTN_SHOW message
/// </summary>
/// <param name="msg">The msg</param>
/// <returns>True if the message has been handled</returns>
/// <remarks>This cannot call base.WndProc() since the msg may have come from another control.</remarks>
public virtual bool HandleShow(ref Message msg) {
//System.Diagnostics.Trace.WriteLine("HandleShow");
return false;
}
/// <summary>
/// Handle a reflected notify message
/// </summary>
/// <param name="msg">The msg</param>
/// <returns>True if the message has been handled</returns>
protected virtual bool HandleReflectNotify(ref Message msg) {
NativeMethods.NMHEADER nmheader = (NativeMethods.NMHEADER)msg.GetLParam(typeof(NativeMethods.NMHEADER));
switch (nmheader.nhdr.code) {
case TTN_SHOW:
//System.Diagnostics.Trace.WriteLine("reflect TTN_SHOW");
if (this.HandleShow(ref msg))
return true;
break;
case TTN_POP:
//System.Diagnostics.Trace.WriteLine("reflect TTN_POP");
if (this.HandlePop(ref msg))
return true;
break;
case TTN_LINKCLICK:
//System.Diagnostics.Trace.WriteLine("reflect TTN_LINKCLICK");
if (this.HandleLinkClick(ref msg))
return true;
break;
case TTN_GETDISPINFO:
//System.Diagnostics.Trace.WriteLine("reflect TTN_GETDISPINFO");
if (this.HandleGetDispInfo(ref msg))
return true;
break;
}
return false;
}
/// <summary>
/// Mess with the basic message pump of the tooltip
/// </summary>
/// <param name="msg"></param>
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
override protected void WndProc(ref Message msg) {
//System.Diagnostics.Trace.WriteLine(String.Format("xx {0:x}", msg.Msg));
switch (msg.Msg) {
case 0x4E: // WM_NOTIFY
if (!this.HandleNotify(ref msg))
return;
break;
case 0x204E: // WM_REFLECT_NOTIFY
if (!this.HandleReflectNotify(ref msg))
return;
break;
}
base.WndProc(ref msg);
}
#endregion
#region Events
/// <summary>
/// Tell the world that a tooltip is about to show
/// </summary>
public event EventHandler<ToolTipShowingEventArgs> Showing;
/// <summary>
/// Tell the world that a tooltip is about to disappear
/// </summary>
public event EventHandler<EventArgs> Pop;
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected virtual void OnShowing(ToolTipShowingEventArgs e) {
if (this.Showing != null)
this.Showing(this, e);
}
/// <summary>
///
/// </summary>
/// <param name="e"></param>
protected virtual void OnPop(EventArgs e) {
if (this.Pop != null)
this.Pop(this, e);
}
#endregion
}
}

2128
TreeListView.cs Normal file

File diff suppressed because it is too large Load Diff

190
Utilities/ColumnSelectionForm.Designer.cs generated Normal file
View File

@ -0,0 +1,190 @@
namespace BrightIdeasSoftware
{
partial class ColumnSelectionForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.buttonMoveUp = new System.Windows.Forms.Button();
this.buttonMoveDown = new System.Windows.Forms.Button();
this.buttonShow = new System.Windows.Forms.Button();
this.buttonHide = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.buttonOK = new System.Windows.Forms.Button();
this.buttonCancel = new System.Windows.Forms.Button();
this.objectListView1 = new BrightIdeasSoftware.ObjectListView();
this.olvColumn1 = new BrightIdeasSoftware.OLVColumn();
((System.ComponentModel.ISupportInitialize)(this.objectListView1)).BeginInit();
this.SuspendLayout();
//
// buttonMoveUp
//
this.buttonMoveUp.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonMoveUp.Location = new System.Drawing.Point(295, 31);
this.buttonMoveUp.Name = "buttonMoveUp";
this.buttonMoveUp.Size = new System.Drawing.Size(87, 23);
this.buttonMoveUp.TabIndex = 1;
this.buttonMoveUp.Text = "Move &Up";
this.buttonMoveUp.UseVisualStyleBackColor = true;
this.buttonMoveUp.Click += new System.EventHandler(this.buttonMoveUp_Click);
//
// buttonMoveDown
//
this.buttonMoveDown.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonMoveDown.Location = new System.Drawing.Point(295, 60);
this.buttonMoveDown.Name = "buttonMoveDown";
this.buttonMoveDown.Size = new System.Drawing.Size(87, 23);
this.buttonMoveDown.TabIndex = 2;
this.buttonMoveDown.Text = "Move &Down";
this.buttonMoveDown.UseVisualStyleBackColor = true;
this.buttonMoveDown.Click += new System.EventHandler(this.buttonMoveDown_Click);
//
// buttonShow
//
this.buttonShow.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonShow.Location = new System.Drawing.Point(295, 89);
this.buttonShow.Name = "buttonShow";
this.buttonShow.Size = new System.Drawing.Size(87, 23);
this.buttonShow.TabIndex = 3;
this.buttonShow.Text = "&Show";
this.buttonShow.UseVisualStyleBackColor = true;
this.buttonShow.Click += new System.EventHandler(this.buttonShow_Click);
//
// buttonHide
//
this.buttonHide.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonHide.Location = new System.Drawing.Point(295, 118);
this.buttonHide.Name = "buttonHide";
this.buttonHide.Size = new System.Drawing.Size(87, 23);
this.buttonHide.TabIndex = 4;
this.buttonHide.Text = "&Hide";
this.buttonHide.UseVisualStyleBackColor = true;
this.buttonHide.Click += new System.EventHandler(this.buttonHide_Click);
//
// label1
//
this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.label1.BackColor = System.Drawing.SystemColors.Control;
this.label1.Location = new System.Drawing.Point(13, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(366, 19);
this.label1.TabIndex = 5;
this.label1.Text = "Choose the columns you want to see in this list. ";
//
// buttonOK
//
this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonOK.Location = new System.Drawing.Point(198, 304);
this.buttonOK.Name = "buttonOK";
this.buttonOK.Size = new System.Drawing.Size(87, 23);
this.buttonOK.TabIndex = 6;
this.buttonOK.Text = "&OK";
this.buttonOK.UseVisualStyleBackColor = true;
this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
//
// buttonCancel
//
this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.buttonCancel.Location = new System.Drawing.Point(295, 304);
this.buttonCancel.Name = "buttonCancel";
this.buttonCancel.Size = new System.Drawing.Size(87, 23);
this.buttonCancel.TabIndex = 7;
this.buttonCancel.Text = "&Cancel";
this.buttonCancel.UseVisualStyleBackColor = true;
this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
//
// objectListView1
//
this.objectListView1.AllColumns.Add(this.olvColumn1);
this.objectListView1.AlternateRowBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
this.objectListView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.objectListView1.CellEditActivation = BrightIdeasSoftware.ObjectListView.CellEditActivateMode.SingleClick;
this.objectListView1.CheckBoxes = true;
this.objectListView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.olvColumn1});
this.objectListView1.FullRowSelect = true;
this.objectListView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
this.objectListView1.HideSelection = false;
this.objectListView1.Location = new System.Drawing.Point(12, 31);
this.objectListView1.MultiSelect = false;
this.objectListView1.Name = "objectListView1";
this.objectListView1.ShowGroups = false;
this.objectListView1.ShowSortIndicators = false;
this.objectListView1.Size = new System.Drawing.Size(273, 259);
this.objectListView1.TabIndex = 0;
this.objectListView1.UseCompatibleStateImageBehavior = false;
this.objectListView1.View = System.Windows.Forms.View.Details;
this.objectListView1.SelectionChanged += new System.EventHandler(this.objectListView1_SelectionChanged);
//
// olvColumn1
//
this.olvColumn1.AspectName = "Text";
this.olvColumn1.IsVisible = true;
this.olvColumn1.Text = "Column";
this.olvColumn1.Width = 267;
//
// ColumnSelectionForm
//
this.AcceptButton = this.buttonOK;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.buttonCancel;
this.ClientSize = new System.Drawing.Size(391, 339);
this.Controls.Add(this.buttonCancel);
this.Controls.Add(this.buttonOK);
this.Controls.Add(this.label1);
this.Controls.Add(this.buttonHide);
this.Controls.Add(this.buttonShow);
this.Controls.Add(this.buttonMoveDown);
this.Controls.Add(this.buttonMoveUp);
this.Controls.Add(this.objectListView1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "ColumnSelectionForm";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.Text = "Column Selection";
((System.ComponentModel.ISupportInitialize)(this.objectListView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private BrightIdeasSoftware.ObjectListView objectListView1;
private System.Windows.Forms.Button buttonMoveUp;
private System.Windows.Forms.Button buttonMoveDown;
private System.Windows.Forms.Button buttonShow;
private System.Windows.Forms.Button buttonHide;
private BrightIdeasSoftware.OLVColumn olvColumn1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button buttonOK;
private System.Windows.Forms.Button buttonCancel;
}
}

View File

@ -0,0 +1,263 @@
/*
* ColumnSelectionForm - A utility form that allows columns to be rearranged and/or hidden
*
* Author: Phillip Piper
* Date: 1/04/2011 11:15 AM
*
* Change log:
* 2013-04-21 JPP - Fixed obscure bug in column re-ordered. Thanks to Edwin Chen.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace BrightIdeasSoftware
{
/// <summary>
/// This form is an example of how an application could allows the user to select which columns
/// an ObjectListView will display, as well as select which order the columns are displayed in.
/// </summary>
/// <remarks>
/// <para>In Tile view, ColumnHeader.DisplayIndex does nothing. To reorder the columns you have
/// to change the order of objects in the Columns property.</para>
/// <para>Remember that the first column is special!
/// It has to remain the first column.</para>
/// </remarks>
public partial class ColumnSelectionForm : Form
{
/// <summary>
/// Make a new ColumnSelectionForm
/// </summary>
public ColumnSelectionForm()
{
InitializeComponent();
}
/// <summary>
/// Open this form so it will edit the columns that are available in the listview's current view
/// </summary>
/// <param name="olv">The ObjectListView whose columns are to be altered</param>
public void OpenOn(ObjectListView olv)
{
this.OpenOn(olv, olv.View);
}
/// <summary>
/// Open this form so it will edit the columns that are available in the given listview
/// when the listview is showing the given type of view.
/// </summary>
/// <param name="olv">The ObjectListView whose columns are to be altered</param>
/// <param name="view">The view that is to be altered. Must be View.Details or View.Tile</param>
public void OpenOn(ObjectListView olv, View view)
{
if (view != View.Details && view != View.Tile)
return;
this.InitializeForm(olv, view);
if (this.ShowDialog() == DialogResult.OK)
this.Apply(olv, view);
}
/// <summary>
/// Initialize the form to show the columns of the given view
/// </summary>
/// <param name="olv"></param>
/// <param name="view"></param>
protected void InitializeForm(ObjectListView olv, View view)
{
this.AllColumns = olv.AllColumns;
this.RearrangableColumns = new List<OLVColumn>(this.AllColumns);
foreach (OLVColumn col in this.RearrangableColumns) {
if (view == View.Details)
this.MapColumnToVisible[col] = col.IsVisible;
else
this.MapColumnToVisible[col] = col.IsTileViewColumn;
}
this.RearrangableColumns.Sort(new SortByDisplayOrder(this));
this.objectListView1.BooleanCheckStateGetter = delegate(Object rowObject) {
return this.MapColumnToVisible[(OLVColumn)rowObject];
};
this.objectListView1.BooleanCheckStatePutter = delegate(Object rowObject, bool newValue) {
// Some columns should always be shown, so ignore attempts to hide them
OLVColumn column = (OLVColumn)rowObject;
if (!column.CanBeHidden)
return true;
this.MapColumnToVisible[column] = newValue;
EnableControls();
return newValue;
};
this.objectListView1.SetObjects(this.RearrangableColumns);
this.EnableControls();
}
private List<OLVColumn> AllColumns = null;
private List<OLVColumn> RearrangableColumns = new List<OLVColumn>();
private Dictionary<OLVColumn, bool> MapColumnToVisible = new Dictionary<OLVColumn, bool>();
/// <summary>
/// The user has pressed OK. Do what's requied.
/// </summary>
/// <param name="olv"></param>
/// <param name="view"></param>
protected void Apply(ObjectListView olv, View view)
{
olv.Freeze();
// Update the column definitions to reflect whether they have been hidden
if (view == View.Details) {
foreach (OLVColumn col in olv.AllColumns)
col.IsVisible = this.MapColumnToVisible[col];
} else {
foreach (OLVColumn col in olv.AllColumns)
col.IsTileViewColumn = this.MapColumnToVisible[col];
}
// Collect the columns are still visible
List<OLVColumn> visibleColumns = this.RearrangableColumns.FindAll(
delegate(OLVColumn x) { return this.MapColumnToVisible[x]; });
// Detail view and Tile view have to be handled in different ways.
if (view == View.Details) {
// Of the still visible columns, change DisplayIndex to reflect their position in the rearranged list
olv.ChangeToFilteredColumns(view);
foreach (OLVColumn col in visibleColumns) {
col.DisplayIndex = visibleColumns.IndexOf((OLVColumn)col);
col.LastDisplayIndex = col.DisplayIndex;
}
} else {
// In Tile view, DisplayOrder does nothing. So to change the display order, we have to change the
// order of the columns in the Columns property.
// Remember, the primary column is special and has to remain first!
OLVColumn primaryColumn = this.AllColumns[0];
visibleColumns.Remove(primaryColumn);
olv.Columns.Clear();
olv.Columns.Add(primaryColumn);
olv.Columns.AddRange(visibleColumns.ToArray());
olv.CalculateReasonableTileSize();
}
olv.Unfreeze();
}
#region Event handlers
private void buttonMoveUp_Click(object sender, EventArgs e)
{
int selectedIndex = this.objectListView1.SelectedIndices[0];
OLVColumn col = this.RearrangableColumns[selectedIndex];
this.RearrangableColumns.RemoveAt(selectedIndex);
this.RearrangableColumns.Insert(selectedIndex-1, col);
this.objectListView1.BuildList();
EnableControls();
}
private void buttonMoveDown_Click(object sender, EventArgs e)
{
int selectedIndex = this.objectListView1.SelectedIndices[0];
OLVColumn col = this.RearrangableColumns[selectedIndex];
this.RearrangableColumns.RemoveAt(selectedIndex);
this.RearrangableColumns.Insert(selectedIndex + 1, col);
this.objectListView1.BuildList();
EnableControls();
}
private void buttonShow_Click(object sender, EventArgs e)
{
this.objectListView1.SelectedItem.Checked = true;
}
private void buttonHide_Click(object sender, EventArgs e)
{
this.objectListView1.SelectedItem.Checked = false;
}
private void buttonOK_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
private void buttonCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void objectListView1_SelectionChanged(object sender, EventArgs e)
{
EnableControls();
}
#endregion
#region Control enabling
/// <summary>
/// Enable the controls on the dialog to match the current state
/// </summary>
protected void EnableControls()
{
if (this.objectListView1.SelectedIndices.Count == 0) {
this.buttonMoveUp.Enabled = false;
this.buttonMoveDown.Enabled = false;
this.buttonShow.Enabled = false;
this.buttonHide.Enabled = false;
} else {
// Can't move the first row up or the last row down
this.buttonMoveUp.Enabled = (this.objectListView1.SelectedIndices[0] != 0);
this.buttonMoveDown.Enabled = (this.objectListView1.SelectedIndices[0] < (this.objectListView1.GetItemCount() - 1));
OLVColumn selectedColumn = (OLVColumn)this.objectListView1.SelectedObject;
// Some columns cannot be hidden (and hence cannot be Shown)
this.buttonShow.Enabled = !this.MapColumnToVisible[selectedColumn] && selectedColumn.CanBeHidden;
this.buttonHide.Enabled = this.MapColumnToVisible[selectedColumn] && selectedColumn.CanBeHidden;
}
}
#endregion
/// <summary>
/// A Comparer that will sort a list of columns so that visible ones come before hidden ones,
/// and that are ordered by their display order.
/// </summary>
private class SortByDisplayOrder : IComparer<OLVColumn>
{
public SortByDisplayOrder(ColumnSelectionForm form)
{
this.Form = form;
}
private ColumnSelectionForm Form;
#region IComparer<OLVColumn> Members
int IComparer<OLVColumn>.Compare(OLVColumn x, OLVColumn y)
{
if (this.Form.MapColumnToVisible[x] && !this.Form.MapColumnToVisible[y])
return -1;
if (!this.Form.MapColumnToVisible[x] && this.Form.MapColumnToVisible[y])
return 1;
if (x.DisplayIndex == y.DisplayIndex)
return x.Text.CompareTo(y.Text);
else
return x.DisplayIndex - y.DisplayIndex;
}
#endregion
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

561
Utilities/Generator.cs Normal file
View File

@ -0,0 +1,561 @@
/*
* Generator - Utility methods that generate columns or methods
*
* Author: Phillip Piper
* Date: 15/08/2009 22:37
*
* Change log:
* 2012-08-16 JPP - Generator now considers [OLVChildren] and [OLVIgnore] attributes.
* 2012-06-14 JPP - Allow columns to be generated even if they are not marked with [OLVColumn]
* - Converted class from static to instance to allow it to be subclassed.
* Also, added IGenerator to allow it to be completely reimplemented.
* v2.5.1
* 2010-11-01 JPP - DisplayIndex is now set correctly for columns that lack that attribute
* v2.4.1
* 2010-08-25 JPP - Generator now also resets sort columns
* v2.4
* 2010-04-14 JPP - Allow Name property to be set
* - Don't double set the Text property
* v2.3
* 2009-08-15 JPP - Initial version
*
* To do:
*
* Copyright (C) 2009-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Reflection.Emit;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace BrightIdeasSoftware
{
/// <summary>
/// An object that implements the IGenerator interface provides the ability
/// to dynamically create columns
/// for an ObjectListView based on the characteristics of a given collection
/// of model objects.
/// </summary>
public interface IGenerator {
/// <summary>
/// Generate columns into the given ObjectListView that come from the given
/// model object type.
/// </summary>
/// <param name="olv">The ObjectListView to modify</param>
/// <param name="type">The model type whose attributes will be considered.</param>
/// <param name="allProperties">Will columns be generated for properties that are not marked with [OLVColumn].</param>
void GenerateAndReplaceColumns(ObjectListView olv, Type type, bool allProperties);
/// <summary>
/// Generate a list of OLVColumns based on the attributes of the given type
/// If allProperties to true, all public properties will have a matching column generated.
/// If allProperties is false, only properties that have a OLVColumn attribute will have a column generated.
/// </summary>
/// <param name="type"></param>
/// <param name="allProperties">Will columns be generated for properties that are not marked with [OLVColumn].</param>
/// <returns>A collection of OLVColumns matching the attributes of Type that have OLVColumnAttributes.</returns>
IList<OLVColumn> GenerateColumns(Type type, bool allProperties);
}
/// <summary>
/// The Generator class provides methods to dynamically create columns
/// for an ObjectListView based on the characteristics of a given collection
/// of model objects.
/// </summary>
/// <remarks>
/// <para>For a given type, a Generator can create columns to match the public properties
/// of that type. The generator can consider all public properties or only those public properties marked with
/// [OLVColumn] attribute.</para>
/// </remarks>
public class Generator : IGenerator {
#region Static convenience methods
/// <summary>
/// Gets or sets the actual generator used by the static convinence methods.
/// </summary>
/// <remarks>If you subclass the standard generator or implement IGenerator yourself,
/// you should install an instance of your subclass/implementation here.</remarks>
public static IGenerator Instance {
get { return Generator.instance ?? (Generator.instance = new Generator()); }
set { Generator.instance = value; }
}
private static IGenerator instance;
/// <summary>
/// Replace all columns of the given ObjectListView with columns generated
/// from the first member of the given enumerable. If the enumerable is
/// empty or null, the ObjectListView will be cleared.
/// </summary>
/// <param name="olv">The ObjectListView to modify</param>
/// <param name="enumerable">The collection whose first element will be used to generate columns.</param>
static public void GenerateColumns(ObjectListView olv, IEnumerable enumerable) {
Generator.GenerateColumns(olv, enumerable, false);
}
/// <summary>
/// Replace all columns of the given ObjectListView with columns generated
/// from the first member of the given enumerable. If the enumerable is
/// empty or null, the ObjectListView will be cleared.
/// </summary>
/// <param name="olv">The ObjectListView to modify</param>
/// <param name="enumerable">The collection whose first element will be used to generate columns.</param>
/// <param name="allProperties">Will columns be generated for properties that are not marked with [OLVColumn].</param>
static public void GenerateColumns(ObjectListView olv, IEnumerable enumerable, bool allProperties) {
// Generate columns based on the type of the first model in the collection and then quit
if (enumerable != null) {
foreach (object model in enumerable) {
Generator.Instance.GenerateAndReplaceColumns(olv, model.GetType(), allProperties);
return;
}
}
// If we reach here, the collection was empty, so we clear the list
Generator.Instance.GenerateAndReplaceColumns(olv, null, allProperties);
}
/// <summary>
/// Generate columns into the given ObjectListView that come from the public properties of the given
/// model object type.
/// </summary>
/// <param name="olv">The ObjectListView to modify</param>
/// <param name="type">The model type whose attributes will be considered.</param>
static public void GenerateColumns(ObjectListView olv, Type type) {
Generator.Instance.GenerateAndReplaceColumns(olv, type, false);
}
/// <summary>
/// Generate columns into the given ObjectListView that come from the public properties of the given
/// model object type.
/// </summary>
/// <param name="olv">The ObjectListView to modify</param>
/// <param name="type">The model type whose attributes will be considered.</param>
/// <param name="allProperties">Will columns be generated for properties that are not marked with [OLVColumn].</param>
static public void GenerateColumns(ObjectListView olv, Type type, bool allProperties) {
Generator.Instance.GenerateAndReplaceColumns(olv, type, allProperties);
}
/// <summary>
/// Generate a list of OLVColumns based on the public properties of the given type
/// that have a OLVColumn attribute.
/// </summary>
/// <param name="type"></param>
/// <returns>A collection of OLVColumns matching the attributes of Type that have OLVColumnAttributes.</returns>
static public IList<OLVColumn> GenerateColumns(Type type) {
return Generator.Instance.GenerateColumns(type, false);
}
#endregion
#region Public interface
/// <summary>
/// Generate columns into the given ObjectListView that come from the given
/// model object type.
/// </summary>
/// <param name="olv">The ObjectListView to modify</param>
/// <param name="type">The model type whose attributes will be considered.</param>
/// <param name="allProperties">Will columns be generated for properties that are not marked with [OLVColumn].</param>
public virtual void GenerateAndReplaceColumns(ObjectListView olv, Type type, bool allProperties) {
IList<OLVColumn> columns = this.GenerateColumns(type, allProperties);
TreeListView tlv = olv as TreeListView;
if (tlv != null)
this.TryGenerateChildrenDelegates(tlv, type);
this.ReplaceColumns(olv, columns);
}
/// <summary>
/// Generate a list of OLVColumns based on the attributes of the given type
/// If allProperties to true, all public properties will have a matching column generated.
/// If allProperties is false, only properties that have a OLVColumn attribute will have a column generated.
/// </summary>
/// <param name="type"></param>
/// <param name="allProperties">Will columns be generated for properties that are not marked with [OLVColumn].</param>
/// <returns>A collection of OLVColumns matching the attributes of Type that have OLVColumnAttributes.</returns>
public virtual IList<OLVColumn> GenerateColumns(Type type, bool allProperties) {
List<OLVColumn> columns = new List<OLVColumn>();
// Sanity
if (type == null)
return columns;
// Iterate all public properties in the class and build columns from those that have
// an OLVColumn attribute and that are not ignored.
foreach (PropertyInfo pinfo in type.GetProperties()) {
if (Attribute.GetCustomAttribute(pinfo, typeof(OLVIgnoreAttribute)) != null)
continue;
OLVColumnAttribute attr = Attribute.GetCustomAttribute(pinfo, typeof(OLVColumnAttribute)) as OLVColumnAttribute;
if (attr == null) {
if (allProperties)
columns.Add(this.MakeColumnFromPropertyInfo(pinfo));
} else {
columns.Add(this.MakeColumnFromAttribute(pinfo, attr));
}
}
// How many columns have DisplayIndex specifically set?
int countPositiveDisplayIndex = 0;
foreach (OLVColumn col in columns) {
if (col.DisplayIndex >= 0)
countPositiveDisplayIndex += 1;
}
// Give columns that don't have a DisplayIndex an incremental index
int columnIndex = countPositiveDisplayIndex;
foreach (OLVColumn col in columns)
if (col.DisplayIndex < 0)
col.DisplayIndex = (columnIndex++);
columns.Sort(delegate(OLVColumn x, OLVColumn y) {
return x.DisplayIndex.CompareTo(y.DisplayIndex);
});
return columns;
}
#endregion
#region Implementation
/// <summary>
/// Replace all the columns in the given listview with the given list of columns.
/// </summary>
/// <param name="olv"></param>
/// <param name="columns"></param>
protected virtual void ReplaceColumns(ObjectListView olv, IList<OLVColumn> columns) {
olv.Reset();
// Are there new columns to add?
if (columns == null || columns.Count == 0)
return;
// Setup the columns
olv.AllColumns.AddRange(columns);
this.PostCreateColumns(olv);
}
/// <summary>
/// Post process columns after creating them and adding them to the AllColumns collection.
/// </summary>
/// <param name="olv"></param>
public virtual void PostCreateColumns(ObjectListView olv) {
if (olv.AllColumns.Exists(delegate(OLVColumn x) { return x.CheckBoxes; }))
olv.UseSubItemCheckBoxes = true;
if (olv.AllColumns.Exists(delegate(OLVColumn x) { return x.Index > 0 && (x.ImageGetter != null || !String.IsNullOrEmpty(x.ImageAspectName)); }))
olv.ShowImagesOnSubItems = true;
olv.RebuildColumns();
olv.AutoSizeColumns();
}
/// <summary>
/// Create a column from the given PropertyInfo and OLVColumn attribute
/// </summary>
/// <param name="pinfo"></param>
/// <param name="attr"></param>
/// <returns></returns>
protected virtual OLVColumn MakeColumnFromAttribute(PropertyInfo pinfo, OLVColumnAttribute attr) {
return MakeColumn(pinfo.Name, DisplayNameToColumnTitle(pinfo.Name), pinfo.CanWrite, pinfo.PropertyType, attr);
}
/// <summary>
/// Make a column from the given PropertyInfo
/// </summary>
/// <param name="pinfo"></param>
/// <returns></returns>
protected virtual OLVColumn MakeColumnFromPropertyInfo(PropertyInfo pinfo) {
return MakeColumn(pinfo.Name, DisplayNameToColumnTitle(pinfo.Name), pinfo.CanWrite, pinfo.PropertyType, null);
}
/// <summary>
/// Make a column from the given PropertyDescriptor
/// </summary>
/// <param name="pd"></param>
/// <returns></returns>
public virtual OLVColumn MakeColumnFromPropertyDescriptor(PropertyDescriptor pd) {
OLVColumnAttribute attr = pd.Attributes[typeof(OLVColumnAttribute)] as OLVColumnAttribute;
return MakeColumn(pd.Name, DisplayNameToColumnTitle(pd.DisplayName), !pd.IsReadOnly, pd.PropertyType, attr);
}
/// <summary>
/// Create a column with all the given information
/// </summary>
/// <param name="aspectName"></param>
/// <param name="title"></param>
/// <param name="editable"></param>
/// <param name="propertyType"></param>
/// <param name="attr"></param>
/// <returns></returns>
protected virtual OLVColumn MakeColumn(string aspectName, string title, bool editable, Type propertyType, OLVColumnAttribute attr) {
OLVColumn column = this.MakeColumn(aspectName, title, attr);
column.Name = (attr == null || String.IsNullOrEmpty(attr.Name)) ? aspectName : attr.Name;
this.ConfigurePossibleBooleanColumn(column, propertyType);
if (attr == null) {
column.IsEditable = editable;
return column;
}
column.AspectToStringFormat = attr.AspectToStringFormat;
if (attr.IsCheckBoxesSet)
column.CheckBoxes = attr.CheckBoxes;
column.DisplayIndex = attr.DisplayIndex;
column.FillsFreeSpace = attr.FillsFreeSpace;
if (attr.IsFreeSpaceProportionSet)
column.FreeSpaceProportion = attr.FreeSpaceProportion;
column.GroupWithItemCountFormat = attr.GroupWithItemCountFormat;
column.GroupWithItemCountSingularFormat = attr.GroupWithItemCountSingularFormat;
column.Hyperlink = attr.Hyperlink;
column.ImageAspectName = attr.ImageAspectName;
column.IsEditable = attr.IsEditableSet ? attr.IsEditable : editable;
column.IsTileViewColumn = attr.IsTileViewColumn;
column.IsVisible = attr.IsVisible;
column.MaximumWidth = attr.MaximumWidth;
column.MinimumWidth = attr.MinimumWidth;
column.Tag = attr.Tag;
if (attr.IsTextAlignSet)
column.TextAlign = attr.TextAlign;
column.ToolTipText = attr.ToolTipText;
if (attr.IsTriStateCheckBoxesSet)
column.TriStateCheckBoxes = attr.TriStateCheckBoxes;
column.UseInitialLetterForGroup = attr.UseInitialLetterForGroup;
column.Width = attr.Width;
if (attr.GroupCutoffs != null && attr.GroupDescriptions != null)
column.MakeGroupies(attr.GroupCutoffs, attr.GroupDescriptions);
return column;
}
/// <summary>
/// Create a column.
/// </summary>
/// <param name="aspectName"></param>
/// <param name="title"></param>
/// <param name="attr"></param>
/// <returns></returns>
protected virtual OLVColumn MakeColumn(string aspectName, string title, OLVColumnAttribute attr) {
string columnTitle = (attr == null || String.IsNullOrEmpty(attr.Title)) ? title : attr.Title;
return new OLVColumn(columnTitle, aspectName);
}
/// <summary>
/// Convert a property name to a displayable title.
/// </summary>
/// <param name="displayName"></param>
/// <returns></returns>
protected virtual string DisplayNameToColumnTitle(string displayName) {
string title = displayName.Replace("_", " ");
// Put a space between a lower-case letter that is followed immediately by an upper case letter
title = Regex.Replace(title, @"(\p{Ll})(\p{Lu})", @"$1 $2");
return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(title);
}
/// <summary>
/// Configure the given column to show a checkbox if appropriate
/// </summary>
/// <param name="column"></param>
/// <param name="propertyType"></param>
protected virtual void ConfigurePossibleBooleanColumn(OLVColumn column, Type propertyType) {
if (propertyType != typeof(bool) && propertyType != typeof(bool?) && propertyType != typeof(CheckState))
return;
column.CheckBoxes = true;
column.TextAlign = HorizontalAlignment.Center;
column.Width = 32;
column.TriStateCheckBoxes = (propertyType == typeof(bool?) || propertyType == typeof(CheckState));
}
/// <summary>
/// If this given type has an property marked with [OLVChildren], make delegates that will
/// traverse that property as the children of an instance of the model
/// </summary>
/// <param name="tlv"></param>
/// <param name="type"></param>
protected virtual void TryGenerateChildrenDelegates(TreeListView tlv, Type type) {
foreach (PropertyInfo pinfo in type.GetProperties()) {
OLVChildrenAttribute attr = Attribute.GetCustomAttribute(pinfo, typeof(OLVChildrenAttribute)) as OLVChildrenAttribute;
if (attr != null) {
this.GenerateChildrenDelegates(tlv, pinfo);
return;
}
}
}
/// <summary>
/// Generate CanExpand and ChildrenGetter delegates from the given property.
/// </summary>
/// <param name="tlv"></param>
/// <param name="pinfo"></param>
protected virtual void GenerateChildrenDelegates(TreeListView tlv, PropertyInfo pinfo) {
Munger childrenGetter = new Munger(pinfo.Name);
tlv.CanExpandGetter = delegate(object x) {
try {
IEnumerable result = childrenGetter.GetValueEx(x) as IEnumerable;
return !ObjectListView.IsEnumerableEmpty(result);
}
catch (MungerException ex) {
System.Diagnostics.Debug.WriteLine(ex);
return false;
}
};
tlv.ChildrenGetter = delegate(object x) {
try {
return childrenGetter.GetValueEx(x) as IEnumerable;
}
catch (MungerException ex) {
System.Diagnostics.Debug.WriteLine(ex);
return null;
}
};
}
#endregion
/*
#region Dynamic methods
/// <summary>
/// Generate methods so that reflection is not needed.
/// </summary>
/// <param name="olv"></param>
/// <param name="type"></param>
public static void GenerateMethods(ObjectListView olv, Type type) {
foreach (OLVColumn column in olv.Columns) {
GenerateColumnMethods(column, type);
}
}
public static void GenerateColumnMethods(OLVColumn column, Type type) {
if (column.AspectGetter == null && !String.IsNullOrEmpty(column.AspectName))
column.AspectGetter = Generator.GenerateAspectGetter(type, column.AspectName);
}
/// <summary>
/// Generates an aspect getter method dynamically. The method will execute
/// the given dotted chain of selectors against a model object given at runtime.
/// </summary>
/// <param name="type">The type of model object to be passed to the generated method</param>
/// <param name="path">A dotted chain of selectors. Each selector can be the name of a
/// field, property or parameter-less method.</param>
/// <returns>A typed delegate</returns>
/// <remarks>
/// <para>
/// If you have an AspectName of "Owner.Address.Postcode", this will generate
/// the equivilent of: <code>this.AspectGetter = delegate (object x) {
/// return x.Owner.Address.Postcode;
/// }
/// </code>
/// </para>
/// </remarks>
private static AspectGetterDelegate GenerateAspectGetter(Type type, string path) {
DynamicMethod getter = new DynamicMethod(String.Empty, typeof(Object), new Type[] { type }, type, true);
Generator.GenerateIL(type, path, getter.GetILGenerator());
return (AspectGetterDelegate)getter.CreateDelegate(typeof(AspectGetterDelegate));
}
/// <summary>
/// This method generates the actual IL for the method.
/// </summary>
/// <param name="type"></param>
/// <param name="path"></param>
/// <param name="il"></param>
private static void GenerateIL(Type modelType, string path, ILGenerator il) {
// Push our model object onto the stack
il.Emit(OpCodes.Ldarg_0);
OpCodes.Castclass
// Generate the IL to access each part of the dotted chain
Type type = modelType;
string[] parts = path.Split('.');
for (int i = 0; i < parts.Length; i++) {
type = Generator.GeneratePart(il, type, parts[i], (i == parts.Length - 1));
if (type == null)
break;
}
// If the object to be returned is a value type (e.g. int, bool), it
// must be boxed, since the delegate returns an Object
if (type != null && type.IsValueType && !modelType.IsValueType)
il.Emit(OpCodes.Box, type);
il.Emit(OpCodes.Ret);
}
private static Type GeneratePart(ILGenerator il, Type type, string pathPart, bool isLastPart) {
// TODO: Generate check for null
// Find the first member with the given nam that is a field, property, or parameter-less method
List<MemberInfo> infos = new List<MemberInfo>(type.GetMember(pathPart));
MemberInfo info = infos.Find(delegate(MemberInfo x) {
if (x.MemberType == MemberTypes.Field || x.MemberType == MemberTypes.Property)
return true;
if (x.MemberType == MemberTypes.Method)
return ((MethodInfo)x).GetParameters().Length == 0;
else
return false;
});
// If we couldn't find anything with that name, pop the current result and return an error
if (info == null) {
il.Emit(OpCodes.Pop);
il.Emit(OpCodes.Ldstr, String.Format("'{0}' is not a parameter-less method, property or field of type '{1}'", pathPart, type.FullName));
return null;
}
// Generate the correct IL to access the member. We remember the type of object that is going to be returned
// so that we can do a method lookup on it at the next iteration
Type resultType = null;
switch (info.MemberType) {
case MemberTypes.Method:
MethodInfo mi = (MethodInfo)info;
if (mi.IsVirtual)
il.Emit(OpCodes.Callvirt, mi);
else
il.Emit(OpCodes.Call, mi);
resultType = mi.ReturnType;
break;
case MemberTypes.Property:
PropertyInfo pi = (PropertyInfo)info;
il.Emit(OpCodes.Call, pi.GetGetMethod());
resultType = pi.PropertyType;
break;
case MemberTypes.Field:
FieldInfo fi = (FieldInfo)info;
il.Emit(OpCodes.Ldfld, fi);
resultType = fi.FieldType;
break;
}
// If the method returned a value type, and something is going to call a method on that value,
// we need to load its address onto the stack, rather than the object itself.
if (resultType.IsValueType && !isLastPart) {
LocalBuilder lb = il.DeclareLocal(resultType);
il.Emit(OpCodes.Stloc, lb);
il.Emit(OpCodes.Ldloca, lb);
}
return resultType;
}
#endregion
*/
}
}

277
Utilities/OLVExporter.cs Normal file
View File

@ -0,0 +1,277 @@
/*
* OLVExporter - Export the contents of an ObjectListView into various text-based formats
*
* Author: Phillip Piper
* Date: 7 August 2012, 10:35pm
*
* Change log:
* 2012-08-07 JPP Initial code
*
* Copyright (C) 2012 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace BrightIdeasSoftware {
/// <summary>
/// An OLVExporter converts a collection of rows from an ObjectListView
/// into a variety of textual formats.
/// </summary>
public class OLVExporter {
/// <summary>
/// What format will be used for exporting
/// </summary>
public enum ExportFormat {
/// <summary>
/// Tab separated values, according to http://www.iana.org/assignments/media-types/text/tab-separated-values
/// </summary>
TabSeparated = 1,
/// <summary>
/// Alias for TabSeparated
/// </summary>
TSV = 1,
/// <summary>
/// Comma separated values, according to http://www.ietf.org/rfc/rfc4180.txt
/// </summary>
CSV,
/// <summary>
/// HTML table, according to me
/// </summary>
HTML
}
#region Life and death
/// <summary>
/// Create an empty exporter
/// </summary>
public OLVExporter() {}
/// <summary>
/// Create an exporter that will export all the rows of the given ObjectListView
/// </summary>
/// <param name="olv"></param>
public OLVExporter(ObjectListView olv) : this(olv, olv.Objects) {}
/// <summary>
/// Create an exporter that will export all the given rows from the given ObjectListView
/// </summary>
/// <param name="olv"></param>
/// <param name="objectsToExport"></param>
public OLVExporter(ObjectListView olv, IEnumerable objectsToExport) {
if (olv == null) throw new ArgumentNullException("olv");
if (objectsToExport == null) throw new ArgumentNullException("objectsToExport");
this.ListView = olv;
this.ModelObjects = ObjectListView.EnumerableToArray(objectsToExport, true);
}
#endregion
#region Properties
/// <summary>
/// Gets or sets whether hidden columns will also be included in the textual
/// representation. If this is false (the default), only visible columns will
/// be included.
/// </summary>
public bool IncludeHiddenColumns {
get { return includeHiddenColumns; }
set { includeHiddenColumns = value; }
}
private bool includeHiddenColumns;
/// <summary>
/// Gets or sets whether column headers will also be included in the text
/// and HTML representation. Default is true.
/// </summary>
public bool IncludeColumnHeaders {
get { return includeColumnHeaders; }
set { includeColumnHeaders = value; }
}
private bool includeColumnHeaders = true;
/// <summary>
/// Gets the ObjectListView that is being used as the source of the data
/// to be exported
/// </summary>
public ObjectListView ListView {
get { return objectListView; }
set { objectListView = value; }
}
private ObjectListView objectListView;
/// <summary>
/// Gets the model objects that are to be placed in the data object
/// </summary>
public IList ModelObjects {
get { return modelObjects; }
set { modelObjects = value; }
}
private IList modelObjects = new ArrayList();
#endregion
#region Commands
/// <summary>
/// Export the nominated rows from the nominated ObjectListView.
/// Returns the result in the expected format.
/// </summary>
/// <param name="format"></param>
/// <returns></returns>
/// <remarks>This will perform only one conversion, even if called multiple times with different formats.</remarks>
public string ExportTo(ExportFormat format) {
if (results == null)
this.Convert();
return results[format];
}
/// <summary>
/// Convert
/// </summary>
public void Convert() {
IList<OLVColumn> columns = this.IncludeHiddenColumns ? this.ListView.AllColumns : this.ListView.ColumnsInDisplayOrder;
StringBuilder sbText = new StringBuilder();
StringBuilder sbCsv = new StringBuilder();
StringBuilder sbHtml = new StringBuilder("<table>");
// Include column headers
if (this.IncludeColumnHeaders) {
List<string> strings = new List<string>();
foreach (OLVColumn col in columns)
strings.Add(col.Text);
WriteOneRow(sbText, strings, "", "\t", "", null);
WriteOneRow(sbHtml, strings, "<tr><td>", "</td><td>", "</td></tr>", HtmlEncode);
WriteOneRow(sbCsv, strings, "", ",", "", CsvEncode);
}
foreach (object modelObject in this.ModelObjects) {
List<string> strings = new List<string>();
foreach (OLVColumn col in columns)
strings.Add(col.GetStringValue(modelObject));
WriteOneRow(sbText, strings, "", "\t", "", null);
WriteOneRow(sbHtml, strings, "<tr><td>", "</td><td>", "</td></tr>", HtmlEncode);
WriteOneRow(sbCsv, strings, "", ",", "", CsvEncode);
}
sbHtml.AppendLine("</table>");
results = new Dictionary<ExportFormat, string>();
results[ExportFormat.TabSeparated] = sbText.ToString();
results[ExportFormat.CSV] = sbCsv.ToString();
results[ExportFormat.HTML] = sbHtml.ToString();
}
private delegate string StringToString(string str);
private void WriteOneRow(StringBuilder sb, IEnumerable<string> strings, string startRow, string betweenCells, string endRow, StringToString encoder) {
sb.Append(startRow);
bool first = true;
foreach (string s in strings) {
if (!first)
sb.Append(betweenCells);
sb.Append(encoder == null ? s : encoder(s));
first = false;
}
sb.AppendLine(endRow);
}
private Dictionary<ExportFormat, string> results;
#endregion
#region Encoding
/// <summary>
/// Encode a string such that it can be used as a value in a CSV file.
/// This basically means replacing any quote mark with two quote marks,
/// and enclosing the whole string in quotes.
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
private static string CsvEncode(string text) {
if (text == null)
return null;
const string DOUBLEQUOTE = @""""; // one double quote
const string TWODOUBEQUOTES = @""""""; // two double quotes
StringBuilder sb = new StringBuilder(DOUBLEQUOTE);
sb.Append(text.Replace(DOUBLEQUOTE, TWODOUBEQUOTES));
sb.Append(DOUBLEQUOTE);
return sb.ToString();
}
/// <summary>
/// HTML-encodes a string and returns the encoded string.
/// </summary>
/// <param name="text">The text string to encode. </param>
/// <returns>The HTML-encoded text.</returns>
/// <remarks>Taken from http://www.west-wind.com/weblog/posts/2009/Feb/05/Html-and-Uri-String-Encoding-without-SystemWeb</remarks>
private static string HtmlEncode(string text) {
if (text == null)
return null;
StringBuilder sb = new StringBuilder(text.Length);
int len = text.Length;
for (int i = 0; i < len; i++) {
switch (text[i]) {
case '<':
sb.Append("&lt;");
break;
case '>':
sb.Append("&gt;");
break;
case '"':
sb.Append("&quot;");
break;
case '&':
sb.Append("&amp;");
break;
default:
if (text[i] > 159) {
// decimal numeric entity
sb.Append("&#");
sb.Append(((int)text[i]).ToString(CultureInfo.InvariantCulture));
sb.Append(";");
} else
sb.Append(text[i]);
break;
}
}
return sb.ToString();
}
#endregion
}
}

View File

@ -0,0 +1,561 @@
/*
* TypedObjectListView - A wrapper around an ObjectListView that provides type-safe delegates.
*
* Author: Phillip Piper
* Date: 27/09/2008 9:15 AM
*
* Change log:
* v2.6
* 2012-10-26 JPP - Handle rare case where a null model object was passed into aspect getters.
* v2.3
* 2009-03-31 JPP - Added Objects property
* 2008-11-26 JPP - Added tool tip getting methods
* 2008-11-05 JPP - Added CheckState handling methods
* 2008-10-24 JPP - Generate dynamic methods MkII. This one handles value types
* 2008-10-21 JPP - Generate dynamic methods
* 2008-09-27 JPP - Separated from ObjectListView.cs
*
* Copyright (C) 2006-2014 Phillip Piper
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
* If you wish to use this code in a closed source application, please contact phillip_piper@bigfoot.com.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Reflection.Emit;
namespace BrightIdeasSoftware
{
/// <summary>
/// A TypedObjectListView is a type-safe wrapper around an ObjectListView.
/// </summary>
/// <remarks>
/// <para>VCS does not support generics on controls. It can be faked to some degree, but it
/// cannot be completely overcome. In our case in particular, there is no way to create
/// the custom OLVColumn's that we need to truly be generic. So this wrapper is an
/// experiment in providing some type-safe access in a way that is useful and available today.</para>
/// <para>A TypedObjectListView is not more efficient than a normal ObjectListView.
/// Underneath, the same name of casts are performed. But it is easier to use since you
/// do not have to write the casts yourself.
/// </para>
/// </remarks>
/// <typeparam name="T">The class of model object that the list will manage</typeparam>
/// <example>
/// To use a TypedObjectListView, you write code like this:
/// <code>
/// TypedObjectListView&lt;Person> tlist = new TypedObjectListView&lt;Person>(this.listView1);
/// tlist.CheckStateGetter = delegate(Person x) { return x.IsActive; };
/// tlist.GetColumn(0).AspectGetter = delegate(Person x) { return x.Name; };
/// ...
/// </code>
/// To iterate over the selected objects, you can write something elegant like this:
/// <code>
/// foreach (Person x in tlist.SelectedObjects) {
/// x.GrantSalaryIncrease();
/// }
/// </code>
/// </example>
public class TypedObjectListView<T> where T : class
{
/// <summary>
/// Create a typed wrapper around the given list.
/// </summary>
/// <param name="olv">The listview to be wrapped</param>
public TypedObjectListView(ObjectListView olv) {
this.olv = olv;
}
//--------------------------------------------------------------------------------------
// Properties
/// <summary>
/// Return the model object that is checked, if only one row is checked.
/// If zero rows are checked, or more than one row, null is returned.
/// </summary>
public virtual T CheckedObject {
get { return (T)this.olv.CheckedObject; }
}
/// <summary>
/// Return the list of all the checked model objects
/// </summary>
public virtual IList<T> CheckedObjects {
get {
IList checkedObjects = this.olv.CheckedObjects;
List<T> objects = new List<T>(checkedObjects.Count);
foreach (object x in checkedObjects)
objects.Add((T)x);
return objects;
}
set { this.olv.CheckedObjects = (IList)value; }
}
/// <summary>
/// The ObjectListView that is being wrapped
/// </summary>
public virtual ObjectListView ListView {
get { return olv; }
set { olv = value; }
}
private ObjectListView olv;
/// <summary>
/// Get or set the list of all model objects
/// </summary>
public virtual IList<T> Objects {
get {
List<T> objects = new List<T>(this.olv.GetItemCount());
for (int i = 0; i < this.olv.GetItemCount(); i++)
objects.Add(this.GetModelObject(i));
return objects;
}
set { this.olv.SetObjects(value); }
}
/// <summary>
/// Return the model object that is selected, if only one row is selected.
/// If zero rows are selected, or more than one row, null is returned.
/// </summary>
public virtual T SelectedObject {
get { return (T)this.olv.SelectedObject; }
set { this.olv.SelectedObject = value; }
}
/// <summary>
/// The list of model objects that are selected.
/// </summary>
public virtual IList<T> SelectedObjects {
get {
List<T> objects = new List<T>(this.olv.SelectedIndices.Count);
foreach (int index in this.olv.SelectedIndices)
objects.Add((T)this.olv.GetModelObject(index));
return objects;
}
set { this.olv.SelectedObjects = (IList)value; }
}
//--------------------------------------------------------------------------------------
// Accessors
/// <summary>
/// Return a typed wrapper around the column at the given index
/// </summary>
/// <param name="i">The index of the column</param>
/// <returns>A typed column or null</returns>
public virtual TypedColumn<T> GetColumn(int i) {
return new TypedColumn<T>(this.olv.GetColumn(i));
}
/// <summary>
/// Return a typed wrapper around the column with the given name
/// </summary>
/// <param name="name">The name of the column</param>
/// <returns>A typed column or null</returns>
public virtual TypedColumn<T> GetColumn(string name) {
return new TypedColumn<T>(this.olv.GetColumn(name));
}
/// <summary>
/// Return the model object at the given index
/// </summary>
/// <param name="index">The index of the model object</param>
/// <returns>The model object or null</returns>
public virtual T GetModelObject(int index) {
return (T)this.olv.GetModelObject(index);
}
//--------------------------------------------------------------------------------------
// Delegates
/// <summary>
/// CheckStateGetter
/// </summary>
/// <param name="rowObject"></param>
/// <returns></returns>
public delegate CheckState TypedCheckStateGetterDelegate(T rowObject);
/// <summary>
/// Gets or sets the check state getter
/// </summary>
public virtual TypedCheckStateGetterDelegate CheckStateGetter {
get { return checkStateGetter; }
set {
this.checkStateGetter = value;
if (value == null)
this.olv.CheckStateGetter = null;
else
this.olv.CheckStateGetter = delegate(object x) {
return this.checkStateGetter((T)x);
};
}
}
private TypedCheckStateGetterDelegate checkStateGetter;
/// <summary>
/// BooleanCheckStateGetter
/// </summary>
/// <param name="rowObject"></param>
/// <returns></returns>
public delegate bool TypedBooleanCheckStateGetterDelegate(T rowObject);
/// <summary>
/// Gets or sets the boolean check state getter
/// </summary>
public virtual TypedBooleanCheckStateGetterDelegate BooleanCheckStateGetter {
set {
if (value == null)
this.olv.BooleanCheckStateGetter = null;
else
this.olv.BooleanCheckStateGetter = delegate(object x) {
return value((T)x);
};
}
}
/// <summary>
/// CheckStatePutter
/// </summary>
/// <param name="rowObject"></param>
/// <param name="newValue"></param>
/// <returns></returns>
public delegate CheckState TypedCheckStatePutterDelegate(T rowObject, CheckState newValue);
/// <summary>
/// Gets or sets the check state putter delegate
/// </summary>
public virtual TypedCheckStatePutterDelegate CheckStatePutter {
get { return checkStatePutter; }
set {
this.checkStatePutter = value;
if (value == null)
this.olv.CheckStatePutter = null;
else
this.olv.CheckStatePutter = delegate(object x, CheckState newValue) {
return this.checkStatePutter((T)x, newValue);
};
}
}
private TypedCheckStatePutterDelegate checkStatePutter;
/// <summary>
/// BooleanCheckStatePutter
/// </summary>
/// <param name="rowObject"></param>
/// <param name="newValue"></param>
/// <returns></returns>
public delegate bool TypedBooleanCheckStatePutterDelegate(T rowObject, bool newValue);
/// <summary>
/// Gets or sets the boolean check state putter
/// </summary>
public virtual TypedBooleanCheckStatePutterDelegate BooleanCheckStatePutter {
set {
if (value == null)
this.olv.BooleanCheckStatePutter = null;
else
this.olv.BooleanCheckStatePutter = delegate(object x, bool newValue) {
return value((T)x, newValue);
};
}
}
/// <summary>
/// ToolTipGetter
/// </summary>
/// <param name="column"></param>
/// <param name="modelObject"></param>
/// <returns></returns>
public delegate String TypedCellToolTipGetterDelegate(OLVColumn column, T modelObject);
/// <summary>
/// Gets or sets the cell tooltip getter
/// </summary>
public virtual TypedCellToolTipGetterDelegate CellToolTipGetter {
set {
if (value == null)
this.olv.CellToolTipGetter = null;
else
this.olv.CellToolTipGetter = delegate(OLVColumn col, Object x) {
return value(col, (T)x);
};
}
}
/// <summary>
/// Gets or sets the header tool tip getter
/// </summary>
public virtual HeaderToolTipGetterDelegate HeaderToolTipGetter {
get { return this.olv.HeaderToolTipGetter; }
set { this.olv.HeaderToolTipGetter = value; }
}
//--------------------------------------------------------------------------------------
// Commands
/// <summary>
/// This method will generate AspectGetters for any column that has an AspectName.
/// </summary>
public virtual void GenerateAspectGetters() {
for (int i = 0; i < this.ListView.Columns.Count; i++)
this.GetColumn(i).GenerateAspectGetter();
}
}
/// <summary>
/// A type-safe wrapper around an OLVColumn
/// </summary>
/// <typeparam name="T"></typeparam>
public class TypedColumn<T> where T : class
{
/// <summary>
/// Creates a TypedColumn
/// </summary>
/// <param name="column"></param>
public TypedColumn(OLVColumn column) {
this.column = column;
}
private OLVColumn column;
/// <summary>
///
/// </summary>
/// <param name="rowObject"></param>
/// <returns></returns>
public delegate Object TypedAspectGetterDelegate(T rowObject);
/// <summary>
///
/// </summary>
/// <param name="rowObject"></param>
/// <param name="newValue"></param>
public delegate void TypedAspectPutterDelegate(T rowObject, Object newValue);
/// <summary>
///
/// </summary>
/// <param name="rowObject"></param>
/// <returns></returns>
public delegate Object TypedGroupKeyGetterDelegate(T rowObject);
/// <summary>
///
/// </summary>
/// <param name="rowObject"></param>
/// <returns></returns>
public delegate Object TypedImageGetterDelegate(T rowObject);
/// <summary>
///
/// </summary>
public TypedAspectGetterDelegate AspectGetter {
get { return this.aspectGetter; }
set {
this.aspectGetter = value;
if (value == null)
this.column.AspectGetter = null;
else
this.column.AspectGetter = delegate(object x) {
return x == null ? null : this.aspectGetter((T)x);
};
}
}
private TypedAspectGetterDelegate aspectGetter;
/// <summary>
///
/// </summary>
public TypedAspectPutterDelegate AspectPutter {
get { return aspectPutter; }
set {
this.aspectPutter = value;
if (value == null)
this.column.AspectPutter = null;
else
this.column.AspectPutter = delegate(object x, object newValue) {
this.aspectPutter((T)x, newValue);
};
}
}
private TypedAspectPutterDelegate aspectPutter;
/// <summary>
///
/// </summary>
public TypedImageGetterDelegate ImageGetter {
get { return imageGetter; }
set {
this.imageGetter = value;
if (value == null)
this.column.ImageGetter = null;
else
this.column.ImageGetter = delegate(object x) {
return this.imageGetter((T)x);
};
}
}
private TypedImageGetterDelegate imageGetter;
/// <summary>
///
/// </summary>
public TypedGroupKeyGetterDelegate GroupKeyGetter {
get { return groupKeyGetter; }
set {
this.groupKeyGetter = value;
if (value == null)
this.column.GroupKeyGetter = null;
else
this.column.GroupKeyGetter = delegate(object x) {
return this.groupKeyGetter((T)x);
};
}
}
private TypedGroupKeyGetterDelegate groupKeyGetter;
#region Dynamic methods
/// <summary>
/// Generate an aspect getter that does the same thing as the AspectName,
/// except without using reflection.
/// </summary>
/// <remarks>
/// <para>
/// If you have an AspectName of "Owner.Address.Postcode", this will generate
/// the equivilent of: <code>this.AspectGetter = delegate (object x) {
/// return x.Owner.Address.Postcode;
/// }
/// </code>
/// </para>
/// <para>
/// If AspectName is empty, this method will do nothing, otherwise
/// this will replace any existing AspectGetter.
/// </para>
/// </remarks>
public void GenerateAspectGetter() {
if (!String.IsNullOrEmpty(this.column.AspectName))
this.AspectGetter = this.GenerateAspectGetter(typeof(T), this.column.AspectName);
}
/// <summary>
/// Generates an aspect getter method dynamically. The method will execute
/// the given dotted chain of selectors against a model object given at runtime.
/// </summary>
/// <param name="type">The type of model object to be passed to the generated method</param>
/// <param name="path">A dotted chain of selectors. Each selector can be the name of a
/// field, property or parameter-less method.</param>
/// <returns>A typed delegate</returns>
private TypedAspectGetterDelegate GenerateAspectGetter(Type type, string path) {
DynamicMethod getter = new DynamicMethod(String.Empty,
typeof(Object), new Type[] { type }, type, true);
this.GenerateIL(type, path, getter.GetILGenerator());
return (TypedAspectGetterDelegate)getter.CreateDelegate(typeof(TypedAspectGetterDelegate));
}
/// <summary>
/// This method generates the actual IL for the method.
/// </summary>
/// <param name="type"></param>
/// <param name="path"></param>
/// <param name="il"></param>
private void GenerateIL(Type type, string path, ILGenerator il) {
// Push our model object onto the stack
il.Emit(OpCodes.Ldarg_0);
// Generate the IL to access each part of the dotted chain
string[] parts = path.Split('.');
for (int i = 0; i < parts.Length; i++) {
type = this.GeneratePart(il, type, parts[i], (i == parts.Length - 1));
if (type == null)
break;
}
// If the object to be returned is a value type (e.g. int, bool), it
// must be boxed, since the delegate returns an Object
if (type != null && type.IsValueType && !typeof(T).IsValueType)
il.Emit(OpCodes.Box, type);
il.Emit(OpCodes.Ret);
}
private Type GeneratePart(ILGenerator il, Type type, string pathPart, bool isLastPart) {
// TODO: Generate check for null
// Find the first member with the given nam that is a field, property, or parameter-less method
List<MemberInfo> infos = new List<MemberInfo>(type.GetMember(pathPart));
MemberInfo info = infos.Find(delegate(MemberInfo x) {
if (x.MemberType == MemberTypes.Field || x.MemberType == MemberTypes.Property)
return true;
if (x.MemberType == MemberTypes.Method)
return ((MethodInfo)x).GetParameters().Length == 0;
else
return false;
});
// If we couldn't find anything with that name, pop the current result and return an error
if (info == null) {
il.Emit(OpCodes.Pop);
if (Munger.IgnoreMissingAspects)
il.Emit(OpCodes.Ldnull);
else
il.Emit(OpCodes.Ldstr, String.Format("'{0}' is not a parameter-less method, property or field of type '{1}'", pathPart, type.FullName));
return null;
}
// Generate the correct IL to access the member. We remember the type of object that is going to be returned
// so that we can do a method lookup on it at the next iteration
Type resultType = null;
switch (info.MemberType) {
case MemberTypes.Method:
MethodInfo mi = (MethodInfo)info;
if (mi.IsVirtual)
il.Emit(OpCodes.Callvirt, mi);
else
il.Emit(OpCodes.Call, mi);
resultType = mi.ReturnType;
break;
case MemberTypes.Property:
PropertyInfo pi = (PropertyInfo)info;
il.Emit(OpCodes.Call, pi.GetGetMethod());
resultType = pi.PropertyType;
break;
case MemberTypes.Field:
FieldInfo fi = (FieldInfo)info;
il.Emit(OpCodes.Ldfld, fi);
resultType = fi.FieldType;
break;
}
// If the method returned a value type, and something is going to call a method on that value,
// we need to load its address onto the stack, rather than the object itself.
if (resultType.IsValueType && !isLastPart) {
LocalBuilder lb = il.DeclareLocal(resultType);
il.Emit(OpCodes.Stloc, lb);
il.Emit(OpCodes.Ldloca, lb);
}
return resultType;
}
#endregion
}
}

1227
VirtualObjectListView.cs Normal file

File diff suppressed because it is too large Load Diff

BIN
olv-keyfile.snk Normal file

Binary file not shown.