]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
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 | #ifndef WX_PRECOMP | |
24 | #include "wx/listbox.h" | |
25 | #include "wx/settings.h" | |
26 | #endif | |
27 | ||
28 | #include "wx/msw/private.h" | |
29 | ||
30 | #include <windows.h> | |
31 | #include <windowsx.h> | |
32 | ||
33 | #ifdef __GNUWIN32__ | |
34 | #include <wx/msw/gnuwin32/extra.h> | |
35 | #endif | |
36 | ||
37 | #ifdef GetCharWidth | |
38 | #undef GetCharWidth | |
39 | #endif | |
40 | ||
47d67540 | 41 | #if wxUSE_OWNER_DRAWN |
2bda0e17 KB |
42 | #include "wx/ownerdrw.h" |
43 | #endif | |
44 | ||
14483330 VZ |
45 | #include "wx/dynarray.h" |
46 | #include "wx/log.h" | |
47 | ||
2bda0e17 KB |
48 | #if !USE_SHARED_LIBRARY |
49 | IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl) | |
50 | #endif | |
51 | ||
52 | // ============================================================================ | |
53 | // list box item declaration and implementation | |
54 | // ============================================================================ | |
55 | ||
47d67540 | 56 | #if wxUSE_OWNER_DRAWN |
2bda0e17 KB |
57 | |
58 | class wxListBoxItem : public wxOwnerDrawn | |
59 | { | |
60 | public: | |
61 | wxListBoxItem(const wxString& str = ""); | |
62 | }; | |
63 | ||
64 | wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE) | |
65 | { | |
66 | // no bitmaps/checkmarks | |
67 | SetMarginWidth(0); | |
68 | } | |
69 | ||
c86f1403 | 70 | wxOwnerDrawn *wxListBox::CreateItem(size_t n) |
2bda0e17 KB |
71 | { |
72 | return new wxListBoxItem(); | |
73 | } | |
74 | ||
75 | #endif //USE_OWNER_DRAWN | |
76 | ||
77 | // ============================================================================ | |
78 | // list box control implementation | |
79 | // ============================================================================ | |
80 | ||
81 | // this macro is dangerous but still better than tons of (HWND)GetHWND() | |
82 | #define hwnd (HWND)GetHWND() | |
83 | ||
debe6624 | 84 | bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) |
2bda0e17 KB |
85 | { |
86 | /* | |
87 | if (param == LBN_SELCANCEL) | |
88 | { | |
89 | event.extraLong = FALSE; | |
90 | } | |
91 | */ | |
92 | if (param == LBN_SELCHANGE) | |
93 | { | |
94 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId); | |
14483330 VZ |
95 | wxArrayInt aSelections; |
96 | int count = GetSelections(aSelections); | |
97 | if ( count > 0 ) | |
2bda0e17 | 98 | { |
14483330 | 99 | event.m_commandInt = aSelections[0] ; |
2bda0e17 KB |
100 | event.m_clientData = GetClientData(event.m_commandInt); |
101 | wxString str(GetString(event.m_commandInt)); | |
102 | if (str != "") | |
103 | event.m_commandString = copystring((char *)(const char *)str); | |
104 | } | |
105 | else | |
106 | { | |
107 | event.m_commandInt = -1 ; | |
108 | event.m_commandString = copystring("") ; | |
109 | } | |
110 | ||
111 | event.SetEventObject( this ); | |
112 | ProcessCommand(event); | |
113 | if (event.m_commandString) | |
114 | delete[] event.m_commandString ; | |
115 | return TRUE; | |
116 | } | |
117 | else if (param == LBN_DBLCLK) | |
118 | { | |
119 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId); | |
120 | event.SetEventObject( this ); | |
debe6624 JS |
121 | GetEventHandler()->ProcessEvent(event) ; |
122 | return TRUE; | |
123 | /* | |
2bda0e17 KB |
124 | { |
125 | #if WXWIN_COMPATIBILITY | |
126 | wxWindow *parent = (wxWindow *)GetParent(); | |
127 | if (parent) | |
128 | parent->GetEventHandler()->OnDefaultAction(this); | |
129 | #endif | |
130 | return TRUE; | |
131 | } | |
debe6624 | 132 | */ |
2bda0e17 KB |
133 | } |
134 | return FALSE; | |
135 | } | |
136 | ||
137 | // Listbox item | |
138 | wxListBox::wxListBox(void) | |
139 | { | |
140 | m_noItems = 0; | |
141 | m_selected = 0; | |
2bda0e17 KB |
142 | } |
143 | ||
debe6624 | 144 | bool wxListBox::Create(wxWindow *parent, wxWindowID id, |
2bda0e17 KB |
145 | const wxPoint& pos, |
146 | const wxSize& size, | |
debe6624 JS |
147 | int n, const wxString choices[], |
148 | long style, | |
2bda0e17 KB |
149 | const wxValidator& validator, |
150 | const wxString& name) | |
151 | { | |
152 | m_noItems = n; | |
153 | m_hWnd = 0; | |
154 | m_selected = 0; | |
2bda0e17 KB |
155 | |
156 | SetName(name); | |
157 | SetValidator(validator); | |
158 | ||
159 | if (parent) parent->AddChild(this); | |
160 | ||
161 | wxSystemSettings settings; | |
162 | SetBackgroundColour(settings.GetSystemColour(wxSYS_COLOUR_WINDOW)); | |
fd71308f | 163 | SetForegroundColour(parent->GetForegroundColour()); |
2bda0e17 KB |
164 | |
165 | m_windowId = ( id == -1 ) ? (int)NewControlId() : id; | |
166 | ||
167 | int x = pos.x; | |
168 | int y = pos.y; | |
169 | int width = size.x; | |
170 | int height = size.y; | |
171 | m_windowStyle = style; | |
172 | ||
173 | DWORD wstyle = WS_VSCROLL | WS_TABSTOP | LBS_NOTIFY | LBS_HASSTRINGS; | |
174 | if (m_windowStyle & wxLB_MULTIPLE) | |
175 | wstyle |= LBS_MULTIPLESEL; | |
176 | else if (m_windowStyle & wxLB_EXTENDED) | |
177 | wstyle |= LBS_EXTENDEDSEL; | |
178 | ||
179 | if (m_windowStyle & wxLB_ALWAYS_SB) | |
180 | wstyle |= LBS_DISABLENOSCROLL ; | |
181 | if (m_windowStyle & wxLB_HSCROLL) | |
182 | wstyle |= WS_HSCROLL; | |
183 | if (m_windowStyle & wxLB_SORT) | |
184 | wstyle |= LBS_SORT; | |
185 | ||
47d67540 | 186 | #if wxUSE_OWNER_DRAWN |
2bda0e17 KB |
187 | if ( m_windowStyle & wxLB_OWNERDRAW ) { |
188 | // we don't support LBS_OWNERDRAWVARIABLE yet | |
189 | wstyle |= LBS_OWNERDRAWFIXED; | |
190 | } | |
2bda0e17 | 191 | #endif |
40e1a9c0 JS |
192 | // Without this style, you get unexpected heights, so e.g. constraint layout |
193 | // doesn't work properly | |
194 | wstyle |= LBS_NOINTEGRALHEIGHT; | |
2bda0e17 KB |
195 | |
196 | bool want3D; | |
197 | WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ; | |
198 | ||
199 | // Even with extended styles, need to combine with WS_BORDER | |
200 | // for them to look right. | |
c085e333 VZ |
201 | if ( want3D || wxStyleHasBorder(m_windowStyle) ) |
202 | { | |
2bda0e17 KB |
203 | wstyle |= WS_BORDER; |
204 | } | |
205 | ||
c085e333 | 206 | m_hWnd = (WXHWND)::CreateWindowEx(exStyle, "LISTBOX", NULL, |
2bda0e17 KB |
207 | wstyle | WS_CHILD, |
208 | 0, 0, 0, 0, | |
209 | (HWND)parent->GetHWND(), (HMENU)m_windowId, | |
210 | wxGetInstance(), NULL); | |
1c089c47 | 211 | |
c085e333 | 212 | wxCHECK_MSG( m_hWnd, FALSE, "Failed to create listbox" ); |
1c089c47 | 213 | |
2bda0e17 KB |
214 | #if CTL3D |
215 | if (want3D) | |
216 | { | |
c085e333 | 217 | Ctl3dSubclassCtl(hwnd); |
2bda0e17 KB |
218 | m_useCtl3D = TRUE; |
219 | } | |
220 | #endif | |
221 | ||
1c089c47 | 222 | // Subclass again to catch messages |
c085e333 | 223 | SubclassWin(m_hWnd); |
1c089c47 | 224 | |
c86f1403 VZ |
225 | size_t ui; |
226 | for (ui = 0; ui < (size_t)n; ui++) { | |
c085e333 | 227 | Append(choices[ui]); |
2bda0e17 KB |
228 | } |
229 | ||
47d67540 | 230 | #if wxUSE_OWNER_DRAWN |
2bda0e17 | 231 | if ( m_windowStyle & wxLB_OWNERDRAW ) { |
c86f1403 | 232 | for (ui = 0; ui < (size_t)n; ui++) { |
2bda0e17 KB |
233 | // create new item which will process WM_{DRAW|MEASURE}ITEM messages |
234 | wxOwnerDrawn *pNewItem = CreateItem(ui); | |
235 | pNewItem->SetName(choices[ui]); | |
236 | m_aItems.Add(pNewItem); | |
c085e333 | 237 | ListBox_SetItemData(hwnd, ui, pNewItem); |
2bda0e17 KB |
238 | } |
239 | } | |
1c089c47 | 240 | #endif |
2bda0e17 | 241 | |
c085e333 VZ |
242 | if ( (m_windowStyle & wxLB_MULTIPLE) == 0 ) |
243 | SendMessage(hwnd, LB_SETCURSEL, 0, 0); | |
2bda0e17 | 244 | |
2bda0e17 KB |
245 | SetFont(* parent->GetFont()); |
246 | ||
247 | SetSize(x, y, width, height); | |
248 | ||
c085e333 | 249 | Show(TRUE); |
1c089c47 | 250 | |
2bda0e17 KB |
251 | return TRUE; |
252 | } | |
253 | ||
254 | wxListBox::~wxListBox(void) | |
255 | { | |
47d67540 | 256 | #if wxUSE_OWNER_DRAWN |
c86f1403 | 257 | size_t uiCount = m_aItems.Count(); |
2bda0e17 KB |
258 | while ( uiCount-- != 0 ) { |
259 | delete m_aItems[uiCount]; | |
260 | } | |
1c089c47 | 261 | #endif |
2bda0e17 KB |
262 | } |
263 | ||
264 | void wxListBox::SetupColours(void) | |
265 | { | |
266 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW)); | |
fd71308f | 267 | SetForegroundColour(GetParent()->GetForegroundColour()); |
2bda0e17 KB |
268 | } |
269 | ||
debe6624 | 270 | void wxListBox::SetFirstItem(int N) |
2bda0e17 KB |
271 | { |
272 | SendMessage(hwnd,LB_SETTOPINDEX,(WPARAM)N,(LPARAM)0) ; | |
273 | } | |
274 | ||
275 | void wxListBox::SetFirstItem(const wxString& s) | |
276 | { | |
277 | int N = FindString(s) ; | |
278 | ||
279 | if (N>=0) | |
280 | SetFirstItem(N) ; | |
281 | } | |
282 | ||
debe6624 | 283 | void wxListBox::Delete(int N) |
2bda0e17 KB |
284 | { |
285 | SendMessage(hwnd, LB_DELETESTRING, N, 0); | |
286 | m_noItems --; | |
287 | SetHorizontalExtent(""); | |
288 | } | |
289 | ||
290 | void wxListBox::Append(const wxString& item) | |
291 | { | |
292 | int index = ListBox_AddString(hwnd, item); | |
293 | m_noItems ++; | |
294 | ||
47d67540 | 295 | #if wxUSE_OWNER_DRAWN |
2bda0e17 | 296 | if ( m_windowStyle & wxLB_OWNERDRAW ) { |
49884e3e | 297 | wxOwnerDrawn *pNewItem = CreateItem(index); // dummy argument |
2bda0e17 KB |
298 | pNewItem->SetName(item); |
299 | m_aItems.Add(pNewItem); | |
300 | ListBox_SetItemData(hwnd, index, pNewItem); | |
301 | } | |
1c089c47 | 302 | #endif |
2bda0e17 KB |
303 | |
304 | SetHorizontalExtent(item); | |
305 | } | |
306 | ||
307 | void wxListBox::Append(const wxString& item, char *Client_data) | |
308 | { | |
309 | int index = ListBox_AddString(hwnd, item); | |
310 | m_noItems ++; | |
311 | ||
47d67540 | 312 | #if wxUSE_OWNER_DRAWN |
2bda0e17 KB |
313 | if ( m_windowStyle & wxLB_OWNERDRAW ) { |
314 | // client data must be pointer to wxOwnerDrawn, otherwise we would crash | |
315 | // in OnMeasure/OnDraw. | |
316 | wxFAIL_MSG("Can't use client data with owner-drawn listboxes"); | |
317 | } | |
318 | else | |
1c089c47 | 319 | #endif |
2bda0e17 KB |
320 | ListBox_SetItemData(hwnd, index, Client_data); |
321 | ||
322 | SetHorizontalExtent(item); | |
323 | } | |
324 | ||
debe6624 | 325 | void wxListBox::Set(int n, const wxString *choices, char** clientData) |
2bda0e17 KB |
326 | { |
327 | ShowWindow(hwnd, SW_HIDE); | |
328 | ListBox_ResetContent(hwnd); | |
329 | int i; | |
330 | for (i = 0; i < n; i++) | |
331 | { | |
332 | ListBox_AddString(hwnd, choices[i]); | |
333 | if ( clientData ) | |
334 | ListBox_SetItemData(hwnd, i, clientData[i]); | |
335 | } | |
336 | m_noItems = n; | |
337 | ||
47d67540 | 338 | #if wxUSE_OWNER_DRAWN |
2bda0e17 KB |
339 | if ( m_windowStyle & wxLB_OWNERDRAW ) { |
340 | // first delete old items | |
c86f1403 | 341 | size_t ui = m_aItems.Count(); |
2bda0e17 KB |
342 | while ( ui-- != 0 ) { |
343 | delete m_aItems[ui]; | |
344 | } | |
345 | m_aItems.Empty(); | |
346 | ||
347 | // then create new ones | |
c86f1403 | 348 | for (ui = 0; ui < (size_t)n; ui++) { |
2bda0e17 KB |
349 | wxOwnerDrawn *pNewItem = CreateItem(ui); |
350 | pNewItem->SetName(choices[ui]); | |
351 | m_aItems.Add(pNewItem); | |
352 | ListBox_SetItemData(hwnd, ui, pNewItem); | |
353 | ||
354 | wxASSERT_MSG(clientData[ui] == NULL, | |
355 | "Can't use client data with owner-drawn listboxes"); | |
356 | } | |
357 | } | |
1c089c47 | 358 | #endif |
2bda0e17 KB |
359 | |
360 | SetHorizontalExtent(""); | |
361 | ShowWindow(hwnd, SW_SHOW); | |
362 | } | |
363 | ||
364 | int wxListBox::FindString(const wxString& s) const | |
365 | { | |
366 | int pos = ListBox_FindStringExact(hwnd, (WPARAM)-1, s); | |
367 | if (pos == LB_ERR) | |
368 | return -1; | |
369 | else | |
370 | return pos; | |
371 | } | |
372 | ||
373 | void wxListBox::Clear(void) | |
374 | { | |
375 | ListBox_ResetContent(hwnd); | |
376 | ||
377 | m_noItems = 0; | |
378 | ListBox_GetHorizontalExtent(hwnd); | |
379 | } | |
380 | ||
debe6624 | 381 | void wxListBox::SetSelection(int N, bool select) |
2bda0e17 KB |
382 | { |
383 | if ((m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED)) | |
384 | SendMessage(hwnd, LB_SETSEL, select, N); | |
385 | else | |
386 | { | |
387 | int N1 = N; | |
388 | if (!select) | |
389 | N1 = -1; | |
390 | SendMessage(hwnd, LB_SETCURSEL, N1, 0); | |
391 | } | |
392 | } | |
393 | ||
debe6624 | 394 | bool wxListBox::Selected(int N) const |
2bda0e17 KB |
395 | { |
396 | return SendMessage(hwnd, LB_GETSEL, N, 0) == 0 ? FALSE : TRUE; | |
397 | } | |
398 | ||
debe6624 | 399 | void wxListBox::Deselect(int N) |
2bda0e17 KB |
400 | { |
401 | if ((m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED)) | |
402 | SendMessage(hwnd, LB_SETSEL, FALSE, N); | |
403 | } | |
404 | ||
debe6624 | 405 | char *wxListBox::GetClientData(int N) const |
2bda0e17 KB |
406 | { |
407 | return (char *)SendMessage(hwnd, LB_GETITEMDATA, N, 0); | |
408 | } | |
409 | ||
debe6624 | 410 | void wxListBox::SetClientData(int N, char *Client_data) |
2bda0e17 | 411 | { |
14483330 VZ |
412 | if ( ListBox_SetItemData(hwnd, N, Client_data) == LB_ERR ) |
413 | wxLogDebug("LB_SETITEMDATA failed"); | |
2bda0e17 KB |
414 | } |
415 | ||
416 | // Return number of selections and an array of selected integers | |
14483330 | 417 | int wxListBox::GetSelections(wxArrayInt& aSelections) const |
2bda0e17 | 418 | { |
14483330 VZ |
419 | aSelections.Empty(); |
420 | ||
2bda0e17 KB |
421 | if ((m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED)) |
422 | { | |
14483330 VZ |
423 | int no_sel = ListBox_GetSelCount(hwnd); |
424 | if (no_sel != 0) { | |
425 | int *selections = new int[no_sel]; | |
426 | if ( ListBox_GetSelItems(hwnd, no_sel, selections) == LB_ERR ) { | |
427 | wxFAIL_MSG("This listbox can't have single-selection style!"); | |
428 | } | |
429 | ||
430 | aSelections.Alloc(no_sel); | |
431 | for ( int n = 0; n < no_sel; n++ ) | |
432 | aSelections.Add(selections[n]); | |
433 | ||
434 | delete [] selections; | |
435 | } | |
436 | ||
2bda0e17 KB |
437 | return no_sel; |
438 | } | |
14483330 | 439 | else // single-selection listbox |
2bda0e17 | 440 | { |
14483330 VZ |
441 | aSelections.Add(ListBox_GetCurSel(hwnd)); |
442 | ||
2bda0e17 KB |
443 | return 1; |
444 | } | |
445 | } | |
446 | ||
447 | // Get single selection, for single choice list items | |
14483330 | 448 | int wxListBox::GetSelection() const |
2bda0e17 | 449 | { |
14483330 VZ |
450 | wxCHECK_MSG( !(m_windowStyle & wxLB_MULTIPLE) && |
451 | !(m_windowStyle & wxLB_EXTENDED), | |
452 | -1, | |
453 | "GetSelection() can't be used with multiple-selection " | |
454 | "listboxes, use GetSelections() instead." ); | |
455 | ||
456 | return ListBox_GetCurSel(hwnd); | |
2bda0e17 KB |
457 | } |
458 | ||
459 | // Find string for position | |
debe6624 | 460 | wxString wxListBox::GetString(int N) const |
2bda0e17 KB |
461 | { |
462 | if (N < 0 || N > m_noItems) | |
463 | return wxString(""); | |
464 | ||
465 | int len = (int)SendMessage(hwnd, LB_GETTEXT, N, (LONG)wxBuffer); | |
466 | wxBuffer[len] = 0; | |
467 | return wxString(wxBuffer); | |
468 | } | |
469 | ||
debe6624 | 470 | void wxListBox::SetSize(int x, int y, int width, int height, int sizeFlags) |
2bda0e17 KB |
471 | { |
472 | int currentX, currentY; | |
473 | GetPosition(¤tX, ¤tY); | |
474 | ||
475 | int x1 = x; | |
476 | int y1 = y; | |
477 | int w1 = width; | |
478 | int h1 = height; | |
479 | ||
480 | if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
481 | x1 = currentX; | |
482 | if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)) | |
483 | y1 = currentY; | |
484 | ||
81d66cf3 JS |
485 | AdjustForParentClientOrigin(x1, y1, sizeFlags); |
486 | ||
2bda0e17 KB |
487 | // If we're prepared to use the existing size, then... |
488 | if (width == -1 && height == -1 && ((sizeFlags & wxSIZE_AUTO) != wxSIZE_AUTO)) | |
489 | { | |
490 | GetSize(&w1, &h1); | |
491 | } | |
492 | ||
493 | int cx; // button font dimensions | |
494 | int cy; | |
495 | ||
496 | wxGetCharSize(GetHWND(), &cx, &cy,GetFont()); | |
497 | ||
498 | float control_width, control_height, control_x, control_y; | |
499 | ||
500 | // Deal with default size (using -1 values) | |
501 | if (w1<=0) | |
502 | w1 = DEFAULT_ITEM_WIDTH; | |
503 | ||
504 | if (h1<=0) | |
505 | h1 = DEFAULT_ITEM_HEIGHT; | |
506 | ||
507 | control_x = (float)x1; | |
508 | control_y = (float)y1; | |
509 | control_width = (float)w1; | |
510 | control_height = (float)h1; | |
511 | ||
512 | // Calculations may have made size too small | |
513 | if (control_height <= 0) | |
514 | control_height = (float)DEFAULT_ITEM_HEIGHT; | |
515 | ||
516 | if (control_width <= 0) | |
517 | control_width = (float)DEFAULT_ITEM_WIDTH; | |
518 | ||
519 | // wxDebugMsg("About to set the listbox height to %d", (int)control_height); | |
520 | MoveWindow(hwnd, (int)control_x, (int)control_y, | |
521 | (int)control_width, (int)control_height, TRUE); | |
522 | ||
2bda0e17 KB |
523 | } |
524 | ||
525 | // Windows-specific code to set the horizontal extent of | |
526 | // the listbox, if necessary. If s is non-NULL, it's | |
527 | // used to calculate the horizontal extent. | |
528 | // Otherwise, all strings are used. | |
529 | void wxListBox::SetHorizontalExtent(const wxString& s) | |
530 | { | |
531 | // Only necessary if we want a horizontal scrollbar | |
532 | if (!(m_windowStyle & wxHSCROLL)) | |
533 | return; | |
534 | TEXTMETRIC lpTextMetric; | |
535 | ||
536 | if (s != "") | |
537 | { | |
538 | int existingExtent = (int)SendMessage(hwnd, LB_GETHORIZONTALEXTENT, 0, 0L); | |
539 | HDC dc = GetWindowDC(hwnd); | |
540 | HFONT oldFont = 0; | |
541 | if (GetFont() && GetFont()->GetResourceHandle()) | |
c4e7c2aa | 542 | oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont()->GetResourceHandle()); |
2bda0e17 KB |
543 | |
544 | GetTextMetrics(dc, &lpTextMetric); | |
545 | SIZE extentXY; | |
546 | ::GetTextExtentPoint(dc, (LPSTR) (const char *)s, s.Length(), &extentXY); | |
547 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); | |
548 | ||
549 | if (oldFont) | |
550 | ::SelectObject(dc, oldFont); | |
551 | ||
552 | ReleaseDC(hwnd, dc); | |
553 | if (extentX > existingExtent) | |
554 | SendMessage(hwnd, LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L); | |
555 | return; | |
556 | } | |
557 | else | |
558 | { | |
559 | int largestExtent = 0; | |
560 | HDC dc = GetWindowDC(hwnd); | |
561 | HFONT oldFont = 0; | |
562 | if (GetFont() && GetFont()->GetResourceHandle()) | |
c4e7c2aa | 563 | oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont()->GetResourceHandle()); |
2bda0e17 KB |
564 | |
565 | GetTextMetrics(dc, &lpTextMetric); | |
566 | int i; | |
567 | for (i = 0; i < m_noItems; i++) | |
568 | { | |
569 | int len = (int)SendMessage(hwnd, LB_GETTEXT, i, (LONG)wxBuffer); | |
570 | wxBuffer[len] = 0; | |
571 | SIZE extentXY; | |
572 | ::GetTextExtentPoint(dc, (LPSTR)wxBuffer, len, &extentXY); | |
573 | int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth); | |
574 | if (extentX > largestExtent) | |
575 | largestExtent = extentX; | |
576 | } | |
577 | if (oldFont) | |
578 | ::SelectObject(dc, oldFont); | |
579 | ||
580 | ReleaseDC(hwnd, dc); | |
581 | SendMessage(hwnd, LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L); | |
582 | } | |
583 | } | |
584 | ||
585 | void | |
debe6624 | 586 | wxListBox::InsertItems(int nItems, const wxString items[], int pos) |
2bda0e17 KB |
587 | { |
588 | int i; | |
589 | for (i = 0; i < nItems; i++) | |
590 | ListBox_InsertString(hwnd, i + pos, items[i]); | |
591 | m_noItems += nItems; | |
592 | ||
47d67540 | 593 | #if wxUSE_OWNER_DRAWN |
2bda0e17 KB |
594 | if ( m_windowStyle & wxLB_OWNERDRAW ) { |
595 | for ( i = 0; i < nItems; i++ ) { | |
c86f1403 | 596 | wxOwnerDrawn *pNewItem = CreateItem((size_t)(pos + i)); |
2bda0e17 | 597 | pNewItem->SetName(items[i]); |
c86f1403 | 598 | m_aItems.Insert(pNewItem, (size_t)(pos + i)); |
2bda0e17 KB |
599 | ListBox_SetItemData(hwnd, i, pNewItem); |
600 | } | |
601 | } | |
602 | #endif | |
603 | ||
604 | SetHorizontalExtent(""); | |
605 | } | |
606 | ||
debe6624 | 607 | void wxListBox::SetString(int N, const wxString& s) |
2bda0e17 | 608 | { |
400735a8 JS |
609 | int sel = -1; |
610 | if (!(m_windowStyle & wxLB_MULTIPLE) && !(m_windowStyle & wxLB_EXTENDED)) | |
611 | sel = GetSelection(); | |
2bda0e17 KB |
612 | |
613 | char *oldData = (char *)wxListBox::GetClientData(N); | |
614 | ||
615 | SendMessage(hwnd, LB_DELETESTRING, N, 0); | |
616 | ||
617 | int newN = N; | |
618 | if (N == (m_noItems - 1)) | |
619 | newN = -1; | |
620 | ||
621 | SendMessage(hwnd, LB_INSERTSTRING, newN, (LPARAM) (const char *)s); | |
622 | if (oldData) | |
623 | wxListBox::SetClientData(N, oldData); | |
624 | ||
625 | // Selection may have changed | |
626 | if (sel >= 0) | |
627 | SetSelection(sel); | |
628 | ||
47d67540 | 629 | #if wxUSE_OWNER_DRAWN |
e1d1da03 JS |
630 | if ( m_windowStyle & wxLB_OWNERDRAW ) |
631 | // update item's text | |
632 | m_aItems[N]->SetName(s); | |
633 | #endif //USE_OWNER_DRAWN | |
2bda0e17 KB |
634 | } |
635 | ||
636 | int wxListBox::Number (void) const | |
637 | { | |
638 | return m_noItems; | |
639 | } | |
640 | ||
641 | // For single selection items only | |
642 | wxString wxListBox::GetStringSelection (void) const | |
643 | { | |
644 | int sel = GetSelection (); | |
645 | if (sel > -1) | |
646 | return this->GetString (sel); | |
647 | else | |
648 | return wxString(""); | |
649 | } | |
650 | ||
debe6624 | 651 | bool wxListBox::SetStringSelection (const wxString& s, bool flag) |
2bda0e17 KB |
652 | { |
653 | int sel = FindString (s); | |
654 | if (sel > -1) | |
655 | { | |
656 | SetSelection (sel, flag); | |
657 | return TRUE; | |
658 | } | |
659 | else | |
660 | return FALSE; | |
661 | } | |
662 | ||
663 | // Is this the right thing? Won't setselection generate a command | |
664 | // event too? No! It'll just generate a setselection event. | |
665 | // But we still can't have this being called whenever a real command | |
666 | // is generated, because it sets the selection, which will already | |
667 | // have been done! (Unless we have an optional argument for calling | |
668 | // by the actual window system, or a separate function, ProcessCommand) | |
669 | void wxListBox::Command (wxCommandEvent & event) | |
670 | { | |
671 | if (event.m_extraLong) | |
672 | SetSelection (event.m_commandInt); | |
673 | else | |
674 | { | |
675 | Deselect (event.m_commandInt); | |
676 | return; | |
677 | } | |
678 | ProcessCommand (event); | |
679 | } | |
680 | ||
debe6624 | 681 | WXHBRUSH wxListBox::OnCtlColor(WXHDC pDC, WXHWND pWnd, WXUINT nCtlColor, |
2bda0e17 KB |
682 | WXUINT message, WXWPARAM wParam, WXLPARAM lParam) |
683 | { | |
684 | #if CTL3D | |
685 | if ( m_useCtl3D ) | |
686 | { | |
687 | HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam); | |
688 | return (WXHBRUSH) hbrush; | |
689 | } | |
690 | #endif | |
691 | ||
692 | if (GetParent()->GetTransparentBackground()) | |
693 | SetBkMode((HDC) pDC, TRANSPARENT); | |
694 | else | |
695 | SetBkMode((HDC) pDC, OPAQUE); | |
696 | ||
697 | ::SetBkColor((HDC) pDC, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue())); | |
698 | ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue())); | |
699 | ||
700 | wxBrush *backgroundBrush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour(), wxSOLID); | |
701 | ||
702 | // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush | |
703 | // has a zero usage count. | |
704 | backgroundBrush->RealizeResource(); | |
705 | return (WXHBRUSH) backgroundBrush->GetResourceHandle(); | |
706 | } | |
707 | ||
708 | long wxListBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
709 | { | |
dabeb021 | 710 | #if 0 |
2bda0e17 KB |
711 | switch (nMsg) |
712 | { | |
713 | case WM_INITDIALOG: | |
714 | case WM_ACTIVATE: | |
715 | case WM_SETFOCUS: | |
716 | case WM_KILLFOCUS: | |
717 | case WM_CREATE: | |
718 | case WM_PAINT: | |
719 | case WM_QUERYDRAGICON: | |
720 | case WM_SIZE: | |
721 | case WM_RBUTTONDOWN: | |
722 | case WM_RBUTTONUP: | |
723 | case WM_RBUTTONDBLCLK: | |
724 | case WM_MBUTTONDOWN: | |
725 | case WM_MBUTTONUP: | |
726 | case WM_MBUTTONDBLCLK: | |
727 | case WM_LBUTTONDOWN: | |
728 | case WM_LBUTTONUP: | |
1c089c47 | 729 | case WM_LBUTTONDBLCLK: |
2bda0e17 | 730 | case WM_MOUSEMOVE: |
2bda0e17 KB |
731 | case WM_COMMAND: |
732 | case WM_NOTIFY: | |
debe6624 | 733 | case WM_DESTROY: |
2bda0e17 KB |
734 | case WM_MENUSELECT: |
735 | case WM_INITMENUPOPUP: | |
736 | case WM_DRAWITEM: | |
737 | case WM_MEASUREITEM: | |
738 | case WM_KEYDOWN: | |
739 | case WM_KEYUP: | |
740 | case WM_CHAR: // Always an ASCII character | |
741 | case WM_HSCROLL: | |
742 | case WM_VSCROLL: | |
743 | case WM_CTLCOLORBTN: | |
744 | case WM_CTLCOLORDLG: | |
745 | case WM_CTLCOLORLISTBOX: | |
746 | case WM_CTLCOLORMSGBOX: | |
747 | case WM_CTLCOLORSCROLLBAR: | |
748 | case WM_CTLCOLORSTATIC: | |
749 | case WM_CTLCOLOREDIT: | |
750 | case WM_SYSCOLORCHANGE: | |
751 | case WM_ERASEBKGND: | |
752 | case WM_MDIACTIVATE: | |
753 | case WM_DROPFILES: | |
754 | case WM_QUERYENDSESSION: | |
755 | case WM_CLOSE: | |
756 | case WM_GETMINMAXINFO: | |
757 | case WM_NCHITTEST: | |
758 | return MSWDefWindowProc(nMsg, wParam, lParam ); | |
759 | } | |
dabeb021 | 760 | #endif |
debe6624 | 761 | |
2bda0e17 KB |
762 | return wxControl::MSWWindowProc(nMsg, wParam, lParam); |
763 | } | |
764 | ||
47d67540 | 765 | #if wxUSE_OWNER_DRAWN |
2bda0e17 KB |
766 | |
767 | // drawing | |
768 | // ------- | |
769 | ||
770 | // space beneath/above each row in pixels | |
771 | // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly. | |
772 | #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1) | |
773 | ||
774 | // the height is the same for all items | |
775 | // ## should be changed for LBS_OWNERDRAWVARIABLE style listboxes | |
776 | // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA | |
777 | // message is not yet sent when we get here! | |
778 | bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) | |
779 | { | |
780 | // only owner-drawn control should receive this message | |
14483330 | 781 | wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE ); |
2bda0e17 KB |
782 | |
783 | MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item; | |
784 | ||
785 | wxDC dc; | |
786 | dc.SetHDC((WXHDC)CreateIC("DISPLAY", NULL, NULL, 0)); | |
787 | dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT)); | |
788 | ||
789 | pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE; | |
790 | pStruct->itemWidth = dc.GetCharWidth(); | |
791 | ||
792 | return TRUE; | |
793 | } | |
794 | ||
795 | // forward the message to the appropriate item | |
796 | bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item) | |
797 | { | |
798 | // only owner-drawn control should receive this message | |
14483330 | 799 | wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE ); |
2bda0e17 KB |
800 | |
801 | DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item; | |
802 | wxListBoxItem *pItem = (wxListBoxItem *)SendMessage(hwnd, LB_GETITEMDATA, | |
803 | pStruct->itemID, 0); | |
804 | ||
14483330 | 805 | wxCHECK( (int)pItem != LB_ERR, FALSE ); |
2bda0e17 KB |
806 | |
807 | wxDC dc; | |
808 | dc.SetHDC((WXHDC)pStruct->hDC, FALSE); | |
809 | wxRect rect(pStruct->rcItem.left, pStruct->rcItem.top, | |
810 | pStruct->rcItem.right - pStruct->rcItem.left, | |
811 | pStruct->rcItem.bottom - pStruct->rcItem.top); | |
812 | ||
813 | return pItem->OnDrawItem(dc, rect, | |
814 | (wxOwnerDrawn::wxODAction)pStruct->itemAction, | |
815 | (wxOwnerDrawn::wxODStatus)pStruct->itemState); | |
816 | } | |
817 | ||
818 | #endif | |
47d67540 | 819 | // wxUSE_OWNER_DRAWN |