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;
43 static string const CONF_ENTRY_NAME("FormRef_name");
46 static string const GOTO_REF_LABEL(N_("Goto reference"));
47 static string const GOTO_BACK_LABEL(N_("Go back"));
49 FormRef::FormRef(LyXView * lv, Dialogs * d)
50 : lv_(lv), d_(d), inset_(0), u_(0), h_(0), ih_(0), dialog_(0)
52 // let the dialog be shown
53 // These are permanent connections so we won't bother
54 // storing a copy because we won't be disconnecting.
55 d->showRef.connect(slot(this, &FormRef::showInset));
56 d->createRef.connect(slot(this, &FormRef::createInset));
65 void FormRef::showInset( InsetCommand * const inset )
67 if( dialog_!=0 || inset == 0 ) return;
70 ih_ = inset_->hideDialog.connect(slot(this, &FormRef::hide));
74 params = inset->params();
79 void FormRef::createInset( string const & arg )
81 if( dialog_!=0 ) return;
85 params.setFromString( arg );
87 refs = lv_->buffer()->getLabelList();
89 if (refs.empty()) showStageError(_("*** No labels found in document ***"));
90 else showStageSelect();
93 void FormRef::showStageError(string const & mess)
97 using namespace Gtk::Box_Helpers;
99 Gtk::Alignment * alig = manage( new Gtk::Alignment(0.5, 0.5, 0, 0) );
100 Gtk::Box * box = manage( new Gtk::HBox() );
102 b_cancel = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_CANCEL) ) );
106 box->children().push_back(Element(*manage( new Gtk::Label(mess) ), false, false));
107 box->children().push_back(Element(*manage(new Gtk::VSeparator()), false, false));
108 box->children().push_back(Element(*b_cancel, false, false));
112 // packing dialog to main window
114 mainAppWin->add_action(*dialog_, _(" Reference "));
117 gtk_widget_grab_focus (GTK_WIDGET(b_cancel->gtkobj()));
119 // connecting signals
120 b_cancel->clicked.connect(slot(mainAppWin, &GLyxAppWin::remove_action));
121 dialog_->destroy.connect(slot(this, &FormRef::free));
123 u_ = d_->updateBufferDependent.connect(slot(this, &FormRef::updateSlot));
124 h_ = d_->hideBufferDependent.connect(slot(this, &FormRef::hide));
129 void FormRef::showStageSelect()
133 using namespace Gtk::Box_Helpers;
135 Gtk::Box * mbox = manage( new Gtk::HBox() );
136 Gtk::ButtonBox * bbox = manage( new Gtk::VButtonBox() );
137 Gtk::Alignment * alig = manage( new Gtk::Alignment(0.5, 0.5, 0, 0) );
138 Gtk::ScrolledWindow * sw = manage( new Gtk::ScrolledWindow() );
140 // constructing CList
141 vector<string> colnames;
142 colnames.push_back("INVISIBLE");
143 list_ = manage( new Gtk::CList(colnames) );
147 vector<string>::const_iterator end = refs.end();
148 for (vector<string>::const_iterator it = refs.begin(); it != end; ++it)
152 list_->rows().push_back(r);
154 list_->rows()[0].select(); // there is always at least one item. otherwise we go to stateError
156 b_ok = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_OK) ) );
157 b_cancel = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_CANCEL) ) );
160 list_->set_selection_mode(GTK_SELECTION_BROWSE); // this ensures that we have always item selected
161 list_->column_titles_hide();
163 sw->set_policy(GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
166 mbox->set_spacing(2);
167 bbox->set_spacing(4);
172 bbox->children().push_back(Element(*b_ok, false, false));
173 bbox->children().push_back(Element(*b_cancel, false, false));
177 mbox->children().push_back(Element(*sw, true, true));
178 mbox->children().push_back(Element(*manage(new Gtk::VSeparator()), false, false));
179 mbox->children().push_back(Element(*alig, false, false));
181 // packing dialog to main window
183 mainAppWin->add_action(*dialog_, _(" Reference: Select reference "), true);
186 GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_ok->gtkobj()), GTK_CAN_DEFAULT);
187 GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_cancel->gtkobj()), GTK_CAN_DEFAULT);
188 GTK_WIDGET_SET_FLAGS (GTK_WIDGET(list_->gtkobj()), GTK_CAN_DEFAULT);
189 gtk_widget_grab_focus (GTK_WIDGET(list_->gtkobj()));
190 gtk_widget_grab_default (GTK_WIDGET(b_ok->gtkobj()));
192 // connecting signals
193 b_ok->clicked.connect(slot(this, &FormRef::moveFromSelectToAction));
194 b_cancel->clicked.connect(slot(mainAppWin, &GLyxAppWin::remove_action));
196 dialog_->destroy.connect(slot(this, &FormRef::free));
198 u_ = d_->updateBufferDependent.connect(slot(this, &FormRef::updateSlot));
199 h_ = d_->hideBufferDependent.connect(slot(this, &FormRef::hide));
203 void FormRef::moveFromSelectToAction()
205 params.setContents( list_->selection()[0][0].get_text() );
207 // moves to stage "search"
208 mainAppWin->remove_action();
212 void FormRef::showStageAction()
216 using namespace Gtk::Box_Helpers;
218 Gtk::Table * table = manage( new Gtk::Table(2, 2, FALSE) );
219 Gtk::Box * mbox = manage( new Gtk::HBox() );
220 Gtk::ButtonBox * bbox = manage( new Gtk::HButtonBox() );
222 b_goto = manage(new Gnome::PixButton(GOTO_REF_LABEL, GNOME_STOCK_PIXMAP_JUMP_TO));
225 name_ = manage( new Gnome::Entry() );
227 choice_ = manage( new Gtk::OptionMenu() );
229 b_ok = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_OK) ) );
230 b_cancel = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_CANCEL) ) );
233 table->set_row_spacings(4);
234 table->set_col_spacings(4);
236 mbox->set_spacing(4);
238 bbox->set_spacing(4);
239 bbox->set_layout(GTK_BUTTONBOX_SPREAD);
242 name_->set_history_id(CONF_ENTRY_NAME);
243 name_->set_max_saved(10);
244 name_->load_history();
245 name_->set_use_arrows_always(true);
246 if( lv_->buffer()->isLatex() ) name_->set_sensitive(false); // Name is irrelevant to LaTeX documents
249 Gtk::Menu * menu = manage( new Gtk::Menu() );
252 e = manage( new Gtk::MenuItem(_("Ref")) );
253 e->activate.connect(bind<Type>(slot(this, &FormRef::changeType), REF));
257 e = manage( new Gtk::MenuItem(_("Page")) );
258 e->activate.connect(bind<Type>(slot(this, &FormRef::changeType), PAGEREF));
262 e = manage( new Gtk::MenuItem(_("TextRef")) );
263 e->activate.connect(bind<Type>(slot(this, &FormRef::changeType), VREF));
267 e = manage( new Gtk::MenuItem(_("TextPage")) );
268 e->activate.connect(bind<Type>(slot(this, &FormRef::changeType), VPAGEREF));
272 e = manage( new Gtk::MenuItem(_("PrettyRef")) );
273 e->activate.connect(bind<Type>(slot(this, &FormRef::changeType), PRETTYREF));
277 choice_-> set_menu ( *menu );
279 switch ( getType() ) {
280 case REF: { choice_-> set_history(0); break; }
281 case PAGEREF: { choice_-> set_history(1); break; }
282 case VREF: { choice_-> set_history(2); break; }
283 case VPAGEREF: { choice_-> set_history(3); break; }
284 case PRETTYREF: { choice_-> set_history(4); break; }
287 changeType( getType() );
289 // filling widgets with data
290 name_->get_entry()->set_text(params.getOptions());
294 bbox->children().push_back(Element(*b_goto, false, false));
295 bbox->children().push_back(Element(*b_ok, false, false));
296 bbox->children().push_back(Element(*b_cancel, false, false));
298 table->attach( *manage( new Gtk::Label(_("Type:")) ), 0, 1, 0, 1, 0, 0 );
299 table->attach( *manage( new Gtk::Label(_("Name:")) ), 0, 1, 1, 2, 0, 0 );
300 table->attach( *choice_, 1, 2, 0, 1 );
301 table->attach( *name_, 1, 2, 1, 2 );
303 mbox->children().push_back(Element(*table, true, true));
304 mbox->children().push_back(Element(*manage( new Gtk::VSeparator() ), false, false ));
305 mbox->children().push_back(Element(*bbox, false, false));
307 // packing dialog to main window
309 mainAppWin->add_action(*dialog_, string(_(" Reference: ")) + params.getContents() + string(" "));
312 GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_cancel->gtkobj()), GTK_CAN_DEFAULT);
313 GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_ok->gtkobj()), GTK_CAN_DEFAULT);
314 GTK_WIDGET_SET_FLAGS (GTK_WIDGET(b_goto->gtkobj()), GTK_CAN_DEFAULT);
315 gtk_widget_grab_default (GTK_WIDGET(b_ok->gtkobj()));
316 gtk_widget_grab_focus (GTK_WIDGET(choice_->gtkobj()));
318 // connecting signals
319 b_cancel->clicked.connect(slot(mainAppWin, &GLyxAppWin::remove_action));
320 b_ok->clicked.connect(slot(this, &FormRef::apply));
321 b_goto->clicked.connect(slot(this, &FormRef::gotoRef));
323 dialog_->destroy.connect(slot(this, &FormRef::free));
325 u_ = d_->updateBufferDependent.connect(slot(this, &FormRef::updateSlot));
326 h_ = d_->hideBufferDependent.connect(slot(this, &FormRef::hide));
330 void FormRef::updateSlot(bool buffchanged)
332 if (buffchanged) hide();
337 if (dialog_!=0) mainAppWin->remove_action();
352 void FormRef::gotoRef()
357 lv_->getLyXFunc()-> Dispatch(LFUN_REF_GOTO, params.getContents());
360 b_goto->set_text(GOTO_BACK_LABEL);
365 lv_->getLyXFunc()->Dispatch(LFUN_BOOKMARK_GOTO, "0");
368 b_goto->set_text(GOTO_REF_LABEL);
374 void FormRef::apply()
376 if ( lv_->buffer()->isReadonly() )
379 params.setCmdName(getName(reftype_));
380 params.setOptions(name_->get_entry()->get_text());
384 // Only update if contents have changed
385 if (params != inset_->params())
387 inset_->setParams(params);
388 lv_->view()->updateInset(inset_, true);
393 lv_->getLyXFunc()->Dispatch(LFUN_REF_INSERT,
394 params.getAsString());
395 lv_->getLyXFunc()->Dispatch(LFUN_BOOKMARK_SAVE, "0");
398 // save configuration
399 name_->save_history();
405 void FormRef::changeType(Type t)
410 FormRef::Type FormRef::getType() const
414 if( params.getCmdName() == "ref" )
417 else if( params.getCmdName() == "pageref" )
420 else if( params.getCmdName() == "vref" )
423 else if( params.getCmdName() == "vpageref" )
433 string FormRef::getName( Type type ) const