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.
14 #include "ControlRef.h"
17 #include "forms/form_ref.h"
18 #include "xforms_helpers.h"
19 #include "insets/insetref.h"
20 #include "helper_funcs.h" // getStringFromVector
21 #include "support/lstrings.h" // trim
22 #include "lyx_forms.h"
26 using namespace lyx::support;
33 typedef FormController<ControlRef, FormView<FD_ref> > base_class;
35 FormRef::FormRef(Dialog & parent)
36 : base_class(parent, _("Cross-reference")),
43 dialog_.reset(build_ref(this));
45 for (int i = 0; !InsetRef::types[i].latex_name.empty(); ++i)
46 fl_addto_choice(dialog_->choice_format,
47 _(InsetRef::types[i].gui_name).c_str());
49 // Force the user to use the browser to change refs.
50 fl_deactivate_object(dialog_->input_ref);
52 fl_set_input_return(dialog_->input_name, FL_RETURN_CHANGED);
53 fl_set_input_return(dialog_->input_ref, FL_RETURN_CHANGED);
55 setPrehandler(dialog_->input_name);
56 setPrehandler(dialog_->input_ref);
58 // Manage the ok and cancel/close buttons
59 bcview().setOK(dialog_->button_ok);
60 bcview().setApply(dialog_->button_apply);
61 bcview().setCancel(dialog_->button_close);
62 bcview().setRestore(dialog_->button_restore);
64 bcview().addReadOnly(dialog_->button_update);
65 bcview().addReadOnly(dialog_->input_name);
66 bcview().addReadOnly(dialog_->input_ref);
68 // set up the tooltips
69 string str = _("Select a document for references.");
70 tooltips().init(dialog_->choice_document, str);
71 str = _("Sort the references alphabetically.");
72 tooltips().init(dialog_->check_sort, str);
73 str = _("Go to selected reference.");
74 tooltips().init(dialog_->button_go, str);
75 str = _("Update the list of references.");
76 tooltips().init(dialog_->button_update, str);
77 str = _("Select format style of the reference.");
78 tooltips().init(dialog_->choice_format, str);
82 void FormRef::update()
84 fl_set_input(dialog_->input_ref,
85 controller().params().getContents().c_str());
86 fl_set_input(dialog_->input_name,
87 controller().params().getOptions().c_str());
88 fl_set_choice(dialog_->choice_format,
89 InsetRef::getType(controller().params().getCmdName()) + 1);
94 // Name is irrelevant to LaTeX/Literate documents
95 Kernel::DocTypes doctype = kernel().docType();
96 if (doctype == Kernel::LATEX || doctype == Kernel::LITERATE) {
97 setEnabled(dialog_->input_name, false);
99 setEnabled(dialog_->input_name, true);
102 // type is irrelevant to LinuxDoc/DocBook.
103 if (doctype == Kernel::LINUXDOC || doctype == Kernel::DOCBOOK) {
104 fl_set_choice(dialog_->choice_format, 1);
105 setEnabled(dialog_->choice_format, false);
107 setEnabled(dialog_->choice_format, true);
110 // Get the available buffers
111 vector<string> const buffers = controller().getBufferList();
112 vector<string> const choice_documents =
113 getVector(dialog_->choice_document);
115 // If different from the current contents of the choice, then update it
116 if (buffers != choice_documents) {
117 // create a string of entries " entry1 | entry2 | entry3 "
118 // with which to initialise the xforms choice object.
119 string const choice =
120 ' ' + getStringFromVector(buffers, " | ") + ' ';
122 fl_clear_choice(dialog_->choice_document);
123 fl_addto_choice(dialog_->choice_document, choice.c_str());
126 fl_set_choice(dialog_->choice_document,
127 controller().getBufferNum() + 1);
129 string const name = controller().
130 getBufferName(fl_get_choice(dialog_->choice_document) - 1);
131 refs_ = controller().getLabelList(name);
133 updateBrowser(refs_);
139 void updateHighlight(FL_OBJECT * browser,
140 vector<string> const & keys,
143 vector<string>::const_iterator cit = (ref.empty())
145 : find(keys.begin(), keys.end(), ref);
147 if (cit == keys.end()) {
148 fl_deselect_browser(browser);
150 int const i = static_cast<int>(cit - keys.begin());
151 fl_set_browser_topline(browser, max(i-5, 1));
152 fl_select_browser_line(browser, i+1);
159 void FormRef::updateBrowser(vector<string> const & akeys) const
161 vector<string> keys(akeys);
162 if (fl_get_button(dialog_->check_sort))
163 sort(keys.begin(), keys.end());
165 vector<string> browser_keys = getVector(dialog_->browser_refs);
167 if (browser_keys == keys) {
168 updateHighlight(dialog_->browser_refs, keys,
169 getString(dialog_->input_ref));
173 fl_clear_browser(dialog_->browser_refs);
174 for (vector<string>::const_iterator it = keys.begin();
175 it != keys.end(); ++it)
176 fl_add_browser_line(dialog_->browser_refs, it->c_str());
179 fl_add_browser_line(dialog_->browser_refs,
180 _("*** No labels found in document ***").c_str());
182 setEnabled(dialog_->browser_refs, false);
183 setEnabled(dialog_->check_sort, false);
185 fl_set_input(dialog_->input_ref, "");
187 setEnabled(dialog_->browser_refs, true);
188 setEnabled(dialog_->check_sort, true);
190 updateHighlight(dialog_->browser_refs, keys,
191 getString(dialog_->input_ref));
196 void FormRef::apply()
198 int const type = fl_get_choice(dialog_->choice_format) - 1;
199 controller().params().setCmdName(InsetRef::getName(type));
201 controller().params().setOptions(getString(dialog_->input_name));
202 controller().params().setContents(getString(dialog_->input_ref));
206 ButtonPolicy::SMInput FormRef::input(FL_OBJECT * ob, long)
208 ButtonPolicy::SMInput activate(ButtonPolicy::SMI_VALID);
210 if (ob == dialog_->button_go) {
211 // goto reference / go back
214 activate = ButtonPolicy::SMI_NOOP;
218 controller().gotoRef(getString(dialog_->input_ref));
220 controller().gotoBookmark();
224 } else if (ob == dialog_->browser_refs) {
226 unsigned int sel = fl_get_browser(dialog_->browser_refs);
227 if (sel < 1 || sel > refs_.size())
228 return ButtonPolicy::SMI_NOOP;
230 if (!kernel().isBufferReadonly()) {
231 string s = fl_get_browser_line(dialog_->browser_refs, sel);
232 fl_set_input(dialog_->input_ref, s.c_str());
236 controller().gotoBookmark();
240 setEnabled(dialog_->choice_format, true);
241 setEnabled(dialog_->button_go, true);
242 fl_set_object_lcol(dialog_->input_ref, FL_BLACK);
244 } else if (ob == dialog_->button_update ||
245 ob == dialog_->check_sort ||
246 ob == dialog_->choice_document) {
249 activate = ButtonPolicy::SMI_NOOP;
251 if (ob == dialog_->button_update ||
252 ob == dialog_->choice_document) {
254 controller().getBufferName(fl_get_choice(dialog_->choice_document) - 1);
255 refs_ = controller().getLabelList(name);
258 fl_freeze_form(form());
259 updateBrowser(refs_);
260 fl_unfreeze_form(form());
262 } else if (ob == dialog_->choice_format) {
264 int const type = fl_get_choice(dialog_->choice_format) - 1;
265 if (controller().params().getCmdName() ==
266 InsetRef::getName(type)) {
267 activate = ButtonPolicy::SMI_NOOP;
275 void FormRef::switch_go_button()
278 fl_set_object_label(dialog_->button_go, _("Go back").c_str());
279 tooltips().init(dialog_->button_go, _("Go back to original place.").c_str());
281 fl_set_object_label(dialog_->button_go, _("Go to").c_str());
282 tooltips().init(dialog_->button_go, _("Go to selected reference.").c_str());
284 fl_set_button_shortcut(dialog_->button_go, "#G", 1);
285 fl_show_object(dialog_->button_go);