]> git.lyx.org Git - lyx.git/blob - src/insets/inseturl.C
76a231a3697a9df0e7b6c04663af352f4983875d
[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();
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, CancelCloseBoxCB, 0);
121         }
122         fl_set_input(fd_form_url->url_name, getContents().c_str());
123         fl_set_input(fd_form_url->name_name, getOptions().c_str());
124         switch(flag) {
125         case InsetUrl::URL:
126                 fl_set_button(fd_form_url->radio_html, 0);
127                 break;
128         case InsetUrl::HTML_URL:
129                 fl_set_button(fd_form_url->radio_html, 1);
130                 break;
131         }
132         
133         if (fd_form_url->form_url->visible) {
134                 fl_raise_form(fd_form_url->form_url);
135         } else {
136                 fl_show_form(fd_form_url->form_url,
137                              FL_PLACE_MOUSE | FL_FREE_SIZE,
138                              FL_FULLBORDER, _("Insert Url"));
139                 if (ow < 0) {
140                         ow = fd_form_url->form_url->w;
141                         oh = fd_form_url->form_url->h;
142                 }
143                 fl_set_form_minsize(fd_form_url->form_url, ow, oh);
144         }
145 }
146
147
148 string InsetUrl::getScreenLabel() const
149 {
150         string temp;
151         if (flag == InsetUrl::HTML_URL)
152                 temp += _("HtmlUrl: ");
153         else 
154                 temp += _("Url: ");
155         temp += getContents();
156         if(!getOptions().empty()) {
157                 temp += "||";
158                 temp += getOptions();
159         }
160         return temp;
161 }
162
163
164 int InsetUrl::Latex(FILE * file, signed char fragile)
165 {
166         string latex_output;
167         int res = Latex(latex_output, fragile);
168         fprintf(file, "%s", latex_output.c_str());
169
170         return res;
171 }
172
173
174 int InsetUrl::Latex(string &file, signed char fragile)
175 {
176         if (!getOptions().empty())
177                 file += getOptions() + ' ';
178         if (fragile)
179                 file += "\\protect";
180
181         file += "\\url{" + getContents() + '}';
182
183         return 0;
184 }
185
186
187 int InsetUrl::Linuxdoc(string & file)
188 {
189         file +=  "<"+ getCmdName() +
190                  " url=\""  + getContents()+"\"" +
191                  " name=\"" + getOptions() +"\">";
192
193         return 0;
194 }
195
196
197 int InsetUrl::DocBook(string & file)
198 {
199         file +=  "<ulink url=\""  + getContents() + "\">" +
200                  getOptions() +"</ulink>";
201
202         return 0;
203 }
204
205
206 void InsetUrl::Validate(LaTeXFeatures & features) const
207 {
208         features.url = true;
209 }