2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 2000 The LyX Team.
9 * ======================================================
15 #pragma implementation
26 #include <gtk--/box.h>
27 #include <gtk--/buttonbox.h>
28 #include <gtk--/alignment.h>
29 #include <gtk--/separator.h>
30 #include <gtk--/label.h>
31 #include <gtk--/scrolledwindow.h>
32 #include <gtk--/entry.h>
33 #include <gtk--/table.h>
35 #include <gtk--/menu.h>
36 #include <gtk--/menuitem.h>
38 // temporary solution for LyXView
40 extern GLyxAppWin * mainAppWin;
45 static string const CONF_ENTRY_NAME("FormRef_name");
48 static string const GOTO_REF_LABEL(N_("Goto reference"));
49 static string const GOTO_BACK_LABEL(N_("Go back"));
51 FormRef::FormRef(LyXView * lv, Dialogs * d)
52 : lv_(lv), d_(d), inset_(0), u_(0), h_(0), ih_(0), dialog_(NULL)
54 // let the dialog be shown
55 // These are permanent connections so we won't bother
56 // storing a copy because we won't be disconnecting.
57 d->showRef.connect(slot(this, &FormRef::showInset));
58 d->createRef.connect(slot(this, &FormRef::createInset));
67 void FormRef::showInset( InsetCommand * const inset )
69 if( dialog_!=NULL || inset == 0 ) return;
72 ih_ = inset_->hide.connect(slot(this, &FormRef::hide));
76 params = inset->params();
81 void FormRef::createInset( string const & arg )
83 if( dialog_!=NULL ) return;
87 params.setFromString( arg );
89 refs = lv_->buffer()->getLabelList();
91 if (refs.empty()) showStageError(_("*** No labels found in document ***"));
92 else showStageSelect();
95 void FormRef::showStageError(string const & mess)
99 using namespace Gtk::Box_Helpers;
101 Gtk::Alignment * alig = manage( new Gtk::Alignment(0.5, 0.5, 0, 0) );
102 Gtk::Box * box = manage( new Gtk::HBox() );
104 b_cancel = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_CANCEL) ) );
108 box->children().push_back(Element(*manage( new Gtk::Label(mess) ), false, false));
109 box->children().push_back(Element(*manage(new Gtk::VSeparator()), false, false));
110 box->children().push_back(Element(*b_cancel, false, false));
114 // packing dialog to main window
116 mainAppWin->add_action(*dialog_, _(" Reference "));
119 gtk_widget_grab_focus (GTK_WIDGET(b_cancel->gtkobj()));
121 // connecting signals
122 b_cancel->clicked.connect(slot(mainAppWin, &GLyxAppWin::remove_action));
123 dialog_->destroy.connect(slot(this, &FormRef::free));
125 u_ = d_->updateBufferDependent.connect(slot(this, &FormRef::update));
126 h_ = d_->hideBufferDependent.connect(slot(this, &FormRef::hide));
131 void FormRef::showStageSelect()
135 using namespace Gtk::Box_Helpers;
137 Gtk::Box * mbox = manage( new Gtk::HBox() );
138 Gtk::ButtonBox * bbox = manage( new Gtk::VButtonBox() );
139 Gtk::Alignment * alig = manage( new Gtk::Alignment(0.5, 0.5, 0, 0) );
140 Gtk::ScrolledWindow * sw = manage( new Gtk::ScrolledWindow() );
142 // constructing CList
143 vector<string> colnames;
144 colnames.push_back("INVISIBLE");
145 list_ = manage( new Gtk::CList(colnames) );
149 vector<string>::const_iterator end = refs.end();
150 for (vector<string>::const_iterator it = refs.begin(); it != end; ++it)
154 list_->rows().push_back(r);
156 list_->rows()[0].select(); // there is always at least one item. otherwise we go to stateError
158 b_ok = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_OK) ) );
159 b_cancel = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_CANCEL) ) );
162 list_->set_selection_mode(GTK_SELECTION_BROWSE); // this ensures that we have always item selected
163 list_->column_titles_hide();
165 sw->set_policy(GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
168 mbox->set_spacing(2);
169 bbox->set_spacing(4);
174 bbox->children().push_back(Element(*b_ok, false, false));
175 bbox->children().push_back(Element(*b_cancel, false, false));
179 mbox->children().push_back(Element(*sw, true, true));
180 mbox->children().push_back(Element(*manage(new Gtk::VSeparator()), false, false));
181 mbox->children().push_back(Element(*alig, false, false));
183 // packing dialog to main window
185 mainAppWin->add_action(*dialog_, _(" Reference: Select reference "), true);
188 GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_ok->gtkobj()), GTK_CAN_DEFAULT);
189 GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_cancel->gtkobj()), GTK_CAN_DEFAULT);
190 GTK_WIDGET_SET_FLAGS (GTK_WIDGET(list_->gtkobj()), GTK_CAN_DEFAULT);
191 gtk_widget_grab_focus (GTK_WIDGET(list_->gtkobj()));
192 gtk_widget_grab_default (GTK_WIDGET(b_ok->gtkobj()));
194 // connecting signals
195 b_ok->clicked.connect(slot(this, &FormRef::moveFromSelectToAction));
196 b_cancel->clicked.connect(slot(mainAppWin, &GLyxAppWin::remove_action));
198 dialog_->destroy.connect(slot(this, &FormRef::free));
200 u_ = d_->updateBufferDependent.connect(slot(this, &FormRef::update));
201 h_ = d_->hideBufferDependent.connect(slot(this, &FormRef::hide));
205 void FormRef::moveFromSelectToAction()
207 params.setContents( list_->selection()[0][0].get_text() );
209 // moves to stage "search"
210 mainAppWin->remove_action();
214 void FormRef::showStageAction()
218 using namespace Gtk::Box_Helpers;
220 Gtk::Table * table = manage( new Gtk::Table(2, 2, FALSE) );
221 Gtk::Box * mbox = manage( new Gtk::HBox() );
222 Gtk::ButtonBox * bbox = manage( new Gtk::HButtonBox() );
224 b_goto = manage(new Gnome::PixButton(GOTO_REF_LABEL, GNOME_STOCK_PIXMAP_JUMP_TO));
227 name_ = manage( new Gnome::Entry() );
229 choice_ = manage( new Gtk::OptionMenu() );
231 b_ok = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_OK) ) );
232 b_cancel = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_CANCEL) ) );
235 table->set_row_spacings(4);
236 table->set_col_spacings(4);
238 mbox->set_spacing(4);
240 bbox->set_spacing(4);
241 bbox->set_layout(GTK_BUTTONBOX_SPREAD);
244 name_->set_history_id(CONF_ENTRY_NAME);
245 name_->set_max_saved(10);
246 name_->load_history();
247 name_->set_use_arrows_always(true);
248 if( lv_->buffer()->isLatex() ) name_->set_sensitive(false); // Name is irrelevant to LaTeX documents
251 Gtk::Menu * menu = manage( new Gtk::Menu() );
254 e = manage( new Gtk::MenuItem(_("Ref")) );
255 e->activate.connect(bind<Type>(slot(this, &FormRef::changeType), REF));
259 e = manage( new Gtk::MenuItem(_("Page")) );
260 e->activate.connect(bind<Type>(slot(this, &FormRef::changeType), PAGEREF));
264 e = manage( new Gtk::MenuItem(_("TextRef")) );
265 e->activate.connect(bind<Type>(slot(this, &FormRef::changeType), VREF));
269 e = manage( new Gtk::MenuItem(_("TextPage")) );
270 e->activate.connect(bind<Type>(slot(this, &FormRef::changeType), VPAGEREF));
274 e = manage( new Gtk::MenuItem(_("PrettyRef")) );
275 e->activate.connect(bind<Type>(slot(this, &FormRef::changeType), PRETTYREF));
279 choice_-> set_menu ( *menu );
281 switch ( getType() ) {
282 case REF: { choice_-> set_history(0); break; }
283 case PAGEREF: { choice_-> set_history(1); break; }
284 case VREF: { choice_-> set_history(2); break; }
285 case VPAGEREF: { choice_-> set_history(3); break; }
286 case PRETTYREF: { choice_-> set_history(4); break; }
289 changeType( getType() );
291 // filling widgets with data
292 name_->get_entry()->set_text(params.getOptions());
296 bbox->children().push_back(Element(*b_goto, false, false));
297 bbox->children().push_back(Element(*b_ok, false, false));
298 bbox->children().push_back(Element(*b_cancel, false, false));
300 table->attach( *manage( new Gtk::Label(_("Type:")) ), 0, 1, 0, 1, 0, 0 );
301 table->attach( *manage( new Gtk::Label(_("Name:")) ), 0, 1, 1, 2, 0, 0 );
302 table->attach( *choice_, 1, 2, 0, 1 );
303 table->attach( *name_, 1, 2, 1, 2 );
305 mbox->children().push_back(Element(*table, true, true));
306 mbox->children().push_back(Element(*manage( new Gtk::VSeparator() ), false, false ));
307 mbox->children().push_back(Element(*bbox, false, false));
309 // packing dialog to main window
311 mainAppWin->add_action(*dialog_, string(_(" Reference: ")) + params.getContents() + string(" "));
314 GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_cancel->gtkobj()), GTK_CAN_DEFAULT);
315 GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_ok->gtkobj()), GTK_CAN_DEFAULT);
316 GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_goto->gtkobj()), GTK_CAN_DEFAULT);
317 gtk_widget_grab_default (GTK_WIDGET(b_ok->gtkobj()));
318 gtk_widget_grab_focus (GTK_WIDGET(choice_->gtkobj()));
320 // connecting signals
321 b_cancel->clicked.connect(slot(mainAppWin, &GLyxAppWin::remove_action));
322 b_ok->clicked.connect(slot(this, &FormRef::apply));
323 b_goto->clicked.connect(slot(this, &FormRef::gotoRef));
325 dialog_->destroy.connect(slot(this, &FormRef::free));
327 u_ = d_->updateBufferDependent.connect(slot(this, &FormRef::update));
328 h_ = d_->hideBufferDependent.connect(slot(this, &FormRef::hide));
332 void FormRef::update(bool buffchanged)
334 if (buffchanged) hide();
339 if (dialog_!=NULL) mainAppWin->remove_action();
354 void FormRef::gotoRef()
359 lv_->getLyXFunc()-> Dispatch(LFUN_REF_GOTO, params.getContents());
362 b_goto->set_text(GOTO_BACK_LABEL);
367 lv_->getLyXFunc()->Dispatch(LFUN_REF_BACK);
370 b_goto->set_text(GOTO_REF_LABEL);
376 void FormRef::apply()
378 if ( lv_->buffer()->isReadonly() )
381 params.setCmdName(getName(reftype_));
382 params.setOptions(name_->get_entry()->get_text());
386 // Only update if contents have changed
387 if (params != inset_->params())
389 inset_->setParams(params);
390 lv_->view()->updateInset(inset_, true);
395 lv_->getLyXFunc()->Dispatch(LFUN_REF_INSERT,
396 params.getAsString());
399 // save configuration
400 name_->save_history();
406 void FormRef::changeType(Type t)
411 FormRef::Type FormRef::getType() const
415 if( params.getCmdName() == "ref" )
418 else if( params.getCmdName() == "pageref" )
421 else if( params.getCmdName() == "vref" )
424 else if( params.getCmdName() == "vpageref" )
434 string FormRef::getName( Type type ) const