]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/FormRef.C
Introduce LFUN_PRINT.
[lyx.git] / src / frontends / xforms / FormRef.C
index 0e3617db9d443cc6bfc4e426677b7ad2b170320d..0fe2db21726f9dfe1561e9710d7335090b200906 100644 (file)
@@ -1,60 +1,84 @@
 /**
  * \file FormRef.C
- * Copyright 2000-2001 The LyX Team.
- * See the file COPYING.
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- * \author Angus Leeming, a.leeming@ic.ac.uk
+ * \author Angus Leeming
+ *
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
-#include <algorithm>
-
-#ifdef __GNUG__
-#pragma implementation
-#endif
 
-#include "xformsBC.h"
-#include "ControlRef.h"
 #include "FormRef.h"
-#include "form_ref.h"
+#include "ControlRef.h"
+#include "forms/form_ref.h"
+
+#include "Tooltips.h"
 #include "xforms_helpers.h"
+#include "xformsBC.h"
+
 #include "insets/insetref.h"
-#include "helper_funcs.h" // getStringFromVector
-#include "support/lstrings.h" // frontStrip, strip
+
+#include "support/lstrings.h" // trim
+
+#include "lyx_forms.h"
+
+using lyx::support::getStringFromVector;
 
 using std::find;
 using std::max;
 using std::sort;
+using std::string;
 using std::vector;
 
-typedef FormCB<ControlRef, FormDB<FD_form_ref> > base_class;
 
-FormRef::FormRef(ControlRef & c)
-       : base_class(c, _("Reference")),
+typedef FormController<ControlRef, FormView<FD_ref> > base_class;
+
+FormRef::FormRef(Dialog & parent)
+       : base_class(parent, _("Cross-reference")),
          at_ref_(false)
 {}
 
 
 void FormRef::build()
 {
-       dialog_.reset(build_ref());
+       dialog_.reset(build_ref(this));
 
-       for (int i = 0; !InsetRef::types[i].latex_name.empty(); ++i)
-               fl_addto_choice(dialog_->choice_type,
-                               _(InsetRef::types[i].gui_name.c_str()));
+       for (int i = 0; !InsetRef::types[i].latex_name.empty(); ++i)
+               fl_addto_choice(dialog_->choice_format,
+                               _(InsetRef::types[i].gui_name).c_str());
 
        // Force the user to use the browser to change refs.
        fl_deactivate_object(dialog_->input_ref);
 
-       // 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);
+       fl_set_input_return(dialog_->input_name, FL_RETURN_CHANGED);
+       fl_set_input_return(dialog_->input_ref,  FL_RETURN_CHANGED);
+
+       setPrehandler(dialog_->input_name);
+       setPrehandler(dialog_->input_ref);
+
+       // Manage the ok and cancel/close buttons
+       bcview().setOK(dialog_->button_ok);
+       bcview().setApply(dialog_->button_apply);
+       bcview().setCancel(dialog_->button_close);
+       bcview().setRestore(dialog_->button_restore);
+
+       bcview().addReadOnly(dialog_->button_update);
+       bcview().addReadOnly(dialog_->input_name);
+       bcview().addReadOnly(dialog_->input_ref);
+
+       // set up the tooltips
+       string str = _("Select a document for references.");
+       tooltips().init(dialog_->choice_document, str);
+       str = _("Sort the references alphabetically.");
+       tooltips().init(dialog_->check_sort, str);
+       str = _("Go to selected reference.");
+       tooltips().init(dialog_->button_go, str);
+       str = _("Update the list of references.");
+       tooltips().init(dialog_->button_update, str);
+       str = _("Select format style of the reference.");
+       tooltips().init(dialog_->choice_format, str);
 }
 
 
@@ -64,64 +88,90 @@ void FormRef::update()
                     controller().params().getContents().c_str());
        fl_set_input(dialog_->input_name,
                     controller().params().getOptions().c_str());
-       fl_set_choice(dialog_->choice_type, 
+       fl_set_choice(dialog_->choice_format,
                      InsetRef::getType(controller().params().getCmdName()) + 1);
 
        at_ref_ = false;
-       fl_set_object_label(dialog_->button_go, _("Go to reference"));
+       switch_go_button();
 
        // Name is irrelevant to LaTeX/Literate documents
-       if (controller().docType() == ControlRef::LATEX ||
-           controller().docType() == ControlRef::LITERATE) {
+       Kernel::DocTypes doctype = kernel().docType();
+       if (doctype == Kernel::LATEX || doctype == Kernel::LITERATE) {
                setEnabled(dialog_->input_name, false);
        } else {
                setEnabled(dialog_->input_name, true);
        }
 
        // 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);
+       if (doctype == Kernel::LINUXDOC || doctype == Kernel::DOCBOOK) {
+               fl_set_choice(dialog_->choice_format, 1);
+               setEnabled(dialog_->choice_format, false);
        } else {
-               setEnabled(dialog_->choice_type, true);
+               setEnabled(dialog_->choice_format, true);
        }
 
        // Get the available buffers
        vector<string> const buffers = controller().getBufferList();
-       vector<string> const choice_buffers =
-               getVectorFromChoice(dialog_->choice_buffer);
-       
+       vector<string> const choice_documents =
+               getVector(dialog_->choice_document);
+
        // If different from the current contents of the choice, then update it
-       if (buffers != choice_buffers) {
+       if (buffers != choice_documents) {
                // 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());
+                       ' ' + getStringFromVector(buffers, " | ") + ' ';
 
-               fl_set_choice(dialog_->choice_buffer,
-                             controller().getBufferNum() + 1);
+               fl_clear_choice(dialog_->choice_document);
+               fl_addto_choice(dialog_->choice_document, choice.c_str());
        }
 
-       refs_ = controller().getLabelList(string());
+       fl_set_choice(dialog_->choice_document,
+                     controller().getBufferNum() + 1);
+
+       string const name = controller().
+               getBufferName(fl_get_choice(dialog_->choice_document) - 1);
+       refs_ = controller().getLabelList(name);
+
        updateBrowser(refs_);
 }
 
 
+namespace {
+
+void updateHighlight(FL_OBJECT * browser,
+                    vector<string> const & keys,
+                    string const & ref)
+{
+       vector<string>::const_iterator cit = (ref.empty())
+               ? keys.end()
+               : find(keys.begin(), keys.end(), ref);
+
+       if (cit == keys.end()) {
+               fl_deselect_browser(browser);
+       } else {
+               int const i = static_cast<int>(cit - keys.begin());
+               fl_set_browser_topline(browser, max(i-5, 1));
+               fl_select_browser_line(browser, i+1);
+       }
+}
+
+} // namespace anon
+
+
 void FormRef::updateBrowser(vector<string> const & akeys) const
 {
        vector<string> keys(akeys);
        if (fl_get_button(dialog_->check_sort))
                sort(keys.begin(), keys.end());
 
-       vector<string> browser_keys =
-               getVectorFromBrowser(dialog_->browser_refs);
+       vector<string> browser_keys = getVector(dialog_->browser_refs);
 
-       if (browser_keys == keys)
+       if (browser_keys == keys) {
+               updateHighlight(dialog_->browser_refs, keys,
+                               getString(dialog_->input_ref));
                return;
+       }
 
        fl_clear_browser(dialog_->browser_refs);
        for (vector<string>::const_iterator it = keys.begin();
@@ -130,8 +180,8 @@ void FormRef::updateBrowser(vector<string> const & akeys) const
 
        if (keys.empty()) {
                fl_add_browser_line(dialog_->browser_refs,
-                                   _("*** No labels found in document ***"));
-       
+                                   _("*** No labels found in document ***").c_str());
+
                setEnabled(dialog_->browser_refs, false);
                setEnabled(dialog_->check_sort,   false);
 
@@ -140,31 +190,19 @@ void FormRef::updateBrowser(vector<string> const & akeys) const
                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);
-               }
+               updateHighlight(dialog_->browser_refs, keys,
+                               getString(dialog_->input_ref));
        }
 }
 
 
 void FormRef::apply()
 {
-       int const type = fl_get_choice(dialog_->choice_type) - 1;
+       int const type = fl_get_choice(dialog_->choice_format) - 1;
        controller().params().setCmdName(InsetRef::getName(type));
 
-       controller().params().setOptions(fl_get_input(dialog_->input_name));
-       controller().params().setContents(fl_get_input(dialog_->input_ref));
+       controller().params().setOptions(getString(dialog_->input_name));
+       controller().params().setContents(getString(dialog_->input_ref));
 }
 
 
@@ -180,13 +218,11 @@ ButtonPolicy::SMInput FormRef::input(FL_OBJECT * ob, long)
 
                at_ref_ = !at_ref_;
                if (at_ref_) {
-                       controller().gotoRef(fl_get_input(dialog_->input_ref));
-                       fl_set_object_label(dialog_->button_go, _("Go back"));
+                       controller().gotoRef(getString(dialog_->input_ref));
                } else {
                        controller().gotoBookmark();
-                       fl_set_object_label(dialog_->button_go,
-                                           _("Go to reference"));
                }
+               switch_go_button();
 
        } else if (ob == dialog_->browser_refs) {
 
@@ -194,7 +230,7 @@ ButtonPolicy::SMInput FormRef::input(FL_OBJECT * ob, long)
                if (sel < 1 || sel > refs_.size())
                        return ButtonPolicy::SMI_NOOP;
 
-               if (!controller().isReadonly()) {
+               if (!kernel().isBufferReadonly()) {
                        string s = fl_get_browser_line(dialog_->browser_refs, sel);
                        fl_set_input(dialog_->input_ref, s.c_str());
                }
@@ -202,20 +238,23 @@ ButtonPolicy::SMInput FormRef::input(FL_OBJECT * ob, long)
                if (at_ref_)
                        controller().gotoBookmark();
                at_ref_ = false;
-               fl_set_object_label(dialog_->button_go, _("Go to reference"));
+               switch_go_button();
 
-               setEnabled(dialog_->choice_type,      true);
+               setEnabled(dialog_->choice_format, true);
                setEnabled(dialog_->button_go, true);
                fl_set_object_lcol(dialog_->input_ref, FL_BLACK);
 
-       } else if (ob == dialog_->button_update || 
+       } else if (ob == dialog_->button_update ||
                   ob == dialog_->check_sort ||
-                  ob == dialog_->choice_buffer) {
+                  ob == dialog_->choice_document) {
+
+               // No change to data
+               activate = ButtonPolicy::SMI_NOOP;
 
                if (ob == dialog_->button_update ||
-                   ob == dialog_->choice_buffer) {
+                   ob == dialog_->choice_document) {
                        string const name =
-                               controller().getBufferName(fl_get_choice(dialog_->choice_buffer) - 1);
+                               controller().getBufferName(fl_get_choice(dialog_->choice_document) - 1);
                        refs_ = controller().getLabelList(name);
                }
 
@@ -223,9 +262,9 @@ ButtonPolicy::SMInput FormRef::input(FL_OBJECT * ob, long)
                updateBrowser(refs_);
                fl_unfreeze_form(form());
 
-       } else if (ob == dialog_->choice_type) {
+       } else if (ob == dialog_->choice_format) {
 
-               int const type = fl_get_choice(dialog_->choice_type) - 1;
+               int const type = fl_get_choice(dialog_->choice_format) - 1;
                if (controller().params().getCmdName() ==
                    InsetRef::getName(type)) {
                        activate = ButtonPolicy::SMI_NOOP;
@@ -234,3 +273,17 @@ ButtonPolicy::SMInput FormRef::input(FL_OBJECT * ob, long)
 
        return activate;
 }
+
+
+void FormRef::switch_go_button()
+{
+       if (at_ref_) {
+               fl_set_object_label(dialog_->button_go, _("Go back").c_str());
+               tooltips().init(dialog_->button_go, _("Go back to original place.").c_str());
+       } else {
+               fl_set_object_label(dialog_->button_go, _("Go to").c_str());
+               tooltips().init(dialog_->button_go, _("Go to selected reference.").c_str());
+       }
+       fl_set_button_shortcut(dialog_->button_go, "#G", 1);
+       fl_show_object(dialog_->button_go);
+}