source: trunk/SRC/Postscript/printps.pro

Last change on this file was 495, checked in by pinsard, 10 years ago

fix thanks to coding rules; typo; dupe empty lines; trailing blanks

  • Property svn:keywords set to Id
File size: 14.0 KB
Line 
1;+
2;
3; @file_comments
4; - archiving possibilities -
5;
6; if archive_ps (common variable of cm_4ps) ne 0 then the postscript
7; can be saved for archiving if it is printed or if the button
8; "archive ps" is pressed. if it is printed and archive_ps = 1 then
9; the archiving is done automatically whereas we ask if the postscript
10; file must be archived or not.
11; If the postscript name is "idl.ps" (default name) then this name will
12; be changed to number.ps (number automatically found to be 1 larger
13; that any of the existing ps file).
14;
15; @categories
16; Postscripts
17;
18; @param event
19;
20; @restrictions
21;
22; 1) this is working only with unix/linux/osX machines
23;
24; 2) definition of the printing command
25;
26; the printing command is defined by the common variable "print_command"
27; in cm_4ps. This command must be defined/build in a way that it the
28; instruction:
29;     print_command[i]+printer_machine_names[i]+' '+file.ps
30; or
31;     print_command+printer_machine_names[i]+' '+file.ps
32; is working. default definition is '\lpr -P'
33;
34; @history
35; Sebastien Masson (smasson\@lodyc.jussieu.fr)
36;                       21/12/98
37;                       25/8/19999 utilisation des widgets
38;                       8/9/1999 utilisation de cw_bgroup
39; June 2005: Sebastien Masson: cleaning, English version with new commons
40;
41; @version
42; $Id$
43;
44;-
45PRO printps_event, event, uval = uval
46;
47  compile_opt idl2, strictarrsubs
48;
49@cm_4ps
50  IF NOT keyword_set(key_forgetold) THEN BEGIN
51@updatenew
52  ENDIF
53;------------------------------------------------------------
54; What kind of event do we have ?
55  IF NOT keyword_set(uval) THEN widget_control, event.id, get_uvalue = uval
56; case on the event
57  CASE uval.name OF
58;------------------------------------------------------------
59; visualize case : postscript visualization
60;------------------------------------------------------------
61    'visualize':BEGIN
62; paper orientation
63      if key_portrait EQ 1 then ori = 'portrait' ELSE ori = 'seascape'
64; paper format
65      CASE round(10*(total(page_size))) OF
66        round(10*(83.9611 + 118.816)) : papsize = 'a0'
67        round(10*(59.4078 + 83.9611)) : papsize = 'a1'
68        round(10*(41.9806 + 59.4078)) : papsize = 'a2'
69        round(10*(29.7039 + 41.9806)) : papsize = 'a3'
70        round(10*(20.9903 + 29.7039)) : papsize = 'a4'
71        round(10*(14.8519 + 20.9903)) : papsize = 'a5'
72        round(10*(10.4775 + 14.8519)) : papsize = 'a6'
73        round(10*(7.40833 + 10.4775)) : papsize = 'a7'
74        round(10*(5.22111 + 7.40833)) : papsize = 'a8'
75        round(10*(3.70417 + 5.22111)) : papsize = 'a9'
76        round(10*(2.61056 + 3.70417)) : papsize = 'a10'
77        round(10*(100.048 + 141.393)) : papsize = 'b0'
78        round(10*(70.6967 + 100.048)) : papsize = 'b1'
79        round(10*(50.0239 + 70.6967)) : papsize = 'b2'
80        round(10*(35.3483 + 50.0239)) : papsize = 'b3'
81        round(10*(25.0119 + 35.3483)) : papsize = 'b4'
82        round(10*(17.6742 + 25.0119)) : papsize = 'b5'
83        round(10*(22.86 + 30.48)) : papsize = 'archA'
84        round(10*(30.48 + 45.72)) : papsize = 'archB'
85        round(10*(45.72 + 60.96)) : papsize = 'archC'
86        round(10*(60.96 + 91.44)) : papsize = 'archD'
87        round(10*(91.44 + 121.92)) : papsize = 'archE'
88        round(10*(21.59 + 33.02)) : papsize = 'flsa'
89        round(10*(21.59 + 33.02)) : papsize = 'flse'
90        round(10*(13.97 + 21.59)) : papsize = 'halfletter'
91        round(10*(19.05 + 25.4)) : papsize = 'note'
92        round(10*(21.59 + 27.94)) : papsize = 'letter'
93        round(10*(21.59 + 35.56)) : papsize = 'legal'
94        round(10*(27.94 + 43.18)) : papsize = '11x17'
95        round(10*(43.18 + 27.94)) : papsize = 'ledger'
96        ELSE:papsize = 'a4'
97      ENDCASE
98; call the viewers
99      CASE event.value OF
100        'GNU Ghostview':cmd = '\gv --media='+papsize+' --orientation='+ori+' '+uval.nameps
101        'Gnome Ghostview':cmd = '\ggv '+uval.nameps
102        'Ghostview':cmd = '\ghostview -'+papsize+' -quiet -'+ori+' '+uval.nameps
103        'Ghostscript':cmd = '\gs -sPAPERSIZE='+papsize+' -q '+uval.nameps
104        'Kghostview':cmd = '\kghostview '+uval.nameps
105      ENDCASE
106      spawn, cmd
107      return
108    END
109;------------------------------------------------------------
110; print case: print and archive the file if needed
111;------------------------------------------------------------
112    'print':BEGIN
113; printer selection
114      printer = printer_machine_names[event.value]
115; print
116      CASE n_elements(print_command) OF
117        0:ptcmd = '\lpr -P'
118        1:ptcmd = print_command[0]
119        n_elements(printer_machine_names):ptcmd = print_command[event.value]
120        ELSE:BEGIN
121          ng = report('bad definition of print_command, common variable of cm_4ps. !C we did not print the postscript file', /simple)
122          return
123        END
124      ENDCASE
125      spawn, ptcmd + printer + ' ' + uval.nameps
126; ; printing informations...
127;       spawn, '\lpq -P'+imprimante+' -l', info
128; ; display them
129;       xdisplayfile, 'nothing', text = info $
130;                     , title = 'Printing Info '+file_basename(uval.nameps) $
131;                     , height = n_elements(info) <  24
132    END
133    ELSE:
134  ENDCASE
135; we destroy the widget
136  IF n_tags(event) GT 1 THEN widget_control, event.top, /destroy
137; if the file was originally gzipped, then we re-gzip it and exit
138  IF uval.gzip THEN BEGIN
139    spawn, '\gzip -f ' + uval.nameps
140    return
141  ENDIF
142;------------------------------------------------------------
143; archiving...
144;------------------------------------------------------------
145  IF (uval.name EQ 'print' OR uval.name EQ 'archive') $
146    AND keyword_set(archive_ps) THEN BEGIN
147    IF archive_ps NE 1 AND uval.name EQ 'print' then begin
148      wesave = report('Shall we archive the postscript?', /default_no, /question)
149      IF wesave EQ 0 THEN RETURN
150    ENDIF
151; if the name of the postscript is idl.ps then we change it
152    IF file_basename(uval.nameps) EQ 'idl.ps' then BEGIN
153; get the name of all the *.ps or *.ps.gz files available in psdir
154       allps = file_search(psdir+'*[.ps|.ps.gz|.pdf]', /test_regular, /nosort)
155       allps = file_basename(file_basename(allps,'.gz'),'.ps')
156       allps = file_basename(allps,'.pdf')
157; find which of these names corresponds to numbers...
158; get ASCII codes of the names
159       testnumb = byte(allps)
160; longest name
161       maxstrlen = (size(testnumb, /dimensions))[0]
162; ASCII codes can be 0 or between byte('0') and byte('9')
163       testnumb = testnumb EQ 0 OR $
164                  (testnumb GE (byte('0'))[0] AND testnumb LE (byte('9'))[0])
165       testnumb = where(total(testnumb, 1) EQ maxstrlen, count)
166       IF count NE 0 THEN BEGIN
167; get the largest number
168         psnumber = fix(allps[testnumb])
169         psnumber = (psnumber[reverse(sort(psnumber))])[0] + 1
170       ENDIF ELSE psnumber = 0
171; update uval.nameps
172      dirname = file_dirname(uval.nameps, /mark_directory)
173      uval.nameps = dirname+strtrim(psnumber, 2)+'.ps'
174; change the name of the file
175      file_move, dirname+'idl.ps', uval.nameps
176    ENDIF
177    spawn, '\gzip -f ' + uval.nameps
178  ENDIF
179;
180  return
181end
182;------------------------------------------------------------
183;------------------------------------------------------------
184;+
185; @file_comments
186; postscript visualisation/archiving/printing
187;
188; @categories
189; Postscripts
190;
191; @param psfilename {in}{optional}
192; The name of the postscript file we want to
193; visualize and/or print and/or archive. It can also refer
194; to a gzipped postscript file. If needed, this name will
195; be completed by .ps and/or .gz.
196;
197; @keyword ARCHIVE {default=0} {type=integer 0 or 1}
198; activate to bypass the widget and directly archive the postscript
199; (useful to use printps in batch mode)
200;
201; @keyword DIRECTPRINT {default=not defined} {type=string}
202; bypass the widget and directly define the printer that must be used
203; to print the postscript file. DIRECTPRINT must correspond to one of
204; the names defined in the common variables printer_machine_names or
205; printer_human_names. useful to use printps in batch mode
206;
207; @uses
208; <pro>cm_4ps</pro>
209;
210; @examples
211;
212;   IDL> printps
213;
214; @history
215; Sebastien Masson (smasson\@lodyc.jussieu.fr)
216;                       21/12/98
217;                       25/8/19999 utilisation des widgets
218;                       8/9/1999 utilisation de cw_bgroup
219; June 2005: Sebastien Masson: cleaning, English version with new commons
220;
221; @version
222; $Id$
223;
224;-
225PRO printps, psfilename, ARCHIVE = archive, DIRECTPRINT = directprint
226;
227; this is working only with unix/linux/osX machines
228;
229  compile_opt idl2, strictarrsubs
230;
231  thisOS = strupcase(strmid(!version.os_family, 0, 3))
232  CASE thisOS OF
233    'MAC':return
234    'WIN':return
235    ELSE:
236  ENDCASE
237;
238@cm_4ps
239  IF NOT keyword_set(key_forgetold) THEN BEGIN
240@updatenew
241  ENDIF
242;------------------------------------------------------------
243; we get the name of the latest created postscript.
244;------------------------------------------------------------
245  psdir = isadirectory(psdir, title = 'Select psdir')
246  CASE N_PARAMS() OF
247    0: BEGIN
248      nameps = file_search(psdir+'*.ps', /test_regular, /nosort)
249      IF nameps[0] EQ '' THEN BEGIN
250        ras = report('no postscript file (ending with .ps) found in : ' + psdir)
251        RETURN
252      ENDIF
253      dates = (file_info(nameps)).mtime
254      lastdate = (reverse(sort(temporary(dates))))[0]
255      nameps = nameps[lastdate]
256    END
257    1: nameps = psfilename
258    ELSE: BEGIN
259      ras = report('printps accept only one element: psfilename')
260      RETURN
261    END
262  ENDCASE
263;------------------------------------------------------------
264; we check if the file is exist in psdir. if necessary we complete
265; its name with .ps and/or ,gz
266;------------------------------------------------------------
267  nameps = find(nameps+'{.ps,}{.gz,}', iodir = psdir, /nopro)
268  nameps = nameps[0]
269  IF nameps EQ 'NOT FOUND' THEN BEGIN
270    ng = report('file '+nameps+'[.ps][.gz] does not exist ')
271    return
272  ENDIF
273  gzipped = strpos(nameps, '.gz')
274; if the file is gzipped we call gunzip and change its name
275  IF gzipped NE -1 AND NOT keyword_set(archive) THEN BEGIN
276    spawn, '\gunzip -f ' + nameps
277    nameps = strmid(nameps, 0, gzipped)
278  ENDIF
279;------------------------------------------------------------
280; what should we do?
281;------------------------------------------------------------
282  CASE 1 OF
283;------------------------------------------------------------
284; direct archiving...
285;------------------------------------------------------------
286    keyword_set(archive):IF gzipped EQ -1 THEN printps_event, uval = {name:'archive', nameps:nameps, gzip:gzipped NE -1}
287;------------------------------------------------------------
288; build the widget
289;------------------------------------------------------------
290    keyword_set(directprint): BEGIN
291; check if directprint is well defined
292      printnumb = (where(strlowcase(printer_machine_names) EQ strlowcase(directprint)))[0]
293      IF printnumb EQ -1 THEN BEGIN
294        printnumb = (where(strlowcase(printer_human_names) EQ strlowcase(directprint)))[0]
295        IF printnumb EQ -1 THEN BEGIN
296          dummy = report('directprint does not correspond to any printer defined in printer_machine_names or printer_human_names common variable')
297          return
298        ENDIF
299      ENDIF
300; call printps_event with predefined event and uvalue
301      printps_event, {value:printnumb}, uval = {name:'print', nameps:nameps, gzip:gzipped NE -1}
302    END
303;------------------------------------------------------------
304; build the widget
305;------------------------------------------------------------
306    ELSE:BEGIN
307      IF keyword_set(key_batch) THEN return
308      base = widget_base(/row, title = 'Postscript file: ' $
309                         + file_basename(nameps))
310; ps viewers grouped button
311      psviewers = ['no psviewers found']
312      IF file_which(getenv('PATH'), 'gv') NE '' THEN $
313         psviewers = [psviewers, 'GNU Ghostview']
314      IF file_which(getenv('PATH'), 'ggv') NE '' THEN $
315         psviewers = [psviewers, 'Gnome Ghostview']
316      IF file_which(getenv('PATH'), 'ghostview') NE '' THEN $
317         psviewers = [psviewers, 'Ghostview']
318      IF file_which(getenv('PATH'), 'gs') NE '' THEN $
319         psviewers = [psviewers, 'Ghostscript']
320      IF file_which(getenv('PATH'), 'kghostview') NE '' THEN $
321         psviewers = [psviewers, 'Kghostview']
322; if at least one of viewer was found we define these buttons
323      IF n_elements(psviewers) GT 1 THEN BEGIN
324        psviewers = psviewers[1:*]
325        notused = cw_bgroup(base, psviewers, /frame, label_top = 'Visualize' $
326                            , uvalue = {name:'visualize', nameps:nameps} $
327                            , /column, /return_name)
328      ENDIF
329; printers list grouped buttons
330; are the common variables printer_human_names and printer_human_names
331; defined in a proper way?
332      CASE 1 OF
333        n_elements(printer_human_names) eq 0: $
334           noting = report('the cm_4ps variable printer_human_names is not defined... !CWe could not propose any printer', /simple)
335        n_elements(printer_human_names) NE n_elements(printer_machine_names): $
336           noting = report('the cm_4ps variables printer_human_names and !Cprinter_machine_names do not have the same number of arguments... !CWe could not propose any printer', /simple)
337        printer_human_names[0] EQ '':
338        ELSE:notused = $
339           cw_bgroup(base, printer_human_names, /frame, /column $
340                     , label_top = 'Select printer' $
341                     , uvalue = {name:'print', nameps:nameps, gzip:gzipped NE -1})
342      ENDCASE
343; 'archive ps' button, can be created only if archive_ps ne 0
344      IF keyword_set(archive_ps) THEN $
345         notused = widget_button(base, value = 'archive ps' $
346                                 , uvalue = {name:'archive', nameps:nameps $
347                                             , gzip:gzipped NE -1})
348; quit button
349      notused = widget_button(base, value = 'quit' $
350                              , uvalue = {name:'quit', nameps:nameps $
351                                          , gzip:gzipped NE -1})
352;------------------------------------------------------------
353      widget_control, base, /realize
354;------------------------------------------------------------
355      xmanager, 'printps', base, /no_block
356;------------------------------------------------------------
357    END
358  ENDCASE
359
360  return
361end
Note: See TracBrowser for help on using the repository browser.