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