]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormRef.C
Really dull and boring header shit
[lyx.git] / src / frontends / xforms / FormRef.C
1 /**
2  * \file xforms/FormRef.C
3  * See the file COPYING.
4  *
5  * \author Angus Leeming 
6  *
7  * Full author contact details are available in file CREDITS
8  */
9
10 #include <config.h>
11 #include <algorithm>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "xformsBC.h"
18 #include "ControlRef.h"
19 #include "FormRef.h"
20 #include "forms/form_ref.h"
21 #include "xforms_helpers.h"
22 #include "insets/insetref.h"
23 #include "helper_funcs.h" // getStringFromVector
24 #include "support/lstrings.h" // frontStrip, strip
25 #include FORMS_H_LOCATION
26
27 using std::find;
28 using std::max;
29 using std::sort;
30 using std::vector;
31
32 typedef FormCB<ControlRef, FormDB<FD_ref> > base_class;
33
34 FormRef::FormRef()
35         : base_class(_("Reference")),
36           at_ref_(false)
37 {}
38
39
40 void FormRef::build()
41 {
42         dialog_.reset(build_ref(this));
43
44         for (int i = 0; !InsetRef::types[i].latex_name.empty(); ++i)
45                 fl_addto_choice(dialog_->choice_type,
46                                 _(InsetRef::types[i].gui_name.c_str()));
47
48         // Force the user to use the browser to change refs.
49         fl_deactivate_object(dialog_->input_ref);
50
51         fl_set_input_return(dialog_->input_name, FL_RETURN_CHANGED);
52         fl_set_input_return(dialog_->input_ref,  FL_RETURN_CHANGED);
53
54         setPrehandler(dialog_->input_name);
55         setPrehandler(dialog_->input_ref);
56
57         // Manage the ok and cancel/close buttons
58         bc().setOK(dialog_->button_ok);
59         bc().setApply(dialog_->button_apply);
60         bc().setCancel(dialog_->button_close);
61         bc().setRestore(dialog_->button_restore);
62
63         bc().addReadOnly(dialog_->button_update);
64         bc().addReadOnly(dialog_->input_name);
65         bc().addReadOnly(dialog_->input_ref);
66 }
67
68
69 void FormRef::update()
70 {
71         fl_set_input(dialog_->input_ref,
72                      controller().params().getContents().c_str());
73         fl_set_input(dialog_->input_name,
74                      controller().params().getOptions().c_str());
75         fl_set_choice(dialog_->choice_type,
76                       InsetRef::getType(controller().params().getCmdName()) + 1);
77
78         at_ref_ = false;
79         fl_set_object_label(dialog_->button_go, _("Go to reference"));
80
81         // Name is irrelevant to LaTeX/Literate documents
82         if (controller().docType() == ControlRef::LATEX ||
83             controller().docType() == ControlRef::LITERATE) {
84                 setEnabled(dialog_->input_name, false);
85         } else {
86                 setEnabled(dialog_->input_name, true);
87         }
88
89         // type is irrelevant to LinuxDoc/DocBook.
90         if (controller().docType() == ControlRef::LINUXDOC ||
91             controller().docType() == ControlRef::DOCBOOK) {
92                 fl_set_choice(dialog_->choice_type, 1);
93                 setEnabled(dialog_->choice_type, false);
94         } else {
95                 setEnabled(dialog_->choice_type, true);
96         }
97
98         // Get the available buffers
99         vector<string> const buffers = controller().getBufferList();
100         vector<string> const choice_buffers =
101                 getVector(dialog_->choice_buffer);
102
103         // If different from the current contents of the choice, then update it
104         if (buffers != choice_buffers) {
105                 // create a string of entries " entry1 | entry2 | entry3 "
106                 // with which to initialise the xforms choice object.
107                 string const choice =
108                         " " + getStringFromVector(buffers, " | ") + " ";
109
110                 fl_clear_choice(dialog_->choice_buffer);
111                 fl_addto_choice(dialog_->choice_buffer, choice.c_str());
112
113                 fl_set_choice(dialog_->choice_buffer,
114                               controller().getBufferNum() + 1);
115         }
116
117         string const name = controller().
118                 getBufferName(fl_get_choice(dialog_->choice_buffer) - 1);
119         refs_ = controller().getLabelList(name);
120
121         updateBrowser(refs_);
122 }
123
124
125 void FormRef::updateBrowser(vector<string> const & akeys) const
126 {
127         vector<string> keys(akeys);
128         if (fl_get_button(dialog_->check_sort))
129                 sort(keys.begin(), keys.end());
130
131         vector<string> browser_keys = getVector(dialog_->browser_refs);
132
133         if (browser_keys == keys)
134                 return;
135
136         fl_clear_browser(dialog_->browser_refs);
137         for (vector<string>::const_iterator it = keys.begin();
138              it != keys.end(); ++it)
139                 fl_add_browser_line(dialog_->browser_refs, it->c_str());
140
141         if (keys.empty()) {
142                 fl_add_browser_line(dialog_->browser_refs,
143                                     _("*** No labels found in document ***"));
144
145                 setEnabled(dialog_->browser_refs, false);
146                 setEnabled(dialog_->check_sort,   false);
147
148                 fl_set_input(dialog_->input_ref, "");
149         } else {
150                 setEnabled(dialog_->browser_refs, true);
151                 setEnabled(dialog_->check_sort,   true);
152
153                 string ref = fl_get_input(dialog_->input_ref);
154                 vector<string>::const_iterator cit = (ref.empty())
155                         ? keys.begin()
156                         : find(keys.begin(), keys.end(), ref);
157                 if (cit == keys.end()) {
158                         fl_deselect_browser(dialog_->browser_refs);
159                 } else {
160                         if (ref.empty())
161                                 fl_set_input(dialog_->input_ref, cit->c_str());
162
163                         int const i = static_cast<int>(cit - keys.begin());
164                         fl_set_browser_topline(dialog_->browser_refs, max(i-5, 1));
165                         fl_select_browser_line(dialog_->browser_refs, i+1);
166                 }
167         }
168 }
169
170
171 void FormRef::apply()
172 {
173         int const type = fl_get_choice(dialog_->choice_type) - 1;
174         controller().params().setCmdName(InsetRef::getName(type));
175
176         controller().params().setOptions(fl_get_input(dialog_->input_name));
177         controller().params().setContents(fl_get_input(dialog_->input_ref));
178 }
179
180
181 ButtonPolicy::SMInput FormRef::input(FL_OBJECT * ob, long)
182 {
183         ButtonPolicy::SMInput activate(ButtonPolicy::SMI_VALID);
184
185         if (ob == dialog_->button_go) {
186                 // goto reference / go back
187
188                 // No change to data
189                 activate = ButtonPolicy::SMI_NOOP;
190
191                 at_ref_ = !at_ref_;
192                 if (at_ref_) {
193                         controller().gotoRef(fl_get_input(dialog_->input_ref));
194                         fl_set_object_label(dialog_->button_go, _("Go back"));
195                 } else {
196                         controller().gotoBookmark();
197                         fl_set_object_label(dialog_->button_go,
198                                             _("Go to reference"));
199                 }
200
201         } else if (ob == dialog_->browser_refs) {
202
203                 unsigned int sel = fl_get_browser(dialog_->browser_refs);
204                 if (sel < 1 || sel > refs_.size())
205                         return ButtonPolicy::SMI_NOOP;
206
207                 if (!controller().bufferIsReadonly()) {
208                         string s = fl_get_browser_line(dialog_->browser_refs, sel);
209                         fl_set_input(dialog_->input_ref, s.c_str());
210                 }
211
212                 if (at_ref_)
213                         controller().gotoBookmark();
214                 at_ref_ = false;
215                 fl_set_object_label(dialog_->button_go, _("Go to reference"));
216
217                 setEnabled(dialog_->choice_type,      true);
218                 setEnabled(dialog_->button_go, true);
219                 fl_set_object_lcol(dialog_->input_ref, FL_BLACK);
220
221         } else if (ob == dialog_->button_update ||
222                    ob == dialog_->check_sort ||
223                    ob == dialog_->choice_buffer) {
224
225                 if (ob == dialog_->button_update ||
226                     ob == dialog_->choice_buffer) {
227                         string const name =
228                                 controller().getBufferName(fl_get_choice(dialog_->choice_buffer) - 1);
229                         refs_ = controller().getLabelList(name);
230                 }
231
232                 fl_freeze_form(form());
233                 updateBrowser(refs_);
234                 fl_unfreeze_form(form());
235
236         } else if (ob == dialog_->choice_type) {
237
238                 int const type = fl_get_choice(dialog_->choice_type) - 1;
239                 if (controller().params().getCmdName() ==
240                     InsetRef::getName(type)) {
241                         activate = ButtonPolicy::SMI_NOOP;
242                 }
243         }
244
245         return activate;
246 }