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