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