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