PureBasic Docs- Ideas/Help needed for a "We start" chapter!?

Everything else that doesn't fall into one of the other PB categories.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: PureBasic Docs- Ideas/Help needed for a "We start" chapt

Post by collectordave »

Hi
Firstly thanks to the three newbies that contacted me by email, and sorry yes I did not post the final code for the end of the chapter, so here it is.

Just a line to clarify. I feel that most newbies start to look at a programming language to support their hobby mostly looking to produce a database of some kind to keep track of their collections or other data related to their hobby. So I wrote a small tutorial to help with getting started. The final code, built through the tutorial, shows multiple windows with multiple databases the usage of modules and a few other bits with two simple databases one for fruits and one for vegetables. The we start chapter I feel would benefit from an approach like this.

Here is the code:-

First the main form.

Code: Select all

EnableExplicit

IncludeFile "Fruit.pb"
IncludeFile "Vegetable.pb"

;Window Variables
Global MainWindow,FruitWindow.i,VegeWindow.i

;Main Window Gadgets
Global btnFruit.i,btnVegetable.i

Define Event.i

Procedure.i IsValidPBEvent(Event)

  Select event
    Case  #PB_Event_Menu,            ; a menu has been selected
          #PB_Event_Gadget ,         ; a gadget has been pushed
          #PB_Event_SysTray,         ; an icon in the systray zone was clicked
          #PB_Event_Timer ,          ; a timer has reached its timeout
          #PB_Event_CloseWindow,     ; the window close gadget has been pushed
          #PB_Event_Repaint,         ; the window content has been destroyed
          #PB_Event_SizeWindow,      ; the window has been resized
          #PB_Event_MoveWindow,      ; the window has been moved
          #PB_Event_MinimizeWindow,  ; the window has been minimized
          #PB_Event_MaximizeWindow,  ; the window has been maximized
          #PB_Event_RestoreWindow,   ; the window has been restored To normal size
          #PB_Event_ActivateWindow,  ; the window has been activated (got the focus)
          #PB_Event_DeactivateWindow,; the window has been deactivated (lost the focus)
          #PB_Event_WindowDrop,      ; a Drag & Drop operation was finished on a window
          #PB_Event_GadgetDrop,      ; a Drag & Drop operation was finished on a gadget 
          #PB_Event_RightClick,      ; a right mouse button click has occurred on the window.
          #PB_Event_LeftClick,       ; a left mouse button click has occurred on the window
          #PB_Event_LeftDoubleClick  ; a left mouse button double-click has occurred on the window

      ProcedureReturn #True
            
    Default
      
      ProcedureReturn #False
              
  EndSelect
            
EndProcedure

Procedure Event_Handler(Event)
  
  Select event
      
    Case #PB_Event_CloseWindow
      End
      
    Case #PB_Event_Gadget
      
      Select EventGadget()
          
        Case btnFruit
          
          If Not IsWindow(FruitWindow)
            ;Show the Fruit window
            FruitWindow = Fruit::Open()
          Else
            ;Make Fruit Window Active
            SetActiveWindow(FruitWindow)
          EndIf
          
        Case btnVegetable
          
          If Not IsWindow(VegeWindow)
            ;Show the Vegetable window
            VegeWindow = Vegetable::Open()
          Else
            ;Make Vegetable Window Active
            SetActiveWindow(VegeWindow)
          EndIf
          
      EndSelect ;EventGadget()
      
  EndSelect ;Event
 
EndProcedure

MainWindow = OpenWindow(#PB_Any, 0, 0, 290, 90, "Fruit & Veg", #PB_Window_SystemMenu)
btnFruit = ButtonGadget(#PB_Any, 50, 20, 70, 40, "Fruits")
btnVegetable = ButtonGadget(#PB_Any, 170, 20, 70, 40, "Vegetables")

Repeat
  
  Event = WaitWindowEvent()
  
  If IsValidPBEvent(Event)
    
    Select EventWindow()
        
      Case MainWindow

        Event_Handler(Event)
        
      Case FruitWindow
        
        Fruit::Event_Handler(Event)
        
      Case VegeWindow
        
        Vegetable::Event_Handler(Event)  
        
    EndSelect ;EventWindow()
    
  EndIf
  
ForEver
Now the fruits module

Code: Select all

DeclareModule Fruit
  
  Declare.i Open()
  Declare Event_Handler(Event)
  
EndDeclareModule

Module Fruit
  
  UseSQLiteDatabase()
  
  ;This window variable
  Global ThisWindow.i
  
  ;This windows gadgets
  Global btnSearch.i,btnExit.i,cmbSearch.i,strSearch.i
  
  ;Database Variable for this module only
  Global DBID.i
  
Procedure LoadCombo()
     
  Define Criteria.s
  
  Criteria = "SELECT * FROM Items; "
  If DatabaseQuery(DBID, Criteria)
    FirstDatabaseRow(DBID)
    AddGadgetItem(cmbSearch, -1, GetDatabaseString(DBID,0))
    While NextDatabaseRow(DBID)
      AddGadgetItem(cmbSearch, -1, GetDatabaseString(DBID,0))
    Wend    
  EndIf
  FinishDatabaseQuery(DBID)

 EndProcedure
 
Procedure SearchData(Name.s)
  
  Define Criteria.s
  
  Criteria.s = "SELECT * FROM Items WHERE Name = '" + Name + "';"
   If DatabaseQuery(DBID, Criteria)
     If FirstDatabaseRow(DBID) = 0
       MessageRequester("Error","No Fruits of type " + Name + " Exist in this database")
     Else
       MessageRequester("Fruits","Fruit " + Name + " Found in this database")
     EndIf
   EndIf
 
   FinishDatabaseQuery(DBID)
    
 EndProcedure   
 
Procedure.i Open()
  
  ThisWindow = OpenWindow(#PB_Any, 10, 10, 320, 80, "Fruits", #PB_Window_Tool)
  cmbSearch = ComboBoxGadget(#PB_Any, 10, 10, 150, 20)
  strSearch = StringGadget(#PB_Any, 180, 10, 120, 20, "") 
  btnSearch = ButtonGadget(#PB_Any, 170, 40, 130, 20, "Search")
  btnExit = ButtonGadget(#PB_Any, 10, 40, 150, 20, "Exit")
  
  ;Open Fruits Database
  DBID = OpenDatabase(#PB_Any,GetCurrentDirectory() + "Fruits.db", "", "") 
  
  LoadCombo()
  
  ProcedureReturn ThisWindow
  
EndProcedure

Procedure Event_Handler(Event.i)
  
  Select event
          
    Case #PB_Event_Gadget
          
      Select EventGadget()
            
        Case btnExit
          CloseWindow(ThisWindow)
            
        Case btnSearch
          SearchData(GetGadgetText(strSearch))
            
        Case cmbSearch
          SearchData(GetGadgetText(cmbSearch))
            
      EndSelect ;EventGadget()
      
  EndSelect ;event
    
EndProcedure 

EndModule
Finally the vegetable module. An almost exact copy of the fruits module.

Code: Select all

DeclareModule Vegetable
  
  Declare.i Open()
  Declare Event_Handler(Event)
  
EndDeclareModule

Module Vegetable
  
  UseSQLiteDatabase()
  
  ;This window variable
  Global ThisWindow.i
  
  ;This windows gadgets
  Global btnSearch.i,btnExit.i,cmbSearch.i,strSearch.i
  
  ;Database Variable for this module only
  Global DBID.i
  
Procedure LoadCombo()
  
  Define Criteria.s
  
  Criteria = "SELECT * FROM Items; "
  If DatabaseQuery(DBID, Criteria)
    FirstDatabaseRow(DBID)
    AddGadgetItem(cmbSearch, -1, GetDatabaseString(DBID,0))
    While NextDatabaseRow(DBID)
      AddGadgetItem(cmbSearch, -1, GetDatabaseString(DBID,0))
    Wend    
  EndIf
  FinishDatabaseQuery(DBID)

 EndProcedure
 
 Procedure SearchData(Name.s)
   
   Define Criteria.s

   Criteria = "SELECT * FROM Items WHERE Name = '" + Name + "';"
    If DatabaseQuery(DBID, Criteria)
      If FirstDatabaseRow(DBID) = 0
        MessageRequester("Error","No Vegetables of type " + Name + " Exist in this database")
      Else
        MessageRequester("Vegetables","Vegetable " + Name + " Found in this database")
      EndIf
    EndIf
    FinishDatabaseQuery(DBID)
    
 EndProcedure   
 
Procedure.i Open()
  
  ThisWindow = OpenWindow(#PB_Any, 10, 10, 320, 80, "Vegetables", #PB_Window_Tool)
  cmbSearch = ComboBoxGadget(#PB_Any, 10, 10, 150, 20)
  strSearch = StringGadget(#PB_Any, 180, 10, 120, 20, "") 
  btnSearch = ButtonGadget(#PB_Any, 170, 40, 130, 20, "Search")
  btnExit = ButtonGadget(#PB_Any, 10, 40, 150, 20, "Exit")
  
  ;Open Vegetables Database
  DBID = OpenDatabase(#PB_Any,GetCurrentDirectory() + "Vegetables.db", "", "") 
  
  LoadCombo()
  
  ProcedureReturn ThisWindow
  
EndProcedure

Procedure Event_Handler(Event.i)
  
  Select event
          
    Case #PB_Event_Gadget
          
      Select EventGadget()
            
        Case btnExit
          CloseWindow(ThisWindow)
            
        Case btnSearch
          SearchData(GetGadgetText(strSearch))
            
        Case cmbSearch
          SearchData(GetGadgetText(cmbSearch))
            
      EndSelect ;EventGadget()
      
  EndSelect ;event
    
EndProcedure 

Sorry cannot upload the databases here but will try if requested.
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
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: PureBasic Docs- Ideas/Help needed for a "We start" chapt

Post by TI-994A »

It should be noted that the IsValidPBEvent() function, in the above example by collectordave, performs a redundant check of events, and is thus not required. Here are the PureBasic developer's comments on it:

...it's not needed. You have to respond to specific events...

...your posted code is wrong, as you don't check for any specific events...

In a properly structured program, valid events would be processed correctly, while others are ignored and simply allowed to fall through. Such a catch-all validation shouldn't be required.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: PureBasic Docs- Ideas/Help needed for a "We start" chapt

Post by collectordave »

Except it does avoid problems such as reported here when developing a program.

http://www.purebasic.fr/english/viewtop ... ow#p439953
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
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: PureBasic Docs- Ideas/Help needed for a "We start" chapt

Post by TI-994A »

collectordave wrote:Except it does avoid problems such as reported here when developing a program.

http://www.purebasic.fr/english/viewtop ... ow#p439953
Besides a query on an apparent change in function behaviour, I can't seem to find any so-called problems reported:
TI-994A wrote:I noticed a change in behaviour in PureBasic's EventWindow() handler. ... Is this an intended change?
It's so sad that you've gone through all this trouble to rummage through old threads, only to miscomprehend them. :lol:

On the other hand, your IsValidPBEvent() function is still simply unnecessary.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: PureBasic Docs- Ideas/Help needed for a "We start" chapt

Post by collectordave »

It's so sad that you've gone through all this trouble to rummage through old threads
Not sad and no trouble.

Besides a query on an apparent change in function behaviour, I can't seem to find any so-called problems reported:
The code shown is as follows:-

Code: Select all

Repeat
  event = WaitWindowEvent()
  Debug EventWindow()
Until event = #PB_Event_CloseWindow
This clearly shows EventWindow() being used immediatly after WaitwindowEvent() and as other programmers here have commented,
You can't use EventWindow() reliably until you know that it is a valid PB event first.
and of course the programmers of Pure Basic have also said
you have to test for a specific event
So the IsValidPBEvent() procedure checks that a valid PB event has occurred before calling the EventWindow() procedure.
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.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: PureBasic Docs- Ideas/Help needed for a "We start" chapt

Post by Little John »

[u]HeX0R[/u] wrote:The good thing with coding is, anyone can do whatever he wants to.
If collectordave wants to use a totaly useless procedure ... no problem, just do it.

But isn't it strange, that no one of the other few thousand PB-users ever were in need of something like this?
Loop, endless ... see Endless loop.
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: PureBasic Docs- Ideas/Help needed for a "We start" chapt

Post by TI-994A »

collectordave wrote:Not sad and no trouble.
Failed gotchas always are. :lol:
collectordave wrote:This clearly shows EventWindow() being used immediatly after WaitwindowEvent()...
You seem to be under the misperception that this is wrong; because it's not.

Clearly, your poor grasp has misled you. Yet again.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
fromVB
User
User
Posts: 81
Joined: Sun Jul 29, 2012 2:27 am

Re: PureBasic Docs- Ideas/Help needed for a "We start" chapt

Post by fromVB »

@collectordave: Why do you check for valid pb events? I am trying to understand the reason. :?
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: PureBasic Docs- Ideas/Help needed for a "We start" chapt

Post by collectordave »

Hi FromVB you can check out this topic

http://www.purebasic.fr/english/viewtop ... 13&t=63983

But basically I am converting a fairly large VB programme to Pure Basic. This programme has a fair number of separate windows where the display of data on some depends on the user input on others. So I had code which was run after events on one window to update another.

I also noted that I was tending to write a very large Reprat..Until loop for message handling. Plus I was trying to invent, at first a large number of constants to give me unique access to gadgets on various windows. After finding that a pain I started to use variables and the #PB_Any to give me the unique identifiers but again I needed a large number of different variable names. As an example the programme has over fifteen windows with Ok and Cancel buttons.

I then found that PB supports modules. A life saver. I reprogrammed what I had achieved so far in modular format with each window being as self contained as possible within a module. This included the code to open the window and an event handling procedure specific for that window. This ensured that specific events for a window were handled correctly in a specific event handling procedure in the specific windows code on which the specific event happened. All that was left was to ensure that the event message was sent to the specific window on which the specific event happened.

The eventwindow() procedure returns the window ID on which the event happened. So I thought great nice and easy.

Hit a problem, I noticed some strange effects on some windows and in tracing what was happening I asked for help and found that the return value of EventWindow() is only valid after a Valid PB event. Waitwindowevent and windowevent return ALL window events whether valid PB events or not, but these non valid PB events report a wrong window ID when eventwindow is used. This was causing the strange effects I had noticed.

Eventually I was pointed towards checking for valid PB events by a couple of forum users who had experienced similar problems. So the procedure was born. It ensures that EventWindow() works correctly each time and as speed of operation is not critical in my application, if we are just talking milliseconds, I decided to leave it in.

This works allthough sadly some members find this new way a little strange.

Hope this helps.
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.
fromVB
User
User
Posts: 81
Joined: Sun Jul 29, 2012 2:27 am

Re: PureBasic Docs- Ideas/Help needed for a "We start" chapt

Post by fromVB »

collectordave wrote:Hit a problem, I noticed some strange effects on some windows and in tracing what was happening I asked for help and found that the return value of EventWindow() is only valid after a Valid PB event. Waitwindowevent and windowevent return ALL window events whether valid PB events or not, but these non valid PB events report a wrong window ID when eventwindow is used. This was causing the strange effects I had noticed.
Thank you for your answer. :)

I know that we get all the events but what strange effects are you getting?
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: PureBasic Docs- Ideas/Help needed for a "We start" chapt

Post by TI-994A »

fromVB wrote:
collectordave wrote:...I noticed some strange effects on some windows...
...what strange effects are you getting?
He can't answer that; because there's nothing! :lol:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: PureBasic Docs- Ideas/Help needed for a "We start" chapt

Post by collectordave »

Hi FromVB

I had reached the point of converting some database search and display routines. The user opened a search form from the main window which needed to update the display on the main window. I wrote the code for updating the main window after the events for the search form expecting just an ok\Update\cancel button to be clicked or criteria to be entered on the form by the user. However after opening the search form the main form data display started to flicker when I moved the mouse over any form.

This was due to EventWindow() reporting that all the mousemove events on the main window and others as happening on the search form. These duly fell through the searchform as I was not interested in mouse move events but they also fired the search routine on the main form which was expecting the search form event to signal a change by the user.

There are many ways to overcome this effect but I chose to check for valid pb events mainly so that all non valid events are rejected immediatly in the main message loop as I have no interest in non valid events and of course EventWindow() works every time. Plus this check prevents similar effects as I convert more of the VB programme.
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
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: PureBasic Docs- Ideas/Help needed for a "We start" chapt

Post by TI-994A »

collectordave wrote:...the main form data display started to flicker when I moved the mouse over any form.

This was due to EventWindow() reporting that all the mousemove events on the main window and others as happening on the search form. These duly fell through the searchform as I was not interested in mouse move events...

There are many ways to overcome this effect...
If there's flickering on any unintended gadget or window:

i. the event did not fall through

ii. it's the result of poor or faulty programming

iii. it's an error to be rectified; not an effect to be overcome

But more importantly, PureBasic does not support mouse move events on windows. :lol:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: PureBasic Docs- Ideas/Help needed for a "We start" chapt

Post by Little John »

[u]Fred[/u] wrote:Topic cleaned.
It seems to me that it's time again for cleaning this topic :-)
(everything after the above quoted message by Fred should be moved e.g. to "Coding questions").
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: PureBasic Docs- Ideas/Help needed for a "We start" chapt

Post by TI-994A »

Little John wrote:It seems to me that it's time again for cleaning this topic
Or better still, just purge that entire block. It clearly has no place in this sticky. :lol:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Post Reply