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"
41 typedef FormCB<ControlRef, FormDB<FD_form_ref> > base_class;
43 FormRef::FormRef(ControlRef & c)
44 : base_class(c, _("Reference")),
51 dialog_.reset(build_ref());
53 for (int i = 0; !InsetRef::types[i].latex_name.empty(); ++i)
54 fl_addto_choice(dialog_->type,
55 _(InsetRef::types[i].gui_name.c_str()));
57 // Force the user to use the browser to change refs.
58 fl_deactivate_object(dialog_->ref);
60 // Manage the ok and cancel/close buttons
61 bc().setOK(dialog_->button_ok);
62 bc().setApply(dialog_->button_apply);
63 bc().setCancel(dialog_->button_cancel);
64 bc().setRestore(dialog_->button_restore);
68 void FormRef::update()
70 fl_set_input(dialog_->ref,
71 controller().params().getContents().c_str());
72 fl_set_input(dialog_->name,
73 controller().params().getOptions().c_str());
74 fl_set_choice(dialog_->type,
75 InsetRef::getType(controller().params().getCmdName()) + 1);
78 fl_set_object_label(dialog_->button_go, _("Goto reference"));
80 // Name is irrelevant to LaTeX/Literate documents
81 if (controller().docType() == ControlRef::LATEX ||
82 controller().docType() == ControlRef::LITERATE) {
83 setEnabled(dialog_->name, false);
85 setEnabled(dialog_->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_->type, 1);
92 setEnabled(dialog_->type, false);
94 setEnabled(dialog_->type, true);
97 refs_ = controller().getLabelList();
102 void FormRef::updateBrowser(vector<string> const & akeys) const
104 vector<string> keys(akeys);
105 if (fl_get_button(dialog_->sort))
106 sort(keys.begin(), keys.end());
108 fl_clear_browser(dialog_->browser);
109 for (vector<string>::const_iterator it = keys.begin();
110 it != keys.end(); ++it)
111 fl_add_browser_line(dialog_->browser, it->c_str());
114 fl_add_browser_line(dialog_->browser,
115 _("*** No labels found in document ***"));
117 setEnabled(dialog_->browser, false);
118 setEnabled(dialog_->sort, false);
120 fl_set_input(dialog_->ref, "");
122 setEnabled(dialog_->browser, true);
123 setEnabled(dialog_->sort, true);
125 string ref = fl_get_input(dialog_->ref);
126 vector<string>::const_iterator cit =
127 find(keys.begin(), keys.end(), ref);
128 if (cit == keys.end()) {
130 fl_set_input(dialog_->ref, cit->c_str());
131 } else if (ref.empty())
132 fl_set_input(dialog_->ref, cit->c_str());
134 int const i = static_cast<int>(cit - keys.begin());
135 fl_set_browser_topline(dialog_->browser, max(i-5, 1));
136 fl_select_browser_line(dialog_->browser, i+1);
141 void FormRef::apply()
143 int const type = fl_get_choice(dialog_->type) - 1;
144 controller().params().setCmdName(InsetRef::getName(type));
146 controller().params().setOptions(fl_get_input(dialog_->name));
147 controller().params().setContents(fl_get_input(dialog_->ref));
151 ButtonPolicy::SMInput FormRef::input(FL_OBJECT * ob, long)
153 ButtonPolicy::SMInput activate(ButtonPolicy::SMI_VALID);
155 if (ob == dialog_->button_go) {
156 // goto reference / go back
159 activate = ButtonPolicy::SMI_NOOP;
163 controller().gotoRef(fl_get_input(dialog_->ref));
164 fl_set_object_label(dialog_->button_go, _("Go back"));
166 controller().gotoBookmark();
167 fl_set_object_label(dialog_->button_go,
168 _("Goto reference"));
171 } else if (ob == dialog_->browser) {
173 unsigned int sel = fl_get_browser(dialog_->browser);
174 if (sel < 1 || sel > refs_.size())
175 return ButtonPolicy::SMI_NOOP;
177 if (!controller().isReadonly()) {
178 string s = fl_get_browser_line(dialog_->browser, sel);
179 fl_set_input(dialog_->ref, s.c_str());
183 controller().gotoBookmark();
185 fl_set_object_label(dialog_->button_go, _("Goto reference"));
187 setEnabled(dialog_->type, true);
188 setEnabled(dialog_->button_go, true);
189 fl_set_object_lcol(dialog_->ref, FL_BLACK);
191 } else if (ob == dialog_->button_update ||
192 ob == dialog_->sort) {
194 if (ob == dialog_->button_update)
195 refs_ = controller().getLabelList();
197 fl_freeze_form(form());
198 updateBrowser(refs_);
199 fl_unfreeze_form(form());
201 } else if (ob == dialog_->type) {
203 int const type = fl_get_choice(dialog_->type) - 1;
204 if (controller().params().getCmdName() ==
205 InsetRef::getName(type)) {
206 activate = ButtonPolicy::SMI_NOOP;