1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000 The LyX Team.
8 * ======================================================
15 #pragma implementation
28 #include "diatoc_interface.h"
32 #include <gtk--/base.h>
33 #include <gtk--/button.h>
34 #include <gtk--/label.h>
35 #include <gtk--/scrolledwindow.h>
36 #include <gtk--/menu.h>
37 #include <gtk--/menuitem.h>
39 FormToc::FormToc(LyXView * lv, Dialogs * d)
40 : lv_(lv), d_(d), inset_(0), u_(0), h_(0), ih_(0), dialog_(0),
41 ignore_callback_(false)
43 // let the dialog be shown
44 // These are permanent connections so we won't bother
45 // storing a copy because we won't be disconnecting.
46 d->showTOC.connect(SigC::slot(this, &FormToc::showInset));
47 d->createTOC.connect(SigC::slot(this, &FormToc::createInset));
56 void FormToc::showInset( InsetCommand * const inset )
58 if ( dialog_!=0 || inset == 0 ) return;
61 ih_ = inset_->hideDialog.connect(SigC::slot(this, &FormToc::hide));
63 params = inset->params();
68 void FormToc::createInset( string const & arg )
70 if( dialog_!=0 ) return;
72 params.setFromString( arg );
79 Gtk::Button * b_refresh;
80 Gtk::Button * b_close;
81 Gtk::ScrolledWindow *scrolled_window;
85 GtkWidget * pd = create_DiaToc();
87 dialog_ = Gtk::wrap( GNOME_DIALOG(pd) );
88 choice_ = Gtk::wrap( GTK_OPTION_MENU( lookup_widget(pd, "choice") ) );
89 scrolled_window = Gtk::wrap( GTK_SCROLLED_WINDOW( lookup_widget(pd, "scrolledwindow") ) );
91 list_ = manage( new Gtk::List() );
92 scrolled_window->add_with_viewport(*list_);
97 choice_->get_menu()->items().clear();
99 e = manage( new Gtk::MenuItem(_("Table of Contents")) );
100 e->activate.connect(SigC::bind<Buffer::TocType>(SigC::slot(this, &FormToc::changeList), Buffer::TOC_TOC));
101 choice_->get_menu()->append( *e );
103 e = manage( new Gtk::MenuItem(_("List of Figures")) );
104 e->activate.connect(SigC::bind<Buffer::TocType>(SigC::slot(this, &FormToc::changeList), Buffer::TOC_LOF));
105 choice_->get_menu()->append( *e );
107 e = manage( new Gtk::MenuItem(_("List of Tables")) );
108 e->activate.connect(SigC::bind<Buffer::TocType>(SigC::slot(this, &FormToc::changeList), Buffer::TOC_LOT));
109 choice_->get_menu()->append( *e );
111 e = manage( new Gtk::MenuItem(_("List of Algorithms")) );
112 e->activate.connect(SigC::bind<Buffer::TocType>(SigC::slot(this, &FormToc::changeList), Buffer::TOC_LOA));
113 choice_->get_menu()->append( *e );
115 // wrap buttons and connect slots
116 b_refresh = Gtk::wrap( GTK_BUTTON( lookup_widget(pd, "button_refresh") ) );
117 b_close = Gtk::wrap( GTK_BUTTON( lookup_widget(pd, "button_close") ) );
119 b_refresh->clicked.connect(SigC::bind<bool>(SigC::slot(this, &FormToc::updateSlot),false));
120 b_close->clicked.connect(dialog_->destroy.slot());
121 dialog_->destroy.connect(SigC::slot(this, &FormToc::free));
123 u_ = d_->updateBufferDependent.connect(SigC::slot(this, &FormToc::updateSlot));
124 h_ = d_->hideBufferDependent.connect(SigC::slot(this, &FormToc::hide));
126 if (!dialog_->is_visible()) dialog_->show_all();
128 updateSlot(); // make sure its up-to-date
132 Gdk_Window dialog_win(dialog_->get_window());
138 // we can safely ignore the parameter because we can always update
139 void FormToc::updateSlot(bool)
141 Buffer::TocType type;
145 !lv_->view()->available())
147 wintitle = _( "*** No Document ***");
148 dialog_->set_title(wintitle);
149 list_->items().clear();
151 Gtk::ListItem * l = manage( new Gtk::ListItem(wintitle) );
152 list_->items().push_back( *l );
158 lv_->view()->available())
163 if( params.getCmdName() == "tableofcontents" )
165 type = Buffer::TOC_TOC;
166 wintitle = _("Table of Contents");
169 else if( params.getCmdName() == "listoffigures" )
171 type = Buffer::TOC_LOF;
172 wintitle = _("List of Figures");
175 else if( params.getCmdName() == "listofalgorithms" )
177 type = Buffer::TOC_LOA;
178 wintitle = _("List of Algorithms");
183 type = Buffer::TOC_LOT;
184 wintitle = _("List of Tables");
188 ignore_callback_ = true;
189 choice_->set_history(selection);
190 ignore_callback_ = false;
192 list_->items().clear();
194 dialog_->set_title(wintitle);
196 vector<Buffer::TocItem> toclist = (lv_->view()->buffer()->getTocList())[type];
198 Gtk::ListItem * item;
200 vector<Buffer::TocItem>::const_iterator end = toclist.end();
201 for (vector<Buffer::TocItem>::const_iterator it = toclist.begin();
204 item = manage( new Gtk::ListItem(string(4*(*it).depth,' ')+(*it).str) );
205 item->select.connect(bind<Buffer::TocItem>(slot(this,&FormToc::apply), (*it)));
213 void FormToc::apply(Buffer::TocItem tg)
216 // Doesn't compile anymore...
217 if (!lv_->view()->available()) return;
219 lv_->view()->beforeChange();
220 lv_->view()->text->SetCursor( lv_->view(), tg.par, 0 );
221 lv_->view()->text->sel_cursor = lv_->view()->text->cursor;
222 lv_->view()->update(BufferView::SELECT|BufferView::FITCUR);
225 string const str = tg.str;
226 lv_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, str);
229 void FormToc::changeList(Buffer::TocType type)
231 if (!ignore_callback_) {
233 case Buffer::TOC_TOC :
234 params.setCmdName("tableofcontents");
236 case Buffer::TOC_LOF :
237 params.setCmdName("listoffigures");
239 case Buffer::TOC_LOT :
240 params.setCmdName("listoftabels");
242 case Buffer::TOC_LOA :
243 params.setCmdName("listofalgorithms");
253 if (dialog_!=0) dialog_->destroy();