]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
several changes and some new insets, read the Changelog
[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 #include "support/lstrings.h"
23
24 using std::ostream;
25
26 InsetCollapsable::InsetCollapsable() : InsetText()
27 {
28     collapsed = true;
29     label = "Label";
30     autocollapse = true;
31     autoBreakRows = true;
32     framecolor = LColor::footnoteframe;
33     widthOffset = 2 * TEXT_TO_INSET_OFFSET + 2; // 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();
42     result->init(this);
43
44     result->collapsed = collapsed;
45     return result;
46 }
47
48
49 void InsetCollapsable::Write(Buffer const * buf, ostream & os) const
50 {
51         os << "collapsed " << tostr(!display()) << "\n";
52         WriteParagraphData(buf, os);
53 }
54
55
56 void InsetCollapsable::Read(Buffer const * buf, LyXLex & lex)
57 {
58     if (lex.IsOK()) {
59         lex.next();
60         string token = lex.GetString();
61         if (token == "collapsed") {
62             lex.next();
63             collapsed = lex.GetBool();
64         }
65     }
66     InsetText::Read(buf, lex);
67 }
68
69
70 int InsetCollapsable::ascent_collapsed(Painter & pain, LyXFont const &) const
71 {
72     int width = 0, ascent = 0, descent = 0;
73     pain.buttonText(0, 0, label.c_str(), labelfont, false, 
74                     width, ascent, descent);
75     return ascent;
76 }
77
78
79 int InsetCollapsable::descent_collapsed(Painter & pain, LyXFont const &) const
80 {
81     int width = 0, ascent = 0, descent = 0;
82     pain.buttonText(0, 0, label.c_str(), labelfont, false, 
83                     width, ascent, descent);
84     return descent;
85 }
86
87
88 int InsetCollapsable::width_collapsed(Painter & pain, LyXFont const &) const
89 {
90     int width, ascent, descent;
91     pain.buttonText(TEXT_TO_INSET_OFFSET, 0, label.c_str(), labelfont, false,
92                     width, ascent, descent);
93     return width + (2*TEXT_TO_INSET_OFFSET);
94 }
95
96
97 int InsetCollapsable::ascent(Painter & pain, LyXFont const & font) const
98 {
99     if (collapsed) 
100         return ascent_collapsed(pain, font);
101     else 
102         return InsetText::ascent(pain, font) + TEXT_TO_TOP_OFFSET;
103 }
104
105
106 int InsetCollapsable::descent(Painter & pain, LyXFont const & font) const
107 {
108     if (collapsed) 
109         return descent_collapsed(pain, font);
110     else 
111         return InsetText::descent(pain, font) + TEXT_TO_BOTTOM_OFFSET;
112 }
113
114
115 int InsetCollapsable::width(Painter & pain, LyXFont const & font) const
116 {
117     if (collapsed) 
118         return width_collapsed(pain, font);
119
120     return InsetText::width(pain, font) + width_collapsed(pain,font) +
121             widthOffset;
122 }
123
124
125 void InsetCollapsable::draw_collapsed(Painter & pain, LyXFont const & font,
126                                       int baseline, float & x) const
127 {
128     int width = 0;
129     pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
130                     baseline-ascent(pain, font)+ascent_collapsed(pain, font),
131                     label.c_str(), labelfont, true, width);
132     x += width + (2 * TEXT_TO_INSET_OFFSET);
133 }
134
135
136 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f, 
137                             int baseline, float & x, bool cleared) const
138 {
139     Painter & pain = bv->painter();
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     int save_x = static_cast<int>(x);
157     x += TEXT_TO_INSET_OFFSET;
158     InsetText::draw(bv, f, baseline, x, cleared);
159     pain.rectangle(save_x, baseline - ascent(pain, f), w, h, framecolor);
160 }
161
162
163 void InsetCollapsable::Edit(BufferView * bv, int x, int y, unsigned int button)
164 {
165     if (collapsed && autocollapse) {
166         collapsed = false;
167         UpdateLocal(bv, FULL, false);
168         InsetText::Edit(bv, 0, 0, button);
169     } else if (!collapsed) {
170         InsetText::Edit(bv, x, y, button);
171     }
172 }
173
174
175 Inset::EDITABLE InsetCollapsable::Editable() const
176 {
177         if (collapsed)
178                 return IS_EDITABLE;
179         return HIGHLY_EDITABLE;
180 }
181
182
183 void InsetCollapsable::InsetUnlock(BufferView * bv)
184 {
185     if (autocollapse) {
186         collapsed = true;
187     }
188     InsetText::InsetUnlock(bv);
189     UpdateLocal(bv, FULL, false);
190 }
191
192
193 void InsetCollapsable::InsetButtonPress(BufferView * bv,int x,int y,int button)
194 {
195     if (!collapsed && (x >= button_length)) {
196         InsetText::InsetButtonPress(bv, x, y, button);
197     }
198 }
199
200
201 void InsetCollapsable::InsetButtonRelease(BufferView * bv,
202                                           int x, int y, int button)
203 {
204     if ((x >= 0)  && (x < button_length) &&
205         (y >= button_top_y) &&  (y < button_bottom_y)) {
206         if (collapsed) {
207             collapsed = false;
208             InsetText::InsetButtonRelease(bv, 0, 0, button);
209             UpdateLocal(bv, FULL, false);
210         } else {
211             collapsed = true;
212             UpdateLocal(bv, FULL, false);
213             bv->unlockInset(this);
214         }
215     } else if (!collapsed && (x >= button_length) && (y >= button_top_y)) {
216         InsetText::InsetButtonRelease(bv, x, y, button);
217     }
218 }
219
220
221 void InsetCollapsable::InsetMotionNotify(BufferView * bv,
222                                          int x, int y, int state)
223 {
224     if (x >= button_length) {
225         InsetText::InsetMotionNotify(bv, x, y, state);
226     }
227 }
228
229
230 int InsetCollapsable::getMaxWidth(Painter & pain,
231                                   UpdatableInset const * inset) const
232 {
233     if ((this == inset) && !owner())
234         return pain.paperWidth();
235     if (this == inset)
236         return static_cast<UpdatableInset*>(owner())->getMaxWidth(pain,inset);
237     if (owner())
238         return static_cast<UpdatableInset*>(owner())->getMaxWidth(pain,inset)-
239                 width_collapsed(pain, labelfont) - widthOffset;
240
241     return pain.paperWidth()-width_collapsed(pain, labelfont) - widthOffset;
242 }
243
244
245 int InsetCollapsable::getMaxTextWidth(Painter & pain,
246                                       UpdatableInset const * inset) const
247 {
248     return getMaxWidth(pain, inset) -
249         width_collapsed(pain, labelfont) - widthOffset;
250 }
251
252
253 void InsetCollapsable::update(BufferView * bv,
254                               LyXFont const & font, bool dodraw)
255 {
256     drawTextXOffset = width_collapsed(bv->painter(), font);
257     InsetText::update(bv, font, dodraw);
258 }