]> git.lyx.org Git - lyx.git/blob - src/LyXSendto.C
the export patch from Dekel
[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 #include "bufferview_funcs.h"
16 #include "exporter.h"
17
18 extern FD_form_sendto * fd_form_sendto;
19 extern BufferView * current_view;
20 extern int MakeLaTeXOutput(Buffer * buffer);
21 extern bool CreatePostscript(Buffer * buffer, bool wait);
22
23 // Whereas this feature is under the menu item File->Export->Custom,
24 // I kept the old name sendto in the code because I am lazy (JMarc)
25
26 void MenuSendto()
27 {
28     static int ow = -1;
29     static int oh;
30
31     // do this only if the command is empty
32     if (!fl_get_input(fd_form_sendto->input_cmd) &&
33         !lyxrc.custom_export_command.empty())
34         fl_set_input(fd_form_sendto->input_cmd,
35                      lyxrc.custom_export_command.c_str());
36     if (fd_form_sendto->form_sendto->visible) {
37         fl_raise_form(fd_form_sendto->form_sendto);
38     } else {  
39         fl_show_form(fd_form_sendto->form_sendto,
40                      FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER,
41                      _("Send Document to Command"));
42         if (ow < 0) {
43                 ow = fd_form_sendto->form_sendto->w;
44                 oh = fd_form_sendto->form_sendto->h;
45         }
46         fl_set_form_minsize(fd_form_sendto->form_sendto, ow, oh);
47     }
48 }
49
50
51 void SendtoApplyCB(FL_OBJECT *, long)
52 {
53     if (!current_view->available())
54         return;
55
56     string command = fl_get_input(fd_form_sendto->input_cmd);
57     if (command.empty())
58         return;
59     Buffer * buffer = current_view->buffer();
60 #ifndef NEW_EXPORT
61     if (fl_get_button(fd_form_sendto->radio_ftype_dvi) ||
62         fl_get_button(fd_form_sendto->radio_ftype_ps)) {
63         ProhibitInput(current_view);
64         // Generate dvi file and check if there are errors in the .lyx file
65         if (MakeLaTeXOutput(buffer) > 0) {
66             AllowInput(current_view);
67             return;
68         }
69         AllowInput(current_view);
70     }
71 #endif
72     string ftypeext;
73     if (fl_get_button(fd_form_sendto->radio_ftype_lyx))
74         ftypeext = ".lyx";
75     else if (fl_get_button(fd_form_sendto->radio_ftype_latex))
76         ftypeext = ".tex";
77     else if (fl_get_button(fd_form_sendto->radio_ftype_dvi))
78         ftypeext = ".dvi";
79     else if (fl_get_button(fd_form_sendto->radio_ftype_ascii))
80         ftypeext = ".txt";
81     else {
82 #ifdef NEW_EXPORT
83         ftypeext = ".ps";
84         if (!Exporter::Export(buffer, "ps", true))
85                 return;
86 #else
87         ftypeext = ".ps_tmp";
88         if (!CreatePostscript(buffer, true)) {
89             return;
90         }
91 #endif
92     }
93
94     string fname = OnlyFilename(ChangeExtension(buffer->getLatexName(),
95                                                 ftypeext));
96     if (!contains(command, "$$FName"))
97         command = "( " + command + " ) <$$FName";
98     command = subst(command, "$$FName", fname);
99     command += " &"; // execute in background
100     // push directorypath, if necessary 
101     string path = OnlyPath(buffer->fileName());
102     if (lyxrc.use_tempdir || (IsDirWriteable(path) < 1)){
103         path = buffer->tmppath;
104     }
105     Path p(path);
106     // save the .lyx file in tmp_dir if this filetype is requested
107     if (fl_get_button(fd_form_sendto->radio_ftype_lyx))
108         buffer->writeFile(fname, true);
109     // if the .tex file is requested save it to the tempdir
110     // as now we don't do the MakeLaTeXOutput anymore
111     if (fl_get_button(fd_form_sendto->radio_ftype_latex))
112         buffer->makeLaTeXFile(fname, path, false);
113     // create the .txt file in tmp_dir if this filetype is requested
114     if (fl_get_button(fd_form_sendto->radio_ftype_ascii))
115         buffer->writeFileAscii(fname, lyxrc.ascii_linelen);
116     Systemcalls one(Systemcalls::System, command);    
117 }
118
119
120 void SendtoCancelCB(FL_OBJECT *, long)
121 {
122     fl_hide_form(fd_form_sendto->form_sendto);
123 }
124
125
126 void SendtoOKCB(FL_OBJECT * ob, long data)
127 {
128     SendtoCancelCB(ob, data);
129     SendtoApplyCB(ob, data);
130 }