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