ComboBox.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. GWEN
  3. Copyright (c) 2010 Facepunch Studios
  4. See license in Gwen.h
  5. */
  6. #include "Gwen/Controls/ComboBox.h"
  7. #include "Gwen/Controls/Menu.h"
  8. using namespace Gwen;
  9. using namespace Gwen::Controls;
  10. using namespace Gwen::ControlsInternal;
  11. class GWEN_EXPORT DownArrow : public Controls::Base
  12. {
  13. public:
  14. GWEN_CONTROL_INLINE( DownArrow, Controls::Base )
  15. {
  16. SetMouseInputEnabled( true );
  17. SetSize( 15, 15 );
  18. }
  19. void Render( Skin::Base* skin )
  20. {
  21. skin->DrawArrowDown(this->m_Bounds);
  22. }
  23. void SetComboBox( ComboBox* p ){ m_ComboBox = p; }
  24. protected:
  25. ComboBox* m_ComboBox;
  26. };
  27. GWEN_CONTROL_CONSTRUCTOR( ComboBox )
  28. {
  29. SetSize( 100, 20 );
  30. SetMouseInputEnabled( true );
  31. m_Menu = new Menu( this );
  32. m_Menu->SetHidden( true );
  33. m_Menu->SetDisableIconMargin( true );
  34. m_Menu->SetTabable( false );
  35. ComboBoxButton* m_OpenButton = new ComboBoxButton( this );
  36. m_OpenButton->onDown.Add( this, &ComboBox::OpenButtonPressed );
  37. m_OpenButton->Dock( Pos::Right );
  38. m_OpenButton->SetMargin( Margin( 2, 2, 2, 2 ) );
  39. m_OpenButton->SetWidth( 16 );
  40. m_OpenButton->SetTabable( false );
  41. m_SelectedItem = NULL;
  42. SetAlignment( Gwen::Pos::Left | Gwen::Pos::CenterV );
  43. SetText( L"" );
  44. SetMargin( Margin( 3, 0, 0, 0 ) );
  45. SetTabable( true );
  46. }
  47. MenuItem* ComboBox::AddItem( const UnicodeString& strLabel, const String& strName, Gwen::Event::Handler* pHandler, Gwen::Event::Handler::Function fn )
  48. {
  49. MenuItem* pItem = m_Menu->AddItem( strLabel, L"", pHandler, fn );
  50. pItem->SetName( strName );
  51. pItem->onMenuItemSelected.Add( this, &ComboBox::OnItemSelected );
  52. //Default
  53. if ( m_SelectedItem == NULL )
  54. OnItemSelected( pItem );
  55. return pItem;
  56. }
  57. void ComboBox::Render( Skin::Base* skin )
  58. {
  59. skin->DrawComboBox( this );
  60. }
  61. void ComboBox::OpenButtonPressed( Controls::Base* /*pControl*/ )
  62. {
  63. OnPress();
  64. }
  65. void ComboBox::OnPress()
  66. {
  67. bool bWasMenuHidden = m_Menu->Hidden();
  68. GetCanvas()->CloseMenus();
  69. if ( bWasMenuHidden )
  70. {
  71. OpenList();
  72. }
  73. else
  74. {
  75. m_Menu->SetHidden( true );
  76. }
  77. }
  78. void ComboBox::ClearItems()
  79. {
  80. if ( m_Menu )
  81. {
  82. m_Menu->ClearItems();
  83. }
  84. }
  85. void ComboBox::OnItemSelected( Controls::Base* pControl )
  86. {
  87. //Convert selected to a menu item
  88. MenuItem* pItem = pControl->DynamicCastMenuItem();
  89. if ( !pItem ) return;
  90. m_SelectedItem = pItem;
  91. SetText( m_SelectedItem->GetText() );
  92. m_Menu->SetHidden( true );
  93. onSelection.Call( this );
  94. Focus();
  95. Invalidate();
  96. }
  97. void ComboBox::OnLostKeyboardFocus()
  98. {
  99. SetTextColor( Color( 0, 0, 0, 255 ) );
  100. }
  101. void ComboBox::OnKeyboardFocus()
  102. {
  103. //Until we add the blue highlighting again
  104. SetTextColor( Color( 0, 0, 0, 255 ) );
  105. //m_SelectedText->SetTextColor( Color( 255, 255, 255, 255 ) );
  106. }
  107. Gwen::Controls::Label* ComboBox::GetSelectedItem()
  108. {
  109. return m_SelectedItem;
  110. }
  111. bool ComboBox::IsMenuOpen()
  112. {
  113. return m_Menu && !m_Menu->Hidden();
  114. }
  115. void ComboBox::OpenList()
  116. {
  117. if ( !m_Menu ) return;
  118. m_Menu->SetParent( GetCanvas() );
  119. m_Menu->SetHidden( false );
  120. m_Menu->BringToFront();
  121. Gwen::Point p = LocalPosToCanvas( Gwen::Point( 0, 0 ) );
  122. m_Menu->SetBounds( Gwen::Rect ( p.x, p.y + Height(), Width(), m_Menu->Height()) );
  123. }
  124. void ComboBox::CloseList()
  125. {
  126. }
  127. bool ComboBox::OnKeyUp( bool bDown )
  128. {
  129. if ( bDown )
  130. {
  131. Base::List & children = m_Menu->GetChildren();
  132. Base::List::reverse_iterator it = std::find( children.rbegin(), children.rend(), m_SelectedItem );
  133. if ( it != children.rend() && ( ++it != children.rend() ) )
  134. {
  135. Base* pUpElement = *it;
  136. OnItemSelected( pUpElement );
  137. }
  138. }
  139. return true;
  140. }
  141. bool ComboBox::OnKeyDown( bool bDown )
  142. {
  143. if ( bDown )
  144. {
  145. Base::List & children = m_Menu->GetChildren();
  146. Base::List::iterator it = std::find( children.begin(), children.end(), m_SelectedItem );
  147. if ( it != children.end() && ( ++it != children.end() ) )
  148. {
  149. Base* pDownElement = *it;
  150. OnItemSelected( pDownElement );
  151. }
  152. }
  153. return true;
  154. }
  155. void ComboBox::RenderFocus( Gwen::Skin::Base* /*skin*/ )
  156. {
  157. }