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