wxMenu compile fix
[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 wxFAIL_MSG( _T("wxComboBox: no selection") );
364
365 return -1;
366 }
367
368 wxString wxComboBox::GetString( int n ) const
369 {
370 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid combobox") );
371
372 GtkWidget *list = GTK_COMBO(m_widget)->list;
373
374 wxString str;
375 GList *child = g_list_nth( GTK_LIST(list)->children, n );
376 if (child)
377 {
378 GtkBin *bin = GTK_BIN( child->data );
379 GtkLabel *label = GTK_LABEL( bin->child );
380 str = wxString(label->label,*wxConvCurrent);
381 }
382 else
383 {
384 wxFAIL_MSG( _T("wxComboBox: wrong index") );
385 }
386
387 return str;
388 }
389
390 wxString wxComboBox::GetStringSelection() const
391 {
392 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid combobox") );
393
394 GtkWidget *list = GTK_COMBO(m_widget)->list;
395
396 GList *selection = GTK_LIST(list)->selection;
397 if (selection)
398 {
399 GtkBin *bin = GTK_BIN( selection->data );
400 wxString tmp = wxString(GTK_LABEL( bin->child )->label,*wxConvCurrent);
401 return tmp;
402 }
403
404 wxFAIL_MSG( _T("wxComboBox: no selection") );
405
406 return _T("");
407 }
408
409 int wxComboBox::Number() const
410 {
411 wxCHECK_MSG( m_widget != NULL, 0, _T("invalid combobox") );
412
413 GtkWidget *list = GTK_COMBO(m_widget)->list;
414
415 GList *child = GTK_LIST(list)->children;
416 int count = 0;
417 while (child) { count++; child = child->next; }
418 return count;
419 }
420
421 void wxComboBox::SetSelection( int n )
422 {
423 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
424
425 GtkWidget *list = GTK_COMBO(m_widget)->list;
426 gtk_list_select_item( GTK_LIST(list), n );
427 }
428
429 void wxComboBox::SetStringSelection( const wxString &string )
430 {
431 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
432
433 int res = FindString( string );
434 if (res == -1) return;
435 SetSelection( res );
436 }
437
438 wxString wxComboBox::GetValue() const
439 {
440 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
441 wxString tmp = wxString(gtk_entry_get_text( GTK_ENTRY(entry) ),*wxConvCurrent);
442 return tmp;
443 }
444
445 void wxComboBox::SetValue( const wxString& value )
446 {
447 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
448
449 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
450 wxString tmp = _T("");
451 if (!value.IsNull()) tmp = value;
452 gtk_entry_set_text( GTK_ENTRY(entry), tmp.mbc_str() );
453 }
454
455 void wxComboBox::Copy()
456 {
457 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
458
459 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
460 #if (GTK_MINOR_VERSION > 0)
461 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) );
462 #else
463 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
464 #endif
465 }
466
467 void wxComboBox::Cut()
468 {
469 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
470
471 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
472 #if (GTK_MINOR_VERSION > 0)
473 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) );
474 #else
475 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
476 #endif
477 }
478
479 void wxComboBox::Paste()
480 {
481 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
482
483 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
484 #if (GTK_MINOR_VERSION > 0)
485 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) );
486 #else
487 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
488 #endif
489 }
490
491 void wxComboBox::SetInsertionPoint( long pos )
492 {
493 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
494
495 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
496 gtk_entry_set_position( GTK_ENTRY(entry), (int)pos );
497 }
498
499 void wxComboBox::SetInsertionPointEnd()
500 {
501 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
502
503 SetInsertionPoint( -1 );
504 }
505
506 long wxComboBox::GetInsertionPoint() const
507 {
508 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
509 return (long) GTK_EDITABLE(entry)->current_pos;
510 }
511
512 long wxComboBox::GetLastPosition() const
513 {
514 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
515 int pos = GTK_ENTRY(entry)->text_length;
516 return (long) pos-1;
517 }
518
519 void wxComboBox::Replace( long from, long to, const wxString& value )
520 {
521 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
522 // FIXME: not quite sure how to do this method right in multibyte mode
523
524 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
525 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
526 if (value.IsNull()) return;
527 gint pos = (gint)to;
528 gtk_editable_insert_text( GTK_EDITABLE(entry), value.mbc_str(), value.Length(), &pos );
529 }
530
531 void wxComboBox::Remove(long from, long to)
532 {
533 wxCHECK_RET( m_widget != NULL, _T("invalid combobox") );
534
535 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
536 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
537 }
538
539 void wxComboBox::SetSelection( long from, long to )
540 {
541 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
542 gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to );
543 }
544
545 void wxComboBox::SetEditable( bool editable )
546 {
547 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
548 gtk_entry_set_editable( GTK_ENTRY(entry), editable );
549 }
550
551 void wxComboBox::OnChar( wxKeyEvent &event )
552 {
553 if ( event.KeyCode() == WXK_RETURN )
554 {
555 wxString value = GetValue();
556
557 if ( Number() == 0 )
558 {
559 // make Enter generate "selected" event if there is only one item
560 // in the combobox - without it, it's impossible to select it at
561 // all!
562 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
563 event.SetInt( 0 );
564 event.SetString( value );
565 event.SetEventObject( this );
566 GetEventHandler()->ProcessEvent( event );
567 }
568 else
569 {
570 // add the item to the list if it's not there yet
571 if ( FindString(value) == wxNOT_FOUND )
572 {
573 Append(value);
574
575 // and generate the selected event for it
576 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
577 event.SetInt( Number() - 1 );
578 event.SetString( value );
579 event.SetEventObject( this );
580 GetEventHandler()->ProcessEvent( event );
581 }
582 //else: do nothing, this will open the listbox
583 }
584 }
585
586 event.Skip();
587 }
588
589 void wxComboBox::OnSize( wxSizeEvent &event )
590 {
591 event.Skip();
592
593 return;
594
595 int w = 21;
596 gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height );
597
598 gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y );
599 gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height );
600 }
601
602 void wxComboBox::ApplyWidgetStyle()
603 {
604 SetWidgetStyle();
605
606 // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
607 gtk_widget_set_style( GTK_COMBO(m_widget)->entry, m_widgetStyle );
608 gtk_widget_set_style( GTK_COMBO(m_widget)->list, m_widgetStyle );
609
610 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
611 GList *child = list->children;
612 while (child)
613 {
614 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
615
616 GtkBin *bin = GTK_BIN(child->data);
617 gtk_widget_set_style( bin->child, m_widgetStyle );
618
619 child = child->next;
620 }
621 }
622
623 GtkWidget* wxComboBox::GetConnectWidget()
624 {
625 return GTK_COMBO(m_widget)->entry;
626 }
627
628 bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
629 {
630 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
631 (window == GTK_COMBO(m_widget)->button->window ) );
632 }
633
634 #endif