]> git.lyx.org Git - lyx.git/blob - src/frontends/kde/FormRef.C
Angus's FormInset work; Dekel's languages patch; my reworking of Angus's stuff +...
[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 "formrefdialog.h"
25 #include "debug.h"
26
27 using std::endl;
28
29 FormRef::FormRef(LyXView *v, Dialogs *d)
30         : dialog_(0), lv_(v), d_(d), inset_(0), h_(0), u_(0), ih_(0),
31         sort(0), gotowhere(GOTOREF), type(REF), refs(0)
32 {
33         // let the dialog be shown
34         // This is a permanent connection so we won't bother
35         // storing a copy because we won't be disconnecting.
36         d->showRef.connect(slot(this, &FormRef::showRef));
37         d->createRef.connect(slot(this, &FormRef::createRef));
38 }
39
40 FormRef::~FormRef()
41 {
42         delete dialog_;
43 }
44
45 void FormRef::showRef(InsetCommand * const inset)
46 {
47         // FIXME: when could inset be 0 here ?
48         if (inset==0)
49                 return;
50
51         inset_ = inset;
52         readonly = lv_->buffer()->isReadonly();
53         ih_ = inset_->hide.connect(slot(this,&FormRef::hide));
54         params = inset->params();
55         
56         show();
57 }
58
59 void FormRef::createRef(string const & arg)
60 {
61         if (inset_)
62                 close();
63
64         readonly = lv_->buffer()->isReadonly();
65         params.setFromString(arg);
66         show();
67 }
68
69 void FormRef::select(const char *text)
70 {
71         highlight(text);
72         goto_ref();
73 }
74
75 void FormRef::highlight(const char *text)
76 {
77         if (gotowhere==GOTOBACK)
78                 goto_ref();
79
80         dialog_->buttonGoto->setEnabled(true);
81         if (!readonly) {
82                 dialog_->type->setEnabled(true);
83                 dialog_->reference->setText(text);
84                 dialog_->buttonOk->setEnabled(true);
85         }
86 }
87
88 void FormRef::set_sort(bool on)
89 {
90         if (on!=sort) {
91                 sort=on;
92                 dialog_->refs->clear();
93                 updateRefs();
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                         break;
105                 case GOTOBACK:
106                         lv_->getLyXFunc()->Dispatch(LFUN_REF_BACK);
107                         gotowhere=GOTOREF;
108                         dialog_->buttonGoto->setText(_("&Goto reference"));
109                         break;
110                 }
111 }
112
113 void FormRef::updateRefs()
114 {
115         // list will be re-done, should go back if necessary
116         if (gotowhere==GOTOBACK) {
117                 lv_->getLyXFunc()->Dispatch(LFUN_REF_BACK);
118                 gotowhere = GOTOREF;
119                 dialog_->buttonGoto->setText(_("&Goto reference"));
120         }
121
122         dialog_->refs->setAutoUpdate(false);
123
124         // need this because Qt will send a highlight() here for
125         // the first item inserted
126         string tmp(dialog_->reference->text());
127
128         for (vector< string >::const_iterator iter = refs.begin();
129                 iter != refs.end(); ++iter) {
130                 if (sort)
131                         dialog_->refs->inSort(iter->c_str());
132                 else
133                         dialog_->refs->insertItem(iter->c_str());
134         }
135
136         dialog_->reference->setText(tmp.c_str());
137
138         for (unsigned int i = 0; i < dialog_->refs->count(); ++i) {
139                 if (!strcmp(dialog_->reference->text(),dialog_->refs->text(i)))
140                         dialog_->refs->setCurrentItem(i);
141         }
142
143         dialog_->refs->setAutoUpdate(true);
144         dialog_->refs->update();
145 }
146
147 void FormRef::do_ref_update()
148 {
149         refs.clear();
150         dialog_->refs->clear();
151         refs = lv_->buffer()->getLabelList();
152         if (!refs.empty())
153                 dialog_->sort->setEnabled(true);
154         updateRefs();
155 }
156
157 void FormRef::update(bool switched)
158 {
159         if (switched) {
160                 hide();
161                 return;
162         }
163
164         dialog_->reference->setText(params.getContents().c_str());
165         dialog_->refname->setText(params.getOptions().c_str());
166
167         if (params.getCmdName()=="pageref") {
168                 type = PAGEREF;
169                 dialog_->type->setCurrentItem(1);
170         } else if (params.getCmdName()=="vref") {
171                 type = VREF;
172                 dialog_->type->setCurrentItem(2);
173         } else if (params.getCmdName()=="vpageref") {
174                 type = VPAGEREF;
175                 dialog_->type->setCurrentItem(3);
176         } else if (params.getCmdName()=="prettyref") {
177                 type = PRETTYREF;
178                 dialog_->type->setCurrentItem(4);
179         } else {
180                 type = REF;
181                 dialog_->type->setCurrentItem(0);
182         }
183
184         dialog_->buttonGoto->setText(_("&Goto reference"));
185
186         gotowhere = GOTOREF;
187
188         dialog_->sort->setChecked(sort);
189
190         do_ref_update();
191
192         dialog_->buttonGoto->setEnabled(params.getContents()!="");
193         dialog_->buttonOk->setEnabled(params.getContents()!="");
194
195         dialog_->type->setEnabled(!readonly);
196         dialog_->sort->setEnabled(!readonly);
197         dialog_->refs->setEnabled(!readonly);
198         dialog_->buttonOk->setEnabled(!readonly);
199         dialog_->buttonUpdate->setEnabled(!readonly);
200         if (readonly)
201                 dialog_->buttonCancel->setText(_("&Close"));
202         else
203                 dialog_->buttonCancel->setText(_("&Cancel"));
204 }
205
206 void FormRef::apply()
207 {
208         if (readonly)
209                 return;
210
211         if (!lv_->view()->available())
212                 return;
213
214         switch (dialog_->type->currentItem()) {
215                 case 0:
216                         params.setCmdName("ref");
217                         break;
218                 case 1:
219                         params.setCmdName("pageref");
220                         break;
221                 case 2:
222                         params.setCmdName("vref");
223                         break;
224                 case 3:
225                         params.setCmdName("vpageref");
226                         break;
227                 case 4:
228                         params.setCmdName("prettyref");
229                         break;
230                 default:
231                         lyxerr[Debug::GUI] << "Unknown Ref Type" << endl;
232         }
233
234         params.setContents(dialog_->reference->text());
235         params.setOptions(dialog_->refname->text());
236
237         if (inset_ != 0) {
238                 if (params != inset_->params()) {
239                         inset_->setParams(params);
240                         lv_->view()->updateInset(inset_, true);
241                 }
242         } else
243                 lv_->getLyXFunc()->Dispatch(LFUN_REF_INSERT, params.getAsString().c_str());
244 }
245
246 void FormRef::show()
247 {
248         if (!dialog_)
249                 dialog_ = new FormRefDialog(this, 0, _("LyX: Cross Reference"), false);
250
251         if (!dialog_->isVisible()) {
252                 h_ = d_->hideBufferDependent.connect(slot(this, &FormRef::hide));
253                 u_ = d_->updateBufferDependent.connect(slot(this, &FormRef::update));
254         }
255
256         dialog_->raise();
257         dialog_->setActiveWindow();
258
259         update();
260         dialog_->show();
261 }
262
263 void FormRef::close()
264 {
265         h_.disconnect();
266         u_.disconnect();
267         ih_.disconnect();
268         inset_ = 0;
269 }
270
271 void FormRef::hide()
272 {
273         dialog_->hide();
274         close();
275 }