]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormCitation.C
introduce namespace lyx::support
[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 "xformsBC.h"
15 #include "ControlCitation.h"
16 #include "FormCitation.h"
17 #include "forms/form_citation.h"
18 #include "Kernel.h"
19 #include "Tooltips.h"
20 #include "helper_funcs.h"
21 #include "xforms_helpers.h"
22 #include "lyx_forms.h"
23
24 #include "gettext.h"
25 #include "support/LAssert.h"
26 #include "support/lstrings.h"
27
28 #include <algorithm>
29
30 using namespace lyx::support;
31
32 using std::find;
33 using std::max;
34 using std::min;
35 using std::pair;
36 using std::sort;
37 using std::vector;
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 cs = biblio::getCitationStyle(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 = biblio::getCiteCommand(styles[choice], full, force);
115         }
116
117         controller().params().setCmdName(command);
118         controller().params().setContents(getStringFromVector(citekeys));
119
120         string const after  = getString(dialog_->input_after);
121         controller().params().setOptions(after);
122 }
123
124
125 void FormCitation::hide()
126 {
127         citekeys.clear();
128         bibkeys.clear();
129
130         FormDialogView::hide();
131 }
132
133
134 void FormCitation::build()
135 {
136         dialog_.reset(build_citation(this));
137
138         // Manage the ok, apply, restore and cancel/close buttons
139         bcview().setOK(dialog_->button_ok);
140         bcview().setApply(dialog_->button_apply);
141         bcview().setCancel(dialog_->button_close);
142         bcview().setRestore(dialog_->button_restore);
143
144         // disable for read-only documents
145         bcview().addReadOnly(dialog_->button_add);
146         bcview().addReadOnly(dialog_->button_del);
147         bcview().addReadOnly(dialog_->button_up);
148         bcview().addReadOnly(dialog_->button_down);
149         bcview().addReadOnly(dialog_->choice_style);
150         bcview().addReadOnly(dialog_->input_before);
151         bcview().addReadOnly(dialog_->input_after);
152         bcview().addReadOnly(dialog_->check_full_author_list);
153         bcview().addReadOnly(dialog_->check_force_uppercase);
154
155         // trigger an input event for cut&paste with middle mouse button.
156         setPrehandler(dialog_->input_search);
157         setPrehandler(dialog_->input_before);
158         setPrehandler(dialog_->input_after);
159
160         fl_set_input_return(dialog_->input_after,  FL_RETURN_CHANGED);
161         fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
162         fl_set_input_return(dialog_->input_search, FL_RETURN_END);
163
164         //set up the tooltip mechanism
165         string str = _("Add the selected entry to the current citation reference.");
166         tooltips().init(dialog_->button_add, str);
167
168         str = _("Delete the selected entry from the current citation reference.");
169         tooltips().init(dialog_->button_del, str);
170
171         str = _("Move the selected entry upwards (in the current list).");
172         tooltips().init(dialog_->button_up, str);
173
174         str = _("Move the selected entry downwards (in the current list).");
175         tooltips().init(dialog_->button_down, str);
176
177         str = _("The entries which will be cited. Select them with the arrow buttons from the right browser window.");
178         tooltips().init(dialog_->browser_cite, str);
179 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
180         // Work-around xforms' bug; enable tooltips for browser widgets.
181         setPrehandler(dialog_->browser_cite);
182 #endif
183
184         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.");
185         tooltips().init(dialog_->browser_bib, str);
186 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
187         // Work-around xforms' bug; enable tooltips for browser widgets.
188         setPrehandler(dialog_->browser_bib);
189 #endif
190
191         str = _("Information about the selected entry");
192         tooltips().init(dialog_->browser_info, str);
193 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
194         // Work-around xforms' bug; enable tooltips for browser widgets.
195         setPrehandler(dialog_->browser_info);
196 #endif
197
198         str = _("Here you may select how the citation label should look inside the text (Natbib).");
199         tooltips().init(dialog_->choice_style, str);
200
201         str = _("Activate if you want to print all authors in a reference with more than three authors, and not \"<First Author> et al.\" (Natbib).");
202         tooltips().init(dialog_->check_full_author_list, str);
203
204         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).");
205         tooltips().init(dialog_->check_force_uppercase, str);
206
207         str = _("Optional text which appears before the citation reference, e.g. \"see <Ref>\"");
208         tooltips().init(dialog_->input_before, str);
209
210         str = _("Optional text which appears after the citation reference, e.g. \"pp. 12\"");
211         tooltips().init(dialog_->input_after, str);
212
213         str = _("Search your database (all fields will be searched).");
214         tooltips().init(dialog_->input_search, str);
215
216         str = _("Activate if you want to have case sensitive search: \"bibtex\" finds \"bibtex\", but not \"BibTeX\".");
217         tooltips().init(dialog_->check_search_case, str);
218
219         str = _("Activate if you want to enter Regular Expressions.");
220         tooltips().init(dialog_->check_search_type, str);
221 }
222
223
224 void FormCitation::findBiblio(biblio::Direction const dir)
225 {
226         string const str = getString(dialog_->input_search);
227         biblio::InfoMap const & theMap = controller().bibkeysInfo();
228         bool const caseSensitive =
229                 fl_get_button(dialog_->check_search_case);
230         biblio::Search const type =
231                 fl_get_button(dialog_->check_search_type) ?
232                 biblio::REGEX : biblio::SIMPLE;
233
234         vector<string>::const_iterator start = bibkeys.begin();
235         int const sel = fl_get_browser(dialog_->browser_bib);
236         if (sel >= 1 && sel <= int(bibkeys.size()))
237                 start += sel - 1;
238
239         // Find the NEXT instance...
240         (dir == biblio::FORWARD) ? ++start : --start;
241
242
243         vector<string>::const_iterator const cit =
244                 biblio::searchKeys(theMap, bibkeys, str,
245                                    start, type, dir, caseSensitive);
246
247         if (cit == bibkeys.end())
248                 return;
249
250         int const found = int(cit - bibkeys.begin()) + 1;
251         if (found == sel)
252                 return;
253
254         // Update the display
255         int const top = max(found - 5, 1);
256         fl_set_browser_topline(dialog_->browser_bib, top);
257         fl_select_browser_line(dialog_->browser_bib, found);
258         input(dialog_->browser_bib, 0);
259 }
260
261
262 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
263 {
264         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
265
266         biblio::InfoMap const & theMap = controller().bibkeysInfo();
267
268         string topCitekey;
269         if (!citekeys.empty()) topCitekey = citekeys[0];
270
271         if (ob == dialog_->browser_bib) {
272                 fl_deselect_browser(dialog_->browser_cite);
273
274                 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
275                 if (sel < 1 || sel > bibkeys.size())
276                         return ButtonPolicy::SMI_NOOP;
277
278                 // Put into browser_info the additional info associated with
279                 // the selected browser_bib key
280                 fl_clear_browser(dialog_->browser_info);
281
282                 string const tmp = formatted(biblio::getInfo(theMap,
283                                                              bibkeys[sel - 1]),
284                                               dialog_->browser_info->w - 10);
285                 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
286
287                 // Highlight the selected browser_bib key in browser_cite if
288                 // present
289                 vector<string>::const_iterator cit =
290                         find(citekeys.begin(), citekeys.end(), bibkeys[sel - 1]);
291
292                 if (cit != citekeys.end()) {
293                         int const n = int(cit - citekeys.begin());
294                         fl_select_browser_line(dialog_->browser_cite, n + 1);
295                         fl_set_browser_topline(dialog_->browser_cite, n + 1);
296                 }
297
298                 if (!kernel().isBufferReadonly()) {
299                         if (cit != citekeys.end()) {
300                                 setBibButtons(OFF);
301                                 setCiteButtons(ON);
302                         } else {
303                                 setBibButtons(ON);
304                                 setCiteButtons(OFF);
305                         }
306                 }
307
308         } else if (ob == dialog_->browser_cite) {
309                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
310                 if (sel < 1 || sel > citekeys.size())
311                         return ButtonPolicy::SMI_NOOP;
312
313                 if (!kernel().isBufferReadonly()) {
314                         setBibButtons(OFF);
315                         setCiteButtons(ON);
316                 }
317
318                 // Highlight the selected browser_cite key in browser_bib
319                 vector<string>::const_iterator cit =
320                         find(bibkeys.begin(), bibkeys.end(), citekeys[sel - 1]);
321
322                 if (cit != bibkeys.end()) {
323                         int const n = int(cit - bibkeys.begin());
324                         fl_select_browser_line(dialog_->browser_bib, n + 1);
325                         fl_set_browser_topline(dialog_->browser_bib, n + 1);
326
327                         // Put into browser_info the additional info associated
328                         // with the selected browser_cite key
329                         fl_clear_browser(dialog_->browser_info);
330                         string const tmp =
331                                 formatted(biblio::getInfo(theMap, citekeys[sel - 1]),
332                                           dialog_->browser_info->w - 10);
333                         fl_add_browser_line(dialog_->browser_info, tmp.c_str());
334                 }
335
336         } else if (ob == dialog_->button_add) {
337                 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
338                 if (sel < 1 || sel > bibkeys.size())
339                         return ButtonPolicy::SMI_NOOP;
340
341                 // Add the selected browser_bib key to browser_cite
342                 fl_addto_browser(dialog_->browser_cite,
343                                   bibkeys[sel - 1].c_str());
344                 citekeys.push_back(bibkeys[sel - 1]);
345
346                 int const n = int(citekeys.size());
347                 fl_select_browser_line(dialog_->browser_cite, n);
348
349                 setBibButtons(OFF);
350                 setCiteButtons(ON);
351                 activate = ButtonPolicy::SMI_VALID;
352
353         } else if (ob == dialog_->button_del) {
354                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
355                 if (sel < 1 || sel > citekeys.size())
356                         return ButtonPolicy::SMI_NOOP;
357
358                 // Remove the selected key from browser_cite
359                 fl_delete_browser_line(dialog_->browser_cite, sel) ;
360                 citekeys.erase(citekeys.begin() + sel - 1);
361
362                 setBibButtons(ON);
363                 setCiteButtons(OFF);
364                 activate = ButtonPolicy::SMI_VALID;
365
366         } else if (ob == dialog_->button_up) {
367                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
368                 if (sel < 2 || sel > citekeys.size())
369                         return ButtonPolicy::SMI_NOOP;
370
371                 // Move the selected key up one line
372                 vector<string>::iterator it = citekeys.begin() + sel - 1;
373                 string const tmp = *it;
374
375                 fl_delete_browser_line(dialog_->browser_cite, sel);
376                 citekeys.erase(it);
377
378                 fl_insert_browser_line(dialog_->browser_cite, sel - 1, tmp.c_str());
379                 fl_select_browser_line(dialog_->browser_cite, sel - 1);
380                 citekeys.insert(it - 1, tmp);
381                 setCiteButtons(ON);
382                 activate = ButtonPolicy::SMI_VALID;
383
384         } else if (ob == dialog_->button_down) {
385                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
386                 if (sel < 1 || sel > citekeys.size() - 1)
387                         return ButtonPolicy::SMI_NOOP;
388
389                 // Move the selected key down one line
390                 vector<string>::iterator it = citekeys.begin() + sel - 1;
391                 string const tmp = *it;
392
393                 fl_delete_browser_line(dialog_->browser_cite, sel);
394                 citekeys.erase(it);
395
396                 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
397                 fl_select_browser_line(dialog_->browser_cite, sel+1);
398                 citekeys.insert(it+1, tmp);
399                 setCiteButtons(ON);
400                 activate = ButtonPolicy::SMI_VALID;
401
402         } else if (ob == dialog_->button_previous) {
403                 findBiblio(biblio::BACKWARD);
404         } else if (ob == dialog_->button_next) {
405                 findBiblio(biblio::FORWARD);
406         } else if (ob == dialog_->input_search) {
407                 findBiblio(biblio::FORWARD);
408         } else if (ob == dialog_->choice_style ||
409                    ob == dialog_->check_full_author_list ||
410                    ob == dialog_->check_force_uppercase ||
411                    ob == dialog_->input_before ||
412                    ob == dialog_->input_after) {
413                 activate = ButtonPolicy::SMI_VALID;
414         }
415
416         string currentCitekey;
417         if (!citekeys.empty())
418                 currentCitekey = citekeys[0];
419
420         if (topCitekey != currentCitekey) {
421                 int choice = std::max(1, fl_get_choice(dialog_->choice_style));
422                 fillChoice(dialog_.get(),
423                            controller().getCiteStrings(currentCitekey));
424                 fl_set_choice(dialog_->choice_style, choice);
425         }
426
427         return activate;
428 }
429
430
431 void FormCitation::update()
432 {
433         // Make the list of all available bibliography keys
434         bibkeys = biblio::getKeys(controller().bibkeysInfo());
435         updateBrowser(dialog_->browser_bib, bibkeys);
436
437         // Ditto for the keys cited in this inset
438         citekeys = getVectorFromString(controller().params().getContents());
439         updateBrowser(dialog_->browser_cite, citekeys);
440
441         // Use the first citekey to fill choice_style
442         string key;
443         if (!citekeys.empty())
444                 key = citekeys[0];
445
446         fillChoice(dialog_.get(), controller().getCiteStrings(key));
447
448         // Use the citation command to update the GUI
449         updateStyle(dialog_.get(), controller().params().getCmdName());
450
451         bool const natbib = controller().usingNatbib();
452         setEnabled(dialog_->check_full_author_list, natbib);
453         setEnabled(dialog_->check_force_uppercase, natbib);
454         setEnabled(dialog_->choice_style, natbib);
455
456         // No keys have been selected yet, so...
457         fl_clear_browser(dialog_->browser_info);
458         setBibButtons(OFF);
459         setCiteButtons(OFF);
460
461         // Natbib can have comments before and after the citation.
462         // This is not yet supported. After only.
463         fl_set_input(dialog_->input_after,
464                      controller().params().getOptions().c_str());
465
466         fl_set_input(dialog_->input_before, _("Not yet supported").c_str());
467         setEnabled(dialog_->input_before, false);
468 }
469
470
471 void FormCitation::updateBrowser(FL_OBJECT * browser,
472                                  vector<string> const & keys) const
473 {
474         // Check whether the current contents of the browser will be
475         // changed by loading the contents of the vec...
476         vector<string> browser_keys = getVector(browser);
477
478         if (browser_keys == keys) {
479                 fl_deselect_browser(browser);
480                 fl_set_browser_topline(browser, 1);
481                 return;
482         }
483
484         // They will be changed. Proceed.
485         fl_clear_browser(browser);
486
487         for (vector<string>::const_iterator it = keys.begin();
488              it != keys.end(); ++it) {
489                 string key = trim(*it);
490                 if (!key.empty())
491                         fl_add_browser_line(browser, key.c_str());
492         }
493 }
494
495
496 void FormCitation::setBibButtons(State status) const
497 {
498         setEnabled(dialog_->button_add, (status == ON));
499 }
500
501
502 void FormCitation::setCiteButtons(State status) const
503 {
504         int const sel     = fl_get_browser(dialog_->browser_cite);
505         int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
506         bool const activate      = (status == ON);
507         bool const activate_up   = (activate && sel != 1);
508         bool const activate_down = (activate && sel != maxline);
509
510         setEnabled(dialog_->button_del,  activate);
511         setEnabled(dialog_->button_up,   activate_up);
512         setEnabled(dialog_->button_down, activate_down);
513 }