10 #include FORMS_H_LOCATION
15 #include "lyx_gui_misc.h"
16 #include "BufferView.h"
20 #include "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 = NULL;
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(LString const & key, LString 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, NULL);
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 LString InsetCitation::getScreenLabel() const
200 if (!options.empty()) {
208 InsetBibKey::InsetBibKey(LString const & key, LString 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 LString InsetBibKey::getScreenLabel() const
258 if (!options.empty())
267 The value in "Key:" isn't allways set right after a few bibkey insets have
268 been added/removed. Perhaps the wrong object is deleted/used somewhere upwards?
271 void InsetBibKey::Edit(int, int)
273 if(current_view->currentBuffer()->isReadonly())
277 bibitem_form = create_form_bibitem_form();
278 fl_set_form_atclose(bibitem_form->bibitem_form,
279 CancelCloseBoxCB, NULL);
281 bibitem_form->vdata = this;
282 // InsetBibtex uses the same form, with different labels
283 fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
284 fl_set_button_shortcut(bibitem_form->key,scex(_("Key:|#K")),1);
285 fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
286 fl_set_button_shortcut(bibitem_form->label,scex(_("Label:|#L")),1);
287 fl_set_input(bibitem_form->key, getContents().c_str());
288 fl_set_input(bibitem_form->label, getOptions().c_str());
289 if (bibitem_form->bibitem_form->visible) {
290 fl_raise_form(bibitem_form->bibitem_form);
292 fl_show_form(bibitem_form->bibitem_form,FL_PLACE_MOUSE,
294 _("Bibliography item"));
300 InsetBibtex::InsetBibtex(LString const & dbase, LString const & style,
302 :InsetCommand("BibTeX", dbase, style), owner(o)
307 InsetBibtex::~InsetBibtex()
312 LString InsetBibtex::getScreenLabel() const
314 return _("BibTeX Generated References");
318 int InsetBibtex::Latex(FILE *file, signed char /*fragile*/)
321 signed char dummy = 0;
322 int result = Latex(bib, dummy);
323 fprintf(file, "%s", bib.c_str());
328 int InsetBibtex::Latex(LString &file, signed char /*fragile*/)
330 // this looks like an horrible hack and it is :) The problem
331 // is that owner is not initialized correctly when the bib
332 // inset is cut and pasted. Such hacks will not be needed
335 owner = current_view->currentBuffer();
337 // If we generate in a temp dir, we might need to give an
338 // absolute path there. This is a bit complicated since we can
339 // have a comma-separated list of bibliographies
340 LString db_in, adb, db_out;
341 db_in = getContents();
342 db_in.split(adb, ',');
343 while(!adb.empty()) {
344 if (!owner->niceFile &&
345 IsFileReadable(MakeAbsPath(adb,owner->filepath)+".bib"))
346 adb = MakeAbsPath(adb,owner->filepath);
349 db_in.split(adb,',');
356 && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
358 style = MakeAbsPath(getOptions(), owner->filepath);
360 style = getOptions();
362 file += "\\bibliographystyle{";
365 file += "\\bibliography{";
371 // This method returns a comma separated list of Bibtex entries
372 LString InsetBibtex::getKeys()
374 // This hack is copied from InsetBibtex::Latex.
375 // Is it still needed? Probably yes.
377 owner = current_view->currentBuffer();
380 // We need to create absolute path names for bibliographies
381 // First look for bib-file in same directory as document,
382 // then in all directories listed in environment variable
384 LString bibfiles, linebuf, tmp, keys;
385 bibfiles = getContents();
386 bibfiles.split(tmp, ',');
387 while(!tmp.empty()) {
388 if (IsFileReadable(MakeAbsPath(tmp,owner->filepath)+".bib"))
389 tmp = MakeAbsPath(tmp,owner->filepath)+".bib";
391 tmp = FileOpenSearch(getEnvPath("BIBINPUTS"),tmp,"bib");
393 tmp = FileOpenSearch(getEnvPath("BIBINPUT"),
396 // If we didn't find a matching file name just fail silently
399 // This is a _very_ simple parser for Bibtex database files.
400 // All it does is to look for lines starting in @ and not
401 // being @preamble and @string entries.
402 // It does NOT do any syntax checking!
403 FilePtr file(tmp,FilePtr::read);
406 // On some systems where feof() is a macro,
407 // the () after file is needed (JMarc)
408 while (! feof(file())) {
411 // At end of each line check if line begins with '@'
413 if ( linebuf.prefixIs("@") ) {
414 linebuf.subst('{','(');
415 linebuf.split(tmp,'(');
417 if ( ! tmp.prefixIs("@string") && !tmp.prefixIs("@preamble") ) {
418 linebuf.split(tmp,',');
420 keys +=tmp.strip()+",";
429 // Get next file name
430 bibfiles.split(tmp, ',');
435 // BibTeX should have its own dialog. This is provisional.
436 void InsetBibtex::Edit(int, int)
439 bibitem_form = create_form_bibitem_form();
440 fl_set_form_atclose(bibitem_form->bibitem_form,
441 CancelCloseBoxCB, NULL);
444 bibitem_form->vdata = this;
445 fl_set_object_label(bibitem_form->key, _("Database:"));
446 fl_set_object_label(bibitem_form->label, _("Style: "));
447 fl_set_input(bibitem_form->key, getContents().c_str());
448 fl_set_input(bibitem_form->label, getOptions().c_str());
449 if (bibitem_form->bibitem_form->visible) {
450 fl_raise_form(bibitem_form->bibitem_form);
452 fl_show_form(bibitem_form->bibitem_form,FL_PLACE_MOUSE, FL_FULLBORDER,
458 bool InsetBibtex::addDatabase(LString const &db)
460 if (!contents.contains(db.c_str())) {
461 if (!contents.empty())
470 bool InsetBibtex::delDatabase(LString const &db)
472 if (contents.contains(db.c_str())) {
474 int n = contents.tokenPos(',', bd);
478 contents.subst(tmp.c_str(), ",");
480 contents.split(bd, ',');
488 // This function should be in LyXView when multiframe works ale970302
489 void BibitemUpdate(Combox* combox)
493 if (!current_view->available())
496 LString tmp, bibkeys = current_view->currentBuffer()->getBibkeyList(',');
497 bibkeys.split(tmp,',');
498 while (!tmp.empty()) {
499 combox->addto(tmp.c_str());
500 bibkeys.split(tmp,',');
506 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
507 int bibitemMaxWidth(const class LyXFont &font)
510 // Does look like a hack? It is! (but will change at 0.13)
511 LyXParagraph *par = current_view->currentBuffer()->paragraph;
515 int wx = par->bibkey->Width(font);
525 LString bibitemWidthest()
528 // Does look like a hack? It is! (but will change at 0.13)
529 LyXParagraph *par = current_view->currentBuffer()->paragraph;
535 int wx = par->bibkey->Width(font);
544 if (bkey && !bkey->getScreenLabel().empty())
545 return bkey->getScreenLabel();