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