]> git.lyx.org Git - lyx.git/blob - src/insets/insetbranch.C
the Paragraph::inInset() changes
[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         string s = "Branch: " + params_.branch;
97         setLabel(isOpen() ? s : getNewLabel(s) );
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 void InsetBranch::priv_dispatch(LCursor & cur, FuncRequest & 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                 break;
123         }
124
125         case LFUN_MOUSE_PRESS:
126                 if (cmd.button() != mouse_button::button3)
127                         InsetCollapsable::priv_dispatch(cur, cmd);
128                 else
129                         cur.undispatched();
130                 break;
131
132         case LFUN_INSET_DIALOG_UPDATE:
133                 InsetBranchMailer(*this).updateDialog(&cur.bv());
134                 break;
135
136         case LFUN_MOUSE_RELEASE:
137                 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
138                         InsetBranchMailer(*this).showDialog(&cur.bv());
139                 else
140                         InsetCollapsable::priv_dispatch(cur, cmd);
141                 break;
142
143
144         case LFUN_INSET_TOGGLE:
145                 // We assume that this lfun is indeed going to be dispatched.
146                 cur.dispatched();
147
148                 if (cmd.argument == "open")
149                         setStatus(Open);
150                 else if (cmd.argument == "close") {
151                         setStatus(Collapsed);
152                         leaveInset(cur, *this);
153         } else if (cmd.argument == "toggle") {
154                         if (isOpen()) {
155                                 setStatus(Collapsed);
156                                 leaveInset(cur, *this);
157                         } else
158                         setStatus(Open);
159
160                 // The branch inset uses "assign".
161                 } else if (cmd.argument == "assign"
162                            || cmd.argument.empty()) {
163                         BranchList const & branchlist =
164                                 cur.buffer().params().branchlist();
165                         if (isBranchSelected(branchlist)) {
166                                 if (status() != Open)
167                                         setStatus(Open);
168                                 else
169                                         cur.undispatched();
170                         } else {
171                                 if (status() != Collapsed) {
172                                         setStatus(Collapsed);
173                                         leaveInset(cur, *this);
174                                 } else
175                                         cur.undispatched();
176                         }
177                 }
178                 break;
179
180         default:
181                 InsetCollapsable::priv_dispatch(cur, cmd);
182                 break;
183         }
184 }
185
186
187 bool InsetBranch::isBranchSelected(BranchList const & branchlist) const
188 {
189         BranchList::const_iterator const end = branchlist.end();
190         BranchList::const_iterator it =
191                 std::find_if(branchlist.begin(), end,
192                              BranchNamesEqual(params_.branch));
193         if (it == end)
194                 return false;
195         return it->getSelected();
196 }
197
198
199 int InsetBranch::latex(Buffer const & buf, ostream & os,
200                        OutputParams const & runparams) const
201 {
202         return isBranchSelected(buf.params().branchlist()) ?
203                 InsetText::latex(buf, os, runparams) : 0;
204 }
205
206
207 int InsetBranch::linuxdoc(Buffer const & buf, std::ostream & os,
208                           OutputParams const & runparams) const
209 {
210         return isBranchSelected(buf.params().branchlist()) ?
211                 InsetText::linuxdoc(buf, os, runparams) : 0;
212 }
213
214
215 int InsetBranch::docbook(Buffer const & buf, std::ostream & os,
216                          OutputParams const & runparams) const
217 {
218         return isBranchSelected(buf.params().branchlist()) ?
219                 InsetText::docbook(buf, os, runparams) : 0;
220 }
221
222
223 int InsetBranch::plaintext(Buffer const & buf, std::ostream & os,
224                            OutputParams const & runparams) const
225 {
226         return isBranchSelected(buf.params().branchlist()) ?
227                 InsetText::plaintext(buf, os, runparams): 0;
228 }
229
230
231 void InsetBranch::validate(LaTeXFeatures & features) const
232 {
233         InsetText::validate(features);
234 }
235
236
237
238 string const InsetBranchMailer::name_("branch");
239
240 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
241         : inset_(inset)
242 {}
243
244
245 string const InsetBranchMailer::inset2string(Buffer const &) const
246 {
247         return params2string(inset_.params());
248 }
249
250
251 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
252 {
253         ostringstream data;
254         data << name_ << ' ';
255         params.write(data);
256         return data.str();
257 }
258
259
260 void InsetBranchMailer::string2params(string const & in,
261                                       InsetBranchParams & params)
262 {
263         params = InsetBranchParams();
264         if (in.empty())
265                 return;
266
267         istringstream data(in);
268         LyXLex lex(0,0);
269         lex.setStream(data);
270
271         string name;
272         lex >> name;
273         if (name != name_)
274                 return print_mailer_error("InsetBranchMailer", in, 1, name_);
275
276         // This is part of the inset proper that is usually swallowed
277         // by LyXText::readInset
278         string id;
279         lex >> id;
280         if (!lex || id != "Branch")
281                 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
282
283         params.read(lex);
284 }
285
286
287 void InsetBranchParams::write(ostream & os) const
288 {
289         os << "Branch " << branch << '\n';
290 }
291
292
293 void InsetBranchParams::read(LyXLex & lex)
294 {
295         lex >> branch;
296 }