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