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 FD_bibitem_form * bibitem_form = 0;
31 FD_bibitem_form * create_form_bibitem_form(void);
35 void bibitem_cb(FL_OBJECT *, long data)
37 InsetBibKey::Holder * holder =
38 static_cast<InsetBibKey::Holder*>
39 (bibitem_form->bibitem_form->u_vdata);
41 holder->inset->callback( bibitem_form, data );
45 FD_bibitem_form * create_form_bibitem_form(void)
48 FD_bibitem_form * fdui = (FD_bibitem_form *) fl_calloc(1, sizeof(FD_bibitem_form));
50 fdui->bibitem_form = fl_bgn_form(FL_NO_BOX, 220, 130);
51 obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
52 fdui->key = obj = fl_add_input(FL_NORMAL_INPUT, 80, 10, 130, 30, idex(_("Key:|#K")));
53 fl_set_input_shortcut(obj, scex(_("Key:|#K")), 1);
54 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
55 obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
56 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
57 fl_set_object_callback(obj, bibitem_cb, 1);
58 obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
59 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
60 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
61 fl_set_object_callback(obj, bibitem_cb, 0);
62 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Label:|#L")));
63 fl_set_input_shortcut(obj, scex(_("Label:|#L")), 1);
64 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
67 //fdui->bibitem_form->fdui = fdui;
73 InsetBibKey::InsetBibKey(InsetCommandParams const & p)
80 InsetBibKey::~InsetBibKey()
82 if(bibitem_form && bibitem_form->bibitem_form
83 && bibitem_form->bibitem_form->visible
84 && bibitem_form->bibitem_form->u_vdata == &holder)
85 fl_hide_form(bibitem_form->bibitem_form);
89 Inset * InsetBibKey::Clone() const
91 InsetBibKey * b = new InsetBibKey(params());
92 b->setCounter(counter);
97 void InsetBibKey::callback( FD_bibitem_form * form, long data )
101 if(!holder.view->buffer()->isReadonly()) {
102 setContents(fl_get_input(form->key));
103 setOptions(fl_get_input(form->label));
104 // shouldn't mark the buffer dirty unless
105 // something was actually altered
106 holder.view->updateInset( this, true );
107 } // fall through to Cancel
109 fl_hide_form(form->bibitem_form);
115 void InsetBibKey::setCounter(int c)
119 if (getCmdName().empty())
120 setCmdName( tostr(counter) );
124 // I'm sorry but this is still necessary because \bibitem is used also
125 // as a LyX 2.x command, and lyxlex is not enough smart to understand
126 // real LaTeX commands. Yes, that could be fixed, but would be a waste
127 // of time cause LyX3 won't use lyxlex anyway. (ale)
128 void InsetBibKey::Write(Buffer const *, ostream & os) const
131 if (! getOptions().empty()) {
133 << getOptions() << ']';
136 << getContents() << "}\n";
140 // This is necessary here because this is written without begin_inset
141 // This should be changed!!! (Jug)
142 void InsetBibKey::Read(Buffer const *, LyXLex & lex)
147 token = lex.GetString();
150 lex.printError("InsetCommand: Parse error: `$$Token'");
154 string const InsetBibKey::getScreenLabel() const
156 if (! getOptions().empty())
159 return tostr(counter);
164 The value in "Key:" isn't allways set right after a few bibkey insets have
165 been added/removed. Perhaps the wrong object is deleted/used somewhere
169 void InsetBibKey::Edit(BufferView * bv, int, int, unsigned int)
171 if(bv->buffer()->isReadonly())
172 WarnReadonly(bv->buffer()->fileName());
175 bibitem_form = create_form_bibitem_form();
176 fl_set_form_atclose(bibitem_form->bibitem_form,
177 CancelCloseBoxCB, 0);
183 bibitem_form->bibitem_form->u_vdata = &holder;
184 // InsetBibtex uses the same form, with different labels
185 fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
186 fl_set_button_shortcut(bibitem_form->key, scex(_("Key:|#K")), 1);
187 fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
188 fl_set_button_shortcut(bibitem_form->label, scex(_("Label:|#L")), 1);
189 fl_set_input(bibitem_form->key, getContents().c_str());
190 fl_set_input(bibitem_form->label, getOptions().c_str());
191 if (bibitem_form->bibitem_form->visible) {
192 fl_raise_form(bibitem_form->bibitem_form);
194 fl_show_form(bibitem_form->bibitem_form, FL_PLACE_MOUSE,
196 _("Bibliography item"));
201 InsetBibtex::InsetBibtex(InsetCommandParams const & p)
206 InsetBibtex::~InsetBibtex()
208 if(bibitem_form && bibitem_form->bibitem_form
209 && bibitem_form->bibitem_form->visible
210 && bibitem_form->bibitem_form->u_vdata == &holder)
211 fl_hide_form(bibitem_form->bibitem_form);
215 string const InsetBibtex::getScreenLabel() const
217 return _("BibTeX Generated References");
221 int InsetBibtex::Latex(Buffer const * buffer, ostream & os,
222 bool /*fragile*/, bool/*fs*/) const
224 // If we generate in a temp dir, we might need to give an
225 // absolute path there. This is a bit complicated since we can
226 // have a comma-separated list of bibliographies
228 string db_in = getContents();
229 db_in = split(db_in, adb, ',');
230 while(!adb.empty()) {
231 if (!buffer->niceFile &&
232 IsFileReadable(MakeAbsPath(adb, buffer->filepath)+".bib"))
233 adb = MakeAbsPath(adb, buffer->filepath);
237 db_in= split(db_in, adb,',');
239 db_out = strip(db_out, ',');
242 if (!buffer->niceFile
243 && IsFileReadable(MakeAbsPath(getOptions(), buffer->filepath)
245 style = MakeAbsPath(getOptions(), buffer->filepath);
247 style = getOptions();
249 os << "\\bibliographystyle{" << style << "}\n"
250 << "\\bibliography{" << db_out << "}\n";
255 // This method returns a comma separated list of Bibtex entries
256 vector<pair<string, string> > const InsetBibtex::getKeys(Buffer const * buffer) const
258 Path p(buffer->filepath);
260 vector<pair<string,string> > keys;
262 string bibfiles = getContents();
263 bibfiles = split(bibfiles, tmp, ',');
264 while(!tmp.empty()) {
265 string fil = findtexfile(ChangeExtension(tmp, "bib"),
267 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
268 // If we didn't find a matching file name just fail silently
270 // This is a _very_ simple parser for Bibtex database
271 // files. All it does is to look for lines starting
272 // in @ and not being @preamble and @string entries.
273 // It does NOT do any syntax checking!
274 ifstream ifs(fil.c_str());
276 while (getline(ifs, linebuf0)) {
277 string linebuf = frontStrip(strip(linebuf0));
278 if( linebuf.empty() ) continue;
279 if (prefixIs(linebuf, "@")) {
280 linebuf = subst(linebuf, '{', '(');
281 linebuf = split(linebuf, tmp, '(');
282 tmp = lowercase(tmp);
283 if (!prefixIs(tmp, "@string")
284 && !prefixIs(tmp, "@preamble")) {
285 linebuf = split(linebuf, tmp, ',');
286 tmp = frontStrip(tmp);
288 keys.push_back(pair<string,string>(tmp,string()));
291 } else if( !keys.empty() ) {
292 keys.back().second += linebuf + "\n";
296 // Get next file name
297 bibfiles = split(bibfiles, tmp, ',');
303 // BibTeX should have its own dialog. This is provisional.
304 void InsetBibtex::Edit(BufferView * bv, int, int, unsigned int)
307 bibitem_form = create_form_bibitem_form();
308 fl_set_form_atclose(bibitem_form->bibitem_form,
309 CancelCloseBoxCB, 0);
314 bibitem_form->bibitem_form->u_vdata = &holder;
316 fl_set_object_label(bibitem_form->key, _("Database:"));
317 fl_set_object_label(bibitem_form->label, _("Style: "));
318 fl_set_input(bibitem_form->key, getContents().c_str());
319 fl_set_input(bibitem_form->label, getOptions().c_str());
320 if (bibitem_form->bibitem_form->visible) {
321 fl_raise_form(bibitem_form->bibitem_form);
323 fl_show_form(bibitem_form->bibitem_form,
324 FL_PLACE_MOUSE, FL_FULLBORDER,
330 bool InsetBibtex::addDatabase(string const & db)
332 string contents(getContents());
333 if (!contains(contents, db)) {
334 if (!contents.empty())
336 setContents(contents + db);
343 bool InsetBibtex::delDatabase(string const & db)
345 if (contains(getContents(), db)) {
347 int n = tokenPos(getContents(), ',', bd);
349 // Weird code, would someone care to explain this?(Lgb)
352 setContents(subst(getContents(), tmp, ", "));
354 setContents(split(getContents(), bd, ','));
362 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
363 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
366 // Does look like a hack? It is! (but will change at 0.13)
367 LyXParagraph * par = bv->buffer()->paragraph;
371 int wx = par->bibkey->width(bv, font);
381 string const bibitemWidest(BufferView * bv)
384 // Does look like a hack? It is! (but will change at 0.13)
385 LyXParagraph * par = bv->buffer()->paragraph;
386 InsetBibKey * bkey = 0;
391 int wx = par->bibkey->width(bv, font);
400 if (bkey && !bkey->getScreenLabel().empty())
401 return bkey->getScreenLabel();