Page 1 of 2

Understanding how the events works

Posted: Sun Jun 24, 2012 3:54 pm
by takis76
I opened one new thread about how the events of ProGUI or generic PureBasic Gadgets works.

I have created one simple menu and one simple toolbar.

How to capture the events from menu and the toolbar?

About events the only thing I know is:

Code: Select all

  Repeat
    Event = WaitWindowEvent()
    If Event = #PB_Event_CloseWindow  ; If the user has pressed on the close button
      Quit = 1
    EndIf
  Until Quit = 1
Nothing Else , and from this I know the WaitWindowEvent return some event with number and stores this number to Event variable.

No complex things to understand how the events work. I started experimenting to see how mechanics works.

First I put "Debug Event" to see what kind of data this WaitWindowEvent() function returns.

I receive numbers like 13104 , 15 , 5 , 512 ...
I don't know what these numbers means and the variable #PB_Event_CloseWindow is equal of some of these numbers.

So if the Event return the equal number as the variable #PB_Event_CloseWindow so the comparison returns true and the program give 1 to quit variable and the loop ends.

So far so good. How to make these numbers more human read. How to translate all of these numbers to real event messages?
And then I will go to the next to see how to know what gadget I press , if the gadget is menu , if the gadget is toolbar , if the gadget is menu what menu item I have clicked etc...

Re: Understanding how the events works

Posted: Sun Jun 24, 2012 4:27 pm
by takis76
New discovery:

There are 3 event emitters

EventGadget()
EventMenu()
EventWindow()

EventGadgets returns numbers of what gadget I have action to.
EventMenu returns numbers of what items on menus I have action to.
And EventWindow returns the window number which the event occurs.

The experiment:

if I use this code bellow does is correct?

Code: Select all

if EventWindow()=main_window
end
Endif
The EventWindow() , returns the window handle of the current window event , so the main_window is the main application window so if I put the above code , just the program will ends.

Re: Understanding how the events works

Posted: Sun Jun 24, 2012 4:36 pm
by takis76
If I will extend the experiment I will try this code:

Code: Select all

    If EventWindow()=main_window
      If EventGadget()=program_menu
        Debug EventGadget()
        End
      EndIf  
    EndIf
Suppose the EventGadget returns the ID of the gadget in my case is my program_menu the menu adventure.
So if just select the adventure menu and just trigger it the program must draw me the debug and ends.
Not worked so the EventGadget() not returns the program_menu as gadget.

So as I understand the EventGadget() not have relation to any menu gadget so the EventGadget not sees the menu events only the EventMenu(). EventGadget is for buttons , listboxes etc..

Re: Understanding how the events works

Posted: Sun Jun 24, 2012 4:46 pm
by netmaestro
A toolbar is a form of menu and it will generate menu events, same as a menu created with CreateMenu(), CreateImageMenu(), etc. These are checked in your main loop with EventMenu(). If you look in the PB help docs under 'Gadget' you will find an overview of the gadgets with a graphically-presented list of all the gadgets. Anything you find there will generate gadget events checkable with EventGadget(). If what you're working with isn't on that list, it isn't a gadget and EventGadget() won't work with it.

Re: Understanding how the events works

Posted: Sun Jun 24, 2012 4:51 pm
by IdeasVacuum
...Not being funny but I suggest you read the PB Help, which includes a simple example:http://www.purebasic.com/documentation/ ... event.html

Re: Understanding how the events works

Posted: Sun Jun 24, 2012 6:01 pm
by BasicallyPure
The 'help' entry for 'WindowEvent()' lists all of the possible event types unlike 'WaitWindowEvent()'.

Here is an example that demonstrates how some events may be handled, including menu events.

Code: Select all

;
#WinNumber = 0  ; assign a unique window number to your window

#Button_A = 0 ; assign a unique gadget number to button 'A'
#Button_B = 1 ; assign a unique gadget number to button 'B'

#MenuNumber = 0 ; assign a unique menu number to your menu

#Menu_Item_A = 0 ; assign a unique menu item number to menu item 'A'
#Menu_Item_B = 1 ; assign a unique menu item number to menu item 'B'
#Menu_Item_C = 2 ; assign a unique menu item number to menu item 'C'
#Menu_Item_D = 3 ; assign a unique menu item number to menu item 'D'

#Timer_X = 0 ; assign a unique timer number to timer 'X'
#Timer_Y = 1 ; assign a unique timer number to timer 'Y'

OpenWindow(#WinNumber, 0, 0, 300, 200, "Window title")
;
; create menu and add menu items
CreateMenu(#MenuNumber, WindowID(#WinNumber))
MenuTitle("Menu 1")
   MenuItem(#Menu_Item_A, "Item_A")
   MenuItem(#Menu_Item_B, "Item_B")
MenuTitle("Menu 2")
   MenuItem(#Menu_Item_C, "Item_C")
   MenuItem(#Menu_Item_D, "Item_D")
   
; create gadgets
ButtonGadget(#Button_A, 20, 20, 50, 25, "A")
ButtonGadget(#Button_B, 20, 60, 50, 25, "B")

AddWindowTimer(#WinNumber, #Timer_X, 4545)
AddWindowTimer(#WinNumber, #Timer_Y, 5454)

Repeat ; event loop
   Event = WaitWindowEvent() ; detect all window events
   Select Event ; what type of event was it?
      Case #PB_Event_Gadget ; the event was from a gadget
         Select EventGadget() ; which gadget produced the event?
            Case #Button_A
               Debug "Button A generated an event"
            Case #Button_B
               Debug "Button B generated an event"
         EndSelect
         
      Case #PB_Event_Menu ; the event was from a menu item
         Select EventMenu() ; which menu item produced the event?
            Case #Menu_Item_A
               Debug "Menu Item_A generated an event"
            Case #Menu_Item_B
               Debug "Menu Item_B generated an event"
            Case #Menu_Item_C
               Debug "Menu Item_C generated an event"
            Case #Menu_Item_D
               Debug "Menu Item_D generated an event"
         EndSelect
         
      Case #PB_Event_Timer ; the event was from a window timer
         Select EventTimer() ; which timer generated the event?
            Case #Timer_X
               Debug "Timer 'X' generated an event"
            Case #Timer_Y
               Debug "Timer 'Y' generated an event"
         EndSelect
         
      Case #PB_Event_CloseWindow
         Break ;exit the loop
   EndSelect
ForEver

End

Re: Understanding how the events works

Posted: Sun Jun 24, 2012 7:00 pm
by takis76
Nice I created one enumeration for all menu and toolbar kind gadgets with names

Like this:

Code: Select all

Enumeration
  
  #Null_Object
  
  #New_Adventure_Menu
  #Load_Adventure_Menu
  #Save_Adventure_Menu
  #Adventure_Properties_Menu
  #Quit_Editor_Menu
  
  #Wall_Tool
  #Door_Tool
  #Wall_Decoration_Tool
  #Fake_Wall_Tool
  #Secret_Wall_Tool
  #Stairs_UP_Tool
  #Stairs_DOWN_Tool
  #Keylock_Tool
  #Switch_Tool
  #Alcove_Tool
  #Pit_Tool
  
EndEnumeration
And now I have all events for all of these menu like gadgets captured with this code:

Code: Select all

    If Event = #PB_Event_Menu 
      
      MenuItem = EventMenu()
      ;I have selected the quit adventure from menu
      If MenuItem=#Quit_Editor_Menu
        End
      EndIf
      
    EndIf

Re: Understanding how the events works

Posted: Sun Jun 24, 2012 7:08 pm
by VB6_to_PBx
when i run your program code above and click on a Menu choice
or one of the Buttons, it displays results in Debug window only once
then you are forced to quit program and start it up again if you want to checkout another
Menu choice or Button choice click's Debug results.

What source code can i use to make the Debug window keep popping up with results
everytime i click on a Button or Menu choice
"without" having to quit or end program and restart each time ????

i want to be able to constantly click on the Button and each time see the Debug results popup
all the while the program continues to Run.

need source code to do that .
i haven't seen an example of this yet in Forum posts , i guess its not possible ???

Re: Understanding how the events works

Posted: Sun Jun 24, 2012 8:03 pm
by takis76
if you want to see the results of the debug window always just put the debug code in the main loop just under the
MenuItem = EventMenu()

You will see the debug window and the results always , in fact you will have a flood of events in the debug window.

Code: Select all

debug MenuItem ;<----- Put the debug command here

If Event = #PB_Event_Menu
     
      MenuItem = EventMenu()
      debug MenuItem ;<----- Or Put the debug command here
      ;I have selected the quit adventure from menu
      If MenuItem=#Quit_Editor_Menu
        End
      EndIf
     
EndIf

Re: Understanding how the events works

Posted: Sun Jun 24, 2012 8:16 pm
by takis76
If you are speaking about the Buttons not toolbars and menus , so far I don't found how to capture the button events.
And now I would ask about the button events , the menu events are ok.

This kind of code is not worked as worked with menus

Code: Select all

Enumeration

  #Null_Object
  ;Button Variables
  #Test_Button1
  #Test_Button2
  
EndEnumeration

  test_button1 = ButtonEx(WindowID(main_window), #Test_Button1, 50,150, 100, 20, "Test Button", null, null, null, null, null)
  test_button2 = ButtonEx(WindowID(main_window), #Test_Button2, 50,170, 100, 20, "Test Button", null, null, null, null, null)

    ;If some gadget even occured
    If Event = #PB_Event_Gadget
      
      GadgetNumber = EventGadget()
      If GadgetNumber=#Test_Button1
      
        End
      EndIf
    
    EndIf

The buttons are enumerated as Test_Button1 and Test_Button2
The event if event gadget. And the EventGadget returns the number of the button number.
I don't know if I trigger the correct gadget and I don't know if I need to check if I press the left mouse button or if any mouse click event occur.

Re: Understanding how the events works

Posted: Sun Jun 24, 2012 9:21 pm
by rsts
Have you tried examples from the help?

When you post code, try to post runnable code which demonstrates a problem.

The code you are posting is not complete. Are you expecting it to work as written?

Re: Understanding how the events works

Posted: Sun Jun 24, 2012 10:46 pm
by Zach
You're going about this all wrong I think.


You should be testing against WaitWindowEvent() for everything, unless you have a very specific need, and the knowledge to manually parse the Wnidows Event Queue.
You should also be doing it with SELECT / CASE because it is much easier to read and organize your event loop IMHO.

Here is an example of the Event Loop from the Windows GUI for my game resource editor.
It's not as big as it used to be, because I am in the middle of a redesign and removed a lot of deprecated code. But in general it gives you the idea of how to check for different Event Types (Menu, Gadget, etc)

The PB help file, when read thoroughly should also give you the same kind of idea. I'm pretty sure that's how I arrived at my design.

Code: Select all

Define Event                          ;// Variable to hold an Event Data.
Define QuitProgram = 0                ;// BOOL, while 0 Program is running.
;//Includes
XIncludeFile  ("Win_GUI_Header.pbi")  ;// Header File contains Procedure definitions and other data
                                      ;// for specific target platform's GUI.


  GUI_Open_Main_Window()   ;// This is just calling the main function which draws my GUI so its not relevant to this code
Repeat  
  Event = WaitWindowEvent()
  
  Select Event
      
    Case #PB_Event_Menu
      Select EventMenu()
          
        Case  #FMENU_ROOM_MANAGER
          ShowPanelExPage(PanelMaster, #ROOM_MANAGER)
          
        Case #FMENU_EXIT
          HideWindow(#MAIN_WINDOW, #True)   ;// Hide the Program Window on Termination so the user doesn't see the gadgets being destroyed.
          QuitProgram = 1
      EndSelect
      
      
    Case #PB_Event_CloseWindow
      HideWindow(#MAIN_WINDOW, #True)       ;// Hide the Program Window on Termination so the user doesn't see the gadgets being destroyed.
      QuitProgram = 1
      
  EndSelect
  
Until QuitProgram = 1                       ;// When QuitProgram = 1 the main execution loop ends & the Program is terminated.
Also important to note when working with ProGUI. When you are casting any Window/Gadget/Object etc into a named variable ( MyButton = ButtonEx() etc..) that variable needs to be global so that it can be accessed in your event loop. Unless you structure your code in such a way that it isn't needed, but in general I have found I need to make certain things Global. So as a good practice I give all GUI objects that trigger events, global variable names. You don't have to worry about this with Constants or Enumerated Numbers, but you do have to make sure for variables.

Re: Understanding how the events works

Posted: Sun Jun 24, 2012 11:09 pm
by takis76
I have uploaded what I have created so far.

https://www.box.com/s/1ef699486154dd82ad68

I found some way to capture the buttons , menus and toolbox events.


Now I am looking how to take the state of the toolbar buttons.

I tried GetMenuExItemState(#TOOLBAR_0, #Wall_Tool) and doesn't seems to work.

You will see in the code the #TOOLBAR_0 is the menu and as you said , all toolbars acts as menus and the #Wall_Tool is the index item of the button from toolbar.

But is not working.

Also there is not any command with name GetToolBarExItemState() this command will be very useful.

Re: Understanding how the events works

Posted: Sun Jun 24, 2012 11:23 pm
by Zach
You should consult the ProGUI help for a list of all commands that ProGUI supports, as well as how to use its existing commands.

It will also help if you post ProGUI related questions in the ProGUI thread in the Announcements section of the folder.
Remember ProGUI is an external Library, and not all users have purchased a license for it so they do not all have it.
So posting code with ProGUI commands may not get you any help in the main forums or may confuse members who do not use ProGUI.


I suggest you go to the ProGUI thread and post the code for the program you are offering for download, and you might get better help with why it is not working..


But your comment about wanting to query the state of the Toolbar is a little vague so I'm not really sure what you want to do with it. Usually you shouldn't need to query the State of a Toolbar with buttons, as I think states are defined beforehand.

So once more I will suggest you read the ProGUI help file (it comes as PDF or .CHM) and ALSO you download the ProGUI examples from the ProGUI site because they show you how a lot of these things work. It is included with all ProGUI releases, so it should be in the directory you extracted your ProGUI ZIP/RAR archive to. Or if you still have the ZIP/RAR download the help files will be in there.

Here is a link to the ProGUI thread in Announcement
http://www.purebasic.fr/english/viewtop ... 14&t=36686

Here are the ProGUI Examples that you can find on the ProGUI site
http://www.progui.co.uk/downloads/ProGUI_Examples.zip

edit:
I just want to add one more thing. When you post code snippets because something doesn't work, and you don't understand why. You should explain what you are trying to accomplish, and what your are trying to do with your code. It is usually best to post more than a small section of the code - because that doesn't help us see what you are trying to do. It is best to post the whole program when possible, especially if it is not big because you are just doing a test to figure things out. If we have the whole picture of what you are drawing in your window, and your entire event loop. Then we can help you better. Because then we won't have to write out own code to open our own window and try to figure out what you were doing with the piece of code you posted. Make sense?

It is easier to fix a broken program when you have the whole program. :)

Re: Understanding how the events works

Posted: Mon Jun 25, 2012 12:21 am
by takis76
I have uploaded what I have created so far , as I saw , and I have already download all example from the first day I used this ProGui library and I am reading and the help manual , but there are a lots of things that I don't understand even with the help , because there are a lots of fundumental things that I don't know.
I am trying ProGui , because the native PureBasic windows and gadget commands are very complex and needs a lots of APi.

As I saw in the help files is impossible to take the state of the toolbar buttons , only ToggleButtonEx function support this , but I made it elsewere , the updated version is uploaded , I use structure (Type) to keep the states of each toolbar buttons and I use
ProGUI_EventCallback(hwnd, message, wParam, lParam) function to capture at least the menus , toolbars and button gadgets for now.

The examples are not very helful yet , because are complex and I don't know where to search. Most of the examples presents how to create new gadgets , but not how to capture the events from these gadgets here I am not asking for ProGui material only , I am asking and for PureBasic native gadgets material.

For example I would like to know how to take the x and y position of the mouse on canvas gadget.

I created one canvas and I manage to put and some image on the canvas. (I read the help file).
The ProGUI thread in Announcement , is only about what new versions of ProGui released , asking in the Announcement thread for proGui coding questions is out of place. Is there a specific forum about ProGui only coding for beginners?