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