ToolBarContainer.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 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 <LibGUI/BoxLayout.h>
  27. #include <LibGUI/Painter.h>
  28. #include <LibGUI/ToolBarContainer.h>
  29. #include <LibGfx/Palette.h>
  30. #include <LibGfx/StylePainter.h>
  31. namespace GUI {
  32. void ToolBarContainer::child_event(Core::ChildEvent& event)
  33. {
  34. Frame::child_event(event);
  35. if (event.type() == Core::Event::ChildAdded) {
  36. if (event.child() && event.child()->is_widget())
  37. did_add_toolbar((Widget&)*event.child());
  38. } else if (event.type() == Core::Event::ChildRemoved) {
  39. if (event.child() && event.child()->is_widget()) {
  40. did_remove_toolbar((Widget&)*event.child());
  41. }
  42. }
  43. }
  44. void ToolBarContainer::did_remove_toolbar(Widget& toolbar)
  45. {
  46. m_toolbars.remove_first_matching([&](auto& entry) { return entry.ptr() == &toolbar; });
  47. recompute_preferred_size();
  48. }
  49. void ToolBarContainer::did_add_toolbar(Widget& toolbar)
  50. {
  51. m_toolbars.append(toolbar);
  52. recompute_preferred_size();
  53. }
  54. void ToolBarContainer::recompute_preferred_size()
  55. {
  56. int preferred_size = 4 + (m_toolbars.size() - 1) * 2;
  57. for (auto& toolbar : m_toolbars) {
  58. if (m_orientation == Gfx::Orientation::Horizontal)
  59. preferred_size += toolbar.preferred_size().height();
  60. else
  61. preferred_size += toolbar.preferred_size().width();
  62. }
  63. if (m_orientation == Gfx::Orientation::Horizontal)
  64. set_preferred_size(0, preferred_size);
  65. else
  66. set_preferred_size(preferred_size, 0);
  67. }
  68. ToolBarContainer::ToolBarContainer(Gfx::Orientation orientation)
  69. : m_orientation(orientation)
  70. {
  71. set_fill_with_background_color(true);
  72. set_frame_thickness(2);
  73. set_frame_shape(Gfx::FrameShape::Box);
  74. set_frame_shadow(Gfx::FrameShadow::Sunken);
  75. if (m_orientation == Gfx::Orientation::Horizontal)
  76. set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  77. else
  78. set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  79. auto& layout = set_layout<VerticalBoxLayout>();
  80. layout.set_spacing(2);
  81. layout.set_margins({ 2, 2, 2, 2 });
  82. }
  83. void ToolBarContainer::paint_event(GUI::PaintEvent& event)
  84. {
  85. Painter painter(*this);
  86. painter.add_clip_rect(event.rect());
  87. for (auto& toolbar : m_toolbars) {
  88. auto rect = toolbar.relative_rect();
  89. painter.draw_line(rect.top_left().translated(0, -1), rect.top_right().translated(0, -1), palette().threed_highlight());
  90. painter.draw_line(rect.bottom_left().translated(0, 1), rect.bottom_right().translated(0, 1), palette().threed_shadow1());
  91. }
  92. Frame::paint_event(event);
  93. }
  94. }