]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsible.h
Loop refactoring
[lyx.git] / src / insets / InsetCollapsible.h
1 // -*- C++ -*-
2 /**
3  * \file InsetCollapsible.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Alejandro Aguilar Sierra
8  * \author Jürgen Vigna
9  * \author Lars Gullik Bjønnes
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef INSETCOLLAPSIBLE_H
15 #define INSETCOLLAPSIBLE_H
16
17 #include "InsetText.h"
18
19 #include "Box.h"
20
21 #include <map>
22
23 namespace lyx {
24
25 class CursorSlice;
26 class InsetLayout;
27
28 namespace support { class TempFile; }
29
30 namespace frontend { class Painter; }
31
32 /** A collapsible text inset
33
34 */
35 class InsetCollapsible : public InsetText {
36 public:
37         ///
38         InsetCollapsible(Buffer *, InsetText::UsePlain = InsetText::PlainLayout);
39         ///
40         InsetCollapsible(InsetCollapsible const & rhs);
41         ///
42         InsetCollapsible & operator=(InsetCollapsible const &);
43         ///
44         virtual ~InsetCollapsible();
45         ///
46         InsetCollapsible * asInsetCollapsible() override { return this; }
47         ///
48         InsetCollapsible const * asInsetCollapsible() const override { return this; }
49         ///
50         docstring toolTip(BufferView const & bv, int x, int y) const override;
51         ///
52         docstring layoutName() const override { return from_ascii("Collapsible"); }
53         ///
54         void read(Lexer &) override;
55         ///
56         void write(std::ostream &) const override;
57
58         ///
59         int topOffset(BufferView const * bv) const override;
60         ///
61         int bottomOffset(BufferView const * bv) const override;
62
63         ///
64         void metrics(MetricsInfo &, Dimension &) const override;
65         ///
66         void draw(PainterInfo & pi, int x, int y) const override;
67         ///
68         void drawBackground(PainterInfo &, int, int) const override {}
69
70         /// return x,y of given position relative to the inset's baseline
71         void cursorPos(BufferView const & bv, CursorSlice const & sl,
72                 bool boundary, int & x, int & y) const override;
73         ///
74         docstring const getNewLabel(docstring const & l) const;
75         ///
76         bool editable() const override;
77         ///
78         bool hasSettings() const override { return true; }
79         /// Returns true if coordinates are over the inset's button.
80         /// Always returns false when the inset does not have a
81         /// button.
82         bool clickable(BufferView const & bv, int x, int y) const override;
83         /// can we go further down on mouse click?
84         bool descendable(BufferView const & bv) const override;
85         ///
86         void setLabel(docstring const & l);
87         ///
88         docstring getLabel() const;
89         ///
90         virtual void setButtonLabel() {}
91         ///
92         virtual docstring const buttonLabel(BufferView const &) const;
93         ///
94         bool isOpen(BufferView const & bv) const
95                 { return geometry(bv) != ButtonOnly; }
96         ///
97         enum CollapseStatus {
98                 Collapsed,
99                 Open
100         };
101         ///
102         virtual void setStatus(Cursor & cur, CollapseStatus st);
103         ///
104         CollapseStatus status(BufferView const & bv) const;
105         /** Of the old CollapseStatus we only keep the values
106          *  Open and Collapsed.
107          * We define a list of possible inset decoration
108          * styles, and a list of possible (concrete, visual)
109          * inset geometries. Relationships between them
110          * (geometries in body of table):
111          *
112          *               \       CollapseStatus:
113          *   Decoration:  \ Open                Collapsed
114          *   -------------+-------------------------------
115          *   Classic      | *) TopButton, <--x) ButtonOnly
116          *                | LeftButton
117          *   Minimalistic | NoButton            ButtonOnly
118          *   Conglomerate | SubLabel            Corners
119          *   ---------------------------------------------
120          *   *) toggled by openinlined_
121          *   x) toggled by auto_open_
122          */
123
124         /// Default looks
125         virtual InsetLayout::InsetDecoration decoration() const;
126         /// Inset font
127         virtual FontInfo getFont() const { return getLayout().font(); }
128         /// Label font
129         virtual FontInfo getLabelfont() const { return getLayout().labelfont(); }
130         ///
131         enum Geometry {
132                 TopButton,
133                 ButtonOnly,
134                 NoButton,
135                 LeftButton,
136                 SubLabel,
137                 Corners
138         };
139         /// Returns the geometry based on CollapseStatus
140         /// (status_), auto_open_[BufferView] and openinlined_,
141         /// and of course decoration().
142         Geometry geometry(BufferView const & bv) const;
143         ///
144         bool canPaintChange(BufferView const & bv) const override;
145         ///
146         bool getStatus(Cursor &, FuncRequest const &, FuncStatus &) const override;
147         ///
148         bool setMouseHover(BufferView const * bv, bool mouse_hover) const override;
149         ///
150         ColorCode backgroundColor(PainterInfo const &) const override
151                 { return getLayout().bgcolor(); }
152         ///
153         ColorCode labelColor() const override { return getLayout().labelfont().color(); }
154         ///
155         InsetCode lyxCode() const override { return COLLAPSIBLE_CODE; }
156
157         ///
158         bool usePlainLayout() const override { return true; }
159         ///
160         std::string contextMenu(BufferView const & bv, int x, int y) const override;
161         ///
162         std::string contextMenuName() const override;
163         ///
164         void addToToc(DocIterator const & dit, bool output_active,
165                       UpdateType utype, TocBackend & backend) const override;
166
167 protected:
168         ///
169         void doDispatch(Cursor & cur, FuncRequest & cmd) override;
170         ///
171         void edit(Cursor & cur, bool front,
172                 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE) override;
173         ///
174         Inset * editXY(Cursor & cur, int x, int y) override;
175         ///
176         mutable CollapseStatus status_;
177         ///
178         unique_ptr<support::TempFile> tempfile_;
179 private:
180         ///
181         Dimension dimensionCollapsed(BufferView const & bv) const;
182         ///
183         docstring labelstring_;
184
185         // These variables depend of the view in which the inset is displayed
186         struct View
187         {
188                 /// The dimension of the inset button
189                 Box button_dim_;
190                 /// a substatus of the Open status, determined automatically in metrics
191                 bool openinlined_;
192                 /// the inset will automatically open when the cursor is inside. This is
193                 /// dependent on the bufferview, compare with InsetMathMacro::editing_.
194                 bool auto_open_;
195                 /// changes color when mouse enters/leaves this inset
196                 bool mouse_hover_;
197         };
198
199         ///
200         mutable std::map<BufferView const *, View> view_;
201 };
202
203 } // namespace lyx
204
205 #endif