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