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