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