]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
Fixed mouse-click-bug and made cut/copy/paste work in text-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     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         return;
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) && (y >= button_top_y)) {
175         InsetText::InsetButtonPress(bv, x-top_x, y, button);
176     }
177 }
178
179
180 void InsetCollapsable::InsetButtonRelease(BufferView *bv, int x, int y, int button)
181 {
182     if ((x < button_x)  && (y >= button_top_y) && (y <= button_bottom_y)) {
183         collapsed = true;
184         UpdateLocal(bv, false);
185         bv->unlockInset(this);
186     } else if ((x >= button_x) && (y >= button_top_y)) {
187         InsetText::InsetButtonRelease(bv, x-top_x, y, button);
188     }
189 }
190
191
192 void InsetCollapsable::InsetMotionNotify(BufferView *bv, int x, int y, int button)
193 {
194     if ((x >= button_x) && (y >= button_top_y)) {
195         InsetText::InsetMotionNotify(bv, x-top_x, y, button);
196     }
197 }