ToolBar.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/String.h>
  27. #include <AK/StringBuilder.h>
  28. #include <LibGUI/Action.h>
  29. #include <LibGUI/ActionGroup.h>
  30. #include <LibGUI/BoxLayout.h>
  31. #include <LibGUI/Button.h>
  32. #include <LibGUI/Painter.h>
  33. #include <LibGUI/SeparatorWidget.h>
  34. #include <LibGUI/ToolBar.h>
  35. #include <LibGfx/Palette.h>
  36. namespace GUI {
  37. ToolBar::ToolBar(Orientation orientation, int button_size)
  38. : m_orientation(orientation)
  39. , m_button_size(button_size)
  40. {
  41. if (m_orientation == Orientation::Horizontal) {
  42. set_fixed_height(button_size + 8);
  43. } else {
  44. set_fixed_width(button_size + 8);
  45. }
  46. set_layout<BoxLayout>(orientation);
  47. layout()->set_spacing(0);
  48. layout()->set_margins({ 2, 2, 2, 2 });
  49. }
  50. ToolBar::~ToolBar()
  51. {
  52. }
  53. class ToolBarButton final : public Button {
  54. C_OBJECT(ToolBarButton);
  55. public:
  56. virtual ~ToolBarButton() override { }
  57. private:
  58. explicit ToolBarButton(Action& action)
  59. {
  60. if (action.group() && action.group()->is_exclusive())
  61. set_exclusive(true);
  62. set_action(action);
  63. set_tooltip(tooltip(action));
  64. set_focus_policy(FocusPolicy::TabFocus);
  65. if (action.icon())
  66. set_icon(action.icon());
  67. else
  68. set_text(action.text());
  69. set_button_style(Gfx::ButtonStyle::CoolBar);
  70. }
  71. String tooltip(const Action& action) const
  72. {
  73. StringBuilder builder;
  74. builder.append(action.text());
  75. if (action.shortcut().is_valid()) {
  76. builder.append(" (");
  77. builder.append(action.shortcut().to_string());
  78. builder.append(")");
  79. }
  80. return builder.to_string();
  81. }
  82. };
  83. void ToolBar::add_action(Action& action)
  84. {
  85. auto item = make<Item>();
  86. item->type = Item::Type::Action;
  87. item->action = action;
  88. auto& button = add<ToolBarButton>(action);
  89. button.set_fixed_size(m_button_size + 8, m_button_size + 8);
  90. m_items.append(move(item));
  91. }
  92. void ToolBar::add_separator()
  93. {
  94. auto item = make<Item>();
  95. item->type = Item::Type::Separator;
  96. add<SeparatorWidget>(m_orientation == Gfx::Orientation::Horizontal ? Gfx::Orientation::Vertical : Gfx::Orientation::Horizontal);
  97. m_items.append(move(item));
  98. }
  99. void ToolBar::paint_event(PaintEvent& event)
  100. {
  101. Painter painter(*this);
  102. painter.add_clip_rect(event.rect());
  103. painter.fill_rect(event.rect(), palette().button());
  104. }
  105. }