More Motif changes (colour/font stuff)
[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 m_backgroundColour = parent->GetBackgroundColour();
44 m_foregroundColour = parent->GetForegroundColour();
45
46 if (parent) parent->AddChild(this);
47
48 if ( id == -1 )
49 m_windowId = (int)NewControlId();
50 else
51 m_windowId = id;
52
53 Widget parentWidget = (Widget) parent->GetClientWidget();
54
55 Widget buttonWidget = XtVaCreateManagedWidget((char*) (const char*) name,
56 xmComboBoxWidgetClass, parentWidget,
57 XmNmarginHeight, 0,
58 XmNmarginWidth, 0,
59 XmNshowLabel, False,
60 XmNeditable, ((style & wxCB_READONLY) != wxCB_READONLY),
61 XmNsorted, ((style & wxCB_SORT) == wxCB_SORT),
62 XmNstaticList, ((style & wxCB_SIMPLE) == wxCB_SIMPLE),
63 NULL);
64
65 XtAddCallback (buttonWidget, XmNselectionCallback, (XtCallbackProc) wxComboBoxCallback,
66 (XtPointer) this);
67 XtAddCallback (buttonWidget, XmNvalueChangedCallback, (XtCallbackProc) wxComboBoxCallback,
68 (XtPointer) this);
69
70 int i;
71 for (i = 0; i < n; i++)
72 {
73 XmString str = XmStringCreateLtoR((char*) (const char*) choices[i], XmSTRING_DEFAULT_CHARSET);
74 XmComboBoxAddItem(buttonWidget, str, 0);
75 XmStringFree(str);
76 m_stringList.Add(choices[i]);
77 }
78 m_noStrings = n;
79
80 m_mainWidget = (Widget) buttonWidget;
81
82 XtManageChild (buttonWidget);
83
84 SetValue(value);
85
86 SetCanAddEventHandler(TRUE);
87 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
88
89 SetFont(* parent->GetFont());
90 ChangeBackgroundColour();
91
92 return TRUE;
93 }
94
95 wxString wxComboBox::GetValue() const
96 {
97 char *s = XmComboBoxGetString ((Widget) m_mainWidget);
98 if (s)
99 {
100 wxString str(s);
101 XtFree (s);
102 return str;
103 }
104 else
105 return wxEmptyString;
106 }
107
108 void wxComboBox::SetValue(const wxString& value)
109 {
110 m_inSetValue = TRUE;
111 if (!value.IsNull())
112 XmComboBoxSetString ((Widget) m_mainWidget, (char*) (const char*) value);
113 m_inSetValue = FALSE;
114 }
115
116 // Clipboard operations
117 void wxComboBox::Copy()
118 {
119 XmComboBoxCopy((Widget) m_mainWidget, CurrentTime);
120 }
121
122 void wxComboBox::Cut()
123 {
124 XmComboBoxCut((Widget) m_mainWidget, CurrentTime);
125 }
126
127 void wxComboBox::Paste()
128 {
129 XmComboBoxPaste((Widget) m_mainWidget);
130 }
131
132 void wxComboBox::SetEditable(bool editable)
133 {
134 // TODO
135 }
136
137 void wxComboBox::SetInsertionPoint(long pos)
138 {
139 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
140 }
141
142 void wxComboBox::SetInsertionPointEnd()
143 {
144 XmTextPosition pos = XmComboBoxGetLastPosition ((Widget) m_mainWidget);
145 XmComboBoxSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) (pos + 1));
146 }
147
148 long wxComboBox::GetInsertionPoint() const
149 {
150 return (long) XmComboBoxGetInsertionPosition ((Widget) m_mainWidget);
151 }
152
153 long wxComboBox::GetLastPosition() const
154 {
155 return (long) XmComboBoxGetLastPosition ((Widget) m_mainWidget);
156 }
157
158 void wxComboBox::Replace(long from, long to, const wxString& value)
159 {
160 XmComboBoxReplace ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
161 (char*) (const char*) value);
162 }
163
164 void wxComboBox::Remove(long from, long to)
165 {
166 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
167 (Time) 0);
168 XmComboBoxRemove ((Widget) m_mainWidget);
169 }
170
171 void wxComboBox::SetSelection(long from, long to)
172 {
173 XmComboBoxSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
174 (Time) 0);
175 }
176
177 void wxComboBoxCallback (Widget w, XtPointer clientData,
178 XmComboBoxSelectionCallbackStruct * cbs)
179 {
180 wxComboBox *item = (wxComboBox *) clientData;
181
182 switch (cbs->reason)
183 {
184 case XmCR_SINGLE_SELECT:
185 case XmCR_BROWSE_SELECT:
186 {
187 wxCommandEvent event (wxEVT_COMMAND_COMBOBOX_SELECTED, item->GetId());
188 event.m_commandInt = cbs->index - 1;
189 // event.m_commandString = item->GetString (event.m_commandInt);
190 event.m_extraLong = TRUE;
191 event.SetEventObject(item);
192 item->ProcessCommand (event);
193 break;
194 }
195 case XmCR_VALUE_CHANGED:
196 {
197 wxCommandEvent event (wxEVT_COMMAND_TEXT_UPDATED, item->GetId());
198 event.m_commandInt = -1;
199 // event.m_commandString = item->GetValue();
200 event.m_extraLong = TRUE;
201 event.SetEventObject(item);
202 item->ProcessCommand (event);
203 break;
204 }
205 default:
206 break;
207 }
208 }
209
210 void wxComboBox::ChangeFont()
211 {
212 // Don't use the base class wxChoice's ChangeFont
213 wxWindow::ChangeFont();
214 }
215
216 void wxComboBox::ChangeBackgroundColour()
217 {
218 wxWindow::ChangeBackgroundColour();
219 }
220
221 void wxComboBox::ChangeForegroundColour()
222 {
223 wxWindow::ChangeBackgroundColour();
224 }
225
226 #endif
227