1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000 The LyX Team.
8 * ======================================================
14 #pragma implementation
25 #include <gtk--/box.h>
26 #include <gtk--/buttonbox.h>
27 #include <gtk--/alignment.h>
28 #include <gtk--/separator.h>
29 #include <gtk--/label.h>
30 #include <gtk--/scrolledwindow.h>
31 #include <gtk--/entry.h>
32 #include <gtk--/table.h>
34 #include <gtk--/menu.h>
35 #include <gtk--/menuitem.h>
37 // temporary solution for LyXView
39 extern GLyxAppWin * mainAppWin;
44 string const CONF_ENTRY_NAME("FormRef_name");
47 string const GOTO_REF_LABEL(N_("Goto reference"));
48 string const GOTO_BACK_LABEL(N_("Go back"));
53 FormRef::FormRef(LyXView * lv, Dialogs * d)
54 : lv_(lv), d_(d), inset_(0), u_(0), h_(0), ih_(0), dialog_(0)
56 // let the dialog be shown
57 // These are permanent connections so we won't bother
58 // storing a copy because we won't be disconnecting.
59 d->showRef.connect(slot(this, &FormRef::showInset));
60 d->createRef.connect(slot(this, &FormRef::createInset));
69 void FormRef::showInset( InsetCommand * const inset )
71 if( dialog_!=0 || inset == 0 ) return;
74 ih_ = inset_->hideDialog.connect(slot(this, &FormRef::hide));
78 params = inset->params();
83 void FormRef::createInset( string const & arg )
85 if( dialog_!=0 ) return;
89 params.setFromString( arg );
91 refs = lv_->buffer()->getLabelList();
93 if (refs.empty()) showStageError(_("*** No labels found in document ***"));
94 else showStageSelect();
97 void FormRef::showStageError(string const & mess)
101 using namespace Gtk::Box_Helpers;
103 Gtk::Alignment * alig = manage( new Gtk::Alignment(0.5, 0.5, 0, 0) );
104 Gtk::Box * box = manage( new Gtk::HBox() );
106 b_cancel = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_CANCEL) ) );
110 box->children().push_back(Element(*manage( new Gtk::Label(mess) ), false, false));
111 box->children().push_back(Element(*manage(new Gtk::VSeparator()), false, false));
112 box->children().push_back(Element(*b_cancel, false, false));
116 // packing dialog to main window
118 mainAppWin->add_action(*dialog_, _(" Reference "));
121 gtk_widget_grab_focus (GTK_WIDGET(b_cancel->gtkobj()));
123 // connecting signals
124 b_cancel->clicked.connect(slot(mainAppWin, &GLyxAppWin::remove_action));
125 dialog_->destroy.connect(slot(this, &FormRef::free));
127 u_ = d_->updateBufferDependent.connect(slot(this, &FormRef::updateSlot));
128 h_ = d_->hideBufferDependent.connect(slot(this, &FormRef::hide));
133 void FormRef::showStageSelect()
137 using namespace Gtk::Box_Helpers;
139 Gtk::Box * mbox = manage( new Gtk::HBox() );
140 Gtk::ButtonBox * bbox = manage( new Gtk::VButtonBox() );
141 Gtk::Alignment * alig = manage( new Gtk::Alignment(0.5, 0.5, 0, 0) );
142 Gtk::ScrolledWindow * sw = manage( new Gtk::ScrolledWindow() );
144 // constructing CList
145 vector<string> colnames;
146 colnames.push_back("INVISIBLE");
147 list_ = manage( new Gtk::CList(colnames) );
151 vector<string>::const_iterator end = refs.end();
152 for (vector<string>::const_iterator it = refs.begin(); it != end; ++it)
156 list_->rows().push_back(r);
158 list_->rows()[0].select(); // there is always at least one item. otherwise we go to stateError
160 b_ok = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_OK) ) );
161 b_cancel = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_CANCEL) ) );
164 list_->set_selection_mode(GTK_SELECTION_BROWSE); // this ensures that we have always item selected
165 list_->column_titles_hide();
167 sw->set_policy(GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
170 mbox->set_spacing(2);
171 bbox->set_spacing(4);
176 bbox->children().push_back(Element(*b_ok, false, false));
177 bbox->children().push_back(Element(*b_cancel, false, false));
181 mbox->children().push_back(Element(*sw, true, true));
182 mbox->children().push_back(Element(*manage(new Gtk::VSeparator()), false, false));
183 mbox->children().push_back(Element(*alig, false, false));
185 // packing dialog to main window
187 mainAppWin->add_action(*dialog_, _(" Reference: Select reference "), true);
190 GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_ok->gtkobj()), GTK_CAN_DEFAULT);
191 GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_cancel->gtkobj()), GTK_CAN_DEFAULT);
192 GTK_WIDGET_SET_FLAGS (GTK_WIDGET(list_->gtkobj()), GTK_CAN_DEFAULT);
193 gtk_widget_grab_focus (GTK_WIDGET(list_->gtkobj()));
194 gtk_widget_grab_default (GTK_WIDGET(b_ok->gtkobj()));
196 // connecting signals
197 b_ok->clicked.connect(slot(this, &FormRef::moveFromSelectToAction));
198 b_cancel->clicked.connect(slot(mainAppWin, &GLyxAppWin::remove_action));
200 dialog_->destroy.connect(slot(this, &FormRef::free));
202 u_ = d_->updateBufferDependent.connect(slot(this, &FormRef::updateSlot));
203 h_ = d_->hideBufferDependent.connect(slot(this, &FormRef::hide));
207 void FormRef::moveFromSelectToAction()
209 params.setContents( list_->selection()[0][0].get_text() );
211 // moves to stage "search"
212 mainAppWin->remove_action();
216 void FormRef::showStageAction()
220 using namespace Gtk::Box_Helpers;
222 Gtk::Table * table = manage( new Gtk::Table(2, 2, FALSE) );
223 Gtk::Box * mbox = manage( new Gtk::HBox() );
224 Gtk::ButtonBox * bbox = manage( new Gtk::HButtonBox() );
226 b_goto = manage(new Gnome::PixButton(GOTO_REF_LABEL, GNOME_STOCK_PIXMAP_JUMP_TO));
229 name_ = manage( new Gnome::Entry() );
231 choice_ = manage( new Gtk::OptionMenu() );
233 b_ok = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_OK) ) );
234 b_cancel = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_CANCEL) ) );
237 table->set_row_spacings(4);
238 table->set_col_spacings(4);
240 mbox->set_spacing(4);
242 bbox->set_spacing(4);
243 bbox->set_layout(GTK_BUTTONBOX_SPREAD);
246 name_->set_history_id(CONF_ENTRY_NAME);
247 name_->set_max_saved(10);
248 name_->load_history();
249 name_->set_use_arrows_always(true);
250 if( lv_->buffer()->isLatex() ) name_->set_sensitive(false); // Name is irrelevant to LaTeX documents
253 Gtk::Menu * menu = manage( new Gtk::Menu() );
256 e = manage( new Gtk::MenuItem(_("Ref")) );
257 e->activate.connect(bind<Type>(slot(this, &FormRef::changeType), REF));
261 e = manage( new Gtk::MenuItem(_("Page")) );
262 e->activate.connect(bind<Type>(slot(this, &FormRef::changeType), PAGEREF));
266 e = manage( new Gtk::MenuItem(_("TextRef")) );
267 e->activate.connect(bind<Type>(slot(this, &FormRef::changeType), VREF));
271 e = manage( new Gtk::MenuItem(_("TextPage")) );
272 e->activate.connect(bind<Type>(slot(this, &FormRef::changeType), VPAGEREF));
276 e = manage( new Gtk::MenuItem(_("PrettyRef")) );
277 e->activate.connect(bind<Type>(slot(this, &FormRef::changeType), PRETTYREF));
281 choice_-> set_menu ( *menu );
283 switch ( getType() ) {
284 case REF: { choice_-> set_history(0); break; }
285 case PAGEREF: { choice_-> set_history(1); break; }
286 case VREF: { choice_-> set_history(2); break; }
287 case VPAGEREF: { choice_-> set_history(3); break; }
288 case PRETTYREF: { choice_-> set_history(4); break; }
291 changeType( getType() );
293 // filling widgets with data
294 name_->get_entry()->set_text(params.getOptions());
298 bbox->children().push_back(Element(*b_goto, false, false));
299 bbox->children().push_back(Element(*b_ok, false, false));
300 bbox->children().push_back(Element(*b_cancel, false, false));
302 table->attach( *manage( new Gtk::Label(_("Type:")) ), 0, 1, 0, 1, 0, 0 );
303 table->attach( *manage( new Gtk::Label(_("Name:")) ), 0, 1, 1, 2, 0, 0 );
304 table->attach( *choice_, 1, 2, 0, 1 );
305 table->attach( *name_, 1, 2, 1, 2 );
307 mbox->children().push_back(Element(*table, true, true));
308 mbox->children().push_back(Element(*manage( new Gtk::VSeparator() ), false, false ));
309 mbox->children().push_back(Element(*bbox, false, false));
311 // packing dialog to main window
313 mainAppWin->add_action(*dialog_, string(_(" Reference: ")) + params.getContents() + string(" "));
316 GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_cancel->gtkobj()), GTK_CAN_DEFAULT);
317 GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_ok->gtkobj()), GTK_CAN_DEFAULT);
318 GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_goto->gtkobj()), GTK_CAN_DEFAULT);
319 gtk_widget_grab_default (GTK_WIDGET(b_ok->gtkobj()));
320 gtk_widget_grab_focus (GTK_WIDGET(choice_->gtkobj()));
322 // connecting signals
323 b_cancel->clicked.connect(slot(mainAppWin, &GLyxAppWin::remove_action));
324 b_ok->clicked.connect(slot(this, &FormRef::apply));
325 b_goto->clicked.connect(slot(this, &FormRef::gotoRef));
327 dialog_->destroy.connect(slot(this, &FormRef::free));
329 u_ = d_->updateBufferDependent.connect(slot(this, &FormRef::updateSlot));
330 h_ = d_->hideBufferDependent.connect(slot(this, &FormRef::hide));
334 void FormRef::updateSlot(bool buffchanged)
336 if (buffchanged) hide();
341 if (dialog_!=0) mainAppWin->remove_action();
356 void FormRef::gotoRef()
361 lv_->getLyXFunc()-> Dispatch(LFUN_REF_GOTO, params.getContents());
364 b_goto->set_text(GOTO_BACK_LABEL);
369 lv_->getLyXFunc()->Dispatch(LFUN_BOOKMARK_GOTO, "0");
372 b_goto->set_text(GOTO_REF_LABEL);
378 void FormRef::apply()
380 if ( lv_->buffer()->isReadonly() )
383 params.setCmdName(getName(reftype_));
384 params.setOptions(name_->get_entry()->get_text());
388 // Only update if contents have changed
389 if (params != inset_->params())
391 inset_->setParams(params);
392 lv_->view()->updateInset(inset_, true);
397 lv_->getLyXFunc()->Dispatch(LFUN_REF_INSERT,
398 params.getAsString());
399 lv_->getLyXFunc()->Dispatch(LFUN_BOOKMARK_SAVE, "0");
402 // save configuration
403 name_->save_history();
409 void FormRef::changeType(Type t)
414 FormRef::Type FormRef::getType() const
418 if( params.getCmdName() == "ref" )
421 else if( params.getCmdName() == "pageref" )
424 else if( params.getCmdName() == "vref" )
427 else if( params.getCmdName() == "vpageref" )
437 string FormRef::getName( Type type ) const