UPS: apcupsd clean sources
[tomato.git] / release / src / router / apcupsd / src / win32 / listview.cpp
blob8796bdc5001a367982222f2b80b379f9822708fb
1 /*
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,
16 * MA 02111-1307, USA.
19 #include <windows.h>
20 #include <commctrl.h>
21 #include <limits.h>
22 #include "listview.h"
24 ListView::ListView(HWND hwnd, UINT id, int cols) :
25 _cols(cols)
27 _hwnd = GetDlgItem(hwnd, id);
29 LVCOLUMN lvc;
30 lvc.mask = LVCF_SUBITEM;
31 for (int i = 0; i < cols; i++)
33 lvc.iSubItem = i;
34 SendMessage(_hwnd, LVM_INSERTCOLUMN, i, (LONG)&lvc);
38 int ListView::AppendItem(const char *text)
40 LVITEM lvi;
41 lvi.mask = LVIF_TEXT;
42 lvi.iItem = INT_MAX;
43 lvi.iSubItem = 0;
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)
51 char str[256];
53 LVITEM lvi;
54 lvi.mask = LVIF_TEXT;
55 lvi.iItem = item;
56 lvi.iSubItem = sub;
57 lvi.pszText = str;
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
94 int num = NumItems();
95 int count = 0;
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.
103 if (count >= num)
104 AppendItem(*iter);
105 else
106 UpdateItem(count, 0, *iter);
108 // On to the next item
109 count++;
112 // Remove any leftover items that are no longer needed.
113 while (count < num)
115 DeleteItem(count);
116 num--;
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++)
123 int count = 0;
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
130 Autosize();