]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormCitation.C
John's recent build.diff patch
[lyx.git] / src / frontends / xforms / FormCitation.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  *
5  *           LyX, The Document Processor
6  *
7  *           Copyright 2000-2001 The LyX Team.
8  *
9  * ======================================================
10  *
11  * \file FormCitation.C
12  * \author Angus Leeming, a.leeming@ic.ac.uk
13  */
14
15 #include <config.h>
16 #include <algorithm>
17
18 #ifdef __GNUG__
19 #pragma implementation
20 #endif
21
22 #include "xformsBC.h"
23 #include "ControlCitation.h"
24 #include "FormCitation.h"
25 #include "form_citation.h"
26 #include "gettext.h"
27 #include "support/lstrings.h"
28 #include "helper_funcs.h"
29 #include "xforms_helpers.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(FL_OBJECT * choice, vector<string> const & vec)
50 {
51         string const str = " " + getStringFromVector(vec, " | ") + " ";
52
53         fl_clear_choice(choice);
54         fl_addto_choice(choice, str.c_str());
55
56         int width = 0;
57         for (vector<string>::const_iterator it = vec.begin();
58              it != vec.end(); ++it) {
59                 width = max(width, string_width(*it));
60         }
61
62         // Paranoia checks
63         int const x = max(5, int(choice->x + 0.5 * (choice->w - width)));
64         if (x + width > choice->form->w)
65                 width = choice->form->w - 10;
66         
67         fl_set_object_geometry(choice, x, choice->y, width + 5, choice->h);
68 }
69
70 void updateStyle(FL_OBJECT * choice, FL_OBJECT * full, FL_OBJECT * force,
71                  string command)
72 {
73         // Find the style of the citekeys
74         vector<biblio::CiteStyle> const & styles =
75                 ControlCitation::getCiteStyles();
76         biblio::CitationStyle cs = biblio::getCitationStyle(command);
77
78         vector<biblio::CiteStyle>::const_iterator cit =
79                 find(styles.begin(), styles.end(), cs.style);
80
81         // Use this to initialise the GUI
82         if (cit == styles.end()) {
83                 fl_set_choice(choice, 1);
84                 fl_set_button(full, 0);
85                 fl_set_button(force, 0);
86         } else {
87                 int const i = int(cit - styles.begin());
88                 fl_set_choice(choice, i+1);
89                 fl_set_button(full,  cs.full);
90                 fl_set_button(force, cs.forceUCase);
91         }
92 }
93
94 } // namespace anon
95
96 typedef FormCB<ControlCitation, FormDB<FD_form_citation> > base_class;
97
98 FormCitation::FormCitation(ControlCitation & c)
99         : base_class(c, _("Citation"))
100 {}
101
102
103 void FormCitation::apply()
104 {
105         vector<biblio::CiteStyle> const & styles =
106                 ControlCitation::getCiteStyles();
107
108         int const choice = fl_get_choice(dialog_->choice_style) - 1;
109         bool const full  = fl_get_button(dialog_->button_full_author_list);
110         bool const force = fl_get_button(dialog_->button_force_uppercase);
111
112         string const command =
113                 biblio::getCiteCommand(styles[choice], full, force);
114
115         controller().params().setCmdName(command);
116         controller().params().setContents(getStringFromVector(citekeys));
117
118         string const after  = fl_get_input(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());
135
136         fl_set_input_return(dialog_->input_after,  FL_RETURN_CHANGED);
137         fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
138         fl_set_input_return(dialog_->input_search, FL_RETURN_END);
139
140         fl_set_button(dialog_->button_search_case, 0);
141         fl_set_button(dialog_->button_search_type, 0);
142
143         // Manage the ok, apply, restore and cancel/close buttons
144         bc().setOK(dialog_->button_ok);
145         bc().setApply(dialog_->button_apply);
146         bc().setCancel(dialog_->button_cancel);
147         bc().setRestore(dialog_->button_restore);
148
149         bc().addReadOnly(dialog_->button_add);
150         bc().addReadOnly(dialog_->button_del);
151         bc().addReadOnly(dialog_->button_up);
152         bc().addReadOnly(dialog_->button_down);
153         bc().addReadOnly(dialog_->choice_style);
154         bc().addReadOnly(dialog_->input_before);
155         bc().addReadOnly(dialog_->input_after);
156         bc().addReadOnly(dialog_->button_full_author_list);
157         bc().addReadOnly(dialog_->button_force_uppercase);
158 }
159
160
161 void FormCitation::findBiblio(biblio::Direction const dir)
162 {
163         string const str = fl_get_input(dialog_->input_search);
164         biblio::InfoMap const & theMap = controller().bibkeysInfo();
165         bool const caseSensitive =
166                 fl_get_button(dialog_->button_search_case);
167         biblio::Search const type =
168                 fl_get_button(dialog_->button_search_type) ?
169                 biblio::REGEX : biblio::SIMPLE;
170
171         vector<string>::const_iterator start = bibkeys.begin();
172         int const sel = fl_get_browser(dialog_->browser_bib);
173         if (sel >= 1 && sel <= int(bibkeys.size()))
174                 start += sel - 1;
175
176         // Find the NEXT instance...
177         (dir == biblio::FORWARD) ? ++start : --start;
178
179
180         vector<string>::const_iterator const cit =
181         biblio::searchKeys(theMap, bibkeys, str,
182                            start, type, dir, caseSensitive);
183
184         if (cit == bibkeys.end())
185                 return;
186
187         int const found = int(cit - bibkeys.begin()) + 1;
188         if (found == sel)
189                 return;
190
191         // Update the display
192         int const top = max(found - 5, 1);
193         fl_set_browser_topline(dialog_->browser_bib, top);
194         fl_select_browser_line(dialog_->browser_bib, found);
195         input(dialog_->browser_bib, 0);
196 }
197  
198
199 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
200 {
201         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
202
203         biblio::InfoMap const & theMap = controller().bibkeysInfo();
204
205         string topCitekey;
206         if (!citekeys.empty()) topCitekey = citekeys[0];
207
208         if (ob == dialog_->browser_bib) {
209                 fl_deselect_browser(dialog_->browser_cite);
210
211                 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
212                 if (sel < 1 || sel > bibkeys.size())
213                         return ButtonPolicy::SMI_NOOP;
214
215                 // Put into browser_info the additional info associated with
216                 // the selected browser_bib key
217                 fl_clear_browser(dialog_->browser_info);
218
219                 string const tmp = formatted(biblio::getInfo(theMap,
220                                                              bibkeys[sel-1]),
221                                               dialog_->browser_info->w-10 );
222                 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
223
224                 // Highlight the selected browser_bib key in browser_cite if
225                 // present
226                 vector<string>::const_iterator cit =
227                         find(citekeys.begin(), citekeys.end(), bibkeys[sel-1]);
228
229                 if (cit != citekeys.end()) {
230                         int const n = int(cit - citekeys.begin());
231                         fl_select_browser_line(dialog_->browser_cite, n+1);
232                         fl_set_browser_topline(dialog_->browser_cite, n+1);
233                 }
234
235                 if (!controller().isReadonly()) {
236                         if (cit != citekeys.end()) {
237                                 setBibButtons(OFF);
238                                 setCiteButtons(ON);
239                         } else {
240                                 setBibButtons(ON);
241                                 setCiteButtons(OFF);
242                         }
243                 }
244
245         } else if (ob == dialog_->browser_cite) {
246                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
247                 if (sel < 1 || sel > citekeys.size())
248                         return ButtonPolicy::SMI_NOOP;
249
250                 if (!controller().isReadonly()) {
251                         setBibButtons(OFF);
252                         setCiteButtons(ON);
253                 }
254
255                 // Highlight the selected browser_cite key in browser_bib
256                 vector<string>::const_iterator cit =
257                         find(bibkeys.begin(), bibkeys.end(), citekeys[sel-1]);
258
259                 if (cit != bibkeys.end()) {
260                         int const n = int(cit - bibkeys.begin());
261                         fl_select_browser_line(dialog_->browser_bib, n+1);
262                         fl_set_browser_topline(dialog_->browser_bib, n+1);
263
264                         // Put into browser_info the additional info associated
265                         // with the selected browser_cite key
266                         fl_clear_browser(dialog_->browser_info);
267                         string const tmp =
268                                 formatted(biblio::getInfo(theMap,
269                                                           citekeys[sel-1]),
270                                           dialog_->browser_info->w-10);
271                         fl_add_browser_line(dialog_->browser_info, tmp.c_str());
272                 }
273
274         } else if (ob == dialog_->button_add) {
275                 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
276                 if (sel < 1 || sel > bibkeys.size())
277                         return ButtonPolicy::SMI_NOOP;
278
279                 // Add the selected browser_bib key to browser_cite
280                 fl_addto_browser(dialog_->browser_cite,
281                                   bibkeys[sel-1].c_str());
282                 citekeys.push_back(bibkeys[sel-1]);
283
284                 int const n = int(citekeys.size());
285                 fl_select_browser_line(dialog_->browser_cite, n);
286
287                 setBibButtons(OFF);
288                 setCiteButtons(ON);
289                 activate = ButtonPolicy::SMI_VALID;
290
291         } else if (ob == dialog_->button_del) {
292                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
293                 if (sel < 1 || sel > citekeys.size())
294                         return ButtonPolicy::SMI_NOOP;
295
296                 // Remove the selected key from browser_cite
297                 fl_delete_browser_line(dialog_->browser_cite, sel) ;
298                 citekeys.erase(citekeys.begin() + sel-1);
299
300                 setBibButtons(ON);
301                 setCiteButtons(OFF);
302                 activate = ButtonPolicy::SMI_VALID;
303
304         } else if (ob == dialog_->button_up) {
305                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
306                 if (sel < 2 || sel > citekeys.size())
307                         return ButtonPolicy::SMI_NOOP;
308
309                 // Move the selected key up one line
310                 vector<string>::iterator it = citekeys.begin() + sel-1;
311                 string const tmp = *it;
312
313                 fl_delete_browser_line(dialog_->browser_cite, sel);
314                 citekeys.erase(it);
315
316                 fl_insert_browser_line(dialog_->browser_cite, sel-1, tmp.c_str());
317                 fl_select_browser_line(dialog_->browser_cite, sel-1);
318                 citekeys.insert(it-1, tmp);
319                 setCiteButtons(ON);
320                 activate = ButtonPolicy::SMI_VALID;
321
322         } else if (ob == dialog_->button_down) {
323                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
324                 if (sel < 1 || sel > citekeys.size()-1)
325                         return ButtonPolicy::SMI_NOOP;
326
327                 // Move the selected key down one line
328                 vector<string>::iterator it = citekeys.begin() + sel-1;
329                 string const tmp = *it;
330
331                 fl_delete_browser_line(dialog_->browser_cite, sel);
332                 citekeys.erase(it);
333
334                 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
335                 fl_select_browser_line(dialog_->browser_cite, sel+1);
336                 citekeys.insert(it+1, tmp);
337                 setCiteButtons(ON);
338                 activate = ButtonPolicy::SMI_VALID;
339
340         } else if (ob == dialog_->button_previous) {
341                 findBiblio(biblio::BACKWARD);
342         } else if (ob == dialog_->button_next) {
343                 findBiblio(biblio::FORWARD);
344         } else if (ob == dialog_->input_search) {
345                 findBiblio(biblio::FORWARD);
346         } else if (ob == dialog_->choice_style ||
347                    ob == dialog_->button_full_author_list ||
348                    ob == dialog_->button_force_uppercase ||
349                    ob == dialog_->input_before ||
350                    ob == dialog_->input_after) {
351                 activate = ButtonPolicy::SMI_VALID;
352         }
353
354         string currentCitekey;
355         if (!citekeys.empty())
356                 currentCitekey = citekeys[0];
357
358         if (topCitekey != currentCitekey) {
359                 int choice = fl_get_choice(dialog_->choice_style);
360                 fillChoice(dialog_->choice_style,
361                            controller().getCiteStrings(currentCitekey));
362                 fl_set_choice(dialog_->choice_style, choice);
363         }
364
365         return activate;
366 }
367
368
369 void FormCitation::update()
370 {
371         // Make the list of all available bibliography keys
372         bibkeys = biblio::getKeys(controller().bibkeysInfo());
373         updateBrowser(dialog_->browser_bib, bibkeys);
374
375         // Ditto for the keys cited in this inset
376         citekeys = getVectorFromString(controller().params().getContents());
377         updateBrowser(dialog_->browser_cite, citekeys);
378
379         // Use the first citekey to fill choice_style
380         string key;
381         if (!citekeys.empty()) key = citekeys[0];
382
383         fillChoice(dialog_->choice_style, controller().getCiteStrings(key));
384
385         // Use the citation command to update the GUI
386         updateStyle(dialog_->choice_style, 
387                     dialog_->button_full_author_list,
388                     dialog_->button_force_uppercase,
389                     controller().params().getCmdName());
390
391         bool const natbib = controller().usingNatbib();
392         setEnabled(dialog_->button_full_author_list, natbib);
393         setEnabled(dialog_->button_force_uppercase, natbib);
394         setEnabled(dialog_->choice_style, natbib);
395         
396         // No keys have been selected yet, so...
397         fl_clear_browser(dialog_->browser_info);
398         setBibButtons(OFF);
399         setCiteButtons(OFF);
400
401         // Natbib can have comments before and after the citation.
402         // This is not yet supported. After only.
403         fl_set_input(dialog_->input_after,
404                      controller().params().getOptions().c_str());
405
406         fl_set_input(dialog_->input_before, _("Not yet supported"));
407         setEnabled(dialog_->input_before, false);
408 }
409
410
411 void FormCitation::updateBrowser(FL_OBJECT * browser,
412                                  vector<string> const & keys) const
413 {
414         fl_clear_browser(browser);
415
416         for (vector<string>::const_iterator it = keys.begin();
417              it < keys.end(); ++it) {
418                 string key = frontStrip(strip(*it));
419                 if (!key.empty())
420                         fl_add_browser_line(browser, key.c_str());
421         }
422 }
423
424
425 void FormCitation::setBibButtons(State status) const
426 {
427         setEnabled(dialog_->button_add, (status == ON));
428 }
429
430
431 void FormCitation::setCiteButtons(State status) const
432 {
433         int const sel     = fl_get_browser(dialog_->browser_cite);
434         int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
435         bool const activate      = (status == ON);
436         bool const activate_up   = (activate && sel != 1);
437         bool const activate_down = (activate && sel != maxline);
438
439         setEnabled(dialog_->button_del,  activate);
440         setEnabled(dialog_->button_up,   activate_up);
441         setEnabled(dialog_->button_down, activate_down);
442 }