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);
36 void bibitem_cb(FL_OBJECT *, long data)
38 InsetBibKey::Holder * holder =
39 static_cast<InsetBibKey::Holder*>
40 (bibitem_form->bibitem_form->u_vdata);
42 holder->inset->callback( bibitem_form, data );
46 FD_bibitem_form * create_form_bibitem_form(void)
49 FD_bibitem_form * fdui = (FD_bibitem_form *) fl_calloc(1, sizeof(FD_bibitem_form));
51 fdui->bibitem_form = fl_bgn_form(FL_NO_BOX, 220, 130);
52 obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
53 fdui->key = obj = fl_add_input(FL_NORMAL_INPUT, 80, 10, 130, 30, idex(_("Key:|#K")));
54 fl_set_input_shortcut(obj, scex(_("Key:|#K")), 1);
55 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
56 obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
57 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
58 fl_set_object_callback(obj, bibitem_cb, 1);
59 obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
60 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
61 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
62 fl_set_object_callback(obj, bibitem_cb, 0);
63 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Label:|#L")));
64 fl_set_input_shortcut(obj, scex(_("Label:|#L")), 1);
65 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
68 //fdui->bibitem_form->fdui = fdui;
74 InsetBibKey::InsetBibKey(InsetCommandParams const & p)
81 InsetBibKey::~InsetBibKey()
83 if(bibitem_form && bibitem_form->bibitem_form
84 && bibitem_form->bibitem_form->visible
85 && bibitem_form->bibitem_form->u_vdata == &holder)
86 fl_hide_form(bibitem_form->bibitem_form);
90 Inset * InsetBibKey::Clone() const
92 InsetBibKey * b = new InsetBibKey(params());
93 b->setCounter(counter);
98 void InsetBibKey::callback( FD_bibitem_form * form, long data )
102 if(!holder.view->buffer()->isReadonly()) {
103 setContents(fl_get_input(form->key));
104 setOptions(fl_get_input(form->label));
105 // shouldn't mark the buffer dirty unless
106 // something was actually altered
107 holder.view->updateInset( this, true );
108 } // fall through to Cancel
110 fl_hide_form(form->bibitem_form);
116 void InsetBibKey::setCounter(int c)
120 if (getCmdName().empty())
121 setCmdName( tostr(counter) );
125 // I'm sorry but this is still necessary because \bibitem is used also
126 // as a LyX 2.x command, and lyxlex is not enough smart to understand
127 // real LaTeX commands. Yes, that could be fixed, but would be a waste
128 // of time cause LyX3 won't use lyxlex anyway. (ale)
129 void InsetBibKey::Write(Buffer const *, ostream & os) const
132 if (! getOptions().empty()) {
134 << getOptions() << ']';
137 << getContents() << "}\n";
141 // This is necessary here because this is written without begin_inset
142 // This should be changed!!! (Jug)
143 void InsetBibKey::Read(Buffer const *, LyXLex & lex)
148 token = lex.GetString();
151 lex.printError("InsetCommand: Parse error: `$$Token'");
155 string const InsetBibKey::getScreenLabel() const
157 if (! getOptions().empty())
160 return tostr(counter);
165 The value in "Key:" isn't allways set right after a few bibkey insets have
166 been added/removed. Perhaps the wrong object is deleted/used somewhere
170 void InsetBibKey::Edit(BufferView * bv, int, int, unsigned int)
172 if(bv->buffer()->isReadonly())
173 WarnReadonly(bv->buffer()->fileName());
176 bibitem_form = create_form_bibitem_form();
177 fl_set_form_atclose(bibitem_form->bibitem_form,
178 CancelCloseBoxCB, 0);
184 bibitem_form->bibitem_form->u_vdata = &holder;
185 // InsetBibtex uses the same form, with different labels
186 fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
187 fl_set_button_shortcut(bibitem_form->key, scex(_("Key:|#K")), 1);
188 fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
189 fl_set_button_shortcut(bibitem_form->label, scex(_("Label:|#L")), 1);
190 fl_set_input(bibitem_form->key, getContents().c_str());
191 fl_set_input(bibitem_form->label, getOptions().c_str());
192 if (bibitem_form->bibitem_form->visible) {
193 fl_raise_form(bibitem_form->bibitem_form);
195 fl_show_form(bibitem_form->bibitem_form, FL_PLACE_MOUSE,
197 _("Bibliography item"));
202 InsetBibtex::InsetBibtex(InsetCommandParams const & p)
207 InsetBibtex::~InsetBibtex()
209 if(bibitem_form && bibitem_form->bibitem_form
210 && bibitem_form->bibitem_form->visible
211 && bibitem_form->bibitem_form->u_vdata == &holder)
212 fl_hide_form(bibitem_form->bibitem_form);
216 string const InsetBibtex::getScreenLabel() const
218 return _("BibTeX Generated References");
222 int InsetBibtex::Latex(Buffer const * buffer, ostream & os,
223 bool /*fragile*/, bool/*fs*/) const
225 // If we generate in a temp dir, we might need to give an
226 // absolute path there. This is a bit complicated since we can
227 // have a comma-separated list of bibliographies
229 string db_in = getContents();
230 db_in = split(db_in, adb, ',');
231 while(!adb.empty()) {
232 if (!buffer->niceFile &&
233 IsFileReadable(MakeAbsPath(adb, buffer->filepath)+".bib"))
234 adb = MakeAbsPath(adb, buffer->filepath);
238 db_in= split(db_in, adb,',');
240 db_out = strip(db_out, ',');
243 if (!buffer->niceFile
244 && IsFileReadable(MakeAbsPath(getOptions(), buffer->filepath)
246 style = MakeAbsPath(getOptions(), buffer->filepath);
248 style = getOptions();
250 os << "\\bibliographystyle{" << style << "}\n"
251 << "\\bibliography{" << db_out << "}\n";
256 // This method returns a comma separated list of Bibtex entries
257 vector<pair<string, string> > const InsetBibtex::getKeys(Buffer const * buffer) const
259 Path p(buffer->filepath);
261 vector<pair<string,string> > keys;
263 string bibfiles = getContents();
264 bibfiles = split(bibfiles, tmp, ',');
265 while(!tmp.empty()) {
266 string fil = findtexfile(ChangeExtension(tmp, "bib"),
268 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
269 // If we didn't find a matching file name just fail silently
271 // This is a _very_ simple parser for Bibtex database
272 // files. All it does is to look for lines starting
273 // in @ and not being @preamble and @string entries.
274 // It does NOT do any syntax checking!
275 ifstream ifs(fil.c_str());
277 while (getline(ifs, linebuf0)) {
278 string linebuf = frontStrip(strip(linebuf0));
279 if( linebuf.empty() ) continue;
280 if (prefixIs(linebuf, "@")) {
281 linebuf = subst(linebuf, '{', '(');
282 linebuf = split(linebuf, tmp, '(');
283 tmp = lowercase(tmp);
284 if (!prefixIs(tmp, "@string")
285 && !prefixIs(tmp, "@preamble")) {
286 linebuf = split(linebuf, tmp, ',');
287 tmp = frontStrip(tmp);
289 keys.push_back(pair<string,string>(tmp,string()));
292 } else if( !keys.empty() ) {
293 keys.back().second += linebuf + "\n";
297 // Get next file name
298 bibfiles = split(bibfiles, tmp, ',');
304 // BibTeX should have its own dialog. This is provisional.
305 void InsetBibtex::Edit(BufferView * bv, int, int, unsigned int)
308 bibitem_form = create_form_bibitem_form();
309 fl_set_form_atclose(bibitem_form->bibitem_form,
310 CancelCloseBoxCB, 0);
315 bibitem_form->bibitem_form->u_vdata = &holder;
317 fl_set_object_label(bibitem_form->key, _("Database:"));
318 fl_set_object_label(bibitem_form->label, _("Style: "));
319 fl_set_input(bibitem_form->key, getContents().c_str());
320 fl_set_input(bibitem_form->label, getOptions().c_str());
321 if (bibitem_form->bibitem_form->visible) {
322 fl_raise_form(bibitem_form->bibitem_form);
324 fl_show_form(bibitem_form->bibitem_form,
325 FL_PLACE_MOUSE, FL_FULLBORDER,
331 bool InsetBibtex::addDatabase(string const & db)
333 string contents(getContents());
334 if (!contains(contents, db)) {
335 if (!contents.empty())
337 setContents(contents + db);
344 bool InsetBibtex::delDatabase(string const & db)
346 if (contains(getContents(), db)) {
348 int n = tokenPos(getContents(), ',', bd);
350 // Weird code, would someone care to explain this?(Lgb)
353 setContents(subst(getContents(), tmp, ", "));
355 setContents(split(getContents(), bd, ','));
363 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
364 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
367 // Does look like a hack? It is! (but will change at 0.13)
368 LyXParagraph * par = bv->buffer()->paragraph;
372 int wx = par->bibkey->width(bv, font);
382 string const bibitemWidest(Buffer const * buffer)
385 // Does look like a hack? It is! (but will change at 0.13)
386 LyXParagraph * par = buffer->paragraph;
387 BufferView * bv = buffer->getUser();
388 InsetBibKey * bkey = 0;
393 int wx = par->bibkey->width(bv, font);
402 if (bkey && !bkey->getScreenLabel().empty())
403 return bkey->getScreenLabel();