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