Page 1 of 1
PureBasic: Sequential or Object-Oriented?
Posted: Wed Mar 15, 2006 10:41 pm
by paleozord
I'm completely new to PureBasic, and I'm just trying to figure out exactly how it works. It seems to me from my brief beginner's foray into it, that unlike Visual Basic where the visual interface drives the program and bits of code are only executed when some sort of object-oriented event occurs, with PB everything is actually generated from code which is executed sequentially, like the old-style console-type QBasics did. Is that right?
In visual basic each "gadget" on the window has its own set of subs that get called automatically depending on what type of interaction occurs with them, but in PB this doesn't seem to be the case. When opening a Window, one has to use some sort of Repeat-Until type loop in order to keep it open and catch any actions taken by the user. Without that loop (which is not used in VB) the Window will just close itself as soon as it opens. So everything has to be executed through nested If..Endif statements to catch all possible types of user action. Am I correct?
I apologize if my understanding of it is way off; as I said I'm completely new to PureBasic, though I've worked with VB extensively. So I'm just trying to adjust to the differences.
If my understanding is correct, how does one set up a visual interface that can respond to the user using any of the numerous gadgets that might be present on the main window? Nested and re-nested IF blocks? If you're treating a Window basically like a Console, I'm not sure how you are able to catch all the various different kinds of events that happen on a Window with gadgets.
One other quick question. If I use the Visual Designer to create a simple start-up window, and select RUN from the project menu, it runs fine, and the Window appears and stays there. But if I then use Generate Code, and try to run it from the Code Editor, the window disappears as soon as it appears. Why does the program not behave identically from the Code Editor and the Visual Designer?
Also, once you have designed a form visually and have switched over to editing code for it, is there no way to return to visual design to add or change things, while keeping your code intact? The visual designer and code editor do not seem to be intrinsically linked together to reflect changes on either side, the way they are in VB.
Again apologies for my confusion, I'm just a total newcomer and trying to pick everything up.
Posted: Wed Mar 15, 2006 11:31 pm
by Killswitch
In answer to your VD question just stick:
Code: Select all
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
...at the end of your code.
To properly catch window events you do have to use a loop, but it's not that complicated (and some are handled for you i.e. minimise/maximise).
Code: Select all
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
End
Case #PB_Event_Gadget
Case #PB_Event_Menu
EndSelect
Forever
That's a really basic example of a window control loop - you just fill in the relevant areas with the code you want to excecute on receipt of a window event.
Posted: Wed Mar 15, 2006 11:47 pm
by srod
Hi there,
I'm completely new to PureBasic, and I'm just trying to figure out exactly how it works. It seems to me from my brief beginner's foray into it, that unlike Visual Basic where the visual interface drives the program and bits of code are only executed when some sort of object-oriented event occurs, with PB everything is actually generated from code which is executed sequentially, like the old-style console-type QBasics did. Is that right?
Yes, PB is a procedural language and as such code is executed in a 'top down' fashion.
In visual basic each "gadget" on the window has its own set of subs that get called automatically depending on what type of interaction occurs with them, but in PB this doesn't seem to be the case. When opening a Window, one has to use some sort of Repeat-Until type loop in order to keep it open and catch any actions taken by the user. Without that loop (which is not used in VB) the Window will just close itself as soon as it opens. So everything has to be executed through nested If..Endif statements to catch all possible types of user action. Am I correct?
The loop your refer to is termed an 'event loop' and every Windows program will have one (though windows terms it a 'message loop'). A windows program enters a message loop and continually checks whether messages have been sent to it by Windows to give notification of some required action or other. For example, everytime the user moves the cursor, Windows will place a message within the application's message queue to inform the program that the cursor has moved etc. The program retrieves this message and decides whether any action is required. This loop continues until notification is received that the program is to close etc.
In Visual Basic, the message loop is hidden from the developer by the compiler, but it nevertheless sits in the background quite merrily examining messages as they arrive and calling the various subroutines declared as 'event procedures' by the developer. In Purebasic, on the other hand, we have to take a little responsibility for processing windows messages (though only a little!) but we do have the option of interfacing directly with Windows and picking up the kind of messages which the VB programmer would find very difficult to trap.
If my understanding is correct, how does one set up a visual interface that can respond to the user using any of the numerous gadgets that might be present on the main window? Nested and re-nested IF blocks? If you're treating a Window basically like a Console, I'm not sure how you are able to catch all the various different kinds of events that happen on a Window with gadgets.
It's really quite simple and best seen in a working program (see demo programs). Most gadget have what I would term a 'default' event which a Purebasic event loop will pick up with no fuss whatsoever. E.g. a button gadget will fire it's 'default event' when the user clicks it etc. Certain other events can be identified by using a few dedicated commands such as EventType(). As I say, you will need to look at a few sample programs to get a feel for the PB way (which is simply the Windows way!) and you will pick it up very quickly.
In a way, I miss the VB way of handling events, but it is not overly difficult to mimick this behaviour in PB.
Posted: Wed Mar 15, 2006 11:55 pm
by netmaestro
srod's response is pretty much on the money. The advantage to doing your own message-handling as opposed to having it done for you is that your code is smaller and faster, not to mention the fact that you have a much better "feel" for what is going on. When I programmed in VB and Delphi I felt more like an end user than a programmer, what with all that dragging and dropping and so much happening behind the scenes that I was "protected" from. PureBasic is so much more fun than these languages! It really is worth the reasonably short learning curve.
Posted: Wed Mar 15, 2006 11:57 pm
by paleozord
Thank you for your reply. I'm glad to see that I wasn't so way off in my interpretation of things, and your example is helpful. Basically you're using Select Case where I said nested IF..ENDIF blocks, but more or less the same thing. I will try to familiarize myself with it some more..
How about the notion of being able to go back and forth between Code Editor and Visual Designer, making changes in one and having them reflected in the other? Can that be done?
Re: PureBasic: Sequential or Object-Oriented?
Posted: Thu Mar 16, 2006 12:24 am
by Kaisen2100
paleozord wrote:When opening a Window, one has to use some sort of Repeat-Until type loop in order to keep it open and catch any actions taken by the user. Without that loop (which is not used in VB) the Window will just close itself as soon as it opens. So everything has to be executed through nested If..Endif statements to catch all possible types of user action. Am I correct?
...
One other quick question. If I use the Visual Designer to create a simple start-up window, and select RUN from the project menu, it runs fine, and the Window appears and stays there. But if I then use Generate Code, and try to run it from the Code Editor, the window disappears as soon as it appears. Why does the program not behave identically from the Code Editor and the Visual Designer?
Yes you are right ... and that loop can be created in an automatic way if you go to the menu (in the Visual Designer) "Project-->Project Options..." and check the option "Include event loop" ... that option will write (when you click "Project-->PureBasic editor" or save the code) everithing you need to catch all the events. After that you just need to write the code to handle the event just after each "If GadgetID = #SomeGadget" , and delete any event that you don't want to use. It is simple
Also, once you have designed a form visually and have switched over to editing code for it, is there no way to return to visual design to add or change things, while keeping your code intact? The visual designer and code editor do not seem to be intrinsically linked together to reflect changes on either side, the way they are in VB.
You can use an include file for the GUI and other for the CODE ... in the main file you put a line (must be the first line) that says "IncludeFile 'MyGUI_IncluedeFile.pb". You can create a GUI and save it in the "MyGUI_IncludeFIle.pb" and the other is used only and ONLY FOR CODING.
What i say here is only my opinion and many other user of PureBasic can think different ... but all of them can help you if you need. The PureBasic's Comunity is very big and will help you always ... there are 2183 users that can help you in any way.
The begining is a little confusing but after learning how to program in PureBasic you will see that it's so easy ... and with 2183 users (and counting) to help you ... it's more easy
Note: Sorry for my bad english

Posted: Thu Mar 16, 2006 6:17 am
by paleozord
I really appreciate all the replies, they have been quite helpful. I actually find it an intriguing idea to have a full GUI application that's in fact programmed in a top-down sequentially executed style. I used QBasic for years before taking up VB, so I'm familiar with and enjoy the line-by-line structure of coding; I'd just never seen it applied to a fully graphic interface before. Very nice!
A follow-up question though: How is it possible to have the GUI in a seperate include file from the main code, when it seems that the main program code must be placed inside the main loop, which must be inside the block of code which opens the main window? Is it possible to open the window and create the gadgets in one file, yet have the main program loop in another file? I guess I will give that a try and find out. As I say, I am coming from VB where the GUI gadgets and code are intrinsically linked back and forth with each other, so just adjusting
I agree with what was said about VB feeling more like a paint-by-numbers programing wizard than an actual language. Most of it is already done for you and you just put it together in the shape and form that you want. More like an EZ-programming kit than a raw pure language. Still some good points about that approach depending on what you're trying to do, but definitely not as floor-level as PB or other true-coding languages.
By the way, I presume that one can do all GUI-related tasks without making any OS-specific API calls, so that programs will work on all supported platforms without the need to make any changes to code?
Posted: Thu Mar 16, 2006 7:03 am
by fsw
paleozord wrote:By the way, I presume that one can do all GUI-related tasks without making any OS-specific API calls, so that programs will work on all supported platforms without the need to make any changes to code?
Correct.
As long your needs don't exceed the GUI-Framework provided by native PureBasic commands.
But if you look at the help file, there are quite a few...
Posted: Thu Mar 16, 2006 9:01 am
by blueznl
for a small framework, which you've probably figured out by now yourself:
http://www.purebasic.fr/english/viewtopic.php?t=18424
Posted: Thu Mar 16, 2006 8:56 pm
by utopiomania
I actually find it an intriguing idea to have a full GUI application that's in fact programmed in a
top-down sequenially executed style.
It isn't exactly executed sequentially, it is event-driven, just like any other Windows programming language out
there.
The event handler referred to here catches events from your GUI (or from Windows) and let's you execute your own
code (a procedure for example) in response to them.
The main difference from VB is that VB lets you tag procedures directly to GUI elements events, like Button1_Click(),
while in PureBasic you detect that event in your eventhandler, which then calls your procedure.
It's very easy once you get the hang of it.
Edit: Here's a sample:
Code: Select all
;Enumeration to set gadget Id's
Enumeration
#Win1 ;=0
#Btn1 ;=1
EndEnumeration
;Procedure declarations
Declare OpenMainWindow()
Declare Button1_Click()
;Start program execution by opening a window with a button on it
OpenMainWindow()
;Then loop while looking for events to react to. This an event handler
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadgetID()
Case #Btn1
Button1_Click()
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
;Dropped down here from the eventhandler above, end the program
End
;Procedures, not executed unless called from somewhere
Procedure OpenMainWindow()
Flags = #PB_Window_ScreenCentered | #PB_Window_Systemmenu
If OpenWindow(#Win1, 0, 0, 300, 200, Flags, "Program Template")
;Create a new gadget list for the current window
If CreateGadgetList(WindowID())
;Add a button
ButtonGadget(#Btn1, 100, 160, 80, 20, "Click Me")
EndIf
EndIf
EndProcedure
Procedure Button1_Click()
MessageRequester("", "Button1 Clicked!")
EndProcedure
Posted: Thu Mar 16, 2006 11:44 pm
by jroad
This topic and examples has been very helpful to me as I am also new to PB. I think there have been some changes to OpenWindow() and WindowID() so I updated the last example to get it to compile in PB4b7:
Code: Select all
;Enumeration to set gadget Id's
Enumeration
#Win1 ;=0
#Btn1 ;=1
EndEnumeration
;Procedure declarations
Declare OpenMainWindow()
Declare Button1_Click()
;Start program execution by opening a window with a button on it
OpenMainWindow()
;Then loop while looking for events to react to. This an event handler
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case #Btn1
Button1_Click()
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
;Dropped down here from the eventhandler above, end the program
End
;Procedures, not executed unless called from somewhere
Procedure OpenMainWindow()
flags = #PB_Window_ScreenCentered | #PB_Window_SystemMenu
If OpenWindow(#Win1, 0, 0, 300, 200, "Program Template",flags)
;Create a new gadget list for the current window
If CreateGadgetList(WindowID(#Win1))
;Add a button
ButtonGadget(#Btn1, 100, 160, 80, 20, "Click Me")
EndIf
EndIf
EndProcedure
Procedure Button1_Click()
MessageRequester("", "Button1 Clicked!")
EndProcedure
Regards