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