]> git.lyx.org Git - lyx.git/blob - src/frontends/gnome/FormUrl.C
small patch from Dekel, begin introducing the real boost framework, get rid of the...
[lyx.git] / src / frontends / gnome / FormUrl.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 "FormUrl.h"
22 #include "LyXView.h"
23 #include "buffer.h"
24 #include "lyxfunc.h"
25
26 #include <gtk--/label.h>
27 #include <gtk--/table.h>
28 #include <gtk--/box.h>
29 #include <gtk--/buttonbox.h>
30 #include <gtk--/base.h>
31 #include <gtk--/separator.h>
32
33 // temporary solution for LyXView
34 #include "mainapp.h"
35 extern GLyxAppWin * mainAppWin;
36
37 // configuration keys
38 static string const CONF_ENTRY_URL("FormUrl_url");
39 static string const CONF_ENTRY_NAME("FormUrl_name");
40
41 FormUrl::FormUrl(LyXView * lv, Dialogs * d)
42         : lv_(lv), d_(d), inset_(0), u_(0), h_(0), ih_(0), dialog_(NULL)
43 {
44   // let the dialog be shown
45   // These are permanent connections so we won't bother
46   // storing a copy because we won't be disconnecting.
47   d->showUrl.connect(slot(this, &FormUrl::showInset));
48   d->createUrl.connect(slot(this, &FormUrl::createInset));
49 }
50
51
52 FormUrl::~FormUrl()
53 {
54   hide();
55 }
56
57 void FormUrl::showInset( InsetCommand * const inset )
58 {
59   if( dialog_!=NULL || inset == 0 ) return;
60   
61   inset_ = inset;
62   ih_ = inset_->hide.connect(slot(this, &FormUrl::hide));
63   
64   params = inset->params();
65   show();
66 }
67
68
69 void FormUrl::createInset( string const & arg )
70 {
71   if( dialog_!=NULL ) return;
72   
73   params.setFromString( arg );
74   show();
75 }
76
77 void FormUrl::show()
78 {
79   if (!dialog_)
80     {
81       using namespace Gtk::Box_Helpers;
82
83       Gtk::Label * label;
84       Gtk::Table * table = manage( new Gtk::Table(2, 2, FALSE) );
85       Gtk::Box * mbox = manage( new Gtk::HBox() );
86       Gtk::ButtonBox * bbox = manage( new Gtk::VButtonBox() );
87       Gtk::Separator * sep = manage( new Gtk::VSeparator() );
88
89       url_ = manage( new Gnome::Entry() );
90       name_ = manage( new Gnome::Entry() );
91       html_type_ = manage( new Gtk::CheckButton(N_("HTML type")) );
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       table->set_row_spacings(2);
98       table->set_col_spacings(2);
99       mbox->set_spacing(2);
100       bbox->set_spacing(4);
101
102       // configure entries
103       url_->set_history_id(CONF_ENTRY_URL);
104       url_->set_max_saved(10);
105       url_->load_history();
106       url_->set_use_arrows_always(true);
107       
108       name_->set_history_id(CONF_ENTRY_NAME);
109       name_->set_max_saved(10);
110       name_->load_history();
111       name_->set_use_arrows_always(true);
112       
113       // pack widgets
114       bbox->children().push_back(Element(*b_ok, false, false));
115       bbox->children().push_back(Element(*b_cancel, false, false));
116       
117       label = manage( new Gtk::Label(N_("URL")) );
118       table->attach( *label, 0, 1, 0, 1, 0, 0 );
119       label = manage( new Gtk::Label(N_("Name")) );
120       table->attach( *label, 0, 1, 1, 2, 0, 0 );
121       table->attach( *url_, 1, 2, 0, 1 );
122       table->attach( *name_, 1, 2, 1, 2 );
123
124       mbox->children().push_back(Element(*table));
125       mbox->children().push_back(Element(*html_type_, false, false));
126       mbox->children().push_back(Element(*sep, false, false));
127       mbox->children().push_back(Element(*bbox, false, false));
128       
129       // packing dialog to main window
130       dialog_ = mbox;
131       mainAppWin->add_action(*dialog_, N_(" URL "));
132
133       // setting focus
134       GTK_WIDGET_SET_FLAGS (GTK_WIDGET(url_->get_entry()->gtkobj()), GTK_CAN_DEFAULT);
135       gtk_widget_grab_focus (GTK_WIDGET(url_->get_entry()->gtkobj()));
136       gtk_widget_grab_default (GTK_WIDGET(url_->get_entry()->gtkobj()));
137
138       // connecting signals
139       b_ok->clicked.connect(slot(this, &FormUrl::apply));
140       name_->get_entry()->activate.connect(slot(this, &FormUrl::apply));
141
142       b_cancel->clicked.connect(slot(mainAppWin, &GLyxAppWin::remove_action));
143
144       dialog_->destroy.connect(slot(this, &FormUrl::free));
145
146       u_ = d_->updateBufferDependent.connect(slot(this, &FormUrl::update));
147       h_ = d_->hideBufferDependent.connect(slot(this, &FormUrl::hide));
148
149       update();  // make sure its up-to-date
150     }
151 }
152
153 void FormUrl::update()
154 {
155   if (dialog_ != NULL &&
156       lv_->view()->available())
157     {
158       url_->get_entry()->set_text(params.getContents().c_str());
159       name_->get_entry()->set_text(params.getOptions().c_str());
160
161       html_type_->set_active( (params.getCmdName() == "htmlurl") );
162
163       bool sens = (!(lv_->buffer()->isReadonly()));
164
165       html_type_->set_sensitive(sens);
166       url_->set_sensitive(sens);
167       name_->set_sensitive(sens);
168       b_ok->set_sensitive(sens);
169     }
170 }
171
172 void FormUrl::hide()
173 {
174   if (dialog_!=NULL) mainAppWin->remove_action();
175 }
176
177 void FormUrl::free()
178 {
179   if (dialog_!=NULL)
180     {
181       dialog_ = NULL;
182       u_.disconnect();
183       h_.disconnect();
184       inset_ = 0;
185       ih_.disconnect();
186     }
187 }
188
189 void FormUrl::apply()
190 {
191   if( lv_->buffer()->isReadonly() ) return;
192
193   params.setContents( url_->get_entry()->get_text() );
194   params.setOptions( name_->get_entry()->get_text() );
195
196   if (html_type_->get_active())
197     params.setCmdName("htmlurl");
198   else
199     params.setCmdName("url");
200   
201   if( inset_ != 0 )
202     {
203       // Only update if contents have changed
204       if( params != inset_->params() ) {
205         inset_->setParams( params );
206         lv_->view()->updateInset( inset_, true );
207       }
208     }
209   else
210     {
211       lv_->getLyXFunc()->Dispatch( LFUN_INSERT_URL,
212                                    params.getAsString() );
213     }
214
215   // save history
216   url_->save_history();
217   name_->save_history();
218
219   // hide the dialog
220   hide();
221 }