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();
97 XtSetArg( args
[0], XmNlistSizePolicy
, XmCONSTANT
); ++count
;
98 XtSetArg( args
[1], XmNselectionPolicy
,
99 ( m_windowStyle
& wxLB_MULTIPLE
) ? XmMULTIPLE_SELECT
:
100 ( m_windowStyle
& wxLB_EXTENDED
) ? XmEXTENDED_SELECT
:
103 if( m_windowStyle
& wxLB_ALWAYS_SB
)
105 XtSetArg( args
[2], XmNscrollBarDisplayPolicy
, XmSTATIC
);
109 Widget listWidget
= XmCreateScrolledList(parentWidget
,
110 (char*)name
.c_str(), args
, count
);
112 m_mainWidget
= (WXWidget
) listWidget
;
116 XtManageChild (listWidget
);
119 long height
= size
.y
;
125 XtAddCallback (listWidget
,
126 XmNbrowseSelectionCallback
,
127 (XtCallbackProc
) wxListBoxCallback
,
129 XtAddCallback (listWidget
,
130 XmNextendedSelectionCallback
,
131 (XtCallbackProc
) wxListBoxCallback
,
133 XtAddCallback (listWidget
,
134 XmNmultipleSelectionCallback
,
135 (XtCallbackProc
) wxListBoxCallback
,
137 XtAddCallback (listWidget
,
138 XmNdefaultActionCallback
,
139 (XtCallbackProc
) wxListBoxCallback
,
144 SetCanAddEventHandler(TRUE
);
145 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
146 pos
.x
, pos
.y
, width
, height
);
148 ChangeBackgroundColour();
153 wxListBox::~wxListBox()
155 if( HasClientObjectData() )
156 m_clientDataDict
.DestroyData();
159 void wxListBox::SetSelectionPolicy()
161 Widget listBox
= (Widget
)m_mainWidget
;
164 XtSetArg( args
[0], XmNlistSizePolicy
, XmCONSTANT
);
166 XtSetArg( args
[1], XmNselectionPolicy
,
167 ( m_windowStyle
& wxLB_MULTIPLE
) ? XmMULTIPLE_SELECT
:
168 ( m_windowStyle
& wxLB_EXTENDED
) ? XmEXTENDED_SELECT
:
171 XtSetValues( listBox
, args
, 2 );
174 void wxListBox::DoSetFirstItem( int N
)
180 XtVaGetValues ((Widget
) m_mainWidget
,
181 XmNvisibleItemCount
, &count
,
182 XmNitemCount
, &length
,
184 if ((N
+ count
) >= length
)
186 XmListSetPos ((Widget
) m_mainWidget
, N
+ 1);
189 void wxListBox::Delete(int N
)
191 wxSizeKeeper
sk( this );
192 Widget listBox
= (Widget
) m_mainWidget
;
194 bool managed
= XtIsManaged(listBox
);
197 XtUnmanageChild (listBox
);
199 XmListDeletePos (listBox
, N
+ 1);
202 XtManageChild (listBox
);
205 m_clientDataDict
.Delete(N
, HasClientObjectData());
209 int wxListBox::DoAppend(const wxString
& item
)
211 wxSizeKeeper
sk( this );
212 Widget listBox
= (Widget
) m_mainWidget
;
214 bool managed
= XtIsManaged(listBox
);
217 XtUnmanageChild (listBox
);
219 XtVaGetValues (listBox
, XmNitemCount
, &n
, NULL
);
220 wxXmString
text( item
);
221 // XmListAddItem(listBox, text, n + 1);
222 XmListAddItemUnselected (listBox
, text(), 0);
224 // It seems that if the list is cleared, we must re-ask for
225 // selection policy!!
226 SetSelectionPolicy();
229 XtManageChild (listBox
);
234 return GetCount() - 1;
237 void wxListBox::DoSetItems(const wxArrayString
& items
, void** clientData
)
239 wxSizeKeeper
sk( this );
240 Widget listBox
= (Widget
) m_mainWidget
;
242 if( HasClientObjectData() )
243 m_clientDataDict
.DestroyData();
245 bool managed
= XtIsManaged(listBox
);
248 XtUnmanageChild (listBox
);
249 XmString
*text
= new XmString
[items
.GetCount()];
251 for (i
= 0; i
< items
.GetCount(); ++i
)
252 text
[i
] = XmStringCreateSimple ((char*)items
[i
].c_str());
255 for (i
= 0; i
< items
.GetCount(); ++i
)
256 m_clientDataDict
.Set(i
, (wxClientData
*)clientData
[i
], FALSE
);
258 XmListAddItems (listBox
, text
, items
.GetCount(), 0);
259 for (i
= 0; i
< items
.GetCount(); i
++)
260 XmStringFree (text
[i
]);
263 // It seems that if the list is cleared, we must re-ask for
264 // selection policy!!
265 SetSelectionPolicy();
268 XtManageChild (listBox
);
272 m_noItems
= items
.GetCount();
275 int wxListBox::FindString(const wxString
& s
) const
278 int *positions
= NULL
;
279 int no_positions
= 0;
280 bool success
= XmListGetMatchPos ((Widget
) m_mainWidget
, str(),
281 &positions
, &no_positions
);
285 int pos
= positions
[0];
287 XtFree ((char *) positions
);
294 void wxListBox::Clear()
299 wxSizeKeeper
sk( this );
300 Widget listBox
= (Widget
) m_mainWidget
;
302 XmListDeleteAllItems (listBox
);
303 if( HasClientObjectData() )
304 m_clientDataDict
.DestroyData();
311 void wxListBox::SetSelection(int N
, bool select
)
317 if (m_windowStyle
& wxLB_MULTIPLE
)
319 int *selections
= NULL
;
320 int n
= GetSelections (&selections
);
322 // This hack is supposed to work, to make it possible
323 // to select more than one item, but it DOESN'T under Motif 1.1.
325 XtVaSetValues ((Widget
) m_mainWidget
,
326 XmNselectionPolicy
, XmMULTIPLE_SELECT
,
330 for (i
= 0; i
< n
; i
++)
331 XmListSelectPos ((Widget
) m_mainWidget
,
332 selections
[i
] + 1, FALSE
);
334 XmListSelectPos ((Widget
) m_mainWidget
, N
+ 1, FALSE
);
336 XtVaSetValues ((Widget
) m_mainWidget
,
337 XmNselectionPolicy
, XmEXTENDED_SELECT
,
342 XmListSelectPos ((Widget
) m_mainWidget
, N
+ 1, FALSE
);
346 XmListDeselectPos ((Widget
) m_mainWidget
, N
+ 1);
348 m_inSetValue
= FALSE
;
351 bool wxListBox::IsSelected(int N
) const
353 // In Motif, no simple way to determine if the item is selected.
354 wxArrayInt theSelections
;
355 int count
= GetSelections (theSelections
);
361 for (j
= 0; j
< count
; j
++)
362 if (theSelections
[j
] == N
)
368 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
370 m_clientDataDict
.Set(n
, clientData
, FALSE
);
373 wxClientData
* wxListBox::DoGetItemClientObject(int n
) const
375 return m_clientDataDict
.Get(n
);
378 void *wxListBox::DoGetItemClientData(int N
) const
380 return (void*)m_clientDataDict
.Get(N
);
383 void wxListBox::DoSetItemClientData(int N
, void *Client_data
)
385 m_clientDataDict
.Set(N
, (wxClientData
*)Client_data
, FALSE
);
388 // Return number of selections and an array of selected integers
389 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
393 Widget listBox
= (Widget
) m_mainWidget
;
396 bool flag
= XmListGetSelectedPos (listBox
, &posList
, &posCnt
);
401 aSelections
.Alloc(posCnt
);
404 for (i
= 0; i
< posCnt
; i
++)
405 aSelections
.Add(posList
[i
] - 1);
407 XtFree ((char *) posList
);
417 // Get single selection, for single choice list items
418 int wxListBox::GetSelection() const
420 Widget listBox
= (Widget
) m_mainWidget
;
423 bool flag
= XmListGetSelectedPos (listBox
, &posList
, &posCnt
);
429 XtFree ((char *) posList
);
436 // Find string for position
437 wxString
wxListBox::GetString(int N
) const
439 Widget listBox
= (Widget
) m_mainWidget
;
442 XtVaGetValues (listBox
, XmNitemCount
, &n
, XmNitems
, &strlist
, NULL
);
443 if (N
<= n
&& N
>= 0)
446 if (XmStringGetLtoR (strlist
[N
], XmSTRING_DEFAULT_CHARSET
, &txt
))
453 return wxEmptyString
;
456 return wxEmptyString
;
459 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
461 wxSizeKeeper
sk( this );
462 Widget listBox
= (Widget
) m_mainWidget
;
464 bool managed
= XtIsManaged(listBox
);
467 XtUnmanageChild(listBox
);
469 XmString
*text
= new XmString
[items
.GetCount()];
471 // Steve Hammes: Motif 1.1 compatibility
472 // #if XmVersion > 1100
473 // Corrected by Sergey Krasnov from Steve Hammes' code
475 for (i
= 0; i
< items
.GetCount(); i
++)
476 text
[i
] = XmStringCreateSimple((char*)items
[i
].c_str());
477 XmListAddItemsUnselected(listBox
, text
, items
.GetCount(), pos
+1);
479 for (i
= 0; i
< items
.GetCount(); i
++)
481 text
[i
] = XmStringCreateSimple((char*)items
[i
].c_str());
482 // Another Sergey correction
483 XmListAddItemUnselected(listBox
, text
[i
], pos
+i
+1);
486 for (i
= 0; i
< items
.GetCount(); i
++)
487 XmStringFree(text
[i
]);
490 // It seems that if the list is cleared, we must re-ask for
491 // selection policy!!
492 SetSelectionPolicy();
495 XtManageChild(listBox
);
499 m_noItems
+= items
.GetCount();
502 void wxListBox::SetString(int N
, const wxString
& s
)
504 wxSizeKeeper
sk( this );
505 Widget listBox
= (Widget
) m_mainWidget
;
507 wxXmString
text( s
);
509 // delete the item and add it again.
510 // FIXME isn't there a way to change it in place?
511 XmListDeletePos (listBox
, N
+1);
512 XmListAddItem (listBox
, text(), N
+1);
517 void wxListBox::Command (wxCommandEvent
& event
)
519 if (event
.m_extraLong
)
520 SetSelection (event
.m_commandInt
);
523 Deselect (event
.m_commandInt
);
526 ProcessCommand (event
);
529 void wxListBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
530 XmListCallbackStruct
* cbs
)
532 wxListBox
*item
= (wxListBox
*) clientData
;
534 if (item
->InSetValue())
539 if( cbs
->reason
== XmCR_DEFAULT_ACTION
)
540 evtType
= wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
;
542 evtType
= wxEVT_COMMAND_LISTBOX_SELECTED
;
544 int n
= cbs
->item_position
- 1;
545 wxCommandEvent
event (evtType
, item
->GetId());
546 if ( item
->HasClientObjectData() )
547 event
.SetClientObject( item
->GetClientObject(n
) );
548 else if ( item
->HasClientUntypedData() )
549 event
.SetClientData( item
->GetClientData(n
) );
550 event
.m_commandInt
= n
;
551 event
.m_extraLong
= TRUE
;
552 event
.SetEventObject(item
);
553 event
.SetString( item
->GetString( n
) );
556 if( NULL
!= cbs
->event
&& cbs
->event
->type
== ButtonRelease
)
558 XButtonEvent
* evt
= (XButtonEvent
*)cbs
->event
;
565 case XmCR_MULTIPLE_SELECT
:
566 case XmCR_BROWSE_SELECT
:
567 #if wxUSE_CHECKLISTBOX
568 item
->DoToggleItem( n
, x
);
570 case XmCR_DEFAULT_ACTION
:
571 item
->GetEventHandler()->ProcessEvent(event
);
573 case XmCR_EXTENDED_SELECT
:
574 switch (cbs
->selection_type
)
579 item
->DoToggleItem( n
, x
);
580 item
->GetEventHandler()->ProcessEvent(event
);
587 WXWidget
wxListBox::GetTopWidget() const
589 return (WXWidget
) XtParent( (Widget
) m_mainWidget
);
592 void wxListBox::ChangeBackgroundColour()
594 wxWindow::ChangeBackgroundColour();
596 Widget parent
= XtParent ((Widget
) m_mainWidget
);
599 XtVaGetValues (parent
,
600 XmNhorizontalScrollBar
, &hsb
,
601 XmNverticalScrollBar
, &vsb
,
604 /* TODO: should scrollbars be affected? Should probably have separate
605 * function to change them (by default, taken from wxSystemSettings)
607 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
608 DoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, TRUE
);
609 DoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, TRUE
);
612 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(hsb
)),
615 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(vsb
)),
618 DoChangeBackgroundColour((WXWidget
) parent
, m_backgroundColour
, TRUE
);
621 void wxListBox::ChangeForegroundColour()
623 wxWindow::ChangeForegroundColour();
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 DoChangeForegroundColour((WXWidget) hsb, m_foregroundColour);
637 DoChangeForegroundColour((WXWidget) vsb, m_foregroundColour);
638 DoChangeForegroundColour((WXWidget) parent, m_foregroundColour);
642 int wxListBox::GetCount() const