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