]> git.lyx.org Git - lyx.git/blob - src/insets/insetbranch.C
* insetCollapsable:
[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 "FuncStatus.h"
22 #include "gettext.h"
23 #include "LColor.h"
24 #include "lyxlex.h"
25 #include "paragraph.h"
26
27 #include <sstream>
28
29 using lyx::docstring;
30
31 using std::string;
32 using std::auto_ptr;
33 using std::istringstream;
34 using std::ostream;
35 using std::ostringstream;
36
37
38 void InsetBranch::init()
39 {
40         setInsetName("Branch");
41         setButtonLabel();
42 }
43
44
45 InsetBranch::InsetBranch(BufferParams const & bp,
46                          InsetBranchParams const & params)
47         : InsetCollapsable(bp), params_(params)
48 {
49         init();
50 }
51
52
53 InsetBranch::InsetBranch(InsetBranch const & in)
54         : InsetCollapsable(in), params_(in.params_)
55 {
56         init();
57 }
58
59
60 InsetBranch::~InsetBranch()
61 {
62         InsetBranchMailer(*this).hideDialog();
63 }
64
65
66 auto_ptr<InsetBase> InsetBranch::doClone() const
67 {
68         return auto_ptr<InsetBase>(new InsetBranch(*this));
69 }
70
71
72 docstring const InsetBranch::editMessage() const
73 {
74         return _("Opened Branch Inset");
75 }
76
77
78 void InsetBranch::write(Buffer const & buf, ostream & os) const
79 {
80         params_.write(os);
81         InsetCollapsable::write(buf, os);
82 }
83
84
85 void InsetBranch::read(Buffer const & buf, LyXLex & lex)
86 {
87         params_.read(lex);
88         InsetCollapsable::read(buf, lex);
89         setButtonLabel();
90 }
91
92
93 void InsetBranch::setButtonLabel()
94 {
95         LyXFont font(LyXFont::ALL_SANE);
96         font.decSize();
97         font.decSize();
98
99         // FIXME UNICODE
100         docstring s = _("Branch: ") + lyx::from_utf8(params_.branch);
101         font.setColor(LColor::foreground);
102         if (!params_.branch.empty()) {
103                 LColor_color c = lcolor.getFromLyXName(params_.branch);
104                 if (c == LColor::none) {
105                         c = LColor::error;
106                         s = _("Undef: ") + s;
107                 }
108                 setBackgroundColor(c);
109         } else
110                 setBackgroundColor(LColor::background);
111         setLabel(isOpen() ? s : getNewLabel(s) );
112         setLabelFont(font);
113 }
114
115
116 bool InsetBranch::showInsetDialog(BufferView * bv) const
117 {
118         InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
119         return true;
120 }
121
122
123 void InsetBranch::doDispatch(LCursor & cur, FuncRequest & cmd)
124 {
125         switch (cmd.action) {
126         case LFUN_INSET_MODIFY: {
127                 InsetBranchParams params;
128                 InsetBranchMailer::string2params(lyx::to_utf8(cmd.argument()), params);
129                 params_.branch = params.branch;
130                 setButtonLabel();
131                 break;
132         }
133
134         case LFUN_MOUSE_PRESS:
135                 if (cmd.button() != mouse_button::button3)
136                         InsetCollapsable::doDispatch(cur, cmd);
137                 else
138                         cur.undispatched();
139                 break;
140
141         case LFUN_INSET_DIALOG_UPDATE:
142                 InsetBranchMailer(*this).updateDialog(&cur.bv());
143                 break;
144
145         case LFUN_MOUSE_RELEASE:
146                 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
147                         InsetBranchMailer(*this).showDialog(&cur.bv());
148                 else
149                         InsetCollapsable::doDispatch(cur, cmd);
150                 break;
151
152
153         case LFUN_INSET_TOGGLE:
154                 if (cmd.argument() == "assign" || cmd.argument().empty()) {
155                         // The branch inset uses "assign".
156                         if (isBranchSelected(cur.buffer())) {
157                                 if (status() != Open)
158                                         setStatus(cur, Open);
159                                 else
160                                         cur.undispatched();
161                         } else {
162                                 if (status() != Collapsed)
163                                         setStatus(cur, Collapsed);
164                                 else
165                                         cur.undispatched();
166                         }
167                 }
168                 else
169                         InsetCollapsable::doDispatch(cur, cmd);
170                 break;
171
172         default:
173                 InsetCollapsable::doDispatch(cur, cmd);
174                 break;
175         }
176 }
177
178
179 bool InsetBranch::getStatus(LCursor & cur, FuncRequest const & cmd,
180                 FuncStatus & flag) const
181 {
182         switch (cmd.action) {
183         case LFUN_INSET_MODIFY:
184         case LFUN_INSET_DIALOG_UPDATE:
185                 flag.enabled(true);
186                 break;
187
188         case LFUN_INSET_TOGGLE:
189                 if (cmd.argument() == "open" || cmd.argument() == "close" ||
190                     cmd.argument() == "toggle")
191                         flag.enabled(true);
192                 else if (cmd.argument() == "assign"
193                            || cmd.argument().empty()) {
194                         if (isBranchSelected(cur.buffer()))
195                                 flag.enabled(status() != Open);
196                         else
197                                 flag.enabled(status() != Collapsed);
198                 } else
199                         flag.enabled(true);
200                 break;
201
202         default:
203                 return InsetCollapsable::getStatus(cur, cmd, flag);
204         }
205         return true;
206 }
207
208
209 bool InsetBranch::isBranchSelected(Buffer const & buffer) const
210 {
211         Buffer const & realbuffer = *buffer.getMasterBuffer();
212         BranchList const & branchlist = realbuffer.params().branchlist();
213         BranchList::const_iterator const end = branchlist.end();
214         BranchList::const_iterator it =
215                 std::find_if(branchlist.begin(), end,
216                              BranchNamesEqual(params_.branch));
217         if (it == end)
218                 return false;
219         return it->getSelected();
220 }
221
222
223 int InsetBranch::latex(Buffer const & buf, ostream & os,
224                        OutputParams const & runparams) const
225 {
226         return isBranchSelected(buf) ?
227                 InsetText::latex(buf, os, runparams) : 0;
228 }
229
230
231 int InsetBranch::docbook(Buffer const & buf, std::ostream & os,
232                          OutputParams const & runparams) const
233 {
234         return isBranchSelected(buf) ?
235                 InsetText::docbook(buf, os, runparams) : 0;
236 }
237
238
239 int InsetBranch::plaintext(Buffer const & buf, std::ostream & os,
240                            OutputParams const & runparams) const
241 {
242         return isBranchSelected(buf) ?
243                 InsetText::plaintext(buf, os, runparams): 0;
244 }
245
246
247 void InsetBranch::validate(LaTeXFeatures & features) const
248 {
249         InsetText::validate(features);
250 }
251
252
253
254 string const InsetBranchMailer::name_("branch");
255
256 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
257         : inset_(inset)
258 {}
259
260
261 string const InsetBranchMailer::inset2string(Buffer const &) const
262 {
263         return params2string(inset_.params());
264 }
265
266
267 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
268 {
269         ostringstream data;
270         data << name_ << ' ';
271         params.write(data);
272         return data.str();
273 }
274
275
276 void InsetBranchMailer::string2params(string const & in,
277                                       InsetBranchParams & params)
278 {
279         params = InsetBranchParams();
280         if (in.empty())
281                 return;
282
283         istringstream data(in);
284         LyXLex lex(0,0);
285         lex.setStream(data);
286
287         string name;
288         lex >> name;
289         if (name != name_)
290                 return print_mailer_error("InsetBranchMailer", in, 1, name_);
291
292         // This is part of the inset proper that is usually swallowed
293         // by LyXText::readInset
294         string id;
295         lex >> id;
296         if (!lex || id != "Branch")
297                 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
298
299         params.read(lex);
300 }
301
302
303 void InsetBranchParams::write(ostream & os) const
304 {
305         os << "Branch " << branch << '\n';
306 }
307
308
309 void InsetBranchParams::read(LyXLex & lex)
310 {
311         lex >> branch;
312 }