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