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 #include "xmcombo/xmcombo.h"
23 void wxComboBoxCallback (Widget w
, XtPointer clientData
,
24 XmComboBoxSelectionCallbackStruct
* cbs
);
26 #if !USE_SHARED_LIBRARY
27 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
30 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
31 const wxString
& value
,
34 int n
, const wxString choices
[],
36 const wxValidator
& validator
,
40 SetValidator(validator
);
42 m_windowStyle
= style
;
43 // m_backgroundColour = parent->GetBackgroundColour();
44 m_backgroundColour
= * wxWHITE
;
45 m_foregroundColour
= parent
->GetForegroundColour();
47 if (parent
) parent
->AddChild(this);
50 m_windowId
= (int)NewControlId();
54 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
56 Widget buttonWidget
= XtVaCreateManagedWidget((char*) (const char*) name
,
57 xmComboBoxWidgetClass
, parentWidget
,
61 XmNeditable
, ((style
& wxCB_READONLY
) != wxCB_READONLY
),
62 XmNsorted
, ((style
& wxCB_SORT
) == wxCB_SORT
),
63 XmNstaticList
, ((style
& wxCB_SIMPLE
) == wxCB_SIMPLE
),
66 XtAddCallback (buttonWidget
, XmNselectionCallback
, (XtCallbackProc
) wxComboBoxCallback
,
68 XtAddCallback (buttonWidget
, XmNvalueChangedCallback
, (XtCallbackProc
) wxComboBoxCallback
,
72 for (i
= 0; i
< n
; i
++)
74 XmString str
= XmStringCreateLtoR((char*) (const char*) choices
[i
], XmSTRING_DEFAULT_CHARSET
);
75 XmComboBoxAddItem(buttonWidget
, str
, 0);
77 m_stringList
.Add(choices
[i
]);
81 m_mainWidget
= (Widget
) buttonWidget
;
83 XtManageChild (buttonWidget
);
87 m_windowFont
= parent
->GetFont();
90 SetCanAddEventHandler(TRUE
);
91 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
93 ChangeBackgroundColour();
98 wxComboBox::~wxComboBox()
100 DetachWidget((Widget
) m_mainWidget
); // Removes event handlers
101 XtDestroyWidget((Widget
) m_mainWidget
);
102 m_mainWidget
= (WXWidget
) 0;
105 void wxComboBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
107 // Necessary so it doesn't call wxChoice::SetSize
108 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
111 wxString
wxComboBox::GetValue() const
113 char *s
= XmComboBoxGetString ((Widget
) m_mainWidget
);
121 return wxEmptyString
;
124 void wxComboBox::SetValue(const wxString
& value
)
128 XmComboBoxSetString ((Widget
) m_mainWidget
, (char*) (const char*) value
);
129 m_inSetValue
= FALSE
;
132 void wxComboBox::Append(const wxString
& item
)
134 XmString str
= XmStringCreateLtoR((char*) (const char*) item
, XmSTRING_DEFAULT_CHARSET
);
135 XmComboBoxAddItem((Widget
) m_mainWidget
, str
, 0);
136 m_stringList
.Add(item
);
141 void wxComboBox::Delete(int n
)
143 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
-1);
144 wxNode
*node
= m_stringList
.Nth(n
);
147 delete[] (char *)node
->Data();
153 void wxComboBox::Clear()
155 XmComboBoxDeleteAllItems((Widget
) m_mainWidget
);
156 m_stringList
.Clear();
159 void wxComboBox::SetSelection (int n
)
161 XmComboBoxSelectPos((Widget
) m_mainWidget
, n
+1, False
);
164 int wxComboBox::GetSelection (void) const
166 int sel
= XmComboBoxGetSelectedPos((Widget
) m_mainWidget
);
173 wxString
wxComboBox::GetString(int n
) const
175 wxNode
*node
= m_stringList
.Nth (n
);
177 return wxString((char *) node
->Data ());
179 return wxEmptyString
;
182 wxString
wxComboBox::GetStringSelection() const
184 int sel
= GetSelection();
186 return wxEmptyString
;
188 return GetString(sel
);
191 bool wxComboBox::SetStringSelection(const wxString
& sel
)
193 int n
= FindString(sel
);
203 int wxComboBox::FindString(const wxString
& s
) const
205 int *pos_list
= NULL
;
207 XmString text
= XmStringCreateSimple ((char*) (const char*) s
);
208 bool found
= (XmComboBoxGetMatchPos((Widget
) m_mainWidget
,
209 text
, &pos_list
, &count
) != 0);
213 if (found
&& count
> 0)
215 int pos
= pos_list
[0] - 1;
223 // Clipboard operations
224 void wxComboBox::Copy()
226 XmComboBoxCopy((Widget
) m_mainWidget
, CurrentTime
);
229 void wxComboBox::Cut()
231 XmComboBoxCut((Widget
) m_mainWidget
, CurrentTime
);
234 void wxComboBox::Paste()
236 XmComboBoxPaste((Widget
) m_mainWidget
);
239 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
244 void wxComboBox::SetInsertionPoint(long pos
)
246 XmComboBoxSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
249 void wxComboBox::SetInsertionPointEnd()
251 XmTextPosition pos
= XmComboBoxGetLastPosition ((Widget
) m_mainWidget
);
252 XmComboBoxSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) (pos
+ 1));
255 long wxComboBox::GetInsertionPoint() const
257 return (long) XmComboBoxGetInsertionPosition ((Widget
) m_mainWidget
);
260 long wxComboBox::GetLastPosition() const
262 return (long) XmComboBoxGetLastPosition ((Widget
) m_mainWidget
);
265 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
267 XmComboBoxReplace ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
268 (char*) (const char*) value
);
271 void wxComboBox::Remove(long from
, long to
)
273 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
275 XmComboBoxRemove ((Widget
) m_mainWidget
);
278 void wxComboBox::SetSelection(long from
, long to
)
280 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
284 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
285 XmComboBoxSelectionCallbackStruct
* cbs
)
287 wxComboBox
*item
= (wxComboBox
*) clientData
;
291 case XmCR_SINGLE_SELECT
:
292 case XmCR_BROWSE_SELECT
:
294 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
, item
->GetId());
295 event
.m_commandInt
= cbs
->index
- 1;
296 // event.m_commandString = item->GetString (event.m_commandInt);
297 event
.m_extraLong
= TRUE
;
298 event
.SetEventObject(item
);
299 item
->ProcessCommand (event
);
302 case XmCR_VALUE_CHANGED
:
304 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
305 event
.m_commandInt
= -1;
306 // event.m_commandString = item->GetValue();
307 event
.m_extraLong
= TRUE
;
308 event
.SetEventObject(item
);
309 item
->ProcessCommand (event
);
317 void wxComboBox::ChangeFont(bool keepOriginalSize
)
319 // Don't use the base class wxChoice's ChangeFont
320 wxWindow::ChangeFont(keepOriginalSize
);
323 void wxComboBox::ChangeBackgroundColour()
325 wxWindow::ChangeBackgroundColour();
328 void wxComboBox::ChangeForegroundColour()
330 wxWindow::ChangeBackgroundColour();