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