tb_menu_window.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #include "tb_menu_window.h"
  6. #include "tb_widgets_listener.h"
  7. namespace tb {
  8. // == TBMenuWindow ==========================================
  9. TBMenuWindow::TBMenuWindow(TBWidget *target, TBID id)
  10. : TBPopupWindow(target)
  11. {
  12. SetID(id);
  13. SetSkinBg(TBIDC("TBMenuWindow"), WIDGET_INVOKE_INFO_NO_CALLBACKS);
  14. m_select_list.GetScrollContainer()->SetAdaptToContentSize(true);
  15. m_select_list.SetIsFocusable(false); ///< Avoid it autoclosing its window on click
  16. m_select_list.SetSkinBg("");
  17. m_select_list.SetRect(GetPaddingRect());
  18. m_select_list.SetGravity(WIDGET_GRAVITY_ALL);
  19. AddChild(&m_select_list);
  20. }
  21. TBMenuWindow::~TBMenuWindow()
  22. {
  23. RemoveChild(&m_select_list);
  24. }
  25. bool TBMenuWindow::Show(TBSelectItemSource *source, const TBPopupAlignment &alignment, int initial_value)
  26. {
  27. m_select_list.SetValue(initial_value);
  28. m_select_list.SetSource(source);
  29. m_select_list.ValidateList();
  30. return TBPopupWindow::Show(alignment);
  31. }
  32. bool TBMenuWindow::OnEvent(const TBWidgetEvent &ev)
  33. {
  34. if (ev.type == EVENT_TYPE_CLICK && &m_select_list == ev.target)
  35. {
  36. TBWidgetSafePointer this_widget(this);
  37. // Invoke the click on the target
  38. TBWidgetEvent target_ev(EVENT_TYPE_CLICK);
  39. target_ev.ref_id = ev.ref_id;
  40. InvokeEvent(target_ev);
  41. // If target got deleted, close
  42. if (this_widget.Get())
  43. Close();
  44. return true;
  45. }
  46. return TBPopupWindow::OnEvent(ev);
  47. }
  48. }; // namespace tb