' Compiler Utility for Rapid-Q
' CodeBox  Version 1.1
' This source file needs folder.bmp

$OPTIMIZE ON
$TypeCheck ON
$Include "rapidq.inc"

$TypeCheck ON

DECLARE FUNCTION Setfocus Lib "user32" Alias "SetFocus"(hwnd As Long) As Long 'FMD: More control to you

'I forgot CGI :(
' Oh well,
' Maybe in the next release...
' Wait a minute!
' It can only be defined in the source code!

DECLARE SUB ShowError
DECLARE SUB OpenSource (Sender AS QCOOLBTN)
DECLARE SUB MakeItWindows (Sender AS QRADIOBUTTON)
DECLARE SUB MakeItGTK (Sender AS QRADIOBUTTON)
DECLARE SUB MakeItConsole (Sender AS QRADIOBUTTON)
DECLARE SUB MakeItCGI (Sender as QRADIOBUTTON)
DECLARE SUB RunExe (Sender AS QCHECKBOX)
DECLARE SUB ChangeIncPath
DECLARE SUB ChangeLibPath
DECLARE SUB UseBytecode (Sender AS QCHECKBOX)
DECLARE SUB OptimizeCode (Sender AS QCheckBox)
DECLARE SUB NoOptimization (Sender AS QCheckBox)
DECLARE SUB DoCompile (Sender AS QBUTTON)
DECLARE SUB ExitNow (Sender AS QBUTTON)
DECLARE SUB UseIcon (Sender AS QBUTTON)
Declare Sub FocusForm
Declare sub DoNotRunEXE(Sender as QCHECKBOX)
Declare sub CheckSource
Declare sub CheckIcon
'Declare sub EditSetup(Sender as qbutton) FMD: I don't know why ???
'declare sub DisableSetup(Sender as qbutton)
declare sub NoByteCode(Sender as QCheckBox)
declare sub SetSetup

$Resource SourceFolder as "folder.bmp" 'FMD: Please check your original-code. You described an other bmp-source!

static Bytecode as string:static AppInfo as string:static IconInfo as string
static LibPath as string:static IncludePath as string: static Optimize as string
static RunAfterCompile as integer

CREATE Form AS QFORM
    Caption = "CodeBox Program"
    Width = 382 'FMD: I changed the dimensions to have a better size for the edit-box 
    Height = 346
    Center
    BorderStyle=bsDialog
    CREATE Label3 AS QLABEL
        Caption = "Main source:"
        Left = 8
        Top = 8
        Width = 60
        Transparent = 1
    END CREATE
    CREATE CoolBtn1 AS QCOOLBTN
        BMPHandle = SourceFolder
        Left = 338
        Top = 7
        Width = 23
        Height = 22
        Flat = 1
        ShowHint = 1
        Hint = "Open source"
        OnClick = OpenSource
    END CREATE
    CREATE Panel1 AS QPANEL
        Left = 8
        Top = 220
        Width = 361
        Height = 60
        BevelOuter = 1 
        CREATE RichEdit1 AS QRICHEDIT
            BorderStyle = 1
            Left = 1
            Top = 1
            Width = 361
            Height = 60
            Align = 5
            ReadOnly = True
            HideScrollbars=0
            ScrollBars=ssVertical
            TabOrder=10
        END CREATE
    END CREATE
    CREATE GroupBox1 AS QGROUPBOX
        Caption = "App&Type"
        Left = 8
        Top = 72
        Width = 361
        Height = 145
        TabOrder = 4
        CREATE Label1 AS QLABEL
            Caption = "Change include path:"
            Left = 8
            Top = 76
            Width = 100
        END CREATE
        CREATE Label2 AS QLABEL
            Caption = "Change library path:"
            Left = 8
            Top = 95
            Width = 117
        END CREATE
        CREATE Label4 AS QLABEL
            Caption = "Icon Path:"
            Left = 8
            Top = 114
            Width = 117
        END CREATE
        CREATE RadioButton1 AS QRADIOBUTTON
            Caption = "&GUI (Windows)"
            Left = 8
            Top = 24
            ShowHint = 1
            Hint = "Creates an application for Windows"
            TabOrder = 4
            Checked = 1
            OnClick = MakeItWindows
        END CREATE
        CREATE RadioButton2 AS QRADIOBUTTON
            Caption = "GT&K (Linux)"
            Left = 8
            Top = 40
            ShowHint = 1
            Hint = "Creates an application for Linux"
            TabOrder = 5
            OnClick = MakeItGTK
        END CREATE
        CREATE RadioButton3 AS QRADIOBUTTON
            Caption = "C&onsole"
            Left = 8
            Top = 56
            ShowHint = 1
            Hint = "Creates an application for the console"
            TabOrder = 6
            OnClick = MakeItConsole
        END CREATE
        CREATE CheckBox1 AS QCHECKBOX
        Caption = "Compile as &byte-code"
        Left = 120
        Top = 22
        Width = 153
        ShowHint = 1
        Hint = "Creates a bytecoded file"
        TabOrder = 7
        OnClick = UseBytecode
        END CREATE
        CREATE CheckBox2 AS QCHECKBOX
            Caption = "&Autorun"
            Left = 120
            Width = 60
            Top = 38
            ShowHint = 1
            Hint = "Runs immediately after compiling"
            TabOrder = 8
            OnClick = RunExe
        END CREATE
        CREATE CheckBox3 as QCheckBox
             Caption = "&Optimize"
             Left = 120
             Width = 60
             Top = 54
             Hint="Makes your executable smaller in size"
             ShowHint=1
             TabOrder=9
             OnClick=OptimizeCode
        END CREATE
        CREATE IncPathText AS QEdit
            Left = 120
            Top = 75
            Width = 220
            TabOrder = 10
        END CREATE
        CREATE LibPathText AS QEdit
            Left = 120
            Top = 96
            Width = 220
            TabOrder = 11
        END CREATE
        CREATE IconPathText AS QEdit 'FMD: Is'nt it nice to have visual control about the iconpath? 
            Left = 120
            Top = 116
            Width = 220
            TabOrder = 12
        END CREATE
    END CREATE
    CREATE Edit1 AS QEDIT
        Text = ""
        Left = 72
        Top = 8
        Width = 265
        TabOrder = 0 'FMD: I set this TabOrder to zero, I think you know why :-)
        ShowHint = 1
        Hint = "Main source to compile"
    END CREATE
    CREATE Button3 AS QBUTTON
        Caption = "&Compile"
        Left = 72
        Top = 40
        ShowHint = 1
        Hint = "Make that code do something :)"
        TabOrder = 1
        OnClick = DoCompile
    END CREATE
    CREATE Button5 AS QBUTTON
        Caption = "&Icon"
        Left = 168
        Top = 40
        ShowHint = 1
        Hint = "Icon for your application (NOTE: Your icon must reside in the same directory)"
        TabOrder = 2
        OnClick = UseIcon
    END CREATE
        CREATE Button4 AS QBUTTON
        Caption = "E&xit"
        Left = 264
        Top = 40
        ShowHint = 1
        Hint = "Exit"
        TabOrder = 3
        OnClick = ExitNow
    END CREATE
    'CREATE Button6 AS QBUTTON
    '    Caption = "&Edit"
    '    Left = 198
    '    Top = 290
    '    TabOrder = 8
    '    OnClick=EditSetup
    'END CREATE
    CREATE Button7 AS QBUTTON
        Caption = "&Show Setup"
        Left = 294
        Top = 290
        TabOrder = 9
        ShowHint=1
        Hint="Preview command line"
        OnClick=SetSetup
    END CREATE
    CREATE Button8 AS QBUTTON 'FMD: The 'nirwana-button'
        Left = 1 'FMD: I know - it's nonsense to set alignments for a non-visible control
        Top = 1
        Width = 1
        Height = 1
        Visible = 0
        END CREATE
   CREATE ProgressMeter as QGAUGE
        Top = 290
        left = 10
        BorderStyle = 0
        Width = 265
        height = 23
        Visible = 0
   END CREATE
 END CREATE
'--------- Subs ---------

SUB OpenSource (Sender AS QCOOLBTN)
Create OpenSource as QOpenDialog
Filter="Rapid-Q source files|*.bas;*.rq;*.rqb;*.inc|All files (*.*)|*.*"
InitialDir=CurDir$
End Create

if OpenSource.execute then
Edit1.text=OpenSource.filename
IncPathText.Text=CurDir$
LibPathText.Text=CurDir$
CheckSource
end if
END SUB

Sub CheckIcon
if FileExists(IconPathText.text)=0 and IconPathText.text<>"" then
MessageBox("The icon: "+IconPathText.text+" does not exist","Warning",48)
RichEdit1.Clear
IconPathText.text=""
UseIcon(QBUTTON)
end if
End Sub

Sub CheckSource
if FileExists(Edit1.text)=0 and Edit1.text<>"" then
MessageBox("The source file: "+Edit1.text+" does not exist","Warning",48)
RichEdit1.Clear
Edit1.text=""
OpenSource(QCOOLBTN)
end if
End Sub

Sub ShowError
if Edit1.text="" then
MessageBox("You need to type in a source file first", _
"Uh oh...",16)
OpenSource(QCOOLBTN)
else
CheckSource
 end if
End Sub

SUB MakeItWindows (Sender AS QRADIOBUTTON)
AppInfo=" "+"-GUI"+" "
END SUB

SUB MakeItGTK (Sender AS QRADIOBUTTON)
AppInfo=" "+"-o"+" " 'GTK (Linux)
END SUB

SUB MakeItConsole (Sender AS QRADIOBUTTON)
AppInfo=" "+"-CONS"+" " 'Console
END SUB

SUB RunExe (Sender AS QCHECKBOX)
RunAfterCompile=1
Sender.OnClick=DoNotRunExe
END SUB

SUB DoNotRunEXE(Sender as QCHECKBOX)
RunAfterCompile=0
Sender.OnClick=RunExe
End SUB

SUB ChangeIncPath
if IncPathText not="" then
IncludePath=" "+"-I"+IncPathText.text+" "
else
IncludePath=" "+"-I"+CurDir$+" "
end if
END SUB

SUB ChangeLibPath
if LibPathText not="" then
LibPath=" "+"-L"+LibPathText.text+" "
else
LibPath=" "+"-L"+CurDir$+" "
end if
END SUB

SUB UseBytecode (Sender AS QCHECKBOX)
ByteCode="-b" 'yes
Sender.OnClick=NoBytecode
END SUB

Sub NoBytecode(Sender as QCheckBox)
ByteCode=""  ' no
Sender.OnClick=UseByteCode
End Sub

Sub OptimizeCode(Sender as QCheckBox)
Optimize="-opt"
Sender.OnClick=NoOptimization
End Sub

Sub NoOptimization(Sender as QCheckBox)
Optimize=""
Sender.OnClick=OptimizeCode
End Sub

SUB UseIcon (Sender AS QBUTTON)
Create OpenIcon as QOpenDialog
filter="Icons (*.ico)|*.ico"
InitialDir=CurDir$
End Create

if OpenIcon.execute then
'for some reason I have to reset it
IconInfo=" "+"-g"+OpenIcon.filename+" "
IconPathText.Text=OpenIcon.filename
CheckIcon
 else
end if
END SUB

SUB DoCompile (Sender AS QBUTTON)
Dim RetVal as Integer, Status as long, i as long
call ChangeIncPath
call ChangeLibPath
IncludePath=" "+"-I"+CurDir$+" "  'command line
LibPath=" "+"-L"+CurDir$+" "

if Edit1.text not="" then
CheckSource
RetVal=Shell(CurDir$+"/rc.exe "+Edit1.text+" "+AppInfo+" "+Optimize+" "+ _
bytecode+" "+IconInfo+" "+IncludePath+" "+LibPath,0) 'FMD: Especially *I* don't need a DOS-Box
'ProgressMeter.Position=0
ProgressMeter.Visible=1

While Status<=100 'Not FileExists(Edit1.text+".exe"-".bas"-".rq"-".rqb"-".inc")
Status++
ProgressMeter.Position=Status
DoEvents
Wend
else
ShowError
 end if
 
if RunAfterCompile then run Edit1.text+".exe"-".bas"-".rq"-".rqb"-".inc"
END SUB

SUB ExitNow (Sender AS QBUTTON)
form.close
END SUB

sub SetSetup
with RichEdit1
.Clear
if Edit1.text not="" then 
.AddStrings Edit1.text
SetFocus (Button8.Handle) 'FMD: throws the focus into nirwana
CheckSource
else
ShowError
end if
if AppInfo not="" then
.AddStrings AppInfo
end if
if Bytecode not="" then 
.AddStrings Bytecode
end if
if Optimize not="" then
.AddStrings Optimize
end if
if IconPathText.Text <>"" then 
.AddStrings IconInfo
end if
call ChangeIncPath
if IncPathText.Text <>"" then
.AddStrings IncludePath
end if
call ChangeLibPath
if LibPathText.Text <>"" then
.AddStrings LibPath
end if
end with
 end sub

form.showmodal