]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormUrl.C
patch from Angus and patch from Kayvan
[lyx.git] / src / frontends / xforms / 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 #include "gettext.h"
14 #include FORMS_H_LOCATION
15 #include "BufferView.h"
16 #include "Dialogs.h"
17 #include "FormUrl.h"
18 #include "LyXView.h"
19 #include "buffer.h"
20 #include "form_url.h"
21 #include "lyxfunc.h"
22 #include "xform_macros.h"
23 #include "insets/insetcommand.h"
24 #include "insets/inseturl.h"
25 #include "support/filetools.h"
26
27 #ifdef __GNUG__
28 #pragma implementation
29 #endif
30
31 C_RETURNCB(FormUrl, WMHideCB)
32 C_GENERICCB(FormUrl, OKCB)
33 C_GENERICCB(FormUrl, CancelCB)
34
35 FormUrl::FormUrl(LyXView * lv, Dialogs * d)
36         : dialog_(0), lv_(lv), d_(d), u_(0), h_(0), ih_(0),
37           inset_(0), dialogIsOpen(false)
38 {
39         // let the dialog be shown
40         // These are permanent connections so we won't bother
41         // storing a copy because we won't be disconnecting.
42         d->showUrl.connect(slot(this, &FormUrl::showInset));
43         d->createUrl.connect(slot(this, &FormUrl::createInset));
44         params = new InsetCommandParams();
45 }
46
47
48 FormUrl::~FormUrl()
49 {
50         free();
51         delete params;
52 }
53
54
55 void FormUrl::build()
56 {
57         dialog_ = build_url();
58 }
59
60
61 void FormUrl::showInset( InsetUrl * inset )
62 {
63         if( dialogIsOpen || inset == 0 ) return;
64
65         inset_ = inset;
66         ih_ = inset_->hide.connect(slot(this, &FormUrl::hide));
67
68         (*params) = inset->params();
69         show();
70 }
71
72
73 void FormUrl::createInset( string const & arg )
74 {
75         if( dialogIsOpen ) return;
76
77         params->setFromString( arg );
78         show();
79 }
80
81
82 void FormUrl::show()
83 {
84         if (!dialog_) {
85                 build();
86                 fl_set_form_atclose(dialog_->form_url,
87                                     C_FormUrlWMHideCB, 0);
88         }
89
90         update();  // make sure its up-to-date
91
92         dialogIsOpen = true;
93         if (dialog_->form_url->visible) {
94                 fl_raise_form(dialog_->form_url);
95         } else {
96                 fl_show_form(dialog_->form_url,
97                              FL_PLACE_MOUSE | FL_FREE_SIZE,
98                              FL_TRANSIENT,
99                              _("Url"));
100                 u_ = d_->updateBufferDependent.
101                          connect(slot(this, &FormUrl::update));
102                 h_ = d_->hideBufferDependent.
103                          connect(slot(this, &FormUrl::hide));
104         }
105 }
106
107
108 void FormUrl::update()
109 {
110         fl_set_input(dialog_->url,  params->getContents().c_str());
111         fl_set_input(dialog_->name, params->getOptions().c_str());
112
113         if ( params->getCmdName() == "url" )
114                 fl_set_button(dialog_->radio_html, 0);
115         else
116                 fl_set_button(dialog_->radio_html, 1);
117
118         static int ow = -1, oh;
119
120         if (ow < 0) {
121                 ow = dialog_->form_url->w;
122                 oh = dialog_->form_url->h;
123         }
124
125         fl_set_form_minsize(dialog_->form_url, ow, oh);
126         fl_set_form_maxsize(dialog_->form_url, 2*ow, oh);
127 }
128
129
130 void FormUrl::apply()
131 {
132         if( lv_->buffer()->isReadonly() ) return;
133
134         params->setContents( fl_get_input(dialog_->url) );
135         params->setOptions( fl_get_input(dialog_->name) );
136
137         if (fl_get_button(dialog_->radio_html))
138                 params->setCmdName("htmlurl");
139         else
140                 params->setCmdName("url");
141
142         if( inset_ != 0 )
143         {
144                 inset_->setParams( *params );
145                 lv_->view()->updateInset( inset_, true );
146         } else {
147                 lv_->getLyXFunc()->Dispatch( LFUN_INSERT_URL,
148                                              params->getAsString().c_str() );
149         }
150 }
151
152
153 void FormUrl::hide()
154 {
155         if (dialog_
156             && dialog_->form_url
157             && dialog_->form_url->visible) {
158                 fl_hide_form(dialog_->form_url);
159                 u_.disconnect();
160                 h_.disconnect();
161         }
162
163         // free up the dialog for another inset
164         inset_ = 0;
165         ih_.disconnect();
166         dialogIsOpen = false;
167 }
168
169
170 void FormUrl::free()
171 {
172         // we don't need to delete u and h here because
173         // hide() does that after disconnecting.
174         if (dialog_) {
175                 if (dialog_->form_url
176                     && dialog_->form_url->visible) {
177                         hide();
178                 }
179                 fl_free_form(dialog_->form_url);
180                 delete dialog_;
181                 dialog_ = 0;
182         }
183 }
184
185
186 int FormUrl::WMHideCB(FL_FORM * form, void *)
187 {
188         // Ensure that the signals (u and h) are disconnected even if the
189         // window manager is used to close the dialog.
190         FormUrl * pre = static_cast<FormUrl*>(form->u_vdata);
191         pre->hide();
192         return FL_CANCEL;
193 }
194
195
196 void FormUrl::OKCB(FL_OBJECT * ob, long)
197 {
198         FormUrl * pre = static_cast<FormUrl*>(ob->form->u_vdata);
199         pre->apply();
200         pre->hide();
201 }
202
203
204 void FormUrl::CancelCB(FL_OBJECT * ob, long)
205 {
206         FormUrl * pre = static_cast<FormUrl*>(ob->form->u_vdata);
207         pre->hide();
208 }