ttk::messageBox

Megabyte

tk_messageBox is very ugly within Linux. Ever since I saw it, I've been unhappy. Until I saw some good work that inspired me. Schelte Bron created a replacement that works great under Unix for the file selection dialogs, present at: [L1 ]. I was inspired by his work and created a brand new ttk::messageBox widget.

What does it do? Its syntax is exactly like a tk_messageBox command, but within Linux it'll draw something much prettier. It uses a subset of icons from the Tango project, which can be changed if you're not happy with them.

See ttk::messageBox in action

[L2 ] [L3 ]

Install it

Place these files in a directory listed by $auto_path, or use the command "lappend" to append the directory containing the files to auto_path

Download it

The files are available at: [L4 ]

Feedback is appreciated. Enjoy!


Comments

Bryan Oakley - the default behavior is not the same as a standard tk_messageBox. Compare the result of "tk_messageBox -message Foo" with "ttk::messageBox -message Foo". In your case you're using no icon when an info icon should be the default. You also neglect to add a default "OK" button as well, making it impossible to properly dismiss the dialog (since you fail to add a WM_DELETE_WINDOW protocol handler to cancel the vwait).

I'd also like to suggest a way to reduce the amount of code and (arguably, at least) make the code more readable and easier to maintain.

You have a lot of code that looks like the following, where you have a switch that sets a variable to the pattern that matched:

switch $parameter {
    ok {
        set default_button "ok" 
    } 
    cancel {
        set default_button "cancel"
    }
    .... 
    default {
        error [format $unrecognized_parameter "-default"]
    }
}

I suggest you replace that with something like the following, using the new "in" operator:

if {$parameter in {ok cancel ...}} {
    set default_button $parameter
else {
    error ...
}

In the case of the -default switch, that replaces 29 lines of code with 5, a hefty savings. A similar change can be made each time you are using a switch merely to assign the pattern to a variable.

Another comment: in the unix_messageBox_check_default_parameter function you are using "puts" to signal errors. You should be calling error (or better, return -code error) instead.

Your use of TkDefaultFont is incorrect. You do this:

font create ttk_message_dialog_Italic_font -family TkDefaultFont -slant italic

However, TkDefaultFont isn't the name of a font family. It is a named font. Using the expand operator and the knowledge that the font command uses the last option in the case of duplicates, you can do this instead (though I personally don't think it's a good idea to create a named font as a side effect of using this dialog)

font create ttk_message_dialog_Italic_font {*}[font actual TkDefaultFont] -slant italic

And a final comment: make sure you add a protocol handler for WM_DELETE_WINDOW


peterc The GNU GPL2 license is a killer and quite different to most Tcl widget and extension licensing. I'd suggest the BSD license unless you want to significantly restrict its use.

Megabyte - peterc, your prayers have been heard. I decided to change the license to ISC, which is "the new BSD". Enjoy.


Megabyte - 12/29/2008

Thanks for the suggestions, Brian. I've changed the code accordingly to match tk_messageBox's behavior and cleaned the code a bit.


Megabyte - 12/30/2008

I noticed this piece of cleanup code on bind:

#A cleanup, just in case.        
bind $window_name <Destroy> {
        foreach name [image names] {
                if {![image inuse $name]} {
                        image delete $name
                }
        }
}

Was causing the file opening dialog to behave unpredictably when it was running, so I deleted it from the code. Would anyone explain me why does this happen?

Megabyte - 12/31/2008

Answer for the last question: unfortunately, not every dialog cleans itself after quitting, so I have to remove the images I create on my own.


Changelog

12/28/2008

  • Fixed a few bugs with the "-parent" option
  • Fixed a few cleanup issues.
  • Finished implementing the option "-default", which I forgot to do. "-default" focus on a requested button.

12/29/2008

  • Changed widget's behavior to match tk_messageBox.
  • Cleaned up the code using the new "in" operator. About 80 lines of code were removed!
  • Added a protocol handler for WM_DELETE_WINDOW
  • Fixed the way the italic font is created.

12/30/2008

  • Removed suspicious <bind> procedure because of problems with the opening file dialog.

12/31/2008

  • Cleaned up images on deletion, and made sure the window will be destroyed properly.
  • Fixed silly bug where the messageBox wouldn't recognize "-type okcancel"

1/09/2009

  • Minor bugfixes (I forgot the brackets in ::ttk::unix_messageBox_quit).

1/31/2009

  • Brazilian Portuguese supported added. The license was changed to ISC.
  • Fixed a serious bug with the -default option

2/2/2009

  • Fixed potential bug with file naming: renamed the catalogs to lowercase.