10 #include FORMS_H_LOCATION
15 #include "lyx_gui_misc.h"
16 #include "BufferView.h"
20 #include "support/filetools.h"
27 extern BufferView * current_view;
29 FD_citation_form * citation_form = 0;
30 FD_bibitem_form * bibitem_form = 0;
31 static Combox * bibcombox = 0;
33 void BibitemUpdate(Combox *);
34 FD_citation_form * create_form_citation_form(void);
35 FD_bibitem_form * create_form_bibitem_form(void);
38 extern "C" void bibitem_cb(FL_OBJECT *, long data)
41 case 1: // OK, citation
43 InsetCitation::Holder * holder =
44 static_cast<InsetCitation::Holder*>
45 (citation_form->citation_form->u_vdata);
46 if(!holder->view->buffer()->isReadonly()) {
48 InsetCitation * inset = holder->inset;
49 inset->setContents(bibcombox->getline());
50 inset->setOptions(fl_get_input(citation_form->label));
51 fl_hide_form(citation_form->citation_form);
52 // shouldn't mark the buffer dirty unless something
53 // was actually altered
54 holder->view->updateInset(inset, true);
57 // fall through to Cancel on RO-mode
59 case 0: fl_hide_form(citation_form->citation_form);
61 case 3: // OK, bibitem
63 InsetBibKey::Holder * holder =
64 static_cast<InsetBibKey::Holder*>
65 (bibitem_form->bibitem_form->u_vdata);
67 if(!holder->view->buffer()->isReadonly()) {
68 InsetBibKey * inset = holder->inset;
69 inset->setContents(fl_get_input(bibitem_form->key));
70 inset->setOptions(fl_get_input(bibitem_form->label));
71 fl_hide_form(bibitem_form->bibitem_form);
72 // Does look like a hack? It is! (but will change at 0.13)
73 holder->view->text->RedoParagraph();
74 holder->view->update(1);
76 } // fall through to Cancel on RO-mode
78 case 2: // Cancel, bibitem
79 fl_hide_form(bibitem_form->bibitem_form); // Cancel, bibitem
85 FD_citation_form * create_form_citation_form(void)
88 FD_citation_form * fdui = (FD_citation_form *) fl_calloc(1, sizeof(FD_citation_form));
90 fdui->citation_form = fl_bgn_form(FL_NO_BOX, 220, 130);
91 obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
92 fdui->key = obj = fl_add_text(FL_NORMAL_TEXT, 20, 10, 60, 30, _("Key:"));
93 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
94 fl_set_object_lalign(obj, FL_ALIGN_RIGHT);
96 bibcombox = new Combox(FL_COMBOX_INPUT);
97 bibcombox->add(80, 10, 130, 30, 300);
99 obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
100 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
101 fl_set_object_callback(obj, bibitem_cb, 1);
102 obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
103 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
104 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
105 fl_set_object_callback(obj, bibitem_cb, 0);
106 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Remark:|#R")));
107 fl_set_input_shortcut(obj, scex(_("Remark:|#R")), 1);
108 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
111 //fdui->citation_form->fdui = fdui;
115 /*---------------------------------------*/
117 FD_bibitem_form * create_form_bibitem_form(void)
120 FD_bibitem_form * fdui = (FD_bibitem_form *) fl_calloc(1, sizeof(FD_bibitem_form));
122 fdui->bibitem_form = fl_bgn_form(FL_NO_BOX, 220, 130);
123 obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
124 fdui->key = obj = fl_add_input(FL_NORMAL_INPUT, 80, 10, 130, 30, idex(_("Key:|#K")));
125 fl_set_input_shortcut(obj, scex(_("Key:|#K")), 1);
126 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
127 obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
128 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
129 fl_set_object_callback(obj, bibitem_cb, 3);
130 obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
131 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
132 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
133 fl_set_object_callback(obj, bibitem_cb, 2);
134 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Label:|#L")));
135 fl_set_input_shortcut(obj, scex(_("Label:|#L")), 1);
136 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
139 //fdui->bibitem_form->fdui = fdui;
143 /*---------------------------------------*/
146 InsetCitation::InsetCitation(string const & key, string const & note)
147 : InsetCommand("cite", key, note)
153 InsetCitation::~InsetCitation()
155 if(citation_form && citation_form->citation_form
156 && citation_form->citation_form->visible
157 && citation_form->citation_form->u_vdata == &holder)
158 fl_hide_form(citation_form->citation_form);
162 void InsetCitation::Edit(BufferView * bv, int, int, unsigned int)
164 if(bv->buffer()->isReadonly())
165 WarnReadonly(bv->buffer()->fileName());
167 if (!citation_form) {
168 citation_form = create_form_citation_form();
169 fl_set_form_atclose(citation_form->citation_form,
170 CancelCloseBoxCB, 0);
176 citation_form->citation_form->u_vdata = &holder;
178 BibitemUpdate(bibcombox);
179 if (!bibcombox->select_text(getContents().c_str()))
180 bibcombox->addline(getContents().c_str());
182 fl_set_input(citation_form->label, getOptions().c_str());
183 if (citation_form->citation_form->visible) {
184 fl_raise_form(citation_form->citation_form);
186 fl_show_form(citation_form->citation_form,
187 FL_PLACE_MOUSE, FL_FULLBORDER,
193 string InsetCitation::getScreenLabel() const
199 if (!options.empty()) {
200 temp += ", " + options;
207 InsetBibKey::InsetBibKey(string const & key, string const & label):
208 InsetCommand("bibitem", key, label)
216 InsetBibKey::InsetBibKey(InsetBibKey const * b):
217 InsetCommand("bibitem", b->contents, b->options)
219 counter = b->counter;
223 InsetBibKey::~InsetBibKey()
225 if(bibitem_form && bibitem_form->bibitem_form
226 && bibitem_form->bibitem_form->visible
227 && bibitem_form->bibitem_form->u_vdata == &holder)
228 fl_hide_form(bibitem_form->bibitem_form);
231 void InsetBibKey::setCounter(int c)
235 if (contents.empty())
236 contents += tostr(counter);
240 // I'm sorry but this is still necessary because \bibitem is used also
241 // as a LyX 2.x command, and lyxlex is not enough smart to understand
242 // real LaTeX commands. Yes, that could be fixed, but would be a waste
243 // of time cause LyX3 won't use lyxlex anyway. (ale)
244 void InsetBibKey::Write(ostream & os) const
247 if (!options.empty()) {
252 << contents << "}\n";
256 string InsetBibKey::getScreenLabel() const
258 if (!options.empty())
261 return tostr(counter);
266 The value in "Key:" isn't allways set right after a few bibkey insets have
267 been added/removed. Perhaps the wrong object is deleted/used somewhere
271 void InsetBibKey::Edit(BufferView * bv, int, int, unsigned int)
273 if(bv->buffer()->isReadonly())
274 WarnReadonly(bv->buffer()->fileName());
277 bibitem_form = create_form_bibitem_form();
278 fl_set_form_atclose(bibitem_form->bibitem_form,
279 CancelCloseBoxCB, 0);
285 bibitem_form->bibitem_form->u_vdata = &holder;
286 // InsetBibtex uses the same form, with different labels
287 fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
288 fl_set_button_shortcut(bibitem_form->key, scex(_("Key:|#K")), 1);
289 fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
290 fl_set_button_shortcut(bibitem_form->label, scex(_("Label:|#L")), 1);
291 fl_set_input(bibitem_form->key, getContents().c_str());
292 fl_set_input(bibitem_form->label, getOptions().c_str());
293 if (bibitem_form->bibitem_form->visible) {
294 fl_raise_form(bibitem_form->bibitem_form);
296 fl_show_form(bibitem_form->bibitem_form, FL_PLACE_MOUSE,
298 _("Bibliography item"));
304 InsetBibtex::InsetBibtex(string const & dbase, string const & style,
306 : InsetCommand("BibTeX", dbase, style), owner(o)
310 InsetBibtex::~InsetBibtex()
312 if(bibitem_form && bibitem_form->bibitem_form
313 && bibitem_form->bibitem_form->visible
314 && bibitem_form->bibitem_form->u_vdata == &holder)
315 fl_hide_form(bibitem_form->bibitem_form);
319 string InsetBibtex::getScreenLabel() const
321 return _("BibTeX Generated References");
325 int InsetBibtex::Latex(ostream & os,
326 bool /*fragile*/, bool/*fs*/) const
328 // this looks like an horrible hack and it is :) The problem
329 // is that owner is not initialized correctly when the bib
330 // inset is cut and pasted. Such hacks will not be needed
333 owner = current_view->buffer();
335 // If we generate in a temp dir, we might need to give an
336 // absolute path there. This is a bit complicated since we can
337 // have a comma-separated list of bibliographies
339 string db_in = getContents();
340 db_in = split(db_in, adb, ',');
341 while(!adb.empty()) {
342 if (!owner->niceFile &&
343 IsFileReadable(MakeAbsPath(adb, owner->filepath)+".bib"))
344 adb = MakeAbsPath(adb, owner->filepath);
347 db_in= split(db_in, adb,',');
349 db_out = strip(db_out, ',');
353 && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
355 style = MakeAbsPath(getOptions(), owner->filepath);
357 style = getOptions();
359 os << "\\bibliographystyle{" << style << "}\n"
360 << "\\bibliography{" << db_out << "}\n";
365 // This method returns a comma separated list of Bibtex entries
366 string InsetBibtex::getKeys(char delim)
368 // This hack is copied from InsetBibtex::Latex.
369 // Is it still needed? Probably yes.
370 // Why is this needed here when it already is in Latex?
371 // Anyway we need a different way to get to the
372 // buffer the inset is in. (Lgb)
375 // owner = current_view->buffer();
379 string bibfiles = getContents();
380 bibfiles = split(bibfiles, tmp, ',');
381 while(!tmp.empty()) {
382 string fil = findtexfile(ChangeExtension(tmp, "bib", false),
384 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
385 // If we didn't find a matching file name just fail silently
387 // This is a _very_ simple parser for Bibtex database
388 // files. All it does is to look for lines starting
389 // in @ and not being @preamble and @string entries.
390 // It does NOT do any syntax checking!
391 ifstream ifs(fil.c_str());
393 while (getline(ifs, linebuf)) {
394 linebuf = frontStrip(linebuf);
395 if (prefixIs(linebuf, "@")) {
396 linebuf = subst(linebuf, '{', '(');
397 linebuf = split(linebuf, tmp, '(');
398 tmp = lowercase(tmp);
399 if (!prefixIs(tmp, "@string")
400 && !prefixIs(tmp, "@preamble")) {
401 linebuf = split(linebuf,
403 tmp = frontStrip(strip(tmp));
412 // Get next file name
413 bibfiles = split(bibfiles, tmp, ',');
419 // BibTeX should have its own dialog. This is provisional.
420 void InsetBibtex::Edit(BufferView * bv, int, int, unsigned int)
423 bibitem_form = create_form_bibitem_form();
424 fl_set_form_atclose(bibitem_form->bibitem_form,
425 CancelCloseBoxCB, 0);
430 bibitem_form->bibitem_form->u_vdata = &holder;
432 fl_set_object_label(bibitem_form->key, _("Database:"));
433 fl_set_object_label(bibitem_form->label, _("Style: "));
434 fl_set_input(bibitem_form->key, getContents().c_str());
435 fl_set_input(bibitem_form->label, getOptions().c_str());
436 if (bibitem_form->bibitem_form->visible) {
437 fl_raise_form(bibitem_form->bibitem_form);
439 fl_show_form(bibitem_form->bibitem_form,
440 FL_PLACE_MOUSE, FL_FULLBORDER,
446 bool InsetBibtex::addDatabase(string const & db)
448 if (!contains(contents, db.c_str())) {
449 if (!contents.empty())
458 bool InsetBibtex::delDatabase(string const & db)
460 if (contains(contents, db.c_str())) {
462 int n = tokenPos(contents, ',', bd);
464 // Weird code, would someone care to explain this?(Lgb)
467 contents = subst(contents, tmp.c_str(), ", ");
469 contents = split(contents, bd, ',');
477 // This function should be in LyXView when multiframe works ale970302
478 void BibitemUpdate(Combox * combox)
482 if (!current_view->available()) return;
484 string tmp, bibkeys = current_view->buffer()->getBibkeyList(',');
485 bibkeys = split(bibkeys, tmp,',');
486 while (!tmp.empty()) {
487 combox->addto(tmp.c_str());
488 bibkeys = split(bibkeys, tmp,',');
494 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
495 int bibitemMaxWidth(Painter & pain, LyXFont const & font)
498 // Does look like a hack? It is! (but will change at 0.13)
499 LyXParagraph * par = current_view->buffer()->paragraph;
503 int wx = par->bibkey->width(pain, font);
513 string bibitemWidthest(Painter & pain)
516 // Does look like a hack? It is! (but will change at 0.13)
517 LyXParagraph * par = current_view->buffer()->paragraph;
518 InsetBibKey * bkey = 0;
523 int wx = par->bibkey->width(pain, font);
532 if (bkey && !bkey->getScreenLabel().empty())
533 return bkey->getScreenLabel();