Re: A good menu tutorial?



Robert Hicks wrote:

Yes, I am not sure what I want to do here. The file format is going to
be a set one. I may create a simple ini file with something like
title=blah as the key/value then a user would just have to drop in the
file and update the ini file and I only have to parse that. Come to
think of it that is what i will do. It saves me from having to do
globbing and such to get directory and file names. I can use the
key/value to get the file name and title of the lesson and I can use
the section to denote the directory it resides in. I could even build a
wizard in the app to help create the ini section itself.

Hmmm, I will try that.

Robert


That seems like a more troublesome implementation. You are forcing the user to a) create an ini file, b) create some files on disk, and c) keep the two in sync. Or am I reading something wrong?

If you just build your menu straight from the disk, all the user has to do is b) create some files on disk.

I think I missed part of the thread -- why is doing the globbing so difficult?

Here's a quick example of how to create a menu based on a folder hierarchy and a file spec. It needs some error checking and perhaps some sanity checking (eg: don't create cascade menus for directories with no *.txt files), but it shows the general idea.

proc makeMenu {dir menu} {
set count 0
# create cascade menus for subfolders, and recurse
foreach subfolder [lsort [glob -nocomplain \
-directory $dir -type d *]] {
if {$subfolder eq "." || $subfolder eq ".."} continue
set name [file tail $subfolder]
set submenu [menu $menu.menu[incr count]]
$menu add cascade -label [string totitle $name] -menu $submenu
makeMenu $subfolder $submenu
}

# create commands for each .txt file
foreach file [lsort [glob -nocomplain \
-directory $dir -type f *.txt]] {
set name [file tail $file]
$menu add command -label [string totitle $name] \
-command [list showFile $file]
}
}

menu .menubar
.. configure -menu .menubar
menu .menubar.courses
..menubar add cascade -label Courses -menu .menubar.courses
makeMenu [pwd]/Courses .menubar.courses



.