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