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