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_foregroundColour
= parent
->GetForegroundColour();
46 if (parent
) parent
->AddChild(this);
49 m_windowId
= (int)NewControlId();
53 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
55 Widget buttonWidget
= XtVaCreateManagedWidget((char*) (const char*) name
,
56 xmComboBoxWidgetClass
, parentWidget
,
60 XmNeditable
, ((style
& wxCB_READONLY
) != wxCB_READONLY
),
61 XmNsorted
, ((style
& wxCB_SORT
) == wxCB_SORT
),
62 XmNstaticList
, ((style
& wxCB_SIMPLE
) == wxCB_SIMPLE
),
65 XtAddCallback (buttonWidget
, XmNselectionCallback
, (XtCallbackProc
) wxComboBoxCallback
,
67 XtAddCallback (buttonWidget
, XmNvalueChangedCallback
, (XtCallbackProc
) wxComboBoxCallback
,
71 for (i
= 0; i
< n
; i
++)
73 XmString str
= XmStringCreateLtoR((char*) (const char*) choices
[i
], XmSTRING_DEFAULT_CHARSET
);
74 XmComboBoxAddItem(buttonWidget
, str
, 0);
76 m_stringList
.Add(choices
[i
]);
80 m_mainWidget
= (Widget
) buttonWidget
;
82 XtManageChild (buttonWidget
);
86 m_windowFont
= parent
->GetFont();
89 SetCanAddEventHandler(TRUE
);
90 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
92 ChangeBackgroundColour();
97 wxComboBox::~wxComboBox()
99 DetachWidget((Widget
) m_mainWidget
); // Removes event handlers
100 XtDestroyWidget((Widget
) m_mainWidget
);
101 m_mainWidget
= (WXWidget
) 0;
104 void wxComboBox::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
106 // Necessary so it doesn't call wxChoice::SetSize
107 wxWindow::SetSize(x
, y
, width
, height
, sizeFlags
);
110 wxString
wxComboBox::GetValue() const
112 char *s
= XmComboBoxGetString ((Widget
) m_mainWidget
);
120 return wxEmptyString
;
123 void wxComboBox::SetValue(const wxString
& value
)
127 XmComboBoxSetString ((Widget
) m_mainWidget
, (char*) (const char*) value
);
128 m_inSetValue
= FALSE
;
131 void wxComboBox::Append(const wxString
& item
)
133 XmString str
= XmStringCreateLtoR((char*) (const char*) item
, XmSTRING_DEFAULT_CHARSET
);
134 XmComboBoxAddItem((Widget
) m_mainWidget
, str
, 0);
135 m_stringList
.Add(item
);
140 void wxComboBox::Delete(int n
)
142 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
-1);
143 wxNode
*node
= m_stringList
.Nth(n
);
146 delete[] (char *)node
->Data();
152 void wxComboBox::Clear()
154 XmComboBoxDeleteAllItems((Widget
) m_mainWidget
);
155 m_stringList
.Clear();
158 void wxComboBox::SetSelection (int n
)
160 XmComboBoxSelectPos((Widget
) m_mainWidget
, n
+1, False
);
163 int wxComboBox::GetSelection (void) const
165 int sel
= XmComboBoxGetSelectedPos((Widget
) m_mainWidget
);
172 wxString
wxComboBox::GetString(int n
) const
174 wxNode
*node
= m_stringList
.Nth (n
);
176 return wxString((char *) node
->Data ());
178 return wxEmptyString
;
181 wxString
wxComboBox::GetStringSelection() const
183 int sel
= GetSelection();
185 return wxEmptyString
;
187 return GetString(sel
);
190 bool wxComboBox::SetStringSelection(const wxString
& sel
)
192 int n
= FindString(sel
);
202 int wxComboBox::FindString(const wxString
& s
) const
204 int *pos_list
= NULL
;
206 XmString text
= XmStringCreateSimple ((char*) (const char*) s
);
207 bool found
= (XmComboBoxGetMatchPos((Widget
) m_mainWidget
,
208 text
, &pos_list
, &count
) != 0);
212 if (found
&& count
> 0)
214 int pos
= pos_list
[0] - 1;
222 // Clipboard operations
223 void wxComboBox::Copy()
225 XmComboBoxCopy((Widget
) m_mainWidget
, CurrentTime
);
228 void wxComboBox::Cut()
230 XmComboBoxCut((Widget
) m_mainWidget
, CurrentTime
);
233 void wxComboBox::Paste()
235 XmComboBoxPaste((Widget
) m_mainWidget
);
238 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
243 void wxComboBox::SetInsertionPoint(long pos
)
245 XmComboBoxSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
248 void wxComboBox::SetInsertionPointEnd()
250 XmTextPosition pos
= XmComboBoxGetLastPosition ((Widget
) m_mainWidget
);
251 XmComboBoxSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) (pos
+ 1));
254 long wxComboBox::GetInsertionPoint() const
256 return (long) XmComboBoxGetInsertionPosition ((Widget
) m_mainWidget
);
259 long wxComboBox::GetLastPosition() const
261 return (long) XmComboBoxGetLastPosition ((Widget
) m_mainWidget
);
264 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
266 XmComboBoxReplace ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
267 (char*) (const char*) value
);
270 void wxComboBox::Remove(long from
, long to
)
272 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
274 XmComboBoxRemove ((Widget
) m_mainWidget
);
277 void wxComboBox::SetSelection(long from
, long to
)
279 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
283 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
284 XmComboBoxSelectionCallbackStruct
* cbs
)
286 wxComboBox
*item
= (wxComboBox
*) clientData
;
290 case XmCR_SINGLE_SELECT
:
291 case XmCR_BROWSE_SELECT
:
293 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
, item
->GetId());
294 event
.m_commandInt
= cbs
->index
- 1;
295 // event.m_commandString = item->GetString (event.m_commandInt);
296 event
.m_extraLong
= TRUE
;
297 event
.SetEventObject(item
);
298 item
->ProcessCommand (event
);
301 case XmCR_VALUE_CHANGED
:
303 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
304 event
.m_commandInt
= -1;
305 // event.m_commandString = item->GetValue();
306 event
.m_extraLong
= TRUE
;
307 event
.SetEventObject(item
);
308 item
->ProcessCommand (event
);
316 void wxComboBox::ChangeFont(bool keepOriginalSize
)
318 // Don't use the base class wxChoice's ChangeFont
319 wxWindow::ChangeFont(keepOriginalSize
);
322 void wxComboBox::ChangeBackgroundColour()
324 wxWindow::ChangeBackgroundColour();
327 void wxComboBox::ChangeForegroundColour()
329 wxWindow::ChangeBackgroundColour();