]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
648bec3e | 2 | // Name: src/msw/listctrl.cpp |
2bda0e17 KB |
3 | // Purpose: wxListCtrl |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa JS |
8 | // Copyright: (c) Julian Smart |
9 | // Licence: wxWindows licence | |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
edccf428 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 | 20 | #ifdef __GNUG__ |
0b5efdc7 | 21 | #pragma implementation "listctrl.h" |
3c1a88d8 | 22 | #pragma implementation "listctrlbase.h" |
2bda0e17 KB |
23 | #endif |
24 | ||
25 | // For compilers that support precompilation, includes "wx.h". | |
26 | #include "wx/wxprec.h" | |
27 | ||
28 | #ifdef __BORLANDC__ | |
0b5efdc7 | 29 | #pragma hdrstop |
2bda0e17 KB |
30 | #endif |
31 | ||
98ec9dbe | 32 | #if wxUSE_LISTCTRL && defined(__WIN95__) |
9f303149 | 33 | |
2bda0e17 | 34 | #ifndef WX_PRECOMP |
9f303149 | 35 | #include "wx/app.h" |
9f303149 VZ |
36 | #include "wx/intl.h" |
37 | #include "wx/log.h" | |
38 | #include "wx/settings.h" | |
2bda0e17 KB |
39 | #endif |
40 | ||
de8e98f1 JS |
41 | #include "wx/textctrl.h" |
42 | #include "wx/imaglist.h" | |
2bda0e17 | 43 | #include "wx/listctrl.h" |
68fdcf29 | 44 | #include "wx/dcclient.h" |
2bda0e17 KB |
45 | |
46 | #include "wx/msw/private.h" | |
47 | ||
ae090fdb | 48 | #if ((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__)) |
edccf428 VZ |
49 | #include "wx/msw/gnuwin32/extra.h" |
50 | #else | |
0b5efdc7 | 51 | #include <commctrl.h> |
2bda0e17 KB |
52 | #endif |
53 | ||
7391216e | 54 | #include "wx/msw/missing.h" |
12979259 | 55 | |
edccf428 VZ |
56 | // ---------------------------------------------------------------------------- |
57 | // private functions | |
58 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 59 | |
8dff2b5c VZ |
60 | // convert our state and mask flags to LV_ITEM constants |
61 | static void wxConvertToMSWFlags(long state, long mask, LV_ITEM& lvItem); | |
62 | ||
63 | // convert wxListItem to LV_ITEM | |
64 | static void wxConvertToMSWListItem(const wxListCtrl *ctrl, | |
65 | const wxListItem& info, LV_ITEM& lvItem); | |
66 | ||
67 | // convert LV_ITEM to wxListItem | |
68 | static void wxConvertFromMSWListItem(HWND hwndListCtrl, | |
69 | wxListItem& info, | |
70 | /* const */ LV_ITEM& lvItem); | |
2bda0e17 | 71 | |
6f806543 VZ |
72 | // convert our wxListItem to LV_COLUMN |
73 | static void wxConvertToMSWListCol(int col, const wxListItem& item, | |
74 | LV_COLUMN& lvCol); | |
75 | ||
8c21905b VS |
76 | // ---------------------------------------------------------------------------- |
77 | // private helper classes | |
78 | // ---------------------------------------------------------------------------- | |
79 | ||
80 | // We have to handle both fooW and fooA notifications in several cases | |
81 | // because of broken commctl.dll and/or unicows.dll. This class is used to | |
73d8c5fd | 82 | // convert LV_ITEMA and LV_ITEMW to LV_ITEM (which is either LV_ITEMA or |
8c21905b VS |
83 | // LV_ITEMW depending on wxUSE_UNICODE setting), so that it can be processed |
84 | // by wxConvertToMSWListItem(). | |
85 | class wxLV_ITEM | |
86 | { | |
87 | public: | |
88 | ~wxLV_ITEM() { delete m_buf; } | |
89 | operator LV_ITEM&() const { return *m_item; } | |
90 | ||
91 | #if wxUSE_UNICODE | |
92 | wxLV_ITEM(LV_ITEMW &item) : m_buf(NULL), m_item(&item) {} | |
93 | wxLV_ITEM(LV_ITEMA &item) | |
94 | { | |
95 | m_item = new LV_ITEM((LV_ITEM&)item); | |
96 | if ( (item.mask & LVIF_TEXT) && item.pszText ) | |
97 | { | |
98 | m_buf = new wxMB2WXbuf(wxConvLocal.cMB2WX(item.pszText)); | |
99 | m_item->pszText = (wxChar*)m_buf->data(); | |
100 | } | |
101 | else | |
102 | m_buf = NULL; | |
73d8c5fd | 103 | } |
8c21905b VS |
104 | private: |
105 | wxMB2WXbuf *m_buf; | |
106 | ||
2b5f62a0 | 107 | #else // !wxUSE_UNICODE |
8c21905b VS |
108 | wxLV_ITEM(LV_ITEMW &item) |
109 | { | |
110 | m_item = new LV_ITEM((LV_ITEM&)item); | |
2b5f62a0 VZ |
111 | |
112 | // the code below doesn't compile without wxUSE_WCHAR_T and as I don't | |
113 | // know if it's useful to have it at all (do we ever get Unicode | |
114 | // notifications in ANSI mode? I don't think so...) I'm not going to | |
115 | // write alternative implementation right now | |
116 | // | |
117 | // but if it is indeed used, we should simply directly use | |
118 | // ::WideCharToMultiByte() here | |
119 | #if wxUSE_WCHAR_T | |
8c21905b VS |
120 | if ( (item.mask & LVIF_TEXT) && item.pszText ) |
121 | { | |
122 | m_buf = new wxWC2WXbuf(wxConvLocal.cWC2WX(item.pszText)); | |
123 | m_item->pszText = (wxChar*)m_buf->data(); | |
124 | } | |
125 | else | |
2b5f62a0 | 126 | #endif // wxUSE_WCHAR_T |
8c21905b | 127 | m_buf = NULL; |
73d8c5fd | 128 | } |
8c21905b VS |
129 | wxLV_ITEM(LV_ITEMA &item) : m_buf(NULL), m_item(&item) {} |
130 | private: | |
131 | wxWC2WXbuf *m_buf; | |
2b5f62a0 | 132 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE |
8c21905b VS |
133 | |
134 | LV_ITEM *m_item; | |
22f3361e VZ |
135 | |
136 | DECLARE_NO_COPY_CLASS(wxLV_ITEM) | |
8c21905b VS |
137 | }; |
138 | ||
7cf46fdb VZ |
139 | /////////////////////////////////////////////////////// |
140 | // Problem: | |
73d8c5fd RD |
141 | // The MSW version had problems with SetTextColour() et |
142 | // al as the wxListItemAttr's were stored keyed on the | |
143 | // item index. If a item was inserted anywhere but the end | |
144 | // of the list the the text attributes (colour etc) for | |
7cf46fdb | 145 | // the following items were out of sync. |
73d8c5fd | 146 | // |
7cf46fdb | 147 | // Solution: |
73d8c5fd RD |
148 | // Under MSW the only way to associate data with a List |
149 | // item independant of its position in the list is to | |
150 | // store a pointer to it in its lParam attribute. However | |
151 | // user programs are already using this (via the | |
7cf46fdb | 152 | // SetItemData() GetItemData() calls). |
73d8c5fd RD |
153 | // |
154 | // However what we can do is store a pointer to a | |
155 | // structure which contains the attributes we want *and* | |
7cf46fdb | 156 | // a lParam for the users data, e.g. |
73d8c5fd | 157 | // |
7cf46fdb VZ |
158 | // class wxListItemInternalData |
159 | // { | |
160 | // public: | |
73d8c5fd | 161 | // wxListItemAttr *attr; |
7cf46fdb VZ |
162 | // long lParam; // user data |
163 | // }; | |
73d8c5fd RD |
164 | // |
165 | // To conserve memory, a wxListItemInternalData is | |
166 | // only allocated for a LV_ITEM if text attributes or | |
7cf46fdb VZ |
167 | // user data(lparam) are being set. |
168 | ||
169 | ||
170 | // class wxListItemInternalData | |
171 | class wxListItemInternalData | |
172 | { | |
173 | public: | |
73d8c5fd | 174 | wxListItemAttr *attr; |
7cf46fdb VZ |
175 | LPARAM lParam; // user data |
176 | ||
177 | wxListItemInternalData() : attr(NULL), lParam(0) {} | |
178 | ~wxListItemInternalData() | |
179 | { | |
180 | if (attr) | |
181 | delete attr; | |
182 | }; | |
22f3361e VZ |
183 | |
184 | DECLARE_NO_COPY_CLASS(wxListItemInternalData) | |
7cf46fdb VZ |
185 | }; |
186 | ||
187 | // Get the internal data structure | |
6149de71 RD |
188 | static wxListItemInternalData *wxGetInternalData(HWND hwnd, long itemId); |
189 | static wxListItemInternalData *wxGetInternalData(wxListCtrl *ctl, long itemId); | |
190 | static wxListItemAttr *wxGetInternalDataAttr(wxListCtrl *ctl, long itemId); | |
191 | static void wxDeleteInternalData(wxListCtrl* ctl, long itemId); | |
7cf46fdb VZ |
192 | |
193 | ||
edccf428 | 194 | // ---------------------------------------------------------------------------- |
2e4df4bf | 195 | // events |
edccf428 VZ |
196 | // ---------------------------------------------------------------------------- |
197 | ||
2e4df4bf VZ |
198 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG) |
199 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG) | |
200 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT) | |
201 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT) | |
202 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM) | |
203 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS) | |
204 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO) | |
205 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO) | |
206 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED) | |
207 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED) | |
208 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN) | |
209 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM) | |
210 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK) | |
11358d39 VZ |
211 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK) |
212 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG) | |
213 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING) | |
214 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG) | |
2e4df4bf VZ |
215 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK) |
216 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK) | |
217 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED) | |
0ddefeb0 | 218 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED) |
614391dc | 219 | DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT) |
2e4df4bf | 220 | |
8f177c8e | 221 | IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxControl) |
bf9b6266 | 222 | IMPLEMENT_DYNAMIC_CLASS(wxListView, wxListCtrl) |
8f177c8e | 223 | IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject) |
2bda0e17 | 224 | |
2d33aec9 VZ |
225 | IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent) |
226 | ||
2702ed5a JS |
227 | BEGIN_EVENT_TABLE(wxListCtrl, wxControl) |
228 | EVT_PAINT(wxListCtrl::OnPaint) | |
229 | END_EVENT_TABLE() | |
230 | ||
edccf428 VZ |
231 | // ============================================================================ |
232 | // implementation | |
233 | // ============================================================================ | |
234 | ||
235 | // ---------------------------------------------------------------------------- | |
236 | // wxListCtrl construction | |
237 | // ---------------------------------------------------------------------------- | |
238 | ||
bdc72a22 | 239 | void wxListCtrl::Init() |
2bda0e17 | 240 | { |
0b5efdc7 VZ |
241 | m_imageListNormal = NULL; |
242 | m_imageListSmall = NULL; | |
243 | m_imageListState = NULL; | |
2e12c11a | 244 | m_ownsImageListNormal = m_ownsImageListSmall = m_ownsImageListState = FALSE; |
0b5efdc7 | 245 | m_baseStyle = 0; |
2bda0e17 | 246 | m_colCount = 0; |
68c124a1 RD |
247 | m_count = 0; |
248 | m_ignoreChangeMessages = FALSE; | |
bbcdf8bc | 249 | m_textCtrl = NULL; |
7cf46fdb | 250 | m_AnyInternalData = FALSE; |
bdc72a22 | 251 | m_hasAnyAttr = FALSE; |
2bda0e17 KB |
252 | } |
253 | ||
0b5efdc7 VZ |
254 | bool wxListCtrl::Create(wxWindow *parent, |
255 | wxWindowID id, | |
256 | const wxPoint& pos, | |
257 | const wxSize& size, | |
258 | long style, | |
259 | const wxValidator& validator, | |
260 | const wxString& name) | |
2bda0e17 | 261 | { |
11b6a93b | 262 | #if wxUSE_VALIDATORS |
0b5efdc7 | 263 | SetValidator(validator); |
11b6a93b VZ |
264 | #endif // wxUSE_VALIDATORS |
265 | ||
0b5efdc7 | 266 | SetName(name); |
2bda0e17 | 267 | |
0b5efdc7 VZ |
268 | int x = pos.x; |
269 | int y = pos.y; | |
270 | int width = size.x; | |
271 | int height = size.y; | |
2bda0e17 | 272 | |
0b5efdc7 | 273 | m_windowStyle = style; |
2bda0e17 | 274 | |
0b5efdc7 | 275 | SetParent(parent); |
2bda0e17 | 276 | |
0b5efdc7 VZ |
277 | if (width <= 0) |
278 | width = 100; | |
279 | if (height <= 0) | |
280 | height = 30; | |
281 | if (x < 0) | |
282 | x = 0; | |
283 | if (y < 0) | |
284 | y = 0; | |
2bda0e17 | 285 | |
0b5efdc7 | 286 | m_windowId = (id == -1) ? NewControlId() : id; |
2bda0e17 | 287 | |
edccf428 VZ |
288 | DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP | |
289 | LVS_SHAREIMAGELISTS | LVS_SHOWSELALWAYS; | |
b0766406 JS |
290 | |
291 | if ( m_windowStyle & wxCLIP_SIBLINGS ) | |
292 | wstyle |= WS_CLIPSIBLINGS; | |
293 | ||
fe3d9123 | 294 | /* |
edccf428 VZ |
295 | if ( wxStyleHasBorder(m_windowStyle) ) |
296 | wstyle |= WS_BORDER; | |
fe3d9123 JS |
297 | */ |
298 | ||
edccf428 VZ |
299 | m_baseStyle = wstyle; |
300 | ||
301 | if ( !DoCreateControl(x, y, width, height) ) | |
302 | return FALSE; | |
303 | ||
304 | if (parent) | |
305 | parent->AddChild(this); | |
306 | ||
307 | return TRUE; | |
308 | } | |
309 | ||
310 | bool wxListCtrl::DoCreateControl(int x, int y, int w, int h) | |
311 | { | |
312 | DWORD wstyle = m_baseStyle; | |
2bda0e17 | 313 | |
fe3d9123 JS |
314 | WXDWORD exStyle = 0; |
315 | (void) MSWGetStyle(GetWindowStyle(), & exStyle) ; | |
2bda0e17 | 316 | |
0b5efdc7 VZ |
317 | long oldStyle = 0; // Dummy |
318 | wstyle |= ConvertToMSWStyle(oldStyle, m_windowStyle); | |
2bda0e17 | 319 | |
0b5efdc7 VZ |
320 | // Create the ListView control. |
321 | m_hWnd = (WXHWND)CreateWindowEx(exStyle, | |
322 | WC_LISTVIEW, | |
223d09f6 | 323 | wxT(""), |
0b5efdc7 | 324 | wstyle, |
edccf428 VZ |
325 | x, y, w, h, |
326 | GetWinHwnd(GetParent()), | |
0b5efdc7 VZ |
327 | (HMENU)m_windowId, |
328 | wxGetInstance(), | |
329 | NULL); | |
f8352807 | 330 | |
edccf428 VZ |
331 | if ( !m_hWnd ) |
332 | { | |
f6bcfd97 | 333 | wxLogError(_("Can't create list control window, check that comctl32.dll is installed.")); |
0b5efdc7 VZ |
334 | |
335 | return FALSE; | |
336 | } | |
337 | ||
338 | // for comctl32.dll v 4.70+ we want to have this attribute because it's | |
339 | // prettier (and also because wxGTK does it like this) | |
225fe9d6 | 340 | if ( (wstyle & LVS_REPORT) && wxTheApp->GetComCtl32Version() >= 470 ) |
0b5efdc7 | 341 | { |
225fe9d6 VZ |
342 | ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE, |
343 | 0, LVS_EX_FULLROWSELECT); | |
0b5efdc7 | 344 | } |
f8352807 | 345 | |
a756f210 | 346 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
edccf428 | 347 | SetForegroundColour(GetParent()->GetForegroundColour()); |
2bda0e17 | 348 | |
edccf428 | 349 | SubclassWin(m_hWnd); |
2bda0e17 | 350 | |
0b5efdc7 | 351 | return TRUE; |
2bda0e17 KB |
352 | } |
353 | ||
edccf428 VZ |
354 | void wxListCtrl::UpdateStyle() |
355 | { | |
356 | if ( GetHWND() ) | |
357 | { | |
358 | // The new window view style | |
359 | long dummy; | |
360 | DWORD dwStyleNew = ConvertToMSWStyle(dummy, m_windowStyle); | |
361 | dwStyleNew |= m_baseStyle; | |
362 | ||
910484a6 | 363 | // Get the current window style. |
edccf428 VZ |
364 | DWORD dwStyleOld = ::GetWindowLong(GetHwnd(), GWL_STYLE); |
365 | ||
910484a6 | 366 | // Only set the window style if the view bits have changed. |
edccf428 VZ |
367 | if ( dwStyleOld != dwStyleNew ) |
368 | { | |
369 | ::SetWindowLong(GetHwnd(), GWL_STYLE, dwStyleNew); | |
370 | } | |
371 | } | |
372 | } | |
373 | ||
7cf46fdb | 374 | void wxListCtrl::FreeAllInternalData() |
2bda0e17 | 375 | { |
7cf46fdb | 376 | if (m_AnyInternalData) |
73d8c5fd | 377 | { |
7cf46fdb VZ |
378 | int n = GetItemCount(); |
379 | int i = 0; | |
6932a32c | 380 | |
68c124a1 | 381 | m_ignoreChangeMessages = TRUE; |
7cf46fdb | 382 | for (i = 0; i < n; i++) |
6149de71 | 383 | wxDeleteInternalData(this, i); |
3cfda0b7 | 384 | m_ignoreChangeMessages = FALSE; |
6149de71 | 385 | |
73d8c5fd RD |
386 | m_AnyInternalData = FALSE; |
387 | } | |
6932a32c | 388 | } |
d62228a6 | 389 | |
6932a32c VZ |
390 | wxListCtrl::~wxListCtrl() |
391 | { | |
7cf46fdb | 392 | FreeAllInternalData(); |
91b4c08d | 393 | |
d62228a6 | 394 | if ( m_textCtrl ) |
bbcdf8bc | 395 | { |
7bf1474a | 396 | m_textCtrl->UnsubclassWin(); |
f3076f9f | 397 | m_textCtrl->SetHWND(0); |
0b5efdc7 VZ |
398 | delete m_textCtrl; |
399 | m_textCtrl = NULL; | |
bbcdf8bc | 400 | } |
33ac7e6f | 401 | |
2e12c11a VS |
402 | if (m_ownsImageListNormal) delete m_imageListNormal; |
403 | if (m_ownsImageListSmall) delete m_imageListSmall; | |
404 | if (m_ownsImageListState) delete m_imageListState; | |
2bda0e17 KB |
405 | } |
406 | ||
edccf428 VZ |
407 | // ---------------------------------------------------------------------------- |
408 | // set/get/change style | |
409 | // ---------------------------------------------------------------------------- | |
410 | ||
2bda0e17 | 411 | // Add or remove a single window style |
debe6624 | 412 | void wxListCtrl::SetSingleStyle(long style, bool add) |
2bda0e17 | 413 | { |
0b5efdc7 | 414 | long flag = GetWindowStyleFlag(); |
2bda0e17 | 415 | |
0b5efdc7 | 416 | // Get rid of conflicting styles |
acb62b84 VZ |
417 | if ( add ) |
418 | { | |
0b5efdc7 | 419 | if ( style & wxLC_MASK_TYPE) |
edccf428 | 420 | flag = flag & ~wxLC_MASK_TYPE; |
0b5efdc7 | 421 | if ( style & wxLC_MASK_ALIGN ) |
edccf428 | 422 | flag = flag & ~wxLC_MASK_ALIGN; |
0b5efdc7 | 423 | if ( style & wxLC_MASK_SORT ) |
edccf428 | 424 | flag = flag & ~wxLC_MASK_SORT; |
acb62b84 | 425 | } |
2bda0e17 | 426 | |
0b5efdc7 VZ |
427 | if ( flag & style ) |
428 | { | |
429 | if ( !add ) | |
430 | flag -= style; | |
431 | } | |
432 | else | |
433 | { | |
434 | if ( add ) | |
435 | { | |
436 | flag |= style; | |
437 | } | |
438 | } | |
439 | ||
440 | m_windowStyle = flag; | |
2bda0e17 | 441 | |
edccf428 | 442 | UpdateStyle(); |
2bda0e17 KB |
443 | } |
444 | ||
445 | // Set the whole window style | |
debe6624 | 446 | void wxListCtrl::SetWindowStyleFlag(long flag) |
2bda0e17 | 447 | { |
0b5efdc7 | 448 | m_windowStyle = flag; |
2bda0e17 | 449 | |
edccf428 | 450 | UpdateStyle(); |
2bda0e17 KB |
451 | } |
452 | ||
0b5efdc7 VZ |
453 | // Can be just a single style, or a bitlist |
454 | long wxListCtrl::ConvertToMSWStyle(long& oldStyle, long style) const | |
2bda0e17 | 455 | { |
0b5efdc7 VZ |
456 | long wstyle = 0; |
457 | if ( style & wxLC_ICON ) | |
458 | { | |
459 | if ( (oldStyle & LVS_TYPEMASK) == LVS_SMALLICON ) | |
460 | oldStyle -= LVS_SMALLICON; | |
461 | if ( (oldStyle & LVS_TYPEMASK) == LVS_REPORT ) | |
462 | oldStyle -= LVS_REPORT; | |
463 | if ( (oldStyle & LVS_TYPEMASK) == LVS_LIST ) | |
464 | oldStyle -= LVS_LIST; | |
465 | wstyle |= LVS_ICON; | |
466 | } | |
467 | ||
468 | if ( style & wxLC_SMALL_ICON ) | |
469 | { | |
470 | if ( (oldStyle & LVS_TYPEMASK) == LVS_ICON ) | |
471 | oldStyle -= LVS_ICON; | |
472 | if ( (oldStyle & LVS_TYPEMASK) == LVS_REPORT ) | |
473 | oldStyle -= LVS_REPORT; | |
474 | if ( (oldStyle & LVS_TYPEMASK) == LVS_LIST ) | |
475 | oldStyle -= LVS_LIST; | |
476 | wstyle |= LVS_SMALLICON; | |
477 | } | |
478 | ||
479 | if ( style & wxLC_LIST ) | |
480 | { | |
481 | if ( (oldStyle & LVS_TYPEMASK) == LVS_ICON ) | |
482 | oldStyle -= LVS_ICON; | |
483 | if ( (oldStyle & LVS_TYPEMASK) == LVS_REPORT ) | |
484 | oldStyle -= LVS_REPORT; | |
485 | if ( (oldStyle & LVS_TYPEMASK) == LVS_SMALLICON ) | |
486 | oldStyle -= LVS_SMALLICON; | |
487 | wstyle |= LVS_LIST; | |
488 | } | |
2bda0e17 | 489 | |
0b5efdc7 VZ |
490 | if ( style & wxLC_REPORT ) |
491 | { | |
492 | if ( (oldStyle & LVS_TYPEMASK) == LVS_ICON ) | |
493 | oldStyle -= LVS_ICON; | |
494 | if ( (oldStyle & LVS_TYPEMASK) == LVS_LIST ) | |
495 | oldStyle -= LVS_LIST; | |
496 | if ( (oldStyle & LVS_TYPEMASK) == LVS_SMALLICON ) | |
497 | oldStyle -= LVS_SMALLICON; | |
498 | ||
499 | wstyle |= LVS_REPORT; | |
500 | } | |
2bda0e17 | 501 | |
0b5efdc7 VZ |
502 | if ( style & wxLC_ALIGN_LEFT ) |
503 | { | |
504 | if ( oldStyle & LVS_ALIGNTOP ) | |
505 | oldStyle -= LVS_ALIGNTOP; | |
506 | wstyle |= LVS_ALIGNLEFT; | |
507 | } | |
2bda0e17 | 508 | |
0b5efdc7 VZ |
509 | if ( style & wxLC_ALIGN_TOP ) |
510 | { | |
511 | if ( oldStyle & LVS_ALIGNLEFT ) | |
512 | oldStyle -= LVS_ALIGNLEFT; | |
513 | wstyle |= LVS_ALIGNTOP; | |
514 | } | |
2bda0e17 | 515 | |
0b5efdc7 VZ |
516 | if ( style & wxLC_AUTOARRANGE ) |
517 | wstyle |= LVS_AUTOARRANGE; | |
2bda0e17 | 518 | |
0b5efdc7 VZ |
519 | if ( style & wxLC_NO_SORT_HEADER ) |
520 | wstyle |= LVS_NOSORTHEADER; | |
2bda0e17 | 521 | |
0b5efdc7 VZ |
522 | if ( style & wxLC_NO_HEADER ) |
523 | wstyle |= LVS_NOCOLUMNHEADER; | |
524 | ||
525 | if ( style & wxLC_EDIT_LABELS ) | |
526 | wstyle |= LVS_EDITLABELS; | |
527 | ||
528 | if ( style & wxLC_SINGLE_SEL ) | |
529 | wstyle |= LVS_SINGLESEL; | |
530 | ||
531 | if ( style & wxLC_SORT_ASCENDING ) | |
532 | { | |
533 | if ( oldStyle & LVS_SORTDESCENDING ) | |
534 | oldStyle -= LVS_SORTDESCENDING; | |
535 | wstyle |= LVS_SORTASCENDING; | |
536 | } | |
537 | ||
538 | if ( style & wxLC_SORT_DESCENDING ) | |
539 | { | |
540 | if ( oldStyle & LVS_SORTASCENDING ) | |
541 | oldStyle -= LVS_SORTASCENDING; | |
542 | wstyle |= LVS_SORTDESCENDING; | |
543 | } | |
544 | ||
bf9b6266 | 545 | #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) |
98ec9dbe VZ |
546 | if ( style & wxLC_VIRTUAL ) |
547 | { | |
30a72e62 VZ |
548 | int ver = wxTheApp->GetComCtl32Version(); |
549 | if ( ver < 470 ) | |
550 | { | |
bf9b6266 | 551 | wxLogWarning(_("Please install a newer version of comctl32.dll\n(at least version 4.70 is required but you have %d.%02d)\nor this program won't operate correctly."), |
30a72e62 VZ |
552 | ver / 100, ver % 100); |
553 | } | |
554 | ||
98ec9dbe VZ |
555 | wstyle |= LVS_OWNERDATA; |
556 | } | |
bf9b6266 | 557 | #endif |
98ec9dbe | 558 | |
0b5efdc7 | 559 | return wstyle; |
2bda0e17 KB |
560 | } |
561 | ||
edccf428 VZ |
562 | // ---------------------------------------------------------------------------- |
563 | // accessors | |
564 | // ---------------------------------------------------------------------------- | |
565 | ||
91b4c08d VZ |
566 | // Sets the foreground, i.e. text, colour |
567 | bool wxListCtrl::SetForegroundColour(const wxColour& col) | |
568 | { | |
569 | if ( !wxWindow::SetForegroundColour(col) ) | |
570 | return FALSE; | |
571 | ||
572 | ListView_SetTextColor(GetHwnd(), wxColourToRGB(col)); | |
573 | ||
574 | return TRUE; | |
575 | } | |
576 | ||
577 | // Sets the background colour | |
cc2b7472 | 578 | bool wxListCtrl::SetBackgroundColour(const wxColour& col) |
2bda0e17 | 579 | { |
cc2b7472 VZ |
580 | if ( !wxWindow::SetBackgroundColour(col) ) |
581 | return FALSE; | |
2bda0e17 | 582 | |
91b4c08d VZ |
583 | // we set the same colour for both the "empty" background and the items |
584 | // background | |
585 | COLORREF color = wxColourToRGB(col); | |
586 | ListView_SetBkColor(GetHwnd(), color); | |
587 | ListView_SetTextBkColor(GetHwnd(), color); | |
cc2b7472 VZ |
588 | |
589 | return TRUE; | |
2bda0e17 KB |
590 | } |
591 | ||
592 | // Gets information about this column | |
debe6624 | 593 | bool wxListCtrl::GetColumn(int col, wxListItem& item) const |
2bda0e17 | 594 | { |
0b5efdc7 | 595 | LV_COLUMN lvCol; |
6f806543 | 596 | wxZeroMemory(lvCol); |
0b5efdc7 | 597 | |
37094564 RD |
598 | lvCol.mask = LVCF_WIDTH; |
599 | ||
0b5efdc7 VZ |
600 | if ( item.m_mask & wxLIST_MASK_TEXT ) |
601 | { | |
602 | lvCol.mask |= LVCF_TEXT; | |
837e5743 | 603 | lvCol.pszText = new wxChar[513]; |
0b5efdc7 VZ |
604 | lvCol.cchTextMax = 512; |
605 | } | |
606 | ||
37094564 RD |
607 | if ( item.m_mask & wxLIST_MASK_FORMAT ) |
608 | { | |
609 | lvCol.mask |= LVCF_FMT; | |
610 | } | |
611 | ||
612 | if ( item.m_mask & wxLIST_MASK_IMAGE ) | |
613 | { | |
614 | lvCol.mask |= LVCF_IMAGE; | |
615 | } | |
616 | ||
0b190ffc | 617 | bool success = ListView_GetColumn(GetHwnd(), col, &lvCol) != 0; |
0b5efdc7 VZ |
618 | |
619 | // item.m_subItem = lvCol.iSubItem; | |
620 | item.m_width = lvCol.cx; | |
621 | ||
622 | if ( (item.m_mask & wxLIST_MASK_TEXT) && lvCol.pszText ) | |
623 | { | |
624 | item.m_text = lvCol.pszText; | |
625 | delete[] lvCol.pszText; | |
626 | } | |
627 | ||
628 | if ( item.m_mask & wxLIST_MASK_FORMAT ) | |
629 | { | |
0b190ffc RD |
630 | switch (lvCol.fmt & LVCFMT_JUSTIFYMASK) { |
631 | case LVCFMT_LEFT: | |
632 | item.m_format = wxLIST_FORMAT_LEFT; | |
633 | break; | |
634 | case LVCFMT_RIGHT: | |
635 | item.m_format = wxLIST_FORMAT_RIGHT; | |
636 | break; | |
637 | case LVCFMT_CENTER: | |
638 | item.m_format = wxLIST_FORMAT_CENTRE; | |
639 | break; | |
640 | default: | |
641 | item.m_format = -1; // Unknown? | |
642 | break; | |
643 | } | |
2bda0e17 KB |
644 | } |
645 | ||
8ac67aa1 | 646 | #if _WIN32_IE >= 0x0300 |
37094564 RD |
647 | if ( item.m_mask & wxLIST_MASK_IMAGE ) |
648 | { | |
649 | item.m_image = lvCol.iImage; | |
650 | } | |
8ac67aa1 | 651 | #endif |
37094564 | 652 | |
0b5efdc7 | 653 | return success; |
2bda0e17 KB |
654 | } |
655 | ||
656 | // Sets information about this column | |
debe6624 | 657 | bool wxListCtrl::SetColumn(int col, wxListItem& item) |
2bda0e17 | 658 | { |
0b5efdc7 | 659 | LV_COLUMN lvCol; |
6f806543 | 660 | wxConvertToMSWListCol(col, item, lvCol); |
0b5efdc7 | 661 | |
6f806543 | 662 | return ListView_SetColumn(GetHwnd(), col, &lvCol) != 0; |
2bda0e17 KB |
663 | } |
664 | ||
665 | // Gets the column width | |
debe6624 | 666 | int wxListCtrl::GetColumnWidth(int col) const |
2bda0e17 | 667 | { |
edccf428 | 668 | return ListView_GetColumnWidth(GetHwnd(), col); |
2bda0e17 KB |
669 | } |
670 | ||
671 | // Sets the column width | |
debe6624 | 672 | bool wxListCtrl::SetColumnWidth(int col, int width) |
2bda0e17 | 673 | { |
0b5efdc7 VZ |
674 | int col2 = col; |
675 | if ( m_windowStyle & wxLC_LIST ) | |
676 | col2 = -1; | |
2bda0e17 | 677 | |
0b5efdc7 VZ |
678 | int width2 = width; |
679 | if ( width2 == wxLIST_AUTOSIZE) | |
680 | width2 = LVSCW_AUTOSIZE; | |
681 | else if ( width2 == wxLIST_AUTOSIZE_USEHEADER) | |
682 | width2 = LVSCW_AUTOSIZE_USEHEADER; | |
2bda0e17 | 683 | |
6f806543 | 684 | return ListView_SetColumnWidth(GetHwnd(), col2, width2) != 0; |
2bda0e17 KB |
685 | } |
686 | ||
687 | // Gets the number of items that can fit vertically in the | |
688 | // visible area of the list control (list or report view) | |
689 | // or the total number of items in the list control (icon | |
690 | // or small icon view) | |
c42404a5 | 691 | int wxListCtrl::GetCountPerPage() const |
2bda0e17 | 692 | { |
edccf428 | 693 | return ListView_GetCountPerPage(GetHwnd()); |
2bda0e17 KB |
694 | } |
695 | ||
696 | // Gets the edit control for editing labels. | |
c42404a5 | 697 | wxTextCtrl* wxListCtrl::GetEditControl() const |
2bda0e17 | 698 | { |
bbcdf8bc | 699 | return m_textCtrl; |
2bda0e17 KB |
700 | } |
701 | ||
702 | // Gets information about the item | |
703 | bool wxListCtrl::GetItem(wxListItem& info) const | |
704 | { | |
0b5efdc7 | 705 | LV_ITEM lvItem; |
11c7d5b6 | 706 | wxZeroMemory(lvItem); |
2bda0e17 | 707 | |
0b5efdc7 | 708 | lvItem.iItem = info.m_itemId; |
fc5a93cd | 709 | lvItem.iSubItem = info.m_col; |
0b5efdc7 VZ |
710 | |
711 | if ( info.m_mask & wxLIST_MASK_TEXT ) | |
712 | { | |
713 | lvItem.mask |= LVIF_TEXT; | |
837e5743 | 714 | lvItem.pszText = new wxChar[513]; |
0b5efdc7 VZ |
715 | lvItem.cchTextMax = 512; |
716 | } | |
717 | else | |
718 | { | |
719 | lvItem.pszText = NULL; | |
720 | } | |
721 | ||
722 | if (info.m_mask & wxLIST_MASK_DATA) | |
edccf428 | 723 | lvItem.mask |= LVIF_PARAM; |
0b5efdc7 | 724 | |
2189c644 JS |
725 | if (info.m_mask & wxLIST_MASK_IMAGE) |
726 | lvItem.mask |= LVIF_IMAGE; | |
727 | ||
0b5efdc7 VZ |
728 | if ( info.m_mask & wxLIST_MASK_STATE ) |
729 | { | |
730 | lvItem.mask |= LVIF_STATE; | |
731 | // the other bits are hardly interesting anyhow | |
732 | lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED; | |
733 | } | |
734 | ||
735 | bool success = ListView_GetItem((HWND)GetHWND(), &lvItem) != 0; | |
736 | if ( !success ) | |
737 | { | |
738 | wxLogError(_("Couldn't retrieve information about list control item %d."), | |
739 | lvItem.iItem); | |
740 | } | |
741 | else | |
742 | { | |
dccde0c0 VZ |
743 | // give NULL as hwnd as we already have everything we need |
744 | wxConvertFromMSWListItem(NULL, info, lvItem); | |
0b5efdc7 VZ |
745 | } |
746 | ||
747 | if (lvItem.pszText) | |
748 | delete[] lvItem.pszText; | |
749 | ||
750 | return success; | |
2bda0e17 KB |
751 | } |
752 | ||
753 | // Sets information about the item | |
754 | bool wxListCtrl::SetItem(wxListItem& info) | |
755 | { | |
0b5efdc7 | 756 | LV_ITEM item; |
2bda0e17 | 757 | wxConvertToMSWListItem(this, info, item); |
bdc72a22 | 758 | |
7cf46fdb VZ |
759 | // we never update the lParam if it contains our pointer |
760 | // to the wxListItemInternalData structure | |
761 | item.mask &= ~LVIF_PARAM; | |
762 | ||
763 | // check if setting attributes or lParam | |
764 | if (info.HasAttributes() || (info.m_mask & wxLIST_MASK_DATA)) | |
765 | { | |
766 | // get internal item data | |
767 | // perhaps a cache here ? | |
6149de71 | 768 | wxListItemInternalData *data = wxGetInternalData(this, info.m_itemId); |
73d8c5fd | 769 | |
7cf46fdb VZ |
770 | if (! data) |
771 | { | |
772 | // need to set it | |
773 | m_AnyInternalData = TRUE; | |
774 | data = new wxListItemInternalData(); | |
775 | item.lParam = (LPARAM) data; | |
776 | item.mask |= LVIF_PARAM; | |
777 | }; | |
778 | ||
779 | ||
780 | // user data | |
781 | if (info.m_mask & wxLIST_MASK_DATA) | |
782 | data->lParam = info.m_data; | |
783 | ||
784 | // attributes | |
785 | if (info.HasAttributes()) | |
786 | { | |
787 | if (data->attr) | |
788 | *data->attr = *info.GetAttributes(); | |
789 | else | |
790 | data->attr = new wxListItemAttr(*info.GetAttributes()); | |
791 | }; | |
792 | }; | |
793 | ||
794 | ||
2d33aec9 VZ |
795 | // we could be changing only the attribute in which case we don't need to |
796 | // call ListView_SetItem() at all | |
797 | if ( item.mask ) | |
bdc72a22 | 798 | { |
2d33aec9 VZ |
799 | item.cchTextMax = 0; |
800 | if ( !ListView_SetItem(GetHwnd(), &item) ) | |
801 | { | |
802 | wxLogDebug(_T("ListView_SetItem() failed")); | |
2702ed5a | 803 | |
2d33aec9 VZ |
804 | return FALSE; |
805 | } | |
233d0295 | 806 | } |
2702ed5a | 807 | |
233d0295 VZ |
808 | // we need to update the item immediately to show the new image |
809 | bool updateNow = (info.m_mask & wxLIST_MASK_IMAGE) != 0; | |
2702ed5a | 810 | |
233d0295 VZ |
811 | // check whether it has any custom attributes |
812 | if ( info.HasAttributes() ) | |
813 | { | |
bdc72a22 | 814 | m_hasAnyAttr = TRUE; |
233d0295 VZ |
815 | |
816 | // if the colour has changed, we must redraw the item | |
817 | updateNow = TRUE; | |
bdc72a22 | 818 | } |
bdc72a22 | 819 | |
233d0295 | 820 | if ( updateNow ) |
7cc98b3e | 821 | { |
233d0295 | 822 | // we need this to make the change visible right now |
2d33aec9 | 823 | RefreshItem(item.iItem); |
7cc98b3e VZ |
824 | } |
825 | ||
233d0295 | 826 | return TRUE; |
2bda0e17 KB |
827 | } |
828 | ||
debe6624 | 829 | long wxListCtrl::SetItem(long index, int col, const wxString& label, int imageId) |
2bda0e17 | 830 | { |
0b5efdc7 VZ |
831 | wxListItem info; |
832 | info.m_text = label; | |
833 | info.m_mask = wxLIST_MASK_TEXT; | |
834 | info.m_itemId = index; | |
835 | info.m_col = col; | |
836 | if ( imageId > -1 ) | |
837 | { | |
838 | info.m_image = imageId; | |
839 | info.m_mask |= wxLIST_MASK_IMAGE; | |
840 | } | |
841 | return SetItem(info); | |
2bda0e17 KB |
842 | } |
843 | ||
844 | ||
845 | // Gets the item state | |
debe6624 | 846 | int wxListCtrl::GetItemState(long item, long stateMask) const |
2bda0e17 | 847 | { |
0b5efdc7 | 848 | wxListItem info; |
2bda0e17 | 849 | |
edccf428 | 850 | info.m_mask = wxLIST_MASK_STATE; |
0b5efdc7 VZ |
851 | info.m_stateMask = stateMask; |
852 | info.m_itemId = item; | |
2bda0e17 | 853 | |
0b5efdc7 VZ |
854 | if (!GetItem(info)) |
855 | return 0; | |
2bda0e17 | 856 | |
0b5efdc7 | 857 | return info.m_state; |
2bda0e17 KB |
858 | } |
859 | ||
860 | // Sets the item state | |
debe6624 | 861 | bool wxListCtrl::SetItemState(long item, long state, long stateMask) |
2bda0e17 | 862 | { |
8dff2b5c VZ |
863 | // NB: don't use SetItem() here as it doesn't work with the virtual list |
864 | // controls | |
865 | LV_ITEM lvItem; | |
866 | wxZeroMemory(lvItem); | |
2bda0e17 | 867 | |
8dff2b5c | 868 | wxConvertToMSWFlags(state, stateMask, lvItem); |
2bda0e17 | 869 | |
2d33aec9 | 870 | // for the virtual list controls we need to refresh the previously focused |
ad9f477d VZ |
871 | // item manually when changing focus without changing selection |
872 | // programmatically because otherwise it keeps its focus rectangle until | |
873 | // next repaint (yet another comctl32 bug) | |
2d33aec9 VZ |
874 | long focusOld; |
875 | if ( IsVirtual() && | |
876 | (stateMask & wxLIST_STATE_FOCUSED) && | |
877 | (state & wxLIST_STATE_FOCUSED) ) | |
878 | { | |
879 | focusOld = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED); | |
880 | } | |
881 | else | |
882 | { | |
883 | focusOld = -1; | |
884 | } | |
885 | ||
8dff2b5c VZ |
886 | if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE, |
887 | (WPARAM)item, (LPARAM)&lvItem) ) | |
888 | { | |
889 | wxLogLastError(_T("ListView_SetItemState")); | |
890 | ||
891 | return FALSE; | |
892 | } | |
893 | ||
2d33aec9 VZ |
894 | if ( focusOld != -1 ) |
895 | { | |
ad9f477d VZ |
896 | // no need to refresh the item if it was previously selected, it would |
897 | // only result in annoying flicker | |
898 | if ( !(GetItemState(focusOld, | |
899 | wxLIST_STATE_SELECTED) & wxLIST_STATE_SELECTED) ) | |
900 | { | |
901 | RefreshItem(focusOld); | |
902 | } | |
2d33aec9 VZ |
903 | } |
904 | ||
8dff2b5c | 905 | return TRUE; |
2bda0e17 KB |
906 | } |
907 | ||
908 | // Sets the item image | |
33ac7e6f | 909 | bool wxListCtrl::SetItemImage(long item, int image, int WXUNUSED(selImage)) |
2bda0e17 | 910 | { |
0b5efdc7 | 911 | wxListItem info; |
2bda0e17 | 912 | |
edccf428 | 913 | info.m_mask = wxLIST_MASK_IMAGE; |
0b5efdc7 VZ |
914 | info.m_image = image; |
915 | info.m_itemId = item; | |
2bda0e17 | 916 | |
0b5efdc7 | 917 | return SetItem(info); |
2bda0e17 KB |
918 | } |
919 | ||
920 | // Gets the item text | |
debe6624 | 921 | wxString wxListCtrl::GetItemText(long item) const |
2bda0e17 | 922 | { |
0b5efdc7 | 923 | wxListItem info; |
2bda0e17 | 924 | |
edccf428 | 925 | info.m_mask = wxLIST_MASK_TEXT; |
0b5efdc7 | 926 | info.m_itemId = item; |
2bda0e17 | 927 | |
0b5efdc7 | 928 | if (!GetItem(info)) |
2b5f62a0 | 929 | return wxEmptyString; |
0b5efdc7 | 930 | return info.m_text; |
2bda0e17 KB |
931 | } |
932 | ||
933 | // Sets the item text | |
debe6624 | 934 | void wxListCtrl::SetItemText(long item, const wxString& str) |
2bda0e17 | 935 | { |
0b5efdc7 | 936 | wxListItem info; |
2bda0e17 | 937 | |
edccf428 | 938 | info.m_mask = wxLIST_MASK_TEXT; |
0b5efdc7 VZ |
939 | info.m_itemId = item; |
940 | info.m_text = str; | |
2bda0e17 | 941 | |
0b5efdc7 | 942 | SetItem(info); |
2bda0e17 KB |
943 | } |
944 | ||
945 | // Gets the item data | |
debe6624 | 946 | long wxListCtrl::GetItemData(long item) const |
2bda0e17 | 947 | { |
0b5efdc7 | 948 | wxListItem info; |
2bda0e17 | 949 | |
edccf428 | 950 | info.m_mask = wxLIST_MASK_DATA; |
0b5efdc7 | 951 | info.m_itemId = item; |
2bda0e17 | 952 | |
0b5efdc7 VZ |
953 | if (!GetItem(info)) |
954 | return 0; | |
955 | return info.m_data; | |
2bda0e17 KB |
956 | } |
957 | ||
958 | // Sets the item data | |
debe6624 | 959 | bool wxListCtrl::SetItemData(long item, long data) |
2bda0e17 | 960 | { |
0b5efdc7 | 961 | wxListItem info; |
2bda0e17 | 962 | |
edccf428 | 963 | info.m_mask = wxLIST_MASK_DATA; |
0b5efdc7 VZ |
964 | info.m_itemId = item; |
965 | info.m_data = data; | |
2bda0e17 | 966 | |
0b5efdc7 | 967 | return SetItem(info); |
2bda0e17 KB |
968 | } |
969 | ||
970 | // Gets the item rectangle | |
16e93305 | 971 | bool wxListCtrl::GetItemRect(long item, wxRect& rect, int code) const |
2bda0e17 | 972 | { |
2d33aec9 | 973 | RECT rectWin; |
2bda0e17 | 974 | |
2d33aec9 | 975 | int codeWin; |
0b5efdc7 | 976 | if ( code == wxLIST_RECT_BOUNDS ) |
2d33aec9 | 977 | codeWin = LVIR_BOUNDS; |
0b5efdc7 | 978 | else if ( code == wxLIST_RECT_ICON ) |
2d33aec9 | 979 | codeWin = LVIR_ICON; |
0b5efdc7 | 980 | else if ( code == wxLIST_RECT_LABEL ) |
2d33aec9 VZ |
981 | codeWin = LVIR_LABEL; |
982 | else | |
983 | { | |
984 | wxFAIL_MSG( _T("incorrect code in GetItemRect()") ); | |
985 | ||
986 | codeWin = LVIR_BOUNDS; | |
987 | } | |
2bda0e17 | 988 | |
2d33aec9 | 989 | bool success = ListView_GetItemRect(GetHwnd(), (int) item, &rectWin, codeWin) != 0; |
2bda0e17 | 990 | |
2d33aec9 VZ |
991 | rect.x = rectWin.left; |
992 | rect.y = rectWin.top; | |
993 | rect.width = rectWin.right - rectWin.left; | |
994 | rect.height = rectWin.bottom - rectWin.top; | |
995 | ||
0b5efdc7 | 996 | return success; |
2bda0e17 KB |
997 | } |
998 | ||
999 | // Gets the item position | |
debe6624 | 1000 | bool wxListCtrl::GetItemPosition(long item, wxPoint& pos) const |
2bda0e17 | 1001 | { |
0b5efdc7 | 1002 | POINT pt; |
2bda0e17 | 1003 | |
edccf428 | 1004 | bool success = (ListView_GetItemPosition(GetHwnd(), (int) item, &pt) != 0); |
2bda0e17 | 1005 | |
0b5efdc7 VZ |
1006 | pos.x = pt.x; pos.y = pt.y; |
1007 | return success; | |
2bda0e17 KB |
1008 | } |
1009 | ||
1010 | // Sets the item position. | |
debe6624 | 1011 | bool wxListCtrl::SetItemPosition(long item, const wxPoint& pos) |
2bda0e17 | 1012 | { |
edccf428 | 1013 | return (ListView_SetItemPosition(GetHwnd(), (int) item, pos.x, pos.y) != 0); |
2bda0e17 KB |
1014 | } |
1015 | ||
1016 | // Gets the number of items in the list control | |
c42404a5 | 1017 | int wxListCtrl::GetItemCount() const |
2bda0e17 | 1018 | { |
68c124a1 | 1019 | return m_count; |
2bda0e17 KB |
1020 | } |
1021 | ||
1022 | // Retrieves the spacing between icons in pixels. | |
1023 | // If small is TRUE, gets the spacing for the small icon | |
1024 | // view, otherwise the large icon view. | |
1025 | int wxListCtrl::GetItemSpacing(bool isSmall) const | |
1026 | { | |
edccf428 | 1027 | return ListView_GetItemSpacing(GetHwnd(), (BOOL) isSmall); |
2bda0e17 KB |
1028 | } |
1029 | ||
5b98eb2f RL |
1030 | void wxListCtrl::SetItemTextColour( long item, const wxColour &col ) |
1031 | { | |
1032 | wxListItem info; | |
1033 | info.m_itemId = item; | |
1034 | info.SetTextColour( col ); | |
1035 | SetItem( info ); | |
1036 | } | |
1037 | ||
1038 | wxColour wxListCtrl::GetItemTextColour( long item ) const | |
1039 | { | |
1040 | wxListItem info; | |
1041 | info.m_itemId = item; | |
1042 | GetItem( info ); | |
1043 | return info.GetTextColour(); | |
1044 | } | |
1045 | ||
1046 | void wxListCtrl::SetItemBackgroundColour( long item, const wxColour &col ) | |
1047 | { | |
1048 | wxListItem info; | |
1049 | info.m_itemId = item; | |
1050 | info.SetBackgroundColour( col ); | |
1051 | SetItem( info ); | |
1052 | } | |
1053 | ||
1054 | wxColour wxListCtrl::GetItemBackgroundColour( long item ) const | |
1055 | { | |
1056 | wxListItem info; | |
1057 | info.m_itemId = item; | |
1058 | GetItem( info ); | |
1059 | return info.GetBackgroundColour(); | |
1060 | } | |
1061 | ||
2bda0e17 | 1062 | // Gets the number of selected items in the list control |
c42404a5 | 1063 | int wxListCtrl::GetSelectedItemCount() const |
2bda0e17 | 1064 | { |
edccf428 | 1065 | return ListView_GetSelectedCount(GetHwnd()); |
2bda0e17 KB |
1066 | } |
1067 | ||
1068 | // Gets the text colour of the listview | |
c42404a5 | 1069 | wxColour wxListCtrl::GetTextColour() const |
2bda0e17 | 1070 | { |
edccf428 | 1071 | COLORREF ref = ListView_GetTextColor(GetHwnd()); |
0b5efdc7 VZ |
1072 | wxColour col(GetRValue(ref), GetGValue(ref), GetBValue(ref)); |
1073 | return col; | |
2bda0e17 KB |
1074 | } |
1075 | ||
1076 | // Sets the text colour of the listview | |
1077 | void wxListCtrl::SetTextColour(const wxColour& col) | |
1078 | { | |
910484a6 | 1079 | ListView_SetTextColor(GetHwnd(), PALETTERGB(col.Red(), col.Green(), col.Blue())); |
2bda0e17 KB |
1080 | } |
1081 | ||
1082 | // Gets the index of the topmost visible item when in | |
1083 | // list or report view | |
c42404a5 | 1084 | long wxListCtrl::GetTopItem() const |
2bda0e17 | 1085 | { |
edccf428 | 1086 | return (long) ListView_GetTopIndex(GetHwnd()); |
2bda0e17 KB |
1087 | } |
1088 | ||
1089 | // Searches for an item, starting from 'item'. | |
1090 | // 'geometry' is one of | |
1091 | // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT. | |
1092 | // 'state' is a state bit flag, one or more of | |
1093 | // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT. | |
1094 | // item can be -1 to find the first item that matches the | |
1095 | // specified flags. | |
1096 | // Returns the item or -1 if unsuccessful. | |
debe6624 | 1097 | long wxListCtrl::GetNextItem(long item, int geom, int state) const |
2bda0e17 | 1098 | { |
0b5efdc7 | 1099 | long flags = 0; |
2bda0e17 | 1100 | |
0b5efdc7 VZ |
1101 | if ( geom == wxLIST_NEXT_ABOVE ) |
1102 | flags |= LVNI_ABOVE; | |
1103 | if ( geom == wxLIST_NEXT_ALL ) | |
1104 | flags |= LVNI_ALL; | |
1105 | if ( geom == wxLIST_NEXT_BELOW ) | |
1106 | flags |= LVNI_BELOW; | |
1107 | if ( geom == wxLIST_NEXT_LEFT ) | |
1108 | flags |= LVNI_TOLEFT; | |
1109 | if ( geom == wxLIST_NEXT_RIGHT ) | |
1110 | flags |= LVNI_TORIGHT; | |
2bda0e17 | 1111 | |
0b5efdc7 VZ |
1112 | if ( state & wxLIST_STATE_CUT ) |
1113 | flags |= LVNI_CUT; | |
1114 | if ( state & wxLIST_STATE_DROPHILITED ) | |
1115 | flags |= LVNI_DROPHILITED; | |
1116 | if ( state & wxLIST_STATE_FOCUSED ) | |
1117 | flags |= LVNI_FOCUSED; | |
1118 | if ( state & wxLIST_STATE_SELECTED ) | |
1119 | flags |= LVNI_SELECTED; | |
2bda0e17 | 1120 | |
edccf428 | 1121 | return (long) ListView_GetNextItem(GetHwnd(), item, flags); |
2bda0e17 KB |
1122 | } |
1123 | ||
1124 | ||
debe6624 | 1125 | wxImageList *wxListCtrl::GetImageList(int which) const |
2bda0e17 | 1126 | { |
0b5efdc7 | 1127 | if ( which == wxIMAGE_LIST_NORMAL ) |
2bda0e17 | 1128 | { |
0b5efdc7 VZ |
1129 | return m_imageListNormal; |
1130 | } | |
1131 | else if ( which == wxIMAGE_LIST_SMALL ) | |
2bda0e17 | 1132 | { |
0b5efdc7 VZ |
1133 | return m_imageListSmall; |
1134 | } | |
1135 | else if ( which == wxIMAGE_LIST_STATE ) | |
2bda0e17 | 1136 | { |
0b5efdc7 VZ |
1137 | return m_imageListState; |
1138 | } | |
1139 | return NULL; | |
2bda0e17 KB |
1140 | } |
1141 | ||
debe6624 | 1142 | void wxListCtrl::SetImageList(wxImageList *imageList, int which) |
2bda0e17 | 1143 | { |
0b5efdc7 VZ |
1144 | int flags = 0; |
1145 | if ( which == wxIMAGE_LIST_NORMAL ) | |
2bda0e17 | 1146 | { |
0b5efdc7 | 1147 | flags = LVSIL_NORMAL; |
2e12c11a | 1148 | if (m_ownsImageListNormal) delete m_imageListNormal; |
0b5efdc7 | 1149 | m_imageListNormal = imageList; |
2e12c11a | 1150 | m_ownsImageListNormal = FALSE; |
0b5efdc7 VZ |
1151 | } |
1152 | else if ( which == wxIMAGE_LIST_SMALL ) | |
2bda0e17 | 1153 | { |
0b5efdc7 | 1154 | flags = LVSIL_SMALL; |
2e12c11a | 1155 | if (m_ownsImageListSmall) delete m_imageListSmall; |
0b5efdc7 | 1156 | m_imageListSmall = imageList; |
2e12c11a | 1157 | m_ownsImageListSmall = FALSE; |
0b5efdc7 VZ |
1158 | } |
1159 | else if ( which == wxIMAGE_LIST_STATE ) | |
2bda0e17 | 1160 | { |
0b5efdc7 | 1161 | flags = LVSIL_STATE; |
2e12c11a | 1162 | if (m_ownsImageListState) delete m_imageListState; |
0b5efdc7 | 1163 | m_imageListState = imageList; |
2e12c11a | 1164 | m_ownsImageListState = FALSE; |
0b5efdc7 | 1165 | } |
edccf428 | 1166 | ListView_SetImageList(GetHwnd(), (HIMAGELIST) imageList ? imageList->GetHIMAGELIST() : 0, flags); |
2bda0e17 KB |
1167 | } |
1168 | ||
2e12c11a VS |
1169 | void wxListCtrl::AssignImageList(wxImageList *imageList, int which) |
1170 | { | |
1171 | SetImageList(imageList, which); | |
1172 | if ( which == wxIMAGE_LIST_NORMAL ) | |
1173 | m_ownsImageListNormal = TRUE; | |
1174 | else if ( which == wxIMAGE_LIST_SMALL ) | |
1175 | m_ownsImageListSmall = TRUE; | |
1176 | else if ( which == wxIMAGE_LIST_STATE ) | |
1177 | m_ownsImageListState = TRUE; | |
1178 | } | |
1179 | ||
edccf428 | 1180 | // ---------------------------------------------------------------------------- |
2bda0e17 | 1181 | // Operations |
edccf428 | 1182 | // ---------------------------------------------------------------------------- |
2bda0e17 KB |
1183 | |
1184 | // Arranges the items | |
debe6624 | 1185 | bool wxListCtrl::Arrange(int flag) |
2bda0e17 | 1186 | { |
0b5efdc7 VZ |
1187 | UINT code = 0; |
1188 | if ( flag == wxLIST_ALIGN_LEFT ) | |
1189 | code = LVA_ALIGNLEFT; | |
1190 | else if ( flag == wxLIST_ALIGN_TOP ) | |
1191 | code = LVA_ALIGNTOP; | |
1192 | else if ( flag == wxLIST_ALIGN_DEFAULT ) | |
1193 | code = LVA_DEFAULT; | |
1194 | else if ( flag == wxLIST_ALIGN_SNAP_TO_GRID ) | |
1195 | code = LVA_SNAPTOGRID; | |
2bda0e17 | 1196 | |
edccf428 | 1197 | return (ListView_Arrange(GetHwnd(), code) != 0); |
2bda0e17 KB |
1198 | } |
1199 | ||
1200 | // Deletes an item | |
debe6624 | 1201 | bool wxListCtrl::DeleteItem(long item) |
2bda0e17 | 1202 | { |
2e9f62da VZ |
1203 | if ( !ListView_DeleteItem(GetHwnd(), (int) item) ) |
1204 | { | |
1205 | wxLogLastError(_T("ListView_DeleteItem")); | |
1206 | return FALSE; | |
1207 | } | |
1208 | ||
68c124a1 RD |
1209 | m_count -= 1; |
1210 | wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()), | |
1211 | wxT("m_count should match ListView_GetItemCount")); | |
1212 | ||
2e9f62da VZ |
1213 | // the virtual list control doesn't refresh itself correctly, help it |
1214 | if ( IsVirtual() ) | |
1215 | { | |
1216 | // we need to refresh all the lines below the one which was deleted | |
1217 | wxRect rectItem; | |
1218 | if ( item > 0 && GetItemCount() ) | |
1219 | { | |
1220 | GetItemRect(item - 1, rectItem); | |
1221 | } | |
1222 | else | |
1223 | { | |
1224 | rectItem.y = | |
1225 | rectItem.height = 0; | |
1226 | } | |
1227 | ||
1228 | wxRect rectWin = GetRect(); | |
1229 | rectWin.height = rectWin.GetBottom() - rectItem.GetBottom(); | |
1230 | rectWin.y = rectItem.GetBottom(); | |
1231 | ||
1232 | RefreshRect(rectWin); | |
1233 | } | |
1234 | ||
1235 | return TRUE; | |
2bda0e17 KB |
1236 | } |
1237 | ||
1238 | // Deletes all items | |
0b5efdc7 | 1239 | bool wxListCtrl::DeleteAllItems() |
2bda0e17 | 1240 | { |
68c124a1 | 1241 | FreeAllInternalData(); |
2e9f62da | 1242 | return ListView_DeleteAllItems(GetHwnd()) != 0; |
2bda0e17 KB |
1243 | } |
1244 | ||
1245 | // Deletes all items | |
0b5efdc7 | 1246 | bool wxListCtrl::DeleteAllColumns() |
2bda0e17 | 1247 | { |
edccf428 | 1248 | while ( m_colCount > 0 ) |
2bda0e17 | 1249 | { |
edccf428 VZ |
1250 | if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 ) |
1251 | { | |
f6bcfd97 | 1252 | wxLogLastError(wxT("ListView_DeleteColumn")); |
edccf428 VZ |
1253 | |
1254 | return FALSE; | |
1255 | } | |
1256 | ||
1257 | m_colCount--; | |
2bda0e17 | 1258 | } |
edccf428 | 1259 | |
223d09f6 | 1260 | wxASSERT_MSG( m_colCount == 0, wxT("no columns should be left") ); |
edccf428 VZ |
1261 | |
1262 | return TRUE; | |
2bda0e17 KB |
1263 | } |
1264 | ||
1265 | // Deletes a column | |
debe6624 | 1266 | bool wxListCtrl::DeleteColumn(int col) |
2bda0e17 | 1267 | { |
edccf428 | 1268 | bool success = (ListView_DeleteColumn(GetHwnd(), col) != 0); |
2bda0e17 KB |
1269 | |
1270 | if ( success && (m_colCount > 0) ) | |
1271 | m_colCount --; | |
1272 | return success; | |
1273 | } | |
1274 | ||
1275 | // Clears items, and columns if there are any. | |
0b5efdc7 | 1276 | void wxListCtrl::ClearAll() |
2bda0e17 KB |
1277 | { |
1278 | DeleteAllItems(); | |
1279 | if ( m_colCount > 0 ) | |
1280 | DeleteAllColumns(); | |
1281 | } | |
1282 | ||
bbcdf8bc JS |
1283 | wxTextCtrl* wxListCtrl::EditLabel(long item, wxClassInfo* textControlClass) |
1284 | { | |
1285 | wxASSERT( (textControlClass->IsKindOf(CLASSINFO(wxTextCtrl))) ); | |
1286 | ||
6aaef140 | 1287 | // ListView_EditLabel requires that the list has focus. |
aa50e893 | 1288 | SetFocus(); |
2b5f62a0 | 1289 | |
6aaef140 | 1290 | WXHWND hWnd = (WXHWND) ListView_EditLabel(GetHwnd(), item); |
2b5f62a0 VZ |
1291 | if ( !hWnd ) |
1292 | { | |
1293 | // failed to start editing | |
1294 | return NULL; | |
1295 | } | |
bbcdf8bc | 1296 | |
2b5f62a0 VZ |
1297 | // [re]create the text control wrapping the HWND we got |
1298 | if ( m_textCtrl ) | |
bbcdf8bc | 1299 | { |
7bf1474a | 1300 | m_textCtrl->UnsubclassWin(); |
fb718549 | 1301 | m_textCtrl->SetHWND(0); |
0b5efdc7 | 1302 | delete m_textCtrl; |
bbcdf8bc JS |
1303 | } |
1304 | ||
2b5f62a0 | 1305 | m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject(); |
6aaef140 VZ |
1306 | m_textCtrl->SetHWND(hWnd); |
1307 | m_textCtrl->SubclassWin(hWnd); | |
1308 | m_textCtrl->SetParent(this); | |
bbcdf8bc | 1309 | |
2b5f62a0 VZ |
1310 | // we must disallow TABbing away from the control while the edit contol is |
1311 | // shown because this leaves it in some strange state (just try removing | |
1312 | // this line and then pressing TAB while editing an item in listctrl | |
1313 | // inside a panel) | |
1314 | m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle() | wxTE_PROCESS_TAB); | |
1315 | ||
bbcdf8bc JS |
1316 | return m_textCtrl; |
1317 | } | |
1318 | ||
1319 | // End label editing, optionally cancelling the edit | |
33ac7e6f | 1320 | bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel)) |
2bda0e17 | 1321 | { |
7bf1474a VZ |
1322 | wxFAIL_MSG( _T("not implemented") ); |
1323 | ||
0b5efdc7 | 1324 | return FALSE; |
2bda0e17 KB |
1325 | } |
1326 | ||
1327 | // Ensures this item is visible | |
debe6624 | 1328 | bool wxListCtrl::EnsureVisible(long item) |
2bda0e17 | 1329 | { |
7bf1474a | 1330 | return ListView_EnsureVisible(GetHwnd(), (int) item, FALSE) != 0; |
2bda0e17 KB |
1331 | } |
1332 | ||
1333 | // Find an item whose label matches this string, starting from the item after 'start' | |
1334 | // or the beginning if 'start' is -1. | |
debe6624 | 1335 | long wxListCtrl::FindItem(long start, const wxString& str, bool partial) |
2bda0e17 | 1336 | { |
0b5efdc7 | 1337 | LV_FINDINFO findInfo; |
2bda0e17 | 1338 | |
0b5efdc7 VZ |
1339 | findInfo.flags = LVFI_STRING; |
1340 | if ( partial ) | |
7edc258e | 1341 | findInfo.flags |= LVFI_PARTIAL; |
3ca6a5f0 | 1342 | findInfo.psz = str; |
2bda0e17 | 1343 | |
3ca6a5f0 BP |
1344 | // ListView_FindItem() excludes the first item from search and to look |
1345 | // through all the items you need to start from -1 which is unnatural and | |
8036af01 JS |
1346 | // inconsistent with the generic version - so we adjust the index |
1347 | if (start != -1) | |
1348 | start --; | |
1349 | return ListView_FindItem(GetHwnd(), (int) start, &findInfo); | |
2bda0e17 KB |
1350 | } |
1351 | ||
1352 | // Find an item whose data matches this data, starting from the item after 'start' | |
1353 | // or the beginning if 'start' is -1. | |
72db8894 RD |
1354 | // NOTE : Lindsay Mathieson - 14-July-2002 |
1355 | // No longer use ListView_FindItem as the data attribute is now stored | |
1356 | // in a wxListItemInternalData structure refernced by the actual lParam | |
debe6624 | 1357 | long wxListCtrl::FindItem(long start, long data) |
2bda0e17 | 1358 | { |
72db8894 RD |
1359 | long idx = start + 1; |
1360 | long count = GetItemCount(); | |
2bda0e17 | 1361 | |
72db8894 RD |
1362 | while (idx < count) |
1363 | { | |
1364 | if (GetItemData(idx) == data) | |
1365 | return idx; | |
1366 | idx++; | |
1367 | }; | |
2bda0e17 | 1368 | |
72db8894 | 1369 | return -1; |
2bda0e17 KB |
1370 | } |
1371 | ||
1372 | // Find an item nearest this position in the specified direction, starting from | |
1373 | // the item after 'start' or the beginning if 'start' is -1. | |
debe6624 | 1374 | long wxListCtrl::FindItem(long start, const wxPoint& pt, int direction) |
2bda0e17 | 1375 | { |
0b5efdc7 | 1376 | LV_FINDINFO findInfo; |
2bda0e17 | 1377 | |
0b5efdc7 VZ |
1378 | findInfo.flags = LVFI_NEARESTXY; |
1379 | findInfo.pt.x = pt.x; | |
1380 | findInfo.pt.y = pt.y; | |
acb62b84 | 1381 | findInfo.vkDirection = VK_RIGHT; |
2bda0e17 | 1382 | |
0b5efdc7 VZ |
1383 | if ( direction == wxLIST_FIND_UP ) |
1384 | findInfo.vkDirection = VK_UP; | |
1385 | else if ( direction == wxLIST_FIND_DOWN ) | |
1386 | findInfo.vkDirection = VK_DOWN; | |
1387 | else if ( direction == wxLIST_FIND_LEFT ) | |
1388 | findInfo.vkDirection = VK_LEFT; | |
1389 | else if ( direction == wxLIST_FIND_RIGHT ) | |
1390 | findInfo.vkDirection = VK_RIGHT; | |
1391 | ||
edccf428 | 1392 | return ListView_FindItem(GetHwnd(), (int) start, & findInfo); |
2bda0e17 KB |
1393 | } |
1394 | ||
1395 | // Determines which item (if any) is at the specified point, | |
1396 | // giving details in 'flags' (see wxLIST_HITTEST_... flags above) | |
1397 | long wxListCtrl::HitTest(const wxPoint& point, int& flags) | |
1398 | { | |
1399 | LV_HITTESTINFO hitTestInfo; | |
0b5efdc7 VZ |
1400 | hitTestInfo.pt.x = (int) point.x; |
1401 | hitTestInfo.pt.y = (int) point.y; | |
2bda0e17 | 1402 | |
edccf428 | 1403 | ListView_HitTest(GetHwnd(), & hitTestInfo); |
2bda0e17 | 1404 | |
0b5efdc7 VZ |
1405 | flags = 0; |
1406 | if ( hitTestInfo.flags & LVHT_ABOVE ) | |
1407 | flags |= wxLIST_HITTEST_ABOVE; | |
1408 | if ( hitTestInfo.flags & LVHT_BELOW ) | |
1409 | flags |= wxLIST_HITTEST_BELOW; | |
1410 | if ( hitTestInfo.flags & LVHT_NOWHERE ) | |
1411 | flags |= wxLIST_HITTEST_NOWHERE; | |
1412 | if ( hitTestInfo.flags & LVHT_ONITEMICON ) | |
1413 | flags |= wxLIST_HITTEST_ONITEMICON; | |
1414 | if ( hitTestInfo.flags & LVHT_ONITEMLABEL ) | |
1415 | flags |= wxLIST_HITTEST_ONITEMLABEL; | |
1416 | if ( hitTestInfo.flags & LVHT_ONITEMSTATEICON ) | |
1417 | flags |= wxLIST_HITTEST_ONITEMSTATEICON; | |
1418 | if ( hitTestInfo.flags & LVHT_TOLEFT ) | |
1419 | flags |= wxLIST_HITTEST_TOLEFT; | |
1420 | if ( hitTestInfo.flags & LVHT_TORIGHT ) | |
1421 | flags |= wxLIST_HITTEST_TORIGHT; | |
1422 | ||
edccf428 | 1423 | return (long) hitTestInfo.iItem; |
2bda0e17 KB |
1424 | } |
1425 | ||
1426 | // Inserts an item, returning the index of the new item if successful, | |
1427 | // -1 otherwise. | |
2bda0e17 KB |
1428 | long wxListCtrl::InsertItem(wxListItem& info) |
1429 | { | |
98ec9dbe VZ |
1430 | wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") ); |
1431 | ||
0b5efdc7 VZ |
1432 | LV_ITEM item; |
1433 | wxConvertToMSWListItem(this, info, item); | |
7cf46fdb | 1434 | item.mask &= ~LVIF_PARAM; |
2bda0e17 | 1435 | |
7cf46fdb VZ |
1436 | // check wether we need to allocate our internal data |
1437 | bool needInternalData = ((info.m_mask & wxLIST_MASK_DATA) || info.HasAttributes()); | |
1438 | if (needInternalData) | |
bdc72a22 | 1439 | { |
7cf46fdb VZ |
1440 | m_AnyInternalData = TRUE; |
1441 | item.mask |= LVIF_PARAM; | |
2702ed5a | 1442 | |
7cf46fdb VZ |
1443 | // internal stucture that manages data |
1444 | wxListItemInternalData *data = new wxListItemInternalData(); | |
1445 | item.lParam = (LPARAM) data; | |
2702ed5a | 1446 | |
7cf46fdb VZ |
1447 | if (info.m_mask & wxLIST_MASK_DATA) |
1448 | data->lParam = info.m_data; | |
2702ed5a | 1449 | |
7cf46fdb VZ |
1450 | // check whether it has any custom attributes |
1451 | if ( info.HasAttributes() ) | |
1452 | { | |
1453 | // take copy of attributes | |
1454 | data->attr = new wxListItemAttr(*info.GetAttributes()); | |
bdc72a22 | 1455 | } |
7cf46fdb VZ |
1456 | }; |
1457 | ||
68c124a1 RD |
1458 | long rv = ListView_InsertItem(GetHwnd(), & item); |
1459 | m_count += 1; | |
1460 | wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()), | |
1461 | wxT("m_count should match ListView_GetItemCount")); | |
bdc72a22 | 1462 | |
68c124a1 | 1463 | return rv; |
2bda0e17 KB |
1464 | } |
1465 | ||
debe6624 | 1466 | long wxListCtrl::InsertItem(long index, const wxString& label) |
2bda0e17 | 1467 | { |
0b5efdc7 VZ |
1468 | wxListItem info; |
1469 | info.m_text = label; | |
1470 | info.m_mask = wxLIST_MASK_TEXT; | |
1471 | info.m_itemId = index; | |
1472 | return InsertItem(info); | |
2bda0e17 KB |
1473 | } |
1474 | ||
1475 | // Inserts an image item | |
debe6624 | 1476 | long wxListCtrl::InsertItem(long index, int imageIndex) |
2bda0e17 | 1477 | { |
0b5efdc7 VZ |
1478 | wxListItem info; |
1479 | info.m_image = imageIndex; | |
1480 | info.m_mask = wxLIST_MASK_IMAGE; | |
1481 | info.m_itemId = index; | |
1482 | return InsertItem(info); | |
2bda0e17 KB |
1483 | } |
1484 | ||
1485 | // Inserts an image/string item | |
debe6624 | 1486 | long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex) |
2bda0e17 | 1487 | { |
0b5efdc7 VZ |
1488 | wxListItem info; |
1489 | info.m_image = imageIndex; | |
1490 | info.m_text = label; | |
1491 | info.m_mask = wxLIST_MASK_IMAGE | wxLIST_MASK_TEXT; | |
1492 | info.m_itemId = index; | |
1493 | return InsertItem(info); | |
2bda0e17 KB |
1494 | } |
1495 | ||
1496 | // For list view mode (only), inserts a column. | |
debe6624 | 1497 | long wxListCtrl::InsertColumn(long col, wxListItem& item) |
2bda0e17 | 1498 | { |
0b5efdc7 | 1499 | LV_COLUMN lvCol; |
6f806543 | 1500 | wxConvertToMSWListCol(col, item, lvCol); |
0b5efdc7 | 1501 | |
6f806543 | 1502 | if ( !(lvCol.mask & LVCF_WIDTH) ) |
e373f51b VZ |
1503 | { |
1504 | // always give some width to the new column: this one is compatible | |
6f806543 VZ |
1505 | // with the generic version |
1506 | lvCol.mask |= LVCF_WIDTH; | |
e373f51b | 1507 | lvCol.cx = 80; |
0b5efdc7 | 1508 | } |
e373f51b | 1509 | |
6f806543 VZ |
1510 | // when we insert a column which can contain an image, we must specify this |
1511 | // flag right now as doing it later in SetColumn() has no effect | |
1512 | // | |
1513 | // we use LVCFMT_BITMAP_ON_RIGHT by default because without it there is no | |
1514 | // way to dynamically set/clear the bitmap as the column without a bitmap | |
1515 | // on the left looks ugly (there is a hole) | |
1516 | // | |
1517 | // unfortunately with my version of comctl32.dll (5.80), the left column | |
1518 | // image is always on the left and it seems that it's a "feature" - I | |
1519 | // didn't find any way to work around it in any case | |
1520 | if ( lvCol.mask & LVCF_IMAGE ) | |
1521 | { | |
1522 | lvCol.mask |= LVCF_FMT; | |
1523 | lvCol.fmt |= LVCFMT_BITMAP_ON_RIGHT; | |
1524 | } | |
2bda0e17 | 1525 | |
6f806543 | 1526 | bool success = ListView_InsertColumn(GetHwnd(), col, &lvCol) != -1; |
2bda0e17 | 1527 | if ( success ) |
e373f51b VZ |
1528 | { |
1529 | m_colCount++; | |
1530 | } | |
1531 | else | |
1532 | { | |
223d09f6 | 1533 | wxLogDebug(wxT("Failed to insert the column '%s' into listview!"), |
e373f51b VZ |
1534 | lvCol.pszText); |
1535 | } | |
1536 | ||
2bda0e17 KB |
1537 | return success; |
1538 | } | |
1539 | ||
bdc72a22 VZ |
1540 | long wxListCtrl::InsertColumn(long col, |
1541 | const wxString& heading, | |
1542 | int format, | |
1543 | int width) | |
2bda0e17 | 1544 | { |
0b5efdc7 VZ |
1545 | wxListItem item; |
1546 | item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT; | |
1547 | item.m_text = heading; | |
1548 | if ( width > -1 ) | |
1549 | { | |
1550 | item.m_mask |= wxLIST_MASK_WIDTH; | |
1551 | item.m_width = width; | |
1552 | } | |
1553 | item.m_format = format; | |
2bda0e17 | 1554 | |
0b5efdc7 | 1555 | return InsertColumn(col, item); |
2bda0e17 KB |
1556 | } |
1557 | ||
2b5f62a0 VZ |
1558 | // scroll the control by the given number of pixels (exception: in list view, |
1559 | // dx is interpreted as number of columns) | |
debe6624 | 1560 | bool wxListCtrl::ScrollList(int dx, int dy) |
2bda0e17 | 1561 | { |
2b5f62a0 VZ |
1562 | if ( !ListView_Scroll(GetHwnd(), dx, dy) ) |
1563 | { | |
1564 | wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx, dy); | |
1565 | ||
1566 | return FALSE; | |
1567 | } | |
1568 | ||
1569 | return TRUE; | |
2bda0e17 KB |
1570 | } |
1571 | ||
1572 | // Sort items. | |
1573 | ||
1574 | // fn is a function which takes 3 long arguments: item1, item2, data. | |
1575 | // item1 is the long data associated with a first item (NOT the index). | |
1576 | // item2 is the long data associated with a second item (NOT the index). | |
1577 | // data is the same value as passed to SortItems. | |
1578 | // The return value is a negative number if the first item should precede the second | |
1579 | // item, a positive number of the second item should precede the first, | |
1580 | // or zero if the two items are equivalent. | |
1581 | ||
1582 | // data is arbitrary data to be passed to the sort function. | |
19230604 | 1583 | |
7cf46fdb VZ |
1584 | // Internal structures for proxying the user compare function |
1585 | // so that we can pass it the *real* user data | |
19230604 | 1586 | |
7cf46fdb VZ |
1587 | // translate lParam data and call user func |
1588 | struct wxInternalDataSort | |
19230604 | 1589 | { |
7cf46fdb VZ |
1590 | wxListCtrlCompare user_fn; |
1591 | long data; | |
1592 | }; | |
19230604 | 1593 | |
7cf46fdb | 1594 | int CALLBACK wxInternalDataCompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) |
2bda0e17 | 1595 | { |
7cf46fdb | 1596 | struct wxInternalDataSort *internalData = (struct wxInternalDataSort *) lParamSort; |
19230604 | 1597 | |
7cf46fdb VZ |
1598 | wxListItemInternalData *data1 = (wxListItemInternalData *) lParam1; |
1599 | wxListItemInternalData *data2 = (wxListItemInternalData *) lParam2; | |
19230604 | 1600 | |
7cf46fdb VZ |
1601 | long d1 = (data1 == NULL ? 0 : data1->lParam); |
1602 | long d2 = (data2 == NULL ? 0 : data2->lParam); | |
19230604 | 1603 | |
7cf46fdb | 1604 | return internalData->user_fn(d1, d2, internalData->data); |
19230604 | 1605 | |
7cf46fdb | 1606 | }; |
19230604 | 1607 | |
7cf46fdb VZ |
1608 | bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data) |
1609 | { | |
1610 | struct wxInternalDataSort internalData; | |
1611 | internalData.user_fn = fn; | |
1612 | internalData.data = data; | |
19230604 | 1613 | |
14090241 VZ |
1614 | // WPARAM cast is needed for mingw/cygwin |
1615 | if ( !ListView_SortItems(GetHwnd(), | |
1616 | wxInternalDataCompareFunc, | |
1617 | (WPARAM) &internalData) ) | |
19230604 RD |
1618 | { |
1619 | wxLogDebug(_T("ListView_SortItems() failed")); | |
1620 | ||
1621 | return FALSE; | |
1622 | } | |
1623 | ||
1624 | return TRUE; | |
2bda0e17 KB |
1625 | } |
1626 | ||
19230604 RD |
1627 | |
1628 | ||
edccf428 VZ |
1629 | // ---------------------------------------------------------------------------- |
1630 | // message processing | |
1631 | // ---------------------------------------------------------------------------- | |
1632 | ||
debe6624 | 1633 | bool wxListCtrl::MSWCommand(WXUINT cmd, WXWORD id) |
2bda0e17 | 1634 | { |
0b5efdc7 | 1635 | if (cmd == EN_UPDATE) |
acb62b84 | 1636 | { |
0b5efdc7 VZ |
1637 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id); |
1638 | event.SetEventObject( this ); | |
1639 | ProcessCommand(event); | |
1640 | return TRUE; | |
acb62b84 | 1641 | } |
0b5efdc7 | 1642 | else if (cmd == EN_KILLFOCUS) |
acb62b84 | 1643 | { |
0b5efdc7 VZ |
1644 | wxCommandEvent event(wxEVT_KILL_FOCUS, id); |
1645 | event.SetEventObject( this ); | |
1646 | ProcessCommand(event); | |
1647 | return TRUE; | |
acb62b84 | 1648 | } |
edccf428 VZ |
1649 | else |
1650 | return FALSE; | |
0b5efdc7 VZ |
1651 | } |
1652 | ||
a23fd0e1 | 1653 | bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) |
0b5efdc7 | 1654 | { |
68c124a1 | 1655 | |
bdc72a22 VZ |
1656 | // prepare the event |
1657 | // ----------------- | |
1658 | ||
0b5efdc7 | 1659 | wxListEvent event(wxEVT_NULL, m_windowId); |
648bec3e VZ |
1660 | event.SetEventObject(this); |
1661 | ||
0b5efdc7 | 1662 | wxEventType eventType = wxEVT_NULL; |
225fe9d6 | 1663 | |
bdc72a22 | 1664 | NMHDR *nmhdr = (NMHDR *)lParam; |
225fe9d6 | 1665 | |
d1f2eb1d VZ |
1666 | // if your compiler is as broken as this, you should really change it: this |
1667 | // code is needed for normal operation! #ifdef below is only useful for | |
1668 | // automatic rebuilds which are done with a very old compiler version | |
0cf5b099 | 1669 | #ifdef HDN_BEGINTRACKA |
d1f2eb1d | 1670 | |
11358d39 VZ |
1671 | // check for messages from the header (in report view) |
1672 | HWND hwndHdr = ListView_GetHeader(GetHwnd()); | |
225fe9d6 | 1673 | |
11358d39 VZ |
1674 | // is it a message from the header? |
1675 | if ( nmhdr->hwndFrom == hwndHdr ) | |
acb62b84 | 1676 | { |
00811ff9 | 1677 | HD_NOTIFY *nmHDR = (HD_NOTIFY *)nmhdr; |
0cf5b099 | 1678 | |
11358d39 | 1679 | event.m_itemIndex = -1; |
cb0f56f9 | 1680 | |
11358d39 VZ |
1681 | switch ( nmhdr->code ) |
1682 | { | |
1683 | // yet another comctl32.dll bug: under NT/W2K it sends Unicode | |
1684 | // TRACK messages even to ANSI programs: on my system I get | |
1685 | // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all! | |
1686 | // | |
1687 | // work around is to simply catch both versions and hope that it | |
1688 | // works (why should this message exist in ANSI and Unicode is | |
1689 | // beyond me as it doesn't deal with strings at all...) | |
2b5f62a0 VZ |
1690 | // |
1691 | // note that fr HDN_TRACK another possibility could be to use | |
1692 | // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when | |
1693 | // something other than the item width changes so we'd have to | |
1694 | // filter out the unwanted events then | |
11358d39 VZ |
1695 | case HDN_BEGINTRACKA: |
1696 | case HDN_BEGINTRACKW: | |
1697 | eventType = wxEVT_COMMAND_LIST_COL_BEGIN_DRAG; | |
1698 | // fall through | |
1699 | ||
1700 | case HDN_TRACKA: | |
1701 | case HDN_TRACKW: | |
1702 | if ( eventType == wxEVT_NULL ) | |
1703 | eventType = wxEVT_COMMAND_LIST_COL_DRAGGING; | |
1704 | // fall through | |
1705 | ||
1706 | case HDN_ENDTRACKA: | |
1707 | case HDN_ENDTRACKW: | |
1708 | if ( eventType == wxEVT_NULL ) | |
1709 | eventType = wxEVT_COMMAND_LIST_COL_END_DRAG; | |
2b5f62a0 VZ |
1710 | |
1711 | event.m_item.m_width = nmHDR->pitem->cxy; | |
11358d39 VZ |
1712 | event.m_col = nmHDR->iItem; |
1713 | break; | |
1714 | ||
1715 | case NM_RCLICK: | |
1716 | { | |
1717 | eventType = wxEVT_COMMAND_LIST_COL_RIGHT_CLICK; | |
1718 | event.m_col = -1; | |
0b5efdc7 | 1719 | |
11358d39 VZ |
1720 | // find the column clicked: we have to search for it |
1721 | // ourselves as the notification message doesn't provide | |
1722 | // this info | |
0b5efdc7 | 1723 | |
11358d39 VZ |
1724 | // where did the click occur? |
1725 | POINT ptClick; | |
1726 | if ( !::GetCursorPos(&ptClick) ) | |
1727 | { | |
1728 | wxLogLastError(_T("GetCursorPos")); | |
1729 | } | |
0b5efdc7 | 1730 | |
11358d39 VZ |
1731 | if ( !::ScreenToClient(hwndHdr, &ptClick) ) |
1732 | { | |
1733 | wxLogLastError(_T("ScreenToClient(listctrl header)")); | |
1734 | } | |
5ea47806 | 1735 | |
11358d39 VZ |
1736 | event.m_pointDrag.x = ptClick.x; |
1737 | event.m_pointDrag.y = ptClick.y; | |
5ea47806 | 1738 | |
11358d39 | 1739 | int colCount = Header_GetItemCount(hwndHdr); |
bdc72a22 | 1740 | |
11358d39 VZ |
1741 | RECT rect; |
1742 | for ( int col = 0; col < colCount; col++ ) | |
1743 | { | |
1744 | if ( Header_GetItemRect(hwndHdr, col, &rect) ) | |
1745 | { | |
1746 | if ( ::PtInRect(&rect, ptClick) ) | |
1747 | { | |
1748 | event.m_col = col; | |
1749 | break; | |
1750 | } | |
1751 | } | |
1752 | } | |
1753 | } | |
1754 | break; | |
5ea47806 | 1755 | |
2b5f62a0 VZ |
1756 | case HDN_GETDISPINFOW: |
1757 | { | |
1758 | LPNMHDDISPINFOW info = (LPNMHDDISPINFOW) lParam; | |
1759 | // This is a fix for a strange bug under XP. | |
1760 | // Normally, info->iItem is a valid index, but | |
1761 | // sometimes this is a silly (large) number | |
1762 | // and when we return FALSE via wxControl::MSWOnNotify | |
1763 | // to indicate that it hasn't yet been processed, | |
1764 | // there's a GPF in Windows. | |
1765 | // By returning TRUE here, we avoid further processing | |
1766 | // of this strange message. | |
7eb833e6 | 1767 | if ( info->iItem >= GetColumnCount() ) |
2b5f62a0 VZ |
1768 | return TRUE; |
1769 | } | |
1770 | // fall through | |
1771 | ||
11358d39 VZ |
1772 | default: |
1773 | return wxControl::MSWOnNotify(idCtrl, lParam, result); | |
1774 | } | |
1775 | } | |
d1f2eb1d | 1776 | else |
0cf5b099 | 1777 | #endif // defined(HDN_BEGINTRACKA) |
d1f2eb1d | 1778 | if ( nmhdr->hwndFrom == GetHwnd() ) |
11358d39 VZ |
1779 | { |
1780 | // almost all messages use NM_LISTVIEW | |
1781 | NM_LISTVIEW *nmLV = (NM_LISTVIEW *)nmhdr; | |
bdc72a22 | 1782 | |
2b5f62a0 VZ |
1783 | const int iItem = nmLV->iItem; |
1784 | ||
68c124a1 RD |
1785 | |
1786 | // FreeAllInternalData will cause LVN_ITEMCHANG* messages, which can be | |
1787 | // ignored for efficiency. It is done here because the internal data is in the | |
1788 | // process of being deleted so we don't want to try and access it below. | |
1789 | if ( m_ignoreChangeMessages && | |
1790 | ( (nmLV->hdr.code == LVN_ITEMCHANGED) || (nmLV->hdr.code == LVN_ITEMCHANGING))) | |
2b5f62a0 | 1791 | { |
68c124a1 RD |
1792 | return TRUE; |
1793 | } | |
7eb833e6 | 1794 | |
2b5f62a0 | 1795 | |
68c124a1 RD |
1796 | // If we have a valid item then check if there is a data value |
1797 | // associated with it and put it in the event. | |
1798 | if ( iItem >= 0 && iItem < GetItemCount() ) | |
1799 | { | |
1800 | wxListItemInternalData *internaldata = | |
1801 | wxGetInternalData(GetHwnd(), iItem); | |
2b5f62a0 | 1802 | |
68c124a1 RD |
1803 | if ( internaldata ) |
1804 | event.m_item.m_data = internaldata->lParam; | |
2b5f62a0 | 1805 | } |
bdc72a22 | 1806 | |
68c124a1 | 1807 | |
11358d39 VZ |
1808 | switch ( nmhdr->code ) |
1809 | { | |
1810 | case LVN_BEGINRDRAG: | |
1811 | eventType = wxEVT_COMMAND_LIST_BEGIN_RDRAG; | |
1812 | // fall through | |
7bf1474a | 1813 | |
11358d39 VZ |
1814 | case LVN_BEGINDRAG: |
1815 | if ( eventType == wxEVT_NULL ) | |
1816 | { | |
1817 | eventType = wxEVT_COMMAND_LIST_BEGIN_DRAG; | |
1818 | } | |
bdc72a22 | 1819 | |
2b5f62a0 | 1820 | event.m_itemIndex = iItem; |
11358d39 VZ |
1821 | event.m_pointDrag.x = nmLV->ptAction.x; |
1822 | event.m_pointDrag.y = nmLV->ptAction.y; | |
1823 | break; | |
1824 | ||
8c21905b VS |
1825 | // NB: we have to handle both *A and *W versions here because some |
1826 | // versions of comctl32.dll send ANSI message to an Unicode app | |
1827 | case LVN_BEGINLABELEDITA: | |
11358d39 VZ |
1828 | { |
1829 | eventType = wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT; | |
8c21905b VS |
1830 | wxLV_ITEM item(((LV_DISPINFOA *)lParam)->item); |
1831 | wxConvertFromMSWListItem(GetHwnd(), event.m_item, item); | |
1832 | event.m_itemIndex = event.m_item.m_itemId; | |
1833 | } | |
1834 | break; | |
1835 | case LVN_BEGINLABELEDITW: | |
1836 | { | |
1837 | eventType = wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT; | |
1838 | wxLV_ITEM item(((LV_DISPINFOW *)lParam)->item); | |
1839 | wxConvertFromMSWListItem(GetHwnd(), event.m_item, item); | |
1840 | event.m_itemIndex = event.m_item.m_itemId; | |
1841 | } | |
1842 | break; | |
1843 | ||
1844 | case LVN_ENDLABELEDITA: | |
1845 | { | |
1846 | eventType = wxEVT_COMMAND_LIST_END_LABEL_EDIT; | |
1847 | wxLV_ITEM item(((LV_DISPINFOA *)lParam)->item); | |
1848 | wxConvertFromMSWListItem(NULL, event.m_item, item); | |
73d8c5fd | 1849 | if ( ((LV_ITEM)item).pszText == NULL || |
8c21905b VS |
1850 | ((LV_ITEM)item).iItem == -1 ) |
1851 | return FALSE; | |
1852 | ||
1853 | event.m_itemIndex = event.m_item.m_itemId; | |
1854 | } | |
1855 | break; | |
1856 | case LVN_ENDLABELEDITW: | |
1857 | { | |
1858 | eventType = wxEVT_COMMAND_LIST_END_LABEL_EDIT; | |
1859 | wxLV_ITEM item(((LV_DISPINFOW *)lParam)->item); | |
1860 | wxConvertFromMSWListItem(NULL, event.m_item, item); | |
73d8c5fd | 1861 | if ( ((LV_ITEM)item).pszText == NULL || |
8c21905b VS |
1862 | ((LV_ITEM)item).iItem == -1 ) |
1863 | return FALSE; | |
1864 | ||
11358d39 VZ |
1865 | event.m_itemIndex = event.m_item.m_itemId; |
1866 | } | |
1867 | break; | |
edccf428 | 1868 | |
11358d39 VZ |
1869 | case LVN_COLUMNCLICK: |
1870 | eventType = wxEVT_COMMAND_LIST_COL_CLICK; | |
1871 | event.m_itemIndex = -1; | |
1872 | event.m_col = nmLV->iSubItem; | |
1873 | break; | |
bdc72a22 | 1874 | |
11358d39 | 1875 | case LVN_DELETEALLITEMS: |
68c124a1 | 1876 | m_count = 0; |
11358d39 VZ |
1877 | eventType = wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS; |
1878 | event.m_itemIndex = -1; | |
11358d39 VZ |
1879 | break; |
1880 | ||
1881 | case LVN_DELETEITEM: | |
68c124a1 RD |
1882 | if (m_count == 0) |
1883 | // this should be prevented by the post-processing code below, | |
1884 | // but "just in case" | |
1885 | return FALSE; | |
1886 | ||
11358d39 | 1887 | eventType = wxEVT_COMMAND_LIST_DELETE_ITEM; |
2b5f62a0 | 1888 | event.m_itemIndex = iItem; |
7cf46fdb | 1889 | // delete the assoicated internal data |
2b5f62a0 | 1890 | wxDeleteInternalData(this, iItem); |
11358d39 VZ |
1891 | break; |
1892 | ||
11358d39 VZ |
1893 | case LVN_SETDISPINFO: |
1894 | { | |
1895 | eventType = wxEVT_COMMAND_LIST_SET_INFO; | |
1896 | LV_DISPINFO *info = (LV_DISPINFO *)lParam; | |
1897 | wxConvertFromMSWListItem(GetHwnd(), event.m_item, info->item); | |
1898 | } | |
1899 | break; | |
1900 | ||
1901 | case LVN_INSERTITEM: | |
1902 | eventType = wxEVT_COMMAND_LIST_INSERT_ITEM; | |
2b5f62a0 | 1903 | event.m_itemIndex = iItem; |
11358d39 | 1904 | break; |
edccf428 | 1905 | |
11358d39 | 1906 | case LVN_ITEMCHANGED: |
648bec3e VZ |
1907 | // we translate this catch all message into more interesting |
1908 | // (and more easy to process) wxWindows events | |
1909 | ||
2b5f62a0 VZ |
1910 | // first of all, we deal with the state change events only and |
1911 | // only for valid items (item == -1 for the virtual list | |
1912 | // control) | |
1913 | if ( nmLV->uChanged & LVIF_STATE && iItem != -1 ) | |
11358d39 | 1914 | { |
648bec3e VZ |
1915 | // temp vars for readability |
1916 | const UINT stOld = nmLV->uOldState; | |
1917 | const UINT stNew = nmLV->uNewState; | |
1918 | ||
2b5f62a0 VZ |
1919 | event.m_item.SetId(iItem); |
1920 | event.m_item.SetMask(wxLIST_MASK_TEXT | | |
1921 | wxLIST_MASK_IMAGE | | |
1922 | wxLIST_MASK_DATA); | |
1923 | GetItem(event.m_item); | |
1924 | ||
648bec3e VZ |
1925 | // has the focus changed? |
1926 | if ( !(stOld & LVIS_FOCUSED) && (stNew & LVIS_FOCUSED) ) | |
1927 | { | |
1928 | eventType = wxEVT_COMMAND_LIST_ITEM_FOCUSED; | |
2b5f62a0 | 1929 | event.m_itemIndex = iItem; |
648bec3e VZ |
1930 | } |
1931 | ||
1932 | if ( (stNew & LVIS_SELECTED) != (stOld & LVIS_SELECTED) ) | |
1933 | { | |
1934 | if ( eventType != wxEVT_NULL ) | |
1935 | { | |
1936 | // focus and selection have both changed: send the | |
1937 | // focus event from here and the selection one | |
1938 | // below | |
1939 | event.SetEventType(eventType); | |
1940 | (void)GetEventHandler()->ProcessEvent(event); | |
1941 | } | |
1942 | else // no focus event to send | |
1943 | { | |
1944 | // then need to set m_itemIndex as it wasn't done | |
1945 | // above | |
2b5f62a0 | 1946 | event.m_itemIndex = iItem; |
648bec3e VZ |
1947 | } |
1948 | ||
1949 | eventType = stNew & LVIS_SELECTED | |
1950 | ? wxEVT_COMMAND_LIST_ITEM_SELECTED | |
1951 | : wxEVT_COMMAND_LIST_ITEM_DESELECTED; | |
1952 | } | |
edccf428 | 1953 | } |
648bec3e VZ |
1954 | |
1955 | if ( eventType == wxEVT_NULL ) | |
edccf428 | 1956 | { |
648bec3e | 1957 | // not an interesting event for us |
11358d39 | 1958 | return FALSE; |
edccf428 | 1959 | } |
648bec3e | 1960 | |
11358d39 | 1961 | break; |
225fe9d6 | 1962 | |
11358d39 VZ |
1963 | case LVN_KEYDOWN: |
1964 | { | |
1965 | LV_KEYDOWN *info = (LV_KEYDOWN *)lParam; | |
1966 | WORD wVKey = info->wVKey; | |
1967 | ||
1968 | // get the current selection | |
1969 | long lItem = GetNextItem(-1, | |
1970 | wxLIST_NEXT_ALL, | |
1971 | wxLIST_STATE_SELECTED); | |
1972 | ||
1973 | // <Enter> or <Space> activate the selected item if any (but | |
1974 | // not with Shift and/or Ctrl as then they have a predefined | |
1975 | // meaning for the list view) | |
1976 | if ( lItem != -1 && | |
1977 | (wVKey == VK_RETURN || wVKey == VK_SPACE) && | |
1978 | !(wxIsShiftDown() || wxIsCtrlDown()) ) | |
1979 | { | |
1980 | eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED; | |
1981 | } | |
1982 | else | |
1983 | { | |
1984 | eventType = wxEVT_COMMAND_LIST_KEY_DOWN; | |
185d0094 VZ |
1985 | |
1986 | // wxCharCodeMSWToWX() returns 0 if the key is an ASCII | |
1987 | // value which should be used as is | |
1988 | int code = wxCharCodeMSWToWX(wVKey); | |
1989 | event.m_code = code ? code : wVKey; | |
11358d39 | 1990 | } |
7db91489 | 1991 | |
11358d39 VZ |
1992 | event.m_itemIndex = |
1993 | event.m_item.m_itemId = lItem; | |
1994 | ||
1995 | if ( lItem != -1 ) | |
1996 | { | |
1997 | // fill the other fields too | |
1998 | event.m_item.m_text = GetItemText(lItem); | |
1999 | event.m_item.m_data = GetItemData(lItem); | |
2000 | } | |
2001 | } | |
2002 | break; | |
2003 | ||
2004 | case NM_DBLCLK: | |
2005 | // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do | |
2006 | // anything else | |
2007 | if ( wxControl::MSWOnNotify(idCtrl, lParam, result) ) | |
f6bcfd97 | 2008 | { |
11358d39 | 2009 | return TRUE; |
f6bcfd97 | 2010 | } |
edccf428 | 2011 | |
11358d39 VZ |
2012 | // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event |
2013 | // if it happened on an item (and not on empty place) | |
2b5f62a0 | 2014 | if ( iItem == -1 ) |
11358d39 VZ |
2015 | { |
2016 | // not on item | |
2017 | return FALSE; | |
2018 | } | |
edccf428 | 2019 | |
11358d39 | 2020 | eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED; |
2b5f62a0 VZ |
2021 | event.m_itemIndex = iItem; |
2022 | event.m_item.m_text = GetItemText(iItem); | |
2023 | event.m_item.m_data = GetItemData(iItem); | |
11358d39 | 2024 | break; |
225fe9d6 | 2025 | |
11358d39 | 2026 | case NM_RCLICK: |
225fe9d6 VZ |
2027 | // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(), |
2028 | // don't do anything else | |
2029 | if ( wxControl::MSWOnNotify(idCtrl, lParam, result) ) | |
2030 | { | |
2031 | return TRUE; | |
2032 | } | |
cb0f56f9 | 2033 | |
225fe9d6 VZ |
2034 | // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event |
2035 | LV_HITTESTINFO lvhti; | |
2036 | wxZeroMemory(lvhti); | |
11c7d5b6 | 2037 | |
225fe9d6 VZ |
2038 | ::GetCursorPos(&(lvhti.pt)); |
2039 | ::ScreenToClient(GetHwnd(),&(lvhti.pt)); | |
2040 | if ( ListView_HitTest(GetHwnd(),&lvhti) != -1 ) | |
cb0f56f9 | 2041 | { |
225fe9d6 VZ |
2042 | if ( lvhti.flags & LVHT_ONITEM ) |
2043 | { | |
2044 | eventType = wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK; | |
2045 | event.m_itemIndex = lvhti.iItem; | |
a1f79c1e VZ |
2046 | event.m_pointDrag.x = lvhti.pt.x; |
2047 | event.m_pointDrag.y = lvhti.pt.y; | |
225fe9d6 | 2048 | } |
cb0f56f9 | 2049 | } |
11358d39 | 2050 | break; |
bdc72a22 | 2051 | |
bf9b6266 | 2052 | #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \ |
11358d39 VZ |
2053 | && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) |
2054 | case NM_CUSTOMDRAW: | |
2055 | *result = OnCustomDraw(lParam); | |
63166611 | 2056 | |
11358d39 | 2057 | return TRUE; |
6ecfe2ac | 2058 | #endif // _WIN32_IE >= 0x300 |
0b5efdc7 | 2059 | |
11358d39 VZ |
2060 | case LVN_ODCACHEHINT: |
2061 | { | |
2062 | const NM_CACHEHINT *cacheHint = (NM_CACHEHINT *)lParam; | |
614391dc | 2063 | |
11358d39 | 2064 | eventType = wxEVT_COMMAND_LIST_CACHE_HINT; |
e623926d | 2065 | |
11358d39 VZ |
2066 | // we get some really stupid cache hints like ones for items in |
2067 | // range 0..0 for an empty control or, after deleting an item, | |
2068 | // for items in invalid range - filter this garbage out | |
2069 | if ( cacheHint->iFrom < cacheHint->iTo ) | |
2070 | { | |
2071 | event.m_oldItemIndex = cacheHint->iFrom; | |
e623926d | 2072 | |
11358d39 VZ |
2073 | long iMax = GetItemCount(); |
2074 | event.m_itemIndex = cacheHint->iTo < iMax ? cacheHint->iTo | |
2075 | : iMax - 1; | |
2076 | } | |
2077 | else | |
2078 | { | |
2079 | return FALSE; | |
2080 | } | |
e623926d | 2081 | } |
11358d39 | 2082 | break; |
614391dc | 2083 | |
11358d39 VZ |
2084 | case LVN_GETDISPINFO: |
2085 | if ( IsVirtual() ) | |
2086 | { | |
2087 | LV_DISPINFO *info = (LV_DISPINFO *)lParam; | |
98ec9dbe | 2088 | |
11358d39 VZ |
2089 | LV_ITEM& lvi = info->item; |
2090 | long item = lvi.iItem; | |
98ec9dbe | 2091 | |
11358d39 VZ |
2092 | if ( lvi.mask & LVIF_TEXT ) |
2093 | { | |
2094 | wxString text = OnGetItemText(item, lvi.iSubItem); | |
2095 | wxStrncpy(lvi.pszText, text, lvi.cchTextMax); | |
2096 | } | |
98ec9dbe | 2097 | |
244531bc | 2098 | #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \ |
5145e34f | 2099 | && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 1 ) ) |
11358d39 VZ |
2100 | if ( lvi.mask & LVIF_IMAGE ) |
2101 | { | |
2102 | lvi.iImage = OnGetItemImage(item); | |
2103 | } | |
244531bc | 2104 | #endif |
98ec9dbe | 2105 | |
11358d39 VZ |
2106 | // a little dose of healthy paranoia: as we never use |
2107 | // LVM_SETCALLBACKMASK we're not supposed to get these ones | |
2108 | wxASSERT_MSG( !(lvi.mask & LVIF_STATE), | |
2109 | _T("we don't support state callbacks yet!") ); | |
98ec9dbe | 2110 | |
11358d39 VZ |
2111 | return TRUE; |
2112 | } | |
2113 | // fall through | |
98ec9dbe | 2114 | |
11358d39 VZ |
2115 | default: |
2116 | return wxControl::MSWOnNotify(idCtrl, lParam, result); | |
2117 | } | |
2118 | } | |
2119 | else | |
2120 | { | |
2121 | // where did this one come from? | |
2122 | return FALSE; | |
acb62b84 VZ |
2123 | } |
2124 | ||
bdc72a22 VZ |
2125 | // process the event |
2126 | // ----------------- | |
2127 | ||
0b5efdc7 | 2128 | event.SetEventType(eventType); |
acb62b84 | 2129 | |
68c124a1 | 2130 | bool processed = GetEventHandler()->ProcessEvent(event); |
acb62b84 | 2131 | |
bdc72a22 VZ |
2132 | // post processing |
2133 | // --------------- | |
225fe9d6 | 2134 | switch ( nmhdr->code ) |
acb62b84 | 2135 | { |
6932a32c VZ |
2136 | case LVN_DELETEALLITEMS: |
2137 | // always return TRUE to suppress all additional LVN_DELETEITEM | |
2138 | // notifications - this makes deleting all items from a list ctrl | |
2139 | // much faster | |
2140 | *result = TRUE; | |
6932a32c VZ |
2141 | return TRUE; |
2142 | ||
8c21905b VS |
2143 | case LVN_ENDLABELEDITA: |
2144 | case LVN_ENDLABELEDITW: | |
614391dc VZ |
2145 | // logic here is inversed compared to all the other messages |
2146 | *result = event.IsAllowed(); | |
2147 | ||
2b5f62a0 VZ |
2148 | // don't keep a stale wxTextCtrl around |
2149 | if ( m_textCtrl ) | |
2150 | { | |
2151 | // EDIT control will be deleted by the list control itself so | |
2152 | // prevent us from deleting it as well | |
2b5f62a0 | 2153 | m_textCtrl->UnsubclassWin(); |
fb718549 | 2154 | m_textCtrl->SetHWND(0); |
2b5f62a0 VZ |
2155 | delete m_textCtrl; |
2156 | m_textCtrl = NULL; | |
2157 | } | |
2158 | ||
614391dc | 2159 | return TRUE; |
acb62b84 | 2160 | } |
acb62b84 | 2161 | |
68c124a1 RD |
2162 | if ( processed ) |
2163 | *result = !event.IsAllowed(); | |
fd3f686c | 2164 | |
68c124a1 | 2165 | return processed; |
2bda0e17 KB |
2166 | } |
2167 | ||
63166611 VZ |
2168 | #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 |
2169 | ||
2170 | WXLPARAM wxListCtrl::OnCustomDraw(WXLPARAM lParam) | |
2171 | { | |
2172 | LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam; | |
2173 | NMCUSTOMDRAW& nmcd = lplvcd->nmcd; | |
2174 | switch ( nmcd.dwDrawStage ) | |
2175 | { | |
2176 | case CDDS_PREPAINT: | |
2177 | // if we've got any items with non standard attributes, | |
2178 | // notify us before painting each item | |
2179 | // | |
2180 | // for virtual controls, always suppose that we have attributes as | |
2181 | // there is no way to check for this | |
2182 | return IsVirtual() || m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW | |
2183 | : CDRF_DODEFAULT; | |
2184 | ||
2185 | case CDDS_ITEMPREPAINT: | |
2186 | { | |
2187 | size_t item = (size_t)nmcd.dwItemSpec; | |
a7f560a2 VZ |
2188 | if ( item >= (size_t)GetItemCount() ) |
2189 | { | |
2190 | // we get this message with item == 0 for an empty control, | |
2191 | // we must ignore it as calling OnGetItemAttr() would be | |
2192 | // wrong | |
2193 | return CDRF_DODEFAULT; | |
2194 | } | |
2195 | ||
63166611 VZ |
2196 | wxListItemAttr *attr = |
2197 | IsVirtual() ? OnGetItemAttr(item) | |
6149de71 | 2198 | : wxGetInternalDataAttr(this, item); |
63166611 VZ |
2199 | |
2200 | if ( !attr ) | |
2201 | { | |
2202 | // nothing to do for this item | |
2203 | return CDRF_DODEFAULT; | |
2204 | } | |
2205 | ||
2206 | HFONT hFont; | |
2207 | wxColour colText, colBack; | |
2208 | if ( attr->HasFont() ) | |
2209 | { | |
2210 | wxFont font = attr->GetFont(); | |
2211 | hFont = (HFONT)font.GetResourceHandle(); | |
2212 | } | |
2213 | else | |
2214 | { | |
2215 | hFont = 0; | |
2216 | } | |
2217 | ||
2218 | if ( attr->HasTextColour() ) | |
2219 | { | |
2220 | colText = attr->GetTextColour(); | |
2221 | } | |
2222 | else | |
2223 | { | |
2224 | colText = GetTextColour(); | |
2225 | } | |
2226 | ||
2227 | if ( attr->HasBackgroundColour() ) | |
2228 | { | |
2229 | colBack = attr->GetBackgroundColour(); | |
2230 | } | |
2231 | else | |
2232 | { | |
2233 | colBack = GetBackgroundColour(); | |
2234 | } | |
2235 | ||
2236 | lplvcd->clrText = wxColourToRGB(colText); | |
2237 | lplvcd->clrTextBk = wxColourToRGB(colBack); | |
2238 | ||
2239 | // note that if we wanted to set colours for | |
2240 | // individual columns (subitems), we would have | |
2241 | // returned CDRF_NOTIFYSUBITEMREDRAW from here | |
2242 | if ( hFont ) | |
2243 | { | |
2244 | ::SelectObject(nmcd.hdc, hFont); | |
2245 | ||
2246 | return CDRF_NEWFONT; | |
2247 | } | |
2248 | } | |
2249 | // fall through to return CDRF_DODEFAULT | |
2250 | ||
2251 | default: | |
2252 | return CDRF_DODEFAULT; | |
2253 | } | |
2254 | } | |
2255 | ||
2256 | #endif // NM_CUSTOMDRAW supported | |
2257 | ||
2702ed5a JS |
2258 | // Necessary for drawing hrules and vrules, if specified |
2259 | void wxListCtrl::OnPaint(wxPaintEvent& event) | |
2260 | { | |
2261 | wxPaintDC dc(this); | |
2262 | ||
2263 | wxControl::OnPaint(event); | |
2264 | ||
2265 | // Reset the device origin since it may have been set | |
2266 | dc.SetDeviceOrigin(0, 0); | |
2267 | ||
2268 | bool drawHRules = ((GetWindowStyle() & wxLC_HRULES) != 0); | |
2269 | bool drawVRules = ((GetWindowStyle() & wxLC_VRULES) != 0); | |
2270 | ||
2271 | if (!drawHRules && !drawVRules) | |
2272 | return; | |
2273 | if ((GetWindowStyle() & wxLC_REPORT) == 0) | |
2274 | return; | |
2275 | ||
a756f210 | 2276 | wxPen pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT), 1, wxSOLID); |
2702ed5a JS |
2277 | dc.SetPen(pen); |
2278 | dc.SetBrush(* wxTRANSPARENT_BRUSH); | |
2279 | ||
2280 | wxSize clientSize = GetClientSize(); | |
2281 | wxRect itemRect; | |
2282 | int cy=0; | |
2283 | ||
2702ed5a JS |
2284 | int itemCount = GetItemCount(); |
2285 | int i; | |
625cb8c0 | 2286 | if (drawHRules) |
2702ed5a | 2287 | { |
6443de02 RD |
2288 | long top = GetTopItem(); |
2289 | for (i = top; i < top + GetCountPerPage() + 1; i++) | |
2702ed5a | 2290 | { |
625cb8c0 | 2291 | if (GetItemRect(i, itemRect)) |
ca286917 | 2292 | { |
625cb8c0 RD |
2293 | cy = itemRect.GetTop(); |
2294 | if (i != 0) // Don't draw the first one | |
2295 | { | |
2296 | dc.DrawLine(0, cy, clientSize.x, cy); | |
2297 | } | |
2298 | // Draw last line | |
2cefcb34 | 2299 | if (i == itemCount - 1) |
625cb8c0 RD |
2300 | { |
2301 | cy = itemRect.GetBottom(); | |
2302 | dc.DrawLine(0, cy, clientSize.x, cy); | |
2303 | } | |
2702ed5a JS |
2304 | } |
2305 | } | |
2306 | } | |
2cefcb34 | 2307 | i = itemCount - 1; |
2702ed5a JS |
2308 | if (drawVRules && (i > -1)) |
2309 | { | |
2310 | wxRect firstItemRect; | |
2311 | GetItemRect(0, firstItemRect); | |
2312 | ||
2313 | if (GetItemRect(i, itemRect)) | |
2314 | { | |
2315 | int col; | |
2316 | int x = itemRect.GetX(); | |
2317 | for (col = 0; col < GetColumnCount(); col++) | |
2318 | { | |
2319 | int colWidth = GetColumnWidth(col); | |
2320 | x += colWidth ; | |
e7d073c3 | 2321 | dc.DrawLine(x-1, firstItemRect.GetY() - 2, x-1, itemRect.GetBottom()); |
2702ed5a JS |
2322 | } |
2323 | } | |
2324 | } | |
2325 | } | |
2326 | ||
98ec9dbe VZ |
2327 | // ---------------------------------------------------------------------------- |
2328 | // virtual list controls | |
2329 | // ---------------------------------------------------------------------------- | |
2330 | ||
d699f48b | 2331 | wxString wxListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const |
98ec9dbe VZ |
2332 | { |
2333 | // this is a pure virtual function, in fact - which is not really pure | |
2334 | // because the controls which are not virtual don't need to implement it | |
271d1110 | 2335 | wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") ); |
98ec9dbe VZ |
2336 | |
2337 | return wxEmptyString; | |
2338 | } | |
2339 | ||
d699f48b | 2340 | int wxListCtrl::OnGetItemImage(long WXUNUSED(item)) const |
98ec9dbe VZ |
2341 | { |
2342 | // same as above | |
271d1110 | 2343 | wxFAIL_MSG( _T("wxListCtrl::OnGetItemImage not supposed to be called") ); |
98ec9dbe VZ |
2344 | |
2345 | return -1; | |
2346 | } | |
2347 | ||
d699f48b | 2348 | wxListItemAttr *wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const |
6c02c329 | 2349 | { |
63166611 | 2350 | wxASSERT_MSG( item >= 0 && item < GetItemCount(), |
6c02c329 VZ |
2351 | _T("invalid item index in OnGetItemAttr()") ); |
2352 | ||
2353 | // no attributes by default | |
2354 | return NULL; | |
2355 | } | |
2356 | ||
98ec9dbe VZ |
2357 | void wxListCtrl::SetItemCount(long count) |
2358 | { | |
2359 | wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") ); | |
2360 | ||
2361 | if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT, (WPARAM)count, 0) ) | |
2362 | { | |
2363 | wxLogLastError(_T("ListView_SetItemCount")); | |
2364 | } | |
68c124a1 RD |
2365 | m_count = count; |
2366 | wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()), | |
2367 | wxT("m_count should match ListView_GetItemCount")); | |
98ec9dbe VZ |
2368 | } |
2369 | ||
a7f560a2 VZ |
2370 | void wxListCtrl::RefreshItem(long item) |
2371 | { | |
2d33aec9 VZ |
2372 | // strangely enough, ListView_Update() results in much more flicker here |
2373 | // than a dumb Refresh() -- why? | |
2374 | #if 0 | |
a7f560a2 VZ |
2375 | if ( !ListView_Update(GetHwnd(), item) ) |
2376 | { | |
2377 | wxLogLastError(_T("ListView_Update")); | |
2378 | } | |
2d33aec9 VZ |
2379 | #else // 1 |
2380 | wxRect rect; | |
2381 | GetItemRect(item, rect); | |
2382 | RefreshRect(rect); | |
2383 | #endif // 0/1 | |
a7f560a2 VZ |
2384 | } |
2385 | ||
2386 | void wxListCtrl::RefreshItems(long itemFrom, long itemTo) | |
2387 | { | |
d0debf52 VZ |
2388 | wxRect rect1, rect2; |
2389 | GetItemRect(itemFrom, rect1); | |
2390 | GetItemRect(itemTo, rect2); | |
2391 | ||
2392 | wxRect rect = rect1; | |
2393 | rect.height = rect2.GetBottom() - rect1.GetTop(); | |
2394 | ||
2395 | RefreshRect(rect); | |
a7f560a2 VZ |
2396 | } |
2397 | ||
6149de71 | 2398 | static wxListItemInternalData *wxGetInternalData(HWND hwnd, long itemId) |
7cf46fdb VZ |
2399 | { |
2400 | LV_ITEM it; | |
2401 | it.mask = LVIF_PARAM; | |
2402 | it.iItem = itemId; | |
2403 | ||
2404 | bool success = ListView_GetItem(hwnd, &it) != 0; | |
2405 | if (success) | |
2406 | return (wxListItemInternalData *) it.lParam; | |
2407 | else | |
2408 | return NULL; | |
2409 | }; | |
2410 | ||
6149de71 | 2411 | static wxListItemInternalData *wxGetInternalData(wxListCtrl *ctl, long itemId) |
7cf46fdb | 2412 | { |
6149de71 | 2413 | return wxGetInternalData((HWND) ctl->GetHWND(), itemId); |
7cf46fdb VZ |
2414 | }; |
2415 | ||
6149de71 | 2416 | static wxListItemAttr *wxGetInternalDataAttr(wxListCtrl *ctl, long itemId) |
7cf46fdb | 2417 | { |
6149de71 | 2418 | wxListItemInternalData *data = wxGetInternalData(ctl, itemId); |
7cf46fdb VZ |
2419 | if (data) |
2420 | return data->attr; | |
2421 | else | |
2422 | return NULL; | |
2423 | }; | |
2424 | ||
6149de71 RD |
2425 | static void wxDeleteInternalData(wxListCtrl* ctl, long itemId) |
2426 | { | |
2427 | wxListItemInternalData *data = wxGetInternalData(ctl, itemId); | |
2428 | if (data) | |
2429 | { | |
6149de71 RD |
2430 | LV_ITEM item; |
2431 | memset(&item, 0, sizeof(item)); | |
2432 | item.iItem = itemId; | |
2433 | item.mask = LVIF_PARAM; | |
2434 | item.lParam = (LPARAM) 0; | |
2435 | ListView_SetItem((HWND)ctl->GetHWND(), &item); | |
cd01e364 | 2436 | delete data; |
6149de71 RD |
2437 | } |
2438 | } | |
7cf46fdb | 2439 | |
8dff2b5c VZ |
2440 | static void wxConvertFromMSWListItem(HWND hwndListCtrl, |
2441 | wxListItem& info, | |
2442 | LV_ITEM& lvItem) | |
2bda0e17 | 2443 | { |
73d8c5fd | 2444 | wxListItemInternalData *internaldata = |
7cf46fdb VZ |
2445 | (wxListItemInternalData *) lvItem.lParam; |
2446 | ||
2447 | if (internaldata) | |
2448 | info.m_data = internaldata->lParam; | |
2449 | ||
0b5efdc7 VZ |
2450 | info.m_mask = 0; |
2451 | info.m_state = 0; | |
2452 | info.m_stateMask = 0; | |
2453 | info.m_itemId = lvItem.iItem; | |
acb62b84 | 2454 | |
0b5efdc7 | 2455 | long oldMask = lvItem.mask; |
acb62b84 | 2456 | |
0b5efdc7 | 2457 | bool needText = FALSE; |
8dff2b5c | 2458 | if (hwndListCtrl != 0) |
acb62b84 | 2459 | { |
0b5efdc7 VZ |
2460 | if ( lvItem.mask & LVIF_TEXT ) |
2461 | needText = FALSE; | |
2462 | else | |
2463 | needText = TRUE; | |
acb62b84 | 2464 | |
0b5efdc7 VZ |
2465 | if ( needText ) |
2466 | { | |
837e5743 | 2467 | lvItem.pszText = new wxChar[513]; |
0b5efdc7 VZ |
2468 | lvItem.cchTextMax = 512; |
2469 | } | |
edccf428 | 2470 | lvItem.mask |= LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM; |
8dff2b5c | 2471 | ::SendMessage(hwndListCtrl, LVM_GETITEM, 0, (LPARAM)& lvItem); |
0b5efdc7 | 2472 | } |
acb62b84 | 2473 | |
0b5efdc7 | 2474 | if ( lvItem.mask & LVIF_STATE ) |
acb62b84 | 2475 | { |
0b5efdc7 VZ |
2476 | info.m_mask |= wxLIST_MASK_STATE; |
2477 | ||
2478 | if ( lvItem.stateMask & LVIS_CUT) | |
2479 | { | |
edccf428 | 2480 | info.m_stateMask |= wxLIST_STATE_CUT; |
0b5efdc7 | 2481 | if ( lvItem.state & LVIS_CUT ) |
edccf428 | 2482 | info.m_state |= wxLIST_STATE_CUT; |
0b5efdc7 VZ |
2483 | } |
2484 | if ( lvItem.stateMask & LVIS_DROPHILITED) | |
2485 | { | |
edccf428 | 2486 | info.m_stateMask |= wxLIST_STATE_DROPHILITED; |
0b5efdc7 | 2487 | if ( lvItem.state & LVIS_DROPHILITED ) |
edccf428 | 2488 | info.m_state |= wxLIST_STATE_DROPHILITED; |
0b5efdc7 VZ |
2489 | } |
2490 | if ( lvItem.stateMask & LVIS_FOCUSED) | |
2491 | { | |
edccf428 | 2492 | info.m_stateMask |= wxLIST_STATE_FOCUSED; |
0b5efdc7 | 2493 | if ( lvItem.state & LVIS_FOCUSED ) |
edccf428 | 2494 | info.m_state |= wxLIST_STATE_FOCUSED; |
0b5efdc7 VZ |
2495 | } |
2496 | if ( lvItem.stateMask & LVIS_SELECTED) | |
2497 | { | |
edccf428 | 2498 | info.m_stateMask |= wxLIST_STATE_SELECTED; |
0b5efdc7 | 2499 | if ( lvItem.state & LVIS_SELECTED ) |
edccf428 | 2500 | info.m_state |= wxLIST_STATE_SELECTED; |
0b5efdc7 | 2501 | } |
acb62b84 | 2502 | } |
0b5efdc7 VZ |
2503 | |
2504 | if ( lvItem.mask & LVIF_TEXT ) | |
acb62b84 | 2505 | { |
0b5efdc7 VZ |
2506 | info.m_mask |= wxLIST_MASK_TEXT; |
2507 | info.m_text = lvItem.pszText; | |
acb62b84 | 2508 | } |
0b5efdc7 | 2509 | if ( lvItem.mask & LVIF_IMAGE ) |
acb62b84 | 2510 | { |
0b5efdc7 VZ |
2511 | info.m_mask |= wxLIST_MASK_IMAGE; |
2512 | info.m_image = lvItem.iImage; | |
acb62b84 | 2513 | } |
0b5efdc7 VZ |
2514 | if ( lvItem.mask & LVIF_PARAM ) |
2515 | info.m_mask |= wxLIST_MASK_DATA; | |
2516 | if ( lvItem.mask & LVIF_DI_SETITEM ) | |
2517 | info.m_mask |= wxLIST_SET_ITEM; | |
2518 | info.m_col = lvItem.iSubItem; | |
2519 | ||
2520 | if (needText) | |
acb62b84 | 2521 | { |
0b5efdc7 VZ |
2522 | if (lvItem.pszText) |
2523 | delete[] lvItem.pszText; | |
acb62b84 | 2524 | } |
edccf428 | 2525 | lvItem.mask = oldMask; |
2bda0e17 KB |
2526 | } |
2527 | ||
8dff2b5c VZ |
2528 | static void wxConvertToMSWFlags(long state, long stateMask, LV_ITEM& lvItem) |
2529 | { | |
2530 | if (stateMask & wxLIST_STATE_CUT) | |
2531 | { | |
2532 | lvItem.stateMask |= LVIS_CUT; | |
2533 | if (state & wxLIST_STATE_CUT) | |
2534 | lvItem.state |= LVIS_CUT; | |
2535 | } | |
2536 | if (stateMask & wxLIST_STATE_DROPHILITED) | |
2537 | { | |
2538 | lvItem.stateMask |= LVIS_DROPHILITED; | |
2539 | if (state & wxLIST_STATE_DROPHILITED) | |
2540 | lvItem.state |= LVIS_DROPHILITED; | |
2541 | } | |
2542 | if (stateMask & wxLIST_STATE_FOCUSED) | |
2543 | { | |
2544 | lvItem.stateMask |= LVIS_FOCUSED; | |
2545 | if (state & wxLIST_STATE_FOCUSED) | |
2546 | lvItem.state |= LVIS_FOCUSED; | |
2547 | } | |
2548 | if (stateMask & wxLIST_STATE_SELECTED) | |
2549 | { | |
2550 | lvItem.stateMask |= LVIS_SELECTED; | |
2551 | if (state & wxLIST_STATE_SELECTED) | |
2552 | lvItem.state |= LVIS_SELECTED; | |
2553 | } | |
2554 | } | |
2555 | ||
2556 | static void wxConvertToMSWListItem(const wxListCtrl *ctrl, | |
2557 | const wxListItem& info, | |
2558 | LV_ITEM& lvItem) | |
2bda0e17 | 2559 | { |
edccf428 | 2560 | lvItem.iItem = (int) info.m_itemId; |
acb62b84 | 2561 | |
edccf428 | 2562 | lvItem.iImage = info.m_image; |
0b5efdc7 VZ |
2563 | lvItem.stateMask = 0; |
2564 | lvItem.state = 0; | |
2565 | lvItem.mask = 0; | |
2566 | lvItem.iSubItem = info.m_col; | |
acb62b84 | 2567 | |
0b5efdc7 | 2568 | if (info.m_mask & wxLIST_MASK_STATE) |
acb62b84 | 2569 | { |
edccf428 | 2570 | lvItem.mask |= LVIF_STATE; |
8dff2b5c VZ |
2571 | |
2572 | wxConvertToMSWFlags(info.m_state, info.m_stateMask, lvItem); | |
acb62b84 | 2573 | } |
acb62b84 | 2574 | |
0b5efdc7 | 2575 | if (info.m_mask & wxLIST_MASK_TEXT) |
acb62b84 | 2576 | { |
edccf428 | 2577 | lvItem.mask |= LVIF_TEXT; |
0b5efdc7 VZ |
2578 | if ( ctrl->GetWindowStyleFlag() & wxLC_USER_TEXT ) |
2579 | { | |
2580 | lvItem.pszText = LPSTR_TEXTCALLBACK; | |
2581 | } | |
2582 | else | |
2583 | { | |
e421922f VZ |
2584 | // pszText is not const, hence the cast |
2585 | lvItem.pszText = (wxChar *)info.m_text.c_str(); | |
0b5efdc7 VZ |
2586 | if ( lvItem.pszText ) |
2587 | lvItem.cchTextMax = info.m_text.Length(); | |
2588 | else | |
2589 | lvItem.cchTextMax = 0; | |
2590 | } | |
acb62b84 | 2591 | } |
0b5efdc7 | 2592 | if (info.m_mask & wxLIST_MASK_IMAGE) |
edccf428 | 2593 | lvItem.mask |= LVIF_IMAGE; |
2bda0e17 KB |
2594 | } |
2595 | ||
574c939e | 2596 | static void wxConvertToMSWListCol(int WXUNUSED(col), const wxListItem& item, |
6f806543 VZ |
2597 | LV_COLUMN& lvCol) |
2598 | { | |
2599 | wxZeroMemory(lvCol); | |
2600 | ||
2601 | if ( item.m_mask & wxLIST_MASK_TEXT ) | |
2602 | { | |
2603 | lvCol.mask |= LVCF_TEXT; | |
2604 | lvCol.pszText = (wxChar *)item.m_text.c_str(); // cast is safe | |
2605 | } | |
2606 | ||
2607 | if ( item.m_mask & wxLIST_MASK_FORMAT ) | |
2608 | { | |
2609 | lvCol.mask |= LVCF_FMT; | |
2610 | ||
2611 | if ( item.m_format == wxLIST_FORMAT_LEFT ) | |
2612 | lvCol.fmt = LVCFMT_LEFT; | |
2613 | else if ( item.m_format == wxLIST_FORMAT_RIGHT ) | |
2614 | lvCol.fmt = LVCFMT_RIGHT; | |
2615 | else if ( item.m_format == wxLIST_FORMAT_CENTRE ) | |
2616 | lvCol.fmt = LVCFMT_CENTER; | |
2617 | } | |
2618 | ||
2619 | if ( item.m_mask & wxLIST_MASK_WIDTH ) | |
2620 | { | |
2621 | lvCol.mask |= LVCF_WIDTH; | |
2622 | if ( item.m_width == wxLIST_AUTOSIZE) | |
2623 | lvCol.cx = LVSCW_AUTOSIZE; | |
2624 | else if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER) | |
2625 | lvCol.cx = LVSCW_AUTOSIZE_USEHEADER; | |
2626 | else | |
2627 | lvCol.cx = item.m_width; | |
2628 | } | |
2629 | ||
244531bc | 2630 | #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 \ |
5145e34f | 2631 | && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 1 ) ) |
6f806543 VZ |
2632 | if ( item.m_mask & wxLIST_MASK_IMAGE ) |
2633 | { | |
2634 | if ( wxTheApp->GetComCtl32Version() >= 470 ) | |
2635 | { | |
2636 | lvCol.mask |= LVCF_IMAGE; | |
2637 | lvCol.iImage = item.m_image; | |
2638 | } | |
2639 | //else: it doesn't support item images anyhow | |
2640 | } | |
244531bc | 2641 | #endif |
6f806543 VZ |
2642 | } |
2643 | ||
1e6feb95 | 2644 | #endif // wxUSE_LISTCTRL |