More Motif additions: mdi and sashtest samples now just about work!
[wxWidgets.git] / src / motif / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: combobox.cpp
3 // Purpose: wxComboBox class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "combobox.h"
14 #endif
15
16 #include "wx/combobox.h"
17
18 #if wxUSE_COMBOBOX
19
20 #include <Xm/Xm.h>
21 #include "xmcombo/xmcombo.h"
22
23 void wxComboBoxCallback (Widget w, XtPointer clientData,
24 XmComboBoxSelectionCallbackStruct * cbs);
25
26 #if !USE_SHARED_LIBRARY
27 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
28 #endif
29
30 bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
31 const wxString& value,
32 const wxPoint& pos,
33 const wxSize& size,
34 int n, const wxString choices[],
35 long style,
36 const wxValidator& validator,
37 const wxString& name)
38 {
39 SetName(name);
40 SetValidator(validator);
41 m_noStrings = n;
42 m_windowStyle = style;
43
44 if (parent) parent->AddChild(this);
45
46 if ( id == -1 )
47 m_windowId = (int)NewControlId();
48 else
49 m_windowId = id;
50
51 Widget parentWidget = (Widget) parent->GetClientWidget();
52
53 Widget buttonWidget = XtVaCreateManagedWidget((char*) (const char*) name,
54 xmComboBoxWidgetClass, parentWidget,
55 XmNmarginHeight, 0,
56 XmNmarginWidth, 0,
57 XmNshowLabel, False,
58 XmNeditable, ((style & wxCB_READONLY) != wxCB_READONLY),
59 XmNsorted, ((style & wxCB_SORT) == wxCB_SORT),
60 XmNstaticList, ((style & wxCB_SIMPLE) == wxCB_SIMPLE),
61 NULL);
62
63 XtAddCallback (buttonWidget, XmNselectionCallback, (XtCallbackProc) wxComboBoxCallback,
64 (XtPointer) this);
65 XtAddCallback (buttonWidget, XmNvalueChangedCallback, (XtCallbackProc) wxComboBoxCallback,
66 (XtPointer) this);
67
68 int i;
69 for (i = 0; i < n; i++)
70 {
71 XmString str = XmStringCreateLtoR((char*) (const char*) choices[i], XmSTRING_DEFAULT_CHARSET);
72 XmComboBoxAddItem(buttonWidget, str, 0);
73 XmStringFree(str);
74 m_stringList.Add(choices[i]);
75 }
76 m_noStrings = n;
77
78 m_mainWidget = (Widget) buttonWidget;
79
80 XtManageChild (buttonWidget);
81
82 SetValue(value);
83
84 SetCanAddEventHandler(TRUE);
85 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
86
87 SetFont(* parent->GetFont());
88 ChangeColour(m_mainWidget);
89
90 return TRUE;
91 }
92
93 wxString wxComboBox::GetValue() const
94 {
95 char *s = XmComboBoxGetString ((Widget) m_mainWidget);
96 if (s)
97 {
98 wxString str(s);
99 XtFree (s);
100 return str;
101 }
102 else
103 return wxEmptyString;
104 }
105
106 void wxComboBox::SetValue(const wxString& value)
107 {
108 m_inSetValue = TRUE;
109 if (!value.IsNull())
110 XmComboBoxSetString ((Widget) m_mainWidget, (char*) (const char*) value);
111 m_inSetValue = FALSE;
112 }
113
114 // Clipboard operations
115 void wxComboBox::Copy()
116 {
117 XmComboBoxCopy((Widget) m_mainWidget, CurrentTime);
118 }
119
120 void wxComboBox::Cut()
121 {
122 XmComboBoxCut((Widget) m_mainWidget, CurrentTime);
123 }
124
125 void wxComboBox::Paste()
126 {
127 XmComboBoxPaste((Widget) m_mainWidget);
128 }
129
130 void wxComboBox::SetEditable(bool editable)
131 {
132 // TODO
133 }
134
135 void wxComboBox::SetInsertionPoint(long pos)
136 {
137 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
138 }
139
140 void wxComboBox::SetInsertionPointEnd()
141 {
142 XmTextPosition pos = XmComboBoxGetLastPosition ((Widget) m_mainWidget);
143 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) (pos + 1));
144 }
145
146 long wxComboBox::GetInsertionPoint() const
147 {
148 return (long) XmComboBoxGetInsertionPosition ((Widget) m_mainWidget);
149 }
150
151 long wxComboBox::GetLastPosition() const
152 {
153 return (long) XmComboBoxGetLastPosition ((Widget) m_mainWidget);
154 }
155
156 void wxComboBox::Replace(long from, long to, const wxString& value)
157 {
158 XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
159 (char*) (const char*) value);
160 }
161
162 void wxComboBox::Remove(long from, long to)
163 {
164 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
165 (Time) 0);
166 XmComboBoxRemove ((Widget) m_mainWidget);
167 }
168
169 void wxComboBox::SetSelection(long from, long to)
170 {
171 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
172 (Time) 0);
173 }
174
175 void wxComboBoxCallback (Widget w, XtPointer clientData,
176 XmComboBoxSelectionCallbackStruct * cbs)
177 {
178 wxComboBox *item = (wxComboBox *) clientData;
179
180 switch (cbs->reason)
181 {
182 case XmCR_SINGLE_SELECT:
183 case XmCR_BROWSE_SELECT:
184 {
185 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED, item->GetId());
186 event.m_commandInt = cbs->index - 1;
187 // event.m_commandString = item->GetString (event.m_commandInt);
188 event.m_extraLong = TRUE;
189 event.SetEventObject(item);
190 item->ProcessCommand (event);
191 break;
192 }
193 case XmCR_VALUE_CHANGED:
194 {
195 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
196 event.m_commandInt = -1;
197 // event.m_commandString = item->GetValue();
198 event.m_extraLong = TRUE;
199 event.SetEventObject(item);
200 item->ProcessCommand (event);
201 break;
202 }
203 default:
204 break;
205 }
206 }
207
208 #endif
209