10 #include FORMS_H_LOCATION
15 #include "lyx_gui_misc.h"
16 #include "BufferView.h"
20 #include "support/filetools.h"
21 #include "support/path.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;
207 int InsetCitation::Ascii(ostream & os) const
209 os << getScreenLabel();;
213 InsetBibKey::InsetBibKey(string const & key, string const & label):
214 InsetCommand("bibitem", key, label)
222 InsetBibKey::InsetBibKey(InsetBibKey const * b):
223 InsetCommand("bibitem", b->contents, b->options)
225 counter = b->counter;
229 InsetBibKey::~InsetBibKey()
231 if(bibitem_form && bibitem_form->bibitem_form
232 && bibitem_form->bibitem_form->visible
233 && bibitem_form->bibitem_form->u_vdata == &holder)
234 fl_hide_form(bibitem_form->bibitem_form);
237 void InsetBibKey::setCounter(int c)
241 if (contents.empty())
242 contents += tostr(counter);
246 // I'm sorry but this is still necessary because \bibitem is used also
247 // as a LyX 2.x command, and lyxlex is not enough smart to understand
248 // real LaTeX commands. Yes, that could be fixed, but would be a waste
249 // of time cause LyX3 won't use lyxlex anyway. (ale)
250 void InsetBibKey::Write(ostream & os) const
253 if (!options.empty()) {
258 << contents << "}\n";
262 string InsetBibKey::getScreenLabel() const
264 if (!options.empty())
267 return tostr(counter);
272 The value in "Key:" isn't allways set right after a few bibkey insets have
273 been added/removed. Perhaps the wrong object is deleted/used somewhere
277 void InsetBibKey::Edit(BufferView * bv, int, int, unsigned int)
279 if(bv->buffer()->isReadonly())
280 WarnReadonly(bv->buffer()->fileName());
283 bibitem_form = create_form_bibitem_form();
284 fl_set_form_atclose(bibitem_form->bibitem_form,
285 CancelCloseBoxCB, 0);
291 bibitem_form->bibitem_form->u_vdata = &holder;
292 // InsetBibtex uses the same form, with different labels
293 fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
294 fl_set_button_shortcut(bibitem_form->key, scex(_("Key:|#K")), 1);
295 fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
296 fl_set_button_shortcut(bibitem_form->label, scex(_("Label:|#L")), 1);
297 fl_set_input(bibitem_form->key, getContents().c_str());
298 fl_set_input(bibitem_form->label, getOptions().c_str());
299 if (bibitem_form->bibitem_form->visible) {
300 fl_raise_form(bibitem_form->bibitem_form);
302 fl_show_form(bibitem_form->bibitem_form, FL_PLACE_MOUSE,
304 _("Bibliography item"));
310 InsetBibtex::InsetBibtex(string const & dbase, string const & style,
312 : InsetCommand("BibTeX", dbase, style), owner(o)
316 InsetBibtex::~InsetBibtex()
318 if(bibitem_form && bibitem_form->bibitem_form
319 && bibitem_form->bibitem_form->visible
320 && bibitem_form->bibitem_form->u_vdata == &holder)
321 fl_hide_form(bibitem_form->bibitem_form);
325 string InsetBibtex::getScreenLabel() const
327 return _("BibTeX Generated References");
331 int InsetBibtex::Latex(ostream & os,
332 bool /*fragile*/, bool/*fs*/) const
334 // this looks like an horrible hack and it is :) The problem
335 // is that owner is not initialized correctly when the bib
336 // inset is cut and pasted. Such hacks will not be needed
339 owner = current_view->buffer();
341 // If we generate in a temp dir, we might need to give an
342 // absolute path there. This is a bit complicated since we can
343 // have a comma-separated list of bibliographies
345 string db_in = getContents();
346 db_in = split(db_in, adb, ',');
347 while(!adb.empty()) {
348 if (!owner->niceFile &&
349 IsFileReadable(MakeAbsPath(adb, owner->filepath)+".bib"))
350 adb = MakeAbsPath(adb, owner->filepath);
354 db_in= split(db_in, adb,',');
356 db_out = strip(db_out, ',');
360 && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
362 style = MakeAbsPath(getOptions(), owner->filepath);
364 style = getOptions();
366 os << "\\bibliographystyle{" << style << "}\n"
367 << "\\bibliography{" << db_out << "}\n";
372 // This method returns a comma separated list of Bibtex entries
373 string InsetBibtex::getKeys(char delim)
375 // This hack is copied from InsetBibtex::Latex.
376 // Is it still needed? Probably yes.
377 // Why is this needed here when it already is in Latex?
378 // Anyway we need a different way to get to the
379 // buffer the inset is in. (Lgb)
382 // owner = current_view->buffer();
385 Path p(owner->filepath);
388 string bibfiles = getContents();
389 bibfiles = split(bibfiles, tmp, ',');
390 while(!tmp.empty()) {
391 string fil = findtexfile(ChangeExtension(tmp, "bib"),
393 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
394 // If we didn't find a matching file name just fail silently
396 // This is a _very_ simple parser for Bibtex database
397 // files. All it does is to look for lines starting
398 // in @ and not being @preamble and @string entries.
399 // It does NOT do any syntax checking!
400 ifstream ifs(fil.c_str());
402 while (getline(ifs, linebuf)) {
403 linebuf = frontStrip(linebuf);
404 if (prefixIs(linebuf, "@")) {
405 linebuf = subst(linebuf, '{', '(');
406 linebuf = split(linebuf, tmp, '(');
407 tmp = lowercase(tmp);
408 if (!prefixIs(tmp, "@string")
409 && !prefixIs(tmp, "@preamble")) {
410 linebuf = split(linebuf,
412 tmp = frontStrip(strip(tmp));
421 // Get next file name
422 bibfiles = split(bibfiles, tmp, ',');
428 // BibTeX should have its own dialog. This is provisional.
429 void InsetBibtex::Edit(BufferView * bv, int, int, unsigned int)
432 bibitem_form = create_form_bibitem_form();
433 fl_set_form_atclose(bibitem_form->bibitem_form,
434 CancelCloseBoxCB, 0);
439 bibitem_form->bibitem_form->u_vdata = &holder;
441 fl_set_object_label(bibitem_form->key, _("Database:"));
442 fl_set_object_label(bibitem_form->label, _("Style: "));
443 fl_set_input(bibitem_form->key, getContents().c_str());
444 fl_set_input(bibitem_form->label, getOptions().c_str());
445 if (bibitem_form->bibitem_form->visible) {
446 fl_raise_form(bibitem_form->bibitem_form);
448 fl_show_form(bibitem_form->bibitem_form,
449 FL_PLACE_MOUSE, FL_FULLBORDER,
455 bool InsetBibtex::addDatabase(string const & db)
457 if (!contains(contents, db.c_str())) {
458 if (!contents.empty())
467 bool InsetBibtex::delDatabase(string const & db)
469 if (contains(contents, db.c_str())) {
471 int n = tokenPos(contents, ',', bd);
473 // Weird code, would someone care to explain this?(Lgb)
476 contents = subst(contents, tmp.c_str(), ", ");
478 contents = split(contents, bd, ',');
486 // This function should be in LyXView when multiframe works ale970302
487 void BibitemUpdate(Combox * combox)
491 if (!current_view->available()) return;
493 string tmp, bibkeys = current_view->buffer()->getBibkeyList(',');
494 bibkeys = split(bibkeys, tmp,',');
495 while (!tmp.empty()) {
496 combox->addto(tmp.c_str());
497 bibkeys = split(bibkeys, tmp,',');
503 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
504 int bibitemMaxWidth(Painter & pain, LyXFont const & font)
507 // Does look like a hack? It is! (but will change at 0.13)
508 LyXParagraph * par = current_view->buffer()->paragraph;
512 int wx = par->bibkey->width(pain, font);
522 string bibitemWidthest(Painter & pain)
525 // Does look like a hack? It is! (but will change at 0.13)
526 LyXParagraph * par = current_view->buffer()->paragraph;
527 InsetBibKey * bkey = 0;
532 int wx = par->bibkey->width(pain, font);
541 if (bkey && !bkey->getScreenLabel().empty())
542 return bkey->getScreenLabel();