10 #pragma implementation
13 #include FORMS_H_LOCATION
18 #include "lyx_gui_misc.h"
19 #include "BufferView.h"
23 #include "support/filetools.h"
28 extern BufferView * current_view;
30 FD_citation_form * citation_form = 0;
31 FD_bibitem_form * bibitem_form = 0;
32 static Combox * bibcombox = 0;
34 void BibitemUpdate(Combox *);
35 FD_citation_form * create_form_citation_form(void);
36 FD_bibitem_form * create_form_bibitem_form(void);
39 extern "C" void bibitem_cb(FL_OBJECT *, long data)
42 case 1: // OK, citation
44 InsetCitation::Holder * holder =
45 static_cast<InsetCitation::Holder*>
46 (citation_form->citation_form->u_vdata);
47 if(!holder->view->buffer()->isReadonly()) {
49 InsetCitation * inset = holder->inset;
50 inset->setContents(bibcombox->getline());
51 inset->setOptions(fl_get_input(citation_form->label));
52 fl_hide_form(citation_form->citation_form);
53 // shouldn't mark the buffer dirty unless something
54 // was actually altered
55 holder->view->updateInset(inset, true);
58 // fall through to Cancel on RO-mode
60 case 0: fl_hide_form(citation_form->citation_form);
62 case 3: // OK, bibitem
64 InsetBibKey::Holder * holder =
65 static_cast<InsetBibKey::Holder*>
66 (bibitem_form->bibitem_form->u_vdata);
68 if(!holder->view->buffer()->isReadonly()) {
69 InsetBibKey * inset = holder->inset;
70 inset->setContents(fl_get_input(bibitem_form->key));
71 inset->setOptions(fl_get_input(bibitem_form->label));
72 fl_hide_form(bibitem_form->bibitem_form);
73 // Does look like a hack? It is! (but will change at 0.13)
74 holder->view->text->RedoParagraph();
75 holder->view->update(1);
77 } // fall through to Cancel on RO-mode
79 case 2: // Cancel, bibitem
80 fl_hide_form(bibitem_form->bibitem_form); // Cancel, bibitem
86 FD_citation_form * create_form_citation_form(void)
89 FD_citation_form * fdui = (FD_citation_form *) fl_calloc(1, sizeof(FD_citation_form));
91 fdui->citation_form = fl_bgn_form(FL_NO_BOX, 220, 130);
92 obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
93 fdui->key = obj = fl_add_text(FL_NORMAL_TEXT, 20, 10, 60, 30, _("Key:"));
94 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
95 fl_set_object_lalign(obj, FL_ALIGN_RIGHT);
97 bibcombox = new Combox(FL_COMBOX_INPUT);
98 bibcombox->add(80, 10, 130, 30, 300);
100 obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
101 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
102 fl_set_object_callback(obj, bibitem_cb, 1);
103 obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
104 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
105 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
106 fl_set_object_callback(obj, bibitem_cb, 0);
107 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Remark:|#R")));
108 fl_set_input_shortcut(obj, scex(_("Remark:|#R")), 1);
109 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
112 //fdui->citation_form->fdui = fdui;
116 /*---------------------------------------*/
118 FD_bibitem_form * create_form_bibitem_form(void)
121 FD_bibitem_form * fdui = (FD_bibitem_form *) fl_calloc(1, sizeof(FD_bibitem_form));
123 fdui->bibitem_form = fl_bgn_form(FL_NO_BOX, 220, 130);
124 obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
125 fdui->key = obj = fl_add_input(FL_NORMAL_INPUT, 80, 10, 130, 30, idex(_("Key:|#K")));
126 fl_set_input_shortcut(obj, scex(_("Key:|#K")), 1);
127 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
128 obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
129 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
130 fl_set_object_callback(obj, bibitem_cb, 3);
131 obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
132 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
133 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
134 fl_set_object_callback(obj, bibitem_cb, 2);
135 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Label:|#L")));
136 fl_set_input_shortcut(obj, scex(_("Label:|#L")), 1);
137 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
140 //fdui->bibitem_form->fdui = fdui;
144 /*---------------------------------------*/
147 InsetCitation::InsetCitation(string const & key, string const & note)
148 : InsetCommand("cite", key, note)
154 InsetCitation::~InsetCitation()
156 if(citation_form && citation_form->citation_form
157 && citation_form->citation_form->visible
158 && citation_form->citation_form->u_vdata == &holder)
159 fl_hide_form(citation_form->citation_form);
163 void InsetCitation::Edit(BufferView * bv, int, int, unsigned int)
165 if(bv->buffer()->isReadonly())
166 WarnReadonly(bv->buffer()->fileName());
168 if (!citation_form) {
169 citation_form = create_form_citation_form();
170 fl_set_form_atclose(citation_form->citation_form,
171 CancelCloseBoxCB, 0);
177 citation_form->citation_form->u_vdata = &holder;
179 BibitemUpdate(bibcombox);
180 if (!bibcombox->select_text(getContents().c_str()))
181 bibcombox->addline(getContents().c_str());
183 fl_set_input(citation_form->label, getOptions().c_str());
184 if (citation_form->citation_form->visible) {
185 fl_raise_form(citation_form->citation_form);
187 fl_show_form(citation_form->citation_form,
188 FL_PLACE_MOUSE, FL_FULLBORDER,
194 string InsetCitation::getScreenLabel() const
200 if (!options.empty()) {
201 temp += ", " + options;
208 InsetBibKey::InsetBibKey(string const & key, string const & label):
209 InsetCommand("bibitem", key, label)
217 InsetBibKey::InsetBibKey(InsetBibKey const * b):
218 InsetCommand("bibitem", b->contents, b->options)
220 counter = b->counter;
224 InsetBibKey::~InsetBibKey()
226 if(bibitem_form && bibitem_form->bibitem_form
227 && bibitem_form->bibitem_form->visible
228 && bibitem_form->bibitem_form->u_vdata == &holder)
229 fl_hide_form(bibitem_form->bibitem_form);
232 void InsetBibKey::setCounter(int c)
236 if (contents.empty())
237 contents += tostr(counter);
241 // I'm sorry but this is still necessary because \bibitem is used also
242 // as a LyX 2.x command, and lyxlex is not enough smart to understand
243 // real LaTeX commands. Yes, that could be fixed, but would be a waste
244 // of time cause LyX3 won't use lyxlex anyway. (ale)
245 void InsetBibKey::Write(ostream & os) const
248 if (!options.empty()) {
253 << contents << "}\n";
257 string InsetBibKey::getScreenLabel() const
259 if (!options.empty())
262 return tostr(counter);
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
272 void InsetBibKey::Edit(BufferView * bv, int, int, unsigned int)
274 if(bv->buffer()->isReadonly())
275 WarnReadonly(bv->buffer()->fileName());
278 bibitem_form = create_form_bibitem_form();
279 fl_set_form_atclose(bibitem_form->bibitem_form,
280 CancelCloseBoxCB, 0);
286 bibitem_form->bibitem_form->u_vdata = &holder;
287 // InsetBibtex uses the same form, with different labels
288 fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
289 fl_set_button_shortcut(bibitem_form->key, scex(_("Key:|#K")), 1);
290 fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
291 fl_set_button_shortcut(bibitem_form->label, scex(_("Label:|#L")), 1);
292 fl_set_input(bibitem_form->key, getContents().c_str());
293 fl_set_input(bibitem_form->label, getOptions().c_str());
294 if (bibitem_form->bibitem_form->visible) {
295 fl_raise_form(bibitem_form->bibitem_form);
297 fl_show_form(bibitem_form->bibitem_form, FL_PLACE_MOUSE,
299 _("Bibliography item"));
305 InsetBibtex::InsetBibtex(string const & dbase, string const & style,
307 : InsetCommand("BibTeX", dbase, style), owner(o)
311 InsetBibtex::~InsetBibtex()
313 if(bibitem_form && bibitem_form->bibitem_form
314 && bibitem_form->bibitem_form->visible
315 && bibitem_form->bibitem_form->u_vdata == &holder)
316 fl_hide_form(bibitem_form->bibitem_form);
320 string InsetBibtex::getScreenLabel() const
322 return _("BibTeX Generated References");
326 int InsetBibtex::Latex(ostream & os,
327 bool /*fragile*/, bool/*fs*/) const
329 // this looks like an horrible hack and it is :) The problem
330 // is that owner is not initialized correctly when the bib
331 // inset is cut and pasted. Such hacks will not be needed
334 owner = current_view->buffer();
336 // If we generate in a temp dir, we might need to give an
337 // absolute path there. This is a bit complicated since we can
338 // have a comma-separated list of bibliographies
340 string db_in = getContents();
341 db_in = split(db_in, adb, ',');
342 while(!adb.empty()) {
343 if (!owner->niceFile &&
344 IsFileReadable(MakeAbsPath(adb, owner->filepath)+".bib"))
345 adb = MakeAbsPath(adb, owner->filepath);
348 db_in= split(db_in, adb,',');
350 db_out = strip(db_out, ',');
354 && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
356 style = MakeAbsPath(getOptions(), owner->filepath);
358 style = getOptions();
360 os << "\\bibliographystyle{" << style << "}\n"
361 << "\\bibliography{" << db_out << "}\n";
366 // This method returns a comma separated list of Bibtex entries
367 string InsetBibtex::getKeys(char delim)
369 // This hack is copied from InsetBibtex::Latex.
370 // Is it still needed? Probably yes.
371 // Why is this needed here when it already is in Latex?
372 // Anyway we need a different way to get to the
373 // buffer the inset is in. (Lgb)
376 // owner = current_view->buffer();
380 string bibfiles = getContents();
381 bibfiles = split(bibfiles, tmp, ',');
382 while(!tmp.empty()) {
383 string fil = findtexfile(ChangeExtension(tmp, "bib", false),
385 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
386 // If we didn't find a matching file name just fail silently
388 // This is a _very_ simple parser for Bibtex database
389 // files. All it does is to look for lines starting
390 // in @ and not being @preamble and @string entries.
391 // It does NOT do any syntax checking!
392 ifstream ifs(fil.c_str());
394 while (getline(ifs, linebuf)) {
395 linebuf = frontStrip(linebuf);
396 if (prefixIs(linebuf, "@")) {
397 linebuf = subst(linebuf, '{', '(');
398 linebuf = split(linebuf, tmp, '(');
399 tmp = lowercase(tmp);
400 if (!prefixIs(tmp, "@string")
401 && !prefixIs(tmp, "@preamble")) {
402 linebuf = split(linebuf,
404 tmp = frontStrip(strip(tmp));
413 // Get next file name
414 bibfiles = split(bibfiles, tmp, ',');
420 // BibTeX should have its own dialog. This is provisional.
421 void InsetBibtex::Edit(BufferView * bv, int, int, unsigned int)
424 bibitem_form = create_form_bibitem_form();
425 fl_set_form_atclose(bibitem_form->bibitem_form,
426 CancelCloseBoxCB, 0);
431 bibitem_form->bibitem_form->u_vdata = &holder;
433 fl_set_object_label(bibitem_form->key, _("Database:"));
434 fl_set_object_label(bibitem_form->label, _("Style: "));
435 fl_set_input(bibitem_form->key, getContents().c_str());
436 fl_set_input(bibitem_form->label, getOptions().c_str());
437 if (bibitem_form->bibitem_form->visible) {
438 fl_raise_form(bibitem_form->bibitem_form);
440 fl_show_form(bibitem_form->bibitem_form,
441 FL_PLACE_MOUSE, FL_FULLBORDER,
447 bool InsetBibtex::addDatabase(string const & db)
449 if (!contains(contents, db.c_str())) {
450 if (!contents.empty())
459 bool InsetBibtex::delDatabase(string const & db)
461 if (contains(contents, db.c_str())) {
463 int n = tokenPos(contents, ',', bd);
465 // Weird code, would someone care to explain this?(Lgb)
468 contents = subst(contents, tmp.c_str(), ", ");
470 contents = split(contents, bd, ',');
478 // This function should be in LyXView when multiframe works ale970302
479 void BibitemUpdate(Combox * combox)
483 if (!current_view->available()) return;
485 string tmp, bibkeys = current_view->buffer()->getBibkeyList(',');
486 bibkeys = split(bibkeys, tmp,',');
487 while (!tmp.empty()) {
488 combox->addto(tmp.c_str());
489 bibkeys = split(bibkeys, tmp,',');
495 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
496 int bibitemMaxWidth(Painter & pain, LyXFont const & font)
499 // Does look like a hack? It is! (but will change at 0.13)
500 LyXParagraph * par = current_view->buffer()->paragraph;
504 int wx = par->bibkey->width(pain, font);
514 string bibitemWidthest(Painter & pain)
517 // Does look like a hack? It is! (but will change at 0.13)
518 LyXParagraph * par = current_view->buffer()->paragraph;
519 InsetBibKey * bkey = 0;
524 int wx = par->bibkey->width(pain, font);
533 if (bkey && !bkey->getScreenLabel().empty())
534 return bkey->getScreenLabel();