1 ///////////////////////////////////////////////////////////////////////////////
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"
16 #define XtParent XTPARENT
17 #define XtDisplay XTDISPLAY
20 # include "wx/listbox.h"
21 #include "wx/settings.h"
22 #include "wx/dynarray.h"
25 #include "wx/arrstr.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();
94 Display
* dpy
= XtDisplay(parentWidget
);
98 XtSetArg( args
[count
], XmNlistSizePolicy
, XmCONSTANT
); ++count
;
99 XtSetArg( args
[count
], XmNselectionPolicy
,
100 ( m_windowStyle
& wxLB_MULTIPLE
) ? XmMULTIPLE_SELECT
:
101 ( m_windowStyle
& wxLB_EXTENDED
) ? XmEXTENDED_SELECT
:
106 XtSetArg( args
[count
],
107 (String
)wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
) );
110 if( m_windowStyle
& wxLB_ALWAYS_SB
)
112 XtSetArg( args
[count
], XmNscrollBarDisplayPolicy
, XmSTATIC
);
117 XmCreateScrolledList(parentWidget
,
118 wxConstCast(name
.c_str(), char), args
, count
);
120 m_mainWidget
= (WXWidget
) listWidget
;
124 XtManageChild (listWidget
);
126 wxSize best
= GetBestSize();
127 if( size
.x
!= -1 ) best
.x
= size
.x
;
128 if( size
.y
!= -1 ) best
.y
= size
.y
;
130 XtAddCallback (listWidget
,
131 XmNbrowseSelectionCallback
,
132 (XtCallbackProc
) wxListBoxCallback
,
134 XtAddCallback (listWidget
,
135 XmNextendedSelectionCallback
,
136 (XtCallbackProc
) wxListBoxCallback
,
138 XtAddCallback (listWidget
,
139 XmNmultipleSelectionCallback
,
140 (XtCallbackProc
) wxListBoxCallback
,
142 XtAddCallback (listWidget
,
143 XmNdefaultActionCallback
,
144 (XtCallbackProc
) wxListBoxCallback
,
147 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
148 pos
.x
, pos
.y
, best
.x
, best
.y
);
150 ChangeBackgroundColour();
155 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
158 const wxArrayString
& choices
,
160 const wxValidator
& validator
,
161 const wxString
& name
)
163 wxCArrayString
chs(choices
);
164 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
165 style
, validator
, name
);
168 wxListBox::~wxListBox()
170 if( HasClientObjectData() )
171 m_clientDataDict
.DestroyData();
174 void wxListBox::SetSelectionPolicy()
176 Widget listBox
= (Widget
)m_mainWidget
;
179 XtSetArg( args
[0], XmNlistSizePolicy
, XmCONSTANT
);
181 XtSetArg( args
[1], XmNselectionPolicy
,
182 ( m_windowStyle
& wxLB_MULTIPLE
) ? XmMULTIPLE_SELECT
:
183 ( m_windowStyle
& wxLB_EXTENDED
) ? XmEXTENDED_SELECT
:
186 XtSetValues( listBox
, args
, 2 );
189 void wxListBox::DoSetFirstItem( int N
)
195 XtVaGetValues ((Widget
) m_mainWidget
,
196 XmNvisibleItemCount
, &count
,
197 XmNitemCount
, &length
,
199 if ((N
+ count
) >= length
)
201 XmListSetPos ((Widget
) m_mainWidget
, N
+ 1);
204 void wxListBox::Delete(int N
)
206 wxSizeKeeper
sk( this );
207 Widget listBox
= (Widget
) m_mainWidget
;
209 bool managed
= XtIsManaged(listBox
);
212 XtUnmanageChild (listBox
);
214 XmListDeletePos (listBox
, N
+ 1);
217 XtManageChild (listBox
);
220 m_clientDataDict
.Delete(N
, HasClientObjectData());
224 int wxListBox::DoAppend(const wxString
& item
)
226 wxSizeKeeper
sk( this );
227 Widget listBox
= (Widget
) m_mainWidget
;
229 bool managed
= XtIsManaged(listBox
);
232 XtUnmanageChild (listBox
);
234 XtVaGetValues (listBox
, XmNitemCount
, &n
, NULL
);
235 wxXmString
text( item
);
236 // XmListAddItem(listBox, text, n + 1);
237 XmListAddItemUnselected (listBox
, text(), 0);
239 // It seems that if the list is cleared, we must re-ask for
240 // selection policy!!
241 SetSelectionPolicy();
244 XtManageChild (listBox
);
249 return GetCount() - 1;
252 void wxListBox::DoSetItems(const wxArrayString
& items
, void** clientData
)
254 wxSizeKeeper
sk( this );
255 Widget listBox
= (Widget
) m_mainWidget
;
257 if( HasClientObjectData() )
258 m_clientDataDict
.DestroyData();
260 bool managed
= XtIsManaged(listBox
);
263 XtUnmanageChild (listBox
);
264 XmString
*text
= new XmString
[items
.GetCount()];
266 for (i
= 0; i
< items
.GetCount(); ++i
)
267 text
[i
] = wxStringToXmString (items
[i
]);
270 for (i
= 0; i
< items
.GetCount(); ++i
)
271 m_clientDataDict
.Set(i
, (wxClientData
*)clientData
[i
], false);
273 XmListAddItems (listBox
, text
, items
.GetCount(), 0);
274 for (i
= 0; i
< items
.GetCount(); i
++)
275 XmStringFree (text
[i
]);
278 // It seems that if the list is cleared, we must re-ask for
279 // selection policy!!
280 SetSelectionPolicy();
283 XtManageChild (listBox
);
287 m_noItems
= items
.GetCount();
290 int wxDoFindStringInList(Widget w
, const wxString
& s
)
293 int *positions
= NULL
;
294 int no_positions
= 0;
295 bool success
= XmListGetMatchPos (w
, str(),
296 &positions
, &no_positions
);
300 int pos
= positions
[0];
302 XtFree ((char *) positions
);
309 int wxListBox::FindString(const wxString
& s
) const
311 return wxDoFindStringInList( (Widget
)m_mainWidget
, s
);
314 void wxListBox::Clear()
319 wxSizeKeeper
sk( this );
320 Widget listBox
= (Widget
) m_mainWidget
;
322 XmListDeleteAllItems (listBox
);
323 if( HasClientObjectData() )
324 m_clientDataDict
.DestroyData();
331 void wxListBox::DoSetSelection(int N
, bool select
)
337 if (m_windowStyle
& wxLB_MULTIPLE
)
339 int *selections
= NULL
;
340 int n
= GetSelections (&selections
);
342 // This hack is supposed to work, to make it possible
343 // to select more than one item, but it DOESN'T under Motif 1.1.
345 XtVaSetValues ((Widget
) m_mainWidget
,
346 XmNselectionPolicy
, XmMULTIPLE_SELECT
,
350 for (i
= 0; i
< n
; i
++)
351 XmListSelectPos ((Widget
) m_mainWidget
,
352 selections
[i
] + 1, False
);
354 XmListSelectPos ((Widget
) m_mainWidget
, N
+ 1, False
);
356 XtVaSetValues ((Widget
) m_mainWidget
,
357 XmNselectionPolicy
, XmEXTENDED_SELECT
,
362 XmListSelectPos ((Widget
) m_mainWidget
, N
+ 1, False
);
366 XmListDeselectPos ((Widget
) m_mainWidget
, N
+ 1);
368 m_inSetValue
= false;
371 bool wxListBox::IsSelected(int N
) const
373 // In Motif, no simple way to determine if the item is selected.
374 wxArrayInt theSelections
;
375 int count
= GetSelections (theSelections
);
381 for (j
= 0; j
< count
; j
++)
382 if (theSelections
[j
] == N
)
388 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
390 m_clientDataDict
.Set(n
, clientData
, false);
393 wxClientData
* wxListBox::DoGetItemClientObject(int n
) const
395 return m_clientDataDict
.Get(n
);
398 void *wxListBox::DoGetItemClientData(int N
) const
400 return (void*)m_clientDataDict
.Get(N
);
403 void wxListBox::DoSetItemClientData(int N
, void *Client_data
)
405 m_clientDataDict
.Set(N
, (wxClientData
*)Client_data
, false);
408 // Return number of selections and an array of selected integers
409 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
413 Widget listBox
= (Widget
) m_mainWidget
;
416 bool flag
= XmListGetSelectedPos (listBox
, &posList
, &posCnt
);
421 aSelections
.Alloc(posCnt
);
424 for (i
= 0; i
< posCnt
; i
++)
425 aSelections
.Add(posList
[i
] - 1);
427 XtFree ((char *) posList
);
437 // Get single selection, for single choice list items
438 int wxDoGetSelectionInList(Widget listBox
)
442 bool flag
= XmListGetSelectedPos (listBox
, &posList
, &posCnt
);
448 XtFree ((char *) posList
);
455 int wxListBox::GetSelection() const
457 return wxDoGetSelectionInList((Widget
) m_mainWidget
);
460 // Find string for position
461 wxString
wxDoGetStringInList( Widget listBox
, int n
)
465 XtVaGetValues( listBox
,
466 XmNitemCount
, &count
,
469 if( n
< count
&& n
>= 0 )
470 return wxXmStringToString( strlist
[n
] );
472 return wxEmptyString
;
475 wxString
wxListBox::GetString( int n
) const
477 return wxDoGetStringInList( (Widget
)m_mainWidget
, n
);
480 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
482 wxSizeKeeper
sk( this );
483 Widget listBox
= (Widget
) m_mainWidget
;
485 bool managed
= XtIsManaged(listBox
);
488 XtUnmanageChild(listBox
);
490 XmString
*text
= new XmString
[items
.GetCount()];
492 // Steve Hammes: Motif 1.1 compatibility
493 // #if XmVersion > 1100
494 // Corrected by Sergey Krasnov from Steve Hammes' code
496 for (i
= 0; i
< items
.GetCount(); i
++)
497 text
[i
] = wxStringToXmString(items
[i
]);
498 XmListAddItemsUnselected(listBox
, text
, items
.GetCount(), pos
+1);
500 for (i
= 0; i
< items
.GetCount(); i
++)
502 text
[i
] = wxStringToXmString(items
[i
]);
503 // Another Sergey correction
504 XmListAddItemUnselected(listBox
, text
[i
], pos
+i
+1);
507 for (i
= 0; i
< items
.GetCount(); i
++)
508 XmStringFree(text
[i
]);
511 // It seems that if the list is cleared, we must re-ask for
512 // selection policy!!
513 SetSelectionPolicy();
516 XtManageChild(listBox
);
520 m_noItems
+= items
.GetCount();
523 void wxListBox::SetString(int N
, const wxString
& s
)
525 wxSizeKeeper
sk( this );
526 Widget listBox
= (Widget
) m_mainWidget
;
528 wxXmString
text( s
);
530 // delete the item and add it again.
531 // FIXME isn't there a way to change it in place?
532 XmListDeletePos (listBox
, N
+1);
533 XmListAddItem (listBox
, text(), N
+1);
538 void wxListBox::Command (wxCommandEvent
& event
)
540 if (event
.GetExtraLong())
541 SetSelection (event
.GetInt());
544 Deselect (event
.GetInt());
547 ProcessCommand (event
);
550 void wxListBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
551 XmListCallbackStruct
* cbs
)
553 wxListBox
*item
= (wxListBox
*) clientData
;
555 if (item
->InSetValue())
560 if( cbs
->reason
== XmCR_DEFAULT_ACTION
)
561 evtType
= wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
;
563 evtType
= wxEVT_COMMAND_LISTBOX_SELECTED
;
565 int n
= cbs
->item_position
- 1;
566 wxCommandEvent
event (evtType
, item
->GetId());
567 if ( item
->HasClientObjectData() )
568 event
.SetClientObject( item
->GetClientObject(n
) );
569 else if ( item
->HasClientUntypedData() )
570 event
.SetClientData( item
->GetClientData(n
) );
572 event
.SetExtraLong(true);
573 event
.SetEventObject(item
);
574 event
.SetString( item
->GetString( n
) );
577 if( NULL
!= cbs
->event
&& cbs
->event
->type
== ButtonRelease
)
579 XButtonEvent
* evt
= (XButtonEvent
*)cbs
->event
;
586 case XmCR_MULTIPLE_SELECT
:
587 case XmCR_BROWSE_SELECT
:
588 #if wxUSE_CHECKLISTBOX
589 item
->DoToggleItem( n
, x
);
591 case XmCR_DEFAULT_ACTION
:
592 item
->GetEventHandler()->ProcessEvent(event
);
594 case XmCR_EXTENDED_SELECT
:
595 switch (cbs
->selection_type
)
600 item
->DoToggleItem( n
, x
);
601 item
->GetEventHandler()->ProcessEvent(event
);
608 WXWidget
wxListBox::GetTopWidget() const
610 return (WXWidget
) XtParent( (Widget
) m_mainWidget
);
613 void wxListBox::ChangeBackgroundColour()
615 wxWindow::ChangeBackgroundColour();
617 Widget parent
= XtParent ((Widget
) m_mainWidget
);
620 XtVaGetValues (parent
,
621 XmNhorizontalScrollBar
, &hsb
,
622 XmNverticalScrollBar
, &vsb
,
625 /* TODO: should scrollbars be affected? Should probably have separate
626 * function to change them (by default, taken from wxSystemSettings)
628 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
629 wxDoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, true);
630 wxDoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, true);
633 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(hsb
)),
636 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(vsb
)),
639 // MBN: why change parent's background? It looks really ugly.
640 // wxDoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, true);
643 void wxListBox::ChangeForegroundColour()
645 wxWindow::ChangeForegroundColour();
647 Widget parent
= XtParent ((Widget
) m_mainWidget
);
650 XtVaGetValues(parent
,
651 XmNhorizontalScrollBar
, &hsb
,
652 XmNverticalScrollBar
, &vsb
,
655 /* TODO: should scrollbars be affected? Should probably have separate
656 function to change them (by default, taken from wxSystemSettings)
658 wxDoChangeForegroundColour((WXWidget) hsb, m_foregroundColour);
659 wxDoChangeForegroundColour((WXWidget) vsb, m_foregroundColour);
660 wxDoChangeForegroundColour((WXWidget) parent, m_foregroundColour);
664 int wxListBox::GetCount() const
669 #define LIST_SCROLL_SPACING 6
671 wxSize
wxDoGetListBoxBestSize( Widget listWidget
, const wxWindow
* window
)
674 Dimension spacing
, highlight
, xmargin
, ymargin
, shadow
;
678 XtVaGetValues( listWidget
,
680 XmNlistSpacing
, &spacing
,
681 XmNhighlightThickness
, &highlight
,
682 XmNlistMarginWidth
, &xmargin
,
683 XmNlistMarginHeight
, &ymargin
,
684 XmNshadowThickness
, &shadow
,
687 for( size_t i
= 0; i
< (size_t)max
; ++i
)
689 window
->GetTextExtent( wxDoGetStringInList( listWidget
, i
), &x
, &y
);
690 width
= wxMax( width
, x
);
693 // use some arbitrary value if there are no strings
698 window
->GetTextExtent( "v", &x
, &y
);
700 // make it a little larger than widest string, plus the scrollbar
701 width
+= wxSystemSettings::GetMetric( wxSYS_VSCROLL_X
)
702 + 2 * highlight
+ LIST_SCROLL_SPACING
+ 2 * xmargin
+ 2 * shadow
;
704 // at least 3 items, at most 10
705 int height
= wxMax( 3, wxMin( 10, max
) ) *
706 ( y
+ spacing
+ 2 * highlight
) + 2 * ymargin
+ 2 * shadow
;
708 return wxSize( width
, height
);
711 wxSize
wxListBox::DoGetBestSize() const
713 return wxDoGetListBoxBestSize( (Widget
)m_mainWidget
, this );