In article <3364DD2D.86195F2@physik.tu-muenchen.de>,
Christopher Eltschka <celtschk@physik.tu-muenchen.de> wrote:
[ ... ]
> BTW, what would the following possible C++ interface for menus look
> like in tcl/tk?
>
> MenuBar MainMenu
> (
> Menu("Main")
> .Item
> (
> Menu("~File")
> .Item(Command("~Open", cmOpenFile), helpOpen)
> .Item(Command("~Save", cmSaveFile), helpSave)
> .Item(Command("~Save ~As", cmSaveFileAs), helpSaveAs)
> .Separator()
> .Item(Switch("Make ~Backup", BackupFlag), helpBackup)
> )
> .Item
> (
> Menu("~Search")
> .Item(Command("~Find"), cmFind), helpFind)
> .Item(Command("~Replace"), cmReplace), helpReplace)
> .Separator()
> .Item(Command("Find ~Again"), cmFindAgain), helpFindAgain)
> )
> )
>
> Here cmXXX specifies the Action (or Message, as usual in GUIs),
> while helpXXX specifies an optional help context for online help.
Well, using a (modified) version of some code that I've used in my own
apps (and which is available on my website), I get something like this:
dkfMenu .mbar hvar {
button -text "Main" -contents {
cascade -label "~File" -menu {
command -label "~Open" -command cmOpenFile -help helpOpen
command -label "~Save" -command cmSaveFile -help helpSave
command -label "Save ~As" -command cmSaveFileAs \
-help helpSaveAs
separator
checkbutton -label "Make ~Backup" -variable BackupFlag \
-help helpBackup
}
cascade -label "~Search" -menu {
command -label "~Find" -command cmFind -help helpFind \
-accel <Control-s>
command -label "~Replace" -command cmReplace \
-help helpReplace -accel <Meta-percent>
separator
command -label "Find ~Again" -command cmFindAgain \
-help helpFindAgain -accel <F3>
}
}
}
Modifications from my published code are as follows:
1) Excessive curlies are removed (a line or two as Tcl has built-in
primitives to help with this trick)
2) "~" processing to extract underline positions (totally trivial in
Tcl)
3) Help is modified to redirect through a message catalogue - not
unreasonable in the circumstances... :^)
Points to note:
1) I've specified my keyboard accelerators as part of the menu
definition. That causes a user-readable string to be attached to
the menu entry and an appropriate keyboard binding to be made
within that window.
2) There are a lot of options that I've not specified in there.
Having a menu item with a picture on it instead of a text label
does not increase the complexity of the code.
3) I wrote the original code nearly two years ago as one of my first
little projects in Tcl. It is _not_ even close to idiomatic IMHO
As you can see, Tcl can easily see everything that you did (without
difficulty I might add) and raise proper keyword-based arguments that
make maintenance much easier while allowing for a much greater range
of behaviour. Indeed, this is an advantage shared with languages like
Perl, Lisp and many others (that should placate Erik a little... :^)
and it is the design of C/C++ which is a stumbling block here.
Admittedly, this can be kludged around (either using the inefficient
mechanism used above or by using a variable number of arguments, which
unfortunately compromises anything even remotely approximating to type
safety)
Donal.
--
Donal K. Fellows http://r8h.cs.man.ac.uk:8000/ (SAY NO TO COMMERCIAL SPAMS!)
(work) fellowsd@cs.man.ac.uk Dept. Comp. Sci, Univ. Manchester, U.K.
| donal@ugglan.demon.co.uk 6,Randall Place, Heaton, Bradford, U.K. (home)
+-> ++44-161-275-6137 Send correspondence to my office ++44-1274-401017 <-+
|