]> git.lyx.org Git - lyx.git/blob - src/LyXSendto.C
271c6a33be599e7b82474bac434d5a44dc6e382f
[lyx.git] / src / LyXSendto.C
1 #include <config.h>
2
3 #include FORMS_H_LOCATION
4 #include "print_form.h"
5 #include "lyx_main.h"
6 #include "lyxrc.h"
7 #include "LString.h"
8 #include "support/filetools.h"
9 #include "support/path.h"
10 #include "buffer.h"
11 #include "lyx_gui_misc.h"
12 #include "support/syscall.h"
13 #include "gettext.h"
14 #include "lyx_cb.h"
15
16 extern FD_form_sendto *fd_form_sendto;
17 extern BufferView *current_view;
18 extern int MakeDVIOutput(Buffer *buffer);
19 extern bool MenuRunDvips(Buffer *buffer, bool wait);
20
21 // Whereas this feature is under the menu item File->Export->Custom,
22 // I kept the old name sendto in the code because I am lazy (JMarc)
23
24 void MenuSendto()
25 {
26     // do this only if the command is empty
27     if (!fl_get_input(fd_form_sendto->input_cmd) &&
28         !lyxrc->custom_export_command.empty())
29         fl_set_input(fd_form_sendto->input_cmd,
30                      lyxrc->custom_export_command.c_str());
31     if (fd_form_sendto->form_sendto->visible) {
32         fl_raise_form(fd_form_sendto->form_sendto);
33     } else {  
34         fl_show_form(fd_form_sendto->form_sendto,
35                      FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER,
36                      _("Send Document to Command"));
37     }
38 }
39
40 void SendtoApplyCB(FL_OBJECT *, long)
41 {
42     if (!current_view->available())
43         return;
44
45     string command = fl_get_input(fd_form_sendto->input_cmd);
46     if (command.empty())
47         return;
48     Buffer *buffer = current_view->currentBuffer();
49     if (fl_get_button(fd_form_sendto->radio_ftype_dvi) ||
50         fl_get_button(fd_form_sendto->radio_ftype_ps)) {
51         ProhibitInput();
52         // Generate dvi file and check if there are errors in the .lyx file
53         if (MakeDVIOutput(buffer) > 0) {
54             AllowInput();
55             return;
56         }
57         AllowInput();
58     }
59     string ftypeext;
60     if (fl_get_button(fd_form_sendto->radio_ftype_lyx))
61         ftypeext = ".lyx";
62     else if (fl_get_button(fd_form_sendto->radio_ftype_latex))
63         ftypeext = ".tex";
64     else if (fl_get_button(fd_form_sendto->radio_ftype_dvi))
65         ftypeext = ".dvi";
66     else if (fl_get_button(fd_form_sendto->radio_ftype_ascii))
67         ftypeext = ".txt";
68     else {
69         ftypeext = ".ps_tmp";
70         if (!MenuRunDvips(buffer, true)) {
71             return;
72         }
73     }
74     string fname = SpaceLess(ChangeExtension(buffer->getFileName(), 
75                                               ftypeext, true));
76     if (!contains(command, "$$FName"))
77         command = "( " + command + " ) <$$FName";
78     subst(command, "$$FName",fname);
79     command += " &"; // execute in background
80     // push directorypath, if necessary 
81     string path = OnlyPath(buffer->getFileName());
82     if (lyxrc->use_tempdir || (IsDirWriteable(path) < 1)){
83         path = buffer->tmppath;
84     }
85     Path p(path);
86     // save the .lyx file in tmp_dir if this filetype is requested
87     if (fl_get_button(fd_form_sendto->radio_ftype_lyx))
88         buffer->writeFile(fname,true);
89     // if the .tex file is requested save it to the tempdir
90     // as now we don't do the MakeDVIOutput anymore
91     if (fl_get_button(fd_form_sendto->radio_ftype_latex))
92         buffer->makeLaTeXFile(fname,path,false);
93     // create the .txt file in tmp_dir if this filetype is requested
94     if (fl_get_button(fd_form_sendto->radio_ftype_ascii))
95         buffer->writeFileAscii(fname, lyxrc->ascii_linelen);
96     Systemcalls one(Systemcalls::System, command);    
97 }
98
99 void SendtoCancelCB(FL_OBJECT *, long)
100 {
101     fl_hide_form(fd_form_sendto->form_sendto);
102 }
103
104 void SendtoOKCB(FL_OBJECT *ob, long data)
105 {
106     SendtoCancelCB(ob,data);
107     SendtoApplyCB(ob,data);
108 }