]> git.lyx.org Git - lyx.git/blob - src/frontends/kde/FormRef.C
small cleanup, doxygen, formatting changes
[lyx.git] / src / frontends / kde / FormRef.C
1 /**
2  * \file FormRef.C
3  * Copyright 2001 the LyX Team
4  * Read the file COPYING
5  *
6  * \author John Levon
7  */
8
9 #include <config.h>
10
11 #include "Dialogs.h"
12 #include "FormRef.h"
13 #include "gettext.h"
14 #include "buffer.h"
15 #include "LyXView.h"
16 #include "lyxfunc.h"
17 #include "refdlg.h"
18 #include "debug.h"
19 #include "insets/insetref.h"
20
21 #include <qtooltip.h>
22
23 using std::endl;
24
25 FormRef::FormRef(LyXView *v, Dialogs *d)
26         : dialog_(0), lv_(v), d_(d), inset_(0), h_(0), u_(0), ih_(0),
27         sort(0), gotowhere(GOTOREF), refs(0)
28 {
29         d->showRef.connect(slot(this, &FormRef::showRef));
30         d->createRef.connect(slot(this, &FormRef::createRef));
31 }
32
33
34 FormRef::~FormRef()
35 {
36         delete dialog_;
37 }
38
39
40 void FormRef::showRef(InsetCommand * const inset)
41 {
42         // FIXME: when could inset be 0 here ?
43         if (inset==0)
44                 return;
45
46         inset_ = inset;
47         readonly = lv_->buffer()->isReadonly();
48         ih_ = inset_->hide.connect(slot(this,&FormRef::hide));
49         params = inset->params();
50         
51         show();
52 }
53
54
55 void FormRef::createRef(string const & arg)
56 {
57         if (inset_)
58                 close();
59
60         readonly = lv_->buffer()->isReadonly();
61         params.setFromString(arg);
62         show();
63 }
64
65
66 void FormRef::select(char const * text)
67 {
68         highlight(text);
69         goto_ref();
70 }
71
72
73 void FormRef::highlight(char const * text)
74 {
75         if (gotowhere==GOTOBACK)
76                 goto_ref();
77
78         dialog_->buttonGoto->setEnabled(true);
79         if (!readonly) {
80                 dialog_->type->setEnabled(true);
81                 dialog_->reference->setText(text);
82                 dialog_->buttonOk->setEnabled(true);
83         }
84 }
85
86
87 void FormRef::set_sort(bool on)
88 {
89         if (on != sort) {
90                 sort=on;
91                 dialog_->refs->clear();
92                 updateRefs();
93         }
94 }
95
96
97 void FormRef::goto_ref()
98 {
99         switch (gotowhere) {
100                 case GOTOREF:
101                         lv_->getLyXFunc()->Dispatch(LFUN_REF_GOTO, dialog_->reference->text());
102                         gotowhere=GOTOBACK;
103                         dialog_->buttonGoto->setText(_("&Go back"));
104                         QToolTip::remove(dialog_->buttonGoto); 
105                         QToolTip::add(dialog_->buttonGoto,_("Jump back to original position"));
106                         break;
107                 case GOTOBACK:
108                         lv_->getLyXFunc()->Dispatch(LFUN_BOOKMARK_GOTO, "0");
109                         gotowhere=GOTOREF;
110                         dialog_->buttonGoto->setText(_("&Goto reference"));
111                         QToolTip::remove(dialog_->buttonGoto); 
112                         QToolTip::add(dialog_->buttonGoto,_("Jump to selected reference"));
113                         break;
114                 }
115 }
116
117
118 void FormRef::updateRefs()
119 {
120         // list will be re-done, should go back if necessary
121         if (gotowhere == GOTOBACK) {
122                 lv_->getLyXFunc()->Dispatch(LFUN_BOOKMARK_GOTO, "0");
123                 gotowhere = GOTOREF;
124                 dialog_->buttonGoto->setText(_("&Goto reference"));
125                 QToolTip::remove(dialog_->buttonGoto); 
126                 QToolTip::add(dialog_->buttonGoto,_("Jump to selected reference"));
127         }
128
129         dialog_->refs->setAutoUpdate(false);
130
131         // need this because Qt will send a highlight() here for
132         // the first item inserted
133         string tmp(dialog_->reference->text());
134
135         for (vector< string >::const_iterator iter = refs.begin();
136                 iter != refs.end(); ++iter) {
137                 if (sort)
138                         dialog_->refs->inSort(iter->c_str());
139                 else
140                         dialog_->refs->insertItem(iter->c_str());
141         }
142
143         dialog_->reference->setText(tmp.c_str());
144
145         for (unsigned int i=0; i < dialog_->refs->count(); ++i) {
146                 if (!compare(dialog_->reference->text(),
147                              dialog_->refs->text(i)))
148                         dialog_->refs->setCurrentItem(i);
149         }
150
151         dialog_->refs->setAutoUpdate(true);
152         dialog_->refs->update();
153 }
154
155
156 void FormRef::do_ref_update()
157 {
158         refs.clear();
159         dialog_->refs->clear();
160         refs = lv_->buffer()->getLabelList();
161         if (!refs.empty())
162                 dialog_->sort->setEnabled(true);
163         updateRefs();
164 }
165
166
167 void FormRef::update(bool switched)
168 {
169         if (switched) {
170                 hide();
171                 return;
172         }
173
174         dialog_->reference->setText(params.getContents().c_str());
175         dialog_->refname->setText(params.getOptions().c_str());
176
177         if (inset_)
178                 dialog_->type->setCurrentItem(InsetRef::getType(params.getCmdName()));
179
180         lv_->getLyXFunc()->Dispatch(LFUN_BOOKMARK_SAVE, "0");
181         dialog_->buttonGoto->setText(_("&Goto reference"));
182         QToolTip::remove(dialog_->buttonGoto); 
183         QToolTip::add(dialog_->buttonGoto,_("Jump to selected reference"));
184
185         gotowhere = GOTOREF;
186
187         dialog_->sort->setChecked(sort);
188
189         do_ref_update();
190
191         dialog_->buttonGoto->setEnabled(params.getContents()!="");
192         dialog_->buttonOk->setEnabled(params.getContents()!="");
193
194         dialog_->type->setEnabled(!readonly);
195         dialog_->sort->setEnabled(!readonly);
196         dialog_->refs->setEnabled(!readonly);
197         dialog_->buttonOk->setEnabled(!readonly);
198         dialog_->buttonUpdate->setEnabled(!readonly);
199         if (readonly)
200                 dialog_->buttonCancel->setText(_("&Close"));
201         else
202                 dialog_->buttonCancel->setText(_("&Cancel"));
203 }
204
205
206 void FormRef::apply()
207 {
208         if (readonly)
209                 return;
210
211         if (!lv_->view()->available())
212                 return;
213
214         params.setCmdName(InsetRef::getName(dialog_->type->currentItem()));
215         params.setContents(dialog_->reference->text());
216         params.setOptions(dialog_->refname->text());
217
218         if (inset_ != 0) {
219                 if (params != inset_->params()) {
220                         inset_->setParams(params);
221                         lv_->view()->updateInset(inset_, true);
222                 }
223         } else
224                 lv_->getLyXFunc()->Dispatch(LFUN_REF_INSERT, params.getAsString().c_str());
225 }
226
227
228 void FormRef::show()
229 {
230         if (!dialog_)
231                 dialog_ = new RefDialog(this, 0, _("LyX: Cross Reference"), false);
232
233         if (!dialog_->isVisible()) {
234                 h_ = d_->hideBufferDependent.connect(slot(this, &FormRef::hide));
235                 u_ = d_->updateBufferDependent.connect(slot(this, &FormRef::update));
236         }
237
238         dialog_->raise();
239         dialog_->setActiveWindow();
240
241         update();
242         dialog_->show();
243 }
244
245
246 void FormRef::close()
247 {
248         h_.disconnect();
249         u_.disconnect();
250         ih_.disconnect();
251         inset_ = 0;
252 }
253
254
255 void FormRef::hide()
256 {
257         dialog_->hide();
258         close();
259 }