]> git.lyx.org Git - features.git/blob - src/insets/insetbranch.C
The 'Branches' mega-patch.
[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 "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 "LaTeXFeatures.h"
22 #include "latexrunparams.h"
23 #include "lyxlex.h"
24 #include "lyxtext.h"
25 #include "insets/insettext.h"
26 #include "support/LOstream.h"
27 #include "support/lstrings.h"
28 #include "debug.h"
29
30 using std::ostream;
31 using std::auto_ptr;
32
33 void InsetBranch::init()
34 {
35         setInsetName("Branch");
36         setButtonLabel();
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(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 void InsetBranch::metrics(MetricsInfo & mi, Dimension & dim) const
113 {
114         InsetCollapsable::metrics(mi, dim);
115         dim_ = dim;
116 }
117
118
119 void InsetBranch::draw(PainterInfo & pi, int x, int y) const
120 {
121         InsetCollapsable::draw(pi, x, y);
122 }
123
124
125 bool InsetBranch::showInsetDialog(BufferView * bv) const
126 {
127         InsetBranchMailer("branch", const_cast<InsetBranch &>(*this)).showDialog(bv);
128         return true;
129 }
130
131
132 dispatch_result InsetBranch::localDispatch(FuncRequest const & cmd)
133 {
134         BufferView * bv = cmd.view();
135         switch (cmd.action) {
136         case LFUN_INSET_MODIFY:
137                 {
138                 InsetBranchParams params;
139                 InsetBranchMailer::string2params(cmd.argument, params);
140                 params_.branch = params.branch;
141                 setButtonLabel();
142                 bv->updateInset();
143                 return DISPATCHED;
144                 }
145         case LFUN_INSET_EDIT:
146                 if (cmd.button() != mouse_button::button3) 
147                         return InsetCollapsable::localDispatch(cmd);
148                 return UNDISPATCHED;
149         case LFUN_INSET_DIALOG_UPDATE:
150                 InsetBranchMailer("branch", *this).updateDialog(bv);
151                 return DISPATCHED;
152         case LFUN_MOUSE_RELEASE:
153                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
154                     InsetBranchMailer("branch", *this).showDialog(bv);
155                         return DISPATCHED;
156                 }
157                 // fallthrough:
158         default:
159                 return InsetCollapsable::localDispatch(cmd);
160         }
161 }
162
163
164
165 int InsetBranch::latex(Buffer const * buf, ostream & os,
166         LatexRunParams const & runparams) const
167 {
168         string const branch_sel = buf->params.branchlist.allSelected();
169         if (branch_sel.find(params_.branch, 0) != string::npos)
170                 return inset.latex(buf, os, runparams);
171         return 0;
172 }
173
174
175 int InsetBranch::linuxdoc(Buffer const *, std::ostream &) const
176
177         return 0; 
178 }
179
180
181 int InsetBranch::docbook(Buffer const * buf, std::ostream & os, bool mixcont) const
182 {
183         // untested - MV
184         string const branch_sel = buf->params.branchlist.allSelected();
185         if (branch_sel.find(params_.branch, 0) != string::npos) 
186                 return inset.docbook(buf, os, mixcont);
187         return 0;
188 }
189
190
191 int InsetBranch::ascii(Buffer const * buf, std::ostream & os, int ll) const
192 {
193         string const branch_sel = buf->params.branchlist.allSelected();
194         if (branch_sel.find(params_.branch, 0) != string::npos) {
195                 return inset.ascii(buf, os, ll);
196         }
197         return 0;
198 }
199
200
201 void InsetBranch::validate(LaTeXFeatures & features) const
202 {
203         inset.validate(features);
204 }
205
206
207
208 InsetBranchMailer::InsetBranchMailer(string const & name,
209                                                 InsetBranch & inset)
210         : name_(name), inset_(inset)
211 {
212 }
213
214
215 string const InsetBranchMailer::inset2string(Buffer const & buf) const
216 {
217         InsetBranchParams params = inset_.params();
218         params.branchlist = buf.params.branchlist;
219         inset_.setParams(params);
220         return params2string(name_, params);
221 }
222
223
224 string const InsetBranchMailer::params2string(string const & name,
225                                 InsetBranchParams const & params)
226 {
227         ostringstream data;
228         data << name << ' ';
229         params.write(data);
230         // Add all_branches parameter to data:
231         data << params.branchlist.allBranches() << "\n";
232         return STRCONV(data.str());
233 }
234
235
236 void InsetBranchMailer::string2params(string const & in,
237                                      InsetBranchParams & params)
238 {
239         params = InsetBranchParams();
240
241         if (in.empty())
242                 return;
243
244         istringstream data(STRCONV(in));
245         LyXLex lex(0,0);
246         lex.setStream(data);
247         params.read(lex);
248         // Process all_branches here:
249         if (lex.isOK()) {
250                 lex.next();
251                 params.branchlist.add(lex.getString());
252         }
253 }
254
255
256 void InsetBranchParams::write(ostream & os) const
257 {
258         os << "Branch" << " " << branch << "\n";
259 }
260
261
262 void InsetBranchParams::read(LyXLex & lex)
263 {
264         if (lex.isOK()) {
265                 lex.next();
266                 string token = lex.getString();
267         }
268         if (lex.isOK()) {
269                 lex.next();
270                 string token = lex.getString();
271         }
272         if (lex.isOK()) {
273                 lex.next();
274                 branch = lex.getString();
275         }
276 }
277