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