12 #include FORMS_H_LOCATION
17 #include "lyx_gui_misc.h"
18 #include "BufferView.h"
22 #include "support/filetools.h"
26 extern BufferView * current_view;
28 FD_citation_form * citation_form = 0;
29 FD_bibitem_form * bibitem_form = 0;
30 static Combox * bibcombox = 0;
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 InsetCitation::Holder * holder =
43 static_cast<InsetCitation::Holder*>
44 (citation_form->citation_form->u_vdata);
45 if(!holder->view->buffer()->isReadonly()) {
47 InsetCitation * inset = holder->inset;
48 inset->setContents(bibcombox->getline());
49 inset->setOptions(fl_get_input(citation_form->label));
50 fl_hide_form(citation_form->citation_form);
51 // shouldn't mark the buffer dirty unless something
52 // was actually altered
53 holder->view->updateInset(inset, true);
56 // fall through to Cancel on RO-mode
58 case 0: fl_hide_form(citation_form->citation_form);
60 case 3: // OK, bibitem
62 InsetBibKey::Holder * holder =
63 static_cast<InsetBibKey::Holder*>
64 (bibitem_form->bibitem_form->u_vdata);
66 if(!holder->view->buffer()->isReadonly()) {
67 InsetBibKey * inset = holder->inset;
68 inset->setContents(fl_get_input(bibitem_form->key));
69 inset->setOptions(fl_get_input(bibitem_form->label));
70 fl_hide_form(bibitem_form->bibitem_form);
71 // Does look like a hack? It is! (but will change at 0.13)
72 holder->view->text->RedoParagraph();
73 holder->view->update(1);
75 } // fall through to Cancel on RO-mode
77 case 2: // Cancel, bibitem
78 fl_hide_form(bibitem_form->bibitem_form); // Cancel, bibitem
84 FD_citation_form * create_form_citation_form(void)
87 FD_citation_form * fdui = (FD_citation_form *) fl_calloc(1, sizeof(FD_citation_form));
89 fdui->citation_form = fl_bgn_form(FL_NO_BOX, 220, 130);
90 obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
91 fdui->key = obj = fl_add_text(FL_NORMAL_TEXT, 20, 10, 60, 30, _("Key:"));
92 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
93 fl_set_object_lalign(obj, FL_ALIGN_RIGHT);
95 bibcombox = new Combox(FL_COMBOX_INPUT);
96 bibcombox->add(80, 10, 130, 30, 300);
98 obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
99 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
100 fl_set_object_callback(obj, bibitem_cb, 1);
101 obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
102 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
103 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
104 fl_set_object_callback(obj, bibitem_cb, 0);
105 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Remark:|#R")));
106 fl_set_input_shortcut(obj, scex(_("Remark:|#R")), 1);
107 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
110 //fdui->citation_form->fdui = fdui;
114 /*---------------------------------------*/
116 FD_bibitem_form * create_form_bibitem_form(void)
119 FD_bibitem_form * fdui = (FD_bibitem_form *) fl_calloc(1, sizeof(FD_bibitem_form));
121 fdui->bibitem_form = fl_bgn_form(FL_NO_BOX, 220, 130);
122 obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
123 fdui->key = obj = fl_add_input(FL_NORMAL_INPUT, 80, 10, 130, 30, idex(_("Key:|#K")));
124 fl_set_input_shortcut(obj, scex(_("Key:|#K")), 1);
125 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
126 obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
127 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
128 fl_set_object_callback(obj, bibitem_cb, 3);
129 obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
130 fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
131 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
132 fl_set_object_callback(obj, bibitem_cb, 2);
133 fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Label:|#L")));
134 fl_set_input_shortcut(obj, scex(_("Label:|#L")), 1);
135 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
138 //fdui->bibitem_form->fdui = fdui;
142 /*---------------------------------------*/
145 InsetCitation::InsetCitation(string const & key, string const & note)
146 : InsetCommand("cite", key, note)
152 InsetCitation::~InsetCitation()
154 if(citation_form && citation_form->citation_form
155 && citation_form->citation_form->visible
156 && citation_form->citation_form->u_vdata == &holder)
157 fl_hide_form(citation_form->citation_form);
161 void InsetCitation::Edit(BufferView * bv, int, int, unsigned int)
163 if(bv->buffer()->isReadonly())
164 WarnReadonly(bv->buffer()->fileName());
166 if (!citation_form) {
167 citation_form = create_form_citation_form();
168 fl_set_form_atclose(citation_form->citation_form,
169 CancelCloseBoxCB, 0);
175 citation_form->citation_form->u_vdata = &holder;
177 BibitemUpdate(bibcombox);
178 if (!bibcombox->select_text(getContents().c_str()))
179 bibcombox->addline(getContents().c_str());
181 fl_set_input(citation_form->label, getOptions().c_str());
182 if (citation_form->citation_form->visible) {
183 fl_raise_form(citation_form->citation_form);
185 fl_show_form(citation_form->citation_form,
186 FL_PLACE_MOUSE, FL_FULLBORDER,
192 string InsetCitation::getScreenLabel() const
198 if (!options.empty()) {
199 temp += ", " + options;
206 InsetBibKey::InsetBibKey(string const & key, string const & label):
207 InsetCommand("bibitem", key, label)
215 InsetBibKey::InsetBibKey(InsetBibKey const * b):
216 InsetCommand("bibitem", b->contents, b->options)
218 counter = b->counter;
222 InsetBibKey::~InsetBibKey()
224 if(bibitem_form && bibitem_form->bibitem_form
225 && bibitem_form->bibitem_form->visible
226 && bibitem_form->bibitem_form->u_vdata == &holder)
227 fl_hide_form(bibitem_form->bibitem_form);
230 void InsetBibKey::setCounter(int c)
234 if (contents.empty())
235 contents += tostr(counter);
239 // I'm sorry but this is still necessary because \bibitem is used also
240 // as a LyX 2.x command, and lyxlex is not enough smart to understand
241 // real LaTeX commands. Yes, that could be fixed, but would be a waste
242 // of time cause LyX3 won't use lyxlex anyway. (ale)
243 void InsetBibKey::Write(ostream & os) const
246 if (!options.empty()) {
251 << contents << "}\n";
255 string InsetBibKey::getScreenLabel() const
257 if (!options.empty())
260 return tostr(counter);
265 The value in "Key:" isn't allways set right after a few bibkey insets have
266 been added/removed. Perhaps the wrong object is deleted/used somewhere
270 void InsetBibKey::Edit(BufferView * bv, int, int, unsigned int)
272 if(bv->buffer()->isReadonly())
273 WarnReadonly(bv->buffer()->fileName());
276 bibitem_form = create_form_bibitem_form();
277 fl_set_form_atclose(bibitem_form->bibitem_form,
278 CancelCloseBoxCB, 0);
284 bibitem_form->bibitem_form->u_vdata = &holder;
285 // InsetBibtex uses the same form, with different labels
286 fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
287 fl_set_button_shortcut(bibitem_form->key, scex(_("Key:|#K")), 1);
288 fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
289 fl_set_button_shortcut(bibitem_form->label, scex(_("Label:|#L")), 1);
290 fl_set_input(bibitem_form->key, getContents().c_str());
291 fl_set_input(bibitem_form->label, getOptions().c_str());
292 if (bibitem_form->bibitem_form->visible) {
293 fl_raise_form(bibitem_form->bibitem_form);
295 fl_show_form(bibitem_form->bibitem_form, FL_PLACE_MOUSE,
297 _("Bibliography item"));
303 InsetBibtex::InsetBibtex(string const & dbase, string const & style,
305 : InsetCommand("BibTeX", dbase, style), owner(o)
309 InsetBibtex::~InsetBibtex()
311 if(bibitem_form && bibitem_form->bibitem_form
312 && bibitem_form->bibitem_form->visible
313 && bibitem_form->bibitem_form->u_vdata == &holder)
314 fl_hide_form(bibitem_form->bibitem_form);
318 string InsetBibtex::getScreenLabel() const
320 return _("BibTeX Generated References");
324 int InsetBibtex::Latex(ostream & os,
325 signed char /*fragile*/, bool/*fs*/) const
327 // this looks like an horrible hack and it is :) The problem
328 // is that owner is not initialized correctly when the bib
329 // inset is cut and pasted. Such hacks will not be needed
332 owner = current_view->buffer();
334 // If we generate in a temp dir, we might need to give an
335 // absolute path there. This is a bit complicated since we can
336 // have a comma-separated list of bibliographies
338 string db_in = getContents();
339 db_in = split(db_in, adb, ',');
340 while(!adb.empty()) {
341 if (!owner->niceFile &&
342 IsFileReadable(MakeAbsPath(adb, owner->filepath)+".bib"))
343 adb = MakeAbsPath(adb, owner->filepath);
346 db_in= split(db_in, adb,',');
348 db_out = strip(db_out, ',');
352 && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
354 style = MakeAbsPath(getOptions(), owner->filepath);
356 style = getOptions();
358 os << "\\bibliographystyle{" << style << "}\n"
359 << "\\bibliography{" << db_out << "}\n";
364 // This method returns a comma separated list of Bibtex entries
365 string InsetBibtex::getKeys(char delim)
367 // This hack is copied from InsetBibtex::Latex.
368 // Is it still needed? Probably yes.
369 // Why is this needed here when it already is in Latex?
370 // Anyway we need a different way to get to the
371 // buffer the inset is in. (Lgb)
374 // owner = current_view->buffer();
378 string bibfiles = getContents();
379 bibfiles = split(bibfiles, tmp, ',');
380 while(!tmp.empty()) {
381 string fil = findtexfile(ChangeExtension(tmp, "bib", false),
383 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
384 // If we didn't find a matching file name just fail silently
386 // This is a _very_ simple parser for Bibtex database
387 // files. All it does is to look for lines starting
388 // in @ and not being @preamble and @string entries.
389 // It does NOT do any syntax checking!
390 ifstream ifs(fil.c_str());
392 while (getline(ifs, linebuf)) {
393 linebuf = frontStrip(linebuf);
394 if (prefixIs(linebuf, "@")) {
395 linebuf = subst(linebuf, '{', '(');
396 linebuf = split(linebuf, tmp, '(');
397 tmp = lowercase(tmp);
398 if (!prefixIs(tmp, "@string")
399 && !prefixIs(tmp, "@preamble")) {
400 linebuf = split(linebuf,
402 tmp = frontStrip(strip(tmp));
411 // Get next file name
412 bibfiles = split(bibfiles, tmp, ',');
418 // BibTeX should have its own dialog. This is provisional.
419 void InsetBibtex::Edit(BufferView * bv, int, int, unsigned int)
422 bibitem_form = create_form_bibitem_form();
423 fl_set_form_atclose(bibitem_form->bibitem_form,
424 CancelCloseBoxCB, 0);
429 bibitem_form->bibitem_form->u_vdata = &holder;
431 fl_set_object_label(bibitem_form->key, _("Database:"));
432 fl_set_object_label(bibitem_form->label, _("Style: "));
433 fl_set_input(bibitem_form->key, getContents().c_str());
434 fl_set_input(bibitem_form->label, getOptions().c_str());
435 if (bibitem_form->bibitem_form->visible) {
436 fl_raise_form(bibitem_form->bibitem_form);
438 fl_show_form(bibitem_form->bibitem_form,
439 FL_PLACE_MOUSE, FL_FULLBORDER,
445 bool InsetBibtex::addDatabase(string const & db)
447 if (!contains(contents, db.c_str())) {
448 if (!contents.empty())
457 bool InsetBibtex::delDatabase(string const & db)
459 if (contains(contents, db.c_str())) {
461 int n = tokenPos(contents, ',', bd);
463 // Weird code, would someone care to explain this?(Lgb)
466 contents = subst(contents, tmp.c_str(), ", ");
468 contents = split(contents, bd, ',');
476 // This function should be in LyXView when multiframe works ale970302
477 void BibitemUpdate(Combox * combox)
481 if (!current_view->available()) return;
483 string tmp, bibkeys = current_view->buffer()->getBibkeyList(',');
484 bibkeys = split(bibkeys, tmp,',');
485 while (!tmp.empty()) {
486 combox->addto(tmp.c_str());
487 bibkeys = split(bibkeys, tmp,',');
493 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
494 int bibitemMaxWidth(Painter & pain, LyXFont const & font)
497 // Does look like a hack? It is! (but will change at 0.13)
498 LyXParagraph * par = current_view->buffer()->paragraph;
502 int wx = par->bibkey->width(pain, 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();