I'm trying to put together a little BWidgets demo that simulates a
Windows Explorer directory display, but I'm having trouble retrieving
and displaying icons.
Here is my code for retrieving icons by document type/extension:
proc findnewIcon {extension} {
global icon
if {[catch {set doctype [registry get HKEY_CLASSES_ROOT\\$extension]}]} {
set doctype [registry get HKEY_CLASSES_ROOT\\$extension ""]
puts $doctype
}
if {[catch {set icon [registry get
HKEY_CLASSES_ROOT\\$doctype\\DefaultIcon {}]}]} {
set icon [registry get HKEY_CLASSES_ROOT\\$doctype\\DefaultIcon {} ]
}
return $icon
puts $icon
}
This code retrieves a reference to an icon. For instance, findnewIcon
.doc returns output like this: "C:/path/to/WordPad.exe", 1
However, I can't figure out how to display this icon, at least using the
::ico::getIcon command. I have tried to extract the path to WordPad with
this command:
set imageicon [split $icon ,]
but trying to load the icon with this command: ::ico::getIcon $imageicon
""
returns this error:
unsupported file format EXE"
Apparently it can't read the path because of the quote.
Has anyone successfully combined registry + ico to get the correct icon
for a specific document type to display? If so, any ideas on what I'm
doing wrong?
--
Kevin Walzer
Poetic Code
http://www.kevin-walzer.com
You are using ::ico::getIcon in a wrong way. Try:
package req ico
pack [label .x -image [::ico::getIcon \
{C:\Program Files\Windows NT\Accessories\wordpad.exe} 1]]
If the above does not work, update your tklib package to the latest one,
or use a newer ActiveTcl distribution...
George
>
> You are using ::ico::getIcon in a wrong way. Try:
>
>
> package req ico
> pack [label .x -image [::ico::getIcon \
> {C:\Program Files\Windows NT\Accessories\wordpad.exe} 1]]
>
> If the above does not work, update your tklib package to the latest one,
> or use a newer ActiveTcl distribution...
>
> George
This approach seems to require that the path to the .exe/.dll be
hard-coded in my script. What I'm trying to do requires that paths be
parsed and loaded at runtime. I'd like to be able to query the registry
for the path to an .exe or .dll containing the default icon, then hand
that off to the ico package in a variable. Any suggestions on how to do
this (based on the code ample I posted earlier)?
You wrote the answer yourself - parse the output from the registry.
Quite often the answer to "how do I parse XXX" is "use regexp".
Maybe the solution is something like this:
set data [registry get ...]
# assuming data has something like: "foo.exe", 1
regexp {\"(.*?)\".*?([0-9]+)} $data -- filename n
::ico::getIcon $filename $n
--
Bryan Oakley
http://www.tclscripting.com
That is, while [regexp] is incredibly powerful, and *very* widely
applicable in computing, Tcl's [scan] is a little gem that I believe
is used less than it deserves, particularly in light of the times
when it yields a more readable result than [regexp]. If I under-
stand our hypothetical example correctly, it might yield to
set datum {"foo.exe", 1}
scan $datum {"%[.a-z]", %d} file number
set file
set number
scan
I thinks this is going to fail in the following cases:
a) The path in "datum" contains / or \
b) it contains spaces
c) it contains unicode chars different to [a-z]
Points a) and b) can be easily corrected. In order to fullfill
point c) I think that it is necessary to go to regexps.
Ramon Ribó
En Thu, 06 Jul 2006 18:55:16 +0200, Cameron Laird <cla...@lairds.us>
escribió:
--
Compass Ing. y Sistemas Dr. Ramon Ribo
http://www.compassis.com ram...@compassis.com
c/ Tuset, 8 7-2 tel. +34 93 218 19 89
08006 Barcelona, Spain fax. +34 93 396 97 46