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#pragma once
28
29#include <AK/Function.h>
30#include <LibGUI/ModelSelection.h>
31#include <LibGUI/ScrollableWidget.h>
32
33namespace GUI {
34
35class AbstractView : public ScrollableWidget {
36 C_OBJECT_ABSTRACT(AbstractView)
37 friend class Model;
38
39public:
40 void set_model(RefPtr<Model>);
41 Model* model() { return m_model.ptr(); }
42 const Model* model() const { return m_model.ptr(); }
43
44 ModelSelection& selection() { return m_selection; }
45 const ModelSelection& selection() const { return m_selection; }
46 virtual void select_all() = 0;
47
48 bool is_editable() const { return m_editable; }
49 void set_editable(bool editable) { m_editable = editable; }
50
51 virtual bool accepts_focus() const override { return true; }
52 virtual void did_update_model();
53 virtual void did_update_selection();
54
55 virtual Gfx::Rect content_rect(const ModelIndex&) const { return {}; }
56 virtual ModelIndex index_at_event_position(const Gfx::Point&) const = 0;
57 void begin_editing(const ModelIndex&);
58 void stop_editing();
59
60 void set_activates_on_selection(bool b) { m_activates_on_selection = b; }
61 bool activates_on_selection() const { return m_activates_on_selection; }
62
63 Function<void()> on_selection_change;
64 Function<void(const ModelIndex&)> on_activation;
65 Function<void(const ModelIndex&)> on_selection;
66 Function<void(const ModelIndex&, const ContextMenuEvent&)> on_context_menu_request;
67 Function<void(const ModelIndex&, const DropEvent&)> on_drop;
68
69 Function<OwnPtr<ModelEditingDelegate>(const ModelIndex&)> aid_create_editing_delegate;
70
71 void notify_selection_changed(Badge<ModelSelection>);
72
73 NonnullRefPtr<Gfx::Font> font_for_index(const ModelIndex&) const;
74
75protected:
76 AbstractView();
77 virtual ~AbstractView() override;
78
79 virtual void mousedown_event(MouseEvent&) override;
80 virtual void mousemove_event(MouseEvent&) override;
81 virtual void mouseup_event(MouseEvent&) override;
82 virtual void doubleclick_event(MouseEvent&) override;
83 virtual void context_menu_event(ContextMenuEvent&) override;
84 virtual void drop_event(DropEvent&) override;
85
86 virtual void did_scroll() override;
87 void activate(const ModelIndex&);
88 void activate_selected();
89 void update_edit_widget_position();
90
91 bool m_editable { false };
92 ModelIndex m_edit_index;
93 RefPtr<Widget> m_edit_widget;
94 Gfx::Rect m_edit_widget_content_rect;
95
96 Gfx::Point m_left_mousedown_position;
97 bool m_might_drag { false };
98
99private:
100 RefPtr<Model> m_model;
101 OwnPtr<ModelEditingDelegate> m_editing_delegate;
102 ModelSelection m_selection;
103 bool m_activates_on_selection { false };
104};
105
106}