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