]> git.lyx.org Git - lyx.git/blob - src/frontends/gnome/FormIndex.C
Mathed fix from Dekel, GNOME patch from Marko, language-code from Garst
[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(N_("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_, N_(" 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()
142 {
143   if (dialog_ != NULL &&
144       lv_->view()->available())
145     {
146       keyword_->get_entry()->set_text(params.getContents().c_str());
147   
148       bool sens = (!(lv_->buffer()->isReadonly()));
149
150       keyword_->set_sensitive(sens);
151       b_ok->set_sensitive(sens);
152     }
153 }
154
155 void FormIndex::hide()
156 {
157   if (dialog_!=NULL) mainAppWin->remove_action();
158 }
159
160 void FormIndex::free()
161 {
162   if (dialog_!=NULL)
163     {
164       dialog_ = NULL;
165       u_.disconnect();
166       h_.disconnect();
167       inset_ = 0;
168       ih_.disconnect();
169     }
170 }
171
172 void FormIndex::apply()
173 {
174   if( lv_->buffer()->isReadonly() ) return;
175
176   params.setContents( keyword_->get_entry()->get_text() );
177
178   if( inset_ != 0 )
179     {
180       // Only update if contents have changed
181       if( params != inset_->params() )
182         {
183           inset_->setParams( params );
184           lv_->view()->updateInset( inset_, true );
185         }
186     }
187   else
188     {
189       lv_->getLyXFunc()->Dispatch( LFUN_INDEX_INSERT,
190                                    params.getAsString() );
191     }
192
193   // save history
194   keyword_->save_history();
195
196   // hide the dialog
197   hide();
198 }
199