Toolbar.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/String.h>
  7. #include <AK/StringBuilder.h>
  8. #include <LibCore/EventLoop.h>
  9. #include <LibGUI/Action.h>
  10. #include <LibGUI/ActionGroup.h>
  11. #include <LibGUI/Application.h>
  12. #include <LibGUI/BoxLayout.h>
  13. #include <LibGUI/Button.h>
  14. #include <LibGUI/Painter.h>
  15. #include <LibGUI/SeparatorWidget.h>
  16. #include <LibGUI/Toolbar.h>
  17. #include <LibGfx/Palette.h>
  18. REGISTER_WIDGET(GUI, Toolbar)
  19. namespace GUI {
  20. Toolbar::Toolbar(Orientation orientation, int button_size)
  21. : m_orientation(orientation)
  22. , m_button_size(button_size)
  23. {
  24. if (m_orientation == Orientation::Horizontal) {
  25. set_fixed_height(button_size + 8);
  26. } else {
  27. set_fixed_width(button_size + 8);
  28. }
  29. set_layout<BoxLayout>(orientation);
  30. layout()->set_spacing(0);
  31. layout()->set_margins({ 2, 2, 2, 2 });
  32. }
  33. Toolbar::~Toolbar()
  34. {
  35. }
  36. class ToolbarButton final : public Button {
  37. C_OBJECT(ToolbarButton);
  38. public:
  39. virtual ~ToolbarButton() override { }
  40. private:
  41. explicit ToolbarButton(Action& action)
  42. {
  43. if (action.group() && action.group()->is_exclusive())
  44. set_exclusive(true);
  45. set_action(action);
  46. set_tooltip(tooltip(action));
  47. set_focus_policy(FocusPolicy::NoFocus);
  48. if (action.icon())
  49. set_icon(action.icon());
  50. else
  51. set_text(action.text());
  52. set_button_style(Gfx::ButtonStyle::Coolbar);
  53. }
  54. String tooltip(const Action& action) const
  55. {
  56. StringBuilder builder;
  57. builder.append(action.text());
  58. if (action.shortcut().is_valid()) {
  59. builder.append(" (");
  60. builder.append(action.shortcut().to_string());
  61. builder.append(")");
  62. }
  63. return builder.to_string();
  64. }
  65. virtual void enter_event(Core::Event& event) override
  66. {
  67. auto* app = Application::the();
  68. if (app && action())
  69. Core::EventLoop::current().post_event(*app, make<ActionEvent>(ActionEvent::Type::ActionEnter, *action()));
  70. return Button::enter_event(event);
  71. }
  72. virtual void leave_event(Core::Event& event) override
  73. {
  74. auto* app = Application::the();
  75. if (app && action())
  76. Core::EventLoop::current().post_event(*app, make<ActionEvent>(ActionEvent::Type::ActionLeave, *action()));
  77. return Button::leave_event(event);
  78. }
  79. };
  80. ErrorOr<NonnullRefPtr<GUI::Button>> Toolbar::try_add_action(Action& action)
  81. {
  82. auto item = TRY(adopt_nonnull_own_or_enomem(new (nothrow) Item));
  83. item->type = Item::Type::Action;
  84. item->action = action;
  85. // NOTE: Grow the m_items capacity before potentially adding a child widget.
  86. // This avoids having to untangle the child widget in case of allocation failure.
  87. TRY(m_items.try_ensure_capacity(m_items.size() + 1));
  88. auto button = TRY(try_add<ToolbarButton>(action));
  89. button->set_fixed_size(m_button_size + 8, m_button_size + 8);
  90. m_items.unchecked_append(move(item));
  91. return button;
  92. }
  93. GUI::Button& Toolbar::add_action(Action& action)
  94. {
  95. auto button = MUST(try_add_action(action));
  96. return *button;
  97. }
  98. ErrorOr<void> Toolbar::try_add_separator()
  99. {
  100. // NOTE: Grow the m_items capacity before potentially adding a child widget.
  101. TRY(m_items.try_ensure_capacity(m_items.size() + 1));
  102. auto item = TRY(adopt_nonnull_own_or_enomem(new (nothrow) Item));
  103. item->type = Item::Type::Separator;
  104. (void)TRY(try_add<SeparatorWidget>(m_orientation == Gfx::Orientation::Horizontal ? Gfx::Orientation::Vertical : Gfx::Orientation::Horizontal));
  105. m_items.unchecked_append(move(item));
  106. return {};
  107. }
  108. void Toolbar::add_separator()
  109. {
  110. MUST(try_add_separator());
  111. }
  112. void Toolbar::paint_event(PaintEvent& event)
  113. {
  114. Painter painter(*this);
  115. painter.add_clip_rect(event.rect());
  116. painter.fill_rect(event.rect(), palette().button());
  117. }
  118. }