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