2 * Copyright (C) 2009 Adam Kropelin
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General
6 * Public License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the Free
15 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 ListView::ListView(HWND hwnd
, UINT id
, int cols
) :
27 _hwnd
= GetDlgItem(hwnd
, id
);
30 lvc
.mask
= LVCF_SUBITEM
;
31 for (int i
= 0; i
< cols
; i
++)
34 SendMessage(_hwnd
, LVM_INSERTCOLUMN
, i
, (LONG
)&lvc
);
38 int ListView::AppendItem(const char *text
)
44 lvi
.pszText
= (char*)text
;
46 return SendMessage(_hwnd
, LVM_INSERTITEM
, 0, (LONG
)&lvi
);
49 void ListView::UpdateItem(int item
, int sub
, const char *text
)
58 lvi
.cchTextMax
= sizeof(str
);
60 int len
= SendMessage(_hwnd
, LVM_GETITEMTEXT
, item
, (LONG
)&lvi
);
61 if (len
== 0 || strcmp(str
, text
))
63 lvi
.pszText
= (char*)text
;
64 SendMessage(_hwnd
, LVM_SETITEMTEXT
, item
, (LONG
)&lvi
);
68 int ListView::NumItems()
70 return SendMessage(_hwnd
, LVM_GETITEMCOUNT
, 0, 0);
73 void ListView::Autosize()
75 for (int i
= 0; i
< _cols
; i
++)
76 SendMessage(_hwnd
, LVM_SETCOLUMNWIDTH
, i
, LVSCW_AUTOSIZE
);
79 void ListView::DeleteItem(int item
)
81 SendMessage(_hwnd
, LVM_DELETEITEM
, item
, 0);
84 void ListView::UpdateAll(alist
<astring
>* data
[])
86 // The simple way to update the listview would be to remove all items and
87 // then add them again. However, that causes the control to flicker and the
88 // scrollbar to reset to the top every time, which makes it pretty much
89 // unusable. To prevent that, we update the items in-place, adding new ones
90 // and removing unused ones as necessary. That way the scroll position stays
91 // put and only the items that change are redrawn.
93 // Get current item count and prepare to update the listview
97 // Add each line to the listview
98 alist
<astring
>::const_iterator iter
;
99 for (iter
= data
[0]->begin(); iter
!= data
[0]->end(); ++iter
)
101 // Set main item (leftmost column). This will be an insert if there is no
102 // existing item at this position or an update if an item already exists.
106 UpdateItem(count
, 0, *iter
);
108 // On to the next item
112 // Remove any leftover items that are no longer needed.
119 // Update remaining columns. This is always an update since main item
120 // (leftmost column) is guaranteed to exist by code above.
121 for (int i
= 1; i
< _cols
; i
++)
124 alist
<astring
>::const_iterator iter
;
125 for (iter
= data
[i
]->begin(); iter
!= data
[i
]->end(); ++iter
)
126 UpdateItem(count
++, i
, *iter
);
129 // Autosize listview columns