10 #pragma implementation
13 #include FORMS_H_LOCATION
18 #include "lyx_gui_misc.h"
19 #include "BufferView.h"
23 #include "support/filetools.h"
25 extern BufferView * current_view;
27 FD_citation_form * citation_form = 0;
28 FD_bibitem_form * bibitem_form = 0;
29 static Combox * bibcombox = 0;
31 extern void UpdateInset(Inset* inset, bool mark_dirty = true);
32 void BibitemUpdate(Combox *);
33 FD_citation_form * create_form_citation_form(void);
34 FD_bibitem_form * create_form_bibitem_form(void);
37 extern "C" void bibitem_cb(FL_OBJECT *, long data)
40 case 1: // OK, citation
42 if(!current_view->buffer()->isReadonly()) {
43 InsetCommand * inset = static_cast<InsetCommand*>(citation_form->citation_form->u_vdata);
44 inset->setContents(bibcombox->getline());
45 inset->setOptions(fl_get_input(citation_form->label));
46 fl_hide_form(citation_form->citation_form);
47 // shouldn't mark the buffer dirty unless something
48 // was actually altered
52 // fall through to Cancel on RO-mode
54 case 0: fl_hide_form(citation_form->citation_form);
56 case 3: // OK, bibitem
58 if(!current_view->buffer()->isReadonly()) {
59 InsetCommand *inset = static_cast<InsetCommand*>(bibitem_form->bibitem_form->u_vdata);
60 inset->setContents(fl_get_input(bibitem_form->key));
61 inset->setOptions(fl_get_input(bibitem_form->label));
62 fl_hide_form(bibitem_form->bibitem_form);
63 // Does look like a hack? It is! (but will change at 0.13)
64 current_view->buffer()->text->RedoParagraph();
65 current_view->buffer()->update(1);
67 } // fall through to Cancel on RO-mode
69 case 2: // Cancel, bibitem
70 fl_hide_form(bibitem_form->bibitem_form); // Cancel, bibitem
76 FD_citation_form * create_form_citation_form(void)
79 FD_citation_form *fdui = (FD_citation_form *) fl_calloc(1, sizeof(FD_citation_form));
81 fdui->citation_form = fl_bgn_form(FL_NO_BOX, 220, 130);
82 obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
83 fdui->key = obj = fl_add_text(FL_NORMAL_TEXT, 20, 10, 60, 30, _("Key:"));
84 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
85 fl_set_object_lalign(obj, FL_ALIGN_RIGHT);
87 bibcombox = new Combox(FL_COMBOX_INPUT);
88 bibcombox->add(80, 10, 130, 30, 120);
90 obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
91 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
92 fl_set_object_callback(obj, bibitem_cb, 1);
93 obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
94 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
95 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
96 fl_set_object_callback(obj, bibitem_cb, 0);
97 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Remark:|#R")));
98 fl_set_input_shortcut(obj, scex(_("Remark:|#R")), 1);
99 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
102 //fdui->citation_form->fdui = fdui;
106 /*---------------------------------------*/
108 FD_bibitem_form * create_form_bibitem_form(void)
111 FD_bibitem_form *fdui = (FD_bibitem_form *) fl_calloc(1, sizeof(FD_bibitem_form));
113 fdui->bibitem_form = fl_bgn_form(FL_NO_BOX, 220, 130);
114 obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
115 fdui->key = obj = fl_add_input(FL_NORMAL_INPUT, 80, 10, 130, 30, idex(_("Key:|#K")));
116 fl_set_input_shortcut(obj, scex(_("Key:|#K")), 1);
117 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
118 obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
119 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
120 fl_set_object_callback(obj, bibitem_cb, 3);
121 obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
122 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
123 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
124 fl_set_object_callback(obj, bibitem_cb, 2);
125 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Label:|#L")));
126 fl_set_input_shortcut(obj, scex(_("Label:|#L")), 1);
127 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
130 //fdui->bibitem_form->fdui = fdui;
134 /*---------------------------------------*/
137 InsetCitation::InsetCitation(string const & key, string const & note)
138 : InsetCommand("cite", key, note)
144 InsetCitation::~InsetCitation()
146 if(citation_form && citation_form->citation_form
147 && citation_form->citation_form->visible
148 && citation_form->citation_form->u_vdata == this)
149 fl_hide_form(citation_form->citation_form);
153 void InsetCitation::Edit(int, int)
155 if(current_view->buffer()->isReadonly())
158 if (!citation_form) {
159 citation_form = create_form_citation_form();
160 fl_set_form_atclose(citation_form->citation_form,
161 CancelCloseBoxCB, 0);
163 citation_form->citation_form->u_vdata = this;
165 BibitemUpdate(bibcombox);
166 if (!bibcombox->select_text(getContents().c_str()))
167 bibcombox->addline(getContents().c_str());
169 fl_set_input(citation_form->label, getOptions().c_str());
170 if (citation_form->citation_form->visible) {
171 fl_raise_form(citation_form->citation_form);
173 fl_show_form(citation_form->citation_form, 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())
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()
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 << "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 if (prefixIs(linebuf, "@")) {
378 linebuf = subst(linebuf, '{', '(');
379 linebuf = split(linebuf, tmp, '(');
380 tmp = lowercase(tmp);
381 if (!prefixIs(tmp, "@string")
382 && !prefixIs(tmp, "@preamble")) {
383 linebuf = split(linebuf,
393 // Get next file name
394 bibfiles = split(bibfiles, tmp, ',');
400 // BibTeX should have its own dialog. This is provisional.
401 void InsetBibtex::Edit(int, int)
404 bibitem_form = create_form_bibitem_form();
405 fl_set_form_atclose(bibitem_form->bibitem_form,
406 CancelCloseBoxCB, 0);
409 bibitem_form->bibitem_form->u_vdata = this;
410 fl_set_object_label(bibitem_form->key, _("Database:"));
411 fl_set_object_label(bibitem_form->label, _("Style: "));
412 fl_set_input(bibitem_form->key, getContents().c_str());
413 fl_set_input(bibitem_form->label, getOptions().c_str());
414 if (bibitem_form->bibitem_form->visible) {
415 fl_raise_form(bibitem_form->bibitem_form);
417 fl_show_form(bibitem_form->bibitem_form,
418 FL_PLACE_MOUSE, FL_FULLBORDER,
424 bool InsetBibtex::addDatabase(string const & db)
426 if (!contains(contents, db.c_str())) {
427 if (!contents.empty())
436 bool InsetBibtex::delDatabase(string const & db)
438 if (contains(contents, db.c_str())) {
440 int n = tokenPos(contents, ',', bd);
442 // Weird code, would someone care to explain this?(Lgb)
445 contents = subst(contents, tmp.c_str(), ", ");
447 contents = split(contents, bd, ',');
455 // This function should be in LyXView when multiframe works ale970302
456 void BibitemUpdate(Combox * combox)
460 if (!current_view->available()) return;
462 string tmp, bibkeys = current_view->buffer()->getBibkeyList(',');
463 bibkeys = split(bibkeys, tmp,',');
464 while (!tmp.empty()) {
465 combox->addto(tmp.c_str());
466 bibkeys = split(bibkeys, tmp,',');
472 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
473 int bibitemMaxWidth(LyXFont const & font)
476 // Does look like a hack? It is! (but will change at 0.13)
477 LyXParagraph * par = current_view->buffer()->paragraph;
481 int wx = par->bibkey->Width(font);
491 string bibitemWidthest()
494 // Does look like a hack? It is! (but will change at 0.13)
495 LyXParagraph * par = current_view->buffer()->paragraph;
496 InsetBibKey * bkey = 0;
501 int wx = par->bibkey->Width(font);
510 if (bkey && !bkey->getScreenLabel().empty())
511 return bkey->getScreenLabel();