tb_window.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_WINDOW_H
  6. #define TB_WINDOW_H
  7. #include "tb_widgets_common.h"
  8. #include "tb_widgets_listener.h"
  9. namespace tb {
  10. enum WINDOW_SETTINGS {
  11. WINDOW_SETTINGS_NONE = 0, ///< Chrome less window without any other settings.
  12. WINDOW_SETTINGS_TITLEBAR = 1, ///< Show a title bar that can also move the window.
  13. WINDOW_SETTINGS_RESIZABLE = 2, ///< Show a widget for resizing the window.
  14. WINDOW_SETTINGS_CLOSE_BUTTON = 4, ///< Show a widget for closing the window.
  15. WINDOW_SETTINGS_CAN_ACTIVATE = 8, ///< Can be activated and deactivate other windows.
  16. WINDOW_SETTINGS_DEFAULT = WINDOW_SETTINGS_TITLEBAR | WINDOW_SETTINGS_RESIZABLE |
  17. WINDOW_SETTINGS_CLOSE_BUTTON | WINDOW_SETTINGS_CAN_ACTIVATE
  18. };
  19. MAKE_ENUM_FLAG_COMBO(WINDOW_SETTINGS);
  20. /** TBWindow is a TBWidget that provides some window-like features.
  21. It can have a titlebar, be movable, resizable etc.
  22. It will activate and deactivate other windows on click (which will restore
  23. focus to the last focused child widget). */
  24. class TBWindow : public TBWidget
  25. {
  26. public:
  27. // For safe typecasting
  28. TBOBJECT_SUBCLASS(TBWindow, TBWidget);
  29. TBWindow();
  30. virtual ~TBWindow();
  31. /** Close this window.
  32. Warning: This window will be deleted after this call! */
  33. void Close();
  34. /** Return true if this window is active. */
  35. bool IsActive() const;
  36. /** Activate this window if it's not already activated.
  37. This will deactivate any currently activated window.
  38. This will automatically call EnsureFocus to restore/set focus to this window. */
  39. void Activate();
  40. /** Ensure that this window has focus by attempting to find a focusable child widget.
  41. It will first try to restore focus to the last focused widget in this window,
  42. or a widget that has received SetFocus while the window was inactive.
  43. If that doesn't succeed, it will go through all children and try to set focus.
  44. Returns false if no focusable child was found. */
  45. bool EnsureFocus();
  46. /** Set the widget that should be focused when this window is activated next time.
  47. This should not be used to change focus. Call TBWidget::SetFocus to focus, which
  48. will call this method if the window is inactive! */
  49. void SetLastFocus(TBWidget *last_focus) { m_last_focus.Set(last_focus); }
  50. /** Set settings for how this window should look and behave. */
  51. void SetSettings(WINDOW_SETTINGS settings);
  52. WINDOW_SETTINGS GetSettings() { return m_settings; }
  53. /** RESIZE_FIT specifies how ResizeToFitContent should resize the window. */
  54. enum RESIZE_FIT {
  55. RESIZE_FIT_PREFERRED, ///< Fit the preferred size of all content
  56. RESIZE_FIT_MINIMAL, ///< Fit the minimal size of all content
  57. RESIZE_FIT_CURRENT_OR_NEEDED ///< Fit the minimal or maximum size only if needed. Will keep
  58. ///< the new size as close as possible to the current size.
  59. };
  60. /** Get a suitable rect for the window based on the contents and the given fit. */
  61. TBRect GetResizeToFitContentRect(RESIZE_FIT fit = RESIZE_FIT_PREFERRED);
  62. /** Resize the window to fit the its content. This is the same as doing
  63. SetRect(GetResizeToFitContentRect(fit)). */
  64. void ResizeToFitContent(RESIZE_FIT fit = RESIZE_FIT_PREFERRED);
  65. /** Set the window title. */
  66. virtual bool SetText(const char *text) { return m_textfield.SetText(text); }
  67. virtual bool GetText(TBStr &text) { return m_textfield.GetText(text); }
  68. using TBWidget::GetText; ///< Make all versions in base class available.
  69. /** Get the height of the title bar (or 0 if the WINDOW_SETTINGS say this window
  70. shouldn't have any title bar) */
  71. int GetTitleHeight();
  72. virtual TBRect GetPaddingRect();
  73. virtual PreferredSize OnCalculatePreferredSize(const SizeConstraints &constraints);
  74. virtual bool OnEvent(const TBWidgetEvent &ev);
  75. virtual void OnAdded();
  76. virtual void OnRemove();
  77. virtual void OnChildAdded(TBWidget *child);
  78. virtual void OnResized(int old_w, int old_h);
  79. // ATOMIC BEGIN
  80. /** Set along which axis the content should be layouted. */
  81. virtual AXIS GetAxis() const { return m_axis; }
  82. virtual void SetAxis(AXIS axis);
  83. // ATOMIC END
  84. protected:
  85. TBMover m_mover;
  86. TBResizer m_resizer;
  87. TBTextField m_textfield;
  88. TBWidget m_close_button;
  89. WINDOW_SETTINGS m_settings;
  90. TBWidgetSafePointer m_last_focus;
  91. // ATOMIC BEGIN
  92. AXIS m_axis;
  93. // ATOMIC END
  94. TBWindow *GetTopMostOtherWindow(bool only_activable_windows);
  95. void SetWindowActiveState(bool active);
  96. void DeActivate();
  97. };
  98. } // namespace tb
  99. #endif // TB_WINDOW_H