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