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