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