]> git.lyx.org Git - lyx.git/blob - src/frontends/kde/FormRef.C
KDE formprint patch
[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()
158 {
159         dialog_->reference->setText(params.getContents().c_str());
160         dialog_->refname->setText(params.getOptions().c_str());
161
162         if (params.getCmdName()=="pageref") {
163                 type = PAGEREF;
164                 dialog_->type->setCurrentItem(1);
165         } else if (params.getCmdName()=="vref") {
166                 type = VREF;
167                 dialog_->type->setCurrentItem(2);
168         } else if (params.getCmdName()=="vpageref") {
169                 type = VPAGEREF;
170                 dialog_->type->setCurrentItem(3);
171         } else if (params.getCmdName()=="prettyref") {
172                 type = PRETTYREF;
173                 dialog_->type->setCurrentItem(4);
174         } else {
175                 type = REF;
176                 dialog_->type->setCurrentItem(0);
177         }
178
179         dialog_->buttonGoto->setText(_("&Goto reference"));
180
181         gotowhere = GOTOREF;
182
183         dialog_->sort->setChecked(sort);
184
185         do_ref_update();
186
187         dialog_->buttonGoto->setEnabled(params.getContents()!="");
188         dialog_->buttonOk->setEnabled(params.getContents()!="");
189
190         dialog_->type->setEnabled(!readonly);
191         dialog_->sort->setEnabled(!readonly);
192         dialog_->refs->setEnabled(!readonly);
193         dialog_->buttonOk->setEnabled(!readonly);
194         dialog_->buttonUpdate->setEnabled(!readonly);
195         if (readonly)
196                 dialog_->buttonCancel->setText(_("&Close"));
197         else
198                 dialog_->buttonCancel->setText(_("&Cancel"));
199 }
200
201 void FormRef::apply()
202 {
203         if (readonly)
204                 return;
205
206         if (!lv_->view()->available())
207                 return;
208
209         switch (dialog_->type->currentItem()) {
210                 case 0:
211                         params.setCmdName("ref");
212                         break;
213                 case 1:
214                         params.setCmdName("pageref");
215                         break;
216                 case 2:
217                         params.setCmdName("vref");
218                         break;
219                 case 3:
220                         params.setCmdName("vpageref");
221                         break;
222                 case 4:
223                         params.setCmdName("prettyref");
224                         break;
225                 default:
226                         lyxerr[Debug::GUI] << "Unknown Ref Type" << endl;
227         }
228
229         params.setContents(dialog_->reference->text());
230         params.setOptions(dialog_->refname->text());
231
232         if (inset_ != 0) {
233                 if (params != inset_->params()) {
234                         inset_->setParams(params);
235                         lv_->view()->updateInset(inset_, true);
236                 }
237         } else
238                 lv_->getLyXFunc()->Dispatch(LFUN_REF_INSERT, params.getAsString().c_str());
239 }
240
241 void FormRef::show()
242 {
243         if (!dialog_)
244                 dialog_ = new FormRefDialog(this, 0, _("LyX: Cross Reference"), false);
245
246         if (!dialog_->isVisible()) {
247                 h_ = d_->hideBufferDependent.connect(slot(this, &FormRef::hide));
248                 u_ = d_->updateBufferDependent.connect(slot(this, &FormRef::update));
249         }
250
251         dialog_->raise();
252         dialog_->setActiveWindow();
253
254         update();
255         dialog_->show();
256 }
257
258 void FormRef::close()
259 {
260         h_.disconnect();
261         u_.disconnect();
262         ih_.disconnect();
263         inset_ = 0;
264 }
265
266 void FormRef::hide()
267 {
268         dialog_->hide();
269         close();
270 }