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