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