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