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