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