Version 24 of messageBox

Updated 2002-11-15 16:00:50

http://purl.org/tcl/home/man/tcl8.4/TkCmd/messageBox.htm

Note: Unlike most Tk widgets, this widget's name (messageBox) is not the same as its creation command (tk_messageBox). Even more confusing to someone is that the source for the tk/tcl version is located in msgbox.tcl and tk.tcl.


incr Widgets also has a messageBox - http://incrtcl.sourceforge.net/iwidgets/iwidgets/messagebox.html

http://incrtcl.sourceforge.net/iwidgets/iwidgets/messagebox.gif


With recent versions, the Windows messageBox is "native", while the Unix one continues to rely on the implementation visible in ./library/msgbox.tcl. Before version 8.something, both Win* and Unix used msgbox.tcl (what about Mac?). Notice that this means that, if there's something about messageBox you don't like, you have the source code readily available for your enhancements.

[Detail example of how to do this.]

DKF: No. There has never been a non-native version of tk_messageBox on Windows and Macintosh, though tk_dialog is an approximation (and implemented purely in Tcl scripts) on all platforms.


[Give example of how to use option database with messageBox.]

On Unix, the default font for message boxes is Times 18, which is way too fat for longer text. Before creating a messageBox, just add the line

 option add *Dialog.msg.font {Times 12} 

to get a better readable font (won't hurt in Windows, where native message boxes are used anyway). (RS)

the wraplength also defaults to 3 inches but can be overriden the same way for long messages (BBH)

 option add *DIalog.msg.wrapLength 6i  

Martin Lemburg Because of the need to have a messagebox in the tclsh, I wrote a tclsh version ... tcl_messageBox. To download the stubs enabled version (tcl v8.1 and higher) use

    ftp://ftp.dcade.de/pub/ml/tcl/packages/tcl_messageBox10.zip
    (package only)
    ftp://ftp.dcade.de/pub/ml/tcl/packages/tcl_messageBox10.all.zip
    (inclusive the source, project files, etc.)

The syntax is simular to the tk_messageBox:

   tcl_messageBox ?option optionArg option optionArg ...?

options:

  • -align: left or right
  • -default: depends on the given type
  • -icon: exclamation, warning, info(rmation), asterisk, question, stop, error or hand
  • -message: string, may be empty
  • -modal: app(lication), window, system or task
  • -title: string, may be empty
  • -type: abortretryignore, ok, okcancel, retrycancel, yesno or yesnocancel
  • -help: this text

Harald Oehlmann On windows the tk_messageBox command sets the toplevel . to the top of the window stacking order. After the message box the toplevel . gets the focus.

To "repair" this behavior on tcl8.4 one can protect the windows over . by wm attributes -topmost 1 and save the focus over the routine.

    # tk_messageBox for Windows which keeps focus and stacking order
    proc messageBox args {
        # > Save focus
        set Focus [focus]
        # > Get the toplevels above . and set topmost attribute
        set lWindows [wm stackorder .]
        set TopPos [lsearch $lWindows .]
        if {-1 != $TopPos && $TopPos != [llength $lWindows]} {
                incr TopPos
                set lWindows [lrange $lWindows $TopPos end]
                foreach Wi $lWindows {
                        wm attributes $Wi -topmost 1
                }
        } else {
                unset lWindows
        }
        }
        # > invoke message box
        set Res [eval tk_messageBox $args]
        set Res [eval tk_messageBox $args]
        # > unset topmost attribute
        if {[info exists lWindows]} {
                foreach Wi $lWindows {
                        wm attributes $Wi -topmost 0
                }
        }
        # > Restore focus
        # Set focus to last widget if we had the focus before
        # otherwise set it to the topmost window
        if {0 < [string length $Focus]} {
                focus -force $Focus
        } elseif {[info exists lWindows]} {
                focus -force [lindex $lWindows end]
        }
        return $Res
    }

    # Test program for demonstration:
    foreach Wi {. .t1 .t2} {
        catch {toplevel $Wi}
        pack [button $Wi.b1 -text messageBox -command "messageBox -message So..."]
        pack [button $Wi.b2 -text tk_messageBox -command "tk_messageBox -message well..."]
    }

[Incorporate material in [L1 ].]


Tk syntax help - Arts and crafts of Tcl-Tk programming - Category Command