1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000-2001 The LyX Team.
8 * ======================================================
11 * \author Angus Leeming, a.leeming@ic.ac.uk
18 #pragma implementation
22 #include "ControlRef.h"
25 #include "xforms_helpers.h"
26 #include "insets/insetref.h"
27 #include "helper_funcs.h" // getStringFromVector
28 #include "support/lstrings.h" // frontStrip, strip
35 typedef FormCB<ControlRef, FormDB<FD_form_ref> > base_class;
37 FormRef::FormRef(ControlRef & c)
38 : base_class(c, _("Reference")),
45 dialog_.reset(build_ref());
47 for (int i = 0; !InsetRef::types[i].latex_name.empty(); ++i)
48 fl_addto_choice(dialog_->choice_type,
49 _(InsetRef::types[i].gui_name.c_str()));
51 // Force the user to use the browser to change refs.
52 fl_deactivate_object(dialog_->input_ref);
54 // Manage the ok and cancel/close buttons
55 bc().setOK(dialog_->button_ok);
56 bc().setApply(dialog_->button_apply);
57 bc().setCancel(dialog_->button_cancel);
58 bc().setRestore(dialog_->button_restore);
60 bc().addReadOnly(dialog_->button_update);
61 bc().addReadOnly(dialog_->input_name);
62 bc().addReadOnly(dialog_->input_ref);
66 void FormRef::update()
68 fl_set_input(dialog_->input_ref,
69 controller().params().getContents().c_str());
70 fl_set_input(dialog_->input_name,
71 controller().params().getOptions().c_str());
72 fl_set_choice(dialog_->choice_type,
73 InsetRef::getType(controller().params().getCmdName()) + 1);
76 fl_set_object_label(dialog_->button_go, _("Go to reference"));
78 // Name is irrelevant to LaTeX/Literate documents
79 if (controller().docType() == ControlRef::LATEX ||
80 controller().docType() == ControlRef::LITERATE) {
81 setEnabled(dialog_->input_name, false);
83 setEnabled(dialog_->input_name, true);
86 // type is irrelevant to LinuxDoc/DocBook.
87 if (controller().docType() == ControlRef::LINUXDOC ||
88 controller().docType() == ControlRef::DOCBOOK) {
89 fl_set_choice(dialog_->choice_type, 1);
90 setEnabled(dialog_->choice_type, false);
92 setEnabled(dialog_->choice_type, true);
95 // Get the available buffers
96 vector<string> const buffers = controller().getBufferList();
97 vector<string> const choice_buffers =
98 getVectorFromChoice(dialog_->choice_buffer);
100 // If different from the current contents of the choice, then update it
101 if (buffers != choice_buffers) {
102 // create a string of entries " entry1 | entry2 | entry3 "
103 // with which to initialise the xforms choice object.
104 string const choice =
105 " " + getStringFromVector(buffers, " | ") + " ";
107 fl_clear_choice(dialog_->choice_buffer);
108 fl_addto_choice(dialog_->choice_buffer, choice.c_str());
110 fl_set_choice(dialog_->choice_buffer,
111 controller().getBufferNum() + 1);
114 refs_ = controller().getLabelList(string());
115 updateBrowser(refs_);
119 void FormRef::updateBrowser(vector<string> const & akeys) const
121 vector<string> keys(akeys);
122 if (fl_get_button(dialog_->check_sort))
123 sort(keys.begin(), keys.end());
125 vector<string> browser_keys =
126 getVectorFromBrowser(dialog_->browser_refs);
128 if (browser_keys == keys)
131 fl_clear_browser(dialog_->browser_refs);
132 for (vector<string>::const_iterator it = keys.begin();
133 it != keys.end(); ++it)
134 fl_add_browser_line(dialog_->browser_refs, it->c_str());
137 fl_add_browser_line(dialog_->browser_refs,
138 _("*** No labels found in document ***"));
140 setEnabled(dialog_->browser_refs, false);
141 setEnabled(dialog_->check_sort, false);
143 fl_set_input(dialog_->input_ref, "");
145 setEnabled(dialog_->browser_refs, true);
146 setEnabled(dialog_->check_sort, true);
148 string ref = fl_get_input(dialog_->input_ref);
149 vector<string>::const_iterator cit = (ref.empty())
151 : find(keys.begin(), keys.end(), ref);
152 if (cit == keys.end()) {
153 fl_deselect_browser(dialog_->browser_refs);
156 fl_set_input(dialog_->input_ref, cit->c_str());
158 int const i = static_cast<int>(cit - keys.begin());
159 fl_set_browser_topline(dialog_->browser_refs, max(i-5, 1));
160 fl_select_browser_line(dialog_->browser_refs, i+1);
166 void FormRef::apply()
168 int const type = fl_get_choice(dialog_->choice_type) - 1;
169 controller().params().setCmdName(InsetRef::getName(type));
171 controller().params().setOptions(fl_get_input(dialog_->input_name));
172 controller().params().setContents(fl_get_input(dialog_->input_ref));
176 ButtonPolicy::SMInput FormRef::input(FL_OBJECT * ob, long)
178 ButtonPolicy::SMInput activate(ButtonPolicy::SMI_VALID);
180 if (ob == dialog_->button_go) {
181 // goto reference / go back
184 activate = ButtonPolicy::SMI_NOOP;
188 controller().gotoRef(fl_get_input(dialog_->input_ref));
189 fl_set_object_label(dialog_->button_go, _("Go back"));
191 controller().gotoBookmark();
192 fl_set_object_label(dialog_->button_go,
193 _("Go to reference"));
196 } else if (ob == dialog_->browser_refs) {
198 unsigned int sel = fl_get_browser(dialog_->browser_refs);
199 if (sel < 1 || sel > refs_.size())
200 return ButtonPolicy::SMI_NOOP;
202 if (!controller().isReadonly()) {
203 string s = fl_get_browser_line(dialog_->browser_refs, sel);
204 fl_set_input(dialog_->input_ref, s.c_str());
208 controller().gotoBookmark();
210 fl_set_object_label(dialog_->button_go, _("Go to reference"));
212 setEnabled(dialog_->choice_type, true);
213 setEnabled(dialog_->button_go, true);
214 fl_set_object_lcol(dialog_->input_ref, FL_BLACK);
216 } else if (ob == dialog_->button_update ||
217 ob == dialog_->check_sort ||
218 ob == dialog_->choice_buffer) {
220 if (ob == dialog_->button_update ||
221 ob == dialog_->choice_buffer) {
223 frontStrip(strip(fl_get_choice_text(dialog_->choice_buffer)));
224 refs_ = controller().getLabelList(name);
227 fl_freeze_form(form());
228 updateBrowser(refs_);
229 fl_unfreeze_form(form());
231 } else if (ob == dialog_->choice_type) {
233 int const type = fl_get_choice(dialog_->choice_type) - 1;
234 if (controller().params().getCmdName() ==
235 InsetRef::getName(type)) {
236 activate = ButtonPolicy::SMI_NOOP;