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