Fix messages opening on the wrong screen

Post your Gambas programming questions here.
Post Reply
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Fix messages opening on the wrong screen

Post by BruceSteers »

Here is a fix for if you have an application that runs on one screen but the message windows annoyingly pop open on the other one.
It's also good for if you want message and other modal windows opening up positioned with other forms instead of the default center screen.

my discovery is that modal windows like Message will open on the lowest positioned screen
(this is also the default for a lot of the gambas ide pop-open windows)

So if you have 2 monitors side by side they will always open on the left screen regardless of what screen is the Primary and what screen your application is on.
if you have one screen above the other then it's always the top screen.

I figured out a fix by Creating a Form.class file that has an override for the ShowModal() method.
Not just a fix, a handy feature :)

it checks if there is a calling window and notes it to get Window.Screen info or a form can be explicitly set.

it creates a timer to move the modal window after it's initiated.

The override means it can operate on ANY ShowModal call your application does itself or triggers. (it certainly works for Message() )

Just pop the Form.class into your own projects .src folder to have your own project use it.

See next post for archive

Hoping it helps..
BruceS
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Fix messages opening on the wrong screen

Post by BruceSteers »

i've edited it to have a couple of additional properies...

Form.ModalMode
select a mode
can be MODAL_NORMAL, MODAL_SAMESCREEN, MODAL_WINDOWSTACK, MODAL_WINDOWCENTER

MODAL_NORMAL, normal gambas mode
MODAL_SAMESCREEN, move centered on the same screen as the active window
MODAL_WINDOWSTACK, move to the calling windows TopLeft position
MODAL_WINDOWCENTER, move centered to the active window

Form.ModalMode_Windows As String[]
to limit the windows it will operate on by checking the Forms name
Eg.
Form.ModalMode_Windows = ["FMessage", "FInputBox"]


Form.ModalUseForm As Form
if for example you set Form.ModalUseForm = FMain then ALL modals will open centered on FMain and not a possible different active window.

Attached is a simple test app to see the different modes on the message box and an InputBox.

Maybe this will help others , or even inspire our Ben to implement something similar in the matrix :)
Attachments
MessageCorrectScreen-0.0.5.tar.gz
(13.15 KiB) Downloaded 61 times
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Fix messages opening on the wrong screen

Post by BruceSteers »

Here is an alternative method.

This intercepts the normal ShowModal() method and uses Show() instead and makes the window pseudo-modal

this works better/cleaner than the other method as the modal windows do not open in their usual place then move , they open at the desired location to begin with.

This adds 2 properties to From.class that will center new windows to the current Application.ActiveWindow (as you can test in the test application)
Form.StickyModals
Form.StickyForms

StickyModals intercepts ShowModal() and works on ALL modal windows.
StickyForms intercepts Show() and works on ALL opening forms if their X,Y coords have not been explicitly set.

There's not various styles with this one just to center on the active window.

Can anyone tell me if my pseudo ShowModal() method is sufficient in the code below?

Cheers All

Test app in post below

the Form.class (updates to V0.2)

' Gambas class file

Create Static

Export

Private $bIsPseudoModal As Boolean
Private $iModalValue As Integer

'' Make all modal windows center on the active window
Static Property StickyModals As Boolean Use $bStickyModals

'' Make all form windows without X,Y set center on the active window
Static Property StickyForms As Boolean Use $bStickyForm

'' Make all form windows without X,Y set center on the Mouse position
Static Property StickToMouse As Boolean Use $bStickToMouse

'' Make StickyForms and StickyModals stick to a specific window not the active one.
Static Property StickToWindow As Window Use $hStickToWindow
Private $hWin As Window

Public Sub Show()

  Dim XY As Integer = -16 ' gtk uses -16 as a non set position value, qt uses 0

  If Me.Parent Then Return 

  If Component.IsLoaded("gb.qt4") Or If Component.IsLoaded("gb.qt5") Then xy = 0

  If $bStickyForm And If Me.X = xy And If Me.Y = xy Then MoveMe
  Super.Show

End

Private Sub MoveMe() As Boolean

  If $bStickToMouse Then Me.Move(Mouse.ScreenX - (Me.W / 2), Mouse.ScreenY - (Me.H / 2))

  $hWin = Application.ActiveWindow
  If $hStickToWindow And If Object.IsValid($hStickToWindow) Then $hWin = $hStickToWindow
  If Not $hWin And If Not $bStickToMouse Then Return False

  If Not $bStickToMouse Then Me.Move($hWin.X + ($hWin.W / 2) - (Me.W / 2), $hWin.Y + ($hWin.H / 2) - (Me.H / 2))

  Return True
  

End

Public Sub ShowModal() As Integer

  If $bStickyModals Then
    If MoveMe() Then
'      Dim hWin As Window = Application.ActiveWindow
      $bIsPseudoModal = True
      If Not $bStickToMouse Then
      Dim bEnabled As Boolean = $hWin.Enabled
      $hWin.Enabled = False
      Endif
      Me.Utility = True
      Super.Show
      While Object.IsValid(Me)
        Wait 0.1
      Wend

      If Object.IsValid($hWin) And If Not $bStickToMouse Then
        $hWin.Enabled = bEnabled
      Endif

      Return $iModalValue

    Endif
  Endif

  Return Super.ShowModal()

End

Public Sub Close(Optional Value As Integer) As Boolean

  If $bIsPseudoModal Then
    $iModalValue = Value
  Endif
  Super.Close(Value)

End


If at first you don't succeed , try doing something differently.
BruceS
User avatar
PJBlack
Posts: 184
Joined: Tuesday 9th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Fix messages opening on the wrong screen

Post by PJBlack »

after a very quick test it seems to work as expected ...
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Fix messages opening on the wrong screen

Post by BruceSteers »

PJBlack wrote: Tuesday 8th November 2022 2:23pm after a very quick test it seems to work as expected ...
Cool :)
i just dropped the Form.class into my gambas/comp/src/gb.form/.src source folder and set StickyModals to be true by default.

So now StickyModals is activated across the board on ALL my progs unless i turn it off :)

I added a property like the other versions Form.ModalUseForm...

Form.StickToWindow = window
if you give that a window it will center on that unless it's gone then it defaults to Activewindow unless that's also gone then it just uses normal ShowModal().

Added property Form.StickToMouse to make modals.forms center at mouse pos. (useful if you have no window to use to center on or if you want a message in your face wherever you may be)

updated test app shows how you can make a message pop up on either the modal window or the main form or with the mouse.

It's a brave new world of windows and messages opening where i want them to , or closer to the area my mouse is probably at :)
Attachments
StickyWindows-0.0.3.tar.gz
(13.7 KiB) Downloaded 64 times
If at first you don't succeed , try doing something differently.
BruceS
Post Reply