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 void wxComboBoxCallback (Widget w
, XtPointer clientData
,
30 XmComboBoxSelectionCallbackStruct
* cbs
);
32 IMPLEMENT_DYNAMIC_CLASS(wxComboBox
, wxControl
)
34 bool wxComboBox::Create(wxWindow
*parent
, wxWindowID id
,
35 const wxString
& value
,
38 int n
, const wxString choices
[],
40 const wxValidator
& validator
,
44 SetValidator(validator
);
46 m_windowStyle
= style
;
47 // m_backgroundColour = parent->GetBackgroundColour();
48 m_backgroundColour
= * wxWHITE
;
49 m_foregroundColour
= parent
->GetForegroundColour();
51 if (parent
) parent
->AddChild(this);
54 m_windowId
= (int)NewControlId();
58 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
60 Widget buttonWidget
= XtVaCreateManagedWidget(name
.c_str(),
61 xmComboBoxWidgetClass
, parentWidget
,
65 XmNeditable
, ((style
& wxCB_READONLY
) != wxCB_READONLY
),
66 XmNsorted
, ((style
& wxCB_SORT
) == wxCB_SORT
),
67 XmNstaticList
, ((style
& wxCB_SIMPLE
) == wxCB_SIMPLE
),
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_font
= parent
->GetFont();
89 XtAddCallback (buttonWidget
, XmNselectionCallback
, (XtCallbackProc
) wxComboBoxCallback
,
91 XtAddCallback (buttonWidget
, XmNvalueChangedCallback
, (XtCallbackProc
) wxComboBoxCallback
,
94 SetCanAddEventHandler(TRUE
);
95 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
97 ChangeBackgroundColour();
102 wxComboBox::~wxComboBox()
104 DetachWidget((Widget
) m_mainWidget
); // Removes event handlers
105 XtDestroyWidget((Widget
) m_mainWidget
);
106 m_mainWidget
= (WXWidget
) 0;
109 void wxComboBox::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
111 // Necessary so it doesn't call wxChoice::SetSize
112 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
115 wxString
wxComboBox::GetValue() const
117 char *s
= XmComboBoxGetString ((Widget
) m_mainWidget
);
125 return wxEmptyString
;
128 void wxComboBox::SetValue(const wxString
& value
)
132 XmComboBoxSetString ((Widget
) m_mainWidget
, (char*) (const char*) value
);
133 m_inSetValue
= FALSE
;
136 void wxComboBox::Append(const wxString
& item
)
138 XmString str
= XmStringCreateLtoR((char*) (const char*) item
, XmSTRING_DEFAULT_CHARSET
);
139 XmComboBoxAddItem((Widget
) m_mainWidget
, str
, 0);
140 m_stringList
.Add(item
);
145 void wxComboBox::Delete(int n
)
147 XmComboBoxDeletePos((Widget
) m_mainWidget
, n
-1);
148 wxNode
*node
= m_stringList
.Nth(n
);
151 delete[] (char *)node
->Data();
154 node
= m_clientList
.Nth( n
);
157 if ( HasClientObjectData() )
159 delete (wxClientData
*)node
->Data();
167 void wxComboBox::Clear()
169 XmComboBoxDeleteAllItems((Widget
) m_mainWidget
);
170 m_stringList
.Clear();
172 if ( HasClientObjectData() )
174 // destroy the data (due to Robert's idea of using wxList<wxObject>
175 // and not wxList<wxClientData> we can't just say
176 // m_clientList.DeleteContents(TRUE) - this would crash!
177 wxNode
*node
= m_clientList
.First();
180 delete (wxClientData
*)node
->Data();
184 m_clientList
.Clear();
188 void wxComboBox::SetSelection (int n
)
190 XmComboBoxSelectPos((Widget
) m_mainWidget
, n
+1, False
);
193 int wxComboBox::GetSelection (void) const
195 int sel
= XmComboBoxGetSelectedPos((Widget
) m_mainWidget
);
202 wxString
wxComboBox::GetString(int n
) const
204 wxNode
*node
= m_stringList
.Nth (n
);
206 return wxString((char *) node
->Data ());
208 return wxEmptyString
;
211 wxString
wxComboBox::GetStringSelection() const
213 int sel
= GetSelection();
215 return wxEmptyString
;
217 return GetString(sel
);
220 bool wxComboBox::SetStringSelection(const wxString
& sel
)
222 int n
= FindString(sel
);
232 int wxComboBox::FindString(const wxString
& s
) const
234 int *pos_list
= NULL
;
236 XmString text
= XmStringCreateSimple ((char*) (const char*) s
);
237 bool found
= (XmComboBoxGetMatchPos((Widget
) m_mainWidget
,
238 text
, &pos_list
, &count
) != 0);
242 if (found
&& count
> 0)
244 int pos
= pos_list
[0] - 1;
252 // Clipboard operations
253 void wxComboBox::Copy()
255 XmComboBoxCopy((Widget
) m_mainWidget
, CurrentTime
);
258 void wxComboBox::Cut()
260 XmComboBoxCut((Widget
) m_mainWidget
, CurrentTime
);
263 void wxComboBox::Paste()
265 XmComboBoxPaste((Widget
) m_mainWidget
);
268 void wxComboBox::SetEditable(bool WXUNUSED(editable
))
273 void wxComboBox::SetInsertionPoint(long pos
)
275 XmComboBoxSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
278 void wxComboBox::SetInsertionPointEnd()
280 XmTextPosition pos
= XmComboBoxGetLastPosition ((Widget
) m_mainWidget
);
281 XmComboBoxSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) (pos
+ 1));
284 long wxComboBox::GetInsertionPoint() const
286 return (long) XmComboBoxGetInsertionPosition ((Widget
) m_mainWidget
);
289 long wxComboBox::GetLastPosition() const
291 return (long) XmComboBoxGetLastPosition ((Widget
) m_mainWidget
);
294 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
296 XmComboBoxReplace ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
297 (char*) (const char*) value
);
300 void wxComboBox::Remove(long from
, long to
)
302 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
304 XmComboBoxRemove ((Widget
) m_mainWidget
);
307 void wxComboBox::SetSelection(long from
, long to
)
309 XmComboBoxSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
313 void wxComboBoxCallback (Widget
WXUNUSED(w
), XtPointer clientData
,
314 XmComboBoxSelectionCallbackStruct
* cbs
)
316 wxComboBox
*item
= (wxComboBox
*) clientData
;
320 case XmCR_SINGLE_SELECT
:
321 case XmCR_BROWSE_SELECT
:
323 wxCommandEvent
event (wxEVT_COMMAND_COMBOBOX_SELECTED
, item
->GetId());
324 event
.m_commandInt
= cbs
->index
- 1;
325 // event.m_commandString = item->GetString (event.m_commandInt);
326 event
.m_extraLong
= TRUE
;
327 event
.SetEventObject(item
);
328 item
->ProcessCommand (event
);
331 case XmCR_VALUE_CHANGED
:
333 wxCommandEvent
event (wxEVT_COMMAND_TEXT_UPDATED
, item
->GetId());
334 event
.m_commandInt
= -1;
335 // event.m_commandString = item->GetValue();
336 event
.m_extraLong
= TRUE
;
337 event
.SetEventObject(item
);
338 item
->ProcessCommand (event
);
346 void wxComboBox::ChangeFont(bool keepOriginalSize
)
348 // Don't use the base class wxChoice's ChangeFont
349 wxWindow::ChangeFont(keepOriginalSize
);
352 void wxComboBox::ChangeBackgroundColour()
354 wxWindow::ChangeBackgroundColour();
357 void wxComboBox::ChangeForegroundColour()
359 wxWindow::ChangeBackgroundColour();