]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/combobox.cpp
added slash to path
[wxWidgets.git] / src / gtk / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: combobox.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Created: 01/02/97
6 // Id:
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifdef __GNUG__
12 #pragma implementation "combobox.h"
13 #endif
14
15 #include "wx/combobox.h"
16
17 //-----------------------------------------------------------------------------
18 // data
19 //-----------------------------------------------------------------------------
20
21 extern bool g_blockEventsOnDrag;
22
23 //-----------------------------------------------------------------------------
24 // wxComboBox
25 //-----------------------------------------------------------------------------
26
27 //-----------------------------------------------------------------------------
28 // clicked
29
30 static void gtk_combo_clicked_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
31 {
32 if (combo->m_alreadySent)
33 {
34 combo->m_alreadySent = FALSE;
35 return;
36 }
37
38 combo->m_alreadySent = TRUE;
39
40 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, combo->GetId());
41 event.SetInt( combo->GetSelection() );
42 wxString tmp( combo->GetStringSelection() );
43 event.SetString( WXSTRINGCAST(tmp) );
44 event.SetEventObject(combo);
45 combo->GetEventHandler()->ProcessEvent(event);
46 };
47
48 //-----------------------------------------------------------------------------
49 // size
50
51 /*
52 static gint gtk_combo_size_callback( GtkCombo *widget, GtkAllocation* alloc, wxComboBox *win )
53 {
54 if (!win->HasVMT()) return FALSE;
55 if (g_blockEventsOnDrag) return FALSE;
56 if (!widget->button) return FALSE;
57
58 widget->button->allocation.x =
59 alloc->width - widget->button->allocation.width;
60
61 return FALSE;
62 };
63 */
64
65 //-----------------------------------------------------------------------------
66
67 IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
68
69 bool wxComboBox::Create(wxWindow *parent, wxWindowID id, const wxString& value,
70 const wxPoint& pos, const wxSize& size,
71 int n, const wxString choices[],
72 long style, const wxString& name )
73 {
74 m_alreadySent = FALSE;
75 m_needParent = TRUE;
76
77 PreCreation( parent, id, pos, size, style, name );
78
79 m_widget = gtk_combo_new();
80
81 wxSize newSize = size;
82 if (newSize.x == -1) newSize.x = 100;
83 if (newSize.y == -1) newSize.y = 26;
84 SetSize( newSize.x, newSize.y );
85
86 GtkWidget *list = GTK_COMBO(m_widget)->list;
87
88 for (int i = 0; i < n; i++)
89 {
90 GtkWidget *list_item;
91 list_item = gtk_list_item_new_with_label( choices[i] );
92
93 gtk_signal_connect( GTK_OBJECT(list_item), "select",
94 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
95
96 gtk_container_add( GTK_CONTAINER(list), list_item );
97
98 m_clientData.Append( (wxObject*)NULL );
99
100 gtk_widget_show( list_item );
101 };
102
103 PostCreation();
104
105 /*
106 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
107 GTK_SIGNAL_FUNC(gtk_combo_size_callback), (gpointer)this );
108 */
109
110 if (!value.IsNull()) SetValue( value );
111
112 Show( TRUE );
113
114 return TRUE;
115 };
116
117 void wxComboBox::Clear(void)
118 {
119 GtkWidget *list = GTK_COMBO(m_widget)->list;
120 gtk_list_clear_items( GTK_LIST(list), 0, Number() );
121
122 m_clientData.Clear();
123 };
124
125 void wxComboBox::Append( const wxString &item )
126 {
127 Append( item, (char*)NULL );
128 };
129
130 void wxComboBox::Append( const wxString &item, char *clientData )
131 {
132 GtkWidget *list = GTK_COMBO(m_widget)->list;
133
134 GtkWidget *list_item;
135 list_item = gtk_list_item_new_with_label( item );
136
137 gtk_signal_connect( GTK_OBJECT(list_item), "select",
138 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
139
140 gtk_container_add( GTK_CONTAINER(list), list_item );
141
142 gtk_widget_show( list_item );
143
144 m_clientData.Append( (wxObject*)clientData );
145 };
146
147 void wxComboBox::Delete( int n )
148 {
149 GtkWidget *list = GTK_COMBO(m_widget)->list;
150 gtk_list_clear_items( GTK_LIST(list), n, n );
151
152 wxNode *node = m_clientData.Nth( n );
153 if (!node)
154 {
155 wxFAIL_MSG("wxComboBox::Delete wrong index");
156 }
157 else
158 m_clientData.DeleteNode( node );
159 };
160
161 int wxComboBox::FindString( const wxString &item )
162 {
163 GtkWidget *list = GTK_COMBO(m_widget)->list;
164
165 GList *child = GTK_LIST(list)->children;
166 int count = 0;
167 while (child)
168 {
169 GtkBin *bin = GTK_BIN( child->data );
170 GtkLabel *label = GTK_LABEL( bin->child );
171 if (item == label->label) return count;
172 count++;
173 child = child->next;
174 };
175 return -1;
176 };
177
178 char* wxComboBox::GetClientData( int n )
179 {
180 wxNode *node = m_clientData.Nth( n );
181 if (node) return (char*)node->Data();
182 return NULL;
183 };
184
185 void wxComboBox::SetClientData( int n, char * clientData )
186 {
187 wxNode *node = m_clientData.Nth( n );
188 if (node) node->SetData( (wxObject*) clientData );
189 };
190
191 int wxComboBox::GetSelection(void) const
192 {
193 GtkWidget *list = GTK_COMBO(m_widget)->list;
194
195 GList *selection = GTK_LIST(list)->selection;
196 if (selection)
197 {
198 GList *child = GTK_LIST(list)->children;
199 int count = 0;
200 while (child)
201 {
202 if (child->data == selection->data) return count;
203 count++;
204 child = child->next;
205 };
206 };
207 return -1;
208 };
209
210 wxString wxComboBox::GetString( int n ) const
211 {
212 GtkWidget *list = GTK_COMBO(m_widget)->list;
213
214 GList *child = g_list_nth( GTK_LIST(list)->children, n );
215 if (child)
216 {
217 GtkBin *bin = GTK_BIN( child->data );
218 GtkLabel *label = GTK_LABEL( bin->child );
219 return label->label;
220 };
221 return "";
222 };
223
224 wxString wxComboBox::GetStringSelection(void) const
225 {
226 GtkWidget *list = GTK_COMBO(m_widget)->list;
227
228 GList *selection = GTK_LIST(list)->selection;
229 if (selection)
230 {
231 GtkBin *bin = GTK_BIN( selection->data );
232 wxString tmp = GTK_LABEL( bin->child )->label;
233 return tmp;
234 };
235 return "";
236 };
237
238 int wxComboBox::Number(void) const
239 {
240 GtkWidget *list = GTK_COMBO(m_widget)->list;
241
242 GList *child = GTK_LIST(list)->children;
243 int count = 0;
244 while (child) { count++; child = child->next; };
245 return count;
246 };
247
248 void wxComboBox::SetSelection( int n )
249 {
250 GtkWidget *list = GTK_COMBO(m_widget)->list;
251 gtk_list_select_item( GTK_LIST(list), n );
252 };
253
254 void wxComboBox::SetStringSelection( const wxString &string )
255 {
256 int res = FindString( string );
257 if (res == -1) return;
258 SetSelection( res );
259 };
260
261 wxString wxComboBox::GetValue(void) const
262 {
263 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
264 wxString tmp = gtk_entry_get_text( GTK_ENTRY(entry) );
265 return tmp;
266 };
267
268 void wxComboBox::SetValue( const wxString& value )
269 {
270 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
271 wxString tmp = "";
272 if (!value.IsNull()) tmp = value;
273 gtk_entry_set_text( GTK_ENTRY(entry), tmp );
274 };
275
276 void wxComboBox::Copy(void)
277 {
278 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
279 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
280 };
281
282 void wxComboBox::Cut(void)
283 {
284 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
285 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
286 };
287
288 void wxComboBox::Paste(void)
289 {
290 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
291 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
292 };
293
294 void wxComboBox::SetInsertionPoint( long pos )
295 {
296 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
297 int tmp = (int) pos;
298 gtk_entry_set_position( GTK_ENTRY(entry), tmp );
299 };
300
301 void wxComboBox::SetInsertionPointEnd(void)
302 {
303 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
304 int pos = GTK_ENTRY(entry)->text_length;
305 SetInsertionPoint( pos-1 );
306 };
307
308 long wxComboBox::GetInsertionPoint(void) const
309 {
310 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
311 return (long) GTK_EDITABLE(entry)->current_pos;
312 };
313
314 long wxComboBox::GetLastPosition(void) const
315 {
316 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
317 int pos = GTK_ENTRY(entry)->text_length;
318 return (long) pos-1;
319 };
320
321 void wxComboBox::Replace( long from, long to, const wxString& value )
322 {
323 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
324 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
325 if (value.IsNull()) return;
326 gint pos = (gint)to;
327 gtk_editable_insert_text( GTK_EDITABLE(entry), value, value.Length(), &pos );
328 };
329
330 void wxComboBox::Remove(long from, long to)
331 {
332 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
333 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
334 };
335
336 void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) )
337 {
338 };
339
340 void wxComboBox::SetEditable( bool WXUNUSED(editable) )
341 {
342 };
343
344