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
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, 120);
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)
141 InsetCitation::~InsetCitation()
143 if(citation_form && citation_form->citation_form
144 && citation_form->citation_form->visible
145 && citation_form->citation_form->u_vdata == this)
146 fl_hide_form(citation_form->citation_form);
150 void InsetCitation::Edit(int, int)
152 if(current_view->buffer()->isReadonly())
155 if (!citation_form) {
156 citation_form = create_form_citation_form();
157 fl_set_form_atclose(citation_form->citation_form,
158 CancelCloseBoxCB, 0);
160 citation_form->citation_form->u_vdata = this;
162 BibitemUpdate(bibcombox);
163 if (!bibcombox->select_text(getContents().c_str()))
164 bibcombox->addline(getContents().c_str());
166 fl_set_input(citation_form->label, getOptions().c_str());
167 if (citation_form->citation_form->visible) {
168 fl_raise_form(citation_form->citation_form);
170 fl_show_form(citation_form->citation_form, FL_PLACE_MOUSE, FL_FULLBORDER,
176 string InsetCitation::getScreenLabel() const
182 if (!options.empty()) {
183 temp += ", " + options;
190 InsetBibKey::InsetBibKey(string const & key, string const & label):
191 InsetCommand("bibitem", key, label)
199 InsetBibKey::InsetBibKey(InsetBibKey const *b):
200 InsetCommand("bibitem", b->contents, b->options)
202 counter = b->counter;
206 InsetBibKey::~InsetBibKey()
208 if(bibitem_form && bibitem_form->bibitem_form
209 && bibitem_form->bibitem_form->visible)
210 fl_hide_form(bibitem_form->bibitem_form);
213 void InsetBibKey::setCounter(int c)
217 if (contents.empty())
218 contents += tostr(counter);
222 // I'm sorry but this is still necessary because \bibitem is used also
223 // as a LyX 2.x command, and lyxlex is not enough smart to understand
224 // real LaTeX commands. Yes, that could be fixed, but would be a waste
225 // of time cause LyX3 won't use lyxlex anyway. (ale)
226 void InsetBibKey::Write(FILE * file)
229 if (!options.empty()) {
235 fprintf(file, "\\bibitem %s\n", s.c_str());
239 string InsetBibKey::getScreenLabel() const
241 if (!options.empty())
244 return tostr(counter);
249 The value in "Key:" isn't allways set right after a few bibkey insets have
250 been added/removed. Perhaps the wrong object is deleted/used somewhere
254 void InsetBibKey::Edit(int, int)
256 if(current_view->buffer()->isReadonly())
260 bibitem_form = create_form_bibitem_form();
261 fl_set_form_atclose(bibitem_form->bibitem_form,
262 CancelCloseBoxCB, 0);
264 bibitem_form->bibitem_form->u_vdata = this;
265 // InsetBibtex uses the same form, with different labels
266 fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
267 fl_set_button_shortcut(bibitem_form->key, scex(_("Key:|#K")), 1);
268 fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
269 fl_set_button_shortcut(bibitem_form->label, scex(_("Label:|#L")), 1);
270 fl_set_input(bibitem_form->key, getContents().c_str());
271 fl_set_input(bibitem_form->label, getOptions().c_str());
272 if (bibitem_form->bibitem_form->visible) {
273 fl_raise_form(bibitem_form->bibitem_form);
275 fl_show_form(bibitem_form->bibitem_form, FL_PLACE_MOUSE,
277 _("Bibliography item"));
283 InsetBibtex::InsetBibtex(string const & dbase, string const & style,
285 :InsetCommand("BibTeX", dbase, style), owner(o)
290 string InsetBibtex::getScreenLabel() const
292 return _("BibTeX Generated References");
296 int InsetBibtex::Latex(FILE *file, signed char /*fragile*/)
299 signed char dummy = 0;
300 int result = Latex(bib, dummy);
301 fprintf(file, "%s", bib.c_str());
306 int InsetBibtex::Latex(string &file, signed char /*fragile*/)
308 // this looks like an horrible hack and it is :) The problem
309 // is that owner is not initialized correctly when the bib
310 // inset is cut and pasted. Such hacks will not be needed
313 owner = current_view->buffer();
315 // If we generate in a temp dir, we might need to give an
316 // absolute path there. This is a bit complicated since we can
317 // have a comma-separated list of bibliographies
318 string db_in, adb, db_out;
319 db_in = getContents();
320 db_in= split(db_in, adb, ',');
321 while(!adb.empty()) {
322 if (!owner->niceFile &&
323 IsFileReadable(MakeAbsPath(adb, owner->filepath)+".bib"))
324 adb = MakeAbsPath(adb, owner->filepath);
327 db_in= split(db_in, adb,',');
329 db_out = strip(db_out, ',');
333 && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
335 style = MakeAbsPath(getOptions(), owner->filepath);
337 style = getOptions();
339 file += "\\bibliographystyle{";
342 file += "\\bibliography{";
349 // This method returns a comma separated list of Bibtex entries
350 string InsetBibtex::getKeys()
352 // This hack is copied from InsetBibtex::Latex.
353 // Is it still needed? Probably yes.
355 owner = current_view->buffer();
359 string bibfiles = getContents();
360 bibfiles = split(bibfiles, tmp, ',');
361 while(!tmp.empty()) {
362 string fil = findtexfile(ChangeExtension(tmp, "bib", false),
364 lyxerr << "Bibfile: " << fil << endl;
365 // If we didn't find a matching file name just fail silently
367 // This is a _very_ simple parser for Bibtex database
368 // files. All it does is to look for lines starting
369 // in @ and not being @preamble and @string entries.
370 // It does NOT do any syntax checking!
371 ifstream ifs(fil.c_str());
373 while (getline(ifs, linebuf)) {
374 if (prefixIs(linebuf, "@")) {
375 linebuf = subst(linebuf, '{', '(');
376 linebuf = split(linebuf, tmp, '(');
377 tmp = lowercase(tmp);
378 if (!prefixIs(tmp, "@string")
379 && !prefixIs(tmp, "@preamble")) {
380 linebuf = split(linebuf,
390 // Get next file name
391 bibfiles = split(bibfiles, tmp, ',');
397 // BibTeX should have its own dialog. This is provisional.
398 void InsetBibtex::Edit(int, int)
401 bibitem_form = create_form_bibitem_form();
402 fl_set_form_atclose(bibitem_form->bibitem_form,
403 CancelCloseBoxCB, 0);
406 bibitem_form->bibitem_form->u_vdata = this;
407 fl_set_object_label(bibitem_form->key, _("Database:"));
408 fl_set_object_label(bibitem_form->label, _("Style: "));
409 fl_set_input(bibitem_form->key, getContents().c_str());
410 fl_set_input(bibitem_form->label, getOptions().c_str());
411 if (bibitem_form->bibitem_form->visible) {
412 fl_raise_form(bibitem_form->bibitem_form);
414 fl_show_form(bibitem_form->bibitem_form,
415 FL_PLACE_MOUSE, FL_FULLBORDER,
421 bool InsetBibtex::addDatabase(string const & db)
423 if (!contains(contents, db.c_str())) {
424 if (!contents.empty())
433 bool InsetBibtex::delDatabase(string const & db)
435 if (contains(contents, db.c_str())) {
437 int n = tokenPos(contents, ',', bd);
439 // Weird code, would someone care to explain this?(Lgb)
442 contents = subst(contents, tmp.c_str(), ", ");
444 contents = split(contents, bd, ',');
452 // This function should be in LyXView when multiframe works ale970302
453 void BibitemUpdate(Combox * combox)
457 if (!current_view->available())
460 string tmp, bibkeys = current_view->buffer()->getBibkeyList(',');
461 bibkeys = split(bibkeys, tmp,',');
462 while (!tmp.empty()) {
463 combox->addto(tmp.c_str());
464 bibkeys = split(bibkeys, tmp,',');
470 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
471 int bibitemMaxWidth(LyXFont const & font)
474 // Does look like a hack? It is! (but will change at 0.13)
475 LyXParagraph * par = current_view->buffer()->paragraph;
479 int wx = par->bibkey->Width(font);
489 string bibitemWidthest()
492 // Does look like a hack? It is! (but will change at 0.13)
493 LyXParagraph * par = current_view->buffer()->paragraph;
494 InsetBibKey * bkey = 0;
499 int wx = par->bibkey->Width(font);
508 if (bkey && !bkey->getScreenLabel().empty())
509 return bkey->getScreenLabel();