]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsable.h
* Do not keep pointers to data structures around if you don't know
[lyx.git] / src / insets / InsetCollapsable.h
1 // -*- C++ -*-
2 /**
3  * \file InsetCollapsable.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 INSETCOLLAPSABLE_H
15 #define INSETCOLLAPSABLE_H
16
17 #include "Inset.h"
18 #include "InsetLayout.h"
19 #include "InsetText.h"
20
21 #include "Box.h"
22 #include "TextClass.h"
23 #include "TextClassPtr.h"
24
25 #include <string>
26
27 namespace lyx {
28
29 class CursorSlice;
30 class FontInfo;
31 class InsetLayout;
32 class Paragraph;
33 class Text;
34
35 namespace frontend { class Painter; }
36
37 /** A collapsable text inset
38
39 */
40 class InsetCollapsable : public InsetText {
41 public:
42         ///
43         InsetCollapsable(
44                 BufferParams const &,
45                 CollapseStatus status = Inset::Open,
46                 TextClassPtr tc = TextClassPtr((TextClass *)0)
47                 );
48         ///
49         InsetCollapsable(InsetCollapsable const & rhs);
50         
51         InsetCollapsable * asInsetCollapsable() { return this; }
52         InsetCollapsable const * asInsetCollapsable() const { return this; }
53         docstring toolTip(BufferView const & bv, int x, int y) const;
54         docstring name() const { return from_ascii("Collapsable"); }
55         InsetLayout const & getLayout(BufferParams const &) const
56         { return *layout_; } 
57         InsetLayout const & getLayout() const
58         { return *layout_; } 
59         ///
60         void setLayout(BufferParams const &);
61         /// (Re-)set the character style parameters from \p tc according
62         /// to name()
63         void setLayout(TextClassPtr tc);
64         ///
65         void read(Buffer const &, Lexer &);
66         ///
67         void write(Buffer const &, std::ostream &) const;
68         ///
69         void metrics(MetricsInfo &, Dimension &) const;
70         ///
71         void draw(PainterInfo & pi, int x, int y) const;
72
73         /// return x,y of given position relative to the inset's baseline
74         void cursorPos(BufferView const & bv, CursorSlice const & sl,
75                 bool boundary, int & x, int & y) const;
76         ///
77         bool hitButton(FuncRequest const &) const;
78         ///
79         docstring const getNewLabel(docstring const & l) const;
80         ///
81         EDITABLE editable() const;
82         /// can we go further down on mouse click?
83         bool descendable() const;
84         ///
85         bool isMacroScope(Buffer const & buf) const;
86         ///
87         void setLabel(docstring const & l);
88         ///
89         virtual void setButtonLabel() {}
90         ///
91         bool isOpen() const { return geometry() != ButtonOnly; }
92         ///
93         CollapseStatus status() const;
94         /** Of the old CollapseStatus we only keep the values  
95          *  Open and Collapsed.
96          * We define a list of possible inset decoration
97          * styles, and a list of possible (concrete, visual)
98          * inset geometries. Relationships between them
99          * (geometries in body of table):
100          *
101          *               \       CollapseStatus:
102          *   Decoration:  \ Open                Collapsed
103          *   -------------+-------------------------------
104          *   Classic      | *) TopButton, <--x) ButtonOnly
105          *                | LeftButton
106          *   Minimalistic | ButtonOnly          NoButton
107          *   Conglomerate | SubLabel            Corners
108          *   ---------------------------------------------
109          *   *) toggled by openinlined_
110          *   x) toggled by autoOpen_
111          */
112
113         ///
114         enum Decoration {
115                 Classic,
116                 Minimalistic,
117                 Conglomerate
118         };
119         /// Default looks
120         virtual Decoration decoration() const;
121         ///
122         enum Geometry {
123                 TopButton,
124                 ButtonOnly,
125                 NoButton,
126                 LeftButton,
127                 SubLabel,
128                 Corners
129         };
130         /// Returns the geometry based on CollapseStatus
131         /// (status_), autoOpen_ and openinlined_, and of
132         /// course decoration().
133         Geometry geometry() const;
134         ///
135         bool allowSpellCheck() const { return true; }
136         ///
137         bool allowMultiPar() const;
138         ///
139         bool getStatus(Cursor &, FuncRequest const &, FuncStatus &) const;
140         ///
141         void setStatus(Cursor & cur, CollapseStatus st);
142         ///
143         bool setMouseHover(bool mouse_hover);
144         ///
145         virtual ColorCode backgroundColor() const {return layout_->bgcolor; }
146
147         int latex(Buffer const &, odocstream &,
148                   OutputParams const &) const;
149         ///
150         void validate(LaTeXFeatures &) const;
151         ///
152         virtual InsetCode lyxCode() const { return COLLAPSABLE_CODE; }
153
154         /// Allow multiple blanks
155         virtual bool isFreeSpacing() const { return layout_->freespacing; }
156         /// Don't eliminate empty paragraphs
157         virtual bool allowEmpty() const { return layout_->keepempty; }
158         /// Force inset into LTR environment if surroundings are RTL?
159         virtual bool forceLTR() const { return layout_->forceltr; }
160
161 protected:
162         ///
163         virtual void doDispatch(Cursor & cur, FuncRequest & cmd);
164         ///
165         void edit(Cursor & cur, bool left);
166         ///
167         Inset * editXY(Cursor & cur, int x, int y);
168         ///
169         docstring floatName(std::string const & type, BufferParams const &) const;
170         ///
171         virtual void resetParagraphsFont();
172
173 private:
174         /// text class to keep the InsetLayout above in memory
175         TextClassPtr textClass_;
176         /// cache for the layout_. Make sure it is in sync with the text class!
177         InsetLayout const * layout_;
178         ///
179         Dimension dimensionCollapsed() const;
180         ///
181         docstring labelstring_;
182         ///
183         mutable Box button_dim;
184         ///
185         mutable CollapseStatus status_;
186         /// a substatus of the Open status, determined automatically in metrics
187         mutable bool openinlined_;
188         /// the inset will automatically open when the cursor is inside
189         mutable bool autoOpen_;
190         /// changes color when mouse enters/leaves this inset
191         bool mouse_hover_;
192 };
193
194 } // namespace lyx
195
196 #endif