]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
68f12ab101f36768546626ced3a4e5ab6d0ddf97
[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(Painter & pain, LyXFont const & f, 
139                             int baseline, float & x) const
140 {
141     button_length = width_collapsed(pain, labelfont) + 2;
142     button_top_y = -ascent_collapsed(pain, f);
143     button_bottom_y = descent_collapsed(pain, f);
144     top_x = int(x);
145     top_baseline = baseline;
146     if (collapsed) {
147         draw_collapsed(pain, f, baseline, x);
148         return;
149     }
150
151     draw_collapsed(pain, f, baseline, x);
152     x -= TEXT_TO_INSET_OFFSET;
153
154     int w =  InsetText::width(pain, f) + 2 * TEXT_TO_INSET_OFFSET;
155     int h = ascent(pain,f) + descent(pain, f);
156     
157     pain.rectangle(int(x), baseline - ascent(pain, f), w, h, framecolor);
158
159     x += TEXT_TO_INSET_OFFSET;
160     drawTextXOffset = int(x) - top_x;
161     InsetText::draw(pain, f, baseline, x);
162 }
163
164
165 void InsetCollapsable::Edit(BufferView * bv, int x, int y, unsigned int button)
166 {
167     if (collapsed && autocollapse) {
168         collapsed = false;
169         UpdateLocal(bv, true, false);
170         InsetText::Edit(bv, 0, 0, button);
171     } else if (!collapsed) {
172         InsetText::Edit(bv, x, y, button);
173     }
174 }
175
176
177 Inset::EDITABLE InsetCollapsable::Editable() const
178 {
179         if (collapsed)
180                 return IS_EDITABLE;
181         return HIGHLY_EDITABLE;
182 }
183
184
185 void InsetCollapsable::InsetUnlock(BufferView * bv)
186 {
187     if (autocollapse) {
188         collapsed = true;
189     }
190     InsetText::InsetUnlock(bv);
191     UpdateLocal(bv, false, false);
192 }
193
194
195 void InsetCollapsable::InsetButtonPress(BufferView * bv,int x,int y,int button)
196 {
197     if (!collapsed && (x >= button_length)) {
198         InsetText::InsetButtonPress(bv, x, y, button);
199     }
200 }
201
202
203 void InsetCollapsable::InsetButtonRelease(BufferView * bv,
204                                           int x, int y, int button)
205 {
206     if ((x >= 0)  && (x < button_length) &&
207         (y >= button_top_y) &&  (y < button_bottom_y)) {
208         if (collapsed) {
209             collapsed = false;
210             InsetText::InsetButtonRelease(bv, 0, 0, button);
211             UpdateLocal(bv, true, false);
212         } else {
213             collapsed = true;
214             UpdateLocal(bv, false, false);
215             bv->unlockInset(this);
216         }
217     } else if (!collapsed && (x >= button_length) && (y >= button_top_y)) {
218         InsetText::InsetButtonRelease(bv, x, y, button);
219     }
220 }
221
222
223 void InsetCollapsable::InsetMotionNotify(BufferView * bv,
224                                          int x, int y, int state)
225 {
226     if (x >= button_length) {
227         InsetText::InsetMotionNotify(bv, x, y, state);
228     }
229 }
230
231
232 int InsetCollapsable::getMaxWidth(Painter & pain,
233                                   UpdatableInset const * inset) const
234 {
235     if ((this == inset) && !owner())
236         return pain.paperWidth();
237     if (this == inset)
238         return static_cast<UpdatableInset*>(owner())->getMaxWidth(pain,inset);
239     if (owner())
240         return static_cast<UpdatableInset*>(owner())->getMaxWidth(pain,inset)-
241                 width_collapsed(pain, labelfont) - widthOffset;
242
243     return pain.paperWidth()-width_collapsed(pain, labelfont) - widthOffset;
244 }
245
246
247 int InsetCollapsable::getMaxTextWidth(Painter & pain,
248                                       UpdatableInset const * inset) const
249 {
250     return getMaxWidth(pain, inset) -
251         width_collapsed(pain, labelfont) - widthOffset;
252 }