2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 2000 The LyX Team.
9 * ======================================================
16 #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>
41 FormToc::FormToc(LyXView * lv, Dialogs * d)
42 : lv_(lv), d_(d), inset_(0), u_(0), h_(0), ih_(0), dialog_(0), ignore_callback_(false)
44 // let the dialog be shown
45 // These are permanent connections so we won't bother
46 // storing a copy because we won't be disconnecting.
47 d->showTOC.connect(slot(this, &FormToc::showInset));
48 d->createTOC.connect(slot(this, &FormToc::createInset));
57 void FormToc::showInset( InsetCommand * const inset )
59 if( dialog_!=0 || inset == 0 ) return;
62 ih_ = inset_->hide.connect(slot(this, &FormToc::hide));
64 params = inset->params();
69 void FormToc::createInset( string const & arg )
71 if( dialog_!=0 ) return;
73 params.setFromString( arg );
80 Gtk::Button * b_refresh;
81 Gtk::Button * b_close;
82 Gtk::ScrolledWindow *scrolled_window;
86 GtkWidget * pd = create_DiaToc();
88 dialog_ = Gtk::wrap( GNOME_DIALOG(pd) );
89 choice_ = Gtk::wrap( GTK_OPTION_MENU( lookup_widget(pd, "choice") ) );
90 scrolled_window = Gtk::wrap( GTK_SCROLLED_WINDOW( lookup_widget(pd, "scrolledwindow") ) );
92 list_ = manage( new Gtk::List() );
93 scrolled_window->add_with_viewport(*list_);
98 choice_->get_menu()->items().clear();
100 e = manage( new Gtk::MenuItem(_("Table of Contents")) );
101 e->activate.connect(bind<Buffer::TocType>(slot(this, &FormToc::changeList), Buffer::TOC_TOC));
102 choice_->get_menu()->append( *e );
104 e = manage( new Gtk::MenuItem(_("List of Figures")) );
105 e->activate.connect(bind<Buffer::TocType>(slot(this, &FormToc::changeList), Buffer::TOC_LOF));
106 choice_->get_menu()->append( *e );
108 e = manage( new Gtk::MenuItem(_("List of Tables")) );
109 e->activate.connect(bind<Buffer::TocType>(slot(this, &FormToc::changeList), Buffer::TOC_LOT));
110 choice_->get_menu()->append( *e );
112 e = manage( new Gtk::MenuItem(_("List of Algorithms")) );
113 e->activate.connect(bind<Buffer::TocType>(slot(this, &FormToc::changeList), Buffer::TOC_LOA));
114 choice_->get_menu()->append( *e );
116 // wrap buttons and connect slots
117 b_refresh = Gtk::wrap( GTK_BUTTON( lookup_widget(pd, "button_refresh") ) );
118 b_close = Gtk::wrap( GTK_BUTTON( lookup_widget(pd, "button_close") ) );
120 b_refresh->clicked.connect(bind<bool>(slot(this, &FormToc::updateSlot),false));
121 b_close->clicked.connect(dialog_->destroy.slot());
122 dialog_->destroy.connect(slot(this, &FormToc::free));
124 u_ = d_->updateBufferDependent.connect(slot(this, &FormToc::updateSlot));
125 h_ = d_->hideBufferDependent.connect(slot(this, &FormToc::hide));
127 if (!dialog_->is_visible()) dialog_->show_all();
129 updateSlot(); // make sure its up-to-date
133 Gdk_Window dialog_win(dialog_->get_window());
139 // we can safely ignore the parameter because we can always update
140 void FormToc::updateSlot(bool)
142 Buffer::TocType type;
146 !lv_->view()->available())
148 wintitle = _( "*** No Document ***");
149 dialog_->set_title(wintitle);
150 list_->items().clear();
152 Gtk::ListItem * l = manage( new Gtk::ListItem(wintitle) );
153 list_->items().push_back( *l );
159 lv_->view()->available())
164 if( params.getCmdName() == "tableofcontents" )
166 type = Buffer::TOC_TOC;
167 wintitle = _("Table of Contents");
170 else if( params.getCmdName() == "listoffigures" )
172 type = Buffer::TOC_LOF;
173 wintitle = _("List of Figures");
176 else if( params.getCmdName() == "listofalgorithms" )
178 type = Buffer::TOC_LOA;
179 wintitle = _("List of Algorithms");
184 type = Buffer::TOC_LOT;
185 wintitle = _("List of Tables");
189 ignore_callback_ = true;
190 choice_->set_history(selection);
191 ignore_callback_ = false;
193 list_->items().clear();
195 dialog_->set_title(wintitle);
197 vector<Buffer::TocItem> toclist = (lv_->view()->buffer()->getTocList())[type];
199 Gtk::ListItem * item;
201 vector<Buffer::TocItem>::const_iterator end = toclist.end();
202 for (vector<Buffer::TocItem>::const_iterator it = toclist.begin();
205 item = manage( new Gtk::ListItem(string(4*(*it).depth,' ')+(*it).str) );
206 item->select.connect(bind<Buffer::TocItem>(slot(this,&FormToc::apply), (*it)));
214 void FormToc::apply(Buffer::TocItem tg)
216 if (!lv_->view()->available()) return;
218 lv_->view()->beforeChange();
219 lv_->view()->text->SetCursor( lv_->view(), tg.par, 0 );
220 lv_->view()->text->sel_cursor = lv_->view()->text->cursor;
221 lv_->view()->update(BufferView::SELECT|BufferView::FITCUR);
224 void FormToc::changeList(Buffer::TocType type)
226 if (!ignore_callback_)
229 case Buffer::TOC_TOC :
231 params.setCmdName("tableofcontents");
234 case Buffer::TOC_LOF :
236 params.setCmdName("listoffigures");
239 case Buffer::TOC_LOT :
241 params.setCmdName("listoftabels");
244 case Buffer::TOC_LOA :
246 params.setCmdName("listofalgorithms");
256 if (dialog_!=0) dialog_->destroy();