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 1: // OK, citation
39 if(!current_view->currentBuffer()->isReadonly()) {
40 InsetCommand *inset = static_cast<InsetCommand*>(citation_form->citation_form->u_vdata);
41 inset->setContents(bibcombox->getline());
42 inset->setOptions(fl_get_input(citation_form->label));
43 fl_hide_form(citation_form->citation_form);
44 // shouldn't mark the buffer dirty unless something
45 // was actually altered
49 // fall through to Cancel on RO-mode
51 case 0: fl_hide_form(citation_form->citation_form);
53 case 3: // OK, bibitem
55 if(!current_view->currentBuffer()->isReadonly()) {
56 InsetCommand *inset = static_cast<InsetCommand*>(bibitem_form->bibitem_form->u_vdata);
57 inset->setContents(fl_get_input(bibitem_form->key));
58 inset->setOptions(fl_get_input(bibitem_form->label));
59 fl_hide_form(bibitem_form->bibitem_form);
60 // Does look like a hack? It is! (but will change at 0.13)
61 current_view->currentBuffer()->text->RedoParagraph();
62 current_view->currentBuffer()->update(1);
64 } // fall through to Cancel on RO-mode
66 case 2: // Cancel, bibitem
67 fl_hide_form(bibitem_form->bibitem_form); // Cancel, bibitem
72 FD_citation_form *create_form_citation_form(void)
75 FD_citation_form *fdui = (FD_citation_form *) fl_calloc(1, sizeof(FD_citation_form));
77 fdui->citation_form = fl_bgn_form(FL_NO_BOX, 220, 130);
78 obj = fl_add_box(FL_UP_BOX,0,0,220,130,"");
79 fdui->key = obj = fl_add_text(FL_NORMAL_TEXT,20,10,60,30,_("Key:"));
80 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
81 fl_set_object_lalign(obj,FL_ALIGN_RIGHT);
83 bibcombox = new Combox(FL_COMBOX_INPUT);
84 bibcombox->add(80,10,130,30, 120);
86 obj = fl_add_button(FL_RETURN_BUTTON,20,90,90,30,_("OK"));
87 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
88 fl_set_object_callback(obj,bibitem_cb,1);
89 obj = fl_add_button(FL_NORMAL_BUTTON,120,90,90,30,idex(_("Cancel|^[")));
90 fl_set_button_shortcut(obj,scex(_("Cancel|^[")),1);
91 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
92 fl_set_object_callback(obj,bibitem_cb,0);
93 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT,80,50,130,30,idex(_("Remark:|#R")));
94 fl_set_input_shortcut(obj,scex(_("Remark:|#R")),1);
95 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
98 //fdui->citation_form->fdui = fdui;
102 /*---------------------------------------*/
104 FD_bibitem_form *create_form_bibitem_form(void)
107 FD_bibitem_form *fdui = (FD_bibitem_form *) fl_calloc(1, sizeof(FD_bibitem_form));
109 fdui->bibitem_form = fl_bgn_form(FL_NO_BOX, 220, 130);
110 obj = fl_add_box(FL_UP_BOX,0,0,220,130,"");
111 fdui->key = obj = fl_add_input(FL_NORMAL_INPUT,80,10,130,30,idex(_("Key:|#K")));
112 fl_set_input_shortcut(obj,scex(_("Key:|#K")),1);
113 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
114 obj = fl_add_button(FL_RETURN_BUTTON,20,90,90,30,_("OK"));
115 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
116 fl_set_object_callback(obj,bibitem_cb,3);
117 obj = fl_add_button(FL_NORMAL_BUTTON,120,90,90,30,idex(_("Cancel|^[")));
118 fl_set_button_shortcut(obj,scex(_("Cancel|^[")),1);
119 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
120 fl_set_object_callback(obj,bibitem_cb,2);
121 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT,80,50,130,30,idex(_("Label:|#L")));
122 fl_set_input_shortcut(obj,scex(_("Label:|#L")),1);
123 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
126 //fdui->bibitem_form->fdui = fdui;
130 /*---------------------------------------*/
133 InsetCitation::InsetCitation(string const & key, string const & note):
134 InsetCommand("cite", key, note)
138 InsetCitation::~InsetCitation()
140 if(citation_form && citation_form->citation_form
141 && citation_form->citation_form->visible
142 && citation_form->citation_form->u_vdata == this)
143 fl_hide_form(citation_form->citation_form);
147 void InsetCitation::Edit(int, int)
149 if(current_view->currentBuffer()->isReadonly())
152 if (!citation_form) {
153 citation_form = create_form_citation_form();
154 fl_set_form_atclose(citation_form->citation_form,
155 CancelCloseBoxCB, 0);
157 citation_form->citation_form->u_vdata = this;
159 BibitemUpdate(bibcombox);
160 if (!bibcombox->select_text(getContents().c_str()))
161 bibcombox->addline(getContents().c_str());
163 fl_set_input(citation_form->label, getOptions().c_str());
164 if (citation_form->citation_form->visible) {
165 fl_raise_form(citation_form->citation_form);
167 fl_show_form(citation_form->citation_form,FL_PLACE_MOUSE, FL_FULLBORDER,
173 string InsetCitation::getScreenLabel() const
179 if (!options.empty()) {
180 temp += "," + options;
187 InsetBibKey::InsetBibKey(string const & key, string const & label):
188 InsetCommand("bibitem", key, label)
196 InsetBibKey::InsetBibKey(InsetBibKey const *b):
197 InsetCommand("bibitem", b->contents, b->options)
199 counter = b->counter;
202 InsetBibKey::~InsetBibKey()
204 if(bibitem_form && bibitem_form->bibitem_form
205 && bibitem_form->bibitem_form->visible)
206 fl_hide_form(bibitem_form->bibitem_form);
209 void InsetBibKey::setCounter(int c)
213 if (contents.empty())
214 contents += tostr(counter);
218 // I'm sorry but this is still necessary because \bibitem is used also
219 // as a LyX 2.x command, and lyxlex is not enough smart to understand
220 // real LaTeX commands. Yes, that could be fixed, but would be a waste
221 // of time cause LyX3 won't use lyxlex anyway. (ale)
222 void InsetBibKey::Write(FILE *file)
225 if (!options.empty()) {
231 fprintf(file,"\\bibitem %s\n", s.c_str());
235 string InsetBibKey::getScreenLabel() const
237 if (!options.empty())
240 return tostr(counter);
245 The value in "Key:" isn't allways set right after a few bibkey insets have
246 been added/removed. Perhaps the wrong object is deleted/used somewhere upwards?
249 void InsetBibKey::Edit(int, int)
251 if(current_view->currentBuffer()->isReadonly())
255 bibitem_form = create_form_bibitem_form();
256 fl_set_form_atclose(bibitem_form->bibitem_form,
257 CancelCloseBoxCB, 0);
259 bibitem_form->bibitem_form->u_vdata = this;
260 // InsetBibtex uses the same form, with different labels
261 fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
262 fl_set_button_shortcut(bibitem_form->key,scex(_("Key:|#K")),1);
263 fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
264 fl_set_button_shortcut(bibitem_form->label,scex(_("Label:|#L")),1);
265 fl_set_input(bibitem_form->key, getContents().c_str());
266 fl_set_input(bibitem_form->label, getOptions().c_str());
267 if (bibitem_form->bibitem_form->visible) {
268 fl_raise_form(bibitem_form->bibitem_form);
270 fl_show_form(bibitem_form->bibitem_form,FL_PLACE_MOUSE,
272 _("Bibliography item"));
278 InsetBibtex::InsetBibtex(string const & dbase, string const & style,
280 :InsetCommand("BibTeX", dbase, style), owner(o)
285 InsetBibtex::~InsetBibtex()
290 string InsetBibtex::getScreenLabel() const
292 return _("BibTeX Generated References");
296 int InsetBibtex::Latex(FILE *file, signed char /*fragile*/)
299 signed char dummy = 0;
300 int result = Latex(bib, dummy);
301 fprintf(file, "%s", bib.c_str());
306 int InsetBibtex::Latex(string &file, signed char /*fragile*/)
308 // this looks like an horrible hack and it is :) The problem
309 // is that owner is not initialized correctly when the bib
310 // inset is cut and pasted. Such hacks will not be needed
313 owner = current_view->currentBuffer();
315 // If we generate in a temp dir, we might need to give an
316 // absolute path there. This is a bit complicated since we can
317 // have a comma-separated list of bibliographies
318 string db_in, adb, db_out;
319 db_in = getContents();
320 db_in=split(db_in, adb, ',');
321 while(!adb.empty()) {
322 if (!owner->niceFile &&
323 IsFileReadable(MakeAbsPath(adb,owner->filepath)+".bib"))
324 adb = MakeAbsPath(adb,owner->filepath);
327 db_in=split(db_in, adb,',');
329 db_out = strip(db_out, ',');
333 && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
335 style = MakeAbsPath(getOptions(), owner->filepath);
337 style = getOptions();
339 file += "\\bibliographystyle{";
342 file += "\\bibliography{";
348 // This method returns a comma separated list of Bibtex entries
349 string InsetBibtex::getKeys()
351 // This hack is copied from InsetBibtex::Latex.
352 // Is it still needed? Probably yes.
354 owner = current_view->currentBuffer();
357 // We need to create absolute path names for bibliographies
358 // First look for bib-file in same directory as document,
359 // then in all directories listed in environment variable
361 string bibfiles, linebuf, tmp, keys;
362 bibfiles = getContents();
363 bibfiles=split(bibfiles, tmp, ',');
364 while(!tmp.empty()) {
365 if (IsFileReadable(MakeAbsPath(tmp,owner->filepath)+".bib"))
366 tmp = MakeAbsPath(tmp,owner->filepath)+".bib";
368 tmp = FileOpenSearch(GetEnvPath("BIBINPUTS"),tmp,"bib");
370 tmp = FileOpenSearch(GetEnvPath("BIBINPUT"),
373 // If we didn't find a matching file name just fail silently
376 // This is a _very_ simple parser for Bibtex database files.
377 // All it does is to look for lines starting in @ and not
378 // being @preamble and @string entries.
379 // It does NOT do any syntax checking!
380 FilePtr file(tmp,FilePtr::read);
383 // On some systems where feof() is a macro,
384 // the () after file is needed (JMarc)
385 while (! feof(file())) {
388 // At end of each line check if line begins with '@'
390 if (prefixIs(linebuf, "@") ) {
391 linebuf = subst(linebuf,
393 linebuf = split(linebuf,
395 tmp = lowercase(tmp);
396 if (!prefixIs(tmp, "@string") && !prefixIs(tmp, "@preamble") ) {
397 linebuf = split(linebuf, tmp,',');
399 keys += strip(tmp) + ",";
408 // Get next file name
409 bibfiles=split(bibfiles, tmp, ',');
414 // BibTeX should have its own dialog. This is provisional.
415 void InsetBibtex::Edit(int, int)
418 bibitem_form = create_form_bibitem_form();
419 fl_set_form_atclose(bibitem_form->bibitem_form,
420 CancelCloseBoxCB, 0);
423 bibitem_form->bibitem_form->u_vdata = this;
424 fl_set_object_label(bibitem_form->key, _("Database:"));
425 fl_set_object_label(bibitem_form->label, _("Style: "));
426 fl_set_input(bibitem_form->key, getContents().c_str());
427 fl_set_input(bibitem_form->label, getOptions().c_str());
428 if (bibitem_form->bibitem_form->visible) {
429 fl_raise_form(bibitem_form->bibitem_form);
431 fl_show_form(bibitem_form->bibitem_form,
432 FL_PLACE_MOUSE, FL_FULLBORDER,
438 bool InsetBibtex::addDatabase(string const & db)
440 if (!contains(contents, db.c_str())) {
441 if (!contents.empty())
450 bool InsetBibtex::delDatabase(string const & db)
452 if (contains(contents, db.c_str())) {
454 int n = tokenPos(contents, ',', bd);
456 // Weird code, would someone care to explain this?(Lgb)
459 contents = subst(contents, tmp.c_str(), ",");
461 contents = split(contents, bd, ',');
469 // This function should be in LyXView when multiframe works ale970302
470 void BibitemUpdate(Combox * combox)
474 if (!current_view->available())
477 string tmp, bibkeys = current_view->currentBuffer()->getBibkeyList(',');
478 bibkeys=split(bibkeys, tmp,',');
479 while (!tmp.empty()) {
480 combox->addto(tmp.c_str());
481 bibkeys=split(bibkeys, tmp,',');
487 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
488 int bibitemMaxWidth(LyXFont const & font)
491 // Does look like a hack? It is! (but will change at 0.13)
492 LyXParagraph *par = current_view->currentBuffer()->paragraph;
496 int wx = par->bibkey->Width(font);
506 string bibitemWidthest()
509 // Does look like a hack? It is! (but will change at 0.13)
510 LyXParagraph * par = current_view->currentBuffer()->paragraph;
511 InsetBibKey * bkey=0;
516 int wx = par->bibkey->Width(font);
525 if (bkey && !bkey->getScreenLabel().empty())
526 return bkey->getScreenLabel();