]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/FormRef.C
Bugfixes: checkboxes to radiobuttons (from J�rgen S) and remove a little
[lyx.git] / src / frontends / xforms / FormRef.C
index f2058edb2c0bba647218dd8d8a44c52e9dfe4067..3dae84746825294d9d607bcbbf5c7ffbb5394cf1 100644 (file)
-// -*- C++ -*-
 /* This file is part of
  * ====================================================== 
  *
  *           LyX, The Document Processor
  *
- *           Copyright 2000 The LyX Team.
+ *           Copyright 2000-2001 The LyX Team.
  *
  * ======================================================
+ * 
+ * \file FormRef.C
+ * \author Angus Leeming, a.leeming@ic.ac.uk 
  */
 
 #include <config.h>
-
-#include FORMS_H_LOCATION
+#include <algorithm>
 
 #ifdef __GNUG__
 #pragma implementation
 #endif
 
-
-#include "Dialogs.h"
+#include "xformsBC.h"
+#include "ControlRef.h"
 #include "FormRef.h"
-#include "LyXView.h"
-#include "buffer.h"
 #include "form_ref.h"
-#include "lyxfunc.h"
-
-#include <algorithm>
+#include "xforms_helpers.h"
+#include "insets/insetref.h"
+#include "helper_funcs.h" // getStringFromVector
+#include "support/lstrings.h" // frontStrip, strip
 
+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),
-         toggle(GOBACK), dialog_(0)
-{
-       // let the dialog be shown
-       // These are permanent connections so we won't bother
-       // storing a copy because we won't be disconnecting.
-       d->showRef.connect(slot(this, &FormRef::showInset));
-       d->createRef.connect(slot(this, &FormRef::createInset));
-}
-
-
-FormRef::~FormRef()
-{
-       delete dialog_;
-}
-
-
-FL_FORM * FormRef::form() const
-{
-       if (dialog_) return dialog_->form;
-       return 0;
-}
+typedef FormCB<ControlRef, FormDB<FD_form_ref> > base_class;
 
-
-void FormRef::connect()
-{
-       fl_set_form_maxsize(form(), 2 * minw_, minh_);
-       FormCommand::connect();
-}
-       
-
-void FormRef::disconnect()
-{
-       refs.clear();
-       FormCommand::disconnect();
-}
+FormRef::FormRef(ControlRef & c)
+       : base_class(c, _("Reference")),
+         at_ref_(false)
+{}
 
 
 void FormRef::build()
 {
-       dialog_ = build_ref();
+       dialog_.reset(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_->choice_type,
+                               _(InsetRef::types[i].gui_name.c_str()));
 
-       // Workaround dumb xforms sizing bug
-       minw_ = form()->w;
-       minh_ = form()->h;
-       minw_sb = minw_;
+       // Force the user to use the browser to change refs.
+       fl_deactivate_object(dialog_->input_ref);
 
-       // 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
-       fl_deactivate_object(dialog_->ref);
-
-       bc_.setOK(dialog_->button_ok);
-       bc_.setCancel(dialog_->button_cancel);
-       bc_.addReadOnly(dialog_->type);
-       bc_.refresh();
+       // Manage the ok and cancel/close buttons
+       bc().setOK(dialog_->button_ok);
+       bc().setApply(dialog_->button_apply);
+       bc().setCancel(dialog_->button_cancel);
+       bc().setRestore(dialog_->button_restore);
+
+       bc().addReadOnly(dialog_->button_update);
+       bc().addReadOnly(dialog_->input_name);
+       bc().addReadOnly(dialog_->input_ref);
 }
 
 
 void FormRef::update()
 {
-       fl_set_input(dialog_->ref,  params.getContents().c_str());
-       fl_set_input(dialog_->name, params.getOptions().c_str());
-
-       Type type = getType();
-       fl_set_choice(dialog_->type, type + 1);
-
-       toggle = GOBACK;
-       fl_set_object_label(dialog_->button_go, _("Goto reference"));
+       fl_set_input(dialog_->input_ref,
+                    controller().params().getContents().c_str());
+       fl_set_input(dialog_->input_name,
+                    controller().params().getOptions().c_str());
+       fl_set_choice(dialog_->choice_type, 
+                     InsetRef::getType(controller().params().getCmdName()) + 1);
+
+       at_ref_ = false;
+       fl_set_object_label(dialog_->button_go, _("Go to reference"));
+
+       // Name is irrelevant to LaTeX/Literate documents
+       if (controller().docType() == ControlRef::LATEX ||
+           controller().docType() == ControlRef::LITERATE) {
+               setEnabled(dialog_->input_name, false);
+       } else {
+               setEnabled(dialog_->input_name, true);
+       }
 
-       refs.clear();
-       if (inset_ == 0) {
-               refs = lv_->buffer()->getLabelList();
-               updateBrowser(refs);
-               showBrowser();
+       // type is irrelevant to LinuxDoc/DocBook.
+       if (controller().docType() == ControlRef::LINUXDOC ||
+           controller().docType() == ControlRef::DOCBOOK) {
+               fl_set_choice(dialog_->choice_type, 1);
+               setEnabled(dialog_->choice_type, false);
        } else {
-               hideBrowser();
+               setEnabled(dialog_->choice_type, true);
        }
-       bc_.readOnly(lv_->buffer()->isReadonly());
+
+       // Get the available buffers
+       vector<string> const buffers = controller().getBufferList();
+       vector<string> const choice_buffers =
+               getVectorFromChoice(dialog_->choice_buffer);
+       
+       // If different from the current contents of the choice, then update it
+       if (buffers != choice_buffers) {
+               // create a string of entries " entry1 | entry2 | entry3 "
+               // with which to initialise the xforms choice object.
+               string const choice =
+                       " " + getStringFromVector(buffers, " | ") + " ";
+
+               fl_clear_choice(dialog_->choice_buffer);
+               fl_addto_choice(dialog_->choice_buffer, choice.c_str());
+
+               fl_set_choice(dialog_->choice_buffer,
+                             controller().getBufferNum() + 1);
+       }
+
+       refs_ = controller().getLabelList(string());
+       updateBrowser(refs_);
 }
 
 
 void FormRef::updateBrowser(vector<string> const & akeys) const
 {
        vector<string> keys(akeys);
-       if (fl_get_button( dialog_->sort))
+       if (fl_get_button(dialog_->check_sort))
                sort(keys.begin(), keys.end());
 
-       fl_clear_browser(dialog_->browser);
+       vector<string> browser_keys =
+               getVectorFromBrowser(dialog_->browser_refs);
+
+       if (browser_keys == keys)
+               return;
+
+       fl_clear_browser(dialog_->browser_refs);
        for (vector<string>::const_iterator it = keys.begin();
             it != keys.end(); ++it)
-               fl_add_browser_line(dialog_->browser, (*it).c_str());
+               fl_add_browser_line(dialog_->browser_refs, it->c_str());
 
        if (keys.empty()) {
-               fl_add_browser_line(dialog_->browser,
+               fl_add_browser_line(dialog_->browser_refs,
                                    _("*** No labels found in document ***"));
+       
+               setEnabled(dialog_->browser_refs, false);
+               setEnabled(dialog_->check_sort,   false);
 
-               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_->input_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);
+               setEnabled(dialog_->browser_refs, true);
+               setEnabled(dialog_->check_sort,   true);
+
+               string ref = fl_get_input(dialog_->input_ref);
+               vector<string>::const_iterator cit = (ref.empty())
+                       ? keys.begin()
+                       : find(keys.begin(), keys.end(), ref);
+               if (cit == keys.end()) {
+                       fl_deselect_browser(dialog_->browser_refs);
+               } else {
+                       if (ref.empty())
+                               fl_set_input(dialog_->input_ref, cit->c_str());
+
+                       int const i = static_cast<int>(cit - keys.begin());
+                       fl_set_browser_topline(dialog_->browser_refs, max(i-5, 1));
+                       fl_select_browser_line(dialog_->browser_refs, i+1);
+               }
        }
 }
 
 
-void FormRef::showBrowser() const
+void FormRef::apply()
 {
-       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);
-}
-
+       int const type = fl_get_choice(dialog_->choice_type) - 1;
+       controller().params().setCmdName(InsetRef::getName(type));
 
-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();
+       controller().params().setOptions(fl_get_input(dialog_->input_name));
+       controller().params().setContents(fl_get_input(dialog_->input_ref));
 }
 
 
-void FormRef::setSize(int w, int dx) const
+ButtonPolicy::SMInput FormRef::input(FL_OBJECT * ob, long)
 {
-       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);
-       }
-         
-       // Can change reference only through browser
-       fl_deactivate_object(dialog_->ref);
-}
+       ButtonPolicy::SMInput activate(ButtonPolicy::SMI_VALID);
 
+       if (ob == dialog_->button_go) {
+               // goto reference / go back
 
-void FormRef::apply()
-{
-       if (!lv_->view()->available())
-               return;
+               // No change to data
+               activate = ButtonPolicy::SMI_NOOP;
 
-       Type const type = static_cast<Type>(fl_get_choice(dialog_->type) - 1);
-       params.setCmdName(getName(type));
+               at_ref_ = !at_ref_;
+               if (at_ref_) {
+                       controller().gotoRef(fl_get_input(dialog_->input_ref));
+                       fl_set_object_label(dialog_->button_go, _("Go back"));
+               } else {
+                       controller().gotoBookmark();
+                       fl_set_object_label(dialog_->button_go,
+                                           _("Go to reference"));
+               }
 
-       params.setOptions(fl_get_input(dialog_->name));
+       } else if (ob == dialog_->browser_refs) {
 
-       if (inset_ != 0) {
-               // Only update if contents have changed
-               if (params != inset_->params()) {
-                       inset_->setParams(params);
-                       lv_->view()->updateInset(inset_, true);
+               unsigned int sel = fl_get_browser(dialog_->browser_refs);
+               if (sel < 1 || sel > refs_.size())
+                       return ButtonPolicy::SMI_NOOP;
+
+               if (!controller().isReadonly()) {
+                       string s = fl_get_browser_line(dialog_->browser_refs, sel);
+                       fl_set_input(dialog_->input_ref, s.c_str());
                }
-       } else {
-               lv_->getLyXFunc()->Dispatch(LFUN_REF_INSERT,
-                                           params.getAsString());
-       }
-}
 
+               if (at_ref_)
+                       controller().gotoBookmark();
+               at_ref_ = false;
+               fl_set_object_label(dialog_->button_go, _("Go to reference"));
 
-#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);
-       switch (data) {
-       // goto reference / go back
-       case 1:
-       {
-               toggle = static_cast<Goto>(toggle + 1);
-               if (toggle == GOFIRST ) toggle = GOREF;
-       
-               switch (toggle) {
-               case GOREF:
-               {
-                       lv_->getLyXFunc()->
-                               Dispatch(LFUN_REF_GOTO,
-                                        params.getContents());
-                       fl_set_object_label(dialog_->button_go, _("Go back"));
-               }
-               break;
+               setEnabled(dialog_->choice_type,      true);
+               setEnabled(dialog_->button_go, true);
+               fl_set_object_lcol(dialog_->input_ref, FL_BLACK);
 
-               case GOBACK:
-               {
-                       lv_->getLyXFunc()->Dispatch(LFUN_REF_BACK);
-                       fl_set_object_label(dialog_->button_go,
-                                           _("Goto reference"));
-               }
-               break;
+       } else if (ob == dialog_->button_update || 
+                  ob == dialog_->check_sort ||
+                  ob == dialog_->choice_buffer) {
 
-               default:
-                       break;
+               if (ob == dialog_->button_update ||
+                   ob == dialog_->choice_buffer) {
+                       string const name =
+                               frontStrip(strip(fl_get_choice_text(dialog_->choice_buffer)));
+                       refs_ = controller().getLabelList(name);
                }
-       }
-       break;
-
-       // choose browser key
-       case 2:
-       {
-               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);
-
-               toggle = GOBACK;
-               lv_->getLyXFunc()->Dispatch(LFUN_REF_BACK);
-               fl_set_object_label(dialog_->button_go, _("Goto reference"));
-
-               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);
-       }
-       break;
 
-       // update or sort
-       case 3:
-       {
                fl_freeze_form(form());
-               updateBrowser(refs);
+               updateBrowser(refs_);
                fl_unfreeze_form(form());
-       }
-       break;
-
-       // changed reference type
-       case 4:
-       {
-               Type type = static_cast<Type>( 
-                       fl_get_choice(dialog_->type) - 1);
-               if (params.getCmdName() == getName(type)
-                   && inset_) {
-                       activate = false;
+
+       } else if (ob == dialog_->choice_type) {
+
+               int const type = fl_get_choice(dialog_->choice_type) - 1;
+               if (controller().params().getCmdName() ==
+                   InsetRef::getName(type)) {
+                       activate = ButtonPolicy::SMI_NOOP;
                }
        }
-       break;
 
-       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;
-}
-
-
-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;
-}