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