10 #include FORMS_H_LOCATION
14 #include "lyx_gui_misc.h"
15 #include "BufferView.h"
19 #include "support/filetools.h"
20 #include "support/path.h"
29 extern BufferView * current_view;
31 FD_bibitem_form * bibitem_form = 0;
33 FD_bibitem_form * create_form_bibitem_form(void);
37 void bibitem_cb(FL_OBJECT *, long data)
39 InsetBibKey::Holder * holder =
40 static_cast<InsetBibKey::Holder*>
41 (bibitem_form->bibitem_form->u_vdata);
43 holder->inset->callback( bibitem_form, data );
47 FD_bibitem_form * create_form_bibitem_form(void)
50 FD_bibitem_form * fdui = (FD_bibitem_form *) fl_calloc(1, sizeof(FD_bibitem_form));
52 fdui->bibitem_form = fl_bgn_form(FL_NO_BOX, 220, 130);
53 obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
54 fdui->key = obj = fl_add_input(FL_NORMAL_INPUT, 80, 10, 130, 30, idex(_("Key:|#K")));
55 fl_set_input_shortcut(obj, scex(_("Key:|#K")), 1);
56 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
57 obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
58 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
59 fl_set_object_callback(obj, bibitem_cb, 1);
60 obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
61 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
62 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
63 fl_set_object_callback(obj, bibitem_cb, 0);
64 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Label:|#L")));
65 fl_set_input_shortcut(obj, scex(_("Label:|#L")), 1);
66 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
69 //fdui->bibitem_form->fdui = fdui;
75 InsetBibKey::InsetBibKey(InsetCommandParams const & p)
82 InsetBibKey::~InsetBibKey()
84 if(bibitem_form && bibitem_form->bibitem_form
85 && bibitem_form->bibitem_form->visible
86 && bibitem_form->bibitem_form->u_vdata == &holder)
87 fl_hide_form(bibitem_form->bibitem_form);
91 Inset * InsetBibKey::Clone() const
93 InsetBibKey * b = new InsetBibKey(params());
94 b->setCounter(counter);
99 void InsetBibKey::callback( FD_bibitem_form * form, long data )
103 if(!holder.view->buffer()->isReadonly()) {
104 setContents(fl_get_input(form->key));
105 setOptions(fl_get_input(form->label));
106 // shouldn't mark the buffer dirty unless
107 // something was actually altered
108 holder.view->updateInset( this, true );
109 } // fall through to Cancel
111 fl_hide_form(form->bibitem_form);
117 void InsetBibKey::setCounter(int c)
121 if (getCmdName().empty())
122 setCmdName( tostr(counter) );
126 // I'm sorry but this is still necessary because \bibitem is used also
127 // as a LyX 2.x command, and lyxlex is not enough smart to understand
128 // real LaTeX commands. Yes, that could be fixed, but would be a waste
129 // of time cause LyX3 won't use lyxlex anyway. (ale)
130 void InsetBibKey::Write(Buffer const *, ostream & os) const
133 if (! getOptions().empty()) {
135 << getOptions() << ']';
138 << getContents() << "}\n";
142 // This is necessary here because this is written without begin_inset
143 // This should be changed!!! (Jug)
144 void InsetBibKey::Read(Buffer const *, LyXLex & lex)
149 token = lex.GetString();
152 lex.printError("InsetCommand: Parse error: `$$Token'");
156 string const InsetBibKey::getScreenLabel() const
158 if (! getOptions().empty())
161 return tostr(counter);
166 The value in "Key:" isn't allways set right after a few bibkey insets have
167 been added/removed. Perhaps the wrong object is deleted/used somewhere
171 void InsetBibKey::Edit(BufferView * bv, int, int, unsigned int)
173 if(bv->buffer()->isReadonly())
174 WarnReadonly(bv->buffer()->fileName());
177 bibitem_form = create_form_bibitem_form();
178 fl_set_form_atclose(bibitem_form->bibitem_form,
179 CancelCloseBoxCB, 0);
185 bibitem_form->bibitem_form->u_vdata = &holder;
186 // InsetBibtex uses the same form, with different labels
187 fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
188 fl_set_button_shortcut(bibitem_form->key, scex(_("Key:|#K")), 1);
189 fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
190 fl_set_button_shortcut(bibitem_form->label, scex(_("Label:|#L")), 1);
191 fl_set_input(bibitem_form->key, getContents().c_str());
192 fl_set_input(bibitem_form->label, getOptions().c_str());
193 if (bibitem_form->bibitem_form->visible) {
194 fl_raise_form(bibitem_form->bibitem_form);
196 fl_show_form(bibitem_form->bibitem_form, FL_PLACE_MOUSE,
198 _("Bibliography item"));
203 InsetBibtex::InsetBibtex(InsetCommandParams const & p, Buffer * o)
204 : InsetCommand(p), owner(o)
208 InsetBibtex::~InsetBibtex()
210 if(bibitem_form && bibitem_form->bibitem_form
211 && bibitem_form->bibitem_form->visible
212 && bibitem_form->bibitem_form->u_vdata == &holder)
213 fl_hide_form(bibitem_form->bibitem_form);
217 string const InsetBibtex::getScreenLabel() const
219 return _("BibTeX Generated References");
223 int InsetBibtex::Latex(Buffer const *, ostream & os,
224 bool /*fragile*/, bool/*fs*/) const
226 // this looks like an horrible hack and it is :) The problem
227 // is that owner is not initialized correctly when the bib
228 // inset is cut and pasted. Such hacks will not be needed
231 owner = current_view->buffer();
233 // If we generate in a temp dir, we might need to give an
234 // absolute path there. This is a bit complicated since we can
235 // have a comma-separated list of bibliographies
237 string db_in = getContents();
238 db_in = split(db_in, adb, ',');
239 while(!adb.empty()) {
240 if (!owner->niceFile &&
241 IsFileReadable(MakeAbsPath(adb, owner->filepath)+".bib"))
242 adb = MakeAbsPath(adb, owner->filepath);
246 db_in= split(db_in, adb,',');
248 db_out = strip(db_out, ',');
252 && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
254 style = MakeAbsPath(getOptions(), owner->filepath);
256 style = getOptions();
258 os << "\\bibliographystyle{" << style << "}\n"
259 << "\\bibliography{" << db_out << "}\n";
264 // This method returns a comma separated list of Bibtex entries
265 vector<pair<string, string> > const InsetBibtex::getKeys() const
267 // This hack is copied from InsetBibtex::Latex.
268 // Is it still needed? Probably yes.
269 // Why is this needed here when it already is in Latex?
270 // Anyway we need a different way to get to the
271 // buffer the inset is in. (Lgb)
274 // owner = current_view->buffer();
277 Path p(owner->filepath);
279 vector<pair<string,string> > keys;
281 string bibfiles = getContents();
282 bibfiles = split(bibfiles, tmp, ',');
283 while(!tmp.empty()) {
284 string fil = findtexfile(ChangeExtension(tmp, "bib"),
286 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
287 // If we didn't find a matching file name just fail silently
289 // This is a _very_ simple parser for Bibtex database
290 // files. All it does is to look for lines starting
291 // in @ and not being @preamble and @string entries.
292 // It does NOT do any syntax checking!
293 ifstream ifs(fil.c_str());
295 while (getline(ifs, linebuf0)) {
296 string linebuf = frontStrip(strip(linebuf0));
297 if( linebuf.empty() ) continue;
298 if (prefixIs(linebuf, "@")) {
299 linebuf = subst(linebuf, '{', '(');
300 linebuf = split(linebuf, tmp, '(');
301 tmp = lowercase(tmp);
302 if (!prefixIs(tmp, "@string")
303 && !prefixIs(tmp, "@preamble")) {
304 linebuf = split(linebuf, tmp, ',');
305 tmp = frontStrip(tmp);
307 keys.push_back(pair<string,string>(tmp,string()));
310 } else if( !keys.empty() ) {
311 keys.back().second += linebuf + "\n";
315 // Get next file name
316 bibfiles = split(bibfiles, tmp, ',');
322 // BibTeX should have its own dialog. This is provisional.
323 void InsetBibtex::Edit(BufferView * bv, int, int, unsigned int)
326 bibitem_form = create_form_bibitem_form();
327 fl_set_form_atclose(bibitem_form->bibitem_form,
328 CancelCloseBoxCB, 0);
333 bibitem_form->bibitem_form->u_vdata = &holder;
335 fl_set_object_label(bibitem_form->key, _("Database:"));
336 fl_set_object_label(bibitem_form->label, _("Style: "));
337 fl_set_input(bibitem_form->key, getContents().c_str());
338 fl_set_input(bibitem_form->label, getOptions().c_str());
339 if (bibitem_form->bibitem_form->visible) {
340 fl_raise_form(bibitem_form->bibitem_form);
342 fl_show_form(bibitem_form->bibitem_form,
343 FL_PLACE_MOUSE, FL_FULLBORDER,
349 bool InsetBibtex::addDatabase(string const & db)
351 string contents(getContents());
352 if (!contains(contents, db)) {
353 if (!contents.empty())
355 setContents(contents + db);
362 bool InsetBibtex::delDatabase(string const & db)
364 if (contains(getContents(), db)) {
366 int n = tokenPos(getContents(), ',', bd);
368 // Weird code, would someone care to explain this?(Lgb)
371 setContents(subst(getContents(), tmp, ", "));
373 setContents(split(getContents(), bd, ','));
381 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
382 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
385 // Does look like a hack? It is! (but will change at 0.13)
386 LyXParagraph * par = current_view->buffer()->paragraph;
390 int wx = par->bibkey->width(bv, font);
400 string const bibitemWidest(BufferView * bv)
403 // Does look like a hack? It is! (but will change at 0.13)
404 LyXParagraph * par = current_view->buffer()->paragraph;
405 InsetBibKey * bkey = 0;
410 int wx = par->bibkey->width(bv, font);
419 if (bkey && !bkey->getScreenLabel().empty())
420 return bkey->getScreenLabel();