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