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);
38 void bibitem_cb(FL_OBJECT *, long data)
40 InsetBibKey::Holder * holder =
41 static_cast<InsetBibKey::Holder*>
42 (bibitem_form->bibitem_form->u_vdata);
44 holder->inset->callback( bibitem_form, data );
48 FD_bibitem_form * create_form_bibitem_form(void)
51 FD_bibitem_form * fdui = (FD_bibitem_form *) fl_calloc(1, sizeof(FD_bibitem_form));
53 fdui->bibitem_form = fl_bgn_form(FL_NO_BOX, 220, 130);
54 obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
55 fdui->key = obj = fl_add_input(FL_NORMAL_INPUT, 80, 10, 130, 30, idex(_("Key:|#K")));
56 fl_set_input_shortcut(obj, scex(_("Key:|#K")), 1);
57 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
58 obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
59 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
60 fl_set_object_callback(obj, bibitem_cb, 1);
61 obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
62 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
63 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
64 fl_set_object_callback(obj, bibitem_cb, 0);
65 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Label:|#L")));
66 fl_set_input_shortcut(obj, scex(_("Label:|#L")), 1);
67 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
70 //fdui->bibitem_form->fdui = fdui;
76 InsetBibKey::InsetBibKey(string const & key, string const & label)
77 : InsetCommand("bibitem", key, label)
85 InsetBibKey::InsetBibKey(InsetBibKey const * b)
86 : InsetCommand("bibitem", b->getContents(), b->getOptions())
92 InsetBibKey::~InsetBibKey()
94 if(bibitem_form && bibitem_form->bibitem_form
95 && bibitem_form->bibitem_form->visible
96 && bibitem_form->bibitem_form->u_vdata == &holder)
97 fl_hide_form(bibitem_form->bibitem_form);
101 void InsetBibKey::callback( FD_bibitem_form * form, long data )
105 if(!holder.view->buffer()->isReadonly()) {
106 setContents(fl_get_input(form->key));
107 setOptions(fl_get_input(form->label));
108 // shouldn't mark the buffer dirty unless
109 // something was actually altered
110 holder.view->updateInset( this, true );
111 } // fall through to Cancel
113 fl_hide_form(form->bibitem_form);
119 void InsetBibKey::setCounter(int c)
123 if (getCmdName().empty())
124 setCmdName( tostr(counter) );
128 // I'm sorry but this is still necessary because \bibitem is used also
129 // as a LyX 2.x command, and lyxlex is not enough smart to understand
130 // real LaTeX commands. Yes, that could be fixed, but would be a waste
131 // of time cause LyX3 won't use lyxlex anyway. (ale)
132 void InsetBibKey::Write(Buffer const *, ostream & os) const
135 if (! getOptions().empty()) {
137 << getOptions() << ']';
140 << getContents() << "}\n";
144 // This is necessary here because this is written without begin_inset
145 // This should be changed!!! (Jug)
146 void InsetBibKey::Read(Buffer const *, LyXLex & lex)
151 token = lex.GetString();
154 lex.printError("InsetCommand: Parse error: `$$Token'");
158 string InsetBibKey::getScreenLabel() const
160 if (! getOptions().empty())
163 return tostr(counter);
168 The value in "Key:" isn't allways set right after a few bibkey insets have
169 been added/removed. Perhaps the wrong object is deleted/used somewhere
173 void InsetBibKey::Edit(BufferView * bv, int, int, unsigned int)
175 if(bv->buffer()->isReadonly())
176 WarnReadonly(bv->buffer()->fileName());
179 bibitem_form = create_form_bibitem_form();
180 fl_set_form_atclose(bibitem_form->bibitem_form,
181 CancelCloseBoxCB, 0);
187 bibitem_form->bibitem_form->u_vdata = &holder;
188 // InsetBibtex uses the same form, with different labels
189 fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
190 fl_set_button_shortcut(bibitem_form->key, scex(_("Key:|#K")), 1);
191 fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
192 fl_set_button_shortcut(bibitem_form->label, scex(_("Label:|#L")), 1);
193 fl_set_input(bibitem_form->key, getContents().c_str());
194 fl_set_input(bibitem_form->label, getOptions().c_str());
195 if (bibitem_form->bibitem_form->visible) {
196 fl_raise_form(bibitem_form->bibitem_form);
198 fl_show_form(bibitem_form->bibitem_form, FL_PLACE_MOUSE,
200 _("Bibliography item"));
205 InsetBibtex::InsetBibtex(string const & dbase, string const & style,
207 : InsetCommand("BibTeX", dbase, style), owner(o)
211 InsetBibtex::~InsetBibtex()
213 if(bibitem_form && bibitem_form->bibitem_form
214 && bibitem_form->bibitem_form->visible
215 && bibitem_form->bibitem_form->u_vdata == &holder)
216 fl_hide_form(bibitem_form->bibitem_form);
220 string InsetBibtex::getScreenLabel() const
222 return _("BibTeX Generated References");
226 int InsetBibtex::Latex(Buffer const *, ostream & os,
227 bool /*fragile*/, bool/*fs*/) const
229 // this looks like an horrible hack and it is :) The problem
230 // is that owner is not initialized correctly when the bib
231 // inset is cut and pasted. Such hacks will not be needed
234 owner = current_view->buffer();
236 // If we generate in a temp dir, we might need to give an
237 // absolute path there. This is a bit complicated since we can
238 // have a comma-separated list of bibliographies
240 string db_in = getContents();
241 db_in = split(db_in, adb, ',');
242 while(!adb.empty()) {
243 if (!owner->niceFile &&
244 IsFileReadable(MakeAbsPath(adb, owner->filepath)+".bib"))
245 adb = MakeAbsPath(adb, owner->filepath);
249 db_in= split(db_in, adb,',');
251 db_out = strip(db_out, ',');
255 && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
257 style = MakeAbsPath(getOptions(), owner->filepath);
259 style = getOptions();
261 os << "\\bibliographystyle{" << style << "}\n"
262 << "\\bibliography{" << db_out << "}\n";
267 // This method returns a comma separated list of Bibtex entries
268 vector<pair<string,string> > InsetBibtex::getKeys() const
270 // This hack is copied from InsetBibtex::Latex.
271 // Is it still needed? Probably yes.
272 // Why is this needed here when it already is in Latex?
273 // Anyway we need a different way to get to the
274 // buffer the inset is in. (Lgb)
277 // owner = current_view->buffer();
280 Path p(owner->filepath);
282 vector<pair<string,string> > keys;
284 string bibfiles = getContents();
285 bibfiles = split(bibfiles, tmp, ',');
286 while(!tmp.empty()) {
287 string fil = findtexfile(ChangeExtension(tmp, "bib"),
289 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
290 // If we didn't find a matching file name just fail silently
292 // This is a _very_ simple parser for Bibtex database
293 // files. All it does is to look for lines starting
294 // in @ and not being @preamble and @string entries.
295 // It does NOT do any syntax checking!
296 ifstream ifs(fil.c_str());
298 while (getline(ifs, linebuf0)) {
299 string linebuf = frontStrip(strip(linebuf0));
300 if( linebuf.empty() ) continue;
301 if (prefixIs(linebuf, "@")) {
302 linebuf = subst(linebuf, '{', '(');
303 linebuf = split(linebuf, tmp, '(');
304 tmp = lowercase(tmp);
305 if (!prefixIs(tmp, "@string")
306 && !prefixIs(tmp, "@preamble")) {
307 linebuf = split(linebuf, tmp, ',');
308 tmp = frontStrip(tmp);
310 keys.push_back(pair<string,string>(tmp,string()));
313 } else if( !keys.empty() ) {
314 keys.back().second += linebuf + "\n";
318 // Get next file name
319 bibfiles = split(bibfiles, tmp, ',');
325 // BibTeX should have its own dialog. This is provisional.
326 void InsetBibtex::Edit(BufferView * bv, int, int, unsigned int)
329 bibitem_form = create_form_bibitem_form();
330 fl_set_form_atclose(bibitem_form->bibitem_form,
331 CancelCloseBoxCB, 0);
336 bibitem_form->bibitem_form->u_vdata = &holder;
338 fl_set_object_label(bibitem_form->key, _("Database:"));
339 fl_set_object_label(bibitem_form->label, _("Style: "));
340 fl_set_input(bibitem_form->key, getContents().c_str());
341 fl_set_input(bibitem_form->label, getOptions().c_str());
342 if (bibitem_form->bibitem_form->visible) {
343 fl_raise_form(bibitem_form->bibitem_form);
345 fl_show_form(bibitem_form->bibitem_form,
346 FL_PLACE_MOUSE, FL_FULLBORDER,
352 bool InsetBibtex::addDatabase(string const & db)
354 if (!contains(getContents(), db.c_str())) {
355 if (!getContents().empty())
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();