summaryrefslogtreecommitdiffstats
path: root/tkimg/tkv.tcl
blob: 382e9e90eaf4e06b72e452b7d0f60a562ffee269 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/sh

# The next line restarts using wish \

exec wish $0 ${1+"$@"}

package require msgcat
::msgcat::mcload [file join [file dirname [info script]] msgs]
catch {namespace import ::msgcat::mc}

proc mmc string {
    regsub & $string {} string
    return $string
}

proc mml string {
    return [string first & $string]
}

#

# Make the Image format available.

#


package require Img

#

# Initialization of some global variables

#


set tkvPriv(count) 0
set tkvPriv(currentdir) [pwd]
set tkvPriv(defaultext) .gif

set tkvPriv(types) [list \
    [list [mc "Image Files"]	{.bmp}		] \
    [list [mc "Image Files"]	{.gif}		] \
    [list [mc "Image Files"]	{.jpeg .jpg}	] \
    [list [mc "Image Files"]	{.png}		] \
    [list [mc "Image Files"]	{.tiff .tif}	] \
    [list [mc "Image Files"]	{.xbm}		] \
    [list [mc "Image Files"]	{.xpm}		] \
    [list [mc "Image Files"]	{.ps .eps}		] \
    [list "BMP [mc Files]"		{.bmp}		] \
    [list "GIF [mc Files]"		{.gif}		] \
    [list "JPEG [mc Files]"		{.jpeg .jpg}	] \
    [list "PNG [mc Files]"		{.png}		] \
    [list "TIFF [mc Files]"		{.tiff .tif}	] \
    [list "XBM [mc Files]"		{.xbm}		] \
    [list "XPM [mc Files]"		{.xpm}		] \
    [list "Postscript [mc Files]"	{.ps .eps}		] \
    [list "GIF [mc Files]"		{}			GIFF] \
    [list "JPEG [mc Files]"		{}			JPEG] \
    [list "PNG [mc Files]"		{}			PNGF] \
    [list "TIFF [mc Files]"		{}			TIFF] \
    [list [mc {All files}]		*] \
]

proc Menu {base name} {
    set menu [ConCat $base menu]
    if {![winfo exists $menu]} {
	menu $menu
	$base configure -menu $menu
    }
    set accelerator [string toupper [string index $name 0]]
    set text [mc "&$accelerator[string range $name 1 end]"]
    set name $menu.$name
    if {![winfo exists $name]} {
	menu $name
	$menu add cascade -label [mmc $text] -menu $name -underline [mml $text]
    }
    return $name
}

#

# Small proc to concatenate window pathnames

#

proc ConCat args {
    regsub -all {[ 	\.]+} $args . args
    return $args
}

#

#  Create a new image window

#


proc image_window {{window {}}} {
    global tkvPriv
    if {![string compare $window {}]} {
	set window .image$tkvPriv(count)
	incr tkvPriv(count)
    }
    if {[winfo exists $window]} {
	catch {eval destroy [winfo children $window]}
    } else {
	toplevel $window
    }
    wm title $window [mc {Viewer}]
    set frame [ConCat $window frame]
    label $frame -relief sunken -bg white -bd 2 -anchor nw
    set w [Menu $window file]
    $w configure -tearoff 0
    set text [mc &Open]
    $w add command -label [mmc $text] -command [list load_image $window] -underline [mml $text]
    set text [mc &Save]
    $w add command -label [mmc $text] -command [list save_image $window] -underline [mml $text]
    $w add separator
    set text [mc &Close]
    $w add command -label [mmc $text] -command [list destroy $window] -underline [mml $text]
    set text [mc E&xit]
    $w add command -label [mmc $text] -command [list destroy .] -underline [mml $text]

    set w [Menu $window images]
    set w [Menu $window help]
    $w configure -tearoff 0
    set text [mc &About]
    $w add command -label [mmc $text] -command About -underline [mml $text]

    catch {wm geometry $window 200x200}
    pack $frame -side top -expand y -fill both
    return $frame
}

proc register_image {w name} {
    set menu [Menu $w images]
    set item [file tail [lindex $name 0]]
    if {[llength $name] > 1} {
	append item " [lindex $name 1]"
    }
    $menu add command -label $item -command \
	[list show_image $w $name]
}

proc show_image {window name} {
    [ConCat $window frame] configure -image $name
    catch {wm geometry $window {}}
}

proc load_image window {
    global tkvPriv
    set filename [tk_getOpenFile -filetypes $tkvPriv(types) -parent \
	    $window -initialdir $tkvPriv(currentdir)]
    if {[string compare $filename {}]} {
	set imagename [list $filename]
	image create photo $imagename -file $filename
	register_image $window $imagename
	show_image $window $imagename
	set tkvPriv(currentdir) [file dirname $filename]
    }
}

proc save_image window {
    global tkvPriv
    set img [[ConCat $window frame] cget -image]
    set filename [tk_getSaveFile -filetypes $tkvPriv(types) -parent \
	    $window -initialdir $tkvPriv(currentdir) -defaultextension .gif \
	    -initialfile [file rootname [lindex $img 0]].gif]
    if {[string compare $filename {}]} {
	$img write $filename -format gif
	set tkvPriv(currentdir) [file dirname $filename]
    }
}

proc About {} {
    tk_dialog .about "[mmc [mc &About]] tkv.tcl" "[mc {Tiny Image viewer}]\n[mc {written by}]\

	    Jan Nijtmans <nijtmans@users.sourceforge.net>" {} 0 [mc O.K.]
}

set w [lindex $argv 1]
if {![string compare $w {}]} {
    set w .
}
set filename [lindex $argv 0]

image_window $w
if {[string compare $filename {}]} {
    set imagename [list $filename]
    image create photo $imagename -file $filename
    register_image $w $imagename
    show_image $w $imagename
}