]> git.lyx.org Git - lyx.git/blob - src/frontends/gnome/FormUrl.C
Marko's GNOME patch and Baruch's insetgraphic patch + some fixes to make all
[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 extern "C" {
27 #include "diainserturl_interface.h"
28 #include "support.h"
29 }
30
31 #include <gtk--/base.h>
32
33 FormUrl::FormUrl(LyXView * lv, Dialogs * d)
34         : lv_(lv), d_(d), u_(0), h_(0), ih_(0), inset_(0), dialog_(NULL)
35 {
36   // let the dialog be shown
37   // These are permanent connections so we won't bother
38   // storing a copy because we won't be disconnecting.
39   d->showUrl.connect(slot(this, &FormUrl::showInset));
40   d->createUrl.connect(slot(this, &FormUrl::createInset));
41 }
42
43
44 FormUrl::~FormUrl()
45 {
46   hide();
47 }
48
49 void FormUrl::showInset( InsetCommand * const inset )
50 {
51   if( dialog_!=NULL || inset == 0 ) return;
52   
53   inset_ = inset;
54   ih_ = inset_->hide.connect(slot(this, &FormUrl::hide));
55   
56   params = inset->params();
57   show();
58 }
59
60
61 void FormUrl::createInset( string const & arg )
62 {
63   if( dialog_!=NULL ) return;
64   
65   params.setFromString( arg );
66   show();
67 }
68
69 void FormUrl::show()
70 {
71   if (!dialog_)
72     {
73       GtkWidget * pd = create_DiaInsertUrl();
74
75       dialog_ = Gtk::wrap(pd);
76       url_ = Gtk::wrap( GNOME_ENTRY( lookup_widget(pd, "url") ) );
77       name_ = Gtk::wrap( GNOME_ENTRY( lookup_widget(pd, "name") ) );
78       html_type_ = Gtk::wrap( GTK_CHECK_BUTTON( lookup_widget(pd, "html_type") ) );
79       
80       b_ok = Gtk::wrap( GTK_BUTTON( lookup_widget(pd, "button_ok") ) );
81       b_cancel = Gtk::wrap( GTK_BUTTON( lookup_widget(pd, "button_cancel") ) );
82
83       b_ok->clicked.connect(slot(this, &FormUrl::apply));
84       b_ok->clicked.connect(dialog_->destroy.slot());
85       b_cancel->clicked.connect(dialog_->destroy.slot());
86       dialog_->destroy.connect(slot(this, &FormUrl::free));
87
88       u_ = d_->updateBufferDependent.connect(slot(this, &FormUrl::update));
89       h_ = d_->hideBufferDependent.connect(slot(this, &FormUrl::hide));
90
91       if (!dialog_->is_visible()) dialog_->show_all();
92
93       update();  // make sure its up-to-date
94     }
95   else
96     {
97       Gdk_Window dialog_win(dialog_->get_window());
98       dialog_win.raise();
99     }
100 }
101
102 void FormUrl::update()
103 {
104   if (dialog_ != NULL &&
105       lv_->view()->available())
106     {
107       url_->get_entry()->set_text(params.getContents().c_str());
108       name_->get_entry()->set_text(params.getOptions().c_str());
109
110       html_type_->set_active( (params.getCmdName() == "htmlurl") );
111
112       bool sens = (!(lv_->buffer()->isReadonly()));
113
114       html_type_->set_sensitive(sens);
115       url_->set_sensitive(sens);
116       name_->set_sensitive(sens);
117       b_ok->set_sensitive(sens);
118     }
119 }
120
121 void FormUrl::hide()
122 {
123   if (dialog_!=NULL) dialog_->destroy();
124 }
125
126 void FormUrl::free()
127 {
128   if (dialog_!=NULL)
129     {
130       dialog_ = NULL;
131       u_.disconnect();
132       h_.disconnect();
133       inset_ = 0;
134       ih_.disconnect();
135     }
136 }
137
138 void FormUrl::apply()
139 {
140   if( lv_->buffer()->isReadonly() ) return;
141
142   params.setContents( url_->get_entry()->get_text() );
143   params.setOptions( name_->get_entry()->get_text() );
144
145   if (html_type_->get_active())
146     params.setCmdName("htmlurl");
147   else
148     params.setCmdName("url");
149   
150   if( inset_ != 0 )
151     {
152       // Only update if contents have changed
153       if( params != inset_->params() ) {
154         inset_->setParams( params );
155         lv_->view()->updateInset( inset_, true );
156       }
157     }
158   else
159     {
160       lv_->getLyXFunc()->Dispatch( LFUN_INSERT_URL,
161                                    params.getAsString().c_str() );
162     }
163 }