1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/combobox.cpp
3 // Purpose: wxComboBox class
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"
17 #include "wx/combobox.h"
20 #include "wx/arrstr.h"
24 #pragma message disable nosimpint
28 #pragma message enable nosimpint
31 // use the old, GPL'd combobox
32 #if (XmVersion < 2000)
34 #include "xmcombo/xmcombo.h"
36 #include "wx/motif/private.h"
38 void wxComboBoxCallback (Widget w
, XtPointer clientData
,
39 XmComboBoxSelectionCallbackStruct
* cbs
);
41 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
42 const wxString
& value
,
45 int n
, const wxString choices
[],
47 const wxValidator
& validator
,
50 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
54 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
56 Widget buttonWidget
= XtVaCreateManagedWidget(name
.c_str(),
57 xmComboBoxWidgetClass
, parentWidget
,
61 XmNeditable
, ((style
& wxCB_READONLY
) != wxCB_READONLY
),
62 XmNsorted
, ((style
& wxCB_SORT
) == wxCB_SORT
),
63 XmNstaticList
, ((style
& wxCB_SIMPLE
) == wxCB_SIMPLE
),
67 for (i
= 0; i
< n
; i
++)
69 wxXmString
str( choices
[i
] );
70 XmComboBoxAddItem(buttonWidget
, str(), 0);
71 m_stringArray
.Add(choices
[i
]);
74 m_mainWidget
= (Widget
) buttonWidget
;
76 XtManageChild (buttonWidget
);
80 XtAddCallback (buttonWidget
, XmNselectionCallback
, (XtCallbackProc
) wxComboBoxCallback
,
82 XtAddCallback (buttonWidget
, XmNvalueChangedCallback
, (XtCallbackProc
) wxComboBoxCallback
,
86 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
91 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
92 const wxString
& value
,
95 const wxArrayString
& choices
,
97 const wxValidator
& validator
,
100 wxCArrayString
chs(choices
);
101 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
102 chs
.GetStrings(), style
, validator
, name
);
105 wxComboBox::~wxComboBox()
107 DetachWidget((Widget
) m_mainWidget
); // Removes event handlers
108 XtDestroyWidget((Widget
) m_mainWidget
);
109 m_mainWidget
= (WXWidget
) 0;
112 void wxComboBox::DoSetSize(int x
, int y
,
113 int width
, int WXUNUSED(height
),
116 // Necessary so it doesn't call wxChoice::SetSize
117 wxWindow::DoSetSize(x
, y
, width
, DoGetBestSize().y
, sizeFlags
);
121 // Already defined in include/motif/combobox.h
122 wxString
wxComboBox::GetValue() const
124 char *s
= XmComboBoxGetString ((Widget
) m_mainWidget
);
132 return wxEmptyString
;
136 void wxComboBox::SetValue(const wxString
& value
)
141 XmComboBoxSetString((Widget
)m_mainWidget
, value
.char_str());
142 m_inSetValue
= false;
146 void wxComboBox::SetString(unsigned int WXUNUSED(n
), const wxString
& WXUNUSED(s
))
148 wxFAIL_MSG( wxT("wxComboBox::SetString only implemented for Motif 2.0") );
151 // TODO auto-sorting is not supported by the code
152 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
155 wxClientDataType type
)
157 const unsigned int numItems
= items
.GetCount();
159 AllocClientData(numItems
);
160 for( unsigned int i
= 0; i
< numItems
; ++i
, ++pos
)
162 wxXmString
str( items
[i
].c_str() );
163 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), GetMotifPosition(pos
));
164 m_stringArray
.Insert(items
[i
], pos
);
165 InsertNewItemClientData(pos
, clientData
, i
, type
);
171 void wxComboBox::DoDeleteOneItem(unsigned int n
)
173 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
174 m_stringArray
.RemoveAt(n
);
175 wxControlWithItems::DoDeleteOneItem(n
);
178 void wxComboBox::Clear()
180 XmComboBoxDeleteAllItems((Widget
) m_mainWidget
);
181 m_stringArray
.Clear();
183 wxControlWithItems::DoClear();
186 void wxComboBox::SetSelection (int n
)
188 XmComboBoxSelectPos((Widget
) m_mainWidget
, n
+1, False
);
191 int wxComboBox::GetSelection (void) const
193 int sel
= XmComboBoxGetSelectedPos((Widget
) m_mainWidget
);
200 wxString
wxComboBox::GetString(unsigned int n
) const
202 return m_stringArray
[n
];
205 int wxComboBox::FindString(const wxString
& s
, bool WXUNUSED(bCase
)) const
207 // FIXME: back to base class for not supported value of bCase
209 int *pos_list
= NULL
;
211 wxXmString
text( s
);
212 bool found
= (XmComboBoxGetMatchPos((Widget
) m_mainWidget
,
213 text(), &pos_list
, &count
) != 0);
215 if (found
&& count
> 0)
217 int pos
= pos_list
[0] - 1;
225 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
226 XmComboBoxSelectionCallbackStruct
* cbs
)
228 wxComboBox
*item
= (wxComboBox
*) clientData
;
232 case XmCR_SINGLE_SELECT
:
233 case XmCR_BROWSE_SELECT
:
235 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
,
237 event
.SetInt(cbs
->index
- 1);
238 event
.SetString( item
->GetString ( event
.GetInt() ) );
239 if ( item
->HasClientObjectData() )
240 event
.SetClientObject( item
->GetClientObject(cbs
->index
- 1) );
241 else if ( item
->HasClientUntypedData() )
242 event
.SetClientData( item
->GetClientData(cbs
->index
- 1) );
243 event
.SetExtraLong(true);
244 event
.SetEventObject(item
);
245 item
->ProcessCommand (event
);
248 case XmCR_VALUE_CHANGED
:
250 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
252 event
.SetString( item
->GetValue() );
253 event
.SetExtraLong(true);
254 event
.SetEventObject(item
);
255 item
->ProcessCommand (event
);
263 void wxComboBox::ChangeFont(bool keepOriginalSize
)
265 // Don't use the base class wxChoice's ChangeFont
266 wxWindow::ChangeFont(keepOriginalSize
);
269 void wxComboBox::ChangeBackgroundColour()
271 wxWindow::ChangeBackgroundColour();
274 void wxComboBox::ChangeForegroundColour()
276 wxWindow::ChangeForegroundColour();
279 wxSize
wxComboBox::DoGetBestSize() const
281 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
282 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
284 wxSize items
= GetItemsSize();
285 // FIXME arbitrary constants
286 return wxSize( ( items
.x
? items
.x
+ 50 : 120 ),
290 return wxWindow::DoGetBestSize();
293 WXWidget
wxComboBox::GetTextWidget() const
295 return (WXWidget
)XmComboBoxGetEditWidget((Widget
) m_mainWidget
);
298 #endif // XmVersion < 2000
300 #endif // wxUSE_COMBOBOX