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(string const & key, string const & label)
76 : InsetCommand("bibitem", key, label)
84 InsetBibKey::InsetBibKey(InsetBibKey const * b)
85 : InsetCommand("bibitem", b->getContents(), b->getOptions())
91 InsetBibKey::~InsetBibKey()
93 if(bibitem_form && bibitem_form->bibitem_form
94 && bibitem_form->bibitem_form->visible
95 && bibitem_form->bibitem_form->u_vdata == &holder)
96 fl_hide_form(bibitem_form->bibitem_form);
100 void InsetBibKey::callback( FD_bibitem_form * form, long data )
104 if(!holder.view->buffer()->isReadonly()) {
105 setContents(fl_get_input(form->key));
106 setOptions(fl_get_input(form->label));
107 // shouldn't mark the buffer dirty unless
108 // something was actually altered
109 holder.view->updateInset( this, true );
110 } // fall through to Cancel
112 fl_hide_form(form->bibitem_form);
118 void InsetBibKey::setCounter(int c)
122 if (getCmdName().empty())
123 setCmdName( tostr(counter) );
127 // I'm sorry but this is still necessary because \bibitem is used also
128 // as a LyX 2.x command, and lyxlex is not enough smart to understand
129 // real LaTeX commands. Yes, that could be fixed, but would be a waste
130 // of time cause LyX3 won't use lyxlex anyway. (ale)
131 void InsetBibKey::Write(Buffer const *, ostream & os) const
134 if (! getOptions().empty()) {
136 << getOptions() << ']';
139 << getContents() << "}\n";
143 // This is necessary here because this is written without begin_inset
144 // This should be changed!!! (Jug)
145 void InsetBibKey::Read(Buffer const *, LyXLex & lex)
150 token = lex.GetString();
153 lex.printError("InsetCommand: Parse error: `$$Token'");
157 string InsetBibKey::getScreenLabel() const
159 if (! getOptions().empty())
162 return tostr(counter);
167 The value in "Key:" isn't allways set right after a few bibkey insets have
168 been added/removed. Perhaps the wrong object is deleted/used somewhere
172 void InsetBibKey::Edit(BufferView * bv, int, int, unsigned int)
174 if(bv->buffer()->isReadonly())
175 WarnReadonly(bv->buffer()->fileName());
178 bibitem_form = create_form_bibitem_form();
179 fl_set_form_atclose(bibitem_form->bibitem_form,
180 CancelCloseBoxCB, 0);
186 bibitem_form->bibitem_form->u_vdata = &holder;
187 // InsetBibtex uses the same form, with different labels
188 fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
189 fl_set_button_shortcut(bibitem_form->key, scex(_("Key:|#K")), 1);
190 fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
191 fl_set_button_shortcut(bibitem_form->label, scex(_("Label:|#L")), 1);
192 fl_set_input(bibitem_form->key, getContents().c_str());
193 fl_set_input(bibitem_form->label, getOptions().c_str());
194 if (bibitem_form->bibitem_form->visible) {
195 fl_raise_form(bibitem_form->bibitem_form);
197 fl_show_form(bibitem_form->bibitem_form, FL_PLACE_MOUSE,
199 _("Bibliography item"));
204 InsetBibtex::InsetBibtex(string const & dbase, string const & style,
206 : InsetCommand("BibTeX", dbase, style), owner(o)
210 InsetBibtex::~InsetBibtex()
212 if(bibitem_form && bibitem_form->bibitem_form
213 && bibitem_form->bibitem_form->visible
214 && bibitem_form->bibitem_form->u_vdata == &holder)
215 fl_hide_form(bibitem_form->bibitem_form);
219 string InsetBibtex::getScreenLabel() const
221 return _("BibTeX Generated References");
225 int InsetBibtex::Latex(Buffer const *, ostream & os,
226 bool /*fragile*/, bool/*fs*/) const
228 // this looks like an horrible hack and it is :) The problem
229 // is that owner is not initialized correctly when the bib
230 // inset is cut and pasted. Such hacks will not be needed
233 owner = current_view->buffer();
235 // If we generate in a temp dir, we might need to give an
236 // absolute path there. This is a bit complicated since we can
237 // have a comma-separated list of bibliographies
239 string db_in = getContents();
240 db_in = split(db_in, adb, ',');
241 while(!adb.empty()) {
242 if (!owner->niceFile &&
243 IsFileReadable(MakeAbsPath(adb, owner->filepath)+".bib"))
244 adb = MakeAbsPath(adb, owner->filepath);
248 db_in= split(db_in, adb,',');
250 db_out = strip(db_out, ',');
254 && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
256 style = MakeAbsPath(getOptions(), owner->filepath);
258 style = getOptions();
260 os << "\\bibliographystyle{" << style << "}\n"
261 << "\\bibliography{" << db_out << "}\n";
266 // This method returns a comma separated list of Bibtex entries
267 vector<pair<string,string> > InsetBibtex::getKeys() const
269 // This hack is copied from InsetBibtex::Latex.
270 // Is it still needed? Probably yes.
271 // Why is this needed here when it already is in Latex?
272 // Anyway we need a different way to get to the
273 // buffer the inset is in. (Lgb)
276 // owner = current_view->buffer();
279 Path p(owner->filepath);
281 vector<pair<string,string> > keys;
283 string bibfiles = getContents();
284 bibfiles = split(bibfiles, tmp, ',');
285 while(!tmp.empty()) {
286 string fil = findtexfile(ChangeExtension(tmp, "bib"),
288 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
289 // If we didn't find a matching file name just fail silently
291 // This is a _very_ simple parser for Bibtex database
292 // files. All it does is to look for lines starting
293 // in @ and not being @preamble and @string entries.
294 // It does NOT do any syntax checking!
295 ifstream ifs(fil.c_str());
297 while (getline(ifs, linebuf0)) {
298 string linebuf = frontStrip(strip(linebuf0));
299 if( linebuf.empty() ) continue;
300 if (prefixIs(linebuf, "@")) {
301 linebuf = subst(linebuf, '{', '(');
302 linebuf = split(linebuf, tmp, '(');
303 tmp = lowercase(tmp);
304 if (!prefixIs(tmp, "@string")
305 && !prefixIs(tmp, "@preamble")) {
306 linebuf = split(linebuf, tmp, ',');
307 tmp = frontStrip(tmp);
309 keys.push_back(pair<string,string>(tmp,string()));
312 } else if( !keys.empty() ) {
313 keys.back().second += linebuf + "\n";
317 // Get next file name
318 bibfiles = split(bibfiles, tmp, ',');
324 // BibTeX should have its own dialog. This is provisional.
325 void InsetBibtex::Edit(BufferView * bv, int, int, unsigned int)
328 bibitem_form = create_form_bibitem_form();
329 fl_set_form_atclose(bibitem_form->bibitem_form,
330 CancelCloseBoxCB, 0);
335 bibitem_form->bibitem_form->u_vdata = &holder;
337 fl_set_object_label(bibitem_form->key, _("Database:"));
338 fl_set_object_label(bibitem_form->label, _("Style: "));
339 fl_set_input(bibitem_form->key, getContents().c_str());
340 fl_set_input(bibitem_form->label, getOptions().c_str());
341 if (bibitem_form->bibitem_form->visible) {
342 fl_raise_form(bibitem_form->bibitem_form);
344 fl_show_form(bibitem_form->bibitem_form,
345 FL_PLACE_MOUSE, FL_FULLBORDER,
351 bool InsetBibtex::addDatabase(string const & db)
353 string contents(getContents());
354 if (!contains(contents, db.c_str())) {
355 if (!contents.empty())
357 setContents(contents + db);
364 bool InsetBibtex::delDatabase(string const & db)
366 if (contains(getContents(), db.c_str())) {
368 int n = tokenPos(getContents(), ',', bd);
370 // Weird code, would someone care to explain this?(Lgb)
373 setContents(subst(getContents(), tmp.c_str(), ", "));
375 setContents(split(getContents(), bd, ','));
383 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
384 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
387 // Does look like a hack? It is! (but will change at 0.13)
388 LyXParagraph * par = current_view->buffer()->paragraph;
392 int wx = par->bibkey->width(bv, font);
402 string bibitemWidest(BufferView * bv)
405 // Does look like a hack? It is! (but will change at 0.13)
406 LyXParagraph * par = current_view->buffer()->paragraph;
407 InsetBibKey * bkey = 0;
412 int wx = par->bibkey->width(bv, font);
421 if (bkey && !bkey->getScreenLabel().empty())
422 return bkey->getScreenLabel();