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