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