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