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