]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormCitation.C
remove defaults stuff, let Qt handle no toolbar
[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 #include <algorithm>
14
15
16 #include "xformsBC.h"
17 #include "ControlCitation.h"
18 #include "FormCitation.h"
19 #include "forms/form_citation.h"
20 #include "Kernel.h"
21 #include "Tooltips.h"
22 #include "helper_funcs.h"
23 #include "xforms_helpers.h"
24 #include FORMS_H_LOCATION
25
26 #include "gettext.h"
27 #include "support/LAssert.h"
28 #include "support/lstrings.h"
29
30 using std::find;
31 using std::max;
32 using std::min;
33 using std::pair;
34 using std::sort;
35 using std::vector;
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         // Work-around xforms' bug; enable tooltips for browser widgets.
178         setPrehandler(dialog_->browser_cite);
179
180         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.");
181         tooltips().init(dialog_->browser_bib, str);
182         // Work-around xforms' bug; enable tooltips for browser widgets.
183         setPrehandler(dialog_->browser_bib);
184
185         str = _("Information about the selected entry");
186         tooltips().init(dialog_->browser_info, str);
187         // Work-around xforms' bug; enable tooltips for browser widgets.
188         setPrehandler(dialog_->browser_info);
189
190         str = _("Here you may select how the citation label should look inside the text (Natbib).");
191         tooltips().init(dialog_->choice_style, str);
192
193         str = _("Activate if you want to print all authors in a reference with more than three authors, and not \"<First Author> et al.\" (Natbib).");
194         tooltips().init(dialog_->check_full_author_list, str);
195
196         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).");
197         tooltips().init(dialog_->check_force_uppercase, str);
198
199         str = _("Optional text which appears before the citation reference, e.g. \"see <Ref>\"");
200         tooltips().init(dialog_->input_before, str);
201
202         str = _("Optional text which appears after the citation reference, e.g. \"pp. 12\"");
203         tooltips().init(dialog_->input_after, str);
204
205         str = _("Search your database (all fields will be searched).");
206         tooltips().init(dialog_->input_search, str);
207
208         str = _("Activate if you want to have case sensitive search: \"bibtex\" finds \"bibtex\", but not \"BibTeX\".");
209         tooltips().init(dialog_->check_search_case, str);
210
211         str = _("Activate if you want to enter Regular Expressions.");
212         tooltips().init(dialog_->check_search_type, str);
213 }
214
215
216 void FormCitation::findBiblio(biblio::Direction const dir)
217 {
218         string const str = getString(dialog_->input_search);
219         biblio::InfoMap const & theMap = controller().bibkeysInfo();
220         bool const caseSensitive =
221                 fl_get_button(dialog_->check_search_case);
222         biblio::Search const type =
223                 fl_get_button(dialog_->check_search_type) ?
224                 biblio::REGEX : biblio::SIMPLE;
225
226         vector<string>::const_iterator start = bibkeys.begin();
227         int const sel = fl_get_browser(dialog_->browser_bib);
228         if (sel >= 1 && sel <= int(bibkeys.size()))
229                 start += sel - 1;
230
231         // Find the NEXT instance...
232         (dir == biblio::FORWARD) ? ++start : --start;
233
234
235         vector<string>::const_iterator const cit =
236                 biblio::searchKeys(theMap, bibkeys, str,
237                                    start, type, dir, caseSensitive);
238
239         if (cit == bibkeys.end())
240                 return;
241
242         int const found = int(cit - bibkeys.begin()) + 1;
243         if (found == sel)
244                 return;
245
246         // Update the display
247         int const top = max(found - 5, 1);
248         fl_set_browser_topline(dialog_->browser_bib, top);
249         fl_select_browser_line(dialog_->browser_bib, found);
250         input(dialog_->browser_bib, 0);
251 }
252
253
254 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
255 {
256         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
257
258         biblio::InfoMap const & theMap = controller().bibkeysInfo();
259
260         string topCitekey;
261         if (!citekeys.empty()) topCitekey = citekeys[0];
262
263         if (ob == dialog_->browser_bib) {
264                 fl_deselect_browser(dialog_->browser_cite);
265
266                 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
267                 if (sel < 1 || sel > bibkeys.size())
268                         return ButtonPolicy::SMI_NOOP;
269
270                 // Put into browser_info the additional info associated with
271                 // the selected browser_bib key
272                 fl_clear_browser(dialog_->browser_info);
273
274                 string const tmp = formatted(biblio::getInfo(theMap,
275                                                              bibkeys[sel - 1]),
276                                               dialog_->browser_info->w - 10);
277                 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
278
279                 // Highlight the selected browser_bib key in browser_cite if
280                 // present
281                 vector<string>::const_iterator cit =
282                         find(citekeys.begin(), citekeys.end(), bibkeys[sel - 1]);
283
284                 if (cit != citekeys.end()) {
285                         int const n = int(cit - citekeys.begin());
286                         fl_select_browser_line(dialog_->browser_cite, n + 1);
287                         fl_set_browser_topline(dialog_->browser_cite, n + 1);
288                 }
289
290                 if (!kernel().isBufferReadonly()) {
291                         if (cit != citekeys.end()) {
292                                 setBibButtons(OFF);
293                                 setCiteButtons(ON);
294                         } else {
295                                 setBibButtons(ON);
296                                 setCiteButtons(OFF);
297                         }
298                 }
299
300         } else if (ob == dialog_->browser_cite) {
301                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
302                 if (sel < 1 || sel > citekeys.size())
303                         return ButtonPolicy::SMI_NOOP;
304
305                 if (!kernel().isBufferReadonly()) {
306                         setBibButtons(OFF);
307                         setCiteButtons(ON);
308                 }
309
310                 // Highlight the selected browser_cite key in browser_bib
311                 vector<string>::const_iterator cit =
312                         find(bibkeys.begin(), bibkeys.end(), citekeys[sel - 1]);
313
314                 if (cit != bibkeys.end()) {
315                         int const n = int(cit - bibkeys.begin());
316                         fl_select_browser_line(dialog_->browser_bib, n + 1);
317                         fl_set_browser_topline(dialog_->browser_bib, n + 1);
318
319                         // Put into browser_info the additional info associated
320                         // with the selected browser_cite key
321                         fl_clear_browser(dialog_->browser_info);
322                         string const tmp =
323                                 formatted(biblio::getInfo(theMap, citekeys[sel - 1]),
324                                           dialog_->browser_info->w - 10);
325                         fl_add_browser_line(dialog_->browser_info, tmp.c_str());
326                 }
327
328         } else if (ob == dialog_->button_add) {
329                 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
330                 if (sel < 1 || sel > bibkeys.size())
331                         return ButtonPolicy::SMI_NOOP;
332
333                 // Add the selected browser_bib key to browser_cite
334                 fl_addto_browser(dialog_->browser_cite,
335                                   bibkeys[sel - 1].c_str());
336                 citekeys.push_back(bibkeys[sel - 1]);
337
338                 int const n = int(citekeys.size());
339                 fl_select_browser_line(dialog_->browser_cite, n);
340
341                 setBibButtons(OFF);
342                 setCiteButtons(ON);
343                 activate = ButtonPolicy::SMI_VALID;
344
345         } else if (ob == dialog_->button_del) {
346                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
347                 if (sel < 1 || sel > citekeys.size())
348                         return ButtonPolicy::SMI_NOOP;
349
350                 // Remove the selected key from browser_cite
351                 fl_delete_browser_line(dialog_->browser_cite, sel) ;
352                 citekeys.erase(citekeys.begin() + sel - 1);
353
354                 setBibButtons(ON);
355                 setCiteButtons(OFF);
356                 activate = ButtonPolicy::SMI_VALID;
357
358         } else if (ob == dialog_->button_up) {
359                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
360                 if (sel < 2 || sel > citekeys.size())
361                         return ButtonPolicy::SMI_NOOP;
362
363                 // Move the selected key up one line
364                 vector<string>::iterator it = citekeys.begin() + sel - 1;
365                 string const tmp = *it;
366
367                 fl_delete_browser_line(dialog_->browser_cite, sel);
368                 citekeys.erase(it);
369
370                 fl_insert_browser_line(dialog_->browser_cite, sel - 1, tmp.c_str());
371                 fl_select_browser_line(dialog_->browser_cite, sel - 1);
372                 citekeys.insert(it - 1, tmp);
373                 setCiteButtons(ON);
374                 activate = ButtonPolicy::SMI_VALID;
375
376         } else if (ob == dialog_->button_down) {
377                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
378                 if (sel < 1 || sel > citekeys.size() - 1)
379                         return ButtonPolicy::SMI_NOOP;
380
381                 // Move the selected key down 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_previous) {
395                 findBiblio(biblio::BACKWARD);
396         } else if (ob == dialog_->button_next) {
397                 findBiblio(biblio::FORWARD);
398         } else if (ob == dialog_->input_search) {
399                 findBiblio(biblio::FORWARD);
400         } else if (ob == dialog_->choice_style ||
401                    ob == dialog_->check_full_author_list ||
402                    ob == dialog_->check_force_uppercase ||
403                    ob == dialog_->input_before ||
404                    ob == dialog_->input_after) {
405                 activate = ButtonPolicy::SMI_VALID;
406         }
407
408         string currentCitekey;
409         if (!citekeys.empty())
410                 currentCitekey = citekeys[0];
411
412         if (topCitekey != currentCitekey) {
413                 int choice = std::max(1, fl_get_choice(dialog_->choice_style));
414                 fillChoice(dialog_.get(),
415                            controller().getCiteStrings(currentCitekey));
416                 fl_set_choice(dialog_->choice_style, choice);
417         }
418
419         return activate;
420 }
421
422
423 void FormCitation::update()
424 {
425         // Make the list of all available bibliography keys
426         bibkeys = biblio::getKeys(controller().bibkeysInfo());
427         updateBrowser(dialog_->browser_bib, bibkeys);
428
429         // Ditto for the keys cited in this inset
430         citekeys = getVectorFromString(controller().params().getContents());
431         updateBrowser(dialog_->browser_cite, citekeys);
432
433         // Use the first citekey to fill choice_style
434         string key;
435         if (!citekeys.empty())
436                 key = citekeys[0];
437
438         fillChoice(dialog_.get(), controller().getCiteStrings(key));
439
440         // Use the citation command to update the GUI
441         updateStyle(dialog_.get(), controller().params().getCmdName());
442
443         bool const natbib = controller().usingNatbib();
444         setEnabled(dialog_->check_full_author_list, natbib);
445         setEnabled(dialog_->check_force_uppercase, natbib);
446         setEnabled(dialog_->choice_style, natbib);
447
448         // No keys have been selected yet, so...
449         fl_clear_browser(dialog_->browser_info);
450         setBibButtons(OFF);
451         setCiteButtons(OFF);
452
453         // Natbib can have comments before and after the citation.
454         // This is not yet supported. After only.
455         fl_set_input(dialog_->input_after,
456                      controller().params().getOptions().c_str());
457
458         fl_set_input(dialog_->input_before, _("Not yet supported"));
459         setEnabled(dialog_->input_before, false);
460 }
461
462
463 void FormCitation::updateBrowser(FL_OBJECT * browser,
464                                  vector<string> const & keys) const
465 {
466         // Check whether the current contents of the browser will be
467         // changed by loading the contents of the vec...
468         vector<string> browser_keys = getVector(browser);
469
470         if (browser_keys == keys) {
471                 fl_deselect_browser(browser);
472                 fl_set_browser_topline(browser, 1);
473                 return;
474         }
475
476         // They will be changed. Proceed.
477         fl_clear_browser(browser);
478
479         for (vector<string>::const_iterator it = keys.begin();
480              it != keys.end(); ++it) {
481                 string key = trim(*it);
482                 if (!key.empty())
483                         fl_add_browser_line(browser, key.c_str());
484         }
485 }
486
487
488 void FormCitation::setBibButtons(State status) const
489 {
490         setEnabled(dialog_->button_add, (status == ON));
491 }
492
493
494 void FormCitation::setCiteButtons(State status) const
495 {
496         int const sel     = fl_get_browser(dialog_->browser_cite);
497         int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
498         bool const activate      = (status == ON);
499         bool const activate_up   = (activate && sel != 1);
500         bool const activate_down = (activate && sel != maxline);
501
502         setEnabled(dialog_->button_del,  activate);
503         setEnabled(dialog_->button_up,   activate_up);
504         setEnabled(dialog_->button_down, activate_down);
505 }