ResizeCorner.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <LibGfx/CharacterBitmap.h>
  27. #include <LibGfx/Palette.h>
  28. #include <LibGUI/Painter.h>
  29. #include <LibGUI/ResizeCorner.h>
  30. #include <LibGUI/Window.h>
  31. namespace GUI {
  32. static const char* s_resize_corner_shadows_data = {
  33. " "
  34. " ## "
  35. " # "
  36. " "
  37. " ## ## "
  38. " # # "
  39. " "
  40. " ## ## ## "
  41. " # # # "
  42. " "
  43. " ## ## ## ## "
  44. " # # # # "
  45. " "
  46. " ## ## ## ## ## "
  47. " # # # # # "
  48. " "
  49. };
  50. static const char* s_resize_corner_highlights_data = {
  51. " "
  52. " "
  53. " # "
  54. " "
  55. " "
  56. " # # "
  57. " "
  58. " "
  59. " # # # "
  60. " "
  61. " "
  62. " # # # # "
  63. " "
  64. " "
  65. " # # # # # "
  66. " "
  67. };
  68. static Gfx::CharacterBitmap* s_resize_corner_shadows_bitmap;
  69. static Gfx::CharacterBitmap* s_resize_corner_highlights_bitmap;
  70. static const int s_resize_corner_bitmap_width = 16;
  71. static const int s_resize_corner_bitmap_height = 16;
  72. ResizeCorner::ResizeCorner()
  73. {
  74. set_background_role(ColorRole::Button);
  75. set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  76. set_preferred_size(16, 18);
  77. }
  78. ResizeCorner::~ResizeCorner()
  79. {
  80. }
  81. void ResizeCorner::paint_event(PaintEvent& event)
  82. {
  83. Painter painter(*this);
  84. painter.add_clip_rect(event.rect());
  85. painter.fill_rect(rect(), palette().color(background_role()));
  86. if (!s_resize_corner_shadows_bitmap)
  87. s_resize_corner_shadows_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_resize_corner_shadows_data, s_resize_corner_bitmap_width, s_resize_corner_bitmap_height).leak_ref();
  88. painter.draw_bitmap({ 0, 2 }, *s_resize_corner_shadows_bitmap, palette().threed_shadow1());
  89. if (!s_resize_corner_highlights_bitmap)
  90. s_resize_corner_highlights_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_resize_corner_highlights_data, s_resize_corner_bitmap_width, s_resize_corner_bitmap_height).leak_ref();
  91. painter.draw_bitmap({ 0, 2 }, *s_resize_corner_highlights_bitmap, palette().threed_highlight());
  92. Widget::paint_event(event);
  93. }
  94. void ResizeCorner::mousedown_event(MouseEvent& event)
  95. {
  96. if (event.button() == MouseButton::Left)
  97. window()->start_wm_resize();
  98. Widget::mousedown_event(event);
  99. }
  100. void ResizeCorner::enter_event(Core::Event& event)
  101. {
  102. window()->set_override_cursor(StandardCursor::ResizeDiagonalTLBR);
  103. Widget::enter_event(event);
  104. }
  105. void ResizeCorner::leave_event(Core::Event& event)
  106. {
  107. window()->set_override_cursor(StandardCursor::None);
  108. Widget::leave_event(event);
  109. }
  110. }