]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBranch.cpp
Fix GRAPHICS_EDIT of InsetGraphics
[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_MOUSE_RELEASE:
134                 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
135                         InsetBranchMailer(*this).showDialog(&cur.bv());
136                 else
137                         InsetCollapsable::doDispatch(cur, cmd);
138                 break;
139
140
141         case LFUN_INSET_TOGGLE:
142                 if (cmd.argument() == "assign") {
143                         // The branch inset uses "assign".
144                         if (isBranchSelected()) {
145                                 if (status() != Open)
146                                         setStatus(cur, Open);
147                                 else
148                                         cur.undispatched();
149                         } else {
150                                 if (status() != Collapsed)
151                                         setStatus(cur, Collapsed);
152                                 else
153                                         cur.undispatched();
154                         }
155                 }
156                 else
157                         InsetCollapsable::doDispatch(cur, cmd);
158                 break;
159
160         default:
161                 InsetCollapsable::doDispatch(cur, cmd);
162                 break;
163         }
164 }
165
166
167 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
168                 FuncStatus & flag) const
169 {
170         switch (cmd.action) {
171         case LFUN_INSET_MODIFY:
172         case LFUN_INSET_DIALOG_UPDATE:
173                 flag.enabled(true);
174                 break;
175
176         case LFUN_INSET_TOGGLE:
177                 if (cmd.argument() == "open" || cmd.argument() == "close" ||
178                     cmd.argument() == "toggle")
179                         flag.enabled(true);
180                 else if (cmd.argument() == "assign" || cmd.argument().empty()) {
181                         if (isBranchSelected())
182                                 flag.enabled(status() != Open);
183                         else
184                                 flag.enabled(status() != Collapsed);
185                 } else
186                         flag.enabled(true);
187                 break;
188
189         default:
190                 return InsetCollapsable::getStatus(cur, cmd, flag);
191         }
192         return true;
193 }
194
195
196 bool InsetBranch::isBranchSelected() const
197 {
198         Buffer const & realbuffer = *buffer().masterBuffer();
199         BranchList const & branchlist = realbuffer.params().branchlist();
200         BranchList::const_iterator const end = branchlist.end();
201         BranchList::const_iterator it =
202                 find_if(branchlist.begin(), end,
203                              BranchNamesEqual(params_.branch));
204         if (it == end)
205                 return false;
206         return it->getSelected();
207 }
208
209
210 void InsetBranch::updateLabels(ParIterator const & it)
211 {
212         if (isBranchSelected())
213                 InsetCollapsable::updateLabels(it);
214         else {
215                 DocumentClass const & tclass = buffer().params().documentClass();
216                 Counters savecnt = tclass.counters();
217                 InsetCollapsable::updateLabels(it);
218                 tclass.counters() = savecnt;
219         }
220 }
221
222
223 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
224 {
225         return isBranchSelected() ?  InsetText::latex(os, runparams) : 0;
226 }
227
228
229 int InsetBranch::plaintext(odocstream & os,
230                            OutputParams const & runparams) const
231 {
232         if (!isBranchSelected())
233                 return 0;
234
235         os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
236         InsetText::plaintext(os, runparams);
237         os << "\n]";
238
239         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
240 }
241
242
243 int InsetBranch::docbook(odocstream & os,
244                          OutputParams const & runparams) const
245 {
246         return isBranchSelected() ?  InsetText::docbook(os, runparams) : 0;
247 }
248
249
250 void InsetBranch::textString(odocstream & os) const
251 {
252         if (isBranchSelected())
253                 os << paragraphs().begin()->asString(true);
254 }
255
256
257 void InsetBranch::validate(LaTeXFeatures & features) const
258 {
259         InsetText::validate(features);
260 }
261
262
263 bool InsetBranch::isMacroScope() const 
264 {
265         // Its own scope if not selected by buffer
266         return !isBranchSelected();
267 }
268
269
270 string const InsetBranchMailer::name_("branch");
271
272 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
273         : inset_(inset)
274 {}
275
276
277 string const InsetBranchMailer::inset2string(Buffer const &) const
278 {
279         return params2string(inset_.params());
280 }
281
282
283 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
284 {
285         ostringstream data;
286         data << name_ << ' ';
287         params.write(data);
288         return data.str();
289 }
290
291
292 void InsetBranchMailer::string2params(string const & in,
293                                       InsetBranchParams & params)
294 {
295         params = InsetBranchParams();
296         if (in.empty())
297                 return;
298
299         istringstream data(in);
300         Lexer lex(0,0);
301         lex.setStream(data);
302
303         string name;
304         lex >> name;
305         if (name != name_)
306                 return print_mailer_error("InsetBranchMailer", in, 1, name_);
307
308         // This is part of the inset proper that is usually swallowed
309         // by Text::readInset
310         string id;
311         lex >> id;
312         if (!lex || id != "Branch")
313                 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
314
315         params.read(lex);
316 }
317
318
319 void InsetBranchParams::write(ostream & os) const
320 {
321         os << "Branch " << to_utf8(branch) << '\n';
322 }
323
324
325 void InsetBranchParams::read(Lexer & lex)
326 {
327         lex >> branch;
328 }
329
330 } // namespace lyx