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