1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxComboBox class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "combobox.h"
16 #include "wx/combobox.h"
21 #pragma message disable nosimpint
25 #pragma message enable nosimpint
27 #include "xmcombo/xmcombo.h"
29 #include "wx/motif/private.h"
31 void wxComboBoxCallback (Widget w
, XtPointer clientData
,
32 XmComboBoxSelectionCallbackStruct
* cbs
);
34 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
36 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
37 const wxString
& value
,
40 int n
, const wxString choices
[],
42 const wxValidator
& validator
,
45 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
50 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
52 Widget buttonWidget
= XtVaCreateManagedWidget(name
.c_str(),
53 xmComboBoxWidgetClass
, parentWidget
,
57 XmNeditable
, ((style
& wxCB_READONLY
) != wxCB_READONLY
),
58 XmNsorted
, ((style
& wxCB_SORT
) == wxCB_SORT
),
59 XmNstaticList
, ((style
& wxCB_SIMPLE
) == wxCB_SIMPLE
),
63 for (i
= 0; i
< n
; i
++)
65 wxXmString
str( choices
[i
] );
66 XmComboBoxAddItem(buttonWidget
, str(), 0);
67 m_stringList
.Add(choices
[i
]);
70 m_mainWidget
= (Widget
) buttonWidget
;
72 XtManageChild (buttonWidget
);
78 XtAddCallback (buttonWidget
, XmNselectionCallback
, (XtCallbackProc
) wxComboBoxCallback
,
80 XtAddCallback (buttonWidget
, XmNvalueChangedCallback
, (XtCallbackProc
) wxComboBoxCallback
,
83 SetCanAddEventHandler(TRUE
);
84 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
86 ChangeBackgroundColour();
91 wxComboBox::~wxComboBox()
93 DetachWidget((Widget
) m_mainWidget
); // Removes event handlers
94 XtDestroyWidget((Widget
) m_mainWidget
);
95 m_mainWidget
= (WXWidget
) 0;
96 if ( HasClientObjectData() )
97 m_clientDataDict
.DestroyData();
100 void wxComboBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
102 // Necessary so it doesn't call wxChoice::SetSize
103 wxWindow::DoSetSize(x
, y
, width
, DoGetBestSize().y
, sizeFlags
);
106 wxString
wxComboBox::GetValue() const
108 char *s
= XmComboBoxGetString ((Widget
) m_mainWidget
);
116 return wxEmptyString
;
119 void wxComboBox::SetValue(const wxString
& value
)
123 XmComboBoxSetString( (Widget
)m_mainWidget
, (char*)value
.c_str() );
124 m_inSetValue
= FALSE
;
127 int wxComboBox::DoAppend(const wxString
& item
)
129 wxXmString
str( item
.c_str() );
130 XmComboBoxAddItem((Widget
) m_mainWidget
, str(), 0);
131 m_stringList
.Add(item
);
134 return GetCount() - 1;
137 void wxComboBox::Delete(int n
)
139 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
+1);
140 wxNode
*node
= m_stringList
.Nth(n
);
143 delete[] (char *)node
->Data();
146 m_clientDataDict
.Delete(n
, HasClientObjectData());
150 void wxComboBox::Clear()
152 XmComboBoxDeleteAllItems((Widget
) m_mainWidget
);
153 m_stringList
.Clear();
155 if ( HasClientObjectData() )
156 m_clientDataDict
.DestroyData();
160 void wxComboBox::SetSelection (int n
)
162 XmComboBoxSelectPos((Widget
) m_mainWidget
, n
+1, False
);
165 int wxComboBox::GetSelection (void) const
167 int sel
= XmComboBoxGetSelectedPos((Widget
) m_mainWidget
);
174 wxString
wxComboBox::GetString(int n
) const
176 wxNode
*node
= m_stringList
.Nth(n
);
178 return wxString((char *) node
->Data ());
180 return wxEmptyString
;
183 int wxComboBox::FindString(const wxString
& s
) const
185 int *pos_list
= NULL
;
187 wxXmString
text( s
);
188 bool found
= (XmComboBoxGetMatchPos((Widget
) m_mainWidget
,
189 text(), &pos_list
, &count
) != 0);
191 if (found
&& count
> 0)
193 int pos
= pos_list
[0] - 1;
201 // Clipboard operations
202 void wxComboBox::Copy()
204 XmComboBoxCopy((Widget
) m_mainWidget
, CurrentTime
);
207 void wxComboBox::Cut()
209 XmComboBoxCut((Widget
) m_mainWidget
, CurrentTime
);
212 void wxComboBox::Paste()
214 XmComboBoxPaste((Widget
) m_mainWidget
);
217 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
222 void wxComboBox::SetInsertionPoint(long pos
)
224 XmComboBoxSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
227 void wxComboBox::SetInsertionPointEnd()
229 XmTextPosition pos
= XmComboBoxGetLastPosition ((Widget
) m_mainWidget
);
230 XmComboBoxSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) (pos
+ 1));
233 long wxComboBox::GetInsertionPoint() const
235 return (long) XmComboBoxGetInsertionPosition ((Widget
) m_mainWidget
);
238 long wxComboBox::GetLastPosition() const
240 return (long) XmComboBoxGetLastPosition ((Widget
) m_mainWidget
);
243 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
245 XmComboBoxReplace ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
246 (char*) (const char*) value
);
249 void wxComboBox::Remove(long from
, long to
)
251 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
253 XmComboBoxRemove ((Widget
) m_mainWidget
);
256 void wxComboBox::SetSelection(long from
, long to
)
258 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
262 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
263 XmComboBoxSelectionCallbackStruct
* cbs
)
265 wxComboBox
*item
= (wxComboBox
*) clientData
;
269 case XmCR_SINGLE_SELECT
:
270 case XmCR_BROWSE_SELECT
:
272 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
, item
->GetId());
273 event
.m_commandInt
= cbs
->index
- 1;
274 event
.m_commandString
= item
->GetString (event
.m_commandInt
);
275 if ( item
->HasClientObjectData() )
276 event
.SetClientObject( item
->GetClientObject(cbs
->index
- 1) );
277 else if ( item
->HasClientUntypedData() )
278 event
.SetClientData( item
->GetClientData(cbs
->index
- 1) );
279 event
.m_extraLong
= TRUE
;
280 event
.SetEventObject(item
);
281 item
->ProcessCommand (event
);
284 case XmCR_VALUE_CHANGED
:
286 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
287 event
.m_commandInt
= -1;
288 event
.m_commandString
= item
->GetValue();
289 event
.m_extraLong
= TRUE
;
290 event
.SetEventObject(item
);
291 item
->ProcessCommand (event
);
299 void wxComboBox::ChangeFont(bool keepOriginalSize
)
301 // Don't use the base class wxChoice's ChangeFont
302 wxWindow::ChangeFont(keepOriginalSize
);
305 void wxComboBox::ChangeBackgroundColour()
307 wxWindow::ChangeBackgroundColour();
310 void wxComboBox::ChangeForegroundColour()
312 wxWindow::ChangeForegroundColour();
315 wxSize
wxComboBox::DoGetBestSize() const
317 if( (GetWindowStyle() & wxCB_DROPDOWN
) == wxCB_DROPDOWN
||
318 (GetWindowStyle() & wxCB_READONLY
) == wxCB_READONLY
)
320 wxSize items
= GetItemsSize();
321 // FIXME arbitrary constants
322 return wxSize( ( items
.x
? items
.x
+ 50 : 120 ),
326 return wxWindow::DoGetBestSize();