summaryrefslogtreecommitdiff
path: root/Applications/PaintBrush/ToolboxWidget.cpp
blob: b4ea7a430b75c92d8374dc4c4ab8debb3e9ce304 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "ToolboxWidget.h"
#include "BucketTool.h"
#include "SprayTool.h"
#include "PaintableWidget.h"
#include "PenTool.h"
#include "PickerTool.h"
#include "EraseTool.h"
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GButton.h>
#include <LibDraw/PNGLoader.h>

class ToolButton final : public GButton {
public:
    ToolButton(const String& name, GWidget* parent, OwnPtr<Tool>&& tool)
        : GButton(parent)
        , m_tool(move(tool))
    {
        set_tooltip(name);
    }

    const Tool& tool() const { return *m_tool; }
    Tool& tool() { return *m_tool; }

    virtual void context_menu_event(GContextMenuEvent& event) override
    {
        m_tool->on_contextmenu(event);
    }

private:
    OwnPtr<Tool> m_tool;
};

ToolboxWidget::ToolboxWidget(GWidget* parent)
    : GFrame(parent)
{
    set_background_color(Color::WarmGray);
    set_fill_with_background_color(true);

    set_frame_thickness(1);
    set_frame_shape(FrameShape::Panel);
    set_frame_shadow(FrameShadow::Raised);

    set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
    set_preferred_size(48, 0);

    set_layout(make<GBoxLayout>(Orientation::Vertical));
    layout()->set_margins({ 4, 4, 4, 4 });

    auto add_tool = [&](const StringView& name, const StringView& icon_name, OwnPtr<Tool>&& tool) {
        auto* button = new ToolButton(name, this, move(tool));
        button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
        button->set_preferred_size(0, 32);
        button->set_checkable(true);
        button->set_exclusive(true);

        button->set_icon(load_png(String::format("/res/icons/paintbrush/%s.png", String(icon_name).characters())));

        button->on_checked = [button](auto checked) {
            if (checked)
                PaintableWidget::the().set_tool(&button->tool());
            else
                PaintableWidget::the().set_tool(nullptr);
        };
    };

    add_tool("Pen", "pen", make<PenTool>());
    add_tool("Bucket Fill", "bucket", make<BucketTool>());
    add_tool("Spray", "spray", make<SprayTool>());
    add_tool("Color Picker", "picker", make<PickerTool>());
    add_tool("Erase", "eraser", make<EraseTool>());
}

ToolboxWidget::~ToolboxWidget()
{
}