]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormRef.C
2002-07-02 Lars Gullik Bj�nnes <larsbj@birdstep.com>
[lyx.git] / src / frontends / xforms / FormRef.C
1 /**
2  * \file xforms/FormRef.C
3  * Copyright 2000-2001 The LyX Team.
4  * See the file COPYING.
5  *
6  * \author Angus Leeming, a.leeming@ic.ac.uk
7  */
8
9 #include <config.h>
10 #include <algorithm>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "xformsBC.h"
17 #include "ControlRef.h"
18 #include "FormRef.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 FormCB<ControlRef, FormDB<FD_ref> > base_class;
32
33 FormRef::FormRef(ControlRef & c, Dialogs & d)
34         : base_class(c, d, _("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_type,
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         bc().setOK(dialog_->button_ok);
58         bc().setApply(dialog_->button_apply);
59         bc().setCancel(dialog_->button_close);
60         bc().setRestore(dialog_->button_restore);
61
62         bc().addReadOnly(dialog_->button_update);
63         bc().addReadOnly(dialog_->input_name);
64         bc().addReadOnly(dialog_->input_ref);
65 }
66
67
68 void FormRef::update()
69 {
70         fl_set_input(dialog_->input_ref,
71                      controller().params().getContents().c_str());
72         fl_set_input(dialog_->input_name,
73                      controller().params().getOptions().c_str());
74         fl_set_choice(dialog_->choice_type,
75                       InsetRef::getType(controller().params().getCmdName()) + 1);
76
77         at_ref_ = false;
78         fl_set_object_label(dialog_->button_go, _("Go to reference"));
79
80         // Name is irrelevant to LaTeX/Literate documents
81         if (controller().docType() == ControlRef::LATEX ||
82             controller().docType() == ControlRef::LITERATE) {
83                 setEnabled(dialog_->input_name, false);
84         } else {
85                 setEnabled(dialog_->input_name, true);
86         }
87
88         // type is irrelevant to LinuxDoc/DocBook.
89         if (controller().docType() == ControlRef::LINUXDOC ||
90             controller().docType() == ControlRef::DOCBOOK) {
91                 fl_set_choice(dialog_->choice_type, 1);
92                 setEnabled(dialog_->choice_type, false);
93         } else {
94                 setEnabled(dialog_->choice_type, true);
95         }
96
97         // Get the available buffers
98         vector<string> const buffers = controller().getBufferList();
99         vector<string> const choice_buffers =
100                 getVectorFromChoice(dialog_->choice_buffer);
101
102         // If different from the current contents of the choice, then update it
103         if (buffers != choice_buffers) {
104                 // create a string of entries " entry1 | entry2 | entry3 "
105                 // with which to initialise the xforms choice object.
106                 string const choice =
107                         " " + getStringFromVector(buffers, " | ") + " ";
108
109                 fl_clear_choice(dialog_->choice_buffer);
110                 fl_addto_choice(dialog_->choice_buffer, choice.c_str());
111
112                 fl_set_choice(dialog_->choice_buffer,
113                               controller().getBufferNum() + 1);
114         }
115
116         refs_ = controller().getLabelList(string());
117         updateBrowser(refs_);
118 }
119
120
121 void FormRef::updateBrowser(vector<string> const & akeys) const
122 {
123         vector<string> keys(akeys);
124         if (fl_get_button(dialog_->check_sort))
125                 sort(keys.begin(), keys.end());
126
127         vector<string> browser_keys =
128                 getVectorFromBrowser(dialog_->browser_refs);
129
130         if (browser_keys == keys)
131                 return;
132
133         fl_clear_browser(dialog_->browser_refs);
134         for (vector<string>::const_iterator it = keys.begin();
135              it != keys.end(); ++it)
136                 fl_add_browser_line(dialog_->browser_refs, it->c_str());
137
138         if (keys.empty()) {
139                 fl_add_browser_line(dialog_->browser_refs,
140                                     _("*** No labels found in document ***"));
141
142                 setEnabled(dialog_->browser_refs, false);
143                 setEnabled(dialog_->check_sort,   false);
144
145                 fl_set_input(dialog_->input_ref, "");
146         } else {
147                 setEnabled(dialog_->browser_refs, true);
148                 setEnabled(dialog_->check_sort,   true);
149
150                 string ref = fl_get_input(dialog_->input_ref);
151                 vector<string>::const_iterator cit = (ref.empty())
152                         ? keys.begin()
153                         : find(keys.begin(), keys.end(), ref);
154                 if (cit == keys.end()) {
155                         fl_deselect_browser(dialog_->browser_refs);
156                 } else {
157                         if (ref.empty())
158                                 fl_set_input(dialog_->input_ref, cit->c_str());
159
160                         int const i = static_cast<int>(cit - keys.begin());
161                         fl_set_browser_topline(dialog_->browser_refs, max(i-5, 1));
162                         fl_select_browser_line(dialog_->browser_refs, i+1);
163                 }
164         }
165 }
166
167
168 void FormRef::apply()
169 {
170         int const type = fl_get_choice(dialog_->choice_type) - 1;
171         controller().params().setCmdName(InsetRef::getName(type));
172
173         controller().params().setOptions(fl_get_input(dialog_->input_name));
174         controller().params().setContents(fl_get_input(dialog_->input_ref));
175 }
176
177
178 ButtonPolicy::SMInput FormRef::input(FL_OBJECT * ob, long)
179 {
180         ButtonPolicy::SMInput activate(ButtonPolicy::SMI_VALID);
181
182         if (ob == dialog_->button_go) {
183                 // goto reference / go back
184
185                 // No change to data
186                 activate = ButtonPolicy::SMI_NOOP;
187
188                 at_ref_ = !at_ref_;
189                 if (at_ref_) {
190                         controller().gotoRef(fl_get_input(dialog_->input_ref));
191                         fl_set_object_label(dialog_->button_go, _("Go back"));
192                 } else {
193                         controller().gotoBookmark();
194                         fl_set_object_label(dialog_->button_go,
195                                             _("Go to reference"));
196                 }
197
198         } else if (ob == dialog_->browser_refs) {
199
200                 unsigned int sel = fl_get_browser(dialog_->browser_refs);
201                 if (sel < 1 || sel > refs_.size())
202                         return ButtonPolicy::SMI_NOOP;
203
204                 if (!controller().isReadonly()) {
205                         string s = fl_get_browser_line(dialog_->browser_refs, sel);
206                         fl_set_input(dialog_->input_ref, s.c_str());
207                 }
208
209                 if (at_ref_)
210                         controller().gotoBookmark();
211                 at_ref_ = false;
212                 fl_set_object_label(dialog_->button_go, _("Go to reference"));
213
214                 setEnabled(dialog_->choice_type,      true);
215                 setEnabled(dialog_->button_go, true);
216                 fl_set_object_lcol(dialog_->input_ref, FL_BLACK);
217
218         } else if (ob == dialog_->button_update ||
219                    ob == dialog_->check_sort ||
220                    ob == dialog_->choice_buffer) {
221
222                 if (ob == dialog_->button_update ||
223                     ob == dialog_->choice_buffer) {
224                         string const name =
225                                 controller().getBufferName(fl_get_choice(dialog_->choice_buffer) - 1);
226                         refs_ = controller().getLabelList(name);
227                 }
228
229                 fl_freeze_form(form());
230                 updateBrowser(refs_);
231                 fl_unfreeze_form(form());
232
233         } else if (ob == dialog_->choice_type) {
234
235                 int const type = fl_get_choice(dialog_->choice_type) - 1;
236                 if (controller().params().getCmdName() ==
237                     InsetRef::getName(type)) {
238                         activate = ButtonPolicy::SMI_NOOP;
239                 }
240         }
241
242         return activate;
243 }