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