Debian Codegraph @

texk/texlive/linked_scripts/epspdf/epspdftk.tcl

raw
1 #!/usr/bin/env wish
2
3 # epspdf conversion utility, GUI frontend
4
5 #####
6 # Copyright (C) 2006-2020 Siep Kroonenberg
7 # siepo at bitmuis dot nl
8 #
9 # This program is free software, licensed under the GNU GPL, >=2.0.
10 # This software comes with absolutely NO WARRANTY. Use at your own risk!
11 #####
12
13 package require Tk
14
15 if {[string range $::tcl_version 0 1] eq "8." &&
16 [string index $::tcl_patchLevel 2] < 5} {
17 tk_messageBox -message "Tcl/Tk version >= 8.5 required;\naborting..."
18 exit
19 }
20
21 # tired of writing out this combined test again and again
22 set classic_unix [expr {$::tcl_platform(platform) eq "unix" && \
23 $::tcl_platform(os) ne "Darwin"}]
24
25 # normally, epspdf.tlu should be in the same directory
26 # and texlua should be on the searchpath.
27 # However, the Windows installer version wraps this script in a starpack.
28
29 # logging: to a log window, not to a file
30 proc write_log {s} {
31 if {[winfo exists .log_t.text]} {
32 .log_t.text configure -state normal
33 .log_t.text insert end "$s\n"
34 .log_t.text yview moveto 1
35 .log_t.text configure -state disabled
36 # } else {
37 # puts $s
38 }
39 }
40
41 ### invoking epspdf.tlu #########################
42
43 # Get full path of epspdf.tlu. It should be in the same directory as
44 # either this script or of the starpack containing this script,
45 # For non-windows versions, epspdftk might be called via a symlink.
46 # Pdftops done elsewhere.
47
48 proc set_progs {} {
49 set scriptfile [file normalize [info script]]
50 set eproot [file dirname $scriptfile]
51 # if symlink, get the directory of the file it points to
52 if {! [catch {file readlink $scriptfile}]} {
53 # evaluate readlink from symlink directory
54 set savedir [pwd]
55 cd $eproot
56 set eproot [file dirname [file normalize [file readlink $scriptfile]]]
57 cd $savedir
58 }
59 # find the lua script
60 set ::epspdf_tlu [file join $eproot "epspdf.tlu"]
61 if {! [file exists $::epspdf_tlu]} {
62 # if starpack, look in binary directory
63 set eproot [file dirname [file normalize [info nameofexecutable]]]
64 set ::epspdf_tlu [file join $eproot "epspdf.tlu"]
65 }
66 if {$::tcl_platform(platform) eq "windows"} {
67 # For actual conversions, epspdf invokes third-party command-line programs.
68 # To prevent the appearing of transient black console windows for such
69 # invocations, epspdf will be invoked via a batchfile.
70 # This is not necessary for getting info about a file.
71 set ::epspdf_cmd [file join $eproot "epspdf4tk.cmd"]
72 }
73 if {! [file exists $::epspdf_tlu]} {
74 tk_messageBox -type ok -icon error -message "Epspdf.tlu not found"
75 exit 1
76 }
77 # texlua should be on the searchpath
78 set ::texlua "texlua"
79
80 # icon for starpack: add with sdx or with Windows utilities
81 }
82
83 set_progs
84
85 ### configured and automatic settings ##################################
86
87 # is_prog used for checking configured viewers under non-osx unix
88 proc is_prog {x} {
89 if {[expr {$::tcl_platform(platform) ne "unix"}]} {return 0}
90 # avoid current directory except with explicit directory
91 if {[expr {[string first "/" $x] >= 0 && \
92 [file executable [file normalize $x]]}]} {
93 return 1
94 }
95 # also check search path
96 set p [split $::env(PATH) ":"] ; # no need to accomodate msw
97 foreach d $p {
98 if {[expr {$d ne "" && [file executable [file join $d $x]]}]} {
99 return 1
100 }
101 }
102 return 0
103 }
104
105 # create a global empty settings array
106 array set ::settings [list]
107
108 # ask epspdf.tlu for currently configured settings.
109 # this does not include automatically configured or transient settings.
110 # the availability of viewers is handled here.
111 proc getsettings {} {
112 if [catch {exec $::texlua $::epspdf_tlu --gui=config_w} set_str] {
113 error "Epspdf configuration error: $set_str"
114 }
115 # write_log "settings from epspdf.tlu:\n$set_str\n"
116 set l [split $set_str "\r\n"]
117 foreach e $l {
118 # puts "settings: $e"
119 # $e is either a string "var = value"
120 # or the empty string between <cr> and <lf>
121 set i [string first "=" $e]
122 if {$i>0} {
123 # write_log $e
124 set para [string trim [string range $e 0 [expr $i-1]]]
125 set val [string trim [string range $e [expr $i+1] end]]
126 if {$val eq "true"} {set val 1}
127 if {$val eq "false"} {set val 0}
128 set ::settings($para) $val
129 # write_log "setting $para is $val"
130 }
131 }
132
133 # unix: viewer settings
134 # It may depend on installed plugins whether a pdf viewer
135 # can render postscript.
136 # AFAIK, no such plugins exist for xpdf or mupdf.
137 # chrome and firefox also only render pdf and not postscript.
138 # configured viewer, if valid, heads the list
139 if {$::classic_unix} {
140
141 set ::pdf_viewers {}
142 if {$::settings(pdf_viewer) ne "" && [is_prog $::settings(pdf_viewer)]} {
143 lappend ::pdf_viewers $::settings(pdf_viewer)
144 }
145 foreach v {
146 atril evince okular qpdfview mupdf xpdf zathura gv firefox chrome \
147 chromium chromium-browser} {
148 if {$v ne $::settings(pdf_viewer) && [is_prog $v]} {
149 lappend ::pdf_viewers $v
150 }
151 }
152 # puts [join $::pdf_viewers " "]
153
154 set ::ps_viewers {}
155 if {$::settings(ps_viewer) ne "" && [is_prog $::settings(ps_viewer)]} {
156 lappend ::ps_viewers $::settings(ps_viewer)
157 }
158 foreach v $::pdf_viewers {
159 if {$v ne $::settings(ps_viewer) && \
160 $v ni {xpdf mupdf firefox chrome chromium chromium-browser}} {
161 lappend ::ps_viewers $v
162 }
163 }
164 # puts [join $::ps_viewers " "]
165
166 if {[llength ::pdf_viewers] == 0} {
167 tk_messageBox -message "No viewers found"
168 } elseif {[llength ::ps_viewers] == 0} {
169 tk_messageBox -message "No PostScript viewers found"
170 }
171 if {[llength ::pdf_viewers] > 0} {
172 set ::settings(pdf_viewer) [lindex $::pdf_viewers 0]
173 }
174 if {[llength ::ps_viewers] > 0} {
175 set ::settings(ps_viewer) [lindex $::ps_viewers 0]
176 }
177 }
178 }
179
180 getsettings
181
182 proc write_settings {} {
183 set s ""
184 foreach el [array names ::settings] {
185 set s "$s$el = $::settings($el)\n"
186 }
187 # write_log "\nsettings for epspdf.tlu\n$s\nend writing settings\n"
188 if [catch {exec $::texlua $::epspdf_tlu --gui=config_r << $s} result] {
189 error "Epspdf configuration error: $result"
190 }
191 }
192
193 # directory and other file data
194
195 if {$::argc > 0 && [file isdirectory [lindex $::argv 0]]} {
196 set ::gfile(dir) [lindex $::argv 0]
197 } elseif {[file isdirectory $::settings(default_dir)]} {
198 set ::gfile(dir) $::settings(default_dir)
199 } else {
200 set ::gfile(dir) $::env(HOME)
201 }
202 set ::gfile(path) ""
203 set ::gfile(type) ""
204 set ::gfile(name) ""
205 set ::gfile(npages) ""
206
207 # transient options
208 array set ::options [list gray "color" format "pdf" bbox 0 clean 1 \
209 pages "single" page 1]
210
211 proc viewable {} {
212 if { $::gfile(name) eq "" || ! [file exists $::gfile(path)] || \
213 $::gfile(type) eq "other" || $::gfile(type) eq ""} {
214 return 0
215 } elseif {! $::classic_unix} {
216 return 1
217 } elseif {$::settings(ps_viewer) ne "" && [regexp {ps} $::gfile(type)]} {
218 return 1
219 } elseif {$::settings(pdf_viewer) ne "" && $::gfile(type) eq "pdf"} {
220 return 1
221 } else {
222 return 0
223 }
224 }
225
226 ### groundwork for GUI #################################
227
228 # padding for buttons and frames
229 proc ppack {b args} {pack $b {*}$args -padx 3 -pady 3}
230
231 # bold font
232 font create bfont {*}[font configure TkDefaultFont]
233 font configure bfont -weight bold
234
235 proc update_combo w {
236 # check that a manually supplied entry is actually a program
237 # proc used for postscript- and pdf viewers
238 set vls [$w cget -values]
239 set new [$w get]
240 if {$new ni $vls} {
241 if {[is_prog $new]} {
242 set vls [linsert $vls 0 $new]
243 $w configure -values $vls
244 } else {
245 tk_messageBox -title Error -icon error -message "$new not a program"
246 raise .config_t
247 focus $w
248 }
249 }
250 }
251
252 proc revert_combo w {
253 # aborts entry of new value
254 if {[$w current] < 0} {
255 $w current 0
256 }
257 }
258
259 ### and now the actual GUI ###################################
260
261 wm title . "PostScript- and pdf conversions"
262
263 # help toplevel
264
265 proc readhelp {} {
266 .help_t.text configure -state normal
267 # this also works for the starpack:
268 set helpfile [file join [file dirname $::epspdf_tlu] "epspdf.help"]
269 if {! [file exists $helpfile]} {
270 # helpfile in starpack
271 set helpfile [file join [file dirname [info script]] "epspdf.help"]
272 }
273 if {[catch {set fid [open $helpfile r]}]} {
274 .help_t.text insert end "No helpfile $helpfile found\n"
275 } else {
276 while {[gets $fid line] >= 0} {.help_t.text insert end "$line\n"}
277 close $fid
278 }
279 .help_t.text configure -state disabled
280 .help_t.text yview moveto 0
281 }
282
283 toplevel .help_t
284 wm title .help_t "EpsPdf help"
285 pack [ttk::frame .help_t.bg -padding 3] -fill both -expand 1
286 ppack [ttk::button .help_t.done -text "Close" -command {wm withdraw .help_t}] \
287 -in .help_t.bg -side bottom -anchor e -padx 50
288 text .help_t.text -wrap word -width 60 -height 20 -setgrid 1 \
289 -yscrollcommand ".help_t.scroll set"
290 ttk::scrollbar .help_t.scroll -command ".help_t.text yview"
291 pack .help_t.scroll -in .help_t.bg -side right -fill y
292 pack .help_t.text -in .help_t.bg -expand 1 -fill both
293 readhelp
294 .help_t.text configure -state disabled
295 wm withdraw .help_t
296
297 # log toplevel
298
299 toplevel .log_t
300 wm title .log_t "Epspdf log"
301 pack [ttk::frame .log_t.bg -padding 3] -fill both -expand 1
302 ppack [ttk::button .log_t.b -text "Close" -command {wm withdraw .log_t}] \
303 -in .log_t.bg -side bottom -anchor e -padx 50
304 text .log_t.text -wrap word -relief flat -height 15 -width 60 \
305 -setgrid 1 -yscrollcommand ".log_t.scroll set"
306 ttk::scrollbar .log_t.scroll -command ".log_t.text yview"
307 pack .log_t.scroll -in .log_t.bg -side right -fill y
308 pack .log_t.text -in .log_t.bg -expand 1 -fill both
309 .log_t.text configure -state disabled
310 wm withdraw .log_t
311
312 ### configure toplevel ###################
313
314 toplevel .config_t
315 wm title .config_t "Configure epspdf"
316 pack [ttk::frame .config_t.bg -padding 3] -fill both -expand 1
317
318 # The settings array is edited directly in the configuration screen:
319 # for most control widgets, we need a global external variable anyway,
320 # so we better use the global that is already there.
321 # If the configuration screen is closed via the cancel button,
322 # the original values will be re-read from disk.
323 # If it is closed via the ok button, the new settings are written to disk.
324
325 # viewers (not on windows or osx)
326
327 if {$::classic_unix} {
328 ppack [ttk::frame .config_t.viewf] -in .config_t.bg -ipadx 4 -fill x
329 grid [ttk::label .config_t.viewf.title -font bfont -text "Viewers"] \
330 -row 0 -column 0 -sticky w
331 grid [ttk::label .config_t.viewf.lb_pdf -text "Pdf"] \
332 -row 1 -column 0 -sticky w
333 grid [ttk::label .config_t.viewf.lb_ps -text "PostScript"] \
334 -row 2 -column 0 -sticky w
335 grid [ttk::combobox .config_t.viewf.pdf] -row 1 -column 1 -sticky e
336 .config_t.viewf.pdf configure -values $::pdf_viewers
337 .config_t.viewf.pdf configure -textvariable ::settings(pdf_viewer)
338 bind .config_t.viewf.pdf <Escape> {revert_combo %W}
339 bind .config_t.viewf.pdf <Return> {update_combo %W}
340 bind .config_t.viewf.pdf <FocusOut> {update_combo %W}
341 grid [ttk::combobox .config_t.viewf.ps] -row 2 -column 1 -sticky e
342 .config_t.viewf.ps configure -values $::ps_viewers
343 .config_t.viewf.ps configure -textvariable ::settings(ps_viewer)
344 bind .config_t.viewf.ps <Escape> {revert_combo %W}
345 bind .config_t.viewf.ps <Return> {update_combo %W}
346 bind .config_t.viewf.ps <FocusOut> {update_combo %W}
347 grid columnconfigure .config_t.viewf 1 -weight 1 -pad 2
348 }
349
350 # settings for conversion to pdf
351
352 ppack [ttk::frame .config_t.pdff] \
353 -in .config_t.bg -ipadx 4 -fill x -pady [list 10 0]
354 pack [ttk::label .config_t.pdff.title -font bfont -text "Conversion to pdf"] \
355 -anchor w
356
357 pack [ttk::label .config_t.pdff.l_target -text "Target use"] -anchor w
358 pack [ttk::frame .config_t.pdff.f_targets] -fill x
359 foreach t {default printer prepress screen ebook} {
360 pack [ttk::radiobutton .config_t.pdff.f_targets.$t \
361 -variable ::settings(pdf_target) \
362 -text $t -value $t] -side left -padx 2 -pady 4 -anchor w
363 }
364
365 pack [ttk::label .config_t.pdff.l_version -text "Pdf version"] -anchor w
366 pack [ttk::frame .config_t.pdff.f_version] -fill x
367 foreach t {1.2 1.3 1.4 1.5 1.6 1.7 default} {
368 regsub {\.} $t _ tp ; # replace dot in name: dots are path separators!
369 pack [ttk::radiobutton .config_t.pdff.f_version.$tp \
370 -variable ::settings(pdf_version) \
371 -text $t -value $t] -side left -padx 2 -pady 4 -anchor w
372 }
373
374 # settings for conversion to EPS and PostScript
375
376 ppack [ttk::frame .config_t.psf] \
377 -in .config_t.bg -pady [list 10 0] -ipadx 4 -fill x
378 pack [ttk::label .config_t.psf.l_ps -text "Conversion to EPS and PostScript" \
379 -font bfont] -anchor w
380
381 pack [ttk::checkbutton .config_t.psf.c \
382 -text "Use pdftops if available"] -anchor w
383 .config_t.psf.c configure -variable ::settings(use_pdftops) \
384 -onvalue 1 -offvalue 0
385
386 # buttons for closing the configuration screen
387
388 pack [ttk::frame .config_t.buttonsf] -in .config_t.bg -pady [list 10 0] -fill x
389 ppack [ttk::button .config_t.buttonsf.done -text "Done" -command putsettings] \
390 -side right
391 ppack [ttk::button .config_t.buttonsf.cancel -text "Cancel" \
392 -command cancelsettings] -side right
393
394 wm transient .config_t
395 wm overrideredirect .config_t
396 wm withdraw .config_t
397
398 proc edit_settings {} {
399 wm deiconify .config_t
400 raise .config_t
401 grab set .config_t
402 }
403
404 # store new settings
405 proc putsettings {} {
406 wm withdraw .config_t
407 grab release .config_t
408 write_settings
409 }
410
411 proc cancelsettings {} {
412 wm withdraw .config_t
413 grab release .config_t
414 # re-read config file / reg entries
415 getsettings
416 }
417
418 ### main screen #############################
419
420 proc show_w {w} {
421 wm deiconify $w
422 raise $w
423 }
424
425 pack [ttk::frame .bg -padding 3] -fill both -expand 1
426
427 # buttons to call up configure-, log- and help screens
428
429 pack [ttk::frame .topf] -in .bg -fill x
430 ppack [ttk::button .topf.config_t -text "Configure" \
431 -command edit_settings] -side left
432 ppack [ttk::button .topf.help_t -text "Help" \
433 -command {show_w .help_t}] -side right
434 ppack [ttk::button .topf.logb -text "Show log" -command {show_w .log_t}] \
435 -side right -anchor w
436
437 # file info in grid layout
438
439 ppack [ttk::frame .infof -relief sunken -border 1 -padding 3] -in .bg -fill x
440 grid [ttk::label .infof.dir_label -text "Directory" -anchor w] \
441 -row 1 -column 1 -sticky w
442 grid [ttk::label .infof.dir_value -textvariable ::gfile(dir) -anchor w] \
443 -row 1 -column 2 -sticky w
444
445 grid [ttk::label .infof.name_label -text "File" -anchor w] \
446 -row 2 -column 1 -sticky w
447 grid [ttk::label .infof.name_value -textvariable ::gfile(name) -anchor w] \
448 -row 2 -column 2 -sticky w
449
450 grid [ttk::label .infof.type_label -text "Type" -anchor w] \
451 -row 3 -column 1 -sticky w
452 grid [ttk::label .infof.type_value -textvariable ::gfile(type) -anchor w] \
453 -row 3 -column 2 -sticky w
454
455 grid [ttk::label .infof.npages_label -text "Pages" -anchor w] \
456 -row 4 -column 1 -sticky w
457 grid [ttk::label .infof.npages_value -textvariable ::gfile(npages) -anchor w] \
458 -row 4 -column 2 -sticky w
459
460 grid columnconfigure .infof 1 -weight 1 -pad 2
461 grid columnconfigure .infof 2 -weight 3 -pad 2
462
463 # conversion options
464
465 pack [ttk::frame .optsf] -in .bg -pady [list 10 0] -fill x
466
467 # grayscaling
468 pack [ttk::frame .optsf.gray] -side left -anchor nw
469 pack [ttk::label .optsf.gray.l -text "Grayscaling"] -anchor w
470 pack [ttk::radiobutton .optsf.gray.off -text "No color conversion" \
471 -variable ::options(gray) -value "color"] -anchor w
472 pack [ttk::radiobutton .optsf.gray.gray -text "Grayscale" \
473 -variable ::options(gray) -value "gray"] -anchor w
474
475 # output format
476 pack [ttk::label .optsf.format] -side right -anchor ne
477 pack [ttk::label .optsf.format.l -text "Output format"] -anchor w
478 ttk::radiobutton .optsf.format.pdf -text "pdf" \
479 -command set_widget_states -variable ::options(format) -value "pdf"
480 ttk::radiobutton .optsf.format.eps -text "eps" -command set_widget_states \
481 -variable ::options(format) -value "eps"
482 ttk::radiobutton .optsf.format.ps -text "ps" -command set_widget_states \
483 -variable ::options(format) -value "ps"
484 pack .optsf.format.pdf -anchor w
485 pack .optsf.format.eps -anchor w
486 pack .optsf.format.ps -anchor w
487
488 # boundingbox
489 pack [ttk::checkbutton .bbox -text "Compute tight boundingbox" \
490 -variable ::options(bbox) -command set_widget_states] \
491 -in .bg -anchor w -pady [list 10 0]
492
493 # page selection
494 pack [ttk::frame .pagesf] -in .bg -fill x
495 pack [ttk::radiobutton .pagesf.all -text "Convert all pages" \
496 -variable ::options(pages) -value "all" -command set_widget_states] \
497 -side left
498 pack [ttk::radiobutton .pagesf.single -text "Page:" \
499 -variable ::options(pages) -value "single" -command set_widget_states] \
500 -side left -padx [list 10 0]
501 pack [entry .pagesf.e -width 6 -textvariable ::options(page)] -side left
502 #.pagesf.e configure -vcmd {page_valid %W} -validate focusout \
503 # -invcmd "focusAndFlash %W [.pagesf.e cget -fg] [.pagesf.e cget -bg]"
504 .pagesf.e configure -vcmd {page_valid %W} -validate focusout \
505 -invcmd "see_red %W"
506
507 # temp files
508 pack [ttk::checkbutton .clean -text "Remove temp files" \
509 -variable ::options(clean)] -in .bg -anchor w -pady [list 10 0]
510
511 proc focusAndFlash {w fg bg {count 9}} {
512 focus $w
513 if {$count<1} {
514 $w configure -foreground $fg -background $bg
515 } else {
516 if {$count%2} {
517 $w configure -foreground $bg -background $fg
518 } else {
519 $w configure -foreground $fg -background $bg
520 }
521 after 200 [list focusAndFlash $w $fg $bg [expr {$count-1}]]
522 }
523 }
524
525 proc see_red {w} {
526 focus $w
527 set orig [$w cget -bg]
528 $w configure -bg red
529 update idletasks
530 after 1000
531 $w configure -bg $orig
532 update idletasks
533 }
534
535 proc page_valid {w} {
536 # w entry widget
537 set p [$w get]
538 set isvalid 1
539 #puts "check pageno $p"
540 if {! [string is integer $p] || $p<=0} {
541 set isvalid 0
542 } elseif {$::gfile(npages) ne "" && [scan $p "%d"] > $::gfile(npages)} {
543 set isvalid 0
544 }
545 return $isvalid
546 }
547
548 # end conversion options
549
550 pack [ttk::label .status -justify left -text "Idle"] \
551 -in .bg -side bottom -anchor w -fill x -expand 1
552
553 # main buttons
554
555 pack [ttk::frame .bottomf] -in .bg -side bottom -fill x
556 ppack [ttk::button .bottomf.view -text "View" -command view] -side left
557 ppack [ttk::button .bottomf.open -text "Open" -command openDialog] \
558 -side left -padx 2
559 ppack [ttk::button .bottomf.convert -text "Convert and save..." \
560 -command saveDialog] -side left -padx 2
561 ppack [ttk::button .bottomf.done -text "Done" -command exit] -side right
562
563 proc view {} {
564 if {! [viewable]} {
565 tk_messageBox -icon warning -type ok \
566 -message "No viewer for $::gfile(path)"
567 return
568 }
569 if {$::tcl_platform(os) eq "Darwin"} {
570 catch {exec open $::gfile(path) &}
571 } elseif {$::tcl_platform(platform) ne "unix"} {
572 # use shortname to sidestep quoting problems
573 catch {exec cmd /x /c start [file attributes $::gfile(path) -shortname]}
574 } else {
575 if {$::gfile(type) eq "pdf"} {
576 catch {exec $::settings(pdf_viewer) $::gfile(path) &}
577 } elseif {$::gfile(type) ne "other" && $::gfile(type) ne ""} {
578 catch {exec $::settings(ps_viewer) $::gfile(path) &}
579 }
580 }
581 }
582
583 proc openDialog {} {
584 set types {
585 {"PostScript and pdf" {.eps .epi .epsi .ps .prn .pdf}}
586 {"Encapsulated PostScript" {.eps .epi .epsi}}
587 {"General PostScript" {.ps .prn}}
588 {"Pdf" {.pdf}}
589 {"All files" *}
590 }
591 if {[file isdirectory $::gfile(dir)]} {
592 set try [tk_getOpenFile -filetypes $types -initialdir $::gfile(dir)]
593 } else {
594 set try [tk_getOpenFile -filetypes $types]
595 }
596 if {$try != ""} {
597 set ::gfile(path) [file normalize $try]
598 set ::gfile(dir) [file dirname $::gfile(path)]
599 set ::gfile(name) [file tail $::gfile(path)]
600 set ::gfile(type) ""
601 if {! [catch {exec $::texlua $::epspdf_tlu --gui=gui -i $::gfile(path)} \
602 result]} {
603 # parse output
604 regexp {has type (\w+)(?:.+ (\d+) pages)?\.} $result \
605 mtc ::gfile(type) ::gfile(npages)
606 if {[regexp {^eps} $::gfile(type)]} {set ::gfile(npages) 1}
607 }
608 if {$::gfile(type) eq ""} {
609 # unsupported type
610 tk_messageBox -message "$try: unreadable or unsupported type" \
611 -title "Error" -icon error
612 }
613 set ::settings(default_dir) $::gfile(dir)
614 putsettings
615 }
616 set_widget_states
617 }
618
619 proc saveDialog {} {
620 if {$::options(format) eq "pdf"} {
621 set types {{"Pdf" {.pdf}}}
622 } elseif {$::options(format) eq "ps"} {
623 set types {{"PostScript" {.ps}}}
624 } elseif {$::options(format) eq "eps"} {
625 set types {{"Encapsulated PostScript" {.eps}}}
626 }
627 set try [tk_getSaveFile -filetypes $types -initialdir $::gfile(dir)]
628 if {$try != ""} {
629 # fix extension
630 set try [file normalize $try]
631 set idot [string last "." [file tail $try]]
632 if {$idot == -1} {
633 append try ".$::options(format)"
634 } else {
635 # don't bother checking whether the extension is already correct
636 set try [string range $try 0 [string last "." $try]]
637 append try $::options(format)
638 }
639 set try [file normalize $try]
640 # epspdf can read persistent options from configuration.
641 # only options from the options array need to be converted to parameters.
642 set args [list]
643 if {$::options(gray) eq "gray"} {lappend args "-g"}
644 if {$::options(bbox)} {lappend args "-b"}
645 if {! $::options(clean)} {lappend args "-d"}
646 if {$::options(pages) eq "single"} {
647 if {$::options(page) eq ""} {set ::options(page) 1}
648 lappend args "-p" $::options(page)
649 }
650 lappend args $::gfile(path) $try
651 .status configure -text "Working..." -justify "left"
652 foreach b {view open convert done} {
653 .bottomf.$b configure -state disabled
654 }
655 update idletasks; # force immediate redisplay main window
656
657 set cmd {} ; # necessary? if- and else blocks do not seem to define scopes
658 if {$::tcl_platform(platform) == "windows"} {
659 set cmd [linsert $args 0 exec -ignorestderr $::epspdf_cmd --gui=gui]
660 } else {
661 set cmd [linsert $args 0 exec -ignorestderr \
662 $::texlua $::epspdf_tlu --gui=gui]
663 }
664 set failed [catch $cmd result]
665 write_log $result
666 if $failed {
667 tk_messageBox -icon error -type ok -message "Error; see log window"
668 } else {
669 set ::gfile(path) [file normalize $try]
670 set ::gfile(dir) [file dirname $::gfile(path)]
671 set ::gfile(type) $::options(format)
672 set ::gfile(name) [file tail $::gfile(path)]
673 if {$::gfile(type) eq "pdf"} {
674 if {! [catch {
675 exec $::texlua $::epspdf_tlu --gui=gui -i $::gfile(path)} \
676 result]} {
677 regexp {(\d+) pages\.$} $result mtc ::gfile(npages)
678 }
679 } elseif {$::gfile(type) eq "eps"} {
680 set ::gfile(npages) 1
681 }
682 set ::settings(default_dir) $::gfile(dir)
683 putsettings
684 set ::options(page) 1
685 }
686 .status configure -text "Idle"
687 foreach b {view open convert done} {
688 .bottomf.$b configure -state normal
689 }
690 focus .bottomf.view
691 set_widget_states
692 }
693 }
694
695 proc set_widget_states {} {
696 # states (normal/disabled) for various widgets.
697 # their values should already be mutually consistent.
698 # widgets concerned:
699 # view / save
700 # output format / bbox / single page / page no.
701
702 # view
703 if {[viewable]} {
704 .bottomf.view configure -state normal
705 } else {
706 .bottomf.view configure -state disabled
707 }
708
709 # convert
710 .bottomf.convert configure -state normal
711 if {$::gfile(path) eq "" || ! [file exists $::gfile(path)]} { \
712 .bottomf.convert configure -state disabled
713 }
714 if {$::gfile(type) eq "other"} {
715 .bottomf.convert configure -state disabled
716 }
717 if {$::gfile(npages) ne "" && $::options(pages) eq "single" && \
718 $::options(page) > $::gfile(npages)} {
719 .bottomf.convert configure -state disabled
720 }
721
722 # type
723 if {[regexp {ps|pdf} $::gfile(type)]} {
724 foreach f {pdf eps ps} {.optsf.format.$f configure -state normal}
725 } else {
726 foreach f {pdf eps ps} {.optsf.format.$f configure -state disabled}
727 }
728
729 # pages and page
730 foreach f {all single} {.pagesf.$f configure -state normal}
731 if {$::options(format) eq "eps"} {
732 .pagesf.all configure -state disabled
733 set ::options(pages) "single"
734 }
735 if {$::options(format) eq "ps"} {
736 .pagesf.single configure -state disabled
737 set ::options(pages) "all"
738 }
739 if {$::options(pages) eq "all"} {
740 .pagesf.e configure -state disabled
741 } else {
742 .pagesf.e configure -state normal
743 }
744
745 # boundingbox
746 if {$::options(format) eq "eps"} {
747 .bbox configure -state normal
748 } elseif {$::options(pages) eq "all"} {
749 .bbox configure -state disabled
750 set ::options(bbox) 0
751 } else {
752 .bbox configure -state normal
753 }
754 update idletasks
755 }
756
757 set_widget_states