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"
30 FD_bibitem_form * bibitem_form = 0;
32 FD_bibitem_form * create_form_bibitem_form(void);
34 extern BufferView * current_view;
37 // called from both InsetBibKey and InsetBibtex dialogs yet cast off
38 // only to InsetBibKey holder. Real problems can ensue.
40 void bibitem_cb(FL_OBJECT *, long data)
42 InsetBibKey::Holder * holder =
43 static_cast<InsetBibKey::Holder*>
44 (bibitem_form->bibitem_form->u_vdata);
46 holder->inset->callback( bibitem_form, data );
50 FD_bibitem_form * create_form_bibitem_form(void)
53 FD_bibitem_form * fdui = (FD_bibitem_form *) fl_calloc(1, sizeof(FD_bibitem_form));
55 fdui->bibitem_form = fl_bgn_form(FL_NO_BOX, 220, 130);
56 obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
57 fdui->key = obj = fl_add_input(FL_NORMAL_INPUT, 80, 10, 130, 30, idex(_("Key:|#K")));
58 fl_set_input_shortcut(obj, scex(_("Key:|#K")), 1);
59 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
60 obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
61 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
62 fl_set_object_callback(obj, bibitem_cb, 1);
63 obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
64 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
65 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
66 fl_set_object_callback(obj, bibitem_cb, 0);
67 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Label:|#L")));
68 fl_set_input_shortcut(obj, scex(_("Label:|#L")), 1);
69 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
72 //fdui->bibitem_form->fdui = fdui;
78 InsetBibKey::InsetBibKey(InsetCommandParams const & p)
85 InsetBibKey::~InsetBibKey()
87 if (bibitem_form && bibitem_form->bibitem_form
88 && bibitem_form->bibitem_form->visible
89 && bibitem_form->bibitem_form->u_vdata == &holder)
90 fl_hide_form(bibitem_form->bibitem_form);
94 Inset * InsetBibKey::Clone(Buffer const &) const
96 InsetBibKey * b = new InsetBibKey(params());
97 b->setCounter(counter);
102 void InsetBibKey::callback( FD_bibitem_form * form, long data )
106 // Do NOT change this to
107 // holder.view->buffer() as this code is used by both
108 // InsetBibKey and InsetBibtex! Ughhhhhhh!!!!
109 if (!current_view->buffer()->isReadonly()) {
110 setContents(fl_get_input(form->key));
111 setOptions(fl_get_input(form->label));
112 // shouldn't mark the buffer dirty unless
113 // something was actually altered
114 current_view->updateInset( this, true );
115 } // fall through to Cancel
117 fl_hide_form(form->bibitem_form);
123 void InsetBibKey::setCounter(int c)
127 if (getCmdName().empty())
128 setCmdName( tostr(counter) );
132 // I'm sorry but this is still necessary because \bibitem is used also
133 // as a LyX 2.x command, and lyxlex is not enough smart to understand
134 // real LaTeX commands. Yes, that could be fixed, but would be a waste
135 // of time cause LyX3 won't use lyxlex anyway. (ale)
136 void InsetBibKey::Write(Buffer const *, ostream & os) const
139 if (! getOptions().empty()) {
141 << getOptions() << ']';
144 << getContents() << "}\n";
148 // This is necessary here because this is written without begin_inset
149 // This should be changed!!! (Jug)
150 void InsetBibKey::Read(Buffer const *, LyXLex & lex)
155 token = lex.GetString();
158 lex.printError("InsetCommand: Parse error: `$$Token'");
162 string const InsetBibKey::getScreenLabel() const
164 if (! getOptions().empty())
167 return tostr(counter);
172 The value in "Key:" isn't allways set right after a few bibkey insets have
173 been added/removed. Perhaps the wrong object is deleted/used somewhere
177 void InsetBibKey::Edit(BufferView * bv, int, int, unsigned int)
179 if (bv->buffer()->isReadonly())
180 WarnReadonly(bv->buffer()->fileName());
183 bibitem_form = create_form_bibitem_form();
184 fl_set_form_atclose(bibitem_form->bibitem_form,
185 CancelCloseBoxCB, 0);
191 bibitem_form->bibitem_form->u_vdata = &holder;
192 // InsetBibtex uses the same form, with different labels
193 fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
194 fl_set_button_shortcut(bibitem_form->key, scex(_("Key:|#K")), 1);
195 fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
196 fl_set_button_shortcut(bibitem_form->label, scex(_("Label:|#L")), 1);
197 fl_set_input(bibitem_form->key, getContents().c_str());
198 fl_set_input(bibitem_form->label, getOptions().c_str());
199 if (bibitem_form->bibitem_form->visible) {
200 fl_raise_form(bibitem_form->bibitem_form);
202 fl_show_form(bibitem_form->bibitem_form, FL_PLACE_MOUSE,
204 _("Bibliography item"));
209 InsetBibtex::InsetBibtex(InsetCommandParams const & p)
214 InsetBibtex::~InsetBibtex()
216 if (bibitem_form && bibitem_form->bibitem_form
217 && bibitem_form->bibitem_form->visible
218 && bibitem_form->bibitem_form->u_vdata == &holder)
219 fl_hide_form(bibitem_form->bibitem_form);
223 string const InsetBibtex::getScreenLabel() const
225 return _("BibTeX Generated References");
229 int InsetBibtex::Latex(Buffer const * buffer, ostream & os,
230 bool /*fragile*/, bool/*fs*/) const
232 // If we generate in a temp dir, we might need to give an
233 // absolute path there. This is a bit complicated since we can
234 // have a comma-separated list of bibliographies
236 string db_in = getContents();
237 db_in = split(db_in, adb, ',');
238 while(!adb.empty()) {
239 if (!buffer->niceFile &&
240 IsFileReadable(MakeAbsPath(adb, buffer->filepath)+".bib"))
241 adb = MakeAbsPath(adb, buffer->filepath);
245 db_in= split(db_in, adb,',');
247 db_out = strip(db_out, ',');
250 if (!buffer->niceFile
251 && IsFileReadable(MakeAbsPath(getOptions(), buffer->filepath)
253 style = MakeAbsPath(getOptions(), buffer->filepath);
255 style = getOptions();
257 os << "\\bibliographystyle{" << style << "}\n"
258 << "\\bibliography{" << db_out << "}\n";
263 // This method returns a comma separated list of Bibtex entries
264 vector<pair<string, string> > const InsetBibtex::getKeys(Buffer const * buffer) const
266 Path p(buffer->filepath);
268 vector<pair<string,string> > keys;
270 string bibfiles = getContents();
271 bibfiles = split(bibfiles, tmp, ',');
272 while(!tmp.empty()) {
273 string fil = findtexfile(ChangeExtension(tmp, "bib"),
275 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
276 // If we didn't find a matching file name just fail silently
278 // This is a _very_ simple parser for Bibtex database
279 // files. All it does is to look for lines starting
280 // in @ and not being @preamble and @string entries.
281 // It does NOT do any syntax checking!
282 ifstream ifs(fil.c_str());
284 while (getline(ifs, linebuf0)) {
285 string linebuf = frontStrip(strip(linebuf0));
286 if (linebuf.empty() ) continue;
287 if (prefixIs(linebuf, "@")) {
288 linebuf = subst(linebuf, '{', '(');
289 linebuf = split(linebuf, tmp, '(');
290 tmp = lowercase(tmp);
291 if (!prefixIs(tmp, "@string")
292 && !prefixIs(tmp, "@preamble")) {
293 linebuf = split(linebuf, tmp, ',');
294 tmp = frontStrip(tmp);
296 keys.push_back(pair<string,string>(tmp,string()));
299 } else if (!keys.empty()) {
300 keys.back().second += linebuf + "\n";
304 // Get next file name
305 bibfiles = split(bibfiles, tmp, ',');
311 // BibTeX should have its own dialog. This is provisional.
312 void InsetBibtex::Edit(BufferView * bv, int, int, unsigned int)
315 bibitem_form = create_form_bibitem_form();
316 fl_set_form_atclose(bibitem_form->bibitem_form,
317 CancelCloseBoxCB, 0);
322 bibitem_form->bibitem_form->u_vdata = &holder;
324 fl_set_object_label(bibitem_form->key, _("Database:"));
325 fl_set_object_label(bibitem_form->label, _("Style: "));
326 fl_set_input(bibitem_form->key, getContents().c_str());
327 fl_set_input(bibitem_form->label, getOptions().c_str());
328 if (bibitem_form->bibitem_form->visible) {
329 fl_raise_form(bibitem_form->bibitem_form);
331 fl_show_form(bibitem_form->bibitem_form,
332 FL_PLACE_MOUSE, FL_FULLBORDER,
338 bool InsetBibtex::addDatabase(string const & db)
340 string contents(getContents());
341 if (!contains(contents, db)) {
342 if (!contents.empty())
344 setContents(contents + db);
351 bool InsetBibtex::delDatabase(string const & db)
353 if (contains(getContents(), db)) {
355 int n = tokenPos(getContents(), ',', bd);
357 // Weird code, would someone care to explain this?(Lgb)
360 setContents(subst(getContents(), tmp, ", "));
362 setContents(split(getContents(), bd, ','));
370 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
371 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
374 // Does look like a hack? It is! (but will change at 0.13)
375 LyXParagraph * par = bv->buffer()->paragraph;
379 int wx = par->bibkey->width(bv, font);
389 string const bibitemWidest(Buffer const * buffer)
392 // Does look like a hack? It is! (but will change at 0.13)
393 LyXParagraph * par = buffer->paragraph;
394 BufferView * bv = buffer->getUser();
395 InsetBibKey * bkey = 0;
400 int wx = par->bibkey->width(bv, font);
409 if (bkey && !bkey->getScreenLabel().empty())
410 return bkey->getScreenLabel();