12 #include FORMS_H_LOCATION
17 #include "lyx_gui_misc.h"
18 #include "BufferView.h"
22 #include "support/filetools.h"
24 extern BufferView * current_view;
26 FD_citation_form * citation_form = 0;
27 FD_bibitem_form * bibitem_form = 0;
28 static Combox * bibcombox = 0;
30 extern void UpdateInset(BufferView *, Inset * inset, bool mark_dirty = true);
31 void BibitemUpdate(Combox *);
32 FD_citation_form * create_form_citation_form(void);
33 FD_bibitem_form * create_form_bibitem_form(void);
36 extern "C" void bibitem_cb(FL_OBJECT *, long data)
39 case 1: // OK, citation
41 if(!current_view->buffer()->isReadonly()) {
42 InsetCommand * inset = static_cast<InsetCommand*>(citation_form->citation_form->u_vdata);
43 inset->setContents(bibcombox->getline());
44 inset->setOptions(fl_get_input(citation_form->label));
45 fl_hide_form(citation_form->citation_form);
46 // shouldn't mark the buffer dirty unless something
47 // was actually altered
48 UpdateInset(current_view, inset);
51 // fall through to Cancel on RO-mode
53 case 0: fl_hide_form(citation_form->citation_form);
55 case 3: // OK, bibitem
57 if(!current_view->buffer()->isReadonly()) {
58 InsetCommand * inset = static_cast<InsetCommand*>(bibitem_form->bibitem_form->u_vdata);
59 inset->setContents(fl_get_input(bibitem_form->key));
60 inset->setOptions(fl_get_input(bibitem_form->label));
61 fl_hide_form(bibitem_form->bibitem_form);
62 // Does look like a hack? It is! (but will change at 0.13)
63 current_view->text->RedoParagraph();
64 current_view->update(1);
66 } // fall through to Cancel on RO-mode
68 case 2: // Cancel, bibitem
69 fl_hide_form(bibitem_form->bibitem_form); // Cancel, bibitem
75 FD_citation_form * create_form_citation_form(void)
78 FD_citation_form * fdui = (FD_citation_form *) fl_calloc(1, sizeof(FD_citation_form));
80 fdui->citation_form = fl_bgn_form(FL_NO_BOX, 220, 130);
81 obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
82 fdui->key = obj = fl_add_text(FL_NORMAL_TEXT, 20, 10, 60, 30, _("Key:"));
83 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
84 fl_set_object_lalign(obj, FL_ALIGN_RIGHT);
86 bibcombox = new Combox(FL_COMBOX_INPUT);
87 bibcombox->add(80, 10, 130, 30, 300);
89 obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
90 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
91 fl_set_object_callback(obj, bibitem_cb, 1);
92 obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
93 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
94 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
95 fl_set_object_callback(obj, bibitem_cb, 0);
96 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Remark:|#R")));
97 fl_set_input_shortcut(obj, scex(_("Remark:|#R")), 1);
98 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
101 //fdui->citation_form->fdui = fdui;
105 /*---------------------------------------*/
107 FD_bibitem_form * create_form_bibitem_form(void)
110 FD_bibitem_form * fdui = (FD_bibitem_form *) fl_calloc(1, sizeof(FD_bibitem_form));
112 fdui->bibitem_form = fl_bgn_form(FL_NO_BOX, 220, 130);
113 obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
114 fdui->key = obj = fl_add_input(FL_NORMAL_INPUT, 80, 10, 130, 30, idex(_("Key:|#K")));
115 fl_set_input_shortcut(obj, scex(_("Key:|#K")), 1);
116 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
117 obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
118 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
119 fl_set_object_callback(obj, bibitem_cb, 3);
120 obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
121 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
122 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
123 fl_set_object_callback(obj, bibitem_cb, 2);
124 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Label:|#L")));
125 fl_set_input_shortcut(obj, scex(_("Label:|#L")), 1);
126 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
129 //fdui->bibitem_form->fdui = fdui;
133 /*---------------------------------------*/
136 InsetCitation::InsetCitation(string const & key, string const & note)
137 : InsetCommand("cite", key, note)
143 InsetCitation::~InsetCitation()
145 if(citation_form && citation_form->citation_form
146 && citation_form->citation_form->visible
147 && citation_form->citation_form->u_vdata == this)
148 fl_hide_form(citation_form->citation_form);
152 void InsetCitation::Edit(int, int)
154 if(current_view->buffer()->isReadonly())
155 WarnReadonly(current_view->buffer()->fileName());
157 if (!citation_form) {
158 citation_form = create_form_citation_form();
159 fl_set_form_atclose(citation_form->citation_form,
160 CancelCloseBoxCB, 0);
162 citation_form->citation_form->u_vdata = this;
164 BibitemUpdate(bibcombox);
165 if (!bibcombox->select_text(getContents().c_str()))
166 bibcombox->addline(getContents().c_str());
168 fl_set_input(citation_form->label, getOptions().c_str());
169 if (citation_form->citation_form->visible) {
170 fl_raise_form(citation_form->citation_form);
172 fl_show_form(citation_form->citation_form,
173 FL_PLACE_MOUSE, FL_FULLBORDER,
179 string InsetCitation::getScreenLabel() const
185 if (!options.empty()) {
186 temp += ", " + options;
193 InsetBibKey::InsetBibKey(string const & key, string const & label):
194 InsetCommand("bibitem", key, label)
202 InsetBibKey::InsetBibKey(InsetBibKey const * b):
203 InsetCommand("bibitem", b->contents, b->options)
205 counter = b->counter;
209 InsetBibKey::~InsetBibKey()
211 if(bibitem_form && bibitem_form->bibitem_form
212 && bibitem_form->bibitem_form->visible)
213 fl_hide_form(bibitem_form->bibitem_form);
216 void InsetBibKey::setCounter(int c)
220 if (contents.empty())
221 contents += tostr(counter);
225 // I'm sorry but this is still necessary because \bibitem is used also
226 // as a LyX 2.x command, and lyxlex is not enough smart to understand
227 // real LaTeX commands. Yes, that could be fixed, but would be a waste
228 // of time cause LyX3 won't use lyxlex anyway. (ale)
229 void InsetBibKey::Write(ostream & os)
232 if (!options.empty()) {
238 os << "\\bibitem " << s << "\n";
242 string InsetBibKey::getScreenLabel() const
244 if (!options.empty())
247 return tostr(counter);
252 The value in "Key:" isn't allways set right after a few bibkey insets have
253 been added/removed. Perhaps the wrong object is deleted/used somewhere
257 void InsetBibKey::Edit(int, int)
259 if(current_view->buffer()->isReadonly())
260 WarnReadonly(current_view->buffer()->fileName());
263 bibitem_form = create_form_bibitem_form();
264 fl_set_form_atclose(bibitem_form->bibitem_form,
265 CancelCloseBoxCB, 0);
267 bibitem_form->bibitem_form->u_vdata = this;
268 // InsetBibtex uses the same form, with different labels
269 fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
270 fl_set_button_shortcut(bibitem_form->key, scex(_("Key:|#K")), 1);
271 fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
272 fl_set_button_shortcut(bibitem_form->label, scex(_("Label:|#L")), 1);
273 fl_set_input(bibitem_form->key, getContents().c_str());
274 fl_set_input(bibitem_form->label, getOptions().c_str());
275 if (bibitem_form->bibitem_form->visible) {
276 fl_raise_form(bibitem_form->bibitem_form);
278 fl_show_form(bibitem_form->bibitem_form, FL_PLACE_MOUSE,
280 _("Bibliography item"));
286 InsetBibtex::InsetBibtex(string const & dbase, string const & style,
288 : InsetCommand("BibTeX", dbase, style), owner(o)
293 string InsetBibtex::getScreenLabel() const
295 return _("BibTeX Generated References");
299 int InsetBibtex::Latex(ostream & os, signed char /*fragile*/)
302 signed char dummy = 0;
303 int result = Latex(bib, dummy);
309 int InsetBibtex::Latex(string & file, signed char /*fragile*/)
311 // this looks like an horrible hack and it is :) The problem
312 // is that owner is not initialized correctly when the bib
313 // inset is cut and pasted. Such hacks will not be needed
316 owner = current_view->buffer();
318 // If we generate in a temp dir, we might need to give an
319 // absolute path there. This is a bit complicated since we can
320 // have a comma-separated list of bibliographies
322 string db_in = getContents();
323 db_in = split(db_in, adb, ',');
324 while(!adb.empty()) {
325 if (!owner->niceFile &&
326 IsFileReadable(MakeAbsPath(adb, owner->filepath)+".bib"))
327 adb = MakeAbsPath(adb, owner->filepath);
330 db_in= split(db_in, adb,',');
332 db_out = strip(db_out, ',');
336 && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
338 style = MakeAbsPath(getOptions(), owner->filepath);
340 style = getOptions();
342 file += "\\bibliographystyle{";
345 file += "\\bibliography{";
352 // This method returns a comma separated list of Bibtex entries
353 string InsetBibtex::getKeys(char delim)
355 // This hack is copied from InsetBibtex::Latex.
356 // Is it still needed? Probably yes.
358 owner = current_view->buffer();
362 string bibfiles = getContents();
363 bibfiles = split(bibfiles, tmp, ',');
364 while(!tmp.empty()) {
365 string fil = findtexfile(ChangeExtension(tmp, "bib", false),
367 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
368 // If we didn't find a matching file name just fail silently
370 // This is a _very_ simple parser for Bibtex database
371 // files. All it does is to look for lines starting
372 // in @ and not being @preamble and @string entries.
373 // It does NOT do any syntax checking!
374 ifstream ifs(fil.c_str());
376 while (getline(ifs, linebuf)) {
377 linebuf = frontStrip(linebuf);
378 if (prefixIs(linebuf, "@")) {
379 linebuf = subst(linebuf, '{', '(');
380 linebuf = split(linebuf, tmp, '(');
381 tmp = lowercase(tmp);
382 if (!prefixIs(tmp, "@string")
383 && !prefixIs(tmp, "@preamble")) {
384 linebuf = split(linebuf,
386 tmp = frontStrip(strip(tmp));
395 // Get next file name
396 bibfiles = split(bibfiles, tmp, ',');
402 // BibTeX should have its own dialog. This is provisional.
403 void InsetBibtex::Edit(int, int)
406 bibitem_form = create_form_bibitem_form();
407 fl_set_form_atclose(bibitem_form->bibitem_form,
408 CancelCloseBoxCB, 0);
411 bibitem_form->bibitem_form->u_vdata = this;
412 fl_set_object_label(bibitem_form->key, _("Database:"));
413 fl_set_object_label(bibitem_form->label, _("Style: "));
414 fl_set_input(bibitem_form->key, getContents().c_str());
415 fl_set_input(bibitem_form->label, getOptions().c_str());
416 if (bibitem_form->bibitem_form->visible) {
417 fl_raise_form(bibitem_form->bibitem_form);
419 fl_show_form(bibitem_form->bibitem_form,
420 FL_PLACE_MOUSE, FL_FULLBORDER,
426 bool InsetBibtex::addDatabase(string const & db)
428 if (!contains(contents, db.c_str())) {
429 if (!contents.empty())
438 bool InsetBibtex::delDatabase(string const & db)
440 if (contains(contents, db.c_str())) {
442 int n = tokenPos(contents, ',', bd);
444 // Weird code, would someone care to explain this?(Lgb)
447 contents = subst(contents, tmp.c_str(), ", ");
449 contents = split(contents, bd, ',');
457 // This function should be in LyXView when multiframe works ale970302
458 void BibitemUpdate(Combox * combox)
462 if (!current_view->available()) return;
464 string tmp, bibkeys = current_view->buffer()->getBibkeyList(',');
465 bibkeys = split(bibkeys, tmp,',');
466 while (!tmp.empty()) {
467 combox->addto(tmp.c_str());
468 bibkeys = split(bibkeys, tmp,',');
474 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
476 int bibitemMaxWidth(Painter & pain, LyXFont const & font)
479 // Does look like a hack? It is! (but will change at 0.13)
480 LyXParagraph * par = current_view->buffer()->paragraph;
484 int wx = par->bibkey->width(pain, font);
492 int bibitemMaxWidth(LyXFont const & font)
495 // Does look like a hack? It is! (but will change at 0.13)
496 LyXParagraph * par = current_view->buffer()->paragraph;
500 int wx = par->bibkey->Width(font);
512 string bibitemWidthest(Painter & pain)
515 // Does look like a hack? It is! (but will change at 0.13)
516 LyXParagraph * par = current_view->buffer()->paragraph;
517 InsetBibKey * bkey = 0;
522 int wx = par->bibkey->width(pain, font);
531 if (bkey && !bkey->getScreenLabel().empty())
532 return bkey->getScreenLabel();
537 string bibitemWidthest()
540 // Does look like a hack? It is! (but will change at 0.13)
541 LyXParagraph * par = current_view->buffer()->paragraph;
542 InsetBibKey * bkey = 0;
547 int wx = par->bibkey->Width(font);
556 if (bkey && !bkey->getScreenLabel().empty())
557 return bkey->getScreenLabel();