]> git.lyx.org Git - features.git/blob - src/insets/insetbranch.C
A slightly buggy lfun all-insets-toggle.
[features.git] / src / insets / insetbranch.C
1 /**
2  * \file insetbranch.C
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 "BufferView.h"
17 #include "bufferparams.h"
18 #include "BranchList.h"
19 #include "cursor.h"
20 #include "dispatchresult.h"
21 #include "funcrequest.h"
22 #include "gettext.h"
23 #include "LColor.h"
24 #include "lyxlex.h"
25 #include "paragraph.h"
26
27 #include "support/std_sstream.h"
28
29 using std::string;
30 using std::auto_ptr;
31 using std::istringstream;
32 using std::ostream;
33 using std::ostringstream;
34
35
36 void InsetBranch::init()
37 {
38         setInsetName("Branch");
39         setButtonLabel();
40 }
41
42
43 InsetBranch::InsetBranch(BufferParams const & bp,
44                          InsetBranchParams const & params)
45         : InsetCollapsable(bp), params_(params)
46 {
47         init();
48 }
49
50
51 InsetBranch::InsetBranch(InsetBranch const & in)
52         : InsetCollapsable(in), params_(in.params_)
53 {
54         init();
55 }
56
57
58 InsetBranch::~InsetBranch()
59 {
60         InsetBranchMailer(*this).hideDialog();
61 }
62
63
64 auto_ptr<InsetBase> InsetBranch::clone() const
65 {
66         return auto_ptr<InsetBase>(new InsetBranch(*this));
67 }
68
69
70 string const InsetBranch::editMessage() const
71 {
72         return _("Opened Branch Inset");
73 }
74
75
76 void InsetBranch::write(Buffer const & buf, ostream & os) const
77 {
78         params_.write(os);
79         InsetCollapsable::write(buf, os);
80 }
81
82
83 void InsetBranch::read(Buffer const & buf, LyXLex & lex)
84 {
85         params_.read(lex);
86         InsetCollapsable::read(buf, lex);
87         setButtonLabel();
88 }
89
90
91 void InsetBranch::setButtonLabel()
92 {
93         LyXFont font(LyXFont::ALL_SANE);
94         font.decSize();
95         font.decSize();
96
97         setLabel("Branch: " + params_.branch);
98         font.setColor(LColor::foreground);
99         if (!params_.branch.empty())
100                 setBackgroundColor(lcolor.getFromLyXName(params_.branch));
101         else
102                 setBackgroundColor(LColor::background);
103         setLabelFont(font);
104 }
105
106
107 bool InsetBranch::showInsetDialog(BufferView * bv) const
108 {
109         InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
110         return true;
111 }
112
113
114 void InsetBranch::priv_dispatch(LCursor & cur, FuncRequest & cmd)
115 {
116         switch (cmd.action) {
117         case LFUN_INSET_MODIFY: {
118                 InsetBranchParams params;
119                 InsetBranchMailer::string2params(cmd.argument, params);
120                 params_.branch = params.branch;
121                 setButtonLabel();
122                 break;
123         }
124
125         case LFUN_MOUSE_PRESS:
126                 if (cmd.button() != mouse_button::button3)
127                         InsetCollapsable::priv_dispatch(cur, cmd);
128                 else
129                         cur.undispatched();
130                 break;
131
132         case LFUN_INSET_DIALOG_UPDATE:
133                 InsetBranchMailer(*this).updateDialog(&cur.bv());
134                 break;
135
136         case LFUN_MOUSE_RELEASE:
137                 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
138                         InsetBranchMailer(*this).showDialog(&cur.bv());
139                 else
140                         InsetCollapsable::priv_dispatch(cur, cmd);
141                 break;
142
143
144         case LFUN_INSET_TOGGLE:
145                 // We assume that this lfun is indeed going to be dispatched.
146                 cur.dispatched();
147
148                 if (cmd.argument == "open")
149                         setStatus(Open);
150                 else if (cmd.argument == "close")
151                         setStatus(Collapsed);
152
153                 // The branch inset specialises its behaviour on "toggle".
154                 else if (cmd.argument == "toggle"
155                          || cmd.argument.empty()) {
156                         BranchList const & branchlist =
157                                 cur.bv().buffer()->params().branchlist();
158                         if (isBranchSelected(branchlist)) {
159                                 if (status() != Open)
160                                         setStatus(Open);
161                                 else
162                                         cur.undispatched();
163                         } else {
164                                 if (status() != Collapsed)
165                                         setStatus(Collapsed);
166                                 else
167                                         cur.undispatched();
168                         }
169                 }
170                 break;
171
172         default:
173                 InsetCollapsable::priv_dispatch(cur, cmd);
174                 break;
175         }
176 }
177
178
179 bool InsetBranch::isBranchSelected(BranchList const & branchlist) const
180 {
181         BranchList::const_iterator const end = branchlist.end();
182         BranchList::const_iterator it =
183                 std::find_if(branchlist.begin(), end,
184                              BranchNamesEqual(params_.branch));
185         if (it == end)
186                 return false;
187         return it->getSelected();
188 }
189
190
191 int InsetBranch::latex(Buffer const & buf, ostream & os,
192                        OutputParams const & runparams) const
193 {
194         return isBranchSelected(buf.params().branchlist()) ?
195                 InsetText::latex(buf, os, runparams) : 0;
196 }
197
198
199 int InsetBranch::linuxdoc(Buffer const & buf, std::ostream & os,
200                           OutputParams const & runparams) const
201 {
202         return isBranchSelected(buf.params().branchlist()) ?
203                 InsetText::linuxdoc(buf, os, runparams) : 0;
204 }
205
206
207 int InsetBranch::docbook(Buffer const & buf, std::ostream & os,
208                          OutputParams const & runparams) const
209 {
210         return isBranchSelected(buf.params().branchlist()) ?
211                 InsetText::docbook(buf, os, runparams) : 0;
212 }
213
214
215 int InsetBranch::plaintext(Buffer const & buf, std::ostream & os,
216                            OutputParams const & runparams) const
217 {
218         return isBranchSelected(buf.params().branchlist()) ?
219                 InsetText::plaintext(buf, os, runparams): 0;
220 }
221
222
223 void InsetBranch::validate(LaTeXFeatures & features) const
224 {
225         InsetText::validate(features);
226 }
227
228
229
230 string const InsetBranchMailer::name_("branch");
231
232 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
233         : inset_(inset)
234 {}
235
236
237 string const InsetBranchMailer::inset2string(Buffer const &) const
238 {
239         return params2string(inset_.params());
240 }
241
242
243 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
244 {
245         ostringstream data;
246         data << name_ << ' ';
247         params.write(data);
248         return data.str();
249 }
250
251
252 void InsetBranchMailer::string2params(string const & in,
253                                       InsetBranchParams & params)
254 {
255         params = InsetBranchParams();
256         if (in.empty())
257                 return;
258
259         istringstream data(in);
260         LyXLex lex(0,0);
261         lex.setStream(data);
262
263         string name;
264         lex >> name;
265         if (name != name_)
266                 return print_mailer_error("InsetBranchMailer", in, 1, name_);
267
268         // This is part of the inset proper that is usually swallowed
269         // by LyXText::readInset
270         string id;
271         lex >> id;
272         if (!lex || id != "Branch")
273                 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
274
275         params.read(lex);
276 }
277
278
279 void InsetBranchParams::write(ostream & os) const
280 {
281         os << "Branch " << branch << '\n';
282 }
283
284
285 void InsetBranchParams::read(LyXLex & lex)
286 {
287         lex >> branch;
288 }