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