]> git.lyx.org Git - lyx.git/blob - src/frontends/gnome/FormIndex.C
fix problem with nroff detection, remove dead code with old floats, bogus message...
[lyx.git] / src / frontends / gnome / FormIndex.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
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17
18 #include "gettext.h"
19 #include "Dialogs.h"
20 #include "FormIndex.h"
21 #include "LyXView.h"
22 #include "buffer.h"
23 #include "lyxfunc.h"
24 #include "form_index.h"
25
26 #include <gtk--/label.h>
27 #include <gtk--/box.h>
28 #include <gtk--/buttonbox.h>
29 #include <gnome--/entry.h>
30 #include <gnome--/stock.h>
31 #include <gtk--/separator.h>
32
33 // temporary solution for LyXView
34 #include "mainapp.h"
35 extern GLyxAppWin * mainAppWin;
36
37 namespace {
38
39 // configuration keys
40 string const CONF_ENTRY("FormIndex_entry");
41
42 } // namespace anon
43
44
45 FormIndex::FormIndex(LyXView * lv, Dialogs * d)
46         : lv_(lv), d_(d), inset_(0), u_(0), h_(0), ih_(0), dialog_(0)
47 {
48   // let the dialog be shown
49   // These are permanent connections so we won't bother
50   // storing a copy because we won't be disconnecting.
51   d->showIndex.connect(slot(this, &FormIndex::showInset));
52   d->createIndex.connect(slot(this, &FormIndex::createInset));
53 }
54
55
56 FormIndex::~FormIndex()
57 {
58   hide();
59 }
60
61 void FormIndex::showInset( InsetCommand * const inset )
62 {
63   if( dialog_!=0 || inset == 0 ) return;
64   
65   inset_ = inset;
66   ih_ = inset_->hideDialog.connect(slot(this, &FormIndex::hide));
67   
68   params = inset->params();
69   show();
70 }
71
72 void FormIndex::createInset( string const & arg )
73 {
74   if( dialog_!=0 ) return;
75   
76   params.setFromString( arg );
77   show();
78 }
79
80 void FormIndex::show()
81 {
82   if (!dialog_)
83     {
84       using namespace Gtk::Box_Helpers;
85       
86       Gtk::Label * label = manage( new Gtk::Label(_("Keyword")) );
87       Gtk::Box * mbox = manage( new Gtk::HBox() );
88       Gtk::ButtonBox * bbox = manage( new Gtk::HButtonBox() );
89       Gtk::Separator * sep = manage( new Gtk::VSeparator() );
90
91       keyword_ = manage( new Gnome::Entry() );
92       
93       b_ok = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_OK) ) );
94       b_cancel = Gtk::wrap( GTK_BUTTON( gnome_stock_button(GNOME_STOCK_BUTTON_CANCEL) ) );
95       
96       // set up spacing
97       mbox->set_spacing(2);
98       bbox->set_spacing(4);
99
100       keyword_->set_history_id(CONF_ENTRY);
101       keyword_->set_max_saved(10);
102       keyword_->load_history();
103       keyword_->set_use_arrows_always(true);
104       
105       // packing
106       bbox->children().push_back(Element(*b_ok, false, false));
107       bbox->children().push_back(Element(*b_cancel, false, false));
108
109       mbox->children().push_back(Element(*label, false, false));
110       mbox->children().push_back(Element(*keyword_, true, true));
111       mbox->children().push_back(Element(*sep, false, false));
112       mbox->children().push_back(Element(*bbox, false, false));
113
114       // packing dialog to main window
115       dialog_ = mbox;
116       mainAppWin->add_action(*dialog_, _(" Index "));
117
118       // setting focus
119       GTK_WIDGET_SET_FLAGS (GTK_WIDGET(keyword_->get_entry()->gtkobj()), GTK_CAN_DEFAULT);
120       gtk_widget_grab_focus (GTK_WIDGET(keyword_->get_entry()->gtkobj()));
121       gtk_widget_grab_default (GTK_WIDGET(keyword_->get_entry()->gtkobj()));
122
123       // connecting signals
124       b_ok->clicked.connect(slot(this, &FormIndex::apply));
125       keyword_->get_entry()->activate.connect(slot(this, &FormIndex::apply));
126
127       b_cancel->clicked.connect(slot(mainAppWin, &GLyxAppWin::remove_action));
128
129       dialog_->destroy.connect(slot(this, &FormIndex::free));
130
131       u_ = d_->updateBufferDependent.connect(slot(this, &FormIndex::updateSlot));
132       h_ = d_->hideBufferDependent.connect(slot(this, &FormIndex::hide));
133
134       updateSlot();  // make sure its up-to-date
135     }
136 }
137
138 void FormIndex::updateSlot(bool switched)
139 {
140   if (switched)
141     {
142       hide();
143       return;
144     }
145   
146   if (dialog_ != 0 &&
147       lv_->view()->available())
148     {
149       keyword_->get_entry()->set_text(params.getContents().c_str());
150       
151       bool sens = (!(lv_->buffer()->isReadonly()));
152       
153       keyword_->set_sensitive(sens);
154       b_ok->set_sensitive(sens);
155     }
156 }
157       
158 void FormIndex::hide()
159 {
160   if (dialog_!=0) mainAppWin->remove_action();
161 }
162
163 void FormIndex::free()
164 {
165   if (dialog_!=0)
166     {
167       dialog_ = 0;
168       u_.disconnect();
169       h_.disconnect();
170       inset_ = 0;
171       ih_.disconnect();
172     }
173 }
174
175 void FormIndex::apply()
176 {
177   if( lv_->buffer()->isReadonly() ) return;
178
179   params.setContents( keyword_->get_entry()->get_text() );
180
181   if( inset_ != 0 )
182     {
183       // Only update if contents have changed
184       if( params != inset_->params() )
185         {
186           inset_->setParams( params );
187           lv_->view()->updateInset( inset_, true );
188         }
189     }
190   else
191     {
192       lv_->getLyXFunc()->Dispatch( LFUN_INDEX_INSERT,
193                                    params.getAsString() );
194     }
195
196   // save history
197   keyword_->save_history();
198
199   // hide the dialog
200   hide();
201 }
202