2 * \file xforms/FormRef.C
3 * Copyright 2000-2001 The LyX Team.
4 * See the file COPYING.
6 * \author Angus Leeming, a.leeming@ic.ac.uk
13 #pragma implementation
17 #include "ControlRef.h"
20 #include "xforms_helpers.h"
21 #include "insets/insetref.h"
22 #include "helper_funcs.h" // getStringFromVector
23 #include "support/lstrings.h" // frontStrip, strip
30 typedef FormCB<ControlRef, FormDB<FD_form_ref> > base_class;
32 FormRef::FormRef(ControlRef & c)
33 : base_class(c, _("Reference")),
40 dialog_.reset(build_ref());
42 for (int i = 0; !InsetRef::types[i].latex_name.empty(); ++i)
43 fl_addto_choice(dialog_->choice_type,
44 _(InsetRef::types[i].gui_name.c_str()));
46 // Force the user to use the browser to change refs.
47 fl_deactivate_object(dialog_->input_ref);
49 fl_set_input_return(dialog_->input_name, FL_RETURN_CHANGED);
50 fl_set_input_return(dialog_->input_ref, FL_RETURN_CHANGED);
52 setPrehandler(dialog_->input_name);
53 setPrehandler(dialog_->input_ref);
55 // Manage the ok and cancel/close buttons
56 bc().setOK(dialog_->button_ok);
57 bc().setApply(dialog_->button_apply);
58 bc().setCancel(dialog_->button_close);
59 bc().setRestore(dialog_->button_restore);
61 bc().addReadOnly(dialog_->button_update);
62 bc().addReadOnly(dialog_->input_name);
63 bc().addReadOnly(dialog_->input_ref);
67 void FormRef::update()
69 fl_set_input(dialog_->input_ref,
70 controller().params().getContents().c_str());
71 fl_set_input(dialog_->input_name,
72 controller().params().getOptions().c_str());
73 fl_set_choice(dialog_->choice_type,
74 InsetRef::getType(controller().params().getCmdName()) + 1);
77 fl_set_object_label(dialog_->button_go, _("Go to reference"));
79 // Name is irrelevant to LaTeX/Literate documents
80 if (controller().docType() == ControlRef::LATEX ||
81 controller().docType() == ControlRef::LITERATE) {
82 setEnabled(dialog_->input_name, false);
84 setEnabled(dialog_->input_name, true);
87 // type is irrelevant to LinuxDoc/DocBook.
88 if (controller().docType() == ControlRef::LINUXDOC ||
89 controller().docType() == ControlRef::DOCBOOK) {
90 fl_set_choice(dialog_->choice_type, 1);
91 setEnabled(dialog_->choice_type, false);
93 setEnabled(dialog_->choice_type, true);
96 // Get the available buffers
97 vector<string> const buffers = controller().getBufferList();
98 vector<string> const choice_buffers =
99 getVectorFromChoice(dialog_->choice_buffer);
101 // If different from the current contents of the choice, then update it
102 if (buffers != choice_buffers) {
103 // create a string of entries " entry1 | entry2 | entry3 "
104 // with which to initialise the xforms choice object.
105 string const choice =
106 " " + getStringFromVector(buffers, " | ") + " ";
108 fl_clear_choice(dialog_->choice_buffer);
109 fl_addto_choice(dialog_->choice_buffer, choice.c_str());
111 fl_set_choice(dialog_->choice_buffer,
112 controller().getBufferNum() + 1);
115 refs_ = controller().getLabelList(string());
116 updateBrowser(refs_);
120 void FormRef::updateBrowser(vector<string> const & akeys) const
122 vector<string> keys(akeys);
123 if (fl_get_button(dialog_->check_sort))
124 sort(keys.begin(), keys.end());
126 vector<string> browser_keys =
127 getVectorFromBrowser(dialog_->browser_refs);
129 if (browser_keys == keys)
132 fl_clear_browser(dialog_->browser_refs);
133 for (vector<string>::const_iterator it = keys.begin();
134 it != keys.end(); ++it)
135 fl_add_browser_line(dialog_->browser_refs, it->c_str());
138 fl_add_browser_line(dialog_->browser_refs,
139 _("*** No labels found in document ***"));
141 setEnabled(dialog_->browser_refs, false);
142 setEnabled(dialog_->check_sort, false);
144 fl_set_input(dialog_->input_ref, "");
146 setEnabled(dialog_->browser_refs, true);
147 setEnabled(dialog_->check_sort, true);
149 string ref = fl_get_input(dialog_->input_ref);
150 vector<string>::const_iterator cit = (ref.empty())
152 : find(keys.begin(), keys.end(), ref);
153 if (cit == keys.end()) {
154 fl_deselect_browser(dialog_->browser_refs);
157 fl_set_input(dialog_->input_ref, cit->c_str());
159 int const i = static_cast<int>(cit - keys.begin());
160 fl_set_browser_topline(dialog_->browser_refs, max(i-5, 1));
161 fl_select_browser_line(dialog_->browser_refs, i+1);
167 void FormRef::apply()
169 int const type = fl_get_choice(dialog_->choice_type) - 1;
170 controller().params().setCmdName(InsetRef::getName(type));
172 controller().params().setOptions(fl_get_input(dialog_->input_name));
173 controller().params().setContents(fl_get_input(dialog_->input_ref));
177 ButtonPolicy::SMInput FormRef::input(FL_OBJECT * ob, long)
179 ButtonPolicy::SMInput activate(ButtonPolicy::SMI_VALID);
181 if (ob == dialog_->button_go) {
182 // goto reference / go back
185 activate = ButtonPolicy::SMI_NOOP;
189 controller().gotoRef(fl_get_input(dialog_->input_ref));
190 fl_set_object_label(dialog_->button_go, _("Go back"));
192 controller().gotoBookmark();
193 fl_set_object_label(dialog_->button_go,
194 _("Go to reference"));
197 } else if (ob == dialog_->browser_refs) {
199 unsigned int sel = fl_get_browser(dialog_->browser_refs);
200 if (sel < 1 || sel > refs_.size())
201 return ButtonPolicy::SMI_NOOP;
203 if (!controller().isReadonly()) {
204 string s = fl_get_browser_line(dialog_->browser_refs, sel);
205 fl_set_input(dialog_->input_ref, s.c_str());
209 controller().gotoBookmark();
211 fl_set_object_label(dialog_->button_go, _("Go to reference"));
213 setEnabled(dialog_->choice_type, true);
214 setEnabled(dialog_->button_go, true);
215 fl_set_object_lcol(dialog_->input_ref, FL_BLACK);
217 } else if (ob == dialog_->button_update ||
218 ob == dialog_->check_sort ||
219 ob == dialog_->choice_buffer) {
221 if (ob == dialog_->button_update ||
222 ob == dialog_->choice_buffer) {
224 controller().getBufferName(fl_get_choice(dialog_->choice_buffer) - 1);
225 refs_ = controller().getLabelList(name);
228 fl_freeze_form(form());
229 updateBrowser(refs_);
230 fl_unfreeze_form(form());
232 } else if (ob == dialog_->choice_type) {
234 int const type = fl_get_choice(dialog_->choice_type) - 1;
235 if (controller().params().getCmdName() ==
236 InsetRef::getName(type)) {
237 activate = ButtonPolicy::SMI_NOOP;