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