Generate forms
Posted: Tue Sep 03, 2013 10:29 am
I have some programs with complex dialogs and everytime I have to add a single button or so, rearranging all items needs a lot of time...
...so I just started to write a simple generator, which should allow to add and remove buttons very simply. It's just a simple skeleton for now, but you can easily check, what happens, when you change the line ;~offset=60 to ;~offset=80 or inserting/removing a line starting with ;Button...
When running the program, all elements will be recalculated and the resulting source code can be copied from the clipboard into a new file.
Next step will be to create a IDE-Tool which automatically adapt source files like the example below...
...actually I don't know, how to find the beginning of a remark! Sounds silly, but how to detect, if a semicolon is part of a code or will the token for a remark, like in the example Debug ";'"+Chr(';')+';'; + ;?
...so I just started to write a simple generator, which should allow to add and remove buttons very simply. It's just a simple skeleton for now, but you can easily check, what happens, when you change the line ;~offset=60 to ;~offset=80 or inserting/removing a line starting with ;Button...
When running the program, all elements will be recalculated and the resulting source code can be copied from the clipboard into a new file.
Code: Select all
; Define
;/v\
;#WinX=840
;#WinY=510
;Num:
;#MainMenu
;#Panel
;#PanelMainProfile
;#PanelMainStart
;#PanelMainCreateList
;#PanelMainTest
;#PanelMainQuit
;#PanelMain=0
;#PanelSettings
;#PanelConversion
;#PanelTitles
;Num
;~offset=60
;Win,#MainMenu,0,0,#WinX,#WinY,"",#PB_Window_SystemMenu
;Panel,#Panel,15,15,#WinX-30,#WinY-30
;Panel+,#Panel,#PanelMain," Main Menu "
;Button,#PanelMainProfile,~x=45,~y=60,~bx=330,~by=45,"Profile: Notebook"
;Button,#PanelMainStart,~x,~y+=~offset,~bx,~by,"Start Processing"
;Button,#PanelMainCreateList,~x,~y+=~offset,~bx,~by,"Create Image List"
;Button,#PanelMainTest,~x,~y+=~offset,~bx,~by,"**** REMOVE THIS LINE ****"
;Button,#PanelMainQuit,~x,~y+=~offset,~bx,~by,"Quit"
;Panel+,#Panel,#PanelSettings," General Settings "
;Panel+,#Panel,#PanelSettings," Image Conversion"
;Panel+,#Panel,#PanelTitles," Master and Title pages"
;Repeat
;Until WaitWindowEvent()=#PB_Event_CloseWindow
;\^/
; EndDefine
; Define Formerl by Michael Vogel
Structure localtype
name.s
val.i
EndStructure
Global NewList locallist.localtype()
Procedure.s MyTrim(s.s)
Protected i
Protected l=Len(s)
Protected b
While l
l-1
b=PeekB(@s+l)
If (b<>' ') And (b<>#TAB)
Break
EndIf
Wend
While i<l
b=PeekB(@s+i)
If (b<>' ') And (b<>#TAB)
Break
EndIf
i+1
Wend
ProcedureReturn PeekS(@s+i,l-i+1)
EndProcedure
Procedure FindChars(string.s,chars.s,start,direction)
#FindChars_Left=0
#FindChars_Right=1
direction=(direction-1)*2+1
start+direction
While start>0 And start<=Len(string)
If FindString(chars,Mid(string,start,1))
ProcedureReturn start
EndIf
start+direction
Wend
ProcedureReturn #Null
EndProcedure
Procedure.s CalcLocal(s.s,n,mode)
Protected a,b,c,d
a=FindChars(s,"+-*/",n,#FindChars_Left)+1
b=FindChars(s,"+-*/",n,#FindChars_Right)
If b=0
b=Len(s)+1
EndIf
c=Val(Mid(s,a,n-a))
d=Val(Mid(s,n+1,b-n-1))
Select mode
Case '+'
c+d
Case '-'
c-d
Case '*'
c*d
Case '/'
If d
c/d
Else
c=0; Error
EndIf
EndSelect
ProcedureReturn Left(s,a-1)+Str(c)+Mid(s,b)
EndProcedure
Procedure FindLocal(name.s)
Enumeration
#FindLocalEmptyList
#FindLocalNotFound
#FindLocalFound
EndEnumeration
Protected flag
ResetList(locallist())
Repeat
If NextElement(locallist())
If locallist()\name=name
flag=#FindLocalFound
EndIf
Else
flag=#FindLocalNotFound
EndIf
Until flag
ProcedureReturn flag
EndProcedure
Procedure ModeLocal(s.s,n)
Protected mode
Protected z=Len(s)
While n<z And mode=0
mode=PeekA(@s+n)
Select mode
Case '+','-','*','/'
Default
mode=0
n+1
EndSelect
Wend
ProcedureReturn n+1
EndProcedure
Procedure EvalLocal(s.s)
Protected n,m,val,z
Protected c.s
n=FindString(s,"~")
While n
m=ModeLocal(s,n)
If FindLocal(Mid(s,n,m-n))=#FindLocalFound
val=locallist()\val
Else
val=0
EndIf
s=Left(s,n-1)+Str(val)+Mid(s,m)
n=FindString(s,"~",n)
Wend
For z=1 To 4
c=Mid("*/+-",z,1)
n=FindString(s,c)
While n
s=CalcLocal(s,n,Asc(c))
n=FindString(s,c)
Wend
Next z
ProcedureReturn Val(s)
EndProcedure
Procedure Local(s.s)
Protected name.s
Protected mode.i
Protected n.i
Protected val.i
Protected flag.i
s=ReplaceString(s," ","")
s=ReplaceString(s,#TAB$,"")
s=LCase(s)
n=FindString(s,"=")
If n
mode=PeekA(@s+n-1)
Select mode
Case '+','-','*','/'
name=Left(s,n-2)
Default
mode='='
name=Left(s,n-1)
EndSelect
s=StringField(s,2,"=")
If FindString("+-*/",Right(name,1))
s=name+s
name=Left(name,Len(name)-1)
EndIf
val=EvalLocal(s)
With locallist()
If FindLocal(name)<>#FindLocalFound
AddElement(locallist())
\name=name
EndIf
Select mode
Case #Null
val=\val
Case '='
; val=Val(s)
EndSelect
;Debug "Set "+name+":"+Chr(mode)+Str(val)+" ("+\name+" was "+Str(\val)+")"
\val=val
ProcedureReturn val
EndWith
Else
If FindLocal(s)
ProcedureReturn locallist()\val
Else
ProcedureReturn #Null
EndIf
EndIf
EndProcedure
Procedure.s Formerl()
Protected n,z
Protected s.s,ts.s,tt.s
Protected flag
Protected result.s
If ReadFile(0,#PB_Compiler_File)
While Eof(0)=#Null
s=MyTrim(ReadString(0))
If PeekA(@s)=';'
If s=";/v\"
flag=#True
ElseIf s=";\^/"
flag=#False
ElseIf flag
s=Mid(s,2)
Select PeekA(@s)
Case '#'
s=" "+s
Case '~'
Local(s)
s=""
Default
n=CountString(s,",")
If n
ts=s
s=StringField(ts,1,",")
For z=2 To n+1
tt=StringField(ts,z,",")
If FindString(tt,"~")
tt=Str(Local(tt))
EndIf
s+","+tt
Next z
EndIf
n=FindString(s,",")
If n
ts="("+Mid(s,n+1)+")"
s=Left(s,n-1)
Else
ts=""
EndIf
Select s
Case "Num:"
s="Enumeration"
Case "Num"
s="EndEnumeration"
Case "Win"
s="OpenWindow"+ts
Case "Panel"
s="PanelGadget"+ts
Case "Panel+"
s="AddGadgetItem"+ts
Case "Button"
s="ButtonGadget"+ts
Default
s=s+"; "+ts
EndSelect
EndSelect
;Debug s
result+s+#CRLF$
EndIf
EndIf
Wend
EndIf
ProcedureReturn result
EndProcedure
; EndDefine
SetClipboardText(Formerl())
Next step will be to create a IDE-Tool which automatically adapt source files like the example below...
Code: Select all
#WinX=840
#WinY=510
Enumeration
#MainMenu
#Panel
#PanelMainProfile
#PanelMainStart
#PanelMainCreateList
#PanelMainTest
#PanelMainQuit
#PanelMain=0
#PanelSettings
#PanelConversion
#PanelTitles
EndEnumeration
OpenWindow(#MainMenu,0,0,#WinX,#WinY,"",#PB_Window_SystemMenu)
PanelGadget(#Panel,15,15,#WinX-30,#WinY-30)
AddGadgetItem(#Panel,#PanelMain," Main Menu ")
; [~offset=60]
ButtonGadget(#PanelMainProfile,45,60,330,45,"Profile: Notebook"); [~x=45,~y=60,~bx=330,~by=45]
ButtonGadget(#PanelMainStart,45,120,330,45,"Start Processing"); [~x,~y+=~offset,~bx,~by]
ButtonGadget(#PanelMainCreateList,45,180,330,45,"Create Image List"); [~x,~y+=~offset,~bx,~by]
ButtonGadget(#PanelMainTest,45,240,330,45,"**** REMOVE THIS LINE ****"); [~x,~y+=~offset,~bx,~by]
ButtonGadget(#PanelMainQuit,45,300,330,45,"Quit"); [~x,~y+=~offset,~bx,~by]
AddGadgetItem(#Panel,#PanelSettings," General Settings ")
AddGadgetItem(#Panel,#PanelSettings," Image Conversion")
AddGadgetItem(#Panel,#PanelTitles," Master and Title pages")
Repeat;
Until WaitWindowEvent()=#PB_Event_CloseWindow;