]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormCitation.C
dont use pragma impementation and interface anymore
[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  * \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 "Tooltips.h"
21 #include "helper_funcs.h"
22 #include "xforms_helpers.h"
23 #include FORMS_H_LOCATION
24
25 #include "gettext.h"
26 #include "support/LAssert.h"
27 #include "support/lstrings.h"
28
29 using std::find;
30 using std::max;
31 using std::min;
32 using std::pair;
33 using std::sort;
34 using std::vector;
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 =
52                 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 FormCB<ControlCitation, FormDB<FD_citation> > base_class;
91
92
93 FormCitation::FormCitation()
94         : base_class(_("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         FormBase::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         bc().setOK(dialog_->button_ok);
138         bc().setApply(dialog_->button_apply);
139         bc().setCancel(dialog_->button_close);
140         bc().setRestore(dialog_->button_restore);
141
142         // disable for read-only documents
143         bc().addReadOnly(dialog_->button_add);
144         bc().addReadOnly(dialog_->button_del);
145         bc().addReadOnly(dialog_->button_up);
146         bc().addReadOnly(dialog_->button_down);
147         bc().addReadOnly(dialog_->choice_style);
148         bc().addReadOnly(dialog_->input_before);
149         bc().addReadOnly(dialog_->input_after);
150         bc().addReadOnly(dialog_->check_full_author_list);
151         bc().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 (!controller().bufferIsReadonly()) {
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 (!controller().bufferIsReadonly()) {
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,
324                                                           citekeys[sel - 1]),
325                                           dialog_->browser_info->w - 10);
326                         fl_add_browser_line(dialog_->browser_info, tmp.c_str());
327                 }
328
329         } else if (ob == dialog_->button_add) {
330                 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
331                 if (sel < 1 || sel > bibkeys.size())
332                         return ButtonPolicy::SMI_NOOP;
333
334                 // Add the selected browser_bib key to browser_cite
335                 fl_addto_browser(dialog_->browser_cite,
336                                   bibkeys[sel - 1].c_str());
337                 citekeys.push_back(bibkeys[sel - 1]);
338
339                 int const n = int(citekeys.size());
340                 fl_select_browser_line(dialog_->browser_cite, n);
341
342                 setBibButtons(OFF);
343                 setCiteButtons(ON);
344                 activate = ButtonPolicy::SMI_VALID;
345
346         } else if (ob == dialog_->button_del) {
347                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
348                 if (sel < 1 || sel > citekeys.size())
349                         return ButtonPolicy::SMI_NOOP;
350
351                 // Remove the selected key from browser_cite
352                 fl_delete_browser_line(dialog_->browser_cite, sel) ;
353                 citekeys.erase(citekeys.begin() + sel - 1);
354
355                 setBibButtons(ON);
356                 setCiteButtons(OFF);
357                 activate = ButtonPolicy::SMI_VALID;
358
359         } else if (ob == dialog_->button_up) {
360                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
361                 if (sel < 2 || sel > citekeys.size())
362                         return ButtonPolicy::SMI_NOOP;
363
364                 // Move the selected key up one line
365                 vector<string>::iterator it = citekeys.begin() + sel - 1;
366                 string const tmp = *it;
367
368                 fl_delete_browser_line(dialog_->browser_cite, sel);
369                 citekeys.erase(it);
370
371                 fl_insert_browser_line(dialog_->browser_cite, sel - 1, tmp.c_str());
372                 fl_select_browser_line(dialog_->browser_cite, sel - 1);
373                 citekeys.insert(it - 1, tmp);
374                 setCiteButtons(ON);
375                 activate = ButtonPolicy::SMI_VALID;
376
377         } else if (ob == dialog_->button_down) {
378                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
379                 if (sel < 1 || sel > citekeys.size() - 1)
380                         return ButtonPolicy::SMI_NOOP;
381
382                 // Move the selected key down one line
383                 vector<string>::iterator it = citekeys.begin() + sel - 1;
384                 string const tmp = *it;
385
386                 fl_delete_browser_line(dialog_->browser_cite, sel);
387                 citekeys.erase(it);
388
389                 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
390                 fl_select_browser_line(dialog_->browser_cite, sel+1);
391                 citekeys.insert(it+1, tmp);
392                 setCiteButtons(ON);
393                 activate = ButtonPolicy::SMI_VALID;
394
395         } else if (ob == dialog_->button_previous) {
396                 findBiblio(biblio::BACKWARD);
397         } else if (ob == dialog_->button_next) {
398                 findBiblio(biblio::FORWARD);
399         } else if (ob == dialog_->input_search) {
400                 findBiblio(biblio::FORWARD);
401         } else if (ob == dialog_->choice_style ||
402                    ob == dialog_->check_full_author_list ||
403                    ob == dialog_->check_force_uppercase ||
404                    ob == dialog_->input_before ||
405                    ob == dialog_->input_after) {
406                 activate = ButtonPolicy::SMI_VALID;
407         }
408
409         string currentCitekey;
410         if (!citekeys.empty())
411                 currentCitekey = citekeys[0];
412
413         if (topCitekey != currentCitekey) {
414                 int choice = std::max(1, fl_get_choice(dialog_->choice_style));
415                 fillChoice(dialog_.get(),
416                            controller().getCiteStrings(currentCitekey));
417                 fl_set_choice(dialog_->choice_style, choice);
418         }
419
420         return activate;
421 }
422
423
424 void FormCitation::update()
425 {
426         // Make the list of all available bibliography keys
427         bibkeys = biblio::getKeys(controller().bibkeysInfo());
428         updateBrowser(dialog_->browser_bib, bibkeys);
429
430         // Ditto for the keys cited in this inset
431         citekeys = getVectorFromString(controller().params().getContents());
432         updateBrowser(dialog_->browser_cite, citekeys);
433
434         // Use the first citekey to fill choice_style
435         string key;
436         if (!citekeys.empty()) 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 }