Tk Library Source Code

View Ticket
Login
2023-10-29
11:18 • Closed ticket [b62d4dac89]: tklib getstring package bug on multi-monitor windows system plus 7 other changes artifact: 5b28bbc979 user: oehhar
11:17
Ticket [b62d4dac89]: fix window position parent center code check-in: f7de779587 user: oehhar tags: trunk
04:41 • Ticket [b62d4dac89] tklib getstring package bug on multi-monitor windows system status still Open with 3 other changes artifact: b0610fc583 user: anonymous
2023-10-28
23:59 • Ticket [b62d4dac89]: 3 changes artifact: b0e9d4d0e6 user: anonymous
20:11 • Ticket [b62d4dac89]: 4 changes artifact: 1286a4cdc8 user: oehhar
19:44 • Ticket [b62d4dac89]: 4 changes artifact: 8f7b93dc87 user: oehhar
19:41 • Ticket [b62d4dac89]: 3 changes artifact: dd5bd470e9 user: anonymous
12:17 • Ticket [b62d4dac89]: 4 changes artifact: a455ff66d1 user: oehhar
12:05 • Ticket [b62d4dac89]: 5 changes artifact: fb3a1781ea user: oehhar
12:01
Ticket [b62d4dac89]: fix window position parent center code closed check-in: 13ac3e4858 user: oehhar tags: ticket-b62d4dac89-getstring-window-pos
11:48 • New ticket [b62d4dac89] tklib getstring package bug on multi-monitor windows system. artifact: 6ad920d71b user: oehhar

Ticket UUID: b62d4dac899af099d2e9c0e4ea38eba485d48b9
Title: tklib getstring package bug on multi-monitor windows system
Type: Bug Version:
Submitter: oehhar Created on: 2023-10-28 11:48:19
Subsystem: (unused) Assigned To: aku
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2023-10-29 11:18:10
Resolution: Fixed Closed By: oehhar
    Closed on: 2023-10-29 11:18:10
Description:

This is the anonymous tk ticket https://core.tcl-lang.org/tk/info/721dad1c1bd8bbc8, closed there and opened here.

The tklib package getstring has a problem on windows 10 when a multi-monitor system is setup where the main monitor is to the right of a second monitor. The main monitor will then have it's upper left corner as 0,0 while the second monitor has a negative x coordinate, in the case of a 2 1080p monitors, it ends up as -1920,0.

Then when ::getstring::tk_getString tries to center its dialog, it ends up placing it far offscreen to the right, and cannot be accessed. Since there is also no window in the taskbar, one cannot move the dialog, so the program hangs waiting for input that cannot be given.

Here's the code they use:

    44      if {[info exists options(-geometry)]} {  
    45          wm geometry $w $options(-geometry)  
    46      } elseif {[winfo parent $w] == "."} {  
    47          set x [expr {[winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 - [winfo vrootx $w]}]  
    48          set y [expr {[winfo screenheight $w]/2 - [winfo reqheight $w]/2 - [winfo vrooty $w]}]  
    49   #        wm geom $w +$x+$y  
    50      } else {  
    51          set t [winfo toplevel [winfo parent $w]]  
    52          set x [expr {[winfo width $t]/2 - [winfo reqwidth $w]/2 - [winfo vrootx $w]}]  
    53          set y [expr {[winfo height $t]/2 - [winfo reqheight $w]/2 - [winfo vrooty $w]}]  
    54   #        wm geom $w +$x+$y  
    55      }  

The problem is that winfo vrootx $w returns -1920 in my case, so we have,

result from uplevel for: ? winfo screenwidth $w ? :
?1920?
result from uplevel for: ? winfo reqwidth $w ? :
?212?
result from uplevel for: ? winfo vrootx $w ? :
?-1920?
result from uplevel for: ? expr {[winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 - [winfo vrootx $w]} ? :
?2774?

this is in proc ::getstring::tk_getString {w var text args}

my workaround is to not do the wm geom, so the dialog is visible. I think not using the winfo rootx would also work.

User Comments: oehhar added on 2023-10-29 11:18:10:

Thank you for the tests. I personally also use a geometry parser.

I suppose, your regexp misses one detail: The syntax of the geometry string is "wxh+x+y" so for negative x, it is "800x600+-800+300".

The regexp might be: {^(\d+)x(\d+)\+([0-9-]+)\+([0-9-]+)

I have never seen negative y coordinates...

So, I will merge the fix and, at least, the bug is fixed.

If you look to the code, it locates on the toplevel of the parent, if the parent is not ".". It does not locate on the parent itself.

Thank you for all and take care, Harald


anonymous added on 2023-10-29 04:41:21:
Ok, it turns out -geometry solves my problem and so I'm using it, like this:

        regexp {^([0-9]+)x([0-9]+)([+-])([+-]?[0-9]+)([+-])([+-]?[0-9]+)} [wm geom .] -> dx dy xs xpos ys ypos
        set geom +$xpos+$ypos
        if {[getstring::tk_getString .g text "click and enter optional label:" -geometry $geom]} {
            puts "user entered: $text"
            ...
        }


Maybe that's what they should be doing when the parent isn't . and -geometry isn't specified. They could parse [wm geom $w] as above, or simpler if you like. That's my goto geom parser.

anonymous added on 2023-10-28 23:59:38:
I too hadn't really read the manual closely, I just copy/pasted the example. 

I've now tested the -geometry option, which is working.

However, when I tried specifying a path .f3.hom.g  where .f3.hom is a button in my window, I was expecting it to be placed on top of my button, but instead it was somewhere else, and always the same place, no matter where my window (and the button) actually were. I didn't test on my prior system however, and so I don't know what it does with negative coords for vrootx.

I think the problem is they're not actually using the location of my .f3.hom (or it's toplevel .) to place the dialog. They do use it's width and reqwidth, but not its location, so it isn't placed relative to my window. 

Perhaps they thought the vrootx/y would provide that info.

oehhar added on 2023-10-28 20:11:00:

Thank you for the test, that is great.

Could you please test and report all 4 cases:

On monitor 1 and 2 times parent of "." and not parent of ".".

tk_messageBox is not positioned by TCL, but by the Windows system. What you report is more a bug, than a feature ;-).

Thank you and take care, Harald


oehhar added on 2023-10-28 19:44:25:

I have now read the manual here:

https://core.tcl-lang.org/tklib/doc/trunk/embedded/md/tklib/files/modules/getstring/tk_getString.md

Ok, on the center of the current screen, if parent is "." and on the parent toplevel, if parent is not "."

I don't understand to much, why this difference is done, but ok.

I suppose on Windows extended screen mode, if the window is on screen 2, it will not be centered on the 2nd screen, but on the first. It might be on the 2nd screen, but not centered, if the screens have different sizes.

The 2nd case should work.

THank you for a test !

Take care, Harald


anonymous added on 2023-10-28 19:41:35:
The fix works; however, it will always place the dialog on the leftmost monitor. It probably would be even better if it did what tk_messageBox does, which centers it on the same screen as window .

But clearly it solves the problem of hanging the program.

oehhar added on 2023-10-28 12:17:57:

I really don't understand what should be acheved by the snippet.

If the widget should be centered over another widget or on the screen, the routine "::tk::PlaceWindow" in tk/library/tk.tcl may be used, at least to get some code.

The problem on windows is also, that the size of any 2nd screen may not be found. Only the size of the first screen may be catched via "screenwidth".

And it is worse. The screens must not be one beside the other. It is possible to hae a screen 1024 pixel wide at position -2000 and have the main screen start at 0.

Thank you, Harald


oehhar added on 2023-10-28 12:05:52:

For me, the formula is just wrong.

IMHO it should be:

set x [expr {[winfo vrootx $w] + [winfo screenwidth $w]/2 - [winfo reqwidth $w]/2}]
set y [expr {[winfo vrooty $w] + [winfo screenheight $w]/2 - [winfo reqheight $w]/2}]

So, the positioning starts from the uper corner.

This is implemented in checkin [13ac3e4858] in branch [ticket-b62d4dac89-getstring-window-pos]. Could you please check, if this works for you ?

Thank you and take care, Harald