10 #include FORMS_H_LOCATION
15 #include "lyx_gui_misc.h"
16 #include "BufferView.h"
20 #include "support/filetools.h"
21 #include "support/path.h"
30 extern BufferView * current_view;
32 FD_bibitem_form * bibitem_form = 0;
34 FD_bibitem_form * create_form_bibitem_form(void);
37 extern "C" void bibitem_cb(FL_OBJECT *, long data)
43 InsetBibKey::Holder * holder =
44 static_cast<InsetBibKey::Holder*>
45 (bibitem_form->bibitem_form->u_vdata);
47 if(!holder->view->buffer()->isReadonly())
49 InsetBibKey * inset = holder->inset;
50 inset->setContents(fl_get_input(bibitem_form->key));
51 inset->setOptions(fl_get_input(bibitem_form->label));
52 fl_hide_form(bibitem_form->bibitem_form);
53 // Does look like a hack? It is! (but will change at 0.13)
54 holder->view->text->RedoParagraph(holder->view);
55 holder->view->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
57 } // fall through to Cancel on RO-mode
60 fl_hide_form(bibitem_form->bibitem_form);
66 FD_bibitem_form * create_form_bibitem_form(void)
69 FD_bibitem_form * fdui = (FD_bibitem_form *) fl_calloc(1, sizeof(FD_bibitem_form));
71 fdui->bibitem_form = fl_bgn_form(FL_NO_BOX, 220, 130);
72 obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
73 fdui->key = obj = fl_add_input(FL_NORMAL_INPUT, 80, 10, 130, 30, idex(_("Key:|#K")));
74 fl_set_input_shortcut(obj, scex(_("Key:|#K")), 1);
75 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
76 obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
77 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
78 fl_set_object_callback(obj, bibitem_cb, 1);
79 obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
80 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
81 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
82 fl_set_object_callback(obj, bibitem_cb, 0);
83 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Label:|#L")));
84 fl_set_input_shortcut(obj, scex(_("Label:|#L")), 1);
85 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
88 //fdui->bibitem_form->fdui = fdui;
94 InsetBibKey::InsetBibKey(string const & key, string const & label):
95 InsetCommand("bibitem", key, label)
103 InsetBibKey::InsetBibKey(InsetBibKey const * b):
104 InsetCommand("bibitem", b->getContents(), b->getOptions())
106 counter = b->counter;
110 InsetBibKey::~InsetBibKey()
112 if(bibitem_form && bibitem_form->bibitem_form
113 && bibitem_form->bibitem_form->visible
114 && bibitem_form->bibitem_form->u_vdata == &holder)
115 fl_hide_form(bibitem_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 string InsetBibKey::getScreenLabel() const
145 if (! getOptions().empty())
148 return tostr(counter);
153 The value in "Key:" isn't allways set right after a few bibkey insets have
154 been added/removed. Perhaps the wrong object is deleted/used somewhere
158 void InsetBibKey::Edit(BufferView * bv, int, int, unsigned int)
160 if(bv->buffer()->isReadonly())
161 WarnReadonly(bv->buffer()->fileName());
164 bibitem_form = create_form_bibitem_form();
165 fl_set_form_atclose(bibitem_form->bibitem_form,
166 CancelCloseBoxCB, 0);
172 bibitem_form->bibitem_form->u_vdata = &holder;
173 // InsetBibtex uses the same form, with different labels
174 fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
175 fl_set_button_shortcut(bibitem_form->key, scex(_("Key:|#K")), 1);
176 fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
177 fl_set_button_shortcut(bibitem_form->label, scex(_("Label:|#L")), 1);
178 fl_set_input(bibitem_form->key, getContents().c_str());
179 fl_set_input(bibitem_form->label, getOptions().c_str());
180 if (bibitem_form->bibitem_form->visible) {
181 fl_raise_form(bibitem_form->bibitem_form);
183 fl_show_form(bibitem_form->bibitem_form, FL_PLACE_MOUSE,
185 _("Bibliography item"));
190 InsetBibtex::InsetBibtex(string const & dbase, string const & style,
192 : InsetCommand("BibTeX", dbase, style), owner(o)
196 InsetBibtex::~InsetBibtex()
198 if(bibitem_form && bibitem_form->bibitem_form
199 && bibitem_form->bibitem_form->visible
200 && bibitem_form->bibitem_form->u_vdata == &holder)
201 fl_hide_form(bibitem_form->bibitem_form);
205 string InsetBibtex::getScreenLabel() const
207 return _("BibTeX Generated References");
211 int InsetBibtex::Latex(Buffer const *, ostream & os,
212 bool /*fragile*/, bool/*fs*/) const
214 // this looks like an horrible hack and it is :) The problem
215 // is that owner is not initialized correctly when the bib
216 // inset is cut and pasted. Such hacks will not be needed
219 owner = current_view->buffer();
221 // If we generate in a temp dir, we might need to give an
222 // absolute path there. This is a bit complicated since we can
223 // have a comma-separated list of bibliographies
225 string db_in = getContents();
226 db_in = split(db_in, adb, ',');
227 while(!adb.empty()) {
228 if (!owner->niceFile &&
229 IsFileReadable(MakeAbsPath(adb, owner->filepath)+".bib"))
230 adb = MakeAbsPath(adb, owner->filepath);
234 db_in= split(db_in, adb,',');
236 db_out = strip(db_out, ',');
240 && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
242 style = MakeAbsPath(getOptions(), owner->filepath);
244 style = getOptions();
246 os << "\\bibliographystyle{" << style << "}\n"
247 << "\\bibliography{" << db_out << "}\n";
252 // This method returns a comma separated list of Bibtex entries
253 vector<pair<string,string> > InsetBibtex::getKeys() const
255 // This hack is copied from InsetBibtex::Latex.
256 // Is it still needed? Probably yes.
257 // Why is this needed here when it already is in Latex?
258 // Anyway we need a different way to get to the
259 // buffer the inset is in. (Lgb)
262 // owner = current_view->buffer();
265 Path p(owner->filepath);
267 vector<pair<string,string> > keys;
269 string bibfiles = getContents();
270 bibfiles = split(bibfiles, tmp, ',');
271 while(!tmp.empty()) {
272 string fil = findtexfile(ChangeExtension(tmp, "bib"),
274 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
275 // If we didn't find a matching file name just fail silently
277 // This is a _very_ simple parser for Bibtex database
278 // files. All it does is to look for lines starting
279 // in @ and not being @preamble and @string entries.
280 // It does NOT do any syntax checking!
281 ifstream ifs(fil.c_str());
283 while (getline(ifs, linebuf0)) {
284 string linebuf = frontStrip(strip(linebuf0));
285 if( linebuf.empty() ) continue;
286 if (prefixIs(linebuf, "@")) {
287 linebuf = subst(linebuf, '{', '(');
288 linebuf = split(linebuf, tmp, '(');
289 tmp = lowercase(tmp);
290 if (!prefixIs(tmp, "@string")
291 && !prefixIs(tmp, "@preamble")) {
292 linebuf = split(linebuf, tmp, ',');
293 tmp = frontStrip(tmp);
295 keys.push_back(pair<string,string>(tmp,string()));
298 } else if( !keys.empty() ) {
299 keys.back().second += linebuf + "\n";
303 // Get next file name
304 bibfiles = split(bibfiles, tmp, ',');
310 // BibTeX should have its own dialog. This is provisional.
311 void InsetBibtex::Edit(BufferView * bv, int, int, unsigned int)
314 bibitem_form = create_form_bibitem_form();
315 fl_set_form_atclose(bibitem_form->bibitem_form,
316 CancelCloseBoxCB, 0);
321 bibitem_form->bibitem_form->u_vdata = &holder;
323 fl_set_object_label(bibitem_form->key, _("Database:"));
324 fl_set_object_label(bibitem_form->label, _("Style: "));
325 fl_set_input(bibitem_form->key, getContents().c_str());
326 fl_set_input(bibitem_form->label, getOptions().c_str());
327 if (bibitem_form->bibitem_form->visible) {
328 fl_raise_form(bibitem_form->bibitem_form);
330 fl_show_form(bibitem_form->bibitem_form,
331 FL_PLACE_MOUSE, FL_FULLBORDER,
337 bool InsetBibtex::addDatabase(string const & db)
339 if (!contains(getContents(), db.c_str())) {
340 if (!getContents().empty())
349 bool InsetBibtex::delDatabase(string const & db)
351 if (contains(getContents(), db.c_str())) {
353 int n = tokenPos(getContents(), ',', bd);
355 // Weird code, would someone care to explain this?(Lgb)
358 setContents(subst(getContents(), tmp.c_str(), ", "));
360 setContents(split(getContents(), bd, ','));
368 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
369 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
372 // Does look like a hack? It is! (but will change at 0.13)
373 LyXParagraph * par = current_view->buffer()->paragraph;
377 int wx = par->bibkey->width(bv, font);
387 string bibitemWidest(BufferView * bv)
390 // Does look like a hack? It is! (but will change at 0.13)
391 LyXParagraph * par = current_view->buffer()->paragraph;
392 InsetBibKey * bkey = 0;
397 int wx = par->bibkey->width(bv, font);
406 if (bkey && !bkey->getScreenLabel().empty())
407 return bkey->getScreenLabel();