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