| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/gtk/choice.cpp |
| 3 | // Purpose: |
| 4 | // Author: Robert Roebling |
| 5 | // Id: $Id$ |
| 6 | // Copyright: (c) 1998 Robert Roebling |
| 7 | // Licence: wxWindows licence |
| 8 | ///////////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | #include "wx/wxprec.h" |
| 11 | |
| 12 | #if wxUSE_CHOICE |
| 13 | |
| 14 | #include "wx/choice.h" |
| 15 | |
| 16 | #ifndef WX_PRECOMP |
| 17 | #include "wx/arrstr.h" |
| 18 | #endif |
| 19 | |
| 20 | // FIXME: We use GtkOptionMenu which has been deprecated since GTK+ 2.3.0 in |
| 21 | // favour of GtkComboBox. |
| 22 | // Later use GtkComboBox if GTK+ runtime version is new enough. |
| 23 | #include <gtk/gtkversion.h> |
| 24 | #if defined(GTK_DISABLE_DEPRECATED) && GTK_CHECK_VERSION(2,3,0) |
| 25 | #undef GTK_DISABLE_DEPRECATED |
| 26 | #endif |
| 27 | |
| 28 | #include "wx/gtk/private.h" |
| 29 | |
| 30 | //----------------------------------------------------------------------------- |
| 31 | // data |
| 32 | //----------------------------------------------------------------------------- |
| 33 | |
| 34 | extern bool g_blockEventsOnDrag; |
| 35 | |
| 36 | //----------------------------------------------------------------------------- |
| 37 | // "activate" |
| 38 | //----------------------------------------------------------------------------- |
| 39 | |
| 40 | extern "C" { |
| 41 | static void gtk_choice_clicked_callback( GtkWidget *WXUNUSED(widget), wxChoice *choice ) |
| 42 | { |
| 43 | if (!choice->m_hasVMT) return; |
| 44 | |
| 45 | if (g_blockEventsOnDrag) return; |
| 46 | |
| 47 | int selection = wxNOT_FOUND; |
| 48 | |
| 49 | selection = gtk_option_menu_get_history( GTK_OPTION_MENU(choice->GetHandle()) ); |
| 50 | |
| 51 | choice->m_selection_hack = selection; |
| 52 | |
| 53 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, choice->GetId() ); |
| 54 | int n = choice->GetSelection(); |
| 55 | |
| 56 | event.SetInt( n ); |
| 57 | event.SetString( choice->GetStringSelection() ); |
| 58 | event.SetEventObject(choice); |
| 59 | |
| 60 | if ( choice->HasClientObjectData() ) |
| 61 | event.SetClientObject( choice->GetClientObject(n) ); |
| 62 | else if ( choice->HasClientUntypedData() ) |
| 63 | event.SetClientData( choice->GetClientData(n) ); |
| 64 | |
| 65 | choice->GetEventHandler()->ProcessEvent(event); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | //----------------------------------------------------------------------------- |
| 70 | // wxChoice |
| 71 | //----------------------------------------------------------------------------- |
| 72 | |
| 73 | IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControlWithItems) |
| 74 | |
| 75 | wxChoice::wxChoice() |
| 76 | { |
| 77 | m_strings = (wxSortedArrayString *)NULL; |
| 78 | } |
| 79 | |
| 80 | bool wxChoice::Create( wxWindow *parent, wxWindowID id, |
| 81 | const wxPoint &pos, const wxSize &size, |
| 82 | const wxArrayString& choices, |
| 83 | long style, const wxValidator& validator, |
| 84 | const wxString &name ) |
| 85 | { |
| 86 | wxCArrayString chs(choices); |
| 87 | |
| 88 | return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(), |
| 89 | style, validator, name ); |
| 90 | } |
| 91 | |
| 92 | bool wxChoice::Create( wxWindow *parent, wxWindowID id, |
| 93 | const wxPoint &pos, const wxSize &size, |
| 94 | int n, const wxString choices[], |
| 95 | long style, const wxValidator& validator, const wxString &name ) |
| 96 | { |
| 97 | if (!PreCreation( parent, pos, size ) || |
| 98 | !CreateBase( parent, id, pos, size, style, validator, name )) |
| 99 | { |
| 100 | wxFAIL_MSG( wxT("wxChoice creation failed") ); |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | m_widget = gtk_option_menu_new(); |
| 105 | |
| 106 | if ( IsSorted() ) |
| 107 | { |
| 108 | // if our m_strings != NULL, Append() will check for it and insert |
| 109 | // items in the correct order |
| 110 | m_strings = new wxSortedArrayString; |
| 111 | } |
| 112 | |
| 113 | // If we have items, GTK will choose the first item by default |
| 114 | m_selection_hack = n > 0 ? 0 : wxNOT_FOUND; |
| 115 | |
| 116 | GtkWidget *menu = gtk_menu_new(); |
| 117 | |
| 118 | for (unsigned int i = 0; i < (unsigned int)n; i++) |
| 119 | { |
| 120 | GtkAddHelper(menu, i, choices[i]); |
| 121 | } |
| 122 | |
| 123 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); |
| 124 | |
| 125 | m_parent->DoAddChild( this ); |
| 126 | |
| 127 | PostCreation(size); |
| 128 | SetInitialSize(size); // need this too because this is a wxControlWithItems |
| 129 | |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | wxChoice::~wxChoice() |
| 134 | { |
| 135 | Clear(); |
| 136 | |
| 137 | delete m_strings; |
| 138 | } |
| 139 | |
| 140 | int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items, |
| 141 | unsigned int pos, |
| 142 | void **clientData, wxClientDataType type) |
| 143 | { |
| 144 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") ); |
| 145 | |
| 146 | const unsigned int count = items.GetCount(); |
| 147 | |
| 148 | GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); |
| 149 | |
| 150 | for ( unsigned int i = 0; i < count; ++i, ++pos ) |
| 151 | { |
| 152 | int n = GtkAddHelper(menu, pos, items[i]); |
| 153 | if ( n == wxNOT_FOUND ) |
| 154 | return n; |
| 155 | |
| 156 | AssignNewItemClientData(n, clientData, i, type); |
| 157 | } |
| 158 | |
| 159 | // if the item to insert is at or before the selection, and the selection is valid |
| 160 | if (((int)pos <= m_selection_hack) && (m_selection_hack != wxNOT_FOUND)) |
| 161 | { |
| 162 | // move the selection forward |
| 163 | m_selection_hack += count; |
| 164 | } |
| 165 | |
| 166 | // We must set the selection so that it can be read back even if |
| 167 | // the user has not modified it since GTK+ will then select the |
| 168 | // first item so well return 0. |
| 169 | if ((count > 0) && (m_selection_hack==wxNOT_FOUND)) |
| 170 | m_selection_hack = 0; |
| 171 | |
| 172 | return pos - 1; |
| 173 | } |
| 174 | |
| 175 | void wxChoice::DoSetItemClientData(unsigned int n, void* clientData) |
| 176 | { |
| 177 | m_clientData[n] = clientData; |
| 178 | } |
| 179 | |
| 180 | void* wxChoice::DoGetItemClientData(unsigned int n) const |
| 181 | { |
| 182 | return m_clientData[n]; |
| 183 | } |
| 184 | |
| 185 | void wxChoice::DoClear() |
| 186 | { |
| 187 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); |
| 188 | |
| 189 | gtk_option_menu_remove_menu( GTK_OPTION_MENU(m_widget) ); |
| 190 | GtkWidget *menu = gtk_menu_new(); |
| 191 | gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); |
| 192 | |
| 193 | m_clientData.Clear(); |
| 194 | |
| 195 | if ( m_strings ) |
| 196 | m_strings->Clear(); |
| 197 | |
| 198 | // begin with no selection |
| 199 | m_selection_hack = wxNOT_FOUND; |
| 200 | } |
| 201 | |
| 202 | void wxChoice::DoDeleteOneItem(unsigned int n) |
| 203 | { |
| 204 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); |
| 205 | wxCHECK_RET( IsValid(n), _T("invalid index in wxChoice::Delete") ); |
| 206 | |
| 207 | // if the item to delete is before the selection, and the selection is valid |
| 208 | if (((int)n < m_selection_hack) && (m_selection_hack != wxNOT_FOUND)) |
| 209 | { |
| 210 | // move the selection back one |
| 211 | m_selection_hack--; |
| 212 | } |
| 213 | else if ((int)n == m_selection_hack) |
| 214 | { |
| 215 | // invalidate the selection |
| 216 | m_selection_hack = wxNOT_FOUND; |
| 217 | } |
| 218 | |
| 219 | // VZ: apparently GTK+ doesn't have a built-in function to do it (not even |
| 220 | // in 2.0), hence this dumb implementation -- still better than nothing |
| 221 | const unsigned int count = GetCount(); |
| 222 | |
| 223 | wxArrayString items; |
| 224 | wxArrayPtrVoid itemsData; |
| 225 | items.Alloc(count); |
| 226 | itemsData.Alloc(count); |
| 227 | for ( unsigned i = 0; i < count; i++ ) |
| 228 | { |
| 229 | if ( i != n ) |
| 230 | { |
| 231 | items.Add(GetString(i)); |
| 232 | itemsData.Add(m_clientData[i]); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | wxChoice::DoClear(); |
| 237 | |
| 238 | void ** const data = &itemsData[0]; |
| 239 | if ( HasClientObjectData() ) |
| 240 | Append(items, wx_reinterpret_cast(wxClientData **, data)); |
| 241 | else |
| 242 | Append(items, data); |
| 243 | } |
| 244 | |
| 245 | int wxChoice::FindString( const wxString &string, bool bCase ) const |
| 246 | { |
| 247 | wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid choice") ); |
| 248 | |
| 249 | // If you read this code once and you think you understand |
| 250 | // it, then you are very wrong. Robert Roebling. |
| 251 | |
| 252 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
| 253 | int count = 0; |
| 254 | GList *child = menu_shell->children; |
| 255 | while (child) |
| 256 | { |
| 257 | GtkBin *bin = GTK_BIN( child->data ); |
| 258 | GtkLabel *label = (GtkLabel *) NULL; |
| 259 | if (bin->child) |
| 260 | label = GTK_LABEL(bin->child); |
| 261 | if (!label) |
| 262 | label = GTK_LABEL(GTK_BIN(m_widget)->child); |
| 263 | |
| 264 | wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); |
| 265 | |
| 266 | wxString tmp( wxGTK_CONV_BACK( gtk_label_get_text( label) ) ); |
| 267 | if (string.IsSameAs( tmp, bCase )) |
| 268 | return count; |
| 269 | |
| 270 | child = child->next; |
| 271 | count++; |
| 272 | } |
| 273 | |
| 274 | return wxNOT_FOUND; |
| 275 | } |
| 276 | |
| 277 | int wxChoice::GetSelection() const |
| 278 | { |
| 279 | wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid choice") ); |
| 280 | |
| 281 | return m_selection_hack; |
| 282 | |
| 283 | } |
| 284 | |
| 285 | void wxChoice::SetString(unsigned int n, const wxString& str) |
| 286 | { |
| 287 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); |
| 288 | |
| 289 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
| 290 | unsigned int count = 0; |
| 291 | GList *child = menu_shell->children; |
| 292 | while (child) |
| 293 | { |
| 294 | GtkBin *bin = GTK_BIN( child->data ); |
| 295 | if (count == n) |
| 296 | { |
| 297 | GtkLabel *label = (GtkLabel *) NULL; |
| 298 | if (bin->child) |
| 299 | label = GTK_LABEL(bin->child); |
| 300 | if (!label) |
| 301 | label = GTK_LABEL(GTK_BIN(m_widget)->child); |
| 302 | |
| 303 | wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); |
| 304 | |
| 305 | gtk_label_set_text( label, wxGTK_CONV( str ) ); |
| 306 | |
| 307 | InvalidateBestSize(); |
| 308 | |
| 309 | return; |
| 310 | } |
| 311 | child = child->next; |
| 312 | count++; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | wxString wxChoice::GetString(unsigned int n) const |
| 317 | { |
| 318 | wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid choice") ); |
| 319 | |
| 320 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
| 321 | unsigned int count = 0; |
| 322 | GList *child = menu_shell->children; |
| 323 | while (child) |
| 324 | { |
| 325 | GtkBin *bin = GTK_BIN( child->data ); |
| 326 | if (count == n) |
| 327 | { |
| 328 | GtkLabel *label = (GtkLabel *) NULL; |
| 329 | if (bin->child) |
| 330 | label = GTK_LABEL(bin->child); |
| 331 | if (!label) |
| 332 | label = GTK_LABEL(GTK_BIN(m_widget)->child); |
| 333 | |
| 334 | wxASSERT_MSG( label != NULL , wxT("wxChoice: invalid label") ); |
| 335 | |
| 336 | return wxString( wxGTK_CONV_BACK( gtk_label_get_text( label) ) ); |
| 337 | } |
| 338 | child = child->next; |
| 339 | count++; |
| 340 | } |
| 341 | |
| 342 | wxFAIL_MSG( wxT("wxChoice: invalid index in GetString()") ); |
| 343 | |
| 344 | return wxEmptyString; |
| 345 | } |
| 346 | |
| 347 | unsigned int wxChoice::GetCount() const |
| 348 | { |
| 349 | wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid choice") ); |
| 350 | |
| 351 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
| 352 | unsigned int count = 0; |
| 353 | GList *child = menu_shell->children; |
| 354 | while (child) |
| 355 | { |
| 356 | count++; |
| 357 | child = child->next; |
| 358 | } |
| 359 | return count; |
| 360 | } |
| 361 | |
| 362 | void wxChoice::SetSelection( int n ) |
| 363 | { |
| 364 | wxCHECK_RET( m_widget != NULL, wxT("invalid choice") ); |
| 365 | |
| 366 | int tmp = n; |
| 367 | gtk_option_menu_set_history( GTK_OPTION_MENU(m_widget), (gint)tmp ); |
| 368 | |
| 369 | // set the local selection variable manually |
| 370 | if ((n >= 0) && ((unsigned int)n < GetCount())) |
| 371 | { |
| 372 | // a valid selection has been made |
| 373 | m_selection_hack = n; |
| 374 | } |
| 375 | else if ((n == wxNOT_FOUND) || (GetCount() == 0)) |
| 376 | { |
| 377 | // invalidates the selection if there are no items |
| 378 | // or if it is specifically set to wxNOT_FOUND |
| 379 | m_selection_hack = wxNOT_FOUND; |
| 380 | } |
| 381 | else |
| 382 | { |
| 383 | // this selects the first item by default if the selection is out of bounds |
| 384 | m_selection_hack = 0; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | void wxChoice::DoApplyWidgetStyle(GtkRcStyle *style) |
| 389 | { |
| 390 | GtkMenuShell *menu_shell = GTK_MENU_SHELL( gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ) ); |
| 391 | |
| 392 | gtk_widget_modify_style( m_widget, style ); |
| 393 | gtk_widget_modify_style( GTK_WIDGET( menu_shell ), style ); |
| 394 | |
| 395 | GList *child = menu_shell->children; |
| 396 | while (child) |
| 397 | { |
| 398 | gtk_widget_modify_style( GTK_WIDGET( child->data ), style ); |
| 399 | |
| 400 | GtkBin *bin = GTK_BIN( child->data ); |
| 401 | GtkWidget *label = (GtkWidget *) NULL; |
| 402 | if (bin->child) |
| 403 | label = bin->child; |
| 404 | if (!label) |
| 405 | label = GTK_BIN(m_widget)->child; |
| 406 | |
| 407 | gtk_widget_modify_style( label, style ); |
| 408 | |
| 409 | child = child->next; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | int wxChoice::GtkAddHelper(GtkWidget *menu, unsigned int pos, const wxString& item) |
| 414 | { |
| 415 | wxCHECK_MSG(pos<=m_clientData.GetCount(), -1, wxT("invalid index")); |
| 416 | |
| 417 | GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) ); |
| 418 | |
| 419 | if ( m_strings ) |
| 420 | { |
| 421 | // sorted control, need to insert at the correct index |
| 422 | pos = m_strings->Add(item); |
| 423 | } |
| 424 | |
| 425 | // don't call wxChoice::GetCount() from here because it doesn't work |
| 426 | // if we're called from ctor (and GtkMenuShell is still NULL) |
| 427 | if (pos == m_clientData.GetCount()) |
| 428 | gtk_menu_shell_append( GTK_MENU_SHELL(menu), menu_item ); |
| 429 | else |
| 430 | gtk_menu_shell_insert( GTK_MENU_SHELL(menu), menu_item, pos ); |
| 431 | |
| 432 | m_clientData.Insert( NULL, pos ); |
| 433 | |
| 434 | if (GTK_WIDGET_REALIZED(m_widget)) |
| 435 | { |
| 436 | gtk_widget_realize( menu_item ); |
| 437 | gtk_widget_realize( GTK_BIN(menu_item)->child ); |
| 438 | |
| 439 | ApplyWidgetStyle(); |
| 440 | } |
| 441 | |
| 442 | // The best size of a wxChoice should probably |
| 443 | // be changed everytime the control has been |
| 444 | // changed, but at least after adding an item |
| 445 | // it has to change. Adapted from Matt Ownby. |
| 446 | InvalidateBestSize(); |
| 447 | |
| 448 | g_signal_connect_after (menu_item, "activate", |
| 449 | G_CALLBACK (gtk_choice_clicked_callback), |
| 450 | this); |
| 451 | |
| 452 | gtk_widget_show( menu_item ); |
| 453 | |
| 454 | // return the index of the item in the control |
| 455 | return pos; |
| 456 | } |
| 457 | |
| 458 | wxSize wxChoice::DoGetBestSize() const |
| 459 | { |
| 460 | wxSize ret( wxControl::DoGetBestSize() ); |
| 461 | |
| 462 | // we know better our horizontal extent: it depends on the longest string |
| 463 | // we have |
| 464 | ret.x = 0; |
| 465 | if ( m_widget ) |
| 466 | { |
| 467 | int width; |
| 468 | unsigned int count = GetCount(); |
| 469 | for ( unsigned int n = 0; n < count; n++ ) |
| 470 | { |
| 471 | GetTextExtent( GetString(n), &width, NULL, NULL, NULL ); |
| 472 | if ( width > ret.x ) |
| 473 | ret.x = width; |
| 474 | } |
| 475 | |
| 476 | // add extra for the choice "=" button |
| 477 | |
| 478 | // VZ: I don't know how to get the right value, it seems to be in |
| 479 | // GtkOptionMenuProps struct from gtkoptionmenu.c but we can't get |
| 480 | // to it - maybe we can use gtk_option_menu_size_request() for this |
| 481 | // somehow? |
| 482 | // |
| 483 | // This default value works only for the default GTK+ theme (i.e. |
| 484 | // no theme at all) (FIXME) |
| 485 | static const int widthChoiceIndicator = 35; |
| 486 | ret.x += widthChoiceIndicator; |
| 487 | } |
| 488 | |
| 489 | // but not less than the minimal width |
| 490 | if ( ret.x < 80 ) |
| 491 | ret.x = 80; |
| 492 | |
| 493 | // If this request_size is called with no entries then |
| 494 | // the returned height is wrong. Give it a reasonable |
| 495 | // default value. |
| 496 | if (ret.y <= 18) |
| 497 | ret.y = 8 + GetCharHeight(); |
| 498 | |
| 499 | CacheBestSize(ret); |
| 500 | return ret; |
| 501 | } |
| 502 | |
| 503 | GdkWindow *wxChoice::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const |
| 504 | { |
| 505 | return GTK_BUTTON(m_widget)->event_window; |
| 506 | } |
| 507 | |
| 508 | // static |
| 509 | wxVisualAttributes |
| 510 | wxChoice::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) |
| 511 | { |
| 512 | return GetDefaultAttributesFromGTKWidget(gtk_option_menu_new); |
| 513 | } |
| 514 | |
| 515 | |
| 516 | #endif // wxUSE_CHOICE |