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"
23 #include "wx/settings.h"
24 #include "wx/arrstr.h"
28 #define XtParent XTPARENT
29 #define XtDisplay XTDISPLAY
33 #pragma message disable nosimpint
37 #pragma message enable nosimpint
39 #include "wx/motif/private.h"
41 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
43 static void wxListBoxCallback(Widget w
,
45 XmListCallbackStruct
* cbs
);
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // helper class to reduce code duplication
58 wxSizeKeeper( wxWindow
* w
)
61 m_wnd
->GetSize( &m_w
, &m_h
);
62 m_wnd
->GetPosition( &m_x
, &m_y
);
69 m_wnd
->GetSize( &x
, &y
);
70 if( x
!= m_x
|| y
!= m_y
)
71 m_wnd
->SetSize( m_x
, m_y
, m_w
, m_h
);
75 // ============================================================================
76 // list box control implementation
77 // ============================================================================
80 wxListBox::wxListBox()
85 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
88 int n
, const wxString choices
[],
90 const wxValidator
& validator
,
93 if( !wxControl::CreateControl( parent
, id
, pos
, size
, style
,
98 m_noItems
= (unsigned int)n
;
100 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
101 Display
* dpy
= XtDisplay(parentWidget
);
105 XtSetArg( args
[count
], XmNlistSizePolicy
, XmCONSTANT
); ++count
;
106 XtSetArg( args
[count
], XmNselectionPolicy
,
107 ( m_windowStyle
& wxLB_MULTIPLE
) ? XmMULTIPLE_SELECT
:
108 ( m_windowStyle
& wxLB_EXTENDED
) ? XmEXTENDED_SELECT
:
113 XtSetArg( args
[count
],
114 (String
)wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
) );
117 if( m_windowStyle
& wxLB_ALWAYS_SB
)
119 XtSetArg( args
[count
], XmNscrollBarDisplayPolicy
, XmSTATIC
);
124 XmCreateScrolledList(parentWidget
,
125 wxConstCast(name
.mb_str(), char), args
, count
);
127 m_mainWidget
= (WXWidget
) listWidget
;
131 XtManageChild (listWidget
);
133 wxSize best
= GetBestSize();
134 if( size
.x
!= -1 ) best
.x
= size
.x
;
135 if( size
.y
!= -1 ) best
.y
= size
.y
;
137 XtAddCallback (listWidget
,
138 XmNbrowseSelectionCallback
,
139 (XtCallbackProc
) wxListBoxCallback
,
141 XtAddCallback (listWidget
,
142 XmNextendedSelectionCallback
,
143 (XtCallbackProc
) wxListBoxCallback
,
145 XtAddCallback (listWidget
,
146 XmNmultipleSelectionCallback
,
147 (XtCallbackProc
) wxListBoxCallback
,
149 XtAddCallback (listWidget
,
150 XmNdefaultActionCallback
,
151 (XtCallbackProc
) wxListBoxCallback
,
155 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
156 pos
.x
, pos
.y
, best
.x
, best
.y
);
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 Widget listBox
= (Widget
) m_mainWidget
;
215 XmListDeletePos (listBox
, n
+ 1);
217 m_clientDataDict
.Delete(n
, HasClientObjectData());
221 int wxListBox::DoAppend(const wxString
& item
)
223 Widget listBox
= (Widget
) m_mainWidget
;
226 XtVaGetValues (listBox
, XmNitemCount
, &n
, NULL
);
227 wxXmString
text( item
);
228 // XmListAddItem(listBox, text, n + 1);
229 XmListAddItemUnselected (listBox
, text(), 0);
231 // It seems that if the list is cleared, we must re-ask for
232 // selection policy!!
233 SetSelectionPolicy();
237 return GetCount() - 1;
240 void wxListBox::DoSetItems(const wxArrayString
& items
, void** clientData
)
242 Widget listBox
= (Widget
) m_mainWidget
;
244 if( HasClientObjectData() )
245 m_clientDataDict
.DestroyData();
247 XmString
*text
= new XmString
[items
.GetCount()];
249 for (i
= 0; i
< items
.GetCount(); ++i
)
250 text
[i
] = wxStringToXmString (items
[i
]);
253 for (i
= 0; i
< items
.GetCount(); ++i
)
254 m_clientDataDict
.Set(i
, (wxClientData
*)clientData
[i
], false);
256 XmListAddItems (listBox
, text
, items
.GetCount(), 0);
257 for (i
= 0; i
< items
.GetCount(); i
++)
258 XmStringFree (text
[i
]);
261 // It seems that if the list is cleared, we must re-ask for
262 // selection policy!!
263 SetSelectionPolicy();
265 m_noItems
= items
.GetCount();
268 int wxDoFindStringInList(Widget w
, const wxString
& s
)
271 int *positions
= NULL
;
272 int no_positions
= 0;
273 bool success
= XmListGetMatchPos (w
, str(),
274 &positions
, &no_positions
);
276 if (success
&& positions
)
278 int pos
= positions
[0];
279 XtFree ((char *) positions
);
286 int wxListBox::FindString(const wxString
& s
, bool WXUNUSED(bCase
)) const
288 // FIXME: back to base class for not supported value of bCase
290 return wxDoFindStringInList( (Widget
)m_mainWidget
, s
);
293 void wxListBox::Clear()
298 wxSizeKeeper
sk( this );
299 Widget listBox
= (Widget
) m_mainWidget
;
301 XmListDeleteAllItems (listBox
);
302 if( HasClientObjectData() )
303 m_clientDataDict
.DestroyData();
310 void wxListBox::DoSetSelection(int N
, bool select
)
316 if (m_windowStyle
& wxLB_MULTIPLE
)
318 int *selections
= NULL
;
319 int n
= GetSelections (&selections
);
321 // This hack is supposed to work, to make it possible
322 // to select more than one item, but it DOESN'T under Motif 1.1.
324 XtVaSetValues ((Widget
) m_mainWidget
,
325 XmNselectionPolicy
, XmMULTIPLE_SELECT
,
329 for (i
= 0; i
< n
; i
++)
330 XmListSelectPos ((Widget
) m_mainWidget
,
331 selections
[i
] + 1, False
);
333 XmListSelectPos ((Widget
) m_mainWidget
, N
+ 1, False
);
335 XtVaSetValues ((Widget
) m_mainWidget
,
336 XmNselectionPolicy
, XmEXTENDED_SELECT
,
341 XmListSelectPos ((Widget
) m_mainWidget
, N
+ 1, False
);
345 XmListDeselectPos ((Widget
) m_mainWidget
, N
+ 1);
347 m_inSetValue
= false;
350 bool wxListBox::IsSelected(int N
) const
352 // In Motif, no simple way to determine if the item is selected.
353 wxArrayInt theSelections
;
354 int count
= GetSelections (theSelections
);
360 for (j
= 0; j
< count
; j
++)
361 if (theSelections
[j
] == N
)
367 void wxListBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
369 m_clientDataDict
.Set(n
, clientData
, false);
372 wxClientData
* wxListBox::DoGetItemClientObject(unsigned int n
) const
374 return m_clientDataDict
.Get(n
);
377 void *wxListBox::DoGetItemClientData(unsigned int n
) const
379 return (void*)m_clientDataDict
.Get(n
);
382 void wxListBox::DoSetItemClientData(unsigned int n
, void *Client_data
)
384 m_clientDataDict
.Set(n
, (wxClientData
*)Client_data
, false);
387 // Return number of selections and an array of selected integers
388 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
392 Widget listBox
= (Widget
) m_mainWidget
;
395 bool flag
= XmListGetSelectedPos (listBox
, &posList
, &posCnt
);
400 aSelections
.Alloc(posCnt
);
403 for (i
= 0; i
< posCnt
; i
++)
404 aSelections
.Add(posList
[i
] - 1);
406 XtFree ((char *) posList
);
416 // Get single selection, for single choice list items
417 int wxDoGetSelectionInList(Widget listBox
)
421 bool flag
= XmListGetSelectedPos (listBox
, &posList
, &posCnt
);
427 XtFree ((char *) posList
);
434 int wxListBox::GetSelection() const
436 return wxDoGetSelectionInList((Widget
) m_mainWidget
);
439 // Find string for position
440 wxString
wxDoGetStringInList( Widget listBox
, int n
)
444 XtVaGetValues( listBox
,
445 XmNitemCount
, &count
,
448 if( n
< count
&& n
>= 0 )
449 return wxXmStringToString( strlist
[n
] );
451 return wxEmptyString
;
454 wxString
wxListBox::GetString(unsigned int n
) const
456 return wxDoGetStringInList( (Widget
)m_mainWidget
, n
);
459 void wxListBox::DoInsertItems(const wxArrayString
& items
, unsigned int pos
)
461 Widget listBox
= (Widget
) m_mainWidget
;
463 XmString
*text
= new XmString
[items
.GetCount()];
465 // Steve Hammes: Motif 1.1 compatibility
466 // #if XmVersion > 1100
467 // Corrected by Sergey Krasnov from Steve Hammes' code
469 for (i
= 0; i
< items
.GetCount(); i
++)
470 text
[i
] = wxStringToXmString(items
[i
]);
471 XmListAddItemsUnselected(listBox
, text
, items
.GetCount(), pos
+1);
473 for (i
= 0; i
< items
.GetCount(); i
++)
475 text
[i
] = wxStringToXmString(items
[i
]);
476 // Another Sergey correction
477 XmListAddItemUnselected(listBox
, text
[i
], pos
+i
+1);
480 for (i
= 0; i
< items
.GetCount(); i
++)
481 XmStringFree(text
[i
]);
484 // It seems that if the list is cleared, we must re-ask for
485 // selection policy!!
486 SetSelectionPolicy();
488 m_noItems
+= items
.GetCount();
491 void wxListBox::SetString(unsigned int n
, const wxString
& s
)
493 wxSizeKeeper
sk( this );
494 Widget listBox
= (Widget
) m_mainWidget
;
496 wxXmString
text( s
);
498 // delete the item and add it again.
499 // FIXME isn't there a way to change it in place?
500 XmListDeletePos (listBox
, n
+1);
501 XmListAddItem (listBox
, text(), n
+1);
506 void wxListBox::Command (wxCommandEvent
& event
)
508 if (event
.GetExtraLong())
509 SetSelection (event
.GetInt());
512 Deselect (event
.GetInt());
515 ProcessCommand (event
);
518 void wxListBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
519 XmListCallbackStruct
* cbs
)
521 wxListBox
*item
= (wxListBox
*) clientData
;
523 if (item
->InSetValue())
528 if( cbs
->reason
== XmCR_DEFAULT_ACTION
)
529 evtType
= wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
;
531 evtType
= wxEVT_COMMAND_LISTBOX_SELECTED
;
533 int n
= cbs
->item_position
- 1;
534 wxCommandEvent
event (evtType
, item
->GetId());
535 if ( item
->HasClientObjectData() )
536 event
.SetClientObject( item
->GetClientObject(n
) );
537 else if ( item
->HasClientUntypedData() )
538 event
.SetClientData( item
->GetClientData(n
) );
540 event
.SetExtraLong(true);
541 event
.SetEventObject(item
);
542 event
.SetString( item
->GetString( n
) );
545 if( NULL
!= cbs
->event
&& cbs
->event
->type
== ButtonRelease
)
547 XButtonEvent
* evt
= (XButtonEvent
*)cbs
->event
;
554 case XmCR_MULTIPLE_SELECT
:
555 case XmCR_BROWSE_SELECT
:
556 #if wxUSE_CHECKLISTBOX
557 item
->DoToggleItem( n
, x
);
559 case XmCR_DEFAULT_ACTION
:
560 item
->GetEventHandler()->ProcessEvent(event
);
562 case XmCR_EXTENDED_SELECT
:
563 switch (cbs
->selection_type
)
568 item
->DoToggleItem( n
, x
);
569 item
->GetEventHandler()->ProcessEvent(event
);
576 WXWidget
wxListBox::GetTopWidget() const
578 return (WXWidget
) XtParent( (Widget
) m_mainWidget
);
581 void wxListBox::ChangeBackgroundColour()
583 wxWindow::ChangeBackgroundColour();
585 Widget parent
= XtParent ((Widget
) m_mainWidget
);
588 XtVaGetValues (parent
,
589 XmNhorizontalScrollBar
, &hsb
,
590 XmNverticalScrollBar
, &vsb
,
593 /* TODO: should scrollbars be affected? Should probably have separate
594 * function to change them (by default, taken from wxSystemSettings)
596 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
597 wxDoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, true);
598 wxDoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, true);
601 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(hsb
)),
604 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(vsb
)),
607 // MBN: why change parent's background? It looks really ugly.
608 // wxDoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, true);
611 void wxListBox::ChangeForegroundColour()
613 wxWindow::ChangeForegroundColour();
615 Widget parent
= XtParent ((Widget
) m_mainWidget
);
618 XtVaGetValues(parent
,
619 XmNhorizontalScrollBar
, &hsb
,
620 XmNverticalScrollBar
, &vsb
,
623 /* TODO: should scrollbars be affected? Should probably have separate
624 function to change them (by default, taken from wxSystemSettings)
626 wxDoChangeForegroundColour((WXWidget) hsb, m_foregroundColour);
627 wxDoChangeForegroundColour((WXWidget) vsb, m_foregroundColour);
628 wxDoChangeForegroundColour((WXWidget) parent, m_foregroundColour);
632 unsigned int wxListBox::GetCount() const
637 #define LIST_SCROLL_SPACING 6
639 wxSize
wxDoGetListBoxBestSize( Widget listWidget
, const wxWindow
* window
)
642 Dimension spacing
, highlight
, xmargin
, ymargin
, shadow
;
646 XtVaGetValues( listWidget
,
648 XmNlistSpacing
, &spacing
,
649 XmNhighlightThickness
, &highlight
,
650 XmNlistMarginWidth
, &xmargin
,
651 XmNlistMarginHeight
, &ymargin
,
652 XmNshadowThickness
, &shadow
,
655 for( size_t i
= 0; i
< (size_t)max
; ++i
)
657 window
->GetTextExtent( wxDoGetStringInList( listWidget
, i
), &x
, &y
);
658 width
= wxMax( width
, x
);
661 // use some arbitrary value if there are no strings
666 window
->GetTextExtent( "v", &x
, &y
);
668 // make it a little larger than widest string, plus the scrollbar
669 width
+= wxSystemSettings::GetMetric( wxSYS_VSCROLL_X
)
670 + 2 * highlight
+ LIST_SCROLL_SPACING
+ 2 * xmargin
+ 2 * shadow
;
672 // at least 3 items, at most 10
673 int height
= wxMax( 3, wxMin( 10, max
) ) *
674 ( y
+ spacing
+ 2 * highlight
) + 2 * ymargin
+ 2 * shadow
;
676 return wxSize( width
, height
);
679 wxSize
wxListBox::DoGetBestSize() const
681 return wxDoGetListBoxBestSize( (Widget
)m_mainWidget
, this );
684 #endif // wxUSE_LISTBOX