]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBranch.cpp
Manage the initial setting of Default or Plain Layout more simply.
[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 "ColorSet.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, InsetText::DefaultLayout), 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 docstring const InsetBranch::buttonLabel(BufferView const & bv) const
80 {
81         docstring s = _("Branch: ") + params_.branch;
82         Buffer const & realbuffer = *buffer().masterBuffer();
83         BranchList const & branchlist = realbuffer.params().branchlist();
84         if (!branchlist.find(params_.branch))
85                 s = _("Branch (child only): ") + params_.branch;
86         if (!params_.branch.empty()) {
87                 // FIXME UNICODE
88                 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
89                 if (c == Color_none)
90                         s = _("Undef: ") + s;
91         }
92         s = char_type(isBranchSelected() ? 0x2714 : 0x2716) + s;
93         if (decoration() == InsetLayout::CLASSIC)
94                 return isOpen(bv) ? s : getNewLabel(s);
95         else
96                 return params_.branch + ": " + getNewLabel(s);
97 }
98
99
100 ColorCode InsetBranch::backgroundColor() const
101 {
102         if (params_.branch.empty())
103                 return Inset::backgroundColor();
104         // FIXME UNICODE
105         ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
106         if (c == Color_none)
107                 c = Color_error;
108         return c;
109 }
110
111
112 bool InsetBranch::showInsetDialog(BufferView * bv) const
113 {
114         bv->showDialog("branch", params2string(params()),
115                         const_cast<InsetBranch *>(this));
116         return true;
117 }
118
119
120 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
121 {
122         switch (cmd.action) {
123         case LFUN_INSET_MODIFY: {
124                 InsetBranchParams params;
125                 InsetBranch::string2params(to_utf8(cmd.argument()), params);
126                 params_.branch = params.branch;
127                 setLayout(cur.buffer()->params());
128                 break;
129         }
130
131         case LFUN_MOUSE_PRESS:
132                 if (cmd.button() != mouse_button::button3)
133                         InsetCollapsable::doDispatch(cur, cmd);
134                 else
135                         cur.undispatched();
136                 break;
137
138         case LFUN_INSET_DIALOG_UPDATE:
139                 cur.bv().updateDialog("branch", params2string(params()));
140                 break;
141
142         case LFUN_BRANCH_ACTIVATE:
143         case LFUN_BRANCH_DEACTIVATE: {
144                 // FIXME: I do not like this cast, but have no other idea...
145                 Buffer * realbuffer = const_cast<Buffer *>(buffer().masterBuffer());
146                 BranchList & branchlist = realbuffer->params().branchlist();
147                 Branch * ourBranch = branchlist.find(params_.branch);
148                 if (!ourBranch)
149                         break;
150                 ourBranch->setSelected(cmd.action == LFUN_BRANCH_ACTIVATE);
151                 break;
152         }
153
154         case LFUN_INSET_TOGGLE:
155                 if (cmd.argument() == "assign")
156                         setStatus(cur, isBranchSelected() ? Open : Collapsed);
157                 else
158                         InsetCollapsable::doDispatch(cur, cmd);
159                 break;
160
161         default:
162                 InsetCollapsable::doDispatch(cur, cmd);
163                 break;
164         }
165 }
166
167
168 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
169                 FuncStatus & flag) const
170 {
171         switch (cmd.action) {
172         case LFUN_INSET_MODIFY:
173         case LFUN_INSET_DIALOG_UPDATE:
174                 flag.setEnabled(true);
175                 break;
176
177         case LFUN_BRANCH_ACTIVATE:
178                 flag.setEnabled(!isBranchSelected());
179                 break;
180
181         case LFUN_BRANCH_DEACTIVATE:
182                 flag.setEnabled(isBranchSelected());
183                 break;
184
185         case LFUN_INSET_TOGGLE:
186                 if (cmd.argument() == "assign")
187                         flag.setEnabled(true);
188                 else
189                         return InsetCollapsable::getStatus(cur, cmd, flag);     
190
191         default:
192                 return InsetCollapsable::getStatus(cur, cmd, flag);
193         }
194         return true;
195 }
196
197
198 bool InsetBranch::isBranchSelected() const
199 {
200         Buffer const & realbuffer = *buffer().masterBuffer();
201         BranchList const & branchlist = realbuffer.params().branchlist();
202         Branch const * ourBranch = branchlist.find(params_.branch);
203         if (!ourBranch)
204                 return false;
205         return ourBranch->isSelected();
206 }
207
208
209 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
210 {
211         return isBranchSelected() ?  InsetText::latex(os, runparams) : 0;
212 }
213
214
215 int InsetBranch::plaintext(odocstream & os,
216                            OutputParams const & runparams) const
217 {
218         if (!isBranchSelected())
219                 return 0;
220
221         os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
222         InsetText::plaintext(os, runparams);
223         os << "\n]";
224
225         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
226 }
227
228
229 int InsetBranch::docbook(odocstream & os,
230                          OutputParams const & runparams) const
231 {
232         return isBranchSelected() ?  InsetText::docbook(os, runparams) : 0;
233 }
234
235
236 void InsetBranch::tocString(odocstream & os) const
237 {
238         if (isBranchSelected())
239                 InsetCollapsable::tocString(os);
240 }
241
242
243 void InsetBranch::validate(LaTeXFeatures & features) const
244 {
245         if (isBranchSelected())
246                 InsetCollapsable::validate(features);
247 }
248
249
250 docstring InsetBranch::contextMenu(BufferView const &, int, int) const
251 {
252         return from_ascii("context-branch");
253 }
254
255
256 bool InsetBranch::isMacroScope() const 
257 {
258         // Its own scope if not selected by buffer
259         return !isBranchSelected();
260 }
261
262
263 string InsetBranch::params2string(InsetBranchParams const & params)
264 {
265         ostringstream data;
266         data << "branch" << ' ';
267         params.write(data);
268         return data.str();
269 }
270
271
272 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
273 {
274         params = InsetBranchParams();
275         if (in.empty())
276                 return;
277
278         istringstream data(in);
279         Lexer lex;
280         lex.setStream(data);
281         lex.setContext("InsetBranch::string2params");
282         lex >> "branch" >> "Branch";
283         params.read(lex);
284 }
285
286
287 void InsetBranch::addToToc(DocIterator const & cpit)
288 {
289         DocIterator pit = cpit;
290         pit.push_back(CursorSlice(*this));
291
292         Toc & toc = buffer().tocBackend().toc("branch");
293         docstring const str = params_.branch + ": " + text().getPar(0).asString();
294         toc.push_back(TocItem(pit, 0, str));
295         // Proceed with the rest of the inset.
296         InsetCollapsable::addToToc(cpit);
297 }
298
299
300 void InsetBranchParams::write(ostream & os) const
301 {
302         os << "Branch " << to_utf8(branch) << '\n';
303 }
304
305
306 void InsetBranchParams::read(Lexer & lex)
307 {
308         lex.eatLine();
309         branch = lex.getDocString();
310 }
311
312 } // namespace lyx