]> git.lyx.org Git - lyx.git/blob - src/insets/insetbranch.C
Move #includes out of header files.
[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 "Lsstream.h"
14
15 #include "insetbranch.h"
16 #include "gettext.h"
17 #include "lyxfont.h"
18 #include "language.h"
19 #include "buffer.h"
20 #include "BufferView.h"
21 #include "funcrequest.h"
22 #include "LaTeXFeatures.h"
23 #include "latexrunparams.h"
24 #include "lyxlex.h"
25 #include "lyxtext.h"
26 #include "insets/insettext.h"
27 #include "support/LOstream.h"
28 #include "support/lstrings.h"
29 #include "debug.h"
30
31 using std::ostream;
32 using std::auto_ptr;
33
34 void InsetBranch::init()
35 {
36         setInsetName("Branch");
37 }
38
39
40 InsetBranch::InsetBranch(BufferParams const & bp, string const & label)
41         : InsetCollapsable(bp)
42 {
43         params_.branch = label;
44         // Hack: stash the list of all allowable branch labels from this
45         // buffer into inset's parm list as a "stowaway":
46         params_.branchlist = bp.branchlist;
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 mailer("branch", *this);
61         mailer.hideDialog();
62 }
63
64
65 auto_ptr<InsetBase> InsetBranch::clone() const
66 {
67         return auto_ptr<InsetBase>(new InsetBranch(*this));
68 }
69
70
71 string const InsetBranch::editMessage() const
72 {
73         return _("Opened Branch Inset");
74 }
75
76
77 void InsetBranch::write(Buffer const & buf, ostream & os) const
78 {
79         params_.write(os);
80         InsetCollapsable::write(buf, os);
81 }
82
83
84 void InsetBranch::read(Buffer const & buf, LyXLex & lex)
85 {
86         if (lex.isOK()) {
87                 lex.next();
88                 params_.branch = lex.getString();
89         }
90         InsetCollapsable::read(buf, lex);
91         setButtonLabel();
92 }
93
94
95 void InsetBranch::setButtonLabel()
96 {
97         LyXFont font(LyXFont::ALL_SANE);
98         font.decSize();
99         font.decSize();
100
101         setLabel("Branch: " + params_.branch);
102         font.setColor(LColor::foreground);
103         string const color = params_.branchlist.getColor(params_.branch);
104         if (!color.empty()) {
105                 setBackgroundColor(lcolor.getFromLyXName(params_.branch));
106         } else
107                 setBackgroundColor(LColor::background);
108         setLabelFont(font);
109 }
110
111
112 bool InsetBranch::showInsetDialog(BufferView * bv) const
113 {
114         InsetBranchMailer("branch", const_cast<InsetBranch &>(*this)).showDialog(bv);
115         return true;
116 }
117
118
119 dispatch_result InsetBranch::localDispatch(FuncRequest const & cmd)
120 {
121         BufferView * bv = cmd.view();
122         switch (cmd.action) {
123         case LFUN_INSET_MODIFY:
124                 {
125                 InsetBranchParams params;
126                 InsetBranchMailer::string2params(cmd.argument, params);
127                 params_.branch = params.branch;
128                 setButtonLabel();
129                 bv->updateInset(this);
130                 return DISPATCHED;
131                 }
132         case LFUN_INSET_EDIT:
133                 if (cmd.button() != mouse_button::button3)
134                         return InsetCollapsable::localDispatch(cmd);
135                 
136                 return UNDISPATCHED;
137         case LFUN_INSET_DIALOG_UPDATE:
138                 InsetBranchMailer("branch", *this).updateDialog(bv);
139                 return DISPATCHED;
140         case LFUN_MOUSE_RELEASE:
141                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
142                     InsetBranchMailer("branch", *this).showDialog(bv);
143                         return DISPATCHED;
144                 }
145                 // fallthrough:
146         default:
147                 return InsetCollapsable::localDispatch(cmd);
148         }
149 }
150
151
152 int InsetBranch::latex(Buffer const & buf, ostream & os,
153         LatexRunParams const & runparams) const
154 {
155         string const branch_sel = buf.params.branchlist.allSelected();
156         if (branch_sel.find(params_.branch, 0) != string::npos)
157                 return inset.latex(buf, os, runparams);
158         return 0;
159 }
160
161
162 int InsetBranch::linuxdoc(Buffer const &, std::ostream &) const
163 {
164         return 0;
165 }
166
167
168 int InsetBranch::docbook(Buffer const & buf, std::ostream & os, bool mixcont) const
169 {
170         // untested - MV
171         string const branch_sel = buf.params.branchlist.allSelected();
172         if (branch_sel.find(params_.branch, 0) != string::npos)
173                 return inset.docbook(buf, os, mixcont);
174         return 0;
175 }
176
177
178 int InsetBranch::ascii(Buffer const & buf, std::ostream & os, int ll) const
179 {
180         string const branch_sel = buf.params.branchlist.allSelected();
181         if (branch_sel.find(params_.branch, 0) != string::npos) {
182                 return inset.ascii(buf, os, ll);
183         }
184         return 0;
185 }
186
187
188 void InsetBranch::validate(LaTeXFeatures & features) const
189 {
190         inset.validate(features);
191 }
192
193
194
195 InsetBranchMailer::InsetBranchMailer(string const & name,
196                                                 InsetBranch & inset)
197         : name_(name), inset_(inset)
198 {
199 }
200
201
202 string const InsetBranchMailer::inset2string(Buffer const & buf) const
203 {
204         InsetBranchParams params = inset_.params();
205         params.branchlist = buf.params.branchlist;
206         inset_.setParams(params);
207         return params2string(name_, params);
208 }
209
210
211 string const InsetBranchMailer::params2string(string const & name,
212                                 InsetBranchParams const & params)
213 {
214         ostringstream data;
215         data << name << ' ';
216         params.write(data);
217         // Add all_branches parameter to data:
218         data << params.branchlist.allBranches() << "\n";
219         return STRCONV(data.str());
220 }
221
222
223 void InsetBranchMailer::string2params(string const & in,
224                                      InsetBranchParams & params)
225 {
226         params = InsetBranchParams();
227
228         if (in.empty())
229                 return;
230
231         istringstream data(STRCONV(in));
232         LyXLex lex(0,0);
233         lex.setStream(data);
234         params.read(lex);
235         // Process all_branches here:
236         if (lex.isOK()) {
237                 lex.next();
238                 params.branchlist.add(lex.getString());
239         }
240 }
241
242
243 void InsetBranchParams::write(ostream & os) const
244 {
245         os << "Branch" << " " << branch << "\n";
246 }
247
248
249 void InsetBranchParams::read(LyXLex & lex)
250 {
251         if (lex.isOK()) {
252                 lex.next();
253                 string token = lex.getString();
254         }
255         if (lex.isOK()) {
256                 lex.next();
257                 string token = lex.getString();
258         }
259         if (lex.isOK()) {
260                 lex.next();
261                 branch = lex.getString();
262         }
263 }