tb_popup_window.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #ifndef TB_POPUP_WINDOW_H
  6. #define TB_POPUP_WINDOW_H
  7. #include "tb_window.h"
  8. #include "tb_widgets_listener.h"
  9. namespace tb {
  10. /** TBPopupAlignment describes the preferred alignment of a popup
  11. relative to a target widget or a given point.
  12. It calculates the rect to be used to match these preferences
  13. for any given popup and target. */
  14. class TBPopupAlignment
  15. {
  16. public:
  17. static const int UNSPECIFIED = TB_INVALID_DIMENSION;
  18. /** Align relative to the target widget. */
  19. TBPopupAlignment(TB_ALIGN align = TB_ALIGN_BOTTOM)
  20. : pos_in_root(UNSPECIFIED, UNSPECIFIED)
  21. , align(align)
  22. , expand_to_target_width(true) {}
  23. /** Align relative to the given position (coordinates relative to the root widget). */
  24. TBPopupAlignment(const TBPoint &pos_in_root, TB_ALIGN align = TB_ALIGN_BOTTOM)
  25. : pos_in_root(pos_in_root)
  26. , align(align)
  27. , expand_to_target_width(true) {}
  28. /** Align relative to the given position (coordinates relative to the root widget).
  29. Applies an additional offset. */
  30. TBPopupAlignment(const TBPoint &pos_in_root, const TBPoint &pos_offset)
  31. : pos_in_root(pos_in_root)
  32. , pos_offset(pos_offset)
  33. , align(TB_ALIGN_BOTTOM)
  34. , expand_to_target_width(true) {}
  35. /** Calculate a good rect for the given popup window using its preferred size and
  36. the preferred alignment information stored in this class. */
  37. TBRect GetAlignedRect(TBWidget *popup, TBWidget *target) const;
  38. TBPoint pos_in_root;
  39. TBPoint pos_offset;
  40. TB_ALIGN align;
  41. /** If true, the width of the popup will be at least the same as the target widget
  42. if the alignment is TB_ALIGN_TOP or TB_ALIGN_BOTTOM. */
  43. bool expand_to_target_width;
  44. };
  45. /** TBPopupWindow is a popup window that redirects any child widgets events
  46. through the given target. It will automatically close on click events that
  47. are not sent through this popup. */
  48. class TBPopupWindow : public TBWindow, private TBWidgetListener
  49. {
  50. public:
  51. // For safe typecasting
  52. TBOBJECT_SUBCLASS(TBPopupWindow, TBWindow);
  53. TBPopupWindow(TBWidget *target);
  54. ~TBPopupWindow();
  55. bool Show(const TBPopupAlignment &alignment);
  56. virtual TBWidget *GetEventDestination() { return m_target.Get(); }
  57. virtual bool OnEvent(const TBWidgetEvent &ev);
  58. private:
  59. TBWidgetSafePointer m_target;
  60. // TBWidgetListener
  61. virtual void OnWidgetFocusChanged(TBWidget *widget, bool focused);
  62. virtual bool OnWidgetInvokeEvent(TBWidget *widget, const TBWidgetEvent &ev);
  63. virtual void OnWidgetDelete(TBWidget *widget);
  64. virtual bool OnWidgetDying(TBWidget *widget);
  65. };
  66. }; // namespace tb
  67. #endif // TB_POPUP_WINDOW_H