RadioButtonController.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. GWEN
  3. Copyright (c) 2010 Facepunch Studios
  4. See license in Gwen.h
  5. */
  6. #include "Gwen/Controls/RadioButtonController.h"
  7. #include "Gwen/Controls/RadioButton.h"
  8. #include "Gwen/Utility.h"
  9. using namespace Gwen;
  10. using namespace Gwen::Controls;
  11. GWEN_CONTROL_CONSTRUCTOR( RadioButtonController )
  12. {
  13. m_Selected = NULL;
  14. SetTabable( false );
  15. SetKeyboardInputEnabled( false );
  16. }
  17. void RadioButtonController::OnRadioClicked( Gwen::Controls::Base* pFromPanel )
  18. {
  19. RadioButton* pCheckedRadioButton = pFromPanel->DynamicCastRadioButton();
  20. //Iterate through all other buttons and set them to false;
  21. for (Base::List::iterator iter = Children.begin(); iter != Children.end(); ++iter)
  22. {
  23. Base* pChild = *iter;
  24. LabeledRadioButton* pLRB = pChild->DynamicCastLabeledRadioButton();
  25. if ( pLRB )
  26. {
  27. RadioButton* pChildRadioButton = pLRB->GetRadioButton();
  28. if ( pChildRadioButton == pCheckedRadioButton )
  29. {
  30. m_Selected = pLRB;
  31. }
  32. else
  33. {
  34. pLRB->GetRadioButton()->SetChecked( false );
  35. }
  36. }
  37. }
  38. OnChange();
  39. }
  40. void RadioButtonController::OnChange()
  41. {
  42. onSelectionChange.Call( this );
  43. }
  44. LabeledRadioButton* RadioButtonController::AddOption( const Gwen::String& strText, const Gwen::String& strOptionName )
  45. {
  46. return AddOption( Gwen::Utility::StringToUnicode( strText ), strOptionName );
  47. }
  48. LabeledRadioButton* RadioButtonController::AddOption( const Gwen::UnicodeString& strText, const Gwen::String& strOptionName )
  49. {
  50. LabeledRadioButton* lrb = new LabeledRadioButton( this );
  51. lrb->SetName( strOptionName );
  52. lrb->GetLabel()->SetText( strText );
  53. lrb->GetRadioButton()->onChecked.Add( this, &RadioButtonController::OnRadioClicked );
  54. lrb->Dock( Pos::Top );
  55. lrb->SetMargin( Margin( 0, 1, 0, 1 ) );
  56. lrb->SetKeyboardInputEnabled( false );
  57. lrb->SetTabable( false );
  58. Invalidate();
  59. return lrb;
  60. }