Page 1 of 1

Using ResizeWindow() to position window appears to fail

Posted: Sat Aug 08, 2015 6:34 pm
by BasicallyPure
I have some code that works as expected with windows but fails when using Linux.

Run the code and drag the main window.
The temp window follows the main window and stays in it's default location.
This is the desired result.
Now drag the temp window off of the main window.
Click the button to hide the temp window.
Now click it again to unhide the temp window.
It will appear at the default location on the main window.
This is the desired result.
Now drag the temp window off of the main window again.
Hide and unhide the temp window by clicking the button two times.
It will not return to the default location but it should. :(
Why?
Now drag the main window and the temp window will return to the proper location.
Now you can drag the temp window off and click the button two times.
The temp window will return to correct position but you can only do it once until
you drag the main window again.

I am using Linux Mint and PureBasic 5.31 (x64).

Code: Select all

EnableExplicit

Define x, y, quit, wx, wy, m$

Macro Position_Temp_Window() ; place the temp window in it's default location
   x = WindowX(0,#PB_Window_InnerCoordinate) + 10  ;: Debug x
   y = WindowY(0,#PB_Window_InnerCoordinate) + 150 ;: Debug y
   ResizeWindow(1,x,y,#PB_Ignore,#PB_Ignore)
EndMacro

OpenWindow(0,0,0,400,400,"main window",#PB_Window_ScreenCentered | #PB_Window_SystemMenu)
   ButtonGadget(0,100,10,200,35,"Hide temp window",#PB_Button_Toggle)

OpenWindow(1,0,0,200,200,"temp window",#Null,WindowID(0))
   Position_Temp_Window()

SetActiveWindow(0)
   
Repeat
   Select WaitWindowEvent()
      Case #PB_Event_Gadget
         If EventGadget() = 0 ; button
            If GetGadgetState(0) = #True ; hide the temp window
               HideWindow(1,1)
               SetGadgetText(0,"UnHide temp window")
            Else ; show the temp window at default location on main window
               Position_Temp_Window() ;<-- only works first time
               HideWindow(1,0,#PB_Window_NoActivate)
               SetGadgetText(0,"Hide temp window")
            EndIf
         EndIf
      Case #PB_Event_MoveWindow ; we are dragging the main window
         If EventWindow() = 0 ; temp window follows main window
            Position_Temp_Window() ;<-- works always
         EndIf
      Case #PB_Event_CloseWindow
         If EventWindow() = 0
            quit = #True
         EndIf
   EndSelect
Until quit = #True

Re: Using ResizeWindow() to position window appears to fail

Posted: Sat Aug 08, 2015 7:09 pm
by Trond
A strange problem. To begin with, you should know that window managers on Linux takes considerable freedoms with regards to how to decorate windows, and where to position them. So not all problems are solvable. Although in this case there should be a way to force it right. Maybe there even is a bug in PB.

What happens is that ResizeWindow() fails when the given position is the same as the previous call. Simply adding a random call makes it work 99% of the time: ResizeWindow(1,x+Random(10),y+Random(10),#PB_Ignore,#PB_Ignore)

Of course this is not an acceptable solution.

Re: Using ResizeWindow() to position window appears to fail

Posted: Sat Aug 08, 2015 7:28 pm
by BasicallyPure
Trond wrote:What happens is that ResizeWindow() fails when the given position is the same as the previous call.
Yes, that's the way it appears.
Trond wrote:Maybe there even is a bug in PB.
I think this is likely but let's see if anyone else has a comment on this.

Thanks for looking at it.

Edit:
It looks like WindowX() and WindowY() have problems as well.
Replace the macro in the first post with this:

Code: Select all

Macro Position_Temp_Window() ; place the temp window in it's default location
   x = WindowX(0,#PB_Window_InnerCoordinate) + 10
   y = WindowY(0,#PB_Window_InnerCoordinate) + 150
   
   ResizeWindow(1,x,y,#PB_Ignore,#PB_Ignore)
   
   wx = WindowX(1) ; wx should = x
   wy = WindowY(1) ; wy should = y
   m$ = "x=" + x + " : WindowX(1)=" + wx : If wx <> x : m$ +" Fail" : EndIf : Debug m$
   m$ = "y=" + y + " : WindowY(1)=" + wy : If wy <> y : m$ +" Fail" : EndIf : Debug m$
   Debug ""
EndMacro
Drag the main window when temp window is visible and notice the errors.
Now hide the temp window and drag the main window.
Notice no errors when temp win is hidden.

Re: Using ResizeWindow() to position window appears to fail

Posted: Sun Aug 09, 2015 8:25 am
by BasicallyPure
I have found a solution to the original problem.
Just unhide the temp window before moving it, not after.

Code: Select all

EnableExplicit

Define x, y, quit

Macro Position_Temp_Window()
   x = WindowX(0,#PB_Window_InnerCoordinate) + 10
   y = WindowY(0,#PB_Window_InnerCoordinate) + 150
   ResizeWindow(1,x,y,#PB_Ignore,#PB_Ignore)
EndMacro

OpenWindow(0,0,0,400,400,"main window",#PB_Window_ScreenCentered | #PB_Window_SystemMenu)
   ButtonGadget(0,100,10,200,35,"Hide temp window",#PB_Button_Toggle)

OpenWindow(1,0,0,200,200,"temp window",#Null,WindowID(0))
   Position_Temp_Window()

SetActiveWindow(0)

Repeat
   Select WaitWindowEvent()
      Case #PB_Event_Gadget
         If EventGadget() = 0 ; button
            If GetGadgetState(0) = #True ; hide the temp window
               HideWindow(1,1)
               SetGadgetText(0,"UnHide temp window")
            Else ; show the temp window at default location on main window
               HideWindow(1,0,#PB_Window_NoActivate) ;<-- must unhide window before move not after
               Position_Temp_Window()
               SetGadgetText(0,"Hide temp window")
            EndIf
         EndIf
      Case #PB_Event_MoveWindow ; we are dragging the main window
         If EventWindow() = 0 ; temp window follows main window
            Position_Temp_Window()
         EndIf
      Case #PB_Event_CloseWindow
         If EventWindow() = 0
            quit = #True
         EndIf
   EndSelect
Until quit = #True