2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 2000 The LyX Team.
9 * ======================================================
11 * \author Angus Leeming <a.leeming@ic.ac.uk>
17 #pragma implementation
20 #include "FormCitationDialogImpl.h"
21 #include "FormCitation.h"
23 #include <qcombobox.h>
24 #include <qlineedit.h>
26 #include <qmultilineedit.h>
27 #include <qpushbutton.h>
31 #include "ControlCitation.h"
33 #include "support/lstrings.h"
35 #include "helper_funcs.h"
45 typedef Qt2CB<ControlCitation, Qt2DB<FormCitationDialogImpl> > base_class;
47 FormCitation::FormCitation(ControlCitation & c)
48 : base_class(c, _("Citation"))
52 void FormCitation::apply()
54 controller().params().setCmdName("cite");
55 controller().params().setContents(getStringFromVector(citekeys));
57 string const after = dialog_->textAfterED->text().latin1();
58 controller().params().setOptions(after);
62 void FormCitation::hide()
71 void FormCitation::build()
73 // PENDING(kalle) Parent?
74 dialog_.reset( new FormCitationDialogImpl( this ));
76 dialog_->searchTypePB->setOn( false );
77 dialog_->searchTypePB->setText( _( "Simple" ) );
79 // Manage the ok, apply, restore and cancel/close buttons
80 bc().setOK(dialog_->okPB);
81 bc().setApply(dialog_->applyPB);
82 bc().setCancel(dialog_->cancelPB);
83 bc().setUndoAll(dialog_->restorePB);
85 bc().addReadOnly(dialog_->addPB);
86 bc().addReadOnly(dialog_->delPB);
87 bc().addReadOnly(dialog_->upPB);
88 bc().addReadOnly(dialog_->downPB);
89 bc().addReadOnly(dialog_->citationStyleCO);
90 bc().addReadOnly(dialog_->textBeforeED);
91 bc().addReadOnly(dialog_->textAfterED);
97 ButtonPolicy::SMInput FormCitation::input(FL_OBJECT * ob, long)
99 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_NOOP;
101 biblio::InfoMap const & theMap = controller().bibkeysInfo();
103 if (ob == dialog_->browser_bib) {
104 fl_deselect_browser(dialog_->browser_cite);
106 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
107 if (sel < 1 || sel > bibkeys.size())
108 return ButtonPolicy::SMI_NOOP;
110 // Put into browser_info the additional info associated with
111 // the selected browser_bib key
112 fl_clear_browser(dialog_->browser_info);
114 string const tmp = formatted(biblio::getInfo(theMap,
116 dialog_->browser_info->w-10 );
117 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
119 // Highlight the selected browser_bib key in browser_cite if
121 vector<string>::const_iterator cit =
122 find(citekeys.begin(), citekeys.end(), bibkeys[sel-1]);
124 if (cit != citekeys.end()) {
125 int const n = int(cit - citekeys.begin());
126 fl_select_browser_line(dialog_->browser_cite, n+1);
127 fl_set_browser_topline(dialog_->browser_cite, n+1);
130 if (!controller().isReadonly()) {
131 if (cit != citekeys.end()) {
140 } else if (ob == dialog_->browser_cite) {
141 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
142 if (sel < 1 || sel > citekeys.size())
143 return ButtonPolicy::SMI_NOOP;
145 if (!controller().isReadonly()) {
150 // Highlight the selected browser_cite key in browser_bib
151 vector<string>::const_iterator cit =
152 find(bibkeys.begin(), bibkeys.end(), citekeys[sel-1]);
154 if (cit != bibkeys.end()) {
155 int const n = int(cit - bibkeys.begin());
156 fl_select_browser_line(dialog_->browser_bib, n+1);
157 fl_set_browser_topline(dialog_->browser_bib, n+1);
159 // Put into browser_info the additional info associated
160 // with the selected browser_cite key
161 fl_clear_browser(dialog_->browser_info);
163 formatted(biblio::getInfo(theMap,
165 dialog_->browser_info->w-10);
166 fl_add_browser_line(dialog_->browser_info, tmp.c_str());
169 } else if (ob == dialog_->button_add) {
170 unsigned int const sel = fl_get_browser(dialog_->browser_bib);
171 if (sel < 1 || sel > bibkeys.size())
172 return ButtonPolicy::SMI_NOOP;
174 // Add the selected browser_bib key to browser_cite
175 fl_addto_browser(dialog_->browser_cite,
176 bibkeys[sel-1].c_str());
177 citekeys.push_back(bibkeys[sel-1]);
179 int const n = int(citekeys.size());
180 fl_select_browser_line(dialog_->browser_cite, n);
184 activate = ButtonPolicy::SMI_VALID;
186 } else if (ob == dialog_->button_del) {
187 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
188 if (sel < 1 || sel > citekeys.size())
189 return ButtonPolicy::SMI_NOOP;
191 // Remove the selected key from browser_cite
192 fl_delete_browser_line(dialog_->browser_cite, sel) ;
193 citekeys.erase(citekeys.begin() + sel-1);
197 activate = ButtonPolicy::SMI_VALID;
199 } else if (ob == dialog_->button_up) {
200 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
201 if (sel < 2 || sel > citekeys.size())
202 return ButtonPolicy::SMI_NOOP;
204 // Move the selected key up one line
205 vector<string>::iterator it = citekeys.begin() + sel-1;
206 string const tmp = *it;
208 fl_delete_browser_line(dialog_->browser_cite, sel);
211 fl_insert_browser_line(dialog_->browser_cite, sel-1, tmp.c_str());
212 fl_select_browser_line(dialog_->browser_cite, sel-1);
213 citekeys.insert(it-1, tmp);
215 activate = ButtonPolicy::SMI_VALID;
217 } else if (ob == dialog_->button_down) {
218 unsigned int const sel = fl_get_browser(dialog_->browser_cite);
219 if (sel < 1 || sel > citekeys.size()-1)
220 return ButtonPolicy::SMI_NOOP;
222 // Move the selected key down one line
223 vector<string>::iterator it = citekeys.begin() + sel-1;
224 string const tmp = *it;
226 fl_delete_browser_line(dialog_->browser_cite, sel);
229 fl_insert_browser_line(dialog_->browser_cite, sel+1, tmp.c_str());
230 fl_select_browser_line(dialog_->browser_cite, sel+1);
231 citekeys.insert(it+1, tmp);
233 activate = ButtonPolicy::SMI_VALID;
235 } else if (ob == dialog_->button_search_type) {
236 if (fl_get_button(dialog_->button_search_type))
237 fl_set_object_label(dialog_->button_search_type,
240 fl_set_object_label(dialog_->button_search_type,
242 return ButtonPolicy::SMI_NOOP;
244 } else if (ob == dialog_->button_previous ||
245 ob == dialog_->button_next) {
247 string const str = fl_get_input(dialog_->input_search);
249 biblio::Direction const dir =
250 (ob == dialog_->button_previous) ?
251 biblio::BACKWARD : biblio::FORWARD;
253 biblio::Search const type =
254 fl_get_button(dialog_->button_search_type) ?
255 biblio::REGEX : biblio::SIMPLE;
257 vector<string>::const_iterator start = bibkeys.begin();
258 int const sel = fl_get_browser(dialog_->browser_bib);
259 if (sel >= 1 && sel <= int(bibkeys.size()))
262 // Find the NEXT instance...
263 if (dir == biblio::FORWARD)
268 vector<string>::const_iterator const cit =
269 biblio::searchKeys(theMap, bibkeys, str,
272 if (cit == bibkeys.end())
273 return ButtonPolicy::SMI_NOOP;
275 int const found = int(cit - bibkeys.begin()) + 1;
277 return ButtonPolicy::SMI_NOOP;
279 // Update the display
280 int const top = max(found-5, 1);
281 fl_set_browser_topline(dialog_->browser_bib, top);
282 fl_select_browser_line(dialog_->browser_bib, found);
283 input(dialog_->browser_bib, 0);
285 } else if (ob == dialog_->choice_style ||
286 ob == dialog_->input_before ||
287 ob == dialog_->input_after) {
288 activate = ButtonPolicy::SMI_VALID;
296 void FormCitation::update()
298 // Make the list of all available bibliography keys
299 bibkeys = biblio::getKeys(controller().bibkeysInfo());
300 updateBrowser(dialog_->bibLB, bibkeys);
302 // Ditto for the keys cited in this inset
303 citekeys = getVectorFromString(controller().params().getContents());
304 updateBrowser(dialog_->citeLB, citekeys);
306 // No keys have been selected yet, so...
307 dialog_->infoML->clear();
311 int noKeys = int(max(bibkeys.size(), citekeys.size()));
313 // Place bounds, so that 4 <= noKeys <= 10
314 noKeys = max(4, min(10, noKeys));
316 dialog_->textAfterED->setText( controller().params().getOptions().c_str());
320 void FormCitation::updateBrowser( QListBox* browser,
321 vector<string> const & keys) const
325 for (vector<string>::const_iterator it = keys.begin();
326 it < keys.end(); ++it) {
327 string key = frontStrip(strip(*it));
328 browser->insertItem( key.c_str() );
333 void FormCitation::setBibButtons(State status) const
335 dialog_->addPB->setEnabled( (status == ON) );
339 void FormCitation::setCiteButtons(State status) const
341 int const sel = dialog_->citeLB->currentItem();
342 int const maxline = dialog_->citeLB->count()-1;
343 bool const activate = (status == ON);
344 bool const activate_up = (activate && sel != 0);
345 bool const activate_down = (activate && sel != maxline);
347 dialog_->delPB->setEnabled( activate );
348 dialog_->upPB->setEnabled( activate_up );
349 dialog_->downPB->setEnabled( activate_down );