2 * \file xforms/FormRef.C
3 * See the file COPYING.
5 * \author Angus Leeming
7 * Full author contact details are available in file CREDITS
14 #pragma implementation
18 #include "ControlRef.h"
20 #include "forms/form_ref.h"
21 #include "xforms_helpers.h"
22 #include "insets/insetref.h"
23 #include "helper_funcs.h" // getStringFromVector
24 #include "support/lstrings.h" // frontStrip, strip
25 #include FORMS_H_LOCATION
32 typedef FormCB<ControlRef, FormDB<FD_ref> > base_class;
35 : base_class(_("Reference")),
42 dialog_.reset(build_ref(this));
44 for (int i = 0; !InsetRef::types[i].latex_name.empty(); ++i)
45 fl_addto_choice(dialog_->choice_type,
46 _(InsetRef::types[i].gui_name.c_str()));
48 // Force the user to use the browser to change refs.
49 fl_deactivate_object(dialog_->input_ref);
51 fl_set_input_return(dialog_->input_name, FL_RETURN_CHANGED);
52 fl_set_input_return(dialog_->input_ref, FL_RETURN_CHANGED);
54 setPrehandler(dialog_->input_name);
55 setPrehandler(dialog_->input_ref);
57 // Manage the ok and cancel/close buttons
58 bc().setOK(dialog_->button_ok);
59 bc().setApply(dialog_->button_apply);
60 bc().setCancel(dialog_->button_close);
61 bc().setRestore(dialog_->button_restore);
63 bc().addReadOnly(dialog_->button_update);
64 bc().addReadOnly(dialog_->input_name);
65 bc().addReadOnly(dialog_->input_ref);
69 void FormRef::update()
71 fl_set_input(dialog_->input_ref,
72 controller().params().getContents().c_str());
73 fl_set_input(dialog_->input_name,
74 controller().params().getOptions().c_str());
75 fl_set_choice(dialog_->choice_type,
76 InsetRef::getType(controller().params().getCmdName()) + 1);
79 fl_set_object_label(dialog_->button_go, _("Go to reference"));
81 // Name is irrelevant to LaTeX/Literate documents
82 if (controller().docType() == ControlRef::LATEX ||
83 controller().docType() == ControlRef::LITERATE) {
84 setEnabled(dialog_->input_name, false);
86 setEnabled(dialog_->input_name, true);
89 // type is irrelevant to LinuxDoc/DocBook.
90 if (controller().docType() == ControlRef::LINUXDOC ||
91 controller().docType() == ControlRef::DOCBOOK) {
92 fl_set_choice(dialog_->choice_type, 1);
93 setEnabled(dialog_->choice_type, false);
95 setEnabled(dialog_->choice_type, true);
98 // Get the available buffers
99 vector<string> const buffers = controller().getBufferList();
100 vector<string> const choice_buffers =
101 getVector(dialog_->choice_buffer);
103 // If different from the current contents of the choice, then update it
104 if (buffers != choice_buffers) {
105 // create a string of entries " entry1 | entry2 | entry3 "
106 // with which to initialise the xforms choice object.
107 string const choice =
108 " " + getStringFromVector(buffers, " | ") + " ";
110 fl_clear_choice(dialog_->choice_buffer);
111 fl_addto_choice(dialog_->choice_buffer, choice.c_str());
113 fl_set_choice(dialog_->choice_buffer,
114 controller().getBufferNum() + 1);
117 string const name = controller().
118 getBufferName(fl_get_choice(dialog_->choice_buffer) - 1);
119 refs_ = controller().getLabelList(name);
121 updateBrowser(refs_);
125 void FormRef::updateBrowser(vector<string> const & akeys) const
127 vector<string> keys(akeys);
128 if (fl_get_button(dialog_->check_sort))
129 sort(keys.begin(), keys.end());
131 vector<string> browser_keys = getVector(dialog_->browser_refs);
133 if (browser_keys == keys)
136 fl_clear_browser(dialog_->browser_refs);
137 for (vector<string>::const_iterator it = keys.begin();
138 it != keys.end(); ++it)
139 fl_add_browser_line(dialog_->browser_refs, it->c_str());
142 fl_add_browser_line(dialog_->browser_refs,
143 _("*** No labels found in document ***"));
145 setEnabled(dialog_->browser_refs, false);
146 setEnabled(dialog_->check_sort, false);
148 fl_set_input(dialog_->input_ref, "");
150 setEnabled(dialog_->browser_refs, true);
151 setEnabled(dialog_->check_sort, true);
153 string ref = fl_get_input(dialog_->input_ref);
154 vector<string>::const_iterator cit = (ref.empty())
156 : find(keys.begin(), keys.end(), ref);
157 if (cit == keys.end()) {
158 fl_deselect_browser(dialog_->browser_refs);
161 fl_set_input(dialog_->input_ref, cit->c_str());
163 int const i = static_cast<int>(cit - keys.begin());
164 fl_set_browser_topline(dialog_->browser_refs, max(i-5, 1));
165 fl_select_browser_line(dialog_->browser_refs, i+1);
171 void FormRef::apply()
173 int const type = fl_get_choice(dialog_->choice_type) - 1;
174 controller().params().setCmdName(InsetRef::getName(type));
176 controller().params().setOptions(fl_get_input(dialog_->input_name));
177 controller().params().setContents(fl_get_input(dialog_->input_ref));
181 ButtonPolicy::SMInput FormRef::input(FL_OBJECT * ob, long)
183 ButtonPolicy::SMInput activate(ButtonPolicy::SMI_VALID);
185 if (ob == dialog_->button_go) {
186 // goto reference / go back
189 activate = ButtonPolicy::SMI_NOOP;
193 controller().gotoRef(fl_get_input(dialog_->input_ref));
194 fl_set_object_label(dialog_->button_go, _("Go back"));
196 controller().gotoBookmark();
197 fl_set_object_label(dialog_->button_go,
198 _("Go to reference"));
201 } else if (ob == dialog_->browser_refs) {
203 unsigned int sel = fl_get_browser(dialog_->browser_refs);
204 if (sel < 1 || sel > refs_.size())
205 return ButtonPolicy::SMI_NOOP;
207 if (!controller().bufferIsReadonly()) {
208 string s = fl_get_browser_line(dialog_->browser_refs, sel);
209 fl_set_input(dialog_->input_ref, s.c_str());
213 controller().gotoBookmark();
215 fl_set_object_label(dialog_->button_go, _("Go to reference"));
217 setEnabled(dialog_->choice_type, true);
218 setEnabled(dialog_->button_go, true);
219 fl_set_object_lcol(dialog_->input_ref, FL_BLACK);
221 } else if (ob == dialog_->button_update ||
222 ob == dialog_->check_sort ||
223 ob == dialog_->choice_buffer) {
225 if (ob == dialog_->button_update ||
226 ob == dialog_->choice_buffer) {
228 controller().getBufferName(fl_get_choice(dialog_->choice_buffer) - 1);
229 refs_ = controller().getLabelList(name);
232 fl_freeze_form(form());
233 updateBrowser(refs_);
234 fl_unfreeze_form(form());
236 } else if (ob == dialog_->choice_type) {
238 int const type = fl_get_choice(dialog_->choice_type) - 1;
239 if (controller().params().getCmdName() ==
240 InsetRef::getName(type)) {
241 activate = ButtonPolicy::SMI_NOOP;