]> git.lyx.org Git - lyx.git/blob - src/insets/insetbranch.C
The speed patch: redraw only rows that have changed
[lyx.git] / src / insets / insetbranch.C
1 /**
2  * \file insetbranch.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Martin Vermeer
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insetbranch.h"
14
15 #include "buffer.h"
16 #include "bufferparams.h"
17 #include "BranchList.h"
18 #include "cursor.h"
19 #include "dispatchresult.h"
20 #include "funcrequest.h"
21 #include "FuncStatus.h"
22 #include "gettext.h"
23 #include "LColor.h"
24 #include "lyxlex.h"
25 #include "paragraph.h"
26
27 #include <sstream>
28
29 using std::string;
30 using std::auto_ptr;
31 using std::istringstream;
32 using std::ostream;
33 using std::ostringstream;
34
35
36 void InsetBranch::init()
37 {
38         setInsetName("Branch");
39         setButtonLabel();
40 }
41
42
43 InsetBranch::InsetBranch(BufferParams const & bp,
44                          InsetBranchParams const & params)
45         : InsetCollapsable(bp), params_(params)
46 {
47         init();
48 }
49
50
51 InsetBranch::InsetBranch(InsetBranch const & in)
52         : InsetCollapsable(in), params_(in.params_)
53 {
54         init();
55 }
56
57
58 InsetBranch::~InsetBranch()
59 {
60         InsetBranchMailer(*this).hideDialog();
61 }
62
63
64 auto_ptr<InsetBase> InsetBranch::doClone() const
65 {
66         return auto_ptr<InsetBase>(new InsetBranch(*this));
67 }
68
69
70 string const InsetBranch::editMessage() const
71 {
72         return _("Opened Branch Inset");
73 }
74
75
76 void InsetBranch::write(Buffer const & buf, ostream & os) const
77 {
78         params_.write(os);
79         InsetCollapsable::write(buf, os);
80 }
81
82
83 void InsetBranch::read(Buffer const & buf, LyXLex & lex)
84 {
85         params_.read(lex);
86         InsetCollapsable::read(buf, lex);
87         setButtonLabel();
88 }
89
90
91 void InsetBranch::setButtonLabel()
92 {
93         LyXFont font(LyXFont::ALL_SANE);
94         font.decSize();
95         font.decSize();
96
97         string s = _("Branch: ") + params_.branch;
98         font.setColor(LColor::foreground);
99         if (!params_.branch.empty()) {
100                 LColor_color c = lcolor.getFromLyXName(params_.branch);
101                 if (c == LColor::none) {
102                         c = LColor::error;
103                         s = _("Undef: ") + s;
104                 }
105                 setBackgroundColor(c);
106         } else
107                 setBackgroundColor(LColor::background);
108         setLabel(isOpen() ? s : getNewLabel(s) );
109         setLabelFont(font);
110 }
111
112
113 bool InsetBranch::showInsetDialog(BufferView * bv) const
114 {
115         InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
116         return true;
117 }
118
119
120 void InsetBranch::doDispatch(LCursor & cur, FuncRequest & cmd)
121 {
122         switch (cmd.action) {
123         case LFUN_INSET_MODIFY: {
124                 InsetBranchParams params;
125                 InsetBranchMailer::string2params(cmd.argument, params);
126                 params_.branch = params.branch;
127                 setButtonLabel();
128                 break;
129         }
130
131         case LFUN_MOUSE_PRESS:
132                 if (cmd.button() != mouse_button::button3)
133                         InsetCollapsable::doDispatch(cur, cmd);
134                 else
135                         cur.undispatched();
136                 break;
137
138         case LFUN_INSET_DIALOG_UPDATE:
139                 InsetBranchMailer(*this).updateDialog(&cur.bv());
140                 break;
141
142         case LFUN_MOUSE_RELEASE:
143                 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
144                         InsetBranchMailer(*this).showDialog(&cur.bv());
145                 else
146                         InsetCollapsable::doDispatch(cur, cmd);
147                 break;
148
149
150         case LFUN_INSET_TOGGLE:
151                 if (cmd.argument == "assign" || cmd.argument.empty()) {
152                         // The branch inset uses "assign".
153                         if (isBranchSelected(cur.buffer())) {
154                                 if (status() != Open)
155                                         setStatus(cur, Open);
156                                 else
157                                         cur.undispatched();
158                         } else {
159                                 if (status() != Collapsed)
160                                         setStatus(cur, Collapsed);
161                                 else
162                                         cur.undispatched();
163                         }
164                 }
165                 else
166                         InsetCollapsable::doDispatch(cur, cmd);
167                 break;
168
169         default:
170                 InsetCollapsable::doDispatch(cur, cmd);
171                 break;
172         }
173 }
174
175
176 bool InsetBranch::getStatus(LCursor & cur, FuncRequest const & cmd,
177                 FuncStatus & flag) const
178 {
179         switch (cmd.action) {
180         case LFUN_INSET_MODIFY:
181         case LFUN_INSET_DIALOG_UPDATE:
182                 flag.enabled(true);
183                 break;
184
185         case LFUN_INSET_TOGGLE:
186                 if (cmd.argument == "open" || cmd.argument == "close" ||
187                     cmd.argument == "toggle")
188                         flag.enabled(true);
189                 else if (cmd.argument == "assign"
190                            || cmd.argument.empty()) {
191                         if (isBranchSelected(cur.buffer()))
192                                 flag.enabled(status() != Open);
193                         else
194                                 flag.enabled(status() != Collapsed);
195                 } else
196                         flag.enabled(true);
197                 break;
198
199         default:
200                 return InsetCollapsable::getStatus(cur, cmd, flag);
201         }
202         return true;
203 }
204
205
206 bool InsetBranch::isBranchSelected(Buffer const & buffer) const
207 {
208         Buffer const & realbuffer = *buffer.getMasterBuffer();
209         BranchList const & branchlist = realbuffer.params().branchlist();
210         BranchList::const_iterator const end = branchlist.end();
211         BranchList::const_iterator it =
212                 std::find_if(branchlist.begin(), end,
213                              BranchNamesEqual(params_.branch));
214         if (it == end)
215                 return false;
216         return it->getSelected();
217 }
218
219
220 int InsetBranch::latex(Buffer const & buf, ostream & os,
221                        OutputParams const & runparams) const
222 {
223         return isBranchSelected(buf) ?
224                 InsetText::latex(buf, os, runparams) : 0;
225 }
226
227
228 int InsetBranch::linuxdoc(Buffer const & buf, std::ostream & os,
229                           OutputParams const & runparams) const
230 {
231         return isBranchSelected(buf) ?
232                 InsetText::linuxdoc(buf, os, runparams) : 0;
233 }
234
235
236 int InsetBranch::docbook(Buffer const & buf, std::ostream & os,
237                          OutputParams const & runparams) const
238 {
239         return isBranchSelected(buf) ?
240                 InsetText::docbook(buf, os, runparams) : 0;
241 }
242
243
244 int InsetBranch::plaintext(Buffer const & buf, std::ostream & os,
245                            OutputParams const & runparams) const
246 {
247         return isBranchSelected(buf) ?
248                 InsetText::plaintext(buf, os, runparams): 0;
249 }
250
251
252 void InsetBranch::validate(LaTeXFeatures & features) const
253 {
254         InsetText::validate(features);
255 }
256
257
258
259 string const InsetBranchMailer::name_("branch");
260
261 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
262         : inset_(inset)
263 {}
264
265
266 string const InsetBranchMailer::inset2string(Buffer const &) const
267 {
268         return params2string(inset_.params());
269 }
270
271
272 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
273 {
274         ostringstream data;
275         data << name_ << ' ';
276         params.write(data);
277         return data.str();
278 }
279
280
281 void InsetBranchMailer::string2params(string const & in,
282                                       InsetBranchParams & params)
283 {
284         params = InsetBranchParams();
285         if (in.empty())
286                 return;
287
288         istringstream data(in);
289         LyXLex lex(0,0);
290         lex.setStream(data);
291
292         string name;
293         lex >> name;
294         if (name != name_)
295                 return print_mailer_error("InsetBranchMailer", in, 1, name_);
296
297         // This is part of the inset proper that is usually swallowed
298         // by LyXText::readInset
299         string id;
300         lex >> id;
301         if (!lex || id != "Branch")
302                 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
303
304         params.read(lex);
305 }
306
307
308 void InsetBranchParams::write(ostream & os) const
309 {
310         os << "Branch " << branch << '\n';
311 }
312
313
314 void InsetBranchParams::read(LyXLex & lex)
315 {
316         lex >> branch;
317 }