| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: radiobox.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 "radiobox.h" |
| 12 | #endif |
| 13 | |
| 14 | #include "wx/radiobox.h" |
| 15 | |
| 16 | #if wxUSE_RADIOBOX |
| 17 | |
| 18 | #include "wx/dialog.h" |
| 19 | #include "wx/frame.h" |
| 20 | |
| 21 | #include "gdk/gdk.h" |
| 22 | #include "gtk/gtk.h" |
| 23 | #include "wx/gtk/win_gtk.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 | // "clicked" |
| 40 | //----------------------------------------------------------------------------- |
| 41 | |
| 42 | static void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioBox *rb ) |
| 43 | { |
| 44 | if (g_isIdle) wxapp_install_idle_handler(); |
| 45 | |
| 46 | if (!rb->m_hasVMT) return; |
| 47 | if (g_blockEventsOnDrag) return; |
| 48 | |
| 49 | if (rb->m_alreadySent) |
| 50 | { |
| 51 | rb->m_alreadySent = FALSE; |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | rb->m_alreadySent = TRUE; |
| 56 | |
| 57 | wxCommandEvent event( wxEVT_COMMAND_RADIOBOX_SELECTED, rb->GetId() ); |
| 58 | event.SetInt( rb->GetSelection() ); |
| 59 | event.SetString( rb->GetStringSelection() ); |
| 60 | event.SetEventObject( rb ); |
| 61 | rb->GetEventHandler()->ProcessEvent(event); |
| 62 | } |
| 63 | |
| 64 | //----------------------------------------------------------------------------- |
| 65 | // wxRadioBox |
| 66 | //----------------------------------------------------------------------------- |
| 67 | |
| 68 | IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl) |
| 69 | |
| 70 | wxRadioBox::wxRadioBox() |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title, |
| 75 | const wxPoint &pos, const wxSize &size, |
| 76 | int n, const wxString choices[], int majorDim, |
| 77 | long style, const wxValidator& validator, |
| 78 | const wxString &name ) |
| 79 | { |
| 80 | m_alreadySent = FALSE; |
| 81 | m_needParent = TRUE; |
| 82 | m_acceptsFocus = TRUE; |
| 83 | |
| 84 | if (!PreCreation( parent, pos, size ) || |
| 85 | !CreateBase( parent, id, pos, size, style, validator, name )) |
| 86 | { |
| 87 | wxFAIL_MSG( wxT("wxRadioBox creation failed") ); |
| 88 | return FALSE; |
| 89 | } |
| 90 | |
| 91 | m_widget = gtk_frame_new( title.mbc_str() ); |
| 92 | |
| 93 | m_majorDim = majorDim; |
| 94 | |
| 95 | GtkRadioButton *m_radio = (GtkRadioButton*) NULL; |
| 96 | |
| 97 | wxString label; |
| 98 | GSList *radio_button_group = (GSList *) NULL; |
| 99 | for (int i = 0; i < n; i++) |
| 100 | { |
| 101 | if ( i != 0 ) |
| 102 | radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) ); |
| 103 | |
| 104 | label.Empty(); |
| 105 | for ( const wxChar *pc = choices[i]; *pc; pc++ ) |
| 106 | { |
| 107 | if ( *pc != wxT('&') ) |
| 108 | label += *pc; |
| 109 | } |
| 110 | |
| 111 | m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, label.mbc_str() ) ); |
| 112 | |
| 113 | m_boxes.Append( (wxObject*) m_radio ); |
| 114 | |
| 115 | ConnectWidget( GTK_WIDGET(m_radio) ); |
| 116 | |
| 117 | if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE ); |
| 118 | |
| 119 | gtk_signal_connect( GTK_OBJECT(m_radio), "clicked", |
| 120 | GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this ); |
| 121 | |
| 122 | gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow), |
| 123 | GTK_WIDGET(m_radio), |
| 124 | m_x+10, m_y+10+(i*24), 10, 10 ); |
| 125 | } |
| 126 | |
| 127 | wxSize ls = LayoutItems(); |
| 128 | |
| 129 | wxSize newSize = size; |
| 130 | if (newSize.x == -1) newSize.x = ls.x; |
| 131 | if (newSize.y == -1) newSize.y = ls.y; |
| 132 | SetSize( newSize.x, newSize.y ); |
| 133 | |
| 134 | m_parent->DoAddChild( this ); |
| 135 | |
| 136 | PostCreation(); |
| 137 | |
| 138 | SetLabel( title ); |
| 139 | |
| 140 | SetBackgroundColour( parent->GetBackgroundColour() ); |
| 141 | SetForegroundColour( parent->GetForegroundColour() ); |
| 142 | SetFont( parent->GetFont() ); |
| 143 | |
| 144 | Show( TRUE ); |
| 145 | |
| 146 | return TRUE; |
| 147 | } |
| 148 | |
| 149 | wxRadioBox::~wxRadioBox() |
| 150 | { |
| 151 | wxNode *node = m_boxes.First(); |
| 152 | while (node) |
| 153 | { |
| 154 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
| 155 | gtk_widget_destroy( button ); |
| 156 | node = node->Next(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | void wxRadioBox::DoSetSize( int x, int y, int width, int height, int sizeFlags ) |
| 161 | { |
| 162 | wxWindow::DoSetSize( x, y, width, height, sizeFlags ); |
| 163 | |
| 164 | LayoutItems(); |
| 165 | } |
| 166 | |
| 167 | wxSize wxRadioBox::LayoutItems() |
| 168 | { |
| 169 | int x = 7; |
| 170 | int y = 15; |
| 171 | |
| 172 | if ( m_majorDim == 0 ) |
| 173 | { |
| 174 | // avoid dividing by 0 below |
| 175 | wxFAIL_MSG( wxT("dimension of radiobox should not be 0!") ); |
| 176 | |
| 177 | m_majorDim = 1; |
| 178 | } |
| 179 | |
| 180 | int num_per_major = (m_boxes.GetCount() - 1) / m_majorDim +1; |
| 181 | |
| 182 | wxSize res( 0, 0 ); |
| 183 | |
| 184 | int num_of_cols = 0; |
| 185 | int num_of_rows = 0; |
| 186 | if (HasFlag(wxRA_SPECIFY_COLS)) |
| 187 | { |
| 188 | num_of_cols = m_majorDim; |
| 189 | num_of_rows = num_per_major; |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | num_of_cols = num_per_major; |
| 194 | num_of_rows = m_majorDim; |
| 195 | } |
| 196 | |
| 197 | if ( HasFlag(wxRA_SPECIFY_COLS) || |
| 198 | (HasFlag(wxRA_SPECIFY_ROWS) && (num_of_cols > 1)) ) |
| 199 | { |
| 200 | for (int j = 0; j < num_of_cols; j++) |
| 201 | { |
| 202 | y = 15; |
| 203 | |
| 204 | int max_len = 0; |
| 205 | wxNode *node = m_boxes.Nth( j*num_of_rows ); |
| 206 | for (int i1 = 0; i1< num_of_rows; i1++) |
| 207 | { |
| 208 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
| 209 | GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child ); |
| 210 | GdkFont *font = m_widget->style->font; |
| 211 | int len = 22+gdk_string_measure( font, label->label ); |
| 212 | if (len > max_len) max_len = len; |
| 213 | |
| 214 | gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, m_x+x, m_y+y ); |
| 215 | y += 22; |
| 216 | |
| 217 | node = node->Next(); |
| 218 | if (!node) break; |
| 219 | } |
| 220 | |
| 221 | // we don't know the max_len before |
| 222 | |
| 223 | node = m_boxes.Nth( j*num_of_rows ); |
| 224 | for (int i2 = 0; i2< num_of_rows; i2++) |
| 225 | { |
| 226 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
| 227 | |
| 228 | gtk_myfixed_resize( GTK_MYFIXED(m_parent->m_wxwindow), button, max_len, 20 ); |
| 229 | |
| 230 | node = node->Next(); |
| 231 | if (!node) break; |
| 232 | } |
| 233 | |
| 234 | if (y > res.y) res.y = y; |
| 235 | |
| 236 | x += max_len + 2; |
| 237 | } |
| 238 | |
| 239 | res.x = x+4; |
| 240 | res.y += 9; |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | int max = 0; |
| 245 | |
| 246 | wxNode *node = m_boxes.First(); |
| 247 | while (node) |
| 248 | { |
| 249 | GtkButton *button = GTK_BUTTON( node->Data() ); |
| 250 | GtkLabel *label = GTK_LABEL( button->child ); |
| 251 | |
| 252 | GdkFont *font = m_widget->style->font; |
| 253 | int len = 22+gdk_string_measure( font, label->label ); |
| 254 | if (len > max) max = len; |
| 255 | |
| 256 | node = node->Next(); |
| 257 | } |
| 258 | |
| 259 | node = m_boxes.First(); |
| 260 | while (node) |
| 261 | { |
| 262 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
| 263 | |
| 264 | gtk_myfixed_set_size( GTK_MYFIXED(m_parent->m_wxwindow), button, m_x+x, m_y+y, max, 20 ); |
| 265 | x += max; |
| 266 | |
| 267 | node = node->Next(); |
| 268 | } |
| 269 | res.x = x+4; |
| 270 | res.y = 40; |
| 271 | } |
| 272 | |
| 273 | return res; |
| 274 | } |
| 275 | |
| 276 | bool wxRadioBox::Show( bool show ) |
| 277 | { |
| 278 | wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobox") ); |
| 279 | |
| 280 | wxWindow::Show( show ); |
| 281 | |
| 282 | if ((m_windowStyle & wxNO_BORDER) != 0) |
| 283 | gtk_widget_hide( m_widget ); |
| 284 | |
| 285 | wxNode *node = m_boxes.First(); |
| 286 | while (node) |
| 287 | { |
| 288 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
| 289 | |
| 290 | if (show) gtk_widget_show( button ); else gtk_widget_hide( button ); |
| 291 | |
| 292 | node = node->Next(); |
| 293 | } |
| 294 | |
| 295 | return TRUE; |
| 296 | } |
| 297 | |
| 298 | int wxRadioBox::FindString( const wxString &s ) const |
| 299 | { |
| 300 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid radiobox") ); |
| 301 | |
| 302 | int count = 0; |
| 303 | |
| 304 | wxNode *node = m_boxes.First(); |
| 305 | while (node) |
| 306 | { |
| 307 | GtkButton *button = GTK_BUTTON( node->Data() ); |
| 308 | |
| 309 | GtkLabel *label = GTK_LABEL( button->child ); |
| 310 | if (s == label->label) return count; |
| 311 | count++; |
| 312 | |
| 313 | node = node->Next(); |
| 314 | } |
| 315 | |
| 316 | return -1; |
| 317 | } |
| 318 | |
| 319 | void wxRadioBox::SetFocus() |
| 320 | { |
| 321 | wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") ); |
| 322 | |
| 323 | if (m_boxes.GetCount() == 0) return; |
| 324 | |
| 325 | wxNode *node = m_boxes.First(); |
| 326 | while (node) |
| 327 | { |
| 328 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); |
| 329 | if (button->active) |
| 330 | { |
| 331 | gtk_widget_grab_focus( GTK_WIDGET(button) ); |
| 332 | |
| 333 | return; |
| 334 | } |
| 335 | node = node->Next(); |
| 336 | } |
| 337 | |
| 338 | } |
| 339 | |
| 340 | void wxRadioBox::SetSelection( int n ) |
| 341 | { |
| 342 | wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") ); |
| 343 | |
| 344 | wxNode *node = m_boxes.Nth( n ); |
| 345 | |
| 346 | wxCHECK_RET( node, wxT("radiobox wrong index") ); |
| 347 | |
| 348 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); |
| 349 | |
| 350 | DisableEvents(); |
| 351 | |
| 352 | gtk_toggle_button_set_state( button, 1 ); |
| 353 | |
| 354 | EnableEvents(); |
| 355 | } |
| 356 | |
| 357 | int wxRadioBox::GetSelection(void) const |
| 358 | { |
| 359 | wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid radiobox") ); |
| 360 | |
| 361 | int count = 0; |
| 362 | |
| 363 | wxNode *node = m_boxes.First(); |
| 364 | while (node) |
| 365 | { |
| 366 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); |
| 367 | if (button->active) return count; |
| 368 | count++; |
| 369 | node = node->Next(); |
| 370 | } |
| 371 | |
| 372 | wxFAIL_MSG( wxT("wxRadioBox none selected") ); |
| 373 | |
| 374 | return -1; |
| 375 | } |
| 376 | |
| 377 | wxString wxRadioBox::GetString( int n ) const |
| 378 | { |
| 379 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid radiobox") ); |
| 380 | |
| 381 | wxNode *node = m_boxes.Nth( n ); |
| 382 | |
| 383 | wxCHECK_MSG( node, wxT(""), wxT("radiobox wrong index") ); |
| 384 | |
| 385 | GtkButton *button = GTK_BUTTON( node->Data() ); |
| 386 | GtkLabel *label = GTK_LABEL( button->child ); |
| 387 | |
| 388 | return wxString( label->label ); |
| 389 | } |
| 390 | |
| 391 | wxString wxRadioBox::GetLabel( int item ) const |
| 392 | { |
| 393 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid radiobox") ); |
| 394 | |
| 395 | return GetString( item ); |
| 396 | } |
| 397 | |
| 398 | void wxRadioBox::SetLabel( const wxString& label ) |
| 399 | { |
| 400 | wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") ); |
| 401 | |
| 402 | wxControl::SetLabel( label ); |
| 403 | |
| 404 | gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel().mbc_str() ); |
| 405 | } |
| 406 | |
| 407 | void wxRadioBox::SetLabel( int item, const wxString& label ) |
| 408 | { |
| 409 | wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") ); |
| 410 | |
| 411 | wxNode *node = m_boxes.Nth( item ); |
| 412 | |
| 413 | wxCHECK_RET( node, wxT("radiobox wrong index") ); |
| 414 | |
| 415 | GtkButton *button = GTK_BUTTON( node->Data() ); |
| 416 | GtkLabel *g_label = GTK_LABEL( button->child ); |
| 417 | |
| 418 | gtk_label_set( g_label, label.mbc_str() ); |
| 419 | } |
| 420 | |
| 421 | void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) ) |
| 422 | { |
| 423 | wxFAIL_MSG(wxT("wxRadioBox::SetLabel not implemented.")); |
| 424 | } |
| 425 | |
| 426 | bool wxRadioBox::Enable( bool enable ) |
| 427 | { |
| 428 | if ( !wxControl::Enable( enable ) ) |
| 429 | return FALSE; |
| 430 | |
| 431 | wxNode *node = m_boxes.First(); |
| 432 | while (node) |
| 433 | { |
| 434 | GtkButton *button = GTK_BUTTON( node->Data() ); |
| 435 | GtkWidget *label = button->child; |
| 436 | gtk_widget_set_sensitive( GTK_WIDGET(button), enable ); |
| 437 | gtk_widget_set_sensitive( label, enable ); |
| 438 | node = node->Next(); |
| 439 | } |
| 440 | |
| 441 | return TRUE; |
| 442 | } |
| 443 | |
| 444 | void wxRadioBox::Enable( int item, bool enable ) |
| 445 | { |
| 446 | wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") ); |
| 447 | |
| 448 | wxNode *node = m_boxes.Nth( item ); |
| 449 | |
| 450 | wxCHECK_RET( node, wxT("radiobox wrong index") ); |
| 451 | |
| 452 | GtkButton *button = GTK_BUTTON( node->Data() ); |
| 453 | GtkWidget *label = button->child; |
| 454 | gtk_widget_set_sensitive( GTK_WIDGET(button), enable ); |
| 455 | gtk_widget_set_sensitive( label, enable ); |
| 456 | } |
| 457 | |
| 458 | void wxRadioBox::Show( int item, bool show ) |
| 459 | { |
| 460 | wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") ); |
| 461 | |
| 462 | wxNode *node = m_boxes.Nth( item ); |
| 463 | |
| 464 | wxCHECK_RET( node, wxT("radiobox wrong index") ); |
| 465 | |
| 466 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
| 467 | |
| 468 | if (show) |
| 469 | gtk_widget_show( button ); |
| 470 | else |
| 471 | gtk_widget_hide( button ); |
| 472 | } |
| 473 | |
| 474 | wxString wxRadioBox::GetStringSelection() const |
| 475 | { |
| 476 | wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid radiobox") ); |
| 477 | |
| 478 | wxNode *node = m_boxes.First(); |
| 479 | while (node) |
| 480 | { |
| 481 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() ); |
| 482 | if (button->active) |
| 483 | { |
| 484 | GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child ); |
| 485 | return label->label; |
| 486 | } |
| 487 | node = node->Next(); |
| 488 | } |
| 489 | |
| 490 | wxFAIL_MSG( wxT("wxRadioBox none selected") ); |
| 491 | return wxT(""); |
| 492 | } |
| 493 | |
| 494 | bool wxRadioBox::SetStringSelection( const wxString &s ) |
| 495 | { |
| 496 | wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobox") ); |
| 497 | |
| 498 | int res = FindString( s ); |
| 499 | if (res == -1) return FALSE; |
| 500 | SetSelection( res ); |
| 501 | |
| 502 | return TRUE; |
| 503 | } |
| 504 | |
| 505 | int wxRadioBox::Number() const |
| 506 | { |
| 507 | return m_boxes.Number(); |
| 508 | } |
| 509 | |
| 510 | int wxRadioBox::GetNumberOfRowsOrCols() const |
| 511 | { |
| 512 | return 1; |
| 513 | } |
| 514 | |
| 515 | void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) ) |
| 516 | { |
| 517 | wxFAIL_MSG(wxT("wxRadioBox::SetNumberOfRowsOrCols not implemented.")); |
| 518 | } |
| 519 | |
| 520 | void wxRadioBox::DisableEvents() |
| 521 | { |
| 522 | wxNode *node = m_boxes.First(); |
| 523 | while (node) |
| 524 | { |
| 525 | gtk_signal_disconnect_by_func( GTK_OBJECT(node->Data()), |
| 526 | GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this ); |
| 527 | |
| 528 | node = node->Next(); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | void wxRadioBox::EnableEvents() |
| 533 | { |
| 534 | wxNode *node = m_boxes.First(); |
| 535 | while (node) |
| 536 | { |
| 537 | gtk_signal_connect( GTK_OBJECT(node->Data()), "clicked", |
| 538 | GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this ); |
| 539 | |
| 540 | node = node->Next(); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | void wxRadioBox::ApplyWidgetStyle() |
| 545 | { |
| 546 | SetWidgetStyle(); |
| 547 | |
| 548 | gtk_widget_set_style( m_widget, m_widgetStyle ); |
| 549 | |
| 550 | wxNode *node = m_boxes.First(); |
| 551 | while (node) |
| 552 | { |
| 553 | GtkWidget *widget = GTK_WIDGET( node->Data() ); |
| 554 | gtk_widget_set_style( widget, m_widgetStyle ); |
| 555 | |
| 556 | GtkButton *button = GTK_BUTTON( node->Data() ); |
| 557 | gtk_widget_set_style( button->child, m_widgetStyle ); |
| 558 | |
| 559 | node = node->Next(); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window ) |
| 564 | { |
| 565 | if (window == m_widget->window) return TRUE; |
| 566 | |
| 567 | wxNode *node = m_boxes.First(); |
| 568 | while (node) |
| 569 | { |
| 570 | GtkWidget *button = GTK_WIDGET( node->Data() ); |
| 571 | |
| 572 | if (window == button->window) return TRUE; |
| 573 | |
| 574 | node = node->Next(); |
| 575 | } |
| 576 | |
| 577 | return FALSE; |
| 578 | } |
| 579 | |
| 580 | #endif |