Winico 64 bit build

Check-in [f9cc280ae4]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:First 64 bit build
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:f9cc280ae4f020076c9fe70e60c4c33bca2734e81c52ed80b7ce22807fdbf96c
User & Date: mjanssen 2017-03-28 14:28:57
Context
2017-03-29
09:31
Build for Tcl 8.6 Leaf check-in: 4c279af0e9 user: mjanssen tags: trunk
2017-03-28
14:28
First 64 bit build check-in: f9cc280ae4 user: mjanssen tags: trunk
14:27
initial empty check-in check-in: 7b6dab3476 user: mjanssen tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added pkg/Winico0.7.1.dll.

cannot compute difference between binary files

Added pkg/demo.tcl.









































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
# demo.tcl - Copyright (C) 2005 Pat Thoyts <patthoyts@users.sourceforge.net>
#
# Winico package demo
#
#
# $Id: demo.tcl,v 1.2 2005/09/27 14:02:54 patthoyts Exp $

package require Tk 8.4

# Load winico - first look in the build directories for a dll to load
#
set srcdir [file dirname [file dirname [info script]]]
set dbgfile [file join $srcdir win Debug winico06g.dll]
set relfile [file join $srcdir win Release winico06.dll]
if {[file exists $dbgfile]} {
    load $dbgfile
} elseif {[file exists $relfile]} {
    load $relfile
}

package require Winico 0.6

# -------------------------------------------------------------------------

variable App
if {![info exists App]} { array set App {run 0 cleanup 0 dlg 0 wmstate normal} }

proc Main {} {
    variable App

    wm title . "Winico [package provide Winico] Demo"

    # Load an icon file with multiple icons or default to a system icon.
    set icofile [file join [file dirname [info script]] tkchat.ico]
    if {[file exists $icofile]} {
        set icon [winico createfrom $icofile]
    } else {
        set icon [winico load exclamation]
    }

    # As of 0.6 unicode is supported in the text strings
    winico text $icon "Nothing selected (unicode: \u043a\u043c\u0436)"

    # Create a context menu to tie to the taskbar
    set m [menu .popup -tearoff 0]
    $m add command -label "Item One"   -command [list MenuCommand $icon 1]
    $m add command -label "Item Two"   -command [list MenuCommand $icon 2]
    $m add command -label "Item Three" -command [list MenuCommand $icon 3]
    $m add command -label "Item Four"  -command [list MenuCommand $icon 4]
    $m add separator
    $m add command -label "Withdraw"   -command [list Withdraw $m]
    $m add command -label "Exit"       -command [list Exit]

    # Hook up our taskbar icon and callback procedure
    winico taskbar add $icon -pos 0 \
        -callback [list WinicoCallback $m %m %i %w %l %x %y %H]

    # Create our GUI
    text .t -width 80 -height 10 -yscrollcommand {.s set}
    scrollbar .s -command {.t yview}
    button .d -text Dialog -command [list Dialog]
    button .b -text Exit -command [list Exit]
    grid .t  - .s -sticky news
    grid .d .b -  -sticky ne
    grid rowconfigure . 0 -weight 1
    grid columnconfigure . 0 -weight 1

    bind . <Control-F2> {console show}
    bind . <Escape> {Exit}

    # Wait for the window to map and then change the icon
    tkwait visibility .
    winico setwindow . $icon small 0
    wm protocol . WM_DELETE_WINDOW Exit

    # Run the program and wait for the user to finish.
    tkwait variable [namespace current]::App(run)

    # Remove our taskbar icon and clean up.
    # This needs to be done before we destroy the window!
    winico taskbar delete $icon
    winico delete $icon

    # Finish.
    if {[winfo exists .]} {destroy .}
}

proc MenuCommand {icon index} {
    set text [format "Last selected item $index (%c)" [expr {0x043a + $index}]]
    .t insert end $text {} "\n" {}
    winico taskbar modify $icon -text $text -pos $index
}

proc Dialog {} {
    variable App
    set dlg [toplevel .dlg[incr App(dlg)]]
    pack [entry $dlg.e -textvariable [namespace current]::App($dlg)]
}

proc Withdraw {m} {
    variable App
    if {[wm state .] eq "withdrawn"} {
        set ndx [$m index "Restore"]
        if {$ndx != -1} {
            $m entryconfigure $ndx -label "Withdraw"
        }
        wm state . $App(wmstate)
        wm deiconify .
    } else {
        set ndx [$m index "Withdraw"]
        if {$ndx != -1} {
            $m entryconfigure $ndx -label "Restore"
        }
        set App(wmstate) [wm state .]
        wm withdraw .
    }
}

proc Exit {} {
    set [namespace current]::App(run) 1
}

proc WinicoCallback {popup msg icon wparam lparam x y args} {
    .t insert end "$msg ico:$icon wp:$wparam lp:$lparam @($x,$y) $args\n"
    .t see end
    switch -exact -- $msg {
        WM_RBUTTONUP {
            $popup post $x $y
        }
        WM_LBUTTONUP {
            $popup post $x $y
        }
    }
}

# -------------------------------------------------------------------------

if {!$tcl_interactive} {
    set r [catch [linsert $argv 0 Main] err]
    if {$r} {puts $err}
    exit $r
}

# -------------------------------------------------------------------------
# Local variables:
# mode: tcl
# indent-tabs-mode: nil
# End:

Added pkg/manpage.css.





















































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * $Id: manpage.css,v 1.5 2004/10/01 00:10:49 jenglish Exp $
 * Author: 	Joe English, <jenglish@flightab.com>
 * Created: 	26 Jun 2000
 * Description:	CSS stylesheet for TCL man pages
 */

HTML {
    background: 	#FFFFFF;
    color: 		black;
}

BODY {
    background: 	#FFFFFF;
    color:	 	black;
}

DIV.body {
    margin-left:	10%;
    margin-right:	10%;
}
DIV.header,DIV.footer {
    width:		100%;
    margin-left:	0%;
    margin-right:	0%;
}

DIV.body H1,DIV.body H2 {
    margin-left:	-5%;
}

/* Navigation material: */

DIV.navbar {
    width:		100%;
    margin-top:		5pt;
    margin-bottom:	5pt;
    margin-left:	0%;
    margin-right:	0%;
    padding-top:  	5pt;
    padding-bottom:	5pt;
    background:		#DDDDDD;
    color:		black;
    border: 		1px solid black;
    text-align:		center;
    font-size:		small;
    font-family:	sans-serif;
}

P.navaid {
    text-align:	center;
}
.navaid {
    font-size:		small;
    font-family:	sans-serif;
}

P.notice {
    text-align: 	center;
    font-size:		small;
    font-family:	sans-serif;
    font-style: 	italic;
    color: 		red;
}

A.navaid:link 		{ color: green;		background: transparent; }
A.navaid:visited	{ color: green;		background: transparent; }
A.navaid:active 	{ color: yellow;	background: transparent; }

/* For most anchors, we should leave colors up to the user's preferences. */
/*--
A:link 		{ color: blue; 		background: transparent; }
A:visited	{ color: purple;	background: transparent; }
A:active 	{ color: red;		background: transparent; }
--*/

H1, H2, H3, H4 {
    margin-top: 	1em;
    font-family:	sans-serif;
    font-size:		large;
    color:		#005A9C;
    background: 	transparent;
    text-align:		left;
}

H1.title {
    text-align: center;
}

UL,OL {
    margin-right: 0em;
    margin-top: 3pt;
    margin-bottom: 3pt;
}
UL LI {
    list-style: disc;
}
OL LI {
    list-style: decimal;
}

DT {
	padding-top: 	1ex;
}

DL.toc {
    font: 	normal 12pt/16pt sans-serif;
    margin-left: 10%;
}

UL.toc,UL.toc UL, UL.toc UL UL {
    font:	normal 12pt/14pt sans-serif;
    list-style:	none;
}
LI.tocentry,LI.tocheading {
    list-style: 	none;
    margin-left: 	0em;
    text-indent:	0em;
    padding: 		0em;
}

.tocheading {
    font-family:	sans-serif;
    font-weight:	bold;
    color:		#005A9C;
    background: 	transparent;
}

PRE {
    display: 		block;
    font-family:	monospace;
    white-space:	pre;
    margin:		0%;
    padding-top:	0.5ex;
    padding-bottom:	0.5ex;
    padding-left:	1ex;
    padding-right:	1ex;
    width:		100%;
}
PRE.syntax {
    color: 		black;
    background: 	#80ffff;
    border:		1px solid black;
    font-family:	serif;
}
PRE.example {
    color: 		black;
    background: 	#f5dcb3;
    border:		1px solid black;
}

PRE.sample {
    color: 		black;
    background: 	#f5dcb3;
    border:		1px solid black;
}

DIV.arglist {
    border:		1px solid black;
    width:		100%;
}
TH, THEAD TR, TR.heading {
    color:		#005A9C;
    background:		#DDDDDD;
    text-align:	 	center;
    font-family:	sans-serif;
    font-weight:	bold;
}
TR.syntax {
    color: 		black;
    background:		#80ffff;
}
TR.desc {
    color: 		black;
    background:		#f5dcb3;
}

/* TR.row[01] are used to get alternately colored table rows. 
 * Could probably choose better colors here...
 */
TR.row0 {
    color: 		black;
    background:		#efffef;
}

TR.row1 {
    color: 		black;
    background:		#efefff;
}

/* Workaround for Netscape bugs:
 * Netscape doesn't seem to compute table widths properly.
 * unless they're wrapped inside a DIV.  (Additionally,
 * it appears to require a non-zero border-width.)
 */
DIV.table {
    border-width:	1px;
    border-color:	white;
    width:		100%;
}
DIV.menu {	/* Wrapper for TABLE class="menu" */
    margin-top:		10px;
    margin-bottom:	10px;
    border:		thin solid #005A9C;
    width:		100%;
    margin-left:	5%;
}

VAR {
    font-style: italic;
}

/* For debugging: highlight unrecognized elements: */
.unrecognized {
    color: red; background: green;
}

/* EOF */

Added pkg/pkgIndex.tcl.





>
>
1
2
# Winico pkgIndex
package ifneeded Winico 0.7.1 [list load [file join $dir Winico0.7.1.dll]]

Added pkg/sample.tcl.





























































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
# sample.tcl -
#
#  This is one of the original test files provided with the winico package.
#
# $Id: sample.tcl,v 1.1 2005/09/27 02:05:50 patthoyts Exp $

package require Tk 8.4

# Load winico - first look in the build directories for a dll to load
#
set srcdir [file dirname [file dirname [info script]]]
set dbgfile [file join $srcdir win Debug winico06g.dll]
set relfile [file join $srcdir win Release winico06.dll]
if {[file exists $dbgfile]} {
    load $dbgfile
} elseif {[file exists $relfile]} {
    load $relfile
} else {
    tk_messageBox -message "not found in $dbgfile"
}

package require Winico 0.6

# -------------------------------------------------------------------------

console show
wm withdraw .
set dir [pwd]
if ![catch {winico load exclamation} msg] {
    puts "1st Exclamation Test passed."
    console eval "wm geometry . 50x25+0+0"
    wm deiconify .;
    raise .;
    update
    wm geometry . +400+0
    update

    winico setwindow . [set msg]
    puts "you should see a exclamation icon in ."
    update
    after 4000
 
    set ndx [lsearch -glob [info loaded] *winico*]
    foreach {dll pkg} [lindex [info loaded] $ndx] break
    set l [winico load leo $dll]
    winico setwindow . [set l]
    puts "you should see a leo icon in ."
    update
    after 4000

    source [file join [file dirname [info script]] sample2.tcl]
    puts "you should see a red smiley in ."
    puts "and a yellow smiley in the Task-List"
    puts "now go to the taskbar status area"
    puts "and move the mouse over the smiley"
    after 20000 "exit 0"

} else {
    puts "[set msg]:Winico failed to load and run"
    after 5000 "exit 1"
}

Added pkg/sample2.tcl.











































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
# sample2.tcl -
#
# This file is loaded by sample.tcl
#
# $Id: sample2.tcl,v 1.1 2005/09/27 02:05:50 patthoyts Exp $

lappend auto_path [pwd]
package require Winico

proc winico_seticon { w icofile } {
  set ico [winico create $icofile]
  set screendepth [winfo screendepth .]
  set bigsize "32x32"
  set bigpos -1
  set bigdepth 0
  set smallsize "16x16"
  set smallpos -1
  set smalldepth 0
  foreach i [winico info $ico] {
    array set opts $i
    set depth    $opts(-bpp)
    set pos      $opts(-pos)
    set geometry $opts(-geometry)
    if { $geometry=="$bigsize" && $depth<=$screendepth } {
      if { $depth>$bigdepth } {
        set bigpos $pos
        set bigdepth $depth
      }
    } elseif { $geometry=="$smallsize" && $depth<=$screendepth } {
      if { $depth>$smalldepth } {
        set smallpos $pos
        set smalldepth $depth
      }
    }
  }
    if { $bigpos==-1 && $smallpos==-1 } {
    puts stderr "couldn't find $bigsize and $smallsize icons in $icofile"
    return $ico
  } elseif { $bigpos==-1 } {
    set bigpos $smallpos
    puts stderr "couldn't find $bigsize icons in $icofile"
  } elseif { $smallpos==-1 } {
    set smallpos $bigpos
    puts stderr "couldn't find $smallsize icons in $icofile"
  }
  #puts stderr "big icon is $bigsize,bpp:$bigdepth,pos:$bigpos"
  #puts stderr "small icon is $smallsize,bpp:$smalldepth,pos:$smallpos"
  winico setwindow $w $ico big   $bigpos
  winico setwindow $w $ico small $smallpos
  return $ico
}
proc winico_delall {} {
  foreach i [winico info] { winico delete $i }
}
proc winico_loadicon { w symbol } {
  set ico [winico load $symbol]
  winico setwindow $w $ico big
  winico setwindow $w $ico small
}
proc taskbar_cmd { message ico wparam lparam x y } {
  puts stderr "taskbar_cmd with $message,ico:$ico,wParam:$wparam,lParam:$lparam,x:$x,y:$y"
}
update
set ico [winico_seticon . [file join [file dirname [info script]] smiley.ico]]
#new new new
winico text $ico "This is a taskbar sample text"
winico taskbar add $ico -callback "taskbar_cmd %m %i %w %l %x %y"

after 20000 "winico delete $ico"

Added pkg/smiley.ico.

cannot compute difference between binary files

Added pkg/tkchat.ico.

cannot compute difference between binary files

Added pkg/winico.html.

































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<html><head>
<title>winico - winico </title>
<link rel="stylesheet" href="manpage.css" type="text/css"></head>
<! -- Generated from file '..\doc\winico.man' by tcllib/doctools with format 'html'
   -->
<! -- Copyright &copy; 1997-1998, Brueckner&amp;Jarosch
   -->
<! -- CVS: $Id$ winico.n
   -->

<body>
<h1> winico(n) 0.6 winico &quot;winico&quot;</h1>
<h2><a name="name">NAME</a></h2>
<p>
<p> winico - Windows Icon extension for Tcl/Tk





<h2><a name="table_of_contents">TABLE OF CONTENTS</a></h2>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#table_of_contents">TABLE OF CONTENTS</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#synopsis">SYNOPSIS</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#description">DESCRIPTION</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#commands">COMMANDS</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#callback_events">CALLBACK EVENTS</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#examples">EXAMPLES</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#authors">AUTHORS</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#keywords">KEYWORDS</a><br>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="#copyright">COPYRIGHT</a><br>
<h2><a name="synopsis">SYNOPSIS</a></h2>
<p>
package require <b>Tcl 8.4</b><br>
package require <b>winico ?0.6?</b><br>
<br><table border=1 width=100% cellspacing=0 cellpadding=0><tr            bgcolor=lightyellow><td bgcolor=lightyellow><table 0 width=100% cellspacing=0 cellpadding=0><tr valign=top ><td ><a href="#1"><b class='cmd'>winico createfrom</b> <i class='arg'>filename</i></a></td></tr>
<tr valign=top ><td ><a href="#2"><b class='cmd'>winico load</b> <i class='arg'>resourcename</i> ?<i class='arg'>filename</i>?</a></td></tr>
<tr valign=top ><td ><a href="#3"><b class='cmd'>winico info</b> ?<i class='arg'>id</i>?</a></td></tr>
<tr valign=top ><td ><a href="#4"><b class='cmd'>winico setwindow</b> <i class='arg'>windowid</i> <i class='arg'>id</i> ?<i class='arg'>big|small</i>? ?<i class='arg'>pos</i>?</a></td></tr>
<tr valign=top ><td ><a href="#5"><b class='cmd'>winico delete</b> <i class='arg'>id</i></a></td></tr>
<tr valign=top ><td ><a href="#6"><b class='cmd'>winico taskbar</b> <i class='arg'>add|modify|delete</i> <i class='arg'>id</i> ?<i class='arg'>-callback procname</i>? ?<i class='arg'>-pos num</i>? ?<i class='arg'>-text tooltiptext</i>?</a></td></tr>
</table></td></tr></table>
<h2><a name="description">DESCRIPTION</a></h2>
<p>
<p>

The Winico extension is an extension that provides enhanced
handling of icons for Tk applications under Window. This includes
loading icons from icon files and from executable resources and also
provides Taskbar icon management.

<h2><a name="commands">COMMANDS</a></h2>
<p>

<dl>

<dt><a name="1"><b class='cmd'>winico createfrom</b> <i class='arg'>filename</i></a><dd>


Load icons from a .ico file. If successful this command returns an
identifier that should be used to access this resource with other 
<b class='cmd'>winico</b> commands.  Icon files often contain several icons for
different color depths of the screen and may contain different sizes
(e.g. 16x16 and 32x32 icons) To obtain information on what has been
loaded, see the <b class='cmd'>info</b> subcommand.

<br><br>
<dt><a name="2"><b class='cmd'>winico load</b> <i class='arg'>resourcename</i> ?<i class='arg'>filename</i>?</a><dd>


Load icons from the resource section of an executable file (.exe or .dll).
Only icons for the current colordepth are loaded (unlike the <b class='cmd'>createfrom</b>
subcommand). <i class='arg'>resourcename</i> may be a string identifier or a number
that identifies the item in the resource file. If no <i class='arg'>filename</i> is
given then you may load some standard windows icons (defined for the
LoadIcon Win32 API function). The following names are recognised:
&quot;application&quot;, &quot;asterisk&quot;, &quot;error&quot;, &quot;exclamation&quot;, &quot;hand&quot;, &quot;question&quot;,
&quot;information&quot;, &quot;warning&quot;, and &quot;winlogo&quot;.

<br><br>
<dt><a name="3"><b class='cmd'>winico info</b> ?<i class='arg'>id</i>?</a><dd>


Returns information about icon resources managed by this extension. If
<i class='arg'>id</i> is ommitted then the command returns a list of all the
current icon ids. If an icon <i class='arg'>id</i> is specified then the result is
a list option value pairs for each icon in the icon resource For
identifiers created via <b class='cmd'>winico load</b> the values given are not
relevant.

<br><br>
<dl>
<dt><strong>-pos</strong><dd>
   position inside the iconresource (beginning from 0)
<br><br>
<dt><strong>-width</strong><dd>
   width of the icon in pixels
<br><br>
<dt><strong>-height</strong><dd>
  height of the icon in pixels
<br><br>
<dt><strong>-geometry</strong><dd>
  widthxheight in pixels (for convenience)
<br><br>
<dt><strong>-bpp</strong><dd>
      color depth in bits per pixel (4 means 16 colors)
<br><br>
<dt><strong>-hicon</strong><dd>
    windows icon handle for this icon (in the actual version
    all hicons of a resource will be immediately created after 
    reading from disk, in later versions this may occur on demand , 
    to economize system load )
<br><br>
<dt><strong>-ptr</strong><dd>
      C-pointer of the internal struct, only interesting for hackers :-)
</dl>

<dt><a name="4"><b class='cmd'>winico setwindow</b> <i class='arg'>windowid</i> <i class='arg'>id</i> ?<i class='arg'>big|small</i>? ?<i class='arg'>pos</i>?</a><dd>


Set the icon for the toplevel window given by <i class='arg'>windowid</i> which may
be either the Tk pathname for the window, or a valid Windows HWND
value (for use with non-Tk windows).
<br><br>
NOTE: the window MUST BE MAPPED. If not then this may fail or crash.
<br><br>
<dl>
<dt><strong>big|small</strong><dd>
   If ?big|small? is &quot;big&quot; (default) the taskbar icon AND the caption icon 
   are set under Win95/WinNT, if it's &quot;small&quot; only the small caption icon
   is set (via WM_SETICON).
   <br><br>
   Under Win32s ?big|small? is ignored (setting done via SetClassLong())
   (for setting individual toplevels different Windows-classnames per toplevel
   would be required , TkToplevel0, TkToplevel1 and so on)

<br><br>
<dt><strong>pos</strong><dd>
   If <i class='arg'>pos</i> is set, the icon with that -pos number is used for setting
   the toplevel icon, otherwise winico chooses the first 32x32 16color icon
   (this could be improved)
   <br><br>
   This option only has an effect on icons created with the &quot;createfrom&quot;
   subcommand (FIX ME: check this)
</dl>

<dt><a name="5"><b class='cmd'>winico delete</b> <i class='arg'>id</i></a><dd>


Free the resource identified by <i class='arg'>id</i>. This command is
always successful, even in <i class='arg'>id</i> is invalid.

<br><br>
<dt><a name="6"><b class='cmd'>winico taskbar</b> <i class='arg'>add|modify|delete</i> <i class='arg'>id</i> ?<i class='arg'>-callback procname</i>? ?<i class='arg'>-pos num</i>? ?<i class='arg'>-text tooltiptext</i>?</a><dd>


   Modify an icon registration with the Windows taskbar. To add and
   icon to the system tray you use the <i class='arg'>add</i> command and provide a
   suitable winico <i class='arg'>id</i>. You should probably also provide a 
   <i class='arg'>-text</i> argument otherwise the icon id will be used for the
   text too (Windows displays the text as a tooltip).
   <br><br>
   To modify the text or the icon displayed in the taskbar status area,
   use the <i class='arg'>-pos</i> and <i class='arg'>-text</i> options to change the current
   settings.
   <br><br>
   The remove an icon from the status area use the <i class='arg'>delete</i> and
   give the <i class='arg'>id</i> provided to the <i class='arg'>add</i> command.
   <br><br>
   The optional <i class='arg'>-callback</i> is for specifying a Tcl procedure to
   be called when events occur on the icon. This includes mouse motion
   and button click events.
   In the callback there are bind-like abbreviations possible :
<br><br>
<dl>
<dt><strong>%m</strong><dd> the windows message specifier (see CALLBACK EVENTS)
<br><br>
<dt><strong>%i</strong><dd> the icon identifier           (ico#1, ico#2 a.s.o)
<br><br>
<dt><strong>%x</strong><dd> current xposition of the cursor 
<br><br>
<dt><strong>%y</strong><dd> current yposition of the cursor 
<br><br>
<dt><strong>%X</strong><dd> xposition of the cursor when the last windows-message was processed
<br><br>
<dt><strong>%Y</strong><dd> yposition of the cursor when the last windows-message was processed
        (%X,%Y may differ from %x,%y however the difference is marginal )
<br><br>
<dt><strong>%t</strong><dd> current tickcount of the system
<br><br>
<dt><strong>%w</strong><dd> wParam of the internal callback message (the integer part of %i)
<br><br>
<dt><strong>%l</strong><dd> lParam of the internal callback message (integer value of %m)
<br><br>
<dt><strong>%%</strong><dd> a %-sign
</dl>

</dl>

<h2><a name="callback_events">CALLBACK EVENTS</a></h2>
<p>
<p>
The following Windows events will generate calls to a callback
procedure registered by the <b class='cmd'>winico taskbar</b> command. The
following lists all the possible values of %m in the callback
substitution. Typically WM_RBUTTONUP is bound to a context menu.
<dl>
<dt><strong>WM_MOUSEMOVE</strong><dd>
<dt><strong>WM_LBUTTONDOWN</strong><dd>
<dt><strong>WM_LBUTTONUP</strong><dd>
<dt><strong>WM_LBUTTONDBLCLK</strong><dd>
<dt><strong>WM_RBUTTONDOWN</strong><dd>
<dt><strong>WM_RBUTTONUP</strong><dd>
<dt><strong>WM_RBUTTONDBLCLK</strong><dd>
<dt><strong>WM_MBUTTONDOWN</strong><dd>
<dt><strong>WM_MBUTTONUP</strong><dd>
<dt><strong>WM_MBUTTONDBLCLK</strong><dd>
</dl>

<h2><a name="examples">EXAMPLES</a></h2>
<p>

<p>
<p><table><tr><td bgcolor=black>&nbsp;</td><td><pre class='sample'>
% winico createfrom icons/smiley.ico
ico#5
% winico info ico#5
{-pos 0 -width 32 -height 32 -geometry 32x32 -bpp 4 -hicon 0x670081 -ptr 0xf99884} {-pos 1 -width 16 -height 16 -geometry 16x16 -bpp 4 -hicon 0xad01b4 -ptr 0xf99884}
</pre></td></tr></table></p>

<p>
<p><table><tr><td bgcolor=black>&nbsp;</td><td><pre class='sample'>
set contextmenu [menu .popup -tearoff 0]
...
winico taskbar add ico#1 -pos 1 -text &quot;big tcp server&quot;  -callback &quot;mytclproc $contextmenu %m %i %X %Y&quot;
...
proc mytclproc {contextmenu msg ico x y } {
  if { $msg==&quot;WM_RBUTTONUP&quot; } {
    $contextmenu post $x $y
  }
}
</pre></td></tr></table></p>
   
<h2><a name="authors">AUTHORS</a></h2>
<p>
Leo Schubert, Brueckner&amp;Jarosch Ing.-GmbH
<p>
Modifications by Pat Thoyts.


<h2><a name="keywords">KEYWORDS</a></h2>
<p>
icons, system tray, taskbar
<h2><a name="copyright">COPYRIGHT</a></h2>
<p>
Copyright &copy; 1997-1998, Brueckner&amp;Jarosch<br>
</body></html>

Added src/build.sh.



>
1
gcc tkwinico.c -L/c/Tcl/lib -I/c/Tcl/include -DPACKAGE_NAME=\"Winico\" -DPACKAGE_VERSION=\"0.7.1\"  -L/c/Tcl/lib -ltclstub87 -L/c/Tcl/lib -ltkstub87 -DUSE_TCL_STUBS -DUSE_TK_STUBS -lgdi32 -shared -o ../pkg/Winico0.7.1.dll

Added src/tkwinico.c.























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
/* 
   tkwinico.c implements a "winico" tcl-command which permits to
   change the Taskbar and Titlebar-Icon of a Tk-Toplevel
   Copyright Brueckner & Jarosch Ing.GmbH,Erfurt,Germany 1998
   hacked by Leo
   ripped my hairs out at MakeIconFromResource16
*/
/* most of the icon handling functions are taken from the Microsoft */
/* IconPro Win32 SDK-sample                                         */
/****************************************************************************\
*     FILES:     ICONS.C  DIB.C
*     PURPOSE:  IconPro Project Icon handing Code C file
*     COMMENTS: This file contains the icon handling code
*     FUNCTIONS:
*       MakeIconFromResource32
*       ReadIconFromICOFile
*       AdjustIconImagePointers
*       ReadICOHeader
*       DrawXORMask
*       DrawANDMask
*       FindDIBBits
*       DIBNumColors
*       PaletteSize
*       BytesPerLine
*
*     Copyright 1995-1996 Microsoft Corp.
* History:
*                July '95 - Created
\****************************************************************************/

/* rest is copyright Brueckner & Jarosch */

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>

#include <tk.h>
#include <tkPlatDecls.h>

#define GETHINSTANCE Tk_GetHINSTANCE()
static int isWin32s=-1;
#define ISWIN32S  isWin32s

#if defined(_MSC_VER)
#    define EXPORT(a,b) __declspec(dllexport) a b
#    define DllEntryPoint DllMain
#    define snprintf _snprintf
#else
#    if defined(__BORLANDC__)
#        define EXPORT(a,b) a _export b
#    else
#        define EXPORT(a,b) a b
#    endif
#endif

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif

/* Deal with Tcl 8.4 constificiation */
#ifndef CONST84
#define CONST84
#endif

//#define ICO_DEBUG

typedef struct {
    UINT         Width, Height, Colors; // Width, Height and bpp
    LPBYTE       lpBits;                // ptr to DIB bits
    DWORD        dwNumBytes;            // how many bytes?
    LPBITMAPINFO lpbi;                  // ptr to header
    LPBYTE       lpXOR;                 // ptr to XOR image bits
    LPBYTE       lpAND;                 // ptr to AND image bits
    HICON        hIcon;                 // DAS ICON
} ICONIMAGE, *LPICONIMAGE;

typedef struct {
    BOOL         bHasChanged;                     // Has image changed?
    TCHAR        szOriginalICOFileName[MAX_PATH]; // Original name
    TCHAR        szOriginalDLLFileName[MAX_PATH]; // Original name
    int          nNumImages;                      // How many images?
    ICONIMAGE    IconImages[1];                   // Image entries
} ICONRESOURCE, *LPICONRESOURCE;

// These next two structs represent how the icon information is stored
// in an ICO file.
// The following two structs are for the use of this program in
// manipulating icons. They are more closely tied to the operation
// of this program than the structures listed above. One of the
// main differences is that they provide a pointer to the DIB
// information of the masks.
typedef struct {
    BYTE         bWidth;               // Width of the image
    BYTE         bHeight;              // Height of the image (times 2)
    BYTE         bColorCount;          // Number of colors (0 if >=8bpp)
    BYTE         bReserved;            // Reserved
    WORD         wPlanes;              // Color Planes
    WORD         wBitCount;            // Bits per pixel
    DWORD        dwBytesInRes;         // how many bytes in this resource?
    DWORD        dwImageOffset;        // where in the file is this image
} ICONDIRENTRY, *LPICONDIRENTRY;

typedef struct {
    WORD         idReserved;           // Reserved
    WORD         idType;               // resource type (1 for icons)
    WORD         idCount;              // how many images?
    ICONDIRENTRY idEntries[1];         // the entries for each image
} ICONDIR, *LPICONDIR;

// How wide, in bytes, would this many bits be, DWORD aligned?
#define WIDTHBYTES(bits)      ((((bits) + 31)>>5)<<2)

// used function prototypes
static LPSTR FindDIBBits (LPSTR lpbi);
static WORD  DIBNumColors (LPSTR lpbi);
static WORD  PaletteSize (LPSTR lpbi);
static DWORD BytesPerLine( LPBITMAPINFOHEADER lpBMIH );

///
// Prototypes for local functions
//
static int ReadICOHeader( Tcl_Channel channel );
static BOOL AdjustIconImagePointers( LPICONIMAGE lpImage );

typedef struct IcoInfo {
    HICON hIcon;           /* icon handle returned by LoadIcon*/
    int itype;
    int id;                /* Integer identifier for command;  used to
                            * cancel it. */
    LPICONRESOURCE lpIR;   /* IconresourcePtr if type==ICO_FILE */
    int iconpos;           /* hIcon is the nth Icon*/
    char* taskbar_txt;     /* malloced text to display in the taskbar*/
    Tcl_Interp* interp;    /* interp which created the icon*/
    char* taskbar_command; /* command to eval if events in the taskbar arrive*/
    int taskbar_flags;     /* taskbar related flags*/
    HWND hwndFocus;
    struct IcoInfo *nextPtr;
} IcoInfo;

static IcoInfo* firstIcoPtr = NULL;
#define ICO_LOAD   1
#define ICO_FILE   2

#define TASKBAR_ICON 1
#define ICON_MESSAGE WM_USER+1234

#ifdef __TURBOC__
#define NIM_ADD         0x00000000
#define NIM_MODIFY      0x00000001
#define NIM_DELETE      0x00000002

#define NIF_MESSAGE     0x00000001
#define NIF_ICON        0x00000002
#define NIF_TIP         0x00000004
typedef struct _NOTIFYICONDATA { // nid
    DWORD cbSize;
    HWND hWnd;
    UINT uID;
    UINT uFlags;
    UINT uCallbackMessage;
    HICON hIcon;
    char szTip[64];
} NOTIFYICONDATA, *PNOTIFYICONDATA;
#endif // __TURBOC__

#define HANDLER_CLASS "Wtk_TaskbarHandler"
static HWND CreateTaskbarHandlerWindow(void);

typedef BOOL (FAR WINAPI * LPFN_SHELLNOTIFYICONW)(DWORD, PNOTIFYICONDATAW);
typedef BOOL (FAR WINAPI * LPFN_SHELLNOTIFYICONA)(DWORD, PNOTIFYICONDATAA);

static LPFN_SHELLNOTIFYICONA notify_funcA = NULL;
static LPFN_SHELLNOTIFYICONW notify_funcW  = NULL;
static HMODULE hmod          = NULL;
static HWND    handlerWindow = NULL;

/* ---------------------------------------------------------------------- */

//
// this bloody proc tries to swap the lines of the bitmap in various formats
//
static void 
swaplines(unsigned char *bits,int width,int height,int bpp)
{
#define MAX_LINE 512
    unsigned char line[MAX_LINE];
    unsigned char* pline;
    int i,bytesperline,middle,end;
    if(bpp>8){
        bytesperline=width*(bpp/8);
        // for size 16  bpp 16 bytesperline 32
        // for size 32  bpp 16 bytesperline 64
        // for size 16  bpp 24 bytesperline 48
        // for size 32  bpp 24 bytesperline 96
    } else {
        bytesperline=width/(8/bpp);
        // for size 16  bpp 8  bytesperline 16
        // for size 32  bpp 8  bytesperline 32
        // for size 16  bpp 4  bytesperline 8
        // for size 32  bpp 4  bytesperline 16
        // for size 16  bpp 1  bytesperline 2
        // for size 32  bpp 1  bytesperline 4
    }
    if (bytesperline<MAX_LINE)
        pline=line;
    else
        pline = (unsigned char*)ckalloc(bytesperline);
    middle = (height * bytesperline) / 2;
    end = (height - 1) * bytesperline;
#ifdef ICO_DEBUG
    dprintf("swap width:%d bpp:%d bytesperline:%d height:%d middle:%d end:%d\n",
        width,bpp,bytesperline,height,middle,end);
#endif
    for (i = 0; i < middle ; i+= bytesperline) {
            memcpy(pline, bits + i, bytesperline);
        memcpy(bits + i, bits + (end - i), bytesperline);
        memcpy(bits + (end - i), pline, bytesperline);
    }
    if (pline != line) {
        ckfree((char*)pline);
    }
}
 
#ifdef ICO_DEBUG
static void 
hex(unsigned char* p)
{
    int h = (int)*p;
    dprintf(" %2.2x",h);
}
 
static void 
printlines(unsigned char *bits,int width,int height,int bpp)
{
    int i,j,bytesperline,end;
    //if(width>16 || height>16)
    //  return;
    if(bpp>8){
        bytesperline=width*(bpp/8);
        // for size 16  bpp 16 bytesperline 32
        // for size 32  bpp 16 bytesperline 64
        // for size 16  bpp 24 bytesperline 48
        // for size 32  bpp 24 bytesperline 96
    } else {
        bytesperline=width/(8/bpp);
        // for size 16  bpp 8  bytesperline 16
        // for size 32  bpp 8  bytesperline 32
        // for size 16  bpp 4  bytesperline 8
        // for size 32  bpp 4  bytesperline 16
        // for size 16  bpp 1  bytesperline 2
        // for size 32  bpp 1  bytesperline 4
    }
    end=height*bytesperline;
    dprintf("--\n");
    for(i=0;i<end;i+=bytesperline){
        for(j=0;j<bytesperline;j++){
            hex(bits+i+j);
        }
        dprintf("\n");
    }
    dprintf("--\n");
}
#endif // ICO_DEBUG
 
static HICON 
MakeIconFromResource16(LPICONIMAGE lpIcon)
{
    HICON hIcon;
    UINT cb;
    HBITMAP hBmp;
    BITMAP bm;
    LPBYTE lpbDst;
    HDC hdc;
    int width=(*(LPBITMAPINFOHEADER)(lpIcon->lpBits)).biWidth;
    int height=(*(LPBITMAPINFOHEADER)(lpIcon->lpBits)).biHeight/2;
#ifdef ICO_DEBUG
    int planes= (*(LPBITMAPINFOHEADER)(lpIcon->lpBits)).biPlanes;
#endif
    int bitCount=(*(LPBITMAPINFOHEADER)(lpIcon->lpBits)).biBitCount;
    if( lpIcon == NULL )
        return NULL;
    if( lpIcon->lpBits == NULL )
        return NULL;
#ifdef ICO_DEBUG
    dprintf("CreateIcon width:%d height:%d planes:%d bpp:%d\n",
        width,height,planes,bitCount);
#endif
    swaplines(lpIcon->lpAND,width,height,1);
#ifdef ICO_DEBUG
    printlines(lpIcon->lpXOR,width,height,bitCount);
#endif
    hdc=GetDC(NULL);
    if ( bitCount==1 || bitCount > 8)
        hBmp=CreateBitmap((UINT)width, (UINT)height, 1, 1, NULL);
    else
        hBmp=CreateCompatibleBitmap(hdc, (UINT)width, (UINT)height);

    if(!hBmp){
        ReleaseDC(NULL,hdc);
        return NULL;
    }
    (*(LPBITMAPINFOHEADER)(lpIcon->lpBits)).biHeight /= 2;
    if (!SetDIBits(hdc, hBmp, 0, (UINT)height, (LPVOID)lpIcon->lpXOR,
        (LPBITMAPINFO)lpIcon->lpBits, DIB_RGB_COLORS)) {
        DeleteObject(hBmp);
        ReleaseDC(NULL,hdc);
        return NULL;
    }
    ReleaseDC(NULL,hdc);
    (*(LPBITMAPINFOHEADER)(lpIcon->lpBits)).biHeight *= 2;
    GetObject(hBmp, sizeof(bm), &bm);
    cb=bm.bmHeight*bm.bmWidthBytes * bm.bmPlanes;
    //if (cb % 4 != 0)        // dword align
    //  cb += 4 - (cb % 4);
    lpbDst=(LPBYTE)ckalloc(cb);
    if(!GetBitmapBits(hBmp, cb, (LPVOID)lpbDst)){
        DeleteObject(hBmp);
        ckfree((char*)lpbDst);
        return NULL;
    }
#ifdef ICO_DEBUG
    printlines(lpbDst,width,height,bm.bmBitsPixel);
#endif
    hIcon = CreateIcon(GETHINSTANCE,width,height,
        (BYTE)bm.bmPlanes,(BYTE)bm.bmBitsPixel,
        lpIcon->lpAND,lpbDst);
    DeleteObject(hBmp);
    ckfree((char*)lpbDst);
    return hIcon;
}
 
/****************************************************************************
 *
 *     FUNCTION: MakeIconFromResource32
 *
 *     PURPOSE:  Makes an HICON from an icon resource
 *
 *     PARAMS:   LPICONIMAGE        lpIcon - pointer to the icon resource
 *
 *     RETURNS:  HICON - handle to the new icon, NULL for failure
 *
 * History:
 *                July '95 - Created
 *
\****************************************************************************/

static HICON 
MakeIconFromResource32( LPICONIMAGE lpIcon )
{
    HICON hIcon ;
    static FARPROC pfnCreateIconFromResourceEx=NULL;
    static int initinfo=0;
    // Sanity Check
    if( lpIcon == NULL )
        return NULL;
    if( lpIcon->lpBits == NULL )
        return NULL;
    if(!initinfo){
        HMODULE hMod = GetModuleHandleA("USER32.DLL");
        initinfo=1;
        if(hMod){
            pfnCreateIconFromResourceEx = GetProcAddress(hMod,"CreateIconFromResourceEx");
        }
    }
    // Let the OS do the real work :)
    if(pfnCreateIconFromResourceEx!=NULL) {
        hIcon = (HICON) (pfnCreateIconFromResourceEx)
            (lpIcon->lpBits, lpIcon->dwNumBytes, TRUE, 0x00030000,
                (*(LPBITMAPINFOHEADER)(lpIcon->lpBits)).biWidth,
                (*(LPBITMAPINFOHEADER)(lpIcon->lpBits)).biHeight/2, 0 );
    } else {
        hIcon = NULL;
    }
    // It failed, odds are good we're on NT so try the non-Ex way
    if( hIcon == NULL )    {
        // We would break on NT if we try with a 16bpp image
        if(lpIcon->lpbi->bmiHeader.biBitCount != 16) {
            hIcon = CreateIconFromResource( lpIcon->lpBits, lpIcon->dwNumBytes, TRUE, 0x00030000 );
        }
    }
    return hIcon;
}
 
static HICON 
MakeIconFromResource( LPICONIMAGE lpIcon )
{
    if (ISWIN32S)
        return MakeIconFromResource16(lpIcon);
    else
        return MakeIconFromResource32(lpIcon);
}
 
static void 
FreeIconResource(LPICONRESOURCE lpIR)
{
    int i;
    if( lpIR == NULL )
        return;
    // Free all the bits
    for( i=0; i< lpIR->nNumImages; i++ ) {
        if( lpIR->IconImages[i].lpBits != NULL )
            ckfree((char*)lpIR->IconImages[i].lpBits );
        if( lpIR->IconImages[i].hIcon != NULL )
            DestroyIcon(lpIR->IconImages[i].hIcon);
    }
    ckfree( (char*)lpIR );
}
 
/****************************************************************************
*
*     FUNCTION: ReadIconFromICOFile
*
*     PURPOSE:  Reads an Icon Resource from an ICO file
*
*     PARAMS:   LPCTSTR szFileName - Name of the ICO file
*
*     RETURNS:  LPICONRESOURCE - pointer to the resource, NULL for failure
*
* History:
*                July '95 - Created
*                14-Apr-2004: Use VFS file access API (Pat Thoyts).
*
\****************************************************************************/

LPICONRESOURCE 
ReadIconFromICOFile(Tcl_Interp* interp, LPCSTR szFileName)
{
    LPICONRESOURCE   lpIR = NULL, lpNew = NULL;
    int              i = 0;
    DWORD            dwBytesRead = 0;
    LPICONDIRENTRY   lpIDE = NULL;
    Tcl_Obj         *oPath = NULL;
    Tcl_Channel      channel;
    
    Tcl_ResetResult(interp);
    
    /*
     * Access the icon file. We use Tcl_FS* methods to support virtual 
     * filesystems.
     */
    oPath = Tcl_NewStringObj(szFileName, -1);
    Tcl_IncrRefCount(oPath);
    
    channel = Tcl_FSOpenFileChannel(interp, oPath, "r", 0644);
    if (channel == NULL) {
        return NULL;
    }
    
    if (Tcl_SetChannelOption(interp, channel, 
        "-translation", "binary") != TCL_OK) {
        return NULL;
    }
    
    // Allocate memory for the resource structure
    if ((lpIR = (LPICONRESOURCE) ckalloc( sizeof(ICONRESOURCE) )) == NULL ) {
        Tcl_AppendResult(interp, "Error Allocating Memory", (char *)NULL);
        goto close_error;
    }
    // Read in the header
    if ((lpIR->nNumImages = ReadICOHeader(channel)) == -1 ) {
        Tcl_AppendResult(interp, "Error Reading File Header", (char*)NULL);
        goto close_error;
    }
    // Adjust the size of the struct to account for the images
    if( (lpNew = (LPICONRESOURCE) ckrealloc( (char*)lpIR, sizeof(ICONRESOURCE) + ((lpIR->nNumImages-1) * sizeof(ICONIMAGE)) )) == NULL )    {
        Tcl_AppendResult(interp,"Error Allocating Memory",(char*)NULL);
        goto close_error;
    }

    lpIR = lpNew;
    // Store the original name
    lstrcpy( lpIR->szOriginalICOFileName, szFileName );
    lstrcpy( lpIR->szOriginalDLLFileName, "" );
    // Allocate enough memory for the icon directory entries
    if( (lpIDE = (LPICONDIRENTRY) ckalloc( lpIR->nNumImages * sizeof( ICONDIRENTRY ) ) ) == NULL )     {
        Tcl_AppendResult(interp,"Error Allocating Memory",(char*)NULL);
        goto close_error;
    }
    // Read in the icon directory entries
    if ((dwBytesRead = Tcl_Read(channel, (char*)lpIDE,
        lpIR->nNumImages * sizeof(ICONDIRENTRY))) < 0) {
        Tcl_AppendResult(interp,"Error Reading File",(char*)NULL);
        goto close_error;
    }
    if( dwBytesRead != lpIR->nNumImages * sizeof( ICONDIRENTRY ) )    {
        Tcl_AppendResult(interp,"Error Reading File",(char*)NULL);
        goto close_error;
    }
    // Loop through and read in each image
    for( i = 0; i < lpIR->nNumImages; i++ )    {
        // Allocate memory for the resource
        if( (lpIR->IconImages[i].lpBits = (LPBYTE) ckalloc(lpIDE[i].dwBytesInRes)) == NULL )
        {
            Tcl_AppendResult(interp,"Error Allocating Memory",(char*)NULL);
            goto close_error;
        }
        lpIR->IconImages[i].dwNumBytes = lpIDE[i].dwBytesInRes;
        // Seek to beginning of this image
        if (Tcl_Seek(channel, lpIDE[i].dwImageOffset, SEEK_SET) < 0) {
            Tcl_AppendResult(interp,"Error Seeking in File",(char*)NULL);
            goto close_error;
        }
        // Read it in
        if ((dwBytesRead = Tcl_Read(channel, lpIR->IconImages[i].lpBits, 
            lpIDE[i].dwBytesInRes)) < 0)
        {
            Tcl_AppendResult(interp,"Error Reading File",(char*)NULL);
            goto close_error;
        }
        if( dwBytesRead != lpIDE[i].dwBytesInRes )
        {
            Tcl_AppendResult(interp,"Error Reading File",(char*)NULL);
            goto close_error;
        }
        // Set the internal pointers appropriately
        if( ! AdjustIconImagePointers( &(lpIR->IconImages[i]) ) )
        {
            Tcl_AppendResult(interp,"Error Converting to Internal Format",(char*)NULL);
            goto close_error;
        }
        lpIR->IconImages[i].hIcon=MakeIconFromResource(&(lpIR->IconImages[i]));
        //lpIR->IconImages[i].hIcon=MakeIconFromResource(&(lpIR->IconImages[i]));
        /*
          dprintf("icon %d:width=%d height=%d bpp=%d hicon=0x%x\n",
          i,
          lpIR->IconImages[i].Width,lpIR->IconImages[i].Height,
          lpIR->IconImages[i].Colors,lpIR->IconImages[i].hIcon);
        */
    }
    // Clean up
    ckfree( (char*)lpIDE );
    Tcl_Close(interp, channel);
    Tcl_DecrRefCount(oPath);
    return lpIR;

 close_error:
    Tcl_Close(interp, channel);
    Tcl_DecrRefCount(oPath);
    ckfree((char *)lpIR);
    ckfree((char *)lpIDE);
    return NULL;
}
 
/****************************************************************************
*
*     FUNCTION: AdjustIconImagePointers
*
*     PURPOSE:  Adjusts internal pointers in icon resource struct
*
*     PARAMS:   LPICONIMAGE lpImage - the resource to handle
*
*     RETURNS:  BOOL - TRUE for success, FALSE for failure
*
* History:
*                July '95 - Created
*
\****************************************************************************/

static BOOL
AdjustIconImagePointers( LPICONIMAGE lpImage )
{
    // Sanity check
    if( lpImage==NULL )
        return FALSE;
    // BITMAPINFO is at beginning of bits
    lpImage->lpbi = (LPBITMAPINFO)lpImage->lpBits;
    // Width - simple enough
    lpImage->Width = lpImage->lpbi->bmiHeader.biWidth;
    // Icons are stored in funky format where height is doubled - account for it
    lpImage->Height = (lpImage->lpbi->bmiHeader.biHeight)/2;
    // How many colors?
    lpImage->Colors = lpImage->lpbi->bmiHeader.biPlanes * lpImage->lpbi->bmiHeader.biBitCount;
    // XOR bits follow the header and color table
    lpImage->lpXOR = (LPBYTE)FindDIBBits((LPSTR)lpImage->lpbi);
    // AND bits follow the XOR bits
    lpImage->lpAND = lpImage->lpXOR + (lpImage->Height*BytesPerLine((LPBITMAPINFOHEADER)(lpImage->lpbi)));
    return TRUE;
}
 
/****************************************************************************
*
*     FUNCTION: ReadICOHeader
*
*     PURPOSE:  Reads the header from an ICO file
*
*     PARAMS:   Tcl_Channel channel - duh!
*
*     RETURNS:  UINT - Number of images in file, -1 for failure
*
* History:
*                July '95 - Created
*
\****************************************************************************/

static int 
ReadICOHeader( Tcl_Channel channel )
{
    WORD    Input;
    DWORD        dwBytesRead;

    // Read the 'reserved' WORD
    if ((dwBytesRead = Tcl_Read(channel, (char *)&Input, sizeof(WORD))) < 0)
        return -1;

    // Did we get a WORD?
    if (dwBytesRead != sizeof( WORD ))
        return -1;

    // Was it 'reserved' ?   (ie 0)
    if( Input != 0 )
        return -1;

    // Read the type WORD
    if ((dwBytesRead = Tcl_Read(channel, (char *)&Input, sizeof(WORD))) < 0)
        return -1;

    // Did we get a WORD?
    if( dwBytesRead != sizeof( WORD ) )
        return -1;
    // Was it type 1?
    if( Input != 1 )
        return -1;

    // Get the count of images
    if ((dwBytesRead = Tcl_Read(channel, (char *)&Input, sizeof( WORD ))) < 0)
        return -1;
    // Did we get a WORD?
    if( dwBytesRead != sizeof( WORD ) )
        return -1;
    // Return the count
    return (int)Input;
}
 
//if someone wants to see the several masks somewhere on the screen...
//set the ICO_DRAW define and feel free to make some tcl-commands
//for accessing it
//the normal drawing of an Icon to a DC is really easy:
//DrawIcon(hdc,x,y,hIcon) or , more complicated
//DrawIconEx32PlusMoreParameters ...
//#define ICO_DRAW
#ifdef ICO_DRAW
#define RectWidth(r)            ((r).right - (r).left + 1)
#define RectHeight(r)           ((r).bottom - (r).top + 1)

/****************************************************************************
*
*     FUNCTION: DrawXORMask
*
*     PURPOSE:  Using DIB functions, draw XOR mask on hDC in Rect
*
*     PARAMS:   HDC         hDC    - The DC on which to draw
*               RECT        Rect   - Bounding rect for drawing area
*               LPICONIMAGE lpIcon - pointer to icon image data
*
*     RETURNS:  BOOL - TRUE for success, FALSE for failure
*
*     COMMENTS: Does not use any palette information since the
*               OS won't when it draws the icon anyway.
*
* History:
*                July '95 - Created
*
\****************************************************************************/

static BOOL 
DrawXORMask( HDC hDC, RECT Rect, LPICONIMAGE lpIcon )
{
    int x, y;

    // Sanity checks
    if( lpIcon == NULL )
        return FALSE;
    if( lpIcon->lpBits == NULL )
        return FALSE;

    // Account for height*2 thing
    lpIcon->lpbi->bmiHeader.biHeight /= 2;

    // Locate it
    x = Rect.left + ((RectWidth(Rect)-lpIcon->lpbi->bmiHeader.biWidth)/2);
    y = Rect.top + ((RectHeight(Rect)-lpIcon->lpbi->bmiHeader.biHeight)/2);

    // Blast it to the screen
    SetDIBitsToDevice( hDC, x, y,
        lpIcon->lpbi->bmiHeader.biWidth,
        lpIcon->lpbi->bmiHeader.biHeight, 
        0, 0, 0, lpIcon->lpbi->bmiHeader.biHeight,
        lpIcon->lpXOR, lpIcon->lpbi, DIB_RGB_COLORS );

    // UnAccount for height*2 thing
    lpIcon->lpbi->bmiHeader.biHeight *= 2;

    return TRUE;
}
 
/****************************************************************************
*
*     FUNCTION: DrawANDMask
*
*     PURPOSE:  Using DIB functions, draw AND mask on hDC in Rect
*
*     PARAMS:   HDC         hDC    - The DC on which to draw
*               RECT        Rect   - Bounding rect for drawing area
*               LPICONIMAGE lpIcon - pointer to icon image data
*
*     RETURNS:  BOOL - TRUE for success, FALSE for failure
*
* History:
*                July '95 - Created
*
\****************************************************************************/

BOOL 
DrawANDMask( HDC hDC, RECT Rect, LPICONIMAGE lpIcon )
{
    LPBITMAPINFO    lpbi;
    int                    x, y;

    // Sanity checks
    if( lpIcon == NULL )
        return FALSE;
    if( lpIcon->lpBits == NULL )
        return FALSE;

    // Need a bitmap header for the mono mask
    lpbi = ckalloc( sizeof(BITMAPINFO) + (2 * sizeof( RGBQUAD )) );
    lpbi->bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
    lpbi->bmiHeader.biWidth = lpIcon->lpbi->bmiHeader.biWidth;
    lpbi->bmiHeader.biHeight = lpIcon->lpbi->bmiHeader.biHeight/2;
    lpbi->bmiHeader.biPlanes = 1;
    lpbi->bmiHeader.biBitCount = 1;
    lpbi->bmiHeader.biCompression = BI_RGB;
    lpbi->bmiHeader.biSizeImage = 0;
    lpbi->bmiHeader.biXPelsPerMeter = 0;
    lpbi->bmiHeader.biYPelsPerMeter = 0;
    lpbi->bmiHeader.biClrUsed = 0;
    lpbi->bmiHeader.biClrImportant = 0;
    lpbi->bmiColors[0].rgbRed = 0;
    lpbi->bmiColors[0].rgbGreen = 0;
    lpbi->bmiColors[0].rgbBlue = 0;
    lpbi->bmiColors[0].rgbReserved = 0;
    lpbi->bmiColors[1].rgbRed = 255;
    lpbi->bmiColors[1].rgbGreen = 255;
    lpbi->bmiColors[1].rgbBlue = 255;
    lpbi->bmiColors[1].rgbReserved = 0;

    // Locate it
    x = Rect.left + ((RectWidth(Rect)-lpbi->bmiHeader.biWidth)/2);
    y = Rect.top + ((RectHeight(Rect)-lpbi->bmiHeader.biHeight)/2);

    // Blast it to the screen
    SetDIBitsToDevice( hDC, x, y, lpbi->bmiHeader.biWidth, lpbi->bmiHeader.biHeight, 0, 0, 0, lpbi->bmiHeader.biHeight, lpIcon->lpAND, lpbi, DIB_RGB_COLORS );

    // clean up
    ckfree( (char*)lpbi );

    return TRUE;
}
#endif
 
/****************************************************************************
*
*     FUNCTION: FindDIBits
*
*     PURPOSE:  Locate the image bits in a CF_DIB format DIB.
*
*     PARAMS:   LPSTR lpbi - pointer to the CF_DIB memory block
*
*     RETURNS:  LPSTR - pointer to the image bits
*
* History:
*                July '95 - Copied <g>
*
\****************************************************************************/

static LPSTR 
FindDIBBits( LPSTR lpbi )
{
    return ( lpbi + *(LPDWORD)lpbi + PaletteSize( lpbi ) );
}
 
/****************************************************************************
*
*     FUNCTION: DIBNumColors
*
*     PURPOSE:  Calculates the number of entries in the color table.
*
*     PARAMS:   LPSTR lpbi - pointer to the CF_DIB memory block
*
*     RETURNS:  WORD - Number of entries in the color table.
*
* History:
*                July '95 - Copied <g>
*
\****************************************************************************/

static WORD 
DIBNumColors( LPSTR lpbi )
{
    WORD wBitCount;
    DWORD dwClrUsed;

    dwClrUsed = ((LPBITMAPINFOHEADER) lpbi)->biClrUsed;

    if (dwClrUsed)
        return (WORD) dwClrUsed;

    wBitCount = ((LPBITMAPINFOHEADER) lpbi)->biBitCount;

    switch (wBitCount)
    {
        case 1: return 2;
        case 4: return 16;
        case 8:        return 256;
        default:return 0;
    }
}
 
/****************************************************************************
*
*     FUNCTION: PaletteSize
*
*     PURPOSE:  Calculates the number of bytes in the color table.
*
*     PARAMS:   LPSTR lpbi - pointer to the CF_DIB memory block
*
*     RETURNS:  WORD - number of bytes in the color table
*
*
* History:
*                July '95 - Copied <g>
*
\****************************************************************************/

static WORD 
PaletteSize( LPSTR lpbi )
{
    return ((WORD)( DIBNumColors( lpbi ) * sizeof( RGBQUAD )) );
}
 
/****************************************************************************
*
*     FUNCTION: BytesPerLine
*
*     PURPOSE:  Calculates the number of bytes in one scan line.
*
*     PARAMS:   LPBITMAPINFOHEADER lpBMIH - pointer to the BITMAPINFOHEADER
*                                           that begins the CF_DIB block
*
*     RETURNS:  DWORD - number of bytes in one scan line (DWORD aligned)
*
* History:
*                July '95 - Created
*
\****************************************************************************/

static DWORD 
BytesPerLine( LPBITMAPINFOHEADER lpBMIH )
{
    return WIDTHBYTES(lpBMIH->biWidth * lpBMIH->biPlanes * lpBMIH->biBitCount);
}
 
static BOOL
NotifyA (IcoInfo* icoPtr, int oper, HICON hIcon, char* txt) 
{
    NOTIFYICONDATAA ni;
    Tcl_DString dst;
    CHAR* str;

    ni.cbSize = sizeof(NOTIFYICONDATAA);
    ni.hWnd = CreateTaskbarHandlerWindow();
    ni.uID = icoPtr->id;
    ni.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
    ni.uCallbackMessage = ICON_MESSAGE;
    ni.hIcon = (HICON)hIcon;

    str = (CHAR*)Tcl_UtfToExternalDString(NULL, txt, -1, &dst);
    strncpy(ni.szTip, str, 63);
    ni.szTip[63] = 0;
    Tcl_DStringFree(&dst);
    return notify_funcA(oper, &ni);
}
 
static BOOL
NotifyW (IcoInfo* icoPtr, int oper, HICON hIcon, char* txt) 
{
    NOTIFYICONDATAW ni;
    Tcl_DString dst;
    WCHAR* str;
    Tcl_Encoding Encoding;

    ni.cbSize = sizeof(NOTIFYICONDATAW);
    ni.hWnd = CreateTaskbarHandlerWindow();
    ni.uID = icoPtr->id;
    ni.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
    ni.uCallbackMessage = ICON_MESSAGE;
    ni.hIcon=(HICON)hIcon;

    Encoding = Tcl_GetEncoding(NULL, "unicode");
    str = (WCHAR*)Tcl_UtfToExternalDString(Encoding, txt, -1, &dst);
    Tcl_FreeEncoding(Encoding);
    wcsncpy(ni.szTip, str, 63);
    ni.szTip[63] = 0;
    Tcl_DStringFree(&dst);
    return notify_funcW(oper, &ni);
}
 
static int 
TaskbarOperation (IcoInfo *icoPtr, int oper, HICON hIcon, char *txt) 
{
    int result;
    
    if (notify_funcA == NULL && notify_funcW == NULL && hmod == NULL) {
        hmod = GetModuleHandle("SHELL32.DLL");
        if (hmod == NULL)
            hmod = LoadLibrary("SHELL32.DLL");
        if (hmod == NULL) {
            Tcl_AppendResult(icoPtr->interp, "Could not Load SHELL32.DLL",(char*)NULL);
            return TCL_ERROR;
        }
        notify_funcW = (LPFN_SHELLNOTIFYICONW)GetProcAddress(hmod, "Shell_NotifyIconW");
        notify_funcA = (LPFN_SHELLNOTIFYICONA)GetProcAddress(hmod, "Shell_NotifyIconA");
        if (notify_funcW == NULL && notify_funcA == NULL) {
            Tcl_AppendResult(icoPtr->interp,
		"Could not get address of Shell_NotifyIconW or Shell_NotifyIconA",
                (char*)NULL);
            return TCL_ERROR;
        }
    } else if (notify_funcA == NULL && notify_funcW == NULL) {
        Tcl_AppendResult(icoPtr->interp,
	    "You probably don't have a Windows shell", (char*)NULL);
        return TCL_ERROR;
    }

    if (notify_funcW != NULL) {
        result = NotifyW(icoPtr, oper, hIcon, txt) 
            || NotifyA(icoPtr, oper, hIcon, txt);
    } else {
        result = NotifyA(icoPtr, oper, hIcon, txt);
    }

    Tcl_SetObjResult(icoPtr->interp, Tcl_NewIntObj(result));
    if (result == 1) {
        if (oper == NIM_ADD || oper == NIM_MODIFY) {
            icoPtr->taskbar_flags |= TASKBAR_ICON;
        }
        if (oper == NIM_DELETE) {
            icoPtr->taskbar_flags &= ~TASKBAR_ICON;
        }
    }
    return TCL_OK;
}
 
static IcoInfo *
NewIcon (Tcl_Interp *interp, HICON hIcon, 
    int itype, LPICONRESOURCE lpIR, int iconpos) 
{
    static int nextId = 1;
    int n;
    char buffer[5 + TCL_INTEGER_SPACE];
    IcoInfo* icoPtr;

    icoPtr = (IcoInfo *) ckalloc((unsigned) (sizeof(IcoInfo)));
    memset(icoPtr, 0, sizeof(IcoInfo));
    icoPtr->id      = nextId;
    icoPtr->hIcon   = hIcon;
    icoPtr->itype   = itype;
    icoPtr->lpIR    = lpIR;
    icoPtr->iconpos = iconpos;
    n = _snprintf(buffer, sizeof(buffer)-1, "ico#%d", icoPtr->id); buffer[n] = 0;
    icoPtr->taskbar_txt = ckalloc((int)strlen(buffer)+1);
    strcpy(icoPtr->taskbar_txt, buffer);
    icoPtr->interp  = interp;
    icoPtr->taskbar_command= NULL;
    icoPtr->taskbar_flags = 0;
    icoPtr->hwndFocus = NULL;
    if (itype == ICO_LOAD) {
        icoPtr->lpIR = (LPICONRESOURCE)NULL;
        icoPtr->iconpos = 0;
    }
    nextId += 1;
    icoPtr->nextPtr = firstIcoPtr;
    firstIcoPtr = icoPtr;
    Tcl_SetObjResult(interp, Tcl_NewStringObj(buffer, -1));
    return icoPtr;
}
 
static void 
FreeIcoPtr(Tcl_Interp *interp, IcoInfo *icoPtr)
{
    IcoInfo *prevPtr;
    if (firstIcoPtr == icoPtr) {
        firstIcoPtr = icoPtr->nextPtr;
    } else {
        for (prevPtr = firstIcoPtr; prevPtr->nextPtr != icoPtr;
             prevPtr = prevPtr->nextPtr) {
            /* Empty loop body. */
        }
        prevPtr->nextPtr = icoPtr->nextPtr;
    }
    if (icoPtr->taskbar_flags & TASKBAR_ICON) {
        TaskbarOperation(icoPtr, NIM_DELETE, NULL, "");
        Tcl_ResetResult(interp);
    }
    if(icoPtr->itype==ICO_FILE){
        FreeIconResource(icoPtr->lpIR);
    }
    if(icoPtr->taskbar_txt!=NULL) {
        ckfree((char *) icoPtr->taskbar_txt);
    }
    if(icoPtr->taskbar_command!=NULL) {
        ckfree((char *) icoPtr->taskbar_command);
    }
    ckfree((char *) icoPtr);
}
static IcoInfo* GetIcoPtr(Tcl_Interp* interp,char* string){
    IcoInfo *icoPtr;
    int id;
    char *end;

    if (strncmp(string, "ico#", 4) != 0) {
        return NULL;
    }
    string += 4;
    id = strtoul(string, &end, 10);
    if ((end == string) || (*end != 0)) {
        return NULL;
    }
    for (icoPtr = firstIcoPtr; icoPtr != NULL;
         icoPtr = icoPtr->nextPtr) {
        if (icoPtr->id == id) {
            return icoPtr;
        }
    }
    Tcl_AppendResult(interp, "icon \"", string,
        "\" doesn't exist", (char *) NULL);
    return NULL;
}
 
static int
GetInt(long theint, char *buffer, size_t len)
{
    snprintf(buffer, len, "0x%lx", theint);
    buffer[len-1] = 0;
    return (int)strlen(buffer);
}
 
static int
GetIntDec(long theint, char *buffer, size_t len)
{
    snprintf(buffer, len - 1, "%ld", theint);
    buffer[len-1] = 0;
    return (int)strlen(buffer);
}
 
static char* 
TaskbarExpandPercents(IcoInfo *icoPtr, char *msgstring,
    WPARAM wParam, LPARAM lParam, char *before, char *after, int *aftersize)
{
#define SPACELEFT (*aftersize-(dst-after)-1)
#define AFTERLEN ((*aftersize>0)?(*aftersize*2):1024)
#define ALLOCLEN ((len>AFTERLEN)?(len*2):AFTERLEN)
    char buffer[TCL_INTEGER_SPACE + 5];
    char* dst;
    dst = after;
    while (*before) {
        char *ptr=before;
        int len=1;
        if(*before=='%') {
            switch(before[1]){
                case 'M':
                case 'm': {
                    before++;
                    len=(int)strlen(msgstring);
                    ptr=msgstring;
                    break;
                }
                /* case 'W': {
                   before++;
                   len=(int)strlen(winstring);
                   ptr=winstring;
                   break;
                   }
                */
                case 'i': {
                    before++;
                    snprintf(buffer, sizeof(buffer) - 1, "ico#%d", icoPtr->id);
                    len=(int)strlen(buffer);
                    ptr=buffer;
                    break;
                }
                case 'w': {
                    before++;
                    len=GetInt((long)wParam,buffer, sizeof(buffer));
                    ptr=buffer;
                    break;
                }
                case 'l': {
                    before++;
                    len=GetInt((long)lParam,buffer, sizeof(buffer));
                    ptr=buffer;
                    break;
                }
                case 't': {
                    before++;
                    len=GetInt((long)GetTickCount(), buffer, sizeof(buffer));
                    ptr=buffer;
                    break;
                }
                case 'x': {
                    POINT pt;
                    GetCursorPos(&pt);
                    before++;
                    len=GetIntDec((long)pt.x, buffer, sizeof(buffer));
                    ptr=buffer;
                    break;
                }
                case 'y': {
                    POINT pt;
                    GetCursorPos(&pt);
                    before++;
                    len=GetIntDec((long)pt.y,buffer, sizeof(buffer));
                    ptr=buffer;
                    break;
                }
                case 'X': {
                    DWORD dw;
                    dw=GetMessagePos();
                    before++;
                    len=GetIntDec((long)LOWORD(dw),buffer, sizeof(buffer));
                    ptr=buffer;
                    break;
                }
                case 'Y': {
                    DWORD dw;
                    dw=GetMessagePos();
                    before++;
                    len=GetIntDec((long)HIWORD(dw),buffer, sizeof(buffer));
                    ptr=buffer;
                    break;
                }
                case 'H': {
                    before++;
                    len = GetInt((long)icoPtr->hwndFocus, buffer, sizeof(buffer));
                    ptr = buffer;
                    break;
                }
                case '%': {
                    before++;
                    len=1;
                    ptr="%";
                    break;
                }
            }
        }
        if (SPACELEFT < len) {
            char *newspace;
            ptrdiff_t dist = dst - after;
            int alloclen = ALLOCLEN;
            newspace = (char *) ckalloc(alloclen);
            if(dist>0)
                memcpy((VOID *) newspace, (VOID *) after, dist);
            if(after && *aftersize)
                ckfree(after);
            *aftersize =alloclen;
            after = newspace;
            dst=after+dist;
        }
        if(len>0)
            memcpy(dst,ptr,len);
        dst+=len;
        if((dst-after)>(*aftersize-1))
            panic("oops\n");
        before++;
    }
    *dst=0;
    return after;
}
 
static void 
TaskbarEval(IcoInfo* icoPtr,WPARAM wParam,LPARAM lParam) {
    char* msgstring = "none";
    char evalspace[200];
    int evalsize = 200;
    char* expanded;
    int fixup = 0;

    switch (lParam) {
        case WM_MOUSEMOVE:
            msgstring = "WM_MOUSEMOVE";
            icoPtr->hwndFocus = GetFocus();
            break;
        case WM_LBUTTONDOWN  :msgstring="WM_LBUTTONDOWN";  fixup = 1; break;
        case WM_LBUTTONUP    :msgstring="WM_LBUTTONUP";    fixup = 1; break;
        case WM_LBUTTONDBLCLK:msgstring="WM_LBUTTONDBLCLK";fixup = 1; break;
        case WM_RBUTTONDOWN  :msgstring="WM_RBUTTONDOWN";  fixup = 1; break;
        case WM_RBUTTONUP    :msgstring="WM_RBUTTONUP";    fixup = 1; break;
        case WM_RBUTTONDBLCLK:msgstring="WM_RBUTTONDBLCLK";fixup = 1; break;
        case WM_MBUTTONDOWN  :msgstring="WM_MBUTTONDOWN";  fixup = 1; break;
        case WM_MBUTTONUP    :msgstring="WM_MBUTTONUP";    fixup = 1; break;
        case WM_MBUTTONDBLCLK:msgstring="WM_MBUTTONDBLCLK";fixup = 1; break;
        default:              msgstring = "WM_NULL";       fixup = 0;
    }
    expanded = TaskbarExpandPercents(icoPtr,msgstring,wParam,lParam,
        icoPtr->taskbar_command,evalspace, &evalsize);
    if (icoPtr->interp!=NULL) {
        int result;
        HWND hwnd = NULL;
        
        /* See http/ /:support.microsoft.com/kb/q135788 */
        if (fixup) {
            if (icoPtr->hwndFocus != NULL && IsWindow(icoPtr->hwndFocus)) {
                hwnd = icoPtr->hwndFocus;
            } else {
                Tk_Window tkwin = Tk_MainWindow(icoPtr->interp);
                hwnd = Tk_GetHWND(Tk_WindowId(tkwin));
            }
            SetForegroundWindow(hwnd);
        }
        
        result = Tcl_GlobalEval(icoPtr->interp, expanded);
        
        if (hwnd != NULL) {
            /* See http:/ /support.microsoft.com/kb/q135788/ */
            PostMessage(hwnd, WM_NULL, 0, 0);
        }
        if (result != TCL_OK) {
            char buffer[100];
            sprintf(buffer, "\n  (command bound to taskbar-icon ico#%d)",icoPtr->id);
            Tcl_AddErrorInfo(icoPtr->interp, buffer);
            Tcl_BackgroundError(icoPtr->interp);
        }
    }
    if (expanded != evalspace) {
        ckfree(expanded);
    }
}
 
/*
 * Windows callback procedure, if ICON_MESSAGE arrives, try to execute
 * the taskbar_command
 */

static LRESULT CALLBACK 
TaskbarHandlerProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 
{
    static UINT msgTaskbarCreated = 0;
    IcoInfo *icoPtr = NULL;

    switch (message) {
        case WM_CREATE:
            msgTaskbarCreated = RegisterWindowMessage(TEXT("TaskbarCreated"));
            break;

        case ICON_MESSAGE:
            for (icoPtr = firstIcoPtr; icoPtr != NULL;
                 icoPtr = icoPtr->nextPtr) {
                if (wParam == (WPARAM)icoPtr->id) {
                    if (icoPtr->taskbar_command != NULL) {
                        TaskbarEval(icoPtr, wParam, lParam);
                    }
                }
            }
            break;
            
        default:
            /* 
             * Check to see if explorer has been restarted and we ned to
             * re-add our icons.
             */
            if (message == msgTaskbarCreated) {
                for (icoPtr = firstIcoPtr; icoPtr != NULL;
                     icoPtr = icoPtr->nextPtr) {
                    if (icoPtr->taskbar_flags & TASKBAR_ICON) {
			HICON hIcon = icoPtr->hIcon;
			if (icoPtr->iconpos != 0 && icoPtr->lpIR != NULL) {
			    hIcon = icoPtr->lpIR->IconImages[icoPtr->iconpos].hIcon;
			}
                        TaskbarOperation(icoPtr, NIM_ADD, hIcon, icoPtr->taskbar_txt);
                    }
                }
            }
            return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0L;
}
 
/*registers the handler window class*/
static int 
RegisterHandlerClass(HINSTANCE hInstance)
{
    WNDCLASS wndclass;
    memset(&wndclass,0,sizeof(WNDCLASS));
    wndclass.style = 0;
    wndclass.lpfnWndProc = TaskbarHandlerProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = HANDLER_CLASS;
    return RegisterClass(&wndclass);
}
 
/*creates a hidden window to handle taskbar messages*/
static HWND 
CreateTaskbarHandlerWindow(void) 
{
    static int registered = 0;
    HINSTANCE hInstance = GETHINSTANCE;
    if (handlerWindow)
        return handlerWindow;
    if (!registered){
        if (!RegisterHandlerClass(hInstance))
            return 0;
        registered = 1;
    }
    return (handlerWindow = CreateWindow(HANDLER_CLASS,"",WS_OVERLAPPED,0,0,
        CW_USEDEFAULT,CW_USEDEFAULT,NULL, NULL, hInstance, NULL));
}
 
static void
DestroyHandlerWindow(void) 
{
    if(handlerWindow)
        DestroyWindow(handlerWindow);
}
 
static char * 
StandardIcon(CONST84 char* arg){
    if (!stricmp(arg, "application"))
        return IDI_APPLICATION;
    if (!stricmp(arg, "asterisk"))
        return IDI_ASTERISK;
    if (!stricmp(arg, "error"))
        return IDI_ERROR;
    if (!stricmp(arg, "exclamation"))
        return IDI_EXCLAMATION;
    if (!stricmp(arg, "hand"))
        return IDI_HAND;
    if (!stricmp(arg, "question"))
        return IDI_QUESTION;
    if (!stricmp(arg, "information"))
        return IDI_INFORMATION;
    if (!stricmp(arg, "warning"))
        return IDI_WARNING;
    if (!stricmp(arg, "winlogo"))
        return IDI_WINLOGO;
    return NULL;
}
 
/*tries to get a valid window handle from a Tk-pathname for a toplevel*/
static int
NameOrHandle(Tcl_Interp* interp, char* arg,HWND* hwndPtr)
{
#define WINFO_FRAME "wm frame "
    Tk_Window tkwin;
    size_t limit = 0;
    char * cmd;

    if (Tcl_GetInt(interp,arg,(int*)hwndPtr) == TCL_OK) {
        return TCL_OK; 
    }
    Tcl_ResetResult(interp);
    tkwin = Tk_NameToWindow(interp, arg,Tk_MainWindow(interp));
    if (tkwin == NULL) {
        Tcl_AppendResult(interp,arg," is no valid windowpath",(char*)NULL);
        return TCL_ERROR;
    }
    if(!Tk_IsTopLevel(tkwin)) {
        Tcl_AppendResult(interp, arg, 
            " is not a toplevel valid windowpath", (char*)NULL);
        return TCL_ERROR;
    }
    limit = strlen(arg) + strlen(WINFO_FRAME);
    cmd = ckalloc((int) limit + 1);
    strcpy(cmd, WINFO_FRAME);
    strcat(cmd, arg);
    if (Tcl_Eval(interp,cmd) == TCL_ERROR) {
        return TCL_ERROR; 
    }
    strncpy(cmd, Tcl_GetStringResult(interp), limit); cmd[limit] = 0;
    if (sscanf(cmd, "0x%x", (int*)hwndPtr) != 1){
        Tcl_AppendResult(interp,"couldn't scan ", cmd, (char*)NULL);
        return TCL_ERROR;
    }
    if (*hwndPtr == NULL){
        Tcl_AppendResult(interp, "couldn't get windowid from ",
            cmd, (char*)NULL);
        return TCL_ERROR;
    }
    return TCL_OK;
}
 
static void 
WinIcoDestroy (ClientData clientData) 
{
    IcoInfo* icoPtr;
    IcoInfo* nextPtr;
    Tcl_Interp* interp=(Tcl_Interp*)clientData;
    DestroyHandlerWindow();
    for (icoPtr = firstIcoPtr; icoPtr != NULL;
         icoPtr = nextPtr) {
        nextPtr=icoPtr->nextPtr;
        FreeIcoPtr(interp,icoPtr);
    }
}
 
static int 
WinIcoCmd(ClientData clientData, Tcl_Interp *interp,
    int argc, CONST84 char *argv[])
{
    size_t length;
    HICON hIcon;
    int i;
    IcoInfo* icoPtr;
    if (argc < 2) {
        Tcl_AppendResult(interp, "wrong # args: should be \"",
            argv[0], " option ?arg arg ...?\"", (char *) NULL);
        return TCL_ERROR;
    }
    
    length = strlen(argv[1]);
    if (strncmp(argv[1],"load",length) == 0 && (length >= 2)) {
        int number;
        char* arg;
        HINSTANCE hinst=GETHINSTANCE;
        if(argc<3) {
            Tcl_AppendResult(interp,"wrong # args,must be:",
                argv[0]," load <resourcename/number> ?dllname?",(char*)NULL);
            return TCL_ERROR;
        }
        if (argc > 3) {
            hinst = LoadLibrary(argv[3]);
            if(hinst==NULL) {
                Tcl_AppendResult(interp,"couldn't load dll ",argv[3],(char*)NULL);
                return TCL_ERROR;
            }
        }
        if(Tcl_GetInt(interp,argv[2],&number)==TCL_OK){
            if(number>32511){
                hinst=NULL;
                if((hIcon=LoadIcon(hinst,MAKEINTRESOURCE(number)))!=NULL){
                    NewIcon(interp,hIcon,ICO_LOAD,NULL,0);
                    return TCL_OK;
                }
            }
        }
        Tcl_ResetResult(interp);
        arg = StandardIcon(argv[2]);
        if(arg == NULL) {
            arg = (char*)argv[2];
        } else {
            hinst = NULL;
        }
        if((hIcon=LoadIcon(hinst,arg))!=NULL){
            NewIcon(interp,hIcon,ICO_LOAD,NULL,0);
            return TCL_OK;
        }
        Tcl_AppendResult(interp,"Could not find a resource for \"",
            argv[2],"\"",(char*)NULL);
        return TCL_ERROR;
    } else if ((strncmp(argv[1], "createfrom", length) == 0) && (length >= 2)) {
        LPICONRESOURCE lpIR = NULL;
        int pos = 0;
        if(argc<3) {
            Tcl_AppendResult(interp,"wrong # args,must be:",
                argv[0]," createfrom <icofilename> ",(char*)NULL);
            return TCL_ERROR;
        }
        lpIR=ReadIconFromICOFile(interp,argv[2]);
        if(lpIR==NULL){
            Tcl_AppendResult(interp,"reading of ",argv[2]," failed!",(char*)NULL);
            return TCL_ERROR;
        }
        hIcon=NULL;
        for( i = 0; i < lpIR->nNumImages; i++ ) {
            /*take the first or a 32x32 16 color icon*/
            if(i==0 ||
                (lpIR->IconImages[i].Height==32 && lpIR->IconImages[i].Width==32
                    && lpIR->IconImages[i].Colors==4)){
                hIcon=lpIR->IconImages[i].hIcon;
                pos=i;
            }
        }
        if(hIcon==NULL){
            FreeIconResource(lpIR);
            Tcl_AppendResult(interp,"Could not find an icon in ",argv[2],(char*)NULL);
            return TCL_ERROR;
        }
        NewIcon(interp,hIcon,ICO_FILE,lpIR,pos);
    } else if ((strncmp(argv[1], "info", length) == 0) && (length >= 2)) {
        Tcl_DString dstr;
        if (argc == 2) {
            char buffer[30];

            for (icoPtr = firstIcoPtr; icoPtr != NULL;
                 icoPtr = icoPtr->nextPtr) {
                sprintf(buffer, "ico#%d", icoPtr->id);
                Tcl_AppendElement(interp, buffer);
            }
            return TCL_OK;
        }
        if (argc != 3) {
            Tcl_AppendResult(interp, "wrong # args: should be \"",
                argv[0], " info ?id?\"", (char *) NULL);
            return TCL_ERROR;
        }
        if (( icoPtr = GetIcoPtr(interp, (char*)argv[2])) == NULL ) return TCL_ERROR;
        if (icoPtr->itype==ICO_LOAD){
            char buffer[200];
            sprintf(buffer,
                "{-pos 0  -width 32 -height 32 -geometry 32x32 -bpp 4 -hicon 0x%x -ptr 0x0}",
                (INT_PTR)icoPtr->hIcon);
            Tcl_AppendResult(interp,buffer,(char*)NULL);
        } else {
            Tcl_DStringInit(&dstr);
            for( i = 0; i < icoPtr->lpIR->nNumImages; i++ ) {
                char buffer[255];
                Tcl_DStringStartSublist(&dstr);
                Tcl_DStringAppendElement(&dstr,"-pos");
                sprintf(buffer,"%d",i);
                Tcl_DStringAppendElement(&dstr,buffer);
                Tcl_DStringAppendElement(&dstr,"-width");
                sprintf(buffer,"%d",icoPtr->lpIR->IconImages[i].Width);
                Tcl_DStringAppendElement(&dstr,buffer);
                Tcl_DStringAppendElement(&dstr,"-height");
                sprintf(buffer,"%d",icoPtr->lpIR->IconImages[i].Height);
                Tcl_DStringAppendElement(&dstr,buffer);
                Tcl_DStringAppendElement(&dstr,"-geometry");
                sprintf(buffer,"%dx%d",
		    icoPtr->lpIR->IconImages[i].Width,
		    icoPtr->lpIR->IconImages[i].Height);
                Tcl_DStringAppendElement(&dstr,buffer);
                Tcl_DStringAppendElement(&dstr,"-bpp");
                sprintf(buffer,"%d",icoPtr->lpIR->IconImages[i].Colors);
                Tcl_DStringAppendElement(&dstr,buffer);
                Tcl_DStringAppendElement(&dstr,"-hicon");
                sprintf(buffer,"0x%x", (INT_PTR)icoPtr->lpIR->IconImages[i].hIcon);
                Tcl_DStringAppendElement(&dstr,buffer);
                Tcl_DStringAppendElement(&dstr,"-ptr");
                sprintf(buffer,"0x%x",(INT_PTR)icoPtr->lpIR);
                Tcl_DStringAppendElement(&dstr,buffer);
                Tcl_DStringEndSublist(&dstr);
            }
            Tcl_DStringResult(interp,&dstr);
        }
        return TCL_OK;
    } else if ((strncmp(argv[1], "delete", length) == 0)
        && (length >= 2)) {
        if (argc != 3) {
            Tcl_AppendResult(interp, "wrong # args: should be \"",
                argv[0], " delete ?id?\"", (char *) NULL);
            return TCL_ERROR;
        }
        icoPtr = GetIcoPtr(interp, (char*)argv[2]);
        if (icoPtr == NULL) {
            Tcl_ResetResult(interp);
            return TCL_OK;
        }
        FreeIcoPtr(interp,icoPtr);
        return TCL_OK;
    } else if ((strncmp(argv[1], "hicon", length) == 0)
        && (length >= 2)) {
        char buf[TCL_INTEGER_SPACE + 3];
        int n;
        if (argc != 3) {
            Tcl_AppendResult(interp, "wrong # args: should be \"",
                argv[0], " hicon <id> \"", (char *) NULL);
            return TCL_ERROR;
        }
        if (( icoPtr = GetIcoPtr(interp, (char*)argv[2])) == NULL ) return TCL_ERROR;
        n = _snprintf(buf, sizeof(buf) - 1, "0x%x", icoPtr->hIcon); buf[n] = 0;
        Tcl_SetObjResult(interp, Tcl_NewStringObj(buf, -1));
        return TCL_OK;
    } else if ((strncmp(argv[1], "pos", length) == 0) && (length >= 2)) {
        if (argc < 3) {
            Tcl_AppendResult(interp, "wrong # args: should be \"",
                argv[0], " pos <id> ?newpos?\"", (char *) NULL);
            return TCL_ERROR;
        }
        if(( icoPtr = GetIcoPtr(interp, (char*)argv[2])) == NULL ) return TCL_ERROR;
        if(argc==3 || icoPtr->itype==ICO_LOAD ){
            Tcl_SetObjResult(interp, Tcl_NewIntObj(icoPtr->iconpos));
        } else {
            int newpos;
            if(Tcl_GetInt(interp,argv[3],&newpos)==TCL_ERROR) {
                return TCL_ERROR;
            }
            if(newpos<0 ||  newpos >= icoPtr->lpIR->nNumImages ) {
                Tcl_AppendResult(interp,"wrong new position",(char*)NULL);
                return TCL_ERROR;
            }
            icoPtr->iconpos = newpos;
            icoPtr->hIcon = icoPtr->lpIR->IconImages[newpos].hIcon;
        }
        return TCL_OK;
    } else if ((strncmp(argv[1], "text", length) == 0) && (length >= 2)) {
        if (argc < 3) {
            Tcl_AppendResult(interp, "wrong # args: should be \"",
                argv[0], " text <id> ?newtext?\"", (char *) NULL);
            return TCL_ERROR;
        }
        if(( icoPtr = GetIcoPtr(interp, (char*)argv[2])) == NULL ) return TCL_ERROR;
        if(argc>3){
            char* newtxt=(char*)argv[3];
            if(icoPtr->taskbar_txt!=NULL){
                ckfree(icoPtr->taskbar_txt);
            }
            icoPtr->taskbar_txt=ckalloc((int)strlen(newtxt)+1);
            strcpy(icoPtr->taskbar_txt,newtxt);
        }
        Tcl_AppendResult(interp,icoPtr->taskbar_txt,(char*)NULL);
        return TCL_OK;
    } else if ((strncmp(argv[1], "setwindow", length) == 0)  && (length >= 2)) {
#ifdef __TURBOC__
#define WM_SETICON 0x80
#define WM_GETICON 0x79
#define ICON_SMALL          0
#define ICON_BIG            1
#endif
        HWND h;
        INT_PTR result = 0;
        int iconsize=ICON_BIG;
        int iconpos;
        if (argc < 4) {
            Tcl_AppendResult(interp, "wrong # args: should be \"",
                argv[0], " <windowid> <id> ?big/small? ?pos?\"", (char *) NULL);
            return TCL_ERROR;
        }
        if (NameOrHandle(interp,(char*)argv[2],&h)==TCL_ERROR) return TCL_ERROR;
        if (( icoPtr = GetIcoPtr(interp, (char*)argv[3])) == NULL ) return TCL_ERROR;
        if(argc>4){
            if(!strcmp(argv[4],"small")) { iconsize=ICON_SMALL;}
            else if(!strcmp(argv[4],"big")) {iconsize=ICON_BIG;}
            else {
                Tcl_AppendResult(interp,"wrong # args: should be \"",
                    argv[0]," setwindow ",argv[2]," ",argv[3]," ?big/small? ?pos?\"", (char *) NULL);
                return TCL_ERROR;
            }
        }
        if(argc>5 && icoPtr->itype==ICO_FILE){
            if(Tcl_GetInt(interp,argv[5],&iconpos)==TCL_ERROR) { return TCL_ERROR; }
            if(iconpos<0 ||  iconpos >= icoPtr->lpIR->nNumImages ) {
                Tcl_AppendResult(interp,"wrong position",(char*)NULL);
                return TCL_ERROR;
            }
        } else {
            iconpos=icoPtr->iconpos;
        }
        if(icoPtr->itype==ICO_FILE){
            hIcon=icoPtr->lpIR->IconImages[iconpos].hIcon;
        } else {
            hIcon=icoPtr->hIcon;
        }
        if (!ISWIN32S){
            result = SendMessage(h, WM_SETICON, iconsize, (LPARAM)hIcon);
        } else {
            if (iconsize==ICON_BIG){
#ifdef GCLP_HICON
                result = SetClassLongPtr(h, GCLP_HICON, (LONG_PTR)hIcon);
#else
		result = SetClassLong(h, GCL_HICON, (LONG)hIcon);
#endif
            } else {
                /*don't permit small icons,bcause they remove the big ones*/
#ifdef GCLP_HICON
                result = GetClassLongPtr(h, GCLP_HICON);
#else
		result = GetClassLong(h, GCL_HICON);
#endif
            }
        }
#if 10 * TCL_MAJOR_VERSION + TCL_MINOR_VERSION < 84
#errror unexpected
        Tcl_SetObjResult(interp, Tcl_NewIntObj((int)result));
#else
        Tcl_SetObjResult(interp, Tcl_NewWideIntObj(result));
#endif
        return TCL_OK;
    } else if ((strncmp(argv[1], "taskbar", length) == 0)  && (length >= 2)) {
        //TkWindow * tkwin=NULL;
        char* callback=NULL;
        //char* windowname=NULL;
        int oper;
        char ** args;
        int c;
        int length;
        int count;
        int newpos;
        char* txt;
        if (argc < 4) {
            Tcl_AppendResult(interp, "wrong # args: should be \"",
                argv[0], " taskbar <add/delete/modify> <id> ?-callback <callback>? \"", (char *) NULL);
            return TCL_ERROR;
        }
        if (strcmp(argv[2],"add")==0) {
            oper = NIM_ADD;
        } else if(strncmp(argv[2],"del",3)==0 ) {
            oper = NIM_DELETE;
        } else if(strncmp(argv[2],"mod",3)==0 ) {
            oper = NIM_MODIFY;
        } else {
            Tcl_AppendResult(interp,"bad argument ",argv[2], "should be add,delete or modify",(char*)NULL);
            return TCL_ERROR;
        }
        if ((icoPtr = GetIcoPtr(interp, (char *)argv[3])) == NULL )
	    return TCL_ERROR;
        hIcon = icoPtr->hIcon;
        txt = icoPtr->taskbar_txt;
        if (argc > 4) {
            for (count = argc-4, args = (char**)argv+4; count > 1; count -= 2, args += 2) {
                if (args[0][0] != '-')
                    goto wrongargs2;
                c = args[0][1];
                length = (int)strlen(args[0]);
                if ((c == '-') && (length == 2)) {
                    break;
                }
                if ((c == 'c') && (strncmp(args[0], "-callback", length) == 0)) {
                    callback = args[1];
                } else if ((c == 'p') && (strncmp(args[0], "-pos", length) == 0)) {
                    if (Tcl_GetInt(interp,args[1],&newpos)==TCL_ERROR) {
                        return TCL_ERROR;
                    }
                    if (icoPtr->itype==ICO_FILE) {
                        if (newpos < 0 ||  newpos >= icoPtr->lpIR->nNumImages ) {
                            Tcl_AppendResult(interp, "wrong position ", args[1], (char*)NULL);
                            return TCL_ERROR;
                        }
			icoPtr->iconpos = newpos;
                        hIcon = icoPtr->lpIR->IconImages[newpos].hIcon;
                    }
                } else if ((c == 't') && (strncmp(args[0], "-text", length) == 0)) {
                    txt = args[1];
                } else {
                    goto wrongargs2;
                }
            }
            if(count==1)
                goto wrongargs2;
        }
        if(callback!=NULL ) {
            /*
              if (tkwin==NULL) {
              Tcl_AppendResult(interp,"you can't set a callback without a tkwin");
              return TCL_ERROR;
              }
            */
            if (icoPtr->taskbar_command!=NULL) {
                ckfree((char*)icoPtr->taskbar_command);
            }
            icoPtr->taskbar_command=ckalloc((int)strlen(callback)+1);
            strcpy(icoPtr->taskbar_command,callback);
        }
        return TaskbarOperation(icoPtr, oper, hIcon, txt);
    wrongargs2:
        Tcl_AppendResult(interp, "unknown option \"", args[0],"\",valid are:",
            "-callback <tcl-callback> -pos <iconposition> -text <tooltiptext>", (char *) NULL);
        return TCL_ERROR;
    } else {
        Tcl_AppendResult(interp, "bad argument \"", argv[1],
            "\": must be load, createfrom, info, hicon, pos, setwindow, text, taskbar",
            (char *) NULL);
        return TCL_ERROR;
    }
    return TCL_OK;
}
 
static int 
DoInit (Tcl_Interp* interp) 
{
    OSVERSIONINFO info;
#ifdef USE_TCL_STUBS
    if (Tcl_InitStubs (interp, TCL_VERSION, 0) == NULL) {
        return TCL_ERROR;
    }
#endif
#ifdef USE_TK_STUBS
    if (Tk_InitStubs (interp, TK_VERSION, 0) == NULL) {
        return TCL_ERROR;
    }
#endif

    info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    GetVersionEx(&info);
    isWin32s = (info.dwPlatformId == VER_PLATFORM_WIN32s);
    
    Tcl_CreateCommand(interp, "winico", WinIcoCmd, (ClientData)interp,
        (Tcl_CmdDeleteProc *) WinIcoDestroy);
    
    return Tcl_PkgProvide (interp, PACKAGE_NAME , PACKAGE_VERSION);
}
 
EXPORT(int,Winico_Init)(Tcl_Interp* interp) 
{
    return DoInit(interp);
}
 
EXPORT(int,Winico_SafeInit)(Tcl_Interp* interp) 
{
    return DoInit(interp);
}
 
/*
 *----------------------------------------------------------------------
 *
 * DllEntryPoint --
 *
 *        This wrapper function is used by Windows to invoke the
 *        initialization code for the DLL.  If we are compiling
 *        with Visual C++, this routine will be renamed to DllMain.
 *        routine.
 *
 *----------------------------------------------------------------------
 */

BOOL APIENTRY
DllEntryPoint(HINSTANCE hInst,DWORD reason,LPVOID  reserved)
{
    return TRUE;
}

/*
 * Local variables:
 * mode: c
 * indent-tabs-mode: nil
 * End:
 */