]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/GCitation.C
292ef9e0bfd792f8df3bd3e0e3b8e706fc36502c
[lyx.git] / src / frontends / gtk / GCitation.C
1 /**
2  * \file GCitation.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Bernhard Reiter
7  *
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 "GCitation.h"
22 #include "ControlCitation.h"
23 #include "ghelpers.h"
24
25 #include "bufferparams.h"
26
27 #include "support/lstrings.h"
28
29 #include <libglademm.h>
30
31 using std::string;
32 using std::vector;
33
34 namespace lyx {
35 namespace frontend {
36
37 class bibModelColumns : public Gtk::TreeModel::ColumnRecord
38 {
39 public:
40
41         bibModelColumns() { add(name); add(cite); add(bib_order); add(info); }
42
43         Gtk::TreeModelColumn<Glib::ustring> name;
44         Gtk::TreeModelColumn<bool> cite;
45         Gtk::TreeModelColumn<int> bib_order;
46         Gtk::TreeModelColumn<Glib::ustring> info;
47 };
48
49 bibModelColumns bibColumns;
50
51 class styleModelColumns : public Gtk::TreeModel::ColumnRecord
52 {
53 public:
54
55         styleModelColumns() { add(name); }
56
57         Gtk::TreeModelColumn<Glib::ustring> name;
58 };
59
60 bool GCitation::bib_visible(const Gtk::TreeModel::const_iterator& iter)
61 {
62         if (iter)
63                 return !((*iter)[bibColumns.cite]);
64         return true;
65 }
66
67 styleModelColumns styleColumns;
68
69
70 GCitation::GCitation(Dialog & parent)
71         : GViewCB<ControlCitation, GViewGladeB>(parent, _("Citation"), false)
72 {}
73
74
75 void GCitation::doBuild()
76 {
77         string const gladeName = findGladeFile("citation");
78         xml_ = Gnome::Glade::Xml::create(gladeName);
79
80         xml_->get_widget("Restore", restorebutton_);
81         setRestore(restorebutton_);
82         xml_->get_widget("Cancel", cancelbutton_);
83         setCancel(cancelbutton_);
84         xml_->get_widget("Apply", applybutton_);
85         setApply(applybutton_);
86         xml_->get_widget("OK", okbutton_);
87         setOK(okbutton_);
88
89         xml_->get_widget("Remove", removebutton_);
90         xml_->get_widget("Add", addbutton_);
91         xml_->get_widget("Back", backbutton_);
92         xml_->get_widget("Forward", forwardbutton_);
93         xml_->get_widget("Up", upbutton_);
94         xml_->get_widget("Down", downbutton_);
95
96         xml_->get_widget("CiteKeys", citekeysview_);
97         xml_->get_widget("BibKeys", bibkeysview_);
98         xml_->get_widget("Info", infoview_);
99         xml_->get_widget("CaseSensitive", casecheck_);
100
101         xml_->get_widget("SearchCite", citeradio_);
102         xml_->get_widget("SearchBib", bibradio_);
103         xml_->get_widget("SearchString", findentry_);
104         xml_->get_widget("CaseSensitive", casecheck_);
105         xml_->get_widget("RegularExpression", regexpcheck_);
106
107         xml_->get_widget("StyleLabel", stylelabel_);
108         xml_->get_widget("Style", stylecombo_);
109         xml_->get_widget("TextBeforeLabel", beforelabel_);
110         xml_->get_widget("TextBefore", beforeentry_);
111         xml_->get_widget("TextAfter", afterentry_);
112         xml_->get_widget("FullAuthorList", authorcheck_);
113         xml_->get_widget("ForceUpperCase", uppercasecheck_);
114
115         info_ = Gtk::TextBuffer::create();
116         infoview_->set_buffer(info_);
117
118         allListStore_ = Gtk::ListStore::create(bibColumns);
119
120         Gtk::TreeModel::Path rootpath; //required for gtkmm < 2.6
121
122         citekeysview_->append_column(_("CiteKeys"), bibColumns.name);
123         citeFilter_ = Gtk::TreeModelFilter::create(allListStore_, rootpath);
124         citeFilter_->set_visible_column(bibColumns.cite);
125         citekeysview_->set_model(citeFilter_);
126         citeselection_ = citekeysview_->get_selection();
127
128         bibkeysview_->append_column(_("BibKeys"), bibColumns.name);
129         bibSort_ = Gtk::TreeModelSort::create(allListStore_);
130         bibSort_->set_sort_column(bibColumns.bib_order, Gtk::SORT_ASCENDING );
131         bibFilter_ = Gtk::TreeModelFilter::create(bibSort_, rootpath);
132         bibFilter_->set_visible_func(&GCitation::bib_visible);
133         bibkeysview_->set_model(bibFilter_);
134         bibselection_ = bibkeysview_->get_selection();
135
136         styleStore_ = Gtk::ListStore::create(styleColumns);
137         stylecombo_->set_model(styleStore_);
138
139         upbutton_->signal_clicked().connect(
140                 sigc::mem_fun(*this, &GCitation::up));
141         downbutton_->signal_clicked().connect(
142                 sigc::mem_fun(*this, &GCitation::down));
143         addbutton_->signal_clicked().connect(
144                 sigc::mem_fun(*this, &GCitation::add));
145         removebutton_->signal_clicked().connect(
146                 sigc::mem_fun(*this, &GCitation::remove));
147         backbutton_->signal_clicked().connect(
148                 sigc::mem_fun(*this, &GCitation::previous));
149         forwardbutton_->signal_clicked().connect(
150                 sigc::mem_fun(*this, &GCitation::next));
151
152         bibradio_->signal_toggled().connect(
153                 sigc::mem_fun(*this, &GCitation::set_search_buttons));
154         citeradio_->signal_toggled().connect(
155                 sigc::mem_fun(*this, &GCitation::set_search_buttons));
156         findentry_->signal_changed().connect(
157                 sigc::mem_fun(*this, &GCitation::set_search_buttons));
158
159         bibkeysview_->signal_row_activated().connect(
160                 sigc::mem_fun(*this, &GCitation::bibkeysview_activated));
161         bibselection_->signal_changed().connect(
162                 sigc::mem_fun(*this, &GCitation::bib_selected));
163         
164         citeselection_->signal_changed().connect(
165                 sigc::mem_fun(*this, &GCitation::cite_selected));
166
167         beforeentry_->signal_changed().connect(
168                 sigc::mem_fun(*this, &GCitation::enable_apply));
169         afterentry_->signal_changed().connect(
170                 sigc::mem_fun(*this, &GCitation::enable_apply));
171         stylecombo_->signal_changed().connect(
172                 sigc::mem_fun(*this, &GCitation::enable_apply));
173         authorcheck_->signal_toggled().connect(
174                 sigc::mem_fun(*this, &GCitation::enable_apply));
175         uppercasecheck_->signal_toggled().connect(
176                 sigc::mem_fun(*this, &GCitation::enable_apply));
177
178 }
179
180
181 void GCitation::enable_apply() {
182         // if we passed !applylock_ directly as an argument, the restore button
183         // would be activated
184         if (!applylock_ && !(citeFilter_->children()).empty())
185                 bc().valid(true);
186 }
187
188
189 void GCitation::fill_styles()
190 {
191         if ((citeFilter_->children()).empty()) {
192                 stylecombo_->set_sensitive(false);
193                 stylelabel_->set_sensitive(false);
194                 return;
195         }
196
197         int orig = stylecombo_->get_active_row_number();
198
199         Gtk::TreeModel::iterator iter = citeselection_->get_selected();
200         if(!iter)
201                 iter = (citeFilter_->children()).begin();
202         string key = Glib::locale_from_utf8((*iter)[bibColumns.name]);
203
204         std::vector<string> const & sty = controller().getCiteStrings(key);
205
206         biblio::CiteEngine const engine = controller().getEngine();
207         bool const basic_engine = engine == biblio::ENGINE_BASIC;
208
209         stylecombo_->set_sensitive(!sty.empty() && !basic_engine);
210         stylelabel_->set_sensitive(!sty.empty() && !basic_engine);
211
212         vector<string>::const_iterator it = sty.begin();
213         vector<string>::const_iterator const end = sty.end();
214
215         styleStore_->clear();
216         for (; it != end; ++it) {
217                 Gtk::TreeModel::iterator iter2 = styleStore_->append();
218                 (*iter2)[styleColumns.name] = Glib::locale_to_utf8(*it);
219         }
220
221         if(orig)
222                 stylecombo_->set_active(orig);
223 }
224
225
226 void GCitation::update_style()
227 {
228         biblio::CiteEngine const engine = controller().getEngine();
229         bool const natbib_engine =
230                 engine == biblio::ENGINE_NATBIB_AUTHORYEAR ||
231                 engine == biblio::ENGINE_NATBIB_NUMERICAL;
232         bool const basic_engine = engine == biblio::ENGINE_BASIC;
233
234         authorcheck_->set_sensitive(natbib_engine);
235         uppercasecheck_->set_sensitive(natbib_engine);
236         beforeentry_->set_sensitive(!basic_engine);
237         beforelabel_->set_sensitive(!basic_engine);
238
239         string const & command = controller().params().getCmdName();
240
241         // Find the style of the citekeys
242         vector<biblio::CiteStyle> const & styles =
243                 ControlCitation::getCiteStyles();
244         biblio::CitationStyle const cs(command);
245
246         vector<biblio::CiteStyle>::const_iterator cit =
247                 std::find(styles.begin(), styles.end(), cs.style);
248
249         //restore the latest natbib style
250         if (style_ >= 0 && Gtk::TreeModel::Children::size_type(style_) <
251                 (styleStore_->children()).size())
252                         stylecombo_->set_active(style_);
253         else
254                 stylecombo_->unset_active();
255
256         authorcheck_->set_active(false);
257         uppercasecheck_->set_active(false);
258
259         if (cit != styles.end()) {
260                 stylecombo_->set_active(cit - styles.begin());
261                 authorcheck_->set_active(cs.full);
262                 uppercasecheck_->set_active(cs.forceUCase);
263         }
264 }
265
266
267 void GCitation::update_contents()
268 {
269         // Make a list of all available bibliography keys
270         biblio::InfoMap const & theMap = controller().bibkeysInfo();
271         std::vector<std::string> bibkeys = biblio::getKeys(theMap);
272         std::vector<std::string> citekeys = support::getVectorFromString(
273                 controller().params().getContents());
274
275         int bib_order = 0;
276         allListStore_->clear();
277
278         for (std::vector<std::string>::const_iterator cit = bibkeys.begin();
279                 cit != bibkeys.end(); ++cit) {
280
281                 Gtk::TreeModel::iterator iter = allListStore_->append();
282                 (*iter)[bibColumns.name] = Glib::locale_to_utf8(*cit);
283                 (*iter)[bibColumns.cite] = false; //reset state
284                 (*iter)[bibColumns.bib_order] = ++bib_order;
285                 (*iter)[bibColumns.info] = Glib::locale_to_utf8(
286                         biblio::getInfo(theMap,*cit));
287         }
288
289         // Now mark cite keys by setting their bibColumns.cite property to true
290         // so they will be filtered and displayed in citekeysview_
291         for (std::vector<std::string>::const_iterator ccit = citekeys.begin();
292                 ccit != citekeys.end(); ++ccit) {
293
294                 bool found = false;
295                 for (Gtk::TreeModel::const_iterator cbit =
296                         (allListStore_->children()).begin();
297                         cbit != (allListStore_->children()).end(); ++cbit) {
298
299                         if ((*cbit)[bibColumns.name] == (*ccit)) {
300                                 found = true;
301                                 (*cbit)[bibColumns.cite] = true;
302                                 allListStore_->move(cbit,
303                                         (allListStore_->children()).end());
304                                 break;
305                         }
306                 }
307                 if (!found) {
308                         // It wasn't in the list of keys, but to support 
309                         // working on a document away from the bibtex file
310                         // we should keep it anyway.
311                         Gtk::TreeModel::iterator iter = allListStore_->append();
312                         (*iter)[bibColumns.name] = Glib::locale_to_utf8(*ccit);
313                         (*iter)[bibColumns.cite] = true;
314                         (*iter)[bibColumns.bib_order] = ++bib_order;
315                         (*iter)[bibColumns.info] = Glib::locale_to_utf8(
316                                 biblio::getInfo(theMap,*ccit));
317                 }
318         }
319 }
320
321
322 void GCitation::update()
323 {
324         applylock_ = true;
325
326         update_contents();
327
328         info_->set_text(""); // Clear Info field
329
330         // Initialise style tab widgets
331         beforeentry_->set_text(Glib::locale_to_utf8(
332                 controller().params().getSecOptions()));
333         afterentry_->set_text(Glib::locale_to_utf8(
334                 controller().params().getOptions()));
335
336         fill_styles();
337         update_style();
338
339         // Deactivate all buttons
340         upbutton_->set_sensitive(false);
341         downbutton_->set_sensitive(false);
342         removebutton_->set_sensitive(false);
343         addbutton_->set_sensitive(false);
344
345         set_search_buttons();
346         applylock_ = false;
347 }
348
349
350 void GCitation::up()
351 {
352         Gtk::TreeModel::iterator src =
353                 citeselection_->get_selected();
354         Gtk::TreeModel::iterator dest = src;
355
356         if(--dest == (citeFilter_->children()).begin())
357                 upbutton_->set_sensitive(false);
358
359         src = citeFilter_->convert_iter_to_child_iter(src);
360         dest = citeFilter_->convert_iter_to_child_iter(dest);
361         allListStore_->iter_swap(src, dest);
362
363         bc().valid(true);
364         downbutton_->set_sensitive(true);
365 }
366
367
368 void GCitation::down()
369 {
370         Gtk::TreeModel::iterator src =
371                 citeselection_->get_selected();
372         Gtk::TreeModel::iterator dest = src;
373         // Avoid slow operator-- by using an extra variable
374         Gtk::TreeModel::iterator endtest = ++dest;
375
376         if(++endtest == (citeFilter_->children()).end())
377                 downbutton_->set_sensitive(false);
378
379         src = citeFilter_->convert_iter_to_child_iter(src);
380         dest = citeFilter_->convert_iter_to_child_iter(dest);
381         allListStore_->iter_swap(src, dest);
382
383         bc().valid(true);
384         upbutton_->set_sensitive(true);
385 }
386
387
388 void GCitation::add()
389 {
390         Gtk::TreeModel::iterator iter = bibselection_->get_selected();
391
392         if (iter) {
393                 Gtk::TreeModel::iterator next_iter = iter;
394
395                 // Select the right key in bibkeysview_ afterwards
396                 if(++next_iter == (bibFilter_->children()).end()) {
397                         if(iter != (bibFilter_->children()).begin()) {
398                                 bibselection_->select(--iter);
399                                 ++iter;
400                         } else { // bibkeysview_ will be left empty...
401                                 addbutton_->set_sensitive(false);
402                         }
403                 } else {
404                         bibselection_->select(next_iter);
405                 }
406
407                 iter = bibFilter_->convert_iter_to_child_iter(iter);
408                 iter = bibSort_->convert_iter_to_child_iter(iter);
409                 (*iter)[bibColumns.cite] = true;
410
411                 // Move key to the right position
412                 // If a cite key is selected, move bib key to the position above
413                 // Otherwise to the last position in citekeysview_
414                 Gtk::TreeModel::iterator cite_iter(citeselection_->get_selected());
415                 if (cite_iter) {
416                         cite_iter = citeFilter_->convert_iter_to_child_iter(cite_iter);
417                 } else {
418                         cite_iter = (allListStore_->children()).end();
419                 }
420                 allListStore_->move(iter,cite_iter);
421
422                 // Highlight and scroll to newly inserted key
423                 iter = citeFilter_->convert_child_iter_to_iter(iter);
424                 citeselection_->select(iter);
425                 citekeysview_->scroll_to_row(Gtk::TreePath(iter));
426
427                 // Set button states
428                 removebutton_->set_sensitive(true);
429                 set_search_buttons();
430
431                 bc().valid(true);
432         }
433 }
434
435
436 void GCitation::remove()
437 {
438         Gtk::TreeModel::iterator iter(citeselection_->get_selected());
439
440         if (iter) {
441                 Gtk::TreeModel::iterator next_iter(iter);
442
443                 if(++next_iter == (citeFilter_->children()).end()) {
444                         if(iter != (citeFilter_->children()).begin()) {
445                                 citeselection_->select(--iter);
446                                 ++iter;
447                                 bc().valid(true);
448                         } else { // citekeysview_ will be left empty...
449                                 removebutton_->set_sensitive(false);
450                                 bc().valid(false);
451                         }
452                 } else {
453                         citeselection_->select(next_iter);
454                         bc().valid(true);
455                 }
456
457                 // Get an iterator to allListStore_
458                 iter = citeFilter_->convert_iter_to_child_iter(iter);
459                 (*iter)[bibColumns.cite] = false;
460
461                 // Highlight and scroll to newly inserted key
462                 iter = bibSort_->convert_child_iter_to_iter(iter);
463                 iter = bibFilter_->convert_child_iter_to_iter(iter);
464                 bibselection_->select(iter);
465                 bibkeysview_->scroll_to_row(Gtk::TreePath(iter));
466
467                 // Set button states
468                 addbutton_->set_sensitive(true);
469                 set_search_buttons();
470         }
471 }
472
473 void GCitation::cite_selected()
474 {
475         Gtk::TreeModel::iterator iter =
476                 citeselection_->get_selected();
477
478         if (iter) {
479                 info_->set_text((*iter)[bibColumns.info]);
480                 removebutton_->set_sensitive(true);
481
482                 // Set sensitivity of Up/Down buttons
483                 if (iter == (citeFilter_->children()).begin()) {
484                         upbutton_->set_sensitive(false);
485                 } else {
486                         upbutton_->set_sensitive(true);
487                 }
488
489                 if (++iter == (citeFilter_->children()).end()) {
490                         downbutton_->set_sensitive(false);
491                 } else {
492                         downbutton_->set_sensitive(true);
493                 }
494
495         } else {
496                 info_->set_text("");
497                 removebutton_->set_sensitive(false);
498
499                 // Set sensitivity of Up/Down buttons
500                 upbutton_->set_sensitive(false);
501                 downbutton_->set_sensitive(false);
502         }
503
504 }
505
506
507 void GCitation::bib_selected()
508 {
509         Gtk::TreeModel::iterator iter =
510                 bibselection_->get_selected();
511
512         if (iter) {
513                 info_->set_text((*iter)[bibColumns.info]);
514                 addbutton_->set_sensitive(true);
515         } else {
516                 info_->set_text("");
517                 addbutton_->set_sensitive(false);
518         }
519 }
520
521 void GCitation::apply()
522 {
523         vector<biblio::CiteStyle> const & styles =
524                 ControlCitation::getCiteStyles();
525
526         int const choice = stylecombo_->get_active_row_number();
527         style_ = stylecombo_->get_active_row_number();
528
529         bool const full  = authorcheck_->get_active();
530         bool const force = uppercasecheck_->get_active();
531
532         string const command =
533                 biblio::CitationStyle(styles[choice], full, force)
534                 .asLatexStr();
535
536         Gtk::TreeNodeChildren children(citeFilter_->children());
537
538         string citekeys;
539         int i = 0;
540         for (Gtk::TreeModel::const_iterator cit=children.begin();
541                 cit!=children.end(); ++cit) {
542
543                 string item(support::trim(Glib::locale_from_utf8((*cit)[bibColumns.name])));
544                 if (item.empty())
545                         continue;
546                 if (i++ > 0)
547                         citekeys += ",";
548                 citekeys += item;
549         }
550
551         controller().params().setCmdName(command);
552         controller().params().setContents(citekeys);
553
554         controller().params().setSecOptions(Glib::locale_from_utf8(beforeentry_->get_text()));
555         controller().params().setOptions(Glib::locale_from_utf8(afterentry_->get_text()));
556
557         update();
558 }
559
560 void GCitation::find(biblio::Direction dir)
561 {
562         biblio::InfoMap const & theMap = controller().bibkeysInfo();
563         std::vector<std::string> bibkeys;
564
565         biblio::Search const type = regexpcheck_->get_active()
566                 ? biblio::REGEX : biblio::SIMPLE;
567
568         vector<string>::const_iterator start;
569
570         bool search_cite = citeradio_->get_active();
571         bool const casesens = casecheck_->get_active();
572         string const str = Glib::locale_from_utf8(findentry_->get_text());
573
574         Gtk::TreeModel::iterator iter;
575         Gtk::TreeModel::Children::difference_type sel = 0;
576
577         if (search_cite) {
578                 for (iter = (citeFilter_->children()).begin();
579                         iter != (citeFilter_->children()).end(); ++iter) {
580
581                         bibkeys.push_back(Glib::locale_from_utf8(
582                                 (*iter)[bibColumns.name]));
583                 }
584
585                 iter = citeselection_->get_selected();
586                 if (iter)
587                         sel = std::distance((citeFilter_->children()).begin(), iter);
588         } else {
589                 for (iter = (bibFilter_->children()).begin();
590                         iter != (bibFilter_->children()).end(); ++iter) {
591
592                         bibkeys.push_back(Glib::locale_from_utf8(
593                                 (*iter)[bibColumns.name]));
594                 }
595
596                 iter = bibselection_->get_selected();
597                 if (iter)
598                         sel = std::distance(
599                                 (bibFilter_->children()).begin(), iter);
600         }
601
602         start = bibkeys.begin();
603
604         if (sel >= 0 &&
605                 Gtk::TreeModel::Children::size_type(sel) < bibkeys.size())
606                         std::advance(start, sel);
607
608         bool is_cite = !search_cite;
609         while(is_cite != search_cite) {
610
611                 // Find the NEXT instance...
612                 if (dir == biblio::FORWARD)
613                         ++start;
614
615                 vector<string>::const_iterator cit =
616                         biblio::searchKeys(theMap, bibkeys, str,
617                                    start, type, dir, casesens);
618
619                 if (cit == bibkeys.end()) {
620                         if (dir == biblio::FORWARD) {
621                                 start = bibkeys.begin();
622                         }
623                         else {
624                                 start = bibkeys.end();
625                                 --start;
626                         }
627
628                         cit = biblio::searchKeys(theMap, bibkeys, str,
629                                  start, type, dir, casesens);
630
631                         if (cit == bibkeys.end()) {
632                                 return;
633                         }
634                 }
635                 vector<string>::const_iterator bibstart = bibkeys.begin();
636                 vector<string>::difference_type const found =
637                         std::distance(bibstart, cit);
638                 if (found == sel)
639                         return;
640
641                 start = cit;
642                 if (search_cite)
643                         iter = (citeFilter_->children()).begin();
644                 else
645                         iter = (bibFilter_->children()).begin();
646                 std::advance(iter, found);
647                 is_cite = (*iter)[bibColumns.cite];
648         }
649
650         // Highlight and scroll to the key that was found
651         if (search_cite) {
652                 citeselection_->select(iter);
653                 citekeysview_->set_cursor(Gtk::TreePath(iter));
654                 citekeysview_->scroll_to_row(Gtk::TreePath(iter));
655                 cite_selected();
656         } else {
657                 bibselection_->select(iter);
658                 bibkeysview_->set_cursor(Gtk::TreePath(iter));
659                 bibkeysview_->scroll_to_row(Gtk::TreePath(iter));
660                 bib_selected();
661         }
662 }
663
664 void GCitation::set_search_buttons()
665 {
666         bool val = findentry_->get_text_length() && (
667                 (citeradio_->get_active() && !(citeFilter_->children()).empty())
668                 || (bibradio_->get_active() && !(bibFilter_->children()).empty())
669                 );
670         backbutton_->set_sensitive(val);
671         forwardbutton_->set_sensitive(val);
672 }
673
674 void GCitation::previous()
675 {
676         find(biblio::BACKWARD);
677 }
678
679
680 void GCitation::next()
681 {
682         find(biblio::FORWARD);
683 }
684
685
686 void GCitation::bibkeysview_activated(const Gtk::TreeModel::Path&, Gtk::TreeViewColumn*)
687 {
688         add();
689 }
690
691 } // namespace frontend
692 } // namespace lyx