1 ///////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "listbox.h"
17 #define XtParent XTPARENT
18 #define XtDisplay XTDISPLAY
21 # include "wx/listbox.h"
22 #include "wx/settings.h"
23 #include "wx/dynarray.h"
28 #pragma message disable nosimpint
32 #pragma message enable nosimpint
34 #include "wx/motif/private.h"
36 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
38 static void wxListBoxCallback(Widget w
,
40 XmListCallbackStruct
* cbs
);
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // helper class to reduce code duplication
52 wxSizeKeeper( wxWindow
* w
)
55 m_w
->GetSize( &m_x
, &m_y
);
62 m_w
->GetSize( &x
, &y
);
63 if( x
!= m_x
|| y
!= m_y
)
64 m_w
->SetSize( -1, -1, m_x
, m_y
);
68 // ============================================================================
69 // list box control implementation
70 // ============================================================================
73 wxListBox::wxListBox()
78 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
81 int n
, const wxString choices
[],
83 const wxValidator
& validator
,
86 if( !wxControl::CreateControl( parent
, id
, pos
, size
, style
,
91 m_backgroundColour
= * wxWHITE
;
93 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
95 WXFontType fontType
= (WXFontType
)NULL
;
99 fontType
= m_font
.GetFontType(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
], (String
)wxFont::GetFontTag(), fontType
);
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 wxListBox::~wxListBox()
162 if( HasClientObjectData() )
163 m_clientDataDict
.DestroyData();
166 void wxListBox::SetSelectionPolicy()
168 Widget listBox
= (Widget
)m_mainWidget
;
171 XtSetArg( args
[0], XmNlistSizePolicy
, XmCONSTANT
);
173 XtSetArg( args
[1], XmNselectionPolicy
,
174 ( m_windowStyle
& wxLB_MULTIPLE
) ? XmMULTIPLE_SELECT
:
175 ( m_windowStyle
& wxLB_EXTENDED
) ? XmEXTENDED_SELECT
:
178 XtSetValues( listBox
, args
, 2 );
181 void wxListBox::DoSetFirstItem( int N
)
187 XtVaGetValues ((Widget
) m_mainWidget
,
188 XmNvisibleItemCount
, &count
,
189 XmNitemCount
, &length
,
191 if ((N
+ count
) >= length
)
193 XmListSetPos ((Widget
) m_mainWidget
, N
+ 1);
196 void wxListBox::Delete(int N
)
198 wxSizeKeeper
sk( this );
199 Widget listBox
= (Widget
) m_mainWidget
;
201 bool managed
= XtIsManaged(listBox
);
204 XtUnmanageChild (listBox
);
206 XmListDeletePos (listBox
, N
+ 1);
209 XtManageChild (listBox
);
212 m_clientDataDict
.Delete(N
, HasClientObjectData());
216 int wxListBox::DoAppend(const wxString
& item
)
218 wxSizeKeeper
sk( this );
219 Widget listBox
= (Widget
) m_mainWidget
;
221 bool managed
= XtIsManaged(listBox
);
224 XtUnmanageChild (listBox
);
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();
236 XtManageChild (listBox
);
241 return GetCount() - 1;
244 void wxListBox::DoSetItems(const wxArrayString
& items
, void** clientData
)
246 wxSizeKeeper
sk( this );
247 Widget listBox
= (Widget
) m_mainWidget
;
249 if( HasClientObjectData() )
250 m_clientDataDict
.DestroyData();
252 bool managed
= XtIsManaged(listBox
);
255 XtUnmanageChild (listBox
);
256 XmString
*text
= new XmString
[items
.GetCount()];
258 for (i
= 0; i
< items
.GetCount(); ++i
)
259 text
[i
] = wxStringToXmString (items
[i
]);
262 for (i
= 0; i
< items
.GetCount(); ++i
)
263 m_clientDataDict
.Set(i
, (wxClientData
*)clientData
[i
], FALSE
);
265 XmListAddItems (listBox
, text
, items
.GetCount(), 0);
266 for (i
= 0; i
< items
.GetCount(); i
++)
267 XmStringFree (text
[i
]);
270 // It seems that if the list is cleared, we must re-ask for
271 // selection policy!!
272 SetSelectionPolicy();
275 XtManageChild (listBox
);
279 m_noItems
= items
.GetCount();
282 int wxDoFindStringInList(Widget w
, const wxString
& s
)
285 int *positions
= NULL
;
286 int no_positions
= 0;
287 bool success
= XmListGetMatchPos (w
, str(),
288 &positions
, &no_positions
);
292 int pos
= positions
[0];
294 XtFree ((char *) positions
);
301 int wxListBox::FindString(const wxString
& s
) const
303 return wxDoFindStringInList( (Widget
)m_mainWidget
, s
);
306 void wxListBox::Clear()
311 wxSizeKeeper
sk( this );
312 Widget listBox
= (Widget
) m_mainWidget
;
314 XmListDeleteAllItems (listBox
);
315 if( HasClientObjectData() )
316 m_clientDataDict
.DestroyData();
323 void wxListBox::SetSelection(int N
, bool select
)
329 if (m_windowStyle
& wxLB_MULTIPLE
)
331 int *selections
= NULL
;
332 int n
= GetSelections (&selections
);
334 // This hack is supposed to work, to make it possible
335 // to select more than one item, but it DOESN'T under Motif 1.1.
337 XtVaSetValues ((Widget
) m_mainWidget
,
338 XmNselectionPolicy
, XmMULTIPLE_SELECT
,
342 for (i
= 0; i
< n
; i
++)
343 XmListSelectPos ((Widget
) m_mainWidget
,
344 selections
[i
] + 1, FALSE
);
346 XmListSelectPos ((Widget
) m_mainWidget
, N
+ 1, FALSE
);
348 XtVaSetValues ((Widget
) m_mainWidget
,
349 XmNselectionPolicy
, XmEXTENDED_SELECT
,
354 XmListSelectPos ((Widget
) m_mainWidget
, N
+ 1, FALSE
);
358 XmListDeselectPos ((Widget
) m_mainWidget
, N
+ 1);
360 m_inSetValue
= FALSE
;
363 bool wxListBox::IsSelected(int N
) const
365 // In Motif, no simple way to determine if the item is selected.
366 wxArrayInt theSelections
;
367 int count
= GetSelections (theSelections
);
373 for (j
= 0; j
< count
; j
++)
374 if (theSelections
[j
] == N
)
380 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
382 m_clientDataDict
.Set(n
, clientData
, FALSE
);
385 wxClientData
* wxListBox::DoGetItemClientObject(int n
) const
387 return m_clientDataDict
.Get(n
);
390 void *wxListBox::DoGetItemClientData(int N
) const
392 return (void*)m_clientDataDict
.Get(N
);
395 void wxListBox::DoSetItemClientData(int N
, void *Client_data
)
397 m_clientDataDict
.Set(N
, (wxClientData
*)Client_data
, FALSE
);
400 // Return number of selections and an array of selected integers
401 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
405 Widget listBox
= (Widget
) m_mainWidget
;
408 bool flag
= XmListGetSelectedPos (listBox
, &posList
, &posCnt
);
413 aSelections
.Alloc(posCnt
);
416 for (i
= 0; i
< posCnt
; i
++)
417 aSelections
.Add(posList
[i
] - 1);
419 XtFree ((char *) posList
);
429 // Get single selection, for single choice list items
430 int wxDoGetSelectionInList(Widget listBox
)
434 bool flag
= XmListGetSelectedPos (listBox
, &posList
, &posCnt
);
440 XtFree ((char *) posList
);
447 int wxListBox::GetSelection() const
449 return wxDoGetSelectionInList((Widget
) m_mainWidget
);
452 // Find string for position
453 wxString
wxDoGetStringInList( Widget listBox
, int n
)
457 XtVaGetValues( listBox
,
458 XmNitemCount
, &count
,
461 if( n
< count
&& n
>= 0 )
462 return wxXmStringToString( strlist
[n
] );
464 return wxEmptyString
;
467 wxString
wxListBox::GetString( int n
) const
469 return wxDoGetStringInList( (Widget
)m_mainWidget
, n
);
472 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
474 wxSizeKeeper
sk( this );
475 Widget listBox
= (Widget
) m_mainWidget
;
477 bool managed
= XtIsManaged(listBox
);
480 XtUnmanageChild(listBox
);
482 XmString
*text
= new XmString
[items
.GetCount()];
484 // Steve Hammes: Motif 1.1 compatibility
485 // #if XmVersion > 1100
486 // Corrected by Sergey Krasnov from Steve Hammes' code
488 for (i
= 0; i
< items
.GetCount(); i
++)
489 text
[i
] = wxStringToXmString(items
[i
]);
490 XmListAddItemsUnselected(listBox
, text
, items
.GetCount(), pos
+1);
492 for (i
= 0; i
< items
.GetCount(); i
++)
494 text
[i
] = wxStringToXmString(items
[i
]);
495 // Another Sergey correction
496 XmListAddItemUnselected(listBox
, text
[i
], pos
+i
+1);
499 for (i
= 0; i
< items
.GetCount(); i
++)
500 XmStringFree(text
[i
]);
503 // It seems that if the list is cleared, we must re-ask for
504 // selection policy!!
505 SetSelectionPolicy();
508 XtManageChild(listBox
);
512 m_noItems
+= items
.GetCount();
515 void wxListBox::SetString(int N
, const wxString
& s
)
517 wxSizeKeeper
sk( this );
518 Widget listBox
= (Widget
) m_mainWidget
;
520 wxXmString
text( s
);
522 // delete the item and add it again.
523 // FIXME isn't there a way to change it in place?
524 XmListDeletePos (listBox
, N
+1);
525 XmListAddItem (listBox
, text(), N
+1);
530 void wxListBox::Command (wxCommandEvent
& event
)
532 if (event
.m_extraLong
)
533 SetSelection (event
.m_commandInt
);
536 Deselect (event
.m_commandInt
);
539 ProcessCommand (event
);
542 void wxListBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
543 XmListCallbackStruct
* cbs
)
545 wxListBox
*item
= (wxListBox
*) clientData
;
547 if (item
->InSetValue())
552 if( cbs
->reason
== XmCR_DEFAULT_ACTION
)
553 evtType
= wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
;
555 evtType
= wxEVT_COMMAND_LISTBOX_SELECTED
;
557 int n
= cbs
->item_position
- 1;
558 wxCommandEvent
event (evtType
, item
->GetId());
559 if ( item
->HasClientObjectData() )
560 event
.SetClientObject( item
->GetClientObject(n
) );
561 else if ( item
->HasClientUntypedData() )
562 event
.SetClientData( item
->GetClientData(n
) );
563 event
.m_commandInt
= n
;
564 event
.m_extraLong
= TRUE
;
565 event
.SetEventObject(item
);
566 event
.SetString( item
->GetString( n
) );
569 if( NULL
!= cbs
->event
&& cbs
->event
->type
== ButtonRelease
)
571 XButtonEvent
* evt
= (XButtonEvent
*)cbs
->event
;
578 case XmCR_MULTIPLE_SELECT
:
579 case XmCR_BROWSE_SELECT
:
580 #if wxUSE_CHECKLISTBOX
581 item
->DoToggleItem( n
, x
);
583 case XmCR_DEFAULT_ACTION
:
584 item
->GetEventHandler()->ProcessEvent(event
);
586 case XmCR_EXTENDED_SELECT
:
587 switch (cbs
->selection_type
)
592 item
->DoToggleItem( n
, x
);
593 item
->GetEventHandler()->ProcessEvent(event
);
600 WXWidget
wxListBox::GetTopWidget() const
602 return (WXWidget
) XtParent( (Widget
) m_mainWidget
);
605 void wxListBox::ChangeBackgroundColour()
607 wxWindow::ChangeBackgroundColour();
609 Widget parent
= XtParent ((Widget
) m_mainWidget
);
612 XtVaGetValues (parent
,
613 XmNhorizontalScrollBar
, &hsb
,
614 XmNverticalScrollBar
, &vsb
,
617 /* TODO: should scrollbars be affected? Should probably have separate
618 * function to change them (by default, taken from wxSystemSettings)
620 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
621 wxDoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, TRUE
);
622 wxDoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, TRUE
);
625 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(hsb
)),
628 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(vsb
)),
631 // MBN: why change parent's background? It looks really ugly.
632 // wxDoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, TRUE);
635 void wxListBox::ChangeForegroundColour()
637 wxWindow::ChangeForegroundColour();
639 Widget parent
= XtParent ((Widget
) m_mainWidget
);
642 XtVaGetValues(parent
,
643 XmNhorizontalScrollBar
, &hsb
,
644 XmNverticalScrollBar
, &vsb
,
647 /* TODO: should scrollbars be affected? Should probably have separate
648 function to change them (by default, taken from wxSystemSettings)
650 wxDoChangeForegroundColour((WXWidget) hsb, m_foregroundColour);
651 wxDoChangeForegroundColour((WXWidget) vsb, m_foregroundColour);
652 wxDoChangeForegroundColour((WXWidget) parent, m_foregroundColour);
656 int wxListBox::GetCount() const
661 #define LIST_SCROLL_SPACING 6
663 wxSize
wxDoGetListBoxBestSize( Widget listWidget
, const wxWindow
* window
)
666 Dimension spacing
, highlight
, xmargin
, ymargin
, shadow
;
670 XtVaGetValues( listWidget
,
672 XmNlistSpacing
, &spacing
,
673 XmNhighlightThickness
, &highlight
,
674 XmNlistMarginWidth
, &xmargin
,
675 XmNlistMarginHeight
, &ymargin
,
676 XmNshadowThickness
, &shadow
,
679 for( size_t i
= 0; i
< (size_t)max
; ++i
)
681 window
->GetTextExtent( wxDoGetStringInList( listWidget
, i
), &x
, &y
);
682 width
= wxMax( width
, x
);
685 // use some arbitrary value if there are no strings
690 window
->GetTextExtent( "v", &x
, &y
);
692 // make it a little larger than widest string, plus the scrollbar
693 width
+= wxSystemSettings::GetMetric( wxSYS_VSCROLL_X
)
694 + 2 * highlight
+ LIST_SCROLL_SPACING
+ 2 * xmargin
+ 2 * shadow
;
696 // at least 3 items, at most 10
697 int height
= wxMax( 3, wxMin( 10, max
) ) *
698 ( y
+ spacing
+ 2 * highlight
) + 2 * ymargin
+ 2 * shadow
;
700 return wxSize( width
, height
);
703 wxSize
wxListBox::DoGetBestSize() const
705 return wxDoGetListBoxBestSize( (Widget
)m_mainWidget
, this );