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