2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 2000-2001 The LyX Team.
9 * ======================================================
12 * \author Angus Leeming, a.leeming@ic.ac.uk
19 #pragma implementation
23 #include "ControlRef.h"
26 #include "xforms_helpers.h"
27 #include "insets/insetref.h"
34 typedef FormCB<ControlRef, FormDB<FD_form_ref> > base_class;
36 FormRef::FormRef(ControlRef & c)
37 : base_class(c, _("Reference")),
44 dialog_.reset(build_ref());
46 for (int i = 0; !InsetRef::types[i].latex_name.empty(); ++i)
47 fl_addto_choice(dialog_->type,
48 _(InsetRef::types[i].gui_name.c_str()));
50 // Force the user to use the browser to change refs.
51 fl_deactivate_object(dialog_->ref);
53 // Manage the ok and cancel/close buttons
54 bc().setOK(dialog_->button_ok);
55 bc().setApply(dialog_->button_apply);
56 bc().setCancel(dialog_->button_cancel);
57 bc().setRestore(dialog_->button_restore);
61 void FormRef::update()
63 fl_set_input(dialog_->ref,
64 controller().params().getContents().c_str());
65 fl_set_input(dialog_->name,
66 controller().params().getOptions().c_str());
67 fl_set_choice(dialog_->type,
68 InsetRef::getType(controller().params().getCmdName()) + 1);
71 fl_set_object_label(dialog_->button_go, _("Goto reference"));
73 // Name is irrelevant to LaTeX/Literate documents
74 if (controller().docType() == ControlRef::LATEX ||
75 controller().docType() == ControlRef::LITERATE) {
76 setEnabled(dialog_->name, false);
78 setEnabled(dialog_->name, true);
81 // type is irrelevant to LinuxDoc/DocBook.
82 if (controller().docType() == ControlRef::LINUXDOC ||
83 controller().docType() == ControlRef::DOCBOOK) {
84 fl_set_choice(dialog_->type, 1);
85 setEnabled(dialog_->type, false);
87 setEnabled(dialog_->type, true);
90 refs_ = controller().getLabelList();
95 void FormRef::updateBrowser(vector<string> const & akeys) const
97 vector<string> keys(akeys);
98 if (fl_get_button(dialog_->sort))
99 sort(keys.begin(), keys.end());
101 fl_clear_browser(dialog_->browser);
102 for (vector<string>::const_iterator it = keys.begin();
103 it != keys.end(); ++it)
104 fl_add_browser_line(dialog_->browser, it->c_str());
107 fl_add_browser_line(dialog_->browser,
108 _("*** No labels found in document ***"));
110 setEnabled(dialog_->browser, false);
111 setEnabled(dialog_->sort, false);
113 fl_set_input(dialog_->ref, "");
115 setEnabled(dialog_->browser, true);
116 setEnabled(dialog_->sort, true);
118 string ref = fl_get_input(dialog_->ref);
119 vector<string>::const_iterator cit =
120 find(keys.begin(), keys.end(), ref);
121 if (cit == keys.end()) {
123 fl_set_input(dialog_->ref, cit->c_str());
124 } else if (ref.empty())
125 fl_set_input(dialog_->ref, cit->c_str());
127 int const i = static_cast<int>(cit - keys.begin());
128 fl_set_browser_topline(dialog_->browser, max(i-5, 1));
129 fl_select_browser_line(dialog_->browser, i+1);
134 void FormRef::apply()
136 int const type = fl_get_choice(dialog_->type) - 1;
137 controller().params().setCmdName(InsetRef::getName(type));
139 controller().params().setOptions(fl_get_input(dialog_->name));
140 controller().params().setContents(fl_get_input(dialog_->ref));
144 ButtonPolicy::SMInput FormRef::input(FL_OBJECT * ob, long)
146 ButtonPolicy::SMInput activate(ButtonPolicy::SMI_VALID);
148 if (ob == dialog_->button_go) {
149 // goto reference / go back
152 activate = ButtonPolicy::SMI_NOOP;
156 controller().gotoRef(fl_get_input(dialog_->ref));
157 fl_set_object_label(dialog_->button_go, _("Go back"));
159 controller().gotoBookmark();
160 fl_set_object_label(dialog_->button_go,
161 _("Goto reference"));
164 } else if (ob == dialog_->browser) {
166 unsigned int sel = fl_get_browser(dialog_->browser);
167 if (sel < 1 || sel > refs_.size())
168 return ButtonPolicy::SMI_NOOP;
170 if (!controller().isReadonly()) {
171 string s = fl_get_browser_line(dialog_->browser, sel);
172 fl_set_input(dialog_->ref, s.c_str());
176 controller().gotoBookmark();
178 fl_set_object_label(dialog_->button_go, _("Goto reference"));
180 setEnabled(dialog_->type, true);
181 setEnabled(dialog_->button_go, true);
182 fl_set_object_lcol(dialog_->ref, FL_BLACK);
184 } else if (ob == dialog_->button_update ||
185 ob == dialog_->sort) {
187 if (ob == dialog_->button_update)
188 refs_ = controller().getLabelList();
190 fl_freeze_form(form());
191 updateBrowser(refs_);
192 fl_unfreeze_form(form());
194 } else if (ob == dialog_->type) {
196 int const type = fl_get_choice(dialog_->type) - 1;
197 if (controller().params().getCmdName() ==
198 InsetRef::getName(type)) {
199 activate = ButtonPolicy::SMI_NOOP;