]> git.lyx.org Git - lyx.git/blob - src/insets/inseturl.C
3c174139d3a7a91cd907eeacfaeef9cf9e9cd7a5
[lyx.git] / src / insets / inseturl.C
1 #include <config.h>
2
3 #include <stdlib.h>
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 = (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->currentBuffer();
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                 inset->fd_form_url = 0;
99         }
100 }
101
102 extern "C" void C_InsetUrl_CloseUrlCB(FL_OBJECT *ob, long)
103 {
104         InsetUrl::CloseUrlCB(ob,0);
105 }
106
107 void InsetUrl::Edit(int, int)
108 {
109         static int ow = -1, oh;
110
111         if(current_view->currentBuffer()->isReadonly())
112                 WarnReadonly();
113
114         if (!fd_form_url) {
115                 fd_form_url = create_form_form_url();
116                 fd_form_url->button_close->u_vdata = this;
117                 fl_set_form_atclose(fd_form_url->form_url,CancelCloseBoxCB, 0);
118         }
119         fl_set_input(fd_form_url->url_name, getContents().c_str());
120         fl_set_input(fd_form_url->name_name, getOptions().c_str());
121         switch(flag) {
122         case InsetUrl::URL:
123                 fl_set_button(fd_form_url->radio_html, 0);
124                 break;
125         case InsetUrl::HTML_URL:
126                 fl_set_button(fd_form_url->radio_html, 1);
127                 break;
128         }
129         
130         if (fd_form_url->form_url->visible) {
131                 fl_raise_form(fd_form_url->form_url);
132         } else {
133                 fl_show_form(fd_form_url->form_url,
134                              FL_PLACE_MOUSE | FL_FREE_SIZE,
135                              FL_FULLBORDER, _("Insert Url"));
136                 if (ow < 0) {
137                         ow = fd_form_url->form_url->w;
138                         oh = fd_form_url->form_url->h;
139                 }
140                 fl_set_form_minsize(fd_form_url->form_url, ow, oh);
141         }
142 }
143
144
145 string InsetUrl::getScreenLabel() const
146 {
147         string temp;
148         if (flag == InsetUrl::HTML_URL)
149                 temp += _("HtmlUrl: ");
150         else 
151                 temp += _("Url: ");
152         temp += getContents();
153         if(!getOptions().empty()) {
154                 temp += "||";
155                 temp += getOptions();
156         }
157         return temp;
158 }
159
160
161 int InsetUrl::Latex(FILE *file, signed char fragile)
162 {
163         string latex_output;
164         int res = Latex(latex_output, fragile);
165         fprintf(file, "%s", latex_output.c_str());
166
167         return res;
168 }
169
170
171 int InsetUrl::Latex(string &file, signed char fragile)
172 {
173         if (!getOptions().empty())
174                 file += getOptions() + ' ';
175         if (fragile)
176                 file += "\\protect";
177
178         file += "\\url{" + getContents() + '}';
179
180         return 0;
181 }
182
183
184 int InsetUrl::Linuxdoc(string &file)
185 {
186         file +=  "<"+ getCmdName() +
187                  " url=\""  + getContents()+"\"" +
188                  " name=\"" + getOptions() +"\">";
189
190         return 0;
191 }
192
193
194 int InsetUrl::DocBook(string &file)
195 {
196         file +=  "<ulink url=\""  + getContents() + "\">" +
197                  getOptions() +"</ulink>";
198
199         return 0;
200 }
201
202
203 void InsetUrl::Validate(LaTeXFeatures& features) const
204 {
205         features.url = true;
206 }