]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/FormRef.C
try this for distinguishing inner and outer tabs
[lyx.git] / src / frontends / xforms / FormRef.C
index 39a02f634175288fef6139c2a7bfd82c0dc8cb5a..4cb2bcf60e86867521c3e98f207326d7b351fe0a 100644 (file)
 #include "buffer.h"
 #include "form_ref.h"
 #include "lyxfunc.h"
+#include "insets/insetref.h"
 
 #include <algorithm>
 
+using std::find;
+using std::max;
 using std::sort;
 using std::vector;
 
-static int const minw_hb = 250;
-static int minw_sb;
-
 FormRef::FormRef(LyXView * lv, Dialogs * d)
-       : FormCommand(lv, d, _("Reference"), new OkCancelReadOnlyPolicy),
+       : FormCommand(lv, d, _("Reference"), new NoRepeatedApplyPolicy),
          toggle(GOBACK), dialog_(0)
 {
        // let the dialog be shown
@@ -58,13 +58,6 @@ FL_FORM * FormRef::form() const
 }
 
 
-void FormRef::connect()
-{
-       fl_set_form_maxsize(form(), 2 * minw_, minh_);
-       FormCommand::connect();
-}
-       
-
 void FormRef::disconnect()
 {
        refs.clear();
@@ -76,51 +69,63 @@ void FormRef::build()
 {
        dialog_ = build_ref();
 
-       fl_addto_choice(dialog_->type,
-                       _(" Ref | Page | TextRef | TextPage | PrettyRef "));
+       for (int i = 0; !InsetRef::types[i].latex_name.empty(); ++i)
+               fl_addto_choice(dialog_->type,
+                               _(InsetRef::types[i].gui_name.c_str()));
 
        // Workaround dumb xforms sizing bug
        minw_ = form()->w;
        minh_ = form()->h;
-       minw_sb = minw_;
 
-       // Name is irrelevant to LaTeX documents
-       if (lv_->buffer()->isLatex()) {
-               fl_deactivate_object(dialog_->name);
-               fl_set_object_lcol(dialog_->name, FL_INACTIVE);
-       }
-         
-       // Can change reference only through browser
+       // Force the user to use the browser to change refs.
        fl_deactivate_object(dialog_->ref);
 
         // Manage the ok and cancel/close buttons
        bc_.setOK(dialog_->button_ok);
+       bc_.setApply(dialog_->button_apply);
        bc_.setCancel(dialog_->button_cancel);
+       bc_.setUndoAll(dialog_->button_restore);
        bc_.refresh();
 
-       bc_.addReadOnly(dialog_->type);
+#warning I had to uncomment this so the buttons could be disabled in update() (dekel)
+       //bc_.addReadOnly(dialog_->type);
+       //bc_.addReadOnly(dialog_->name);
 }
 
 
 void FormRef::update()
 {
-       fl_set_input(dialog_->ref,  params.getContents().c_str());
-       fl_set_input(dialog_->name, params.getOptions().c_str());
+       if (inset_) {
+               fl_set_input(dialog_->ref,  params.getContents().c_str());
+               fl_set_input(dialog_->name, params.getOptions().c_str());
+               fl_set_choice(dialog_->type, 
+                             InsetRef::getType(params.getCmdName()) + 1);
+       }
 
-       Type type = getType();
-       fl_set_choice(dialog_->type, type + 1);
+       lv_->getLyXFunc()->Dispatch(LFUN_BOOKMARK_SAVE, "0");
 
        toggle = GOBACK;
        fl_set_object_label(dialog_->button_go, _("Goto reference"));
 
-       refs.clear();
-       if (inset_ == 0) {
-               refs = lv_->buffer()->getLabelList();
-               updateBrowser(refs);
-               showBrowser();
+       // Name is irrelevant to LaTeX/Literate documents, while
+       // type is irrelevant to LinuxDoc/DocBook.
+       if (lv_->buffer()->isLatex() || lv_->buffer()->isLatex()) {
+               fl_deactivate_object(dialog_->name);
+               fl_set_object_lcol(dialog_->name, FL_INACTIVE);
+               fl_activate_object(dialog_->type);
+               fl_set_object_lcol(dialog_->type, FL_BLACK);
        } else {
-               hideBrowser();
+               fl_set_choice(dialog_->type, 1);
+
+               fl_activate_object(dialog_->name);
+               fl_set_object_lcol(dialog_->name, FL_BLACK);
+               fl_deactivate_object(dialog_->type);
+               fl_set_object_lcol(dialog_->type, FL_INACTIVE);
        }
+
+       refs = lv_->buffer()->getLabelList();
+       updateBrowser(refs);
+
        bc_.readOnly(lv_->buffer()->isReadonly());
 }
 
@@ -128,7 +133,7 @@ void FormRef::update()
 void FormRef::updateBrowser(vector<string> const & akeys) const
 {
        vector<string> keys(akeys);
-       if (fl_get_button( dialog_->sort))
+       if (fl_get_button(dialog_->sort))
                sort(keys.begin(), keys.end());
 
        fl_clear_browser(dialog_->browser);
@@ -141,94 +146,29 @@ void FormRef::updateBrowser(vector<string> const & akeys) const
                                    _("*** No labels found in document ***"));
 
                fl_deactivate_object(dialog_->browser);
-               fl_deactivate_object(dialog_->button_update);
                fl_deactivate_object(dialog_->sort);
                fl_set_object_lcol(dialog_->browser, FL_INACTIVE);
-               fl_set_object_lcol(dialog_->button_update, FL_INACTIVE);
                fl_set_object_lcol(dialog_->sort, FL_INACTIVE);
+               fl_set_input(dialog_->ref, "");
        } else {
-               fl_set_browser_topline(dialog_->browser, 1);
                fl_activate_object(dialog_->browser);
                fl_set_object_lcol(dialog_->browser, FL_BLACK);
-               fl_activate_object(dialog_->button_update);
-               fl_set_object_lcol(dialog_->button_update, FL_BLACK);
                fl_activate_object(dialog_->sort);
                fl_set_object_lcol(dialog_->sort, FL_BLACK);
-       }
-}
-
-
-void FormRef::showBrowser() const
-{
-       fl_show_object(dialog_->browser);
-       fl_show_object(dialog_->button_update);
-       fl_show_object(dialog_->sort);
-
-       setSize(minw_sb, 0);
-
-       fl_deactivate_object(dialog_->type);
-       fl_set_object_lcol(dialog_->type, FL_INACTIVE);
-       fl_deactivate_object(dialog_->button_go);
-       fl_set_object_lcol(dialog_->button_go, FL_INACTIVE);
-       fl_set_object_lcol(dialog_->ref, FL_INACTIVE);
-       bc_.valid(false);
-}
-
 
-void FormRef::hideBrowser() const
-{
-       fl_hide_object(dialog_->browser);
-       fl_hide_object(dialog_->button_update);
-       fl_hide_object(dialog_->sort);
-
-       setSize(minw_hb, 280);
-
-       fl_activate_object(dialog_->type);
-       fl_set_object_lcol(dialog_->type, FL_BLACK);
-       fl_activate_object(dialog_->button_go);
-       fl_set_object_lcol(dialog_->button_go, FL_BLACK);
-       fl_set_object_lcol(dialog_->ref, FL_BLACK);
-       bc_.invalid();
-}
-
-
-void FormRef::setSize(int w, int dx) const
-{
-       static int x1 = dialog_->name->x;
-       static int y1 = dialog_->name->y;
-       static int x2 = dialog_->ref->x;
-       static int y2 = dialog_->ref->y;
-       static int x3 = dialog_->type->x;
-       static int y3 = dialog_->type->y;
-       static int x4 = dialog_->button_go->x;
-       static int y4 = dialog_->button_go->y;
-       static int x5 = dialog_->button_ok->x;
-       static int y5 = dialog_->button_ok->y;
-       static int x6 = dialog_->button_cancel->x;
-       static int y6 = dialog_->button_cancel->y;
-
-       if (form()->w != w) {
-               minw_ = w;
-               fl_set_form_size(form(), minw_, minh_);
-       } else
-               return;
-       
-       fl_set_object_position(dialog_->name,   x1 - dx, y1);
-       fl_set_object_position(dialog_->ref,    x2 - dx, y2);
-       fl_set_object_position(dialog_->type,   x3 - dx, y3);
-       fl_set_object_position(dialog_->button_go,     x4 - dx, y4);
-       fl_set_object_position(dialog_->button_ok,     x5 - dx, y5);
-       fl_set_object_position(dialog_->button_cancel, x6 - dx, y6);
-
-       // These two must be reset apparently
-       // Name is irrelevant to LaTeX documents
-       if (lv_->buffer()->isLatex()) {
-               fl_deactivate_object(dialog_->name);
-               fl_set_object_lcol(dialog_->name, FL_INACTIVE);
+               string ref = fl_get_input(dialog_->ref);
+               vector<string>::const_iterator cit =
+                       find(keys.begin(), keys.end(), ref);
+               if (cit == keys.end()) {
+                       cit = keys.begin();
+                       fl_set_input(dialog_->ref, (*cit).c_str());
+               } else if (ref.empty())
+                       fl_set_input(dialog_->ref, (*cit).c_str());
+
+               int const i = static_cast<int>(cit - keys.begin());
+               fl_set_browser_topline(dialog_->browser, max(i-5, 1));
+               fl_select_browser_line(dialog_->browser, i+1);
        }
-         
-       // Can change reference only through browser
-       fl_deactivate_object(dialog_->ref);
 }
 
 
@@ -237,10 +177,11 @@ void FormRef::apply()
        if (!lv_->view()->available())
                return;
 
-       Type const type = static_cast<Type>(fl_get_choice(dialog_->type) - 1);
-       params.setCmdName(getName(type));
+       int const type = fl_get_choice(dialog_->type) - 1;
+       params.setCmdName(InsetRef::getName(type));
 
        params.setOptions(fl_get_input(dialog_->name));
+       params.setContents(fl_get_input(dialog_->ref));
 
        if (inset_ != 0) {
                // Only update if contents have changed
@@ -255,11 +196,6 @@ void FormRef::apply()
 }
 
 
-#ifdef WITH_WARNINGS
-#warning check use of buttoncontroller
-// Seems okay except that goref and goback shouldn't
-// affect the status of ok.
-#endif
 bool FormRef::input(FL_OBJECT *, long data)
 {
        bool activate(true);
@@ -267,6 +203,9 @@ bool FormRef::input(FL_OBJECT *, long data)
        // goto reference / go back
        case 1:
        {
+               // No change to data
+               activate = false;
+               
                toggle = static_cast<Goto>(toggle + 1);
                if (toggle == GOFIRST ) toggle = GOREF;
        
@@ -275,14 +214,14 @@ bool FormRef::input(FL_OBJECT *, long data)
                {
                        lv_->getLyXFunc()->
                                Dispatch(LFUN_REF_GOTO,
-                                        params.getContents());
+                                        fl_get_input(dialog_->ref));
                        fl_set_object_label(dialog_->button_go, _("Go back"));
                }
                break;
 
                case GOBACK:
                {
-                       lv_->getLyXFunc()->Dispatch(LFUN_REF_BACK);
+                       lv_->getLyXFunc()->Dispatch(LFUN_BOOKMARK_GOTO, "0");
                        fl_set_object_label(dialog_->button_go,
                                            _("Goto reference"));
                }
@@ -300,12 +239,13 @@ bool FormRef::input(FL_OBJECT *, long data)
                unsigned int sel = fl_get_browser(dialog_->browser);
                if (sel < 1 || sel > refs.size()) break;
 
-               string s = fl_get_browser_line(dialog_->browser, sel);
-               fl_set_input(dialog_->ref, s.c_str());
-               params.setContents(s);
+               if (!lv_->buffer()->isReadonly()) {
+                       string s = fl_get_browser_line(dialog_->browser, sel);
+                       fl_set_input(dialog_->ref, s.c_str());
+               }
 
                toggle = GOBACK;
-               lv_->getLyXFunc()->Dispatch(LFUN_REF_BACK);
+               lv_->getLyXFunc()->Dispatch(LFUN_BOOKMARK_GOTO, "0");
                fl_set_object_label(dialog_->button_go, _("Goto reference"));
 
                fl_activate_object(dialog_->type);
@@ -318,20 +258,20 @@ bool FormRef::input(FL_OBJECT *, long data)
 
        // update or sort
        case 3:
-       {
+               refs = lv_->buffer()->getLabelList();
+
+               // fall through to...
+       case 4:
                fl_freeze_form(form());
                updateBrowser(refs);
                fl_unfreeze_form(form());
-       }
-       break;
+               break;
 
        // changed reference type
-       case 4:
+       case 5:
        {
-               Type type = static_cast<Type>( 
-                       fl_get_choice(dialog_->type) - 1);
-               if (params.getCmdName() == getName(type)
-                   && inset_) {
+               int const type = fl_get_choice(dialog_->type) - 1;
+               if (params.getCmdName() == InsetRef::getName(type) && inset_) {
                        activate = false;
                }
        }
@@ -340,54 +280,8 @@ bool FormRef::input(FL_OBJECT *, long data)
        default:
                break;
        }
-       return activate;
-}
-
-
-FormRef::Type FormRef::getType() const
-{
-       Type type;
 
-       if (params.getCmdName() == "ref" )
-               type = REF;
-
-       else if (params.getCmdName() == "pageref" )
-               type = PAGEREF;
-
-       else if (params.getCmdName() == "vref" )
-               type = VREF;
-
-       else if (params.getCmdName() == "vpageref" )
-               type = VPAGEREF;
-
-       else
-               type = PRETTYREF;
-       
-       return type;
+       return activate;
 }
 
 
-string const FormRef::getName(Type type) const
-{
-       string name;
-
-       switch (type) {
-       case REF:
-               name = "ref";
-               break;
-       case PAGEREF:
-               name = "pageref";
-               break;
-       case VREF:
-               name = "vref";
-               break;
-       case VPAGEREF:
-               name = "vpageref";
-               break;
-       case PRETTYREF:
-               name = "prettyref";
-               break;
-       }
-       
-       return name;
-}