Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

wheel mouse un-focus a window?

Status
Not open for further replies.

erixire

Technical User
Jun 4, 2002
72
CA
Hi,

I made a little script in TCL/TK, but I have a little bug in it and I want to know if someone know how to solve it. I have a master window with some information in it and I make appear a small window (that I want to have the focus on it). The focus is okay normally, but someone found that when you press in the master window and then scroll the wheel mouse, the focus goes into the master window and the small window disappear (goes to the back of the master window). Here is my code for the small window:

toplevel .user

# ... some code ...

wm deiconify .
focus .user
grab .user
tkwait window .user

Does somebody had the same problem?
Thanks.
 
With Windows NT (4.0 or 2k) I have a similar phenomenon: scrolling the wheel over a window does not focus this window but scrolls the old window having the focus. I need to click inside the new window before scrolling.

Try 'focus -force .user' to be sure the small window takes effectively the focus.
Or bind to <FocusOut> to keep the focus on the small window.

ulis
 
Hi,

I tried to bind to this:

bind .user <FocusOut> &quot;wm deiconify .; focus .user&quot;

but windows was freaking out. If I only put &quot;focus .user&quot;, it didn't work at all. I also tried <Leave> without result. Any other ideas?

Thanks
 
As Avia explained 'focus' can be unsufficient if you don't give focus to a widget of the toplevel.
Can you try 'focus -force' in the binding to FocusOut ?

ulis
 
Odd. I must admit that I've never encountered this problem before, but then I almost never use a mouse with a mouse wheel. I wasn't able to recreate the problem you described when I hooked up a mouse with mouse wheel, either.

Checking the bind command manual page, this is what it had to say about <MouseWheel> events:

&quot;By rolling the wheel, the system will generate MouseWheel events that the application can use to scroll. Like Key events the event is always routed to the window that currently has focus....&quot;

So, it doesn't look as though the mouse wheel is causing the focus problems. Which seems to point to the user clicking in the main window before scrolling with the mouse wheel. My guess is that you're somehow not establishing your grab properly.

Grabs are very tricky things. In particular, the window must be already visible when you execute the grab command, or else the grab fails. So if your code does:

Code:
wm deiconify .dialog
grab .dialog

The window manager might not have had time to display the window before the grab command executes.

The best way to solve this problem is as follows:

Code:
wm deiconify .dialog
tkwait visibility .dialog
grab .dialog

The tkwait visibility command will cause your script to pause until the dialog window is displayed. It's then safe to establish the grab.

Modal dialogs are actually quite tricky beasts to create. There are a lot of things to think through and handle. I'm going to include a sample program demonstrating how I usually go about creating modal dialogs. It illustrates the different things to consider and how to go about handling them in your script.

Code:
package require Tk

# Create a simple dialog and withdraw
# it for later use

toplevel .dialog

label .dialog.msg -text &quot;You have mail!&quot;
pack .dialog.msg -padx 4 -pady 4

# Setting the global variable &quot;done&quot; is
# how we signal that we're ready to dismiss
# the dialog.

button .dialog.ok -text &quot;Ok&quot; -command {
    set done 1
}
pack .dialog.ok -padx 4 -pady 4

wm title .dialog &quot;New Mail&quot;
wm group .dialog .

# Set the minimum size of the window when
# it is first displayed

bind .dialog <Configure> {

    # We'll use the initial size as
    # the minimum size of the window

    wm minsize .dialog         [winfo reqwidth .dialog]         [winfo reqheight .dialog]

    # We no longer need our <Configure>
    # binding, so delete it.

    bind .dialog <Configure> {}
}

# If the user closes the window with the
# window manager (e.g., by clicking on the
# X in the titlebar), just invoke the Ok
# button to dismiss the window without
# destroying it.

wm protocol .dialog WM_DELETE_WINDOW {
    .dialog.ok invoke
}

# Once we've grabbed events, any mouse
# clicks on other windows in the application
# get redirected to our dialog. In case our
# dialog has been obscured by other windows,
# let's raise the dialog on top of all other
# windows.

bind .dialog <ButtonPress> {
    raise .dialog
}

# Hide our dialog window until we need it

wm withdraw .dialog

# This is the procedure that displays the
# dialog, and then waits until it is
# dismissed.

proc DisplayDialog {} {
    global done

    # Display the window
    
    wm deiconify .dialog

    # Wait until the window is really
    # visible before grabbing
    
    tkwait visibility .dialog

    # Now grab events and assign focus to
    # the Ok button
    
    grab .dialog
    focus .dialog.ok

    # Wait until the user clicks the Ok button
    
    tkwait variable done
    
    # Release the grab and hide the window

    grab release .dialog
    wm withdraw .dialog
}

# This is our main window

text .t -width 20 -height 4
button .popup -text &quot;Check Mail&quot;     -command DisplayDialog
button .quit -text &quot;Quit&quot; -command exit
pack .t .popup .quit -padx 4 -pady 4
- Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
I tried to do the same as you did, but it really seems to be a focus problem. Because when I try to click the main window, the small window remain with the focus on it. If I try to click on the main window and press any key, it's really the small window that receives it and stay with the focus. But, when I use the wheel mouse, the small window disappear at the back of the main window. Then, if I click in the main window, the small window appear without the focus on it and the main has it now. But even if the main window has the focus, the mouse and keys have not effect on it. The small window remain at the top, without the focus. At that point, the wheel mouse has no effect. If I click on the small window, then everything come back at the beginning. I'm using Win2000 with TCL 8.3

Thanks for the good example AviaTraining, I learn something new in it. Do you have another idea of what this problem can be?
 
Hi again AviaTraining,

I copy-pasted your code, run it and I had the same problem: when I click the &quot;Check Mail&quot; button, the &quot;new mail&quot; window appear. But, if I put the mouse over the main window (named &quot;focus&quot;) and scroll the wheel, I have the exact same problem that I have with my window: the main window takes the focus. So it don't seem to be an error in the code. Maybe it's Win2000? Maybe it's my Tcl version?
 
oh, I forgot to put my exact version: 8.3.2 (Win2000)
Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top