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