]> git.lyx.org Git - features.git/blob - src/insets/inseturl.C
read the Changelog
[features.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 using std::ostream;
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                 bv->updateInset(inset, true);
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"
109 void C_InsetUrl_CloseUrlCB(FL_OBJECT * ob, long data)
110 {
111         InsetUrl::CloseUrlCB(ob, data);
112 }
113
114
115 char const * InsetUrl::EditMessage() const 
116 {
117         return _("Opened Url");
118 }
119
120
121 void InsetUrl::Edit(BufferView * bv, int, int, unsigned int)
122 {
123         static int ow = -1, oh;
124
125         if(bv->buffer()->isReadonly())
126                 WarnReadonly(bv->buffer()->fileName());
127
128         if (!fd_form_url) {
129                 fd_form_url = create_form_form_url();
130                 holder.inset = this;
131                 fd_form_url->button_close->u_vdata = &holder;
132                 fl_set_form_atclose(fd_form_url->form_url,
133                                     CancelCloseBoxCB, 0);
134         }
135         holder.view = bv;
136         fl_set_input(fd_form_url->url_name, getContents().c_str());
137         fl_set_input(fd_form_url->name_name, getOptions().c_str());
138         switch(flag) {
139         case InsetUrl::URL:
140                 fl_set_button(fd_form_url->radio_html, 0);
141                 break;
142         case InsetUrl::HTML_URL:
143                 fl_set_button(fd_form_url->radio_html, 1);
144                 break;
145         }
146         
147         if (fd_form_url->form_url->visible) {
148                 fl_raise_form(fd_form_url->form_url);
149         } else {
150                 fl_show_form(fd_form_url->form_url,
151                              FL_PLACE_MOUSE | FL_FREE_SIZE,
152                              FL_FULLBORDER, _("Insert Url"));
153                 if (ow < 0) {
154                         ow = fd_form_url->form_url->w;
155                         oh = fd_form_url->form_url->h;
156                 }
157                 fl_set_form_minsize(fd_form_url->form_url, ow, oh);
158         }
159 }
160
161
162 string InsetUrl::getScreenLabel() const
163 {
164         string temp;
165         if (flag == InsetUrl::HTML_URL)
166                 temp += _("HtmlUrl: ");
167         else 
168                 temp += _("Url: ");
169         if(!getOptions().empty()) {
170                 temp += getOptions();
171         } else {
172           temp += getContents();
173         }
174         return temp;
175 }
176
177
178 int InsetUrl::Latex(Buffer const *, ostream & os,
179                     bool fragile, bool /*free_spc*/) const
180 {
181         if (!getOptions().empty())
182                 os << getOptions() + ' ';
183         if (fragile)
184                 os << "\\protect";
185         os << "\\url{" << getContents() << '}';
186         return 0;
187 }
188
189
190 int InsetUrl::Ascii(Buffer const *, ostream & os) const
191 {
192         if (getOptions().empty())
193                 os << "[" << getContents() << "]";
194         else
195                 os << "[" << getContents() << "||" <<  getOptions() << "]";
196         return 0;
197 }
198
199
200 int InsetUrl::Linuxdoc(Buffer const *, ostream & os) const
201 {
202         os << "<" << getCmdName()
203            << " url=\""  << getContents() << "\""
204            << " name=\"" << getOptions() << "\">";
205
206         return 0;
207 }
208
209
210 int InsetUrl::DocBook(Buffer const *, ostream & os) const
211 {
212         os << "<ulink url=\"" << getContents() << "\">"
213            << getOptions() << "</ulink>";
214         return 0;
215 }
216
217
218 void InsetUrl::Validate(LaTeXFeatures & features) const
219 {
220         features.url = true;
221 }