1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
|
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2004 Novell, Inc. (http://www.novell.com)
//
// Author:
// Ravindra (rkumar@novell.com)
// Mike Kestner <mkestner@novell.com>
// Daniel Nauck (dna(at)mono-project(dot)de)
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.Serialization;
namespace System.Windows.Forms
{
[DefaultProperty ("Text")]
[DesignTimeVisible (false)]
[Serializable]
[ToolboxItem (false)]
[TypeConverter (typeof (ListViewItemConverter))]
public class ListViewItem : ICloneable, ISerializable
{
#region Instance Variables
private int image_index = -1;
private bool is_checked = false;
private int state_image_index = -1;
private ListViewSubItemCollection sub_items;
private object tag;
private bool use_item_style = true;
int display_index = -1; // actual position in ListView
private ListViewGroup group = null;
private string name = String.Empty;
private string image_key = String.Empty;
string tooltip_text = String.Empty;
int indent_count;
Point position = new Point (-1, -1); // cached to mimic .Net behaviour
Rectangle bounds = Rectangle.Empty;
Rectangle checkbox_rect; // calculated by CalcListViewItem method
Rectangle icon_rect;
Rectangle item_rect;
Rectangle label_rect;
ListView owner;
Font font;
Font hot_font; // cached font for hot tracking
bool selected;
internal int row;
internal int col;
#region UIA Framework: Methods, Properties and Events
internal event EventHandler UIATextChanged;
internal event LabelEditEventHandler UIASubItemTextChanged;
internal void OnUIATextChanged ()
{
if (UIATextChanged != null)
UIATextChanged (this, EventArgs.Empty);
}
internal void OnUIASubItemTextChanged (LabelEditEventArgs args)
{
//If our index is 0 we also generate TextChanged for the ListViewItem
//because ListViewItem.Text is the same as ListViewItem.SubItems [0].Text
if (args.Item == 0)
OnUIATextChanged ();
if (UIASubItemTextChanged != null)
UIASubItemTextChanged (this, args);
}
#endregion // UIA Framework: Methods, Properties and Events
#endregion Instance Variables
#region Public Constructors
public ListViewItem () : this (string.Empty)
{
}
public ListViewItem (string text) : this (text, -1)
{
}
public ListViewItem (string [] items) : this (items, -1)
{
}
public ListViewItem (ListViewItem.ListViewSubItem [] subItems, int imageIndex)
{
this.sub_items = new ListViewSubItemCollection (this, null);
for (int i = 0; i < subItems.Length; i++)
sub_items.Add (subItems [i]);
this.image_index = imageIndex;
}
public ListViewItem (string text, int imageIndex)
{
this.image_index = imageIndex;
this.sub_items = new ListViewSubItemCollection (this, text);
}
public ListViewItem (string [] items, int imageIndex)
{
this.sub_items = new ListViewSubItemCollection (this, null);
if (items != null) {
for (int i = 0; i < items.Length; i++)
sub_items.Add (new ListViewSubItem (this, items [i]));
}
this.image_index = imageIndex;
}
public ListViewItem (string [] items, int imageIndex, Color foreColor,
Color backColor, Font font) : this (items, imageIndex)
{
ForeColor = foreColor;
BackColor = backColor;
this.font = font;
}
public ListViewItem(string[] items, string imageKey) : this(items)
{
this.ImageKey = imageKey;
}
public ListViewItem(string text, string imageKey) : this(text)
{
this.ImageKey = imageKey;
}
public ListViewItem(ListViewSubItem[] subItems, string imageKey)
{
this.sub_items = new ListViewSubItemCollection (this, null);
for (int i = 0; i < subItems.Length; i++)
this.sub_items.Add (subItems [i]);
this.ImageKey = imageKey;
}
public ListViewItem(string[] items, string imageKey, Color foreColor,
Color backColor, Font font) : this(items, imageKey)
{
ForeColor = foreColor;
BackColor = backColor;
this.font = font;
}
public ListViewItem(ListViewGroup group) : this()
{
Group = group;
}
public ListViewItem(string text, ListViewGroup group) : this(text)
{
Group = group;
}
public ListViewItem(string[] items, ListViewGroup group) : this(items)
{
Group = group;
}
public ListViewItem(ListViewSubItem[] subItems, int imageIndex, ListViewGroup group)
: this(subItems, imageIndex)
{
Group = group;
}
public ListViewItem(ListViewSubItem[] subItems, string imageKey, ListViewGroup group)
: this(subItems, imageKey)
{
Group = group;
}
public ListViewItem(string text, int imageIndex, ListViewGroup group)
: this(text, imageIndex)
{
Group = group;
}
public ListViewItem(string text, string imageKey, ListViewGroup group)
: this(text, imageKey)
{
Group = group;
}
public ListViewItem(string[] items, int imageIndex, ListViewGroup group)
: this(items, imageIndex)
{
Group = group;
}
public ListViewItem(string[] items, string imageKey, ListViewGroup group)
: this(items, imageKey)
{
Group = group;
}
public ListViewItem(string[] items, int imageIndex, Color foreColor, Color backColor,
Font font, ListViewGroup group)
: this(items, imageIndex, foreColor, backColor, font)
{
Group = group;
}
public ListViewItem(string[] items, string imageKey, Color foreColor, Color backColor,
Font font, ListViewGroup group)
: this(items, imageKey, foreColor, backColor, font)
{
Group = group;
}
#endregion // Public Constructors
protected ListViewItem (SerializationInfo info, StreamingContext context)
{
Deserialize (info, context);
}
#region Public Instance Properties
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public Color BackColor {
get {
if (sub_items.Count > 0)
return sub_items[0].BackColor;
if (owner != null)
return owner.BackColor;
return ThemeEngine.Current.ColorWindow;
}
set { SubItems [0].BackColor = value; }
}
[Browsable (false)]
public Rectangle Bounds {
get {
return GetBounds (ItemBoundsPortion.Entire);
}
}
[DefaultValue (false)]
[RefreshProperties (RefreshProperties.Repaint)]
public bool Checked {
get { return is_checked; }
set {
if (is_checked == value)
return;
if (owner != null) {
CheckState current_value = is_checked ? CheckState.Checked : CheckState.Unchecked;
CheckState new_value = value ? CheckState.Checked : CheckState.Unchecked;
ItemCheckEventArgs icea = new ItemCheckEventArgs (Index,
new_value, current_value);
owner.OnItemCheck (icea);
if (new_value != current_value) {
// force re-population of list
owner.CheckedItems.Reset ();
is_checked = new_value == CheckState.Checked;
Invalidate ();
ItemCheckedEventArgs args = new ItemCheckedEventArgs (this);
owner.OnItemChecked (args);
}
} else
is_checked = value;
}
}
[Browsable (false)]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public bool Focused {
get {
if (owner == null)
return false;
// In virtual mode the checks are always done using indexes
if (owner.VirtualMode)
return Index == owner.focused_item_index;
// Light check
return owner.FocusedItem == this;
}
set {
if (owner == null)
return;
if (Focused == value)
return;
ListViewItem prev_focused_item = owner.FocusedItem;
if (prev_focused_item != null)
prev_focused_item.UpdateFocusedState ();
owner.focused_item_index = value ? Index : -1;
if (value)
owner.OnUIAFocusedItemChanged ();
UpdateFocusedState ();
}
}
[Localizable (true)]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public Font Font {
get {
if (font != null)
return font;
else if (owner != null)
return owner.Font;
return ThemeEngine.Current.DefaultFont;
}
set {
if (font == value)
return;
font = value;
hot_font = null;
if (owner != null)
Layout ();
Invalidate ();
}
}
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public Color ForeColor {
get {
if (sub_items.Count > 0)
return sub_items[0].ForeColor;
if (owner != null)
return owner.ForeColor;
return ThemeEngine.Current.ColorWindowText;
}
set { SubItems [0].ForeColor = value; }
}
[DefaultValue (-1)]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
[Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design,
typeof (System.Drawing.Design.UITypeEditor))]
[Localizable (true)]
[RefreshProperties (RefreshProperties.Repaint)]
[TypeConverter (typeof (NoneExcludedImageIndexConverter))]
public int ImageIndex {
get { return image_index; }
set {
if (value < -1)
throw new ArgumentException ("Invalid ImageIndex. It must be greater than or equal to -1.");
image_index = value;
image_key = String.Empty;
if (owner != null)
Layout ();
Invalidate ();
}
}
[DefaultValue ("")]
[LocalizableAttribute (true)]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
[Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design,
typeof (System.Drawing.Design.UITypeEditor))]
[RefreshProperties (RefreshProperties.Repaint)]
[TypeConverter (typeof (ImageKeyConverter))]
public string ImageKey {
get {
return image_key;
}
set {
image_key = value == null ? String.Empty : value;
image_index = -1;
if (owner != null)
Layout ();
Invalidate ();
}
}
[Browsable (false)]
public ImageList ImageList {
get {
if (owner == null)
return null;
else if (owner.View == View.LargeIcon)
return owner.large_image_list;
else
return owner.small_image_list;
}
}
[DefaultValue (0)]
public int IndentCount {
get {
return indent_count;
}
set {
if (value < 0)
throw new ArgumentOutOfRangeException ("value");
if (value == indent_count)
return;
indent_count = value;
Invalidate ();
}
}
[Browsable (false)]
public int Index {
get {
if (owner == null)
return -1;
if (owner.VirtualMode)
return display_index;
if (display_index == -1)
return owner.Items.IndexOf (this);
return owner.GetItemIndex (display_index);
}
}
[Browsable (false)]
public ListView ListView {
get { return owner; }
}
[Browsable (false)]
[Localizable (true)]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public string Name {
get {
return name;
}
set {
name = value == null ? String.Empty : value;
}
}
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
[Browsable (false)]
public Point Position {
get {
if (owner != null && owner.VirtualMode)
return owner.GetItemLocation (display_index);
if (owner != null && !owner.IsHandleCreated)
return new Point (-1, -1);
return position;
}
set {
if (owner == null || owner.View == View.Details || owner.View == View.List)
return;
if (owner.VirtualMode)
throw new InvalidOperationException ();
owner.ChangeItemLocation (display_index, value);
}
}
// When ListView uses VirtualMode, selection state info
// lives in the ListView, not in the item
// Also, in VirtualMode we can't Reset() the selection
[Browsable (false)]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public bool Selected {
get {
if (owner != null && owner.VirtualMode)
return owner.SelectedIndices.Contains (Index);
return selected;
}
set {
if (selected == value && owner != null && !owner.VirtualMode)
return;
SetSelectedCore (value);
}
}
// Expose this method as internal so we can force an update in the selection.
internal void SetSelectedCore (bool value)
{
if (owner != null) {
if (value && !owner.MultiSelect)
owner.SelectedIndices.Clear ();
if (owner.VirtualMode) {
if (value)
owner.SelectedIndices.InsertIndex (Index);
else
owner.SelectedIndices.RemoveIndex (Index);
} else {
selected = value;
owner.SelectedIndices.Reset (); // force re-population of list
}
owner.OnItemSelectionChanged (new ListViewItemSelectionChangedEventArgs (this, Index, value));
owner.OnSelectedIndexChanged ();
Invalidate ();
} else
selected = value;
}
[DefaultValue (-1)]
[Editor ("System.Windows.Forms.Design.ImageIndexEditor, " + Consts.AssemblySystem_Design,
typeof (System.Drawing.Design.UITypeEditor))]
[Localizable (true)]
[RefreshProperties (RefreshProperties.Repaint)]
[RelatedImageListAttribute ("ListView.StateImageList")]
[TypeConverter (typeof (NoneExcludedImageIndexConverter))]
public int StateImageIndex {
get { return state_image_index; }
set {
if (value < -1 || value > 14)
throw new ArgumentOutOfRangeException ("Invalid StateImageIndex. It must be in the range of [-1, 14].");
state_image_index = value;
}
}
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
[Editor ("System.Windows.Forms.Design.ListViewSubItemCollectionEditor, " + Consts.AssemblySystem_Design,
typeof (System.Drawing.Design.UITypeEditor))]
public ListViewSubItemCollection SubItems {
get {
if (sub_items.Count == 0)
this.sub_items.Add (string.Empty);
return sub_items;
}
}
[Bindable (true)]
[DefaultValue (null)]
[Localizable (false)]
[TypeConverter (typeof (StringConverter))]
public object Tag {
get { return tag; }
set { tag = value; }
}
[Localizable (true)]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public string Text {
get {
if (this.sub_items.Count > 0)
return this.sub_items [0].Text;
else
return string.Empty;
}
set {
if (SubItems [0].Text == value)
return;
sub_items [0].Text = value;
if (owner != null)
Layout ();
Invalidate ();
//UIA Framework: Generates Text changed
OnUIATextChanged ();
}
}
[DefaultValue (true)]
public bool UseItemStyleForSubItems {
get { return use_item_style; }
set { use_item_style = value; }
}
[LocalizableAttribute(true)]
[DefaultValue (null)]
public ListViewGroup Group {
get { return this.group; }
set {
if (group != value) {
if (value == null)
group.Items.Remove (this);
else
value.Items.Add (this);
group = value;
}
}
}
[DefaultValue ("")]
public string ToolTipText {
get {
return tooltip_text;
}
set {
if (value == null)
value = String.Empty;
tooltip_text = value;
}
}
#endregion // Public Instance Properties
#region Public Instance Methods
public void BeginEdit ()
{
if (owner != null && owner.LabelEdit) {
owner.item_control.BeginEdit (this);
}
// FIXME: TODO
// if (owner != null && owner.LabelEdit
// && owner.Activation == ItemActivation.Standard)
// allow editing
// else
// throw new InvalidOperationException ();
}
public virtual object Clone ()
{
ListViewItem clone = new ListViewItem ();
clone.image_index = this.image_index;
clone.is_checked = this.is_checked;
clone.selected = this.selected;
clone.font = this.font;
clone.state_image_index = this.state_image_index;
clone.sub_items = new ListViewSubItemCollection (this, null);
foreach (ListViewSubItem subItem in this.sub_items)
clone.sub_items.Add (subItem.Text, subItem.ForeColor,
subItem.BackColor, subItem.Font);
clone.tag = this.tag;
clone.use_item_style = this.use_item_style;
clone.owner = null;
clone.name = name;
clone.tooltip_text = tooltip_text;
return clone;
}
public virtual void EnsureVisible ()
{
if (this.owner != null) {
owner.EnsureVisible (owner.Items.IndexOf (this));
}
}
public ListViewItem FindNearestItem (SearchDirectionHint searchDirection)
{
if (owner == null)
return null;
Point loc = owner.GetItemLocation (display_index);
return owner.FindNearestItem (searchDirection, loc);
}
public Rectangle GetBounds (ItemBoundsPortion portion)
{
if (owner == null)
return Rectangle.Empty;
Rectangle rect;
switch (portion) {
case ItemBoundsPortion.Icon:
rect = icon_rect;
break;
case ItemBoundsPortion.Label:
rect = label_rect;
break;
case ItemBoundsPortion.ItemOnly:
rect = item_rect;
break;
case ItemBoundsPortion.Entire:
rect = bounds;
break;
default:
throw new ArgumentException ("Invalid value for portion.");
}
Point item_loc = owner.GetItemLocation (DisplayIndex);
rect.X += item_loc.X;
rect.Y += item_loc.Y;
return rect;
}
void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
{
Serialize (info, context);
}
public ListViewSubItem GetSubItemAt (int x, int y)
{
if (owner != null && owner.View != View.Details)
return null;
foreach (ListViewSubItem sub_item in sub_items)
if (sub_item.Bounds.Contains (x, y))
return sub_item;
return null;
}
public virtual void Remove ()
{
if (owner == null)
return;
owner.item_control.CancelEdit (this);
owner.Items.Remove (this);
owner = null;
}
public override string ToString ()
{
return string.Format ("ListViewItem: {0}", this.Text);
}
#endregion // Public Instance Methods
#region Protected Methods
protected virtual void Deserialize (SerializationInfo info, StreamingContext context)
{
sub_items = new ListViewSubItemCollection (this, null);
int sub_items_count = 0;
foreach (SerializationEntry entry in info) {
switch (entry.Name) {
case "Text":
sub_items.Add ((string)entry.Value);
break;
case "Font":
font = (Font)entry.Value;
break;
case "Checked":
is_checked = (bool)entry.Value;
break;
case "ImageIndex":
image_index = (int)entry.Value;
break;
case "StateImageIndex":
state_image_index = (int)entry.Value;
break;
case "UseItemStyleForSubItems":
use_item_style = (bool)entry.Value;
break;
case "SubItemCount":
sub_items_count = (int)entry.Value;
break;
case "Group":
group = (ListViewGroup)entry.Value;
break;
case "ImageKey":
if (image_index == -1)
image_key = (string)entry.Value;
break;
}
}
Type subitem_type = typeof (ListViewSubItem);
if (sub_items_count > 0) {
sub_items.Clear (); // .net fixup
Text = info.GetString ("Text");
for (int i = 0; i < sub_items_count - 1; i++)
sub_items.Add ((ListViewSubItem)info.GetValue ("SubItem" + (i + 1), subitem_type));
}
// After any sub item has been added.
ForeColor = (Color)info.GetValue ("ForeColor", typeof (Color));
BackColor = (Color)info.GetValue ("BackColor", typeof (Color));
}
protected virtual void Serialize (SerializationInfo info, StreamingContext context)
{
info.AddValue ("Text", Text);
info.AddValue ("Font", Font);
info.AddValue ("ImageIndex", image_index);
info.AddValue ("Checked", is_checked);
info.AddValue ("StateImageIndex", state_image_index);
info.AddValue ("UseItemStyleForSubItems", use_item_style);
info.AddValue ("BackColor", BackColor);
info.AddValue ("ForeColor", ForeColor);
info.AddValue ("ImageKey", image_key);
if (group != null)
info.AddValue ("Group", group);
if (sub_items.Count > 1) {
info.AddValue ("SubItemCount", sub_items.Count);
for (int i = 1; i < sub_items.Count; i++) {
info.AddValue ("SubItem" + i, sub_items [i]);
}
}
}
#endregion // Protected Methods
#region Private Internal Methods
internal Rectangle CheckRectReal {
get {
Rectangle rect = checkbox_rect;
Point item_loc = owner.GetItemLocation (DisplayIndex);
rect.X += item_loc.X;
rect.Y += item_loc.Y;
return rect;
}
}
Rectangle text_bounds;
internal Rectangle TextBounds {
get {
// Call Layout() if it hasn't been called before.
if (owner.VirtualMode && bounds == new Rectangle (-1, -1, -1, -1))
Layout ();
Rectangle result = text_bounds;
Point loc = owner.GetItemLocation (DisplayIndex);
result.X += loc.X;
result.Y += loc.Y;
return result;
}
}
internal int DisplayIndex {
get {
// Special case for Details view
// and no columns (which means no Layout at all)
if (display_index == -1)
return owner.Items.IndexOf (this);
return display_index;
}
set {
display_index = value;
}
}
internal bool Hot {
get {
return Index == owner.HotItemIndex;
}
}
internal Font HotFont {
get {
if (hot_font == null)
hot_font = new Font (Font, Font.Style | FontStyle.Underline);
return hot_font;
}
}
internal ListView Owner {
set {
if (owner == value)
return;
owner = value;
}
}
internal void SetGroup (ListViewGroup group)
{
this.group = group;
}
internal void SetPosition (Point position)
{
this.position = position;
}
// When focus changed, we need to invalidate area
// with previous layout and with the new one
void UpdateFocusedState ()
{
if (owner != null) {
Invalidate ();
Layout ();
Invalidate ();
}
}
internal void Invalidate ()
{
if (owner == null || owner.item_control == null || owner.updating)
return;
// Add some padding to bounds (focused extra space, selection)
Rectangle rect = Bounds;
rect.Inflate (1, 1);
owner.item_control.Invalidate (rect);
}
internal void Layout ()
{
if (owner == null)
return;
int item_ht;
Rectangle total;
Size text_size = owner.text_size;
checkbox_rect = Rectangle.Empty;
if (owner.CheckBoxes)
checkbox_rect.Size = owner.CheckBoxSize;
switch (owner.View) {
case View.Details:
// LAMESPEC: MSDN says, "In all views except the details
// view of the ListView, this value specifies the same
// bounding rectangle as the Entire value." Actually, it
// returns same bounding rectangles for Item and Entire
// values in the case of Details view.
int x_offset = 0;
if (owner.SmallImageList != null)
x_offset = indent_count * owner.SmallImageList.ImageSize.Width;
// Handle reordered column
if (owner.Columns.Count > 0)
checkbox_rect.X = owner.Columns[0].Rect.X + x_offset;
icon_rect = label_rect = Rectangle.Empty;
icon_rect.X = checkbox_rect.Right + 2;
item_ht = owner.ItemSize.Height;
if (owner.SmallImageList != null)
icon_rect.Width = owner.SmallImageList.ImageSize.Width;
label_rect.Height = icon_rect.Height = item_ht;
checkbox_rect.Y = item_ht - checkbox_rect.Height;
label_rect.X = icon_rect.Width > 0 ? icon_rect.Right + 1 : icon_rect.Right;
if (owner.Columns.Count > 0)
label_rect.Width = owner.Columns[0].Wd - label_rect.X + checkbox_rect.X;
else
label_rect.Width = text_size.Width;
SizeF text_sz = TextRenderer.MeasureString (Text, Font);
text_bounds = label_rect;
text_bounds.Width = (int) text_sz.Width;
item_rect = total = Rectangle.Union
(Rectangle.Union (checkbox_rect, icon_rect), label_rect);
bounds.Size = total.Size;
item_rect.Width = 0;
bounds.Width = 0;
for (int i = 0; i < owner.Columns.Count; i++) {
item_rect.Width += owner.Columns [i].Wd;
bounds.Width += owner.Columns [i].Wd;
}
// Bounds for sub items
int n = Math.Min (owner.Columns.Count, sub_items.Count);
for (int i = 0; i < n; i++) {
Rectangle col_rect = owner.Columns [i].Rect;
sub_items [i].SetBounds (col_rect.X, 0, col_rect.Width, item_ht);
}
break;
case View.LargeIcon:
label_rect = icon_rect = Rectangle.Empty;
SizeF sz = TextRenderer.MeasureString (Text, Font);
if ((int) sz.Width > text_size.Width) {
if (Focused && owner.InternalContainsFocus) {
int text_width = text_size.Width;
StringFormat format = new StringFormat ();
format.Alignment = StringAlignment.Center;
sz = TextRenderer.MeasureString (Text, Font, text_width, format);
text_size.Height = (int) sz.Height;
} else
text_size.Height = 2 * (int) sz.Height;
}
if (owner.LargeImageList != null) {
icon_rect.Width = owner.LargeImageList.ImageSize.Width;
icon_rect.Height = owner.LargeImageList.ImageSize.Height;
}
if (checkbox_rect.Height > icon_rect.Height)
icon_rect.Y = checkbox_rect.Height - icon_rect.Height;
else
checkbox_rect.Y = icon_rect.Height - checkbox_rect.Height;
if (text_size.Width <= icon_rect.Width) {
icon_rect.X = checkbox_rect.Width + 1;
label_rect.X = icon_rect.X + (icon_rect.Width - text_size.Width) / 2;
label_rect.Y = icon_rect.Bottom + 2;
label_rect.Size = text_size;
} else {
int centerX = text_size.Width / 2;
icon_rect.X = checkbox_rect.Width + 1 + centerX - icon_rect.Width / 2;
label_rect.X = checkbox_rect.Width + 1;
label_rect.Y = icon_rect.Bottom + 2;
label_rect.Size = text_size;
}
item_rect = Rectangle.Union (icon_rect, label_rect);
total = Rectangle.Union (item_rect, checkbox_rect);
bounds.Size = total.Size;
break;
case View.List:
case View.SmallIcon:
label_rect = icon_rect = Rectangle.Empty;
icon_rect.X = checkbox_rect.Width + 1;
item_ht = Math.Max (owner.CheckBoxSize.Height, text_size.Height);
if (owner.SmallImageList != null) {
item_ht = Math.Max (item_ht, owner.SmallImageList.ImageSize.Height);
icon_rect.Width = owner.SmallImageList.ImageSize.Width;
icon_rect.Height = owner.SmallImageList.ImageSize.Height;
}
checkbox_rect.Y = item_ht - checkbox_rect.Height;
label_rect.X = icon_rect.Right + 1;
label_rect.Width = text_size.Width;
label_rect.Height = icon_rect.Height = item_ht;
item_rect = Rectangle.Union (icon_rect, label_rect);
total = Rectangle.Union (item_rect, checkbox_rect);
bounds.Size = total.Size;
break;
case View.Tile:
if (!Application.VisualStylesEnabled)
goto case View.LargeIcon;
label_rect = icon_rect = Rectangle.Empty;
if (owner.LargeImageList != null) {
icon_rect.Width = owner.LargeImageList.ImageSize.Width;
icon_rect.Height = owner.LargeImageList.ImageSize.Height;
}
int separation = 2;
SizeF tsize = TextRenderer.MeasureString (Text, Font);
int main_item_height = (int) Math.Ceiling (tsize.Height);
int main_item_width = (int) Math.Ceiling (tsize.Width);
sub_items [0].bounds.Height = main_item_height;
// Set initial values for subitem's layout
int total_height = main_item_height;
int max_subitem_width = main_item_width;
int count = Math.Min (owner.Columns.Count, sub_items.Count);
for (int i = 1; i < count; i++) { // Ignore first column and first subitem
ListViewSubItem sub_item = sub_items [i];
if (sub_item.Text == null || sub_item.Text.Length == 0)
continue;
tsize = TextRenderer.MeasureString (sub_item.Text, sub_item.Font);
int width = (int)Math.Ceiling (tsize.Width);
if (width > max_subitem_width)
max_subitem_width = width;
int height = (int)Math.Ceiling (tsize.Height);
total_height += height + separation;
sub_item.bounds.Height = height;
}
max_subitem_width = Math.Min (max_subitem_width, owner.TileSize.Width - (icon_rect.Width + 4));
label_rect.X = icon_rect.Right + 4;
label_rect.Y = owner.TileSize.Height / 2 - total_height / 2;
label_rect.Width = max_subitem_width;
label_rect.Height = total_height;
// Main item - always set bounds for it
sub_items [0].SetBounds (label_rect.X, label_rect.Y, max_subitem_width, sub_items [0].bounds.Height);
// Second pass to assign bounds for every sub item
int current_y = sub_items [0].bounds.Bottom + separation;
for (int j = 1; j < count; j++) {
ListViewSubItem sub_item = sub_items [j];
if (sub_item.Text == null || sub_item.Text.Length == 0)
continue;
sub_item.SetBounds (label_rect.X, current_y, max_subitem_width, sub_item.bounds.Height);
current_y += sub_item.Bounds.Height + separation;
}
item_rect = Rectangle.Union (icon_rect, label_rect);
bounds.Size = item_rect.Size;
break;
}
}
#endregion // Private Internal Methods
#region Subclasses
[DefaultProperty ("Text")]
[DesignTimeVisible (false)]
[Serializable]
[ToolboxItem (false)]
[TypeConverter (typeof(ListViewSubItemConverter))]
public class ListViewSubItem
{
[NonSerialized]
internal ListViewItem owner;
private string text = string.Empty;
private string name;
private object userData;
private SubItemStyle style;
[NonSerialized]
internal Rectangle bounds;
#region UIA Framework: Methods, Properties and Events
[field:NonSerialized]
internal event EventHandler UIATextChanged;
private void OnUIATextChanged ()
{
if (UIATextChanged != null)
UIATextChanged (this, EventArgs.Empty);
}
#endregion // UIA Framework: Methods, Properties and Events
#region Public Constructors
public ListViewSubItem ()
: this (null, string.Empty, Color.Empty,
Color.Empty, null)
{
}
public ListViewSubItem (ListViewItem owner, string text)
: this (owner, text, Color.Empty,
Color.Empty, null)
{
}
public ListViewSubItem (ListViewItem owner, string text, Color foreColor,
Color backColor, Font font)
{
this.owner = owner;
Text = text;
this.style = new SubItemStyle (foreColor,
backColor, font);
}
#endregion // Public Constructors
#region Public Instance Properties
public Color BackColor {
get {
if (style.backColor != Color.Empty)
return style.backColor;
if (this.owner != null && this.owner.ListView != null)
return this.owner.ListView.BackColor;
return ThemeEngine.Current.ColorWindow;
}
set {
style.backColor = value;
Invalidate ();
}
}
[Browsable (false)]
public Rectangle Bounds {
get {
Rectangle retval = bounds;
if (owner != null) {
retval.X += owner.Bounds.X;
retval.Y += owner.Bounds.Y;
}
return retval;
}
}
[Localizable (true)]
public Font Font {
get {
if (style.font != null)
return style.font;
else if (owner != null)
return owner.Font;
return ThemeEngine.Current.DefaultFont;
}
set {
if (style.font == value)
return;
style.font = value;
Invalidate ();
}
}
public Color ForeColor {
get {
if (style.foreColor != Color.Empty)
return style.foreColor;
if (this.owner != null && this.owner.ListView != null)
return this.owner.ListView.ForeColor;
return ThemeEngine.Current.ColorWindowText;
}
set {
style.foreColor = value;
Invalidate ();
}
}
[Localizable (true)]
public string Name {
get {
if (name == null)
return string.Empty;
return name;
}
set {
name = value;
}
}
[TypeConverter (typeof (StringConverter))]
[BindableAttribute (true)]
[DefaultValue (null)]
[Localizable (false)]
public object Tag {
get {
return userData;
}
set {
userData = value;
}
}
[Localizable (true)]
public string Text {
get { return text; }
set {
if(text == value)
return;
if(value == null)
text = string.Empty;
else
text = value;
Invalidate ();
// UIA Framework: Generates SubItem TextChanged
OnUIATextChanged ();
}
}
#endregion // Public Instance Properties
#region Public Methods
public void ResetStyle ()
{
style.Reset ();
Invalidate ();
}
public override string ToString ()
{
return string.Format ("ListViewSubItem {{0}}", text);
}
#endregion // Public Methods
#region Private Methods
private void Invalidate ()
{
if (owner == null || owner.owner == null)
return;
owner.Invalidate ();
}
[OnDeserialized]
void OnDeserialized (StreamingContext context)
{
name = null;
userData = null;
}
internal int Height {
get {
return bounds.Height;
}
}
internal void SetBounds (int x, int y, int width, int height)
{
bounds = new Rectangle (x, y, width, height);
}
#endregion // Private Methods
[Serializable]
class SubItemStyle
{
public SubItemStyle ()
{
}
public SubItemStyle (Color foreColor, Color backColor, Font font)
{
this.foreColor = foreColor;
this.backColor = backColor;
this.font = font;
}
public void Reset ()
{
foreColor = Color.Empty;
backColor = Color.Empty;
font = null;
}
public Color backColor;
public Color foreColor;
public Font font;
}
}
public class ListViewSubItemCollection : IList, ICollection, IEnumerable
{
private ArrayList list;
internal ListViewItem owner;
#region Public Constructors
public ListViewSubItemCollection (ListViewItem owner) : this (owner, owner.Text)
{
}
#endregion // Public Constructors
internal ListViewSubItemCollection (ListViewItem owner, string text)
{
this.owner = owner;
this.list = new ArrayList ();
if (text != null)
Add (text);
}
#region Public Properties
[Browsable (false)]
public int Count {
get { return list.Count; }
}
public bool IsReadOnly {
get { return false; }
}
public ListViewSubItem this [int index] {
get { return (ListViewSubItem) list [index]; }
set {
value.owner = owner;
list [index] = value;
owner.Layout ();
owner.Invalidate ();
}
}
public virtual ListViewSubItem this [string key] {
get {
int idx = IndexOfKey (key);
if (idx == -1)
return null;
return (ListViewSubItem) list [idx];
}
}
bool ICollection.IsSynchronized {
get { return list.IsSynchronized; }
}
object ICollection.SyncRoot {
get { return list.SyncRoot; }
}
bool IList.IsFixedSize {
get { return list.IsFixedSize; }
}
object IList.this [int index] {
get { return this [index]; }
set {
if (! (value is ListViewSubItem))
throw new ArgumentException ("Not of type ListViewSubItem", "value");
this [index] = (ListViewSubItem) value;
}
}
#endregion // Public Properties
#region Public Methods
public ListViewSubItem Add (ListViewSubItem item)
{
AddSubItem (item);
owner.Layout ();
owner.Invalidate ();
return item;
}
public ListViewSubItem Add (string text)
{
ListViewSubItem item = new ListViewSubItem (owner, text);
return Add (item);
}
public ListViewSubItem Add (string text, Color foreColor,
Color backColor, Font font)
{
ListViewSubItem item = new ListViewSubItem (owner, text,
foreColor, backColor, font);
return Add (item);
}
public void AddRange (ListViewSubItem [] items)
{
if (items == null)
throw new ArgumentNullException ("items");
foreach (ListViewSubItem item in items) {
if (item == null)
continue;
AddSubItem (item);
}
owner.Layout ();
owner.Invalidate ();
}
public void AddRange (string [] items)
{
if (items == null)
throw new ArgumentNullException ("items");
foreach (string item in items) {
if (item == null)
continue;
AddSubItem (new ListViewSubItem (owner, item));
}
owner.Layout ();
owner.Invalidate ();
}
public void AddRange (string [] items, Color foreColor,
Color backColor, Font font)
{
if (items == null)
throw new ArgumentNullException ("items");
foreach (string item in items) {
if (item == null)
continue;
AddSubItem (new ListViewSubItem (owner, item, foreColor, backColor, font));
}
owner.Layout ();
owner.Invalidate ();
}
void AddSubItem (ListViewSubItem subItem)
{
subItem.owner = owner;
list.Add (subItem);
//UIA Framework
subItem.UIATextChanged += OnUIASubItemTextChanged;
}
public void Clear ()
{
list.Clear ();
}
public bool Contains (ListViewSubItem subItem)
{
return list.Contains (subItem);
}
public virtual bool ContainsKey (string key)
{
return IndexOfKey (key) != -1;
}
public IEnumerator GetEnumerator ()
{
return list.GetEnumerator ();
}
void ICollection.CopyTo (Array dest, int index)
{
list.CopyTo (dest, index);
}
int IList.Add (object item)
{
if (! (item is ListViewSubItem)) {
throw new ArgumentException ("Not of type ListViewSubItem", "item");
}
ListViewSubItem sub_item = (ListViewSubItem) item;
sub_item.owner = this.owner;
//UIA Framework
sub_item.UIATextChanged += OnUIASubItemTextChanged;
return list.Add (sub_item);
}
bool IList.Contains (object subItem)
{
if (! (subItem is ListViewSubItem)) {
throw new ArgumentException ("Not of type ListViewSubItem", "subItem");
}
return this.Contains ((ListViewSubItem) subItem);
}
int IList.IndexOf (object subItem)
{
if (! (subItem is ListViewSubItem)) {
throw new ArgumentException ("Not of type ListViewSubItem", "subItem");
}
return this.IndexOf ((ListViewSubItem) subItem);
}
void IList.Insert (int index, object item)
{
if (! (item is ListViewSubItem)) {
throw new ArgumentException ("Not of type ListViewSubItem", "item");
}
this.Insert (index, (ListViewSubItem) item);
}
void IList.Remove (object item)
{
if (! (item is ListViewSubItem)) {
throw new ArgumentException ("Not of type ListViewSubItem", "item");
}
this.Remove ((ListViewSubItem) item);
}
public int IndexOf (ListViewSubItem subItem)
{
return list.IndexOf (subItem);
}
public virtual int IndexOfKey (string key)
{
if (key == null || key.Length == 0)
return -1;
for (int i = 0; i < list.Count; i++) {
ListViewSubItem l = (ListViewSubItem) list [i];
if (String.Compare (l.Name, key, true) == 0)
return i;
}
return -1;
}
public void Insert (int index, ListViewSubItem item)
{
item.owner = this.owner;
list.Insert (index, item);
owner.Layout ();
owner.Invalidate ();
//UIA Framework
item.UIATextChanged += OnUIASubItemTextChanged;
}
public void Remove (ListViewSubItem item)
{
list.Remove (item);
owner.Layout ();
owner.Invalidate ();
//UIA Framework
item.UIATextChanged -= OnUIASubItemTextChanged;
}
public virtual void RemoveByKey (string key)
{
int idx = IndexOfKey (key);
if (idx != -1)
RemoveAt (idx);
}
public void RemoveAt (int index)
{
//UIA Framework
if (index >= 0 && index < list.Count)
((ListViewSubItem) list [index]).UIATextChanged -= OnUIASubItemTextChanged;
list.RemoveAt (index);
}
#endregion // Public Methods
#region UIA Event Handler
private void OnUIASubItemTextChanged (object sender, EventArgs args)
{
owner.OnUIASubItemTextChanged (new LabelEditEventArgs (list.IndexOf (sender)));
}
#endregion
}
#endregion // Subclasses
}
}
|