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