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