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