]> git.lyx.org Git - lyx.git/blob - src/insets/insetbib.C
clear()->erase() ; lots of using directives for cxx
[lyx.git] / src / insets / insetbib.C
1 #include <config.h>
2
3 #include <fstream>
4 using std::ifstream;
5 using std::getline;
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 using std::ostream;
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
208 InsetBibKey::InsetBibKey(string const & key, string const & label):
209         InsetCommand("bibitem", key, label)
210 {
211         counter = 1;
212         if (key.empty())
213                 contents = ' ';
214 }
215
216
217 InsetBibKey::InsetBibKey(InsetBibKey const * b):
218         InsetCommand("bibitem", b->contents, b->options)
219 {
220         counter = b->counter;
221 }
222
223
224 InsetBibKey::~InsetBibKey()
225 {
226         if(bibitem_form && bibitem_form->bibitem_form
227            && bibitem_form->bibitem_form->visible
228            && bibitem_form->bibitem_form->u_vdata == &holder)
229                 fl_hide_form(bibitem_form->bibitem_form);
230 }
231
232 void InsetBibKey::setCounter(int c) 
233
234         counter = c; 
235     
236         if (contents.empty())
237                 contents += tostr(counter);
238 }
239
240
241 // I'm sorry but this is still necessary because \bibitem is used also
242 // as a LyX 2.x command, and lyxlex is not enough smart to understand
243 // real LaTeX commands. Yes, that could be fixed, but would be a waste 
244 // of time cause LyX3 won't use lyxlex anyway.  (ale)
245 void InsetBibKey::Write(ostream & os) const
246 {
247         os << "\\bibitem ";
248         if (!options.empty()) {
249                 os << '['
250                    << options << ']';
251         }
252         os << '{'
253            << contents << "}\n";
254 }
255
256
257 string InsetBibKey::getScreenLabel() const
258 {
259         if (!options.empty())
260                 return options;
261     
262         return tostr(counter);
263 }
264
265
266 /*
267   The value in "Key:" isn't allways set right after a few bibkey insets have
268   been added/removed.  Perhaps the wrong object is deleted/used somewhere
269   upwards?
270   (Joacim 1998-03-04)
271 */
272 void InsetBibKey::Edit(BufferView * bv, int, int, unsigned int)
273 {
274         if(bv->buffer()->isReadonly())
275                 WarnReadonly(bv->buffer()->fileName());
276         
277         if (!bibitem_form) {
278                 bibitem_form = create_form_bibitem_form();
279                 fl_set_form_atclose(bibitem_form->bibitem_form, 
280                                     CancelCloseBoxCB, 0);
281         }
282
283         holder.inset = this;
284         holder.view = bv;
285         
286         bibitem_form->bibitem_form->u_vdata = &holder;
287         // InsetBibtex uses the same form, with different labels
288         fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
289         fl_set_button_shortcut(bibitem_form->key, scex(_("Key:|#K")), 1);
290         fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
291         fl_set_button_shortcut(bibitem_form->label, scex(_("Label:|#L")), 1);
292         fl_set_input(bibitem_form->key, getContents().c_str());
293         fl_set_input(bibitem_form->label, getOptions().c_str());
294         if (bibitem_form->bibitem_form->visible) {
295                 fl_raise_form(bibitem_form->bibitem_form);
296         } else {
297                 fl_show_form(bibitem_form->bibitem_form, FL_PLACE_MOUSE,
298                              FL_FULLBORDER,
299                              _("Bibliography item"));
300         }   
301 }
302
303
304
305 InsetBibtex::InsetBibtex(string const & dbase, string const & style,
306                          Buffer * o)
307         : InsetCommand("BibTeX", dbase, style), owner(o)
308 {
309 }
310
311 InsetBibtex::~InsetBibtex()
312 {
313         if(bibitem_form && bibitem_form->bibitem_form
314            && bibitem_form->bibitem_form->visible
315            && bibitem_form->bibitem_form->u_vdata == &holder)
316                 fl_hide_form(bibitem_form->bibitem_form);
317 }
318
319
320 string InsetBibtex::getScreenLabel() const
321 {
322         return _("BibTeX Generated References");
323 }
324
325
326 int InsetBibtex::Latex(ostream & os,
327                        bool /*fragile*/, bool/*fs*/) const
328 {
329         // this looks like an horrible hack and it is :) The problem
330         // is that owner is not initialized correctly when the bib
331         // inset is cut and pasted. Such hacks will not be needed
332         // later (JMarc)
333         if (!owner) {
334                 owner = current_view->buffer();
335         }
336         // If we generate in a temp dir, we might need to give an
337         // absolute path there. This is a bit complicated since we can
338         // have a comma-separated list of bibliographies
339         string adb, db_out;
340         string db_in = getContents();
341         db_in = split(db_in, adb, ',');
342         while(!adb.empty()) {
343                 if (!owner->niceFile &&
344                     IsFileReadable(MakeAbsPath(adb, owner->filepath)+".bib")) 
345                         adb = MakeAbsPath(adb, owner->filepath);
346                 db_out += adb;
347                 db_out += ',';
348                 db_in= split(db_in, adb,',');
349         }
350         db_out = strip(db_out, ',');
351         // Idem, but simpler
352         string style;
353         if (!owner->niceFile 
354             && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
355                               + ".bst")) 
356                 style = MakeAbsPath(getOptions(), owner->filepath);
357         else
358                 style = getOptions();
359
360         os << "\\bibliographystyle{" << style << "}\n"
361            << "\\bibliography{" << db_out << "}\n";
362         return 2;
363 }
364
365
366 // This method returns a comma separated list of Bibtex entries
367 string InsetBibtex::getKeys(char delim)
368 {
369         // This hack is copied from InsetBibtex::Latex.
370         // Is it still needed? Probably yes.
371         // Why is this needed here when it already is in Latex?
372         // Anyway we need a different way to get to the
373         // buffer the inset is in. (Lgb)
374         
375         //if (!owner) {
376         //      owner = current_view->buffer();
377         //}
378         
379         string tmp, keys;
380         string bibfiles = getContents();
381         bibfiles = split(bibfiles, tmp, ',');
382         while(!tmp.empty()) {
383                 string fil = findtexfile(ChangeExtension(tmp, "bib", false),
384                                          "bib");
385                 lyxerr[Debug::LATEX] << "Bibfile: " << fil << endl;
386                 // If we didn't find a matching file name just fail silently
387                 if (!fil.empty()) {
388                         // This is a _very_ simple parser for Bibtex database
389                         // files. All it does is to look for lines starting
390                         // in @ and not being @preamble and @string entries.
391                         // It does NOT do any syntax checking!
392                         ifstream ifs(fil.c_str());
393                         string linebuf;
394                         while (getline(ifs, linebuf)) {
395                                 linebuf = frontStrip(linebuf);
396                                 if (prefixIs(linebuf, "@")) {
397                                         linebuf = subst(linebuf, '{', '(');
398                                         linebuf = split(linebuf, tmp, '(');
399                                         tmp = lowercase(tmp);
400                                         if (!prefixIs(tmp, "@string")
401                                             && !prefixIs(tmp, "@preamble")) {
402                                                 linebuf = split(linebuf,
403                                                                 tmp, ',');
404                                                 tmp = frontStrip(strip(tmp));
405                                                 if (!tmp.empty()) {
406                                                         keys += tmp;
407                                                         keys += delim;
408                                                 }
409                                         }
410                                 }
411                         }
412                 }
413                 // Get next file name
414                 bibfiles = split(bibfiles, tmp, ',');
415         }
416         return keys;
417 }
418
419
420 // BibTeX should have its own dialog. This is provisional.
421 void InsetBibtex::Edit(BufferView * bv, int, int, unsigned int)
422 {
423         if (!bibitem_form) {
424                 bibitem_form = create_form_bibitem_form();
425                 fl_set_form_atclose(bibitem_form->bibitem_form, 
426                                     CancelCloseBoxCB, 0);
427         }
428
429         holder.inset = this;
430         holder.view = bv;
431         bibitem_form->bibitem_form->u_vdata = &holder;
432
433         fl_set_object_label(bibitem_form->key, _("Database:"));
434         fl_set_object_label(bibitem_form->label, _("Style:  "));
435         fl_set_input(bibitem_form->key, getContents().c_str());
436         fl_set_input(bibitem_form->label, getOptions().c_str());
437         if (bibitem_form->bibitem_form->visible) {
438                 fl_raise_form(bibitem_form->bibitem_form);
439         } else {
440                 fl_show_form(bibitem_form->bibitem_form,
441                              FL_PLACE_MOUSE, FL_FULLBORDER,
442                              _("BibTeX"));
443         }   
444 }
445
446
447 bool InsetBibtex::addDatabase(string const & db)
448 {
449         if (!contains(contents, db.c_str())) {
450                 if (!contents.empty()) 
451                         contents += ',';
452                 contents += db;
453                 return true;
454         }
455         return false;
456 }
457
458
459 bool InsetBibtex::delDatabase(string const & db)
460 {
461         if (contains(contents, db.c_str())) {
462                 string bd = db;
463                 int n = tokenPos(contents, ',', bd);
464                 if (n > 0) {
465                         // Weird code, would someone care to explain this?(Lgb)
466                         string tmp(", ");
467                         tmp += bd;
468                         contents = subst(contents, tmp.c_str(), ", ");
469                 } else if (n == 0)
470                         contents = split(contents, bd, ',');
471                 else 
472                         return false;
473         }
474         return true;
475 }
476
477
478 // This function should be in LyXView when multiframe works ale970302
479 void BibitemUpdate(Combox * combox)
480 {
481         combox->clear();
482         
483         if (!current_view->available()) return;
484         
485         string tmp, bibkeys = current_view->buffer()->getBibkeyList(',');
486         bibkeys = split(bibkeys, tmp,',');
487         while (!tmp.empty()) {
488                 combox->addto(tmp.c_str());
489                 bibkeys = split(bibkeys, tmp,',');
490         }
491 }
492
493
494
495 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
496 int bibitemMaxWidth(Painter & pain, LyXFont const & font)
497 {
498         int w = 0;
499         // Does look like a hack? It is! (but will change at 0.13)
500         LyXParagraph * par = current_view->buffer()->paragraph;
501     
502         while (par) {
503                 if (par->bibkey) {
504                         int wx = par->bibkey->width(pain, font);
505                         if (wx > w) w = wx;
506                 }
507                 par = par->next;
508         }
509         return w;
510 }
511
512
513 // ale070405
514 string bibitemWidthest(Painter & pain)
515 {
516         int w = 0;
517         // Does look like a hack? It is! (but will change at 0.13)
518         LyXParagraph * par = current_view->buffer()->paragraph;
519         InsetBibKey * bkey = 0;
520         LyXFont font;
521       
522         while (par) {
523                 if (par->bibkey) {
524                         int wx = par->bibkey->width(pain, font);
525                         if (wx > w) {
526                                 w = wx;
527                                 bkey = par->bibkey;
528                         }
529                 }
530                 par = par->next;
531         }
532     
533         if (bkey && !bkey->getScreenLabel().empty())
534                 return bkey->getScreenLabel();
535     
536         return "99";
537 }