]> git.lyx.org Git - lyx.git/blob - src/insets/insetbib.C
61f71bcb4ad48df8e842e6e0ffeb2ec96258e5a1
[lyx.git] / src / insets / insetbib.C
1 #include <config.h>
2
3 #include <fstream>
4 using std::ifstream;
5
6 #include <cstdlib>
7
8 #ifdef __GNUG__
9 #pragma implementation
10 #endif
11
12 #include FORMS_H_LOCATION  
13 #include "insetbib.h"
14 #include "combox.h"
15 #include "buffer.h"
16 #include "debug.h"
17 #include "lyx_gui_misc.h"
18 #include "BufferView.h"
19 #include "gettext.h"
20 #include "bibforms.h"
21 #include "lyxtext.h"
22 #include "support/filetools.h"
23
24 extern BufferView * current_view;
25
26 FD_citation_form * citation_form = 0;
27 FD_bibitem_form * bibitem_form = 0;
28 static Combox * bibcombox = 0;
29
30 void BibitemUpdate(Combox *);
31 FD_citation_form * create_form_citation_form(void);
32 FD_bibitem_form * create_form_bibitem_form(void);
33
34
35 extern "C" void bibitem_cb(FL_OBJECT *, long data)
36 {
37         switch (data) {
38         case 1: // OK, citation
39         {
40                 InsetCitation::Holder * holder =
41                         static_cast<InsetCitation::Holder*>
42                         (citation_form->citation_form->u_vdata);
43                 if(!holder->view->buffer()->isReadonly()) {
44                         
45                         InsetCitation * inset = holder->inset;
46                         inset->setContents(bibcombox->getline());
47                         inset->setOptions(fl_get_input(citation_form->label));
48                         fl_hide_form(citation_form->citation_form);
49                         // shouldn't mark the buffer dirty unless something
50                         // was actually altered
51                         holder->view->updateInset(inset, true);
52                         break;
53                 }
54                 // fall through to Cancel on RO-mode
55         }       
56         case 0: fl_hide_form(citation_form->citation_form);
57                 break;
58         case 3: // OK, bibitem
59         {
60                 InsetBibKey::Holder * holder =
61                         static_cast<InsetBibKey::Holder*>
62                         (bibitem_form->bibitem_form->u_vdata);
63                 
64                 if(!holder->view->buffer()->isReadonly()) {
65                         InsetBibKey * inset = holder->inset;
66                         inset->setContents(fl_get_input(bibitem_form->key));
67                         inset->setOptions(fl_get_input(bibitem_form->label));
68                         fl_hide_form(bibitem_form->bibitem_form);
69                         // Does look like a hack? It is! (but will change at 0.13)
70                         holder->view->text->RedoParagraph();
71                         holder->view->update(1);
72                         break;
73                 } // fall through to Cancel on RO-mode
74         }
75         case 2: // Cancel, bibitem
76                 fl_hide_form(bibitem_form->bibitem_form); // Cancel, bibitem
77                 break;
78         }
79 }
80
81
82 FD_citation_form * create_form_citation_form(void)
83 {
84         FL_OBJECT * obj;
85         FD_citation_form * fdui = (FD_citation_form *) fl_calloc(1, sizeof(FD_citation_form));
86
87         fdui->citation_form = fl_bgn_form(FL_NO_BOX, 220, 130);
88         obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
89         fdui->key = obj = fl_add_text(FL_NORMAL_TEXT, 20, 10, 60, 30, _("Key:"));
90         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
91         fl_set_object_lalign(obj, FL_ALIGN_RIGHT);
92
93         bibcombox = new Combox(FL_COMBOX_INPUT);
94         bibcombox->add(80, 10, 130, 30, 300);
95
96         obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
97         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
98         fl_set_object_callback(obj, bibitem_cb, 1);
99         obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
100         fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
101         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
102         fl_set_object_callback(obj, bibitem_cb, 0);
103         fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Remark:|#R")));
104         fl_set_input_shortcut(obj, scex(_("Remark:|#R")), 1);
105         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
106         fl_end_form();
107
108         //fdui->citation_form->fdui = fdui;
109
110         return fdui;
111 }
112 /*---------------------------------------*/
113
114 FD_bibitem_form * create_form_bibitem_form(void)
115 {
116         FL_OBJECT * obj;
117         FD_bibitem_form * fdui = (FD_bibitem_form *) fl_calloc(1, sizeof(FD_bibitem_form));
118
119         fdui->bibitem_form = fl_bgn_form(FL_NO_BOX, 220, 130);
120         obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
121         fdui->key = obj = fl_add_input(FL_NORMAL_INPUT, 80, 10, 130, 30, idex(_("Key:|#K")));
122         fl_set_input_shortcut(obj, scex(_("Key:|#K")), 1);
123         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
124         obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
125         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
126         fl_set_object_callback(obj, bibitem_cb, 3);
127         obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
128         fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
129         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
130         fl_set_object_callback(obj, bibitem_cb, 2);
131         fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Label:|#L")));
132         fl_set_input_shortcut(obj, scex(_("Label:|#L")), 1);
133         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
134         fl_end_form();
135
136         //fdui->bibitem_form->fdui = fdui;
137
138         return fdui;
139 }
140 /*---------------------------------------*/
141
142
143 InsetCitation::InsetCitation(string const & key, string const & note)
144         : InsetCommand("cite", key, note)
145 {
146
147 }
148
149
150 InsetCitation::~InsetCitation()
151 {
152         if(citation_form && citation_form->citation_form
153            && citation_form->citation_form->visible
154            && citation_form->citation_form->u_vdata == &holder)
155                 fl_hide_form(citation_form->citation_form);
156 }
157
158
159 void InsetCitation::Edit(BufferView * bv, int, int, unsigned int)
160 {
161         if(bv->buffer()->isReadonly())
162                 WarnReadonly(bv->buffer()->fileName());
163
164         if (!citation_form) {
165                 citation_form = create_form_citation_form();
166                 fl_set_form_atclose(citation_form->citation_form, 
167                                     CancelCloseBoxCB, 0);
168         }
169
170         holder.inset = this;
171         holder.view = bv;
172                 
173         citation_form->citation_form->u_vdata = &holder;
174
175         BibitemUpdate(bibcombox);
176         if (!bibcombox->select_text(getContents().c_str()))
177                 bibcombox->addline(getContents().c_str());
178             
179         fl_set_input(citation_form->label, getOptions().c_str());
180         if (citation_form->citation_form->visible) {
181                 fl_raise_form(citation_form->citation_form);
182         } else {
183                 fl_show_form(citation_form->citation_form,
184                              FL_PLACE_MOUSE, FL_FULLBORDER,
185                              _("Citation"));
186         }   
187 }
188
189
190 string InsetCitation::getScreenLabel() const
191 {
192         string temp("[");
193
194         temp += contents;
195
196         if (!options.empty()) {
197                 temp += ", " + options;
198         }
199
200         return temp + ']';
201 }
202
203
204 InsetBibKey::InsetBibKey(string const & key, string const & label):
205         InsetCommand("bibitem", key, label)
206 {
207         counter = 1;
208         if (key.empty())
209                 contents = ' ';
210 }
211
212
213 InsetBibKey::InsetBibKey(InsetBibKey const * b):
214         InsetCommand("bibitem", b->contents, b->options)
215 {
216         counter = b->counter;
217 }
218
219
220 InsetBibKey::~InsetBibKey()
221 {
222         if(bibitem_form && bibitem_form->bibitem_form
223            && bibitem_form->bibitem_form->visible)
224                 fl_hide_form(bibitem_form->bibitem_form);
225 }
226
227 void InsetBibKey::setCounter(int c) 
228
229         counter = c; 
230     
231         if (contents.empty())
232                 contents += tostr(counter);
233 }
234
235
236 // I'm sorry but this is still necessary because \bibitem is used also
237 // as a LyX 2.x command, and lyxlex is not enough smart to understand
238 // real LaTeX commands. Yes, that could be fixed, but would be a waste 
239 // of time cause LyX3 won't use lyxlex anyway.  (ale)
240 void InsetBibKey::Write(ostream & os) const
241 {
242         string s;
243         if (!options.empty()) {
244                 s += '[';
245                 s += options + ']';
246         }
247         s += '{';
248         s += contents + '}';
249         os << "\\bibitem " << s << "\n";
250 }
251
252
253 string InsetBibKey::getScreenLabel() const
254 {
255         if (!options.empty())
256                 return options;
257     
258         return tostr(counter);
259 }
260
261
262 /*
263   The value in "Key:" isn't allways set right after a few bibkey insets have
264   been added/removed.  Perhaps the wrong object is deleted/used somewhere
265   upwards?
266   (Joacim 1998-03-04)
267 */
268 void InsetBibKey::Edit(BufferView * bv, int, int, unsigned int)
269 {
270         if(bv->buffer()->isReadonly())
271                 WarnReadonly(bv->buffer()->fileName());
272         
273         if (!bibitem_form) {
274                 bibitem_form = create_form_bibitem_form();
275                 fl_set_form_atclose(bibitem_form->bibitem_form, 
276                                     CancelCloseBoxCB, 0);
277         }
278
279         holder.inset = this;
280         holder.view = bv;
281         
282         bibitem_form->bibitem_form->u_vdata = &holder;
283         // InsetBibtex uses the same form, with different labels
284         fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
285         fl_set_button_shortcut(bibitem_form->key, scex(_("Key:|#K")), 1);
286         fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
287         fl_set_button_shortcut(bibitem_form->label, scex(_("Label:|#L")), 1);
288         fl_set_input(bibitem_form->key, getContents().c_str());
289         fl_set_input(bibitem_form->label, getOptions().c_str());
290         if (bibitem_form->bibitem_form->visible) {
291                 fl_raise_form(bibitem_form->bibitem_form);
292         } else {
293                 fl_show_form(bibitem_form->bibitem_form, FL_PLACE_MOUSE,
294                              FL_FULLBORDER,
295                              _("Bibliography item"));
296         }   
297 }
298
299
300
301 InsetBibtex::InsetBibtex(string const & dbase, string const & style,
302                          Buffer * o)
303         : InsetCommand("BibTeX", dbase, style), owner(o)
304 {
305 }
306
307
308 string InsetBibtex::getScreenLabel() const
309 {
310         return _("BibTeX Generated References");
311 }
312
313
314 int InsetBibtex::Latex(ostream & os, signed char /*fragile*/) const
315 {
316 #ifdef USE_OSTREAM_ONLY
317         // this looks like an horrible hack and it is :) The problem
318         // is that owner is not initialized correctly when the bib
319         // inset is cut and pasted. Such hacks will not be needed
320         // later (JMarc)
321         if (!owner) {
322                 owner = current_view->buffer();
323         }
324         // If we generate in a temp dir, we might need to give an
325         // absolute path there. This is a bit complicated since we can
326         // have a comma-separated list of bibliographies
327         string adb, db_out;
328         string db_in = getContents();
329         db_in = split(db_in, adb, ',');
330         while(!adb.empty()) {
331                 if (!owner->niceFile &&
332                     IsFileReadable(MakeAbsPath(adb, owner->filepath)+".bib")) 
333                         adb = MakeAbsPath(adb, owner->filepath);
334                 db_out += adb;
335                 db_out += ',';
336                 db_in= split(db_in, adb,',');
337         }
338         db_out = strip(db_out, ',');
339         // Idem, but simpler
340         string style;
341         if (!owner->niceFile 
342             && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
343                               + ".bst")) 
344                 style = MakeAbsPath(getOptions(), owner->filepath);
345         else
346                 style = getOptions();
347
348         os << "\\bibliographystyle{" << style << "}\n"
349            << "\\bibliography{" << db_out << "}\n";
350         return 2;
351 #else
352         string bib;
353         signed char dummy = 0;
354         int result = Latex(bib, dummy);
355         os << bib;
356         return result;
357 #endif
358 }
359
360
361 #ifndef USE_OSTREAM_ONLY
362 int InsetBibtex::Latex(string & file, signed char /*fragile*/) const
363 {
364         // this looks like an horrible hack and it is :) The problem
365         // is that owner is not initialized correctly when the bib
366         // inset is cut and pasted. Such hacks will not be needed
367         // later (JMarc)
368         if (!owner) {
369                 owner = current_view->buffer();
370         }
371         // If we generate in a temp dir, we might need to give an
372         // absolute path there. This is a bit complicated since we can
373         // have a comma-separated list of bibliographies
374         string adb, db_out;
375         string db_in = getContents();
376         db_in = split(db_in, adb, ',');
377         while(!adb.empty()) {
378                 if (!owner->niceFile &&
379                     IsFileReadable(MakeAbsPath(adb, owner->filepath)+".bib")) 
380                         adb = MakeAbsPath(adb, owner->filepath);
381                 db_out += adb;
382                 db_out += ',';
383                 db_in= split(db_in, adb,',');
384         }
385         db_out = strip(db_out, ',');
386         // Idem, but simpler
387         string style;
388         if (!owner->niceFile 
389             && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
390                               + ".bst")) 
391                 style = MakeAbsPath(getOptions(), owner->filepath);
392         else
393                 style = getOptions();
394
395         file += "\\bibliographystyle{";
396         file += style;
397         file += "}\n";
398         file += "\\bibliography{";
399         file += db_out;
400         file += "}\n";
401         return 2;
402 }
403 #endif
404
405 // This method returns a comma separated list of Bibtex entries
406 string InsetBibtex::getKeys(char delim)
407 {
408         // This hack is copied from InsetBibtex::Latex.
409         // Is it still needed? Probably yes.
410         // Why is this needed here when it already is in Latex?
411         // Anyway we need a different way to get to the
412         // buffer the inset is in. (Lgb)
413         
414         //if (!owner) {
415         //      owner = current_view->buffer();
416         //}
417         
418         string tmp, keys;
419         string bibfiles = getContents();
420         bibfiles = split(bibfiles, tmp, ',');
421         while(!tmp.empty()) {
422                 string fil = findtexfile(ChangeExtension(tmp, "bib", false),
423                                          "bib");
424                 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
425                 // If we didn't find a matching file name just fail silently
426                 if (!fil.empty()) {
427                         // This is a _very_ simple parser for Bibtex database
428                         // files. All it does is to look for lines starting
429                         // in @ and not being @preamble and @string entries.
430                         // It does NOT do any syntax checking!
431                         ifstream ifs(fil.c_str());
432                         string linebuf;
433                         while (getline(ifs, linebuf)) {
434                                 linebuf = frontStrip(linebuf);
435                                 if (prefixIs(linebuf, "@")) {
436                                         linebuf = subst(linebuf, '{', '(');
437                                         linebuf = split(linebuf, tmp, '(');
438                                         tmp = lowercase(tmp);
439                                         if (!prefixIs(tmp, "@string")
440                                             && !prefixIs(tmp, "@preamble")) {
441                                                 linebuf = split(linebuf,
442                                                                 tmp, ',');
443                                                 tmp = frontStrip(strip(tmp));
444                                                 if (!tmp.empty()) {
445                                                         keys += tmp;
446                                                         keys += delim;
447                                                 }
448                                         }
449                                 }
450                         }
451                 }
452                 // Get next file name
453                 bibfiles = split(bibfiles, tmp, ',');
454         }
455         return keys;
456 }
457
458
459 // BibTeX should have its own dialog. This is provisional.
460 void InsetBibtex::Edit(BufferView *, int, int, unsigned int)
461 {
462         if (!bibitem_form) {
463                 bibitem_form = create_form_bibitem_form();
464                 fl_set_form_atclose(bibitem_form->bibitem_form, 
465                                     CancelCloseBoxCB, 0);
466         }
467
468         bibitem_form->bibitem_form->u_vdata = this;
469         fl_set_object_label(bibitem_form->key, _("Database:"));
470         fl_set_object_label(bibitem_form->label, _("Style:  "));
471         fl_set_input(bibitem_form->key, getContents().c_str());
472         fl_set_input(bibitem_form->label, getOptions().c_str());
473         if (bibitem_form->bibitem_form->visible) {
474                 fl_raise_form(bibitem_form->bibitem_form);
475         } else {
476                 fl_show_form(bibitem_form->bibitem_form,
477                              FL_PLACE_MOUSE, FL_FULLBORDER,
478                              _("BibTeX"));
479         }   
480 }
481
482
483 bool InsetBibtex::addDatabase(string const & db)
484 {
485         if (!contains(contents, db.c_str())) {
486                 if (!contents.empty()) 
487                         contents += ',';
488                 contents += db;
489                 return true;
490         }
491         return false;
492 }
493
494
495 bool InsetBibtex::delDatabase(string const & db)
496 {
497         if (contains(contents, db.c_str())) {
498                 string bd = db;
499                 int n = tokenPos(contents, ',', bd);
500                 if (n > 0) {
501                         // Weird code, would someone care to explain this?(Lgb)
502                         string tmp(", ");
503                         tmp += bd;
504                         contents = subst(contents, tmp.c_str(), ", ");
505                 } else if (n == 0)
506                         contents = split(contents, bd, ',');
507                 else 
508                         return false;
509         }
510         return true;
511 }
512
513
514 // This function should be in LyXView when multiframe works ale970302
515 void BibitemUpdate(Combox * combox)
516 {
517         combox->clear();
518         
519         if (!current_view->available()) return;
520         
521         string tmp, bibkeys = current_view->buffer()->getBibkeyList(',');
522         bibkeys = split(bibkeys, tmp,',');
523         while (!tmp.empty()) {
524                 combox->addto(tmp.c_str());
525                 bibkeys = split(bibkeys, tmp,',');
526         }
527 }
528
529
530
531 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
532 int bibitemMaxWidth(Painter & pain, LyXFont const & font)
533 {
534         int w = 0;
535         // Does look like a hack? It is! (but will change at 0.13)
536         LyXParagraph * par = current_view->buffer()->paragraph;
537     
538         while (par) {
539                 if (par->bibkey) {
540                         int wx = par->bibkey->width(pain, font);
541                         if (wx > w) w = wx;
542                 }
543                 par = par->next;
544         }
545         return w;
546 }
547
548
549 // ale070405
550 string bibitemWidthest(Painter & pain)
551 {
552         int w = 0;
553         // Does look like a hack? It is! (but will change at 0.13)
554         LyXParagraph * par = current_view->buffer()->paragraph;
555         InsetBibKey * bkey = 0;
556         LyXFont font;
557       
558         while (par) {
559                 if (par->bibkey) {
560                         int wx = par->bibkey->width(pain, font);
561                         if (wx > w) {
562                                 w = wx;
563                                 bkey = par->bibkey;
564                         }
565                 }
566                 par = par->next;
567         }
568     
569         if (bkey && !bkey->getScreenLabel().empty())
570                 return bkey->getScreenLabel();
571     
572         return "99";
573 }