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