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