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