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