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