Add includes while modifying minimum of code

Just starting out? Need help? Post your questions and find answers here.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5611
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Add includes while modifying minimum of code

Post by Kwai chang caine »

Hello at all,

For my project, after use the method "embeded addons.exe", i search another method for adding include while modifying minimum of code in the main windows but also in the include too (The more simple way)
With this method, each include can be compiled and working alone, and also adding before compiling in the mother application like an ADDON

I'd like to get your opinion before I start making all the changes, and also to know if you know of anything simpler.
I have take a look to the PB IDE code of FRED, and i see, he use different constants and functions for each addon, at my advice it's the more simple way, because i have understand :mrgreen: :lol:
But perhaps another method can be automatic, just adding the line "IncludeFile MyAddon.pbi" and the mother code can include all the events of the addon :idea:
I have try with the MACROS, but it's a little bit too complex for KCC :oops:

The mother code

Code: Select all

Enumeration
 #FormMain
 #ButtonMain
EndEnumeration

Structure Include
 EvenementsWin.i
 EvenementsGad.i
EndStructure
Global NewMap MapInclude.Include()

IncludeFile "Include_1.pbi"

Procedure Main_GadgetsEvents(EvenementGadget)
  
 Select EvenementGadget
  
  Case #ButtonMain
   
   MessageRequester("", "Le bouton dans la fenetre principale a été cliqué")
 
 EndSelect 
 
EndProcedure

Procedure Main_WindowEvents(Evenement)
 
 Fenetre = EventWindow()
     
 Select Evenement
 
  Case #PB_Event_MoveWindow
  
   Debug WindowX(#FormMain)
   Debug WindowY(#FormMain)
  
  Case #PB_Event_LeftDoubleClick
  
   Debug "Double clic sur la fenetre principale"
     
 EndSelect
 
EndProcedure

OpenWindow(#FormMain, 150, 150, 250, 250, "Fenetre principale", #PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
ButtonGadget(#ButtonMain, 50, 50, 160, 40, "Boutton principal")

Repeat
 
 Evenement = WaitWindowEvent()
 EvenementGadget = EventGadget()
 IdFenetre = EventWindow()
 
 Select Evenement
 
  Case #PB_Event_Gadget
   
   If IdFenetre = #FormMain
   
    Main_GadgetsEvents(EvenementGadget)
   
   Else
    
    ForEach MapInclude()
     CallFunctionFast(MapInclude()\EvenementsGad, EvenementGadget)
    Next    
       
   EndIf
   
  Case  #PB_Event_CloseWindow
  
   If IdFenetre = #FormMain
   
    End
   
   Else
    
    CloseWindow(IdFenetre)
    
   EndIf
   
  Default
   
   If IdFenetre = #FormMain
   
    Main_WindowEvents(Evenement)
   
   Else
    
    ForEach MapInclude()
     CallFunctionFast(MapInclude()\EvenementsWin, Evenement)
    Next
    
   EndIf
   
 EndSelect  
  
ForEver
The include code

Code: Select all

Enumeration #PB_Compiler_EnumerationValue
 #FormInclude1
 #ButtonInclude1
EndEnumeration

Procedure Include1_WindowEvents(Evenement)
 
 Fenetre = EventWindow()
  
 Select Evenement
  
  Case #PB_Event_MoveWindow
  
   Debug WindowX(#FormInclude1)
   Debug WindowY(#FormInclude1)
  
  Case #PB_Event_LeftDoubleClick
  
   Debug "Double clic sur la fenetre Include1"
     
 EndSelect
 
EndProcedure

Procedure Include1_GadgetsEvents(EvenementGadget)
  
 Select EvenementGadget
  
  Case #ButtonInclude1
   
   MessageRequester("", "Le bouton dans la fenetre Include1 a été cliqué")
 
 EndSelect 
 
EndProcedure

OpenWindow(#FormInclude1, 150, 150, 250, 250, "Include1", #PB_Window_SizeGadget|#PB_Window_MinimizeGadget)
ButtonGadget(#ButtonInclude1, 50, 50, 160, 40, "Boutton Include1")

CompilerIf #PB_Compiler_IsMainFile
 
 Repeat
 
  Evenement = WaitWindowEvent()
  EvenementGadget = EventGadget()
  
  Select Evenement
  
   Case #PB_Event_Gadget
    
    Include1_GadgetsEvents(EvenementGadget)
    
   Default
    
    Include1_WindowEvents(Evenement)
    
  EndSelect  
   
 Until Evenement = #PB_Event_CloseWindow
 
CompilerElse
 
 MapInclude("Include1")\EvenementsWin = @Include1_WindowEvents()
 MapInclude("Include1")\EvenementsGad = @Include1_GadgetsEvents()

CompilerEndIf
Have a good day
ImageThe happiness is a road...
Not a destination

PureBasic French Forum
User avatar
ChrisR
Addict
Addict
Posts: 1538
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Add includes while modifying minimum of code

Post by ChrisR »

Looks good and functional :)
Here is more or less the same with a single event procedure for each window and with only XIncludeFile “IncludeXXX.pb” to add

main.pb

Code: Select all

CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf

Enumeration Window
  #FormMain
EndEnumeration

Enumeration Gadgets
  #BoutonMain
EndEnumeration

CompilerIf #PB_Compiler_IsMainFile
  Global NewMap MapInclude.i()
  Global Event, Quit
CompilerEndIf

XIncludeFile "Include1.pb"
;XIncludeFile "Include2.pb"

Procedure Event_FormMain(Event)
  Select Event
    Case #PB_Event_CloseWindow
      CloseWindow(EventWindow())
      CompilerIf #PB_Compiler_IsMainFile
        Quit = #True
      CompilerEndIf
      
    Case #PB_Event_MoveWindow
      Debug GetWindowTitle(EventWindow()) + " position(" + WindowX(EventWindow()) + ", " + WindowY(EventWindow()) + ")"
      
    Case #PB_Event_LeftDoubleClick
      Debug "Double clic sur " + GetWindowTitle(EventWindow()) 
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #BoutonMain
          Debug "Clic sur " + GetGadgetText(EventGadget())
      EndSelect
      
  EndSelect
EndProcedure

OpenWindow(#FormMain, 240, 120, 240, 120, "Fenetre principale", #PB_Window_SystemMenu)
ButtonGadget(#BoutonMain, 30, 30, 180, 50, "Bouton principal")
MapInclude(Str(#FormMain)) = @Event_FormMain()

CompilerIf #PB_Compiler_IsMainFile
  Repeat
    Event = WaitWindowEvent()
    If FindMapElement(MapInclude(), Str(EventWindow()))
      CallFunctionFast(MapInclude(), Event)
    EndIf
  Until Quit
CompilerEndIf

Include1.pb

Code: Select all

CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf

Enumeration Window
  #FormInclude1
EndEnumeration

Enumeration Gadgets
  #BoutonInclude1
EndEnumeration

CompilerIf #PB_Compiler_IsMainFile
  Global NewMap MapInclude.i()
  Global Event, Quit
CompilerEndIf

Procedure Event_FormInclude1(Event)
  Select Event
    Case #PB_Event_CloseWindow
      CloseWindow(EventWindow())
      CompilerIf #PB_Compiler_IsMainFile
        Quit = #True
      CompilerEndIf
      
    Case #PB_Event_MoveWindow
      Debug GetWindowTitle(EventWindow()) + " position(" + WindowX(EventWindow()) + ", " + WindowY(EventWindow()) + ")"
      
    Case #PB_Event_LeftDoubleClick
      Debug "Double clic sur " + GetWindowTitle(EventWindow())
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #BoutonInclude1
          Debug "Clic sur " + GetGadgetText(EventGadget())
      EndSelect
      
  EndSelect
EndProcedure

OpenWindow(#FormInclude1, 480, 240, 240, 120, "Fenetre Include1", #PB_Window_SystemMenu)
ButtonGadget(#BoutonInclude1, 30, 30, 180, 50, "Bouton Include1")
MapInclude(Str(#FormInclude1)) = @Event_FormInclude1()

CompilerIf #PB_Compiler_IsMainFile
  Repeat
    Event = WaitWindowEvent() 
    If FindMapElement(MapInclude(), Str(EventWindow()))
      CallFunctionFast(MapInclude(), Event)
    EndIf
  Until Quit
CompilerEndIf
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5611
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Add includes while modifying minimum of code

Post by Kwai chang caine »

Thanks a lot ChrisR for your version, works perfectly 8)

Yesterday i have create a working MODULE version, thanks to the help of MkSoft, but finally i see it's very complcated to use the modules, it's the fiirst time i use it, furthermore that change completely the code, and this history of create common module for accessing a variable in the main of program.. :shock:

When i have beginning my project, i never thought it would be so complicated to add add-ons to an existing program. I've been experimenting with as many methods as possible for a month now (DLLs, embedded executables, include, modules)... and so far... each method has its drawbacks.

I don't know if there's one method that's most commonly used by professionals, probably DLL... but even then, I'm not sure.
But I believe the professional method will surely be far too difficult for KCC to understand and program, as usual. :mrgreen:

Thanks again to your precious advice :wink:
ImageThe happiness is a road...
Not a destination

PureBasic French Forum
Post Reply