2 * \file xforms/FormRef.C
3 * Copyright 2000-2001 The LyX Team.
4 * See the file COPYING.
6 * \author Angus Leeming <leeming@lyx.org>
13 #pragma implementation
17 #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" // frontStrip, strip
24 #include FORMS_H_LOCATION
31 typedef FormCB<ControlRef, FormDB<FD_ref> > base_class;
34 : base_class(_("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_type,
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 bc().setOK(dialog_->button_ok);
58 bc().setApply(dialog_->button_apply);
59 bc().setCancel(dialog_->button_close);
60 bc().setRestore(dialog_->button_restore);
62 bc().addReadOnly(dialog_->button_update);
63 bc().addReadOnly(dialog_->input_name);
64 bc().addReadOnly(dialog_->input_ref);
68 void FormRef::update()
70 fl_set_input(dialog_->input_ref,
71 controller().params().getContents().c_str());
72 fl_set_input(dialog_->input_name,
73 controller().params().getOptions().c_str());
74 fl_set_choice(dialog_->choice_type,
75 InsetRef::getType(controller().params().getCmdName()) + 1);
78 fl_set_object_label(dialog_->button_go, _("Go to reference"));
80 // Name is irrelevant to LaTeX/Literate documents
81 if (controller().docType() == ControlRef::LATEX ||
82 controller().docType() == ControlRef::LITERATE) {
83 setEnabled(dialog_->input_name, false);
85 setEnabled(dialog_->input_name, true);
88 // type is irrelevant to LinuxDoc/DocBook.
89 if (controller().docType() == ControlRef::LINUXDOC ||
90 controller().docType() == ControlRef::DOCBOOK) {
91 fl_set_choice(dialog_->choice_type, 1);
92 setEnabled(dialog_->choice_type, false);
94 setEnabled(dialog_->choice_type, true);
97 // Get the available buffers
98 vector<string> const buffers = controller().getBufferList();
99 vector<string> const choice_buffers =
100 getVector(dialog_->choice_buffer);
102 // If different from the current contents of the choice, then update it
103 if (buffers != choice_buffers) {
104 // create a string of entries " entry1 | entry2 | entry3 "
105 // with which to initialise the xforms choice object.
106 string const choice =
107 " " + getStringFromVector(buffers, " | ") + " ";
109 fl_clear_choice(dialog_->choice_buffer);
110 fl_addto_choice(dialog_->choice_buffer, choice.c_str());
112 fl_set_choice(dialog_->choice_buffer,
113 controller().getBufferNum() + 1);
116 string const name = controller().
117 getBufferName(fl_get_choice(dialog_->choice_buffer) - 1);
118 refs_ = controller().getLabelList(name);
120 updateBrowser(refs_);
124 void FormRef::updateBrowser(vector<string> const & akeys) const
126 vector<string> keys(akeys);
127 if (fl_get_button(dialog_->check_sort))
128 sort(keys.begin(), keys.end());
130 vector<string> browser_keys = getVector(dialog_->browser_refs);
132 if (browser_keys == keys)
135 fl_clear_browser(dialog_->browser_refs);
136 for (vector<string>::const_iterator it = keys.begin();
137 it != keys.end(); ++it)
138 fl_add_browser_line(dialog_->browser_refs, it->c_str());
141 fl_add_browser_line(dialog_->browser_refs,
142 _("*** No labels found in document ***"));
144 setEnabled(dialog_->browser_refs, false);
145 setEnabled(dialog_->check_sort, false);
147 fl_set_input(dialog_->input_ref, "");
149 setEnabled(dialog_->browser_refs, true);
150 setEnabled(dialog_->check_sort, true);
152 string ref = fl_get_input(dialog_->input_ref);
153 vector<string>::const_iterator cit = (ref.empty())
155 : find(keys.begin(), keys.end(), ref);
156 if (cit == keys.end()) {
157 fl_deselect_browser(dialog_->browser_refs);
160 fl_set_input(dialog_->input_ref, cit->c_str());
162 int const i = static_cast<int>(cit - keys.begin());
163 fl_set_browser_topline(dialog_->browser_refs, max(i-5, 1));
164 fl_select_browser_line(dialog_->browser_refs, i+1);
170 void FormRef::apply()
172 int const type = fl_get_choice(dialog_->choice_type) - 1;
173 controller().params().setCmdName(InsetRef::getName(type));
175 controller().params().setOptions(fl_get_input(dialog_->input_name));
176 controller().params().setContents(fl_get_input(dialog_->input_ref));
180 ButtonPolicy::SMInput FormRef::input(FL_OBJECT * ob, long)
182 ButtonPolicy::SMInput activate(ButtonPolicy::SMI_VALID);
184 if (ob == dialog_->button_go) {
185 // goto reference / go back
188 activate = ButtonPolicy::SMI_NOOP;
192 controller().gotoRef(fl_get_input(dialog_->input_ref));
193 fl_set_object_label(dialog_->button_go, _("Go back"));
195 controller().gotoBookmark();
196 fl_set_object_label(dialog_->button_go,
197 _("Go to reference"));
200 } else if (ob == dialog_->browser_refs) {
202 unsigned int sel = fl_get_browser(dialog_->browser_refs);
203 if (sel < 1 || sel > refs_.size())
204 return ButtonPolicy::SMI_NOOP;
206 if (!controller().bufferIsReadonly()) {
207 string s = fl_get_browser_line(dialog_->browser_refs, sel);
208 fl_set_input(dialog_->input_ref, s.c_str());
212 controller().gotoBookmark();
214 fl_set_object_label(dialog_->button_go, _("Go to reference"));
216 setEnabled(dialog_->choice_type, true);
217 setEnabled(dialog_->button_go, true);
218 fl_set_object_lcol(dialog_->input_ref, FL_BLACK);
220 } else if (ob == dialog_->button_update ||
221 ob == dialog_->check_sort ||
222 ob == dialog_->choice_buffer) {
224 if (ob == dialog_->button_update ||
225 ob == dialog_->choice_buffer) {
227 controller().getBufferName(fl_get_choice(dialog_->choice_buffer) - 1);
228 refs_ = controller().getLabelList(name);
231 fl_freeze_form(form());
232 updateBrowser(refs_);
233 fl_unfreeze_form(form());
235 } else if (ob == dialog_->choice_type) {
237 int const type = fl_get_choice(dialog_->choice_type) - 1;
238 if (controller().params().getCmdName() ==
239 InsetRef::getName(type)) {
240 activate = ButtonPolicy::SMI_NOOP;