]> git.lyx.org Git - lyx.git/blob - src/insets/inseturl.C
last updates from 1.0.4, no more updates expected from that branch now
[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(LString const & cmd)
21         : form(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         : form(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(LString const &ins_name,LString const &ins_cont,
45                    LString const &ins_opt)
46         : form(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 (form) {
61                 fl_hide_form(form);
62                 fl_free_form(form);
63                 form = 0;
64         }
65 }
66
67
68 void InsetUrl::CloseUrlCB(FL_OBJECT *ob, long)
69 {
70         InsetUrl *inset = (InsetUrl*) ob->u_vdata;
71         LString url = fl_get_input(inset->url_name);
72         LString name = fl_get_input(inset->name_name);
73         LString cmdname;
74         if (fl_get_button(inset->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->form) {
97                 fl_hide_form(inset->form);
98                 inset->form = NULL;
99         }
100 }
101
102
103 void InsetUrl::Edit(int, int)
104 {
105         if(current_view->currentBuffer()->isReadonly())
106                 WarnReadonly();
107
108         if (!form) {
109                 FL_OBJECT *obj;
110                 form = fl_bgn_form(FL_NO_BOX, 530, 170);
111                 obj = fl_add_box(FL_UP_BOX,0,0,530,170,"");
112                 url_name = obj = fl_add_input(FL_NORMAL_INPUT,50,30,460,30,idex(_("Url|#U")));
113                 fl_set_button_shortcut(obj,scex(_("Url|#U")),1);
114                 name_name = obj = fl_add_input(FL_NORMAL_INPUT,50,80,460,30,idex(_("Name|#N")));
115                 fl_set_button_shortcut(obj,scex(_("Name|#N")),1);
116                 obj = fl_add_button(FL_RETURN_BUTTON,360,130,100,30,idex(_("Close|#C^[^M")));
117                 fl_set_button_shortcut(obj,scex(_("Close|#C^[^M")),1);
118                 obj->u_vdata = this;
119                 fl_set_object_callback(obj,CloseUrlCB,0);
120                 radio_html = obj = fl_add_checkbutton(FL_PUSH_BUTTON,50,130,240,30,idex(_("HTML type|#H")));
121                 fl_set_button_shortcut(obj,scex(_("HTML type|#H")),1);
122                 fl_set_object_lsize(obj,FL_NORMAL_SIZE);
123                 fl_end_form();
124                 fl_set_form_atclose(form, CancelCloseBoxCB, NULL);
125         }
126         fl_set_input(url_name, getContents().c_str());
127         fl_set_input(name_name, getOptions().c_str());
128         switch(flag) {
129         case InsetUrl::URL:
130                 fl_set_button(radio_html, 0);
131                 break;
132         case InsetUrl::HTML_URL:
133                 fl_set_button(radio_html, 1);
134                 break;
135         }
136         
137         if (form->visible) {
138                 fl_raise_form(form);
139         } else {
140                 fl_show_form(form, FL_PLACE_MOUSE,
141                              FL_FULLBORDER, _("Insert Url"));
142         }
143 }
144
145
146 LString InsetUrl::getScreenLabel() const
147 {
148         LString temp;
149         if (flag == InsetUrl::HTML_URL)
150                 temp += _("HtmlUrl: ");
151         else 
152                 temp += _("Url: ");
153         temp += getContents();
154         if(!getOptions().empty()) {
155                 temp += "||";
156                 temp += getOptions();
157         }
158         return temp;
159 }
160
161
162 int InsetUrl::Latex(FILE *file, signed char fragile)
163 {
164         LString latex_output;
165         int res = Latex(latex_output, fragile);
166         fprintf(file, "%s", latex_output.c_str());
167
168         return res;
169 }
170
171
172 int InsetUrl::Latex(LString &file, signed char fragile)
173 {
174         if (!getOptions().empty())
175                 file += getOptions() + ' ';
176         if (fragile)
177                 file += "\\protect";
178
179         file += "\\url{" + getContents() + '}';
180
181         return 0;
182 }
183
184
185 int InsetUrl::Linuxdoc(LString &file)
186 {
187         file +=  "<"+ getCmdName() +
188                  " url=\""  + getContents()+"\"" +
189                  " name=\"" + getOptions() +"\">";
190
191         return 0;
192 }
193
194
195 int InsetUrl::DocBook(LString &file)
196 {
197         file +=  "<ulink url=\""  + getContents() + "\">" +
198                  getOptions() +"</ulink>";
199
200         return 0;
201 }
202
203
204 void InsetUrl::Validate(LaTeXFeatures& features) const
205 {
206         features.url = true;
207 }