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