]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormRef.C
Removed // -*- C++ -*- from all .C files!
[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
59
60 void FormRef::update()
61 {
62         fl_set_input(dialog_->ref,
63                      controller().params().getContents().c_str());
64         fl_set_input(dialog_->name,
65                      controller().params().getOptions().c_str());
66         fl_set_choice(dialog_->type, 
67                       InsetRef::getType(controller().params().getCmdName()) + 1);
68
69         at_ref_ = false;
70         fl_set_object_label(dialog_->button_go, _("Goto reference"));
71
72         // Name is irrelevant to LaTeX/Literate documents
73         if (controller().docType() == ControlRef::LATEX ||
74             controller().docType() == ControlRef::LITERATE) {
75                 setEnabled(dialog_->name, false);
76         } else {
77                 setEnabled(dialog_->name, true);
78         }
79
80         // type is irrelevant to LinuxDoc/DocBook.
81         if (controller().docType() == ControlRef::LINUXDOC ||
82             controller().docType() == ControlRef::DOCBOOK) {
83                 fl_set_choice(dialog_->type, 1);
84                 setEnabled(dialog_->type, false);
85         } else {
86                 setEnabled(dialog_->type, true);
87         }
88
89         refs_ = controller().getLabelList();
90         updateBrowser(refs_);
91 }
92
93
94 void FormRef::updateBrowser(vector<string> const & akeys) const
95 {
96         vector<string> keys(akeys);
97         if (fl_get_button(dialog_->sort))
98                 sort(keys.begin(), keys.end());
99
100         fl_clear_browser(dialog_->browser);
101         for (vector<string>::const_iterator it = keys.begin();
102              it != keys.end(); ++it)
103                 fl_add_browser_line(dialog_->browser, it->c_str());
104
105         if (keys.empty()) {
106                 fl_add_browser_line(dialog_->browser,
107                                     _("*** No labels found in document ***"));
108         
109                 setEnabled(dialog_->browser, false);
110                 setEnabled(dialog_->sort,    false);
111
112                 fl_set_input(dialog_->ref, "");
113         } else {
114                 setEnabled(dialog_->browser, true);
115                 setEnabled(dialog_->sort,    true);
116
117                 string ref = fl_get_input(dialog_->ref);
118                 vector<string>::const_iterator cit =
119                         find(keys.begin(), keys.end(), ref);
120                 if (cit == keys.end()) {
121                         cit = keys.begin();
122                         fl_set_input(dialog_->ref, cit->c_str());
123                 } else if (ref.empty())
124                         fl_set_input(dialog_->ref, cit->c_str());
125
126                 int const i = static_cast<int>(cit - keys.begin());
127                 fl_set_browser_topline(dialog_->browser, max(i-5, 1));
128                 fl_select_browser_line(dialog_->browser, i+1);
129         }
130 }
131
132
133 void FormRef::apply()
134 {
135         int const type = fl_get_choice(dialog_->type) - 1;
136         controller().params().setCmdName(InsetRef::getName(type));
137
138         controller().params().setOptions(fl_get_input(dialog_->name));
139         controller().params().setContents(fl_get_input(dialog_->ref));
140 }
141
142
143 ButtonPolicy::SMInput FormRef::input(FL_OBJECT * ob, long)
144 {
145         ButtonPolicy::SMInput activate(ButtonPolicy::SMI_VALID);
146
147         if (ob == dialog_->button_go) {
148                 // goto reference / go back
149
150                 // No change to data
151                 activate = ButtonPolicy::SMI_NOOP;
152
153                 at_ref_ = !at_ref_;
154                 if (at_ref_) {
155                         controller().gotoRef(fl_get_input(dialog_->ref));
156                         fl_set_object_label(dialog_->button_go, _("Go back"));
157                 } else {
158                         controller().gotoBookmark();
159                         fl_set_object_label(dialog_->button_go,
160                                             _("Goto reference"));
161                 }
162
163         } else if (ob == dialog_->browser) {
164
165                 unsigned int sel = fl_get_browser(dialog_->browser);
166                 if (sel < 1 || sel > refs_.size())
167                         return ButtonPolicy::SMI_NOOP;
168
169                 if (!controller().isReadonly()) {
170                         string s = fl_get_browser_line(dialog_->browser, sel);
171                         fl_set_input(dialog_->ref, s.c_str());
172                 }
173
174                 if (at_ref_)
175                         controller().gotoBookmark();
176                 at_ref_ = false;
177                 fl_set_object_label(dialog_->button_go, _("Goto reference"));
178
179                 setEnabled(dialog_->type,      true);
180                 setEnabled(dialog_->button_go, true);
181                 fl_set_object_lcol(dialog_->ref, FL_BLACK);
182
183         } else if (ob == dialog_->button_update || 
184                    ob == dialog_->sort) {
185
186                 if (ob == dialog_->button_update)
187                         refs_ = controller().getLabelList();
188
189                 fl_freeze_form(form());
190                 updateBrowser(refs_);
191                 fl_unfreeze_form(form());
192
193         } else if (ob == dialog_->type) {
194
195                 int const type = fl_get_choice(dialog_->type) - 1;
196                 if (controller().params().getCmdName() ==
197                     InsetRef::getName(type)) {
198                         activate = ButtonPolicy::SMI_NOOP;
199                 }
200         }
201
202         return activate;
203 }