]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
use more explicit on constructors use the pimpl idom to reduce size with about 500k
[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
22
23 InsetCollapsable::InsetCollapsable(Buffer * bf)
24                 : InsetText(bf)
25 {
26     collapsed = true;
27     label = "Label";
28     autocolapse = true;
29     autoBreakRows = true;
30     framecolor = LColor::footnoteframe;
31     widthOffset = 10;
32     button_x = button_top_y = button_bottom_y = top_x = -1;
33 }
34
35
36 Inset * InsetCollapsable::Clone() const
37 {
38     InsetCollapsable * result = new InsetCollapsable(buffer);
39     result->init(buffer, par);
40
41     result->collapsed = collapsed;
42     return result;
43 }
44
45 int InsetCollapsable::ascent_collapsed(Painter & pain, LyXFont const &) const
46 {
47     int width = 0, ascent = 0, descent = 0;
48     pain.buttonText(0, 0, label.c_str(), labelfont, false, 
49                     width, ascent, descent);
50     return ascent;
51 }
52
53
54 int InsetCollapsable::descent_collapsed(Painter & pain, LyXFont const &) const
55 {
56     int width = 0, ascent = 0, descent = 0;
57     pain.buttonText(0, 0, label.c_str(), labelfont, false, 
58                     width, ascent, descent);
59     return descent;
60 }
61
62
63 int InsetCollapsable::width_collapsed(Painter & pain, LyXFont const &) const
64 {
65     int width, ascent, descent;
66     pain.buttonText(TEXT_TO_INSET_OFFSET, 0, label.c_str(), labelfont, false,
67                     width, ascent, descent);
68     return width + (2*TEXT_TO_INSET_OFFSET);
69 }
70
71
72 int InsetCollapsable::ascent(Painter & pain, LyXFont const & font) const
73 {
74     if (collapsed) 
75         return ascent_collapsed(pain, font);
76     else 
77         return InsetText::ascent(pain, font) + TEXT_TO_TOP_OFFSET;
78 }
79
80
81 int InsetCollapsable::descent(Painter & pain, LyXFont const & font) const
82 {
83     if (collapsed) 
84         return descent_collapsed(pain, font);
85     else 
86         return InsetText::descent(pain, font) + TEXT_TO_BOTTOM_OFFSET;
87 }
88
89
90 int InsetCollapsable::width(Painter & pain, LyXFont const & font) const
91 {
92     if (collapsed) 
93         return width_collapsed(pain, font);
94
95     return getMaxWidth(pain) - widthOffset + 2;
96 }
97
98
99 void InsetCollapsable::draw_collapsed(Painter & pain, LyXFont const &,
100                                       int baseline, float & x) const
101 {
102     int width = 0;
103     pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET, baseline,
104                     label.c_str(), labelfont, true, width);
105     x += width + (2 * TEXT_TO_INSET_OFFSET);
106 }
107
108
109 void InsetCollapsable::draw(Painter & pain, LyXFont const & f, 
110                             int baseline, float & x) const
111 {
112     if (collapsed) {
113         draw_collapsed(pain, f, baseline, x);
114         return;
115     }
116     top_x = int(x);
117     top_baseline = baseline;
118     draw_collapsed(pain, f, baseline, x);
119     button_x = int(x);
120     button_top_y = -ascent_collapsed(pain, f);
121     button_bottom_y = descent_collapsed(pain, f);
122     
123     maxWidth = getMaxWidth(pain) - button_x;
124     x += 2;
125     int
126         w = maxWidth - widthOffset,
127         h = ascent(pain,f) + descent(pain,f);
128     
129     pain.rectangle(int(x), baseline - ascent(pain, f), w, h, framecolor);
130
131     x += 4;
132     top_x = int(x - top_x);
133     InsetText::draw(pain, f, baseline, x);
134 }
135
136
137 void InsetCollapsable::Edit(BufferView *bv, int x, int y, unsigned int button)
138 {
139     if (collapsed) {
140         collapsed = false;
141         UpdateLocal(bv, true);
142         InsetText::Edit(bv, 0, 0, button);
143     } else if (button && (x < button_x)) {
144         return;
145     } else {
146         InsetText::Edit(bv, x-top_x, y, button);
147     }
148 }
149
150
151 Inset::EDITABLE InsetCollapsable::Editable() const
152 {
153         if (collapsed)
154                 return IS_EDITABLE;
155         return HIGHLY_EDITABLE;
156 }
157
158 void InsetCollapsable::InsetUnlock(BufferView *bv)
159 {
160     if (autocolapse) {
161         collapsed = true;
162     }
163     InsetText::InsetUnlock(bv);
164     UpdateLocal(bv, true);
165 }
166
167
168 void InsetCollapsable::UpdateLocal(BufferView *bv, bool flag)
169 {
170     maxWidth = getMaxWidth(bv->getPainter()) -
171         width_collapsed(bv->getPainter(), labelfont);
172     InsetText::UpdateLocal(bv, flag);
173 }
174
175
176 void InsetCollapsable::InsetButtonPress(BufferView *bv,int x,int y,int button)
177 {
178     if ((x >= button_x) && (y >= button_top_y)) {
179         InsetText::InsetButtonPress(bv, x-top_x, y, button);
180     }
181 }
182
183
184 void InsetCollapsable::InsetButtonRelease(BufferView *bv, int x, int y, int button)
185 {
186     if ((x < button_x)  && (y >= button_top_y) && (y <= button_bottom_y)) {
187         collapsed = true;
188         UpdateLocal(bv, false);
189         bv->unlockInset(this);
190     } else if ((x >= button_x) && (y >= button_top_y)) {
191         InsetText::InsetButtonRelease(bv, x-top_x, y, button);
192     }
193 }
194
195
196 void InsetCollapsable::InsetMotionNotify(BufferView *bv, int x, int y, int button)
197 {
198     if ((x >= button_x) && (y >= button_top_y)) {
199         InsetText::InsetMotionNotify(bv, x-top_x, y, button);
200     }
201 }