]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormRef.C
create and use a little setEnabled() xforms wrapper function.
[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 The LyX Team.
8  *
9  * ======================================================
10  */
11
12 #include <config.h>
13
14 #include <algorithm>
15 #include FORMS_H_LOCATION
16
17 #ifdef __GNUG__
18 #pragma implementation
19 #endif
20
21 #include "Dialogs.h"
22 #include "FormRef.h"
23 #include "LyXView.h"
24 #include "buffer.h"
25 #include "form_ref.h"
26 #include "lyxfunc.h"
27 #include "insets/insetref.h"
28 #include "xforms_helpers.h"
29
30 using std::find;
31 using std::max;
32 using std::sort;
33 using std::vector;
34
35 bool saved_position;
36
37 FormRef::FormRef(LyXView * lv, Dialogs * d)
38         : FormCommand(lv, d, _("Reference"), new NoRepeatedApplyPolicy),
39           at_ref(false), dialog_(0)
40 {
41         // let the dialog be shown
42         // These are permanent connections so we won't bother
43         // storing a copy because we won't be disconnecting.
44         d->showRef.connect(slot(this, &FormRef::showInset));
45         d->createRef.connect(slot(this, &FormRef::createInset));
46 }
47
48
49 FormRef::~FormRef()
50 {
51         delete dialog_;
52 }
53
54
55 FL_FORM * FormRef::form() const
56 {
57         if (dialog_) return dialog_->form;
58         return 0;
59 }
60
61
62 void FormRef::disconnect()
63 {
64         refs.clear();
65         FormCommand::disconnect();
66 }
67
68
69 void FormRef::build()
70 {
71         dialog_ = build_ref();
72
73         for (int i = 0; !InsetRef::types[i].latex_name.empty(); ++i)
74                 fl_addto_choice(dialog_->type,
75                                 _(InsetRef::types[i].gui_name.c_str()));
76
77         // Workaround dumb xforms sizing bug
78         minw_ = form()->w;
79         minh_ = form()->h;
80
81         // Force the user to use the browser to change refs.
82         fl_deactivate_object(dialog_->ref);
83
84         // Manage the ok and cancel/close buttons
85         bc_.setOK(dialog_->button_ok);
86         bc_.setApply(dialog_->button_apply);
87         bc_.setCancel(dialog_->button_cancel);
88         bc_.setUndoAll(dialog_->button_restore);
89         bc_.refresh();
90
91 #warning I had to uncomment this so the buttons could be disabled in update() (dekel)
92         //bc_.addReadOnly(dialog_->type);
93         //bc_.addReadOnly(dialog_->name);
94 }
95
96
97 void FormRef::update()
98 {
99         if (inset_) {
100                 fl_set_input(dialog_->ref,  params.getContents().c_str());
101                 fl_set_input(dialog_->name, params.getOptions().c_str());
102                 fl_set_choice(dialog_->type, 
103                               InsetRef::getType(params.getCmdName()) + 1);
104         }
105
106         at_ref = false;
107         fl_set_object_label(dialog_->button_go, _("Goto reference"));
108
109         // Name is irrelevant to LaTeX/Literate documents, while
110         // type is irrelevant to LinuxDoc/DocBook.
111         if (lv_->buffer()->isLatex() || lv_->buffer()->isLatex()) {
112                 setEnabled(dialog_->name, false);
113                 setEnabled(dialog_->type, true);
114         } else {
115                 fl_set_choice(dialog_->type, 1);
116
117                 setEnabled(dialog_->name, true);
118                 setEnabled(dialog_->type, false);
119         }
120
121         refs = lv_->buffer()->getLabelList();
122         updateBrowser(refs);
123
124         bc_.readOnly(lv_->buffer()->isReadonly());
125 }
126
127
128 void FormRef::updateBrowser(vector<string> const & akeys) const
129 {
130         vector<string> keys(akeys);
131         if (fl_get_button(dialog_->sort))
132                 sort(keys.begin(), keys.end());
133
134         fl_clear_browser(dialog_->browser);
135         for (vector<string>::const_iterator it = keys.begin();
136              it != keys.end(); ++it)
137                 fl_add_browser_line(dialog_->browser, (*it).c_str());
138
139         if (keys.empty()) {
140                 fl_add_browser_line(dialog_->browser,
141                                     _("*** No labels found in document ***"));
142         
143                 setEnabled(dialog_->browser, false);
144                 setEnabled(dialog_->sort,    false);
145
146                 fl_set_input(dialog_->ref, "");
147         } else {
148                 setEnabled(dialog_->browser, true);
149                 setEnabled(dialog_->sort,    true);
150
151                 string ref = fl_get_input(dialog_->ref);
152                 vector<string>::const_iterator cit =
153                         find(keys.begin(), keys.end(), ref);
154                 if (cit == keys.end()) {
155                         cit = keys.begin();
156                         fl_set_input(dialog_->ref, (*cit).c_str());
157                 } else if (ref.empty())
158                         fl_set_input(dialog_->ref, (*cit).c_str());
159
160                 int const i = static_cast<int>(cit - keys.begin());
161                 fl_set_browser_topline(dialog_->browser, max(i-5, 1));
162                 fl_select_browser_line(dialog_->browser, i+1);
163         }
164 }
165
166
167 void FormRef::apply()
168 {
169         if (!lv_->view()->available())
170                 return;
171
172         int const type = fl_get_choice(dialog_->type) - 1;
173         params.setCmdName(InsetRef::getName(type));
174
175         params.setOptions(fl_get_input(dialog_->name));
176         params.setContents(fl_get_input(dialog_->ref));
177
178         if (inset_ != 0) {
179                 // Only update if contents have changed
180                 if (params != inset_->params()) {
181                         inset_->setParams(params);
182                         lv_->view()->updateInset(inset_, true);
183                 }
184         } else {
185                 lv_->getLyXFunc()->Dispatch(LFUN_REF_INSERT,
186                                             params.getAsString());
187         }
188 }
189
190
191 bool FormRef::input(FL_OBJECT *, long data)
192 {
193         bool activate(true);
194         switch (data) {
195         // goto reference / go back
196         case 1:
197         {
198                 // No change to data
199                 activate = false;
200                 
201                 at_ref = !at_ref;
202                 if (at_ref) {
203                         lv_->getLyXFunc()->Dispatch(LFUN_BOOKMARK_SAVE, "0");
204                         lv_->getLyXFunc()->
205                                 Dispatch(LFUN_REF_GOTO,
206                                          fl_get_input(dialog_->ref));
207                         fl_set_object_label(dialog_->button_go, _("Go back"));
208                 } else {
209                         lv_->getLyXFunc()->Dispatch(LFUN_BOOKMARK_GOTO, "0");
210                         fl_set_object_label(dialog_->button_go,
211                                             _("Goto reference"));
212                 }
213         }
214         break;
215
216         // choose browser key
217         case 2:
218         {
219                 unsigned int sel = fl_get_browser(dialog_->browser);
220                 if (sel < 1 || sel > refs.size()) break;
221
222                 if (!lv_->buffer()->isReadonly()) {
223                         string s = fl_get_browser_line(dialog_->browser, sel);
224                         fl_set_input(dialog_->ref, s.c_str());
225                 }
226
227                 if (at_ref)
228                         lv_->getLyXFunc()->Dispatch(LFUN_BOOKMARK_GOTO, "0");
229                 at_ref = false;
230                 fl_set_object_label(dialog_->button_go, _("Goto reference"));
231
232                 setEnabled(dialog_->type,      true);
233                 setEnabled(dialog_->button_go, true);
234                 fl_set_object_lcol(dialog_->ref, FL_BLACK);
235         }
236         break;
237
238         // update or sort
239         case 3:
240                 refs = lv_->buffer()->getLabelList();
241
242                 // fall through to...
243         case 4:
244                 fl_freeze_form(form());
245                 updateBrowser(refs);
246                 fl_unfreeze_form(form());
247                 break;
248
249         // changed reference type
250         case 5:
251         {
252                 int const type = fl_get_choice(dialog_->type) - 1;
253                 if (params.getCmdName() == InsetRef::getName(type) && inset_) {
254                         activate = false;
255                 }
256         }
257         break;
258
259         default:
260                 break;
261         }
262
263         return activate;
264 }
265
266