home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / lyx-0.13.2.tar.gz / lyx-0.13.2.tar / lyx-0.13.2 / src / insetbib.C < prev    next >
C/C++ Source or Header  |  1998-04-23  |  11KB  |  438 lines

  1.  
  2. #include <config.h>
  3.  
  4. #include <stdlib.h>
  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 "error.h"
  15. #include "lyx_gui_misc.h"
  16. #include "BufferView.h"
  17. #include "gettext.h"
  18. #include "bibforms.h"
  19. #include "lyxtext.h"
  20.  
  21. extern BufferView *current_view;
  22.  
  23. FD_citation_form *citation_form=0;
  24. FD_bibitem_form *bibitem_form=0;
  25. static Combox *bibcombox = NULL;
  26.  
  27. extern void UpdateInset(Inset* inset, bool mark_dirty = true);
  28. void BibitemUpdate(Combox *);
  29. void bibitem_cb(FL_OBJECT *, long);
  30. FD_citation_form * create_form_citation_form(void);
  31. FD_bibitem_form * create_form_bibitem_form(void);
  32.  
  33.  
  34. void bibitem_cb(FL_OBJECT *, long data)
  35. {
  36.     switch (data) {
  37.     case 1: // OK, citation
  38.         {
  39.         if(!current_view->currentBuffer()->isReadonly()) {
  40.             InsetCommand *inset = (InsetCommand*)citation_form->vdata;
  41.             inset->setContents(bibcombox->getline());
  42.             inset->setOptions(fl_get_input(citation_form->label));
  43.             fl_hide_form(citation_form->citation_form);
  44.             // shouldn't mark the buffer dirty unless something
  45.             // was actually altered
  46.             UpdateInset(inset);
  47.             break;
  48.         }
  49.         // fall through to Cancel on RO-mode
  50.         }       
  51.     case 0: fl_hide_form(citation_form->citation_form);
  52.                 break;
  53.  
  54.     case 3: // OK, bibitem
  55.         {
  56.         if(!current_view->currentBuffer()->isReadonly()) {
  57.             InsetCommand *inset = (InsetCommand *)bibitem_form->vdata;
  58.             inset->setContents(fl_get_input(bibitem_form->key));
  59.             inset->setOptions(fl_get_input(bibitem_form->label));
  60.             fl_hide_form(bibitem_form->bibitem_form);
  61.             // Does look like a hack? It is! (but will change at 0.13)
  62.             current_view->currentBuffer()->text->RedoParagraph();
  63.             current_view->currentBuffer()->update(1);
  64.             break;
  65.         } // fall through to Cancel on RO-mode
  66.         }
  67.     case 2: // Cancel, bibitem
  68.         fl_hide_form(bibitem_form->bibitem_form); // Cancel, bibitem
  69.         break;
  70.         }
  71. }
  72.  
  73. FD_citation_form *create_form_citation_form(void)
  74. {
  75.     FL_OBJECT *obj;
  76.     FD_citation_form *fdui = (FD_citation_form *) fl_calloc(1, sizeof(*fdui));
  77.  
  78.     fdui->citation_form = fl_bgn_form(FL_NO_BOX, 220, 130);
  79.     obj = fl_add_box(FL_UP_BOX,0,0,220,130,"");
  80.     fdui->key = obj = fl_add_text(FL_NORMAL_TEXT,20,10,60,30,_("Key:"));
  81.       fl_set_object_lsize(obj,FL_NORMAL_SIZE);
  82.       fl_set_object_lalign(obj,FL_ALIGN_RIGHT);
  83.  
  84.     bibcombox = new Combox(Combox::FL_COMBOX_INPUT);
  85.     bibcombox->add(80,10,130,30, 120);
  86.  
  87.     obj = fl_add_button(FL_RETURN_BUTTON,20,90,90,30,_("OK"));
  88.       fl_set_object_lsize(obj,FL_NORMAL_SIZE);
  89.       fl_set_object_callback(obj,bibitem_cb,1);
  90.     obj = fl_add_button(FL_NORMAL_BUTTON,120,90,90,30,idex(_("Cancel|^[")));
  91.       fl_set_button_shortcut(obj,scex(_("Cancel|^[")),1);
  92.       fl_set_object_lsize(obj,FL_NORMAL_SIZE);
  93.       fl_set_object_callback(obj,bibitem_cb,0);
  94.     fdui->label = obj = fl_add_input(FL_NORMAL_INPUT,80,50,130,30,idex(_("Remark:|#R")));
  95.       fl_set_input_shortcut(obj,scex(_("Remark:|#R")),1);
  96.       fl_set_object_lsize(obj,FL_NORMAL_SIZE);
  97.     fl_end_form();
  98.  
  99.     fdui->citation_form->fdui = fdui;
  100.  
  101.     return fdui;
  102. }
  103. /*---------------------------------------*/
  104.  
  105. FD_bibitem_form *create_form_bibitem_form(void)
  106. {
  107.     FL_OBJECT *obj;
  108.     FD_bibitem_form *fdui = (FD_bibitem_form *) fl_calloc(1, sizeof(*fdui));
  109.  
  110.     fdui->bibitem_form = fl_bgn_form(FL_NO_BOX, 220, 130);
  111.     obj = fl_add_box(FL_UP_BOX,0,0,220,130,"");
  112.     fdui->key = obj = fl_add_input(FL_NORMAL_INPUT,80,10,130,30,idex(_("Key:|#K")));
  113.       fl_set_input_shortcut(obj,scex(_("Key:|#K")),1);
  114.       fl_set_object_lsize(obj,FL_NORMAL_SIZE);
  115.     obj = fl_add_button(FL_RETURN_BUTTON,20,90,90,30,_("OK"));
  116.       fl_set_object_lsize(obj,FL_NORMAL_SIZE);
  117.       fl_set_object_callback(obj,bibitem_cb,3);
  118.     obj = fl_add_button(FL_NORMAL_BUTTON,120,90,90,30,idex(_("Cancel|^[")));
  119.       fl_set_button_shortcut(obj,scex(_("Cancel|^[")),1);
  120.       fl_set_object_lsize(obj,FL_NORMAL_SIZE);
  121.       fl_set_object_callback(obj,bibitem_cb,2);
  122.     fdui->label = obj = fl_add_input(FL_NORMAL_INPUT,80,50,130,30,idex(_("Label:|#L")));
  123.       fl_set_input_shortcut(obj,scex(_("Label:|#L")),1);
  124.       fl_set_object_lsize(obj,FL_NORMAL_SIZE);
  125.     fl_end_form();
  126.  
  127.     fdui->bibitem_form->fdui = fdui;
  128.  
  129.     return fdui;
  130. }
  131. /*---------------------------------------*/
  132.  
  133.  
  134. InsetCitation::InsetCitation(LString const & key, LString const & note):
  135.     InsetCommand("cite", key, note)
  136. {
  137.  
  138. }
  139. InsetCitation::~InsetCitation()
  140. {
  141.     if(citation_form && citation_form->citation_form
  142.        && citation_form->citation_form->visible
  143.        && citation_form->vdata == this)
  144.         fl_hide_form(citation_form->citation_form);
  145. }
  146.  
  147.  
  148. void InsetCitation::Edit(int, int)
  149. {
  150.     if(current_view->currentBuffer()->isReadonly())
  151.         WarnReadonly();
  152.  
  153.     if (!citation_form) {
  154.         citation_form = create_form_citation_form();
  155.         fl_set_form_atclose(citation_form->citation_form, 
  156.                     CancelCloseBoxCB, NULL);
  157.     }
  158.     citation_form->vdata = this;
  159.  
  160.     BibitemUpdate(bibcombox);
  161.     if (!bibcombox->select_text(getContents().c_str()))
  162.         bibcombox->addline(getContents().c_str());
  163.         
  164.     fl_set_input(citation_form->label, getOptions().c_str());
  165.     if (citation_form->citation_form->visible) {
  166.         fl_raise_form(citation_form->citation_form);
  167.     } else {
  168.         fl_show_form(citation_form->citation_form,FL_PLACE_MOUSE, FL_FULLBORDER,
  169.                  _("Citation"));
  170.     }   
  171. }
  172.  
  173.  
  174. LString InsetCitation::getScreenLabel() const
  175. {
  176.     LString temp("[");
  177.  
  178.     if (contents.empty()) {
  179.         temp+="???";
  180.     } else {
  181.         temp+=contents;
  182.     }
  183.  
  184.     if (!options.empty()) {
  185.         temp+=","+options;
  186.     }
  187.  
  188.     return temp+']';
  189. }
  190.  
  191.  
  192. InsetBibKey::InsetBibKey(LString const & key, LString const & label):
  193.     InsetCommand("bibitem", key, label)
  194. {
  195.     counter = 1;
  196.     if (key.empty())
  197.         contents = ' ';
  198. }
  199.  
  200.  
  201. InsetBibKey::InsetBibKey(InsetBibKey const *b):
  202.     InsetCommand("bibitem", b->contents, b->options)
  203. {
  204.     counter = b->counter;
  205. }
  206.  
  207. InsetBibKey::~InsetBibKey()
  208. {
  209.     if(bibitem_form && bibitem_form->bibitem_form
  210.        && bibitem_form->bibitem_form->visible)
  211.                 fl_hide_form(bibitem_form->bibitem_form);
  212. }
  213.  
  214. void InsetBibKey::setCounter(int c) 
  215.     counter = c; 
  216.     
  217.     if (contents.empty())
  218.         contents += 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.     LString 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. LString InsetBibKey::getScreenLabel() const
  240. {
  241.     if (!options.empty())
  242.         return options;
  243.     
  244.     LString s;
  245.     return s + counter;
  246. }
  247.  
  248.  
  249. /*
  250.   The value in "Key:" isn't allways set right after a few bibkey insets have
  251.   been added/removed.  Perhaps the wrong object is deleted/used somewhere upwards?
  252.   (Joacim 1998-03-04)
  253. */
  254. void InsetBibKey::Edit(int, int)
  255. {
  256.     if(current_view->currentBuffer()->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, NULL);
  263.     }
  264.     bibitem_form->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(LString const & dbase, LString const & style,
  284.              Buffer *o)
  285.     :InsetCommand("BibTeX", dbase, style), owner(o)
  286. {
  287. }
  288.  
  289.  
  290. InsetBibtex::~InsetBibtex()
  291. {
  292. }
  293.  
  294.  
  295. LString InsetBibtex::getScreenLabel() const
  296. {
  297.     return _("BibTeX Generated References");
  298. }
  299.  
  300.  
  301. int InsetBibtex::Latex(FILE *file, signed char /*fragile*/)
  302. {
  303.     LString bib;
  304.     signed char dummy = 0;
  305.     int result = Latex(bib, dummy);
  306.     fprintf(file, "%s", bib.c_str());
  307.     return result;
  308. }
  309.  
  310.  
  311. int InsetBibtex::Latex(LString &file, signed char /*fragile*/)
  312. {
  313.     file += "\\bibliographystyle{";
  314.     file += getOptions();
  315.     file += "}\n";
  316.     file += "\\bibliography{";
  317.     file += getContents();
  318.     file += "}\n";
  319.     return 2;
  320. }
  321.  
  322.  
  323. // BibTeX should have its own dialog. This is provisional.
  324. void InsetBibtex::Edit(int, int)
  325. {
  326.     if (!bibitem_form) {
  327.         bibitem_form = create_form_bibitem_form();
  328.         fl_set_form_atclose(bibitem_form->bibitem_form, 
  329.                     CancelCloseBoxCB, NULL);
  330.     }
  331.  
  332.     bibitem_form->vdata = this;
  333.     fl_set_object_label(bibitem_form->key, _("Database:"));
  334.     fl_set_object_label(bibitem_form->label, _("Style:  "));
  335.     fl_set_input(bibitem_form->key, getContents().c_str());
  336.     fl_set_input(bibitem_form->label, getOptions().c_str());
  337.     if (bibitem_form->bibitem_form->visible) {
  338.         fl_raise_form(bibitem_form->bibitem_form);
  339.     } else {
  340.         fl_show_form(bibitem_form->bibitem_form,FL_PLACE_MOUSE, FL_FULLBORDER,
  341.                  _("BibTeX"));
  342.     }   
  343. }
  344.  
  345.  
  346. bool InsetBibtex::addDatabase(LString const &db)
  347. {
  348.     if (!contents.contains(db.c_str())) {
  349.         if (!contents.empty()) 
  350.             contents += ',';
  351.         contents += db;
  352.         return true;
  353.     }
  354.     return false;
  355. }
  356.  
  357.  
  358. bool InsetBibtex::delDatabase(LString const &db)
  359. {
  360.     if (contents.contains(db.c_str())) {
  361.         LString bd = db;
  362.         int n = contents.tokenPos(',', bd);
  363.         if (n > 0) {
  364.             LString tmp(",");
  365.             tmp += bd;
  366.             contents.subst(tmp.c_str(), ",");
  367.         } else if (n==0)
  368.             contents.split(bd, ',');
  369.         else 
  370.             return false;
  371.     }
  372.     return true;
  373. }
  374.  
  375.  
  376. // This function should be in LyXView when multiframe works ale970302
  377. void BibitemUpdate(Combox* combox)
  378. {
  379.     combox->clear();
  380.     
  381.     if (!current_view->available())
  382.         return;
  383.  
  384.     LyXParagraph *par = current_view->currentBuffer()->paragraph;    
  385.     while (par) {
  386.         if (par->bibkey) 
  387.             combox->addto(par->bibkey->getContents().c_str());
  388.     
  389.         par = par->next;
  390.     }
  391. }
  392.  
  393.  
  394. // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
  395. int bibitemMaxWidth(const class LyXFont &font)
  396. {
  397.     int w = 0;
  398.     // Does look like a hack? It is! (but will change at 0.13)
  399.     LyXParagraph *par = current_view->currentBuffer()->paragraph;
  400.     
  401.     while (par) {
  402.         if (par->bibkey) {
  403.             int wx = par->bibkey->Width(font);
  404.             if (wx>w) w = wx;
  405.         }
  406.         par = par->next;
  407.     }
  408.     return w;
  409. }
  410.  
  411.  
  412. // ale070405 
  413. LString bibitemWidthest()
  414. {
  415.     int w = 0;
  416.     // Does look like a hack? It is! (but will change at 0.13)
  417.     LyXParagraph *par = current_view->currentBuffer()->paragraph;
  418.     InsetBibKey *bkey=0;
  419.     LyXFont font;
  420.       
  421.     while (par) {
  422.         if (par->bibkey) {
  423.             int wx = par->bibkey->Width(font);
  424.             if (wx>w) {
  425.                 w = wx;
  426.                 bkey = par->bibkey;
  427.             }
  428.         }
  429.         par = par->next;
  430.     }
  431.     
  432.     if (bkey)
  433.         return bkey->getOptions();
  434.     
  435.     return LString();
  436. }
  437.