]> git.lyx.org Git - lyx.git/blob - src/insets/insetbib.C
1f1dcfbf9a3141a55fe0003bd7d51edb37a47557
[lyx.git] / src / insets / insetbib.C
1 #include <config.h>
2
3 #include <fstream>
4 #include <cstdlib>
5
6 #ifdef __GNUG__
7 #pragma implementation
8 #endif
9
10 #include FORMS_H_LOCATION  
11 #include "insetbib.h"
12 #include "buffer.h"
13 #include "debug.h"
14 #include "lyx_gui_misc.h"
15 #include "BufferView.h"
16 #include "gettext.h"
17 #include "bibforms.h"
18 #include "lyxtext.h"
19 #include "support/filetools.h"
20 #include "support/path.h"
21
22 using std::ostream;
23 using std::ifstream;
24 using std::getline;
25 using std::endl;
26 using std::vector;
27 using std::pair;
28
29 extern BufferView * current_view;
30
31 FD_bibitem_form * bibitem_form = 0;
32
33 FD_bibitem_form * create_form_bibitem_form(void);
34
35
36 extern "C"
37 void bibitem_cb(FL_OBJECT *, long data)
38 {
39         InsetBibKey::Holder * holder =
40                 static_cast<InsetBibKey::Holder*>
41                 (bibitem_form->bibitem_form->u_vdata);
42         
43         holder->inset->callback( bibitem_form, data );
44 }
45
46
47 FD_bibitem_form * create_form_bibitem_form(void)
48 {
49         FL_OBJECT * obj;
50         FD_bibitem_form * fdui = (FD_bibitem_form *) fl_calloc(1, sizeof(FD_bibitem_form));
51
52         fdui->bibitem_form = fl_bgn_form(FL_NO_BOX, 220, 130);
53         obj = fl_add_box(FL_UP_BOX, 0, 0, 220, 130, "");
54         fdui->key = obj = fl_add_input(FL_NORMAL_INPUT, 80, 10, 130, 30, idex(_("Key:|#K")));
55         fl_set_input_shortcut(obj, scex(_("Key:|#K")), 1);
56         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
57         obj = fl_add_button(FL_RETURN_BUTTON, 20, 90, 90, 30, _("OK"));
58         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
59         fl_set_object_callback(obj, bibitem_cb, 1);
60         obj = fl_add_button(FL_NORMAL_BUTTON, 120, 90, 90, 30, idex(_("Cancel|^[")));
61         fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
62         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
63         fl_set_object_callback(obj, bibitem_cb, 0);
64         fdui->label = obj = fl_add_input(FL_NORMAL_INPUT, 80, 50, 130, 30, idex(_("Label:|#L")));
65         fl_set_input_shortcut(obj, scex(_("Label:|#L")), 1);
66         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
67         fl_end_form();
68
69         //fdui->bibitem_form->fdui = fdui;
70
71         return fdui;
72 }
73
74
75 InsetBibKey::InsetBibKey(InsetCommandParams const & p)
76         : InsetCommand(p)
77 {
78         counter = 1;
79 }
80
81
82 InsetBibKey::~InsetBibKey()
83 {
84         if(bibitem_form && bibitem_form->bibitem_form
85            && bibitem_form->bibitem_form->visible
86            && bibitem_form->bibitem_form->u_vdata == &holder)
87                 fl_hide_form(bibitem_form->bibitem_form);
88 }
89
90
91 Inset * InsetBibKey::Clone() const
92 {
93         InsetBibKey * b = new InsetBibKey(params());
94         b->setCounter(counter);
95         return b;
96 }
97
98
99 void InsetBibKey::callback( FD_bibitem_form * form, long data )
100 {
101         switch (data) {
102         case 1:
103                 if(!holder.view->buffer()->isReadonly()) {
104                         setContents(fl_get_input(form->key));
105                         setOptions(fl_get_input(form->label));
106                         // shouldn't mark the buffer dirty unless
107                         // something was actually altered
108                         holder.view->updateInset( this, true );
109                 } // fall through to Cancel
110         case 0:
111                 fl_hide_form(form->bibitem_form);
112                 break;
113         }
114 }
115
116
117 void InsetBibKey::setCounter(int c) 
118
119         counter = c; 
120     
121         if (getCmdName().empty())
122                 setCmdName( tostr(counter) );
123 }
124
125
126 // I'm sorry but this is still necessary because \bibitem is used also
127 // as a LyX 2.x command, and lyxlex is not enough smart to understand
128 // real LaTeX commands. Yes, that could be fixed, but would be a waste 
129 // of time cause LyX3 won't use lyxlex anyway.  (ale)
130 void InsetBibKey::Write(Buffer const *, ostream & os) const
131 {
132         os << "\\bibitem ";
133         if (! getOptions().empty()) {
134                 os << '['
135                    << getOptions() << ']';
136         }
137         os << '{'
138            << getContents() << "}\n";
139 }
140
141
142 // This is necessary here because this is written without begin_inset
143 // This should be changed!!! (Jug)
144 void InsetBibKey::Read(Buffer const *, LyXLex & lex)
145 {    
146         string token;
147
148         if (lex.EatLine()) {
149                 token = lex.GetString();
150                 scanCommand(token);
151         } else
152                 lex.printError("InsetCommand: Parse error: `$$Token'");
153 }
154
155
156 string InsetBibKey::getScreenLabel() const
157 {
158         if (! getOptions().empty())
159                 return getOptions();
160     
161         return tostr(counter);
162 }
163
164
165 /**
166   The value in "Key:" isn't allways set right after a few bibkey insets have
167   been added/removed.  Perhaps the wrong object is deleted/used somewhere
168   upwards?
169   (Joacim 1998-03-04)
170 */
171 void InsetBibKey::Edit(BufferView * bv, int, int, unsigned int)
172 {
173         if(bv->buffer()->isReadonly())
174                 WarnReadonly(bv->buffer()->fileName());
175         
176         if (!bibitem_form) {
177                 bibitem_form = create_form_bibitem_form();
178                 fl_set_form_atclose(bibitem_form->bibitem_form, 
179                                     CancelCloseBoxCB, 0);
180         }
181
182         holder.inset = this;
183         holder.view = bv;
184         
185         bibitem_form->bibitem_form->u_vdata = &holder;
186         // InsetBibtex uses the same form, with different labels
187         fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
188         fl_set_button_shortcut(bibitem_form->key, scex(_("Key:|#K")), 1);
189         fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
190         fl_set_button_shortcut(bibitem_form->label, scex(_("Label:|#L")), 1);
191         fl_set_input(bibitem_form->key, getContents().c_str());
192         fl_set_input(bibitem_form->label, getOptions().c_str());
193         if (bibitem_form->bibitem_form->visible) {
194                 fl_raise_form(bibitem_form->bibitem_form);
195         } else {
196                 fl_show_form(bibitem_form->bibitem_form, FL_PLACE_MOUSE,
197                              FL_FULLBORDER,
198                              _("Bibliography item"));
199         }   
200 }
201
202
203 InsetBibtex::InsetBibtex(InsetCommandParams const & p, Buffer * o)
204         : InsetCommand(p), owner(o)
205 {}
206
207
208 InsetBibtex::~InsetBibtex()
209 {
210         if(bibitem_form && bibitem_form->bibitem_form
211            && bibitem_form->bibitem_form->visible
212            && bibitem_form->bibitem_form->u_vdata == &holder)
213                 fl_hide_form(bibitem_form->bibitem_form);
214 }
215
216
217 string InsetBibtex::getScreenLabel() const
218 {
219         return _("BibTeX Generated References");
220 }
221
222
223 int InsetBibtex::Latex(Buffer const *, ostream & os,
224                        bool /*fragile*/, bool/*fs*/) const
225 {
226         // this looks like an horrible hack and it is :) The problem
227         // is that owner is not initialized correctly when the bib
228         // inset is cut and pasted. Such hacks will not be needed
229         // later (JMarc)
230         if (!owner) {
231                 owner = current_view->buffer();
232         }
233         // If we generate in a temp dir, we might need to give an
234         // absolute path there. This is a bit complicated since we can
235         // have a comma-separated list of bibliographies
236         string adb, db_out;
237         string db_in = getContents();
238         db_in = split(db_in, adb, ',');
239         while(!adb.empty()) {
240                 if (!owner->niceFile &&
241                     IsFileReadable(MakeAbsPath(adb, owner->filepath)+".bib")) 
242                          adb = MakeAbsPath(adb, owner->filepath);
243
244                 db_out += adb;
245                 db_out += ',';
246                 db_in= split(db_in, adb,',');
247         }
248         db_out = strip(db_out, ',');
249         // Idem, but simpler
250         string style;
251         if (!owner->niceFile 
252             && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
253                               + ".bst")) 
254                 style = MakeAbsPath(getOptions(), owner->filepath);
255         else
256                 style = getOptions();
257
258         os << "\\bibliographystyle{" << style << "}\n"
259            << "\\bibliography{" << db_out << "}\n";
260         return 2;
261 }
262
263
264 // This method returns a comma separated list of Bibtex entries
265 vector<pair<string,string> > InsetBibtex::getKeys() const
266 {
267         // This hack is copied from InsetBibtex::Latex.
268         // Is it still needed? Probably yes.
269         // Why is this needed here when it already is in Latex?
270         // Anyway we need a different way to get to the
271         // buffer the inset is in. (Lgb)
272         
273         //if (!owner) {
274         //      owner = current_view->buffer();
275         //}
276         
277         Path p(owner->filepath);
278
279         vector<pair<string,string> > keys;
280         string tmp;
281         string bibfiles = getContents();
282         bibfiles = split(bibfiles, tmp, ',');
283         while(!tmp.empty()) {
284                 string fil = findtexfile(ChangeExtension(tmp, "bib"),
285                                          "bib");
286                 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
287                 // If we didn't find a matching file name just fail silently
288                 if (!fil.empty()) {
289                         // This is a _very_ simple parser for Bibtex database
290                         // files. All it does is to look for lines starting
291                         // in @ and not being @preamble and @string entries.
292                         // It does NOT do any syntax checking!
293                         ifstream ifs(fil.c_str());
294                         string linebuf0;
295                         while (getline(ifs, linebuf0)) {
296                                 string linebuf = frontStrip(strip(linebuf0));
297                                 if( linebuf.empty() ) continue;
298                                 if (prefixIs(linebuf, "@")) {
299                                         linebuf = subst(linebuf, '{', '(');
300                                         linebuf = split(linebuf, tmp, '(');
301                                         tmp = lowercase(tmp);
302                                         if (!prefixIs(tmp, "@string")
303                                             && !prefixIs(tmp, "@preamble")) {
304                                                 linebuf = split(linebuf, tmp, ',');
305                                                 tmp = frontStrip(tmp);
306                                                 if (!tmp.empty()) {
307                                                         keys.push_back(pair<string,string>(tmp,string()));
308                                                 }
309                                         }
310                                 } else if( !keys.empty() ) {
311                                         keys.back().second += linebuf + "\n";
312                                 }
313                         }
314                 }
315                 // Get next file name
316                 bibfiles = split(bibfiles, tmp, ',');
317         }
318         return keys;
319 }
320
321
322 // BibTeX should have its own dialog. This is provisional.
323 void InsetBibtex::Edit(BufferView * bv, int, int, unsigned int)
324 {
325         if (!bibitem_form) {
326                 bibitem_form = create_form_bibitem_form();
327                 fl_set_form_atclose(bibitem_form->bibitem_form, 
328                                     CancelCloseBoxCB, 0);
329         }
330
331         holder.inset = this;
332         holder.view = bv;
333         bibitem_form->bibitem_form->u_vdata = &holder;
334
335         fl_set_object_label(bibitem_form->key, _("Database:"));
336         fl_set_object_label(bibitem_form->label, _("Style:  "));
337         fl_set_input(bibitem_form->key, getContents().c_str());
338         fl_set_input(bibitem_form->label, getOptions().c_str());
339         if (bibitem_form->bibitem_form->visible) {
340                 fl_raise_form(bibitem_form->bibitem_form);
341         } else {
342                 fl_show_form(bibitem_form->bibitem_form,
343                              FL_PLACE_MOUSE, FL_FULLBORDER,
344                              _("BibTeX"));
345         }   
346 }
347
348
349 bool InsetBibtex::addDatabase(string const & db)
350 {
351         string contents(getContents());
352         if (!contains(contents, db.c_str())) {
353                 if (!contents.empty()) 
354                         contents += ",";
355                 setContents(contents + db);
356                 return true;
357         }
358         return false;
359 }
360
361
362 bool InsetBibtex::delDatabase(string const & db)
363 {
364         if (contains(getContents(), db.c_str())) {
365                 string bd = db;
366                 int n = tokenPos(getContents(), ',', bd);
367                 if (n > 0) {
368                         // Weird code, would someone care to explain this?(Lgb)
369                         string tmp(", ");
370                         tmp += bd;
371                         setContents(subst(getContents(), tmp.c_str(), ", "));
372                 } else if (n == 0)
373                         setContents(split(getContents(), bd, ','));
374                 else 
375                         return false;
376         }
377         return true;
378 }
379
380
381 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
382 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
383 {
384         int w = 0;
385         // Does look like a hack? It is! (but will change at 0.13)
386         LyXParagraph * par = current_view->buffer()->paragraph;
387     
388         while (par) {
389                 if (par->bibkey) {
390                         int wx = par->bibkey->width(bv, font);
391                         if (wx > w) w = wx;
392                 }
393                 par = par->next;
394         }
395         return w;
396 }
397
398
399 // ale070405
400 string bibitemWidest(BufferView * bv)
401 {
402         int w = 0;
403         // Does look like a hack? It is! (but will change at 0.13)
404         LyXParagraph * par = current_view->buffer()->paragraph;
405         InsetBibKey * bkey = 0;
406         LyXFont font;
407       
408         while (par) {
409                 if (par->bibkey) {
410                         int wx = par->bibkey->width(bv, font);
411                         if (wx > w) {
412                                 w = wx;
413                                 bkey = par->bibkey;
414                         }
415                 }
416                 par = par->next;
417         }
418     
419         if (bkey && !bkey->getScreenLabel().empty())
420                 return bkey->getScreenLabel();
421     
422         return "99";
423 }