PureBasic Forum
https://www.purebasic.fr/english/

Multiple window problem
https://www.purebasic.fr/english/viewtopic.php?f=13&t=63778
Page 1 of 4

Author:  collectordave [ Mon Oct 19, 2015 2:08 pm ]
Post subject:  Multiple window problem

Just trying to open a second window from a main window but thought I noticed something strange so programmed the following to test.

I see this as very strange or a bug. What am I doing wrong?

Code:
IncludeFile "Window2.pb"

Global MainWindow.l,Wnd2.l

MainWindow = OpenWindow(#PB_Any, 0, 0, 600, 220, "", #PB_Window_SystemMenu)
frmMain = OpenWindow(#PB_Any, 0, 0, 600, 220, "", #PB_Window_SystemMenu)
CreateMenu(100, WindowID(frmMain))
  MenuTitle("File")
  MenuItem(101, "Open")

Repeat
 
  event = WaitWindowEvent()
 
  If EventWindow() = Wnd2;Select window.

      Window2::Event_Handler(event)
      Debug EventWindow()
     
    EndIf ;EventWindow()
     
 ;Not the second window so must be the main window
 Select event
     
   Case #PB_Event_Menu
     
     Select EventMenu()
             
       ;Only one menu item
      Case 101
        ;Show the 2nd window
        Debug "Open 2nd"
        Wnd2 = Window2::OpenWnd2()
       
     EndSelect ;Eventmenu
       
   Case   #PB_Event_CloseWindow
         
     End
         
 EndSelect ;Event

ForEver


The above is the main window

Code:

DeclareModule Window2
 
  Global ThisWindow.l
  Global Ok.i = 0
 
  Declare.l OpenWnd2()
  Declare Event_Handler(event)
 
EndDeclareModule

Module Window2

Global btnOk, btnCancel

Procedure.l OpenWnd2()
 
  ThisWindow = OpenWindow(#PB_Any, x, y, 280, 180, "Test Window", #PB_Window_SystemMenu)
  btnOk = ButtonGadget(#PB_Any, 130, 140, 60, 30, "Ok")
  btnCancel = ButtonGadget(#PB_Any, 210, 140, 60, 30, "Cancel")
 
  ProcedureReturn ThisWindow
 
EndProcedure

Procedure Event_Handler(event)
 
  Select event
     
    Case #PB_Event_CloseWindow
     
      CloseWindow(ThisWindow)

    Case #PB_Event_Gadget
     
      Select EventGadget()
         
        Case   btnOk
         
          Ok = 1
          CloseWindow(ThisWindow)
         
        Case btnCancel
         
          Ok = 0
          CloseWindow(ThisWindow)

      EndSelect
     
  EndSelect
 
EndProcedure

EndModule


That is the include file.

Works fine. Click menu item and second window opens click close and all is ok.
However I noticed that whatever event happens on either window when both are open EventWindow() allways comes back with the second window identifier even with mouse move events on the main window! Moving the two apart has no effect. You can see this from the debug statements I have added.

Author:  acreis [ Mon Oct 19, 2015 2:21 pm ]
Post subject:  Re: Multiple window problem

Hi,

Not sure about the issue, but I think an "Else" statement in needed in the event loop:

Code:


DeclareModule Window2
 
  Global ThisWindow.l
  Global Ok.i = 0
 
  Declare.l OpenWnd2()
  Declare Event_Handler(event)
 
EndDeclareModule

Module Window2

Global btnOk, btnCancel

Procedure.l OpenWnd2()
 
  ThisWindow = OpenWindow(#PB_Any, x, y, 280, 180, "Test Window", #PB_Window_SystemMenu)
  btnOk = ButtonGadget(#PB_Any, 130, 140, 60, 30, "Ok")
  btnCancel = ButtonGadget(#PB_Any, 210, 140, 60, 30, "Cancel")
 
  ProcedureReturn ThisWindow
 
EndProcedure

Procedure Event_Handler(event)
 
  Select event
     
    Case #PB_Event_CloseWindow
     
      CloseWindow(ThisWindow)

    Case #PB_Event_Gadget
     
      Select EventGadget()
         
        Case   btnOk
         
          Ok = 1
          CloseWindow(ThisWindow)
         
        Case btnCancel
         
          Ok = 0
          CloseWindow(ThisWindow)

      EndSelect
     
  EndSelect
 
EndProcedure

EndModule



Global MainWindow.l,Wnd2.l

MainWindow = OpenWindow(#PB_Any, 0, 0, 600, 220, "", #PB_Window_SystemMenu)
frmMain = OpenWindow(#PB_Any, 0, 0, 600, 220, "", #PB_Window_SystemMenu)
CreateMenu(100, WindowID(frmMain))
  MenuTitle("File")
  MenuItem(101, "Open")

Repeat
 
  event = WaitWindowEvent()
 
  If EventWindow() = Wnd2;Select window.

      Window2::Event_Handler(event)
      Debug EventWindow()
     
     
     
 Else ;   <------ Not the second window so must be the main window
     
 
 Select event
     
   Case #PB_Event_Menu
     
     Select EventMenu()
             
       ;Only one menu item
      Case 101
        ;Show the 2nd window
        Debug "Open 2nd"
        Wnd2 = Window2::OpenWnd2()
       
     EndSelect ;Eventmenu
       
   Case   #PB_Event_CloseWindow
         
     End
         
 EndSelect ;Event
 
    EndIf ;EventWindow()
 
ForEver



Author:  collectordave [ Mon Oct 19, 2015 2:28 pm ]
Post subject:  Re: Multiple window problem

Sorry

tried that but still the same. eventWindow() still saying all events are from the 2nd window.

if you change the code after eventwindow() to this

If EventWindow() = Wnd2;Select window.

Window2::Event_Handler(event)
Debug EventWindow()
Debug wnd2

you can see in the debug window that the same ID is displayed at all times even when you are nowhere near the second window. I expected an event on the mainwindow would raise an event for the mainwindow but seems I am mistaken

Author:  collectordave [ Mon Oct 19, 2015 2:38 pm ]
Post subject:  Re: Multiple window problem

A bit more info

I added the following

If EventWindow() = MainWindow
Debug "Main"
EndIf

right after the waitwindowevent just in case

The debug shows "Main" once when opened but not for mousemoves etc these still goto the 2nd window.

The behaviour stops after closing the 2nd window.

Author:  infratec [ Mon Oct 19, 2015 3:00 pm ]
Post subject:  Re: Multiple window problem

Hi,

forget my post, I made a typo.

Bernd

Author:  Fred [ Mon Oct 19, 2015 3:08 pm ]
Post subject:  Re: Multiple window problem

You use frmMain for creation and WindowMain for testing, sounds like a typo error.

Author:  infratec [ Mon Oct 19, 2015 3:18 pm ]
Post subject:  Re: Multiple window problem

I can not reproduce it:
Code:
DeclareModule Window2
 
  Global ThisWindow.i
  Global Ok.i = 0
 
  Declare.i OpenWnd2()
  Declare Event_Handler(event)
 
EndDeclareModule

Module Window2

  Global btnOk, btnCancel
 
  Procedure.i OpenWnd2()
   
    ThisWindow = OpenWindow(#PB_Any, x, y, 280, 180, "Wnd2", #PB_Window_SystemMenu)
    btnOk = ButtonGadget(#PB_Any, 130, 140, 60, 30, "Ok")
    btnCancel = ButtonGadget(#PB_Any, 210, 140, 60, 30, "Cancel")
   
    ProcedureReturn ThisWindow
   
  EndProcedure
 
  Procedure Event_Handler(event)
   
    Select event
     
      Case #PB_Event_CloseWindow
        CloseWindow(ThisWindow)
       
      Case #PB_Event_Gadget
        Select EventGadget()
          Case btnOk
            Ok = 1
            CloseWindow(ThisWindow)
           
          Case btnCancel
            Ok = 0
            CloseWindow(ThisWindow)
           
        EndSelect
       
    EndSelect
   
  EndProcedure
 
EndModule

Global MainWindow.i, Wnd2.i

MainWindow = OpenWindow(#PB_Any, 0, 0, 600, 220, "MainWindow", #PB_Window_SystemMenu)
frmMain = OpenWindow(#PB_Any, 0, 0, 600, 220, "frmMain", #PB_Window_SystemMenu)
CreateMenu(100, WindowID(frmMain))
  MenuTitle("File")
  MenuItem(101, "Open")

Repeat
 
  event = WaitWindowEvent()
 
  Select EventWindow()
    Case Wnd2;Select window.
      Debug "Wnd2"
      Window2::Event_Handler(event)
     
    Case frmMain
      Debug "frmWindow"
      Select event
        Case #PB_Event_Menu
          Select EventMenu()
            Case 101
              ;Show the 2nd window
              Debug "Open 2nd"
              Wnd2 = Window2::OpenWnd2()
           
          EndSelect ;Eventmenu
       
        Case #PB_Event_CloseWindow
          End
         
      EndSelect
     
    Case MainWindow
      Debug "MainWindow"
     
  EndSelect
 
ForEver


Bernd

Author:  collectordave [ Mon Oct 19, 2015 3:27 pm ]
Post subject:  Re: Multiple window problem

Hi Bernd

Copied and posted your code above straight in to a new file and the same problem happens.

I am running windows 7 if that has any bearing

Author:  Fred [ Mon Oct 19, 2015 3:30 pm ]
Post subject:  Re: Multiple window problem

No problem here, and it's a very simple code so I don't think there is any bug here.

Author:  infratec [ Mon Oct 19, 2015 3:35 pm ]
Post subject:  Re: Multiple window problem

Hi,

you know that you get only mouse events for an active window?

Author:  collectordave [ Mon Oct 19, 2015 4:01 pm ]
Post subject:  Re: Multiple window problem

thanks yes

Really optimised the code to

Code:

OpenWindow(1000, 0, 0, 600, 220, "MainWindow", #PB_Window_SystemMenu)
OpenWindow(1001, 300, 300, 280, 180, "Wnd2", #PB_Window_SystemMenu)

Repeat
 
  event = WaitWindowEvent()
 
  Select EventWindow()
     
    Case 1001
      Debug "Wnd2"
     
    Case 1000
      Debug "MainWindow"
     
   EndSelect  ;EventWindow()

Until event = #PB_Event_CloseWindow


So nothing at all should get in the way.

Starts and runs fine. Both windows are created debug window just shows Main Window once. I then clicked on the debug window and Wnd2 turned up.

Going a little further I moved the mouse over the second window and nothing. Moved the mouse over the main window and nothing not quite what I was expecting but good. I then clicked on the main window and hey presto lots and lots of main window events. Just to be sure i moved the mouse to the second window and still lots more main window events. You do have to keep clearing the debug window of course. I then click on the second window (after clearing the debug window again) and hey presto lots of 2nd window events even when moving to the main window with the mouse, clearing as i go.

Next step was to close down PureBasic completely and restart. Same thing happens.

The code cannot get any simpler can it?

Author:  Fred [ Mon Oct 19, 2015 5:07 pm ]
Post subject:  Re: Multiple window problem

Some events can be fired here and here, you have to test for a specific event (like PB_Event_CloseWindow etc.). Just monitoring if a window gets an event isn't reliable (and doesn't make sens).

Author:  bamsagla [ Mon Oct 19, 2015 6:07 pm ]
Post subject:  Re: Multiple window problem

Hi everyone,
playing around with some values I made the following change on the second example of acreis:

Code:
If EventWindow() = Wnd2 ; Select window.

  Window2::Event_Handler(event)
  ; Debug EventWindow()

  Debug Str(Wnd2) + " - " + str(event) ; modified line

Else ; <------ Not the second window so must be the main window


Now we can see which events are fired; with my system there are many 512 codes fired and acording to the #PB_Event_xxx constants that code is no official event response of PB.
However, if you don't close the opened second window and click on the first window the fired events for the second window are gone. So the events only show up if the second window is active and you hover over any window.

Maybe this helps collectordave to get closer into debugging this behaviour. In my opinion no bug because you have to explicly examine a matching PB-eventcode (#PB_Event_xxx) in your eventloop.

Does this make sense? :D
Bye Harry.

Author:  collectordave [ Mon Oct 19, 2015 6:31 pm ]
Post subject:  Re: Multiple window problem

The point is not the messages that are fired these are handled where required. It is when moveing the mouse over the second window when the main one has the focus fires events for the mainwindow. Vice versa as well.

windows message 512 is the mousemove event. they can all be seen here

http://wiki.winehq.org/List_Of_Windows_Messages

So not the messages. It is eventwindow() reporting the wrong window for the messages. It is this that makes no sense, I expect to get lots of messages for each window when the window is active such as 512 that is not a bother. When i make the main window active by clicking the titlebar then move the mouse pointer to the second window I still get the 512 message assigned to the main window while moveing the mouse over the second window.

Taking it a little further I have uninstalled pureBasic and reinstalled the latest release but the bug is still there.

Next is another computer that has not been used for a while then the mac mini.

Author:  Demivec [ Tue Oct 20, 2015 2:34 am ]
Post subject:  Re: Multiple window problem

Here is my version of your code, it should not be run with the debugger.

Code:
OpenWindow(1000, 0, 0, 600, 220, "MainWindow", #PB_Window_SystemMenu)
OpenWindow(1001, 300, 300, 280, 180, "Wnd2", #PB_Window_SystemMenu)
OpenWindow(1002, 600, 300, 250, 500, "Output", #PB_Window_SystemMenu)
ButtonGadget(1, 75, 20, 100, 20, "Clear")
TextGadget(#PB_Any, 0, 0, 250, 20, "Newest events listed first.")
ListViewGadget(0, 0, 40, 250, 480)

Repeat
 
  event = WaitWindowEvent()
 
  Select event
    Case #PB_Event_Gadget
     
      Select EventGadget()
        Case 1 ;button
          ClearGadgetItems(0)
      EndSelect
     
    Case #PB_Event_CloseWindow
      End
    Default
     
      Select EventWindow()
         
        Case 1001
          AddGadgetItem(0, 0, "Wnd2: event " + event)
         
        Case 1000
          AddGadgetItem(0, 0, "MainWindow: event " + event)

        Default
          If EventWindow() <> 1002
            AddGadgetItem(0, 0, "(" + EventWindow() + "): event " + event)
          EndIf
      EndSelect  ;EventWindow()
  EndSelect
 
 
Until event = #PB_Event_CloseWindow


I set things up this way to avoid any complications from the debugger interactions in the code.

It shows similar results by reporting events for the active window even when the mouse is moved over a different window.

Page 1 of 4 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/