how to make a modal dialog

18 views
Skip to first unread message

Zhang Weiwu

unread,
Jun 10, 2010, 11:54:07 AM6/10/10
to
[for impatient skip to the second paragraph]

I had nice experience on usenet that people knows information much more
up to date than wiki. However I can also describe the experience as
dreadful because usually I do googling before asking a question, only to
find hours of googling and reading on wiki is waisted time, as it turns
out there is a new and better method existing /not/ documented on wiki.
On example is case-insensitive list search, I just added explanation of
how to do it by adding 7 characters to the program, the *modern* (late 3
years) way to the wiki. Before I added it, the original description
teach a complicated method of regular expression search for the same
purpose. Consider I am absolutely newbie on tcl/tk, you can see how many
other newbie quited without asking usenet / changing wiki.

Back to the topic. The method of how to build a modal dialog on wiki is
obscure:

http://wiki.tcl.tk/3541

And more frightening, its author said:

the implementation is fairly rigorous.

He means, if I change his method, it won't grantee to work. But if his
method doesn't work in my application, then I have to change something
to make it work, which I as a newbie am not allowed ("rigorous").

So before really decipher the logic and subtleties of making a modal
dialog, I think I should be smart and ask usenet first, is there a new /
different way now? Hate to say that but I still miss more than a decade
ago some days in 90s programming for my customer on top of Microsoft
Access where change a dialog to modal is done by only adding a parameter
to some command (forgive me if I got it wrong, and don't ask which
command, since it has been too long to remember in detail).


Aric Bills

unread,
Jun 10, 2010, 11:16:35 PM6/10/10
to

Regarding up-to-date sources of information: the first place to look
is the man pages. [lsearch -nocase], for example, is well documented
on the lsearch manpage: http://www.tcl.tk/man/tcl8.5/TclCmd/lsearch.htm

Regarding modal dialogs: the easiest way to create a modal dialog is
with [tk_dialog] or [tk_messageBox]. If you need something more
complex, you can create your own modal dialog, but it does involve
some extra work, as the wiki page demonstrates. The code on the wiki
is still essentially the right way to create a modal dialog. The
things you need to understand are:

* [grab $win] prevents users from interacting with any window in your
application other than $win.
* [grab release $win] restores interactivity of the other windows in
your application.
* [wm transient] changes the window decoration on Windows.
* [wm protocol $win WM_DELETE_WINDOW] lets you provide commands to be
executed when someone tries to delete toplevel window $win. In the
example, the window is configured to release the grab and destroy
itself if someone tries to close the window.
* [raise $win] was probably included in the script to bring $win to
the foreground, but I don't think it actually does that (rather,
[raise] brings a widget to the top of the toplevel window in which it
lives); instead, use [wm withdraw $win] followed by [wm deiconify
$win], as in the other code on the modal dialog wiki page
* [tkwait window $win] pauses execution of the script until $win is
destroyed

Of course, all of those commands are well documented in the man pages.

Hopefully that gives you enough information to understand the code and
why it's there, and how to adapt it to your needs. If not, feel free
to ask again.

Best regards,
Aric

Zhang Weiwu

unread,
Jun 11, 2010, 6:56:20 AM6/11/10
to
On 2010年06月11日 11:16, Aric Bills wrote:
> Regarding up-to-date sources of information: the first place to look
> is the man pages. [lsearch -nocase], for example, is well documented
> on the lsearch manpage: http://www.tcl.tk/man/tcl8.5/TclCmd/lsearch.htm
>
>
Sure, but I did without luck.

The reason people use web is to get updated information that is less
formal but more updated than manual. In my case, I was using tk 8.4, and
there is nothing like -nocase in the manual. Then I look up the web,
hope to be hinted there is something new worth make me upgrade to tk8.5,
and found nothing. One week later I upgraded to 8.5 for a different
reason and found -nocase there.

So my behavior is pretty newbie but common. It is the way people usually
think and do. And, people don't often instantly upgrade to latest
version as soon as they hear it, with various reasons (I have one too),
as it might also cut him/herself, so they need to know why they need to
upgrade, from the web. Yes I could have googled tk8.5 manual instead of
the wiki, but I just learned this, a lot more newbies there probably are
not so talkative (and perhaps show their stupidity) like me on the
usenet to learn this:)

Thank you very much for other part of intopic detailed technical answer.
I for sure will read them later today.


Jeff Hobbs

unread,
Jun 11, 2010, 1:06:02 PM6/11/10
to
On Jun 10, 8:54 am, Zhang Weiwu <zhangweiwu+J...@realss.com> wrote:
> Back to the topic. The method of how to build a modal dialog on wiki is
> obscure:
>
> http://wiki.tcl.tk/3541
>
> And more frightening, its author said:
>
>     the implementation is fairly rigorous.
>
> He means, if I change his method, it won't grantee to work. But if his
> method doesn't work in my application, then I have to change something
> to make it work, which I as a newbie am not allowed ("rigorous").

An alternative for you is to use the tklib 'widget' package (available
in ActiveTcl, but also pure Tcl for whatever distro). Specifically
'widget::dialog'. While it doesn't have published docs, the source
file itself does include docs and examples. It abstracts all the
"required" elements behind a (hopefully) friendlier interface, with
added features (like timeouts and fine-tuned control).

Jeff

tom.rmadilo

unread,
Jun 11, 2010, 3:04:32 PM6/11/10
to

You can check out my nvlist package, which does case insensitive
search in Tcl8.4.

Although 8.5 was available at the time I wrote nvlist, I didn't want
to upgrade every Tcl installation I maintain to 8.5, so I
compromised.

Besides -nocase, the new [lsearch] has an -index option which makes
searching more exact.

Anyway, check out the code, which you can extract from the larger
framework:

http://junom.com/gitweb/gitweb.perl?p=tnt.git;a=tree;f=packages/tnt/tcl

(check nvlist-procs.tcl for code and nvlist-init.tcl for some
examples).

Or, here is the proc, called iSearch:

proc ::tnt::nvlist::iSearch { nvlistVar searchList } {

upvar $nvlistVar nvlist

# This will not work until 8.5
#return [lsearch -inline -all -nocase $nvlist $searchList]

set lowerSearchList [string tolower $searchList]

set resultList [list]
foreach item $nvlist {
#puts $item
set lowerItem [string tolower $item]
#puts "lItem = $lowerItem lsl = $lowerSearchList"
if {[llength [lsearch -all [list $lowerItem] $lowerSearchList]]} {
lappend resultList $item
}
}

return $resultList
}

Note the comment: one line of Tcl 8.5 code replaces all of the
following code.

Also, construction of the searchList is necessary because 8.4's
[lsearch] doesn't have the -index option (although searchList also
covers a range of list items, so I can't always benefit from -index).

Maybe the best advice, which you didn't ask for, is to just experiment
with a particular Tcl command. Instead of trying to read and
comprehend other peoples' code, Tcl is simple enough to play around
with on your own. Use tclsh as an interactive shell and whenever you
get an error do "set errorInfo" to find out more info on the error
(the i in errorInfo is capitalized).

Also: write your own procedures to handle even the most simple
patterns of use. Many Tcl commands have more than a handful of options
which, taken together, obscure the meaning of the line of code. You
will also be forced, over time, to upgrade dozens or hundreds of code
fragments to take advantage of future improvements in core commands.
Picking a set of Tcl command options is equivalent to picking an
algorithm. Once you have made that choice, wrap it up into something
easy to use, reuse and remember.

Reply all
Reply to author
Forward
0 new messages