]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBranch.cpp
Ask for saving changes when branch state in child was altered
[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 "LyX.h"
27 #include "OutputParams.h"
28 #include "output_xhtml.h"
29 #include "TextClass.h"
30 #include "TocBackend.h"
31
32 #include "support/debug.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
35
36 #include "frontends/alert.h"
37 #include "frontends/Application.h"
38
39 #include <sstream>
40
41 using namespace std;
42
43
44 namespace lyx {
45
46 InsetBranch::InsetBranch(Buffer * buf, InsetBranchParams const & params)
47         : InsetCollapsable(buf, InsetText::DefaultLayout), params_(params)
48 {}
49
50
51 void InsetBranch::write(ostream & os) const
52 {
53         os << "Branch ";
54         params_.write(os);
55         os << '\n';
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 & bv, int, int) const
68 {
69         docstring const masterstatus = isBranchSelected() ?
70                 _("active") : _("non-active");
71         docstring const childstatus = isBranchSelected(true) ?
72                 _("active") : _("non-active");
73         docstring const status = (masterstatus == childstatus) ?
74                 masterstatus :
75                 support::bformat(_("master: %1$s, child: %2$s"),
76                                                  masterstatus, childstatus);
77         docstring const heading = 
78                 support::bformat(_("Branch (%1$s): %2$s"), status, params_.branch);
79         if (isOpen(bv))
80                 return heading;
81         return toolTipText(heading + from_ascii("\n"));
82 }
83
84
85 docstring const InsetBranch::buttonLabel(BufferView const & bv) const
86 {
87         docstring s = _("Branch: ") + params_.branch;
88         Buffer const & realbuffer = *buffer().masterBuffer();
89         BranchList const & branchlist = realbuffer.params().branchlist();
90         bool const inmaster = branchlist.find(params_.branch);
91         bool const inchild = buffer().params().branchlist().find(params_.branch);
92         if (!inmaster && inchild)
93                 s = _("Branch (child only): ") + params_.branch;
94         else if (inmaster && !inchild)
95                 s = _("Branch (master only): ") + params_.branch;
96         else if (!inmaster)
97                 s = _("Branch (undefined): ") + params_.branch;
98         if (!params_.branch.empty()) {
99                 // FIXME UNICODE
100                 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
101                 if (c == Color_none)
102                         s = _("Undef: ") + s;
103         }
104         bool const master_selected = isBranchSelected();
105         bool const child_selected = isBranchSelected(true);
106         docstring symb = docstring(1, char_type(master_selected ? 0x2714 : 0x2716));
107         if (inchild && master_selected != child_selected)
108                 symb += char_type(child_selected ? 0x2714 : 0x2716);
109         s = symb + s;
110         if (decoration() == InsetLayout::CLASSIC)
111                 return isOpen(bv) ? s : getNewLabel(s);
112         else
113                 return params_.branch + ": " + getNewLabel(s);
114 }
115
116
117 ColorCode InsetBranch::backgroundColor(PainterInfo const & pi) const
118 {
119         if (params_.branch.empty())
120                 return Inset::backgroundColor(pi);
121         // FIXME UNICODE
122         ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
123         if (c == Color_none)
124                 c = Color_error;
125         return c;
126 }
127
128
129 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
130 {
131         switch (cmd.action()) {
132         case LFUN_INSET_MODIFY: {
133                 InsetBranchParams params;
134                 InsetBranch::string2params(to_utf8(cmd.argument()), params);
135
136                 cur.recordUndoInset(ATOMIC_UNDO, this);
137                 params_.branch = params.branch;
138                 // what we really want here is a TOC update, but that means
139                 // a full buffer update
140                 cur.forceBufferUpdate();
141                 break;
142         }
143         case LFUN_BRANCH_ACTIVATE:
144         case LFUN_BRANCH_DEACTIVATE:
145         case LFUN_BRANCH_MASTER_ACTIVATE:
146         case LFUN_BRANCH_MASTER_DEACTIVATE: {
147                 bool const master = (cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE
148                                      || cmd.action() == LFUN_BRANCH_MASTER_DEACTIVATE);
149                 Buffer * buf = master ? const_cast<Buffer *>(buffer().masterBuffer())
150                                       : &buffer();
151
152                 Branch * our_branch = buf->params().branchlist().find(params_.branch);
153                 if (!our_branch)
154                         break;
155
156                 bool const activate = (cmd.action() == LFUN_BRANCH_ACTIVATE
157                                        || cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE);
158                 if (our_branch->isSelected() != activate) {
159                         // FIXME If the branch is in the master document, we cannot
160                         // call recordUndo..., because the master may be hidden, and
161                         // the code presently assumes that hidden documents can never
162                         // be dirty. See GuiView::closeBufferAll(), for example.
163                         // An option would be to check if the master is hidden.
164                         // If it is, unhide.
165                         if (!master)
166                                 buffer().undo().recordUndoFullDocument(cur);
167                         else
168                                 // at least issue a warning for now (ugly, but better than dataloss).
169                                 frontend::Alert::warning(_("Branch state changes in master document"),
170                                     lyx::support::bformat(_("The state of the branch '%1$s' "
171                                         "was changed in the master file. "
172                                         "Please make sure to save the master."), params_.branch), true);
173                         our_branch->setSelected(activate);
174                         // cur.forceBufferUpdate() is not enough
175                         buf->updateBuffer();
176                 }
177                 break;
178         }
179         case LFUN_BRANCH_ADD:
180                 lyx::dispatch(FuncRequest(LFUN_BRANCH_ADD, params_.branch));
181                 break;
182         case LFUN_INSET_TOGGLE:
183                 if (cmd.argument() == "assign")
184                         setStatus(cur, isBranchSelected() ? Open : Collapsed);
185                 else
186                         InsetCollapsable::doDispatch(cur, cmd);
187                 break;
188
189         default:
190                 InsetCollapsable::doDispatch(cur, cmd);
191                 break;
192         }
193 }
194
195
196 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
197                 FuncStatus & flag) const
198 {
199         bool const known_branch =
200                 buffer().params().branchlist().find(params_.branch);
201
202         switch (cmd.action()) {
203         case LFUN_INSET_MODIFY:
204                 flag.setEnabled(true);
205                 break;
206
207         case LFUN_BRANCH_ACTIVATE:
208                 flag.setEnabled(known_branch && !isBranchSelected(true));
209                 break;
210
211         case LFUN_BRANCH_ADD:
212                 flag.setEnabled(!known_branch);
213                 break;
214
215         case LFUN_BRANCH_DEACTIVATE:
216                 flag.setEnabled(isBranchSelected(true));
217                 break;
218
219         case LFUN_BRANCH_MASTER_ACTIVATE:
220                 flag.setEnabled(buffer().parent()
221                                 && buffer().masterBuffer()->params().branchlist().find(params_.branch)
222                                 && !isBranchSelected());
223                 break;
224
225         case LFUN_BRANCH_MASTER_DEACTIVATE:
226                 flag.setEnabled(buffer().parent() && isBranchSelected());
227                 break;
228
229         case LFUN_INSET_TOGGLE:
230                 if (cmd.argument() == "assign")
231                         flag.setEnabled(true);
232                 else
233                         return InsetCollapsable::getStatus(cur, cmd, flag);     
234                 break;
235
236         default:
237                 return InsetCollapsable::getStatus(cur, cmd, flag);
238         }
239         return true;
240 }
241
242
243 bool InsetBranch::isBranchSelected(bool const child) const
244 {
245         Buffer const & realbuffer = child ? buffer() : *buffer().masterBuffer();
246         BranchList const & branchlist = realbuffer.params().branchlist();
247         Branch const * ourBranch = branchlist.find(params_.branch);
248
249         if (!ourBranch) {
250                 // this branch is defined in child only
251                 ourBranch = buffer().params().branchlist().find(params_.branch);
252                 if (!ourBranch)
253                         return false;
254         }
255         return ourBranch->isSelected();
256 }
257
258
259 void InsetBranch::latex(otexstream & os, OutputParams const & runparams) const
260 {
261         if (isBranchSelected())
262                 InsetText::latex(os, runparams);
263 }
264
265
266 int InsetBranch::plaintext(odocstream & os,
267                            OutputParams const & runparams) const
268 {
269         if (!isBranchSelected())
270                 return 0;
271
272         int len = InsetText::plaintext(os, runparams);
273         return len;
274 }
275
276
277 int InsetBranch::docbook(odocstream & os,
278                          OutputParams const & runparams) const
279 {
280         return isBranchSelected() ?  InsetText::docbook(os, runparams) : 0;
281 }
282
283
284 docstring InsetBranch::xhtml(XHTMLStream & xs, OutputParams const & rp) const
285 {
286         if (isBranchSelected()) {
287                 OutputParams newrp = rp;
288                 newrp.par_begin = 0;
289                 newrp.par_end = text().paragraphs().size();
290                 xhtmlParagraphs(text(), buffer(), xs, newrp);
291         }
292         return docstring();
293 }
294
295
296 void InsetBranch::toString(odocstream & os) const
297 {
298         if (isBranchSelected())
299                 InsetCollapsable::toString(os);
300 }
301
302
303 void InsetBranch::forToc(docstring & os, size_t maxlen) const
304 {
305         if (isBranchSelected())
306                 InsetCollapsable::forToc(os, maxlen);
307 }
308
309
310 void InsetBranch::validate(LaTeXFeatures & features) const
311 {
312         if (isBranchSelected())
313                 InsetCollapsable::validate(features);
314 }
315
316
317 string InsetBranch::contextMenuName() const
318 {
319         return "context-branch";
320 }
321
322
323 bool InsetBranch::isMacroScope() const 
324 {
325         // Its own scope if not selected by buffer
326         return !isBranchSelected();
327 }
328
329
330 string InsetBranch::params2string(InsetBranchParams const & params)
331 {
332         ostringstream data;
333         params.write(data);
334         return data.str();
335 }
336
337
338 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
339 {
340         params = InsetBranchParams();
341         if (in.empty())
342                 return;
343
344         istringstream data(in);
345         Lexer lex;
346         lex.setStream(data);
347         lex.setContext("InsetBranch::string2params");
348         params.read(lex);
349 }
350
351
352 void InsetBranch::addToToc(DocIterator const & cpit) const
353 {
354         DocIterator pit = cpit;
355         pit.push_back(CursorSlice(const_cast<InsetBranch &>(*this)));
356
357         Toc & toc = buffer().tocBackend().toc("branch");
358         docstring str = params_.branch + ": ";
359         text().forToc(str, TOC_ENTRY_LENGTH);
360         toc.push_back(TocItem(pit, 0, str, toolTipText(docstring(), 3, 60)));
361         // Proceed with the rest of the inset.
362         InsetCollapsable::addToToc(cpit);
363 }
364
365
366 void InsetBranchParams::write(ostream & os) const
367 {
368         os << to_utf8(branch);
369 }
370
371
372 void InsetBranchParams::read(Lexer & lex)
373 {
374         lex.eatLine();
375         branch = lex.getDocString();
376 }
377
378 } // namespace lyx