GToolBar.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 <LibDraw/Palette.h>
  27. #include <LibGUI/GAction.h>
  28. #include <LibGUI/GActionGroup.h>
  29. #include <LibGUI/GBoxLayout.h>
  30. #include <LibGUI/GButton.h>
  31. #include <LibGUI/GPainter.h>
  32. #include <LibGUI/GToolBar.h>
  33. GToolBar::GToolBar(GWidget* parent)
  34. : GToolBar(Orientation::Horizontal, 16, parent)
  35. {
  36. }
  37. GToolBar::GToolBar(Orientation orientation, int button_size, GWidget* parent)
  38. : GWidget(parent)
  39. , m_button_size(button_size)
  40. {
  41. if (orientation == Orientation::Horizontal) {
  42. set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  43. set_preferred_size(0, button_size + 12);
  44. } else {
  45. set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  46. set_preferred_size(button_size + 12, 0);
  47. }
  48. set_layout(make<GBoxLayout>(orientation));
  49. layout()->set_spacing(0);
  50. layout()->set_margins({ 2, 2, 2, 2 });
  51. }
  52. GToolBar::~GToolBar()
  53. {
  54. }
  55. void GToolBar::add_action(GAction& action)
  56. {
  57. auto item = make<Item>();
  58. item->type = Item::Action;
  59. item->action = action;
  60. auto button = GButton::construct(this);
  61. if (action.group() && action.group()->is_exclusive())
  62. button->set_exclusive(true);
  63. button->set_action(*item->action);
  64. button->set_tooltip(item->action->text());
  65. if (item->action->icon())
  66. button->set_icon(item->action->icon());
  67. else
  68. button->set_text(item->action->text());
  69. button->set_button_style(ButtonStyle::CoolBar);
  70. button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  71. ASSERT(button->size_policy(Orientation::Horizontal) == SizePolicy::Fixed);
  72. ASSERT(button->size_policy(Orientation::Vertical) == SizePolicy::Fixed);
  73. button->set_preferred_size(m_button_size + 8, m_button_size + 8);
  74. m_items.append(move(item));
  75. }
  76. class SeparatorWidget final : public GWidget {
  77. C_OBJECT(SeparatorWidget)
  78. public:
  79. SeparatorWidget(GWidget* parent)
  80. : GWidget(parent)
  81. {
  82. set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  83. set_preferred_size(8, 22);
  84. }
  85. virtual ~SeparatorWidget() override {}
  86. virtual void paint_event(GPaintEvent& event) override
  87. {
  88. GPainter painter(*this);
  89. painter.add_clip_rect(event.rect());
  90. painter.translate(rect().center().x() - 1, 0);
  91. painter.draw_line({ 0, 0 }, { 0, rect().bottom() }, palette().threed_shadow1());
  92. painter.draw_line({ 1, 0 }, { 1, rect().bottom() }, palette().threed_highlight());
  93. }
  94. };
  95. void GToolBar::add_separator()
  96. {
  97. auto item = make<Item>();
  98. item->type = Item::Separator;
  99. new SeparatorWidget(this);
  100. m_items.append(move(item));
  101. }
  102. void GToolBar::paint_event(GPaintEvent& event)
  103. {
  104. GPainter painter(*this);
  105. painter.add_clip_rect(event.rect());
  106. if (m_has_frame)
  107. StylePainter::paint_surface(painter, rect(), palette(), x() != 0, y() != 0);
  108. else
  109. painter.fill_rect(event.rect(), palette().button());
  110. }