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