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