]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormRef.C
remove defaults stuff, let Qt handle no toolbar
[lyx.git] / src / frontends / xforms / FormRef.C
1 /**
2  * \file FormRef.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12 #include <algorithm>
13
14
15 #include "xformsBC.h"
16 #include "ControlRef.h"
17 #include "FormRef.h"
18 #include "Tooltips.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
25
26 using std::find;
27 using std::max;
28 using std::sort;
29 using std::vector;
30
31 typedef FormController<ControlRef, FormView<FD_ref> > base_class;
32
33 FormRef::FormRef(Dialog & parent)
34         : base_class(parent, _("Reference")),
35           at_ref_(false)
36 {}
37
38
39 void FormRef::build()
40 {
41         dialog_.reset(build_ref(this));
42
43         for (int i = 0; !InsetRef::types[i].latex_name.empty(); ++i)
44                 fl_addto_choice(dialog_->choice_format,
45                                 _(InsetRef::types[i].gui_name.c_str()));
46
47         // Force the user to use the browser to change refs.
48         fl_deactivate_object(dialog_->input_ref);
49
50         fl_set_input_return(dialog_->input_name, FL_RETURN_CHANGED);
51         fl_set_input_return(dialog_->input_ref,  FL_RETURN_CHANGED);
52
53         setPrehandler(dialog_->input_name);
54         setPrehandler(dialog_->input_ref);
55
56         // Manage the ok and cancel/close buttons
57         bcview().setOK(dialog_->button_ok);
58         bcview().setApply(dialog_->button_apply);
59         bcview().setCancel(dialog_->button_close);
60         bcview().setRestore(dialog_->button_restore);
61
62         bcview().addReadOnly(dialog_->button_update);
63         bcview().addReadOnly(dialog_->input_name);
64         bcview().addReadOnly(dialog_->input_ref);
65
66         // set up the tooltips
67         string str = _("Select a document for references.");
68         tooltips().init(dialog_->choice_document, str);
69         str = _("Sort the references alphabetically.");
70         tooltips().init(dialog_->check_sort, str);
71         str = _("Go to selected reference.");
72         tooltips().init(dialog_->button_go, str);
73         str = _("Update the list of references.");
74         tooltips().init(dialog_->button_update, str);
75         str = _("Select format style of the reference.");
76         tooltips().init(dialog_->choice_format, str);
77 }
78
79
80 void FormRef::update()
81 {
82         fl_set_input(dialog_->input_ref,
83                      controller().params().getContents().c_str());
84         fl_set_input(dialog_->input_name,
85                      controller().params().getOptions().c_str());
86         fl_set_choice(dialog_->choice_format,
87                       InsetRef::getType(controller().params().getCmdName()) + 1);
88
89         at_ref_ = false;
90         switch_go_button();
91
92         // Name is irrelevant to LaTeX/Literate documents
93         Kernel::DocTypes doctype = kernel().docType();
94         if (doctype == Kernel::LATEX || doctype == Kernel::LITERATE) {
95                 setEnabled(dialog_->input_name, false);
96         } else {
97                 setEnabled(dialog_->input_name, true);
98         }
99
100         // type is irrelevant to LinuxDoc/DocBook.
101         if (doctype == Kernel::LINUXDOC || doctype == Kernel::DOCBOOK) {
102                 fl_set_choice(dialog_->choice_format, 1);
103                 setEnabled(dialog_->choice_format, false);
104         } else {
105                 setEnabled(dialog_->choice_format, true);
106         }
107
108         // Get the available buffers
109         vector<string> const buffers = controller().getBufferList();
110         vector<string> const choice_documents =
111                 getVector(dialog_->choice_document);
112
113         // If different from the current contents of the choice, then update it
114         if (buffers != choice_documents) {
115                 // create a string of entries " entry1 | entry2 | entry3 "
116                 // with which to initialise the xforms choice object.
117                 string const choice =
118                         ' ' + getStringFromVector(buffers, " | ") + ' ';
119
120                 fl_clear_choice(dialog_->choice_document);
121                 fl_addto_choice(dialog_->choice_document, choice.c_str());
122         }
123
124         fl_set_choice(dialog_->choice_document,
125                       controller().getBufferNum() + 1);
126
127         string const name = controller().
128                 getBufferName(fl_get_choice(dialog_->choice_document) - 1);
129         refs_ = controller().getLabelList(name);
130
131         updateBrowser(refs_);
132 }
133
134
135 namespace {
136
137 void updateHighlight(FL_OBJECT * browser,
138                      vector<string> const & keys,
139                      string const & ref)
140 {
141         vector<string>::const_iterator cit = (ref.empty())
142                 ? keys.end()
143                 : find(keys.begin(), keys.end(), ref);
144
145         if (cit == keys.end()) {
146                 fl_deselect_browser(browser);
147         } else {
148                 int const i = static_cast<int>(cit - keys.begin());
149                 fl_set_browser_topline(browser, max(i-5, 1));
150                 fl_select_browser_line(browser, i+1);
151         }
152 }
153
154 } // namespace anon
155
156
157 void FormRef::updateBrowser(vector<string> const & akeys) const
158 {
159         vector<string> keys(akeys);
160         if (fl_get_button(dialog_->check_sort))
161                 sort(keys.begin(), keys.end());
162
163         vector<string> browser_keys = getVector(dialog_->browser_refs);
164
165         if (browser_keys == keys) {
166                 updateHighlight(dialog_->browser_refs, keys,
167                                 getString(dialog_->input_ref));
168                 return;
169         }
170
171         fl_clear_browser(dialog_->browser_refs);
172         for (vector<string>::const_iterator it = keys.begin();
173              it != keys.end(); ++it)
174                 fl_add_browser_line(dialog_->browser_refs, it->c_str());
175
176         if (keys.empty()) {
177                 fl_add_browser_line(dialog_->browser_refs,
178                                     _("*** No labels found in document ***"));
179
180                 setEnabled(dialog_->browser_refs, false);
181                 setEnabled(dialog_->check_sort,   false);
182
183                 fl_set_input(dialog_->input_ref, "");
184         } else {
185                 setEnabled(dialog_->browser_refs, true);
186                 setEnabled(dialog_->check_sort,   true);
187
188                 updateHighlight(dialog_->browser_refs, keys,
189                                 getString(dialog_->input_ref));
190         }
191 }
192
193
194 void FormRef::apply()
195 {
196         int const type = fl_get_choice(dialog_->choice_format) - 1;
197         controller().params().setCmdName(InsetRef::getName(type));
198
199         controller().params().setOptions(getString(dialog_->input_name));
200         controller().params().setContents(getString(dialog_->input_ref));
201 }
202
203
204 ButtonPolicy::SMInput FormRef::input(FL_OBJECT * ob, long)
205 {
206         ButtonPolicy::SMInput activate(ButtonPolicy::SMI_VALID);
207
208         if (ob == dialog_->button_go) {
209                 // goto reference / go back
210
211                 // No change to data
212                 activate = ButtonPolicy::SMI_NOOP;
213
214                 at_ref_ = !at_ref_;
215                 if (at_ref_) {
216                         controller().gotoRef(getString(dialog_->input_ref));
217                 } else {
218                         controller().gotoBookmark();
219                 }
220                 switch_go_button();
221
222         } else if (ob == dialog_->browser_refs) {
223
224                 unsigned int sel = fl_get_browser(dialog_->browser_refs);
225                 if (sel < 1 || sel > refs_.size())
226                         return ButtonPolicy::SMI_NOOP;
227
228                 if (!kernel().isBufferReadonly()) {
229                         string s = fl_get_browser_line(dialog_->browser_refs, sel);
230                         fl_set_input(dialog_->input_ref, s.c_str());
231                 }
232
233                 if (at_ref_)
234                         controller().gotoBookmark();
235                 at_ref_ = false;
236                 switch_go_button();
237
238                 setEnabled(dialog_->choice_format, true);
239                 setEnabled(dialog_->button_go, true);
240                 fl_set_object_lcol(dialog_->input_ref, FL_BLACK);
241
242         } else if (ob == dialog_->button_update ||
243                    ob == dialog_->check_sort ||
244                    ob == dialog_->choice_document) {
245
246                 // No change to data
247                 activate = ButtonPolicy::SMI_NOOP;
248
249                 if (ob == dialog_->button_update ||
250                     ob == dialog_->choice_document) {
251                         string const name =
252                                 controller().getBufferName(fl_get_choice(dialog_->choice_document) - 1);
253                         refs_ = controller().getLabelList(name);
254                 }
255
256                 fl_freeze_form(form());
257                 updateBrowser(refs_);
258                 fl_unfreeze_form(form());
259
260         } else if (ob == dialog_->choice_format) {
261
262                 int const type = fl_get_choice(dialog_->choice_format) - 1;
263                 if (controller().params().getCmdName() ==
264                     InsetRef::getName(type)) {
265                         activate = ButtonPolicy::SMI_NOOP;
266                 }
267         }
268
269         return activate;
270 }
271
272
273 void FormRef::switch_go_button()
274 {
275         if (at_ref_) {
276                 fl_set_object_label(dialog_->button_go, _("Go back"));
277                 tooltips().init(dialog_->button_go, _("Go back to original place."));
278         } else {
279                 fl_set_object_label(dialog_->button_go, _("Go to"));
280                 tooltips().init(dialog_->button_go, _("Go to selected reference."));
281         }
282         fl_set_button_shortcut(dialog_->button_go, "#G", 1);
283         fl_show_object(dialog_->button_go);
284 }