]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/combobox.cpp
Removed usage of GdkImlib
[wxWidgets.git] / src / gtk / combobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: combobox.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "combobox.h"
12 #endif
13
14 #include "wx/combobox.h"
15 #include <wx/intl.h>
16
17 //-----------------------------------------------------------------------------
18 // data
19 //-----------------------------------------------------------------------------
20
21 extern bool g_blockEventsOnDrag;
22
23 //-----------------------------------------------------------------------------
24 // "select"
25 //-----------------------------------------------------------------------------
26
27 static void gtk_combo_clicked_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
28 {
29 if (!combo->HasVMT()) return;
30 if (g_blockEventsOnDrag) return;
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 // wxComboBox
50 //-----------------------------------------------------------------------------
51
52 IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
53
54 BEGIN_EVENT_TABLE(wxComboBox, wxControl)
55 EVT_SIZE(wxComboBox::OnSize)
56 END_EVENT_TABLE()
57
58 bool wxComboBox::Create(wxWindow *parent, wxWindowID id, const wxString& value,
59 const wxPoint& pos, const wxSize& size,
60 int n, const wxString choices[],
61 long style, const wxValidator& validator, const wxString& name )
62 {
63 m_alreadySent = FALSE;
64 m_needParent = TRUE;
65
66 PreCreation( parent, id, pos, size, style, name );
67
68 SetValidator( validator );
69
70 m_widget = gtk_combo_new();
71
72 wxSize newSize = size;
73 if (newSize.x == -1) newSize.x = 100;
74 if (newSize.y == -1) newSize.y = 26;
75 SetSize( newSize.x, newSize.y );
76
77 GtkWidget *list = GTK_COMBO(m_widget)->list;
78
79 for (int i = 0; i < n; i++)
80 {
81 GtkWidget *list_item;
82 list_item = gtk_list_item_new_with_label( choices[i] );
83
84 gtk_container_add( GTK_CONTAINER(list), list_item );
85
86 m_clientData.Append( (wxObject*)NULL );
87
88 gtk_widget_show( list_item );
89
90 gtk_signal_connect( GTK_OBJECT(list_item), "select",
91 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
92 }
93
94 PostCreation();
95
96 ConnectWidget( GTK_COMBO(m_widget)->button );
97
98 if (!value.IsNull()) SetValue( value );
99
100 Show( TRUE );
101
102 return TRUE;
103 }
104
105 void wxComboBox::Clear(void)
106 {
107 GtkWidget *list = GTK_COMBO(m_widget)->list;
108 gtk_list_clear_items( GTK_LIST(list), 0, Number() );
109
110 m_clientData.Clear();
111 }
112
113 void wxComboBox::Append( const wxString &item )
114 {
115 Append( item, (char*)NULL );
116 }
117
118 void wxComboBox::Append( const wxString &item, char *clientData )
119 {
120 GtkWidget *list = GTK_COMBO(m_widget)->list;
121
122 GtkWidget *list_item = gtk_list_item_new_with_label( item );
123
124 if (m_hasOwnStyle)
125 {
126 GtkBin *bin = GTK_BIN( list_item );
127 gtk_widget_set_style( bin->child,
128 gtk_style_ref(
129 gtk_widget_get_style( m_widget ) ) );
130 }
131
132 if (m_backgroundColour != wxNullColour)
133 {
134 GtkBin *bin = GTK_BIN( list_item );
135 SetBackgroundColourHelper( bin->child->window );
136 }
137
138 gtk_signal_connect( GTK_OBJECT(list_item), "select",
139 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
140
141 m_clientData.Append( (wxObject*)clientData );
142
143 gtk_container_add( GTK_CONTAINER(list), list_item );
144
145 gtk_widget_show( list_item );
146 }
147
148 void wxComboBox::Delete( int n )
149 {
150 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
151
152 GList *child = g_list_nth( listbox->children, n );
153
154 if (!child)
155 {
156 wxFAIL_MSG("wrong index");
157 return;
158 }
159
160 GList *list = g_list_append( NULL, child->data );
161 gtk_list_remove_items( listbox, list );
162 g_list_free( list );
163
164 wxNode *node = m_clientData.Nth( n );
165 if (!node)
166 {
167 wxFAIL_MSG( "wrong index" );
168 }
169 else
170 m_clientData.DeleteNode( node );
171 }
172
173 int wxComboBox::FindString( const wxString &item )
174 {
175 GtkWidget *list = GTK_COMBO(m_widget)->list;
176
177 GList *child = GTK_LIST(list)->children;
178 int count = 0;
179 while (child)
180 {
181 GtkBin *bin = GTK_BIN( child->data );
182 GtkLabel *label = GTK_LABEL( bin->child );
183 if (item == label->label) return count;
184 count++;
185 child = child->next;
186 }
187
188 wxFAIL_MSG( "wxComboBox: string not found" );
189
190 return -1;
191 }
192
193 char* wxComboBox::GetClientData( int n )
194 {
195 wxNode *node = m_clientData.Nth( n );
196 if (node) return (char*)node->Data();
197
198 wxFAIL_MSG( "wxComboBox: wrong index" );
199
200 return (char *) NULL;
201 }
202
203 void wxComboBox::SetClientData( int n, char * clientData )
204 {
205 wxNode *node = m_clientData.Nth( n );
206 if (node) node->SetData( (wxObject*) clientData );
207
208 wxFAIL_MSG( "wxComboBox: wrong index" );
209 }
210
211 int wxComboBox::GetSelection(void) const
212 {
213 GtkWidget *list = GTK_COMBO(m_widget)->list;
214
215 GList *selection = GTK_LIST(list)->selection;
216 if (selection)
217 {
218 GList *child = GTK_LIST(list)->children;
219 int count = 0;
220 while (child)
221 {
222 if (child->data == selection->data) return count;
223 count++;
224 child = child->next;
225 }
226 }
227
228 wxFAIL_MSG( "wxComboBox: no selection" );
229
230 return -1;
231 }
232
233 wxString wxComboBox::GetString( int n ) const
234 {
235 GtkWidget *list = GTK_COMBO(m_widget)->list;
236
237 GList *child = g_list_nth( GTK_LIST(list)->children, n );
238 if (child)
239 {
240 GtkBin *bin = GTK_BIN( child->data );
241 GtkLabel *label = GTK_LABEL( bin->child );
242 return label->label;
243 }
244
245 wxFAIL_MSG( "wxComboBox: wrong index" );
246
247 return "";
248 }
249
250 wxString wxComboBox::GetStringSelection(void) const
251 {
252 GtkWidget *list = GTK_COMBO(m_widget)->list;
253
254 GList *selection = GTK_LIST(list)->selection;
255 if (selection)
256 {
257 GtkBin *bin = GTK_BIN( selection->data );
258 wxString tmp = GTK_LABEL( bin->child )->label;
259 return tmp;
260 }
261
262 wxFAIL_MSG( "wxComboBox: no selection" );
263
264 return "";
265 }
266
267 int wxComboBox::Number(void) const
268 {
269 GtkWidget *list = GTK_COMBO(m_widget)->list;
270
271 GList *child = GTK_LIST(list)->children;
272 int count = 0;
273 while (child) { count++; child = child->next; }
274 return count;
275 }
276
277 void wxComboBox::SetSelection( int n )
278 {
279 GtkWidget *list = GTK_COMBO(m_widget)->list;
280 gtk_list_select_item( GTK_LIST(list), n );
281 }
282
283 void wxComboBox::SetStringSelection( const wxString &string )
284 {
285 int res = FindString( string );
286 if (res == -1) return;
287 SetSelection( res );
288 }
289
290 wxString wxComboBox::GetValue(void) const
291 {
292 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
293 wxString tmp = gtk_entry_get_text( GTK_ENTRY(entry) );
294 return tmp;
295 }
296
297 void wxComboBox::SetValue( const wxString& value )
298 {
299 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
300 wxString tmp = "";
301 if (!value.IsNull()) tmp = value;
302 gtk_entry_set_text( GTK_ENTRY(entry), tmp );
303 }
304
305 void wxComboBox::Copy(void)
306 {
307 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
308 #if (GTK_MINOR_VERSION == 1)
309 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) );
310 #else
311 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
312 #endif
313 }
314
315 void wxComboBox::Cut(void)
316 {
317 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
318 #if (GTK_MINOR_VERSION == 1)
319 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) );
320 #else
321 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
322 #endif
323 }
324
325 void wxComboBox::Paste(void)
326 {
327 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
328 #if (GTK_MINOR_VERSION == 1)
329 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) );
330 #else
331 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
332 #endif
333 }
334
335 void wxComboBox::SetInsertionPoint( long pos )
336 {
337 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
338 int tmp = (int) pos;
339 gtk_entry_set_position( GTK_ENTRY(entry), tmp );
340 }
341
342 void wxComboBox::SetInsertionPointEnd(void)
343 {
344 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
345 int pos = GTK_ENTRY(entry)->text_length;
346 SetInsertionPoint( pos-1 );
347 }
348
349 long wxComboBox::GetInsertionPoint(void) const
350 {
351 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
352 return (long) GTK_EDITABLE(entry)->current_pos;
353 }
354
355 long wxComboBox::GetLastPosition(void) const
356 {
357 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
358 int pos = GTK_ENTRY(entry)->text_length;
359 return (long) pos-1;
360 }
361
362 void wxComboBox::Replace( long from, long to, const wxString& value )
363 {
364 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
365 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
366 if (value.IsNull()) return;
367 gint pos = (gint)to;
368 gtk_editable_insert_text( GTK_EDITABLE(entry), value, value.Length(), &pos );
369 }
370
371 void wxComboBox::Remove(long from, long to)
372 {
373 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
374 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
375 }
376
377 void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) )
378 {
379 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
380 }
381
382 void wxComboBox::SetEditable( bool WXUNUSED(editable) )
383 {
384 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
385 }
386
387 void wxComboBox::OnSize( wxSizeEvent &event )
388 {
389 wxControl::OnSize( event );
390
391 int w = 22;
392
393 gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height );
394
395 gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y );
396 gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height );
397 }
398
399 void wxComboBox::SetFont( const wxFont &font )
400 {
401 wxWindow::SetFont( font );
402
403 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
404
405 gtk_widget_set_style( entry,
406 gtk_style_ref(
407 gtk_widget_get_style( m_widget ) ) );
408
409 GtkWidget *list = GTK_COMBO(m_widget)->list;
410
411 GList *child = GTK_LIST(list)->children;
412 while (child)
413 {
414 GtkBin *bin = (GtkBin*) child->data;
415 gtk_widget_set_style( bin->child,
416 gtk_style_ref(
417 gtk_widget_get_style( m_widget ) ) );
418
419 child = child->next;
420 }
421 }
422
423 GtkWidget* wxComboBox::GetConnectWidget(void)
424 {
425 return GTK_COMBO(m_widget)->entry;
426 }
427
428 bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
429 {
430 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
431 (window == GTK_COMBO(m_widget)->button->window ) );
432 }
433
434 void wxComboBox::SetBackgroundColour( const wxColour &colour )
435 {
436 wxWindow::SetBackgroundColour( colour );
437
438 GtkWidget *list = GTK_COMBO(m_widget)->list;
439
440 GList *child = GTK_LIST(list)->children;
441 while (child)
442 {
443 GtkBin *bin = (GtkBin*) child->data;
444 SetBackgroundColourHelper( bin->child->window );
445 child = child->next;
446 }
447 }
448