OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [libgui/] [library/] [print.tcl] - Blame information for rev 1770

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
# print.tcl -- some procedures for dealing with printing.  To print
2
# PostScript on Windows, tkmswin.dll will need to be present.
3
 
4
proc send_printer { args } {
5
    global tcl_platform
6
 
7
    parse_args {
8
        {printer {}}
9
        {outfile {}}
10
        {parent {}}
11
        ascii
12
        file
13
    }
14
 
15
    if {[llength $args] == 0} {
16
        error "No filename or data provided."
17
    }
18
 
19
    if {$ascii == 1} {
20
        if {$tcl_platform(platform) == "windows"} then {
21
            PRINT_windows_ascii -file $file -parent $parent [lindex $args 0]
22
        } else {
23
            send_printer_ascii -printer $printer -file $file \
24
                    -outfile $outfile [lindex $args 0]
25
        }
26
        return
27
    }
28
 
29
    if {$outfile != ""} {
30
        if {$file} {
31
            file copy [lindex 0 $args] $outfile
32
        } else {
33
            set F [open $outfile w]
34
            puts $F [lindex 0 $args]
35
            close $F
36
        }
37
        return
38
    }
39
 
40
    if {$tcl_platform(platform) == "windows"} then {
41
        load tkmswin.dll
42
 
43
        set cmd {tkmswin print -postscript}
44
        if {$printer != ""} {
45
            lappend cmd -printer $printer
46
        }
47
        if {$file} {
48
            lappend cmd -file
49
        }
50
        lappend cmd [lindex $args 0]
51
        eval $cmd
52
 
53
    } else {
54
 
55
        # Unix box, assume lpr, but if it fails try lp.
56
        foreach prog {lpr lp} {
57
            set cmd [list exec $prog]
58
            if {$printer != ""} {
59
                if {$prog == "lpr"} {
60
                    lappend cmd "-P$printer"
61
                } else {
62
                    lappend cmd "-d$printer"
63
                }
64
            }
65
            if {$file} {
66
                lappend cmd "<"
67
            } else {
68
                lappend cmd "<<"
69
            }
70
            # tack on data or filename
71
            lappend cmd [lindex $args 0]
72
 
73
            # attempt to run the command, and exit if successful
74
            if ![catch {eval $cmd} ret] {
75
                return
76
            }
77
        }
78
        error "Couldn't run either `lpr' or `lp' to print"
79
    }
80
}
81
 
82
proc send_printer_ascii { args } {
83
    global tcl_platform
84
 
85
    parse_args {
86
        {printer {}}
87
        {outfile {}}
88
        {file 0}
89
        {font Courier}
90
        {fontsize 10}
91
        {pageheight 11}
92
        {pagewidth 8.5}
93
        {margin .5}
94
    }
95
    if {[llength $args] == 0} {
96
        error "No filename or data provided."
97
    }
98
 
99
    if {$tcl_platform(platform) == "windows"} then {
100
        PRINT_windows_ascii -file $file [lindex $args 0]
101
        return
102
    }
103
 
104
    # convert the filename or data to ascii, and then send to the printer.
105
 
106
    set inch 72
107
    set pageheight [expr $pageheight*$inch]
108
    set pagewidth [expr $pagewidth*$inch]
109
    set margin [expr $margin*$inch]
110
 
111
    set output "%!PS-Adobe-1.0\n"
112
    append output "%%Creator: libgui ASCII-to-PS converter\n"
113
    append output "%%DocumentFonts: $font\n"
114
    append output "%%Pages: (atend)\n"
115
    append output "/$font findfont $fontsize scalefont setfont\n"
116
    append output "/M{moveto}def\n"
117
    append output "/S{show}def\n"
118
 
119
    set pages 1
120
    set y [expr $pageheight-$margin-$fontsize]
121
 
122
    if {$file == 1} {
123
        set G [open [lindex $args 0] r]
124
        set strlen [gets $G str]
125
    } else {
126
        # make sure that we end with a newline
127
        set args [lindex $args 0]
128
        append args "\n"
129
 
130
        set strlen [string first "\n" $args]
131
        if {$strlen != -1} {
132
            set str [string range $args 0 [expr $strlen-1]]
133
            set args [string range $args [expr $strlen+1] end]
134
        }
135
    }
136
    while {$strlen != -1} {
137
        if {$y < $margin} {
138
            append output "showpage\n"
139
            incr pages
140
            set y [expr $pageheight-$margin-$fontsize]
141
        }
142
        regsub -all {[()\\]} $str {\\&} str
143
        append output "$margin $y M ($str) S\n"
144
        set y [expr $y-($fontsize+1)]
145
 
146
        if {$file == 1} {
147
            set strlen [gets $G str]
148
        } else {
149
            set strlen [string first "\n" $args]
150
            if {$strlen != -1} {
151
                set str [string range $args 0 [expr $strlen-1]]
152
                set args [string range $args [expr $strlen+1] end]
153
            }
154
        }
155
 
156
    }
157
    append output "showpage\n"
158
    append output "%%Pages: $pages\n"
159
 
160
    if {$file == 1} {
161
        close $G
162
    }
163
 
164
    send_printer -printer $printer -outfile $outfile $output
165
}
166
 
167
# Print ASCII text on Windows.
168
 
169
proc PRINT_windows_ascii { args } {
170
    global tcl_platform errorInfo
171
    global PRINT_state
172
 
173
    parse_args {
174
        {file 0}
175
        {parent {}}
176
    }
177
    if {[llength $args] == 0} {
178
        error "No filename or data provided."
179
    }
180
 
181
    if {$tcl_platform(platform) != "windows"} then {
182
        error "Only works on Windows"
183
    }
184
 
185
    # Copied from tk_dialog, except that it returns.
186
    catch {destroy .cancelprint}
187
    toplevel .cancelprint -class Dialog
188
    wm withdraw .cancelprint
189
    wm title .cancelprint [gettext "Printing"]
190
    frame .cancelprint.bot
191
    frame .cancelprint.top
192
    pack .cancelprint.bot -side bottom -fill both
193
    pack .cancelprint.top -side top -fill both -expand 1
194
    set PRINT_state(pageno) [format [gettext "Now printing page %d"] 0]
195
    label .cancelprint.msg -justify left -textvariable PRINT_state(pageno)
196
    pack .cancelprint.msg -in .cancelprint.top -side right -expand 1 \
197
            -fill both -padx 1i -pady 5
198
    button .cancelprint.button -text [gettext "Cancel"] \
199
            -command { ide_winprint abort } -default active
200
    grid .cancelprint.button -in .cancelprint.bot -column 0 -row 0 \
201
            -sticky ew -padx 10
202
    grid columnconfigure .cancelprint.bot 0
203
 
204
    update idletasks
205
    set x [expr [winfo screenwidth .cancelprint]/2 \
206
            - [winfo reqwidth .cancelprint]/2 \
207
            - [winfo vrootx [winfo parent .cancelprint]]]
208
    set y [expr [winfo screenheight .cancelprint]/2 \
209
            - [winfo reqheight .cancelprint]/2 \
210
            - [winfo vrooty [winfo parent .cancelprint]]]
211
    wm geom .cancelprint +$x+$y
212
    update
213
 
214
    # We're going to change the focus and the grab as soon as we start
215
    # printing, so remember them now.
216
    set oldFocus [focus]
217
    set oldGrab [grab current .cancelprint]
218
    if {$oldGrab != ""} then {
219
        set grabStatus [grab status $oldGrab]
220
    }
221
 
222
    focus .cancelprint.button
223
 
224
    set PRINT_state(start) 1
225
    set PRINT_state(file) $file
226
    if {$file == 1} then {
227
        set PRINT_state(fp) [open [lindex $args 0] r]
228
    } else {
229
        set PRINT_state(text) [lindex $args 0]
230
    }
231
 
232
    set cmd [list ide_winprint print_text PRINT_query PRINT_text \
233
               -pageproc PRINT_page]
234
    if {$parent != {}} then {
235
        lappend cmd -parent $parent
236
    }
237
 
238
    set code [catch $cmd errmsg]
239
    set errinfo $errorInfo
240
 
241
    catch { focus $oldFocus }
242
    catch { destroy .cancelprint }
243
    if {$oldGrab != ""} then {
244
        if {$grabStatus == "global"} then {
245
            grab -global $oldGrab
246
        } else {
247
            grab $oldGrab
248
        }
249
    }
250
 
251
    if {$code == 1} then {
252
        error $errmsg $errinfo
253
    }
254
}
255
 
256
# The query procedure passed to ide_winprint print_text.  This should
257
# return one of "continue", "done", or "newpage".
258
 
259
proc PRINT_query { } {
260
    global PRINT_state
261
 
262
    # Fetch the next line into PRINT_state(str).
263
 
264
    if {$PRINT_state(file) == 1} then {
265
        set strlen [gets $PRINT_state(fp) PRINT_state(str)]
266
    } else {
267
        set strlen [string first "\n" $PRINT_state(text)]
268
        if {$strlen != -1} then {
269
            set PRINT_state(str) \
270
                    [string range $PRINT_state(text) 0 [expr $strlen-1]]
271
            set PRINT_state(text) \
272
                    [string range $PRINT_state(text) [expr $strlen+1] end]
273
        } else {
274
            if {$PRINT_state(text) != ""} then {
275
                set strlen 0
276
                set PRINT_state(str) $PRINT_state(text)
277
                set PRINT_state(text) ""
278
            }
279
        }
280
    }
281
 
282
    if {$strlen != -1} then {
283
 
284
        # Expand tabs assuming tabstops every 8 spaces and a fixed
285
        # pitch font.  Text written to other assumptions will have to
286
        # be handled by the caller.
287
 
288
        set str $PRINT_state(str)
289
        while {[set i [string first "\t" $str]] >= 0} {
290
            set c [expr 8 - ($i % 8)]
291
            set spaces ""
292
            while {$c > 0} {
293
                set spaces "$spaces "
294
                incr c -1
295
            }
296
            set str "[string range $str 0 [expr $i - 1]]$spaces[string range $str [expr $i + 1] end]"
297
        }
298
        set PRINT_state(str) $str
299
 
300
        return "continue"
301
    } else {
302
        return "done"
303
    }
304
}
305
 
306
# The text procedure passed to ide_winprint print_text.  This should
307
# return the next line to print.
308
 
309
proc PRINT_text { } {
310
    global PRINT_state
311
 
312
    return $PRINT_state(str)
313
}
314
 
315
# This page procedure passed to ide_winprint print_text.  This is
316
# called at the start of each page.
317
 
318
proc PRINT_page { pageno } {
319
    global PRINT_state
320
 
321
    set PRINT_state(pageno) [format [gettext "Now printing page %d"] $pageno]
322
 
323
    if {$PRINT_state(start)} then {
324
        wm deiconify .cancelprint
325
 
326
        grab .cancelprint
327
        focus .cancelprint.button
328
 
329
        set PRINT_state(start) 0
330
    }
331
 
332
    update
333
    return "continue"
334
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.