]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormCitation.C
doxygen fixes
[lyx.git] / src / frontends / xforms / FormCitation.C
1 /**
2  * \file xforms/FormCitation.C
3  * Copyright 2000-2001 The LyX Team.
4  * See the file COPYING.
5  *
6  * \author Angus Leeming, a.leeming@ic.ac.uk
7  */
8
9 #include <config.h>
10 #include <algorithm>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "xformsBC.h"
17 #include "ControlCitation.h"
18 #include "FormCitation.h"
19 #include "form_citation.h"
20 #include "Tooltips.h"
21 #include "helper_funcs.h"
22 #include "xforms_helpers.h"
23 #include "gettext.h"
24 #include "support/LAssert.h"
25 #include "support/lstrings.h"
26
27 using std::find;
28 using std::max;
29 using std::min;
30 using std::pair;
31 using std::sort;
32 using std::vector;
33
34 namespace {
35
36 // shamelessly stolen from Menubar_pimpl.C
37 int string_width(string const & str) 
38 {
39         return fl_get_string_widthTAB(FL_NORMAL_STYLE, FL_NORMAL_SIZE,
40                                       str.c_str(),
41                                       static_cast<int>(str.length()));
42 }
43
44
45 void fillChoice(FD_form_citation * dialog, vector<string> vec)
46 {
47         // Check whether the current contents of the browser will be
48         // changed by loading the contents of the vec...
49         vector<string> const choice_style =
50                 getVectorFromChoice(dialog->choice_style);
51
52         if (vec == choice_style)
53                 return;
54
55         // They will be changed. Proceed
56         string const str = " " + getStringFromVector(vec, " | ") + " ";
57
58         fl_clear_choice(dialog->choice_style);
59         fl_addto_choice(dialog->choice_style, str.c_str());
60
61         // The width of the choice varies with the contents.
62         // Ensure that it is centred in the frame.
63
64         int width = 0;
65         for (vector<string>::const_iterator it = vec.begin();
66              it != vec.end(); ++it) {
67                 width = max(width, string_width(*it));
68         }
69
70         int const dx =
71                 max(5, int(0.5 * (dialog->frame_style->w - width)));
72
73         fl_set_object_geometry(dialog->choice_style,
74                                dialog->frame_style->x + dx,
75                                dialog->choice_style->y,
76                                width,
77                                dialog->choice_style->h);
78 }
79
80
81 void updateStyle(FD_form_citation * dialog, string command)
82 {
83         // Find the style of the citekeys
84         vector<biblio::CiteStyle> const & styles =
85                 ControlCitation::getCiteStyles();
86         biblio::CitationStyle cs = biblio::getCitationStyle(command);
87
88         vector<biblio::CiteStyle>::const_iterator cit =
89                 find(styles.begin(), styles.end(), cs.style);
90
91         // Use this to initialise the GUI
92         if (cit == styles.end()) {
93                 fl_set_choice(dialog->choice_style, 1);
94                 fl_set_button(dialog->button_full_author_list, 0);
95                 fl_set_button(dialog->button_force_uppercase, 0);
96         } else {
97                 int const i = int(cit - styles.begin());
98                 fl_set_choice(dialog->choice_style, i+1);
99                 fl_set_button(dialog->button_full_author_list,  cs.full);
100                 fl_set_button(dialog->button_force_uppercase, cs.forceUCase);
101         }
102 }
103
104 } // namespace anon
105
106 typedef FormCB<ControlCitation, FormDB<FD_form_citation> > base_class;
107
108 FormCitation::FormCitation(ControlCitation & c)
109         : base_class(c, _("Citation"), false)
110 {}
111
112
113 void FormCitation::apply()
114 {
115         vector<biblio::CiteStyle> const & styles =
116                 ControlCitation::getCiteStyles();
117
118         int const choice = fl_get_choice(dialog_->choice_style) - 1;
119         bool const full  = fl_get_button(dialog_->button_full_author_list);
120         bool const force = fl_get_button(dialog_->button_force_uppercase);
121
122         string const command =
123                 biblio::getCiteCommand(styles[choice], full, force);
124
125         controller().params().setCmdName(command);
126         controller().params().setContents(getStringFromVector(citekeys));
127
128         string const after  = fl_get_input(dialog_->input_after);
129         controller().params().setOptions(after);
130 }
131
132
133 void FormCitation::hide()
134 {
135         citekeys.clear();
136         bibkeys.clear();
137
138         FormBase::hide();
139 }
140
141
142 void FormCitation::build()
143 {
144         dialog_.reset(build_citation());
145
146         fl_set_input_return(dialog_->input_after,  FL_RETURN_CHANGED);
147         fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
148         fl_set_input_return(dialog_->input_search, FL_RETURN_END);
149
150         fl_set_button(dialog_->button_search_case, 0);
151         fl_set_button(dialog_->button_search_type, 0);
152
153         setPrehandler(dialog_->input_search);
154         setPrehandler(dialog_->input_before);
155         setPrehandler(dialog_->input_after);
156
157         // Manage the ok, apply, restore and cancel/close buttons
158         bc().setOK(dialog_->button_ok);
159         bc().setApply(dialog_->button_apply);
160         bc().setCancel(dialog_->button_cancel);
161         bc().setRestore(dialog_->button_restore);
162
163         bc().addReadOnly(dialog_->button_add);
164         bc().addReadOnly(dialog_->button_del);
165         bc().addReadOnly(dialog_->button_up);
166         bc().addReadOnly(dialog_->button_down);
167         bc().addReadOnly(dialog_->choice_style);
168         bc().addReadOnly(dialog_->input_before);
169         bc().addReadOnly(dialog_->input_after);
170         bc().addReadOnly(dialog_->button_full_author_list);
171         bc().addReadOnly(dialog_->button_force_uppercase);
172
173         //set up the tooltip mechanism
174         string str = N_("Add the selected entry to the current citation reference.");
175         tooltips().initTooltip(dialog_->button_add, str);
176
177         str = N_("Delete the selected entry from the current citation reference.");
178         tooltips().initTooltip(dialog_->button_del, str);
179
180         str = N_("Move the selected entry upwards (in the current list).");
181         tooltips().initTooltip(dialog_->button_up, str);
182
183         str = N_("Move the selected entry downwards (in the current list).");
184         tooltips().initTooltip(dialog_->button_down, str);
185
186         str = N_("The entries which will be cited. Select them with the arrow buttons from the right browser window.");
187         tooltips().initTooltip(dialog_->browser_cite, str);
188
189         str = N_("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.");
190         tooltips().initTooltip(dialog_->browser_bib, str);
191
192         str = N_("Information about the selected entry");
193         tooltips().initTooltip(dialog_->browser_info, str);
194
195         str = N_("Here you may select how the citation label should look inside the text (Natbib).");
196         tooltips().initTooltip(dialog_->choice_style, str);
197
198         str = N_("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().initTooltip(dialog_->button_full_author_list, str);
200
201         str = N_("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().initTooltip(dialog_->button_force_uppercase, str);
203
204         str = N_("Optional text which appears before the citation reference, e.g. \"see <Ref>\"");
205         tooltips().initTooltip(dialog_->input_before, str);
206
207         str = N_("Optional text which appears after the citation reference, e.g. \"pp. 12\"");
208         tooltips().initTooltip(dialog_->input_after, str);
209
210         str = N_("Search your database (all fields will be searched).");
211         tooltips().initTooltip(dialog_->input_search, str);
212
213         str = N_("Activate if you want to have case sensitive search: \"bibtex\" finds \"bibtex\", but not \"BibTeX\".");
214         tooltips().initTooltip(dialog_->button_search_case, str);
215
216         str = N_("Activate if you want to enter Regular Expressions.");
217         tooltips().initTooltip(dialog_->button_search_type, str);
218 }
219
220
221 void FormCitation::findBiblio(biblio::Direction const dir)
222 {
223         string const str = fl_get_input(dialog_->input_search);
224         biblio::InfoMap const & theMap = controller().bibkeysInfo();
225         bool const caseSensitive =
226                 fl_get_button(dialog_->button_search_case);
227         biblio::Search const type =
228                 fl_get_button(dialog_->button_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 (!controller().isReadonly()) {
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 (!controller().isReadonly()) {
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,
329                                                           citekeys[sel-1]),
330                                           dialog_->browser_info->w-10);
331                         fl_add_browser_line(dialog_->browser_info, tmp.c_str());
332                 }
333
334         } else if (ob == dialog_->button_add) {
335                 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
336                 if (sel < 1 || sel > bibkeys.size())
337                         return ButtonPolicy::SMI_NOOP;
338
339                 // Add the selected browser_bib key to browser_cite
340                 fl_addto_browser(dialog_->browser_cite,
341                                   bibkeys[sel-1].c_str());
342                 citekeys.push_back(bibkeys[sel-1]);
343
344                 int const n = int(citekeys.size());
345                 fl_select_browser_line(dialog_->browser_cite, n);
346
347                 setBibButtons(OFF);
348                 setCiteButtons(ON);
349                 activate = ButtonPolicy::SMI_VALID;
350
351         } else if (ob == dialog_->button_del) {
352                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
353                 if (sel < 1 || sel > citekeys.size())
354                         return ButtonPolicy::SMI_NOOP;
355
356                 // Remove the selected key from browser_cite
357                 fl_delete_browser_line(dialog_->browser_cite, sel) ;
358                 citekeys.erase(citekeys.begin() + sel-1);
359
360                 setBibButtons(ON);
361                 setCiteButtons(OFF);
362                 activate = ButtonPolicy::SMI_VALID;
363
364         } else if (ob == dialog_->button_up) {
365                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
366                 if (sel < 2 || sel > citekeys.size())
367                         return ButtonPolicy::SMI_NOOP;
368
369                 // Move the selected key up one line
370                 vector<string>::iterator it = citekeys.begin() + sel-1;
371                 string const tmp = *it;
372
373                 fl_delete_browser_line(dialog_->browser_cite, sel);
374                 citekeys.erase(it);
375
376                 fl_insert_browser_line(dialog_->browser_cite, sel-1, tmp.c_str());
377                 fl_select_browser_line(dialog_->browser_cite, sel-1);
378                 citekeys.insert(it-1, tmp);
379                 setCiteButtons(ON);
380                 activate = ButtonPolicy::SMI_VALID;
381
382         } else if (ob == dialog_->button_down) {
383                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
384                 if (sel < 1 || sel > citekeys.size()-1)
385                         return ButtonPolicy::SMI_NOOP;
386
387                 // Move the selected key down one line
388                 vector<string>::iterator it = citekeys.begin() + sel-1;
389                 string const tmp = *it;
390
391                 fl_delete_browser_line(dialog_->browser_cite, sel);
392                 citekeys.erase(it);
393
394                 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
395                 fl_select_browser_line(dialog_->browser_cite, sel+1);
396                 citekeys.insert(it+1, tmp);
397                 setCiteButtons(ON);
398                 activate = ButtonPolicy::SMI_VALID;
399
400         } else if (ob == dialog_->button_previous) {
401                 findBiblio(biblio::BACKWARD);
402         } else if (ob == dialog_->button_next) {
403                 findBiblio(biblio::FORWARD);
404         } else if (ob == dialog_->input_search) {
405                 findBiblio(biblio::FORWARD);
406         } else if (ob == dialog_->choice_style ||
407                    ob == dialog_->button_full_author_list ||
408                    ob == dialog_->button_force_uppercase ||
409                    ob == dialog_->input_before ||
410                    ob == dialog_->input_after) {
411                 activate = ButtonPolicy::SMI_VALID;
412         }
413
414         string currentCitekey;
415         if (!citekeys.empty())
416                 currentCitekey = citekeys[0];
417
418         if (topCitekey != currentCitekey) {
419                 int choice = fl_get_choice(dialog_->choice_style);
420                 fillChoice(dialog_.get(),
421                            controller().getCiteStrings(currentCitekey));
422                 fl_set_choice(dialog_->choice_style, choice);
423         }
424
425         return activate;
426 }
427
428
429 void FormCitation::update()
430 {
431         // Make the list of all available bibliography keys
432         bibkeys = biblio::getKeys(controller().bibkeysInfo());
433         updateBrowser(dialog_->browser_bib, bibkeys);
434
435         // Ditto for the keys cited in this inset
436         citekeys = getVectorFromString(controller().params().getContents());
437         updateBrowser(dialog_->browser_cite, citekeys);
438
439         // Use the first citekey to fill choice_style
440         string key;
441         if (!citekeys.empty()) 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_->button_full_author_list, natbib);
450         setEnabled(dialog_->button_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"));
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 = getVectorFromBrowser(browser);
474
475         if (browser_keys == keys)
476                 return;
477
478         // They will be changed. Proceed.
479         fl_clear_browser(browser);
480
481         for (vector<string>::const_iterator it = keys.begin();
482              it < keys.end(); ++it) {
483                 string key = frontStrip(strip(*it));
484                 if (!key.empty())
485                         fl_add_browser_line(browser, key.c_str());
486         }
487 }
488
489
490 void FormCitation::setBibButtons(State status) const
491 {
492         setEnabled(dialog_->button_add, (status == ON));
493 }
494
495
496 void FormCitation::setCiteButtons(State status) const
497 {
498         int const sel     = fl_get_browser(dialog_->browser_cite);
499         int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
500         bool const activate      = (status == ON);
501         bool const activate_up   = (activate && sel != 1);
502         bool const activate_down = (activate && sel != maxline);
503
504         setEnabled(dialog_->button_del,  activate);
505         setEnabled(dialog_->button_up,   activate_up);
506         setEnabled(dialog_->button_down, activate_down);
507 }