]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormCitation.C
Yet more dialog tweaking from Rob.
[lyx.git] / src / frontends / xforms / FormCitation.C
1 /**
2  * \file xforms/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  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12 #include <algorithm>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "xformsBC.h"
19 #include "ControlCitation.h"
20 #include "FormCitation.h"
21 #include "forms/form_citation.h"
22 #include "Tooltips.h"
23 #include "helper_funcs.h"
24 #include "xforms_helpers.h"
25 #include FORMS_H_LOCATION
26
27 #include "gettext.h"
28 #include "support/LAssert.h"
29 #include "support/lstrings.h"
30
31 using std::find;
32 using std::max;
33 using std::min;
34 using std::pair;
35 using std::sort;
36 using std::vector;
37
38 namespace {
39
40 // shamelessly stolen from Menubar_pimpl.C
41 int string_width(string const & str)
42 {
43         return fl_get_string_widthTAB(FL_NORMAL_STYLE, FL_NORMAL_SIZE,
44                                       str.c_str(),
45                                       static_cast<int>(str.length()));
46 }
47
48
49 void fillChoice(FD_citation * dialog, vector<string> vec)
50 {
51         // Check whether the current contents of the browser will be
52         // changed by loading the contents of the vec...
53         vector<string> const choice_style =
54                 getVector(dialog->choice_style);
55
56         if (vec == choice_style)
57                 return;
58
59         // They will be changed. Proceed
60         string str = " ";
61         if (!vec.empty())
62                 str += getStringFromVector(vec, " | ") + " ";
63
64         fl_clear_choice(dialog->choice_style);
65         fl_addto_choice(dialog->choice_style, str.c_str());
66
67         setEnabled(dialog->choice_style, !vec.empty());
68         if (vec.empty())
69                 return;
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 cs = biblio::getCitationStyle(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         if (cit == styles.end()) {
85                 fl_set_choice(dialog->choice_style, 1);
86                 fl_set_button(dialog->check_full_author_list, 0);
87                 fl_set_button(dialog->check_force_uppercase, 0);
88         } else {
89                 int const i = int(cit - styles.begin());
90                 fl_set_choice(dialog->choice_style, i+1);
91                 fl_set_button(dialog->check_full_author_list,  cs.full);
92                 fl_set_button(dialog->check_force_uppercase, cs.forceUCase);
93         }
94 }
95
96 } // namespace anon
97
98
99 typedef FormCB<ControlCitation, FormDB<FD_citation> > base_class;
100
101
102 FormCitation::FormCitation()
103         : base_class(_("Citation"))
104 {}
105
106
107 void FormCitation::apply()
108 {
109         string command = "cite";
110         if (dialog_->choice_style->active != 0) {
111                 vector<biblio::CiteStyle> const & styles =
112                         ControlCitation::getCiteStyles();
113
114                 int const choice =
115                         std::max(0, fl_get_choice(dialog_->choice_style) - 1);
116                 bool const full  =
117                         fl_get_button(dialog_->check_full_author_list);
118                 bool const force =
119                         fl_get_button(dialog_->check_force_uppercase);
120
121                 command = biblio::getCiteCommand(styles[choice], full, force);
122         }
123
124         controller().params().setCmdName(command);
125         controller().params().setContents(getStringFromVector(citekeys));
126
127         string const after  = fl_get_input(dialog_->input_after);
128         controller().params().setOptions(after);
129 }
130
131
132 void FormCitation::hide()
133 {
134         citekeys.clear();
135         bibkeys.clear();
136
137         FormBase::hide();
138 }
139
140
141 void FormCitation::build()
142 {
143         dialog_.reset(build_citation(this));
144
145         fl_set_input_return(dialog_->input_after,  FL_RETURN_CHANGED);
146         fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
147         fl_set_input_return(dialog_->input_search, FL_RETURN_END);
148
149         fl_set_button(dialog_->check_search_case, 0);
150         fl_set_button(dialog_->check_search_type, 0);
151
152         setPrehandler(dialog_->input_search);
153         setPrehandler(dialog_->input_before);
154         setPrehandler(dialog_->input_after);
155
156         // Manage the ok, apply, restore and cancel/close buttons
157         bc().setOK(dialog_->button_ok);
158         bc().setApply(dialog_->button_apply);
159         bc().setCancel(dialog_->button_close);
160         bc().setRestore(dialog_->button_restore);
161
162         bc().addReadOnly(dialog_->button_add);
163         bc().addReadOnly(dialog_->button_del);
164         bc().addReadOnly(dialog_->button_up);
165         bc().addReadOnly(dialog_->button_down);
166         bc().addReadOnly(dialog_->choice_style);
167         bc().addReadOnly(dialog_->input_before);
168         bc().addReadOnly(dialog_->input_after);
169         bc().addReadOnly(dialog_->check_full_author_list);
170         bc().addReadOnly(dialog_->check_force_uppercase);
171
172         //set up the tooltip mechanism
173         string str = _("Add the selected entry to the current citation reference.");
174         tooltips().init(dialog_->button_add, str);
175
176         str = _("Delete the selected entry from the current citation reference.");
177         tooltips().init(dialog_->button_del, str);
178
179         str = _("Move the selected entry upwards (in the current list).");
180         tooltips().init(dialog_->button_up, str);
181
182         str = _("Move the selected entry downwards (in the current list).");
183         tooltips().init(dialog_->button_down, str);
184
185         str = _("The entries which will be cited. Select them with the arrow buttons from the right browser window.");
186         tooltips().init(dialog_->browser_cite, str);
187
188         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.");
189         tooltips().init(dialog_->browser_bib, str);
190
191         str = _("Information about the selected entry");
192         tooltips().init(dialog_->browser_info, str);
193
194         str = _("Here you may select how the citation label should look inside the text (Natbib).");
195         tooltips().init(dialog_->choice_style, str);
196
197         str = _("Activate if you want to print all authors in a reference with more than three authors, and not \"<First Author> et al.\" (Natbib).");
198         tooltips().init(dialog_->check_full_author_list, str);
199
200         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).");
201         tooltips().init(dialog_->check_force_uppercase, str);
202
203         str = _("Optional text which appears before the citation reference, e.g. \"see <Ref>\"");
204         tooltips().init(dialog_->input_before, str);
205
206         str = _("Optional text which appears after the citation reference, e.g. \"pp. 12\"");
207         tooltips().init(dialog_->input_after, str);
208
209         str = _("Search your database (all fields will be searched).");
210         tooltips().init(dialog_->input_search, str);
211
212         str = _("Activate if you want to have case sensitive search: \"bibtex\" finds \"bibtex\", but not \"BibTeX\".");
213         tooltips().init(dialog_->check_search_case, str);
214
215         str = _("Activate if you want to enter Regular Expressions.");
216         tooltips().init(dialog_->check_search_type, str);
217 }
218
219
220 void FormCitation::findBiblio(biblio::Direction const dir)
221 {
222         string const str = fl_get_input(dialog_->input_search);
223         biblio::InfoMap const & theMap = controller().bibkeysInfo();
224         bool const caseSensitive =
225                 fl_get_button(dialog_->check_search_case);
226         biblio::Search const type =
227                 fl_get_button(dialog_->check_search_type) ?
228                 biblio::REGEX : biblio::SIMPLE;
229
230         vector<string>::const_iterator start = bibkeys.begin();
231         int const sel = fl_get_browser(dialog_->browser_bib);
232         if (sel >= 1 && sel <= int(bibkeys.size()))
233                 start += sel - 1;
234
235         // Find the NEXT instance...
236         (dir == biblio::FORWARD) ? ++start : --start;
237
238
239         vector<string>::const_iterator const cit =
240                 biblio::searchKeys(theMap, bibkeys, str,
241                                    start, type, dir, caseSensitive);
242
243         if (cit == bibkeys.end())
244                 return;
245
246         int const found = int(cit - bibkeys.begin()) + 1;
247         if (found == sel)
248                 return;
249
250         // Update the display
251         int const top = max(found - 5, 1);
252         fl_set_browser_topline(dialog_->browser_bib, top);
253         fl_select_browser_line(dialog_->browser_bib, found);
254         input(dialog_->browser_bib, 0);
255 }
256
257
258 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
259 {
260         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
261
262         biblio::InfoMap const & theMap = controller().bibkeysInfo();
263
264         string topCitekey;
265         if (!citekeys.empty()) topCitekey = citekeys[0];
266
267         if (ob == dialog_->browser_bib) {
268                 fl_deselect_browser(dialog_->browser_cite);
269
270                 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
271                 if (sel < 1 || sel > bibkeys.size())
272                         return ButtonPolicy::SMI_NOOP;
273
274                 // Put into browser_info the additional info associated with
275                 // the selected browser_bib key
276                 fl_clear_browser(dialog_->browser_info);
277
278                 string const tmp = formatted(biblio::getInfo(theMap,
279                                                              bibkeys[sel - 1]),
280                                               dialog_->browser_info->w - 10);
281                 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
282
283                 // Highlight the selected browser_bib key in browser_cite if
284                 // present
285                 vector<string>::const_iterator cit =
286                         find(citekeys.begin(), citekeys.end(), bibkeys[sel - 1]);
287
288                 if (cit != citekeys.end()) {
289                         int const n = int(cit - citekeys.begin());
290                         fl_select_browser_line(dialog_->browser_cite, n + 1);
291                         fl_set_browser_topline(dialog_->browser_cite, n + 1);
292                 }
293
294                 if (!controller().bufferIsReadonly()) {
295                         if (cit != citekeys.end()) {
296                                 setBibButtons(OFF);
297                                 setCiteButtons(ON);
298                         } else {
299                                 setBibButtons(ON);
300                                 setCiteButtons(OFF);
301                         }
302                 }
303
304         } else if (ob == dialog_->browser_cite) {
305                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
306                 if (sel < 1 || sel > citekeys.size())
307                         return ButtonPolicy::SMI_NOOP;
308
309                 if (!controller().bufferIsReadonly()) {
310                         setBibButtons(OFF);
311                         setCiteButtons(ON);
312                 }
313
314                 // Highlight the selected browser_cite key in browser_bib
315                 vector<string>::const_iterator cit =
316                         find(bibkeys.begin(), bibkeys.end(), citekeys[sel - 1]);
317
318                 if (cit != bibkeys.end()) {
319                         int const n = int(cit - bibkeys.begin());
320                         fl_select_browser_line(dialog_->browser_bib, n + 1);
321                         fl_set_browser_topline(dialog_->browser_bib, n + 1);
322
323                         // Put into browser_info the additional info associated
324                         // with the selected browser_cite key
325                         fl_clear_browser(dialog_->browser_info);
326                         string const tmp =
327                                 formatted(biblio::getInfo(theMap,
328                                                           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()) key = citekeys[0];
441
442         fillChoice(dialog_.get(), controller().getCiteStrings(key));
443
444         // Use the citation command to update the GUI
445         updateStyle(dialog_.get(), controller().params().getCmdName());
446
447         bool const natbib = controller().usingNatbib();
448         setEnabled(dialog_->check_full_author_list, natbib);
449         setEnabled(dialog_->check_force_uppercase, natbib);
450         setEnabled(dialog_->choice_style, natbib);
451
452         // No keys have been selected yet, so...
453         fl_clear_browser(dialog_->browser_info);
454         setBibButtons(OFF);
455         setCiteButtons(OFF);
456
457         // Natbib can have comments before and after the citation.
458         // This is not yet supported. After only.
459         fl_set_input(dialog_->input_after,
460                      controller().params().getOptions().c_str());
461
462         fl_set_input(dialog_->input_before, _("Not yet supported"));
463         setEnabled(dialog_->input_before, false);
464 }
465
466
467 void FormCitation::updateBrowser(FL_OBJECT * browser,
468                                  vector<string> const & keys) const
469 {
470         // Check whether the current contents of the browser will be
471         // changed by loading the contents of the vec...
472         vector<string> browser_keys = getVector(browser);
473
474         if (browser_keys == keys)
475                 return;
476
477         // They will be changed. Proceed.
478         fl_clear_browser(browser);
479
480         for (vector<string>::const_iterator it = keys.begin();
481              it < keys.end(); ++it) {
482                 string key = trim(*it);
483                 if (!key.empty())
484                         fl_add_browser_line(browser, key.c_str());
485         }
486 }
487
488
489 void FormCitation::setBibButtons(State status) const
490 {
491         setEnabled(dialog_->button_add, (status == ON));
492 }
493
494
495 void FormCitation::setCiteButtons(State status) const
496 {
497         int const sel     = fl_get_browser(dialog_->browser_cite);
498         int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
499         bool const activate      = (status == ON);
500         bool const activate_up   = (activate && sel != 1);
501         bool const activate_down = (activate && sel != maxline);
502
503         setEnabled(dialog_->button_del,  activate);
504         setEnabled(dialog_->button_up,   activate_up);
505         setEnabled(dialog_->button_down, activate_down);
506 }