]> git.lyx.org Git - lyx.git/blob - src/frontends/gnome/FormUrl.C
More preference work from Angus
[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(_("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(_("URL")) );
118       table->attach( *label, 0, 1, 0, 1, 0, 0 );
119       label = manage( new Gtk::Label(_("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_, _(" 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(bool switched)
154 {
155   if (switched)
156     {
157       hide();
158       return;
159     }
160   
161   if (dialog_ != NULL &&
162       lv_->view()->available())
163     {
164       url_->get_entry()->set_text(params.getContents().c_str());
165       name_->get_entry()->set_text(params.getOptions().c_str());
166
167       html_type_->set_active( (params.getCmdName() == "htmlurl") );
168
169       bool sens = (!(lv_->buffer()->isReadonly()));
170
171       html_type_->set_sensitive(sens);
172       url_->set_sensitive(sens);
173       name_->set_sensitive(sens);
174       b_ok->set_sensitive(sens);
175     }
176 }
177
178 void FormUrl::hide()
179 {
180   if (dialog_!=NULL) mainAppWin->remove_action();
181 }
182
183 void FormUrl::free()
184 {
185   if (dialog_!=NULL)
186     {
187       dialog_ = NULL;
188       u_.disconnect();
189       h_.disconnect();
190       inset_ = 0;
191       ih_.disconnect();
192     }
193 }
194
195 void FormUrl::apply()
196 {
197   if( lv_->buffer()->isReadonly() ) return;
198
199   params.setContents( url_->get_entry()->get_text() );
200   params.setOptions( name_->get_entry()->get_text() );
201
202   if (html_type_->get_active())
203     params.setCmdName("htmlurl");
204   else
205     params.setCmdName("url");
206   
207   if( inset_ != 0 )
208     {
209       // Only update if contents have changed
210       if( params != inset_->params() ) {
211         inset_->setParams( params );
212         lv_->view()->updateInset( inset_, true );
213       }
214     }
215   else
216     {
217       lv_->getLyXFunc()->Dispatch( LFUN_INSERT_URL,
218                                    params.getAsString() );
219     }
220
221   // save history
222   url_->save_history();
223   name_->save_history();
224
225   // hide the dialog
226   hide();
227 }