]> git.lyx.org Git - lyx.git/blob - src/frontends/Liason.C
controller-view split of FormLog and FormVCLog.
[lyx.git] / src / frontends / Liason.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *        
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2000 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "Liason.h"
18 #include "LyXView.h"
19 #include "BufferView.h"
20 #include "buffer.h"
21 #include "lyxrc.h"
22 #include "PrinterParams.h"
23 #include "lyx_gui_misc.h"
24 #include "support/lstrings.h"
25 #include "support/filetools.h"
26 #include "support/path.h"
27 #include "exporter.h"
28 #include "converter.h"
29 #include "minibuffer.h"
30 #include "support/syscall.h"
31
32 using std::endl;
33
34 extern LyXRC lyxrc;
35
36 namespace Liason {
37
38 PrinterParams getPrinterParams(Buffer * buffer)
39 {
40         return PrinterParams(PrinterParams::PRINTER,
41                              lyxrc.printer,
42                              ChangeExtension(buffer->fileName(),
43                                              lyxrc.print_file_extension));
44 }
45
46
47 bool printBuffer(Buffer * buffer, PrinterParams const & pp) 
48 {
49         string command(lyxrc.print_command + ' ');
50         
51         if (pp.target == PrinterParams::PRINTER
52             && lyxrc.print_adapt_output  // dvips wants a printer name
53             && !pp.printer_name.empty()) {// printer name given
54                 command += lyxrc.print_to_printer
55                         + pp.printer_name
56                         + ' ';
57         }
58
59         switch (pp.which_pages) {
60         case PrinterParams::EVEN:
61                 command += lyxrc.print_evenpage_flag + ' ';
62                 break;
63
64         case PrinterParams::ODD:
65                 command += lyxrc.print_oddpage_flag + ' ';
66                 break;
67     
68         default:
69                 // only option left is print all of them
70                 break;
71         }
72
73         if (!pp.from_page.empty()) {
74                 command += lyxrc.print_pagerange_flag + ' ';
75                 command += pp.from_page;
76                 if (pp.to_page) {
77                                 // we have a range "from-to"
78                         command += '-';
79                         command += tostr(pp.to_page);
80                 }
81                 command += ' ';
82         }
83
84         if (pp.reverse_order) {
85                 command += lyxrc.print_reverse_flag + ' ';
86         }
87
88         if (1 < pp.count_copies) {
89                 if (pp.unsorted_copies) {
90                         command += lyxrc.print_copies_flag;
91                 } else {
92                         command += lyxrc.print_collcopies_flag;
93                 }
94                 command += ' ';
95                 command += tostr(pp.count_copies);
96                 command += ' ';
97         }
98
99         if (!lyxrc.print_extra_options.empty()) {
100                 command += lyxrc.print_extra_options + ' ';
101         }
102
103         command += converters.dvips_options(buffer) + ' ';
104
105         if (!Exporter::Export(buffer, "dvi", true))
106                 return false;
107
108         // Push directory path.
109         string path = OnlyPath(buffer->fileName());
110         if (lyxrc.use_tempdir || (IsDirWriteable(path) < 1)) {
111                 path = buffer->tmppath;
112         }
113         Path p(path);
114
115         // there are three cases here:
116         // 1. we print to a file
117         // 2. we print direct to a printer
118         // 3. we print using a spool command (print to file first)
119         Systemcalls one;
120         int res = 0;
121         string dviname = ChangeExtension(buffer->getLatexName(true), "dvi");
122         switch (pp.target) {
123         case PrinterParams::PRINTER:
124                 if (!lyxrc.print_spool_command.empty()) {
125                         // case 3
126                         string psname = ChangeExtension(dviname, ".ps");
127                         command += lyxrc.print_to_file
128                                 + QuoteName(psname) + ' ';
129                         command += QuoteName(dviname);
130                         string command2 = lyxrc.print_spool_command + ' ';
131                         if (!pp.printer_name.empty())
132                                 command2 += lyxrc.print_spool_printerprefix
133                                         + pp.printer_name + ' ';
134                         command2 += QuoteName(psname);
135                         // First run dvips.
136                         // If successful, then spool command
137                         res = one.startscript(Systemcalls::System, command);
138                         if (res == 0)
139                                 res = one.startscript(Systemcalls::SystemDontWait,
140                                                       command2);
141                 } else
142                         // case 2
143                         res = one.startscript(Systemcalls::SystemDontWait, command);
144                 break;
145
146         case PrinterParams::FILE:
147                 // case 1
148                 command += lyxrc.print_to_file
149                         + QuoteName(MakeAbsPath(pp.file_name, path));
150                 command += ' ' + QuoteName(dviname);
151                 res = one.startscript(Systemcalls::SystemDontWait, command);
152                 break;
153         }
154         return res == 0;
155 }
156
157 void setMinibuffer(LyXView * lv, char const * msg)
158 {
159         lv->getMiniBuffer()->Set(msg);
160 }
161
162 } // namespace Liason