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