]>
Commit | Line | Data |
---|---|---|
0e320a79 DW |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: listbox.cpp | |
3 | // Purpose: wxListBox | |
fb9010ed | 4 | // Author: David Webster |
0e320a79 | 5 | // Modified by: |
fb9010ed | 6 | // Created: 10/09/99 |
0e320a79 | 7 | // RCS-ID: $Id$ |
fb9010ed | 8 | // Copyright: (c) David Webster |
0e320a79 DW |
9 | // Licence: wxWindows licence |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
fb9010ed DW |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #include "wx/window.h" | |
16 | #include "wx/os2/private.h" | |
0e320a79 | 17 | |
fb9010ed | 18 | #ifndef WX_PRECOMP |
0e320a79 DW |
19 | #include "wx/listbox.h" |
20 | #include "wx/settings.h" | |
fb9010ed DW |
21 | #include "wx/brush.h" |
22 | #include "wx/font.h" | |
23 | #include "wx/dc.h" | |
24 | #include "wx/utils.h" | |
25 | #endif | |
26 | ||
27 | #define INCL_M | |
28 | #include <os2.h> | |
29 | ||
0e320a79 DW |
30 | #include "wx/dynarray.h" |
31 | #include "wx/log.h" | |
32 | ||
fb9010ed DW |
33 | #if wxUSE_OWNER_DRAWN |
34 | #include "wx/ownerdrw.h" | |
35 | #endif | |
36 | ||
0e320a79 DW |
37 | #if !USE_SHARED_LIBRARY |
38 | IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl) | |
39 | #endif | |
40 | ||
fb9010ed DW |
41 | // ============================================================================ |
42 | // list box item declaration and implementation | |
43 | // ============================================================================ | |
44 | ||
45 | #if wxUSE_OWNER_DRAWN | |
46 | ||
47 | class wxListBoxItem : public wxOwnerDrawn | |
48 | { | |
49 | public: | |
50 | wxListBoxItem(const wxString& str = ""); | |
51 | }; | |
52 | ||
53 | wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE) | |
54 | { | |
55 | // no bitmaps/checkmarks | |
56 | SetMarginWidth(0); | |
57 | } | |
58 | ||
59 | wxOwnerDrawn *wxListBox::CreateItem(size_t n) | |
60 | { | |
61 | return new wxListBoxItem(); | |
62 | } | |
63 | ||
64 | #endif //USE_OWNER_DRAWN | |
65 | ||
0e320a79 DW |
66 | // ============================================================================ |
67 | // list box control implementation | |
68 | // ============================================================================ | |
69 | ||
70 | // Listbox item | |
71 | wxListBox::wxListBox() | |
72 | { | |
dcd307ee DW |
73 | m_noItems = 0; |
74 | m_selected = 0; | |
0e320a79 DW |
75 | } |
76 | ||
dcd307ee DW |
77 | bool wxListBox::Create(wxWindow *parent, |
78 | wxWindowID id, | |
0e320a79 DW |
79 | const wxPoint& pos, |
80 | const wxSize& size, | |
81 | int n, const wxString choices[], | |
82 | long style, | |
5d4b632b DW |
83 | #if wxUSE_VALIDATORS |
84 | # if defined(__VISAGECPP__) | |
85 | const wxValidator* validator, | |
86 | # else | |
0e320a79 | 87 | const wxValidator& validator, |
5d4b632b DW |
88 | # endif |
89 | #endif | |
0e320a79 DW |
90 | const wxString& name) |
91 | { | |
dcd307ee DW |
92 | m_noItems = 0; |
93 | m_hWnd = 0; | |
94 | m_selected = 0; | |
0e320a79 | 95 | |
dcd307ee | 96 | SetName(name); |
5d4b632b | 97 | #if wxUSE_VALIDATORS |
dcd307ee | 98 | SetValidator(validator); |
5d4b632b | 99 | #endif |
0e320a79 | 100 | |
dcd307ee DW |
101 | if (parent) |
102 | parent->AddChild(this); | |
0e320a79 | 103 | |
dcd307ee DW |
104 | wxSystemSettings settings; |
105 | SetBackgroundColour(settings.GetSystemColour(wxSYS_COLOUR_WINDOW)); | |
106 | SetForegroundColour(parent->GetForegroundColour()); | |
0e320a79 | 107 | |
dcd307ee | 108 | m_windowId = ( id == -1 ) ? (int)NewControlId() : id; |
0e320a79 | 109 | |
dcd307ee DW |
110 | int x = pos.x; |
111 | int y = pos.y; | |
112 | int width = size.x; | |
113 | int height = size.y; | |
114 | m_windowStyle = style; | |
fb9010ed | 115 | |
dcd307ee | 116 | // TODO: |
fb9010ed DW |
117 | /* |
118 | DWORD wstyle = WS_VISIBLE | WS_VSCROLL | WS_TABSTOP | | |
119 | LBS_NOTIFY | LBS_HASSTRINGS; | |
120 | if (m_windowStyle & wxLB_MULTIPLE) | |
121 | wstyle |= LBS_MULTIPLESEL; | |
122 | else if (m_windowStyle & wxLB_EXTENDED) | |
123 | wstyle |= LBS_EXTENDEDSEL; | |
124 | ||
125 | if (m_windowStyle & wxLB_ALWAYS_SB) | |
dcd307ee | 126 | wstyle |= LBS_DISABLENOSCROLL; |
fb9010ed DW |
127 | if (m_windowStyle & wxLB_HSCROLL) |
128 | wstyle |= WS_HSCROLL; | |
129 | if (m_windowStyle & wxLB_SORT) | |
130 | wstyle |= LBS_SORT; | |
131 | ||
132 | #if wxUSE_OWNER_DRAWN | |
133 | if ( m_windowStyle & wxLB_OWNERDRAW ) { | |
134 | // we don't support LBS_OWNERDRAWVARIABLE yet | |
135 | wstyle |= LBS_OWNERDRAWFIXED; | |
136 | } | |
137 | #endif | |
138 | ||
139 | // Without this style, you get unexpected heights, so e.g. constraint layout | |
140 | // doesn't work properly | |
141 | wstyle |= LBS_NOINTEGRALHEIGHT; | |
142 | ||
143 | bool want3D; | |
dcd307ee | 144 | WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D); |
fb9010ed | 145 | |
dcd307ee DW |
146 | // Even with extended styles, need to combine with WS_BORDER for them to |
147 | // look right. | |
fb9010ed DW |
148 | if ( want3D || wxStyleHasBorder(m_windowStyle) ) |
149 | { | |
150 | wstyle |= WS_BORDER; | |
151 | } | |
152 | ||
153 | m_hWnd = (WXHWND)::CreateWindowEx(exStyle, wxT("LISTBOX"), NULL, | |
154 | wstyle | WS_CHILD, | |
155 | 0, 0, 0, 0, | |
156 | (HWND)parent->GetHWND(), (HMENU)m_windowId, | |
157 | wxGetInstance(), NULL); | |
158 | ||
dcd307ee | 159 | wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create listbox") ); |
fb9010ed DW |
160 | |
161 | // Subclass again to catch messages | |
162 | SubclassWin(m_hWnd); | |
163 | ||
164 | size_t ui; | |
165 | for (ui = 0; ui < (size_t)n; ui++) { | |
166 | Append(choices[ui]); | |
167 | } | |
168 | ||
169 | if ( (m_windowStyle & wxLB_MULTIPLE) == 0 ) | |
170 | SendMessage(GetHwnd(), LB_SETCURSEL, 0, 0); | |
dcd307ee | 171 | */ |
fb9010ed DW |
172 | SetFont(parent->GetFont()); |
173 | ||
174 | SetSize(x, y, width, height); | |
175 | ||
176 | Show(TRUE); | |
0e320a79 | 177 | |
dcd307ee | 178 | return TRUE; |
0e320a79 DW |
179 | } |
180 | ||
181 | wxListBox::~wxListBox() | |
182 | { | |
fb9010ed DW |
183 | #if wxUSE_OWNER_DRAWN |
184 | size_t uiCount = m_aItems.Count(); | |
185 | while ( uiCount-- != 0 ) { | |
186 | delete m_aItems[uiCount]; | |
187 | } | |
188 | #endif // wxUSE_OWNER_DRAWN | |
189 | } | |
190 | ||
191 | void wxListBox::SetupColours() | |
192 | { | |
193 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW)); | |
194 | SetForegroundColour(GetParent()->GetForegroundColour()); | |
0e320a79 DW |
195 | } |
196 | ||
dcd307ee DW |
197 | // ---------------------------------------------------------------------------- |
198 | // implementation of wxListBoxBase methods | |
199 | // ---------------------------------------------------------------------------- | |
200 | ||
201 | void wxListBox::DoSetFirstItem(int N) | |
0e320a79 | 202 | { |
fb9010ed DW |
203 | wxCHECK_RET( N >= 0 && N < m_noItems, |
204 | wxT("invalid index in wxListBox::SetFirstItem") ); | |
205 | ||
dcd307ee | 206 | // SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0); |
0e320a79 DW |
207 | } |
208 | ||
209 | void wxListBox::Delete(int N) | |
210 | { | |
fb9010ed DW |
211 | wxCHECK_RET( N >= 0 && N < m_noItems, |
212 | wxT("invalid index in wxListBox::Delete") ); | |
213 | ||
dcd307ee DW |
214 | #if wxUSE_OWNER_DRAWN |
215 | delete m_aItems[N]; | |
216 | m_aItems.Remove(N); | |
217 | #else // !wxUSE_OWNER_DRAWN | |
218 | if ( HasClientObjectData() ) | |
219 | { | |
220 | delete GetClientObject(N); | |
221 | } | |
222 | #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN | |
223 | ||
224 | // SendMessage(GetHwnd(), LB_DELETESTRING, N, 0); | |
225 | m_noItems--; | |
226 | ||
fb9010ed | 227 | SetHorizontalExtent(""); |
0e320a79 DW |
228 | } |
229 | ||
dcd307ee | 230 | int wxListBox::DoAppend(const wxString& item) |
0e320a79 | 231 | { |
dcd307ee DW |
232 | // TODO: |
233 | /* | |
234 | int index = ListBox_AddString(GetHwnd(), item); | |
235 | m_noItems++; | |
fb9010ed DW |
236 | |
237 | #if wxUSE_OWNER_DRAWN | |
238 | if ( m_windowStyle & wxLB_OWNERDRAW ) { | |
239 | wxOwnerDrawn *pNewItem = CreateItem(index); // dummy argument | |
240 | pNewItem->SetName(item); | |
241 | m_aItems.Add(pNewItem); | |
dcd307ee | 242 | ListBox_SetItemData(GetHwnd(), index, pNewItem); |
fb9010ed DW |
243 | } |
244 | #endif | |
0e320a79 | 245 | |
fb9010ed | 246 | SetHorizontalExtent(item); |
dcd307ee DW |
247 | |
248 | return index; | |
249 | */ | |
250 | return 0; | |
0e320a79 DW |
251 | } |
252 | ||
dcd307ee | 253 | void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData) |
0e320a79 | 254 | { |
dcd307ee DW |
255 | // TODO: |
256 | /* | |
257 | ShowWindow(GetHwnd(), SW_HIDE); | |
0e320a79 | 258 | |
dcd307ee | 259 | ListBox_ResetContent(GetHwnd()); |
0e320a79 | 260 | |
dcd307ee | 261 | m_noItems = choices.GetCount(); |
fb9010ed | 262 | int i; |
dcd307ee | 263 | for (i = 0; i < m_noItems; i++) |
fb9010ed | 264 | { |
dcd307ee DW |
265 | ListBox_AddString(GetHwnd(), choices[i]); |
266 | if ( clientData ) | |
267 | { | |
268 | #if wxUSE_OWNER_DRAWN | |
269 | wxASSERT_MSG(clientData[ui] == NULL, | |
270 | wxT("Can't use client data with owner-drawn listboxes")); | |
271 | #else // !wxUSE_OWNER_DRAWN | |
272 | ListBox_SetItemData(GetHwnd(), i, clientData[i]); | |
273 | #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN | |
274 | } | |
fb9010ed | 275 | } |
fb9010ed DW |
276 | |
277 | #if wxUSE_OWNER_DRAWN | |
278 | if ( m_windowStyle & wxLB_OWNERDRAW ) { | |
279 | // first delete old items | |
280 | size_t ui = m_aItems.Count(); | |
281 | while ( ui-- != 0 ) { | |
282 | delete m_aItems[ui]; | |
283 | } | |
284 | m_aItems.Empty(); | |
285 | ||
286 | // then create new ones | |
dcd307ee | 287 | for ( ui = 0; ui < (size_t)m_noItems; ui++ ) { |
fb9010ed DW |
288 | wxOwnerDrawn *pNewItem = CreateItem(ui); |
289 | pNewItem->SetName(choices[ui]); | |
290 | m_aItems.Add(pNewItem); | |
dcd307ee | 291 | ListBox_SetItemData(GetHwnd(), ui, pNewItem); |
fb9010ed DW |
292 | } |
293 | } | |
dcd307ee | 294 | #endif // wxUSE_OWNER_DRAWN |
0e320a79 | 295 | |
dcd307ee DW |
296 | SetHorizontalExtent(); |
297 | ||
298 | ShowWindow(GetHwnd(), SW_SHOW); | |
299 | */ | |
0e320a79 DW |
300 | } |
301 | ||
302 | int wxListBox::FindString(const wxString& s) const | |
303 | { | |
dcd307ee DW |
304 | // TODO: |
305 | /* | |
306 | int pos = ListBox_FindStringExact(GetHwnd(), (WPARAM)-1, s); | |
307 | if (pos == LB_ERR) | |
308 | return wxNOT_FOUND; | |
309 | else | |
fb9010ed | 310 | return pos; |
dcd307ee DW |
311 | */ |
312 | return 0; | |
0e320a79 DW |
313 | } |
314 | ||
315 | void wxListBox::Clear() | |
316 | { | |
fb9010ed DW |
317 | #if wxUSE_OWNER_DRAWN |
318 | size_t uiCount = m_aItems.Count(); | |
319 | while ( uiCount-- != 0 ) { | |
320 | delete m_aItems[uiCount]; | |
321 | } | |
322 | ||
323 | m_aItems.Clear(); | |
dcd307ee DW |
324 | #else // !wxUSE_OWNER_DRAWN |
325 | if ( HasClientObjectData() ) | |
326 | { | |
327 | for ( size_t n = 0; n < (size_t)m_noItems; n++ ) | |
328 | { | |
329 | delete GetClientObject(n); | |
330 | } | |
331 | } | |
332 | #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN | |
333 | // TODO: | |
334 | /* | |
335 | ||
336 | ListBox_ResetContent(GetHwnd()); | |
fb9010ed DW |
337 | |
338 | m_noItems = 0; | |
dcd307ee DW |
339 | SetHorizontalExtent(); |
340 | */ | |
0e320a79 DW |
341 | } |
342 | ||
343 | void wxListBox::SetSelection(int N, bool select) | |
344 | { | |
fb9010ed DW |
345 | wxCHECK_RET( N >= 0 && N < m_noItems, |
346 | wxT("invalid index in wxListBox::SetSelection") ); | |
dcd307ee DW |
347 | // TODO: |
348 | /* | |
fb9010ed | 349 | |
dcd307ee | 350 | if ( HasMultipleSelection() ) |
fb9010ed | 351 | { |
dcd307ee | 352 | SendMessage(GetHwnd(), LB_SETSEL, select, N); |
fb9010ed DW |
353 | } |
354 | else | |
355 | { | |
dcd307ee | 356 | SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0); |
fb9010ed | 357 | } |
dcd307ee | 358 | */ |
0e320a79 DW |
359 | } |
360 | ||
dcd307ee | 361 | bool wxListBox::IsSelected(int N) const |
0e320a79 | 362 | { |
fb9010ed DW |
363 | wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE, |
364 | wxT("invalid index in wxListBox::Selected") ); | |
365 | ||
366 | // return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? FALSE : TRUE; | |
0e320a79 DW |
367 | return FALSE; |
368 | } | |
369 | ||
dcd307ee | 370 | wxClientData* wxListBox::DoGetItemClientObject(int n) const |
0e320a79 | 371 | { |
dcd307ee | 372 | return (wxClientData *)DoGetItemClientData(n); |
0e320a79 DW |
373 | } |
374 | ||
dcd307ee | 375 | void *wxListBox::DoGetItemClientData(int n) const |
0e320a79 | 376 | { |
dcd307ee | 377 | wxCHECK_MSG( n >= 0 && n < m_noItems, NULL, |
fb9010ed DW |
378 | wxT("invalid index in wxListBox::GetClientData") ); |
379 | ||
dcd307ee DW |
380 | // return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0); |
381 | return NULL; | |
0e320a79 DW |
382 | } |
383 | ||
dcd307ee | 384 | void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData) |
0e320a79 | 385 | { |
dcd307ee DW |
386 | DoSetItemClientData(n, clientData); |
387 | } | |
388 | ||
389 | void wxListBox::DoSetItemClientData(int n, void *clientData) | |
390 | { | |
391 | wxCHECK_RET( n >= 0 && n < m_noItems, | |
fb9010ed DW |
392 | wxT("invalid index in wxListBox::SetClientData") ); |
393 | ||
dcd307ee DW |
394 | #if wxUSE_OWNER_DRAWN |
395 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
396 | { | |
397 | // client data must be pointer to wxOwnerDrawn, otherwise we would crash | |
398 | // in OnMeasure/OnDraw. | |
399 | wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes")); | |
400 | } | |
401 | #endif // wxUSE_OWNER_DRAWN | |
402 | ||
403 | // TODO: | |
404 | /* | |
405 | if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR ) | |
406 | wxLogDebug(wxT("LB_SETITEMDATA failed")); | |
407 | */ | |
408 | } | |
409 | ||
410 | bool wxListBox::HasMultipleSelection() const | |
411 | { | |
412 | return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED); | |
0e320a79 DW |
413 | } |
414 | ||
415 | // Return number of selections and an array of selected integers | |
416 | int wxListBox::GetSelections(wxArrayInt& aSelections) const | |
417 | { | |
418 | aSelections.Empty(); | |
419 | ||
dcd307ee DW |
420 | // TODO: |
421 | /* | |
422 | if ( HasMultipleSelection() ) | |
0e320a79 | 423 | { |
dcd307ee | 424 | int no_sel = ListBox_GetSelCount(GetHwnd()); |
fb9010ed DW |
425 | if (no_sel != 0) { |
426 | int *selections = new int[no_sel]; | |
dcd307ee DW |
427 | int rc = ListBox_GetSelItems(GetHwnd(), no_sel, selections); |
428 | ||
429 | wxCHECK_MSG(rc != LB_ERR, -1, wxT("ListBox_GetSelItems failed")); | |
fb9010ed DW |
430 | |
431 | aSelections.Alloc(no_sel); | |
432 | for ( int n = 0; n < no_sel; n++ ) | |
433 | aSelections.Add(selections[n]); | |
434 | ||
435 | delete [] selections; | |
436 | } | |
0e320a79 DW |
437 | |
438 | return no_sel; | |
439 | } | |
440 | else // single-selection listbox | |
441 | { | |
dcd307ee | 442 | aSelections.Add(ListBox_GetCurSel(GetHwnd())); |
0e320a79 DW |
443 | |
444 | return 1; | |
445 | } | |
dcd307ee DW |
446 | */ |
447 | return 0; | |
0e320a79 DW |
448 | } |
449 | ||
450 | // Get single selection, for single choice list items | |
451 | int wxListBox::GetSelection() const | |
452 | { | |
dcd307ee | 453 | wxCHECK_MSG( !HasMultipleSelection(), |
fb9010ed DW |
454 | -1, |
455 | wxT("GetSelection() can't be used with multiple-selection " | |
456 | "listboxes, use GetSelections() instead.") ); | |
457 | ||
458 | // return ListBox_GetCurSel(GetHwnd()); | |
dcd307ee | 459 | return 0; |
0e320a79 DW |
460 | } |
461 | ||
462 | // Find string for position | |
463 | wxString wxListBox::GetString(int N) const | |
464 | { | |
fb9010ed DW |
465 | wxCHECK_MSG( N >= 0 && N < m_noItems, "", |
466 | wxT("invalid index in wxListBox::GetClientData") ); | |
467 | ||
dcd307ee DW |
468 | // TODO: |
469 | /* | |
470 | int len = ListBox_GetTextLen(GetHwnd(), N); | |
fb9010ed DW |
471 | |
472 | // +1 for terminating NUL | |
473 | wxString result; | |
dcd307ee | 474 | ListBox_GetText(GetHwnd(), N, result.GetWriteBuf(len + 1)); |
fb9010ed DW |
475 | result.UngetWriteBuf(); |
476 | ||
477 | return result; | |
dcd307ee DW |
478 | */ |
479 | return((wxString)""); | |
0e320a79 DW |
480 | } |
481 | ||
dcd307ee DW |
482 | void |
483 | wxListBox::DoInsertItems(const wxArrayString& items, int pos) | |
484 | { | |
485 | wxCHECK_RET( pos >= 0 && pos <= m_noItems, | |
486 | wxT("invalid index in wxListBox::InsertItems") ); | |
487 | ||
488 | // TODO: | |
489 | /* | |
490 | int nItems = items.GetCount(); | |
491 | for ( int i = 0; i < nItems; i++ ) | |
492 | ListBox_InsertString(GetHwnd(), i + pos, items[i]); | |
493 | m_noItems += nItems; | |
494 | ||
495 | SetHorizontalExtent(); | |
496 | */ | |
497 | } | |
498 | ||
499 | void wxListBox::SetString(int N, const wxString& s) | |
500 | { | |
501 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
502 | wxT("invalid index in wxListBox::SetString") ); | |
503 | ||
504 | // remember the state of the item | |
505 | bool wasSelected = IsSelected(N); | |
506 | ||
507 | void *oldData = NULL; | |
508 | wxClientData *oldObjData = NULL; | |
509 | if ( m_clientDataItemsType == ClientData_Void ) | |
510 | oldData = GetClientData(N); | |
511 | else if ( m_clientDataItemsType == ClientData_Object ) | |
512 | oldObjData = GetClientObject(N); | |
513 | // TODO: | |
514 | /* | |
515 | ||
516 | // delete and recreate it | |
517 | SendMessage(GetHwnd(), LB_DELETESTRING, N, 0); | |
518 | ||
519 | int newN = N; | |
520 | if ( N == m_noItems - 1 ) | |
521 | newN = -1; | |
522 | ||
523 | ListBox_InsertString(GetHwnd(), newN, s); | |
524 | ||
525 | // restore the client data | |
526 | if ( oldData ) | |
527 | SetClientData(N, oldData); | |
528 | else if ( oldObjData ) | |
529 | SetClientObject(N, oldObjData); | |
530 | ||
531 | // we may have lost the selection | |
532 | if ( wasSelected ) | |
533 | Select(N); | |
534 | ||
535 | #if wxUSE_OWNER_DRAWN | |
536 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
537 | // update item's text | |
538 | m_aItems[N]->SetName(s); | |
539 | #endif //USE_OWNER_DRAWN | |
540 | */ | |
541 | } | |
542 | ||
543 | int wxListBox::GetCount() const | |
544 | { | |
545 | return m_noItems; | |
546 | } | |
547 | ||
548 | // ---------------------------------------------------------------------------- | |
549 | // helpers | |
550 | // ---------------------------------------------------------------------------- | |
551 | ||
552 | // Windows-specific code to set the horizontal extent of the listbox, if | |
fb9010ed DW |
553 | // necessary. If s is non-NULL, it's used to calculate the horizontal extent. |
554 | // Otherwise, all strings are used. | |
555 | void wxListBox::SetHorizontalExtent(const wxString& s) | |
0e320a79 | 556 | { |
dcd307ee | 557 | // TODO: |
fb9010ed DW |
558 | /* |
559 | // Only necessary if we want a horizontal scrollbar | |
560 | if (!(m_windowStyle & wxHSCROLL)) | |
561 | return; | |
562 | TEXTMETRIC lpTextMetric; | |
0e320a79 | 563 | |
dcd307ee | 564 | if ( !s.IsEmpty() ) |
fb9010ed DW |
565 | { |
566 | int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L); | |
567 | HDC dc = GetWindowDC(GetHwnd()); | |
568 | HFONT oldFont = 0; | |
569 | if (GetFont().Ok() && GetFont().GetResourceHandle()) | |
570 | oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle()); | |
571 | ||
572 | GetTextMetrics(dc, &lpTextMetric); | |
573 | SIZE extentXY; | |
574 | ::GetTextExtentPoint(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY); | |
575 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); | |
576 | ||
577 | if (oldFont) | |
578 | ::SelectObject(dc, oldFont); | |
579 | ||
580 | ReleaseDC(GetHwnd(), dc); | |
581 | if (extentX > existingExtent) | |
582 | SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L); | |
fb9010ed DW |
583 | } |
584 | else | |
585 | { | |
586 | int largestExtent = 0; | |
587 | HDC dc = GetWindowDC(GetHwnd()); | |
588 | HFONT oldFont = 0; | |
589 | if (GetFont().Ok() && GetFont().GetResourceHandle()) | |
590 | oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle()); | |
591 | ||
592 | GetTextMetrics(dc, &lpTextMetric); | |
593 | int i; | |
594 | for (i = 0; i < m_noItems; i++) | |
595 | { | |
596 | int len = (int)SendMessage(GetHwnd(), LB_GETTEXT, i, (LONG)wxBuffer); | |
597 | wxBuffer[len] = 0; | |
598 | SIZE extentXY; | |
599 | ::GetTextExtentPoint(dc, (LPTSTR)wxBuffer, len, &extentXY); | |
600 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); | |
601 | if (extentX > largestExtent) | |
602 | largestExtent = extentX; | |
603 | } | |
604 | if (oldFont) | |
605 | ::SelectObject(dc, oldFont); | |
606 | ||
607 | ReleaseDC(GetHwnd(), dc); | |
608 | SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L); | |
609 | } | |
610 | */ | |
611 | } | |
0e320a79 | 612 | |
fb9010ed DW |
613 | wxSize wxListBox::DoGetBestSize() |
614 | { | |
615 | // find the widest string | |
616 | int wLine; | |
617 | int wListbox = 0; | |
618 | for ( int i = 0; i < m_noItems; i++ ) | |
619 | { | |
620 | wxString str(GetString(i)); | |
621 | GetTextExtent(str, &wLine, NULL); | |
622 | if ( wLine > wListbox ) | |
623 | wListbox = wLine; | |
624 | } | |
625 | ||
626 | // give it some reasonable default value if there are no strings in the | |
627 | // list | |
628 | if ( wListbox == 0 ) | |
629 | wListbox = 100; | |
630 | ||
631 | // the listbox should be slightly larger than the widest string | |
632 | int cx, cy; | |
633 | wxGetCharSize(GetHWND(), &cx, &cy, &GetFont()); | |
634 | ||
635 | wListbox += 3*cx; | |
636 | ||
637 | int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*(wxMax(m_noItems, 7)); | |
638 | ||
639 | return wxSize(wListbox, hListbox); | |
640 | } | |
641 | ||
dcd307ee DW |
642 | // ---------------------------------------------------------------------------- |
643 | // callbacks | |
644 | // ---------------------------------------------------------------------------- | |
645 | ||
646 | bool wxListBox::OS2Command(WXUINT param, WXWORD WXUNUSED(id)) | |
0e320a79 | 647 | { |
dcd307ee DW |
648 | /* |
649 | if (param == LBN_SELCANCEL) | |
650 | { | |
651 | event.extraLong = FALSE; | |
652 | } | |
653 | */ | |
654 | // TODO: | |
655 | /* | |
656 | if (param == LBN_SELCHANGE) | |
657 | { | |
658 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId); | |
659 | wxArrayInt aSelections; | |
660 | int count = GetSelections(aSelections); | |
661 | if ( count > 0 ) | |
662 | { | |
663 | event.m_commandInt = aSelections[0]; | |
664 | event.m_clientData = GetClientData(event.m_commandInt); | |
665 | wxString str(GetString(event.m_commandInt)); | |
666 | if (str != wxT("")) | |
667 | { | |
668 | event.m_commandString = str; | |
669 | } | |
670 | } | |
671 | else | |
672 | { | |
673 | event.m_commandInt = -1; | |
674 | event.m_commandString.Empty(); | |
675 | } | |
676 | ||
677 | event.SetEventObject( this ); | |
678 | ProcessCommand(event); | |
679 | return TRUE; | |
680 | } | |
681 | else if (param == LBN_DBLCLK) | |
0e320a79 | 682 | { |
dcd307ee DW |
683 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId); |
684 | event.SetEventObject( this ); | |
685 | GetEventHandler()->ProcessEvent(event); | |
686 | return TRUE; | |
0e320a79 | 687 | } |
dcd307ee DW |
688 | */ |
689 | return FALSE; | |
0e320a79 DW |
690 | } |
691 | ||
fb9010ed | 692 | WXHBRUSH wxListBox::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor, |
dcd307ee | 693 | WXUINT message, WXWPARAM wParam, WXLPARAM lParam) |
fb9010ed | 694 | { |
dcd307ee | 695 | // TODO: |
fb9010ed | 696 | /* |
fb9010ed DW |
697 | if (GetParent()->GetTransparentBackground()) |
698 | SetBkMode((HDC) pDC, TRANSPARENT); | |
699 | else | |
700 | SetBkMode((HDC) pDC, OPAQUE); | |
701 | ||
702 | ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue())); | |
703 | ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue())); | |
704 | ||
705 | wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID); | |
706 | ||
707 | // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush | |
708 | // has a zero usage count. | |
709 | backgroundBrush->RealizeResource(); | |
fb9010ed | 710 | return (WXHBRUSH) backgroundBrush->GetResourceHandle(); |
dcd307ee | 711 | */ |
e6ebb514 | 712 | return (WXHBRUSH)0; |
fb9010ed DW |
713 | } |
714 | ||
dcd307ee DW |
715 | // ---------------------------------------------------------------------------- |
716 | // wxCheckListBox support | |
717 | // ---------------------------------------------------------------------------- | |
fb9010ed DW |
718 | |
719 | #if wxUSE_OWNER_DRAWN | |
720 | ||
721 | // drawing | |
722 | // ------- | |
723 | ||
724 | // space beneath/above each row in pixels | |
725 | // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly. | |
726 | #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1) | |
727 | ||
728 | // the height is the same for all items | |
729 | // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes | |
730 | ||
731 | // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA | |
732 | // message is not yet sent when we get here! | |
733 | bool wxListBox::OS2OnMeasure(WXMEASUREITEMSTRUCT *item) | |
734 | { | |
dcd307ee DW |
735 | // TODO: |
736 | /* | |
fb9010ed DW |
737 | // only owner-drawn control should receive this message |
738 | wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE ); | |
739 | ||
dcd307ee | 740 | MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item; |
fb9010ed DW |
741 | |
742 | wxDC dc; | |
dcd307ee | 743 | dc.SetHDC((WXHDC)CreateIC(wxT("DISPLAY"), NULL, NULL, 0)); |
fb9010ed DW |
744 | dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT)); |
745 | ||
dcd307ee DW |
746 | pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE; |
747 | pStruct->itemWidth = dc.GetCharWidth(); | |
fb9010ed | 748 | |
dcd307ee DW |
749 | return TRUE; |
750 | */ | |
fb9010ed DW |
751 | return TRUE; |
752 | } | |
753 | ||
754 | // forward the message to the appropriate item | |
755 | bool wxListBox::OS2OnDraw(WXDRAWITEMSTRUCT *item) | |
756 | { | |
dcd307ee DW |
757 | // TODO: |
758 | /* | |
fb9010ed DW |
759 | // only owner-drawn control should receive this message |
760 | wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE ); | |
dcd307ee | 761 | |
fb9010ed DW |
762 | DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item; |
763 | ||
764 | long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID); | |
765 | ||
766 | wxCHECK( data && (data != LB_ERR), FALSE ); | |
767 | ||
768 | wxListBoxItem *pItem = (wxListBoxItem *)data; | |
769 | ||
770 | wxDC dc; | |
771 | dc.SetHDC((WXHDC)pStruct->hDC, FALSE); | |
772 | wxRect rect(wxPoint(pStruct->rcItem.left, pStruct->rcItem.top), | |
773 | wxPoint(pStruct->rcItem.right, pStruct->rcItem.bottom)); | |
774 | ||
775 | return pItem->OnDrawItem(dc, rect, | |
776 | (wxOwnerDrawn::wxODAction)pStruct->itemAction, | |
777 | (wxOwnerDrawn::wxODStatus)pStruct->itemState); | |
778 | */ | |
dcd307ee | 779 | return FALSE; |
fb9010ed | 780 | } |
fb9010ed DW |
781 | #endif |
782 | // wxUSE_OWNER_DRAWN |