| Ticket UUID: | 1192095 | |||
| Title: | Toplevel menus return incorrect active index | |||
| Type: | Bug | Version: | obsolete: 8.5a3 | |
| Submitter: | wildcard_25 | Created on: | 2005-04-29 00:28:44 | |
| Subsystem: | 10. Generic Menus | Assigned To: | fvogel | |
| Priority: | 5 Medium | Severity: | Minor | |
| Status: | Closed | Last Modified: | 2016-03-27 10:01:08 | |
| Resolution: | Fixed | Closed By: | fvogel | |
| Closed on: | 2016-03-27 10:01:08 | |||
| Description: |
Tk: v8.5a3 HEAD, v8.4.9 HEAD OS: WinXPsp2, Debian Linux-woody-testing When the attached script is run some unusual behaviour is observable. First on Windows with 8.5 & 8.4 Mouse over the menus without opening them - no output Now open the 'help' menu, active index is correct (if zero based like cascade menus) but returned label says you are in the 'edit' menu. Moving to the 'edit' menu will produce similar results. Moving to the 'file' menu will cause a crash. Also noticable is that the binding is called twice. Moving onto Linux and 8.5 and 8.4 Everything works as expected. Mousing over menu without opening them triggers binding. Labels are returned correctly. Indices are returned and are one based unlike cascade menus which are zero based. Binding is also called twice. | |||
| User Comments: |
fvogel added on 2016-03-27 10:01:08:
Fix (for issue 2) merged to trunk and core-8-6-branch. fvogel added on 2016-03-17 11:02:58: I tried to sketch a nex test case for the fix of issue #2 below (Active index numbering) but I failed.
Tests based on things like:
package require Tk
menu .m
.m add cascade -label "Item 1"
. configure -menu .m
update
set res ""
bind .m <<MenuSelect>> {lappend res [%W index active]}
event generate . <1> -x $x -y $y ; # with x and y in the menubar
do not work because Windows just does not sends the WM_MENUSELECT event when generating a click this way on the menubar.
I don not see any way to test this. Help welcome, otherwise there will be no test on this.
fvogel added on 2016-03-16 21:36:10: Regarding 1. "Mouse over the menus without opening them - no output": Apparently Windows just does not send the WM_MENUSELECT message before the menu is clicked, see: https://social.msdn.microsoft.com/Forums/vstudio/en-US/c216002c-8120-4936-9608-b33387592323/newbie-question-event-when-a-mouse-is-over-a-particular-menu-item?forum=vcgeneral There seem to be no other relevant message. I see no way to have this done on Win. Therefore... will not do! fvogel added on 2016-03-16 16:18:27: Atdded attachment containing the original test case fixed (by changing order of [. configure -menu] and [bind]) This works after application of the original patch slightly modified, see [78578f859d]. fvogel added on 2016-03-16 16:15:12: A few comments on each item you report:
1. "Mouse over the menus without opening them - no output":
Indeed in this case <<MenuSelect>> does not trigger on Win whereas it does on Linux. No idea why there is this difference between platforms. Perhaps this is just right, but reading the "Introduction" of the [menu] man page I believe <<MenuSelect>> should trigger whatever the platform whenever the mouse is over a menu entry.
There may be a specific Windows message dealing with this, that needs to be trapped and used in TkWinHandleMenuEvent(). Help welcome here.
2. Active index numbering:
The proposed patch breaks if (in the provided test case) the menu has "-tearoff 0" --> patch refined.
Committed in branch bug-1192095.
3. <<MenuSelect>> triggers twice (on Win and Linux) when a menu item is selected:
This is because when a menu is used as a menubar (through the -menu option for the toplevel), which is the case here, this menubar menu gets cloned.
When cloning the menu, Tk propagates the bindings of the master menu to the clone. In more details this happens as follows:
TkSetWindowMenuBar() is called when setting "-menu" on a toplevel.
This calls CloneMenu(), which copies the menu through a Tcl proc tk::MenuDup
tk::MenuDup duplicates the menu, and also its bindings and binding tags.
CloneMenu() adds the master menu window to the binding tags for the clone, after the clone tag
For instance one therefore gets:
% package require Tk
8.7a0
% toplevel .top
.top
% menu .top.m
.top.m
% .top.m add cascade -label "Item 1"
% bind .top.m <<MenuSelect>> {puts "Hello"}
% .top configure -menu .top.m
% bind .top.m <<MenuSelect>>
puts "Hello"
% bind .top.#top#m <<MenuSelect>>
puts "Hello"
See how the cloned menu has a non-empty binding?
Also:
% bindtags .top.#top#m
.top.#top#m .top.m Menu all
So, when clicking on a menu item, the <<MenuSelect>> event fires for the clone (.top.#top#m) menu, and then also for the master menu (.top.m). Since the bindings were copied between them, the bound script is executed twice.
This design is intentional: This is so the user can bind to either the clone (which may not be easy to do) or the entire menu clone structure.
The solution to this double binding triggering is simple: bind AFTER using the "-menu" option, as follows:
% package require Tk
8.7a0
% toplevel .top
.top
% menu .top.m
.top.m
% .top.m add cascade -label "Item 1"
% .top configure -menu .top.m
% bind .top.m <<MenuSelect>> {puts "Hello"}
% bind .top.m <<MenuSelect>>
puts "Hello from %W"
% bind .top.#top#m <<MenuSelect>> ; # returns nothing now
% bindtags .top.#top#m
.top.#top#m .top.m Menu all
No bug here IMO.
wildcard_25 added on 2005-05-01 10:22:12: File Added - 132475: tkWinMenu_tk85.diff wildcard_25 added on 2005-05-01 10:20:53: File Added - 132474: tkWinMenu_tk84.diff wildcard_25 added on 2005-05-01 10:20:52: Logged In: YES user_id=596509 Looks like tk expects a zero based response, but zero refers to the tearoff menu entry, whether its shown or not. The windows menubar doesn't have a hidden tearoff entry and therefore returns 0 instead of 1 for the file menu in the example. Patch bumps the index number up by one if it is a menubar. Patch for Tk8.4 also includes the fix in Tk8.5 for index being greater than number of entries, as I use the variable it creates. wildcard_25 added on 2005-04-29 07:28:44: File Added - 132211: testmenu.tcl | |||
Attachments:
- \testmenu_FIXED.tcl [download] added by fvogel on 2016-03-16 16:17:32. [details]
- tkWinMenu_tk85.diff [download] added by wildcard_25 on 2005-05-01 10:22:12. [details]
- tkWinMenu_tk84.diff [download] added by wildcard_25 on 2005-05-01 10:20:53. [details]
- testmenu.tcl [download] added by wildcard_25 on 2005-04-29 07:28:44. [details]
