Serenity Operating System
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
27#include "PaintableWidget.h"
28#include "Tool.h"
29#include <LibGfx/Bitmap.h>
30#include <LibGfx/Palette.h>
31#include <LibGUI/Painter.h>
32
33static PaintableWidget* s_the;
34
35PaintableWidget& PaintableWidget::the()
36{
37 return *s_the;
38}
39
40PaintableWidget::PaintableWidget()
41{
42 ASSERT(!s_the);
43 s_the = this;
44 set_fill_with_background_color(true);
45 auto pal = palette();
46 pal.set_color(ColorRole::Window, Color::MidGray);
47 set_palette(pal);
48 set_background_color(Color::MidGray);
49 m_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { 600, 400 });
50 m_bitmap->fill(Color::White);
51}
52
53PaintableWidget::~PaintableWidget()
54{
55}
56
57void PaintableWidget::paint_event(GUI::PaintEvent& event)
58{
59 GUI::Painter painter(*this);
60 painter.add_clip_rect(event.rect());
61 painter.blit({ 0, 0 }, *m_bitmap, m_bitmap->rect());
62}
63
64void PaintableWidget::set_tool(Tool* tool)
65{
66 if (m_tool)
67 m_tool->clear();
68 m_tool = tool;
69 if (m_tool)
70 m_tool->setup(*this);
71}
72
73Tool* PaintableWidget::tool()
74{
75 return m_tool;
76}
77
78Color PaintableWidget::color_for(GUI::MouseButton button) const
79{
80 if (button == GUI::MouseButton::Left)
81 return m_primary_color;
82 if (button == GUI::MouseButton::Right)
83 return m_secondary_color;
84 ASSERT_NOT_REACHED();
85}
86
87Color PaintableWidget::color_for(const GUI::MouseEvent& event) const
88{
89 if (event.buttons() & GUI::MouseButton::Left)
90 return m_primary_color;
91 if (event.buttons() & GUI::MouseButton::Right)
92 return m_secondary_color;
93 ASSERT_NOT_REACHED();
94}
95
96void PaintableWidget::mousedown_event(GUI::MouseEvent& event)
97{
98 if (event.button() == GUI::MouseButton::Left || event.button() == GUI::MouseButton::Right) {
99 if (m_tool)
100 m_tool->on_mousedown(event);
101 }
102 GUI::Widget::mousedown_event(event);
103}
104
105void PaintableWidget::mouseup_event(GUI::MouseEvent& event)
106{
107 if (event.button() == GUI::MouseButton::Left || event.button() == GUI::MouseButton::Right) {
108 if (m_tool)
109 m_tool->on_mouseup(event);
110 }
111 GUI::Widget::mouseup_event(event);
112}
113
114void PaintableWidget::mousemove_event(GUI::MouseEvent& event)
115{
116 if (m_tool)
117 m_tool->on_mousemove(event);
118 GUI::Widget::mousemove_event(event);
119}
120
121void PaintableWidget::second_paint_event(GUI::PaintEvent& event)
122{
123 if (m_tool)
124 m_tool->on_second_paint(event);
125 GUI::Widget::second_paint_event(event);
126}
127
128void PaintableWidget::keydown_event(GUI::KeyEvent& event)
129{
130 if (m_tool)
131 m_tool->on_keydown(event);
132 GUI::Widget::keydown_event(event);
133}
134
135void PaintableWidget::keyup_event(GUI::KeyEvent& event)
136{
137 if (m_tool)
138 m_tool->on_keyup(event);
139 GUI::Widget::keyup_event(event);
140}
141
142void PaintableWidget::set_primary_color(Color color)
143{
144 if (m_primary_color == color)
145 return;
146 m_primary_color = color;
147 if (on_primary_color_change)
148 on_primary_color_change(color);
149}
150
151void PaintableWidget::set_secondary_color(Color color)
152{
153 if (m_secondary_color == color)
154 return;
155 m_secondary_color = color;
156 if (on_secondary_color_change)
157 on_secondary_color_change(color);
158}
159
160void PaintableWidget::set_bitmap(const Gfx::Bitmap& bitmap)
161{
162 m_bitmap = bitmap;
163 update();
164}