ColorInput.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
  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 <LibCore/Timer.h>
  27. #include <LibGUI/ColorPicker.h>
  28. #include <LibGUI/Painter.h>
  29. #include <LibGUI/ColorInput.h>
  30. #include <LibGfx/Palette.h>
  31. namespace GUI {
  32. ColorInput::ColorInput()
  33. : TextEditor(TextEditor::SingleLine)
  34. {
  35. set_readonly(true);
  36. m_auto_repeat_timer = add<Core::Timer>();
  37. m_auto_repeat_timer->on_timeout = [this] {
  38. click();
  39. };
  40. }
  41. ColorInput::~ColorInput()
  42. {
  43. }
  44. void ColorInput::set_color(Color color)
  45. {
  46. m_color = color;
  47. set_text(color.to_string());
  48. update();
  49. if (on_change)
  50. on_change();
  51. };
  52. void ColorInput::mousedown_event(MouseEvent& event)
  53. {
  54. if (event.button() == MouseButton::Left) {
  55. if (is_enabled()) {
  56. m_being_pressed = true;
  57. update();
  58. if (m_auto_repeat_interval) {
  59. click();
  60. m_auto_repeat_timer->start(m_auto_repeat_interval);
  61. }
  62. }
  63. }
  64. Widget::mousedown_event(event);
  65. }
  66. void ColorInput::mouseup_event(MouseEvent& event)
  67. {
  68. if (event.button() == MouseButton::Left) {
  69. bool was_auto_repeating = m_auto_repeat_timer->is_active();
  70. m_auto_repeat_timer->stop();
  71. if (is_enabled()) {
  72. bool was_being_pressed = m_being_pressed;
  73. m_being_pressed = false;
  74. update();
  75. if (was_being_pressed && !was_auto_repeating)
  76. click();
  77. }
  78. }
  79. Widget::mouseup_event(event);
  80. }
  81. void ColorInput::enter_event(Core::Event&)
  82. {
  83. ASSERT(window());
  84. window()->set_override_cursor(StandardCursor::Arrow);
  85. }
  86. void ColorInput::paint_event(PaintEvent& event)
  87. {
  88. // Set cursor color to base color and stop timer. FIXME: Find better way to hide cursor.
  89. auto pal = palette();
  90. pal.set_color(ColorRole::TextCursor, palette().base());
  91. set_palette(pal);
  92. stop_timer();
  93. TextEditor::paint_event(event);
  94. auto color_box_padding = 3;
  95. auto color_box_size = event.rect().height() - color_box_padding - color_box_padding;
  96. Painter painter(*this);
  97. painter.fill_rect({ event.rect().width() - color_box_size - color_box_padding , color_box_padding, color_box_size, color_box_size}, m_color);
  98. }
  99. void ColorInput::click()
  100. {
  101. if (!is_enabled())
  102. return;
  103. auto dialog = GUI::ColorPicker::construct(m_color, window(), m_color_picker_title);
  104. if (dialog->exec() == GUI::Dialog::ExecOK) {
  105. auto tmp = dialog->color();
  106. set_color(tmp);
  107. }
  108. }
  109. }