]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetcite.C
Don't remove cell selections after fontchange.
[lyx.git] / src / insets / insetcite.C
index 3e657546d368b44643a86c024334fed8f5cad2b8..6eedb6df8ccf46d8bf539f5fe5370078359b374f 100644 (file)
-#include <config.h>
+/* This file is part of*
+ * ====================================================== 
+ *
+ *           LyX, The Document Processor
+ *      
+ *           Copyright 2000-2001 The LyX Team.
+ * 
+ * ====================================================== */
 
-#include <fstream>
-#include <cstdlib>
-#include <algorithm>
+#include <config.h>
 
 #ifdef __GNUG__
 #pragma implementation
 #endif
 
-#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"
+#include "LyXView.h"
 
-using std::getline;
-using std::vector;
-using std::pair;
-using std::max;
-using std::min;
-using std::find;
+#include "frontends/Dialogs.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;
 
-extern "C" void citation_cb( FL_OBJECT *, long data )
-{
-       InsetCitation::Holder * holder =
-               static_cast<InsetCitation::Holder*>(citation_form->form->u_vdata);
+using std::ostream;
 
-       holder->inset->callback( citation_form,
-                                static_cast<InsetCitation::State>(data) );
-}
 
+InsetCitation::InsetCitation(InsetCommandParams const & p, bool)
+       : InsetCommand(p)
+{}
 
-FD_citation_form * create_form_citation_form(void)
+string const InsetCitation::getScreenLabel(Buffer const *) const
 {
-       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)
-{
-}
-
-
-InsetCitation::~InsetCitation()
-{
-       if(citation_form && citation_form->form
-          && citation_form->form->visible
-          && citation_form->form->u_vdata == &holder)
-               fl_hide_form(citation_form->form);
-}
-
-
-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 );
-       }
-
-       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, ',') );
-       }
-
-       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 );
+       string keys(getContents());
+
+       // If keys is "too long" then only print out the first few tokens
+       string label;
+       if (contains(keys, ",")) {
+               // Final comma allows while loop to cover all keys
+               keys = frontStrip(split(keys, label, ',')) + ",";
+
+               string::size_type const maxSize = 40;
+               while (contains(keys, ",")) {
+                       string key;
+                       keys = frontStrip(split(keys, key, ','));
+
+                       string::size_type size = label.size() + 2 + key.size();
+                       if (size >= maxSize) {
+                               label += ", ...";
+                               break;
+                       }
+                       label += ", " + key;
+               }
        } else {
-               fl_show_form(citation_form->form,
-                            FL_PLACE_MOUSE | FL_FREE_SIZE,
-                            FL_FULLBORDER,
-                            _("Citation") );
+               label = keys;
        }
-}
 
+       if (!getOptions().empty())
+               label += ", " + getOptions();
 
-void InsetCitation::updateBrowser( FL_OBJECT * browser,
-                                  vector<string> const & inkeys ) const
-{
-       fl_clear_browser( browser );
-
-       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 );
+       return "[" + label + "]";
 }
 
-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 );
-               }
-
-               if( !holder.view->buffer()->isReadonly() ) {
-                       if( it != insetkeys.end() ) {
-                               setBibButtons(  form, OFF );
-                               setCiteButtons( form, ON );
-                       } else {
-                               setBibButtons(  form, ON );
-                               setCiteButtons( form, OFF );
-                       }
-               }
-               break;
-
-       } case CITEBRSR: {
-               unsigned int sel = fl_get_browser( form->citeBrsr );
-               if( sel < 1 || sel > insetkeys.size() ) break;
-
-               if( !holder.view->buffer()->isReadonly() ) {
-                       setBibButtons(  form, OFF );
-                       setCiteButtons( form, ON );
-               }
-
-               // Highlight the selected citeBrsr key in bibBrsr
-               vector<string>::iterator it =
-                       find( bibkeys.begin(), bibkeys.end(), insetkeys[sel-1] );
-
-               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() );
-               }
-               break;
-
-       } case ADD: {
-               if( holder.view->buffer()->isReadonly() ) break;
-
-               unsigned int sel = fl_get_browser( form->bibBrsr );
-               if( sel < 1 || sel > bibkeys.size() ) break;
-
-               // Add the selected bibBrsr key to citeBrsr
-               fl_addto_browser( form->citeBrsr,
-                                 bibkeys[sel-1].c_str() );
-               insetkeys.push_back( bibkeys[sel-1] );
-
-               int n = insetkeys.size();
-               fl_select_browser_line( form->citeBrsr, n );
-
-               setBibButtons(  form, OFF );
-               setCiteButtons( form, ON );
-
-               break;
-
-       } case DELETE: {
-               if( holder.view->buffer()->isReadonly() ) break;
-
-               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 );
-
-               setBibButtons(  form, ON );
-               setCiteButtons( form, OFF );
-               break;
-
-       } case UP: {
-               if( holder.view->buffer()->isReadonly() ) break;
-
-               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;
-
-               fl_delete_browser_line( form->citeBrsr, sel );
-               insetkeys.erase( it );
-
-               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 );
-
-               break;
-
-       } case DOWN: {
-               if( holder.view->buffer()->isReadonly() ) break;
-
-               unsigned int sel = fl_get_browser( form->citeBrsr );
-               if( sel < 1 || sel > insetkeys.size()-1 ) break;
-
-               // Move the selected key down one line
-               vector<string>::iterator it = insetkeys.begin() + sel-1;
-               string tmp = *it;
-
-               fl_delete_browser_line( form->citeBrsr, sel );
-               insetkeys.erase( it );
-
-               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 );
-
-               break;
-
-       } 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
-
-       } case CANCEL: {
-               fl_hide_form( form->form );
-               break;
-
-        } default:
-               break;
-       }
+void InsetCitation::edit(BufferView * bv, int, int, unsigned int)
+{
+       bv->owner()->getDialogs()->showCitation(this);
 }
 
-
-void InsetCitation::setSize( FD_citation_form * form,
-                            int brsrHeight, bool bibPresent ) const
+void InsetCitation::edit(BufferView * bv, bool)
 {
-       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 );
-
-       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 );
+       edit(bv, 0, 0, 0);
 }
 
-
-void InsetCitation::setBibButtons( FD_citation_form * form, State status ) const
+int InsetCitation::ascii(Buffer const *, ostream & os, int) const
 {
-       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;
-       }
+        os << "[" << getContents() << "]";
+        return 0;
 }
 
-
-void InsetCitation::setCiteButtons( FD_citation_form * form, State status ) const
+// 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
 {
-       switch( status ) {
-       case ON:
-        {
-               fl_activate_object( form->delBtn );
-               fl_set_object_lcol( form->delBtn, FL_BLACK );
-
-               int sel = fl_get_browser( form->citeBrsr );
-
-               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 << "\\";
+       if (buffer->params.use_natbib)
+               os << getCmdName();
+       else
+               os << "cite";
 
-               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 );
-               }
+       if (!getOptions().empty())
+               os << "[" << getOptions() << "]";
 
-               break;
+       // Paranoia check: make sure that there is no whitespace in here
+       string content;
+       for (string::const_iterator it = getContents().begin();
+            it != getContents().end(); ++it) {
+               if (*it != ' ') 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");
 }