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