Splitter.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 <LibGUI/BoxLayout.h>
  27. #include <LibGUI/Painter.h>
  28. #include <LibGUI/Splitter.h>
  29. #include <LibGUI/Window.h>
  30. #include <LibGfx/Palette.h>
  31. REGISTER_WIDGET(GUI, HorizontalSplitter)
  32. REGISTER_WIDGET(GUI, VerticalSplitter)
  33. namespace GUI {
  34. Splitter::Splitter(Orientation orientation)
  35. : m_orientation(orientation)
  36. {
  37. set_background_role(ColorRole::Button);
  38. set_layout<BoxLayout>(orientation);
  39. set_fill_with_background_color(true);
  40. layout()->set_spacing(3);
  41. }
  42. Splitter::~Splitter()
  43. {
  44. }
  45. void Splitter::paint_event(PaintEvent& event)
  46. {
  47. Painter painter(*this);
  48. painter.add_clip_rect(event.rect());
  49. painter.fill_rect(m_grabbable_rect, palette().hover_highlight());
  50. }
  51. void Splitter::resize_event(ResizeEvent& event)
  52. {
  53. Widget::resize_event(event);
  54. m_grabbable_rect = {};
  55. }
  56. void Splitter::override_cursor(bool do_override)
  57. {
  58. if (do_override) {
  59. if (!m_overriding_cursor) {
  60. set_override_cursor(m_orientation == Orientation::Horizontal ? Gfx::StandardCursor::ResizeColumn : Gfx::StandardCursor::ResizeRow);
  61. m_overriding_cursor = true;
  62. }
  63. } else {
  64. if (m_overriding_cursor) {
  65. set_override_cursor(Gfx::StandardCursor::None);
  66. m_overriding_cursor = false;
  67. }
  68. }
  69. }
  70. void Splitter::leave_event(Core::Event&)
  71. {
  72. if (!m_resizing)
  73. override_cursor(false);
  74. if (!m_grabbable_rect.is_empty()) {
  75. m_grabbable_rect = {};
  76. update();
  77. }
  78. }
  79. bool Splitter::get_resize_candidates_at(const Gfx::IntPoint& position, Widget*& first, Widget*& second)
  80. {
  81. int x_or_y = position.primary_offset_for_orientation(m_orientation);
  82. Widget* previous_widget = nullptr;
  83. bool found_candidates = false;
  84. for_each_child_widget([&](auto& child_widget) {
  85. if (!child_widget.is_visible()) {
  86. // We need to skip over widgets that are not visible as they
  87. // are not necessarily in the correct location (anymore)
  88. return IterationDecision::Continue;
  89. }
  90. if (!previous_widget) {
  91. previous_widget = &child_widget;
  92. return IterationDecision::Continue;
  93. }
  94. if (x_or_y > previous_widget->content_rect().last_edge_for_orientation(m_orientation)
  95. && x_or_y <= child_widget.content_rect().first_edge_for_orientation(m_orientation)) {
  96. first = previous_widget;
  97. second = &child_widget;
  98. found_candidates = true;
  99. return IterationDecision::Break;
  100. }
  101. previous_widget = &child_widget;
  102. return IterationDecision::Continue;
  103. });
  104. return found_candidates;
  105. }
  106. void Splitter::mousedown_event(MouseEvent& event)
  107. {
  108. if (event.button() != MouseButton::Left)
  109. return;
  110. m_resizing = true;
  111. Widget* first { nullptr };
  112. Widget* second { nullptr };
  113. if (!get_resize_candidates_at(event.position(), first, second))
  114. return;
  115. m_first_resizee = *first;
  116. m_second_resizee = *second;
  117. m_first_resizee_start_size = first->size();
  118. m_second_resizee_start_size = second->size();
  119. m_resize_origin = event.position();
  120. }
  121. void Splitter::recompute_grabbable_rect(const Widget& first, const Widget& second)
  122. {
  123. auto first_edge = first.content_rect().primary_offset_for_orientation(m_orientation) + first.content_rect().primary_size_for_orientation(m_orientation);
  124. auto second_edge = second.content_rect().primary_offset_for_orientation(m_orientation);
  125. Gfx::IntRect rect;
  126. rect.set_primary_offset_for_orientation(m_orientation, first_edge);
  127. rect.set_primary_size_for_orientation(m_orientation, second_edge - first_edge);
  128. rect.set_secondary_offset_for_orientation(m_orientation, first.content_rect().secondary_offset_for_orientation(m_orientation));
  129. rect.set_secondary_size_for_orientation(m_orientation, first.content_rect().secondary_size_for_orientation(m_orientation));
  130. if (m_grabbable_rect != rect) {
  131. m_grabbable_rect = rect;
  132. update();
  133. }
  134. }
  135. void Splitter::mousemove_event(MouseEvent& event)
  136. {
  137. if (!m_resizing) {
  138. Widget* first { nullptr };
  139. Widget* second { nullptr };
  140. if (!get_resize_candidates_at(event.position(), first, second)) {
  141. override_cursor(false);
  142. return;
  143. }
  144. recompute_grabbable_rect(*first, *second);
  145. override_cursor(m_grabbable_rect.contains(event.position()));
  146. return;
  147. }
  148. auto delta = event.position() - m_resize_origin;
  149. if (!m_first_resizee || !m_second_resizee) {
  150. // One or both of the resizees were deleted during an ongoing resize, screw this.
  151. m_resizing = false;
  152. return;
  153. }
  154. int minimum_size = 0;
  155. auto new_first_resizee_size = m_first_resizee_start_size;
  156. auto new_second_resizee_size = m_second_resizee_start_size;
  157. new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) + delta.primary_offset_for_orientation(m_orientation));
  158. new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) - delta.primary_offset_for_orientation(m_orientation));
  159. if (new_first_resizee_size.primary_size_for_orientation(m_orientation) < minimum_size) {
  160. int correction = minimum_size - new_first_resizee_size.primary_size_for_orientation(m_orientation);
  161. new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) + correction);
  162. new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) - correction);
  163. }
  164. if (new_second_resizee_size.primary_size_for_orientation(m_orientation) < minimum_size) {
  165. int correction = minimum_size - new_second_resizee_size.primary_size_for_orientation(m_orientation);
  166. new_second_resizee_size.set_primary_size_for_orientation(m_orientation, new_second_resizee_size.primary_size_for_orientation(m_orientation) + correction);
  167. new_first_resizee_size.set_primary_size_for_orientation(m_orientation, new_first_resizee_size.primary_size_for_orientation(m_orientation) - correction);
  168. }
  169. if (m_orientation == Orientation::Horizontal) {
  170. m_first_resizee->set_fixed_width(new_first_resizee_size.width());
  171. m_second_resizee->set_fixed_width(-1);
  172. } else {
  173. m_first_resizee->set_fixed_height(new_first_resizee_size.height());
  174. m_second_resizee->set_fixed_height(-1);
  175. }
  176. invalidate_layout();
  177. }
  178. void Splitter::did_layout()
  179. {
  180. if (m_first_resizee && m_second_resizee)
  181. recompute_grabbable_rect(*m_first_resizee, *m_second_resizee);
  182. }
  183. void Splitter::mouseup_event(MouseEvent& event)
  184. {
  185. if (event.button() != MouseButton::Left)
  186. return;
  187. m_resizing = false;
  188. m_first_resizee = nullptr;
  189. m_second_resizee = nullptr;
  190. if (!rect().contains(event.position()))
  191. set_override_cursor(Gfx::StandardCursor::None);
  192. }
  193. }