For Fbide users.

General FreeBASIC programming questions.
Post Reply
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

For Fbide users.

Post by dodicat »

Here is a little api to set compiler options
eg
-gen gcc -Wc -O3
To test your code outwith fbide.
In the api:
Set your compiler first, and add options in the little box.
Then get your file and compile then run.
It is a bit like quick run in as much as it creates and destroys TEMP files .bas/.exe.
Compile this and pop it somewhere handy.
(It doesn't work very well if you just quick run it!)
EDIT: included ClipBoard option.

Code: Select all

#define WIN_INCLUDEALL
#Include Once "windows.bi"
#Include once "/win/commctrl.bi"
#include "file.bi"
shell "title Compiler output"
Dim Shared As String fbpath,s1,cd,cpy,L
cd=Curdir

Declare Function SetWindowTheme Lib "UxTheme.dll" Alias "SetWindowTheme"(As Any Ptr,As zstring Ptr,As zstring Ptr) As Long 
Declare Sub getfiles(Byref BMPfile As OpenFileName,msg As String,flag As String)
Declare Sub CreateMessageWindow
Declare Function loadfile(file As String,flag As Long=1) As String
Declare Sub savefile(filename As String,p As String)
Declare Function getclipboard() As String

 

' Globals (unavoidable)
Dim Shared As HFONT guiFont
Dim Shared As zString * 255 textMessage=""
textmessage=loadfile("Buildoptions.txt")
Dim As String tmp
savefile(cd+"\Clipboard.txt","")
fbpath=loadfile("CompilerPath.txt",0)

Dim Shared As Long flag
Dim Shared As HWND  MainWindow, MessageWindow,btn,btn2,runbut,compiler
Dim Shared As HWND EditBox, Button,msgon,label,bar2,label2,minilabel,clabel,file,clipboard
Dim Shared As Long trackpos
Dim Shared BMPfile As OpenFileName
Function WndProc(hWnd As HWND, msg As UINT, wParam As WPARAM, lParam As LPARAM) As LRESULT
    
    Select Case hWnd
    
    Case MainWindow  
        Select Case msg
        
        Case WM_PAINT
            Dim As PAINTSTRUCT ps
            BeginPaint(hWnd, @ps)
            FillRect(ps.hdc, @ps.rcPaint, CreateSolidBrush(BGR(100,100,255)))
            EndPaint(hWnd, @ps)
            
        Case WM_CLOSE
            Kill   cd+"/buildoptionsTEMP.exe"
            Kill   cd+"/BuildoptionsTEMP.bas"
            Kill   cd+"/Clipboard.txt"
            PostQuitMessage(NULL) 
            
        Case WM_COMMAND
            Select Case lParam  
            Case msgon 
                CreateMessageWindow 
                
            Case clipboard
                L=getclipboard
                savefile("Clipboard.txt",L)
                cpy="clipboard.txt"
                setwindowtext(file,"File" +Chr(13,10)+cpy)
                Goto label
                
            Case btn 'open
                getfiles(BMPfile, "FreeBASIC Files","*.bas")
                s1=*BMPfile.lpstrFile
                cpy=s1
                setwindowtext(file,"File" +Chr(13,10)+cpy)
                L=loadfile(s1,3)
                label:
                savefile(cd +"/BuildoptionsTEMP.bas",L)
                s1=cd+"/BuildoptionsTEMP.bas"
                ShellExecute (0,"","notepad.exe ",cpy,"",SW_SHOWNORMAL)
                
            Case btn2'compile
                if len(fbpath)=0 then setwindowtext(label,"PLEASE CHOOSE A COMPILER"):exit select
                If Len(s1) Then
                    textmessage=loadfile(cd+"\Buildoptions.txt",3)
                    s1=" "+cd+"/BuildoptionsTEMP.bas"
                    s1+=" "+textmessage+" "
                    Dim As String tm="Command "+Chr(13,10)+fbpath+s1
                    setwindowtext(clabel,tm)
                    Shell   fbpath+s1
                Else
                    messagebox(0,"No file chosen","",0)
                End If
            Case runbut'run
                
                Dim As String tm="Command "+Chr(13,10)+cd+"/buildoptionsTEMP.exe"
                setwindowtext(clabel,tm)
                
                If Fileexists (  cd+"/buildoptionsTEMP.exe")  Then
                    ShellExecute (0,"", cd+"/buildoptionsTEMP.exe","","",SW_SHOWNORMAL) 
                End If
                
            Case compiler'select a compiler
                getfiles(BMPfile, "Fbc.exe","fbc*.exe")
                s1=*BMPfile.lpstrFile
                savefile(cd+"/CompilerPath.txt",s1)
                
                fbpath=loadfile(cd +"/CompilerPath.txt",3)
                
                setwindowtext(label, fbpath)
                
            End Select
            
        End Select
        
    Case MessageWindow  
        Select Case msg
        Case WM_COMMAND
            Select Case lParam  
            Case Button   
                GetWindowText(EditBox, @textMessage, 255)
                flag=0
                savefile(cd+"\buildoptions.txt",textmessage)
                destroywindow(messagewindow)
            End Select
            
        Case WM_CLOSE
            flag=0
        End Select
        
    End Select
    
    Return DefWindowProc(hWnd, msg, wParam, lParam)
End Function


' Create  window class:
Dim As WNDCLASS wcls

With wcls
    .style      = CS_HREDRAW Or CS_VREDRAW
    .lpfnWndProc  = Cast(WNDPROC, @WndProc)
    .hInstance    = GetModuleHandle(NULL)
    .hIcon      = LoadIcon(NULL, IDI_APPLICATION)
    .hCursor      = LoadCursor(NULL, IDC_ARROW)
    .hbrBackground  = GetStockObject(WHITE_BRUSH)
    .lpszMenuName  = NULL
    .lpszClassName  = Strptr("WindowClass")
End With

If RegisterClass(@wcls) = FALSE Then
    MessageBox(NULL, "RegisterClass('WindowClass') FAIL!", "Error!", MB_OK Or MB_ICONERROR)
    End
End If


MainWindow = CreateWindowEx(NULL, "WindowClass", "Flag setter", WS_OVERLAPPEDWINDOW Or WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 650, 500, NULL, NULL, NULL, NULL)
msgon= CreateWindowEx(NULL, "Button", "Build options", WS_VISIBLE Or WS_CHILD , 10, 40, 90, 24, MainWindow, NULL, NULL, NULL)
btn=CreateWindowEx( 0,"BUTTON","Open", ws_border Or WS_VISIBLE Or WS_CHILD ,150,50,100,30,  MainWindow,0,0,0)
btn2  =createwindowex(0,"BUTTON","Compile"  ,ws_border Or WS_VISIBLE Or WS_CHILD,280,50,120,30,MainWindow,0,0,0)
runbut=createwindowex(0,"BUTTON","Run"  ,ws_border Or WS_VISIBLE Or WS_CHILD,430,50,120,30,MainWindow,0,0,0)
compiler=createwindowex(0,"BUTTON","Select Compiler"  ,ws_border Or WS_VISIBLE Or WS_CHILD,230,150,120,30,MainWindow,0,0,0)
label=createwindowex(0,"STATIC",fbpath , WS_VISIBLE Or WS_CHILD,0,200,600,60,MainWindow,0,0,0)
minilabel=Createwindowex(0,"STATIC","Compiler:"  ,WS_VISIBLE Or WS_CHILD,0,170,100,30,MainWindow,0,0,0)
clabel=createwindowex(0,"STATIC","Command " , WS_VISIBLE Or WS_CHILD,0,280,600,80,MainWindow,0,0,0)
file=createwindowex(0,"STATIC","File" , WS_VISIBLE Or WS_CHILD,0,380,600,60,MainWindow,0,0,0)
clipboard=CreateWindowEx( 0,"BUTTON","Clipboard", ws_border Or WS_VISIBLE Or WS_CHILD ,150,100,100,30,  MainWindow,0,0,0)
Sub CreateMessageWindow
    If flag=0 Then
        flag=1
        MessageWindow = CreateWindowEx(NULL, "WindowClass", "Options", WS_OVERLAPPEDWINDOW Or WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 300, 150, NULL, NULL, NULL, NULL)
        EditBox = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", textmessage, WS_VISIBLE Or WS_CHILD Or WS_HSCROLL  Or ES_AUTOHSCROLL Or ES_MULTILINE, 10, 0, 250, 50, MessageWindow, NULL, NULL, NULL)
        Button = CreateWindowEx(NULL, "Button", "Save", WS_VISIBLE Or WS_CHILD, 10, 70, 200, 24, MessageWindow, NULL, NULL, NULL)
        
        SetWindowTheme(messagewindow," "," ")'  optional
    End If 
End Sub
Sub getfiles(Byref BMPfile As OpenFileName,msg As String,flag As String)
    Dim As zstring * 2048 SELFILE
    Dim As String MYFILTER
    MYFILTER = msg+Chr(0)+flag+Chr(0)
    With BMPfile
        .lStructSize = Sizeof(OpenFileName)
        .hInstance = null 
        .lpstrFilter = Strptr(MYFILTER)
        .lpstrFile = @SELFILE
        .nMaxFile = 2048
        .nMaxFileTitle = 0
        .lpstrTitle =@"Open"
    End With
    GetOpenFileName(@BMPfile)
End Sub
Function loadfile(file As String,flag As Long=1) As String
    If flag=1 Then
        If Fileexists("Buildoptions.txt")=0 Then savefile("Buildoptions.txt","")
    End If
    If flag=0 Then
        If Fileexists("CompilerPath.txt")=0 Then savefile("CompilerPath.txt","")
    End If
    Var  f=Freefile
    Open file For Binary Access Read As #f
    Dim As String text
    If Lof(1) > 0 Then
        text = String(Lof(f), 0)
        Get #f, , text
    End If
    Close #f
    Return text
End Function

Sub savefile(filename As String,p As String)
    Dim As Integer n
    n=Freefile
    If Open (filename For Binary Access Write As #n)=0 Then
        Put #n,,p
        Close
    Else
        Print "Unable to load " + filename
    End If
End Sub
Function getclipboard() As String
    If IsClipboardFormatAvailable(CF_TEXT) = 0 Then Return "Error"
    If OpenClipboard(0) = 0 Then Return "Error"
    Function = *Cast(zstring Ptr,GetClipboardData(CF_TEXT))
    CloseClipboard()
End Function
Sub cleanup Destructor
    Kill   cd+"/buildoptionsTEMP.exe"
    Kill   cd+"/BuildoptionsTEMP.bas"
    Kill   cd+"/Clipboard.txt"
End Sub


Dim As MSG uMsg

While GetMessage(@uMsg, NULL, NULL, NULL) <> FALSE
    TranslateMessage(@uMsg)
    DispatchMessage(@uMsg)
Wend

 
Last edited by dodicat on Jun 27, 2017 13:31, edited 2 times in total.
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: For Fbide users.

Post by deltarho[1859] »

I hope you don't mind but I have tweaked your GUI a little to suit my tastes.

Image

However, I have not touched the functionality in any way.

I chose a compiler and opened a file to compile with a build option of simply '-s console'. I know that is the default but I still like to use it.

As you can see all seems well in the 'Command' area but when I click 'Run' I get this in the 'Command area'.

F:\FreeBASIC/PCG\PCG32IITest.exe

but it doesn't run. Nothing seems to happen.

When finished I noticed that Buildoptions.txt was a zero byte file which I reckon is causing the trouble.
deltarho[1859]
Posts: 4313
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: For Fbide users.

Post by deltarho[1859] »

Oddly enough, it just worked fine when I used the 64 bit compiler.

Went back to the 32 bit compiler and noticed that two versions of Buildoptions.txt existed: a 10 byte file in the same folder as your application, "-s console", and another in the PCG folder of zero bytes - the PCG folder being where the source code to compile resides.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: For Fbide users.

Post by dodicat »

Thanks for testing deltarho[]
I have adjusted the first post.
1) I made all loadfile flags = 3 (except the first), so the buildoptions.txt is strictly in the executable folder.
2) I put in a destructor sub to clean up after an unexpected end.
3) line 5 added a shell command to title the console
4) After case btn2 (compile), added
if len(fbpath)=0 then setwindowtext(label,"PLEASE CHOOSE A COMPILER"):exit select
(So it doesn't stall)
I kept the original box positions, but your's look better.

As for you question in the other thread about quick run in fbide/fbedit
I think pressing quick run copies the ide contents to the clipboard and creates two temp files. .bas and .exe.
Post Reply