1 ///////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "listbox.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
20 #define XtParent XTPARENT
21 #define XtDisplay XTDISPLAY
24 # include "wx/listbox.h"
25 #include "wx/settings.h"
26 #include "wx/dynarray.h"
29 #include "wx/arrstr.h"
32 #pragma message disable nosimpint
36 #pragma message enable nosimpint
38 #include "wx/motif/private.h"
40 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
42 static void wxListBoxCallback(Widget w
,
44 XmListCallbackStruct
* cbs
);
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // helper class to reduce code duplication
56 wxSizeKeeper( wxWindow
* w
)
59 m_w
->GetSize( &m_x
, &m_y
);
66 m_w
->GetSize( &x
, &y
);
67 if( x
!= m_x
|| y
!= m_y
)
68 m_w
->SetSize( -1, -1, m_x
, m_y
);
72 // ============================================================================
73 // list box control implementation
74 // ============================================================================
77 wxListBox::wxListBox()
82 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
85 int n
, const wxString choices
[],
87 const wxValidator
& validator
,
90 if( !wxControl::CreateControl( parent
, id
, pos
, size
, style
,
95 m_backgroundColour
= * wxWHITE
;
97 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
98 Display
* dpy
= XtDisplay(parentWidget
);
102 XtSetArg( args
[count
], XmNlistSizePolicy
, XmCONSTANT
); ++count
;
103 XtSetArg( args
[count
], XmNselectionPolicy
,
104 ( m_windowStyle
& wxLB_MULTIPLE
) ? XmMULTIPLE_SELECT
:
105 ( m_windowStyle
& wxLB_EXTENDED
) ? XmEXTENDED_SELECT
:
110 XtSetArg( args
[count
],
111 (String
)wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
) );
114 if( m_windowStyle
& wxLB_ALWAYS_SB
)
116 XtSetArg( args
[count
], XmNscrollBarDisplayPolicy
, XmSTATIC
);
121 XmCreateScrolledList(parentWidget
,
122 wxConstCast(name
.c_str(), char), args
, count
);
124 m_mainWidget
= (WXWidget
) listWidget
;
128 XtManageChild (listWidget
);
130 wxSize best
= GetBestSize();
131 if( size
.x
!= -1 ) best
.x
= size
.x
;
132 if( size
.y
!= -1 ) best
.y
= size
.y
;
134 XtAddCallback (listWidget
,
135 XmNbrowseSelectionCallback
,
136 (XtCallbackProc
) wxListBoxCallback
,
138 XtAddCallback (listWidget
,
139 XmNextendedSelectionCallback
,
140 (XtCallbackProc
) wxListBoxCallback
,
142 XtAddCallback (listWidget
,
143 XmNmultipleSelectionCallback
,
144 (XtCallbackProc
) wxListBoxCallback
,
146 XtAddCallback (listWidget
,
147 XmNdefaultActionCallback
,
148 (XtCallbackProc
) wxListBoxCallback
,
151 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
152 pos
.x
, pos
.y
, best
.x
, best
.y
);
154 ChangeBackgroundColour();
159 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
162 const wxArrayString
& choices
,
164 const wxValidator
& validator
,
165 const wxString
& name
)
167 wxCArrayString
chs(choices
);
168 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
169 style
, validator
, name
);
172 wxListBox::~wxListBox()
174 if( HasClientObjectData() )
175 m_clientDataDict
.DestroyData();
178 void wxListBox::SetSelectionPolicy()
180 Widget listBox
= (Widget
)m_mainWidget
;
183 XtSetArg( args
[0], XmNlistSizePolicy
, XmCONSTANT
);
185 XtSetArg( args
[1], XmNselectionPolicy
,
186 ( m_windowStyle
& wxLB_MULTIPLE
) ? XmMULTIPLE_SELECT
:
187 ( m_windowStyle
& wxLB_EXTENDED
) ? XmEXTENDED_SELECT
:
190 XtSetValues( listBox
, args
, 2 );
193 void wxListBox::DoSetFirstItem( int N
)
199 XtVaGetValues ((Widget
) m_mainWidget
,
200 XmNvisibleItemCount
, &count
,
201 XmNitemCount
, &length
,
203 if ((N
+ count
) >= length
)
205 XmListSetPos ((Widget
) m_mainWidget
, N
+ 1);
208 void wxListBox::Delete(int N
)
210 wxSizeKeeper
sk( this );
211 Widget listBox
= (Widget
) m_mainWidget
;
213 bool managed
= XtIsManaged(listBox
);
216 XtUnmanageChild (listBox
);
218 XmListDeletePos (listBox
, N
+ 1);
221 XtManageChild (listBox
);
224 m_clientDataDict
.Delete(N
, HasClientObjectData());
228 int wxListBox::DoAppend(const wxString
& item
)
230 wxSizeKeeper
sk( this );
231 Widget listBox
= (Widget
) m_mainWidget
;
233 bool managed
= XtIsManaged(listBox
);
236 XtUnmanageChild (listBox
);
238 XtVaGetValues (listBox
, XmNitemCount
, &n
, NULL
);
239 wxXmString
text( item
);
240 // XmListAddItem(listBox, text, n + 1);
241 XmListAddItemUnselected (listBox
, text(), 0);
243 // It seems that if the list is cleared, we must re-ask for
244 // selection policy!!
245 SetSelectionPolicy();
248 XtManageChild (listBox
);
253 return GetCount() - 1;
256 void wxListBox::DoSetItems(const wxArrayString
& items
, void** clientData
)
258 wxSizeKeeper
sk( this );
259 Widget listBox
= (Widget
) m_mainWidget
;
261 if( HasClientObjectData() )
262 m_clientDataDict
.DestroyData();
264 bool managed
= XtIsManaged(listBox
);
267 XtUnmanageChild (listBox
);
268 XmString
*text
= new XmString
[items
.GetCount()];
270 for (i
= 0; i
< items
.GetCount(); ++i
)
271 text
[i
] = wxStringToXmString (items
[i
]);
274 for (i
= 0; i
< items
.GetCount(); ++i
)
275 m_clientDataDict
.Set(i
, (wxClientData
*)clientData
[i
], false);
277 XmListAddItems (listBox
, text
, items
.GetCount(), 0);
278 for (i
= 0; i
< items
.GetCount(); i
++)
279 XmStringFree (text
[i
]);
282 // It seems that if the list is cleared, we must re-ask for
283 // selection policy!!
284 SetSelectionPolicy();
287 XtManageChild (listBox
);
291 m_noItems
= items
.GetCount();
294 int wxDoFindStringInList(Widget w
, const wxString
& s
)
297 int *positions
= NULL
;
298 int no_positions
= 0;
299 bool success
= XmListGetMatchPos (w
, str(),
300 &positions
, &no_positions
);
304 int pos
= positions
[0];
306 XtFree ((char *) positions
);
313 int wxListBox::FindString(const wxString
& s
) const
315 return wxDoFindStringInList( (Widget
)m_mainWidget
, s
);
318 void wxListBox::Clear()
323 wxSizeKeeper
sk( this );
324 Widget listBox
= (Widget
) m_mainWidget
;
326 XmListDeleteAllItems (listBox
);
327 if( HasClientObjectData() )
328 m_clientDataDict
.DestroyData();
335 void wxListBox::DoSetSelection(int N
, bool select
)
341 if (m_windowStyle
& wxLB_MULTIPLE
)
343 int *selections
= NULL
;
344 int n
= GetSelections (&selections
);
346 // This hack is supposed to work, to make it possible
347 // to select more than one item, but it DOESN'T under Motif 1.1.
349 XtVaSetValues ((Widget
) m_mainWidget
,
350 XmNselectionPolicy
, XmMULTIPLE_SELECT
,
354 for (i
= 0; i
< n
; i
++)
355 XmListSelectPos ((Widget
) m_mainWidget
,
356 selections
[i
] + 1, False
);
358 XmListSelectPos ((Widget
) m_mainWidget
, N
+ 1, False
);
360 XtVaSetValues ((Widget
) m_mainWidget
,
361 XmNselectionPolicy
, XmEXTENDED_SELECT
,
366 XmListSelectPos ((Widget
) m_mainWidget
, N
+ 1, False
);
370 XmListDeselectPos ((Widget
) m_mainWidget
, N
+ 1);
372 m_inSetValue
= false;
375 bool wxListBox::IsSelected(int N
) const
377 // In Motif, no simple way to determine if the item is selected.
378 wxArrayInt theSelections
;
379 int count
= GetSelections (theSelections
);
385 for (j
= 0; j
< count
; j
++)
386 if (theSelections
[j
] == N
)
392 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
394 m_clientDataDict
.Set(n
, clientData
, false);
397 wxClientData
* wxListBox::DoGetItemClientObject(int n
) const
399 return m_clientDataDict
.Get(n
);
402 void *wxListBox::DoGetItemClientData(int N
) const
404 return (void*)m_clientDataDict
.Get(N
);
407 void wxListBox::DoSetItemClientData(int N
, void *Client_data
)
409 m_clientDataDict
.Set(N
, (wxClientData
*)Client_data
, false);
412 // Return number of selections and an array of selected integers
413 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
417 Widget listBox
= (Widget
) m_mainWidget
;
420 bool flag
= XmListGetSelectedPos (listBox
, &posList
, &posCnt
);
425 aSelections
.Alloc(posCnt
);
428 for (i
= 0; i
< posCnt
; i
++)
429 aSelections
.Add(posList
[i
] - 1);
431 XtFree ((char *) posList
);
441 // Get single selection, for single choice list items
442 int wxDoGetSelectionInList(Widget listBox
)
446 bool flag
= XmListGetSelectedPos (listBox
, &posList
, &posCnt
);
452 XtFree ((char *) posList
);
459 int wxListBox::GetSelection() const
461 return wxDoGetSelectionInList((Widget
) m_mainWidget
);
464 // Find string for position
465 wxString
wxDoGetStringInList( Widget listBox
, int n
)
469 XtVaGetValues( listBox
,
470 XmNitemCount
, &count
,
473 if( n
< count
&& n
>= 0 )
474 return wxXmStringToString( strlist
[n
] );
476 return wxEmptyString
;
479 wxString
wxListBox::GetString( int n
) const
481 return wxDoGetStringInList( (Widget
)m_mainWidget
, n
);
484 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
486 wxSizeKeeper
sk( this );
487 Widget listBox
= (Widget
) m_mainWidget
;
489 bool managed
= XtIsManaged(listBox
);
492 XtUnmanageChild(listBox
);
494 XmString
*text
= new XmString
[items
.GetCount()];
496 // Steve Hammes: Motif 1.1 compatibility
497 // #if XmVersion > 1100
498 // Corrected by Sergey Krasnov from Steve Hammes' code
500 for (i
= 0; i
< items
.GetCount(); i
++)
501 text
[i
] = wxStringToXmString(items
[i
]);
502 XmListAddItemsUnselected(listBox
, text
, items
.GetCount(), pos
+1);
504 for (i
= 0; i
< items
.GetCount(); i
++)
506 text
[i
] = wxStringToXmString(items
[i
]);
507 // Another Sergey correction
508 XmListAddItemUnselected(listBox
, text
[i
], pos
+i
+1);
511 for (i
= 0; i
< items
.GetCount(); i
++)
512 XmStringFree(text
[i
]);
515 // It seems that if the list is cleared, we must re-ask for
516 // selection policy!!
517 SetSelectionPolicy();
520 XtManageChild(listBox
);
524 m_noItems
+= items
.GetCount();
527 void wxListBox::SetString(int N
, const wxString
& s
)
529 wxSizeKeeper
sk( this );
530 Widget listBox
= (Widget
) m_mainWidget
;
532 wxXmString
text( s
);
534 // delete the item and add it again.
535 // FIXME isn't there a way to change it in place?
536 XmListDeletePos (listBox
, N
+1);
537 XmListAddItem (listBox
, text(), N
+1);
542 void wxListBox::Command (wxCommandEvent
& event
)
544 if (event
.GetExtraLong())
545 SetSelection (event
.GetInt());
548 Deselect (event
.GetInt());
551 ProcessCommand (event
);
554 void wxListBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
555 XmListCallbackStruct
* cbs
)
557 wxListBox
*item
= (wxListBox
*) clientData
;
559 if (item
->InSetValue())
564 if( cbs
->reason
== XmCR_DEFAULT_ACTION
)
565 evtType
= wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
;
567 evtType
= wxEVT_COMMAND_LISTBOX_SELECTED
;
569 int n
= cbs
->item_position
- 1;
570 wxCommandEvent
event (evtType
, item
->GetId());
571 if ( item
->HasClientObjectData() )
572 event
.SetClientObject( item
->GetClientObject(n
) );
573 else if ( item
->HasClientUntypedData() )
574 event
.SetClientData( item
->GetClientData(n
) );
576 event
.SetExtraLong(true);
577 event
.SetEventObject(item
);
578 event
.SetString( item
->GetString( n
) );
581 if( NULL
!= cbs
->event
&& cbs
->event
->type
== ButtonRelease
)
583 XButtonEvent
* evt
= (XButtonEvent
*)cbs
->event
;
590 case XmCR_MULTIPLE_SELECT
:
591 case XmCR_BROWSE_SELECT
:
592 #if wxUSE_CHECKLISTBOX
593 item
->DoToggleItem( n
, x
);
595 case XmCR_DEFAULT_ACTION
:
596 item
->GetEventHandler()->ProcessEvent(event
);
598 case XmCR_EXTENDED_SELECT
:
599 switch (cbs
->selection_type
)
604 item
->DoToggleItem( n
, x
);
605 item
->GetEventHandler()->ProcessEvent(event
);
612 WXWidget
wxListBox::GetTopWidget() const
614 return (WXWidget
) XtParent( (Widget
) m_mainWidget
);
617 void wxListBox::ChangeBackgroundColour()
619 wxWindow::ChangeBackgroundColour();
621 Widget parent
= XtParent ((Widget
) m_mainWidget
);
624 XtVaGetValues (parent
,
625 XmNhorizontalScrollBar
, &hsb
,
626 XmNverticalScrollBar
, &vsb
,
629 /* TODO: should scrollbars be affected? Should probably have separate
630 * function to change them (by default, taken from wxSystemSettings)
632 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
633 wxDoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, true);
634 wxDoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, true);
637 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(hsb
)),
640 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(vsb
)),
643 // MBN: why change parent's background? It looks really ugly.
644 // wxDoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, true);
647 void wxListBox::ChangeForegroundColour()
649 wxWindow::ChangeForegroundColour();
651 Widget parent
= XtParent ((Widget
) m_mainWidget
);
654 XtVaGetValues(parent
,
655 XmNhorizontalScrollBar
, &hsb
,
656 XmNverticalScrollBar
, &vsb
,
659 /* TODO: should scrollbars be affected? Should probably have separate
660 function to change them (by default, taken from wxSystemSettings)
662 wxDoChangeForegroundColour((WXWidget) hsb, m_foregroundColour);
663 wxDoChangeForegroundColour((WXWidget) vsb, m_foregroundColour);
664 wxDoChangeForegroundColour((WXWidget) parent, m_foregroundColour);
668 int wxListBox::GetCount() const
673 #define LIST_SCROLL_SPACING 6
675 wxSize
wxDoGetListBoxBestSize( Widget listWidget
, const wxWindow
* window
)
678 Dimension spacing
, highlight
, xmargin
, ymargin
, shadow
;
682 XtVaGetValues( listWidget
,
684 XmNlistSpacing
, &spacing
,
685 XmNhighlightThickness
, &highlight
,
686 XmNlistMarginWidth
, &xmargin
,
687 XmNlistMarginHeight
, &ymargin
,
688 XmNshadowThickness
, &shadow
,
691 for( size_t i
= 0; i
< (size_t)max
; ++i
)
693 window
->GetTextExtent( wxDoGetStringInList( listWidget
, i
), &x
, &y
);
694 width
= wxMax( width
, x
);
697 // use some arbitrary value if there are no strings
702 window
->GetTextExtent( "v", &x
, &y
);
704 // make it a little larger than widest string, plus the scrollbar
705 width
+= wxSystemSettings::GetMetric( wxSYS_VSCROLL_X
)
706 + 2 * highlight
+ LIST_SCROLL_SPACING
+ 2 * xmargin
+ 2 * shadow
;
708 // at least 3 items, at most 10
709 int height
= wxMax( 3, wxMin( 10, max
) ) *
710 ( y
+ spacing
+ 2 * highlight
) + 2 * ymargin
+ 2 * shadow
;
712 return wxSize( width
, height
);
715 wxSize
wxListBox::DoGetBestSize() const
717 return wxDoGetListBoxBestSize( (Widget
)m_mainWidget
, this );