| 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 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
| 11 | #pragma implementation "combobox.h" |
| 12 | #endif |
| 13 | |
| 14 | // For compilers that support precompilation, includes "wx.h". |
| 15 | #include "wx/wxprec.h" |
| 16 | |
| 17 | #include "wx/combobox.h" |
| 18 | |
| 19 | #if wxUSE_COMBOBOX |
| 20 | |
| 21 | #include "wx/settings.h" |
| 22 | #include "wx/arrstr.h" |
| 23 | #include "wx/intl.h" |
| 24 | |
| 25 | #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED |
| 26 | |
| 27 | #include "wx/gtk/private.h" |
| 28 | |
| 29 | //----------------------------------------------------------------------------- |
| 30 | // idle system |
| 31 | //----------------------------------------------------------------------------- |
| 32 | |
| 33 | extern void wxapp_install_idle_handler(); |
| 34 | extern bool g_isIdle; |
| 35 | |
| 36 | //----------------------------------------------------------------------------- |
| 37 | // data |
| 38 | //----------------------------------------------------------------------------- |
| 39 | |
| 40 | extern bool g_blockEventsOnDrag; |
| 41 | |
| 42 | //----------------------------------------------------------------------------- |
| 43 | // "select-child" - click/cursor get select-child, changed, select-child |
| 44 | //----------------------------------------------------------------------------- |
| 45 | |
| 46 | static void |
| 47 | gtk_combo_select_child_callback( GtkList *WXUNUSED(list), GtkWidget *WXUNUSED(widget), wxComboBox *combo ) |
| 48 | { |
| 49 | if (g_isIdle) wxapp_install_idle_handler(); |
| 50 | |
| 51 | if (!combo->m_hasVMT) return; |
| 52 | |
| 53 | if (g_blockEventsOnDrag) return; |
| 54 | |
| 55 | int curSelection = combo->GetSelection(); |
| 56 | |
| 57 | if (combo->m_prevSelection != curSelection) |
| 58 | { |
| 59 | GtkWidget *list = GTK_COMBO(combo->m_widget)->list; |
| 60 | gtk_list_unselect_item( GTK_LIST(list), combo->m_prevSelection ); |
| 61 | } |
| 62 | combo->m_prevSelection = curSelection; |
| 63 | |
| 64 | wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, combo->GetId() ); |
| 65 | event.SetInt( curSelection ); |
| 66 | event.SetString( combo->GetStringSelection() ); |
| 67 | event.SetEventObject( combo ); |
| 68 | |
| 69 | combo->GetEventHandler()->ProcessEvent( event ); |
| 70 | } |
| 71 | |
| 72 | //----------------------------------------------------------------------------- |
| 73 | // "changed" - typing and list item matches get changed, select-child |
| 74 | // if it doesn't match an item then just get a single changed |
| 75 | //----------------------------------------------------------------------------- |
| 76 | |
| 77 | static void |
| 78 | gtk_text_changed_callback( GtkWidget *WXUNUSED(widget), wxComboBox *combo ) |
| 79 | { |
| 80 | if (g_isIdle) wxapp_install_idle_handler(); |
| 81 | |
| 82 | if (!combo->m_hasVMT) return; |
| 83 | |
| 84 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, combo->GetId() ); |
| 85 | event.SetString( combo->GetValue() ); |
| 86 | event.SetEventObject( combo ); |
| 87 | combo->GetEventHandler()->ProcessEvent( event ); |
| 88 | } |
| 89 | |
| 90 | static void |
| 91 | gtk_dummy_callback(GtkEntry *WXUNUSED(entry), GtkCombo *WXUNUSED(combo)) |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | //----------------------------------------------------------------------------- |
| 96 | // wxComboBox |
| 97 | //----------------------------------------------------------------------------- |
| 98 | |
| 99 | IMPLEMENT_DYNAMIC_CLASS(wxComboBox,wxControl) |
| 100 | |
| 101 | BEGIN_EVENT_TABLE(wxComboBox, wxControl) |
| 102 | EVT_SIZE(wxComboBox::OnSize) |
| 103 | EVT_CHAR(wxComboBox::OnChar) |
| 104 | END_EVENT_TABLE() |
| 105 | |
| 106 | bool wxComboBox::Create( wxWindow *parent, wxWindowID id, |
| 107 | const wxString& value, |
| 108 | const wxPoint& pos, const wxSize& size, |
| 109 | const wxArrayString& choices, |
| 110 | long style, const wxValidator& validator, |
| 111 | const wxString& name ) |
| 112 | { |
| 113 | wxCArrayString chs(choices); |
| 114 | |
| 115 | return Create( parent, id, value, pos, size, chs.GetCount(), |
| 116 | chs.GetStrings(), style, validator, name ); |
| 117 | } |
| 118 | |
| 119 | bool wxComboBox::Create( wxWindow *parent, wxWindowID id, const wxString& value, |
| 120 | const wxPoint& pos, const wxSize& size, |
| 121 | int n, const wxString choices[], |
| 122 | long style, const wxValidator& validator, |
| 123 | const wxString& name ) |
| 124 | { |
| 125 | m_alreadySent = FALSE; |
| 126 | m_needParent = TRUE; |
| 127 | m_acceptsFocus = TRUE; |
| 128 | m_prevSelection = 0; |
| 129 | |
| 130 | if (!PreCreation( parent, pos, size ) || |
| 131 | !CreateBase( parent, id, pos, size, style, validator, name )) |
| 132 | { |
| 133 | wxFAIL_MSG( wxT("wxComboBox creation failed") ); |
| 134 | return FALSE; |
| 135 | } |
| 136 | |
| 137 | m_widget = gtk_combo_new(); |
| 138 | GtkCombo *combo = GTK_COMBO(m_widget); |
| 139 | |
| 140 | // Disable GTK's broken events ... |
| 141 | gtk_signal_disconnect( GTK_OBJECT(combo->entry), combo->entry_change_id ); |
| 142 | // ... and add surogate handler. |
| 143 | combo->entry_change_id = gtk_signal_connect (GTK_OBJECT (combo->entry), "changed", |
| 144 | (GtkSignalFunc) gtk_dummy_callback, combo); |
| 145 | |
| 146 | // make it more useable |
| 147 | gtk_combo_set_use_arrows_always( GTK_COMBO(m_widget), TRUE ); |
| 148 | |
| 149 | // and case-sensitive |
| 150 | gtk_combo_set_case_sensitive( GTK_COMBO(m_widget), TRUE ); |
| 151 | |
| 152 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
| 153 | |
| 154 | #ifndef __WXGTK20__ |
| 155 | // gtk_list_set_selection_mode( GTK_LIST(list), GTK_SELECTION_MULTIPLE ); |
| 156 | #endif |
| 157 | |
| 158 | for (int i = 0; i < n; i++) |
| 159 | { |
| 160 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( choices[i] ) ); |
| 161 | |
| 162 | m_clientDataList.Append( (wxObject*)NULL ); |
| 163 | m_clientObjectList.Append( (wxObject*)NULL ); |
| 164 | |
| 165 | gtk_container_add( GTK_CONTAINER(list), list_item ); |
| 166 | |
| 167 | gtk_widget_show( list_item ); |
| 168 | } |
| 169 | |
| 170 | m_parent->DoAddChild( this ); |
| 171 | |
| 172 | m_focusWidget = combo->entry; |
| 173 | |
| 174 | PostCreation(); |
| 175 | InheritAttributes(); |
| 176 | |
| 177 | ConnectWidget( combo->button ); |
| 178 | |
| 179 | // MSW's combo box shows the value and the selection is -1 |
| 180 | gtk_entry_set_text( GTK_ENTRY(combo->entry), wxGTK_CONV(value) ); |
| 181 | gtk_list_unselect_all( GTK_LIST(combo->list) ); |
| 182 | |
| 183 | if (style & wxCB_READONLY) |
| 184 | gtk_entry_set_editable( GTK_ENTRY( combo->entry ), FALSE ); |
| 185 | |
| 186 | gtk_signal_connect( GTK_OBJECT(combo->entry), "changed", |
| 187 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); |
| 188 | |
| 189 | gtk_signal_connect( GTK_OBJECT(combo->list), "select-child", |
| 190 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); |
| 191 | |
| 192 | wxSize size_best( DoGetBestSize() ); |
| 193 | wxSize new_size( size ); |
| 194 | if (new_size.x == -1) |
| 195 | new_size.x = size_best.x; |
| 196 | if (new_size.y == -1) |
| 197 | new_size.y = size_best.y; |
| 198 | if (new_size.y > size_best.y) |
| 199 | new_size.y = size_best.y; |
| 200 | if ((new_size.x != size.x) || (new_size.y != size.y)) |
| 201 | { |
| 202 | SetSize( new_size.x, new_size.y ); |
| 203 | |
| 204 | // This is required for tool bar support |
| 205 | gtk_widget_set_usize( m_widget, new_size.x, new_size.y ); |
| 206 | } |
| 207 | |
| 208 | Show( TRUE ); |
| 209 | |
| 210 | return TRUE; |
| 211 | } |
| 212 | |
| 213 | wxComboBox::~wxComboBox() |
| 214 | { |
| 215 | wxList::compatibility_iterator node = m_clientObjectList.GetFirst(); |
| 216 | while (node) |
| 217 | { |
| 218 | wxClientData *cd = (wxClientData*)node->GetData(); |
| 219 | if (cd) delete cd; |
| 220 | node = node->GetNext(); |
| 221 | } |
| 222 | m_clientObjectList.Clear(); |
| 223 | |
| 224 | m_clientDataList.Clear(); |
| 225 | } |
| 226 | |
| 227 | void wxComboBox::SetFocus() |
| 228 | { |
| 229 | if ( m_hasFocus ) |
| 230 | { |
| 231 | // don't do anything if we already have focus |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | gtk_widget_grab_focus( m_focusWidget ); |
| 236 | } |
| 237 | |
| 238 | int wxComboBox::DoAppend( const wxString &item ) |
| 239 | { |
| 240 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
| 241 | |
| 242 | DisableEvents(); |
| 243 | |
| 244 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
| 245 | |
| 246 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); |
| 247 | |
| 248 | gtk_container_add( GTK_CONTAINER(list), list_item ); |
| 249 | |
| 250 | if (GTK_WIDGET_REALIZED(m_widget)) |
| 251 | { |
| 252 | gtk_widget_realize( list_item ); |
| 253 | gtk_widget_realize( GTK_BIN(list_item)->child ); |
| 254 | |
| 255 | if (m_widgetStyle) ApplyWidgetStyle(); |
| 256 | } |
| 257 | |
| 258 | gtk_widget_show( list_item ); |
| 259 | |
| 260 | const int count = GetCount(); |
| 261 | |
| 262 | if ( (int)m_clientDataList.GetCount() < count ) |
| 263 | m_clientDataList.Append( (wxObject*) NULL ); |
| 264 | if ( (int)m_clientObjectList.GetCount() < count ) |
| 265 | m_clientObjectList.Append( (wxObject*) NULL ); |
| 266 | |
| 267 | EnableEvents(); |
| 268 | |
| 269 | return count - 1; |
| 270 | } |
| 271 | |
| 272 | int wxComboBox::DoInsert( const wxString &item, int pos ) |
| 273 | { |
| 274 | wxCHECK_MSG( !(GetWindowStyle() & wxCB_SORT), -1, |
| 275 | wxT("can't insert into sorted list")); |
| 276 | |
| 277 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
| 278 | |
| 279 | int count = GetCount(); |
| 280 | wxCHECK_MSG( (pos >= 0) && (pos <= count), -1, wxT("invalid index") ); |
| 281 | |
| 282 | if (pos == count) |
| 283 | return Append(item); |
| 284 | |
| 285 | DisableEvents(); |
| 286 | |
| 287 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
| 288 | |
| 289 | GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); |
| 290 | |
| 291 | GList *gitem_list = g_list_alloc (); |
| 292 | gitem_list->data = list_item; |
| 293 | gtk_list_insert_items( GTK_LIST (list), gitem_list, pos ); |
| 294 | |
| 295 | if (GTK_WIDGET_REALIZED(m_widget)) |
| 296 | { |
| 297 | gtk_widget_realize( list_item ); |
| 298 | gtk_widget_realize( GTK_BIN(list_item)->child ); |
| 299 | |
| 300 | if (m_widgetStyle) |
| 301 | ApplyWidgetStyle(); |
| 302 | } |
| 303 | |
| 304 | gtk_widget_show( list_item ); |
| 305 | |
| 306 | count = GetCount(); |
| 307 | |
| 308 | if ( (int)m_clientDataList.GetCount() < count ) |
| 309 | m_clientDataList.Insert( pos, (wxObject*) NULL ); |
| 310 | if ( (int)m_clientObjectList.GetCount() < count ) |
| 311 | m_clientObjectList.Insert( pos, (wxObject*) NULL ); |
| 312 | |
| 313 | EnableEvents(); |
| 314 | |
| 315 | return pos; |
| 316 | } |
| 317 | |
| 318 | void wxComboBox::DoSetItemClientData( int n, void* clientData ) |
| 319 | { |
| 320 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 321 | |
| 322 | wxList::compatibility_iterator node = m_clientDataList.Item( n ); |
| 323 | if (!node) return; |
| 324 | |
| 325 | node->SetData( (wxObject*) clientData ); |
| 326 | } |
| 327 | |
| 328 | void* wxComboBox::DoGetItemClientData( int n ) const |
| 329 | { |
| 330 | wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid combobox") ); |
| 331 | |
| 332 | wxList::compatibility_iterator node = m_clientDataList.Item( n ); |
| 333 | |
| 334 | return node ? node->GetData() : NULL; |
| 335 | } |
| 336 | |
| 337 | void wxComboBox::DoSetItemClientObject( int n, wxClientData* clientData ) |
| 338 | { |
| 339 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 340 | |
| 341 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); |
| 342 | if (!node) return; |
| 343 | |
| 344 | // wxItemContainer already deletes data for us |
| 345 | |
| 346 | node->SetData( (wxObject*) clientData ); |
| 347 | } |
| 348 | |
| 349 | wxClientData* wxComboBox::DoGetItemClientObject( int n ) const |
| 350 | { |
| 351 | wxCHECK_MSG( m_widget != NULL, (wxClientData*)NULL, wxT("invalid combobox") ); |
| 352 | |
| 353 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); |
| 354 | |
| 355 | return node ? (wxClientData*) node->GetData() : NULL; |
| 356 | } |
| 357 | |
| 358 | void wxComboBox::Clear() |
| 359 | { |
| 360 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 361 | |
| 362 | DisableEvents(); |
| 363 | |
| 364 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
| 365 | gtk_list_clear_items( GTK_LIST(list), 0, GetCount() ); |
| 366 | |
| 367 | wxList::compatibility_iterator node = m_clientObjectList.GetFirst(); |
| 368 | while (node) |
| 369 | { |
| 370 | wxClientData *cd = (wxClientData*)node->GetData(); |
| 371 | if (cd) delete cd; |
| 372 | node = node->GetNext(); |
| 373 | } |
| 374 | m_clientObjectList.Clear(); |
| 375 | |
| 376 | m_clientDataList.Clear(); |
| 377 | |
| 378 | EnableEvents(); |
| 379 | } |
| 380 | |
| 381 | void wxComboBox::Delete( int n ) |
| 382 | { |
| 383 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 384 | |
| 385 | GtkList *listbox = GTK_LIST( GTK_COMBO(m_widget)->list ); |
| 386 | |
| 387 | GList *child = g_list_nth( listbox->children, n ); |
| 388 | |
| 389 | if (!child) |
| 390 | { |
| 391 | wxFAIL_MSG(wxT("wrong index")); |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | DisableEvents(); |
| 396 | |
| 397 | GList *list = g_list_append( (GList*) NULL, child->data ); |
| 398 | gtk_list_remove_items( listbox, list ); |
| 399 | g_list_free( list ); |
| 400 | |
| 401 | wxList::compatibility_iterator node = m_clientObjectList.Item( n ); |
| 402 | if (node) |
| 403 | { |
| 404 | wxClientData *cd = (wxClientData*)node->GetData(); |
| 405 | if (cd) delete cd; |
| 406 | m_clientObjectList.Erase( node ); |
| 407 | } |
| 408 | |
| 409 | node = m_clientDataList.Item( n ); |
| 410 | if (node) |
| 411 | m_clientDataList.Erase( node ); |
| 412 | |
| 413 | EnableEvents(); |
| 414 | } |
| 415 | |
| 416 | void wxComboBox::SetString(int n, const wxString &text) |
| 417 | { |
| 418 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 419 | |
| 420 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
| 421 | |
| 422 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); |
| 423 | if (child) |
| 424 | { |
| 425 | GtkBin *bin = GTK_BIN( child->data ); |
| 426 | GtkLabel *label = GTK_LABEL( bin->child ); |
| 427 | gtk_label_set_text(label, wxGTK_CONV(text)); |
| 428 | } |
| 429 | else |
| 430 | { |
| 431 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | int wxComboBox::FindString( const wxString &item ) const |
| 436 | { |
| 437 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
| 438 | |
| 439 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
| 440 | |
| 441 | GList *child = GTK_LIST(list)->children; |
| 442 | int count = 0; |
| 443 | while (child) |
| 444 | { |
| 445 | GtkBin *bin = GTK_BIN( child->data ); |
| 446 | GtkLabel *label = GTK_LABEL( bin->child ); |
| 447 | #ifdef __WXGTK20__ |
| 448 | wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); |
| 449 | #else |
| 450 | wxString str( label->label ); |
| 451 | #endif |
| 452 | if (item == str) |
| 453 | return count; |
| 454 | |
| 455 | count++; |
| 456 | child = child->next; |
| 457 | } |
| 458 | |
| 459 | return wxNOT_FOUND; |
| 460 | } |
| 461 | |
| 462 | int wxComboBox::GetSelection() const |
| 463 | { |
| 464 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid combobox") ); |
| 465 | |
| 466 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
| 467 | |
| 468 | GList *selection = GTK_LIST(list)->selection; |
| 469 | if (selection) |
| 470 | { |
| 471 | GList *child = GTK_LIST(list)->children; |
| 472 | int count = 0; |
| 473 | while (child) |
| 474 | { |
| 475 | if (child->data == selection->data) return count; |
| 476 | count++; |
| 477 | child = child->next; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | return -1; |
| 482 | } |
| 483 | |
| 484 | wxString wxComboBox::GetString( int n ) const |
| 485 | { |
| 486 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); |
| 487 | |
| 488 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
| 489 | |
| 490 | wxString str; |
| 491 | GList *child = g_list_nth( GTK_LIST(list)->children, n ); |
| 492 | if (child) |
| 493 | { |
| 494 | GtkBin *bin = GTK_BIN( child->data ); |
| 495 | GtkLabel *label = GTK_LABEL( bin->child ); |
| 496 | #ifdef __WXGTK20__ |
| 497 | str = wxGTK_CONV_BACK( gtk_label_get_text(label) ); |
| 498 | #else |
| 499 | str = wxString( label->label ); |
| 500 | #endif |
| 501 | } |
| 502 | else |
| 503 | { |
| 504 | wxFAIL_MSG( wxT("wxComboBox: wrong index") ); |
| 505 | } |
| 506 | |
| 507 | return str; |
| 508 | } |
| 509 | |
| 510 | wxString wxComboBox::GetStringSelection() const |
| 511 | { |
| 512 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid combobox") ); |
| 513 | |
| 514 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
| 515 | |
| 516 | GList *selection = GTK_LIST(list)->selection; |
| 517 | if (selection) |
| 518 | { |
| 519 | GtkBin *bin = GTK_BIN( selection->data ); |
| 520 | GtkLabel *label = GTK_LABEL( bin->child ); |
| 521 | #ifdef __WXGTK20__ |
| 522 | wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text(label) ) ); |
| 523 | #else |
| 524 | wxString tmp( label->label ); |
| 525 | #endif |
| 526 | return tmp; |
| 527 | } |
| 528 | |
| 529 | wxFAIL_MSG( wxT("wxComboBox: no selection") ); |
| 530 | |
| 531 | return wxT(""); |
| 532 | } |
| 533 | |
| 534 | int wxComboBox::GetCount() const |
| 535 | { |
| 536 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid combobox") ); |
| 537 | |
| 538 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
| 539 | |
| 540 | GList *child = GTK_LIST(list)->children; |
| 541 | int count = 0; |
| 542 | while (child) { count++; child = child->next; } |
| 543 | return count; |
| 544 | } |
| 545 | |
| 546 | void wxComboBox::SetSelection( int n ) |
| 547 | { |
| 548 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 549 | |
| 550 | DisableEvents(); |
| 551 | |
| 552 | GtkWidget *list = GTK_COMBO(m_widget)->list; |
| 553 | gtk_list_unselect_item( GTK_LIST(list), m_prevSelection ); |
| 554 | gtk_list_select_item( GTK_LIST(list), n ); |
| 555 | m_prevSelection = n; |
| 556 | |
| 557 | EnableEvents(); |
| 558 | } |
| 559 | |
| 560 | void wxComboBox::SetStringSelection( const wxString &string ) |
| 561 | { |
| 562 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 563 | |
| 564 | int res = FindString( string ); |
| 565 | if (res == -1) return; |
| 566 | SetSelection( res ); |
| 567 | } |
| 568 | |
| 569 | wxString wxComboBox::GetValue() const |
| 570 | { |
| 571 | GtkEntry *entry = GTK_ENTRY( GTK_COMBO(m_widget)->entry ); |
| 572 | wxString tmp( wxGTK_CONV_BACK( gtk_entry_get_text( entry ) ) ); |
| 573 | |
| 574 | #if 0 |
| 575 | for (int i = 0; i < wxStrlen(tmp.c_str()) +1; i++) |
| 576 | { |
| 577 | wxChar c = tmp[i]; |
| 578 | printf( "%d ", (int) (c) ); |
| 579 | } |
| 580 | printf( "\n" ); |
| 581 | #endif |
| 582 | |
| 583 | return tmp; |
| 584 | } |
| 585 | |
| 586 | void wxComboBox::SetValue( const wxString& value ) |
| 587 | { |
| 588 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 589 | |
| 590 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
| 591 | wxString tmp = wxT(""); |
| 592 | if (!value.IsNull()) tmp = value; |
| 593 | gtk_entry_set_text( GTK_ENTRY(entry), wxGTK_CONV( tmp ) ); |
| 594 | } |
| 595 | |
| 596 | void wxComboBox::Copy() |
| 597 | { |
| 598 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 599 | |
| 600 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
| 601 | gtk_editable_copy_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); |
| 602 | } |
| 603 | |
| 604 | void wxComboBox::Cut() |
| 605 | { |
| 606 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 607 | |
| 608 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
| 609 | gtk_editable_cut_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG ); |
| 610 | } |
| 611 | |
| 612 | void wxComboBox::Paste() |
| 613 | { |
| 614 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 615 | |
| 616 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
| 617 | gtk_editable_paste_clipboard( GTK_EDITABLE(entry) DUMMY_CLIPBOARD_ARG); |
| 618 | } |
| 619 | |
| 620 | void wxComboBox::SetInsertionPoint( long pos ) |
| 621 | { |
| 622 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 623 | |
| 624 | if ( pos == GetLastPosition() ) |
| 625 | pos = -1; |
| 626 | |
| 627 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
| 628 | gtk_entry_set_position( GTK_ENTRY(entry), (int)pos ); |
| 629 | } |
| 630 | |
| 631 | long wxComboBox::GetInsertionPoint() const |
| 632 | { |
| 633 | return (long) GET_EDITABLE_POS( GTK_COMBO(m_widget)->entry ); |
| 634 | } |
| 635 | |
| 636 | long wxComboBox::GetLastPosition() const |
| 637 | { |
| 638 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
| 639 | int pos = GTK_ENTRY(entry)->text_length; |
| 640 | return (long) pos-1; |
| 641 | } |
| 642 | |
| 643 | void wxComboBox::Replace( long from, long to, const wxString& value ) |
| 644 | { |
| 645 | wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); |
| 646 | |
| 647 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
| 648 | gtk_editable_delete_text( GTK_EDITABLE(entry), (gint)from, (gint)to ); |
| 649 | if (value.IsNull()) return; |
| 650 | gint pos = (gint)to; |
| 651 | |
| 652 | #if wxUSE_UNICODE |
| 653 | wxCharBuffer buffer = wxConvUTF8.cWX2MB( value ); |
| 654 | gtk_editable_insert_text( GTK_EDITABLE(entry), (const char*) buffer, strlen( (const char*) buffer ), &pos ); |
| 655 | #else |
| 656 | gtk_editable_insert_text( GTK_EDITABLE(entry), value.c_str(), value.Length(), &pos ); |
| 657 | #endif |
| 658 | } |
| 659 | |
| 660 | void wxComboBox::SetSelection( long from, long to ) |
| 661 | { |
| 662 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
| 663 | gtk_editable_select_region( GTK_EDITABLE(entry), (gint)from, (gint)to ); |
| 664 | } |
| 665 | |
| 666 | void wxComboBox::SetEditable( bool editable ) |
| 667 | { |
| 668 | GtkWidget *entry = GTK_COMBO(m_widget)->entry; |
| 669 | gtk_entry_set_editable( GTK_ENTRY(entry), editable ); |
| 670 | } |
| 671 | |
| 672 | void wxComboBox::OnChar( wxKeyEvent &event ) |
| 673 | { |
| 674 | if ( event.GetKeyCode() == WXK_RETURN ) |
| 675 | { |
| 676 | // GTK automatically selects an item if its in the list |
| 677 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, GetId()); |
| 678 | event.SetString( GetValue() ); |
| 679 | event.SetInt( GetSelection() ); |
| 680 | event.SetEventObject( this ); |
| 681 | |
| 682 | if (!GetEventHandler()->ProcessEvent( event )) |
| 683 | { |
| 684 | // This will invoke the dialog default action, such |
| 685 | // as the clicking the default button. |
| 686 | |
| 687 | wxWindow *top_frame = m_parent; |
| 688 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
| 689 | top_frame = top_frame->GetParent(); |
| 690 | |
| 691 | if (top_frame && GTK_IS_WINDOW(top_frame->m_widget)) |
| 692 | { |
| 693 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); |
| 694 | |
| 695 | if (window->default_widget) |
| 696 | gtk_widget_activate (window->default_widget); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | // Catch GTK event so that GTK doesn't open the drop |
| 701 | // down list upon RETURN. |
| 702 | return; |
| 703 | } |
| 704 | |
| 705 | event.Skip(); |
| 706 | } |
| 707 | |
| 708 | void wxComboBox::DisableEvents() |
| 709 | { |
| 710 | gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->list), |
| 711 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); |
| 712 | gtk_signal_disconnect_by_func( GTK_OBJECT(GTK_COMBO(m_widget)->entry), |
| 713 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); |
| 714 | } |
| 715 | |
| 716 | void wxComboBox::EnableEvents() |
| 717 | { |
| 718 | gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->list), "select-child", |
| 719 | GTK_SIGNAL_FUNC(gtk_combo_select_child_callback), (gpointer)this ); |
| 720 | gtk_signal_connect( GTK_OBJECT(GTK_COMBO(m_widget)->entry), "changed", |
| 721 | GTK_SIGNAL_FUNC(gtk_text_changed_callback), (gpointer)this ); |
| 722 | } |
| 723 | |
| 724 | void wxComboBox::OnSize( wxSizeEvent &event ) |
| 725 | { |
| 726 | event.Skip(); |
| 727 | |
| 728 | #if 0 |
| 729 | int w = 21; |
| 730 | gtk_widget_set_usize( GTK_COMBO(m_widget)->entry, m_width-w-1, m_height ); |
| 731 | |
| 732 | gtk_widget_set_uposition( GTK_COMBO(m_widget)->button, m_x+m_width-w, m_y ); |
| 733 | gtk_widget_set_usize( GTK_COMBO(m_widget)->button, w, m_height ); |
| 734 | #endif // 0 |
| 735 | } |
| 736 | |
| 737 | void wxComboBox::ApplyWidgetStyle() |
| 738 | { |
| 739 | SetWidgetStyle(); |
| 740 | |
| 741 | // gtk_widget_set_style( GTK_COMBO(m_widget)->button, m_widgetStyle ); |
| 742 | gtk_widget_set_style( GTK_COMBO(m_widget)->entry, m_widgetStyle ); |
| 743 | gtk_widget_set_style( GTK_COMBO(m_widget)->list, m_widgetStyle ); |
| 744 | |
| 745 | GtkList *list = GTK_LIST( GTK_COMBO(m_widget)->list ); |
| 746 | GList *child = list->children; |
| 747 | while (child) |
| 748 | { |
| 749 | gtk_widget_set_style( GTK_WIDGET(child->data), m_widgetStyle ); |
| 750 | |
| 751 | GtkBin *bin = GTK_BIN(child->data); |
| 752 | gtk_widget_set_style( bin->child, m_widgetStyle ); |
| 753 | |
| 754 | child = child->next; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | GtkWidget* wxComboBox::GetConnectWidget() |
| 759 | { |
| 760 | return GTK_COMBO(m_widget)->entry; |
| 761 | } |
| 762 | |
| 763 | bool wxComboBox::IsOwnGtkWindow( GdkWindow *window ) |
| 764 | { |
| 765 | return ( (window == GTK_ENTRY( GTK_COMBO(m_widget)->entry )->text_area) || |
| 766 | (window == GTK_COMBO(m_widget)->button->window ) ); |
| 767 | } |
| 768 | |
| 769 | wxSize wxComboBox::DoGetBestSize() const |
| 770 | { |
| 771 | wxSize ret( wxControl::DoGetBestSize() ); |
| 772 | |
| 773 | // we know better our horizontal extent: it depends on the longest string |
| 774 | // in the combobox |
| 775 | ret.x = 0; |
| 776 | if ( m_widget ) |
| 777 | { |
| 778 | int width; |
| 779 | size_t count = GetCount(); |
| 780 | for ( size_t n = 0; n < count; n++ ) |
| 781 | { |
| 782 | GetTextExtent( GetString(n), &width, NULL, NULL, NULL, &m_font ); |
| 783 | if ( width > ret.x ) |
| 784 | ret.x = width; |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | // empty combobox should have some reasonable default size too |
| 789 | if ( ret.x < 100 ) |
| 790 | ret.x = 100; |
| 791 | return ret; |
| 792 | } |
| 793 | |
| 794 | #endif |