1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/combobox.cpp
3 // Purpose: wxComboBox class
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/combobox.h"
19 #include "wx/arrstr.h"
23 #pragma message disable nosimpint
27 #pragma message enable nosimpint
30 // use the old, GPL'd combobox
31 #if (XmVersion < 2000)
33 #include "xmcombo/xmcombo.h"
35 #include "wx/motif/private.h"
37 void wxComboBoxCallback (Widget w
, XtPointer clientData
,
38 XmComboBoxSelectionCallbackStruct
* cbs
);
40 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
41 const wxString
& value
,
44 int n
, const wxString choices
[],
46 const wxValidator
& validator
,
49 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
53 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
55 Widget buttonWidget
= XtVaCreateManagedWidget(name
.c_str(),
56 xmComboBoxWidgetClass
, parentWidget
,
60 XmNeditable
, ((style
& wxCB_READONLY
) != wxCB_READONLY
),
61 XmNsorted
, ((style
& wxCB_SORT
) == wxCB_SORT
),
62 XmNstaticList
, ((style
& wxCB_SIMPLE
) == wxCB_SIMPLE
),
66 for (i
= 0; i
< n
; i
++)
68 wxXmString
str( choices
[i
] );
69 XmComboBoxAddItem(buttonWidget
, str(), 0);
70 m_stringArray
.Add(choices
[i
]);
73 m_mainWidget
= (Widget
) buttonWidget
;
75 XtManageChild (buttonWidget
);
79 XtAddCallback (buttonWidget
, XmNselectionCallback
, (XtCallbackProc
) wxComboBoxCallback
,
81 XtAddCallback (buttonWidget
, XmNvalueChangedCallback
, (XtCallbackProc
) wxComboBoxCallback
,
85 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
90 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
91 const wxString
& value
,
94 const wxArrayString
& choices
,
96 const wxValidator
& validator
,
99 wxCArrayString
chs(choices
);
100 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
101 chs
.GetStrings(), style
, validator
, name
);
104 wxComboBox::~wxComboBox()
106 DetachWidget((Widget
) m_mainWidget
); // Removes event handlers
107 XtDestroyWidget((Widget
) m_mainWidget
);
108 m_mainWidget
= (WXWidget
) 0;
111 void wxComboBox::DoSetSize(int x
, int y
,
112 int width
, int WXUNUSED(height
),
115 // Necessary so it doesn't call wxChoice::SetSize
116 wxWindow::DoSetSize(x
, y
, width
, DoGetBestSize().y
, sizeFlags
);
120 // Already defined in include/motif/combobox.h
121 wxString
wxComboBox::GetValue() const
123 char *s
= XmComboBoxGetString ((Widget
) m_mainWidget
);
131 return wxEmptyString
;
135 void wxComboBox::SetValue(const wxString
& value
)
140 XmComboBoxSetString((Widget
)m_mainWidget
, value
.char_str());
141 m_inSetValue
= false;
145 void wxComboBox::SetString(unsigned int WXUNUSED(n
), const wxString
& WXUNUSED(s
))
147 wxFAIL_MSG( wxT("wxComboBox::SetString only implemented for Motif 2.0") );
150 // TODO auto-sorting is not supported by the code
151 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
154 wxClientDataType type
)
156 const unsigned int numItems
= items
.GetCount();
158 AllocClientData(numItems
);
159 for( unsigned int i
= 0; i
< numItems
; ++i
, ++pos
)
161 wxXmString
str( items
[i
].c_str() );
162 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), GetMotifPosition(pos
));
163 m_stringArray
.Insert(items
[i
], pos
);
164 InsertNewItemClientData(pos
, clientData
, i
, type
);
170 void wxComboBox::DoDeleteOneItem(unsigned int n
)
172 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
173 m_stringArray
.RemoveAt(n
);
174 wxControlWithItems::DoDeleteOneItem(n
);
177 void wxComboBox::Clear()
179 XmComboBoxDeleteAllItems((Widget
) m_mainWidget
);
180 m_stringArray
.Clear();
182 wxControlWithItems::DoClear();
185 void wxComboBox::SetSelection (int n
)
187 XmComboBoxSelectPos((Widget
) m_mainWidget
, n
+1, False
);
190 int wxComboBox::GetSelection (void) const
192 int sel
= XmComboBoxGetSelectedPos((Widget
) m_mainWidget
);
199 wxString
wxComboBox::GetString(unsigned int n
) const
201 return m_stringArray
[n
];
204 int wxComboBox::FindString(const wxString
& s
, bool WXUNUSED(bCase
)) const
206 // FIXME: back to base class for not supported value of bCase
208 int *pos_list
= NULL
;
210 wxXmString
text( s
);
211 bool found
= (XmComboBoxGetMatchPos((Widget
) m_mainWidget
,
212 text(), &pos_list
, &count
) != 0);
214 if (found
&& count
> 0)
216 int pos
= pos_list
[0] - 1;
224 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
225 XmComboBoxSelectionCallbackStruct
* cbs
)
227 wxComboBox
*item
= (wxComboBox
*) clientData
;
231 case XmCR_SINGLE_SELECT
:
232 case XmCR_BROWSE_SELECT
:
234 wxCommandEvent
event (wxEVT_COMBOBOX
,
236 event
.SetInt(cbs
->index
- 1);
237 event
.SetString( item
->GetString ( event
.GetInt() ) );
238 if ( item
->HasClientObjectData() )
239 event
.SetClientObject( item
->GetClientObject(cbs
->index
- 1) );
240 else if ( item
->HasClientUntypedData() )
241 event
.SetClientData( item
->GetClientData(cbs
->index
- 1) );
242 event
.SetExtraLong(true);
243 event
.SetEventObject(item
);
244 item
->ProcessCommand (event
);
247 case XmCR_VALUE_CHANGED
:
249 wxCommandEvent
event (wxEVT_TEXT
, item
->GetId());
251 event
.SetString( item
->GetValue() );
252 event
.SetExtraLong(true);
253 event
.SetEventObject(item
);
254 item
->ProcessCommand (event
);
262 void wxComboBox::ChangeFont(bool keepOriginalSize
)
264 // Don't use the base class wxChoice's ChangeFont
265 wxWindow::ChangeFont(keepOriginalSize
);
268 void wxComboBox::ChangeBackgroundColour()
270 wxWindow::ChangeBackgroundColour();
273 void wxComboBox::ChangeForegroundColour()
275 wxWindow::ChangeForegroundColour();
278 wxSize
wxComboBox::DoGetBestSize() const
280 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
281 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
283 wxSize items
= GetItemsSize();
284 // FIXME arbitrary constants
285 return wxSize( ( items
.x
? items
.x
+ 50 : 120 ),
289 return wxWindow::DoGetBestSize();
292 WXWidget
wxComboBox::GetTextWidget() const
294 return (WXWidget
)XmComboBoxGetEditWidget((Widget
) m_mainWidget
);
297 #endif // XmVersion < 2000
299 #endif // wxUSE_COMBOBOX