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
57 wxSizeKeeper( wxWindow
* w
)
60 m_w
->GetSize( &m_x
, &m_y
);
67 m_w
->GetSize( &x
, &y
);
68 if( x
!= m_x
|| y
!= m_y
)
69 m_w
->SetSize( -1, -1, m_x
, m_y
);
73 // ============================================================================
74 // list box control implementation
75 // ============================================================================
78 wxListBox::wxListBox()
83 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
86 int n
, const wxString choices
[],
88 const wxValidator
& validator
,
91 if( !wxControl::CreateControl( parent
, id
, pos
, size
, style
,
95 m_noItems
= (unsigned int)n
;
96 m_backgroundColour
= * wxWHITE
;
98 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
99 Display
* dpy
= XtDisplay(parentWidget
);
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
:
111 XtSetArg( args
[count
],
112 (String
)wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
) );
115 if( m_windowStyle
& wxLB_ALWAYS_SB
)
117 XtSetArg( args
[count
], XmNscrollBarDisplayPolicy
, XmSTATIC
);
122 XmCreateScrolledList(parentWidget
,
123 wxConstCast(name
.c_str(), char), args
, count
);
125 m_mainWidget
= (WXWidget
) listWidget
;
129 XtManageChild (listWidget
);
131 wxSize best
= GetBestSize();
132 if( size
.x
!= -1 ) best
.x
= size
.x
;
133 if( size
.y
!= -1 ) best
.y
= size
.y
;
135 XtAddCallback (listWidget
,
136 XmNbrowseSelectionCallback
,
137 (XtCallbackProc
) wxListBoxCallback
,
139 XtAddCallback (listWidget
,
140 XmNextendedSelectionCallback
,
141 (XtCallbackProc
) wxListBoxCallback
,
143 XtAddCallback (listWidget
,
144 XmNmultipleSelectionCallback
,
145 (XtCallbackProc
) wxListBoxCallback
,
147 XtAddCallback (listWidget
,
148 XmNdefaultActionCallback
,
149 (XtCallbackProc
) wxListBoxCallback
,
152 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
153 pos
.x
, pos
.y
, best
.x
, best
.y
);
155 ChangeBackgroundColour();
160 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
163 const wxArrayString
& choices
,
165 const wxValidator
& validator
,
166 const wxString
& name
)
168 wxCArrayString
chs(choices
);
169 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
170 style
, validator
, name
);
173 wxListBox::~wxListBox()
175 if( HasClientObjectData() )
176 m_clientDataDict
.DestroyData();
179 void wxListBox::SetSelectionPolicy()
181 Widget listBox
= (Widget
)m_mainWidget
;
184 XtSetArg( args
[0], XmNlistSizePolicy
, XmCONSTANT
);
186 XtSetArg( args
[1], XmNselectionPolicy
,
187 ( m_windowStyle
& wxLB_MULTIPLE
) ? XmMULTIPLE_SELECT
:
188 ( m_windowStyle
& wxLB_EXTENDED
) ? XmEXTENDED_SELECT
:
191 XtSetValues( listBox
, args
, 2 );
194 void wxListBox::DoSetFirstItem( int N
)
201 XtVaGetValues ((Widget
) m_mainWidget
,
202 XmNvisibleItemCount
, &count
,
203 XmNitemCount
, &length
,
205 if ((N
+ count
) >= length
)
207 XmListSetPos ((Widget
) m_mainWidget
, N
+ 1);
210 void wxListBox::Delete(unsigned int n
)
212 wxSizeKeeper
sk( this );
213 Widget listBox
= (Widget
) m_mainWidget
;
215 bool managed
= XtIsManaged(listBox
);
218 XtUnmanageChild (listBox
);
220 XmListDeletePos (listBox
, n
+ 1);
223 XtManageChild (listBox
);
226 m_clientDataDict
.Delete(n
, HasClientObjectData());
230 int wxListBox::DoAppend(const wxString
& item
)
232 wxSizeKeeper
sk( this );
233 Widget listBox
= (Widget
) m_mainWidget
;
235 bool managed
= XtIsManaged(listBox
);
238 XtUnmanageChild (listBox
);
240 XtVaGetValues (listBox
, XmNitemCount
, &n
, NULL
);
241 wxXmString
text( item
);
242 // XmListAddItem(listBox, text, n + 1);
243 XmListAddItemUnselected (listBox
, text(), 0);
245 // It seems that if the list is cleared, we must re-ask for
246 // selection policy!!
247 SetSelectionPolicy();
250 XtManageChild (listBox
);
255 return GetCount() - 1;
258 void wxListBox::DoSetItems(const wxArrayString
& items
, void** clientData
)
260 wxSizeKeeper
sk( this );
261 Widget listBox
= (Widget
) m_mainWidget
;
263 if( HasClientObjectData() )
264 m_clientDataDict
.DestroyData();
266 bool managed
= XtIsManaged(listBox
);
269 XtUnmanageChild (listBox
);
270 XmString
*text
= new XmString
[items
.GetCount()];
272 for (i
= 0; i
< items
.GetCount(); ++i
)
273 text
[i
] = wxStringToXmString (items
[i
]);
276 for (i
= 0; i
< items
.GetCount(); ++i
)
277 m_clientDataDict
.Set(i
, (wxClientData
*)clientData
[i
], false);
279 XmListAddItems (listBox
, text
, items
.GetCount(), 0);
280 for (i
= 0; i
< items
.GetCount(); i
++)
281 XmStringFree (text
[i
]);
284 // It seems that if the list is cleared, we must re-ask for
285 // selection policy!!
286 SetSelectionPolicy();
289 XtManageChild (listBox
);
293 m_noItems
= items
.GetCount();
296 int wxDoFindStringInList(Widget w
, const wxString
& s
)
299 int *positions
= NULL
;
300 int no_positions
= 0;
301 bool success
= XmListGetMatchPos (w
, str(),
302 &positions
, &no_positions
);
306 int pos
= positions
[0];
308 XtFree ((char *) positions
);
315 int wxListBox::FindString(const wxString
& s
, bool WXUNUSED(bCase
)) const
317 // FIXME: back to base class for not supported value of bCase
319 return wxDoFindStringInList( (Widget
)m_mainWidget
, s
);
322 void wxListBox::Clear()
327 wxSizeKeeper
sk( this );
328 Widget listBox
= (Widget
) m_mainWidget
;
330 XmListDeleteAllItems (listBox
);
331 if( HasClientObjectData() )
332 m_clientDataDict
.DestroyData();
339 void wxListBox::DoSetSelection(int N
, bool select
)
345 if (m_windowStyle
& wxLB_MULTIPLE
)
347 int *selections
= NULL
;
348 int n
= GetSelections (&selections
);
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.
353 XtVaSetValues ((Widget
) m_mainWidget
,
354 XmNselectionPolicy
, XmMULTIPLE_SELECT
,
358 for (i
= 0; i
< n
; i
++)
359 XmListSelectPos ((Widget
) m_mainWidget
,
360 selections
[i
] + 1, False
);
362 XmListSelectPos ((Widget
) m_mainWidget
, N
+ 1, False
);
364 XtVaSetValues ((Widget
) m_mainWidget
,
365 XmNselectionPolicy
, XmEXTENDED_SELECT
,
370 XmListSelectPos ((Widget
) m_mainWidget
, N
+ 1, False
);
374 XmListDeselectPos ((Widget
) m_mainWidget
, N
+ 1);
376 m_inSetValue
= false;
379 bool wxListBox::IsSelected(int N
) const
381 // In Motif, no simple way to determine if the item is selected.
382 wxArrayInt theSelections
;
383 int count
= GetSelections (theSelections
);
389 for (j
= 0; j
< count
; j
++)
390 if (theSelections
[j
] == N
)
396 void wxListBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
398 m_clientDataDict
.Set(n
, clientData
, false);
401 wxClientData
* wxListBox::DoGetItemClientObject(unsigned int n
) const
403 return m_clientDataDict
.Get(n
);
406 void *wxListBox::DoGetItemClientData(unsigned int n
) const
408 return (void*)m_clientDataDict
.Get(n
);
411 void wxListBox::DoSetItemClientData(unsigned int n
, void *Client_data
)
413 m_clientDataDict
.Set(n
, (wxClientData
*)Client_data
, false);
416 // Return number of selections and an array of selected integers
417 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
421 Widget listBox
= (Widget
) m_mainWidget
;
424 bool flag
= XmListGetSelectedPos (listBox
, &posList
, &posCnt
);
429 aSelections
.Alloc(posCnt
);
432 for (i
= 0; i
< posCnt
; i
++)
433 aSelections
.Add(posList
[i
] - 1);
435 XtFree ((char *) posList
);
445 // Get single selection, for single choice list items
446 int wxDoGetSelectionInList(Widget listBox
)
450 bool flag
= XmListGetSelectedPos (listBox
, &posList
, &posCnt
);
456 XtFree ((char *) posList
);
463 int wxListBox::GetSelection() const
465 return wxDoGetSelectionInList((Widget
) m_mainWidget
);
468 // Find string for position
469 wxString
wxDoGetStringInList( Widget listBox
, int n
)
473 XtVaGetValues( listBox
,
474 XmNitemCount
, &count
,
477 if( n
< count
&& n
>= 0 )
478 return wxXmStringToString( strlist
[n
] );
480 return wxEmptyString
;
483 wxString
wxListBox::GetString(unsigned int n
) const
485 return wxDoGetStringInList( (Widget
)m_mainWidget
, n
);
488 void wxListBox::DoInsertItems(const wxArrayString
& items
, unsigned int pos
)
490 wxSizeKeeper
sk( this );
491 Widget listBox
= (Widget
) m_mainWidget
;
493 bool managed
= XtIsManaged(listBox
);
496 XtUnmanageChild(listBox
);
498 XmString
*text
= new XmString
[items
.GetCount()];
500 // Steve Hammes: Motif 1.1 compatibility
501 // #if XmVersion > 1100
502 // Corrected by Sergey Krasnov from Steve Hammes' code
504 for (i
= 0; i
< items
.GetCount(); i
++)
505 text
[i
] = wxStringToXmString(items
[i
]);
506 XmListAddItemsUnselected(listBox
, text
, items
.GetCount(), pos
+1);
508 for (i
= 0; i
< items
.GetCount(); i
++)
510 text
[i
] = wxStringToXmString(items
[i
]);
511 // Another Sergey correction
512 XmListAddItemUnselected(listBox
, text
[i
], pos
+i
+1);
515 for (i
= 0; i
< items
.GetCount(); i
++)
516 XmStringFree(text
[i
]);
519 // It seems that if the list is cleared, we must re-ask for
520 // selection policy!!
521 SetSelectionPolicy();
524 XtManageChild(listBox
);
528 m_noItems
+= items
.GetCount();
531 void wxListBox::SetString(unsigned int n
, const wxString
& s
)
533 wxSizeKeeper
sk( this );
534 Widget listBox
= (Widget
) m_mainWidget
;
536 wxXmString
text( s
);
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);
546 void wxListBox::Command (wxCommandEvent
& event
)
548 if (event
.GetExtraLong())
549 SetSelection (event
.GetInt());
552 Deselect (event
.GetInt());
555 ProcessCommand (event
);
558 void wxListBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
559 XmListCallbackStruct
* cbs
)
561 wxListBox
*item
= (wxListBox
*) clientData
;
563 if (item
->InSetValue())
568 if( cbs
->reason
== XmCR_DEFAULT_ACTION
)
569 evtType
= wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
;
571 evtType
= wxEVT_COMMAND_LISTBOX_SELECTED
;
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
) );
580 event
.SetExtraLong(true);
581 event
.SetEventObject(item
);
582 event
.SetString( item
->GetString( n
) );
585 if( NULL
!= cbs
->event
&& cbs
->event
->type
== ButtonRelease
)
587 XButtonEvent
* evt
= (XButtonEvent
*)cbs
->event
;
594 case XmCR_MULTIPLE_SELECT
:
595 case XmCR_BROWSE_SELECT
:
596 #if wxUSE_CHECKLISTBOX
597 item
->DoToggleItem( n
, x
);
599 case XmCR_DEFAULT_ACTION
:
600 item
->GetEventHandler()->ProcessEvent(event
);
602 case XmCR_EXTENDED_SELECT
:
603 switch (cbs
->selection_type
)
608 item
->DoToggleItem( n
, x
);
609 item
->GetEventHandler()->ProcessEvent(event
);
616 WXWidget
wxListBox::GetTopWidget() const
618 return (WXWidget
) XtParent( (Widget
) m_mainWidget
);
621 void wxListBox::ChangeBackgroundColour()
623 wxWindow::ChangeBackgroundColour();
625 Widget parent
= XtParent ((Widget
) m_mainWidget
);
628 XtVaGetValues (parent
,
629 XmNhorizontalScrollBar
, &hsb
,
630 XmNverticalScrollBar
, &vsb
,
633 /* TODO: should scrollbars be affected? Should probably have separate
634 * function to change them (by default, taken from wxSystemSettings)
636 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
637 wxDoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, true);
638 wxDoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, true);
641 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(hsb
)),
644 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(vsb
)),
647 // MBN: why change parent's background? It looks really ugly.
648 // wxDoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, true);
651 void wxListBox::ChangeForegroundColour()
653 wxWindow::ChangeForegroundColour();
655 Widget parent
= XtParent ((Widget
) m_mainWidget
);
658 XtVaGetValues(parent
,
659 XmNhorizontalScrollBar
, &hsb
,
660 XmNverticalScrollBar
, &vsb
,
663 /* TODO: should scrollbars be affected? Should probably have separate
664 function to change them (by default, taken from wxSystemSettings)
666 wxDoChangeForegroundColour((WXWidget) hsb, m_foregroundColour);
667 wxDoChangeForegroundColour((WXWidget) vsb, m_foregroundColour);
668 wxDoChangeForegroundColour((WXWidget) parent, m_foregroundColour);
672 unsigned int wxListBox::GetCount() const
677 #define LIST_SCROLL_SPACING 6
679 wxSize
wxDoGetListBoxBestSize( Widget listWidget
, const wxWindow
* window
)
682 Dimension spacing
, highlight
, xmargin
, ymargin
, shadow
;
686 XtVaGetValues( listWidget
,
688 XmNlistSpacing
, &spacing
,
689 XmNhighlightThickness
, &highlight
,
690 XmNlistMarginWidth
, &xmargin
,
691 XmNlistMarginHeight
, &ymargin
,
692 XmNshadowThickness
, &shadow
,
695 for( size_t i
= 0; i
< (size_t)max
; ++i
)
697 window
->GetTextExtent( wxDoGetStringInList( listWidget
, i
), &x
, &y
);
698 width
= wxMax( width
, x
);
701 // use some arbitrary value if there are no strings
706 window
->GetTextExtent( "v", &x
, &y
);
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
;
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
;
716 return wxSize( width
, height
);
719 wxSize
wxListBox::DoGetBestSize() const
721 return wxDoGetListBoxBestSize( (Widget
)m_mainWidget
, this );
724 #endif // wxUSE_LISTBOX