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