]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
24457fba6dd235230d5a6f2fbbf0343e7034ba11
[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() : InsetText()
26 {
27     collapsed = true;
28     label = "Label";
29     autocollapse = true;
30     autoBreakRows = true;
31     framecolor = LColor::footnoteframe;
32     widthOffset = 6; // 2+2 (behind+back), 1+1 (frame)
33     button_length = button_top_y = button_bottom_y = 0;
34     setInsetName("Collapsable");
35 }
36
37
38 Inset * InsetCollapsable::Clone() const
39 {
40     InsetCollapsable * result = new InsetCollapsable();
41     result->init(this);
42
43     result->collapsed = collapsed;
44     return result;
45 }
46
47 void InsetCollapsable::Write(Buffer const * buf, ostream & os) const
48 {
49     os << getInsetName() << "\n\ncollapsed ";
50     if (display())
51         os << "false\n";
52     else
53         os << "true\n";
54     WriteParagraphData(buf, os);
55 }
56
57
58 void InsetCollapsable::Read(Buffer const * buf, LyXLex & lex)
59 {
60     if (lex.IsOK()) {
61         lex.next();
62         string token = lex.GetString();
63         if (token == "collapsed") {
64             lex.next();
65             collapsed = lex.GetBool();
66         }
67     }
68     InsetText::Read(buf, lex);
69 }
70
71
72 int InsetCollapsable::ascent_collapsed(Painter & pain, LyXFont const &) const
73 {
74     int width = 0, ascent = 0, descent = 0;
75     pain.buttonText(0, 0, label.c_str(), labelfont, false, 
76                     width, ascent, descent);
77     return ascent;
78 }
79
80
81 int InsetCollapsable::descent_collapsed(Painter & pain, LyXFont const &) const
82 {
83     int width = 0, ascent = 0, descent = 0;
84     pain.buttonText(0, 0, label.c_str(), labelfont, false, 
85                     width, ascent, descent);
86     return descent;
87 }
88
89
90 int InsetCollapsable::width_collapsed(Painter & pain, LyXFont const &) const
91 {
92     int width, ascent, descent;
93     pain.buttonText(TEXT_TO_INSET_OFFSET, 0, label.c_str(), labelfont, false,
94                     width, ascent, descent);
95     return width + (2*TEXT_TO_INSET_OFFSET);
96 }
97
98
99 int InsetCollapsable::ascent(Painter & pain, LyXFont const & font) const
100 {
101     if (collapsed) 
102         return ascent_collapsed(pain, font);
103     else 
104         return InsetText::ascent(pain, font) + TEXT_TO_TOP_OFFSET;
105 }
106
107
108 int InsetCollapsable::descent(Painter & pain, LyXFont const & font) const
109 {
110     if (collapsed) 
111         return descent_collapsed(pain, font);
112     else 
113         return InsetText::descent(pain, font) + TEXT_TO_BOTTOM_OFFSET;
114 }
115
116
117 int InsetCollapsable::width(Painter & pain, LyXFont const & font) const
118 {
119     if (collapsed) 
120         return width_collapsed(pain, font);
121
122     return InsetText::width(pain, font) + width_collapsed(pain,font) +
123             widthOffset;
124 }
125
126
127 void InsetCollapsable::draw_collapsed(Painter & pain, LyXFont const & font,
128                                       int baseline, float & x) const
129 {
130     int width = 0;
131     pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
132                     baseline-ascent(pain, font)+ascent_collapsed(pain, font),
133                     label.c_str(), labelfont, true, width);
134     x += width + (2 * TEXT_TO_INSET_OFFSET);
135 }
136
137
138 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f, 
139                             int baseline, float & x, bool cleared) const
140 {
141     Painter & pain = bv->painter();
142
143     button_length = width_collapsed(pain, labelfont) + 2;
144     button_top_y = -ascent_collapsed(pain, f);
145     button_bottom_y = descent_collapsed(pain, f);
146     top_x = int(x);
147     top_baseline = baseline;
148     if (collapsed) {
149         draw_collapsed(pain, f, baseline, x);
150         return;
151     }
152
153     draw_collapsed(pain, f, baseline, x);
154     x -= TEXT_TO_INSET_OFFSET;
155
156     int w =  InsetText::width(pain, f) + 2 * TEXT_TO_INSET_OFFSET;
157     int h = ascent(pain,f) + descent(pain, f);
158     int save_x = static_cast<int>(x);
159     x += TEXT_TO_INSET_OFFSET;
160     drawTextXOffset = int(x) - top_x;
161     InsetText::draw(bv, f, baseline, x, cleared);
162     pain.rectangle(save_x, baseline - ascent(pain, f), w, h, framecolor);
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, FULL, false);
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, FULL, false);
193 }
194
195
196 void InsetCollapsable::InsetButtonPress(BufferView * bv,int x,int y,int button)
197 {
198     if (!collapsed && (x >= button_length)) {
199         InsetText::InsetButtonPress(bv, x, y, button);
200     }
201 }
202
203
204 void InsetCollapsable::InsetButtonRelease(BufferView * bv,
205                                           int x, int y, int button)
206 {
207     if ((x >= 0)  && (x < button_length) &&
208         (y >= button_top_y) &&  (y < button_bottom_y)) {
209         if (collapsed) {
210             collapsed = false;
211             InsetText::InsetButtonRelease(bv, 0, 0, button);
212             UpdateLocal(bv, FULL, false);
213         } else {
214             collapsed = true;
215             UpdateLocal(bv, FULL, false);
216             bv->unlockInset(this);
217         }
218     } else if (!collapsed && (x >= button_length) && (y >= button_top_y)) {
219         InsetText::InsetButtonRelease(bv, x, y, button);
220     }
221 }
222
223
224 void InsetCollapsable::InsetMotionNotify(BufferView * bv,
225                                          int x, int y, int state)
226 {
227     if (x >= button_length) {
228         InsetText::InsetMotionNotify(bv, x, y, state);
229     }
230 }
231
232
233 int InsetCollapsable::getMaxWidth(Painter & pain,
234                                   UpdatableInset const * inset) const
235 {
236     if ((this == inset) && !owner())
237         return pain.paperWidth();
238     if (this == inset)
239         return static_cast<UpdatableInset*>(owner())->getMaxWidth(pain,inset);
240     if (owner())
241         return static_cast<UpdatableInset*>(owner())->getMaxWidth(pain,inset)-
242                 width_collapsed(pain, labelfont) - widthOffset;
243
244     return pain.paperWidth()-width_collapsed(pain, labelfont) - widthOffset;
245 }
246
247
248 int InsetCollapsable::getMaxTextWidth(Painter & pain,
249                                       UpdatableInset const * inset) const
250 {
251     return getMaxWidth(pain, inset) -
252         width_collapsed(pain, labelfont) - widthOffset;
253 }