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