Multiple window problem

Just starting out? Need help? Post your questions and find answers here.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Multiple window problem

Post by collectordave »

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: Select all

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: Select all


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.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
acreis
Enthusiast
Enthusiast
Posts: 182
Joined: Fri Jun 01, 2012 12:20 am

Re: Multiple window problem

Post by acreis »

Hi,

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

Code: Select all



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 


collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Multiple window problem

Post by collectordave »

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
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Multiple window problem

Post by collectordave »

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.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Multiple window problem

Post by infratec »

Hi,

forget my post, I made a typo.

Bernd
Last edited by infratec on Mon Oct 19, 2015 3:08 pm, edited 1 time in total.
Fred
Administrator
Administrator
Posts: 16617
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Multiple window problem

Post by Fred »

You use frmMain for creation and WindowMain for testing, sounds like a typo error.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Multiple window problem

Post by infratec »

I can not reproduce it:

Code: Select all

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
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Multiple window problem

Post by collectordave »

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
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Fred
Administrator
Administrator
Posts: 16617
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Multiple window problem

Post by Fred »

No problem here, and it's a very simple code so I don't think there is any bug here.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Multiple window problem

Post by infratec »

Hi,

you know that you get only mouse events for an active window?
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Multiple window problem

Post by collectordave »

thanks yes

Really optimised the code to

Code: Select all


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?
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Fred
Administrator
Administrator
Posts: 16617
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Multiple window problem

Post by Fred »

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).
User avatar
bamsagla
User
User
Posts: 62
Joined: Sat Jan 30, 2010 10:10 am
Location: Laufen, Bavaria, Germany

Re: Multiple window problem

Post by bamsagla »

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

Code: Select all

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.
- Sherlock Holmes - "When you have eliminated the impossible, whatever remains, however improbable, must be the truth."
In my opinion, he must have been a programmer.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Multiple window problem

Post by collectordave »

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.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
Demivec
Addict
Addict
Posts: 4085
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Multiple window problem

Post by Demivec »

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

Code: Select all

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.
Post Reply