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->buffer()->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->buffer()->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->buffer()->text->RedoParagraph();
62 current_view->buffer()->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->buffer()->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;
203 InsetBibKey::~InsetBibKey()
205 if(bibitem_form && bibitem_form->bibitem_form
206 && bibitem_form->bibitem_form->visible)
207 fl_hide_form(bibitem_form->bibitem_form);
210 void InsetBibKey::setCounter(int c)
214 if (contents.empty())
215 contents += tostr(counter);
219 // I'm sorry but this is still necessary because \bibitem is used also
220 // as a LyX 2.x command, and lyxlex is not enough smart to understand
221 // real LaTeX commands. Yes, that could be fixed, but would be a waste
222 // of time cause LyX3 won't use lyxlex anyway. (ale)
223 void InsetBibKey::Write(FILE * file)
226 if (!options.empty()) {
232 fprintf(file, "\\bibitem %s\n", s.c_str());
236 string InsetBibKey::getScreenLabel() const
238 if (!options.empty())
241 return tostr(counter);
246 The value in "Key:" isn't allways set right after a few bibkey insets have
247 been added/removed. Perhaps the wrong object is deleted/used somewhere
251 void InsetBibKey::Edit(int, int)
253 if(current_view->buffer()->isReadonly())
257 bibitem_form = create_form_bibitem_form();
258 fl_set_form_atclose(bibitem_form->bibitem_form,
259 CancelCloseBoxCB, 0);
261 bibitem_form->bibitem_form->u_vdata = this;
262 // InsetBibtex uses the same form, with different labels
263 fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
264 fl_set_button_shortcut(bibitem_form->key, scex(_("Key:|#K")), 1);
265 fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
266 fl_set_button_shortcut(bibitem_form->label, scex(_("Label:|#L")), 1);
267 fl_set_input(bibitem_form->key, getContents().c_str());
268 fl_set_input(bibitem_form->label, getOptions().c_str());
269 if (bibitem_form->bibitem_form->visible) {
270 fl_raise_form(bibitem_form->bibitem_form);
272 fl_show_form(bibitem_form->bibitem_form, FL_PLACE_MOUSE,
274 _("Bibliography item"));
280 InsetBibtex::InsetBibtex(string const & dbase, string const & style,
282 :InsetCommand("BibTeX", dbase, style), owner(o)
287 string InsetBibtex::getScreenLabel() const
289 return _("BibTeX Generated References");
293 int InsetBibtex::Latex(FILE *file, signed char /*fragile*/)
296 signed char dummy = 0;
297 int result = Latex(bib, dummy);
298 fprintf(file, "%s", bib.c_str());
303 int InsetBibtex::Latex(string &file, signed char /*fragile*/)
305 // this looks like an horrible hack and it is :) The problem
306 // is that owner is not initialized correctly when the bib
307 // inset is cut and pasted. Such hacks will not be needed
310 owner = current_view->buffer();
312 // If we generate in a temp dir, we might need to give an
313 // absolute path there. This is a bit complicated since we can
314 // have a comma-separated list of bibliographies
315 string db_in, adb, db_out;
316 db_in = getContents();
317 db_in= split(db_in, adb, ',');
318 while(!adb.empty()) {
319 if (!owner->niceFile &&
320 IsFileReadable(MakeAbsPath(adb, owner->filepath)+".bib"))
321 adb = MakeAbsPath(adb, owner->filepath);
324 db_in= split(db_in, adb,',');
326 db_out = strip(db_out, ',');
330 && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
332 style = MakeAbsPath(getOptions(), owner->filepath);
334 style = getOptions();
336 file += "\\bibliographystyle{";
339 file += "\\bibliography{";
345 // This method returns a comma separated list of Bibtex entries
346 string InsetBibtex::getKeys()
348 // This hack is copied from InsetBibtex::Latex.
349 // Is it still needed? Probably yes.
351 owner = current_view->buffer();
354 // We need to create absolute path names for bibliographies
355 // First look for bib-file in same directory as document,
356 // then in all directories listed in environment variable
358 string bibfiles, linebuf, tmp, keys;
359 bibfiles = getContents();
360 bibfiles= split(bibfiles, tmp, ',');
361 while(!tmp.empty()) {
362 if (IsFileReadable(MakeAbsPath(tmp, owner->filepath)+".bib"))
363 tmp = MakeAbsPath(tmp, owner->filepath)+".bib";
365 tmp = FileOpenSearch(GetEnvPath("BIBINPUTS"), tmp, "bib");
367 tmp = FileOpenSearch(GetEnvPath("BIBINPUT"),
370 // If we didn't find a matching file name just fail silently
373 // This is a _very_ simple parser for Bibtex database files.
374 // All it does is to look for lines starting in @ and not
375 // being @preamble and @string entries.
376 // It does NOT do any syntax checking!
377 FilePtr file(tmp, FilePtr::read);
380 // On some systems where feof() is a macro,
381 // the () after file is needed (JMarc)
382 while (! feof(file())) {
385 // At end of each line check if line begins with '@'
387 if (prefixIs(linebuf, "@") ) {
388 linebuf = subst(linebuf,
390 linebuf = split(linebuf,
392 tmp = lowercase(tmp);
393 if (!prefixIs(tmp, "@string") && !prefixIs(tmp, "@preamble") ) {
394 linebuf = split(linebuf, tmp,',');
396 keys += strip(tmp) + ", ";
405 // Get next file name
406 bibfiles= split(bibfiles, tmp, ',');
411 // BibTeX should have its own dialog. This is provisional.
412 void InsetBibtex::Edit(int, int)
415 bibitem_form = create_form_bibitem_form();
416 fl_set_form_atclose(bibitem_form->bibitem_form,
417 CancelCloseBoxCB, 0);
420 bibitem_form->bibitem_form->u_vdata = this;
421 fl_set_object_label(bibitem_form->key, _("Database:"));
422 fl_set_object_label(bibitem_form->label, _("Style: "));
423 fl_set_input(bibitem_form->key, getContents().c_str());
424 fl_set_input(bibitem_form->label, getOptions().c_str());
425 if (bibitem_form->bibitem_form->visible) {
426 fl_raise_form(bibitem_form->bibitem_form);
428 fl_show_form(bibitem_form->bibitem_form,
429 FL_PLACE_MOUSE, FL_FULLBORDER,
435 bool InsetBibtex::addDatabase(string const & db)
437 if (!contains(contents, db.c_str())) {
438 if (!contents.empty())
447 bool InsetBibtex::delDatabase(string const & db)
449 if (contains(contents, db.c_str())) {
451 int n = tokenPos(contents, ',', bd);
453 // Weird code, would someone care to explain this?(Lgb)
456 contents = subst(contents, tmp.c_str(), ", ");
458 contents = split(contents, bd, ',');
466 // This function should be in LyXView when multiframe works ale970302
467 void BibitemUpdate(Combox * combox)
471 if (!current_view->available())
474 string tmp, bibkeys = current_view->buffer()->getBibkeyList(',');
475 bibkeys = split(bibkeys, tmp,',');
476 while (!tmp.empty()) {
477 combox->addto(tmp.c_str());
478 bibkeys = split(bibkeys, tmp,',');
484 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
485 int bibitemMaxWidth(LyXFont const & font)
488 // Does look like a hack? It is! (but will change at 0.13)
489 LyXParagraph * par = current_view->buffer()->paragraph;
493 int wx = par->bibkey->Width(font);
503 string bibitemWidthest()
506 // Does look like a hack? It is! (but will change at 0.13)
507 LyXParagraph * par = current_view->buffer()->paragraph;
508 InsetBibKey * bkey = 0;
513 int wx = par->bibkey->Width(font);
522 if (bkey && !bkey->getScreenLabel().empty())
523 return bkey->getScreenLabel();