Menu.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. GWEN
  3. Copyright (c) 2010 Facepunch Studios
  4. See license in Gwen.h
  5. */
  6. #include "Gwen/Gwen.h"
  7. #include "Gwen/Controls/Menu.h"
  8. #include "Gwen/Skin.h"
  9. #include "Gwen/Utility.h"
  10. using namespace Gwen;
  11. using namespace Gwen::Controls;
  12. GWEN_CONTROL_CONSTRUCTOR( Menu )
  13. {
  14. SetBounds( 0, 0, 10, 10 );
  15. SetPadding( Padding( 2, 2, 2, 2 ) );
  16. SetDisableIconMargin( false );
  17. SetAutoHideBars( true );
  18. SetScroll( false, true );
  19. }
  20. void Menu::Render( Skin::Base* skin )
  21. {
  22. skin->DrawMenu( this, IconMarginDisabled() );
  23. }
  24. void Menu::RenderUnder( Skin::Base* skin )
  25. {
  26. BaseClass::RenderUnder( skin );
  27. skin->DrawShadow( this );
  28. }
  29. void Menu::Layout( Skin::Base* skin )
  30. {
  31. int childrenHeight = 0;
  32. for ( Base::List::iterator it = m_InnerPanel->Children.begin(); it != m_InnerPanel->Children.end(); ++it )
  33. {
  34. Base* pChild = (*it);
  35. if ( !pChild )
  36. continue;
  37. childrenHeight += pChild->Height();
  38. }
  39. if ( Y() + childrenHeight > GetCanvas()->Height() )
  40. childrenHeight = GetCanvas()->Height() - Y();
  41. SetSize( Width(), childrenHeight );
  42. BaseClass::Layout( skin );
  43. }
  44. MenuItem* Menu::AddItem( const Gwen::UnicodeString& strName, const UnicodeString& strIconName, Gwen::Event::Handler* pHandler, Gwen::Event::Handler::Function fn )
  45. {
  46. MenuItem* pItem = new MenuItem( this );
  47. pItem->SetText( strName );
  48. pItem->SetImage( strIconName );
  49. if ( fn && pHandler )
  50. {
  51. pItem->onMenuItemSelected.Add( pHandler, fn );
  52. }
  53. OnAddItem( pItem );
  54. return pItem;
  55. }
  56. void Menu::OnAddItem( MenuItem* item )
  57. {
  58. item->Dock( Pos::Top );
  59. item->SetTextPadding( Padding( IconMarginDisabled() ? 0 : 24, 0, 16, 0 ) );
  60. item->SetPadding( Padding( 4, 4, 4, 4 ) );
  61. item->SizeToContents();
  62. item->SetAlignment( Pos::CenterV | Pos::Left );
  63. item->onHoverEnter.Add( this, &Menu::OnHoverItem );
  64. // Do this here - after Top Docking these values mean nothing in layout
  65. int w = item->Width() + 10 + 32;
  66. if ( w < Width() ) w = Width();
  67. SetSize( w, Height() );
  68. }
  69. void Menu::ClearItems()
  70. {
  71. for ( Base::List::iterator it = m_InnerPanel->Children.begin(); it != m_InnerPanel->Children.end(); ++it )
  72. {
  73. Base* pChild = *it;
  74. if ( !pChild ) continue;
  75. pChild->DelayedDelete();
  76. }
  77. }
  78. MenuItem* Menu::AddItem( const Gwen::String& strName, const String& strIconName, Gwen::Event::Handler* pHandler, Gwen::Event::Handler::Function fn )
  79. {
  80. return AddItem( Gwen::Utility::StringToUnicode( strName ), Gwen::Utility::StringToUnicode( strIconName ), pHandler, fn );
  81. }
  82. void Menu::CloseAll()
  83. {
  84. for ( Base::List::iterator it = m_InnerPanel->Children.begin(); it != m_InnerPanel->Children.end(); ++it )
  85. {
  86. Base* pChild = *it;
  87. MenuItem* pItem = pChild->DynamicCastMenuItem();
  88. if ( !pItem ) continue;
  89. pItem->CloseMenu();
  90. }
  91. }
  92. bool Menu::IsMenuOpen()
  93. {
  94. for ( Base::List::iterator it = m_InnerPanel->Children.begin(); it != m_InnerPanel->Children.end(); ++it )
  95. {
  96. Base* pChild = *it;
  97. MenuItem* pItem = pChild->DynamicCastMenuItem();
  98. if ( !pItem ) continue;
  99. if ( pItem->IsMenuOpen() )
  100. return true;
  101. }
  102. return false;
  103. }
  104. void Menu::OnHoverItem( Gwen::Controls::Base* pControl )
  105. {
  106. if ( !ShouldHoverOpenMenu() ) return;
  107. MenuItem* pItem = pControl->DynamicCastMenuItem();
  108. if (!pItem) return;
  109. if ( pItem->IsMenuOpen() ) return;
  110. CloseAll();
  111. pItem->OpenMenu();
  112. }
  113. void Menu::Close()
  114. {
  115. SetHidden( true );
  116. }
  117. void Menu::CloseMenus()
  118. {
  119. BaseClass::CloseMenus();
  120. CloseAll();
  121. Close();
  122. }
  123. void Menu::AddDivider()
  124. {
  125. MenuDivider* divider = new MenuDivider( this );
  126. divider->Dock( Pos::Top );
  127. divider->SetMargin( Margin( IconMarginDisabled() ? 0 : 24, 0, 4, 0 ) );
  128. }
  129. void MenuDivider::Render( Gwen::Skin::Base* skin )
  130. {
  131. skin->DrawMenuDivider( this );
  132. }