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