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