]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBranch.cpp
Require a buffer on construction of InsetGraphics and InsetExternal. Eventually,...
[lyx.git] / src / insets / InsetBranch.cpp
1 /**
2  * \file InsetBranch.cpp
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 "Color.h"
19 #include "Counters.h"
20 #include "Cursor.h"
21 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "support/gettext.h"
25 #include "Lexer.h"
26 #include "OutputParams.h"
27 #include "TextClass.h"
28
29 #include <sstream>
30
31 using namespace std;
32
33 namespace lyx {
34
35
36 InsetBranch::InsetBranch(Buffer const & buf, InsetBranchParams const & params)
37         : InsetCollapsable(buf), params_(params)
38 {}
39
40
41 InsetBranch::~InsetBranch()
42 {
43         InsetBranchMailer(*this).hideDialog();
44 }
45
46
47 docstring InsetBranch::editMessage() const
48 {
49         return _("Opened Branch Inset");
50 }
51
52
53 void InsetBranch::write(ostream & os) const
54 {
55         params_.write(os);
56         InsetCollapsable::write(os);
57 }
58
59
60 void InsetBranch::read(Lexer & lex)
61 {
62         params_.read(lex);
63         InsetCollapsable::read(lex);
64 }
65
66
67 docstring InsetBranch::toolTip(BufferView const &, int, int) const
68 {
69         return _("Branch: ") + params_.branch;
70 }
71
72
73 void InsetBranch::setButtonLabel()
74 {
75         docstring s = _("Branch: ") + params_.branch;
76         if (!params_.branch.empty()) {
77                 // FIXME UNICODE
78                 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
79                 if (c == Color_none) {
80                         s = _("Undef: ") + s;
81                 }
82         }
83         if (decoration() == InsetLayout::Classic)
84                 setLabel(isOpen() ? s : getNewLabel(s) );
85         else
86                 setLabel(params_.branch + ": " + getNewLabel(s));
87 }
88
89
90 ColorCode InsetBranch::backgroundColor() const
91 {
92         if (!params_.branch.empty()) {
93                 // FIXME UNICODE
94                 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
95                 if (c == Color_none) {
96                         c = Color_error;
97                 }
98                 return c;
99         } else
100                 return Inset::backgroundColor();
101 }
102
103
104 bool InsetBranch::showInsetDialog(BufferView * bv) const
105 {
106         InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
107         return true;
108 }
109
110
111 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
112 {
113         switch (cmd.action) {
114         case LFUN_INSET_MODIFY: {
115                 InsetBranchParams params;
116                 InsetBranchMailer::string2params(to_utf8(cmd.argument()), params);
117                 params_.branch = params.branch;
118                 setLayout(cur.buffer().params());
119                 break;
120         }
121
122         case LFUN_MOUSE_PRESS:
123                 if (cmd.button() != mouse_button::button3)
124                         InsetCollapsable::doDispatch(cur, cmd);
125                 else
126                         cur.undispatched();
127                 break;
128
129         case LFUN_INSET_DIALOG_UPDATE:
130                 InsetBranchMailer(*this).updateDialog(&cur.bv());
131                 break;
132
133         case LFUN_INSET_TOGGLE:
134                 if (cmd.argument() == "assign") {
135                         // The branch inset uses "assign".
136                         if (isBranchSelected()) {
137                                 if (status() != Open)
138                                         setStatus(cur, Open);
139                                 else
140                                         cur.undispatched();
141                         } else {
142                                 if (status() != Collapsed)
143                                         setStatus(cur, Collapsed);
144                                 else
145                                         cur.undispatched();
146                         }
147                 }
148                 else
149                         InsetCollapsable::doDispatch(cur, cmd);
150                 break;
151
152         default:
153                 InsetCollapsable::doDispatch(cur, cmd);
154                 break;
155         }
156 }
157
158
159 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
160                 FuncStatus & flag) const
161 {
162         switch (cmd.action) {
163         case LFUN_INSET_MODIFY:
164         case LFUN_INSET_DIALOG_UPDATE:
165                 flag.enabled(true);
166                 break;
167
168         case LFUN_INSET_TOGGLE:
169                 if (cmd.argument() == "open" || cmd.argument() == "close" ||
170                     cmd.argument() == "toggle")
171                         flag.enabled(true);
172                 else if (cmd.argument() == "assign" || cmd.argument().empty()) {
173                         if (isBranchSelected())
174                                 flag.enabled(status() != Open);
175                         else
176                                 flag.enabled(status() != Collapsed);
177                 } else
178                         flag.enabled(true);
179                 break;
180
181         default:
182                 return InsetCollapsable::getStatus(cur, cmd, flag);
183         }
184         return true;
185 }
186
187
188 bool InsetBranch::isBranchSelected() const
189 {
190         Buffer const & realbuffer = *buffer().masterBuffer();
191         BranchList const & branchlist = realbuffer.params().branchlist();
192         BranchList::const_iterator const end = branchlist.end();
193         BranchList::const_iterator it =
194                 find_if(branchlist.begin(), end,
195                              BranchNamesEqual(params_.branch));
196         if (it == end)
197                 return false;
198         return it->getSelected();
199 }
200
201
202 void InsetBranch::updateLabels(ParIterator const & it)
203 {
204         if (isBranchSelected())
205                 InsetCollapsable::updateLabels(it);
206         else {
207                 DocumentClass const & tclass = buffer().params().documentClass();
208                 Counters savecnt = tclass.counters();
209                 InsetCollapsable::updateLabels(it);
210                 tclass.counters() = savecnt;
211         }
212 }
213
214
215 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
216 {
217         return isBranchSelected() ?  InsetText::latex(os, runparams) : 0;
218 }
219
220
221 int InsetBranch::plaintext(odocstream & os,
222                            OutputParams const & runparams) const
223 {
224         if (!isBranchSelected())
225                 return 0;
226
227         os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
228         InsetText::plaintext(os, runparams);
229         os << "\n]";
230
231         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
232 }
233
234
235 int InsetBranch::docbook(odocstream & os,
236                          OutputParams const & runparams) const
237 {
238         return isBranchSelected() ?  InsetText::docbook(os, runparams) : 0;
239 }
240
241
242 void InsetBranch::textString(odocstream & os) const
243 {
244         if (isBranchSelected())
245                 os << paragraphs().begin()->asString(true);
246 }
247
248
249 void InsetBranch::validate(LaTeXFeatures & features) const
250 {
251         InsetText::validate(features);
252 }
253
254
255 bool InsetBranch::isMacroScope() const 
256 {
257         // Its own scope if not selected by buffer
258         return !isBranchSelected();
259 }
260
261
262 string const InsetBranchMailer::name_("branch");
263
264 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
265         : inset_(inset)
266 {}
267
268
269 string const InsetBranchMailer::inset2string(Buffer const &) const
270 {
271         return params2string(inset_.params());
272 }
273
274
275 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
276 {
277         ostringstream data;
278         data << name_ << ' ';
279         params.write(data);
280         return data.str();
281 }
282
283
284 void InsetBranchMailer::string2params(string const & in,
285                                       InsetBranchParams & params)
286 {
287         params = InsetBranchParams();
288         if (in.empty())
289                 return;
290
291         istringstream data(in);
292         Lexer lex(0,0);
293         lex.setStream(data);
294
295         string name;
296         lex >> name;
297         if (name != name_)
298                 return print_mailer_error("InsetBranchMailer", in, 1, name_);
299
300         // This is part of the inset proper that is usually swallowed
301         // by Text::readInset
302         string id;
303         lex >> id;
304         if (!lex || id != "Branch")
305                 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
306
307         params.read(lex);
308 }
309
310
311 void InsetBranchParams::write(ostream & os) const
312 {
313         os << "Branch " << to_utf8(branch) << '\n';
314 }
315
316
317 void InsetBranchParams::read(Lexer & lex)
318 {
319         lex >> branch;
320 }
321
322 } // namespace lyx