10 #include FORMS_H_LOCATION
15 #include "lyx_gui_misc.h"
16 #include "BufferView.h"
20 #include "support/filetools.h"
22 extern BufferView *current_view;
24 FD_citation_form *citation_form=0;
25 FD_bibitem_form *bibitem_form=0;
26 static Combox *bibcombox = 0;
28 extern void UpdateInset(Inset* inset, bool mark_dirty = true);
29 void BibitemUpdate(Combox *);
30 FD_citation_form * create_form_citation_form(void);
31 FD_bibitem_form * create_form_bibitem_form(void);
34 extern "C" void bibitem_cb(FL_OBJECT *, long data)
37 // case 0: fl_hide_form(citation_form->citation_form);
40 case 1: // OK, citation
42 // InsetCommand *inset = (InsetCommand*)citation_form->vdata;
43 // inset->setContents(bibcombox->getline());
44 // inset->setOptions(fl_get_input(citation_form->label));
45 // fl_hide_form(citation_form->citation_form);
46 // UpdateInset(inset);
48 if(!current_view->currentBuffer()->isReadonly()) {
49 InsetCommand *inset = (InsetCommand*)citation_form->vdata;
50 inset->setContents(bibcombox->getline());
51 inset->setOptions(fl_get_input(citation_form->label));
52 fl_hide_form(citation_form->citation_form);
53 // shouldn't mark the buffer dirty unless something
54 // was actually altered
58 // fall through to Cancel on RO-mode
60 // case 2: fl_hide_form(bibitem_form->bibitem_form);
61 case 0: fl_hide_form(citation_form->citation_form);
65 case 3: // OK, bibitem
67 // InsetCommand *inset = (InsetCommand *)bibitem_form->vdata;
68 // inset->setContents(fl_get_input(bibitem_form->key));
69 // inset->setOptions(fl_get_input(bibitem_form->label));
70 // fl_hide_form(bibitem_form->bibitem_form);
71 // // Does look like a hack? It is! (but will change at 0.13)
72 // current_view->currentBuffer()->text->RedoParagraph();
73 // current_view->currentBuffer()->update(1);
75 if(!current_view->currentBuffer()->isReadonly()) {
76 InsetCommand *inset = (InsetCommand *)bibitem_form->vdata;
77 inset->setContents(fl_get_input(bibitem_form->key));
78 inset->setOptions(fl_get_input(bibitem_form->label));
79 fl_hide_form(bibitem_form->bibitem_form);
80 // Does look like a hack? It is! (but will change at 0.13)
81 current_view->currentBuffer()->text->RedoParagraph();
82 current_view->currentBuffer()->update(1);
84 } // fall through to Cancel on RO-mode
86 case 2: // Cancel, bibitem
87 fl_hide_form(bibitem_form->bibitem_form); // Cancel, bibitem
92 FD_citation_form *create_form_citation_form(void)
95 FD_citation_form *fdui = (FD_citation_form *) fl_calloc(1, sizeof(FD_citation_form));
97 fdui->citation_form = fl_bgn_form(FL_NO_BOX, 220, 130);
98 obj = fl_add_box(FL_UP_BOX,0,0,220,130,"");
99 fdui->key = obj = fl_add_text(FL_NORMAL_TEXT,20,10,60,30,_("Key:"));
100 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
101 fl_set_object_lalign(obj,FL_ALIGN_RIGHT);
103 bibcombox = new Combox(FL_COMBOX_INPUT);
104 bibcombox->add(80,10,130,30, 120);
106 obj = fl_add_button(FL_RETURN_BUTTON,20,90,90,30,_("OK"));
107 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
108 fl_set_object_callback(obj,bibitem_cb,1);
109 obj = fl_add_button(FL_NORMAL_BUTTON,120,90,90,30,idex(_("Cancel|^[")));
110 fl_set_button_shortcut(obj,scex(_("Cancel|^[")),1);
111 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
112 fl_set_object_callback(obj,bibitem_cb,0);
113 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT,80,50,130,30,idex(_("Remark:|#R")));
114 fl_set_input_shortcut(obj,scex(_("Remark:|#R")),1);
115 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
118 //fdui->citation_form->fdui = fdui;
122 /*---------------------------------------*/
124 FD_bibitem_form *create_form_bibitem_form(void)
127 FD_bibitem_form *fdui = (FD_bibitem_form *) fl_calloc(1, sizeof(FD_bibitem_form));
129 fdui->bibitem_form = fl_bgn_form(FL_NO_BOX, 220, 130);
130 obj = fl_add_box(FL_UP_BOX,0,0,220,130,"");
131 fdui->key = obj = fl_add_input(FL_NORMAL_INPUT,80,10,130,30,idex(_("Key:|#K")));
132 fl_set_input_shortcut(obj,scex(_("Key:|#K")),1);
133 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
134 obj = fl_add_button(FL_RETURN_BUTTON,20,90,90,30,_("OK"));
135 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
136 fl_set_object_callback(obj,bibitem_cb,3);
137 obj = fl_add_button(FL_NORMAL_BUTTON,120,90,90,30,idex(_("Cancel|^[")));
138 fl_set_button_shortcut(obj,scex(_("Cancel|^[")),1);
139 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
140 fl_set_object_callback(obj,bibitem_cb,2);
141 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT,80,50,130,30,idex(_("Label:|#L")));
142 fl_set_input_shortcut(obj,scex(_("Label:|#L")),1);
143 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
146 //fdui->bibitem_form->fdui = fdui;
150 /*---------------------------------------*/
153 InsetCitation::InsetCitation(string const & key, string const & note):
154 InsetCommand("cite", key, note)
158 InsetCitation::~InsetCitation()
160 if(citation_form && citation_form->citation_form
161 && citation_form->citation_form->visible
162 && citation_form->vdata == this)
163 fl_hide_form(citation_form->citation_form);
167 void InsetCitation::Edit(int, int)
169 if(current_view->currentBuffer()->isReadonly())
172 if (!citation_form) {
173 citation_form = create_form_citation_form();
174 fl_set_form_atclose(citation_form->citation_form,
175 CancelCloseBoxCB, 0);
177 citation_form->vdata = this;
179 BibitemUpdate(bibcombox);
180 if (!bibcombox->select_text(getContents().c_str()))
181 bibcombox->addline(getContents().c_str());
183 fl_set_input(citation_form->label, getOptions().c_str());
184 if (citation_form->citation_form->visible) {
185 fl_raise_form(citation_form->citation_form);
187 fl_show_form(citation_form->citation_form,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;
222 InsetBibKey::~InsetBibKey()
224 if(bibitem_form && bibitem_form->bibitem_form
225 && bibitem_form->bibitem_form->visible)
226 fl_hide_form(bibitem_form->bibitem_form);
229 void InsetBibKey::setCounter(int c)
233 if (contents.empty())
234 contents += tostr(counter);
238 // I'm sorry but this is still necessary because \bibitem is used also
239 // as a LyX 2.x command, and lyxlex is not enough smart to understand
240 // real LaTeX commands. Yes, that could be fixed, but would be a waste
241 // of time cause LyX3 won't use lyxlex anyway. (ale)
242 void InsetBibKey::Write(FILE *file)
245 if (!options.empty()) {
251 fprintf(file,"\\bibitem %s\n", s.c_str());
255 string InsetBibKey::getScreenLabel() const
257 if (!options.empty())
260 return tostr(counter);
265 The value in "Key:" isn't allways set right after a few bibkey insets have
266 been added/removed. Perhaps the wrong object is deleted/used somewhere upwards?
269 void InsetBibKey::Edit(int, int)
271 if(current_view->currentBuffer()->isReadonly())
275 bibitem_form = create_form_bibitem_form();
276 fl_set_form_atclose(bibitem_form->bibitem_form,
277 CancelCloseBoxCB, 0);
279 bibitem_form->vdata = this;
280 // InsetBibtex uses the same form, with different labels
281 fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
282 fl_set_button_shortcut(bibitem_form->key,scex(_("Key:|#K")),1);
283 fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
284 fl_set_button_shortcut(bibitem_form->label,scex(_("Label:|#L")),1);
285 fl_set_input(bibitem_form->key, getContents().c_str());
286 fl_set_input(bibitem_form->label, getOptions().c_str());
287 if (bibitem_form->bibitem_form->visible) {
288 fl_raise_form(bibitem_form->bibitem_form);
290 fl_show_form(bibitem_form->bibitem_form,FL_PLACE_MOUSE,
292 _("Bibliography item"));
298 InsetBibtex::InsetBibtex(string const & dbase, string const & style,
300 :InsetCommand("BibTeX", dbase, style), owner(o)
305 InsetBibtex::~InsetBibtex()
310 string InsetBibtex::getScreenLabel() const
312 return _("BibTeX Generated References");
316 int InsetBibtex::Latex(FILE *file, signed char /*fragile*/)
319 signed char dummy = 0;
320 int result = Latex(bib, dummy);
321 fprintf(file, "%s", bib.c_str());
326 int InsetBibtex::Latex(string &file, signed char /*fragile*/)
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->currentBuffer();
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
338 string db_in, adb, db_out;
339 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 file += "\\bibliographystyle{";
362 file += "\\bibliography{";
368 // This method returns a comma separated list of Bibtex entries
369 string InsetBibtex::getKeys()
371 // This hack is copied from InsetBibtex::Latex.
372 // Is it still needed? Probably yes.
374 owner = current_view->currentBuffer();
377 // We need to create absolute path names for bibliographies
378 // First look for bib-file in same directory as document,
379 // then in all directories listed in environment variable
381 string bibfiles, linebuf, tmp, keys;
382 bibfiles = getContents();
383 bibfiles=split(bibfiles, tmp, ',');
384 while(!tmp.empty()) {
385 if (IsFileReadable(MakeAbsPath(tmp,owner->filepath)+".bib"))
386 tmp = MakeAbsPath(tmp,owner->filepath)+".bib";
388 tmp = FileOpenSearch(GetEnvPath("BIBINPUTS"),tmp,"bib");
390 tmp = FileOpenSearch(GetEnvPath("BIBINPUT"),
393 // If we didn't find a matching file name just fail silently
396 // This is a _very_ simple parser for Bibtex database files.
397 // All it does is to look for lines starting in @ and not
398 // being @preamble and @string entries.
399 // It does NOT do any syntax checking!
400 FilePtr file(tmp,FilePtr::read);
403 // On some systems where feof() is a macro,
404 // the () after file is needed (JMarc)
405 while (! feof(file())) {
408 // At end of each line check if line begins with '@'
410 if (prefixIs(linebuf, "@") ) {
411 subst(linebuf, '{','(');
412 linebuf=split(linebuf, tmp,'(');
413 tmp = lowercase(tmp);
414 if (!prefixIs(tmp, "@string") && !prefixIs(tmp, "@preamble") ) {
415 linebuf = split(linebuf, tmp,',');
417 keys += strip(tmp) + ",";
426 // Get next file name
427 bibfiles=split(bibfiles, tmp, ',');
432 // BibTeX should have its own dialog. This is provisional.
433 void InsetBibtex::Edit(int, int)
436 bibitem_form = create_form_bibitem_form();
437 fl_set_form_atclose(bibitem_form->bibitem_form,
438 CancelCloseBoxCB, 0);
441 bibitem_form->vdata = this;
442 fl_set_object_label(bibitem_form->key, _("Database:"));
443 fl_set_object_label(bibitem_form->label, _("Style: "));
444 fl_set_input(bibitem_form->key, getContents().c_str());
445 fl_set_input(bibitem_form->label, getOptions().c_str());
446 if (bibitem_form->bibitem_form->visible) {
447 fl_raise_form(bibitem_form->bibitem_form);
449 fl_show_form(bibitem_form->bibitem_form,FL_PLACE_MOUSE, FL_FULLBORDER,
455 bool InsetBibtex::addDatabase(string const &db)
457 if (!contains(contents, db.c_str())) {
458 if (!contents.empty())
467 bool InsetBibtex::delDatabase(string const &db)
469 if (contains(contents, db.c_str())) {
471 int n = tokenPos(contents, ',', bd);
475 subst(contents, tmp.c_str(), ",");
477 contents = split(contents, bd, ',');
485 // This function should be in LyXView when multiframe works ale970302
486 void BibitemUpdate(Combox* combox)
490 if (!current_view->available())
493 string tmp, bibkeys = current_view->currentBuffer()->getBibkeyList(',');
494 bibkeys=split(bibkeys, tmp,',');
495 while (!tmp.empty()) {
496 combox->addto(tmp.c_str());
497 bibkeys=split(bibkeys, tmp,',');
503 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
504 int bibitemMaxWidth(const class LyXFont &font)
507 // Does look like a hack? It is! (but will change at 0.13)
508 LyXParagraph *par = current_view->currentBuffer()->paragraph;
512 int wx = par->bibkey->Width(font);
522 string bibitemWidthest()
525 // Does look like a hack? It is! (but will change at 0.13)
526 LyXParagraph *par = current_view->currentBuffer()->paragraph;
532 int wx = par->bibkey->Width(font);
541 if (bkey && !bkey->getScreenLabel().empty())
542 return bkey->getScreenLabel();