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