]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/combobox.cpp
bug in wxString::Printf() corrected (the length of the string wasn't updated
[wxWidgets.git] / src / gtk / combobox.cpp
CommitLineData
53010e52
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: combobox.cpp
3// Purpose:
4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
53010e52
RR
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "combobox.h"
12#endif
13
ac57418f 14
53010e52 15#include "wx/combobox.h"
ac57418f
RR
16
17#ifdef wxUSE_COMBOBOX
18
1a5a8367 19#include <wx/intl.h>
53010e52 20
83624f79
RR
21#include "gdk/gdk.h"
22#include "gtk/gtk.h"
23
47908e25
RR
24//-----------------------------------------------------------------------------
25// data
26//-----------------------------------------------------------------------------
27
28extern bool g_blockEventsOnDrag;
29
53010e52 30//-----------------------------------------------------------------------------
e1e955e1 31// "select"
47908e25 32//-----------------------------------------------------------------------------
47908e25
RR
33
34static void gtk_combo_clicked_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo )
53010e52 35{
fd0eed64
RR
36 if (!combo->HasVMT()) return;
37 if (g_blockEventsOnDrag) return;
66bd6b93 38
fd0eed64
RR
39 if (combo->m_alreadySent)
40 {
41 combo->m_alreadySent = FALSE;
42 return;
43 }
47908e25 44
fd0eed64 45 combo->m_alreadySent = TRUE;
47908e25 46
fd0eed64
RR
47 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, combo->GetId());
48 event.SetInt( combo->GetSelection() );
49 wxString tmp( combo->GetStringSelection() );
50 event.SetString( WXSTRINGCAST(tmp) );
51 event.SetEventObject(combo);
52 combo->GetEventHandler()->ProcessEvent(event);
6de97a3b 53}
47908e25 54
e1e955e1
RR
55//-----------------------------------------------------------------------------
56// wxComboBox
53010e52
RR
57//-----------------------------------------------------------------------------
58
59IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl)
60
b4071e91 61BEGIN_EVENT_TABLE(wxComboBox, wxControl)
fd0eed64 62 EVT_SIZE(wxComboBox::OnSize)
b4071e91
RR
63END_EVENT_TABLE()
64
fd0eed64
RR
65bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value,
66 const wxPoint& pos, const wxSize& size,
67 int n, const wxString choices[],
68 long style, const wxValidator& validator,
69 const wxString& name )
53010e52 70{
fd0eed64
RR
71 m_alreadySent = FALSE;
72 m_needParent = TRUE;
b292e2f5 73 m_acceptsFocus = TRUE;
53010e52 74
fd0eed64 75 PreCreation( parent, id, pos, size, style, name );
53010e52 76
fd0eed64 77 SetValidator( validator );
6de97a3b 78
fd0eed64 79 m_widget = gtk_combo_new();
53010e52 80
fd0eed64
RR
81 wxSize newSize = size;
82 if (newSize.x == -1) newSize.x = 100;
83 if (newSize.y == -1) newSize.y = 26;
84 SetSize( newSize.x, newSize.y );
53010e52 85
fd0eed64 86 GtkWidget *list = GTK_COMBO(m_widget)->list;
53010e52 87
fd0eed64
RR
88 for (int i = 0; i < n; i++)
89 {
90 GtkWidget *list_item = gtk_list_item_new_with_label( choices[i] );
53010e52 91
fd0eed64 92 m_clientDataList.Append( (wxObject*)NULL );
f5e27805 93 m_clientObjectList.Append( (wxObject*)NULL );
f96aa4d9 94
fd0eed64 95 gtk_container_add( GTK_CONTAINER(list), list_item );
53010e52 96
fd0eed64
RR
97 gtk_widget_realize( list_item );
98 gtk_widget_realize( GTK_BIN(list_item)->child );
47908e25 99
fd0eed64 100 gtk_widget_show( list_item );
66bd6b93 101
fd0eed64
RR
102 gtk_signal_connect( GTK_OBJECT(list_item), "select",
103 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
104 }
53010e52 105
fd0eed64 106 m_parent->AddChild( this );
6ca41e57 107
fd0eed64 108 (m_parent->m_insertCallback)( m_parent, this );
6ca41e57 109
fd0eed64 110 PostCreation();
53010e52 111
fd0eed64 112 ConnectWidget( GTK_COMBO(m_widget)->button );
b4071e91 113
fd0eed64 114 if (!value.IsNull()) SetValue( value );
53010e52 115
fd0eed64
RR
116 gtk_widget_realize( GTK_COMBO(m_widget)->list );
117 gtk_widget_realize( GTK_COMBO(m_widget)->entry );
118 gtk_widget_realize( GTK_COMBO(m_widget)->button );
f96aa4d9 119
fd0eed64
RR
120 SetBackgroundColour( parent->GetBackgroundColour() );
121 SetForegroundColour( parent->GetForegroundColour() );
f96aa4d9 122
fd0eed64 123 Show( TRUE );
53010e52 124
fd0eed64
RR
125 return TRUE;
126}
127
128wxComboBox::~wxComboBox()
129{
130 wxNode *node = m_clientDataList.First();
131 while (node)
132 {
133 wxClientData *cd = (wxClientData*)node->Data();
134 if (cd) delete cd;
135 node = node->Next();
136 }
137 m_clientDataList.Clear();
6de97a3b 138}
53010e52 139
fd0eed64 140void wxComboBox::AppendCommon( const wxString &item )
53010e52 141{
fd0eed64
RR
142 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
143
144 GtkWidget *list = GTK_COMBO(m_widget)->list;
145
146 GtkWidget *list_item = gtk_list_item_new_with_label( item );
f96aa4d9 147
fd0eed64
RR
148 gtk_signal_connect( GTK_OBJECT(list_item), "select",
149 GTK_SIGNAL_FUNC(gtk_combo_clicked_callback), (gpointer)this );
150
151 gtk_container_add( GTK_CONTAINER(list), list_item );
152
153 if (m_widgetStyle) ApplyWidgetStyle();
47908e25 154
fd0eed64 155 gtk_widget_show( list_item );
6de97a3b 156}
53010e52
RR
157
158void wxComboBox::Append( const wxString &item )
47908e25 159{
f5e27805
RR
160 m_clientDataList.Append( (wxObject*) NULL );
161 m_clientObjectList.Append( (wxObject*) NULL );
f96aa4d9 162
fd0eed64 163 AppendCommon( item );
6de97a3b 164}
47908e25 165
fd0eed64 166void wxComboBox::Append( const wxString &item, void *clientData )
53010e52 167{
f5e27805
RR
168 m_clientDataList.Append( (wxObject*) clientData );
169 m_clientObjectList.Append( (wxObject*)NULL );
2f6407b9 170
fd0eed64 171 AppendCommon( item );
6de97a3b 172}
53010e52 173
fd0eed64 174void wxComboBox::Append( const wxString &item, wxClientData *clientData )
53010e52 175{
f5e27805
RR
176 m_clientDataList.Append( (wxObject*) NULL );
177 m_clientObjectList.Append( (wxObject*) clientData );
f96aa4d9 178
fd0eed64
RR
179 AppendCommon( item );
180}
181
182void wxComboBox::SetClientData( int n, void* clientData )
183{
184 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
2f6407b9 185
fd0eed64
RR
186 wxNode *node = m_clientDataList.Nth( n );
187 if (!node) return;
2f6407b9 188
f5e27805 189 node->SetData( (wxObject*) clientData );
6de97a3b 190}
53010e52 191
fd0eed64 192void* wxComboBox::GetClientData( int n )
53010e52 193{
fd0eed64 194 wxCHECK_MSG( m_widget != NULL, NULL, "invalid combobox" );
f96aa4d9 195
fd0eed64
RR
196 wxNode *node = m_clientDataList.Nth( n );
197 if (!node) return NULL;
198
f5e27805 199 return node->Data();
fd0eed64
RR
200}
201
202void wxComboBox::SetClientObject( int n, wxClientData* clientData )
203{
204 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
205
f5e27805 206 wxNode *node = m_clientObjectList.Nth( n );
fd0eed64 207 if (!node) return;
b6af8d80 208
fd0eed64
RR
209 wxClientData *cd = (wxClientData*) node->Data();
210 if (cd) delete cd;
f5e27805 211
fd0eed64 212 node->SetData( (wxObject*) clientData );
6de97a3b 213}
53010e52 214
fd0eed64 215wxClientData* wxComboBox::GetClientObject( int n )
53010e52 216{
fd0eed64 217 wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, "invalid combobox" );
f96aa4d9 218
fd0eed64
RR
219 wxNode *node = m_clientDataList.Nth( n );
220 if (!node) return (wxClientData*) NULL;
221
222 return (wxClientData*) node->Data();
223}
224
225void wxComboBox::Clear()
226{
227 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
b6af8d80 228
fd0eed64
RR
229 GtkWidget *list = GTK_COMBO(m_widget)->list;
230 gtk_list_clear_items( GTK_LIST(list), 0, Number() );
b6af8d80 231
f5e27805 232 wxNode *node = m_clientObjectList.First();
fd0eed64
RR
233 while (node)
234 {
235 wxClientData *cd = (wxClientData*)node->Data();
236 if (cd) delete cd;
237 node = node->Next();
238 }
f5e27805
RR
239 m_clientObjectList.Clear();
240
fd0eed64 241 m_clientDataList.Clear();
6de97a3b 242}
53010e52 243
fd0eed64 244void wxComboBox::Delete( int n )
53010e52 245{
fd0eed64
RR
246 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
247
248 GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list );
f96aa4d9 249
fd0eed64 250 GList *child = g_list_nth( listbox->children, n );
b6af8d80 251
fd0eed64
RR
252 if (!child)
253 {
254 wxFAIL_MSG("wrong index");
255 return;
256 }
257
258 GList *list = g_list_append( NULL, child->data );
259 gtk_list_remove_items( listbox, list );
260 g_list_free( list );
261
f5e27805
RR
262 wxNode *node = m_clientObjectList.Nth( n );
263 if (node)
fd0eed64
RR
264 {
265 wxClientData *cd = (wxClientData*)node->Data();
266 if (cd) delete cd;
f5e27805
RR
267 m_clientObjectList.DeleteNode( node );
268 }
269
270 node = m_clientDataList.Nth( n );
271 if (node)
272 {
fd0eed64
RR
273 m_clientDataList.DeleteNode( node );
274 }
6de97a3b 275}
53010e52 276
fd0eed64 277int wxComboBox::FindString( const wxString &item )
53010e52 278{
fd0eed64 279 wxCHECK_MSG( m_widget != NULL, -1, "invalid combobox" );
f96aa4d9 280
fd0eed64 281 GtkWidget *list = GTK_COMBO(m_widget)->list;
53010e52 282
53010e52
RR
283 GList *child = GTK_LIST(list)->children;
284 int count = 0;
285 while (child)
286 {
fd0eed64
RR
287 GtkBin *bin = GTK_BIN( child->data );
288 GtkLabel *label = GTK_LABEL( bin->child );
289 if (item == label->label) return count;
290 count++;
291 child = child->next;
292 }
293
294 wxFAIL_MSG( "wxComboBox: string not found" );
295
296 return -1;
297}
298
299int wxComboBox::GetSelection() const
300{
301 wxCHECK_MSG( m_widget != NULL, -1, "invalid combobox" );
302
303 GtkWidget *list = GTK_COMBO(m_widget)->list;
304
305 GList *selection = GTK_LIST(list)->selection;
306 if (selection)
307 {
308 GList *child = GTK_LIST(list)->children;
309 int count = 0;
310 while (child)
311 {
312 if (child->data == selection->data) return count;
313 count++;
314 child = child->next;
315 }
6de97a3b 316 }
b6af8d80 317
fd0eed64 318 wxFAIL_MSG( "wxComboBox: no selection" );
b6af8d80 319
fd0eed64 320 return -1;
6de97a3b 321}
53010e52 322
debe6624 323wxString wxComboBox::GetString( int n ) const
53010e52 324{
fd0eed64 325 wxCHECK_MSG( m_widget != NULL, "", "invalid combobox" );
f96aa4d9 326
fd0eed64 327 GtkWidget *list = GTK_COMBO(m_widget)->list;
53010e52 328
fd0eed64
RR
329 GList *child = g_list_nth( GTK_LIST(list)->children, n );
330 if (child)
331 {
332 GtkBin *bin = GTK_BIN( child->data );
333 GtkLabel *label = GTK_LABEL( bin->child );
334 return label->label;
335 }
b6af8d80 336
fd0eed64 337 wxFAIL_MSG( "wxComboBox: wrong index" );
b6af8d80 338
fd0eed64 339 return "";
6de97a3b 340}
53010e52 341
fd0eed64 342wxString wxComboBox::GetStringSelection() const
53010e52 343{
fd0eed64 344 wxCHECK_MSG( m_widget != NULL, "", "invalid combobox" );
f96aa4d9 345
fd0eed64 346 GtkWidget *list = GTK_COMBO(m_widget)->list;
53010e52 347
fd0eed64
RR
348 GList *selection = GTK_LIST(list)->selection;
349 if (selection)
350 {
351 GtkBin *bin = GTK_BIN( selection->data );
352 wxString tmp = GTK_LABEL( bin->child )->label;
353 return tmp;
354 }
b6af8d80 355
fd0eed64 356 wxFAIL_MSG( "wxComboBox: no selection" );
b6af8d80 357
fd0eed64 358 return "";
6de97a3b 359}
53010e52 360
fd0eed64 361int wxComboBox::Number() const
53010e52 362{
fd0eed64 363 wxCHECK_MSG( m_widget != NULL, 0, "invalid combobox" );
f96aa4d9 364
fd0eed64 365 GtkWidget *list = GTK_COMBO(m_widget)->list;
53010e52 366
fd0eed64
RR
367 GList *child = GTK_LIST(list)->children;
368 int count = 0;
369 while (child) { count++; child = child->next; }
370 return count;
6de97a3b 371}
53010e52 372
debe6624 373void wxComboBox::SetSelection( int n )
53010e52 374{
fd0eed64 375 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
f96aa4d9 376
fd0eed64
RR
377 GtkWidget *list = GTK_COMBO(m_widget)->list;
378 gtk_list_select_item( GTK_LIST(list), n );
6de97a3b 379}
53010e52 380
47908e25
RR
381void wxComboBox::SetStringSelection( const wxString &string )
382{
fd0eed64 383 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
f96aa4d9 384
fd0eed64
RR
385 int res = FindString( string );
386 if (res == -1) return;
387 SetSelection( res );
6de97a3b 388}
47908e25 389
fd0eed64 390wxString wxComboBox::GetValue() const
53010e52 391{
fd0eed64
RR
392 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
393 wxString tmp = gtk_entry_get_text( GTK_ENTRY(entry) );
394 return tmp;
6de97a3b 395}
53010e52
RR
396
397void wxComboBox::SetValue( const wxString& value )
398{
fd0eed64 399 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
f96aa4d9 400
fd0eed64
RR
401 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
402 wxString tmp = "";
403 if (!value.IsNull()) tmp = value;
404 gtk_entry_set_text( GTK_ENTRY(entry), tmp );
6de97a3b 405}
53010e52 406
fd0eed64 407void wxComboBox::Copy()
53010e52 408{
fd0eed64 409 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
f96aa4d9 410
fd0eed64 411 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
75ed1d15 412#if (GTK_MINOR_VERSION == 1)
fd0eed64 413 gtk_editable_copy_clipboard( GTK_EDITABLE(entry) );
75ed1d15 414#else
fd0eed64 415 gtk_editable_copy_clipboard( GTK_EDITABLE(entry), 0 );
75ed1d15 416#endif
6de97a3b 417}
53010e52 418
fd0eed64 419void wxComboBox::Cut()
53010e52 420{
fd0eed64 421 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
f96aa4d9 422
fd0eed64 423 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
75ed1d15 424#if (GTK_MINOR_VERSION == 1)
fd0eed64 425 gtk_editable_cut_clipboard( GTK_EDITABLE(entry) );
75ed1d15 426#else
fd0eed64 427 gtk_editable_cut_clipboard( GTK_EDITABLE(entry), 0 );
75ed1d15 428#endif
6de97a3b 429}
53010e52 430
fd0eed64 431void wxComboBox::Paste()
53010e52 432{
fd0eed64 433 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
f96aa4d9 434
fd0eed64 435 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
75ed1d15 436#if (GTK_MINOR_VERSION == 1)
fd0eed64 437 gtk_editable_paste_clipboard( GTK_EDITABLE(entry) );
75ed1d15 438#else
fd0eed64 439 gtk_editable_paste_clipboard( GTK_EDITABLE(entry), 0 );
75ed1d15 440#endif
6de97a3b 441}
53010e52 442
debe6624 443void wxComboBox::SetInsertionPoint( long pos )
53010e52 444{
fd0eed64 445 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
f96aa4d9 446
fd0eed64
RR
447 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
448 int tmp = (int) pos;
449 gtk_entry_set_position( GTK_ENTRY(entry), tmp );
6de97a3b 450}
53010e52 451
fd0eed64 452void wxComboBox::SetInsertionPointEnd()
53010e52 453{
fd0eed64 454 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
f96aa4d9 455
fd0eed64
RR
456 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
457 int pos = GTK_ENTRY(entry)->text_length;
458 SetInsertionPoint( pos-1 );
6de97a3b 459}
53010e52 460
fd0eed64 461long wxComboBox::GetInsertionPoint() const
53010e52 462{
fd0eed64
RR
463 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
464 return (long) GTK_EDITABLE(entry)->current_pos;
6de97a3b 465}
53010e52 466
fd0eed64 467long wxComboBox::GetLastPosition() const
53010e52 468{
fd0eed64
RR
469 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
470 int pos = GTK_ENTRY(entry)->text_length;
471 return (long) pos-1;
6de97a3b 472}
53010e52 473
debe6624 474void wxComboBox::Replace( long from, long to, const wxString& value )
53010e52 475{
fd0eed64 476 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
f96aa4d9 477
fd0eed64
RR
478 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
479 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
480 if (value.IsNull()) return;
481 gint pos = (gint)to;
482 gtk_editable_insert_text( GTK_EDITABLE(entry), value, value.Length(), &pos );
6de97a3b 483}
53010e52 484
debe6624 485void wxComboBox::Remove(long from, long to)
53010e52 486{
fd0eed64 487 wxCHECK_RET( m_widget != NULL, "invalid combobox" );
f96aa4d9 488
fd0eed64
RR
489 GtkWidget *entry = GTK_COMBO(m_widget)->entry;
490 gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to );
6de97a3b 491}
53010e52 492
debe6624 493void wxComboBox::SetSelection( long WXUNUSED(from), long WXUNUSED(to) )
53010e52 494{
fd0eed64 495 wxFAIL_MSG( "wxComboBox::SetSelection not implemented" );
6de97a3b 496}
53010e52 497
debe6624 498void wxComboBox::SetEditable( bool WXUNUSED(editable) )
53010e52 499{
fd0eed64 500 wxFAIL_MSG( "wxComboBox::SetEditable not implemented" );
b4071e91
RR
501}
502
503void wxComboBox::OnSize( wxSizeEvent &event )
504{
fd0eed64 505 wxControl::OnSize( event );
b4071e91 506
fd0eed64
RR
507 int w = 21;
508 gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height );
b4071e91 509
fd0eed64
RR
510 gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y );
511 gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height );
6de97a3b 512}
53010e52 513
58614078 514void wxComboBox::ApplyWidgetStyle()
868a2826 515{
fd0eed64 516 SetWidgetStyle();
f96aa4d9 517
fd0eed64
RR
518 gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle );
519 gtk_widget_set_style( GTK_COMBO(m_widget)->entry, m_widgetStyle );
520 gtk_widget_set_style( GTK_COMBO(m_widget)->list, m_widgetStyle );
868a2826 521
fd0eed64
RR
522 GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list );
523 GList *child = list->children;
524 while (child)
525 {
526 gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle );
58614078 527
fd0eed64
RR
528 GtkBin *bin = GTK_BIN(child->data);
529 gtk_widget_set_style( bin->child, m_widgetStyle );
868a2826 530
fd0eed64
RR
531 child = child->next;
532 }
868a2826 533}
b4071e91 534
fd0eed64 535GtkWidget* wxComboBox::GetConnectWidget()
97b3455a 536{
fd0eed64 537 return GTK_COMBO(m_widget)->entry;
97b3455a
RR
538}
539
b4071e91
RR
540bool wxComboBox::IsOwnGtkWindow( GdkWindow *window )
541{
fd0eed64
RR
542 return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) ||
543 (window == GTK_COMBO(m_widget)->button->window ) );
b4071e91 544}
ac57418f
RR
545
546#endif
547
548 // wxUSE_COMBOBOX