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