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