Version 2 of menu

Updated 2002-02-05 08:06:04

Contents:

  • How to add to the System menu on Windows
  • How to add control of the console to the System menu

See also Multilingual menu


How to add to the System menu in windows: Courtesy KBK (Kevin Kenny) on the comp.lang.tcl newsgroup: Chris Nelson wrote: I'm trying to add an item to the system menu of an NT-based Tcl application and nothing is happening. What am I doing wrong?

 % menu .mb
 .mb
 % . configure -menu .mb
 % .mb add command -label "file"
 % menu .mb.system
 .mb.system
 % .mb.system add  command -label "foo"

This confused me for a while, too. You also need to do:

    .mb add cascade -label System -menu .mb.system

Also, tearoffs look awful in the system menu. You probably want to do:

    .mb.system configure -tearoff 0

KBK: (14 November 2000) -- After I posted this, I went to put it on the Wiki, and found that someone already had! (I guess the Wiki runs on Internet time, too!) Well, I can improve on my own work with this little convenience:


How to add console control to the System menu on Windows:

I've found it useful to be able to do 'console hide' and 'console show' in applications that are already deployed, and found that the system menu is one place to put the function that doesn't clutter up the user interface. (A View menu, if the app has one, might also be a logical choice.)

As soon as she saw it, my system tester said that the separate Show and Hide functions ought to be a checkbutton. I found that required a little bit more hacking, and this was the result:

    # The ''consoleAux'' namespace holds variables and procedures
    # that help manage the system console on Windows

    namespace eval consoleAux {

        namespace export setup;         # Initialization

        namespace export toggle;        # Command to map/unmap the
                                        # console on demand

        variable mapped;                # Flag == 1 iff the console
                                        # is currently mapped

    }

    #------------------------------------------------------------------
    #
    # consoleAux::setup --
    #
    #       Set up the system console control on Windows.
    # 
    # Parameters:
    #       None.
    #
    # Results:
    #       None.
    #
    # Side effects:
    #       Bindings are established so that the variable,
    #       '::consoleAux::mapped' is set to reflect the state
    #       of the console.
    #
    # Notes:
    #       Depends on undocumented internal API's of Tk and
    #       therefore may not work on future releases.
    #
    #------------------------------------------------------------------

    proc consoleAux::setup {} {

        # Make the console have a sensible title

        console title "Console: [tk appname]"

        console eval {

            # Determine whether the console has started in the
            # mapped state.

            if { [winfo ismapped .console] } {
                consoleinterp eval {
                    set ::consoleAux::mapped 1
                }
            } else {
                consoleinterp eval {
                    set ::consoleAux::mapped 0
                }
            }

            # Establish bindings to reflect the state of the
            # console in the 'mapped' variable.

            bind .console <Map> {
                consoleinterp eval {
                    set ::consoleAux::mapped 1
                }
            }
            bind .console <Unmap> {
                consoleinterp eval {
                    set ::consoleAux::mapped 0
                }
            }
        }

        return
    }

    #------------------------------------------------------------------
    #
    # consoleAux::toggle --
    #
    #       Change the 'mapped' state of the console in response
    #       to a checkbutton.
    #
    # Parameters:
    #       None.
    #
    # Results:
    #       None.
    #
    # Side effects:
    #       If the console is marked 'mapped', shows and raises it.
    #       Otherwise, hides it.
    #
    #------------------------------------------------------------------

    proc consoleAux::toggle {} {
        variable mapped
        if {$mapped} {
            console show
            console eval { raise . }
        } else {
            console hide
        }
        return
    }

    # Sample application that shows the use of the console in the
    # system menu.

    # First, make sure there is a system menu.

    menu .menubar
    . configure -menu .menubar

    .menubar add cascade -label File -menu .menubar.file -underline 0

    menu .menubar.file
    .menubar.file add command -label Exit -underline 1 -command exit

    if { [string equal $tcl_platform(platform) windows] } {
        .menubar add cascade -label System -menu .menubar.system

        menu .menubar.system -tearoff 0

        # Add the 'Show Console' item to the system menu

        ::consoleAux::setup
        .menubar.system add checkbutton \
            -label {Show Console} \
            -variable ::consoleAux::mapped \
            -command ::consoleAux::toggle

    }

Tk syntax help - Category Command