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