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