]> git.lyx.org Git - lyx.git/blob - src/insets/insetbib.C
more tostr fixes and stricter lyxstring again + lyxstring & operator+=(int)
[lyx.git] / src / insets / insetbib.C
1
2 #include <config.h>
3
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
22 extern BufferView *current_view;
23
24 FD_citation_form *citation_form=0;
25 FD_bibitem_form *bibitem_form=0;
26 static Combox *bibcombox = 0;
27
28 extern void UpdateInset(Inset* inset, bool mark_dirty = true);
29 void BibitemUpdate(Combox *);
30 FD_citation_form * create_form_citation_form(void);
31 FD_bibitem_form * create_form_bibitem_form(void);
32
33
34 extern "C" void bibitem_cb(FL_OBJECT *, long data)
35 {
36         switch (data) {
37 //       case 0: fl_hide_form(citation_form->citation_form);
38 //               break;
39 //       case 1: 
40         case 1: // OK, citation
41         {
42 //               InsetCommand *inset = (InsetCommand*)citation_form->vdata;
43 //               inset->setContents(bibcombox->getline());
44 //               inset->setOptions(fl_get_input(citation_form->label));
45 //               fl_hide_form(citation_form->citation_form);
46 //               UpdateInset(inset);
47 //               break;
48                 if(!current_view->currentBuffer()->isReadonly()) {
49                         InsetCommand *inset = (InsetCommand*)citation_form->vdata;
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                         UpdateInset(inset);
56                         break;
57                 }
58                 // fall through to Cancel on RO-mode
59         }       
60 //       case 2:  fl_hide_form(bibitem_form->bibitem_form);
61         case 0: fl_hide_form(citation_form->citation_form);
62                 break;
63 //       case 3:
64
65         case 3: // OK, bibitem
66         {
67 //               InsetCommand *inset = (InsetCommand *)bibitem_form->vdata;
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 //               current_view->currentBuffer()->text->RedoParagraph();
73 //               current_view->currentBuffer()->update(1);
74 //               break;
75                 if(!current_view->currentBuffer()->isReadonly()) {
76                         InsetCommand *inset = (InsetCommand *)bibitem_form->vdata;
77                         inset->setContents(fl_get_input(bibitem_form->key));
78                         inset->setOptions(fl_get_input(bibitem_form->label));
79                         fl_hide_form(bibitem_form->bibitem_form);
80                         // Does look like a hack? It is! (but will change at 0.13)
81                         current_view->currentBuffer()->text->RedoParagraph();
82                         current_view->currentBuffer()->update(1);
83                         break;
84                 } // fall through to Cancel on RO-mode
85         }
86         case 2: // Cancel, bibitem
87                 fl_hide_form(bibitem_form->bibitem_form); // Cancel, bibitem
88                 break;
89         }
90 }
91
92 FD_citation_form *create_form_citation_form(void)
93 {
94         FL_OBJECT *obj;
95         FD_citation_form *fdui = (FD_citation_form *) fl_calloc(1, sizeof(FD_citation_form));
96
97         fdui->citation_form = fl_bgn_form(FL_NO_BOX, 220, 130);
98         obj = fl_add_box(FL_UP_BOX,0,0,220,130,"");
99         fdui->key = obj = fl_add_text(FL_NORMAL_TEXT,20,10,60,30,_("Key:"));
100           fl_set_object_lsize(obj,FL_NORMAL_SIZE);
101           fl_set_object_lalign(obj,FL_ALIGN_RIGHT);
102
103         bibcombox = new Combox(FL_COMBOX_INPUT);
104         bibcombox->add(80,10,130,30, 120);
105
106         obj = fl_add_button(FL_RETURN_BUTTON,20,90,90,30,_("OK"));
107           fl_set_object_lsize(obj,FL_NORMAL_SIZE);
108           fl_set_object_callback(obj,bibitem_cb,1);
109         obj = fl_add_button(FL_NORMAL_BUTTON,120,90,90,30,idex(_("Cancel|^[")));
110           fl_set_button_shortcut(obj,scex(_("Cancel|^[")),1);
111           fl_set_object_lsize(obj,FL_NORMAL_SIZE);
112           fl_set_object_callback(obj,bibitem_cb,0);
113         fdui->label = obj = fl_add_input(FL_NORMAL_INPUT,80,50,130,30,idex(_("Remark:|#R")));
114           fl_set_input_shortcut(obj,scex(_("Remark:|#R")),1);
115           fl_set_object_lsize(obj,FL_NORMAL_SIZE);
116         fl_end_form();
117
118         //fdui->citation_form->fdui = fdui;
119
120         return fdui;
121 }
122 /*---------------------------------------*/
123
124 FD_bibitem_form *create_form_bibitem_form(void)
125 {
126         FL_OBJECT *obj;
127         FD_bibitem_form *fdui = (FD_bibitem_form *) fl_calloc(1, sizeof(FD_bibitem_form));
128
129         fdui->bibitem_form = fl_bgn_form(FL_NO_BOX, 220, 130);
130         obj = fl_add_box(FL_UP_BOX,0,0,220,130,"");
131         fdui->key = obj = fl_add_input(FL_NORMAL_INPUT,80,10,130,30,idex(_("Key:|#K")));
132           fl_set_input_shortcut(obj,scex(_("Key:|#K")),1);
133           fl_set_object_lsize(obj,FL_NORMAL_SIZE);
134         obj = fl_add_button(FL_RETURN_BUTTON,20,90,90,30,_("OK"));
135           fl_set_object_lsize(obj,FL_NORMAL_SIZE);
136           fl_set_object_callback(obj,bibitem_cb,3);
137         obj = fl_add_button(FL_NORMAL_BUTTON,120,90,90,30,idex(_("Cancel|^[")));
138           fl_set_button_shortcut(obj,scex(_("Cancel|^[")),1);
139           fl_set_object_lsize(obj,FL_NORMAL_SIZE);
140           fl_set_object_callback(obj,bibitem_cb,2);
141         fdui->label = obj = fl_add_input(FL_NORMAL_INPUT,80,50,130,30,idex(_("Label:|#L")));
142           fl_set_input_shortcut(obj,scex(_("Label:|#L")),1);
143           fl_set_object_lsize(obj,FL_NORMAL_SIZE);
144         fl_end_form();
145
146         //fdui->bibitem_form->fdui = fdui;
147
148         return fdui;
149 }
150 /*---------------------------------------*/
151
152
153 InsetCitation::InsetCitation(string const & key, string const & note):
154         InsetCommand("cite", key, note)
155 {
156
157 }
158 InsetCitation::~InsetCitation()
159 {
160         if(citation_form && citation_form->citation_form
161            && citation_form->citation_form->visible
162            && citation_form->vdata == this)
163                 fl_hide_form(citation_form->citation_form);
164 }
165
166
167 void InsetCitation::Edit(int, int)
168 {
169         if(current_view->currentBuffer()->isReadonly())
170                 WarnReadonly();
171
172         if (!citation_form) {
173                 citation_form = create_form_citation_form();
174                 fl_set_form_atclose(citation_form->citation_form, 
175                                     CancelCloseBoxCB, 0);
176         }
177         citation_form->vdata = this;
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,FL_PLACE_MOUSE, FL_FULLBORDER,
188                              _("Citation"));
189         }   
190 }
191
192
193 string InsetCitation::getScreenLabel() const
194 {
195         string temp("[");
196
197         temp += contents;
198
199         if (!options.empty()) {
200                 temp += "," + options;
201         }
202
203         return temp + ']';
204 }
205
206
207 InsetBibKey::InsetBibKey(string const & key, string const & label):
208         InsetCommand("bibitem", key, label)
209 {
210         counter = 1;
211         if (key.empty())
212                 contents = ' ';
213 }
214
215
216 InsetBibKey::InsetBibKey(InsetBibKey const *b):
217         InsetCommand("bibitem", b->contents, b->options)
218 {
219         counter = b->counter;
220 }
221
222 InsetBibKey::~InsetBibKey()
223 {
224         if(bibitem_form && bibitem_form->bibitem_form
225            && bibitem_form->bibitem_form->visible)
226                                 fl_hide_form(bibitem_form->bibitem_form);
227 }
228
229 void InsetBibKey::setCounter(int c) 
230
231         counter = c; 
232     
233         if (contents.empty())
234                 contents += tostr(counter);
235 }
236
237
238 // I'm sorry but this is still necessary because \bibitem is used also
239 // as a LyX 2.x command, and lyxlex is not enough smart to understand
240 // real LaTeX commands. Yes, that could be fixed, but would be a waste 
241 // of time cause LyX3 won't use lyxlex anyway.  (ale)
242 void InsetBibKey::Write(FILE *file)
243 {
244         string s;
245         if (!options.empty()) {
246                 s += '[';
247                 s += options + ']';
248         }
249         s += '{';
250         s += contents + '}';
251         fprintf(file,"\\bibitem %s\n", s.c_str());
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 upwards?
267   (Joacim 1998-03-04)
268 */
269 void InsetBibKey::Edit(int, int)
270 {
271         if(current_view->currentBuffer()->isReadonly())
272                 WarnReadonly();
273         
274         if (!bibitem_form) {
275                 bibitem_form = create_form_bibitem_form();
276                 fl_set_form_atclose(bibitem_form->bibitem_form, 
277                                     CancelCloseBoxCB, 0);
278         }
279         bibitem_form->vdata = this;
280         // InsetBibtex uses the same form, with different labels
281         fl_set_object_label(bibitem_form->key, idex(_("Key:|#K")));
282         fl_set_button_shortcut(bibitem_form->key,scex(_("Key:|#K")),1);
283         fl_set_object_label(bibitem_form->label, idex(_("Label:|#L")));
284         fl_set_button_shortcut(bibitem_form->label,scex(_("Label:|#L")),1);
285         fl_set_input(bibitem_form->key, getContents().c_str());
286         fl_set_input(bibitem_form->label, getOptions().c_str());
287         if (bibitem_form->bibitem_form->visible) {
288                 fl_raise_form(bibitem_form->bibitem_form);
289         } else {
290                 fl_show_form(bibitem_form->bibitem_form,FL_PLACE_MOUSE,
291                              FL_FULLBORDER,
292                              _("Bibliography item"));
293         }   
294 }
295
296
297
298 InsetBibtex::InsetBibtex(string const & dbase, string const & style,
299                          Buffer *o)
300         :InsetCommand("BibTeX", dbase, style), owner(o)
301 {
302 }
303
304
305 InsetBibtex::~InsetBibtex()
306 {
307 }
308
309
310 string InsetBibtex::getScreenLabel() const
311 {
312         return _("BibTeX Generated References");
313 }
314
315
316 int InsetBibtex::Latex(FILE *file, signed char /*fragile*/)
317 {
318         string bib;
319         signed char dummy = 0;
320         int result = Latex(bib, dummy);
321         fprintf(file, "%s", bib.c_str());
322         return result;
323 }
324
325
326 int InsetBibtex::Latex(string &file, signed char /*fragile*/)
327 {
328         // this looks like an horrible hack and it is :) The problem
329         // is that owner is not initialized correctly when the bib
330         // inset is cut and pasted. Such hacks will not be needed
331         // later (JMarc)
332         if (!owner) {
333                 owner = current_view->currentBuffer();
334         }
335         // If we generate in a temp dir, we might need to give an
336         // absolute path there. This is a bit complicated since we can
337         // have a comma-separated list of bibliographies
338         string db_in, adb, db_out;
339         db_in = getContents();
340         db_in=split(db_in, adb, ',');
341         while(!adb.empty()) {
342                 if (!owner->niceFile &&
343                     IsFileReadable(MakeAbsPath(adb,owner->filepath)+".bib")) 
344                         adb = MakeAbsPath(adb,owner->filepath);
345                 db_out += adb;
346                 db_out += ',';
347                 db_in=split(db_in, adb,',');
348         }
349         db_out = strip(db_out, ',');
350         // Idem, but simpler
351         string style;
352         if (!owner->niceFile 
353             && IsFileReadable(MakeAbsPath(getOptions(), owner->filepath)
354                               + ".bst")) 
355                 style = MakeAbsPath(getOptions(), owner->filepath);
356         else
357                 style = getOptions();
358
359         file += "\\bibliographystyle{";
360         file += style;
361         file += "}\n";
362         file += "\\bibliography{";
363         file += db_out;
364         file += "}\n";
365         return 2;
366 }
367
368 // This method returns a comma separated list of Bibtex entries
369 string InsetBibtex::getKeys()
370 {
371         // This hack is copied from InsetBibtex::Latex.
372         // Is it still needed? Probably yes.
373         if (!owner) {
374                 owner = current_view->currentBuffer();
375         }
376
377         // We need to create absolute path names for bibliographies
378         // First look for bib-file in same directory as document,
379         // then in all directories listed in environment variable 
380         // BIBINPUTS
381         string bibfiles, linebuf, tmp, keys;
382         bibfiles = getContents();
383         bibfiles=split(bibfiles, tmp, ',');
384         while(!tmp.empty()) {
385                 if (IsFileReadable(MakeAbsPath(tmp,owner->filepath)+".bib"))
386                         tmp = MakeAbsPath(tmp,owner->filepath)+".bib";
387                 else {
388                         tmp = FileOpenSearch(GetEnvPath("BIBINPUTS"),tmp,"bib");
389                         if (tmp.empty())
390                                 tmp = FileOpenSearch(GetEnvPath("BIBINPUT"),
391                                                      tmp, "bib");
392                 }
393                 // If we didn't find a matching file name just fail silently
394                 if (!tmp.empty()) {
395       
396                         // This is a _very_ simple parser for Bibtex database files.
397                         // All it does is to look for lines starting in @ and not
398                         // being @preamble and @string entries.
399                         // It does NOT do any syntax checking!
400                         FilePtr file(tmp,FilePtr::read);
401                         char c;
402
403                         // On some systems where feof() is a macro,
404                         // the () after file is needed (JMarc)
405                         while (! feof(file())) {
406                                 c = fgetc(file);
407
408                                 // At end of each line check if line begins with '@'
409                                 if ( c == '\n') {
410                                         if (prefixIs(linebuf, "@") ) {
411                                                 subst(linebuf, '{','(');
412                                                 linebuf=split(linebuf, tmp,'(');
413                                                 tmp = lowercase(tmp);
414                                                 if (!prefixIs(tmp, "@string") && !prefixIs(tmp, "@preamble") ) {
415                                                         linebuf = split(linebuf, tmp,',');
416                                                         if (!tmp.empty())
417                                                                 keys += strip(tmp) + ",";
418                                                 }
419                                         }
420                                         linebuf.erase();
421                                 } else {
422                                         linebuf += c;
423                                 }
424                         }
425                 }
426                 // Get next file name
427                 bibfiles=split(bibfiles, tmp, ',');
428         }
429         return keys;
430 }
431
432 // BibTeX should have its own dialog. This is provisional.
433 void InsetBibtex::Edit(int, int)
434 {
435         if (!bibitem_form) {
436                 bibitem_form = create_form_bibitem_form();
437                 fl_set_form_atclose(bibitem_form->bibitem_form, 
438                                     CancelCloseBoxCB, 0);
439         }
440
441         bibitem_form->vdata = this;
442         fl_set_object_label(bibitem_form->key, _("Database:"));
443         fl_set_object_label(bibitem_form->label, _("Style:  "));
444         fl_set_input(bibitem_form->key, getContents().c_str());
445         fl_set_input(bibitem_form->label, getOptions().c_str());
446         if (bibitem_form->bibitem_form->visible) {
447                 fl_raise_form(bibitem_form->bibitem_form);
448         } else {
449                 fl_show_form(bibitem_form->bibitem_form,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                         string tmp(",");
474                         tmp += bd;
475                         subst(contents, tmp.c_str(), ",");
476                 } else if (n==0)
477                         contents = split(contents, bd, ',');
478                 else 
479                         return false;
480         }
481         return true;
482 }
483
484
485 // This function should be in LyXView when multiframe works ale970302
486 void BibitemUpdate(Combox* combox)
487 {
488         combox->clear();
489     
490         if (!current_view->available())
491                 return;
492
493         string tmp, bibkeys = current_view->currentBuffer()->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(const class LyXFont &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->currentBuffer()->paragraph;
509     
510         while (par) {
511                 if (par->bibkey) {
512                         int wx = par->bibkey->Width(font);
513                         if (wx>w) w = wx;
514                 }
515                 par = par->next;
516         }
517         return w;
518 }
519
520
521 // ale070405 
522 string bibitemWidthest()
523 {
524         int w = 0;
525         // Does look like a hack? It is! (but will change at 0.13)
526         LyXParagraph *par = current_view->currentBuffer()->paragraph;
527         InsetBibKey *bkey=0;
528         LyXFont font;
529       
530         while (par) {
531                 if (par->bibkey) {
532                         int wx = par->bibkey->Width(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 }