1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/listbox.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
17 #include "wx/listbox.h"
20 #include "wx/dynarray.h"
26 #define XtParent XTPARENT
27 #define XtDisplay XTDISPLAY
30 #include "wx/settings.h"
31 #include "wx/arrstr.h"
34 #pragma message disable nosimpint
38 #pragma message enable nosimpint
40 #include "wx/motif/private.h"
42 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
44 static void wxListBoxCallback(Widget w
,
46 XmListCallbackStruct
* cbs
);
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // helper class to reduce code duplication
58 wxSizeKeeper( wxWindow
* w
)
61 m_w
->GetSize( &m_x
, &m_y
);
68 m_w
->GetSize( &x
, &y
);
69 if( x
!= m_x
|| y
!= m_y
)
70 m_w
->SetSize( -1, -1, m_x
, m_y
);
74 // ============================================================================
75 // list box control implementation
76 // ============================================================================
79 wxListBox::wxListBox()
84 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
87 int n
, const wxString choices
[],
89 const wxValidator
& validator
,
92 if( !wxControl::CreateControl( parent
, id
, pos
, size
, style
,
96 m_noItems
= (unsigned int)n
;
97 m_backgroundColour
= * wxWHITE
;
99 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
100 Display
* dpy
= XtDisplay(parentWidget
);
104 XtSetArg( args
[count
], XmNlistSizePolicy
, XmCONSTANT
); ++count
;
105 XtSetArg( args
[count
], XmNselectionPolicy
,
106 ( m_windowStyle
& wxLB_MULTIPLE
) ? XmMULTIPLE_SELECT
:
107 ( m_windowStyle
& wxLB_EXTENDED
) ? XmEXTENDED_SELECT
:
112 XtSetArg( args
[count
],
113 (String
)wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
) );
116 if( m_windowStyle
& wxLB_ALWAYS_SB
)
118 XtSetArg( args
[count
], XmNscrollBarDisplayPolicy
, XmSTATIC
);
123 XmCreateScrolledList(parentWidget
,
124 wxConstCast(name
.c_str(), char), args
, count
);
126 m_mainWidget
= (WXWidget
) listWidget
;
130 XtManageChild (listWidget
);
132 wxSize best
= GetBestSize();
133 if( size
.x
!= -1 ) best
.x
= size
.x
;
134 if( size
.y
!= -1 ) best
.y
= size
.y
;
136 XtAddCallback (listWidget
,
137 XmNbrowseSelectionCallback
,
138 (XtCallbackProc
) wxListBoxCallback
,
140 XtAddCallback (listWidget
,
141 XmNextendedSelectionCallback
,
142 (XtCallbackProc
) wxListBoxCallback
,
144 XtAddCallback (listWidget
,
145 XmNmultipleSelectionCallback
,
146 (XtCallbackProc
) wxListBoxCallback
,
148 XtAddCallback (listWidget
,
149 XmNdefaultActionCallback
,
150 (XtCallbackProc
) wxListBoxCallback
,
153 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
154 pos
.x
, pos
.y
, best
.x
, best
.y
);
156 ChangeBackgroundColour();
161 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
164 const wxArrayString
& choices
,
166 const wxValidator
& validator
,
167 const wxString
& name
)
169 wxCArrayString
chs(choices
);
170 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
171 style
, validator
, name
);
174 wxListBox::~wxListBox()
176 if( HasClientObjectData() )
177 m_clientDataDict
.DestroyData();
180 void wxListBox::SetSelectionPolicy()
182 Widget listBox
= (Widget
)m_mainWidget
;
185 XtSetArg( args
[0], XmNlistSizePolicy
, XmCONSTANT
);
187 XtSetArg( args
[1], XmNselectionPolicy
,
188 ( m_windowStyle
& wxLB_MULTIPLE
) ? XmMULTIPLE_SELECT
:
189 ( m_windowStyle
& wxLB_EXTENDED
) ? XmEXTENDED_SELECT
:
192 XtSetValues( listBox
, args
, 2 );
195 void wxListBox::DoSetFirstItem( int N
)
202 XtVaGetValues ((Widget
) m_mainWidget
,
203 XmNvisibleItemCount
, &count
,
204 XmNitemCount
, &length
,
206 if ((N
+ count
) >= length
)
208 XmListSetPos ((Widget
) m_mainWidget
, N
+ 1);
211 void wxListBox::Delete(unsigned int n
)
213 wxSizeKeeper
sk( this );
214 Widget listBox
= (Widget
) m_mainWidget
;
216 bool managed
= XtIsManaged(listBox
);
219 XtUnmanageChild (listBox
);
221 XmListDeletePos (listBox
, n
+ 1);
224 XtManageChild (listBox
);
227 m_clientDataDict
.Delete(n
, HasClientObjectData());
231 int wxListBox::DoAppend(const wxString
& item
)
233 wxSizeKeeper
sk( this );
234 Widget listBox
= (Widget
) m_mainWidget
;
236 bool managed
= XtIsManaged(listBox
);
239 XtUnmanageChild (listBox
);
241 XtVaGetValues (listBox
, XmNitemCount
, &n
, NULL
);
242 wxXmString
text( item
);
243 // XmListAddItem(listBox, text, n + 1);
244 XmListAddItemUnselected (listBox
, text(), 0);
246 // It seems that if the list is cleared, we must re-ask for
247 // selection policy!!
248 SetSelectionPolicy();
251 XtManageChild (listBox
);
256 return GetCount() - 1;
259 void wxListBox::DoSetItems(const wxArrayString
& items
, void** clientData
)
261 wxSizeKeeper
sk( this );
262 Widget listBox
= (Widget
) m_mainWidget
;
264 if( HasClientObjectData() )
265 m_clientDataDict
.DestroyData();
267 bool managed
= XtIsManaged(listBox
);
270 XtUnmanageChild (listBox
);
271 XmString
*text
= new XmString
[items
.GetCount()];
273 for (i
= 0; i
< items
.GetCount(); ++i
)
274 text
[i
] = wxStringToXmString (items
[i
]);
277 for (i
= 0; i
< items
.GetCount(); ++i
)
278 m_clientDataDict
.Set(i
, (wxClientData
*)clientData
[i
], false);
280 XmListAddItems (listBox
, text
, items
.GetCount(), 0);
281 for (i
= 0; i
< items
.GetCount(); i
++)
282 XmStringFree (text
[i
]);
285 // It seems that if the list is cleared, we must re-ask for
286 // selection policy!!
287 SetSelectionPolicy();
290 XtManageChild (listBox
);
294 m_noItems
= items
.GetCount();
297 int wxDoFindStringInList(Widget w
, const wxString
& s
)
300 int *positions
= NULL
;
301 int no_positions
= 0;
302 bool success
= XmListGetMatchPos (w
, str(),
303 &positions
, &no_positions
);
307 int pos
= positions
[0];
309 XtFree ((char *) positions
);
316 int wxListBox::FindString(const wxString
& s
, bool WXUNUSED(bCase
)) const
318 // FIXME: back to base class for not supported value of bCase
320 return wxDoFindStringInList( (Widget
)m_mainWidget
, s
);
323 void wxListBox::Clear()
328 wxSizeKeeper
sk( this );
329 Widget listBox
= (Widget
) m_mainWidget
;
331 XmListDeleteAllItems (listBox
);
332 if( HasClientObjectData() )
333 m_clientDataDict
.DestroyData();
340 void wxListBox::DoSetSelection(int N
, bool select
)
346 if (m_windowStyle
& wxLB_MULTIPLE
)
348 int *selections
= NULL
;
349 int n
= GetSelections (&selections
);
351 // This hack is supposed to work, to make it possible
352 // to select more than one item, but it DOESN'T under Motif 1.1.
354 XtVaSetValues ((Widget
) m_mainWidget
,
355 XmNselectionPolicy
, XmMULTIPLE_SELECT
,
359 for (i
= 0; i
< n
; i
++)
360 XmListSelectPos ((Widget
) m_mainWidget
,
361 selections
[i
] + 1, False
);
363 XmListSelectPos ((Widget
) m_mainWidget
, N
+ 1, False
);
365 XtVaSetValues ((Widget
) m_mainWidget
,
366 XmNselectionPolicy
, XmEXTENDED_SELECT
,
371 XmListSelectPos ((Widget
) m_mainWidget
, N
+ 1, False
);
375 XmListDeselectPos ((Widget
) m_mainWidget
, N
+ 1);
377 m_inSetValue
= false;
380 bool wxListBox::IsSelected(int N
) const
382 // In Motif, no simple way to determine if the item is selected.
383 wxArrayInt theSelections
;
384 int count
= GetSelections (theSelections
);
390 for (j
= 0; j
< count
; j
++)
391 if (theSelections
[j
] == N
)
397 void wxListBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
399 m_clientDataDict
.Set(n
, clientData
, false);
402 wxClientData
* wxListBox::DoGetItemClientObject(unsigned int n
) const
404 return m_clientDataDict
.Get(n
);
407 void *wxListBox::DoGetItemClientData(unsigned int n
) const
409 return (void*)m_clientDataDict
.Get(n
);
412 void wxListBox::DoSetItemClientData(unsigned int n
, void *Client_data
)
414 m_clientDataDict
.Set(n
, (wxClientData
*)Client_data
, false);
417 // Return number of selections and an array of selected integers
418 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
422 Widget listBox
= (Widget
) m_mainWidget
;
425 bool flag
= XmListGetSelectedPos (listBox
, &posList
, &posCnt
);
430 aSelections
.Alloc(posCnt
);
433 for (i
= 0; i
< posCnt
; i
++)
434 aSelections
.Add(posList
[i
] - 1);
436 XtFree ((char *) posList
);
446 // Get single selection, for single choice list items
447 int wxDoGetSelectionInList(Widget listBox
)
451 bool flag
= XmListGetSelectedPos (listBox
, &posList
, &posCnt
);
457 XtFree ((char *) posList
);
464 int wxListBox::GetSelection() const
466 return wxDoGetSelectionInList((Widget
) m_mainWidget
);
469 // Find string for position
470 wxString
wxDoGetStringInList( Widget listBox
, int n
)
474 XtVaGetValues( listBox
,
475 XmNitemCount
, &count
,
478 if( n
< count
&& n
>= 0 )
479 return wxXmStringToString( strlist
[n
] );
481 return wxEmptyString
;
484 wxString
wxListBox::GetString(unsigned int n
) const
486 return wxDoGetStringInList( (Widget
)m_mainWidget
, n
);
489 void wxListBox::DoInsertItems(const wxArrayString
& items
, unsigned int pos
)
491 wxSizeKeeper
sk( this );
492 Widget listBox
= (Widget
) m_mainWidget
;
494 bool managed
= XtIsManaged(listBox
);
497 XtUnmanageChild(listBox
);
499 XmString
*text
= new XmString
[items
.GetCount()];
501 // Steve Hammes: Motif 1.1 compatibility
502 // #if XmVersion > 1100
503 // Corrected by Sergey Krasnov from Steve Hammes' code
505 for (i
= 0; i
< items
.GetCount(); i
++)
506 text
[i
] = wxStringToXmString(items
[i
]);
507 XmListAddItemsUnselected(listBox
, text
, items
.GetCount(), pos
+1);
509 for (i
= 0; i
< items
.GetCount(); i
++)
511 text
[i
] = wxStringToXmString(items
[i
]);
512 // Another Sergey correction
513 XmListAddItemUnselected(listBox
, text
[i
], pos
+i
+1);
516 for (i
= 0; i
< items
.GetCount(); i
++)
517 XmStringFree(text
[i
]);
520 // It seems that if the list is cleared, we must re-ask for
521 // selection policy!!
522 SetSelectionPolicy();
525 XtManageChild(listBox
);
529 m_noItems
+= items
.GetCount();
532 void wxListBox::SetString(unsigned int n
, const wxString
& s
)
534 wxSizeKeeper
sk( this );
535 Widget listBox
= (Widget
) m_mainWidget
;
537 wxXmString
text( s
);
539 // delete the item and add it again.
540 // FIXME isn't there a way to change it in place?
541 XmListDeletePos (listBox
, n
+1);
542 XmListAddItem (listBox
, text(), n
+1);
547 void wxListBox::Command (wxCommandEvent
& event
)
549 if (event
.GetExtraLong())
550 SetSelection (event
.GetInt());
553 Deselect (event
.GetInt());
556 ProcessCommand (event
);
559 void wxListBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
560 XmListCallbackStruct
* cbs
)
562 wxListBox
*item
= (wxListBox
*) clientData
;
564 if (item
->InSetValue())
569 if( cbs
->reason
== XmCR_DEFAULT_ACTION
)
570 evtType
= wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
;
572 evtType
= wxEVT_COMMAND_LISTBOX_SELECTED
;
574 int n
= cbs
->item_position
- 1;
575 wxCommandEvent
event (evtType
, item
->GetId());
576 if ( item
->HasClientObjectData() )
577 event
.SetClientObject( item
->GetClientObject(n
) );
578 else if ( item
->HasClientUntypedData() )
579 event
.SetClientData( item
->GetClientData(n
) );
581 event
.SetExtraLong(true);
582 event
.SetEventObject(item
);
583 event
.SetString( item
->GetString( n
) );
586 if( NULL
!= cbs
->event
&& cbs
->event
->type
== ButtonRelease
)
588 XButtonEvent
* evt
= (XButtonEvent
*)cbs
->event
;
595 case XmCR_MULTIPLE_SELECT
:
596 case XmCR_BROWSE_SELECT
:
597 #if wxUSE_CHECKLISTBOX
598 item
->DoToggleItem( n
, x
);
600 case XmCR_DEFAULT_ACTION
:
601 item
->GetEventHandler()->ProcessEvent(event
);
603 case XmCR_EXTENDED_SELECT
:
604 switch (cbs
->selection_type
)
609 item
->DoToggleItem( n
, x
);
610 item
->GetEventHandler()->ProcessEvent(event
);
617 WXWidget
wxListBox::GetTopWidget() const
619 return (WXWidget
) XtParent( (Widget
) m_mainWidget
);
622 void wxListBox::ChangeBackgroundColour()
624 wxWindow::ChangeBackgroundColour();
626 Widget parent
= XtParent ((Widget
) m_mainWidget
);
629 XtVaGetValues (parent
,
630 XmNhorizontalScrollBar
, &hsb
,
631 XmNverticalScrollBar
, &vsb
,
634 /* TODO: should scrollbars be affected? Should probably have separate
635 * function to change them (by default, taken from wxSystemSettings)
637 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
638 wxDoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, true);
639 wxDoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, true);
642 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(hsb
)),
645 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(vsb
)),
648 // MBN: why change parent's background? It looks really ugly.
649 // wxDoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, true);
652 void wxListBox::ChangeForegroundColour()
654 wxWindow::ChangeForegroundColour();
656 Widget parent
= XtParent ((Widget
) m_mainWidget
);
659 XtVaGetValues(parent
,
660 XmNhorizontalScrollBar
, &hsb
,
661 XmNverticalScrollBar
, &vsb
,
664 /* TODO: should scrollbars be affected? Should probably have separate
665 function to change them (by default, taken from wxSystemSettings)
667 wxDoChangeForegroundColour((WXWidget) hsb, m_foregroundColour);
668 wxDoChangeForegroundColour((WXWidget) vsb, m_foregroundColour);
669 wxDoChangeForegroundColour((WXWidget) parent, m_foregroundColour);
673 unsigned int wxListBox::GetCount() const
678 #define LIST_SCROLL_SPACING 6
680 wxSize
wxDoGetListBoxBestSize( Widget listWidget
, const wxWindow
* window
)
683 Dimension spacing
, highlight
, xmargin
, ymargin
, shadow
;
687 XtVaGetValues( listWidget
,
689 XmNlistSpacing
, &spacing
,
690 XmNhighlightThickness
, &highlight
,
691 XmNlistMarginWidth
, &xmargin
,
692 XmNlistMarginHeight
, &ymargin
,
693 XmNshadowThickness
, &shadow
,
696 for( size_t i
= 0; i
< (size_t)max
; ++i
)
698 window
->GetTextExtent( wxDoGetStringInList( listWidget
, i
), &x
, &y
);
699 width
= wxMax( width
, x
);
702 // use some arbitrary value if there are no strings
707 window
->GetTextExtent( "v", &x
, &y
);
709 // make it a little larger than widest string, plus the scrollbar
710 width
+= wxSystemSettings::GetMetric( wxSYS_VSCROLL_X
)
711 + 2 * highlight
+ LIST_SCROLL_SPACING
+ 2 * xmargin
+ 2 * shadow
;
713 // at least 3 items, at most 10
714 int height
= wxMax( 3, wxMin( 10, max
) ) *
715 ( y
+ spacing
+ 2 * highlight
) + 2 * ymargin
+ 2 * shadow
;
717 return wxSize( width
, height
);
720 wxSize
wxListBox::DoGetBestSize() const
722 return wxDoGetListBoxBestSize( (Widget
)m_mainWidget
, this );
725 #endif // wxUSE_LISTBOX