]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormCitation.C
Added // -*- C++ -*- to the top of all files in controllers/ and xforms/
[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 "biblio.h"
29 #include "helper_funcs.h"
30 #include "xforms_helpers.h"
31
32 using std::find;
33 using std::max;
34 using std::min;
35 using std::pair;
36 using std::sort;
37 using std::vector;
38
39 typedef FormCB<ControlCitation, FormDB<FD_form_citation> > base_class;
40
41 FormCitation::FormCitation(ControlCitation & c)
42         : base_class(c, _("Citation"))
43 {}
44
45
46 void FormCitation::apply()
47 {
48         controller().params().setCmdName("cite");
49         controller().params().setContents(getStringFromVector(citekeys));
50
51         string const after  = fl_get_input(dialog_->input_after);
52         controller().params().setOptions(after);
53 }
54
55
56 void FormCitation::hide()
57 {
58         citekeys.clear();
59         bibkeys.clear();
60
61         FormBase::hide();
62 }
63
64
65 void FormCitation::build()
66 {
67         dialog_.reset(build_citation());
68
69         fl_set_input_return(dialog_->input_after,  FL_RETURN_CHANGED);
70         fl_set_input_return(dialog_->input_before, FL_RETURN_CHANGED);
71
72         fl_set_button(dialog_->button_search_case, 0);
73         fl_set_button(dialog_->button_search_type, 0);
74
75         // Manage the ok, apply, restore and cancel/close buttons
76         bc().setOK(dialog_->button_ok);
77         bc().setApply(dialog_->button_apply);
78         bc().setCancel(dialog_->button_cancel);
79         bc().setRestore(dialog_->button_restore);
80
81         bc().addReadOnly(dialog_->button_add);
82         bc().addReadOnly(dialog_->button_del);
83         bc().addReadOnly(dialog_->button_up);
84         bc().addReadOnly(dialog_->button_down);
85         bc().addReadOnly(dialog_->choice_style);
86         bc().addReadOnly(dialog_->input_before);
87         bc().addReadOnly(dialog_->input_after);
88
89 }
90
91
92 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
93 {
94         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
95
96         biblio::InfoMap const & theMap = controller().bibkeysInfo();
97
98         if (ob == dialog_->browser_bib) {
99                 fl_deselect_browser(dialog_->browser_cite);
100
101                 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
102                 if (sel < 1 || sel > bibkeys.size())
103                         return ButtonPolicy::SMI_NOOP;
104
105                 // Put into browser_info the additional info associated with
106                 // the selected browser_bib key
107                 fl_clear_browser(dialog_->browser_info);
108
109                 string const tmp = formatted(biblio::getInfo(theMap,
110                                                              bibkeys[sel-1]),
111                                               dialog_->browser_info->w-10 );
112                 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
113
114                 // Highlight the selected browser_bib key in browser_cite if
115                 // present
116                 vector<string>::const_iterator cit =
117                         find(citekeys.begin(), citekeys.end(), bibkeys[sel-1]);
118
119                 if (cit != citekeys.end()) {
120                         int const n = int(cit - citekeys.begin());
121                         fl_select_browser_line(dialog_->browser_cite, n+1);
122                         fl_set_browser_topline(dialog_->browser_cite, n+1);
123                 }
124
125                 if (!controller().isReadonly()) {
126                         if (cit != citekeys.end()) {
127                                 setBibButtons(OFF);
128                                 setCiteButtons(ON);
129                         } else {
130                                 setBibButtons(ON);
131                                 setCiteButtons(OFF);
132                         }
133                 }
134
135         } else if (ob == dialog_->browser_cite) {
136                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
137                 if (sel < 1 || sel > citekeys.size())
138                         return ButtonPolicy::SMI_NOOP;
139
140                 if (!controller().isReadonly()) {
141                         setBibButtons(OFF);
142                         setCiteButtons(ON);
143                 }
144
145                 // Highlight the selected browser_cite key in browser_bib
146                 vector<string>::const_iterator cit =
147                         find(bibkeys.begin(), bibkeys.end(), citekeys[sel-1]);
148
149                 if (cit != bibkeys.end()) {
150                         int const n = int(cit - bibkeys.begin());
151                         fl_select_browser_line(dialog_->browser_bib, n+1);
152                         fl_set_browser_topline(dialog_->browser_bib, n+1);
153
154                         // Put into browser_info the additional info associated
155                         // with the selected browser_cite key
156                         fl_clear_browser(dialog_->browser_info);
157                         string const tmp =
158                                 formatted(biblio::getInfo(theMap,
159                                                           bibkeys[sel-1]),
160                                           dialog_->browser_info->w-10);
161                         fl_add_browser_line(dialog_->browser_info, tmp.c_str());
162                 }
163
164         } else if (ob == dialog_->button_add) {
165                 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
166                 if (sel < 1 || sel > bibkeys.size())
167                         return ButtonPolicy::SMI_NOOP;
168
169                 // Add the selected browser_bib key to browser_cite
170                 fl_addto_browser(dialog_->browser_cite,
171                                   bibkeys[sel-1].c_str());
172                 citekeys.push_back(bibkeys[sel-1]);
173
174                 int const n = int(citekeys.size());
175                 fl_select_browser_line(dialog_->browser_cite, n);
176
177                 setBibButtons(OFF);
178                 setCiteButtons(ON);
179                 activate = ButtonPolicy::SMI_VALID;
180
181         } else if (ob == dialog_->button_del) {
182                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
183                 if (sel < 1 || sel > citekeys.size())
184                         return ButtonPolicy::SMI_NOOP;
185
186                 // Remove the selected key from browser_cite
187                 fl_delete_browser_line(dialog_->browser_cite, sel) ;
188                 citekeys.erase(citekeys.begin() + sel-1);
189
190                 setBibButtons(ON);
191                 setCiteButtons(OFF);
192                 activate = ButtonPolicy::SMI_VALID;
193
194         } else if (ob == dialog_->button_up) {
195                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
196                 if (sel < 2 || sel > citekeys.size())
197                         return ButtonPolicy::SMI_NOOP;
198
199                 // Move the selected key up one line
200                 vector<string>::iterator it = citekeys.begin() + sel-1;
201                 string const tmp = *it;
202
203                 fl_delete_browser_line(dialog_->browser_cite, sel);
204                 citekeys.erase(it);
205
206                 fl_insert_browser_line(dialog_->browser_cite, sel-1, tmp.c_str());
207                 fl_select_browser_line(dialog_->browser_cite, sel-1);
208                 citekeys.insert(it-1, tmp);
209                 setCiteButtons(ON);
210                 activate = ButtonPolicy::SMI_VALID;
211
212         } else if (ob == dialog_->button_down) {
213                 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
214                 if (sel < 1 || sel > citekeys.size()-1)
215                         return ButtonPolicy::SMI_NOOP;
216
217                 // Move the selected key down one line
218                 vector<string>::iterator it = citekeys.begin() + sel-1;
219                 string const tmp = *it;
220
221                 fl_delete_browser_line(dialog_->browser_cite, sel);
222                 citekeys.erase(it);
223
224                 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
225                 fl_select_browser_line(dialog_->browser_cite, sel+1);
226                 citekeys.insert(it+1, tmp);
227                 setCiteButtons(ON);
228                 activate = ButtonPolicy::SMI_VALID;
229
230         } else if (ob == dialog_->button_previous ||
231                    ob == dialog_->button_next) {
232
233                 string const str = fl_get_input(dialog_->input_search);
234
235                 biblio::Direction const dir =
236                         (ob == dialog_->button_previous) ?
237                         biblio::BACKWARD : biblio::FORWARD;
238
239                 biblio::Search const type =
240                         fl_get_button(dialog_->button_search_type) ?
241                         biblio::REGEX : biblio::SIMPLE;
242
243                 vector<string>::const_iterator start = bibkeys.begin();
244                 int const sel = fl_get_browser(dialog_->browser_bib);
245                 if (sel >= 1 && sel <= int(bibkeys.size()))
246                         start += sel-1;
247
248                 // Find the NEXT instance...
249                 if (dir == biblio::FORWARD)
250                         start += 1;
251                 else
252                         start -= 1;
253
254                 bool const caseSensitive =
255                         fl_get_button(dialog_->button_search_case);
256
257                 vector<string>::const_iterator const cit =
258                         biblio::searchKeys(theMap, bibkeys, str,
259                                            start, type, dir, caseSensitive);
260
261                 if (cit == bibkeys.end())
262                         return ButtonPolicy::SMI_NOOP;
263
264                 int const found = int(cit - bibkeys.begin()) + 1;
265                 if (found == sel)
266                         return ButtonPolicy::SMI_NOOP;
267
268                 // Update the display
269                 int const top = max(found-5, 1);
270                 fl_set_browser_topline(dialog_->browser_bib, top);
271                 fl_select_browser_line(dialog_->browser_bib, found);
272                 input(dialog_->browser_bib, 0);
273
274         } else if (ob == dialog_->choice_style ||
275                    ob == dialog_->input_before ||
276                    ob == dialog_->input_after) {
277                 activate = ButtonPolicy::SMI_VALID;
278         }
279
280         return activate;
281 }
282
283
284 void FormCitation::update()
285 {
286         // Make the list of all available bibliography keys
287         bibkeys = biblio::getKeys(controller().bibkeysInfo());
288         updateBrowser(dialog_->browser_bib, bibkeys);
289
290         // Ditto for the keys cited in this inset
291         citekeys = getVectorFromString(controller().params().getContents());
292         updateBrowser(dialog_->browser_cite, citekeys);
293
294         // No keys have been selected yet, so...
295         fl_clear_browser(dialog_->browser_info);
296         setBibButtons(OFF);
297         setCiteButtons(OFF);
298
299         int noKeys = int(max(bibkeys.size(), citekeys.size()));
300
301         // Place bounds, so that 4 <= noKeys <= 10
302         noKeys = max(4, min(10, noKeys));
303
304         // Re-size the form to accommodate the new browser size
305         int const size = 20 * noKeys;
306         bool const bibPresent = (bibkeys.size() > 0);
307         setSize(size, bibPresent);
308
309         fl_set_input(dialog_->input_after,
310                      controller().params().getOptions().c_str());
311 }
312
313
314 void FormCitation::updateBrowser(FL_OBJECT * browser,
315                                  vector<string> const & keys) const
316 {
317         fl_clear_browser(browser);
318
319         for (vector<string>::const_iterator it = keys.begin();
320              it < keys.end(); ++it) {
321                 string key = frontStrip(strip(*it));
322                 if (!key.empty())
323                         fl_add_browser_line(browser, key.c_str());
324         }
325 }
326
327
328 void FormCitation::setBibButtons(State status) const
329 {
330         setEnabled(dialog_->button_add, (status == ON));
331 }
332
333
334 void FormCitation::setCiteButtons(State status) const
335 {
336         int const sel     = fl_get_browser(dialog_->browser_cite);
337         int const maxline = fl_get_browser_maxline(dialog_->browser_cite);
338         bool const activate      = (status == ON);
339         bool const activate_up   = (activate && sel != 1);
340         bool const activate_down = (activate && sel != maxline);
341
342         setEnabled(dialog_->button_del,  activate);
343         setEnabled(dialog_->button_up,   activate_up);
344         setEnabled(dialog_->button_down, activate_down);
345 }
346
347
348 void FormCitation::setSize(int hbrsr, bool bibPresent) const
349 {
350         bool const natbib = false; // will eventually be input
351         hbrsr = max(hbrsr, 175); // limit max size of cite/bib brsrs
352
353         // dh1, dh2, dh3 are the vertical separation between elements.
354         // These can be specified because the browser height is fixed
355         // so they are not changed by dynamic resizing
356         static int const dh1 = 30; // top of form to top of cite/bib brsrs;
357                                    // bottom of cite/bib brsrs to top of info;
358                                    // bottom of info to top search frame;
359                                    // bottom of search frame to top next elemnt;
360                                    // bottom of style to top input_before;
361                                    // bottom of text to top ok/cancel buttons.
362         static int const dh2 = 10; // bottom of input_before to top input_after;
363                                    // bottom of ok/cancel buttons to bottom form
364         static int const dh3 = 5;  // spacing between add/delete/... buttons.
365
366         int const wbrsr  = dialog_->browser_cite->w;
367         static int const hinfo  = dialog_->browser_info->h;
368         static int const hframe = dialog_->frame_search->h;
369         static int const hstyle = dialog_->choice_style->h;
370         static int const htext  = dialog_->input_after->h;
371         static int const hok    = dialog_->button_ok->h;
372
373         int hform = dh1 + hbrsr + dh1 + hframe + dh1;
374         if (bibPresent) hform += hinfo + dh1;
375         if (natbib) hform += hstyle + dh1 + htext + dh2;
376         hform += htext + dh1 + hok + dh2;
377
378         if (hform != minh_) {
379                 minh_ = hform;
380                 fl_set_form_size(dialog_->form, minw_, minh_);
381         } else
382                 return;
383
384         int x = 0;
385         int y = 0;
386         fl_set_object_geometry(dialog_->box, x, y, minw_, minh_);
387
388         x = dialog_->browser_cite->x;
389         y += dh1; 
390         fl_set_object_geometry(dialog_->browser_cite, x, y, wbrsr, hbrsr);
391         x = dialog_->browser_bib->x;
392         fl_set_object_geometry(dialog_->browser_bib,  x, y, wbrsr, hbrsr);
393
394         x = dialog_->button_add->x;
395         fl_set_object_position(dialog_->button_add,  x, y);
396         y += dh3 + dialog_->button_add->h;
397         fl_set_object_position(dialog_->button_del,  x, y);
398         y += dh3 + dialog_->button_del->h;
399         fl_set_object_position(dialog_->button_up,   x, y);
400         y += dh3 + dialog_->button_up->h;
401         fl_set_object_position(dialog_->button_down, x, y);
402
403         y = dh1 + hbrsr + dh1; // in position for next element
404
405         if (bibPresent) {
406                 x = dialog_->browser_info->x;
407                 fl_set_object_position(dialog_->browser_info, x, y);
408                 fl_show_object(dialog_->browser_info);
409                 y += hinfo + dh1;
410         } else
411                 fl_hide_object(dialog_->browser_info);
412
413         x = dialog_->frame_search->x;
414         // ??? The frame height seems to be reduced. Use geometry to enforce it.
415         fl_set_object_geometry(dialog_->frame_search, x, y,
416                                dialog_->frame_search->w, hframe);
417         //fl_set_object_position(dialog_->frame_search, x, y);
418
419         x = dialog_->input_search->x;
420         y += 15;
421         fl_set_object_position(dialog_->input_search, x, y);
422
423         x = dialog_->button_previous->x;
424         y += dialog_->input_search->h + 5;
425         fl_set_object_position(dialog_->button_previous, x, y);
426
427         x = dialog_->button_next->x;
428         y += dialog_->button_previous->h + 5;
429         fl_set_object_position(dialog_->button_next, x, y);
430
431         x = dialog_->button_search_type->x;
432         y = dialog_->button_previous->y;
433         fl_set_object_position(dialog_->button_search_type, x, y);
434
435         x = dialog_->button_search_case->x;
436         y = dialog_->button_next->y;
437         fl_set_object_position(dialog_->button_search_case, x, y);
438
439         y = dialog_->frame_search->y + hframe + dh1;
440         
441         if (natbib) {
442                 x = dialog_->choice_style->x;
443                 fl_set_object_position(dialog_->choice_style, x, y);
444                 fl_show_object(dialog_->choice_style);
445                 x = dialog_->input_before->x;
446                 y += hstyle + dh1;
447                 fl_set_object_position(dialog_->input_before, x, y);
448                 fl_show_object(dialog_->input_before);
449                 y += htext + dh2;
450         } else {
451                 fl_hide_object(dialog_->choice_style);
452                 fl_hide_object(dialog_->input_before);
453         }
454
455         x = dialog_->input_after->x;
456         fl_set_object_position(dialog_->input_after, x, y);
457
458         y += htext + dh1;
459         x = dialog_->button_restore->x;
460         fl_set_object_position(dialog_->button_restore, x, y);
461         x = dialog_->button_ok->x;
462         fl_set_object_position(dialog_->button_ok, x, y);
463         x = dialog_->button_apply->x;
464         fl_set_object_position(dialog_->button_apply, x, y);
465         x = dialog_->button_cancel->x;
466         fl_set_object_position(dialog_->button_cancel, x, y);
467 }