]> git.lyx.org Git - lyx.git/blob - src/insets/insetbranch.C
Overhaul the branches code.
[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
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 DispatchResult
115 InsetBranch::priv_dispatch(FuncRequest const & cmd,
116                            idx_type & idx, pos_type & pos)
117 {
118         BufferView * bv = cmd.view();
119         switch (cmd.action) {
120         case LFUN_INSET_MODIFY: {
121                 InsetBranchParams params;
122                 InsetBranchMailer::string2params(cmd.argument, params);
123                 params_.branch = params.branch;
124                 setButtonLabel();
125                 return DispatchResult(true, true);
126         }
127
128         case LFUN_MOUSE_PRESS:
129                 if (cmd.button() != mouse_button::button3)
130                         return InsetCollapsable::priv_dispatch(cmd, idx, pos);
131                 return DispatchResult(false);
132
133         case LFUN_INSET_DIALOG_UPDATE:
134                 InsetBranchMailer(*this).updateDialog(bv);
135                 return DispatchResult(true);
136
137         case LFUN_MOUSE_RELEASE:
138                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
139                         InsetBranchMailer(*this).showDialog(bv);
140                         return DispatchResult(true);
141                 }
142                 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
143                 
144         default:
145                 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
146         }
147 }
148
149
150 namespace {
151
152 struct SameBranch {
153         SameBranch(string const & branch_name) : bn(branch_name) {}
154         bool operator()(Branch const & branch) const
155                 { return bn == branch.getBranch(); }
156 private:
157         string bn;
158 };
159
160 } // namespace anon
161
162
163 bool InsetBranch::isBranchSelected(BranchList const & branchlist) const
164 {
165         BranchList::const_iterator it = branchlist.begin();
166         BranchList::const_iterator const end = branchlist.end();
167         it = std::find_if(it, end, SameBranch(params_.branch));
168         if (it == end)
169                 return false;
170         return it->getSelected();
171 }
172
173
174 int InsetBranch::latex(Buffer const & buf, ostream & os,
175                        OutputParams const & runparams) const
176 {
177         return isBranchSelected(buf.params().branchlist()) ?
178                 inset.latex(buf, os, runparams) : 0;
179 }
180
181
182 int InsetBranch::linuxdoc(Buffer const & buf, std::ostream & os,
183                           OutputParams const & runparams) const
184 {
185         return isBranchSelected(buf.params().branchlist()) ?
186                 inset.linuxdoc(buf, os, runparams) : 0;
187 }
188
189
190 int InsetBranch::docbook(Buffer const & buf, std::ostream & os,
191                          OutputParams const & runparams) const
192 {
193         return isBranchSelected(buf.params().branchlist()) ?
194                 inset.docbook(buf, os, runparams) : 0;
195 }
196
197
198 int InsetBranch::plaintext(Buffer const & buf, std::ostream & os,
199                            OutputParams const & runparams) const
200 {
201         return isBranchSelected(buf.params().branchlist()) ?
202                 inset.plaintext(buf, os, runparams): 0;
203 }
204
205
206 void InsetBranch::validate(LaTeXFeatures & features) const
207 {
208         inset.validate(features);
209 }
210
211
212
213 string const InsetBranchMailer:: name_("branch");
214
215 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
216         : inset_(inset)
217 {}
218
219
220 string const InsetBranchMailer::inset2string(Buffer const &) const
221 {
222         return params2string(inset_.params());
223 }
224
225
226 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
227 {
228         ostringstream data;
229         data << name_ << ' ';
230         params.write(data);
231         return data.str();
232 }
233
234
235 void InsetBranchMailer::string2params(string const & in,
236                                       InsetBranchParams & params)
237 {
238         params = InsetBranchParams();
239         if (in.empty())
240                 return;
241
242         istringstream data(in);
243         LyXLex lex(0,0);
244         lex.setStream(data);
245
246         string name;
247         lex >> name;
248         if (name != name_)
249                 return print_mailer_error("InsetBranchMailer", in, 1, name_);
250
251         // This is part of the inset proper that is usually swallowed
252         // by LyXText::readInset
253         string id;
254         lex >> id;
255         if (!lex || id != "Branch")
256                 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
257
258         params.read(lex);
259 }
260
261
262 void InsetBranchParams::write(ostream & os) const
263 {
264         os << "Branch " << branch << '\n';
265 }
266
267
268 void InsetBranchParams::read(LyXLex & lex)
269 {
270         lex >> branch;
271 }