]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
bba9cf9442f0f35bc17f3922a1a2944408a7d2ff
[lyx.git] / src / insets / insetcollapsable.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright (C) 1998 The LyX Team.
7  *
8  *======================================================*/
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "insetcollapsable.h"
17 #include "gettext.h"
18 #include "lyxfont.h"
19 #include "BufferView.h"
20 #include "Painter.h"
21 #include "support/LOstream.h"
22
23 using std::ostream;
24
25 InsetCollapsable::InsetCollapsable(Buffer * bf)
26                 : InsetText(bf)
27 {
28     collapsed = true;
29     label = "Label";
30     autocollapse = true;
31     autoBreakRows = true;
32     framecolor = LColor::footnoteframe;
33     widthOffset = 6; // 2+2 (behind+back), 1+1 (frame)
34     button_length = button_top_y = button_bottom_y = 0;
35     setInsetName("Collapsable");
36 }
37
38
39 Inset * InsetCollapsable::Clone() const
40 {
41     InsetCollapsable * result = new InsetCollapsable(buffer);
42     result->init(buffer, this);
43
44     result->collapsed = collapsed;
45     return result;
46 }
47
48 void InsetCollapsable::Write(ostream & os) const
49 {
50     os << getInsetName() << "\n\ncollapsed ";
51     if (display())
52         os << "false\n";
53     else
54         os << "true\n";
55     WriteParagraphData(os);
56 }
57
58
59 void InsetCollapsable::Read(LyXLex & lex)
60 {
61     if (lex.IsOK()) {
62         lex.next();
63         string token = lex.GetString();
64         if (token == "collapsed") {
65             lex.next();
66             collapsed = lex.GetBool();
67         }
68     }
69     InsetText::Read(lex);
70 }
71
72
73 int InsetCollapsable::ascent_collapsed(Painter & pain, LyXFont const &) const
74 {
75     int width = 0, ascent = 0, descent = 0;
76     pain.buttonText(0, 0, label.c_str(), labelfont, false, 
77                     width, ascent, descent);
78     return ascent;
79 }
80
81
82 int InsetCollapsable::descent_collapsed(Painter & pain, LyXFont const &) const
83 {
84     int width = 0, ascent = 0, descent = 0;
85     pain.buttonText(0, 0, label.c_str(), labelfont, false, 
86                     width, ascent, descent);
87     return descent;
88 }
89
90
91 int InsetCollapsable::width_collapsed(Painter & pain, LyXFont const &) const
92 {
93     int width, ascent, descent;
94     pain.buttonText(TEXT_TO_INSET_OFFSET, 0, label.c_str(), labelfont, false,
95                     width, ascent, descent);
96     return width + (2*TEXT_TO_INSET_OFFSET);
97 }
98
99
100 int InsetCollapsable::ascent(Painter & pain, LyXFont const & font) const
101 {
102     if (collapsed) 
103         return ascent_collapsed(pain, font);
104     else 
105         return InsetText::ascent(pain, font) + TEXT_TO_TOP_OFFSET;
106 }
107
108
109 int InsetCollapsable::descent(Painter & pain, LyXFont const & font) const
110 {
111     if (collapsed) 
112         return descent_collapsed(pain, font);
113     else 
114         return InsetText::descent(pain, font) + TEXT_TO_BOTTOM_OFFSET;
115 }
116
117
118 int InsetCollapsable::width(Painter & pain, LyXFont const & font) const
119 {
120     if (collapsed) 
121         return width_collapsed(pain, font);
122
123     return InsetText::width(pain, font) + width_collapsed(pain,font) +
124             widthOffset;
125 }
126
127
128 void InsetCollapsable::draw_collapsed(Painter & pain, LyXFont const & font,
129                                       int baseline, float & x) const
130 {
131     int width = 0;
132     pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
133                     baseline-ascent(pain, font)+ascent_collapsed(pain, font),
134                     label.c_str(), labelfont, true, width);
135     x += width + (2 * TEXT_TO_INSET_OFFSET);
136 }
137
138
139 void InsetCollapsable::draw(Painter & pain, LyXFont const & f, 
140                             int baseline, float & x) const
141 {
142     button_length = width_collapsed(pain, labelfont) + 2;
143     button_top_y = -ascent_collapsed(pain, f);
144     button_bottom_y = descent_collapsed(pain, f);
145     top_x = int(x);
146     top_baseline = baseline;
147     if (collapsed) {
148         draw_collapsed(pain, f, baseline, x);
149         return;
150     }
151
152     draw_collapsed(pain, f, baseline, x);
153     x -= TEXT_TO_INSET_OFFSET;
154
155     int w =  InsetText::width(pain, f) + TEXT_TO_INSET_OFFSET;
156     int h = ascent(pain,f) + descent(pain, f);
157     
158     pain.rectangle(int(x), baseline - ascent(pain, f), w, h, framecolor);
159
160     x += TEXT_TO_INSET_OFFSET;
161     drawTextXOffset = int(x) - top_x;
162     InsetText::draw(pain, f, baseline, x);
163 }
164
165
166 void InsetCollapsable::Edit(BufferView * bv, int x, int y, unsigned int button)
167 {
168     if (collapsed && autocollapse) {
169         collapsed = false;
170         UpdateLocal(bv, true);
171         InsetText::Edit(bv, 0, 0, button);
172     } else if (!collapsed) {
173         InsetText::Edit(bv, x, y, button);
174     }
175 }
176
177
178 Inset::EDITABLE InsetCollapsable::Editable() const
179 {
180         if (collapsed)
181                 return IS_EDITABLE;
182         return HIGHLY_EDITABLE;
183 }
184
185
186 void InsetCollapsable::InsetUnlock(BufferView * bv)
187 {
188     if (autocollapse) {
189         collapsed = true;
190     }
191     InsetText::InsetUnlock(bv);
192     UpdateLocal(bv, false);
193 }
194
195
196 void InsetCollapsable::UpdateLocal(BufferView * bv, bool flag)
197 {
198     InsetText::UpdateLocal(bv, flag);
199 }
200
201
202 void InsetCollapsable::InsetButtonPress(BufferView * bv,int x,int y,int button)
203 {
204     if (!collapsed && (x >= button_length)) {
205         InsetText::InsetButtonPress(bv, x, y, button);
206     }
207 }
208
209
210 void InsetCollapsable::InsetButtonRelease(BufferView * bv,
211                                           int x, int y, int button)
212 {
213     if ((x >= 0)  && (x < button_length) &&
214         (y >= button_top_y) &&  (y < button_bottom_y)) {
215         if (collapsed) {
216             collapsed = false;
217             InsetText::InsetButtonRelease(bv, 0, 0, button);
218             UpdateLocal(bv, true);
219         } else {
220             collapsed = true;
221             UpdateLocal(bv, false);
222             bv->unlockInset(this);
223         }
224     } else if (!collapsed && (x >= button_length) && (y >= button_top_y)) {
225         InsetText::InsetButtonRelease(bv, x, y, button);
226     }
227 }
228
229
230 void InsetCollapsable::InsetMotionNotify(BufferView * bv,
231                                          int x, int y, int state)
232 {
233     if (x >= button_length) {
234         InsetText::InsetMotionNotify(bv, x, y, state);
235     }
236 }
237
238
239 int InsetCollapsable::getMaxWidth(Painter & pain,
240                                   UpdatableInset const * inset) const
241 {
242     if ((this == inset) && !owner())
243         return pain.paperWidth();
244     if (this == inset)
245         return static_cast<UpdatableInset*>(owner())->getMaxWidth(pain,inset);
246     if (owner())
247         return static_cast<UpdatableInset*>(owner())->getMaxWidth(pain,inset)-
248                 width_collapsed(pain, labelfont) - widthOffset;
249
250     return pain.paperWidth()-width_collapsed(pain, labelfont) - widthOffset;
251 }
252
253
254 int InsetCollapsable::getMaxTextWidth(Painter & pain,
255                                       UpdatableInset const * inset) const
256 {
257     return getMaxWidth(pain, inset) -
258         width_collapsed(pain, labelfont) - widthOffset;
259 }