Breadcrumbbar.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/Breadcrumbbar.h>
  28. #include <LibGUI/Button.h>
  29. #include <LibGUI/Painter.h>
  30. #include <LibGfx/Font.h>
  31. #include <LibGfx/Palette.h>
  32. REGISTER_WIDGET(GUI, Breadcrumbbar)
  33. namespace GUI {
  34. class BreadcrumbButton : public Button {
  35. C_OBJECT(BreadcrumbButton);
  36. public:
  37. virtual ~BreadcrumbButton() override { }
  38. virtual bool is_uncheckable() const override { return false; }
  39. virtual void drop_event(DropEvent& event) override
  40. {
  41. if (on_drop)
  42. on_drop(event);
  43. }
  44. virtual void drag_enter_event(DragEvent& event) override
  45. {
  46. update();
  47. if (on_drag_enter)
  48. on_drag_enter(event);
  49. }
  50. virtual void drag_leave_event(Event&) override
  51. {
  52. update();
  53. }
  54. virtual void paint_event(PaintEvent& event) override
  55. {
  56. Button::paint_event(event);
  57. if (has_pending_drop()) {
  58. Painter painter(*this);
  59. painter.draw_rect(rect(), palette().selection(), true);
  60. }
  61. }
  62. Function<void(DropEvent&)> on_drop;
  63. Function<void(DragEvent&)> on_drag_enter;
  64. private:
  65. BreadcrumbButton() { }
  66. };
  67. Breadcrumbbar::Breadcrumbbar()
  68. {
  69. auto& layout = set_layout<HorizontalBoxLayout>();
  70. layout.set_spacing(0);
  71. }
  72. Breadcrumbbar::~Breadcrumbbar()
  73. {
  74. }
  75. void Breadcrumbbar::clear_segments()
  76. {
  77. m_segments.clear();
  78. remove_all_children();
  79. }
  80. void Breadcrumbbar::append_segment(String text, const Gfx::Bitmap* icon, String data, String tooltip)
  81. {
  82. auto& button = add<BreadcrumbButton>();
  83. button.set_button_style(Gfx::ButtonStyle::Coolbar);
  84. button.set_text(text);
  85. button.set_icon(icon);
  86. button.set_tooltip(move(tooltip));
  87. button.set_focus_policy(FocusPolicy::TabFocus);
  88. button.set_checkable(true);
  89. button.set_exclusive(true);
  90. button.on_click = [this, index = m_segments.size()](auto) {
  91. if (on_segment_click)
  92. on_segment_click(index);
  93. };
  94. button.on_drop = [this, index = m_segments.size()](auto& drop_event) {
  95. if (on_segment_drop)
  96. on_segment_drop(index, drop_event);
  97. };
  98. button.on_drag_enter = [this, index = m_segments.size()](auto& event) {
  99. if (on_segment_drag_enter)
  100. on_segment_drag_enter(index, event);
  101. };
  102. auto button_text_width = button.font().width(text);
  103. auto icon_width = icon ? icon->width() : 0;
  104. auto icon_padding = icon ? 4 : 0;
  105. button.set_fixed_size(button_text_width + icon_width + icon_padding + 16, 16 + 8);
  106. Segment segment { icon, text, data, button.make_weak_ptr<GUI::Button>() };
  107. m_segments.append(move(segment));
  108. }
  109. void Breadcrumbbar::set_selected_segment(Optional<size_t> index)
  110. {
  111. if (!index.has_value()) {
  112. for_each_child_of_type<GUI::AbstractButton>([&](auto& button) {
  113. button.set_checked(false);
  114. return IterationDecision::Continue;
  115. });
  116. return;
  117. }
  118. auto& segment = m_segments[index.value()];
  119. VERIFY(segment.button);
  120. segment.button->set_checked(true);
  121. }
  122. void Breadcrumbbar::doubleclick_event(MouseEvent& event)
  123. {
  124. if (on_doubleclick)
  125. on_doubleclick(event);
  126. }
  127. }