]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msw/listctrl.cpp | |
3 | // Purpose: wxListCtrl | |
4 | // Author: Julian Smart | |
5 | // Modified by: Agron Selimaj | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #if wxUSE_LISTCTRL | |
28 | ||
29 | #include "wx/listctrl.h" | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" | |
33 | #include "wx/app.h" | |
34 | #include "wx/intl.h" | |
35 | #include "wx/log.h" | |
36 | #include "wx/settings.h" | |
37 | #include "wx/dcclient.h" | |
38 | #include "wx/textctrl.h" | |
39 | #endif | |
40 | ||
41 | #include "wx/imaglist.h" | |
42 | #include "wx/vector.h" | |
43 | ||
44 | #include "wx/msw/private.h" | |
45 | #include "wx/msw/private/keyboard.h" | |
46 | ||
47 | #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) | |
48 | #include <ole2.h> | |
49 | #include <shellapi.h> | |
50 | #if _WIN32_WCE < 400 | |
51 | #include <aygshell.h> | |
52 | #endif | |
53 | #endif | |
54 | ||
55 | // Currently gcc and watcom don't define NMLVFINDITEM, and DMC only defines | |
56 | // it by its old name NM_FINDTIEM. | |
57 | // | |
58 | #if defined(__VISUALC__) || defined(__BORLANDC__) || defined(NMLVFINDITEM) | |
59 | #define HAVE_NMLVFINDITEM 1 | |
60 | #elif defined(__DMC__) || defined(NM_FINDITEM) | |
61 | #define HAVE_NMLVFINDITEM 1 | |
62 | #define NMLVFINDITEM NM_FINDITEM | |
63 | #endif | |
64 | ||
65 | // ---------------------------------------------------------------------------- | |
66 | // private functions | |
67 | // ---------------------------------------------------------------------------- | |
68 | ||
69 | // convert our state and mask flags to LV_ITEM constants | |
70 | static void wxConvertToMSWFlags(long state, long mask, LV_ITEM& lvItem); | |
71 | ||
72 | // convert wxListItem to LV_ITEM | |
73 | static void wxConvertToMSWListItem(const wxListCtrl *ctrl, | |
74 | const wxListItem& info, LV_ITEM& lvItem); | |
75 | ||
76 | // convert LV_ITEM to wxListItem | |
77 | static void wxConvertFromMSWListItem(HWND hwndListCtrl, | |
78 | wxListItem& info, | |
79 | /* const */ LV_ITEM& lvItem); | |
80 | ||
81 | // convert our wxListItem to LV_COLUMN | |
82 | static void wxConvertToMSWListCol(HWND hwndList, | |
83 | int col, | |
84 | const wxListItem& item, | |
85 | LV_COLUMN& lvCol); | |
86 | ||
87 | namespace | |
88 | { | |
89 | ||
90 | // replacement for ListView_GetSubItemRect() which provokes warnings like | |
91 | // "the address of 'rc' will always evaluate as 'true'" when used with mingw32 | |
92 | // 4.3+ | |
93 | // | |
94 | // this function does no error checking on item and subitem parameters, notice | |
95 | // that subitem 0 means the whole item so there is no way to retrieve the | |
96 | // rectangle of the first subitem using this function, in particular notice | |
97 | // that the index is *not* 1-based, in spite of what MSDN says | |
98 | inline bool | |
99 | wxGetListCtrlSubItemRect(HWND hwnd, int item, int subitem, int flags, RECT& rect) | |
100 | { | |
101 | rect.top = subitem; | |
102 | rect.left = flags; | |
103 | return ::SendMessage(hwnd, LVM_GETSUBITEMRECT, item, (LPARAM)&rect) != 0; | |
104 | } | |
105 | ||
106 | inline bool | |
107 | wxGetListCtrlItemRect(HWND hwnd, int item, int flags, RECT& rect) | |
108 | { | |
109 | return wxGetListCtrlSubItemRect(hwnd, item, 0, flags, rect); | |
110 | } | |
111 | ||
112 | } // anonymous namespace | |
113 | ||
114 | // ---------------------------------------------------------------------------- | |
115 | // private helper classes | |
116 | // ---------------------------------------------------------------------------- | |
117 | ||
118 | // We have to handle both fooW and fooA notifications in several cases | |
119 | // because of broken comctl32.dll and/or unicows.dll. This class is used to | |
120 | // convert LV_ITEMA and LV_ITEMW to LV_ITEM (which is either LV_ITEMA or | |
121 | // LV_ITEMW depending on wxUSE_UNICODE setting), so that it can be processed | |
122 | // by wxConvertToMSWListItem(). | |
123 | #if wxUSE_UNICODE | |
124 | #define LV_ITEM_NATIVE LV_ITEMW | |
125 | #define LV_ITEM_OTHER LV_ITEMA | |
126 | ||
127 | #define LV_CONV_TO_WX cMB2WX | |
128 | #define LV_CONV_BUF wxMB2WXbuf | |
129 | #else // ANSI | |
130 | #define LV_ITEM_NATIVE LV_ITEMA | |
131 | #define LV_ITEM_OTHER LV_ITEMW | |
132 | ||
133 | #define LV_CONV_TO_WX cWC2WX | |
134 | #define LV_CONV_BUF wxWC2WXbuf | |
135 | #endif // Unicode/ANSI | |
136 | ||
137 | class wxLV_ITEM | |
138 | { | |
139 | public: | |
140 | // default ctor, use Init() later | |
141 | wxLV_ITEM() { m_buf = NULL; m_pItem = NULL; } | |
142 | ||
143 | // init without conversion | |
144 | void Init(LV_ITEM_NATIVE& item) | |
145 | { | |
146 | wxASSERT_MSG( !m_pItem, wxT("Init() called twice?") ); | |
147 | ||
148 | m_pItem = &item; | |
149 | } | |
150 | ||
151 | // init with conversion | |
152 | void Init(const LV_ITEM_OTHER& item) | |
153 | { | |
154 | // avoid unnecessary dynamic memory allocation, jjust make m_pItem | |
155 | // point to our own m_item | |
156 | ||
157 | // memcpy() can't work if the struct sizes are different | |
158 | wxCOMPILE_TIME_ASSERT( sizeof(LV_ITEM_OTHER) == sizeof(LV_ITEM_NATIVE), | |
159 | CodeCantWorkIfDiffSizes); | |
160 | ||
161 | memcpy(&m_item, &item, sizeof(LV_ITEM_NATIVE)); | |
162 | ||
163 | // convert text from ANSI to Unicod if necessary | |
164 | if ( (item.mask & LVIF_TEXT) && item.pszText ) | |
165 | { | |
166 | m_buf = new LV_CONV_BUF(wxConvLocal.LV_CONV_TO_WX(item.pszText)); | |
167 | m_item.pszText = (wxChar *)m_buf->data(); | |
168 | } | |
169 | } | |
170 | ||
171 | // ctor without conversion | |
172 | wxLV_ITEM(LV_ITEM_NATIVE& item) : m_buf(NULL), m_pItem(&item) { } | |
173 | ||
174 | // ctor with conversion | |
175 | wxLV_ITEM(LV_ITEM_OTHER& item) : m_buf(NULL) | |
176 | { | |
177 | Init(item); | |
178 | } | |
179 | ||
180 | ~wxLV_ITEM() { delete m_buf; } | |
181 | ||
182 | // conversion to the real LV_ITEM | |
183 | operator LV_ITEM_NATIVE&() const { return *m_pItem; } | |
184 | ||
185 | private: | |
186 | LV_CONV_BUF *m_buf; | |
187 | ||
188 | LV_ITEM_NATIVE *m_pItem; | |
189 | LV_ITEM_NATIVE m_item; | |
190 | ||
191 | wxDECLARE_NO_COPY_CLASS(wxLV_ITEM); | |
192 | }; | |
193 | ||
194 | /////////////////////////////////////////////////////// | |
195 | // Problem: | |
196 | // The MSW version had problems with SetTextColour() et | |
197 | // al as the wxListItemAttr's were stored keyed on the | |
198 | // item index. If a item was inserted anywhere but the end | |
199 | // of the list the text attributes (colour etc) for | |
200 | // the following items were out of sync. | |
201 | // | |
202 | // Solution: | |
203 | // Under MSW the only way to associate data with a List | |
204 | // item independent of its position in the list is to | |
205 | // store a pointer to it in its lParam attribute. However | |
206 | // user programs are already using this (via the | |
207 | // SetItemData() GetItemData() calls). | |
208 | // | |
209 | // However what we can do is store a pointer to a | |
210 | // structure which contains the attributes we want *and* | |
211 | // a lParam -- and this is what wxMSWListItemData does. | |
212 | // | |
213 | // To conserve memory, a wxMSWListItemData is | |
214 | // only allocated for a LV_ITEM if text attributes or | |
215 | // user data(lparam) are being set. | |
216 | class wxMSWListItemData | |
217 | { | |
218 | public: | |
219 | wxMSWListItemData() : attr(NULL), lParam(0) {} | |
220 | ~wxMSWListItemData() { delete attr; } | |
221 | ||
222 | wxListItemAttr *attr; | |
223 | LPARAM lParam; // real user data | |
224 | ||
225 | wxDECLARE_NO_COPY_CLASS(wxMSWListItemData); | |
226 | }; | |
227 | ||
228 | BEGIN_EVENT_TABLE(wxListCtrl, wxControl) | |
229 | EVT_PAINT(wxListCtrl::OnPaint) | |
230 | END_EVENT_TABLE() | |
231 | ||
232 | // ============================================================================ | |
233 | // implementation | |
234 | // ============================================================================ | |
235 | ||
236 | // ---------------------------------------------------------------------------- | |
237 | // wxListCtrl construction | |
238 | // ---------------------------------------------------------------------------- | |
239 | ||
240 | void wxListCtrl::Init() | |
241 | { | |
242 | m_imageListNormal = | |
243 | m_imageListSmall = | |
244 | m_imageListState = NULL; | |
245 | m_ownsImageListNormal = | |
246 | m_ownsImageListSmall = | |
247 | m_ownsImageListState = false; | |
248 | ||
249 | m_colCount = 0; | |
250 | m_count = 0; | |
251 | m_textCtrl = NULL; | |
252 | ||
253 | m_hasAnyAttr = false; | |
254 | } | |
255 | ||
256 | bool wxListCtrl::Create(wxWindow *parent, | |
257 | wxWindowID id, | |
258 | const wxPoint& pos, | |
259 | const wxSize& size, | |
260 | long style, | |
261 | const wxValidator& validator, | |
262 | const wxString& name) | |
263 | { | |
264 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) | |
265 | return false; | |
266 | ||
267 | if ( !MSWCreateControl(WC_LISTVIEW, wxEmptyString, pos, size) ) | |
268 | return false; | |
269 | ||
270 | // explicitly say that we want to use Unicode because otherwise we get ANSI | |
271 | // versions of _some_ messages (notably LVN_GETDISPINFOA) in MSLU build | |
272 | wxSetCCUnicodeFormat(GetHwnd()); | |
273 | ||
274 | // We must set the default text colour to the system/theme color, otherwise | |
275 | // GetTextColour will always return black | |
276 | SetTextColour(GetDefaultAttributes().colFg); | |
277 | ||
278 | if ( InReportView() ) | |
279 | MSWSetExListStyles(); | |
280 | ||
281 | return true; | |
282 | } | |
283 | ||
284 | void wxListCtrl::MSWSetExListStyles() | |
285 | { | |
286 | // for comctl32.dll v 4.70+ we want to have some non default extended | |
287 | // styles because it's prettier (and also because wxGTK does it like this) | |
288 | if ( wxApp::GetComCtl32Version() >= 470 ) | |
289 | { | |
290 | ::SendMessage | |
291 | ( | |
292 | GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE, 0, | |
293 | // LVS_EX_LABELTIP shouldn't be used under Windows CE where it's | |
294 | // not defined in the SDK headers | |
295 | #ifdef LVS_EX_LABELTIP | |
296 | LVS_EX_LABELTIP | | |
297 | #endif | |
298 | LVS_EX_FULLROWSELECT | | |
299 | LVS_EX_SUBITEMIMAGES | | |
300 | // normally this should be governed by a style as it's probably not | |
301 | // always appropriate, but we don't have any free styles left and | |
302 | // it seems better to enable it by default than disable | |
303 | LVS_EX_HEADERDRAGDROP | |
304 | ); | |
305 | } | |
306 | } | |
307 | ||
308 | WXDWORD wxListCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const | |
309 | { | |
310 | WXDWORD wstyle = wxControl::MSWGetStyle(style, exstyle); | |
311 | ||
312 | wstyle |= LVS_SHAREIMAGELISTS | LVS_SHOWSELALWAYS; | |
313 | ||
314 | #if wxDEBUG_LEVEL | |
315 | size_t nModes = 0; | |
316 | ||
317 | #define MAP_MODE_STYLE(wx, ms) \ | |
318 | if ( style & (wx) ) { wstyle |= (ms); nModes++; } | |
319 | #else // !wxDEBUG_LEVEL | |
320 | #define MAP_MODE_STYLE(wx, ms) \ | |
321 | if ( style & (wx) ) wstyle |= (ms); | |
322 | #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL | |
323 | ||
324 | MAP_MODE_STYLE(wxLC_ICON, LVS_ICON) | |
325 | MAP_MODE_STYLE(wxLC_SMALL_ICON, LVS_SMALLICON) | |
326 | MAP_MODE_STYLE(wxLC_LIST, LVS_LIST) | |
327 | MAP_MODE_STYLE(wxLC_REPORT, LVS_REPORT) | |
328 | ||
329 | wxASSERT_MSG( nModes == 1, | |
330 | wxT("wxListCtrl style should have exactly one mode bit set") ); | |
331 | ||
332 | #undef MAP_MODE_STYLE | |
333 | ||
334 | if ( style & wxLC_ALIGN_LEFT ) | |
335 | wstyle |= LVS_ALIGNLEFT; | |
336 | ||
337 | if ( style & wxLC_ALIGN_TOP ) | |
338 | wstyle |= LVS_ALIGNTOP; | |
339 | ||
340 | if ( style & wxLC_AUTOARRANGE ) | |
341 | wstyle |= LVS_AUTOARRANGE; | |
342 | ||
343 | if ( style & wxLC_NO_SORT_HEADER ) | |
344 | wstyle |= LVS_NOSORTHEADER; | |
345 | ||
346 | if ( style & wxLC_NO_HEADER ) | |
347 | wstyle |= LVS_NOCOLUMNHEADER; | |
348 | ||
349 | if ( style & wxLC_EDIT_LABELS ) | |
350 | wstyle |= LVS_EDITLABELS; | |
351 | ||
352 | if ( style & wxLC_SINGLE_SEL ) | |
353 | wstyle |= LVS_SINGLESEL; | |
354 | ||
355 | if ( style & wxLC_SORT_ASCENDING ) | |
356 | { | |
357 | wstyle |= LVS_SORTASCENDING; | |
358 | ||
359 | wxASSERT_MSG( !(style & wxLC_SORT_DESCENDING), | |
360 | wxT("can't sort in ascending and descending orders at once") ); | |
361 | } | |
362 | else if ( style & wxLC_SORT_DESCENDING ) | |
363 | wstyle |= LVS_SORTDESCENDING; | |
364 | ||
365 | #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) ) | |
366 | if ( style & wxLC_VIRTUAL ) | |
367 | { | |
368 | int ver = wxApp::GetComCtl32Version(); | |
369 | if ( ver < 470 ) | |
370 | { | |
371 | 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."), | |
372 | ver / 100, ver % 100); | |
373 | } | |
374 | ||
375 | wstyle |= LVS_OWNERDATA; | |
376 | } | |
377 | #endif // ancient cygwin | |
378 | ||
379 | return wstyle; | |
380 | } | |
381 | ||
382 | void wxListCtrl::UpdateStyle() | |
383 | { | |
384 | if ( GetHwnd() ) | |
385 | { | |
386 | // The new window view style | |
387 | DWORD dwStyleNew = MSWGetStyle(m_windowStyle, NULL); | |
388 | ||
389 | // some styles are not returned by MSWGetStyle() | |
390 | if ( IsShown() ) | |
391 | dwStyleNew |= WS_VISIBLE; | |
392 | ||
393 | // Get the current window style. | |
394 | DWORD dwStyleOld = ::GetWindowLong(GetHwnd(), GWL_STYLE); | |
395 | ||
396 | // we don't have wxVSCROLL style, but the list control may have it, | |
397 | // don't change it then | |
398 | dwStyleNew |= dwStyleOld & (WS_HSCROLL | WS_VSCROLL); | |
399 | ||
400 | // Only set the window style if the view bits have changed. | |
401 | if ( dwStyleOld != dwStyleNew ) | |
402 | { | |
403 | ::SetWindowLong(GetHwnd(), GWL_STYLE, dwStyleNew); | |
404 | ||
405 | // if we switched to the report view, set the extended styles for | |
406 | // it too | |
407 | if ( !(dwStyleOld & LVS_REPORT) && (dwStyleNew & LVS_REPORT) ) | |
408 | MSWSetExListStyles(); | |
409 | } | |
410 | } | |
411 | } | |
412 | ||
413 | void wxListCtrl::FreeAllInternalData() | |
414 | { | |
415 | const unsigned count = m_internalData.size(); | |
416 | for ( unsigned n = 0; n < count; n++ ) | |
417 | delete m_internalData[n]; | |
418 | ||
419 | m_internalData.clear(); | |
420 | } | |
421 | ||
422 | void wxListCtrl::DeleteEditControl() | |
423 | { | |
424 | if ( m_textCtrl ) | |
425 | { | |
426 | m_textCtrl->UnsubclassWin(); | |
427 | m_textCtrl->SetHWND(0); | |
428 | wxDELETE(m_textCtrl); | |
429 | } | |
430 | } | |
431 | ||
432 | wxListCtrl::~wxListCtrl() | |
433 | { | |
434 | FreeAllInternalData(); | |
435 | ||
436 | DeleteEditControl(); | |
437 | ||
438 | if (m_ownsImageListNormal) | |
439 | delete m_imageListNormal; | |
440 | if (m_ownsImageListSmall) | |
441 | delete m_imageListSmall; | |
442 | if (m_ownsImageListState) | |
443 | delete m_imageListState; | |
444 | } | |
445 | ||
446 | // ---------------------------------------------------------------------------- | |
447 | // set/get/change style | |
448 | // ---------------------------------------------------------------------------- | |
449 | ||
450 | // Add or remove a single window style | |
451 | void wxListCtrl::SetSingleStyle(long style, bool add) | |
452 | { | |
453 | long flag = GetWindowStyleFlag(); | |
454 | ||
455 | // Get rid of conflicting styles | |
456 | if ( add ) | |
457 | { | |
458 | if ( style & wxLC_MASK_TYPE) | |
459 | flag = flag & ~wxLC_MASK_TYPE; | |
460 | if ( style & wxLC_MASK_ALIGN ) | |
461 | flag = flag & ~wxLC_MASK_ALIGN; | |
462 | if ( style & wxLC_MASK_SORT ) | |
463 | flag = flag & ~wxLC_MASK_SORT; | |
464 | } | |
465 | ||
466 | if ( add ) | |
467 | flag |= style; | |
468 | else | |
469 | flag &= ~style; | |
470 | ||
471 | SetWindowStyleFlag(flag); | |
472 | } | |
473 | ||
474 | // Set the whole window style | |
475 | void wxListCtrl::SetWindowStyleFlag(long flag) | |
476 | { | |
477 | if ( flag != m_windowStyle ) | |
478 | { | |
479 | wxControl::SetWindowStyleFlag(flag); | |
480 | ||
481 | UpdateStyle(); | |
482 | ||
483 | Refresh(); | |
484 | } | |
485 | } | |
486 | ||
487 | // ---------------------------------------------------------------------------- | |
488 | // accessors | |
489 | // ---------------------------------------------------------------------------- | |
490 | ||
491 | /* static */ wxVisualAttributes | |
492 | wxListCtrl::GetClassDefaultAttributes(wxWindowVariant variant) | |
493 | { | |
494 | wxVisualAttributes attrs = GetCompositeControlsDefaultAttributes(variant); | |
495 | ||
496 | // common controls have their own default font | |
497 | attrs.font = wxGetCCDefaultFont(); | |
498 | ||
499 | return attrs; | |
500 | } | |
501 | ||
502 | // Sets the foreground, i.e. text, colour | |
503 | bool wxListCtrl::SetForegroundColour(const wxColour& col) | |
504 | { | |
505 | if ( !wxWindow::SetForegroundColour(col) ) | |
506 | return false; | |
507 | ||
508 | ListView_SetTextColor(GetHwnd(), wxColourToRGB(col)); | |
509 | ||
510 | return true; | |
511 | } | |
512 | ||
513 | // Sets the background colour | |
514 | bool wxListCtrl::SetBackgroundColour(const wxColour& col) | |
515 | { | |
516 | if ( !wxWindow::SetBackgroundColour(col) ) | |
517 | return false; | |
518 | ||
519 | // we set the same colour for both the "empty" background and the items | |
520 | // background | |
521 | COLORREF color = wxColourToRGB(col); | |
522 | ListView_SetBkColor(GetHwnd(), color); | |
523 | ListView_SetTextBkColor(GetHwnd(), color); | |
524 | ||
525 | return true; | |
526 | } | |
527 | ||
528 | // Gets information about this column | |
529 | bool wxListCtrl::GetColumn(int col, wxListItem& item) const | |
530 | { | |
531 | LV_COLUMN lvCol; | |
532 | wxZeroMemory(lvCol); | |
533 | ||
534 | lvCol.mask = LVCF_WIDTH; | |
535 | ||
536 | if ( item.m_mask & wxLIST_MASK_TEXT ) | |
537 | { | |
538 | lvCol.mask |= LVCF_TEXT; | |
539 | lvCol.pszText = new wxChar[513]; | |
540 | lvCol.cchTextMax = 512; | |
541 | } | |
542 | ||
543 | if ( item.m_mask & wxLIST_MASK_FORMAT ) | |
544 | { | |
545 | lvCol.mask |= LVCF_FMT; | |
546 | } | |
547 | ||
548 | if ( item.m_mask & wxLIST_MASK_IMAGE ) | |
549 | { | |
550 | lvCol.mask |= LVCF_IMAGE; | |
551 | } | |
552 | ||
553 | bool success = ListView_GetColumn(GetHwnd(), col, &lvCol) != 0; | |
554 | ||
555 | // item.m_subItem = lvCol.iSubItem; | |
556 | item.m_width = lvCol.cx; | |
557 | ||
558 | if ( (item.m_mask & wxLIST_MASK_TEXT) && lvCol.pszText ) | |
559 | { | |
560 | item.m_text = lvCol.pszText; | |
561 | delete[] lvCol.pszText; | |
562 | } | |
563 | ||
564 | if ( item.m_mask & wxLIST_MASK_FORMAT ) | |
565 | { | |
566 | switch (lvCol.fmt & LVCFMT_JUSTIFYMASK) { | |
567 | case LVCFMT_LEFT: | |
568 | item.m_format = wxLIST_FORMAT_LEFT; | |
569 | break; | |
570 | case LVCFMT_RIGHT: | |
571 | item.m_format = wxLIST_FORMAT_RIGHT; | |
572 | break; | |
573 | case LVCFMT_CENTER: | |
574 | item.m_format = wxLIST_FORMAT_CENTRE; | |
575 | break; | |
576 | default: | |
577 | item.m_format = -1; // Unknown? | |
578 | break; | |
579 | } | |
580 | } | |
581 | ||
582 | // the column images were not supported in older versions but how to check | |
583 | // for this? we can't use _WIN32_IE because we always define it to a very | |
584 | // high value, so see if another symbol which is only defined starting from | |
585 | // comctl32.dll 4.70 is available | |
586 | #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300 | |
587 | if ( item.m_mask & wxLIST_MASK_IMAGE ) | |
588 | { | |
589 | item.m_image = lvCol.iImage; | |
590 | } | |
591 | #endif // LVCOLUMN::iImage exists | |
592 | ||
593 | return success; | |
594 | } | |
595 | ||
596 | // Sets information about this column | |
597 | bool wxListCtrl::SetColumn(int col, const wxListItem& item) | |
598 | { | |
599 | LV_COLUMN lvCol; | |
600 | wxConvertToMSWListCol(GetHwnd(), col, item, lvCol); | |
601 | ||
602 | return ListView_SetColumn(GetHwnd(), col, &lvCol) != 0; | |
603 | } | |
604 | ||
605 | // Gets the column width | |
606 | int wxListCtrl::GetColumnWidth(int col) const | |
607 | { | |
608 | return ListView_GetColumnWidth(GetHwnd(), col); | |
609 | } | |
610 | ||
611 | // Sets the column width | |
612 | bool wxListCtrl::SetColumnWidth(int col, int width) | |
613 | { | |
614 | if ( m_windowStyle & wxLC_LIST ) | |
615 | col = 0; | |
616 | ||
617 | if ( width == wxLIST_AUTOSIZE) | |
618 | width = LVSCW_AUTOSIZE; | |
619 | else if ( width == wxLIST_AUTOSIZE_USEHEADER) | |
620 | width = LVSCW_AUTOSIZE_USEHEADER; | |
621 | ||
622 | return ListView_SetColumnWidth(GetHwnd(), col, width) != 0; | |
623 | } | |
624 | ||
625 | // ---------------------------------------------------------------------------- | |
626 | // columns order | |
627 | // ---------------------------------------------------------------------------- | |
628 | ||
629 | int wxListCtrl::GetColumnIndexFromOrder(int order) const | |
630 | { | |
631 | const int numCols = GetColumnCount(); | |
632 | wxCHECK_MSG( order >= 0 && order < numCols, -1, | |
633 | wxT("Column position out of bounds") ); | |
634 | ||
635 | wxArrayInt indexArray(numCols); | |
636 | if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols, &indexArray[0]) ) | |
637 | return -1; | |
638 | ||
639 | return indexArray[order]; | |
640 | } | |
641 | ||
642 | int wxListCtrl::GetColumnOrder(int col) const | |
643 | { | |
644 | const int numCols = GetColumnCount(); | |
645 | wxASSERT_MSG( col >= 0 && col < numCols, wxT("Column index out of bounds") ); | |
646 | ||
647 | wxArrayInt indexArray(numCols); | |
648 | if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols, &indexArray[0]) ) | |
649 | return -1; | |
650 | ||
651 | for ( int pos = 0; pos < numCols; pos++ ) | |
652 | { | |
653 | if ( indexArray[pos] == col ) | |
654 | return pos; | |
655 | } | |
656 | ||
657 | wxFAIL_MSG( wxT("no column with with given order?") ); | |
658 | ||
659 | return -1; | |
660 | } | |
661 | ||
662 | // Gets the column order for all columns | |
663 | wxArrayInt wxListCtrl::GetColumnsOrder() const | |
664 | { | |
665 | const int numCols = GetColumnCount(); | |
666 | ||
667 | wxArrayInt orders(numCols); | |
668 | if ( !ListView_GetColumnOrderArray(GetHwnd(), numCols, &orders[0]) ) | |
669 | orders.clear(); | |
670 | ||
671 | return orders; | |
672 | } | |
673 | ||
674 | // Sets the column order for all columns | |
675 | bool wxListCtrl::SetColumnsOrder(const wxArrayInt& orders) | |
676 | { | |
677 | const int numCols = GetColumnCount(); | |
678 | ||
679 | wxCHECK_MSG( orders.size() == (size_t)numCols, false, | |
680 | wxT("wrong number of elements in column orders array") ); | |
681 | ||
682 | return ListView_SetColumnOrderArray(GetHwnd(), numCols, &orders[0]) != 0; | |
683 | } | |
684 | ||
685 | ||
686 | // Gets the number of items that can fit vertically in the | |
687 | // visible area of the list control (list or report view) | |
688 | // or the total number of items in the list control (icon | |
689 | // or small icon view) | |
690 | int wxListCtrl::GetCountPerPage() const | |
691 | { | |
692 | return ListView_GetCountPerPage(GetHwnd()); | |
693 | } | |
694 | ||
695 | // Gets the edit control for editing labels. | |
696 | wxTextCtrl* wxListCtrl::GetEditControl() const | |
697 | { | |
698 | // first check corresponds to the case when the label editing was started | |
699 | // by user and hence m_textCtrl wasn't created by EditLabel() at all, while | |
700 | // the second case corresponds to us being called from inside EditLabel() | |
701 | // (e.g. from a user wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT handler): in this | |
702 | // case EditLabel() did create the control but it didn't have an HWND to | |
703 | // initialize it with yet | |
704 | if ( !m_textCtrl || !m_textCtrl->GetHWND() ) | |
705 | { | |
706 | HWND hwndEdit = ListView_GetEditControl(GetHwnd()); | |
707 | if ( hwndEdit ) | |
708 | { | |
709 | wxListCtrl * const self = const_cast<wxListCtrl *>(this); | |
710 | ||
711 | if ( !m_textCtrl ) | |
712 | self->m_textCtrl = new wxTextCtrl; | |
713 | self->InitEditControl((WXHWND)hwndEdit); | |
714 | } | |
715 | } | |
716 | ||
717 | return m_textCtrl; | |
718 | } | |
719 | ||
720 | // Gets information about the item | |
721 | bool wxListCtrl::GetItem(wxListItem& info) const | |
722 | { | |
723 | LV_ITEM lvItem; | |
724 | wxZeroMemory(lvItem); | |
725 | ||
726 | lvItem.iItem = info.m_itemId; | |
727 | lvItem.iSubItem = info.m_col; | |
728 | ||
729 | if ( info.m_mask & wxLIST_MASK_TEXT ) | |
730 | { | |
731 | lvItem.mask |= LVIF_TEXT; | |
732 | lvItem.pszText = new wxChar[513]; | |
733 | lvItem.cchTextMax = 512; | |
734 | } | |
735 | else | |
736 | { | |
737 | lvItem.pszText = NULL; | |
738 | } | |
739 | ||
740 | if (info.m_mask & wxLIST_MASK_DATA) | |
741 | lvItem.mask |= LVIF_PARAM; | |
742 | ||
743 | if (info.m_mask & wxLIST_MASK_IMAGE) | |
744 | lvItem.mask |= LVIF_IMAGE; | |
745 | ||
746 | if ( info.m_mask & wxLIST_MASK_STATE ) | |
747 | { | |
748 | lvItem.mask |= LVIF_STATE; | |
749 | wxConvertToMSWFlags(0, info.m_stateMask, lvItem); | |
750 | } | |
751 | ||
752 | bool success = ListView_GetItem((HWND)GetHWND(), &lvItem) != 0; | |
753 | if ( !success ) | |
754 | { | |
755 | wxLogError(_("Couldn't retrieve information about list control item %d."), | |
756 | lvItem.iItem); | |
757 | } | |
758 | else | |
759 | { | |
760 | // give NULL as hwnd as we already have everything we need | |
761 | wxConvertFromMSWListItem(NULL, info, lvItem); | |
762 | } | |
763 | ||
764 | if (lvItem.pszText) | |
765 | delete[] lvItem.pszText; | |
766 | ||
767 | return success; | |
768 | } | |
769 | ||
770 | // Sets information about the item | |
771 | bool wxListCtrl::SetItem(wxListItem& info) | |
772 | { | |
773 | const long id = info.GetId(); | |
774 | wxCHECK_MSG( id >= 0 && id < GetItemCount(), false, | |
775 | wxT("invalid item index in SetItem") ); | |
776 | ||
777 | LV_ITEM item; | |
778 | wxConvertToMSWListItem(this, info, item); | |
779 | ||
780 | // we never update the lParam if it contains our pointer | |
781 | // to the wxMSWListItemData structure | |
782 | item.mask &= ~LVIF_PARAM; | |
783 | ||
784 | // check if setting attributes or lParam | |
785 | if ( info.HasAttributes() || (info.m_mask & wxLIST_MASK_DATA) ) | |
786 | { | |
787 | // get internal item data | |
788 | wxMSWListItemData *data = MSWGetItemData(id); | |
789 | ||
790 | if ( !data ) | |
791 | { | |
792 | // need to allocate the internal data object | |
793 | data = new wxMSWListItemData; | |
794 | m_internalData.push_back(data); | |
795 | item.lParam = (LPARAM) data; | |
796 | item.mask |= LVIF_PARAM; | |
797 | } | |
798 | ||
799 | ||
800 | // user data | |
801 | if ( info.m_mask & wxLIST_MASK_DATA ) | |
802 | data->lParam = info.m_data; | |
803 | ||
804 | // attributes | |
805 | if ( info.HasAttributes() ) | |
806 | { | |
807 | const wxListItemAttr& attrNew = *info.GetAttributes(); | |
808 | ||
809 | // don't overwrite the already set attributes if we have them | |
810 | if ( data->attr ) | |
811 | data->attr->AssignFrom(attrNew); | |
812 | else | |
813 | data->attr = new wxListItemAttr(attrNew); | |
814 | } | |
815 | } | |
816 | ||
817 | ||
818 | // we could be changing only the attribute in which case we don't need to | |
819 | // call ListView_SetItem() at all | |
820 | if ( item.mask ) | |
821 | { | |
822 | if ( !ListView_SetItem(GetHwnd(), &item) ) | |
823 | { | |
824 | wxLogDebug(wxT("ListView_SetItem() failed")); | |
825 | ||
826 | return false; | |
827 | } | |
828 | } | |
829 | ||
830 | // we need to update the item immediately to show the new image | |
831 | bool updateNow = (info.m_mask & wxLIST_MASK_IMAGE) != 0; | |
832 | ||
833 | // check whether it has any custom attributes | |
834 | if ( info.HasAttributes() ) | |
835 | { | |
836 | m_hasAnyAttr = true; | |
837 | ||
838 | // if the colour has changed, we must redraw the item | |
839 | updateNow = true; | |
840 | } | |
841 | ||
842 | if ( updateNow ) | |
843 | { | |
844 | // we need this to make the change visible right now | |
845 | RefreshItem(item.iItem); | |
846 | } | |
847 | ||
848 | return true; | |
849 | } | |
850 | ||
851 | long wxListCtrl::SetItem(long index, int col, const wxString& label, int imageId) | |
852 | { | |
853 | wxListItem info; | |
854 | info.m_text = label; | |
855 | info.m_mask = wxLIST_MASK_TEXT; | |
856 | info.m_itemId = index; | |
857 | info.m_col = col; | |
858 | if ( imageId > -1 ) | |
859 | { | |
860 | info.m_image = imageId; | |
861 | info.m_mask |= wxLIST_MASK_IMAGE; | |
862 | } | |
863 | return SetItem(info); | |
864 | } | |
865 | ||
866 | ||
867 | // Gets the item state | |
868 | int wxListCtrl::GetItemState(long item, long stateMask) const | |
869 | { | |
870 | wxListItem info; | |
871 | ||
872 | info.m_mask = wxLIST_MASK_STATE; | |
873 | info.m_stateMask = stateMask; | |
874 | info.m_itemId = item; | |
875 | ||
876 | if (!GetItem(info)) | |
877 | return 0; | |
878 | ||
879 | return info.m_state; | |
880 | } | |
881 | ||
882 | // Sets the item state | |
883 | bool wxListCtrl::SetItemState(long item, long state, long stateMask) | |
884 | { | |
885 | // NB: don't use SetItem() here as it doesn't work with the virtual list | |
886 | // controls | |
887 | LV_ITEM lvItem; | |
888 | wxZeroMemory(lvItem); | |
889 | ||
890 | wxConvertToMSWFlags(state, stateMask, lvItem); | |
891 | ||
892 | const bool changingFocus = (stateMask & wxLIST_STATE_FOCUSED) && | |
893 | (state & wxLIST_STATE_FOCUSED); | |
894 | ||
895 | // for the virtual list controls we need to refresh the previously focused | |
896 | // item manually when changing focus without changing selection | |
897 | // programmatically because otherwise it keeps its focus rectangle until | |
898 | // next repaint (yet another comctl32 bug) | |
899 | long focusOld; | |
900 | if ( IsVirtual() && changingFocus ) | |
901 | { | |
902 | focusOld = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED); | |
903 | } | |
904 | else | |
905 | { | |
906 | focusOld = -1; | |
907 | } | |
908 | ||
909 | if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE, | |
910 | (WPARAM)item, (LPARAM)&lvItem) ) | |
911 | { | |
912 | wxLogLastError(wxT("ListView_SetItemState")); | |
913 | ||
914 | return false; | |
915 | } | |
916 | ||
917 | if ( focusOld != -1 ) | |
918 | { | |
919 | // no need to refresh the item if it was previously selected, it would | |
920 | // only result in annoying flicker | |
921 | if ( !(GetItemState(focusOld, | |
922 | wxLIST_STATE_SELECTED) & wxLIST_STATE_SELECTED) ) | |
923 | { | |
924 | RefreshItem(focusOld); | |
925 | } | |
926 | } | |
927 | ||
928 | // we expect the selection anchor, i.e. the item from which multiple | |
929 | // selection (such as performed with e.g. Shift-arrows) starts, to be the | |
930 | // same as the currently focused item but the native control doesn't update | |
931 | // it when we change focus and leaves at the last item it set itself focus | |
932 | // to, so do it explicitly | |
933 | if ( changingFocus && !HasFlag(wxLC_SINGLE_SEL) ) | |
934 | { | |
935 | ListView_SetSelectionMark(GetHwnd(), item); | |
936 | } | |
937 | ||
938 | return true; | |
939 | } | |
940 | ||
941 | // Sets the item image | |
942 | bool wxListCtrl::SetItemImage(long item, int image, int WXUNUSED(selImage)) | |
943 | { | |
944 | return SetItemColumnImage(item, 0, image); | |
945 | } | |
946 | ||
947 | // Sets the item image | |
948 | bool wxListCtrl::SetItemColumnImage(long item, long column, int image) | |
949 | { | |
950 | wxListItem info; | |
951 | ||
952 | info.m_mask = wxLIST_MASK_IMAGE; | |
953 | info.m_image = image; | |
954 | info.m_itemId = item; | |
955 | info.m_col = column; | |
956 | ||
957 | return SetItem(info); | |
958 | } | |
959 | ||
960 | // Gets the item text | |
961 | wxString wxListCtrl::GetItemText(long item, int col) const | |
962 | { | |
963 | wxListItem info; | |
964 | ||
965 | info.m_mask = wxLIST_MASK_TEXT; | |
966 | info.m_itemId = item; | |
967 | info.m_col = col; | |
968 | ||
969 | if (!GetItem(info)) | |
970 | return wxEmptyString; | |
971 | return info.m_text; | |
972 | } | |
973 | ||
974 | // Sets the item text | |
975 | void wxListCtrl::SetItemText(long item, const wxString& str) | |
976 | { | |
977 | wxListItem info; | |
978 | ||
979 | info.m_mask = wxLIST_MASK_TEXT; | |
980 | info.m_itemId = item; | |
981 | info.m_text = str; | |
982 | ||
983 | SetItem(info); | |
984 | } | |
985 | ||
986 | // Gets the internal item data | |
987 | wxMSWListItemData *wxListCtrl::MSWGetItemData(long itemId) const | |
988 | { | |
989 | LV_ITEM it; | |
990 | it.mask = LVIF_PARAM; | |
991 | it.iItem = itemId; | |
992 | ||
993 | if ( !ListView_GetItem(GetHwnd(), &it) ) | |
994 | return NULL; | |
995 | ||
996 | return (wxMSWListItemData *) it.lParam; | |
997 | } | |
998 | ||
999 | // Gets the item data | |
1000 | wxUIntPtr wxListCtrl::GetItemData(long item) const | |
1001 | { | |
1002 | wxListItem info; | |
1003 | ||
1004 | info.m_mask = wxLIST_MASK_DATA; | |
1005 | info.m_itemId = item; | |
1006 | ||
1007 | if (!GetItem(info)) | |
1008 | return 0; | |
1009 | return info.m_data; | |
1010 | } | |
1011 | ||
1012 | // Sets the item data | |
1013 | bool wxListCtrl::SetItemPtrData(long item, wxUIntPtr data) | |
1014 | { | |
1015 | wxListItem info; | |
1016 | ||
1017 | info.m_mask = wxLIST_MASK_DATA; | |
1018 | info.m_itemId = item; | |
1019 | info.m_data = data; | |
1020 | ||
1021 | return SetItem(info); | |
1022 | } | |
1023 | ||
1024 | wxRect wxListCtrl::GetViewRect() const | |
1025 | { | |
1026 | wxRect rect; | |
1027 | ||
1028 | // ListView_GetViewRect() can only be used in icon and small icon views | |
1029 | // (this is documented in MSDN and, indeed, it returns bogus results in | |
1030 | // report view, at least with comctl32.dll v6 under Windows 2003) | |
1031 | if ( HasFlag(wxLC_ICON | wxLC_SMALL_ICON) ) | |
1032 | { | |
1033 | RECT rc; | |
1034 | if ( !ListView_GetViewRect(GetHwnd(), &rc) ) | |
1035 | { | |
1036 | wxLogDebug(wxT("ListView_GetViewRect() failed.")); | |
1037 | ||
1038 | wxZeroMemory(rc); | |
1039 | } | |
1040 | ||
1041 | wxCopyRECTToRect(rc, rect); | |
1042 | } | |
1043 | else if ( HasFlag(wxLC_REPORT) ) | |
1044 | { | |
1045 | const long count = GetItemCount(); | |
1046 | if ( count ) | |
1047 | { | |
1048 | GetItemRect(wxMin(GetTopItem() + GetCountPerPage(), count - 1), rect); | |
1049 | ||
1050 | // extend the rectangle to start at the top (we include the column | |
1051 | // headers, if any, for compatibility with the generic version) | |
1052 | rect.height += rect.y; | |
1053 | rect.y = 0; | |
1054 | } | |
1055 | } | |
1056 | else | |
1057 | { | |
1058 | wxFAIL_MSG( wxT("not implemented in this mode") ); | |
1059 | } | |
1060 | ||
1061 | return rect; | |
1062 | } | |
1063 | ||
1064 | // Gets the item rectangle | |
1065 | bool wxListCtrl::GetItemRect(long item, wxRect& rect, int code) const | |
1066 | { | |
1067 | return GetSubItemRect( item, wxLIST_GETSUBITEMRECT_WHOLEITEM, rect, code) ; | |
1068 | } | |
1069 | ||
1070 | bool wxListCtrl::GetSubItemRect(long item, long subItem, wxRect& rect, int code) const | |
1071 | { | |
1072 | // ListView_GetSubItemRect() doesn't do subItem error checking and returns | |
1073 | // true even for the out of range values of it (even if the results are | |
1074 | // completely bogus in this case), so we check item validity ourselves | |
1075 | wxCHECK_MSG( subItem == wxLIST_GETSUBITEMRECT_WHOLEITEM || | |
1076 | (subItem >= 0 && subItem < GetColumnCount()), | |
1077 | false, wxT("invalid sub item index") ); | |
1078 | ||
1079 | // use wxCHECK_MSG against "item" too, for coherency with the generic implementation: | |
1080 | wxCHECK_MSG( item >= 0 && item < GetItemCount(), false, | |
1081 | wxT("invalid item in GetSubItemRect") ); | |
1082 | ||
1083 | int codeWin; | |
1084 | if ( code == wxLIST_RECT_BOUNDS ) | |
1085 | codeWin = LVIR_BOUNDS; | |
1086 | else if ( code == wxLIST_RECT_ICON ) | |
1087 | codeWin = LVIR_ICON; | |
1088 | else if ( code == wxLIST_RECT_LABEL ) | |
1089 | codeWin = LVIR_LABEL; | |
1090 | else | |
1091 | { | |
1092 | wxFAIL_MSG( wxT("incorrect code in GetItemRect() / GetSubItemRect()") ); | |
1093 | codeWin = LVIR_BOUNDS; | |
1094 | } | |
1095 | ||
1096 | RECT rectWin; | |
1097 | if ( !wxGetListCtrlSubItemRect | |
1098 | ( | |
1099 | GetHwnd(), | |
1100 | item, | |
1101 | subItem == wxLIST_GETSUBITEMRECT_WHOLEITEM ? 0 : subItem, | |
1102 | codeWin, | |
1103 | rectWin | |
1104 | ) ) | |
1105 | { | |
1106 | return false; | |
1107 | } | |
1108 | ||
1109 | wxCopyRECTToRect(rectWin, rect); | |
1110 | ||
1111 | // there is no way to retrieve the first sub item bounding rectangle using | |
1112 | // wxGetListCtrlSubItemRect() as 0 means the whole item, so we need to | |
1113 | // truncate it at first column ourselves | |
1114 | if ( subItem == 0 && code == wxLIST_RECT_BOUNDS ) | |
1115 | rect.width = GetColumnWidth(0); | |
1116 | ||
1117 | return true; | |
1118 | } | |
1119 | ||
1120 | ||
1121 | ||
1122 | ||
1123 | // Gets the item position | |
1124 | bool wxListCtrl::GetItemPosition(long item, wxPoint& pos) const | |
1125 | { | |
1126 | POINT pt; | |
1127 | ||
1128 | bool success = (ListView_GetItemPosition(GetHwnd(), (int) item, &pt) != 0); | |
1129 | ||
1130 | pos.x = pt.x; pos.y = pt.y; | |
1131 | return success; | |
1132 | } | |
1133 | ||
1134 | // Sets the item position. | |
1135 | bool wxListCtrl::SetItemPosition(long item, const wxPoint& pos) | |
1136 | { | |
1137 | return (ListView_SetItemPosition(GetHwnd(), (int) item, pos.x, pos.y) != 0); | |
1138 | } | |
1139 | ||
1140 | // Gets the number of items in the list control | |
1141 | int wxListCtrl::GetItemCount() const | |
1142 | { | |
1143 | return m_count; | |
1144 | } | |
1145 | ||
1146 | wxSize wxListCtrl::GetItemSpacing() const | |
1147 | { | |
1148 | const int spacing = ListView_GetItemSpacing(GetHwnd(), (BOOL)HasFlag(wxLC_SMALL_ICON)); | |
1149 | ||
1150 | return wxSize(LOWORD(spacing), HIWORD(spacing)); | |
1151 | } | |
1152 | ||
1153 | #if WXWIN_COMPATIBILITY_2_6 | |
1154 | ||
1155 | int wxListCtrl::GetItemSpacing(bool isSmall) const | |
1156 | { | |
1157 | return ListView_GetItemSpacing(GetHwnd(), (BOOL) isSmall); | |
1158 | } | |
1159 | ||
1160 | #endif // WXWIN_COMPATIBILITY_2_6 | |
1161 | ||
1162 | void wxListCtrl::SetItemTextColour( long item, const wxColour &col ) | |
1163 | { | |
1164 | wxListItem info; | |
1165 | info.m_itemId = item; | |
1166 | info.SetTextColour( col ); | |
1167 | SetItem( info ); | |
1168 | } | |
1169 | ||
1170 | wxColour wxListCtrl::GetItemTextColour( long item ) const | |
1171 | { | |
1172 | wxColour col; | |
1173 | wxMSWListItemData *data = MSWGetItemData(item); | |
1174 | if ( data && data->attr ) | |
1175 | col = data->attr->GetTextColour(); | |
1176 | ||
1177 | return col; | |
1178 | } | |
1179 | ||
1180 | void wxListCtrl::SetItemBackgroundColour( long item, const wxColour &col ) | |
1181 | { | |
1182 | wxListItem info; | |
1183 | info.m_itemId = item; | |
1184 | info.SetBackgroundColour( col ); | |
1185 | SetItem( info ); | |
1186 | } | |
1187 | ||
1188 | wxColour wxListCtrl::GetItemBackgroundColour( long item ) const | |
1189 | { | |
1190 | wxColour col; | |
1191 | wxMSWListItemData *data = MSWGetItemData(item); | |
1192 | if ( data && data->attr ) | |
1193 | col = data->attr->GetBackgroundColour(); | |
1194 | ||
1195 | return col; | |
1196 | } | |
1197 | ||
1198 | void wxListCtrl::SetItemFont( long item, const wxFont &f ) | |
1199 | { | |
1200 | wxListItem info; | |
1201 | info.m_itemId = item; | |
1202 | info.SetFont( f ); | |
1203 | SetItem( info ); | |
1204 | } | |
1205 | ||
1206 | wxFont wxListCtrl::GetItemFont( long item ) const | |
1207 | { | |
1208 | wxFont f; | |
1209 | wxMSWListItemData *data = MSWGetItemData(item); | |
1210 | if ( data && data->attr ) | |
1211 | f = data->attr->GetFont(); | |
1212 | ||
1213 | return f; | |
1214 | } | |
1215 | ||
1216 | // Gets the number of selected items in the list control | |
1217 | int wxListCtrl::GetSelectedItemCount() const | |
1218 | { | |
1219 | return ListView_GetSelectedCount(GetHwnd()); | |
1220 | } | |
1221 | ||
1222 | // Gets the text colour of the listview | |
1223 | wxColour wxListCtrl::GetTextColour() const | |
1224 | { | |
1225 | COLORREF ref = ListView_GetTextColor(GetHwnd()); | |
1226 | wxColour col(GetRValue(ref), GetGValue(ref), GetBValue(ref)); | |
1227 | return col; | |
1228 | } | |
1229 | ||
1230 | // Sets the text colour of the listview | |
1231 | void wxListCtrl::SetTextColour(const wxColour& col) | |
1232 | { | |
1233 | ListView_SetTextColor(GetHwnd(), PALETTERGB(col.Red(), col.Green(), col.Blue())); | |
1234 | } | |
1235 | ||
1236 | // Gets the index of the topmost visible item when in | |
1237 | // list or report view | |
1238 | long wxListCtrl::GetTopItem() const | |
1239 | { | |
1240 | return (long) ListView_GetTopIndex(GetHwnd()); | |
1241 | } | |
1242 | ||
1243 | // Searches for an item, starting from 'item'. | |
1244 | // 'geometry' is one of | |
1245 | // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT. | |
1246 | // 'state' is a state bit flag, one or more of | |
1247 | // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT. | |
1248 | // item can be -1 to find the first item that matches the | |
1249 | // specified flags. | |
1250 | // Returns the item or -1 if unsuccessful. | |
1251 | long wxListCtrl::GetNextItem(long item, int geom, int state) const | |
1252 | { | |
1253 | long flags = 0; | |
1254 | ||
1255 | if ( geom == wxLIST_NEXT_ABOVE ) | |
1256 | flags |= LVNI_ABOVE; | |
1257 | if ( geom == wxLIST_NEXT_ALL ) | |
1258 | flags |= LVNI_ALL; | |
1259 | if ( geom == wxLIST_NEXT_BELOW ) | |
1260 | flags |= LVNI_BELOW; | |
1261 | if ( geom == wxLIST_NEXT_LEFT ) | |
1262 | flags |= LVNI_TOLEFT; | |
1263 | if ( geom == wxLIST_NEXT_RIGHT ) | |
1264 | flags |= LVNI_TORIGHT; | |
1265 | ||
1266 | if ( state & wxLIST_STATE_CUT ) | |
1267 | flags |= LVNI_CUT; | |
1268 | if ( state & wxLIST_STATE_DROPHILITED ) | |
1269 | flags |= LVNI_DROPHILITED; | |
1270 | if ( state & wxLIST_STATE_FOCUSED ) | |
1271 | flags |= LVNI_FOCUSED; | |
1272 | if ( state & wxLIST_STATE_SELECTED ) | |
1273 | flags |= LVNI_SELECTED; | |
1274 | ||
1275 | return (long) ListView_GetNextItem(GetHwnd(), item, flags); | |
1276 | } | |
1277 | ||
1278 | ||
1279 | wxImageList *wxListCtrl::GetImageList(int which) const | |
1280 | { | |
1281 | if ( which == wxIMAGE_LIST_NORMAL ) | |
1282 | { | |
1283 | return m_imageListNormal; | |
1284 | } | |
1285 | else if ( which == wxIMAGE_LIST_SMALL ) | |
1286 | { | |
1287 | return m_imageListSmall; | |
1288 | } | |
1289 | else if ( which == wxIMAGE_LIST_STATE ) | |
1290 | { | |
1291 | return m_imageListState; | |
1292 | } | |
1293 | return NULL; | |
1294 | } | |
1295 | ||
1296 | void wxListCtrl::SetImageList(wxImageList *imageList, int which) | |
1297 | { | |
1298 | int flags = 0; | |
1299 | if ( which == wxIMAGE_LIST_NORMAL ) | |
1300 | { | |
1301 | flags = LVSIL_NORMAL; | |
1302 | if (m_ownsImageListNormal) delete m_imageListNormal; | |
1303 | m_imageListNormal = imageList; | |
1304 | m_ownsImageListNormal = false; | |
1305 | } | |
1306 | else if ( which == wxIMAGE_LIST_SMALL ) | |
1307 | { | |
1308 | flags = LVSIL_SMALL; | |
1309 | if (m_ownsImageListSmall) delete m_imageListSmall; | |
1310 | m_imageListSmall = imageList; | |
1311 | m_ownsImageListSmall = false; | |
1312 | } | |
1313 | else if ( which == wxIMAGE_LIST_STATE ) | |
1314 | { | |
1315 | flags = LVSIL_STATE; | |
1316 | if (m_ownsImageListState) delete m_imageListState; | |
1317 | m_imageListState = imageList; | |
1318 | m_ownsImageListState = false; | |
1319 | } | |
1320 | (void) ListView_SetImageList(GetHwnd(), (HIMAGELIST) imageList ? imageList->GetHIMAGELIST() : 0, flags); | |
1321 | } | |
1322 | ||
1323 | void wxListCtrl::AssignImageList(wxImageList *imageList, int which) | |
1324 | { | |
1325 | SetImageList(imageList, which); | |
1326 | if ( which == wxIMAGE_LIST_NORMAL ) | |
1327 | m_ownsImageListNormal = true; | |
1328 | else if ( which == wxIMAGE_LIST_SMALL ) | |
1329 | m_ownsImageListSmall = true; | |
1330 | else if ( which == wxIMAGE_LIST_STATE ) | |
1331 | m_ownsImageListState = true; | |
1332 | } | |
1333 | ||
1334 | // ---------------------------------------------------------------------------- | |
1335 | // Operations | |
1336 | // ---------------------------------------------------------------------------- | |
1337 | ||
1338 | // Arranges the items | |
1339 | bool wxListCtrl::Arrange(int flag) | |
1340 | { | |
1341 | UINT code = 0; | |
1342 | if ( flag == wxLIST_ALIGN_LEFT ) | |
1343 | code = LVA_ALIGNLEFT; | |
1344 | else if ( flag == wxLIST_ALIGN_TOP ) | |
1345 | code = LVA_ALIGNTOP; | |
1346 | else if ( flag == wxLIST_ALIGN_DEFAULT ) | |
1347 | code = LVA_DEFAULT; | |
1348 | else if ( flag == wxLIST_ALIGN_SNAP_TO_GRID ) | |
1349 | code = LVA_SNAPTOGRID; | |
1350 | ||
1351 | return (ListView_Arrange(GetHwnd(), code) != 0); | |
1352 | } | |
1353 | ||
1354 | // Deletes an item | |
1355 | bool wxListCtrl::DeleteItem(long item) | |
1356 | { | |
1357 | if ( !ListView_DeleteItem(GetHwnd(), (int) item) ) | |
1358 | { | |
1359 | wxLogLastError(wxT("ListView_DeleteItem")); | |
1360 | return false; | |
1361 | } | |
1362 | ||
1363 | m_count--; | |
1364 | wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()), | |
1365 | wxT("m_count should match ListView_GetItemCount")); | |
1366 | ||
1367 | // the virtual list control doesn't refresh itself correctly, help it | |
1368 | if ( IsVirtual() ) | |
1369 | { | |
1370 | // we need to refresh all the lines below the one which was deleted | |
1371 | wxRect rectItem; | |
1372 | if ( item > 0 && GetItemCount() ) | |
1373 | { | |
1374 | GetItemRect(item - 1, rectItem); | |
1375 | } | |
1376 | else | |
1377 | { | |
1378 | rectItem.y = | |
1379 | rectItem.height = 0; | |
1380 | } | |
1381 | ||
1382 | wxRect rectWin = GetRect(); | |
1383 | rectWin.height = rectWin.GetBottom() - rectItem.GetBottom(); | |
1384 | rectWin.y = rectItem.GetBottom(); | |
1385 | ||
1386 | RefreshRect(rectWin); | |
1387 | } | |
1388 | ||
1389 | return true; | |
1390 | } | |
1391 | ||
1392 | // Deletes all items | |
1393 | bool wxListCtrl::DeleteAllItems() | |
1394 | { | |
1395 | // Calling ListView_DeleteAllItems() will always generate an event but we | |
1396 | // shouldn't do it if the control is empty | |
1397 | return !GetItemCount() || ListView_DeleteAllItems(GetHwnd()) != 0; | |
1398 | } | |
1399 | ||
1400 | // Deletes all items | |
1401 | bool wxListCtrl::DeleteAllColumns() | |
1402 | { | |
1403 | while ( m_colCount > 0 ) | |
1404 | { | |
1405 | if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 ) | |
1406 | { | |
1407 | wxLogLastError(wxT("ListView_DeleteColumn")); | |
1408 | ||
1409 | return false; | |
1410 | } | |
1411 | ||
1412 | m_colCount--; | |
1413 | } | |
1414 | ||
1415 | wxASSERT_MSG( m_colCount == 0, wxT("no columns should be left") ); | |
1416 | ||
1417 | return true; | |
1418 | } | |
1419 | ||
1420 | // Deletes a column | |
1421 | bool wxListCtrl::DeleteColumn(int col) | |
1422 | { | |
1423 | bool success = (ListView_DeleteColumn(GetHwnd(), col) != 0); | |
1424 | ||
1425 | if ( success && (m_colCount > 0) ) | |
1426 | m_colCount --; | |
1427 | return success; | |
1428 | } | |
1429 | ||
1430 | // Clears items, and columns if there are any. | |
1431 | void wxListCtrl::ClearAll() | |
1432 | { | |
1433 | DeleteAllItems(); | |
1434 | if ( m_colCount > 0 ) | |
1435 | DeleteAllColumns(); | |
1436 | } | |
1437 | ||
1438 | void wxListCtrl::InitEditControl(WXHWND hWnd) | |
1439 | { | |
1440 | m_textCtrl->SetHWND(hWnd); | |
1441 | m_textCtrl->SubclassWin(hWnd); | |
1442 | m_textCtrl->SetParent(this); | |
1443 | ||
1444 | // we must disallow TABbing away from the control while the edit contol is | |
1445 | // shown because this leaves it in some strange state (just try removing | |
1446 | // this line and then pressing TAB while editing an item in listctrl | |
1447 | // inside a panel) | |
1448 | m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle() | wxTE_PROCESS_TAB); | |
1449 | } | |
1450 | ||
1451 | wxTextCtrl* wxListCtrl::EditLabel(long item, wxClassInfo* textControlClass) | |
1452 | { | |
1453 | wxCHECK_MSG( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)), NULL, | |
1454 | "control used for label editing must be a wxTextCtrl" ); | |
1455 | ||
1456 | // ListView_EditLabel requires that the list has focus. | |
1457 | SetFocus(); | |
1458 | ||
1459 | // create m_textCtrl here before calling ListView_EditLabel() because it | |
1460 | // generates wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT event from inside it and | |
1461 | // the user handler for it can call GetEditControl() resulting in an on | |
1462 | // demand creation of a stock wxTextCtrl instead of the control of a | |
1463 | // (possibly) custom wxClassInfo | |
1464 | DeleteEditControl(); | |
1465 | m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject(); | |
1466 | ||
1467 | WXHWND hWnd = (WXHWND) ListView_EditLabel(GetHwnd(), item); | |
1468 | if ( !hWnd ) | |
1469 | { | |
1470 | // failed to start editing | |
1471 | wxDELETE(m_textCtrl); | |
1472 | ||
1473 | return NULL; | |
1474 | } | |
1475 | ||
1476 | // if GetEditControl() hasn't been called, we need to initialize the edit | |
1477 | // control ourselves | |
1478 | if ( !m_textCtrl->GetHWND() ) | |
1479 | InitEditControl(hWnd); | |
1480 | ||
1481 | return m_textCtrl; | |
1482 | } | |
1483 | ||
1484 | // End label editing, optionally cancelling the edit | |
1485 | bool wxListCtrl::EndEditLabel(bool cancel) | |
1486 | { | |
1487 | // m_textCtrl is not always ready, ie. in EVT_LIST_BEGIN_LABEL_EDIT | |
1488 | HWND hwnd = ListView_GetEditControl(GetHwnd()); | |
1489 | if ( !hwnd ) | |
1490 | return false; | |
1491 | ||
1492 | // Newer versions of Windows have a special message for cancelling editing, | |
1493 | // use it if available. | |
1494 | #ifdef ListView_CancelEditLabel | |
1495 | if ( cancel && (wxApp::GetComCtl32Version() >= 600) ) | |
1496 | { | |
1497 | ListView_CancelEditLabel(GetHwnd()); | |
1498 | } | |
1499 | else | |
1500 | #endif // ListView_CancelEditLabel | |
1501 | { | |
1502 | // We shouldn't destroy the control ourselves according to MSDN, which | |
1503 | // proposes WM_CANCELMODE to do this, but it doesn't seem to work so | |
1504 | // emulate the corresponding user action instead. | |
1505 | ::SendMessage(hwnd, WM_KEYDOWN, cancel ? VK_ESCAPE : VK_RETURN, 0); | |
1506 | } | |
1507 | ||
1508 | return true; | |
1509 | } | |
1510 | ||
1511 | // Ensures this item is visible | |
1512 | bool wxListCtrl::EnsureVisible(long item) | |
1513 | { | |
1514 | return ListView_EnsureVisible(GetHwnd(), (int) item, FALSE) != FALSE; | |
1515 | } | |
1516 | ||
1517 | // Find an item whose label matches this string, starting from the item after 'start' | |
1518 | // or the beginning if 'start' is -1. | |
1519 | long wxListCtrl::FindItem(long start, const wxString& str, bool partial) | |
1520 | { | |
1521 | LV_FINDINFO findInfo; | |
1522 | ||
1523 | findInfo.flags = LVFI_STRING; | |
1524 | if ( partial ) | |
1525 | findInfo.flags |= LVFI_PARTIAL; | |
1526 | findInfo.psz = str.wx_str(); | |
1527 | ||
1528 | // ListView_FindItem() excludes the first item from search and to look | |
1529 | // through all the items you need to start from -1 which is unnatural and | |
1530 | // inconsistent with the generic version - so we adjust the index | |
1531 | if (start != -1) | |
1532 | start --; | |
1533 | return ListView_FindItem(GetHwnd(), start, &findInfo); | |
1534 | } | |
1535 | ||
1536 | // Find an item whose data matches this data, starting from the item after | |
1537 | // 'start' or the beginning if 'start' is -1. | |
1538 | long wxListCtrl::FindItem(long start, wxUIntPtr data) | |
1539 | { | |
1540 | // we can't use ListView_FindItem() directly as we don't store the data | |
1541 | // pointer itself in the control but rather our own internal data, so first | |
1542 | // we need to find the right value to search for (and there can be several | |
1543 | // of them) | |
1544 | int idx = wxNOT_FOUND; | |
1545 | const unsigned count = m_internalData.size(); | |
1546 | for ( unsigned n = 0; n < count; n++ ) | |
1547 | { | |
1548 | if ( m_internalData[n]->lParam == (LPARAM)data ) | |
1549 | { | |
1550 | LV_FINDINFO findInfo; | |
1551 | findInfo.flags = LVFI_PARAM; | |
1552 | findInfo.lParam = (LPARAM)wxPtrToUInt(m_internalData[n]); | |
1553 | ||
1554 | int rc = ListView_FindItem(GetHwnd(), start, &findInfo); | |
1555 | if ( rc != -1 ) | |
1556 | { | |
1557 | if ( idx == wxNOT_FOUND || rc < idx ) | |
1558 | { | |
1559 | idx = rc; | |
1560 | if ( idx == start + 1 ) | |
1561 | { | |
1562 | // we can stop here, we don't risk finding a closer | |
1563 | // match | |
1564 | break; | |
1565 | } | |
1566 | } | |
1567 | //else: this item is after the previously found one | |
1568 | } | |
1569 | } | |
1570 | } | |
1571 | ||
1572 | return idx; | |
1573 | } | |
1574 | ||
1575 | // Find an item nearest this position in the specified direction, starting from | |
1576 | // the item after 'start' or the beginning if 'start' is -1. | |
1577 | long wxListCtrl::FindItem(long start, const wxPoint& pt, int direction) | |
1578 | { | |
1579 | LV_FINDINFO findInfo; | |
1580 | ||
1581 | findInfo.flags = LVFI_NEARESTXY; | |
1582 | findInfo.pt.x = pt.x; | |
1583 | findInfo.pt.y = pt.y; | |
1584 | findInfo.vkDirection = VK_RIGHT; | |
1585 | ||
1586 | if ( direction == wxLIST_FIND_UP ) | |
1587 | findInfo.vkDirection = VK_UP; | |
1588 | else if ( direction == wxLIST_FIND_DOWN ) | |
1589 | findInfo.vkDirection = VK_DOWN; | |
1590 | else if ( direction == wxLIST_FIND_LEFT ) | |
1591 | findInfo.vkDirection = VK_LEFT; | |
1592 | else if ( direction == wxLIST_FIND_RIGHT ) | |
1593 | findInfo.vkDirection = VK_RIGHT; | |
1594 | ||
1595 | return ListView_FindItem(GetHwnd(), start, &findInfo); | |
1596 | } | |
1597 | ||
1598 | // Determines which item (if any) is at the specified point, | |
1599 | // giving details in 'flags' (see wxLIST_HITTEST_... flags above) | |
1600 | long | |
1601 | wxListCtrl::HitTest(const wxPoint& point, int& flags, long *ptrSubItem) const | |
1602 | { | |
1603 | LV_HITTESTINFO hitTestInfo; | |
1604 | hitTestInfo.pt.x = (int) point.x; | |
1605 | hitTestInfo.pt.y = (int) point.y; | |
1606 | ||
1607 | long item; | |
1608 | #ifdef LVM_SUBITEMHITTEST | |
1609 | if ( ptrSubItem && wxApp::GetComCtl32Version() >= 470 ) | |
1610 | { | |
1611 | item = ListView_SubItemHitTest(GetHwnd(), &hitTestInfo); | |
1612 | *ptrSubItem = hitTestInfo.iSubItem; | |
1613 | } | |
1614 | else | |
1615 | #endif // LVM_SUBITEMHITTEST | |
1616 | { | |
1617 | item = ListView_HitTest(GetHwnd(), &hitTestInfo); | |
1618 | } | |
1619 | ||
1620 | flags = 0; | |
1621 | ||
1622 | if ( hitTestInfo.flags & LVHT_ABOVE ) | |
1623 | flags |= wxLIST_HITTEST_ABOVE; | |
1624 | if ( hitTestInfo.flags & LVHT_BELOW ) | |
1625 | flags |= wxLIST_HITTEST_BELOW; | |
1626 | if ( hitTestInfo.flags & LVHT_TOLEFT ) | |
1627 | flags |= wxLIST_HITTEST_TOLEFT; | |
1628 | if ( hitTestInfo.flags & LVHT_TORIGHT ) | |
1629 | flags |= wxLIST_HITTEST_TORIGHT; | |
1630 | ||
1631 | if ( hitTestInfo.flags & LVHT_NOWHERE ) | |
1632 | flags |= wxLIST_HITTEST_NOWHERE; | |
1633 | ||
1634 | // note a bug or at least a very strange feature of comtl32.dll (tested | |
1635 | // with version 4.0 under Win95 and 6.0 under Win 2003): if you click to | |
1636 | // the right of the item label, ListView_HitTest() returns a combination of | |
1637 | // LVHT_ONITEMICON, LVHT_ONITEMLABEL and LVHT_ONITEMSTATEICON -- filter out | |
1638 | // the bits which don't make sense | |
1639 | if ( hitTestInfo.flags & LVHT_ONITEMLABEL ) | |
1640 | { | |
1641 | flags |= wxLIST_HITTEST_ONITEMLABEL; | |
1642 | ||
1643 | // do not translate LVHT_ONITEMICON here, as per above | |
1644 | } | |
1645 | else | |
1646 | { | |
1647 | if ( hitTestInfo.flags & LVHT_ONITEMICON ) | |
1648 | flags |= wxLIST_HITTEST_ONITEMICON; | |
1649 | if ( hitTestInfo.flags & LVHT_ONITEMSTATEICON ) | |
1650 | flags |= wxLIST_HITTEST_ONITEMSTATEICON; | |
1651 | } | |
1652 | ||
1653 | return item; | |
1654 | } | |
1655 | ||
1656 | ||
1657 | // Inserts an item, returning the index of the new item if successful, | |
1658 | // -1 otherwise. | |
1659 | long wxListCtrl::InsertItem(const wxListItem& info) | |
1660 | { | |
1661 | wxASSERT_MSG( !IsVirtual(), wxT("can't be used with virtual controls") ); | |
1662 | ||
1663 | LV_ITEM item; | |
1664 | wxConvertToMSWListItem(this, info, item); | |
1665 | item.mask &= ~LVIF_PARAM; | |
1666 | ||
1667 | // check whether we need to allocate our internal data | |
1668 | bool needInternalData = (info.m_mask & wxLIST_MASK_DATA) || | |
1669 | info.HasAttributes(); | |
1670 | if ( needInternalData ) | |
1671 | { | |
1672 | item.mask |= LVIF_PARAM; | |
1673 | ||
1674 | wxMSWListItemData * const data = new wxMSWListItemData; | |
1675 | m_internalData.push_back(data); | |
1676 | item.lParam = (LPARAM)data; | |
1677 | ||
1678 | if ( info.m_mask & wxLIST_MASK_DATA ) | |
1679 | data->lParam = info.m_data; | |
1680 | ||
1681 | // check whether it has any custom attributes | |
1682 | if ( info.HasAttributes() ) | |
1683 | { | |
1684 | // take copy of attributes | |
1685 | data->attr = new wxListItemAttr(*info.GetAttributes()); | |
1686 | ||
1687 | // and remember that we have some now... | |
1688 | m_hasAnyAttr = true; | |
1689 | } | |
1690 | } | |
1691 | ||
1692 | const long rv = ListView_InsertItem(GetHwnd(), & item); | |
1693 | ||
1694 | // failing to insert the item is really unexpected | |
1695 | wxCHECK_MSG( rv != -1, rv, "failed to insert an item in wxListCtrl" ); | |
1696 | ||
1697 | m_count++; | |
1698 | wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()), | |
1699 | wxT("m_count should match ListView_GetItemCount")); | |
1700 | ||
1701 | return rv; | |
1702 | } | |
1703 | ||
1704 | long wxListCtrl::InsertItem(long index, const wxString& label) | |
1705 | { | |
1706 | wxListItem info; | |
1707 | info.m_text = label; | |
1708 | info.m_mask = wxLIST_MASK_TEXT; | |
1709 | info.m_itemId = index; | |
1710 | return InsertItem(info); | |
1711 | } | |
1712 | ||
1713 | // Inserts an image item | |
1714 | long wxListCtrl::InsertItem(long index, int imageIndex) | |
1715 | { | |
1716 | wxListItem info; | |
1717 | info.m_image = imageIndex; | |
1718 | info.m_mask = wxLIST_MASK_IMAGE; | |
1719 | info.m_itemId = index; | |
1720 | return InsertItem(info); | |
1721 | } | |
1722 | ||
1723 | // Inserts an image/string item | |
1724 | long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex) | |
1725 | { | |
1726 | wxListItem info; | |
1727 | info.m_image = imageIndex; | |
1728 | info.m_text = label; | |
1729 | info.m_mask = wxLIST_MASK_IMAGE | wxLIST_MASK_TEXT; | |
1730 | info.m_itemId = index; | |
1731 | return InsertItem(info); | |
1732 | } | |
1733 | ||
1734 | // For list view mode (only), inserts a column. | |
1735 | long wxListCtrl::InsertColumn(long col, const wxListItem& item) | |
1736 | { | |
1737 | LV_COLUMN lvCol; | |
1738 | wxConvertToMSWListCol(GetHwnd(), col, item, lvCol); | |
1739 | ||
1740 | if ( !(lvCol.mask & LVCF_WIDTH) ) | |
1741 | { | |
1742 | // always give some width to the new column: this one is compatible | |
1743 | // with the generic version | |
1744 | lvCol.mask |= LVCF_WIDTH; | |
1745 | lvCol.cx = 80; | |
1746 | } | |
1747 | ||
1748 | long n = ListView_InsertColumn(GetHwnd(), col, &lvCol); | |
1749 | if ( n != -1 ) | |
1750 | { | |
1751 | m_colCount++; | |
1752 | } | |
1753 | else // failed to insert? | |
1754 | { | |
1755 | wxLogDebug(wxT("Failed to insert the column '%s' into listview!"), | |
1756 | lvCol.pszText); | |
1757 | } | |
1758 | ||
1759 | return n; | |
1760 | } | |
1761 | ||
1762 | long wxListCtrl::InsertColumn(long col, | |
1763 | const wxString& heading, | |
1764 | int format, | |
1765 | int width) | |
1766 | { | |
1767 | wxListItem item; | |
1768 | item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT; | |
1769 | item.m_text = heading; | |
1770 | if ( width > -1 ) | |
1771 | { | |
1772 | item.m_mask |= wxLIST_MASK_WIDTH; | |
1773 | item.m_width = width; | |
1774 | } | |
1775 | item.m_format = format; | |
1776 | ||
1777 | return InsertColumn(col, item); | |
1778 | } | |
1779 | ||
1780 | // scroll the control by the given number of pixels (exception: in list view, | |
1781 | // dx is interpreted as number of columns) | |
1782 | bool wxListCtrl::ScrollList(int dx, int dy) | |
1783 | { | |
1784 | if ( !ListView_Scroll(GetHwnd(), dx, dy) ) | |
1785 | { | |
1786 | wxLogDebug(wxT("ListView_Scroll(%d, %d) failed"), dx, dy); | |
1787 | ||
1788 | return false; | |
1789 | } | |
1790 | ||
1791 | return true; | |
1792 | } | |
1793 | ||
1794 | // Sort items. | |
1795 | ||
1796 | // fn is a function which takes 3 long arguments: item1, item2, data. | |
1797 | // item1 is the long data associated with a first item (NOT the index). | |
1798 | // item2 is the long data associated with a second item (NOT the index). | |
1799 | // data is the same value as passed to SortItems. | |
1800 | // The return value is a negative number if the first item should precede the second | |
1801 | // item, a positive number of the second item should precede the first, | |
1802 | // or zero if the two items are equivalent. | |
1803 | ||
1804 | // data is arbitrary data to be passed to the sort function. | |
1805 | ||
1806 | // Internal structures for proxying the user compare function | |
1807 | // so that we can pass it the *real* user data | |
1808 | ||
1809 | // translate lParam data and call user func | |
1810 | struct wxInternalDataSort | |
1811 | { | |
1812 | wxListCtrlCompare user_fn; | |
1813 | wxIntPtr data; | |
1814 | }; | |
1815 | ||
1816 | int CALLBACK wxInternalDataCompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) | |
1817 | { | |
1818 | wxInternalDataSort * const internalData = (wxInternalDataSort *) lParamSort; | |
1819 | ||
1820 | wxMSWListItemData *data1 = (wxMSWListItemData *) lParam1; | |
1821 | wxMSWListItemData *data2 = (wxMSWListItemData *) lParam2; | |
1822 | ||
1823 | long d1 = (data1 == NULL ? 0 : data1->lParam); | |
1824 | long d2 = (data2 == NULL ? 0 : data2->lParam); | |
1825 | ||
1826 | return internalData->user_fn(d1, d2, internalData->data); | |
1827 | ||
1828 | } | |
1829 | ||
1830 | bool wxListCtrl::SortItems(wxListCtrlCompare fn, wxIntPtr data) | |
1831 | { | |
1832 | wxInternalDataSort internalData; | |
1833 | internalData.user_fn = fn; | |
1834 | internalData.data = data; | |
1835 | ||
1836 | // WPARAM cast is needed for mingw/cygwin | |
1837 | if ( !ListView_SortItems(GetHwnd(), | |
1838 | wxInternalDataCompareFunc, | |
1839 | (WPARAM) &internalData) ) | |
1840 | { | |
1841 | wxLogDebug(wxT("ListView_SortItems() failed")); | |
1842 | ||
1843 | return false; | |
1844 | } | |
1845 | ||
1846 | return true; | |
1847 | } | |
1848 | ||
1849 | ||
1850 | ||
1851 | // ---------------------------------------------------------------------------- | |
1852 | // message processing | |
1853 | // ---------------------------------------------------------------------------- | |
1854 | ||
1855 | bool wxListCtrl::MSWShouldPreProcessMessage(WXMSG* msg) | |
1856 | { | |
1857 | if ( msg->message == WM_KEYDOWN ) | |
1858 | { | |
1859 | // Only eat VK_RETURN if not being used by the application in | |
1860 | // conjunction with modifiers | |
1861 | if ( msg->wParam == VK_RETURN && !wxIsAnyModifierDown() ) | |
1862 | { | |
1863 | // we need VK_RETURN to generate wxEVT_COMMAND_LIST_ITEM_ACTIVATED | |
1864 | return false; | |
1865 | } | |
1866 | } | |
1867 | return wxControl::MSWShouldPreProcessMessage(msg); | |
1868 | } | |
1869 | ||
1870 | bool wxListCtrl::MSWCommand(WXUINT cmd, WXWORD id_) | |
1871 | { | |
1872 | const int id = (signed short)id_; | |
1873 | if (cmd == EN_UPDATE) | |
1874 | { | |
1875 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id); | |
1876 | event.SetEventObject( this ); | |
1877 | ProcessCommand(event); | |
1878 | return true; | |
1879 | } | |
1880 | else if (cmd == EN_KILLFOCUS) | |
1881 | { | |
1882 | wxCommandEvent event(wxEVT_KILL_FOCUS, id); | |
1883 | event.SetEventObject( this ); | |
1884 | ProcessCommand(event); | |
1885 | return true; | |
1886 | } | |
1887 | else | |
1888 | return false; | |
1889 | } | |
1890 | ||
1891 | // utility used by wxListCtrl::MSWOnNotify and by wxDataViewHeaderWindowMSW::MSWOnNotify | |
1892 | int WXDLLIMPEXP_CORE wxMSWGetColumnClicked(NMHDR *nmhdr, POINT *ptClick) | |
1893 | { | |
1894 | // find the column clicked: we have to search for it ourselves as the | |
1895 | // notification message doesn't provide this info | |
1896 | ||
1897 | // where did the click occur? | |
1898 | #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400 | |
1899 | if ( nmhdr->code == GN_CONTEXTMENU ) | |
1900 | { | |
1901 | *ptClick = ((NMRGINFO*)nmhdr)->ptAction; | |
1902 | } | |
1903 | else | |
1904 | #endif //__WXWINCE__ | |
1905 | if ( !::GetCursorPos(ptClick) ) | |
1906 | { | |
1907 | wxLogLastError(wxT("GetCursorPos")); | |
1908 | } | |
1909 | ||
1910 | // we need to use listctrl coordinates for the event point so this is what | |
1911 | // we return in ptClick, but for comparison with Header_GetItemRect() | |
1912 | // result below we need to use header window coordinates | |
1913 | POINT ptClickHeader = *ptClick; | |
1914 | if ( !::ScreenToClient(nmhdr->hwndFrom, &ptClickHeader) ) | |
1915 | { | |
1916 | wxLogLastError(wxT("ScreenToClient(listctrl header)")); | |
1917 | } | |
1918 | ||
1919 | if ( !::ScreenToClient(::GetParent(nmhdr->hwndFrom), ptClick) ) | |
1920 | { | |
1921 | wxLogLastError(wxT("ScreenToClient(listctrl)")); | |
1922 | } | |
1923 | ||
1924 | const int colCount = Header_GetItemCount(nmhdr->hwndFrom); | |
1925 | for ( int col = 0; col < colCount; col++ ) | |
1926 | { | |
1927 | RECT rect; | |
1928 | if ( Header_GetItemRect(nmhdr->hwndFrom, col, &rect) ) | |
1929 | { | |
1930 | if ( ::PtInRect(&rect, ptClickHeader) ) | |
1931 | { | |
1932 | return col; | |
1933 | } | |
1934 | } | |
1935 | } | |
1936 | ||
1937 | return wxNOT_FOUND; | |
1938 | } | |
1939 | ||
1940 | bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) | |
1941 | { | |
1942 | ||
1943 | // prepare the event | |
1944 | // ----------------- | |
1945 | ||
1946 | wxListEvent event(wxEVT_NULL, m_windowId); | |
1947 | event.SetEventObject(this); | |
1948 | ||
1949 | wxEventType eventType = wxEVT_NULL; | |
1950 | ||
1951 | NMHDR *nmhdr = (NMHDR *)lParam; | |
1952 | ||
1953 | // if your compiler is as broken as this, you should really change it: this | |
1954 | // code is needed for normal operation! #ifdef below is only useful for | |
1955 | // automatic rebuilds which are done with a very old compiler version | |
1956 | #ifdef HDN_BEGINTRACKA | |
1957 | ||
1958 | // check for messages from the header (in report view) | |
1959 | HWND hwndHdr = ListView_GetHeader(GetHwnd()); | |
1960 | ||
1961 | // is it a message from the header? | |
1962 | if ( nmhdr->hwndFrom == hwndHdr ) | |
1963 | { | |
1964 | HD_NOTIFY *nmHDR = (HD_NOTIFY *)nmhdr; | |
1965 | ||
1966 | event.m_itemIndex = -1; | |
1967 | ||
1968 | bool ignore = false; | |
1969 | switch ( nmhdr->code ) | |
1970 | { | |
1971 | // yet another comctl32.dll bug: under NT/W2K it sends Unicode | |
1972 | // TRACK messages even to ANSI programs: on my system I get | |
1973 | // HDN_BEGINTRACKW and HDN_ENDTRACKA! | |
1974 | // | |
1975 | // work around is to simply catch both versions and hope that it | |
1976 | // works (why should this message exist in ANSI and Unicode is | |
1977 | // beyond me as it doesn't deal with strings at all...) | |
1978 | // | |
1979 | // another problem is that HDN_TRACK is not sent at all by header | |
1980 | // with HDS_FULLDRAG style which is used by default by wxListCtrl | |
1981 | // under recent Windows versions (starting from at least XP) so we | |
1982 | // need to use HDN_ITEMCHANGING instead of it | |
1983 | case HDN_BEGINTRACKA: | |
1984 | case HDN_BEGINTRACKW: | |
1985 | eventType = wxEVT_COMMAND_LIST_COL_BEGIN_DRAG; | |
1986 | // fall through | |
1987 | ||
1988 | case HDN_ITEMCHANGING: | |
1989 | if ( eventType == wxEVT_NULL ) | |
1990 | { | |
1991 | if ( !nmHDR->pitem || !(nmHDR->pitem->mask & HDI_WIDTH) ) | |
1992 | { | |
1993 | // something other than the width is being changed, | |
1994 | // ignore it | |
1995 | ignore = true; | |
1996 | break; | |
1997 | } | |
1998 | ||
1999 | // also ignore the events sent when the width didn't really | |
2000 | // change: this is not just an optimization but also gets | |
2001 | // rid of a useless and unexpected DRAGGING event which | |
2002 | // would otherwise be sent after the END_DRAG one as we get | |
2003 | // an HDN_ITEMCHANGING after HDN_ENDTRACK for some reason | |
2004 | if ( nmHDR->pitem->cxy == GetColumnWidth(nmHDR->iItem) ) | |
2005 | { | |
2006 | ignore = true; | |
2007 | break; | |
2008 | } | |
2009 | ||
2010 | eventType = wxEVT_COMMAND_LIST_COL_DRAGGING; | |
2011 | } | |
2012 | // fall through | |
2013 | ||
2014 | case HDN_ENDTRACKA: | |
2015 | case HDN_ENDTRACKW: | |
2016 | if ( eventType == wxEVT_NULL ) | |
2017 | eventType = wxEVT_COMMAND_LIST_COL_END_DRAG; | |
2018 | ||
2019 | event.m_item.m_width = nmHDR->pitem->cxy; | |
2020 | event.m_col = nmHDR->iItem; | |
2021 | break; | |
2022 | ||
2023 | #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400 | |
2024 | case GN_CONTEXTMENU: | |
2025 | #endif //__WXWINCE__ | |
2026 | case NM_RCLICK: | |
2027 | { | |
2028 | POINT ptClick; | |
2029 | ||
2030 | eventType = wxEVT_COMMAND_LIST_COL_RIGHT_CLICK; | |
2031 | event.m_col = wxMSWGetColumnClicked(nmhdr, &ptClick); | |
2032 | event.m_pointDrag.x = ptClick.x; | |
2033 | event.m_pointDrag.y = ptClick.y; | |
2034 | } | |
2035 | break; | |
2036 | ||
2037 | case HDN_GETDISPINFOW: | |
2038 | // letting Windows XP handle this message results in mysterious | |
2039 | // crashes in comctl32.dll seemingly because of bad message | |
2040 | // parameters | |
2041 | // | |
2042 | // I have no idea what is the real cause of the bug (which is, | |
2043 | // just to make things interesting, impossible to reproduce | |
2044 | // reliably) but ignoring all these messages does fix it and | |
2045 | // doesn't seem to have any negative consequences | |
2046 | return true; | |
2047 | ||
2048 | default: | |
2049 | ignore = true; | |
2050 | } | |
2051 | ||
2052 | if ( ignore ) | |
2053 | return wxControl::MSWOnNotify(idCtrl, lParam, result); | |
2054 | } | |
2055 | else | |
2056 | #endif // defined(HDN_BEGINTRACKA) | |
2057 | if ( nmhdr->hwndFrom == GetHwnd() ) | |
2058 | { | |
2059 | // almost all messages use NM_LISTVIEW | |
2060 | NM_LISTVIEW *nmLV = (NM_LISTVIEW *)nmhdr; | |
2061 | ||
2062 | const int iItem = nmLV->iItem; | |
2063 | ||
2064 | ||
2065 | // If we have a valid item then check if there is a data value | |
2066 | // associated with it and put it in the event. | |
2067 | if ( iItem >= 0 && iItem < GetItemCount() ) | |
2068 | { | |
2069 | wxMSWListItemData *internaldata = | |
2070 | MSWGetItemData(iItem); | |
2071 | ||
2072 | if ( internaldata ) | |
2073 | event.m_item.m_data = internaldata->lParam; | |
2074 | } | |
2075 | ||
2076 | bool processed = true; | |
2077 | switch ( nmhdr->code ) | |
2078 | { | |
2079 | case LVN_BEGINRDRAG: | |
2080 | eventType = wxEVT_COMMAND_LIST_BEGIN_RDRAG; | |
2081 | // fall through | |
2082 | ||
2083 | case LVN_BEGINDRAG: | |
2084 | if ( eventType == wxEVT_NULL ) | |
2085 | { | |
2086 | eventType = wxEVT_COMMAND_LIST_BEGIN_DRAG; | |
2087 | } | |
2088 | ||
2089 | event.m_itemIndex = iItem; | |
2090 | event.m_pointDrag.x = nmLV->ptAction.x; | |
2091 | event.m_pointDrag.y = nmLV->ptAction.y; | |
2092 | break; | |
2093 | ||
2094 | // NB: we have to handle both *A and *W versions here because some | |
2095 | // versions of comctl32.dll send ANSI messages even to the | |
2096 | // Unicode windows | |
2097 | case LVN_BEGINLABELEDITA: | |
2098 | case LVN_BEGINLABELEDITW: | |
2099 | { | |
2100 | wxLV_ITEM item; | |
2101 | if ( nmhdr->code == LVN_BEGINLABELEDITA ) | |
2102 | { | |
2103 | item.Init(((LV_DISPINFOA *)lParam)->item); | |
2104 | } | |
2105 | else // LVN_BEGINLABELEDITW | |
2106 | { | |
2107 | item.Init(((LV_DISPINFOW *)lParam)->item); | |
2108 | } | |
2109 | ||
2110 | eventType = wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT; | |
2111 | wxConvertFromMSWListItem(GetHwnd(), event.m_item, item); | |
2112 | event.m_itemIndex = event.m_item.m_itemId; | |
2113 | } | |
2114 | break; | |
2115 | ||
2116 | case LVN_ENDLABELEDITA: | |
2117 | case LVN_ENDLABELEDITW: | |
2118 | { | |
2119 | wxLV_ITEM item; | |
2120 | if ( nmhdr->code == LVN_ENDLABELEDITA ) | |
2121 | { | |
2122 | item.Init(((LV_DISPINFOA *)lParam)->item); | |
2123 | } | |
2124 | else // LVN_ENDLABELEDITW | |
2125 | { | |
2126 | item.Init(((LV_DISPINFOW *)lParam)->item); | |
2127 | } | |
2128 | ||
2129 | // was editing cancelled? | |
2130 | const LV_ITEM& lvi = (LV_ITEM)item; | |
2131 | if ( !lvi.pszText || lvi.iItem == -1 ) | |
2132 | { | |
2133 | // EDIT control will be deleted by the list control | |
2134 | // itself so prevent us from deleting it as well | |
2135 | DeleteEditControl(); | |
2136 | ||
2137 | event.SetEditCanceled(true); | |
2138 | } | |
2139 | ||
2140 | eventType = wxEVT_COMMAND_LIST_END_LABEL_EDIT; | |
2141 | wxConvertFromMSWListItem(NULL, event.m_item, item); | |
2142 | event.m_itemIndex = event.m_item.m_itemId; | |
2143 | } | |
2144 | break; | |
2145 | ||
2146 | case LVN_COLUMNCLICK: | |
2147 | eventType = wxEVT_COMMAND_LIST_COL_CLICK; | |
2148 | event.m_itemIndex = -1; | |
2149 | event.m_col = nmLV->iSubItem; | |
2150 | break; | |
2151 | ||
2152 | case LVN_DELETEALLITEMS: | |
2153 | eventType = wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS; | |
2154 | event.m_itemIndex = -1; | |
2155 | break; | |
2156 | ||
2157 | case LVN_DELETEITEM: | |
2158 | if ( m_count == 0 ) | |
2159 | { | |
2160 | // this should be prevented by the post-processing code | |
2161 | // below, but "just in case" | |
2162 | return false; | |
2163 | } | |
2164 | ||
2165 | eventType = wxEVT_COMMAND_LIST_DELETE_ITEM; | |
2166 | event.m_itemIndex = iItem; | |
2167 | ||
2168 | break; | |
2169 | ||
2170 | case LVN_INSERTITEM: | |
2171 | eventType = wxEVT_COMMAND_LIST_INSERT_ITEM; | |
2172 | event.m_itemIndex = iItem; | |
2173 | break; | |
2174 | ||
2175 | case LVN_ITEMCHANGED: | |
2176 | // we translate this catch all message into more interesting | |
2177 | // (and more easy to process) wxWidgets events | |
2178 | ||
2179 | // first of all, we deal with the state change events only and | |
2180 | // only for valid items (item == -1 for the virtual list | |
2181 | // control) | |
2182 | if ( nmLV->uChanged & LVIF_STATE && iItem != -1 ) | |
2183 | { | |
2184 | // temp vars for readability | |
2185 | const UINT stOld = nmLV->uOldState; | |
2186 | const UINT stNew = nmLV->uNewState; | |
2187 | ||
2188 | event.m_item.SetId(iItem); | |
2189 | event.m_item.SetMask(wxLIST_MASK_TEXT | | |
2190 | wxLIST_MASK_IMAGE | | |
2191 | wxLIST_MASK_DATA); | |
2192 | GetItem(event.m_item); | |
2193 | ||
2194 | // has the focus changed? | |
2195 | if ( !(stOld & LVIS_FOCUSED) && (stNew & LVIS_FOCUSED) ) | |
2196 | { | |
2197 | eventType = wxEVT_COMMAND_LIST_ITEM_FOCUSED; | |
2198 | event.m_itemIndex = iItem; | |
2199 | } | |
2200 | ||
2201 | if ( (stNew & LVIS_SELECTED) != (stOld & LVIS_SELECTED) ) | |
2202 | { | |
2203 | if ( eventType != wxEVT_NULL ) | |
2204 | { | |
2205 | // focus and selection have both changed: send the | |
2206 | // focus event from here and the selection one | |
2207 | // below | |
2208 | event.SetEventType(eventType); | |
2209 | (void)HandleWindowEvent(event); | |
2210 | } | |
2211 | else // no focus event to send | |
2212 | { | |
2213 | // then need to set m_itemIndex as it wasn't done | |
2214 | // above | |
2215 | event.m_itemIndex = iItem; | |
2216 | } | |
2217 | ||
2218 | eventType = stNew & LVIS_SELECTED | |
2219 | ? wxEVT_COMMAND_LIST_ITEM_SELECTED | |
2220 | : wxEVT_COMMAND_LIST_ITEM_DESELECTED; | |
2221 | } | |
2222 | } | |
2223 | ||
2224 | if ( eventType == wxEVT_NULL ) | |
2225 | { | |
2226 | // not an interesting event for us | |
2227 | return false; | |
2228 | } | |
2229 | ||
2230 | break; | |
2231 | ||
2232 | case LVN_KEYDOWN: | |
2233 | { | |
2234 | LV_KEYDOWN *info = (LV_KEYDOWN *)lParam; | |
2235 | WORD wVKey = info->wVKey; | |
2236 | ||
2237 | // get the current selection | |
2238 | long lItem = GetNextItem(-1, | |
2239 | wxLIST_NEXT_ALL, | |
2240 | wxLIST_STATE_SELECTED); | |
2241 | ||
2242 | // <Enter> or <Space> activate the selected item if any (but | |
2243 | // not with any modifiers as they have a predefined meaning | |
2244 | // then) | |
2245 | if ( lItem != -1 && | |
2246 | (wVKey == VK_RETURN || wVKey == VK_SPACE) && | |
2247 | !wxIsAnyModifierDown() ) | |
2248 | { | |
2249 | eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED; | |
2250 | } | |
2251 | else | |
2252 | { | |
2253 | eventType = wxEVT_COMMAND_LIST_KEY_DOWN; | |
2254 | ||
2255 | event.m_code = wxMSWKeyboard::VKToWX(wVKey); | |
2256 | ||
2257 | if ( event.m_code == WXK_NONE ) | |
2258 | { | |
2259 | // We can't translate this to a standard key code, | |
2260 | // until support for Unicode key codes is added to | |
2261 | // wxListEvent we just ignore them. | |
2262 | return false; | |
2263 | } | |
2264 | } | |
2265 | ||
2266 | event.m_itemIndex = | |
2267 | event.m_item.m_itemId = lItem; | |
2268 | ||
2269 | if ( lItem != -1 ) | |
2270 | { | |
2271 | // fill the other fields too | |
2272 | event.m_item.m_text = GetItemText(lItem); | |
2273 | event.m_item.m_data = GetItemData(lItem); | |
2274 | } | |
2275 | } | |
2276 | break; | |
2277 | ||
2278 | case NM_DBLCLK: | |
2279 | // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do | |
2280 | // anything else | |
2281 | if ( wxControl::MSWOnNotify(idCtrl, lParam, result) ) | |
2282 | { | |
2283 | return true; | |
2284 | } | |
2285 | ||
2286 | // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event | |
2287 | // if it happened on an item (and not on empty place) | |
2288 | if ( iItem == -1 ) | |
2289 | { | |
2290 | // not on item | |
2291 | return false; | |
2292 | } | |
2293 | ||
2294 | eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED; | |
2295 | event.m_itemIndex = iItem; | |
2296 | event.m_item.m_text = GetItemText(iItem); | |
2297 | event.m_item.m_data = GetItemData(iItem); | |
2298 | break; | |
2299 | ||
2300 | #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400 | |
2301 | case GN_CONTEXTMENU: | |
2302 | #endif //__WXWINCE__ | |
2303 | case NM_RCLICK: | |
2304 | // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(), | |
2305 | // don't do anything else | |
2306 | if ( wxControl::MSWOnNotify(idCtrl, lParam, result) ) | |
2307 | { | |
2308 | return true; | |
2309 | } | |
2310 | ||
2311 | // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event | |
2312 | LV_HITTESTINFO lvhti; | |
2313 | wxZeroMemory(lvhti); | |
2314 | ||
2315 | #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400 | |
2316 | if ( nmhdr->code == GN_CONTEXTMENU ) | |
2317 | { | |
2318 | lvhti.pt = ((NMRGINFO*)nmhdr)->ptAction; | |
2319 | } | |
2320 | else | |
2321 | #endif //__WXWINCE__ | |
2322 | { | |
2323 | ::GetCursorPos(&(lvhti.pt)); | |
2324 | } | |
2325 | ||
2326 | ::ScreenToClient(GetHwnd(), &lvhti.pt); | |
2327 | if ( ListView_HitTest(GetHwnd(), &lvhti) != -1 ) | |
2328 | { | |
2329 | if ( lvhti.flags & LVHT_ONITEM ) | |
2330 | { | |
2331 | eventType = wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK; | |
2332 | event.m_itemIndex = lvhti.iItem; | |
2333 | event.m_pointDrag.x = lvhti.pt.x; | |
2334 | event.m_pointDrag.y = lvhti.pt.y; | |
2335 | } | |
2336 | } | |
2337 | break; | |
2338 | ||
2339 | #ifdef NM_CUSTOMDRAW | |
2340 | case NM_CUSTOMDRAW: | |
2341 | *result = OnCustomDraw(lParam); | |
2342 | ||
2343 | return *result != CDRF_DODEFAULT; | |
2344 | #endif // _WIN32_IE >= 0x300 | |
2345 | ||
2346 | case LVN_ODCACHEHINT: | |
2347 | { | |
2348 | const NM_CACHEHINT *cacheHint = (NM_CACHEHINT *)lParam; | |
2349 | ||
2350 | eventType = wxEVT_COMMAND_LIST_CACHE_HINT; | |
2351 | ||
2352 | // we get some really stupid cache hints like ones for | |
2353 | // items in range 0..0 for an empty control or, after | |
2354 | // deleting an item, for items in invalid range -- filter | |
2355 | // this garbage out | |
2356 | if ( cacheHint->iFrom > cacheHint->iTo ) | |
2357 | return false; | |
2358 | ||
2359 | event.m_oldItemIndex = cacheHint->iFrom; | |
2360 | ||
2361 | const long iMax = GetItemCount(); | |
2362 | event.m_itemIndex = cacheHint->iTo < iMax ? cacheHint->iTo | |
2363 | : iMax - 1; | |
2364 | } | |
2365 | break; | |
2366 | ||
2367 | #ifdef HAVE_NMLVFINDITEM | |
2368 | case LVN_ODFINDITEM: | |
2369 | // this message is only used with the virtual list control but | |
2370 | // even there we don't want to always use it: in a control with | |
2371 | // sufficiently big number of items (defined as > 1000 here), | |
2372 | // accidentally pressing a key could result in hanging an | |
2373 | // application waiting while it performs linear search | |
2374 | if ( IsVirtual() && GetItemCount() <= 1000 ) | |
2375 | { | |
2376 | NMLVFINDITEM* pFindInfo = (NMLVFINDITEM*)lParam; | |
2377 | ||
2378 | // no match by default | |
2379 | *result = -1; | |
2380 | ||
2381 | // we only handle string-based searches here | |
2382 | // | |
2383 | // TODO: what about LVFI_PARTIAL, should we handle this? | |
2384 | if ( !(pFindInfo->lvfi.flags & LVFI_STRING) ) | |
2385 | { | |
2386 | return false; | |
2387 | } | |
2388 | ||
2389 | const wxChar * const searchstr = pFindInfo->lvfi.psz; | |
2390 | const size_t len = wxStrlen(searchstr); | |
2391 | ||
2392 | // this is the first item we should examine, search from it | |
2393 | // wrapping if necessary | |
2394 | const int startPos = pFindInfo->iStart; | |
2395 | const int maxPos = GetItemCount(); | |
2396 | wxCHECK_MSG( startPos <= maxPos, false, | |
2397 | wxT("bad starting position in LVN_ODFINDITEM") ); | |
2398 | ||
2399 | int currentPos = startPos; | |
2400 | do | |
2401 | { | |
2402 | // wrap to the beginning if necessary | |
2403 | if ( currentPos == maxPos ) | |
2404 | { | |
2405 | // somewhat surprisingly, LVFI_WRAP isn't set in | |
2406 | // flags but we still should wrap | |
2407 | currentPos = 0; | |
2408 | } | |
2409 | ||
2410 | // does this item begin with searchstr? | |
2411 | if ( wxStrnicmp(searchstr, | |
2412 | GetItemText(currentPos), len) == 0 ) | |
2413 | { | |
2414 | *result = currentPos; | |
2415 | break; | |
2416 | } | |
2417 | } | |
2418 | while ( ++currentPos != startPos ); | |
2419 | ||
2420 | if ( *result == -1 ) | |
2421 | { | |
2422 | // not found | |
2423 | return false; | |
2424 | } | |
2425 | ||
2426 | SetItemState(*result, | |
2427 | wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED, | |
2428 | wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED); | |
2429 | EnsureVisible(*result); | |
2430 | return true; | |
2431 | } | |
2432 | else | |
2433 | { | |
2434 | processed = false; | |
2435 | } | |
2436 | break; | |
2437 | #endif // HAVE_NMLVFINDITEM | |
2438 | ||
2439 | case LVN_GETDISPINFO: | |
2440 | if ( IsVirtual() ) | |
2441 | { | |
2442 | LV_DISPINFO *info = (LV_DISPINFO *)lParam; | |
2443 | ||
2444 | LV_ITEM& lvi = info->item; | |
2445 | long item = lvi.iItem; | |
2446 | ||
2447 | if ( lvi.mask & LVIF_TEXT ) | |
2448 | { | |
2449 | wxString text = OnGetItemText(item, lvi.iSubItem); | |
2450 | wxStrlcpy(lvi.pszText, text.c_str(), lvi.cchTextMax); | |
2451 | } | |
2452 | ||
2453 | // see comment at the end of wxListCtrl::GetColumn() | |
2454 | #ifdef NM_CUSTOMDRAW | |
2455 | if ( lvi.mask & LVIF_IMAGE ) | |
2456 | { | |
2457 | lvi.iImage = OnGetItemColumnImage(item, lvi.iSubItem); | |
2458 | } | |
2459 | #endif // NM_CUSTOMDRAW | |
2460 | ||
2461 | // even though we never use LVM_SETCALLBACKMASK, we still | |
2462 | // can get messages with LVIF_STATE in lvi.mask under Vista | |
2463 | if ( lvi.mask & LVIF_STATE ) | |
2464 | { | |
2465 | // we don't have anything to return from here... | |
2466 | lvi.stateMask = 0; | |
2467 | } | |
2468 | ||
2469 | return true; | |
2470 | } | |
2471 | // fall through | |
2472 | ||
2473 | default: | |
2474 | processed = false; | |
2475 | } | |
2476 | ||
2477 | if ( !processed ) | |
2478 | return wxControl::MSWOnNotify(idCtrl, lParam, result); | |
2479 | } | |
2480 | else | |
2481 | { | |
2482 | // where did this one come from? | |
2483 | return false; | |
2484 | } | |
2485 | ||
2486 | // process the event | |
2487 | // ----------------- | |
2488 | ||
2489 | event.SetEventType(eventType); | |
2490 | ||
2491 | // fill in the item before passing it to the event handler if we do have a | |
2492 | // valid item index and haven't filled it yet (e.g. for LVN_ITEMCHANGED) | |
2493 | if ( event.m_itemIndex != -1 && !event.m_item.GetMask() ) | |
2494 | { | |
2495 | wxListItem& item = event.m_item; | |
2496 | ||
2497 | item.SetId(event.m_itemIndex); | |
2498 | item.SetMask(wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE | wxLIST_MASK_DATA); | |
2499 | GetItem(item); | |
2500 | } | |
2501 | ||
2502 | bool processed = HandleWindowEvent(event); | |
2503 | ||
2504 | // post processing | |
2505 | // --------------- | |
2506 | switch ( nmhdr->code ) | |
2507 | { | |
2508 | case LVN_DELETEALLITEMS: | |
2509 | // always return true to suppress all additional LVN_DELETEITEM | |
2510 | // notifications - this makes deleting all items from a list ctrl | |
2511 | // much faster | |
2512 | *result = TRUE; | |
2513 | ||
2514 | // also, we may free all user data now (couldn't do it before as | |
2515 | // the user should have access to it in OnDeleteAllItems() handler) | |
2516 | FreeAllInternalData(); | |
2517 | ||
2518 | // the control is empty now, synchronize the cached number of items | |
2519 | // with the real one | |
2520 | m_count = 0; | |
2521 | return true; | |
2522 | ||
2523 | case LVN_DELETEITEM: | |
2524 | // Delete the associated internal data. Notice that this can be | |
2525 | // done only after the event has been handled as the data could be | |
2526 | // accessed during the handling of the event. | |
2527 | if ( wxMSWListItemData *data = MSWGetItemData(event.m_itemIndex) ) | |
2528 | { | |
2529 | const unsigned count = m_internalData.size(); | |
2530 | for ( unsigned n = 0; n < count; n++ ) | |
2531 | { | |
2532 | if ( m_internalData[n] == data ) | |
2533 | { | |
2534 | m_internalData.erase(m_internalData.begin() + n); | |
2535 | wxDELETE(data); | |
2536 | break; | |
2537 | } | |
2538 | } | |
2539 | ||
2540 | wxASSERT_MSG( !data, "invalid internal data pointer?" ); | |
2541 | } | |
2542 | break; | |
2543 | ||
2544 | case LVN_ENDLABELEDITA: | |
2545 | case LVN_ENDLABELEDITW: | |
2546 | // logic here is inverted compared to all the other messages | |
2547 | *result = event.IsAllowed(); | |
2548 | ||
2549 | // EDIT control will be deleted by the list control itself so | |
2550 | // prevent us from deleting it as well | |
2551 | DeleteEditControl(); | |
2552 | ||
2553 | return true; | |
2554 | } | |
2555 | ||
2556 | if ( processed ) | |
2557 | *result = !event.IsAllowed(); | |
2558 | ||
2559 | return processed; | |
2560 | } | |
2561 | ||
2562 | // ---------------------------------------------------------------------------- | |
2563 | // custom draw stuff | |
2564 | // ---------------------------------------------------------------------------- | |
2565 | ||
2566 | // see comment at the end of wxListCtrl::GetColumn() | |
2567 | #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300 | |
2568 | ||
2569 | static RECT GetCustomDrawnItemRect(const NMCUSTOMDRAW& nmcd) | |
2570 | { | |
2571 | RECT rc; | |
2572 | wxGetListCtrlItemRect(nmcd.hdr.hwndFrom, nmcd.dwItemSpec, LVIR_BOUNDS, rc); | |
2573 | ||
2574 | RECT rcIcon; | |
2575 | wxGetListCtrlItemRect(nmcd.hdr.hwndFrom, nmcd.dwItemSpec, LVIR_ICON, rcIcon); | |
2576 | ||
2577 | // exclude the icon part, neither the selection background nor focus rect | |
2578 | // should cover it | |
2579 | rc.left = rcIcon.right; | |
2580 | ||
2581 | return rc; | |
2582 | } | |
2583 | ||
2584 | static | |
2585 | bool HandleSubItemPrepaint(LPNMLVCUSTOMDRAW pLVCD, HFONT hfont, int colCount) | |
2586 | { | |
2587 | NMCUSTOMDRAW& nmcd = pLVCD->nmcd; | |
2588 | ||
2589 | HDC hdc = nmcd.hdc; | |
2590 | HWND hwndList = nmcd.hdr.hwndFrom; | |
2591 | const int col = pLVCD->iSubItem; | |
2592 | const DWORD item = nmcd.dwItemSpec; | |
2593 | ||
2594 | // the font must be valid, otherwise we wouldn't be painting the item at all | |
2595 | SelectInHDC selFont(hdc, hfont); | |
2596 | ||
2597 | // get the rectangle to paint | |
2598 | int subitem = colCount ? col + 1 : col; | |
2599 | RECT rc; | |
2600 | wxGetListCtrlSubItemRect(hwndList, item, subitem, LVIR_BOUNDS, rc); | |
2601 | rc.left += 6; | |
2602 | ||
2603 | // get the image and text to draw | |
2604 | wxChar text[512]; | |
2605 | LV_ITEM it; | |
2606 | wxZeroMemory(it); | |
2607 | it.mask = LVIF_TEXT | LVIF_IMAGE; | |
2608 | it.iItem = item; | |
2609 | it.iSubItem = col; | |
2610 | it.pszText = text; | |
2611 | it.cchTextMax = WXSIZEOF(text); | |
2612 | ListView_GetItem(hwndList, &it); | |
2613 | ||
2614 | HIMAGELIST himl = ListView_GetImageList(hwndList, LVSIL_SMALL); | |
2615 | if ( himl && ImageList_GetImageCount(himl) ) | |
2616 | { | |
2617 | if ( it.iImage != -1 ) | |
2618 | { | |
2619 | ImageList_Draw(himl, it.iImage, hdc, rc.left, rc.top, | |
2620 | nmcd.uItemState & CDIS_SELECTED ? ILD_SELECTED | |
2621 | : ILD_TRANSPARENT); | |
2622 | } | |
2623 | ||
2624 | // notice that even if this item doesn't have any image, the list | |
2625 | // control still leaves space for the image in the first column if the | |
2626 | // image list is not empty (presumably so that items with and without | |
2627 | // images align?) | |
2628 | if ( it.iImage != -1 || it.iSubItem == 0 ) | |
2629 | { | |
2630 | int wImage, hImage; | |
2631 | ImageList_GetIconSize(himl, &wImage, &hImage); | |
2632 | ||
2633 | rc.left += wImage + 2; | |
2634 | } | |
2635 | } | |
2636 | ||
2637 | ::SetBkMode(hdc, TRANSPARENT); | |
2638 | ||
2639 | UINT fmt = DT_SINGLELINE | | |
2640 | #ifndef __WXWINCE__ | |
2641 | DT_WORD_ELLIPSIS | | |
2642 | #endif // __WXWINCE__ | |
2643 | DT_NOPREFIX | | |
2644 | DT_VCENTER; | |
2645 | ||
2646 | LV_COLUMN lvCol; | |
2647 | wxZeroMemory(lvCol); | |
2648 | lvCol.mask = LVCF_FMT; | |
2649 | if ( ListView_GetColumn(hwndList, col, &lvCol) ) | |
2650 | { | |
2651 | switch ( lvCol.fmt & LVCFMT_JUSTIFYMASK ) | |
2652 | { | |
2653 | case LVCFMT_LEFT: | |
2654 | fmt |= DT_LEFT; | |
2655 | break; | |
2656 | ||
2657 | case LVCFMT_CENTER: | |
2658 | fmt |= DT_CENTER; | |
2659 | break; | |
2660 | ||
2661 | case LVCFMT_RIGHT: | |
2662 | fmt |= DT_RIGHT; | |
2663 | break; | |
2664 | } | |
2665 | } | |
2666 | //else: failed to get alignment, assume it's DT_LEFT (default) | |
2667 | ||
2668 | DrawText(hdc, text, -1, &rc, fmt); | |
2669 | ||
2670 | return true; | |
2671 | } | |
2672 | ||
2673 | static void HandleItemPostpaint(NMCUSTOMDRAW nmcd) | |
2674 | { | |
2675 | if ( nmcd.uItemState & CDIS_FOCUS ) | |
2676 | { | |
2677 | RECT rc = GetCustomDrawnItemRect(nmcd); | |
2678 | ||
2679 | // don't use the provided HDC, it's in some strange state by now | |
2680 | ::DrawFocusRect(WindowHDC(nmcd.hdr.hwndFrom), &rc); | |
2681 | } | |
2682 | } | |
2683 | ||
2684 | // pLVCD->clrText and clrTextBk should contain the colours to use | |
2685 | static void HandleItemPaint(LPNMLVCUSTOMDRAW pLVCD, HFONT hfont) | |
2686 | { | |
2687 | NMCUSTOMDRAW& nmcd = pLVCD->nmcd; // just a shortcut | |
2688 | ||
2689 | const HWND hwndList = nmcd.hdr.hwndFrom; | |
2690 | const int item = nmcd.dwItemSpec; | |
2691 | ||
2692 | // unfortunately we can't trust CDIS_SELECTED, it is often set even when | |
2693 | // the item is not at all selected for some reason (comctl32 6), but we | |
2694 | // also can't always trust ListView_GetItem() as it could return the old | |
2695 | // item status if we're called just after the (de)selection, so remember | |
2696 | // the last item to gain selection and also check for it here | |
2697 | for ( int i = -1;; ) | |
2698 | { | |
2699 | i = ListView_GetNextItem(hwndList, i, LVNI_SELECTED); | |
2700 | if ( i == -1 ) | |
2701 | { | |
2702 | nmcd.uItemState &= ~CDIS_SELECTED; | |
2703 | break; | |
2704 | } | |
2705 | ||
2706 | if ( i == item ) | |
2707 | { | |
2708 | nmcd.uItemState |= CDIS_SELECTED; | |
2709 | break; | |
2710 | } | |
2711 | } | |
2712 | ||
2713 | // same thing for CDIS_FOCUS (except simpler as there is only one of them) | |
2714 | // | |
2715 | // NB: cast is needed to work around the bug in mingw32 headers which don't | |
2716 | // have it inside ListView_GetNextItem() itself (unlike SDK ones) | |
2717 | if ( ::GetFocus() == hwndList && | |
2718 | ListView_GetNextItem( | |
2719 | hwndList, static_cast<WPARAM>(-1), LVNI_FOCUSED) == item ) | |
2720 | { | |
2721 | nmcd.uItemState |= CDIS_FOCUS; | |
2722 | } | |
2723 | else | |
2724 | { | |
2725 | nmcd.uItemState &= ~CDIS_FOCUS; | |
2726 | } | |
2727 | ||
2728 | if ( nmcd.uItemState & CDIS_SELECTED ) | |
2729 | { | |
2730 | int syscolFg, syscolBg; | |
2731 | if ( ::GetFocus() == hwndList ) | |
2732 | { | |
2733 | syscolFg = COLOR_HIGHLIGHTTEXT; | |
2734 | syscolBg = COLOR_HIGHLIGHT; | |
2735 | } | |
2736 | else // selected but unfocused | |
2737 | { | |
2738 | syscolFg = COLOR_WINDOWTEXT; | |
2739 | syscolBg = COLOR_BTNFACE; | |
2740 | ||
2741 | // don't grey out the icon in this case neither | |
2742 | nmcd.uItemState &= ~CDIS_SELECTED; | |
2743 | } | |
2744 | ||
2745 | pLVCD->clrText = ::GetSysColor(syscolFg); | |
2746 | pLVCD->clrTextBk = ::GetSysColor(syscolBg); | |
2747 | } | |
2748 | //else: not selected, use normal colours from pLVCD | |
2749 | ||
2750 | HDC hdc = nmcd.hdc; | |
2751 | RECT rc = GetCustomDrawnItemRect(nmcd); | |
2752 | ||
2753 | ::SetTextColor(hdc, pLVCD->clrText); | |
2754 | ::FillRect(hdc, &rc, AutoHBRUSH(pLVCD->clrTextBk)); | |
2755 | ||
2756 | // we could use CDRF_NOTIFYSUBITEMDRAW here but it results in weird repaint | |
2757 | // problems so just draw everything except the focus rect from here instead | |
2758 | const int colCount = Header_GetItemCount(ListView_GetHeader(hwndList)); | |
2759 | for ( int col = 0; col < colCount; col++ ) | |
2760 | { | |
2761 | pLVCD->iSubItem = col; | |
2762 | HandleSubItemPrepaint(pLVCD, hfont, colCount); | |
2763 | } | |
2764 | ||
2765 | HandleItemPostpaint(nmcd); | |
2766 | } | |
2767 | ||
2768 | static WXLPARAM HandleItemPrepaint(wxListCtrl *listctrl, | |
2769 | LPNMLVCUSTOMDRAW pLVCD, | |
2770 | wxListItemAttr *attr) | |
2771 | { | |
2772 | if ( !attr ) | |
2773 | { | |
2774 | // nothing to do for this item | |
2775 | return CDRF_DODEFAULT; | |
2776 | } | |
2777 | ||
2778 | ||
2779 | // set the colours to use for text drawing | |
2780 | pLVCD->clrText = attr->HasTextColour() | |
2781 | ? wxColourToRGB(attr->GetTextColour()) | |
2782 | : wxColourToRGB(listctrl->GetTextColour()); | |
2783 | pLVCD->clrTextBk = attr->HasBackgroundColour() | |
2784 | ? wxColourToRGB(attr->GetBackgroundColour()) | |
2785 | : wxColourToRGB(listctrl->GetBackgroundColour()); | |
2786 | ||
2787 | // select the font if non default one is specified | |
2788 | if ( attr->HasFont() ) | |
2789 | { | |
2790 | wxFont font = attr->GetFont(); | |
2791 | if ( font.GetEncoding() != wxFONTENCODING_SYSTEM ) | |
2792 | { | |
2793 | // the standard control ignores the font encoding/charset, at least | |
2794 | // with recent comctl32.dll versions (5 and 6, it uses to work with | |
2795 | // 4.something) so we have to draw the item entirely ourselves in | |
2796 | // this case | |
2797 | HandleItemPaint(pLVCD, GetHfontOf(font)); | |
2798 | return CDRF_SKIPDEFAULT; | |
2799 | } | |
2800 | ||
2801 | ::SelectObject(pLVCD->nmcd.hdc, GetHfontOf(font)); | |
2802 | ||
2803 | return CDRF_NEWFONT; | |
2804 | } | |
2805 | ||
2806 | return CDRF_DODEFAULT; | |
2807 | } | |
2808 | ||
2809 | WXLPARAM wxListCtrl::OnCustomDraw(WXLPARAM lParam) | |
2810 | { | |
2811 | LPNMLVCUSTOMDRAW pLVCD = (LPNMLVCUSTOMDRAW)lParam; | |
2812 | NMCUSTOMDRAW& nmcd = pLVCD->nmcd; | |
2813 | switch ( nmcd.dwDrawStage ) | |
2814 | { | |
2815 | case CDDS_PREPAINT: | |
2816 | // if we've got any items with non standard attributes, | |
2817 | // notify us before painting each item | |
2818 | // | |
2819 | // for virtual controls, always suppose that we have attributes as | |
2820 | // there is no way to check for this | |
2821 | if ( IsVirtual() || m_hasAnyAttr ) | |
2822 | return CDRF_NOTIFYITEMDRAW; | |
2823 | break; | |
2824 | ||
2825 | case CDDS_ITEMPREPAINT: | |
2826 | // get a message for each subitem | |
2827 | return CDRF_NOTIFYITEMDRAW; | |
2828 | ||
2829 | case CDDS_SUBITEM | CDDS_ITEMPREPAINT: | |
2830 | const int item = nmcd.dwItemSpec; | |
2831 | const int column = pLVCD->iSubItem; | |
2832 | ||
2833 | // we get this message with item == 0 for an empty control, we | |
2834 | // must ignore it as calling OnGetItemAttr() would be wrong | |
2835 | if ( item < 0 || item >= GetItemCount() ) | |
2836 | break; | |
2837 | // same for columns | |
2838 | if ( column < 0 || column >= GetColumnCount() ) | |
2839 | break; | |
2840 | ||
2841 | return HandleItemPrepaint(this, pLVCD, DoGetItemColumnAttr(item, column)); | |
2842 | } | |
2843 | ||
2844 | return CDRF_DODEFAULT; | |
2845 | } | |
2846 | ||
2847 | #endif // NM_CUSTOMDRAW supported | |
2848 | ||
2849 | // Necessary for drawing hrules and vrules, if specified | |
2850 | void wxListCtrl::OnPaint(wxPaintEvent& event) | |
2851 | { | |
2852 | const int itemCount = GetItemCount(); | |
2853 | const bool drawHRules = HasFlag(wxLC_HRULES); | |
2854 | const bool drawVRules = HasFlag(wxLC_VRULES); | |
2855 | ||
2856 | if (!InReportView() || !(drawHRules || drawVRules) || !itemCount) | |
2857 | { | |
2858 | event.Skip(); | |
2859 | return; | |
2860 | } | |
2861 | ||
2862 | wxPaintDC dc(this); | |
2863 | ||
2864 | wxControl::OnPaint(event); | |
2865 | ||
2866 | // Reset the device origin since it may have been set | |
2867 | dc.SetDeviceOrigin(0, 0); | |
2868 | ||
2869 | wxPen pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT)); | |
2870 | dc.SetPen(pen); | |
2871 | dc.SetBrush(* wxTRANSPARENT_BRUSH); | |
2872 | ||
2873 | wxSize clientSize = GetClientSize(); | |
2874 | wxRect itemRect; | |
2875 | ||
2876 | if (drawHRules) | |
2877 | { | |
2878 | const long top = GetTopItem(); | |
2879 | for ( int i = top; i < top + GetCountPerPage() + 1; i++ ) | |
2880 | { | |
2881 | if (GetItemRect(i, itemRect)) | |
2882 | { | |
2883 | int cy = itemRect.GetTop(); | |
2884 | if (i != 0) // Don't draw the first one | |
2885 | { | |
2886 | dc.DrawLine(0, cy, clientSize.x, cy); | |
2887 | } | |
2888 | // Draw last line | |
2889 | if (i == itemCount - 1) | |
2890 | { | |
2891 | cy = itemRect.GetBottom(); | |
2892 | dc.DrawLine(0, cy, clientSize.x, cy); | |
2893 | break; | |
2894 | } | |
2895 | } | |
2896 | } | |
2897 | } | |
2898 | ||
2899 | if (drawVRules) | |
2900 | { | |
2901 | wxRect firstItemRect; | |
2902 | GetItemRect(0, firstItemRect); | |
2903 | ||
2904 | if (GetItemRect(itemCount - 1, itemRect)) | |
2905 | { | |
2906 | // this is a fix for bug 673394: erase the pixels which we would | |
2907 | // otherwise leave on the screen | |
2908 | static const int gap = 2; | |
2909 | dc.SetPen(*wxTRANSPARENT_PEN); | |
2910 | dc.SetBrush(wxBrush(GetBackgroundColour())); | |
2911 | dc.DrawRectangle(0, firstItemRect.GetY() - gap, | |
2912 | clientSize.GetWidth(), gap); | |
2913 | ||
2914 | dc.SetPen(pen); | |
2915 | dc.SetBrush(*wxTRANSPARENT_BRUSH); | |
2916 | ||
2917 | const int numCols = GetColumnCount(); | |
2918 | wxVector<int> indexArray(numCols); | |
2919 | if ( !ListView_GetColumnOrderArray(GetHwnd(), | |
2920 | numCols, | |
2921 | &indexArray[0]) ) | |
2922 | { | |
2923 | wxFAIL_MSG( wxT("invalid column index array in OnPaint()") ); | |
2924 | return; | |
2925 | } | |
2926 | ||
2927 | int x = itemRect.GetX(); | |
2928 | for (int col = 0; col < numCols; col++) | |
2929 | { | |
2930 | int colWidth = GetColumnWidth(indexArray[col]); | |
2931 | x += colWidth ; | |
2932 | dc.DrawLine(x-1, firstItemRect.GetY() - gap, | |
2933 | x-1, itemRect.GetBottom()); | |
2934 | } | |
2935 | } | |
2936 | } | |
2937 | } | |
2938 | ||
2939 | WXLRESULT | |
2940 | wxListCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
2941 | { | |
2942 | switch ( nMsg ) | |
2943 | { | |
2944 | #ifdef WM_PRINT | |
2945 | case WM_PRINT: | |
2946 | // we should bypass our own WM_PRINT handling as we don't handle | |
2947 | // PRF_CHILDREN flag, so leave it to the native control itself | |
2948 | return MSWDefWindowProc(nMsg, wParam, lParam); | |
2949 | #endif // WM_PRINT | |
2950 | ||
2951 | case WM_CONTEXTMENU: | |
2952 | // because this message is propagated upwards the child-parent | |
2953 | // chain, we get it for the right clicks on the header window but | |
2954 | // this is confusing in wx as right clicking there already | |
2955 | // generates a separate wxEVT_COMMAND_LIST_COL_RIGHT_CLICK event | |
2956 | // so just ignore them | |
2957 | if ( (HWND)wParam == ListView_GetHeader(GetHwnd()) ) | |
2958 | return 0; | |
2959 | //else: break | |
2960 | } | |
2961 | ||
2962 | return wxControl::MSWWindowProc(nMsg, wParam, lParam); | |
2963 | } | |
2964 | ||
2965 | // ---------------------------------------------------------------------------- | |
2966 | // virtual list controls | |
2967 | // ---------------------------------------------------------------------------- | |
2968 | ||
2969 | wxString wxListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const | |
2970 | { | |
2971 | // this is a pure virtual function, in fact - which is not really pure | |
2972 | // because the controls which are not virtual don't need to implement it | |
2973 | wxFAIL_MSG( wxT("wxListCtrl::OnGetItemText not supposed to be called") ); | |
2974 | ||
2975 | return wxEmptyString; | |
2976 | } | |
2977 | ||
2978 | int wxListCtrl::OnGetItemImage(long WXUNUSED(item)) const | |
2979 | { | |
2980 | wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL), | |
2981 | -1, | |
2982 | wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden.")); | |
2983 | return -1; | |
2984 | } | |
2985 | ||
2986 | int wxListCtrl::OnGetItemColumnImage(long item, long column) const | |
2987 | { | |
2988 | if (!column) | |
2989 | return OnGetItemImage(item); | |
2990 | ||
2991 | return -1; | |
2992 | } | |
2993 | ||
2994 | wxListItemAttr *wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const | |
2995 | { | |
2996 | wxASSERT_MSG( item >= 0 && item < GetItemCount(), | |
2997 | wxT("invalid item index in OnGetItemAttr()") ); | |
2998 | ||
2999 | // no attributes by default | |
3000 | return NULL; | |
3001 | } | |
3002 | ||
3003 | wxListItemAttr *wxListCtrl::DoGetItemColumnAttr(long item, long column) const | |
3004 | { | |
3005 | if ( IsVirtual() ) | |
3006 | return OnGetItemColumnAttr(item, column); | |
3007 | ||
3008 | wxMSWListItemData * const data = MSWGetItemData(item); | |
3009 | return data ? data->attr : NULL; | |
3010 | } | |
3011 | ||
3012 | void wxListCtrl::SetItemCount(long count) | |
3013 | { | |
3014 | wxASSERT_MSG( IsVirtual(), wxT("this is for virtual controls only") ); | |
3015 | ||
3016 | if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT, (WPARAM)count, | |
3017 | LVSICF_NOSCROLL | LVSICF_NOINVALIDATEALL) ) | |
3018 | { | |
3019 | wxLogLastError(wxT("ListView_SetItemCount")); | |
3020 | } | |
3021 | m_count = count; | |
3022 | wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()), | |
3023 | wxT("m_count should match ListView_GetItemCount")); | |
3024 | } | |
3025 | ||
3026 | void wxListCtrl::RefreshItem(long item) | |
3027 | { | |
3028 | RefreshItems(item, item); | |
3029 | } | |
3030 | ||
3031 | void wxListCtrl::RefreshItems(long itemFrom, long itemTo) | |
3032 | { | |
3033 | ListView_RedrawItems(GetHwnd(), itemFrom, itemTo); | |
3034 | } | |
3035 | ||
3036 | // ---------------------------------------------------------------------------- | |
3037 | // wxWin <-> MSW items conversions | |
3038 | // ---------------------------------------------------------------------------- | |
3039 | ||
3040 | static void wxConvertFromMSWListItem(HWND hwndListCtrl, | |
3041 | wxListItem& info, | |
3042 | LV_ITEM& lvItem) | |
3043 | { | |
3044 | wxMSWListItemData *internaldata = | |
3045 | (wxMSWListItemData *) lvItem.lParam; | |
3046 | ||
3047 | if (internaldata) | |
3048 | info.m_data = internaldata->lParam; | |
3049 | ||
3050 | info.m_mask = 0; | |
3051 | info.m_state = 0; | |
3052 | info.m_stateMask = 0; | |
3053 | info.m_itemId = lvItem.iItem; | |
3054 | ||
3055 | long oldMask = lvItem.mask; | |
3056 | ||
3057 | bool needText = false; | |
3058 | if (hwndListCtrl != 0) | |
3059 | { | |
3060 | if ( lvItem.mask & LVIF_TEXT ) | |
3061 | needText = false; | |
3062 | else | |
3063 | needText = true; | |
3064 | ||
3065 | if ( needText ) | |
3066 | { | |
3067 | lvItem.pszText = new wxChar[513]; | |
3068 | lvItem.cchTextMax = 512; | |
3069 | } | |
3070 | lvItem.mask |= LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM; | |
3071 | ::SendMessage(hwndListCtrl, LVM_GETITEM, 0, (LPARAM)& lvItem); | |
3072 | } | |
3073 | ||
3074 | if ( lvItem.mask & LVIF_STATE ) | |
3075 | { | |
3076 | info.m_mask |= wxLIST_MASK_STATE; | |
3077 | ||
3078 | if ( lvItem.stateMask & LVIS_CUT) | |
3079 | { | |
3080 | info.m_stateMask |= wxLIST_STATE_CUT; | |
3081 | if ( lvItem.state & LVIS_CUT ) | |
3082 | info.m_state |= wxLIST_STATE_CUT; | |
3083 | } | |
3084 | if ( lvItem.stateMask & LVIS_DROPHILITED) | |
3085 | { | |
3086 | info.m_stateMask |= wxLIST_STATE_DROPHILITED; | |
3087 | if ( lvItem.state & LVIS_DROPHILITED ) | |
3088 | info.m_state |= wxLIST_STATE_DROPHILITED; | |
3089 | } | |
3090 | if ( lvItem.stateMask & LVIS_FOCUSED) | |
3091 | { | |
3092 | info.m_stateMask |= wxLIST_STATE_FOCUSED; | |
3093 | if ( lvItem.state & LVIS_FOCUSED ) | |
3094 | info.m_state |= wxLIST_STATE_FOCUSED; | |
3095 | } | |
3096 | if ( lvItem.stateMask & LVIS_SELECTED) | |
3097 | { | |
3098 | info.m_stateMask |= wxLIST_STATE_SELECTED; | |
3099 | if ( lvItem.state & LVIS_SELECTED ) | |
3100 | info.m_state |= wxLIST_STATE_SELECTED; | |
3101 | } | |
3102 | } | |
3103 | ||
3104 | if ( lvItem.mask & LVIF_TEXT ) | |
3105 | { | |
3106 | info.m_mask |= wxLIST_MASK_TEXT; | |
3107 | info.m_text = lvItem.pszText; | |
3108 | } | |
3109 | if ( lvItem.mask & LVIF_IMAGE ) | |
3110 | { | |
3111 | info.m_mask |= wxLIST_MASK_IMAGE; | |
3112 | info.m_image = lvItem.iImage; | |
3113 | } | |
3114 | if ( lvItem.mask & LVIF_PARAM ) | |
3115 | info.m_mask |= wxLIST_MASK_DATA; | |
3116 | if ( lvItem.mask & LVIF_DI_SETITEM ) | |
3117 | info.m_mask |= wxLIST_SET_ITEM; | |
3118 | info.m_col = lvItem.iSubItem; | |
3119 | ||
3120 | if (needText) | |
3121 | { | |
3122 | if (lvItem.pszText) | |
3123 | delete[] lvItem.pszText; | |
3124 | } | |
3125 | lvItem.mask = oldMask; | |
3126 | } | |
3127 | ||
3128 | static void wxConvertToMSWFlags(long state, long stateMask, LV_ITEM& lvItem) | |
3129 | { | |
3130 | if (stateMask & wxLIST_STATE_CUT) | |
3131 | { | |
3132 | lvItem.stateMask |= LVIS_CUT; | |
3133 | if (state & wxLIST_STATE_CUT) | |
3134 | lvItem.state |= LVIS_CUT; | |
3135 | } | |
3136 | if (stateMask & wxLIST_STATE_DROPHILITED) | |
3137 | { | |
3138 | lvItem.stateMask |= LVIS_DROPHILITED; | |
3139 | if (state & wxLIST_STATE_DROPHILITED) | |
3140 | lvItem.state |= LVIS_DROPHILITED; | |
3141 | } | |
3142 | if (stateMask & wxLIST_STATE_FOCUSED) | |
3143 | { | |
3144 | lvItem.stateMask |= LVIS_FOCUSED; | |
3145 | if (state & wxLIST_STATE_FOCUSED) | |
3146 | lvItem.state |= LVIS_FOCUSED; | |
3147 | } | |
3148 | if (stateMask & wxLIST_STATE_SELECTED) | |
3149 | { | |
3150 | lvItem.stateMask |= LVIS_SELECTED; | |
3151 | if (state & wxLIST_STATE_SELECTED) | |
3152 | lvItem.state |= LVIS_SELECTED; | |
3153 | } | |
3154 | } | |
3155 | ||
3156 | static void wxConvertToMSWListItem(const wxListCtrl *ctrl, | |
3157 | const wxListItem& info, | |
3158 | LV_ITEM& lvItem) | |
3159 | { | |
3160 | if ( ctrl->InReportView() ) | |
3161 | { | |
3162 | wxASSERT_MSG( 0 <= info.m_col && info.m_col < ctrl->GetColumnCount(), | |
3163 | "wxListCtrl column index out of bounds" ); | |
3164 | } | |
3165 | else // not in report view | |
3166 | { | |
3167 | wxASSERT_MSG( info.m_col == 0, "columns only exist in report view" ); | |
3168 | } | |
3169 | ||
3170 | lvItem.iItem = (int) info.m_itemId; | |
3171 | ||
3172 | lvItem.iImage = info.m_image; | |
3173 | lvItem.stateMask = 0; | |
3174 | lvItem.state = 0; | |
3175 | lvItem.mask = 0; | |
3176 | lvItem.iSubItem = info.m_col; | |
3177 | ||
3178 | if (info.m_mask & wxLIST_MASK_STATE) | |
3179 | { | |
3180 | lvItem.mask |= LVIF_STATE; | |
3181 | ||
3182 | wxConvertToMSWFlags(info.m_state, info.m_stateMask, lvItem); | |
3183 | } | |
3184 | ||
3185 | if (info.m_mask & wxLIST_MASK_TEXT) | |
3186 | { | |
3187 | lvItem.mask |= LVIF_TEXT; | |
3188 | if ( ctrl->HasFlag(wxLC_USER_TEXT) ) | |
3189 | { | |
3190 | lvItem.pszText = LPSTR_TEXTCALLBACK; | |
3191 | } | |
3192 | else | |
3193 | { | |
3194 | // pszText is not const, hence the cast | |
3195 | lvItem.pszText = (wxChar *)info.m_text.wx_str(); | |
3196 | if ( lvItem.pszText ) | |
3197 | lvItem.cchTextMax = info.m_text.length(); | |
3198 | else | |
3199 | lvItem.cchTextMax = 0; | |
3200 | } | |
3201 | } | |
3202 | if (info.m_mask & wxLIST_MASK_IMAGE) | |
3203 | lvItem.mask |= LVIF_IMAGE; | |
3204 | } | |
3205 | ||
3206 | static void wxConvertToMSWListCol(HWND hwndList, | |
3207 | int col, | |
3208 | const wxListItem& item, | |
3209 | LV_COLUMN& lvCol) | |
3210 | { | |
3211 | wxZeroMemory(lvCol); | |
3212 | ||
3213 | if ( item.m_mask & wxLIST_MASK_TEXT ) | |
3214 | { | |
3215 | lvCol.mask |= LVCF_TEXT; | |
3216 | lvCol.pszText = (wxChar *)item.m_text.wx_str(); // cast is safe | |
3217 | } | |
3218 | ||
3219 | if ( item.m_mask & wxLIST_MASK_FORMAT ) | |
3220 | { | |
3221 | lvCol.mask |= LVCF_FMT; | |
3222 | ||
3223 | if ( item.m_format == wxLIST_FORMAT_LEFT ) | |
3224 | lvCol.fmt = LVCFMT_LEFT; | |
3225 | else if ( item.m_format == wxLIST_FORMAT_RIGHT ) | |
3226 | lvCol.fmt = LVCFMT_RIGHT; | |
3227 | else if ( item.m_format == wxLIST_FORMAT_CENTRE ) | |
3228 | lvCol.fmt = LVCFMT_CENTER; | |
3229 | } | |
3230 | ||
3231 | if ( item.m_mask & wxLIST_MASK_WIDTH ) | |
3232 | { | |
3233 | lvCol.mask |= LVCF_WIDTH; | |
3234 | if ( item.m_width == wxLIST_AUTOSIZE) | |
3235 | lvCol.cx = LVSCW_AUTOSIZE; | |
3236 | else if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER) | |
3237 | lvCol.cx = LVSCW_AUTOSIZE_USEHEADER; | |
3238 | else | |
3239 | lvCol.cx = item.m_width; | |
3240 | } | |
3241 | ||
3242 | // see comment at the end of wxListCtrl::GetColumn() | |
3243 | #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300 | |
3244 | if ( item.m_mask & wxLIST_MASK_IMAGE ) | |
3245 | { | |
3246 | if ( wxApp::GetComCtl32Version() >= 470 ) | |
3247 | { | |
3248 | lvCol.mask |= LVCF_IMAGE; | |
3249 | ||
3250 | // we use LVCFMT_BITMAP_ON_RIGHT because the images on the right | |
3251 | // seem to be generally nicer than on the left and the generic | |
3252 | // version only draws them on the right (we don't have a flag to | |
3253 | // specify the image location anyhow) | |
3254 | // | |
3255 | // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to | |
3256 | // make any difference in my tests -- but maybe we should? | |
3257 | if ( item.m_image != -1 ) | |
3258 | { | |
3259 | // as we're going to overwrite the format field, get its | |
3260 | // current value first -- unless we want to overwrite it anyhow | |
3261 | if ( !(lvCol.mask & LVCF_FMT) ) | |
3262 | { | |
3263 | LV_COLUMN lvColOld; | |
3264 | wxZeroMemory(lvColOld); | |
3265 | lvColOld.mask = LVCF_FMT; | |
3266 | if ( ListView_GetColumn(hwndList, col, &lvColOld) ) | |
3267 | { | |
3268 | lvCol.fmt = lvColOld.fmt; | |
3269 | } | |
3270 | ||
3271 | lvCol.mask |= LVCF_FMT; | |
3272 | } | |
3273 | ||
3274 | lvCol.fmt |= LVCFMT_BITMAP_ON_RIGHT | LVCFMT_IMAGE; | |
3275 | } | |
3276 | ||
3277 | lvCol.iImage = item.m_image; | |
3278 | } | |
3279 | //else: it doesn't support item images anyhow | |
3280 | } | |
3281 | #endif // _WIN32_IE >= 0x0300 | |
3282 | } | |
3283 | ||
3284 | #endif // wxUSE_LISTCTRL |