]> git.lyx.org Git - features.git/blob - src/frontends/gtk/GRef.C
Use the new InsetCommandParams interface (frontend part), from Ugras and me
[features.git] / src / frontends / gtk / GRef.C
1 /**
2  * \file GRef.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Spray
7  * \author Andreas Klostermann
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 // Too hard to make concept checks work with this file
14 #ifdef _GLIBCXX_CONCEPT_CHECKS
15 #undef _GLIBCXX_CONCEPT_CHECKS
16 #endif
17 #ifdef _GLIBCPP_CONCEPT_CHECKS
18 #undef _GLIBCPP_CONCEPT_CHECKS
19 #endif
20
21 #include "GRef.h"
22 #include "ControlRef.h"
23 #include "ghelpers.h"
24 #include "insets/insetref.h"
25 #include "debug.h"
26 #include "buffer.h"
27 #include "insets/insetnote.h"
28
29 #include <libglademm.h>
30
31 using lyx::docstring;
32
33 using std::string;
34 using std::vector;
35
36
37 namespace lyx {
38 namespace frontend {
39
40 class refModelColumns : public Gtk::TreeModel::ColumnRecord
41 {
42 public:
43
44   refModelColumns()
45     { add(name);}
46
47   Gtk::TreeModelColumn<Glib::ustring> name;
48 };
49
50 refModelColumns refColumns;
51
52
53 class bufferModelColumns : public Gtk::TreeModel::ColumnRecord
54 {
55 public:
56
57   bufferModelColumns()
58     { add(name);}
59
60   Gtk::TreeModelColumn<Glib::ustring> name;
61 };
62
63 bufferModelColumns bufferColumns;
64
65
66 GRef::GRef(Dialog & parent)
67         : GViewCB<ControlRef, GViewGladeB>(parent, _("Cross-reference"), false)
68 {}
69
70
71 void GRef::doBuild()
72 {
73         string const gladeName = findGladeFile("ref");
74         xml_ = Gnome::Glade::Xml::create(gladeName);
75         xml_->get_widget("Cancel", cancelbutton_);
76         setCancel(cancelbutton_);
77         xml_->get_widget("Apply", applybutton_);
78         setApply(applybutton_);
79         xml_->get_widget("OK", okbutton_);
80         setOK(okbutton_);
81
82         xml_->get_widget("Labels", refview_);
83         xml_->get_widget("Label", labelentry_);
84         xml_->get_widget("Name", nameentry_);
85         xml_->get_widget("Format", formatcombo_);
86         xml_->get_widget("Buffer", buffercombo_ );
87         xml_->get_widget("JumpTo", jumptobutton_);
88         xml_->get_widget("Back", backbutton_);
89         xml_->get_widget("Refresh", refreshbutton_);
90
91         refview_->append_column(lyx::to_utf8(_("Label")), refColumns.name);
92
93         buffercombo_->signal_changed().connect(
94                 sigc::mem_fun(*this, &GRef::buffer_changed));
95         refview_->signal_cursor_changed().connect(
96                 sigc::mem_fun(*this, &GRef::selection_changed));
97         refview_->signal_row_activated().connect(
98                 sigc::mem_fun(*this, &GRef::refview_activated));
99         jumptobutton_->signal_clicked().connect(
100                 sigc::mem_fun(*this, &GRef::jumpto));
101         backbutton_->signal_clicked().connect(
102                 sigc::mem_fun(*this, &GRef::back));
103         refreshbutton_->signal_clicked().connect(
104                 sigc::mem_fun(*this, &GRef::update_labels));
105
106         labelentry_->signal_changed().connect(
107                 sigc::mem_fun(*this, &GRef::update_validity));
108         formatcombo_->signal_changed().connect(
109                 sigc::mem_fun(*this, &GRef::update_validity));
110         nameentry_->signal_changed().connect(
111                 sigc::mem_fun(*this, &GRef::update_validity));
112
113         applylock_ = false;
114         bc().valid(false);
115 }
116
117
118 void GRef::selection_changed ()
119 {
120         if (applylock_)
121                 return;
122
123         Gtk::TreeModel::iterator iter = refview_->get_selection()->get_selected();
124         if(iter) {
125                 Gtk::TreeModel::Row row = *iter;
126                 labelentry_->set_text(row[refColumns.name]);
127         }
128 }
129
130
131 void GRef::jumpto()
132 {
133
134         if (backbutton_->is_sensitive()) {
135 //              controller().gotoAnotherRef(labelentry_->get_text());
136 //              OR
137 //              kernel().dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
138 //              OR
139                 controller().gotoBookmark();
140                 controller().gotoRef(labelentry_->get_text());
141         } else {
142                 controller().gotoRef(labelentry_->get_text());
143         }
144         backbutton_->set_sensitive(true);
145 }
146
147
148 void GRef::back()
149 {
150         controller().gotoBookmark();
151         backbutton_->set_sensitive(false);
152         jumptobutton_->set_sensitive(true);
153 }
154
155
156 void GRef::buffer_changed()
157 {
158         if (applylock_)
159                 return;
160         update_labels();
161 }
162
163
164 void GRef::update()
165 {
166         applylock_ = true;
167
168         bc().refreshReadOnly();
169         jumptobutton_->set_sensitive(true);
170         backbutton_->set_sensitive(false);
171         labelentry_->set_text(lyx::to_utf8(controller().params()["reference"]));
172         nameentry_->set_text(lyx::to_utf8(controller().params()["name"]));
173
174         // Name is irrelevant to LaTeX/Literate documents
175         Kernel::DocType doctype = kernel().docType();
176         if (doctype == Kernel::LATEX || doctype == Kernel::LITERATE) {
177                 nameentry_->set_sensitive(false);
178         } else {
179                 nameentry_->set_sensitive(true);
180         }
181
182         // Format is irrelevant to DocBook.
183         if (doctype == Kernel::DOCBOOK) {
184                 formatcombo_->set_active(0);
185                 formatcombo_->set_sensitive(false);
186
187         } else {
188                 formatcombo_->set_active(InsetRef::getType(controller().params().getCmdName()));
189                 formatcombo_->set_sensitive(true);
190         }
191
192         bufferstore_ = Gtk::ListStore::create(bufferColumns);
193         vector<string> const buffers = controller().getBufferList();
194         buffercombo_->set_model(bufferstore_);
195
196         vector<string>::const_iterator it = buffers.begin();
197         vector<string>::const_iterator const end = buffers.end();
198         for (; it != end; ++it) {
199                 Gtk::TreeModel::iterator iter = bufferstore_->append();
200                 (*iter)[bufferColumns.name]  = *it;
201         }
202
203         buffercombo_->set_active(controller().getBufferNum());
204
205         update_labels();
206         applylock_ = false;
207         bc().valid(false);
208 }
209
210
211 void GRef::update_labels()
212 {
213         int buffernum = buffercombo_->get_active_row_number();
214         if (buffernum < 0)
215                 buffernum=0;
216
217         string const name = controller().getBufferName(buffernum);
218         vector<docstring> keys = controller().getLabelList(name);
219         refListStore_ = Gtk::ListStore::create(refColumns);
220
221         if (!keys.empty()) {
222                 vector<docstring>::const_iterator it = keys.begin();
223                 vector<docstring>::const_iterator end = keys.end();
224                 for (;it != keys.end(); ++it) {
225                         Gtk::TreeModel::iterator iter =refListStore_->append();
226                         (*iter)[refColumns.name] = lyx::to_utf8(*it);
227                 }
228                 refview_->set_sensitive(true);
229         } else {
230                 Gtk::TreeModel::iterator iter =refListStore_->append();
231                 (*iter)[refColumns.name] = lyx::to_utf8(_("No labels found."));
232                 refview_->set_sensitive(false);
233         }
234         refview_->set_model(refListStore_);
235 }
236
237
238 void GRef::apply()
239 {
240         if (applylock_)
241                 return;
242
243         controller().params()["reference"] = lyx::from_utf8(labelentry_->get_text());
244         controller().params()["name"] = lyx::from_utf8(nameentry_->get_text());
245         int const type = formatcombo_->get_active_row_number();
246         controller().params().setCmdName(InsetRef::getName(type));
247 }
248
249
250 void GRef::update_validity()
251 {
252         bc().valid(!labelentry_->get_text().empty());
253 }
254
255
256 void GRef::refview_activated(const Gtk::TreeModel::Path&, Gtk::TreeViewColumn*)
257 {
258         if (!labelentry_->get_text().empty())
259                 okbutton_->clicked();
260 }
261
262
263 } // namespace frontend
264 } // namespace lyx