]> git.lyx.org Git - lyx.git/blob - src/insets/insetbranch.C
bug 2092: branches not propagated to child documents
[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         setLabel(isOpen() ? s : getNewLabel(s) );
99         font.setColor(LColor::foreground);
100         if (!params_.branch.empty())
101                 setBackgroundColor(lcolor.getFromLyXName(params_.branch));
102         else
103                 setBackgroundColor(LColor::background);
104         setLabelFont(font);
105 }
106
107
108 bool InsetBranch::showInsetDialog(BufferView * bv) const
109 {
110         InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
111         return true;
112 }
113
114
115 void InsetBranch::doDispatch(LCursor & cur, FuncRequest & cmd)
116 {
117         switch (cmd.action) {
118         case LFUN_INSET_MODIFY: {
119                 InsetBranchParams params;
120                 InsetBranchMailer::string2params(cmd.argument, params);
121                 params_.branch = params.branch;
122                 setButtonLabel();
123                 break;
124         }
125
126         case LFUN_MOUSE_PRESS:
127                 if (cmd.button() != mouse_button::button3)
128                         InsetCollapsable::doDispatch(cur, cmd);
129                 else
130                         cur.undispatched();
131                 break;
132
133         case LFUN_INSET_DIALOG_UPDATE:
134                 InsetBranchMailer(*this).updateDialog(&cur.bv());
135                 break;
136
137         case LFUN_MOUSE_RELEASE:
138                 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
139                         InsetBranchMailer(*this).showDialog(&cur.bv());
140                 else
141                         InsetCollapsable::doDispatch(cur, cmd);
142                 break;
143
144
145         case LFUN_INSET_TOGGLE:
146                 if (cmd.argument == "assign" || cmd.argument.empty()) {
147                         // The branch inset uses "assign".
148                         if (isBranchSelected(cur.buffer())) {
149                                 if (status() != Open)
150                                         setStatus(cur, Open);
151                                 else
152                                         cur.undispatched();
153                         } else {
154                                 if (status() != Collapsed)
155                                         setStatus(cur, Collapsed);
156                                 else
157                                         cur.undispatched();
158                         }
159                 }
160                 else
161                         InsetCollapsable::doDispatch(cur, cmd);
162                 break;
163
164         default:
165                 InsetCollapsable::doDispatch(cur, cmd);
166                 break;
167         }
168 }
169
170
171 bool InsetBranch::getStatus(LCursor & cur, FuncRequest const & cmd,
172                 FuncStatus & flag) const
173 {
174         switch (cmd.action) {
175         case LFUN_INSET_MODIFY:
176         case LFUN_INSET_DIALOG_UPDATE:
177                 flag.enabled(true);
178                 break;
179
180         case LFUN_INSET_TOGGLE:
181                 if (cmd.argument == "open" || cmd.argument == "close" ||
182                     cmd.argument == "toggle")
183                         flag.enabled(true);
184                 else if (cmd.argument == "assign"
185                            || cmd.argument.empty()) {
186                         if (isBranchSelected(cur.buffer()))
187                                 flag.enabled(status() != Open);
188                         else
189                                 flag.enabled(status() != Collapsed);
190                 } else
191                         flag.enabled(true);
192                 break;
193
194         default:
195                 return InsetCollapsable::getStatus(cur, cmd, flag);
196         }
197         return true;
198 }
199
200
201 bool InsetBranch::isBranchSelected(Buffer const & buffer) const
202 {
203         Buffer const & realbuffer = *buffer.getMasterBuffer();
204         BranchList const & branchlist = realbuffer.params().branchlist();
205         BranchList::const_iterator const end = branchlist.end();
206         BranchList::const_iterator it =
207                 std::find_if(branchlist.begin(), end,
208                              BranchNamesEqual(params_.branch));
209         if (it == end)
210                 return false;
211         return it->getSelected();
212 }
213
214
215 int InsetBranch::latex(Buffer const & buf, ostream & os,
216                        OutputParams const & runparams) const
217 {
218         return isBranchSelected(buf) ?
219                 InsetText::latex(buf, os, runparams) : 0;
220 }
221
222
223 int InsetBranch::linuxdoc(Buffer const & buf, std::ostream & os,
224                           OutputParams const & runparams) const
225 {
226         return isBranchSelected(buf) ?
227                 InsetText::linuxdoc(buf, os, runparams) : 0;
228 }
229
230
231 int InsetBranch::docbook(Buffer const & buf, std::ostream & os,
232                          OutputParams const & runparams) const
233 {
234         return isBranchSelected(buf) ?
235                 InsetText::docbook(buf, os, runparams) : 0;
236 }
237
238
239 int InsetBranch::plaintext(Buffer const & buf, std::ostream & os,
240                            OutputParams const & runparams) const
241 {
242         return isBranchSelected(buf) ?
243                 InsetText::plaintext(buf, os, runparams): 0;
244 }
245
246
247 void InsetBranch::validate(LaTeXFeatures & features) const
248 {
249         InsetText::validate(features);
250 }
251
252
253
254 string const InsetBranchMailer::name_("branch");
255
256 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
257         : inset_(inset)
258 {}
259
260
261 string const InsetBranchMailer::inset2string(Buffer const &) const
262 {
263         return params2string(inset_.params());
264 }
265
266
267 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
268 {
269         ostringstream data;
270         data << name_ << ' ';
271         params.write(data);
272         return data.str();
273 }
274
275
276 void InsetBranchMailer::string2params(string const & in,
277                                       InsetBranchParams & params)
278 {
279         params = InsetBranchParams();
280         if (in.empty())
281                 return;
282
283         istringstream data(in);
284         LyXLex lex(0,0);
285         lex.setStream(data);
286
287         string name;
288         lex >> name;
289         if (name != name_)
290                 return print_mailer_error("InsetBranchMailer", in, 1, name_);
291
292         // This is part of the inset proper that is usually swallowed
293         // by LyXText::readInset
294         string id;
295         lex >> id;
296         if (!lex || id != "Branch")
297                 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
298
299         params.read(lex);
300 }
301
302
303 void InsetBranchParams::write(ostream & os) const
304 {
305         os << "Branch " << branch << '\n';
306 }
307
308
309 void InsetBranchParams::read(LyXLex & lex)
310 {
311         lex >> branch;
312 }