]> git.lyx.org Git - lyx.git/blob - src/insets/inseturl.C
pos=string::npos for regex not found, use handcoded transform in lstring.C, fix the...
[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 BufferView * current_view;
18 extern void UpdateInset(Inset * inset, bool mark_dirty = true);
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         InsetUrl * inset = static_cast<InsetUrl*>(ob->u_vdata);
71         string url = fl_get_input(inset->fd_form_url->url_name);
72         string name = fl_get_input(inset->fd_form_url->name_name);
73         string cmdname;
74         if (fl_get_button(inset->fd_form_url->radio_html))
75                 cmdname = "htmlurl";
76         else
77                 cmdname = "url";
78         
79         Buffer * buffer = current_view->buffer();
80         
81         if ((url != inset->getContents() ||
82              name != inset->getOptions() ||
83              cmdname != inset->getCmdName())
84             && !(buffer->isReadonly()) ) {
85                 buffer->markDirty();
86                 inset->setContents(url);
87                 inset->setOptions(name);
88                 inset->setCmdName(cmdname);
89                 if (cmdname == "url")
90                         inset->flag = InsetUrl::URL;
91                 else
92                         inset->flag = InsetUrl::HTML_URL;
93                 UpdateInset(inset);
94         }
95         
96         if (inset->fd_form_url) {
97                 fl_hide_form(inset->fd_form_url->form_url);
98                 fl_free_form(inset->fd_form_url->form_url);
99                 inset->fd_form_url = 0;
100         }
101 }
102
103
104 extern "C" void C_InsetUrl_CloseUrlCB(FL_OBJECT * ob, long data)
105 {
106         InsetUrl::CloseUrlCB(ob, data);
107 }
108
109
110 void InsetUrl::Edit(int, int)
111 {
112         static int ow = -1, oh;
113
114         if(current_view->buffer()->isReadonly())
115                 WarnReadonly(current_view->buffer()->fileName());
116
117         if (!fd_form_url) {
118                 fd_form_url = create_form_form_url();
119                 fd_form_url->button_close->u_vdata = this;
120                 fl_set_form_atclose(fd_form_url->form_url,
121                                     CancelCloseBoxCB, 0);
122         }
123         fl_set_input(fd_form_url->url_name, getContents().c_str());
124         fl_set_input(fd_form_url->name_name, getOptions().c_str());
125         switch(flag) {
126         case InsetUrl::URL:
127                 fl_set_button(fd_form_url->radio_html, 0);
128                 break;
129         case InsetUrl::HTML_URL:
130                 fl_set_button(fd_form_url->radio_html, 1);
131                 break;
132         }
133         
134         if (fd_form_url->form_url->visible) {
135                 fl_raise_form(fd_form_url->form_url);
136         } else {
137                 fl_show_form(fd_form_url->form_url,
138                              FL_PLACE_MOUSE | FL_FREE_SIZE,
139                              FL_FULLBORDER, _("Insert Url"));
140                 if (ow < 0) {
141                         ow = fd_form_url->form_url->w;
142                         oh = fd_form_url->form_url->h;
143                 }
144                 fl_set_form_minsize(fd_form_url->form_url, ow, oh);
145         }
146 }
147
148
149 string InsetUrl::getScreenLabel() const
150 {
151         string temp;
152         if (flag == InsetUrl::HTML_URL)
153                 temp += _("HtmlUrl: ");
154         else 
155                 temp += _("Url: ");
156         temp += getContents();
157         if(!getOptions().empty()) {
158                 temp += "||";
159                 temp += getOptions();
160         }
161         return temp;
162 }
163
164
165 int InsetUrl::Latex(ostream & os, signed char fragile)
166 {
167         string latex_output;
168         int res = Latex(latex_output, fragile);
169         os << latex_output;
170
171         return res;
172 }
173
174
175 int InsetUrl::Latex(string & file, signed char fragile)
176 {
177         if (!getOptions().empty())
178                 file += getOptions() + ' ';
179         if (fragile)
180                 file += "\\protect";
181
182         file += "\\url{" + getContents() + '}';
183
184         return 0;
185 }
186
187
188 int InsetUrl::Linuxdoc(string & file)
189 {
190         file +=  "<"+ getCmdName() +
191                  " url=\""  + getContents()+"\"" +
192                  " name=\"" + getOptions() +"\">";
193
194         return 0;
195 }
196
197
198 int InsetUrl::DocBook(string & file)
199 {
200         file +=  "<ulink url=\""  + getContents() + "\">" +
201                  getOptions() +"</ulink>";
202
203         return 0;
204 }
205
206
207 void InsetUrl::Validate(LaTeXFeatures & features) const
208 {
209         features.url = true;
210 }