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