]>
Commit | Line | Data |
---|---|---|
4bb6408c | 1 | /////////////////////////////////////////////////////////////////////////////// |
11e62fe6 | 2 | // Name: src/motif/listbox.cpp |
4bb6408c JS |
3 | // Purpose: wxListBox |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
65571936 | 9 | // Licence: wxWindows licence |
4bb6408c JS |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
1248b41f MB |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
8228b893 WS |
15 | #if wxUSE_LISTBOX |
16 | ||
e4db172a WS |
17 | #include "wx/listbox.h" |
18 | ||
ad9835c9 WS |
19 | #ifndef WX_PRECOMP |
20 | #include "wx/dynarray.h" | |
e4db172a | 21 | #include "wx/log.h" |
de6185e2 | 22 | #include "wx/utils.h" |
9eddec69 | 23 | #include "wx/settings.h" |
aaa6d89a | 24 | #include "wx/arrstr.h" |
ad9835c9 WS |
25 | #endif |
26 | ||
4dff3400 JJ |
27 | #ifdef __VMS |
28 | #define XtParent XTPARENT | |
29 | #define XtDisplay XTDISPLAY | |
30 | #endif | |
31 | ||
338dd992 JJ |
32 | #ifdef __VMS__ |
33 | #pragma message disable nosimpint | |
34 | #endif | |
f97c9854 | 35 | #include <Xm/List.h> |
338dd992 JJ |
36 | #ifdef __VMS__ |
37 | #pragma message enable nosimpint | |
38 | #endif | |
f97c9854 | 39 | #include "wx/motif/private.h" |
4bb6408c | 40 | |
29006414 | 41 | IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl) |
4bb6408c | 42 | |
29006414 VZ |
43 | static void wxListBoxCallback(Widget w, |
44 | XtPointer clientData, | |
45 | XmListCallbackStruct * cbs); | |
f97c9854 | 46 | |
99ab3e3f MB |
47 | // ---------------------------------------------------------------------------- |
48 | // wxSizeKeeper | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
51 | // helper class to reduce code duplication | |
52 | class wxSizeKeeper | |
53 | { | |
54 | int m_x, m_y; | |
55 | wxWindow* m_w; | |
56 | public: | |
57 | wxSizeKeeper( wxWindow* w ) | |
58 | : m_w( w ) | |
59 | { | |
60 | m_w->GetSize( &m_x, &m_y ); | |
61 | } | |
62 | ||
63 | void Restore() | |
64 | { | |
65 | int x, y; | |
66 | ||
67 | m_w->GetSize( &x, &y ); | |
68 | if( x != m_x || y != m_y ) | |
69 | m_w->SetSize( -1, -1, m_x, m_y ); | |
70 | } | |
71 | }; | |
72 | ||
4bb6408c JS |
73 | // ============================================================================ |
74 | // list box control implementation | |
75 | // ============================================================================ | |
76 | ||
77 | // Listbox item | |
99ab3e3f | 78 | wxListBox::wxListBox() |
4bb6408c | 79 | { |
f97c9854 | 80 | m_noItems = 0; |
4bb6408c JS |
81 | } |
82 | ||
83 | bool wxListBox::Create(wxWindow *parent, wxWindowID id, | |
84 | const wxPoint& pos, | |
85 | const wxSize& size, | |
86 | int n, const wxString choices[], | |
87 | long style, | |
88 | const wxValidator& validator, | |
89 | const wxString& name) | |
90 | { | |
99ab3e3f MB |
91 | if( !wxControl::CreateControl( parent, id, pos, size, style, |
92 | validator, name ) ) | |
96be256b | 93 | return false; |
99ab3e3f | 94 | |
aa61d352 | 95 | m_noItems = (unsigned int)n; |
94b49b93 | 96 | m_backgroundColour = * wxWHITE; |
29006414 | 97 | |
f97c9854 | 98 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
73608949 | 99 | Display* dpy = XtDisplay(parentWidget); |
e1aae528 MB |
100 | |
101 | Arg args[4]; | |
99ab3e3f | 102 | int count = 0; |
e1aae528 MB |
103 | XtSetArg( args[count], XmNlistSizePolicy, XmCONSTANT ); ++count; |
104 | XtSetArg( args[count], XmNselectionPolicy, | |
99ab3e3f MB |
105 | ( m_windowStyle & wxLB_MULTIPLE ) ? XmMULTIPLE_SELECT : |
106 | ( m_windowStyle & wxLB_EXTENDED ) ? XmEXTENDED_SELECT : | |
107 | XmBROWSE_SELECT ); | |
108 | ++count; | |
73608949 | 109 | if( m_font.Ok() ) |
e1aae528 | 110 | { |
73608949 MB |
111 | XtSetArg( args[count], |
112 | (String)wxFont::GetFontTag(), m_font.GetFontTypeC(dpy) ); | |
e1aae528 MB |
113 | ++count; |
114 | } | |
99ab3e3f | 115 | if( m_windowStyle & wxLB_ALWAYS_SB ) |
f97c9854 | 116 | { |
e1aae528 | 117 | XtSetArg( args[count], XmNscrollBarDisplayPolicy, XmSTATIC ); |
99ab3e3f | 118 | ++count; |
f97c9854 | 119 | } |
29006414 | 120 | |
d3a80c92 MB |
121 | Widget listWidget = |
122 | XmCreateScrolledList(parentWidget, | |
9b135950 | 123 | wxConstCast(name.mb_str(), char), args, count); |
29006414 | 124 | |
f97c9854 | 125 | m_mainWidget = (WXWidget) listWidget; |
29006414 | 126 | |
a4294b78 | 127 | Set(n, choices); |
29006414 | 128 | |
f97c9854 | 129 | XtManageChild (listWidget); |
29006414 | 130 | |
e1aae528 MB |
131 | wxSize best = GetBestSize(); |
132 | if( size.x != -1 ) best.x = size.x; | |
133 | if( size.y != -1 ) best.y = size.y; | |
29006414 | 134 | |
ef41d80c MB |
135 | XtAddCallback (listWidget, |
136 | XmNbrowseSelectionCallback, | |
137 | (XtCallbackProc) wxListBoxCallback, | |
138 | (XtPointer) this); | |
139 | XtAddCallback (listWidget, | |
140 | XmNextendedSelectionCallback, | |
141 | (XtCallbackProc) wxListBoxCallback, | |
142 | (XtPointer) this); | |
143 | XtAddCallback (listWidget, | |
144 | XmNmultipleSelectionCallback, | |
145 | (XtCallbackProc) wxListBoxCallback, | |
146 | (XtPointer) this); | |
147 | XtAddCallback (listWidget, | |
148 | XmNdefaultActionCallback, | |
149 | (XtCallbackProc) wxListBoxCallback, | |
150 | (XtPointer) this); | |
29006414 | 151 | |
ef41d80c | 152 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, |
e1aae528 | 153 | pos.x, pos.y, best.x, best.y); |
29006414 | 154 | |
0d57be45 | 155 | ChangeBackgroundColour(); |
29006414 | 156 | |
96be256b | 157 | return true; |
4bb6408c JS |
158 | } |
159 | ||
584ad2a3 MB |
160 | bool wxListBox::Create(wxWindow *parent, wxWindowID id, |
161 | const wxPoint& pos, | |
162 | const wxSize& size, | |
163 | const wxArrayString& choices, | |
164 | long style, | |
165 | const wxValidator& validator, | |
166 | const wxString& name) | |
167 | { | |
168 | wxCArrayString chs(choices); | |
169 | return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(), | |
170 | style, validator, name); | |
171 | } | |
172 | ||
4bb6408c JS |
173 | wxListBox::~wxListBox() |
174 | { | |
99ab3e3f MB |
175 | if( HasClientObjectData() ) |
176 | m_clientDataDict.DestroyData(); | |
177 | } | |
178 | ||
179 | void wxListBox::SetSelectionPolicy() | |
180 | { | |
181 | Widget listBox = (Widget)m_mainWidget; | |
182 | Arg args[3]; | |
183 | ||
184 | XtSetArg( args[0], XmNlistSizePolicy, XmCONSTANT ); | |
185 | ||
186 | XtSetArg( args[1], XmNselectionPolicy, | |
187 | ( m_windowStyle & wxLB_MULTIPLE ) ? XmMULTIPLE_SELECT : | |
188 | ( m_windowStyle & wxLB_EXTENDED ) ? XmEXTENDED_SELECT : | |
189 | XmBROWSE_SELECT ); | |
190 | ||
191 | XtSetValues( listBox, args, 2 ); | |
4bb6408c JS |
192 | } |
193 | ||
ef41d80c | 194 | void wxListBox::DoSetFirstItem( int N ) |
4bb6408c | 195 | { |
2d120f83 | 196 | int count, length; |
29006414 | 197 | |
8228b893 | 198 | if (!IsValid(N)) |
2d120f83 | 199 | return; |
8228b893 | 200 | |
2d120f83 | 201 | XtVaGetValues ((Widget) m_mainWidget, |
29006414 VZ |
202 | XmNvisibleItemCount, &count, |
203 | XmNitemCount, &length, | |
204 | NULL); | |
2d120f83 JS |
205 | if ((N + count) >= length) |
206 | N = length - count; | |
207 | XmListSetPos ((Widget) m_mainWidget, N + 1); | |
4bb6408c JS |
208 | } |
209 | ||
aa61d352 | 210 | void wxListBox::Delete(unsigned int n) |
4bb6408c | 211 | { |
2d120f83 | 212 | Widget listBox = (Widget) m_mainWidget; |
29006414 | 213 | |
aa61d352 | 214 | XmListDeletePos (listBox, n + 1); |
29006414 | 215 | |
aa61d352 | 216 | m_clientDataDict.Delete(n, HasClientObjectData()); |
2d120f83 | 217 | m_noItems --; |
4bb6408c JS |
218 | } |
219 | ||
ef41d80c | 220 | int wxListBox::DoAppend(const wxString& item) |
4bb6408c | 221 | { |
2d120f83 | 222 | Widget listBox = (Widget) m_mainWidget; |
29006414 | 223 | |
2d120f83 JS |
224 | int n; |
225 | XtVaGetValues (listBox, XmNitemCount, &n, NULL); | |
99ab3e3f | 226 | wxXmString text( item ); |
2d120f83 | 227 | // XmListAddItem(listBox, text, n + 1); |
99ab3e3f | 228 | XmListAddItemUnselected (listBox, text(), 0); |
29006414 | 229 | |
2d120f83 JS |
230 | // It seems that if the list is cleared, we must re-ask for |
231 | // selection policy!! | |
99ab3e3f | 232 | SetSelectionPolicy(); |
29006414 | 233 | |
2d120f83 | 234 | m_noItems ++; |
ef41d80c MB |
235 | |
236 | return GetCount() - 1; | |
4bb6408c JS |
237 | } |
238 | ||
ef41d80c | 239 | void wxListBox::DoSetItems(const wxArrayString& items, void** clientData) |
4bb6408c | 240 | { |
2d120f83 | 241 | Widget listBox = (Widget) m_mainWidget; |
99ab3e3f MB |
242 | |
243 | if( HasClientObjectData() ) | |
244 | m_clientDataDict.DestroyData(); | |
ef41d80c | 245 | |
ef41d80c | 246 | XmString *text = new XmString[items.GetCount()]; |
aa61d352 | 247 | unsigned int i; |
ef41d80c | 248 | for (i = 0; i < items.GetCount(); ++i) |
d3a80c92 | 249 | text[i] = wxStringToXmString (items[i]); |
29006414 | 250 | |
ef41d80c MB |
251 | if ( clientData ) |
252 | for (i = 0; i < items.GetCount(); ++i) | |
96be256b | 253 | m_clientDataDict.Set(i, (wxClientData*)clientData[i], false); |
ef41d80c MB |
254 | |
255 | XmListAddItems (listBox, text, items.GetCount(), 0); | |
256 | for (i = 0; i < items.GetCount(); i++) | |
257 | XmStringFree (text[i]); | |
258 | delete[] text; | |
29006414 | 259 | |
2d120f83 JS |
260 | // It seems that if the list is cleared, we must re-ask for |
261 | // selection policy!! | |
99ab3e3f | 262 | SetSelectionPolicy(); |
29006414 | 263 | |
ef41d80c | 264 | m_noItems = items.GetCount(); |
4bb6408c JS |
265 | } |
266 | ||
9b1bd0c6 | 267 | int wxDoFindStringInList(Widget w, const wxString& s) |
4bb6408c | 268 | { |
99ab3e3f | 269 | wxXmString str( s ); |
2d120f83 JS |
270 | int *positions = NULL; |
271 | int no_positions = 0; | |
9b1bd0c6 | 272 | bool success = XmListGetMatchPos (w, str(), |
ef41d80c | 273 | &positions, &no_positions); |
99ab3e3f | 274 | |
26564cf2 | 275 | if (success && positions) |
f97c9854 | 276 | { |
2d120f83 | 277 | int pos = positions[0]; |
26564cf2 | 278 | XtFree ((char *) positions); |
2d120f83 | 279 | return pos - 1; |
f97c9854 | 280 | } |
2d120f83 JS |
281 | else |
282 | return -1; | |
4bb6408c JS |
283 | } |
284 | ||
355b4d3d | 285 | int wxListBox::FindString(const wxString& s, bool WXUNUSED(bCase)) const |
9b1bd0c6 | 286 | { |
11e62fe6 WS |
287 | // FIXME: back to base class for not supported value of bCase |
288 | ||
9b1bd0c6 MB |
289 | return wxDoFindStringInList( (Widget)m_mainWidget, s ); |
290 | } | |
291 | ||
4bb6408c JS |
292 | void wxListBox::Clear() |
293 | { | |
2d120f83 JS |
294 | if (m_noItems <= 0) |
295 | return; | |
29006414 | 296 | |
99ab3e3f | 297 | wxSizeKeeper sk( this ); |
2d120f83 | 298 | Widget listBox = (Widget) m_mainWidget; |
29006414 | 299 | |
2d120f83 | 300 | XmListDeleteAllItems (listBox); |
99ab3e3f MB |
301 | if( HasClientObjectData() ) |
302 | m_clientDataDict.DestroyData(); | |
29006414 | 303 | |
99ab3e3f | 304 | sk.Restore(); |
29006414 | 305 | |
2d120f83 | 306 | m_noItems = 0; |
4bb6408c JS |
307 | } |
308 | ||
c6179a84 | 309 | void wxListBox::DoSetSelection(int N, bool select) |
4bb6408c | 310 | { |
96be256b | 311 | m_inSetValue = true; |
2d120f83 | 312 | if (select) |
f97c9854 | 313 | { |
29006414 VZ |
314 | #if 0 |
315 | if (m_windowStyle & wxLB_MULTIPLE) | |
316 | { | |
317 | int *selections = NULL; | |
318 | int n = GetSelections (&selections); | |
319 | ||
ef41d80c MB |
320 | // This hack is supposed to work, to make it possible |
321 | // to select more than one item, but it DOESN'T under Motif 1.1. | |
29006414 | 322 | |
ef41d80c MB |
323 | XtVaSetValues ((Widget) m_mainWidget, |
324 | XmNselectionPolicy, XmMULTIPLE_SELECT, | |
325 | NULL); | |
29006414 VZ |
326 | |
327 | int i; | |
328 | for (i = 0; i < n; i++) | |
ef41d80c | 329 | XmListSelectPos ((Widget) m_mainWidget, |
96be256b | 330 | selections[i] + 1, False); |
29006414 | 331 | |
96be256b | 332 | XmListSelectPos ((Widget) m_mainWidget, N + 1, False); |
29006414 | 333 | |
ef41d80c MB |
334 | XtVaSetValues ((Widget) m_mainWidget, |
335 | XmNselectionPolicy, XmEXTENDED_SELECT, | |
336 | NULL); | |
29006414 VZ |
337 | } |
338 | else | |
339 | #endif // 0 | |
96be256b | 340 | XmListSelectPos ((Widget) m_mainWidget, N + 1, False); |
29006414 | 341 | |
f97c9854 | 342 | } |
2d120f83 JS |
343 | else |
344 | XmListDeselectPos ((Widget) m_mainWidget, N + 1); | |
29006414 | 345 | |
96be256b | 346 | m_inSetValue = false; |
4bb6408c JS |
347 | } |
348 | ||
d7d38ea4 | 349 | bool wxListBox::IsSelected(int N) const |
4bb6408c | 350 | { |
2d120f83 JS |
351 | // In Motif, no simple way to determine if the item is selected. |
352 | wxArrayInt theSelections; | |
353 | int count = GetSelections (theSelections); | |
354 | if (count == 0) | |
96be256b | 355 | return false; |
2d120f83 JS |
356 | else |
357 | { | |
358 | int j; | |
359 | for (j = 0; j < count; j++) | |
360 | if (theSelections[j] == N) | |
96be256b | 361 | return true; |
2d120f83 | 362 | } |
96be256b | 363 | return false; |
4bb6408c JS |
364 | } |
365 | ||
aa61d352 | 366 | void wxListBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData) |
ef41d80c | 367 | { |
96be256b | 368 | m_clientDataDict.Set(n, clientData, false); |
ef41d80c MB |
369 | } |
370 | ||
aa61d352 | 371 | wxClientData* wxListBox::DoGetItemClientObject(unsigned int n) const |
4bb6408c | 372 | { |
99ab3e3f | 373 | return m_clientDataDict.Get(n); |
4bb6408c JS |
374 | } |
375 | ||
aa61d352 | 376 | void *wxListBox::DoGetItemClientData(unsigned int n) const |
4bb6408c | 377 | { |
aa61d352 | 378 | return (void*)m_clientDataDict.Get(n); |
4bb6408c JS |
379 | } |
380 | ||
aa61d352 | 381 | void wxListBox::DoSetItemClientData(unsigned int n, void *Client_data) |
4bb6408c | 382 | { |
aa61d352 | 383 | m_clientDataDict.Set(n, (wxClientData*)Client_data, false); |
4bb6408c JS |
384 | } |
385 | ||
386 | // Return number of selections and an array of selected integers | |
387 | int wxListBox::GetSelections(wxArrayInt& aSelections) const | |
388 | { | |
2d120f83 | 389 | aSelections.Empty(); |
29006414 | 390 | |
2d120f83 JS |
391 | Widget listBox = (Widget) m_mainWidget; |
392 | int *posList = NULL; | |
393 | int posCnt = 0; | |
394 | bool flag = XmListGetSelectedPos (listBox, &posList, &posCnt); | |
395 | if (flag) | |
396 | { | |
397 | if (posCnt > 0) | |
398 | { | |
399 | aSelections.Alloc(posCnt); | |
29006414 | 400 | |
2d120f83 JS |
401 | int i; |
402 | for (i = 0; i < posCnt; i++) | |
403 | aSelections.Add(posList[i] - 1); | |
29006414 | 404 | |
2d120f83 JS |
405 | XtFree ((char *) posList); |
406 | return posCnt; | |
407 | } | |
408 | else | |
409 | return 0; | |
4bb6408c | 410 | } |
2d120f83 JS |
411 | else |
412 | return 0; | |
4bb6408c JS |
413 | } |
414 | ||
415 | // Get single selection, for single choice list items | |
9b1bd0c6 | 416 | int wxDoGetSelectionInList(Widget listBox) |
4bb6408c | 417 | { |
f97c9854 JS |
418 | int *posList = NULL; |
419 | int posCnt = 0; | |
420 | bool flag = XmListGetSelectedPos (listBox, &posList, &posCnt); | |
421 | if (flag) | |
422 | { | |
423 | int id = -1; | |
424 | if (posCnt > 0) | |
425 | id = posList[0] - 1; | |
426 | XtFree ((char *) posList); | |
427 | return id; | |
428 | } | |
429 | else | |
430 | return -1; | |
4bb6408c JS |
431 | } |
432 | ||
9b1bd0c6 MB |
433 | int wxListBox::GetSelection() const |
434 | { | |
435 | return wxDoGetSelectionInList((Widget) m_mainWidget); | |
436 | } | |
437 | ||
4bb6408c | 438 | // Find string for position |
e1aae528 | 439 | wxString wxDoGetStringInList( Widget listBox, int n ) |
4bb6408c | 440 | { |
f97c9854 | 441 | XmString *strlist; |
e1aae528 MB |
442 | int count; |
443 | XtVaGetValues( listBox, | |
444 | XmNitemCount, &count, | |
445 | XmNitems, &strlist, | |
446 | NULL ); | |
d40708e2 | 447 | if( n < count && n >= 0 ) |
e1aae528 | 448 | return wxXmStringToString( strlist[n] ); |
f97c9854 JS |
449 | else |
450 | return wxEmptyString; | |
4bb6408c JS |
451 | } |
452 | ||
aa61d352 | 453 | wxString wxListBox::GetString(unsigned int n) const |
e1aae528 MB |
454 | { |
455 | return wxDoGetStringInList( (Widget)m_mainWidget, n ); | |
456 | } | |
457 | ||
aa61d352 | 458 | void wxListBox::DoInsertItems(const wxArrayString& items, unsigned int pos) |
4bb6408c | 459 | { |
f97c9854 | 460 | Widget listBox = (Widget) m_mainWidget; |
29006414 | 461 | |
ef41d80c | 462 | XmString *text = new XmString[items.GetCount()]; |
aa61d352 | 463 | unsigned int i; |
2d120f83 JS |
464 | // Steve Hammes: Motif 1.1 compatibility |
465 | // #if XmVersion > 1100 | |
466 | // Corrected by Sergey Krasnov from Steve Hammes' code | |
f97c9854 | 467 | #if XmVersion > 1001 |
ef41d80c | 468 | for (i = 0; i < items.GetCount(); i++) |
d3a80c92 | 469 | text[i] = wxStringToXmString(items[i]); |
ef41d80c | 470 | XmListAddItemsUnselected(listBox, text, items.GetCount(), pos+1); |
f97c9854 | 471 | #else |
ef41d80c | 472 | for (i = 0; i < items.GetCount(); i++) |
f97c9854 | 473 | { |
d3a80c92 | 474 | text[i] = wxStringToXmString(items[i]); |
ef41d80c MB |
475 | // Another Sergey correction |
476 | XmListAddItemUnselected(listBox, text[i], pos+i+1); | |
f97c9854 JS |
477 | } |
478 | #endif | |
ef41d80c | 479 | for (i = 0; i < items.GetCount(); i++) |
f97c9854 | 480 | XmStringFree(text[i]); |
f97c9854 | 481 | delete[] text; |
29006414 | 482 | |
f97c9854 JS |
483 | // It seems that if the list is cleared, we must re-ask for |
484 | // selection policy!! | |
99ab3e3f | 485 | SetSelectionPolicy(); |
29006414 | 486 | |
ef41d80c | 487 | m_noItems += items.GetCount(); |
4bb6408c JS |
488 | } |
489 | ||
aa61d352 | 490 | void wxListBox::SetString(unsigned int n, const wxString& s) |
4bb6408c | 491 | { |
99ab3e3f | 492 | wxSizeKeeper sk( this ); |
f97c9854 | 493 | Widget listBox = (Widget) m_mainWidget; |
29006414 | 494 | |
99ab3e3f | 495 | wxXmString text( s ); |
29006414 VZ |
496 | |
497 | // delete the item and add it again. | |
498 | // FIXME isn't there a way to change it in place? | |
aa61d352 VZ |
499 | XmListDeletePos (listBox, n+1); |
500 | XmListAddItem (listBox, text(), n+1); | |
29006414 | 501 | |
99ab3e3f | 502 | sk.Restore(); |
4bb6408c JS |
503 | } |
504 | ||
4bb6408c JS |
505 | void wxListBox::Command (wxCommandEvent & event) |
506 | { | |
687706f5 KH |
507 | if (event.GetExtraLong()) |
508 | SetSelection (event.GetInt()); | |
f97c9854 JS |
509 | else |
510 | { | |
687706f5 | 511 | Deselect (event.GetInt()); |
f97c9854 JS |
512 | return; |
513 | } | |
514 | ProcessCommand (event); | |
515 | } | |
516 | ||
af111fc3 | 517 | void wxListBoxCallback (Widget WXUNUSED(w), XtPointer clientData, |
2d120f83 | 518 | XmListCallbackStruct * cbs) |
f97c9854 | 519 | { |
f97c9854 | 520 | wxListBox *item = (wxListBox *) clientData; |
29006414 | 521 | |
a4294b78 | 522 | if (item->InSetValue()) |
f97c9854 | 523 | return; |
29006414 | 524 | |
ef41d80c MB |
525 | wxEventType evtType; |
526 | ||
527 | if( cbs->reason == XmCR_DEFAULT_ACTION ) | |
528 | evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED; | |
529 | else | |
530 | evtType = wxEVT_COMMAND_LISTBOX_SELECTED; | |
531 | ||
532 | int n = cbs->item_position - 1; | |
533 | wxCommandEvent event (evtType, item->GetId()); | |
534 | if ( item->HasClientObjectData() ) | |
535 | event.SetClientObject( item->GetClientObject(n) ); | |
536 | else if ( item->HasClientUntypedData() ) | |
537 | event.SetClientData( item->GetClientData(n) ); | |
687706f5 | 538 | event.SetInt(n); |
96be256b | 539 | event.SetExtraLong(true); |
ef41d80c MB |
540 | event.SetEventObject(item); |
541 | event.SetString( item->GetString( n ) ); | |
542 | ||
543 | int x = -1; | |
2b5f62a0 | 544 | if( NULL != cbs->event && cbs->event->type == ButtonRelease ) |
ef41d80c MB |
545 | { |
546 | XButtonEvent* evt = (XButtonEvent*)cbs->event; | |
547 | ||
548 | x = evt->x; | |
549 | } | |
550 | ||
f97c9854 | 551 | switch (cbs->reason) |
4bb6408c | 552 | { |
2d120f83 JS |
553 | case XmCR_MULTIPLE_SELECT: |
554 | case XmCR_BROWSE_SELECT: | |
ef41d80c MB |
555 | #if wxUSE_CHECKLISTBOX |
556 | item->DoToggleItem( n, x ); | |
557 | #endif | |
558 | case XmCR_DEFAULT_ACTION: | |
559 | item->GetEventHandler()->ProcessEvent(event); | |
560 | break; | |
2d120f83 | 561 | case XmCR_EXTENDED_SELECT: |
ef41d80c | 562 | switch (cbs->selection_type) |
f97c9854 | 563 | { |
ef41d80c MB |
564 | case XmINITIAL: |
565 | case XmADDITION: | |
566 | case XmMODIFICATION: | |
567 | item->DoToggleItem( n, x ); | |
568 | item->GetEventHandler()->ProcessEvent(event); | |
f97c9854 JS |
569 | break; |
570 | } | |
ef41d80c | 571 | break; |
4bb6408c | 572 | } |
4bb6408c JS |
573 | } |
574 | ||
89c7e962 JS |
575 | WXWidget wxListBox::GetTopWidget() const |
576 | { | |
2d120f83 | 577 | return (WXWidget) XtParent( (Widget) m_mainWidget ); |
89c7e962 | 578 | } |
0d57be45 | 579 | |
0d57be45 JS |
580 | void wxListBox::ChangeBackgroundColour() |
581 | { | |
321db4b6 | 582 | wxWindow::ChangeBackgroundColour(); |
29006414 | 583 | |
02800301 JS |
584 | Widget parent = XtParent ((Widget) m_mainWidget); |
585 | Widget hsb, vsb; | |
29006414 | 586 | |
02800301 | 587 | XtVaGetValues (parent, |
2d120f83 JS |
588 | XmNhorizontalScrollBar, &hsb, |
589 | XmNverticalScrollBar, &vsb, | |
590 | NULL); | |
29006414 | 591 | |
a91b47e8 JS |
592 | /* TODO: should scrollbars be affected? Should probably have separate |
593 | * function to change them (by default, taken from wxSystemSettings) | |
2d120f83 | 594 | */ |
a756f210 | 595 | wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); |
96be256b MB |
596 | wxDoChangeBackgroundColour((WXWidget) hsb, backgroundColour, true); |
597 | wxDoChangeBackgroundColour((WXWidget) vsb, backgroundColour, true); | |
15d5ab67 JS |
598 | |
599 | XtVaSetValues (hsb, | |
600 | XmNtroughColor, backgroundColour.AllocColour(XtDisplay(hsb)), | |
601 | NULL); | |
602 | XtVaSetValues (vsb, | |
603 | XmNtroughColor, backgroundColour.AllocColour(XtDisplay(vsb)), | |
604 | NULL); | |
29006414 | 605 | |
73d33f1a | 606 | // MBN: why change parent's background? It looks really ugly. |
96be256b | 607 | // wxDoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, true); |
0d57be45 JS |
608 | } |
609 | ||
610 | void wxListBox::ChangeForegroundColour() | |
611 | { | |
321db4b6 | 612 | wxWindow::ChangeForegroundColour(); |
29006414 | 613 | |
02800301 JS |
614 | Widget parent = XtParent ((Widget) m_mainWidget); |
615 | Widget hsb, vsb; | |
29006414 VZ |
616 | |
617 | XtVaGetValues(parent, | |
618 | XmNhorizontalScrollBar, &hsb, | |
619 | XmNverticalScrollBar, &vsb, | |
620 | NULL); | |
621 | ||
622 | /* TODO: should scrollbars be affected? Should probably have separate | |
623 | function to change them (by default, taken from wxSystemSettings) | |
624 | ||
a8680e3e MB |
625 | wxDoChangeForegroundColour((WXWidget) hsb, m_foregroundColour); |
626 | wxDoChangeForegroundColour((WXWidget) vsb, m_foregroundColour); | |
627 | wxDoChangeForegroundColour((WXWidget) parent, m_foregroundColour); | |
02800301 | 628 | */ |
0d57be45 JS |
629 | } |
630 | ||
aa61d352 | 631 | unsigned int wxListBox::GetCount() const |
6adaedf0 | 632 | { |
ef41d80c | 633 | return m_noItems; |
6adaedf0 | 634 | } |
e1aae528 MB |
635 | |
636 | #define LIST_SCROLL_SPACING 6 | |
637 | ||
638 | wxSize wxDoGetListBoxBestSize( Widget listWidget, const wxWindow* window ) | |
639 | { | |
640 | int max; | |
641 | Dimension spacing, highlight, xmargin, ymargin, shadow; | |
642 | int width = 0; | |
643 | int x, y; | |
644 | ||
645 | XtVaGetValues( listWidget, | |
646 | XmNitemCount, &max, | |
647 | XmNlistSpacing, &spacing, | |
648 | XmNhighlightThickness, &highlight, | |
649 | XmNlistMarginWidth, &xmargin, | |
650 | XmNlistMarginHeight, &ymargin, | |
651 | XmNshadowThickness, &shadow, | |
652 | NULL ); | |
653 | ||
654 | for( size_t i = 0; i < (size_t)max; ++i ) | |
655 | { | |
656 | window->GetTextExtent( wxDoGetStringInList( listWidget, i ), &x, &y ); | |
657 | width = wxMax( width, x ); | |
658 | } | |
659 | ||
660 | // use some arbitrary value if there are no strings | |
661 | if( width == 0 ) | |
662 | width = 100; | |
663 | ||
664 | // get my | |
665 | window->GetTextExtent( "v", &x, &y ); | |
666 | ||
667 | // make it a little larger than widest string, plus the scrollbar | |
668 | width += wxSystemSettings::GetMetric( wxSYS_VSCROLL_X ) | |
669 | + 2 * highlight + LIST_SCROLL_SPACING + 2 * xmargin + 2 * shadow; | |
670 | ||
671 | // at least 3 items, at most 10 | |
672 | int height = wxMax( 3, wxMin( 10, max ) ) * | |
673 | ( y + spacing + 2 * highlight ) + 2 * ymargin + 2 * shadow; | |
674 | ||
675 | return wxSize( width, height ); | |
676 | } | |
677 | ||
678 | wxSize wxListBox::DoGetBestSize() const | |
679 | { | |
680 | return wxDoGetListBoxBestSize( (Widget)m_mainWidget, this ); | |
681 | } | |
8228b893 WS |
682 | |
683 | #endif // wxUSE_LISTBOX |