]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetcite.C
parlist-a-1.diff
[lyx.git] / src / insets / insetcite.C
index 7b523bdfa07c6d7b672014c9f14983cb519094c2..8bd95bfdbea5f73c6cad53c8bd0767386521126c 100644 (file)
-#include <config.h>
-
-#include <fstream>
-#include <cstdlib>
-#include <algorithm>
+/**
+ * \file insetcite.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Angus Leeming
+ * \author Herbert Voss
+ *
+ * Full author contact details are available in file CREDITS
+ */
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
+#include <config.h>
 
-#include FORMS_H_LOCATION  
 #include "insetcite.h"
 #include "buffer.h"
-#include "debug.h"
-#include "lyx_gui_misc.h"
 #include "BufferView.h"
-#include "gettext.h"
-#include "lyxtext.h"
-#include "support/filetools.h"
+#include "LaTeXFeatures.h"
 
-using std::getline;
-using std::vector;
-using std::pair;
-using std::max;
-using std::min;
-using std::find;
+#include "frontends/controllers/biblio.h"
 
-FD_citation_form * citation_form = 0;
-FD_citation_form * create_form_citation_form(void);
-void set_size_citation_form(FD_citation_form *, int, bool);
+#include "support/lstrings.h"
 
-static vector<pair<string,string> > bibkeys_info;
-static vector<string> bibkeys;
-static vector<string> insetkeys;
+#include <map>
 
-extern "C" void citation_cb( FL_OBJECT *, long data )
-{
-       InsetCitation::Holder * holder =
-               static_cast<InsetCitation::Holder*>(citation_form->form->u_vdata);
+using std::ostream;
+using std::vector;
+using std::map;
 
-       holder->inset->callback( citation_form,
-                                static_cast<InsetCitation::State>(data) );
-}
+namespace {
 
+// An optimisation. We assume that until the first InsetCitation::edit is
+// called, we're loading the buffer and that, therefore, we don't need to
+// reload the bibkey list
+std::map<Buffer const *, bool> loading_buffer;
 
-FD_citation_form * create_form_citation_form(void)
+string const getNatbibLabel(Buffer const * buffer,
+                           string const & citeType, string const & keyList,
+                           string const & before, string const & after,
+                           bool numerical)
 {
-       FL_OBJECT * obj;
-       FD_citation_form * fdui = (FD_citation_form *) fl_calloc(1, sizeof(*fdui));
-
-       // NOTE: dialog geometry is set in setSize(). 
-       // Initial size is simply non-zero.
-       fdui->form = fl_bgn_form(FL_NO_BOX, 10, 10);
-       fdui->box = obj = fl_add_box(FL_UP_BOX, 0, 0, 10, 10, "");
-
-       fdui->citeBrsr = obj =
-         fl_add_browser(FL_HOLD_BROWSER, 0, 0, 10, 10, _("Citation keys"));
-       fl_set_object_lalign(obj, FL_ALIGN_TOP_LEFT);
-       fl_set_object_lsize(obj, FL_NORMAL_SIZE);
-       fl_set_object_callback(obj, citation_cb, InsetCitation::CITEBRSR);
-
-       fdui->bibBrsr = obj =
-         fl_add_browser(FL_HOLD_BROWSER, 0, 0, 10, 10, _("Bibliography keys"));
-       fl_set_object_lalign(obj, FL_ALIGN_TOP_LEFT);
-       fl_set_object_lsize(obj, FL_NORMAL_SIZE);
-       fl_set_object_callback(obj, citation_cb, InsetCitation::BIBBRSR);
-
-       fdui->addBtn = obj =
-         fl_add_button(FL_NORMAL_BUTTON, 0, 0, 10, 10, "@4->");
-       fl_set_object_lsize(obj, FL_NORMAL_SIZE);
-       fl_set_object_callback(obj, citation_cb, InsetCitation::ADD);
-
-       fdui->delBtn = obj =
-         fl_add_button(FL_NORMAL_BUTTON, 0, 0, 10, 10, "@9+");
-       fl_set_object_lsize(obj, FL_NORMAL_SIZE);
-       fl_set_object_callback(obj, citation_cb, InsetCitation::DELETE);
-
-       fdui->upBtn = obj =
-         fl_add_button(FL_NORMAL_BUTTON, 0, 0, 10, 10, "@8->");
-       fl_set_object_lsize(obj, FL_NORMAL_SIZE);
-       fl_set_object_callback(obj, citation_cb, InsetCitation::UP);
-       
-       fdui->downBtn = obj =
-         fl_add_button(FL_NORMAL_BUTTON, 0, 0, 10, 10, "@2->");
-       fl_set_object_lsize(obj, FL_NORMAL_SIZE);
-       fl_set_object_callback(obj, citation_cb, InsetCitation::DOWN);
-
-       fdui->infoBrsr = obj =
-         fl_add_browser(FL_NORMAL_BROWSER, 0, 0, 10, 10, _("Info"));
-         fl_set_object_lsize(obj, FL_NORMAL_SIZE) ;
-         fl_set_object_lalign(obj, FL_ALIGN_TOP_LEFT);
-
-       fdui->textAftr = obj =
-         fl_add_input(FL_NORMAL_INPUT, 0, 0, 10, 10, _("Text after"));
-       fl_set_object_lsize(obj, FL_NORMAL_SIZE);
-
-       fdui->ok = obj =
-         fl_add_button(FL_RETURN_BUTTON, 0, 0, 10, 10, _("OK"));
-       fl_set_object_lsize(obj, FL_NORMAL_SIZE);
-       fl_set_object_callback(obj, citation_cb, InsetCitation::OK);
-
-       fdui->cancel = obj =
-         fl_add_button(FL_NORMAL_BUTTON, 0, 0, 10, 10, idex(_("Cancel|^[")));
-       fl_set_button_shortcut(obj, scex(_("Cancel|^[")), 1);
-       fl_set_object_lsize(obj, FL_NORMAL_SIZE);
-       fl_set_object_callback(obj, citation_cb, InsetCitation::CANCEL);
-
-       fl_end_form();
-  
-       return fdui;
-}
-
-
-InsetCitation::InsetCitation(string const & key, string const & note)
-       : InsetCommand("cite", key, note)
-{
-}
+       typedef std::map<Buffer const *, biblio::InfoMap> CachedMap;
+       static CachedMap cached_keys;
+
+       // Only load the bibkeys once if we're loading up the buffer,
+       // else load them afresh each time.
+       map<Buffer const *, bool>::iterator lit = loading_buffer.find(buffer);
+       if (lit == loading_buffer.end())
+               loading_buffer[buffer] = true;
+
+       bool loadkeys = !loading_buffer[buffer];
+       if (!loadkeys) {
+               CachedMap::iterator kit = cached_keys.find(buffer);
+               loadkeys = kit == cached_keys.end();
+       }
 
+       if (loadkeys) {
+               // build the keylist
+               typedef vector<std::pair<string, string> > InfoType;
+               InfoType bibkeys;
+               buffer->fillWithBibKeys(bibkeys);
 
-InsetCitation::~InsetCitation()
-{
-       if(citation_form && citation_form->form
-          && citation_form->form->visible
-          && citation_form->form->u_vdata == &holder)
-               fl_hide_form(citation_form->form);
-}
+               InfoType::const_iterator bit  = bibkeys.begin();
+               InfoType::const_iterator bend = bibkeys.end();
 
+               biblio::InfoMap infomap;
+               for (; bit != bend; ++bit) {
+                       infomap[bit->first] = bit->second;
+               }
+               if (infomap.empty())
+                       return string();
 
-void InsetCitation::Edit( BufferView * bv, int, int, unsigned int )
-{
-       if ( !citation_form ) {
-               citation_form = create_form_citation_form();
-               fl_set_form_atclose( citation_form->form, 
-                                    CancelCloseBoxCB, 0 );
+               cached_keys[buffer] = infomap;
        }
 
-       holder.inset = this;
-       holder.view = bv;
-               
-       citation_form->form->u_vdata = &holder;
-
-       // update the browsers, noting the number of keys.
-       bibkeys_info = bv->buffer()->getBibkeyList();
-       bibkeys.clear();
-       insetkeys.clear();
-       for( unsigned int i = 0; i < bibkeys_info.size(); ++i )
-               bibkeys.push_back(bibkeys_info[i].first);
-
-       string tmp;
-       string keys = getContents();
-       keys = frontStrip( split(keys, tmp, ',') );
-       while( !tmp.empty() ) {
-               insetkeys.push_back( tmp );
-               keys = frontStrip( split(keys, tmp, ',') );
+       biblio::InfoMap infomap = cached_keys[buffer];
+
+       // the natbib citation-styles
+       // CITET:       author (year)
+       // CITEP:       (author,year)
+       // CITEALT:     author year
+       // CITEALP:     author, year
+       // CITEAUTHOR:  author
+       // CITEYEAR:    year
+       // CITEYEARPAR: (year)
+
+       // We don't currently use the full or forceUCase fields.
+       // bool const forceUCase = citeType[0] == 'C';
+       bool const full = citeType[citeType.size() - 1] == '*';
+
+       string const cite_type = full ?
+               ascii_lowercase(citeType.substr(0, citeType.size() - 1)) :
+               ascii_lowercase(citeType);
+
+       string before_str;
+       if (!before.empty()) {
+               // In CITET and CITEALT mode, the "before" string is
+               // attached to the label associated with each and every key.
+               // In CITEP, CITEALP and CITEYEARPAR mode, it is attached
+               // to the front of the whole only.
+               // In other modes, it is not used at all.
+               if (cite_type == "citet" ||
+                   cite_type == "citealt" ||
+                   cite_type == "citep" ||
+                   cite_type == "citealp" ||
+                   cite_type == "citeyearpar")
+                       before_str = before + ' ';
        }
 
-       updateBrowser( citation_form->bibBrsr, bibkeys );
-       updateBrowser( citation_form->citeBrsr, insetkeys );
-       fl_clear_browser( citation_form->infoBrsr );
-
-       // No keys have been selected yet, so...
-       setBibButtons(  citation_form, OFF );
-       setCiteButtons( citation_form, OFF );
-
-       int noKeys = max( bibkeys.size(), insetkeys.size() );
-
-       // Place bounds, so that 4 <= noKeys <= 15
-       noKeys = max( 4, min(15, noKeys) );
-
-       // Re-size the form to accommodate the new browser size
-       int size = 20 * noKeys;
-       bool bibPresent = ( bibkeys.size() > 0 );
-       setSize(citation_form, size, bibPresent);
-
-       fl_set_input( citation_form->textAftr, getOptions().c_str() );
-       if( holder.view->buffer()->isReadonly() )
-               fl_deactivate_object( citation_form->textAftr );
-
-       if( citation_form->form->visible ) {
-               fl_raise_form( citation_form->form );
-       } else {
-               fl_show_form( citation_form->form,
-                             FL_PLACE_MOUSE, FL_FULLBORDER,
-                             _("Citation") );
-       } 
-}
+       string after_str;
+       if (!after.empty()) {
+               // The "after" key is appended only to the end of the whole.
+               after_str = ", " + after;
+       }
 
+       // One day, these might be tunable (as they are in BibTeX).
+       char const op  = '('; // opening parenthesis.
+       char const cp  = ')'; // closing parenthesis.
+       // puctuation mark separating citation entries.
+       char const * const sep = ";";
+
+       string const op_str(' ' + string(1, op));
+       string const cp_str(string(1, cp) + ' ');
+       string const sep_str(string(sep) + ' ');
+
+       string label;
+       vector<string> keys = getVectorFromString(keyList);
+       vector<string>::const_iterator it  = keys.begin();
+       vector<string>::const_iterator end = keys.end();
+       for (; it != end; ++it) {
+               // get the bibdata corresponding to the key
+               string const author(biblio::getAbbreviatedAuthor(infomap, *it));
+               string const year(biblio::getYear(infomap, *it));
+
+               // Something isn't right. Fail safely.
+               if (author.empty() || year.empty())
+                       return string();
+
+               // (authors1 (<before> year);  ... ;
+               //  authors_last (<before> year, <after>)
+               if (cite_type == "citet") {
+                       string const tmp = numerical ? '#' + *it : year;
+                       label += author + op_str + before_str + tmp +
+                               cp + sep_str;
+
+               // author, year; author, year; ...
+               } else if (cite_type == "citep" ||
+                          cite_type == "citealp") {
+                       if (numerical) {
+                               label += *it + sep_str;
+                       } else {
+                               label += author + ", " + year + sep_str;
+                       }
 
-void InsetCitation::updateBrowser( FL_OBJECT * browser,
-                                  vector<string> const & inkeys ) const
-{
-       fl_clear_browser( browser );
+               // (authors1 <before> year;
+               //  authors_last <before> year, <after>)
+               } else if (cite_type == "citealt") {
+                       string const tmp = numerical ? '#' + *it : year;
+                       label += author + ' ' + before_str + tmp + sep_str;
 
-       fl_freeze_form( browser->form );
-       for( unsigned int i = 0; i < inkeys.size(); ++i )
-               fl_add_browser_line( browser, inkeys[i].c_str() );
-       fl_unfreeze_form( browser->form );
-}
+               // author; author; ...
+               } else if (cite_type == "citeauthor") {
+                       label += author + sep_str;
 
-void InsetCitation::callback( FD_citation_form * form, State cb )
-{
-       switch( cb ) {
-       case BIBBRSR: {
-               fl_deselect_browser( form->citeBrsr );
-               
-               unsigned int sel = fl_get_browser( form->bibBrsr );
-               if( sel < 1 || sel > bibkeys.size() ) break;
-
-               // Put into infoBrsr the additional info associated with
-               // the selected bibBrsr key
-               fl_clear_browser( form->infoBrsr );
-               fl_add_browser_line( form->infoBrsr,
-                                    bibkeys_info[sel-1].second.c_str() );
-
-               // Highlight the selected bibBrsr key in citeBrsr if present
-               vector<string>::iterator it =
-                       find( insetkeys.begin(), insetkeys.end(), bibkeys[sel-1] );
-
-               if( it != insetkeys.end() ) {
-                       int n = it - insetkeys.begin();
-                       fl_select_browser_line( form->citeBrsr, n+1 );
-                       fl_set_browser_topline( form->citeBrsr, n+1 );
+               // year; year; ...
+               } else if (cite_type == "citeyear" ||
+                          cite_type == "citeyearpar") {
+                       label += year + sep_str;
                }
+       }
+       label = rtrim(rtrim(label), sep);
 
-               if( !holder.view->buffer()->isReadonly() ) {
-                       if( it != insetkeys.end() ) {
-                               setBibButtons(  form, OFF );
-                               setCiteButtons( form, ON );
-                       } else {
-                               setBibButtons(  form, ON );
-                               setCiteButtons( form, OFF );
-                       }
+       if (!after_str.empty()) {
+               if (cite_type == "citet") {
+                       // insert "after" before last ')'
+                       label.insert(label.size() - 1, after_str);
+               } else {
+                       bool const add = !(numerical &&
+                                          (cite_type == "citeauthor" ||
+                                           cite_type == "citeyear"));
+                       if (add)
+                               label += after_str;
                }
-               break;
+       }
 
-       } case CITEBRSR: {
-               unsigned int sel = fl_get_browser( form->citeBrsr );
-               if( sel < 1 || sel > insetkeys.size() ) break;
+       if (!before_str.empty() && (cite_type == "citep" ||
+                                   cite_type == "citealp" ||
+                                   cite_type == "citeyearpar")) {
+               label = before_str + label;
+       }
 
-               if( !holder.view->buffer()->isReadonly() ) {
-                       setBibButtons(  form, OFF );
-                       setCiteButtons( form, ON );
-               }
+       if (cite_type == "citep" || cite_type == "citeyearpar")
+               label = string(1, op) + label + string(1, cp);
 
-               // Highlight the selected citeBrsr key in bibBrsr
-               vector<string>::iterator it =
-                       find( bibkeys.begin(), bibkeys.end(), insetkeys[sel-1] );
+       return label;
+}
 
-               if (it != bibkeys.end()) {
-                       int n = it - bibkeys.begin();
-                       fl_select_browser_line( form->bibBrsr, n+1 );
-                       fl_set_browser_topline( form->bibBrsr, n+1 );
 
-                       // Put into infoBrsr the additional info associated with
-                       // the selected citeBrsr key
-                       fl_clear_browser( form->infoBrsr );
-                       fl_add_browser_line( form->infoBrsr,
-                                            bibkeys_info[n].second.c_str() );
+string const getBasicLabel(string const & keyList, string const & after)
+{
+       string keys(keyList);
+       string label;
+
+       if (contains(keys, ",")) {
+               // Final comma allows while loop to cover all keys
+               keys = ltrim(split(keys, label, ',')) + ',';
+               while (contains(keys, ",")) {
+                       string key;
+                       keys = ltrim(split(keys, key, ','));
+                       label += ", " + key;
                }
-               break;
+       } else
+               label = keys;
 
-       } case ADD: {
-               if( holder.view->buffer()->isReadonly() ) break;
+       if (!after.empty())
+               label += ", " + after;
 
-               unsigned int sel = fl_get_browser( form->bibBrsr );
-               if( sel < 1 || sel > bibkeys.size() ) break;
+       return '[' + label + ']';
+}
 
-               // Add the selected bibBrsr key to citeBrsr
-               fl_addto_browser( form->citeBrsr,
-                                 bibkeys[sel-1].c_str() );
-               insetkeys.push_back( bibkeys[sel-1] );
+} // anon namespace
 
-               int n = insetkeys.size();
-               fl_select_browser_line( form->citeBrsr, n );
 
-               setBibButtons(  form, OFF );
-               setCiteButtons( form, ON );
+InsetCitation::InsetCitation(InsetCommandParams const & p, bool)
+       : InsetCommand(p)
+{}
 
-               break;
 
-       } case DELETE: {
-               if( holder.view->buffer()->isReadonly() ) break;
+InsetCitation::~InsetCitation()
+{
+       InsetCommandMailer mailer("citation", *this);
+       mailer.hideDialog();
+}
 
-               unsigned int sel = fl_get_browser( form->citeBrsr );
-               if( sel < 1 || sel > insetkeys.size() ) break;
 
-               // Remove the selected key from citeBrsr
-               fl_delete_browser_line( form->citeBrsr, sel ) ;
-               insetkeys.erase( insetkeys.begin() + sel-1 );
+string const InsetCitation::generateLabel(Buffer const * buffer) const
+{
+       string const before = string();
+       string const after  = getOptions();
+
+       string label;
+       if (buffer->params.use_natbib) {
+               string cmd = getCmdName();
+               if (cmd == "cite") {
+                       // We may be "upgrading" from an older LyX version.
+                       // If, however, we use "cite" because the necessary
+                       // author/year info is not present in the biblio
+                       // database, then getNatbibLabel will exit gracefully
+                       // and we'll call getBasicLabel.
+                       if (buffer->params.use_numerical_citations)
+                               cmd = "citep";
+                       else
+                               cmd = "citet";
+               }
+               label = getNatbibLabel(buffer, cmd, getContents(),
+                                      before, after,
+                                      buffer->params.use_numerical_citations);
+       }
 
-               setBibButtons(  form, ON );
-               setCiteButtons( form, OFF );
-               break;
+       // Fallback to fail-safe
+       if (label.empty()) {
+               label = getBasicLabel(getContents(), after);
+       }
 
-       } case UP: {
-               if( holder.view->buffer()->isReadonly() ) break;
+       return label;
+}
 
-               unsigned int sel = fl_get_browser( form->citeBrsr );
-               if( sel < 2 || sel > insetkeys.size() ) break;
 
-               // Move the selected key up one line
-               vector<string>::iterator it = insetkeys.begin() + sel-1;
-               string tmp = *it;
+InsetCitation::Cache::Style InsetCitation::getStyle(Buffer const * buffer) const
+{
+       Cache::Style style = Cache::BASIC;
 
-               fl_delete_browser_line( form->citeBrsr, sel );
-               insetkeys.erase( it );
+       if (buffer->params.use_natbib) {
+               if (buffer->params.use_numerical_citations) {
+                       style = Cache::NATBIB_NUM;
+               } else {
+                       style = Cache::NATBIB_AY;
+               }
+       }
 
-               fl_insert_browser_line( form->citeBrsr, sel-1, tmp.c_str() );
-               fl_select_browser_line( form->citeBrsr, sel-1 );
-               insetkeys.insert( it-1, tmp );
-               setCiteButtons( form, ON );
+       return style;
+}
 
-               break;
 
-       } case DOWN: {
-               if( holder.view->buffer()->isReadonly() ) break;
+string const InsetCitation::getScreenLabel(Buffer const * buffer) const
+{
+       Cache::Style const style = getStyle(buffer);
+       if (cache.params == params() && cache.style == style)
+               return cache.screen_label;
 
-               unsigned int sel = fl_get_browser( form->citeBrsr );
-               if( sel < 1 || sel > insetkeys.size()-1 ) break;
+       // The label has changed, so we have to re-create it.
+       string const before = string();
+       string const after  = getOptions();
 
-               // Move the selected key down one line
-               vector<string>::iterator it = insetkeys.begin() + sel-1;
-               string tmp = *it;
+       string const glabel = generateLabel(buffer);
 
-               fl_delete_browser_line( form->citeBrsr, sel );
-               insetkeys.erase( it );
+       unsigned int const maxLabelChars = 45;
 
-               fl_insert_browser_line( form->citeBrsr, sel+1, tmp.c_str() );
-               fl_select_browser_line( form->citeBrsr, sel+1 );
-               insetkeys.insert( it+1, tmp );
-               setCiteButtons( form, ON );
+       string label = glabel;
+       if (label.size() > maxLabelChars) {
+               label.erase(maxLabelChars-3);
+               label += "...";
+       }
 
-               break;
+       cache.style  = style;
+       cache.params = params();
+       cache.generated_label = glabel;
+       cache.screen_label = label;
 
-       } case OK: {
-               // The inset contains a comma separated list of the keys
-               // in citeBrsr
-               if( !holder.view->buffer()->isReadonly() )
-               {
-                       string tmp;
-                       for( unsigned int i = 0; i < insetkeys.size(); ++i ) {
-                               if (i > 0)
-                                       tmp += ", ";
-                               tmp += insetkeys[i];
-                       }
-                       setContents( tmp );
-                       setOptions( fl_get_input(form->textAftr) );
-                       // shouldn't mark the buffer dirty unless something
-                       // was actually altered
-                       holder.view->updateInset( this, true );
-               }
-               // fall through to Cancel
+       return label;
+}
 
-       } case CANCEL: {
-               fl_hide_form( form->form );
-               break;
 
-        } default:
-               break;
-       }
+void InsetCitation::setLoadingBuffer(Buffer const * buffer, bool state) const
+{
+       // Doesn't matter if there is no bv->buffer() entry in the map.
+       loading_buffer[buffer] = state;
 }
 
 
-void InsetCitation::setSize( FD_citation_form * form,
-                            int brsrHeight, bool bibPresent ) const
+void InsetCitation::edit(BufferView * bv, int, int, mouse_button::state)
 {
-       int const infoHeight  = 110;
-       int const otherHeight = 140;
-       brsrHeight = max( brsrHeight, 175 );
-       int formHeight = brsrHeight + otherHeight;
-
-       if( bibPresent ) formHeight += infoHeight + 30;
-       fl_set_form_size( form->form, 430, formHeight );
-
-       int ypos = 0;
-       fl_set_object_geometry( form->box,      0,   ypos, 430, formHeight );
-       ypos += 30;
-       fl_set_object_geometry( form->citeBrsr, 10,  ypos, 180, brsrHeight );
-       fl_set_object_geometry( form->bibBrsr,  240, ypos, 180, brsrHeight );
-       fl_set_object_geometry( form->addBtn,   200, ypos,  30, 30 );
-       ypos += 35;
-       fl_set_object_geometry( form->delBtn,   200, ypos,  30, 30 );
-       ypos += 35;
-       fl_set_object_geometry( form->upBtn,    200, ypos,  30, 30 );
-       ypos += 35;
-       fl_set_object_geometry( form->downBtn,  200, ypos,  30, 30 );
-
-       ypos = brsrHeight+30; // base of Citation/Bibliography browsers
-
-       if( bibPresent ) {
-               ypos += 30;
-               fl_set_object_geometry( form->infoBrsr, 10, ypos, 410, infoHeight );
-               fl_show_object( form->infoBrsr );
-               ypos += infoHeight;
-       }
-       else
-               fl_hide_object( form->infoBrsr );
+       // A call to edit() indicates that we're no longer loading the
+       // buffer but doing some real work.
+       setLoadingBuffer(bv->buffer(), false);
 
-       ypos += 20;
-       fl_set_object_geometry( form->textAftr, 100, ypos,   250, 30 );
-       fl_set_object_geometry( form->ok,       230, ypos+50, 90, 30 );
-       fl_set_object_geometry( form->cancel,   330, ypos+50, 90, 30 );
+       InsetCommandMailer mailer("citation", *this);
+       mailer.showDialog(bv);
 }
 
 
-void InsetCitation::setBibButtons( FD_citation_form * form, State status ) const
+void InsetCitation::edit(BufferView * bv, bool)
 {
-       switch (status) {
-       case ON:
-        {
-               fl_activate_object( form->addBtn );
-               fl_set_object_lcol( form->addBtn, FL_BLACK );
-
-               break;
-       }
-       case OFF:
-       {
-               fl_deactivate_object( form->addBtn );
-               fl_set_object_lcol( form->addBtn, FL_INACTIVE );
-       }
-       default:
-               break;
-       }
+       edit(bv, 0, 0, mouse_button::none);
 }
 
 
-void InsetCitation::setCiteButtons( FD_citation_form * form, State status ) const
+int InsetCitation::ascii(Buffer const * buffer, ostream & os, int) const
 {
-       switch( status ) {
-       case ON:
-        {
-               fl_activate_object( form->delBtn );
-               fl_set_object_lcol( form->delBtn, FL_BLACK );
+       string label;
 
-               int sel = fl_get_browser( form->citeBrsr );
+       if (cache.params == params() && cache.style == getStyle(buffer))
+               label = cache.generated_label;
+       else
+               label = generateLabel(buffer);
 
-               if( sel != 1 ) {
-                       fl_activate_object( form->upBtn );
-                       fl_set_object_lcol( form->upBtn, FL_BLACK );
-               } else {
-                       fl_deactivate_object( form->upBtn );
-                       fl_set_object_lcol( form->upBtn, FL_INACTIVE );
-               }
+       os << label;
+       return 0;
+}
 
-               if( sel != fl_get_browser_maxline(form->citeBrsr)) {
-                       fl_activate_object( form->downBtn );
-                       fl_set_object_lcol( form->downBtn, FL_BLACK );
-               } else {
-                       fl_deactivate_object( form->downBtn );
-                       fl_set_object_lcol( form->downBtn, FL_INACTIVE );
-               }
 
-               break;
+// Have to overwrite the default InsetCommand method in order to check that
+// the \cite command is valid. Eg, the user has natbib enabled, inputs some
+// citations and then changes his mind, turning natbib support off. The output
+// should revert to \cite[]{}
+int InsetCitation::latex(Buffer const * buffer, ostream & os,
+                       bool /*fragile*/, bool/*fs*/) const
+{
+       os << "\\";
+       if (buffer->params.use_natbib)
+               os << getCmdName();
+       else
+               os << "cite";
+
+#warning What is this code supposed to do? (Lgb)
+// my guess is that this is just waiting for when we support before,
+// so it's a oneliner. But this is very silly ! - jbl
+
+#if 1
+       // The current strange code
+
+       string const before = string();
+       string const after  = getOptions();
+       if (!before.empty() && buffer->params.use_natbib)
+               os << '[' << before << "][" << after << ']';
+       else if (!after.empty())
+               os << '[' << after << ']';
+#else
+       // and the cleaned up equvalent, should it just be changed? (Lgb)
+       string const after  = getOptions();
+       if (!after.empty())
+               os << '[' << after << ']';
+#endif
+       string::const_iterator it  = getContents().begin();
+       string::const_iterator end = getContents().end();
+       // Paranoia check: make sure that there is no whitespace in here
+       string content;
+       char last = ',';
+       for (; it != end; ++it) {
+               if (*it != ' ')
+                       last = *it;
+               if (*it != ' ' || last != ',')
+                       content += *it;
        }
-       case OFF:
-       {
-               fl_deactivate_object( form->delBtn );
-               fl_set_object_lcol( form->delBtn, FL_INACTIVE );
 
-               fl_deactivate_object( form->upBtn );
-               fl_set_object_lcol( form->upBtn, FL_INACTIVE );
+       os << '{' << content << '}';
 
-               fl_deactivate_object( form->downBtn );
-               fl_set_object_lcol( form->downBtn, FL_INACTIVE );
-       }
-       default:
-               break;
-       }
+       return 0;
 }
 
 
-string InsetCitation::getScreenLabel() const
+void InsetCitation::validate(LaTeXFeatures & features) const
 {
-       string temp("[");
-
-       temp += getContents();
-
-       if( !getOptions().empty() ) {
-               temp += ", " + getOptions();
-       }
-
-       return temp + ']';
+       if (features.bufferParams().use_natbib)
+               features.require("natbib");
 }