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