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