]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormRef.C
Choose labels from other documents
[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_->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_->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_->name);
62         bc().addReadOnly(dialog_->ref);
63 }
64
65
66 void FormRef::update()
67 {
68         fl_set_input(dialog_->ref,
69                      controller().params().getContents().c_str());
70         fl_set_input(dialog_->name,
71                      controller().params().getOptions().c_str());
72         fl_set_choice(dialog_->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_->name, false);
82         } else {
83                 setEnabled(dialog_->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_->type, 1);
90                 setEnabled(dialog_->type, false);
91         } else {
92                 setEnabled(dialog_->type, true);
93         }
94
95         string const choice =
96                 " " + getStringFromVector(controller().getBufferList(), " | ") + " ";
97         fl_clear_choice(dialog_->buffer);
98         fl_addto_choice(dialog_->buffer, choice.c_str());
99         fl_set_choice(dialog_->buffer, controller().getBufferNum() + 1);
100
101         refs_ = controller().getLabelList(string());
102         updateBrowser(refs_);
103 }
104
105
106 void FormRef::updateBrowser(vector<string> const & akeys) const
107 {
108         vector<string> keys(akeys);
109         if (fl_get_button(dialog_->sort))
110                 sort(keys.begin(), keys.end());
111
112         fl_clear_browser(dialog_->browser);
113         for (vector<string>::const_iterator it = keys.begin();
114              it != keys.end(); ++it)
115                 fl_add_browser_line(dialog_->browser, it->c_str());
116
117         if (keys.empty()) {
118                 fl_add_browser_line(dialog_->browser,
119                                     _("*** No labels found in document ***"));
120         
121                 setEnabled(dialog_->browser, false);
122                 setEnabled(dialog_->sort,    false);
123
124                 fl_set_input(dialog_->ref, "");
125         } else {
126                 setEnabled(dialog_->browser, true);
127                 setEnabled(dialog_->sort,    true);
128
129                 string ref = fl_get_input(dialog_->ref);
130                 vector<string>::const_iterator cit = (ref.empty())
131                         ? keys.begin()
132                         : find(keys.begin(), keys.end(), ref);
133                 if (cit == keys.end()) {
134                         fl_deselect_browser(dialog_->browser);
135                 } else {
136                         if (ref.empty())
137                                 fl_set_input(dialog_->ref, cit->c_str());
138
139                         int const i = static_cast<int>(cit - keys.begin());
140                         fl_set_browser_topline(dialog_->browser, max(i-5, 1));
141                         fl_select_browser_line(dialog_->browser, i+1);
142                 }
143         }
144 }
145
146
147 void FormRef::apply()
148 {
149         int const type = fl_get_choice(dialog_->type) - 1;
150         controller().params().setCmdName(InsetRef::getName(type));
151
152         controller().params().setOptions(fl_get_input(dialog_->name));
153         controller().params().setContents(fl_get_input(dialog_->ref));
154 }
155
156
157 ButtonPolicy::SMInput FormRef::input(FL_OBJECT * ob, long)
158 {
159         ButtonPolicy::SMInput activate(ButtonPolicy::SMI_VALID);
160
161         if (ob == dialog_->button_go) {
162                 // goto reference / go back
163
164                 // No change to data
165                 activate = ButtonPolicy::SMI_NOOP;
166
167                 at_ref_ = !at_ref_;
168                 if (at_ref_) {
169                         controller().gotoRef(fl_get_input(dialog_->ref));
170                         fl_set_object_label(dialog_->button_go, _("Go back"));
171                 } else {
172                         controller().gotoBookmark();
173                         fl_set_object_label(dialog_->button_go,
174                                             _("Go to reference"));
175                 }
176
177         } else if (ob == dialog_->browser) {
178
179                 unsigned int sel = fl_get_browser(dialog_->browser);
180                 if (sel < 1 || sel > refs_.size())
181                         return ButtonPolicy::SMI_NOOP;
182
183                 if (!controller().isReadonly()) {
184                         string s = fl_get_browser_line(dialog_->browser, sel);
185                         fl_set_input(dialog_->ref, s.c_str());
186                 }
187
188                 if (at_ref_)
189                         controller().gotoBookmark();
190                 at_ref_ = false;
191                 fl_set_object_label(dialog_->button_go, _("Go to reference"));
192
193                 setEnabled(dialog_->type,      true);
194                 setEnabled(dialog_->button_go, true);
195                 fl_set_object_lcol(dialog_->ref, FL_BLACK);
196
197         } else if (ob == dialog_->button_update || 
198                    ob == dialog_->sort ||
199                    ob == dialog_->buffer) {
200
201                 if (ob == dialog_->button_update ||
202                     ob == dialog_->buffer) {
203                         string const name =
204                                 frontStrip(strip(fl_get_choice_text(dialog_->buffer)));
205                         refs_ = controller().getLabelList(name);
206                 }
207
208                 fl_freeze_form(form());
209                 updateBrowser(refs_);
210                 fl_unfreeze_form(form());
211
212         } else if (ob == dialog_->type) {
213
214                 int const type = fl_get_choice(dialog_->type) - 1;
215                 if (controller().params().getCmdName() ==
216                     InsetRef::getName(type)) {
217                         activate = ButtonPolicy::SMI_NOOP;
218                 }
219         }
220
221         return activate;
222 }