GSplitter.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <LibGUI/GSplitter.h>
  2. #include <LibGUI/GBoxLayout.h>
  3. #include <LibGUI/GWindow.h>
  4. GSplitter::GSplitter(Orientation orientation, GWidget* parent)
  5. : GFrame(parent)
  6. , m_orientation(orientation)
  7. {
  8. set_layout(make<GBoxLayout>(orientation));
  9. set_fill_with_background_color(true);
  10. set_background_color(Color::LightGray);
  11. layout()->set_spacing(4);
  12. }
  13. GSplitter::~GSplitter()
  14. {
  15. }
  16. void GSplitter::enter_event(CEvent&)
  17. {
  18. set_background_color(Color::from_rgb(0xd6d2ce));
  19. window()->set_override_cursor(m_orientation == Orientation::Horizontal ? GStandardCursor::ResizeHorizontal : GStandardCursor::ResizeVertical);
  20. update();
  21. }
  22. void GSplitter::leave_event(CEvent&)
  23. {
  24. set_background_color(Color::LightGray);
  25. if (!m_resizing)
  26. window()->set_override_cursor(GStandardCursor::None);
  27. update();
  28. }
  29. void GSplitter::mousedown_event(GMouseEvent& event)
  30. {
  31. if (event.button() != GMouseButton::Left)
  32. return;
  33. m_resizing = true;
  34. int x_or_y = m_orientation == Orientation::Horizontal ? event.x() : event.y();
  35. GWidget* first_resizee { nullptr };
  36. GWidget* second_resizee { nullptr };
  37. int fudge = layout()->spacing();
  38. for_each_child_widget([&] (auto& child) {
  39. int child_start = m_orientation == Orientation::Horizontal ? child.relative_rect().left() : child.relative_rect().top();
  40. int child_end = m_orientation == Orientation::Horizontal ? child.relative_rect().right() : child.relative_rect().bottom();
  41. if (x_or_y > child_end && (x_or_y - fudge) <= child_end)
  42. first_resizee = &child;
  43. if (x_or_y < child_start && (x_or_y + fudge) >= child_start)
  44. second_resizee = &child;
  45. return IterationDecision::Continue;
  46. });
  47. ASSERT(first_resizee && second_resizee);
  48. m_first_resizee = first_resizee->make_weak_ptr();
  49. m_second_resizee = second_resizee->make_weak_ptr();
  50. m_first_resizee_start_size = first_resizee->size();
  51. m_second_resizee_start_size = second_resizee->size();
  52. m_resize_origin = event.position();
  53. }
  54. void GSplitter::mousemove_event(GMouseEvent& event)
  55. {
  56. if (!m_resizing)
  57. return;
  58. auto delta = event.position() - m_resize_origin;
  59. if (!m_first_resizee || !m_second_resizee) {
  60. // One or both of the resizees were deleted during an ongoing resize, screw this.
  61. m_resizing = false;
  62. return;;
  63. }
  64. int minimum_size = 0;
  65. auto new_first_resizee_size = m_first_resizee_start_size;
  66. auto new_second_resizee_size = m_second_resizee_start_size;
  67. if (m_orientation == Orientation::Horizontal) {
  68. new_first_resizee_size.set_width(new_first_resizee_size.width() + delta.x());
  69. new_second_resizee_size.set_width(new_second_resizee_size.width() - delta.x());
  70. if (new_first_resizee_size.width() < minimum_size) {
  71. int correction = minimum_size - new_first_resizee_size.width();
  72. new_first_resizee_size.set_width(new_first_resizee_size.width() + correction);
  73. new_second_resizee_size.set_width(new_second_resizee_size.width() - correction);
  74. }
  75. if (new_second_resizee_size.width() < minimum_size) {
  76. int correction = minimum_size - new_second_resizee_size.width();
  77. new_second_resizee_size.set_width(new_second_resizee_size.width() + correction);
  78. new_first_resizee_size.set_width(new_first_resizee_size.width() - correction);
  79. }
  80. } else {
  81. new_first_resizee_size.set_height(new_first_resizee_size.height() + delta.y());
  82. new_second_resizee_size.set_height(new_second_resizee_size.height() - delta.y());
  83. if (new_first_resizee_size.height() < minimum_size) {
  84. int correction = minimum_size - new_first_resizee_size.height();
  85. new_first_resizee_size.set_height(new_first_resizee_size.height() + correction);
  86. new_second_resizee_size.set_height(new_second_resizee_size.height() - correction);
  87. }
  88. if (new_second_resizee_size.height() < minimum_size) {
  89. int correction = minimum_size - new_second_resizee_size.height();
  90. new_second_resizee_size.set_height(new_second_resizee_size.height() + correction);
  91. new_first_resizee_size.set_height(new_first_resizee_size.height() - correction);
  92. }
  93. }
  94. m_first_resizee->set_preferred_size(new_first_resizee_size);
  95. m_second_resizee->set_preferred_size(new_second_resizee_size);
  96. invalidate_layout();
  97. }
  98. void GSplitter::mouseup_event(GMouseEvent& event)
  99. {
  100. if (event.button() != GMouseButton::Left)
  101. return;
  102. m_resizing = false;
  103. if (!rect().contains(event.position()))
  104. window()->set_override_cursor(GStandardCursor::None);
  105. }