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 void bibitem_cb(FL_OBJECT *, long);
31 FD_citation_form * create_form_citation_form(void);
32 FD_bibitem_form * create_form_bibitem_form(void);
35 void bibitem_cb(FL_OBJECT *, long data)
38 // case 0: fl_hide_form(citation_form->citation_form);
41 case 1: // OK, citation
43 // InsetCommand *inset = (InsetCommand*)citation_form->vdata;
44 // inset->setContents(bibcombox->getline());
45 // inset->setOptions(fl_get_input(citation_form->label));
46 // fl_hide_form(citation_form->citation_form);
47 // UpdateInset(inset);
49 if(!current_view->currentBuffer()->isReadonly()) {
50 InsetCommand *inset = (InsetCommand*)citation_form->vdata;
51 inset->setContents(bibcombox->getline());
52 inset->setOptions(fl_get_input(citation_form->label));
53 fl_hide_form(citation_form->citation_form);
54 // shouldn't mark the buffer dirty unless something
55 // was actually altered
59 // fall through to Cancel on RO-mode
61 // case 2: fl_hide_form(bibitem_form->bibitem_form);
62 case 0: fl_hide_form(citation_form->citation_form);
66 case 3: // OK, bibitem
68 // InsetCommand *inset = (InsetCommand *)bibitem_form->vdata;
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 // current_view->currentBuffer()->text->RedoParagraph();
74 // current_view->currentBuffer()->update(1);
76 if(!current_view->currentBuffer()->isReadonly()) {
77 InsetCommand *inset = (InsetCommand *)bibitem_form->vdata;
78 inset->setContents(fl_get_input(bibitem_form->key));
79 inset->setOptions(fl_get_input(bibitem_form->label));
80 fl_hide_form(bibitem_form->bibitem_form);
81 // Does look like a hack? It is! (but will change at 0.13)
82 current_view->currentBuffer()->text->RedoParagraph();
83 current_view->currentBuffer()->update(1);
85 } // fall through to Cancel on RO-mode
87 case 2: // Cancel, bibitem
88 fl_hide_form(bibitem_form->bibitem_form); // Cancel, bibitem
93 FD_citation_form *create_form_citation_form(void)
96 FD_citation_form *fdui = (FD_citation_form *) fl_calloc(1, sizeof(FD_citation_form));
98 fdui->citation_form = fl_bgn_form(FL_NO_BOX, 220, 130);
99 obj = fl_add_box(FL_UP_BOX,0,0,220,130,"");
100 fdui->key = obj = fl_add_text(FL_NORMAL_TEXT,20,10,60,30,_("Key:"));
101 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
102 fl_set_object_lalign(obj,FL_ALIGN_RIGHT);
104 bibcombox = new Combox(FL_COMBOX_INPUT);
105 bibcombox->add(80,10,130,30, 120);
107 obj = fl_add_button(FL_RETURN_BUTTON,20,90,90,30,_("OK"));
108 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
109 fl_set_object_callback(obj,bibitem_cb,1);
110 obj = fl_add_button(FL_NORMAL_BUTTON,120,90,90,30,idex(_("Cancel|^[")));
111 fl_set_button_shortcut(obj,scex(_("Cancel|^[")),1);
112 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
113 fl_set_object_callback(obj,bibitem_cb,0);
114 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT,80,50,130,30,idex(_("Remark:|#R")));
115 fl_set_input_shortcut(obj,scex(_("Remark:|#R")),1);
116 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
119 //fdui->citation_form->fdui = fdui;
123 /*---------------------------------------*/
125 FD_bibitem_form *create_form_bibitem_form(void)
128 FD_bibitem_form *fdui = (FD_bibitem_form *) fl_calloc(1, sizeof(FD_bibitem_form));
130 fdui->bibitem_form = fl_bgn_form(FL_NO_BOX, 220, 130);
131 obj = fl_add_box(FL_UP_BOX,0,0,220,130,"");
132 fdui->key = obj = fl_add_input(FL_NORMAL_INPUT,80,10,130,30,idex(_("Key:|#K")));
133 fl_set_input_shortcut(obj,scex(_("Key:|#K")),1);
134 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
135 obj = fl_add_button(FL_RETURN_BUTTON,20,90,90,30,_("OK"));
136 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
137 fl_set_object_callback(obj,bibitem_cb,3);
138 obj = fl_add_button(FL_NORMAL_BUTTON,120,90,90,30,idex(_("Cancel|^[")));
139 fl_set_button_shortcut(obj,scex(_("Cancel|^[")),1);
140 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
141 fl_set_object_callback(obj,bibitem_cb,2);
142 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT,80,50,130,30,idex(_("Label:|#L")));
143 fl_set_input_shortcut(obj,scex(_("Label:|#L")),1);
144 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
147 //fdui->bibitem_form->fdui = fdui;
151 /*---------------------------------------*/
154 InsetCitation::InsetCitation(string const & key, string const & note):
155 InsetCommand("cite", key, note)
159 InsetCitation::~InsetCitation()
161 if(citation_form && citation_form->citation_form
162 && citation_form->citation_form->visible
163 && citation_form->vdata == this)
164 fl_hide_form(citation_form->citation_form);
168 void InsetCitation::Edit(int, int)
170 if(current_view->currentBuffer()->isReadonly())
173 if (!citation_form) {
174 citation_form = create_form_citation_form();
175 fl_set_form_atclose(citation_form->citation_form,
176 CancelCloseBoxCB, 0);
178 citation_form->vdata = this;
180 BibitemUpdate(bibcombox);
181 if (!bibcombox->select_text(getContents().c_str()))
182 bibcombox->addline(getContents().c_str());
184 fl_set_input(citation_form->label, getOptions().c_str());
185 if (citation_form->citation_form->visible) {
186 fl_raise_form(citation_form->citation_form);
188 fl_show_form(citation_form->citation_form,FL_PLACE_MOUSE, FL_FULLBORDER,
194 string InsetCitation::getScreenLabel() const
200 if (!options.empty()) {
201 temp += "," + options;
208 InsetBibKey::InsetBibKey(string const & key, string const & label):
209 InsetCommand("bibitem", key, label)
217 InsetBibKey::InsetBibKey(InsetBibKey const *b):
218 InsetCommand("bibitem", b->contents, b->options)
220 counter = b->counter;
223 InsetBibKey::~InsetBibKey()
225 if(bibitem_form && bibitem_form->bibitem_form
226 && bibitem_form->bibitem_form->visible)
227 fl_hide_form(bibitem_form->bibitem_form);
230 void InsetBibKey::setCounter(int c)
234 if (contents.empty())
239 // I'm sorry but this is still necessary because \bibitem is used also
240 // as a LyX 2.x command, and lyxlex is not enough smart to understand
241 // real LaTeX commands. Yes, that could be fixed, but would be a waste
242 // of time cause LyX3 won't use lyxlex anyway. (ale)
243 void InsetBibKey::Write(FILE *file)
246 if (!options.empty()) {
252 fprintf(file,"\\bibitem %s\n", s.c_str());
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 upwards?
270 void InsetBibKey::Edit(int, int)
272 if(current_view->currentBuffer()->isReadonly())
276 bibitem_form = create_form_bibitem_form();
277 fl_set_form_atclose(bibitem_form->bibitem_form,
278 CancelCloseBoxCB, 0);
280 bibitem_form->vdata = this;
281 // InsetBibtex uses the same form, with different labels
282 fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
283 fl_set_button_shortcut(bibitem_form->key,scex(_("Key:|#K")),1);
284 fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
285 fl_set_button_shortcut(bibitem_form->label,scex(_("Label:|#L")),1);
286 fl_set_input(bibitem_form->key, getContents().c_str());
287 fl_set_input(bibitem_form->label, getOptions().c_str());
288 if (bibitem_form->bibitem_form->visible) {
289 fl_raise_form(bibitem_form->bibitem_form);
291 fl_show_form(bibitem_form->bibitem_form,FL_PLACE_MOUSE,
293 _("Bibliography item"));
299 InsetBibtex::InsetBibtex(string const & dbase, string const & style,
301 :InsetCommand("BibTeX", dbase, style), owner(o)
306 InsetBibtex::~InsetBibtex()
311 string InsetBibtex::getScreenLabel() const
313 return _("BibTeX Generated References");
317 int InsetBibtex::Latex(FILE *file, signed char /*fragile*/)
320 signed char dummy = 0;
321 int result = Latex(bib, dummy);
322 fprintf(file, "%s", bib.c_str());
327 int InsetBibtex::Latex(string &file, signed char /*fragile*/)
329 // this looks like an horrible hack and it is :) The problem
330 // is that owner is not initialized correctly when the bib
331 // inset is cut and pasted. Such hacks will not be needed
334 owner = current_view->currentBuffer();
336 // If we generate in a temp dir, we might need to give an
337 // absolute path there. This is a bit complicated since we can
338 // have a comma-separated list of bibliographies
339 string db_in, adb, db_out;
340 db_in = getContents();
341 db_in=split(db_in, adb, ',');
342 while(!adb.empty()) {
343 if (!owner->niceFile &&
344 IsFileReadable(MakeAbsPath(adb,owner->filepath)+".bib"))
345 adb = MakeAbsPath(adb,owner->filepath);
348 db_in=split(db_in, adb,',');
350 db_out = strip(db_out, ',');
354 && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
356 style = MakeAbsPath(getOptions(), owner->filepath);
358 style = getOptions();
360 file += "\\bibliographystyle{";
363 file += "\\bibliography{";
369 // This method returns a comma separated list of Bibtex entries
370 string InsetBibtex::getKeys()
372 // This hack is copied from InsetBibtex::Latex.
373 // Is it still needed? Probably yes.
375 owner = current_view->currentBuffer();
378 // We need to create absolute path names for bibliographies
379 // First look for bib-file in same directory as document,
380 // then in all directories listed in environment variable
382 string bibfiles, linebuf, tmp, keys;
383 bibfiles = getContents();
384 bibfiles=split(bibfiles, tmp, ',');
385 while(!tmp.empty()) {
386 if (IsFileReadable(MakeAbsPath(tmp,owner->filepath)+".bib"))
387 tmp = MakeAbsPath(tmp,owner->filepath)+".bib";
389 tmp = FileOpenSearch(GetEnvPath("BIBINPUTS"),tmp,"bib");
391 tmp = FileOpenSearch(GetEnvPath("BIBINPUT"),
394 // If we didn't find a matching file name just fail silently
397 // This is a _very_ simple parser for Bibtex database files.
398 // All it does is to look for lines starting in @ and not
399 // being @preamble and @string entries.
400 // It does NOT do any syntax checking!
401 FilePtr file(tmp,FilePtr::read);
404 // On some systems where feof() is a macro,
405 // the () after file is needed (JMarc)
406 while (! feof(file())) {
409 // At end of each line check if line begins with '@'
411 if (prefixIs(linebuf, "@") ) {
412 subst(linebuf, '{','(');
413 linebuf=split(linebuf, tmp,'(');
414 tmp = lowercase(tmp);
415 if (!prefixIs(tmp, "@string") && !prefixIs(tmp, "@preamble") ) {
416 linebuf = split(linebuf, tmp,',');
418 keys += strip(tmp) + ",";
427 // Get next file name
428 bibfiles=split(bibfiles, tmp, ',');
433 // BibTeX should have its own dialog. This is provisional.
434 void InsetBibtex::Edit(int, int)
437 bibitem_form = create_form_bibitem_form();
438 fl_set_form_atclose(bibitem_form->bibitem_form,
439 CancelCloseBoxCB, 0);
442 bibitem_form->vdata = this;
443 fl_set_object_label(bibitem_form->key, _("Database:"));
444 fl_set_object_label(bibitem_form->label, _("Style: "));
445 fl_set_input(bibitem_form->key, getContents().c_str());
446 fl_set_input(bibitem_form->label, getOptions().c_str());
447 if (bibitem_form->bibitem_form->visible) {
448 fl_raise_form(bibitem_form->bibitem_form);
450 fl_show_form(bibitem_form->bibitem_form,FL_PLACE_MOUSE, FL_FULLBORDER,
456 bool InsetBibtex::addDatabase(string const &db)
458 if (!contains(contents, db.c_str())) {
459 if (!contents.empty())
468 bool InsetBibtex::delDatabase(string const &db)
470 if (contains(contents, db.c_str())) {
472 int n = tokenPos(contents, ',', bd);
476 subst(contents, tmp.c_str(), ",");
478 contents = split(contents, bd, ',');
486 // This function should be in LyXView when multiframe works ale970302
487 void BibitemUpdate(Combox* combox)
491 if (!current_view->available())
494 string tmp, bibkeys = current_view->currentBuffer()->getBibkeyList(',');
495 bibkeys=split(bibkeys, tmp,',');
496 while (!tmp.empty()) {
497 combox->addto(tmp.c_str());
498 bibkeys=split(bibkeys, tmp,',');
504 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
505 int bibitemMaxWidth(const class LyXFont &font)
508 // Does look like a hack? It is! (but will change at 0.13)
509 LyXParagraph *par = current_view->currentBuffer()->paragraph;
513 int wx = par->bibkey->Width(font);
523 string bibitemWidthest()
526 // Does look like a hack? It is! (but will change at 0.13)
527 LyXParagraph *par = current_view->currentBuffer()->paragraph;
533 int wx = par->bibkey->Width(font);
542 if (bkey && !bkey->getScreenLabel().empty())
543 return bkey->getScreenLabel();