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