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