1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/listbox.cpp
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
16 #include "wx/listbox.h"
19 #include "wx/dynarray.h"
22 #include "wx/settings.h"
23 #include "wx/arrstr.h"
27 #pragma message disable nosimpint
31 #pragma message enable nosimpint
33 #include "wx/motif/private.h"
35 static void wxListBoxCallback(Widget w
,
37 XmListCallbackStruct
* cbs
);
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // helper class to reduce code duplication
50 wxSizeKeeper( wxWindow
* w
)
53 m_wnd
->GetSize( &m_w
, &m_h
);
54 m_wnd
->GetPosition( &m_x
, &m_y
);
61 m_wnd
->GetSize( &x
, &y
);
62 if( x
!= m_x
|| y
!= m_y
)
63 m_wnd
->SetSize( m_x
, m_y
, m_w
, m_h
);
67 // ============================================================================
68 // list box control implementation
69 // ============================================================================
72 wxListBox::wxListBox()
77 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
80 int n
, const wxString choices
[],
82 const wxValidator
& validator
,
85 if( !wxControl::CreateControl( parent
, id
, pos
, size
, style
,
90 m_noItems
= (unsigned int)n
;
92 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
93 Display
* dpy
= XtDisplay(parentWidget
);
97 XtSetArg( args
[count
], XmNlistSizePolicy
, XmCONSTANT
); ++count
;
98 XtSetArg( args
[count
], XmNselectionPolicy
,
99 ( m_windowStyle
& wxLB_MULTIPLE
) ? XmMULTIPLE_SELECT
:
100 ( m_windowStyle
& wxLB_EXTENDED
) ? XmEXTENDED_SELECT
:
105 XtSetArg( args
[count
],
106 (String
)wxFont::GetFontTag(), m_font
.GetFontTypeC(dpy
) );
109 if( m_windowStyle
& wxLB_ALWAYS_SB
)
111 XtSetArg( args
[count
], XmNscrollBarDisplayPolicy
, XmSTATIC
);
116 XmCreateScrolledList(parentWidget
,
117 name
.char_str(), args
, count
);
119 m_mainWidget
= (WXWidget
) listWidget
;
123 XtManageChild (listWidget
);
125 wxSize best
= GetBestSize();
126 if( size
.x
!= -1 ) best
.x
= size
.x
;
127 if( size
.y
!= -1 ) best
.y
= size
.y
;
129 XtAddCallback (listWidget
,
130 XmNbrowseSelectionCallback
,
131 (XtCallbackProc
) wxListBoxCallback
,
133 XtAddCallback (listWidget
,
134 XmNextendedSelectionCallback
,
135 (XtCallbackProc
) wxListBoxCallback
,
137 XtAddCallback (listWidget
,
138 XmNmultipleSelectionCallback
,
139 (XtCallbackProc
) wxListBoxCallback
,
141 XtAddCallback (listWidget
,
142 XmNdefaultActionCallback
,
143 (XtCallbackProc
) wxListBoxCallback
,
147 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
148 pos
.x
, pos
.y
, best
.x
, best
.y
);
153 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
156 const wxArrayString
& choices
,
158 const wxValidator
& validator
,
159 const wxString
& name
)
161 wxCArrayString
chs(choices
);
162 return Create(parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
163 style
, validator
, name
);
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
)
188 XtVaGetValues ((Widget
) m_mainWidget
,
189 XmNvisibleItemCount
, &count
,
190 XmNitemCount
, &length
,
192 if ((N
+ count
) >= length
)
194 XmListSetPos ((Widget
) m_mainWidget
, N
+ 1);
197 void wxListBox::DoDeleteOneItem(unsigned int n
)
199 Widget listBox
= (Widget
) m_mainWidget
;
201 XmListDeletePos (listBox
, n
+ 1);
203 wxListBoxBase::DoDeleteOneItem(n
);
207 int wxDoFindStringInList(Widget w
, const wxString
& s
)
210 int *positions
= NULL
;
211 int no_positions
= 0;
212 bool success
= XmListGetMatchPos (w
, str(),
213 &positions
, &no_positions
);
215 if (success
&& positions
)
217 int pos
= positions
[0];
218 XtFree ((char *) positions
);
225 int wxListBox::FindString(const wxString
& s
, bool WXUNUSED(bCase
)) const
227 // FIXME: back to base class for not supported value of bCase
229 return wxDoFindStringInList( (Widget
)m_mainWidget
, s
);
232 void wxListBox::DoClear()
237 wxSizeKeeper
sk( this );
238 Widget listBox
= (Widget
) m_mainWidget
;
240 XmListDeleteAllItems (listBox
);
244 wxListBoxBase::DoClear();
248 void wxListBox::DoSetSelection(int N
, bool select
)
254 if (m_windowStyle
& wxLB_MULTIPLE
)
256 int *selections
= NULL
;
257 int n
= GetSelections (&selections
);
259 // This hack is supposed to work, to make it possible
260 // to select more than one item, but it DOESN'T under Motif 1.1.
262 XtVaSetValues ((Widget
) m_mainWidget
,
263 XmNselectionPolicy
, XmMULTIPLE_SELECT
,
267 for (i
= 0; i
< n
; i
++)
268 XmListSelectPos ((Widget
) m_mainWidget
,
269 selections
[i
] + 1, False
);
271 XmListSelectPos ((Widget
) m_mainWidget
, N
+ 1, False
);
273 XtVaSetValues ((Widget
) m_mainWidget
,
274 XmNselectionPolicy
, XmEXTENDED_SELECT
,
279 XmListSelectPos ((Widget
) m_mainWidget
, N
+ 1, False
);
283 XmListDeselectPos ((Widget
) m_mainWidget
, N
+ 1);
285 m_inSetValue
= false;
288 bool wxListBox::IsSelected(int N
) const
290 // In Motif, no simple way to determine if the item is selected.
291 wxArrayInt theSelections
;
292 int count
= GetSelections (theSelections
);
298 for (j
= 0; j
< count
; j
++)
299 if (theSelections
[j
] == N
)
305 // Return number of selections and an array of selected integers
306 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
310 Widget listBox
= (Widget
) m_mainWidget
;
313 bool flag
= XmListGetSelectedPos (listBox
, &posList
, &posCnt
);
318 aSelections
.Alloc(posCnt
);
321 for (i
= 0; i
< posCnt
; i
++)
322 aSelections
.Add(posList
[i
] - 1);
324 XtFree ((char *) posList
);
334 // Get single selection, for single choice list items
335 int wxDoGetSelectionInList(Widget listBox
)
339 bool flag
= XmListGetSelectedPos (listBox
, &posList
, &posCnt
);
345 XtFree ((char *) posList
);
352 int wxListBox::GetSelection() const
354 return wxDoGetSelectionInList((Widget
) m_mainWidget
);
357 // Find string for position
358 wxString
wxDoGetStringInList( Widget listBox
, int n
)
362 XtVaGetValues( listBox
,
363 XmNitemCount
, &count
,
366 if( n
< count
&& n
>= 0 )
367 return wxXmStringToString( strlist
[n
] );
369 return wxEmptyString
;
372 wxString
wxListBox::GetString(unsigned int n
) const
374 return wxDoGetStringInList( (Widget
)m_mainWidget
, n
);
377 int wxListBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
379 void **clientData
, wxClientDataType type
)
381 Widget listBox
= (Widget
) m_mainWidget
;
383 const unsigned int numItems
= items
.GetCount();
385 XmString
*text
= new XmString
[numItems
];
388 for (i
= 0; i
< numItems
; i
++)
390 text
[i
] = wxStringToXmString(items
[i
]);
392 XmListAddItemsUnselected(listBox
, text
, numItems
, GetMotifPosition(pos
));
393 InsertNewItemsClientData(pos
, numItems
, clientData
, type
);
395 AllocClientData(numItems
);
397 unsigned int idx
= pos
;
398 for ( i
= 0; i
< numItems
; i
++, idx
++ )
400 text
[i
] = wxStringToXmString(items
[i
]);
401 XmListAddItemUnselected(listBox
, text
[i
], GetMotifPosition(idx
));
402 InsertNewItemClientData(idx
, clientData
, i
, type
);
405 for (i
= 0; i
< numItems
; i
++)
406 XmStringFree(text
[i
]);
409 m_noItems
+= numItems
;
411 SetSelectionPolicy();
413 return pos
+ numItems
- 1;
416 void wxListBox::SetString(unsigned int n
, const wxString
& s
)
418 wxSizeKeeper
sk( this );
419 Widget listBox
= (Widget
) m_mainWidget
;
421 wxXmString
text( s
);
423 // delete the item and add it again.
424 // FIXME isn't there a way to change it in place?
425 XmListDeletePos (listBox
, n
+1);
426 XmListAddItem (listBox
, text(), n
+1);
431 void wxListBox::Command (wxCommandEvent
& event
)
433 if (event
.GetExtraLong())
434 SetSelection (event
.GetInt());
437 Deselect (event
.GetInt());
440 ProcessCommand (event
);
443 void wxListBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
444 XmListCallbackStruct
* cbs
)
446 wxListBox
*item
= (wxListBox
*) clientData
;
448 if (item
->InSetValue())
453 if( cbs
->reason
== XmCR_DEFAULT_ACTION
)
454 evtType
= wxEVT_LISTBOX_DCLICK
;
456 evtType
= wxEVT_LISTBOX
;
458 int n
= cbs
->item_position
- 1;
459 wxCommandEvent
event (evtType
, item
->GetId());
460 if ( item
->HasClientObjectData() )
461 event
.SetClientObject( item
->GetClientObject(n
) );
462 else if ( item
->HasClientUntypedData() )
463 event
.SetClientData( item
->GetClientData(n
) );
465 event
.SetExtraLong(true);
466 event
.SetEventObject(item
);
467 event
.SetString( item
->GetString( n
) );
470 if( NULL
!= cbs
->event
&& cbs
->event
->type
== ButtonRelease
)
472 XButtonEvent
* evt
= (XButtonEvent
*)cbs
->event
;
479 case XmCR_MULTIPLE_SELECT
:
480 case XmCR_BROWSE_SELECT
:
481 #if wxUSE_CHECKLISTBOX
482 item
->DoToggleItem( n
, x
);
484 case XmCR_DEFAULT_ACTION
:
485 item
->HandleWindowEvent(event
);
487 case XmCR_EXTENDED_SELECT
:
488 switch (cbs
->selection_type
)
493 item
->DoToggleItem( n
, x
);
494 item
->HandleWindowEvent(event
);
501 WXWidget
wxListBox::GetTopWidget() const
503 return (WXWidget
) XtParent( (Widget
) m_mainWidget
);
506 void wxListBox::ChangeBackgroundColour()
508 wxWindow::ChangeBackgroundColour();
510 Widget parent
= XtParent ((Widget
) m_mainWidget
);
513 XtVaGetValues (parent
,
514 XmNhorizontalScrollBar
, &hsb
,
515 XmNverticalScrollBar
, &vsb
,
518 /* TODO: should scrollbars be affected? Should probably have separate
519 * function to change them (by default, taken from wxSystemSettings)
521 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
522 wxDoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, true);
523 wxDoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, true);
526 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(hsb
)),
529 XmNtroughColor
, backgroundColour
.AllocColour(XtDisplay(vsb
)),
532 // MBN: why change parent's background? It looks really ugly.
533 // wxDoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, true);
536 void wxListBox::ChangeForegroundColour()
538 wxWindow::ChangeForegroundColour();
540 Widget parent
= XtParent ((Widget
) m_mainWidget
);
543 XtVaGetValues(parent
,
544 XmNhorizontalScrollBar
, &hsb
,
545 XmNverticalScrollBar
, &vsb
,
548 /* TODO: should scrollbars be affected? Should probably have separate
549 function to change them (by default, taken from wxSystemSettings)
551 wxDoChangeForegroundColour((WXWidget) hsb, m_foregroundColour);
552 wxDoChangeForegroundColour((WXWidget) vsb, m_foregroundColour);
553 wxDoChangeForegroundColour((WXWidget) parent, m_foregroundColour);
557 unsigned int wxListBox::GetCount() const
562 #define LIST_SCROLL_SPACING 6
564 wxSize
wxDoGetListBoxBestSize( Widget listWidget
, const wxWindow
* window
)
567 Dimension spacing
, highlight
, xmargin
, ymargin
, shadow
;
571 XtVaGetValues( listWidget
,
573 XmNlistSpacing
, &spacing
,
574 XmNhighlightThickness
, &highlight
,
575 XmNlistMarginWidth
, &xmargin
,
576 XmNlistMarginHeight
, &ymargin
,
577 XmNshadowThickness
, &shadow
,
580 for( size_t i
= 0; i
< (size_t)max
; ++i
)
582 window
->GetTextExtent( wxDoGetStringInList( listWidget
, i
), &x
, &y
);
583 width
= wxMax( width
, x
);
586 // use some arbitrary value if there are no strings
591 window
->GetTextExtent( "v", &x
, &y
);
593 // make it a little larger than widest string, plus the scrollbar
594 width
+= wxSystemSettings::GetMetric( wxSYS_VSCROLL_X
)
595 + 2 * highlight
+ LIST_SCROLL_SPACING
+ 2 * xmargin
+ 2 * shadow
;
597 // at least 3 items, at most 10
598 int height
= wxMax( 3, wxMin( 10, max
) ) *
599 ( y
+ spacing
+ 2 * highlight
) + 2 * ymargin
+ 2 * shadow
;
601 return wxSize( width
, height
);
604 wxSize
wxListBox::DoGetBestSize() const
606 return wxDoGetListBoxBestSize( (Widget
)m_mainWidget
, this );
609 #endif // wxUSE_LISTBOX