]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormRef.C
Compile fixes for DEC cxx, John's maths and keymap patches.
[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         // Workaround dumb xforms sizing bug
74         minw_ = form()->w;
75         minh_ = form()->h;
76
77         // Force the user to use the browser to change refs.
78         fl_deactivate_object(dialog_->ref);
79
80         // Manage the ok and cancel/close buttons
81         bc().setOK(dialog_->button_ok);
82         bc().setApply(dialog_->button_apply);
83         bc().setCancel(dialog_->button_cancel);
84         bc().setUndoAll(dialog_->button_restore);
85         bc().refresh();
86
87 #warning I had to uncomment this so the buttons could be disabled in update() (dekel)
88         //bc().addReadOnly(dialog_->type);
89         //bc().addReadOnly(dialog_->name);
90 }
91
92
93 void FormRef::update()
94 {
95         if (inset_) {
96                 fl_set_input(dialog_->ref,  params.getContents().c_str());
97                 fl_set_input(dialog_->name, params.getOptions().c_str());
98                 fl_set_choice(dialog_->type, 
99                               InsetRef::getType(params.getCmdName()) + 1);
100         }
101
102         at_ref = false;
103         fl_set_object_label(dialog_->button_go, _("Goto reference"));
104
105         // Name is irrelevant to LaTeX/Literate documents, while
106         // type is irrelevant to LinuxDoc/DocBook.
107         if (lv_->buffer()->isLatex() || lv_->buffer()->isLatex()) {
108                 setEnabled(dialog_->name, false);
109                 setEnabled(dialog_->type, true);
110         } else {
111                 fl_set_choice(dialog_->type, 1);
112
113                 setEnabled(dialog_->name, true);
114                 setEnabled(dialog_->type, false);
115         }
116
117         refs = lv_->buffer()->getLabelList();
118         updateBrowser(refs);
119
120         bc().readOnly(lv_->buffer()->isReadonly());
121 }
122
123
124 void FormRef::updateBrowser(vector<string> const & akeys) const
125 {
126         vector<string> keys(akeys);
127         if (fl_get_button(dialog_->sort))
128                 sort(keys.begin(), keys.end());
129
130         fl_clear_browser(dialog_->browser);
131         for (vector<string>::const_iterator it = keys.begin();
132              it != keys.end(); ++it)
133                 fl_add_browser_line(dialog_->browser, (*it).c_str());
134
135         if (keys.empty()) {
136                 fl_add_browser_line(dialog_->browser,
137                                     _("*** No labels found in document ***"));
138         
139                 setEnabled(dialog_->browser, false);
140                 setEnabled(dialog_->sort,    false);
141
142                 fl_set_input(dialog_->ref, "");
143         } else {
144                 setEnabled(dialog_->browser, true);
145                 setEnabled(dialog_->sort,    true);
146
147                 string ref = fl_get_input(dialog_->ref);
148                 vector<string>::const_iterator cit =
149                         find(keys.begin(), keys.end(), ref);
150                 if (cit == keys.end()) {
151                         cit = keys.begin();
152                         fl_set_input(dialog_->ref, (*cit).c_str());
153                 } else if (ref.empty())
154                         fl_set_input(dialog_->ref, (*cit).c_str());
155
156                 int const i = static_cast<int>(cit - keys.begin());
157                 fl_set_browser_topline(dialog_->browser, max(i-5, 1));
158                 fl_select_browser_line(dialog_->browser, i+1);
159         }
160 }
161
162
163 void FormRef::apply()
164 {
165         if (!lv_->view()->available())
166                 return;
167
168         int const type = fl_get_choice(dialog_->type) - 1;
169         params.setCmdName(InsetRef::getName(type));
170
171         params.setOptions(fl_get_input(dialog_->name));
172         params.setContents(fl_get_input(dialog_->ref));
173
174         if (inset_ != 0) {
175                 // Only update if contents have changed
176                 if (params != inset_->params()) {
177                         inset_->setParams(params);
178                         lv_->view()->updateInset(inset_, true);
179                 }
180         } else {
181                 lv_->getLyXFunc()->Dispatch(LFUN_REF_INSERT,
182                                             params.getAsString());
183         }
184 }
185
186
187 bool FormRef::input(FL_OBJECT *, long data)
188 {
189         bool activate(true);
190         switch (data) {
191         // goto reference / go back
192         case 1:
193         {
194                 // No change to data
195                 activate = false;
196                 
197                 at_ref = !at_ref;
198                 if (at_ref) {
199                         lv_->getLyXFunc()->Dispatch(LFUN_BOOKMARK_SAVE, "0");
200                         lv_->getLyXFunc()->
201                                 Dispatch(LFUN_REF_GOTO,
202                                          fl_get_input(dialog_->ref));
203                         fl_set_object_label(dialog_->button_go, _("Go back"));
204                 } else {
205                         lv_->getLyXFunc()->Dispatch(LFUN_BOOKMARK_GOTO, "0");
206                         fl_set_object_label(dialog_->button_go,
207                                             _("Goto reference"));
208                 }
209         }
210         break;
211
212         // choose browser key
213         case 2:
214         {
215                 unsigned int sel = fl_get_browser(dialog_->browser);
216                 if (sel < 1 || sel > refs.size()) break;
217
218                 if (!lv_->buffer()->isReadonly()) {
219                         string s = fl_get_browser_line(dialog_->browser, sel);
220                         fl_set_input(dialog_->ref, s.c_str());
221                 }
222
223                 if (at_ref)
224                         lv_->getLyXFunc()->Dispatch(LFUN_BOOKMARK_GOTO, "0");
225                 at_ref = false;
226                 fl_set_object_label(dialog_->button_go, _("Goto reference"));
227
228                 setEnabled(dialog_->type,      true);
229                 setEnabled(dialog_->button_go, true);
230                 fl_set_object_lcol(dialog_->ref, FL_BLACK);
231         }
232         break;
233
234         // update or sort
235         case 3:
236                 refs = lv_->buffer()->getLabelList();
237
238                 // fall through to...
239         case 4:
240                 fl_freeze_form(form());
241                 updateBrowser(refs);
242                 fl_unfreeze_form(form());
243                 break;
244
245         // changed reference type
246         case 5:
247         {
248                 int const type = fl_get_choice(dialog_->type) - 1;
249                 if (params.getCmdName() == InsetRef::getName(type) && inset_) {
250                         activate = false;
251                 }
252         }
253         break;
254
255         default:
256                 break;
257         }
258
259         return activate;
260 }
261
262