12 #include FORMS_H_LOCATION
17 #include "lyx_gui_misc.h"
18 #include "BufferView.h"
22 #include "support/filetools.h"
24 extern BufferView * current_view;
26 FD_citation_form * citation_form = 0;
27 FD_bibitem_form * bibitem_form = 0;
28 static Combox * bibcombox = 0;
30 void BibitemUpdate(Combox *);
31 FD_citation_form * create_form_citation_form(void);
32 FD_bibitem_form * create_form_bibitem_form(void);
35 extern "C" void bibitem_cb(FL_OBJECT *, long data)
38 case 1: // OK, citation
40 InsetCitation::Holder * holder =
41 static_cast<InsetCitation::Holder*>
42 (citation_form->citation_form->u_vdata);
43 if(!holder->view->buffer()->isReadonly()) {
45 InsetCitation * inset = holder->inset;
46 inset->setContents(bibcombox->getline());
47 inset->setOptions(fl_get_input(citation_form->label));
48 fl_hide_form(citation_form->citation_form);
49 // shouldn't mark the buffer dirty unless something
50 // was actually altered
51 holder->view->updateInset(inset, true);
54 // fall through to Cancel on RO-mode
56 case 0: fl_hide_form(citation_form->citation_form);
58 case 3: // OK, bibitem
60 InsetBibKey::Holder * holder =
61 static_cast<InsetBibKey::Holder*>
62 (bibitem_form->bibitem_form->u_vdata);
64 if(!holder->view->buffer()->isReadonly()) {
65 InsetBibKey * inset = holder->inset;
66 inset->setContents(fl_get_input(bibitem_form->key));
67 inset->setOptions(fl_get_input(bibitem_form->label));
68 fl_hide_form(bibitem_form->bibitem_form);
69 // Does look like a hack? It is! (but will change at 0.13)
70 holder->view->text->RedoParagraph();
71 holder->view->update(1);
73 } // fall through to Cancel on RO-mode
75 case 2: // Cancel, bibitem
76 fl_hide_form(bibitem_form->bibitem_form); // Cancel, bibitem
82 FD_citation_form * create_form_citation_form(void)
85 FD_citation_form * fdui = (FD_citation_form *) fl_calloc(1, sizeof(FD_citation_form));
87 fdui->citation_form = fl_bgn_form(FL_NO_BOX, 220, 130);
88 obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
89 fdui->key = obj = fl_add_text(FL_NORMAL_TEXT, 20, 10, 60, 30, _("Key:"));
90 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
91 fl_set_object_lalign(obj, FL_ALIGN_RIGHT);
93 bibcombox = new Combox(FL_COMBOX_INPUT);
94 bibcombox->add(80, 10, 130, 30, 300);
96 obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
97 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
98 fl_set_object_callback(obj, bibitem_cb, 1);
99 obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
100 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
101 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
102 fl_set_object_callback(obj, bibitem_cb, 0);
103 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Remark:|#R")));
104 fl_set_input_shortcut(obj, scex(_("Remark:|#R")), 1);
105 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
108 //fdui->citation_form->fdui = fdui;
112 /*---------------------------------------*/
114 FD_bibitem_form * create_form_bibitem_form(void)
117 FD_bibitem_form * fdui = (FD_bibitem_form *) fl_calloc(1, sizeof(FD_bibitem_form));
119 fdui->bibitem_form = fl_bgn_form(FL_NO_BOX, 220, 130);
120 obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
121 fdui->key = obj = fl_add_input(FL_NORMAL_INPUT, 80, 10, 130, 30, idex(_("Key:|#K")));
122 fl_set_input_shortcut(obj, scex(_("Key:|#K")), 1);
123 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
124 obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
125 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
126 fl_set_object_callback(obj, bibitem_cb, 3);
127 obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
128 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
129 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
130 fl_set_object_callback(obj, bibitem_cb, 2);
131 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Label:|#L")));
132 fl_set_input_shortcut(obj, scex(_("Label:|#L")), 1);
133 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
136 //fdui->bibitem_form->fdui = fdui;
140 /*---------------------------------------*/
143 InsetCitation::InsetCitation(string const & key, string const & note)
144 : InsetCommand("cite", key, note)
150 InsetCitation::~InsetCitation()
152 if(citation_form && citation_form->citation_form
153 && citation_form->citation_form->visible
154 && citation_form->citation_form->u_vdata == &holder)
155 fl_hide_form(citation_form->citation_form);
159 void InsetCitation::Edit(BufferView * bv, int, int, unsigned int)
161 if(bv->buffer()->isReadonly())
162 WarnReadonly(bv->buffer()->fileName());
164 if (!citation_form) {
165 citation_form = create_form_citation_form();
166 fl_set_form_atclose(citation_form->citation_form,
167 CancelCloseBoxCB, 0);
173 citation_form->citation_form->u_vdata = &holder;
175 BibitemUpdate(bibcombox);
176 if (!bibcombox->select_text(getContents().c_str()))
177 bibcombox->addline(getContents().c_str());
179 fl_set_input(citation_form->label, getOptions().c_str());
180 if (citation_form->citation_form->visible) {
181 fl_raise_form(citation_form->citation_form);
183 fl_show_form(citation_form->citation_form,
184 FL_PLACE_MOUSE, FL_FULLBORDER,
190 string InsetCitation::getScreenLabel() const
196 if (!options.empty()) {
197 temp += ", " + options;
204 InsetBibKey::InsetBibKey(string const & key, string const & label):
205 InsetCommand("bibitem", key, label)
213 InsetBibKey::InsetBibKey(InsetBibKey const * b):
214 InsetCommand("bibitem", b->contents, b->options)
216 counter = b->counter;
220 InsetBibKey::~InsetBibKey()
222 if(bibitem_form && bibitem_form->bibitem_form
223 && bibitem_form->bibitem_form->visible)
224 fl_hide_form(bibitem_form->bibitem_form);
227 void InsetBibKey::setCounter(int c)
231 if (contents.empty())
232 contents += tostr(counter);
236 // I'm sorry but this is still necessary because \bibitem is used also
237 // as a LyX 2.x command, and lyxlex is not enough smart to understand
238 // real LaTeX commands. Yes, that could be fixed, but would be a waste
239 // of time cause LyX3 won't use lyxlex anyway. (ale)
240 void InsetBibKey::Write(ostream & os) const
242 #ifdef USE_OSTREAM_ONLY
244 if (!options.empty()) {
249 << contents << "}\n";
252 if (!options.empty()) {
258 os << "\\bibitem " << s << "\n";
263 string InsetBibKey::getScreenLabel() const
265 if (!options.empty())
268 return tostr(counter);
273 The value in "Key:" isn't allways set right after a few bibkey insets have
274 been added/removed. Perhaps the wrong object is deleted/used somewhere
278 void InsetBibKey::Edit(BufferView * bv, int, int, unsigned int)
280 if(bv->buffer()->isReadonly())
281 WarnReadonly(bv->buffer()->fileName());
284 bibitem_form = create_form_bibitem_form();
285 fl_set_form_atclose(bibitem_form->bibitem_form,
286 CancelCloseBoxCB, 0);
292 bibitem_form->bibitem_form->u_vdata = &holder;
293 // InsetBibtex uses the same form, with different labels
294 fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
295 fl_set_button_shortcut(bibitem_form->key, scex(_("Key:|#K")), 1);
296 fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
297 fl_set_button_shortcut(bibitem_form->label, scex(_("Label:|#L")), 1);
298 fl_set_input(bibitem_form->key, getContents().c_str());
299 fl_set_input(bibitem_form->label, getOptions().c_str());
300 if (bibitem_form->bibitem_form->visible) {
301 fl_raise_form(bibitem_form->bibitem_form);
303 fl_show_form(bibitem_form->bibitem_form, FL_PLACE_MOUSE,
305 _("Bibliography item"));
311 InsetBibtex::InsetBibtex(string const & dbase, string const & style,
313 : InsetCommand("BibTeX", dbase, style), owner(o)
318 string InsetBibtex::getScreenLabel() const
320 return _("BibTeX Generated References");
324 int InsetBibtex::Latex(ostream & os, signed char /*fragile*/, bool/*fs*/) const
326 #ifdef USE_OSTREAM_ONLY
327 // this looks like an horrible hack and it is :) The problem
328 // is that owner is not initialized correctly when the bib
329 // inset is cut and pasted. Such hacks will not be needed
332 owner = current_view->buffer();
334 // If we generate in a temp dir, we might need to give an
335 // absolute path there. This is a bit complicated since we can
336 // have a comma-separated list of bibliographies
338 string db_in = getContents();
339 db_in = split(db_in, adb, ',');
340 while(!adb.empty()) {
341 if (!owner->niceFile &&
342 IsFileReadable(MakeAbsPath(adb, owner->filepath)+".bib"))
343 adb = MakeAbsPath(adb, owner->filepath);
346 db_in= split(db_in, adb,',');
348 db_out = strip(db_out, ',');
352 && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
354 style = MakeAbsPath(getOptions(), owner->filepath);
356 style = getOptions();
358 os << "\\bibliographystyle{" << style << "}\n"
359 << "\\bibliography{" << db_out << "}\n";
363 signed char dummy = 0;
364 int result = Latex(bib, dummy, 0);
371 #ifndef USE_OSTREAM_ONLY
372 int InsetBibtex::Latex(string & file, signed char /*fragile*/, bool/*fs*/) const
374 // this looks like an horrible hack and it is :) The problem
375 // is that owner is not initialized correctly when the bib
376 // inset is cut and pasted. Such hacks will not be needed
379 owner = current_view->buffer();
381 // If we generate in a temp dir, we might need to give an
382 // absolute path there. This is a bit complicated since we can
383 // have a comma-separated list of bibliographies
385 string db_in = getContents();
386 db_in = split(db_in, adb, ',');
387 while(!adb.empty()) {
388 if (!owner->niceFile &&
389 IsFileReadable(MakeAbsPath(adb, owner->filepath)+".bib"))
390 adb = MakeAbsPath(adb, owner->filepath);
393 db_in= split(db_in, adb,',');
395 db_out = strip(db_out, ',');
399 && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
401 style = MakeAbsPath(getOptions(), owner->filepath);
403 style = getOptions();
405 file += "\\bibliographystyle{";
408 file += "\\bibliography{";
415 // This method returns a comma separated list of Bibtex entries
416 string InsetBibtex::getKeys(char delim)
418 // This hack is copied from InsetBibtex::Latex.
419 // Is it still needed? Probably yes.
420 // Why is this needed here when it already is in Latex?
421 // Anyway we need a different way to get to the
422 // buffer the inset is in. (Lgb)
425 // owner = current_view->buffer();
429 string bibfiles = getContents();
430 bibfiles = split(bibfiles, tmp, ',');
431 while(!tmp.empty()) {
432 string fil = findtexfile(ChangeExtension(tmp, "bib", false),
434 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
435 // If we didn't find a matching file name just fail silently
437 // This is a _very_ simple parser for Bibtex database
438 // files. All it does is to look for lines starting
439 // in @ and not being @preamble and @string entries.
440 // It does NOT do any syntax checking!
441 ifstream ifs(fil.c_str());
443 while (getline(ifs, linebuf)) {
444 linebuf = frontStrip(linebuf);
445 if (prefixIs(linebuf, "@")) {
446 linebuf = subst(linebuf, '{', '(');
447 linebuf = split(linebuf, tmp, '(');
448 tmp = lowercase(tmp);
449 if (!prefixIs(tmp, "@string")
450 && !prefixIs(tmp, "@preamble")) {
451 linebuf = split(linebuf,
453 tmp = frontStrip(strip(tmp));
462 // Get next file name
463 bibfiles = split(bibfiles, tmp, ',');
469 // BibTeX should have its own dialog. This is provisional.
470 void InsetBibtex::Edit(BufferView *, int, int, unsigned int)
473 bibitem_form = create_form_bibitem_form();
474 fl_set_form_atclose(bibitem_form->bibitem_form,
475 CancelCloseBoxCB, 0);
478 bibitem_form->bibitem_form->u_vdata = this;
479 fl_set_object_label(bibitem_form->key, _("Database:"));
480 fl_set_object_label(bibitem_form->label, _("Style: "));
481 fl_set_input(bibitem_form->key, getContents().c_str());
482 fl_set_input(bibitem_form->label, getOptions().c_str());
483 if (bibitem_form->bibitem_form->visible) {
484 fl_raise_form(bibitem_form->bibitem_form);
486 fl_show_form(bibitem_form->bibitem_form,
487 FL_PLACE_MOUSE, FL_FULLBORDER,
493 bool InsetBibtex::addDatabase(string const & db)
495 if (!contains(contents, db.c_str())) {
496 if (!contents.empty())
505 bool InsetBibtex::delDatabase(string const & db)
507 if (contains(contents, db.c_str())) {
509 int n = tokenPos(contents, ',', bd);
511 // Weird code, would someone care to explain this?(Lgb)
514 contents = subst(contents, tmp.c_str(), ", ");
516 contents = split(contents, bd, ',');
524 // This function should be in LyXView when multiframe works ale970302
525 void BibitemUpdate(Combox * combox)
529 if (!current_view->available()) return;
531 string tmp, bibkeys = current_view->buffer()->getBibkeyList(',');
532 bibkeys = split(bibkeys, tmp,',');
533 while (!tmp.empty()) {
534 combox->addto(tmp.c_str());
535 bibkeys = split(bibkeys, tmp,',');
541 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
542 int bibitemMaxWidth(Painter & pain, LyXFont const & font)
545 // Does look like a hack? It is! (but will change at 0.13)
546 LyXParagraph * par = current_view->buffer()->paragraph;
550 int wx = par->bibkey->width(pain, font);
560 string bibitemWidthest(Painter & pain)
563 // Does look like a hack? It is! (but will change at 0.13)
564 LyXParagraph * par = current_view->buffer()->paragraph;
565 InsetBibKey * bkey = 0;
570 int wx = par->bibkey->width(pain, font);
579 if (bkey && !bkey->getScreenLabel().empty())
580 return bkey->getScreenLabel();