]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormCitation.C
Move the biblio::CiteEngine enum into bufferparams.h to minimize
[lyx.git] / src / frontends / xforms / FormCitation.C
1 /**
2  * \file FormCitation.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Rob Lahaye
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "FormCitation.h"
15 #include "ControlCitation.h"
16 #include "forms/form_citation.h"
17
18 #include "Tooltips.h"
19 #include "xforms_helpers.h"
20 #include "xformsBC.h"
21
22 #include "bufferparams.h"
23
24 #include "support/lstrings.h"
25
26 #include "lyx_forms.h"
27
28 using lyx::support::getStringFromVector;
29 using lyx::support::getVectorFromString;
30 using lyx::support::trim;
31
32 using std::find;
33 using std::max;
34
35 using std::vector;
36 using std::string;
37
38
39 namespace {
40
41 // shamelessly stolen from Menubar_pimpl.C
42 int string_width(string const & str)
43 {
44         return fl_get_string_widthTAB(FL_NORMAL_STYLE, FL_NORMAL_SIZE,
45                                       str.c_str(),
46                                       static_cast<int>(str.length()));
47 }
48
49
50 void fillChoice(FD_citation * dialog, vector<string> vec)
51 {
52         // Check whether the current contents of the browser will be
53         // changed by loading the contents of the vec...
54         vector<string> const choice_style = getVector(dialog->choice_style);
55
56         if (vec == choice_style)
57                 return;
58
59         // They will be changed. Proceed
60         bool const noVec = vec.empty();
61         string const str = noVec ? string() : getStringFromVector(vec, "|");
62
63         fl_clear_choice(dialog->choice_style);
64         fl_addto_choice(dialog->choice_style, str.c_str());
65         setEnabled(dialog->choice_style, !noVec);
66 }
67
68
69 void updateStyle(FD_citation * dialog, string command)
70 {
71         // Find the style of the citekeys
72         vector<biblio::CiteStyle> const & styles =
73                 ControlCitation::getCiteStyles();
74         biblio::CitationStyle const cs(command);
75
76         vector<biblio::CiteStyle>::const_iterator cit =
77                 find(styles.begin(), styles.end(), cs.style);
78
79         // Use this to initialise the GUI
80         bool const noStyles = cit == styles.end();
81         int const index = 1 + ( noStyles ? 0 : int(cit - styles.begin()) );
82         fl_set_choice(dialog->choice_style, index);
83
84         // Disable if there are no styles, otherwise use cs member settings.
85         fl_set_button(dialog->check_full_author_list, !noStyles && cs.full);
86         fl_set_button(dialog->check_force_uppercase, !noStyles && cs.forceUCase);
87 }
88
89 } // namespace anon
90
91
92 typedef FormController<ControlCitation, FormView<FD_citation> > base_class;
93
94
95 FormCitation::FormCitation(Dialog & parent)
96         : base_class(parent, _("Citation"))
97 {}
98
99
100 void FormCitation::apply()
101 {
102         string command = "cite";
103         if (isActive(dialog_->choice_style)) {
104                 vector<biblio::CiteStyle> const & styles =
105                         ControlCitation::getCiteStyles();
106
107                 int const choice =
108                         std::max(0, fl_get_choice(dialog_->choice_style) - 1);
109                 bool const full  =
110                         fl_get_button(dialog_->check_full_author_list);
111                 bool const force =
112                         fl_get_button(dialog_->check_force_uppercase);
113
114                 command =
115                         biblio::CitationStyle(styles[choice], full, force)
116                         .asLatexStr();
117
118         }
119
120         controller().params().setCmdName(command);
121         controller().params().setContents(getStringFromVector(citekeys));
122
123         string const before  = getString(dialog_->input_before);
124         controller().params().setSecOptions(before);
125
126         string const after  = getString(dialog_->input_after);
127         controller().params().setOptions(after);
128 }
129
130
131 void FormCitation::hide()
132 {
133         citekeys.clear();
134         bibkeys.clear();
135
136         FormDialogView::hide();
137 }
138
139
140 void FormCitation::build()
141 {
142         dialog_.reset(build_citation(this));
143
144         // Manage the ok, apply, restore and cancel/close buttons
145         bcview().setOK(dialog_->button_ok);
146         bcview().setApply(dialog_->button_apply);
147         bcview().setCancel(dialog_->button_close);
148         bcview().setRestore(dialog_->button_restore);
149
150         // disable for read-only documents
151         bcview().addReadOnly(dialog_->button_add);
152         bcview().addReadOnly(dialog_->button_del);
153         bcview().addReadOnly(dialog_->button_up);
154         bcview().addReadOnly(dialog_->button_down);
155         bcview().addReadOnly(dialog_->choice_style);
156         bcview().addReadOnly(dialog_->input_before);
157         bcview().addReadOnly(dialog_->input_after);
158         bcview().addReadOnly(dialog_->check_full_author_list);
159         bcview().addReadOnly(dialog_->check_force_uppercase);
160
161         // trigger an input event for cut&paste with middle mouse button.
162         setPrehandler(dialog_->input_search);
163         setPrehandler(dialog_->input_before);
164         setPrehandler(dialog_->input_after);
165
166         fl_set_input_return(dialog_->input_after,  FL_RETURN_CHANGED);
167         fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
168         fl_set_input_return(dialog_->input_search, FL_RETURN_END);
169
170         //set up the tooltip mechanism
171         string str = _("Add the selected entry to the current citation reference.");
172         tooltips().init(dialog_->button_add, str);
173
174         str = _("Delete the selected entry from the current citation reference.");
175         tooltips().init(dialog_->button_del, str);
176
177         str = _("Move the selected entry upwards (in the current list).");
178         tooltips().init(dialog_->button_up, str);
179
180         str = _("Move the selected entry downwards (in the current list).");
181         tooltips().init(dialog_->button_down, str);
182
183         str = _("The entries which will be cited. Select them with the arrow buttons from the right browser window.");
184         tooltips().init(dialog_->browser_cite, str);
185 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
186         // Work-around xforms' bug; enable tooltips for browser widgets.
187         setPrehandler(dialog_->browser_cite);
188 #endif
189
190         str = _("All entries in the database you have loaded (via \"Insert->Lists&TOC->BibTex Reference\"). Move the ones you want to cite with the arrow buttons into the left browser window.");
191         tooltips().init(dialog_->browser_bib, str);
192 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
193         // Work-around xforms' bug; enable tooltips for browser widgets.
194         setPrehandler(dialog_->browser_bib);
195 #endif
196
197         str = _("Information about the selected entry");
198         tooltips().init(dialog_->browser_info, str);
199 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
200         // Work-around xforms' bug; enable tooltips for browser widgets.
201         setPrehandler(dialog_->browser_info);
202 #endif
203
204         str = _("Here you may select how the citation label should look inside the text (Natbib).");
205         tooltips().init(dialog_->choice_style, str);
206
207         str = _("Activate if you want to print all authors in a reference with more than three authors, and not \"<First Author> et al.\" (Natbib).");
208         tooltips().init(dialog_->check_full_author_list, str);
209
210         str = _("Activate if you want to print the first character of the author name as uppercase (\"Van Gogh\", not \"van Gogh\"). Useful at the beginning of sentences (Natbib).");
211         tooltips().init(dialog_->check_force_uppercase, str);
212
213         str = _("Optional text which appears before the citation reference, e.g. \"see <Ref>\"");
214         tooltips().init(dialog_->input_before, str);
215
216         str = _("Optional text which appears after the citation reference, e.g. \"pp. 12\"");
217         tooltips().init(dialog_->input_after, str);
218
219         str = _("Search your database (all fields will be searched).");
220         tooltips().init(dialog_->input_search, str);
221
222         str = _("Activate if you want to have case sensitive search: \"bibtex\" finds \"bibtex\", but not \"BibTeX\".");
223         tooltips().init(dialog_->check_search_case, str);
224
225         str = _("Activate if you want to enter Regular Expressions.");
226         tooltips().init(dialog_->check_search_type, str);
227 }
228
229
230 void FormCitation::findBiblio(biblio::Direction const dir)
231 {
232         string const str = getString(dialog_->input_search);
233         biblio::InfoMap const & theMap = controller().bibkeysInfo();
234         bool const caseSensitive =
235                 fl_get_button(dialog_->check_search_case);
236         biblio::Search const type =
237                 fl_get_button(dialog_->check_search_type) ?
238                 biblio::REGEX : biblio::SIMPLE;
239
240         vector<string>::const_iterator start = bibkeys.begin();
241         int const sel = fl_get_browser(dialog_->browser_bib);
242         if (sel >= 1 && sel <= int(bibkeys.size()))
243                 start += sel - 1;
244
245         // Find the NEXT instance...
246         (dir == biblio::FORWARD) ? ++start : --start;
247
248
249         vector<string>::const_iterator const cit =
250                 biblio::searchKeys(theMap, bibkeys, str,
251                                    start, type, dir, caseSensitive);
252
253         if (cit == bibkeys.end())
254                 return;
255
256         int const found = int(cit - bibkeys.begin()) + 1;
257         if (found == sel)
258                 return;
259
260         // Update the display
261         int const top = max(found - 5, 1);
262         fl_set_browser_topline(dialog_->browser_bib, top);
263         fl_select_browser_line(dialog_->browser_bib, found);
264         input(dialog_->browser_bib, 0);
265 }
266
267
268 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
269 {
270         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
271
272         biblio::InfoMap const & theMap = controller().bibkeysInfo();
273
274         string topCitekey;
275         if (!citekeys.empty()) topCitekey = citekeys[0];
276
277         if (ob == dialog_->browser_bib) {
278                 fl_deselect_browser(dialog_->browser_cite);
279
280                 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
281                 if (sel < 1 || sel > bibkeys.size())
282                         return ButtonPolicy::SMI_NOOP;
283
284                 // Put into browser_info the additional info associated with
285                 // the selected browser_bib key
286                 fl_clear_browser(dialog_->browser_info);
287
288                 string const tmp = formatted(biblio::getInfo(theMap,
289                                                              bibkeys[sel - 1]),
290                                               dialog_->browser_info->w - 10);
291                 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
292
293                 // Highlight the selected browser_bib key in browser_cite if
294                 // present
295                 vector<string>::const_iterator cit =
296                         find(citekeys.begin(), citekeys.end(), bibkeys[sel - 1]);
297
298                 if (cit != citekeys.end()) {
299                         int const n = int(cit - citekeys.begin());
300                         fl_select_browser_line(dialog_->browser_cite, n + 1);
301                         fl_set_browser_topline(dialog_->browser_cite, n + 1);
302                 }
303
304                 if (!kernel().isBufferReadonly()) {
305                         if (cit != citekeys.end()) {
306                                 setBibButtons(OFF);
307                                 setCiteButtons(ON);
308                         } else {
309                                 setBibButtons(ON);
310                                 setCiteButtons(OFF);
311                         }
312                 }
313
314         } else if (ob == dialog_->browser_cite) {
315                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
316                 if (sel < 1 || sel > citekeys.size())
317                         return ButtonPolicy::SMI_NOOP;
318
319                 if (!kernel().isBufferReadonly()) {
320                         setBibButtons(OFF);
321                         setCiteButtons(ON);
322                 }
323
324                 // Highlight the selected browser_cite key in browser_bib
325                 vector<string>::const_iterator cit =
326                         find(bibkeys.begin(), bibkeys.end(), citekeys[sel - 1]);
327
328                 if (cit != bibkeys.end()) {
329                         int const n = int(cit - bibkeys.begin());
330                         fl_select_browser_line(dialog_->browser_bib, n + 1);
331                         fl_set_browser_topline(dialog_->browser_bib, n + 1);
332
333                         // Put into browser_info the additional info associated
334                         // with the selected browser_cite key
335                         fl_clear_browser(dialog_->browser_info);
336                         string const tmp =
337                                 formatted(biblio::getInfo(theMap, citekeys[sel - 1]),
338                                           dialog_->browser_info->w - 10);
339                         fl_add_browser_line(dialog_->browser_info, tmp.c_str());
340                 }
341
342         } else if (ob == dialog_->button_add) {
343                 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
344                 if (sel < 1 || sel > bibkeys.size())
345                         return ButtonPolicy::SMI_NOOP;
346
347                 // Add the selected browser_bib key to browser_cite
348                 fl_addto_browser(dialog_->browser_cite,
349                                   bibkeys[sel - 1].c_str());
350                 citekeys.push_back(bibkeys[sel - 1]);
351
352                 int const n = int(citekeys.size());
353                 fl_select_browser_line(dialog_->browser_cite, n);
354
355                 setBibButtons(OFF);
356                 setCiteButtons(ON);
357                 activate = ButtonPolicy::SMI_VALID;
358
359         } else if (ob == dialog_->button_del) {
360                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
361                 if (sel < 1 || sel > citekeys.size())
362                         return ButtonPolicy::SMI_NOOP;
363
364                 // Remove the selected key from browser_cite
365                 fl_delete_browser_line(dialog_->browser_cite, sel) ;
366                 citekeys.erase(citekeys.begin() + sel - 1);
367
368                 setBibButtons(ON);
369                 setCiteButtons(OFF);
370                 activate = ButtonPolicy::SMI_VALID;
371
372         } else if (ob == dialog_->button_up) {
373                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
374                 if (sel < 2 || sel > citekeys.size())
375                         return ButtonPolicy::SMI_NOOP;
376
377                 // Move the selected key up one line
378                 vector<string>::iterator it = citekeys.begin() + sel - 1;
379                 string const tmp = *it;
380
381                 fl_delete_browser_line(dialog_->browser_cite, sel);
382                 citekeys.erase(it);
383
384                 fl_insert_browser_line(dialog_->browser_cite, sel - 1, tmp.c_str());
385                 fl_select_browser_line(dialog_->browser_cite, sel - 1);
386                 citekeys.insert(it - 1, tmp);
387                 setCiteButtons(ON);
388                 activate = ButtonPolicy::SMI_VALID;
389
390         } else if (ob == dialog_->button_down) {
391                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
392                 if (sel < 1 || sel > citekeys.size() - 1)
393                         return ButtonPolicy::SMI_NOOP;
394
395                 // Move the selected key down one line
396                 vector<string>::iterator it = citekeys.begin() + sel - 1;
397                 string const tmp = *it;
398
399                 fl_delete_browser_line(dialog_->browser_cite, sel);
400                 citekeys.erase(it);
401
402                 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
403                 fl_select_browser_line(dialog_->browser_cite, sel+1);
404                 citekeys.insert(it+1, tmp);
405                 setCiteButtons(ON);
406                 activate = ButtonPolicy::SMI_VALID;
407
408         } else if (ob == dialog_->button_previous) {
409                 findBiblio(biblio::BACKWARD);
410         } else if (ob == dialog_->button_next) {
411                 findBiblio(biblio::FORWARD);
412         } else if (ob == dialog_->input_search) {
413                 findBiblio(biblio::FORWARD);
414         } else if (ob == dialog_->choice_style ||
415                    ob == dialog_->check_full_author_list ||
416                    ob == dialog_->check_force_uppercase ||
417                    ob == dialog_->input_before ||
418                    ob == dialog_->input_after) {
419                 activate = ButtonPolicy::SMI_VALID;
420         }
421
422         string currentCitekey;
423         if (!citekeys.empty())
424                 currentCitekey = citekeys[0];
425
426         if (topCitekey != currentCitekey) {
427                 int choice = std::max(1, fl_get_choice(dialog_->choice_style));
428                 fillChoice(dialog_.get(),
429                            controller().getCiteStrings(currentCitekey));
430                 fl_set_choice(dialog_->choice_style, choice);
431         }
432
433         return activate;
434 }
435
436
437 void FormCitation::update()
438 {
439         // Make the list of all available bibliography keys
440         bibkeys = biblio::getKeys(controller().bibkeysInfo());
441         updateBrowser(dialog_->browser_bib, bibkeys);
442
443         // Ditto for the keys cited in this inset
444         citekeys = getVectorFromString(controller().params().getContents());
445         updateBrowser(dialog_->browser_cite, citekeys);
446
447         // Use the first citekey to fill choice_style
448         string key;
449         if (!citekeys.empty())
450                 key = citekeys[0];
451
452         fillChoice(dialog_.get(), controller().getCiteStrings(key));
453
454         // Use the citation command to update the GUI
455         updateStyle(dialog_.get(), controller().params().getCmdName());
456
457         biblio::CiteEngine const engine = controller().getEngine();
458         bool const natbib_engine =
459                 engine == biblio::ENGINE_NATBIB_AUTHORYEAR ||
460                 engine == biblio::ENGINE_NATBIB_NUMERICAL;
461         bool const basic_engine = engine == biblio::ENGINE_BASIC;
462
463         setEnabled(dialog_->check_full_author_list, natbib_engine);
464         setEnabled(dialog_->check_force_uppercase, natbib_engine);
465         setEnabled(dialog_->choice_style, !basic_engine);
466         setEnabled(dialog_->input_before, !basic_engine);
467
468         // No keys have been selected yet, so...
469         fl_clear_browser(dialog_->browser_info);
470         setBibButtons(OFF);
471         setCiteButtons(OFF);
472
473         // Natbib can have comments before and after the citation.
474         fl_set_input(dialog_->input_after,
475                      controller().params().getOptions().c_str());
476
477         fl_set_input(dialog_->input_before,
478                      controller().params().getSecOptions().c_str());
479 }
480
481
482 void FormCitation::updateBrowser(FL_OBJECT * browser,
483                                  vector<string> const & keys) const
484 {
485         // Check whether the current contents of the browser will be
486         // changed by loading the contents of the vec...
487         vector<string> browser_keys = getVector(browser);
488
489         if (browser_keys == keys) {
490                 fl_deselect_browser(browser);
491                 fl_set_browser_topline(browser, 1);
492                 return;
493         }
494
495         // They will be changed. Proceed.
496         fl_clear_browser(browser);
497
498         for (vector<string>::const_iterator it = keys.begin();
499              it != keys.end(); ++it) {
500                 string key = trim(*it);
501                 if (!key.empty())
502                         fl_add_browser_line(browser, key.c_str());
503         }
504 }
505
506
507 void FormCitation::setBibButtons(State status) const
508 {
509         setEnabled(dialog_->button_add, (status == ON));
510 }
511
512
513 void FormCitation::setCiteButtons(State status) const
514 {
515         int const sel     = fl_get_browser(dialog_->browser_cite);
516         int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
517         bool const activate      = (status == ON);
518         bool const activate_up   = (activate && sel != 1);
519         bool const activate_down = (activate && sel != maxline);
520
521         setEnabled(dialog_->button_del,  activate);
522         setEnabled(dialog_->button_up,   activate_up);
523         setEnabled(dialog_->button_down, activate_down);
524 }