Newbie code organization question

Everything else that doesn't fall into one of the other PB categories.
User avatar
kpeters58
Enthusiast
Enthusiast
Posts: 341
Joined: Tue Nov 22, 2011 5:11 pm
Location: Kelowna, BC, Canada

Newbie code organization question

Post by kpeters58 »

Hi all,

just stumbled on PB and snooped around a bit trying to find information on source code organization - I didn't find much (but I also haven't spent a great amount of time looking either)

I am mostly working in Delphi and if I had to write a simple Order Entry System, I'd typically have a main form with a menu linking to Customer, Item, Invoice Maintenance forms etc. The code for each of these forms would be in its own source code files and usually these secondary forms would be dynamically created and then destroyed when the user closed them and returned to the main form.

How do you guys organize your source code for bigger applications?

Thanks for sharing your knowledge,
Kai
PB 5.73 on Windows 10 & OS X High Sierra
Zach
Addict
Addict
Posts: 1675
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: Newbie code organization question

Post by Zach »

I think most people tend to adopt the "whatever works for you" attitude. I posted a similar thread (or two) before, but not much discussion went on about it.

I like to break my code up into separate source files myself, and keep them divided by subsystem for the most part.

i.e One "Main.pb" file for the compile target and linking all the other files together. A file for the GUI components (or multiple files for different OS targets), a file for database related stuff, a file for more general procedures, and so on and so forth.

Kind of depends on the project scale and complexity. I am doing mostly simple things right now, so I have 3 or 4 files I work out of.
User avatar
kpeters58
Enthusiast
Enthusiast
Posts: 341
Joined: Tue Nov 22, 2011 5:11 pm
Location: Kelowna, BC, Canada

Re: Newbie code organization question

Post by kpeters58 »

Thanks for your quick input!

Do you use IncludeFile/XincludeFile to 'stitch' source code units together?

Looked at all supplied code samples that came with the demo download and they are all single file...
Does someone have a sample demonstrating the following:

Start up form with menu items 'A' and 'B' where form A or form B get dynamically created upon selection of the respective menu item and execution returns to the initial main form once the "subform" is dismissed?

Thanks for any pointers,
Kai
PB 5.73 on Windows 10 & OS X High Sierra
User avatar
Kuron
Addict
Addict
Posts: 1626
Joined: Sat Oct 17, 2009 10:51 pm
Location: Pacific Northwest

Re: Newbie code organization question

Post by Kuron »

There are "include" commands. The IDE is also capable of working as a "project-based" IDE. There are also third-party IDEs and Form/GUI editors.

If you are using Delphi, you're obviously not a dumbass. Based on your experience, the best way to come to terms with PB is dive in up to your elbows and get your hands dirty. If you want examples on almost anything you would need to do code-wise, I encourage you to visit the Code Archive which is located here: http://purearea.net/pb/CodeArchiv/CodeArchiv.html There is literally a few hundred examples* there which cover almost everything.

*If you are using the demo version of PB some things may not run for you because of the demo limitations.

Regardless, Welcome to the community!
Best wishes to the PB community. Thank you for the memories. ♥️
User avatar
spikey
Enthusiast
Enthusiast
Posts: 749
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Newbie code organization question

Post by spikey »

I concur with Kuron - download CodeArchiv - there are examples for all sorts of interesting things in there. Some of them need tweaking to run under the most recent versions of PB. In the Windows\MultipleWindows subfolder you will find a couple of ways of achieving what you discuss, but here's one way.

Code: Select all

;{- Enumerations / DataSections
;{ Windows
Enumeration
  #Window_0
  #Window_1
  #Window_2
EndEnumeration
;}
;{ Menu bars
Enumeration
  #Menu_Window_0
EndEnumeration
;}
;{ Menu/Toolbar items
Enumeration
  #Menu_Window_0_Window_1
  #Menu_Window_0_Window_2
EndEnumeration
;}
;{ Gadgets
Enumeration
  #Calendar_0
  #ExplorerList_1
EndEnumeration
;}
Define.l Event, EventWindow, EventGadget, EventType, EventMenu
;}
Procedure OpenWindow_Window_0()
  If OpenWindow(#Window_0, 450, 200, 400, 420, "Window 0", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
    If CreateMenu(#Menu_Window_0, WindowID(#Window_0))
      MenuTitle("Window")
      MenuItem(#Menu_Window_0_Window_1, "Window 1")
      MenuItem(#Menu_Window_0_Window_2, "Window 2")
    EndIf
  EndIf
EndProcedure

Procedure OpenWindow_Window_1()
  If OpenWindow(#Window_1, 488, 274, 400, 400, "Window 1", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar, WindowID(#Window_0))
    CalendarGadget(#Calendar_0, 75, 60, 245, 195)
  EndIf
EndProcedure

Procedure OpenWindow_Window_2()
  If OpenWindow(#Window_2, 526, 340, 400, 400, "Window 2", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar, WindowID(#Window_0))
    ExplorerListGadget(#ExplorerList_1, 20, 10, 310, 330, "c:\")
  EndIf
EndProcedure

OpenWindow_Window_0()

;{- Event loop
Repeat
  Event = WaitWindowEvent()
  Select Event
    ; ///////////////////
    Case #PB_Event_Gadget
      EventGadget = EventGadget()
      EventType = EventType()
      If EventGadget = #Calendar_0
      ElseIf EventGadget = #ExplorerList_1
      EndIf
      
    ; /////////////////
    Case #PB_Event_Menu
      EventMenu = EventMenu()
      If EventMenu = #Menu_Window_0_Window_1
        OpenWindow_Window_1()

      ElseIf EventMenu = #Menu_Window_0_Window_2
        OpenWindow_Window_2()
      EndIf
      
    ; ////////////////////////
    Case #PB_Event_CloseWindow
      EventWindow = EventWindow()
      If EventWindow = #Window_0
        CloseWindow(#Window_0)
        End
        
      ElseIf EventWindow = #Window_1
        CloseWindow(#Window_1)
        
      ElseIf EventWindow = #Window_2
        CloseWindow(#Window_2)
        
      EndIf
  EndSelect
ForEver
;
;}
I usually organise big projects as follows:-
1) One master .pb file which just contains XInclude references to everything else the project needs, usually called something like appThingy.pb. I don't put any executable code in here at all.
2) Seperate .pbi files for each different window/dialog/'class' and one for the event loop too. Although PB isn't intrinsically an OOP language (see other articles on the forum on that topic ad nauseum), I usually seperate procedures into functional units based upon some kind of common project class (storage file, preferences file, business object whatever)
Window files I start frmThingy1.pbi (for form because I came from VB), (maybe wdw for window would be more sensible), Class files I start clsThingy2.pbi etc. The event loop I just call Event.pbi.
3) appThingy.pb includes all the form and class files and then the event loop.
4) I use a PureBasic project file to hang all the files together so I can get to them easily from inside the IDE.
5) I even put change control information into a pbi file and add that to the project so that I can even get to that from withing the IDE - I just don't 'include' it in the source code that is compiled.
User avatar
kpeters58
Enthusiast
Enthusiast
Posts: 341
Joined: Tue Nov 22, 2011 5:11 pm
Location: Kelowna, BC, Canada

Re: Newbie code organization question

Post by kpeters58 »

Thanks for the warm welcome and all the pointers - much appreciated!

Kai
PB 5.73 on Windows 10 & OS X High Sierra
Yogi Yang
Enthusiast
Enthusiast
Posts: 107
Joined: Sun Dec 11, 2005 2:19 pm

Re: Newbie code organization question

Post by Yogi Yang »

kpeters58 wrote:I am mostly working in Delphi and if I had to write a simple Order Entry System, I'd typically have a main form with a menu linking to Customer, Item, Invoice Maintenance forms etc. The code for each of these forms would be in its own source code files and usually these secondary forms would be dynamically created and then destroyed when the user closed them and returned to the main form.
If you have ever used any descent IDE like Delphi then working in PB would be very hard initially. I would suggest you to look up PBDev. It is a very good IDE and it is freeware (now). Do check it: http://www.hellobasic.com/pbdev/download/setup.exe

This will easy your transition to PB from Delphi. ;)
--
Yogi Yang
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Newbie code organization question

Post by IdeasVacuum »

There are a number of tools for coding in PB. I personally use Ultra Edit for my code (I use it for C/CPP, Lua and others as well), then compile via PB's IDE. To define forms, I use gonzal's PureFORM http://gnozal.ucoz.com/. Gonzal is a famous PB Guru and has many excellent PB Libraries.

Yogi, I tried your download for PBDev, but my first thoughts were that it doesn't feel like it was made specifically for PureBasic. There are things that don't belong? Modules? References to PowerBasic? It looks easy to use though, especially the form designer, but ultimately does not get my vote.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Kuron
Addict
Addict
Posts: 1626
Joined: Sat Oct 17, 2009 10:51 pm
Location: Pacific Northwest

Re: Newbie code organization question

Post by Kuron »

IdeasVacuum wrote:Yogi, I tried your download for PBDev, but my first thoughts were that it doesn't feel like it was made specifically for PureBasic. There are things that don't belong? Modules? References to PowerBasic? It looks easy to use though, especially the form designer, but ultimately does not get my vote.
It is also worth pointing out that you can no longer register it and the free version can only be used for free projects. It is also unsupported.
Best wishes to the PB community. Thank you for the memories. ♥️
Yogi Yang
Enthusiast
Enthusiast
Posts: 107
Joined: Sun Dec 11, 2005 2:19 pm

Re: Newbie code organization question

Post by Yogi Yang »

IdeasVacuum wrote:Yogi, I tried your download for PBDev, but my first thoughts were that it doesn't feel like it was made specifically for PureBasic. There are things that don't belong? Modules? References to PowerBasic? It looks easy to use though, especially the form designer, but ultimately does not get my vote.
I agree to that, but then this is the only IDE that has the convenience of placing a control visually on a form and then double clicking it to open its click event and coding in it. I have till date not come across any other tool that allow this for PureBasic programming.

Another thing is that it does add a few bytes to the exe so a project with only one single form will be bigger then what will be generated but hand coding. But one can live with it.
--
Yogi Yang
Post Reply