3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS
16 #include "ControlRef.h"
19 #include "forms/form_ref.h"
20 #include "xforms_helpers.h"
21 #include "insets/insetref.h"
22 #include "helper_funcs.h" // getStringFromVector
23 #include "support/lstrings.h" // trim
24 #include "lyx_forms.h"
31 typedef FormController<ControlRef, FormView<FD_ref> > base_class;
33 FormRef::FormRef(Dialog & parent)
34 : base_class(parent, _("Cross-reference")),
41 dialog_.reset(build_ref(this));
43 for (int i = 0; !InsetRef::types[i].latex_name.empty(); ++i)
44 fl_addto_choice(dialog_->choice_format,
45 _(InsetRef::types[i].gui_name).c_str());
47 // Force the user to use the browser to change refs.
48 fl_deactivate_object(dialog_->input_ref);
50 fl_set_input_return(dialog_->input_name, FL_RETURN_CHANGED);
51 fl_set_input_return(dialog_->input_ref, FL_RETURN_CHANGED);
53 setPrehandler(dialog_->input_name);
54 setPrehandler(dialog_->input_ref);
56 // Manage the ok and cancel/close buttons
57 bcview().setOK(dialog_->button_ok);
58 bcview().setApply(dialog_->button_apply);
59 bcview().setCancel(dialog_->button_close);
60 bcview().setRestore(dialog_->button_restore);
62 bcview().addReadOnly(dialog_->button_update);
63 bcview().addReadOnly(dialog_->input_name);
64 bcview().addReadOnly(dialog_->input_ref);
66 // set up the tooltips
67 string str = _("Select a document for references.");
68 tooltips().init(dialog_->choice_document, str);
69 str = _("Sort the references alphabetically.");
70 tooltips().init(dialog_->check_sort, str);
71 str = _("Go to selected reference.");
72 tooltips().init(dialog_->button_go, str);
73 str = _("Update the list of references.");
74 tooltips().init(dialog_->button_update, str);
75 str = _("Select format style of the reference.");
76 tooltips().init(dialog_->choice_format, str);
80 void FormRef::update()
82 fl_set_input(dialog_->input_ref,
83 controller().params().getContents().c_str());
84 fl_set_input(dialog_->input_name,
85 controller().params().getOptions().c_str());
86 fl_set_choice(dialog_->choice_format,
87 InsetRef::getType(controller().params().getCmdName()) + 1);
92 // Name is irrelevant to LaTeX/Literate documents
93 Kernel::DocTypes doctype = kernel().docType();
94 if (doctype == Kernel::LATEX || doctype == Kernel::LITERATE) {
95 setEnabled(dialog_->input_name, false);
97 setEnabled(dialog_->input_name, true);
100 // type is irrelevant to LinuxDoc/DocBook.
101 if (doctype == Kernel::LINUXDOC || doctype == Kernel::DOCBOOK) {
102 fl_set_choice(dialog_->choice_format, 1);
103 setEnabled(dialog_->choice_format, false);
105 setEnabled(dialog_->choice_format, true);
108 // Get the available buffers
109 vector<string> const buffers = controller().getBufferList();
110 vector<string> const choice_documents =
111 getVector(dialog_->choice_document);
113 // If different from the current contents of the choice, then update it
114 if (buffers != choice_documents) {
115 // create a string of entries " entry1 | entry2 | entry3 "
116 // with which to initialise the xforms choice object.
117 string const choice =
118 ' ' + getStringFromVector(buffers, " | ") + ' ';
120 fl_clear_choice(dialog_->choice_document);
121 fl_addto_choice(dialog_->choice_document, choice.c_str());
124 fl_set_choice(dialog_->choice_document,
125 controller().getBufferNum() + 1);
127 string const name = controller().
128 getBufferName(fl_get_choice(dialog_->choice_document) - 1);
129 refs_ = controller().getLabelList(name);
131 updateBrowser(refs_);
137 void updateHighlight(FL_OBJECT * browser,
138 vector<string> const & keys,
141 vector<string>::const_iterator cit = (ref.empty())
143 : find(keys.begin(), keys.end(), ref);
145 if (cit == keys.end()) {
146 fl_deselect_browser(browser);
148 int const i = static_cast<int>(cit - keys.begin());
149 fl_set_browser_topline(browser, max(i-5, 1));
150 fl_select_browser_line(browser, i+1);
157 void FormRef::updateBrowser(vector<string> const & akeys) const
159 vector<string> keys(akeys);
160 if (fl_get_button(dialog_->check_sort))
161 sort(keys.begin(), keys.end());
163 vector<string> browser_keys = getVector(dialog_->browser_refs);
165 if (browser_keys == keys) {
166 updateHighlight(dialog_->browser_refs, keys,
167 getString(dialog_->input_ref));
171 fl_clear_browser(dialog_->browser_refs);
172 for (vector<string>::const_iterator it = keys.begin();
173 it != keys.end(); ++it)
174 fl_add_browser_line(dialog_->browser_refs, it->c_str());
177 fl_add_browser_line(dialog_->browser_refs,
178 _("*** No labels found in document ***").c_str());
180 setEnabled(dialog_->browser_refs, false);
181 setEnabled(dialog_->check_sort, false);
183 fl_set_input(dialog_->input_ref, "");
185 setEnabled(dialog_->browser_refs, true);
186 setEnabled(dialog_->check_sort, true);
188 updateHighlight(dialog_->browser_refs, keys,
189 getString(dialog_->input_ref));
194 void FormRef::apply()
196 int const type = fl_get_choice(dialog_->choice_format) - 1;
197 controller().params().setCmdName(InsetRef::getName(type));
199 controller().params().setOptions(getString(dialog_->input_name));
200 controller().params().setContents(getString(dialog_->input_ref));
204 ButtonPolicy::SMInput FormRef::input(FL_OBJECT * ob, long)
206 ButtonPolicy::SMInput activate(ButtonPolicy::SMI_VALID);
208 if (ob == dialog_->button_go) {
209 // goto reference / go back
212 activate = ButtonPolicy::SMI_NOOP;
216 controller().gotoRef(getString(dialog_->input_ref));
218 controller().gotoBookmark();
222 } else if (ob == dialog_->browser_refs) {
224 unsigned int sel = fl_get_browser(dialog_->browser_refs);
225 if (sel < 1 || sel > refs_.size())
226 return ButtonPolicy::SMI_NOOP;
228 if (!kernel().isBufferReadonly()) {
229 string s = fl_get_browser_line(dialog_->browser_refs, sel);
230 fl_set_input(dialog_->input_ref, s.c_str());
234 controller().gotoBookmark();
238 setEnabled(dialog_->choice_format, true);
239 setEnabled(dialog_->button_go, true);
240 fl_set_object_lcol(dialog_->input_ref, FL_BLACK);
242 } else if (ob == dialog_->button_update ||
243 ob == dialog_->check_sort ||
244 ob == dialog_->choice_document) {
247 activate = ButtonPolicy::SMI_NOOP;
249 if (ob == dialog_->button_update ||
250 ob == dialog_->choice_document) {
252 controller().getBufferName(fl_get_choice(dialog_->choice_document) - 1);
253 refs_ = controller().getLabelList(name);
256 fl_freeze_form(form());
257 updateBrowser(refs_);
258 fl_unfreeze_form(form());
260 } else if (ob == dialog_->choice_format) {
262 int const type = fl_get_choice(dialog_->choice_format) - 1;
263 if (controller().params().getCmdName() ==
264 InsetRef::getName(type)) {
265 activate = ButtonPolicy::SMI_NOOP;
273 void FormRef::switch_go_button()
276 fl_set_object_label(dialog_->button_go, _("Go back").c_str());
277 tooltips().init(dialog_->button_go, _("Go back to original place.").c_str());
279 fl_set_object_label(dialog_->button_go, _("Go to").c_str());
280 tooltips().init(dialog_->button_go, _("Go to selected reference.").c_str());
282 fl_set_button_shortcut(dialog_->button_go, "#G", 1);
283 fl_show_object(dialog_->button_go);