]> git.lyx.org Git - lyx.git/blob - src/frontends/gnome/FormToc.C
fix problem with nroff detection, remove dead code with old floats, bogus message...
[lyx.git] / src / frontends / gnome / FormToc.C
1 /* This file is part of
2  * ====================================================== 
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 2000 The LyX Team.
7  *
8  * ======================================================
9  */
10
11 #include <config.h>
12 #include <vector>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18
19 #include "gettext.h"
20 #include "Dialogs.h"
21 #include "FormToc.h"
22 #include "LyXView.h"
23 #include "form_toc.h"
24 #include "lyxtext.h"
25 #include "lyxfunc.h"
26
27 extern "C" {
28 #include "diatoc_interface.h"
29 #include "support.h"
30 }
31
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>
38
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)
42 {
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));
48 }
49
50
51 FormToc::~FormToc()
52 {
53   hide();
54 }
55
56 void FormToc::showInset( InsetCommand * const inset )
57 {
58   if ( dialog_!=0 || inset == 0 ) return;
59   
60   inset_ = inset;
61   ih_ = inset_->hideDialog.connect(SigC::slot(this, &FormToc::hide));
62   
63   params = inset->params();
64   show();
65 }
66
67
68 void FormToc::createInset( string const & arg )
69 {
70   if( dialog_!=0 ) return;
71   
72   params.setFromString( arg );
73   show();
74 }
75
76
77 void FormToc::show()
78 {
79   Gtk::Button * b_refresh;
80   Gtk::Button * b_close;
81   Gtk::ScrolledWindow *scrolled_window;
82
83   if (!dialog_)
84     {
85       GtkWidget * pd = create_DiaToc();
86
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") ) );
90
91       list_ = manage( new Gtk::List() );
92       scrolled_window->add_with_viewport(*list_);
93
94       // fill choice
95       Gtk::MenuItem * e;
96
97       choice_->get_menu()->items().clear();
98       
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 );
102
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 );
106
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 );
110
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 );
114
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") ) );
118       
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));
122
123       u_ = d_->updateBufferDependent.connect(SigC::slot(this, &FormToc::updateSlot));
124       h_ = d_->hideBufferDependent.connect(SigC::slot(this, &FormToc::hide));
125
126       if (!dialog_->is_visible()) dialog_->show_all();
127
128       updateSlot();  // make sure its up-to-date
129     }
130   else
131     {
132       Gdk_Window dialog_win(dialog_->get_window());
133       dialog_win.raise();
134     }
135 }
136
137
138 // we can safely ignore the parameter because we can always update
139 void FormToc::updateSlot(bool)
140 {
141   Buffer::TocType type;
142   string wintitle;
143
144   if (dialog_ != 0 &&
145       !lv_->view()->available())
146     {
147       wintitle = _( "*** No Document ***");
148       dialog_->set_title(wintitle);
149       list_->items().clear();
150
151       Gtk::ListItem * l = manage( new Gtk::ListItem(wintitle) );
152       list_->items().push_back( *l );
153       return;
154     }
155   
156   
157   if (dialog_ != 0 &&
158       lv_->view()->available())
159     {
160
161       int selection = 0;
162       
163       if( params.getCmdName() == "tableofcontents" )
164         {
165           type = Buffer::TOC_TOC;
166           wintitle = _("Table of Contents");
167           selection = 0;
168         }
169       else if( params.getCmdName() == "listoffigures" )
170         {
171           type = Buffer::TOC_LOF;
172           wintitle = _("List of Figures");
173           selection = 1;
174         }
175       else if( params.getCmdName() == "listofalgorithms" )
176         {
177           type = Buffer::TOC_LOA;
178           wintitle = _("List of Algorithms");
179           selection = 3;
180         }
181       else
182         {
183           type = Buffer::TOC_LOT;
184           wintitle = _("List of Tables");
185           selection = 2;
186         }
187
188       ignore_callback_ = true;
189       choice_->set_history(selection);
190       ignore_callback_ = false;
191       
192       list_->items().clear();
193
194       dialog_->set_title(wintitle);
195
196       vector<Buffer::TocItem> toclist = (lv_->view()->buffer()->getTocList())[type];
197
198       Gtk::ListItem * item;
199
200       vector<Buffer::TocItem>::const_iterator end = toclist.end();
201       for (vector<Buffer::TocItem>::const_iterator it = toclist.begin();
202            it != end; ++it)
203         {
204           item = manage( new Gtk::ListItem(string(4*(*it).depth,' ')+(*it).str) );
205           item->select.connect(bind<Buffer::TocItem>(slot(this,&FormToc::apply), (*it)));
206           list_->add( *item );
207         }
208     }
209
210   dialog_->show_all();
211 }
212
213 void FormToc::apply(Buffer::TocItem tg)
214 {
215 #if 0  
216   // Doesn't compile anymore...
217   if (!lv_->view()->available()) return;
218
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);
223 #endif
224
225   string const str = tg.str;
226   lv_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, str);
227 }
228
229 void FormToc::changeList(Buffer::TocType type)
230 {
231         if (!ignore_callback_) {
232                 switch (type) {
233                 case Buffer::TOC_TOC :
234                         params.setCmdName("tableofcontents");
235                         break;
236                 case Buffer::TOC_LOF :
237                         params.setCmdName("listoffigures");
238                         break;
239                 case Buffer::TOC_LOT :
240                         params.setCmdName("listoftabels");
241                         break;
242                 case Buffer::TOC_LOA :
243                         params.setCmdName("listofalgorithms");
244                         break;
245                 };
246
247                 updateSlot();
248         }
249 }
250
251 void FormToc::hide()
252 {
253   if (dialog_!=0) dialog_->destroy();
254 }
255
256 void FormToc::free()
257 {
258   if (dialog_!=0)
259     {
260       dialog_ = 0;
261       u_.disconnect();
262       h_.disconnect();
263       inset_ = 0;
264       ih_.disconnect();
265     }
266 }
267