]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/combobox.cpp
ad273a35ea41074b4ade1b3e7cb614ea5abaec8c
[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
15 #include "wx/combobox.h"
16
17 #include <wx/intl.h>
18
19 #include "gdk/gdk.h"
20 #include "gtk/gtk.h"
21
22 //-----------------------------------------------------------------------------
23 // data
24 //-----------------------------------------------------------------------------
25
26 extern bool g_blockEventsOnDrag;
27
28 //-----------------------------------------------------------------------------
29 // "select"
30 //-----------------------------------------------------------------------------
31
32 static void gtk_combo_clicked_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
33 {
34 if (!combo->HasVMT()) return;
35 if (g_blockEventsOnDrag) return;
36
37 if (combo->m_alreadySent)
38 {
39 combo->m_alreadySent = FALSE;
40 return;
41 }
42
43 combo->m_alreadySent = TRUE;
44
45 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId());
46 event.SetInt( combo->GetSelection() );
47 wxString tmp( combo->GetStringSelection() );
48 event.SetString( WXSTRINGCAST(tmp) );
49 event.SetEventObject(combo);
50 combo->GetEventHandler()->ProcessEvent(event);
51 }
52
53 //-----------------------------------------------------------------------------
54 // "changed"
55 //-----------------------------------------------------------------------------
56
57 static void
58 gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
59 {
60 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->m_windowId );
61 event.SetString( copystring(combo->GetValue()) );
62 event.SetEventObject( combo );
63 combo->GetEventHandler()->ProcessEvent( event );
64 delete[] event.GetString();
65 }
66
67 //-----------------------------------------------------------------------------
68 // wxComboBox
69 //-----------------------------------------------------------------------------
70
71 IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
72
73 BEGIN_EVENT_TABLE(wxComboBox, wxControl)
74 EVT_SIZE(wxComboBox::OnSize)
75 END_EVENT_TABLE()
76
77 bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
78 const wxPoint& pos, const wxSize& size,
79 int n, const wxString choices[],
80 long style, const wxValidator& validator,
81 const wxString& name )
82 {
83 m_alreadySent = FALSE;
84 m_needParent = TRUE;
85 m_acceptsFocus = TRUE;
86
87 PreCreation( parent, id, pos, size, style, name );
88
89 SetValidator( validator );
90
91 m_widget = gtk_combo_new();
92
93 wxSize newSize = size;
94 if (newSize.x == -1) newSize.x = 100;
95 if (newSize.y == -1) newSize.y = 26;
96 SetSize( newSize.x, newSize.y );
97
98 GtkWidget *list = GTK_COMBO(m_widget)->list;
99
100 for (int i = 0; i < n; i++)
101 {
102 GtkWidget *list_item = gtk_list_item_new_with_label( choices[i] );
103
104 m_clientDataList.Append( (wxObject*)NULL );
105 m_clientObjectList.Append( (wxObject*)NULL );
106
107 gtk_container_add( GTK_CONTAINER(list), list_item );
108
109 gtk_widget_realize( list_item );
110 gtk_widget_realize( GTK_BIN(list_item)->child );
111
112 gtk_widget_show( list_item );
113
114 gtk_signal_connect( GTK_OBJECT(list_item), "select",
115 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
116 }
117
118 m_parent->AddChild( this );
119
120 (m_parent->m_insertCallback)( m_parent, this );
121
122 PostCreation();
123
124 ConnectWidget( GTK_COMBO(m_widget)->button );
125
126 if (!value.IsNull()) SetValue( value );
127
128 gtk_widget_realize( GTK_COMBO(m_widget)->list );
129 gtk_widget_realize( GTK_COMBO(m_widget)->entry );
130 gtk_widget_realize( GTK_COMBO(m_widget)->button );
131
132 gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed",
133 GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this);
134
135 SetBackgroundColour( parent->GetBackgroundColour() );
136 SetForegroundColour( parent->GetForegroundColour() );
137 SetFont( parent->GetFont() );
138
139 Show( TRUE );
140
141 return TRUE;
142 }
143
144 wxComboBox::~wxComboBox()
145 {
146 wxNode *node = m_clientDataList.First();
147 while (node)
148 {
149 wxClientData *cd = (wxClientData*)node->Data();
150 if (cd) delete cd;
151 node = node->Next();
152 }
153 m_clientDataList.Clear();
154 }
155
156 void wxComboBox::AppendCommon( const wxString &item )
157 {
158 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
159
160 GtkWidget *list = GTK_COMBO(m_widget)->list;
161
162 GtkWidget *list_item = gtk_list_item_new_with_label( item );
163
164 gtk_signal_connect( GTK_OBJECT(list_item), "select",
165 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
166
167 gtk_container_add( GTK_CONTAINER(list), list_item );
168
169 if (m_widgetStyle) ApplyWidgetStyle();
170
171 gtk_widget_show( list_item );
172 }
173
174 void wxComboBox::Append( const wxString &item )
175 {
176 m_clientDataList.Append( (wxObject*) NULL );
177 m_clientObjectList.Append( (wxObject*) NULL );
178
179 AppendCommon( item );
180 }
181
182 void wxComboBox::Append( const wxString &item, void *clientData )
183 {
184 m_clientDataList.Append( (wxObject*) clientData );
185 m_clientObjectList.Append( (wxObject*)NULL );
186
187 AppendCommon( item );
188 }
189
190 void wxComboBox::Append( const wxString &item, wxClientData *clientData )
191 {
192 m_clientDataList.Append( (wxObject*) NULL );
193 m_clientObjectList.Append( (wxObject*) clientData );
194
195 AppendCommon( item );
196 }
197
198 void wxComboBox::SetClientData( int n, void* clientData )
199 {
200 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
201
202 wxNode *node = m_clientDataList.Nth( n );
203 if (!node) return;
204
205 node->SetData( (wxObject*) clientData );
206 }
207
208 void* wxComboBox::GetClientData( int n )
209 {
210 wxCHECK_MSG( m_widget != NULL, NULL, "invalid combobox" );
211
212 wxNode *node = m_clientDataList.Nth( n );
213 if (!node) return NULL;
214
215 return node->Data();
216 }
217
218 void wxComboBox::SetClientObject( int n, wxClientData* clientData )
219 {
220 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
221
222 wxNode *node = m_clientObjectList.Nth( n );
223 if (!node) return;
224
225 wxClientData *cd = (wxClientData*) node->Data();
226 if (cd) delete cd;
227
228 node->SetData( (wxObject*) clientData );
229 }
230
231 wxClientData* wxComboBox::GetClientObject( int n )
232 {
233 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, "invalid combobox" );
234
235 wxNode *node = m_clientDataList.Nth( n );
236 if (!node) return (wxClientData*) NULL;
237
238 return (wxClientData*) node->Data();
239 }
240
241 void wxComboBox::Clear()
242 {
243 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
244
245 GtkWidget *list = GTK_COMBO(m_widget)->list;
246 gtk_list_clear_items( GTK_LIST(list), 0, Number() );
247
248 wxNode *node = m_clientObjectList.First();
249 while (node)
250 {
251 wxClientData *cd = (wxClientData*)node->Data();
252 if (cd) delete cd;
253 node = node->Next();
254 }
255 m_clientObjectList.Clear();
256
257 m_clientDataList.Clear();
258 }
259
260 void wxComboBox::Delete( int n )
261 {
262 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
263
264 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
265
266 GList *child = g_list_nth( listbox->children, n );
267
268 if (!child)
269 {
270 wxFAIL_MSG("wrong index");
271 return;
272 }
273
274 GList *list = g_list_append( (GList*) NULL, child->data );
275 gtk_list_remove_items( listbox, list );
276 g_list_free( list );
277
278 wxNode *node = m_clientObjectList.Nth( n );
279 if (node)
280 {
281 wxClientData *cd = (wxClientData*)node->Data();
282 if (cd) delete cd;
283 m_clientObjectList.DeleteNode( node );
284 }
285
286 node = m_clientDataList.Nth( n );
287 if (node)
288 {
289 m_clientDataList.DeleteNode( node );
290 }
291 }
292
293 int wxComboBox::FindString( const wxString &item )
294 {
295 wxCHECK_MSG( m_widget != NULL, -1, "invalid combobox" );
296
297 GtkWidget *list = GTK_COMBO(m_widget)->list;
298
299 GList *child = GTK_LIST(list)->children;
300 int count = 0;
301 while (child)
302 {
303 GtkBin *bin = GTK_BIN( child->data );
304 GtkLabel *label = GTK_LABEL( bin->child );
305 if (item == label->label) return count;
306 count++;
307 child = child->next;
308 }
309
310 wxFAIL_MSG( "wxComboBox: string not found" );
311
312 return -1;
313 }
314
315 int wxComboBox::GetSelection() const
316 {
317 wxCHECK_MSG( m_widget != NULL, -1, "invalid combobox" );
318
319 GtkWidget *list = GTK_COMBO(m_widget)->list;
320
321 GList *selection = GTK_LIST(list)->selection;
322 if (selection)
323 {
324 GList *child = GTK_LIST(list)->children;
325 int count = 0;
326 while (child)
327 {
328 if (child->data == selection->data) return count;
329 count++;
330 child = child->next;
331 }
332 }
333
334 wxFAIL_MSG( "wxComboBox: no selection" );
335
336 return -1;
337 }
338
339 wxString wxComboBox::GetString( int n ) const
340 {
341 wxCHECK_MSG( m_widget != NULL, "", "invalid combobox" );
342
343 GtkWidget *list = GTK_COMBO(m_widget)->list;
344
345 GList *child = g_list_nth( GTK_LIST(list)->children, n );
346 if (child)
347 {
348 GtkBin *bin = GTK_BIN( child->data );
349 GtkLabel *label = GTK_LABEL( bin->child );
350 return label->label;
351 }
352
353 wxFAIL_MSG( "wxComboBox: wrong index" );
354
355 return "";
356 }
357
358 wxString wxComboBox::GetStringSelection() const
359 {
360 wxCHECK_MSG( m_widget != NULL, "", "invalid combobox" );
361
362 GtkWidget *list = GTK_COMBO(m_widget)->list;
363
364 GList *selection = GTK_LIST(list)->selection;
365 if (selection)
366 {
367 GtkBin *bin = GTK_BIN( selection->data );
368 wxString tmp = GTK_LABEL( bin->child )->label;
369 return tmp;
370 }
371
372 wxFAIL_MSG( "wxComboBox: no selection" );
373
374 return "";
375 }
376
377 int wxComboBox::Number() const
378 {
379 wxCHECK_MSG( m_widget != NULL, 0, "invalid combobox" );
380
381 GtkWidget *list = GTK_COMBO(m_widget)->list;
382
383 GList *child = GTK_LIST(list)->children;
384 int count = 0;
385 while (child) { count++; child = child->next; }
386 return count;
387 }
388
389 void wxComboBox::SetSelection( int n )
390 {
391 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
392
393 GtkWidget *list = GTK_COMBO(m_widget)->list;
394 gtk_list_select_item( GTK_LIST(list), n );
395 }
396
397 void wxComboBox::SetStringSelection( const wxString &string )
398 {
399 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
400
401 int res = FindString( string );
402 if (res == -1) return;
403 SetSelection( res );
404 }
405
406 wxString wxComboBox::GetValue() const
407 {
408 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
409 wxString tmp = gtk_entry_get_text( GTK_ENTRY(entry) );
410 return tmp;
411 }
412
413 void wxComboBox::SetValue( const wxString& value )
414 {
415 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
416
417 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
418 wxString tmp = "";
419 if (!value.IsNull()) tmp = value;
420 gtk_entry_set_text( GTK_ENTRY(entry), tmp );
421 }
422
423 void wxComboBox::Copy()
424 {
425 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
426
427 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
428 #if (GTK_MINOR_VERSION == 1)
429 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) );
430 #else
431 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
432 #endif
433 }
434
435 void wxComboBox::Cut()
436 {
437 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
438
439 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
440 #if (GTK_MINOR_VERSION == 1)
441 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) );
442 #else
443 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
444 #endif
445 }
446
447 void wxComboBox::Paste()
448 {
449 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
450
451 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
452 #if (GTK_MINOR_VERSION == 1)
453 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) );
454 #else
455 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
456 #endif
457 }
458
459 void wxComboBox::SetInsertionPoint( long pos )
460 {
461 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
462
463 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
464 gtk_entry_set_position( GTK_ENTRY(entry), (int)pos );
465 }
466
467 void wxComboBox::SetInsertionPointEnd()
468 {
469 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
470
471 SetInsertionPoint( -1 );
472 }
473
474 long wxComboBox::GetInsertionPoint() const
475 {
476 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
477 return (long) GTK_EDITABLE(entry)->current_pos;
478 }
479
480 long wxComboBox::GetLastPosition() const
481 {
482 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
483 int pos = GTK_ENTRY(entry)->text_length;
484 return (long) pos-1;
485 }
486
487 void wxComboBox::Replace( long from, long to, const wxString& value )
488 {
489 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
490
491 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
492 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
493 if (value.IsNull()) return;
494 gint pos = (gint)to;
495 gtk_editable_insert_text( GTK_EDITABLE(entry), value, value.Length(), &pos );
496 }
497
498 void wxComboBox::Remove(long from, long to)
499 {
500 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
501
502 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
503 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
504 }
505
506 void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) )
507 {
508 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
509 }
510
511 void wxComboBox::SetEditable( bool WXUNUSED(editable) )
512 {
513 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
514 }
515
516 void wxComboBox::OnSize( wxSizeEvent &event )
517 {
518 wxControl::OnSize( event );
519
520 int w = 21;
521 gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height );
522
523 gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y );
524 gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height );
525 }
526
527 void wxComboBox::ApplyWidgetStyle()
528 {
529 SetWidgetStyle();
530
531 gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
532 gtk_widget_set_style( GTK_COMBO(m_widget)->entry, m_widgetStyle );
533 gtk_widget_set_style( GTK_COMBO(m_widget)->list, m_widgetStyle );
534
535 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
536 GList *child = list->children;
537 while (child)
538 {
539 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
540
541 GtkBin *bin = GTK_BIN(child->data);
542 gtk_widget_set_style( bin->child, m_widgetStyle );
543
544 child = child->next;
545 }
546 }
547
548 GtkWidget* wxComboBox::GetConnectWidget()
549 {
550 return GTK_COMBO(m_widget)->entry;
551 }
552
553 bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
554 {
555 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
556 (window == GTK_COMBO(m_widget)->button->window ) );
557 }
558