]> git.lyx.org Git - lyx.git/blob - src/insets/inseturl.C
9416d82660695e8b8fc3b8f3a86d43bfaa57021d
[lyx.git] / src / insets / inseturl.C
1 #include <config.h>
2
3 #include <cstdlib>
4
5 #ifdef __GNUG__
6 #pragma implementation
7 #endif
8
9 #include FORMS_H_LOCATION 
10 #include "inseturl.h"
11 #include "LString.h"
12 #include "commandtags.h"
13 #include "gettext.h"
14 #include "LaTeXFeatures.h"
15 #include "lyx_gui_misc.h" // CancelCloseBoxCB
16
17
18 InsetUrl::InsetUrl(string const & cmd)
19         : fd_form_url(0)
20 {
21         scanCommand(cmd);
22         if (getCmdName() == "url")
23                 flag = InsetUrl::URL;
24         else
25                 flag = InsetUrl::HTML_URL;
26 }
27
28
29 InsetUrl::InsetUrl(InsetCommand const & inscmd)
30         : fd_form_url(0)
31 {
32         setCmdName(inscmd.getCmdName());
33         setContents(inscmd.getContents());
34         setOptions(inscmd.getOptions());
35         if (getCmdName() == "url")
36                 flag = InsetUrl::URL;
37         else
38                 flag = InsetUrl::HTML_URL;
39 }
40
41
42 InsetUrl::InsetUrl(string const & ins_name, string const & ins_cont,
43                    string const & ins_opt)
44         : fd_form_url(0)
45 {
46         setCmdName(ins_name);
47         setContents(ins_cont);
48         setOptions(ins_opt);
49         if (ins_name == "url")
50                 flag = InsetUrl::URL;
51         else
52                 flag = InsetUrl::HTML_URL;
53 }
54
55
56 InsetUrl::~InsetUrl()
57 {
58         if (fd_form_url) {
59                 fl_hide_form(fd_form_url->form_url);
60                 fl_free_form(fd_form_url->form_url);
61                 fd_form_url = 0;
62         }
63 }
64
65
66 void InsetUrl::CloseUrlCB(FL_OBJECT * ob, long)
67 {
68         Holder * holder = static_cast<Holder*>(ob->u_vdata);
69         
70         InsetUrl * inset = holder->inset;
71         BufferView * bv = holder->view;
72         
73         string url = fl_get_input(inset->fd_form_url->url_name);
74         string name = fl_get_input(inset->fd_form_url->name_name);
75         string cmdname;
76         if (fl_get_button(inset->fd_form_url->radio_html))
77                 cmdname = "htmlurl";
78         else
79                 cmdname = "url";
80         
81         Buffer * buffer = bv->buffer();
82         
83         if ((url != inset->getContents() ||
84              name != inset->getOptions() ||
85              cmdname != inset->getCmdName())
86             && !(buffer->isReadonly()) ) {
87                 buffer->markDirty();
88                 inset->setContents(url);
89                 inset->setOptions(name);
90                 inset->setCmdName(cmdname);
91                 if (cmdname == "url")
92                         inset->flag = InsetUrl::URL;
93                 else
94                         inset->flag = InsetUrl::HTML_URL;
95                 bv->updateInset(inset, true);
96         }
97         
98         if (inset->fd_form_url) {
99                 fl_hide_form(inset->fd_form_url->form_url);
100                 fl_free_form(inset->fd_form_url->form_url);
101                 inset->fd_form_url = 0;
102         }
103 }
104
105
106 extern "C" void C_InsetUrl_CloseUrlCB(FL_OBJECT * ob, long data)
107 {
108         InsetUrl::CloseUrlCB(ob, data);
109 }
110
111
112 void InsetUrl::Edit(BufferView * bv, int, int, unsigned int)
113 {
114         static int ow = -1, oh;
115
116         if(bv->buffer()->isReadonly())
117                 WarnReadonly(bv->buffer()->fileName());
118
119         if (!fd_form_url) {
120                 fd_form_url = create_form_form_url();
121                 holder.inset = this;
122                 fd_form_url->button_close->u_vdata = &holder;
123                 fl_set_form_atclose(fd_form_url->form_url,
124                                     CancelCloseBoxCB, 0);
125         }
126         holder.view = bv;
127         fl_set_input(fd_form_url->url_name, getContents().c_str());
128         fl_set_input(fd_form_url->name_name, getOptions().c_str());
129         switch(flag) {
130         case InsetUrl::URL:
131                 fl_set_button(fd_form_url->radio_html, 0);
132                 break;
133         case InsetUrl::HTML_URL:
134                 fl_set_button(fd_form_url->radio_html, 1);
135                 break;
136         }
137         
138         if (fd_form_url->form_url->visible) {
139                 fl_raise_form(fd_form_url->form_url);
140         } else {
141                 fl_show_form(fd_form_url->form_url,
142                              FL_PLACE_MOUSE | FL_FREE_SIZE,
143                              FL_FULLBORDER, _("Insert Url"));
144                 if (ow < 0) {
145                         ow = fd_form_url->form_url->w;
146                         oh = fd_form_url->form_url->h;
147                 }
148                 fl_set_form_minsize(fd_form_url->form_url, ow, oh);
149         }
150 }
151
152
153 string InsetUrl::getScreenLabel() const
154 {
155         string temp;
156         if (flag == InsetUrl::HTML_URL)
157                 temp += _("HtmlUrl: ");
158         else 
159                 temp += _("Url: ");
160         temp += getContents();
161         if(!getOptions().empty()) {
162                 temp += "||";
163                 temp += getOptions();
164         }
165         return temp;
166 }
167
168
169 int InsetUrl::Latex(ostream & os, signed char fragile) const
170 {
171 #ifdef USE_OSTREAM_ONLY
172         if (!getOptions().empty())
173                 os << getOptions() + ' ';
174         if (fragile)
175                 os << "\\protect";
176         os << "\\url{" << getContents() << '}';
177         return 0;
178 #else
179         string latex_output;
180         int res = Latex(latex_output, fragile);
181         os << latex_output;
182
183         return res;
184 #endif
185 }
186
187
188 #ifndef USE_OSTREAM_ONLY
189 int InsetUrl::Latex(string & file, signed char fragile) const
190 {
191         if (!getOptions().empty())
192                 file += getOptions() + ' ';
193         if (fragile)
194                 file += "\\protect";
195
196         file += "\\url{" + getContents() + '}';
197
198         return 0;
199 }
200 #endif
201
202
203 int InsetUrl::Linuxdoc(string & file) const
204 {
205         file +=  "<"+ getCmdName() +
206                  " url=\""  + getContents()+"\"" +
207                  " name=\"" + getOptions() +"\">";
208
209         return 0;
210 }
211
212
213 int InsetUrl::DocBook(string & file) const
214 {
215         file +=  "<ulink url=\""  + getContents() + "\">" +
216                  getOptions() +"</ulink>";
217
218         return 0;
219 }
220
221
222 void InsetUrl::Validate(LaTeXFeatures & features) const
223 {
224         features.url = true;
225 }