2
#package require TkCximage
3
package provide scalable-bg 0.1
5
snit::type scalable-bg {
6
option -n -configuremethod setOption
7
option -e -configuremethod setOption
8
option -s -configuremethod setOption
9
option -w -configuremethod setOption
11
option -source -configuremethod setOption
12
option -width -configuremethod setOption -default 1
13
option -height -configuremethod setOption -default 1
24
constructor { args } {
25
$self configurelist $args
27
set base [image create photo -width $options(-width) -height $options(-height)]
28
set left [image create photo -width $options(-w) -height [expr {$options(-height) - $options(-n) - $options(-s)}]]
29
set centre [image create photo -width [expr {$options(-width) - $options(-w) - $options(-e)}] -height [expr {$options(-height) - $options(-n) - $options(-s)}]]
30
set right [image create photo -width $options(-e) -height [expr {$options(-height) - $options(-n) - $options(-s)}]]
31
set top [image create photo -width [expr {$options(-width) - $options(-e)}] -height $options(-n)]
32
set bottom [image create photo -width [expr {$options(-width) - $options(-e)}] -height $options(-n)]
33
set src $options(-source)
38
method BuildImage { } {
39
$base configure -width $options(-width) -height $options(-height)
40
#Check if requested size is too small:
42
$options(-width) < [expr {$options(-w) + $options(-e) + 1}] || \
43
$options(-height) < [expr {$options(-n) + $options(-s) + 1}] || \
44
[image width $src] < [expr {$options(-w) + $options(-e) + 1}] || \
45
[image height $src] < [expr {$options(-n) + $options(-s) + 1}]
50
$left configure -width $options(-w) \
51
-height [expr [image height $src] - $options(-n) - $options(-s)]
53
if { [expr {[image height $src] - $options(-s)}] < 0 } { exit }
54
$left copy $src -from 0 $options(-n) $options(-w) [expr [image height $src] - $options(-s)] -to 0 0
55
if { $options(-resizemethod) == "scale" } {
56
::CxImage::Resize $left $options(-w) [expr {$options(-height) - $options(-n) - $options(-s)}]
59
#Resize middle section:
60
$centre configure -width [expr {[image width $src] - $options(-w) - $options(-e)}] \
61
-height [expr {[image height $src] - $options(-n) - $options(-s)}]
63
$centre copy $src -from $options(-w) $options(-n) [expr {[image width $src] - $options(-e)}] \
64
[expr {[image height $src] - $options(-s)}] -to 0 0
65
if { $options(-resizemethod) == "scale" } {
66
::CxImage::Resize $centre [expr {$options(-width) - $options(-w) - $options(-e)}] \
67
[expr {$options(-height) - $options(-n) - $options(-s)}]
70
#Resize right section:
71
$right configure -width $options(-e) \
72
-height [expr {[image height $src] - $options(-n) - $options(-s)}]
74
$right copy $src -from [expr {[image width $src] - $options(-e)}] $options(-n) [image width $src] [expr {[image height $src] - $options(-s)}] -to 0 0
75
if { $options(-resizemethod) == "scale" } {
76
::CxImage::Resize $right $options(-e) [expr {$options(-height) - $options(-n) - $options(-s)}]
80
$top configure -width [expr {[image width $src] - $options(-w) - $options(-e)}] -height $options(-n)
82
$top copy $src -from $options(-w) 0 [expr {[image width $src] - $options(-e)}] $options(-n)
83
if { $options(-resizemethod) == "scale" } {
84
::CxImage::Resize $top [expr {$options(-width) - $options(-w) - $options(-e)}] $options(-n)
87
#Resize bottom section:
88
$bottom configure -width [expr {[image width $src] - $options(-w) - $options(-e)}] -height $options(-s)
90
$bottom copy $src -from $options(-w) [expr {[image height $src] - $options(-s)}] \
91
[expr {[image width $src] - $options(-e)}] [image height $src]
92
if { $options(-resizemethod) == "scale" } {
93
::CxImage::Resize $bottom [expr {$options(-width) - $options(-w) - $options(-e)}] \
97
#Build up button image:
98
$base configure -width $options(-width) -height $options(-height)
101
$base copy $src -from 0 0 $options(-w) $options(-n) -to 0 0
103
$base copy $top -to $options(-w) 0 [expr {$options(-width) - $options(-e)}] $options(-n)
105
$base copy $src -from [expr {[image width $src] - $options(-e)}] 0 [image width $src] \
106
$options(-n) -to [expr {$options(-width) - $options(-e)}] 0
108
$base copy $left -to 0 $options(-n) $options(-w) [expr {$options(-height) - $options(-s)}]
110
$base copy $centre -to $options(-w) $options(-n) [expr {$options(-width) - $options(-e)}] \
111
[expr {$options(-height) - $options(-s)}]
113
$base copy $right -to [expr {$options(-width) - $options(-e)}] $options(-n) $options(-width) \
114
[expr {$options(-height) - $options(-s)}]
116
$base copy $src -from 0 [expr {[image height $src] - $options(-s)}] $options(-w) \
117
[image height $src] -to 0 [expr {$options(-height) - $options(-s)}]
119
$base copy $bottom -to $options(-w) [expr {[image height $base] - $options(-s)}] \
120
[expr {$options(-width) - $options(-e)}] $options(-height)
122
$base copy $src -from [expr {[image width $src] - $options(-e)}] \
123
[expr {[image height $src] - $options(-s)}] [image width $src] [image height $src] \
124
-to [expr {$options(-width) - $options(-e)}] [expr {$options(-height) - $options(-s)}]
128
method setOption { option value } {
129
set options($option) $value
131
[info exists top] && \
132
[info exists right] && \
133
[info exists bottom] && \
134
[info exists left] && \
135
[info exists centre] && \
136
[info exists src] && \