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