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