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