Page 1 of 1

Scripts for Harbour and FWH !!!

Posted: Tue May 17, 2011 8:25 am
by Antonio Linares
This is a powerful example that shows you how to add scripting support into your own applications:

scripts.prg

Code: Select all

// Scripting for Harbour and FiveWin only (not supported on xHarbour)

#include "FiveWin.ch"
#include "Splitter.ch"
#include "xbrowse.ch"

static oWnd

function Main()

   local oBrw, oFont, oMemo, oSplit
   
   if ! File( "scripts.dbf" )
      DbCreate( "scripts.dbf", { { "NAME", "C", 20, 0 }, { "DESCRIPT", "C", 100, 0 }, { "CODE", "M", 80, 0 } } )
   endif      
   
   USE scripts
   
   if RecCount() == 0
      APPEND BLANK
      Scripts->Name := "Test"
      Scripts->Descript := "This is a test script"
      Scripts->Code := "function Test()" + CRLF + CRLF + '   MsgInfo( "Hello world!" )' + CRLF + CRLF + "return nil"
   endif   
   
   DEFINE WINDOW oWnd TITLE "Scripts" MENU BuildMenu()
   
   DEFINE BUTTONBAR oBar OF oWnd SIZE 80, 80 2007
   
   DEFINE BUTTON OF oBar PROMPT "New" FILE "..\bitmaps\alphabmp\plus.bmp" TOOLTIP "New"

   DEFINE BUTTON OF oBar PROMPT "Run" FILE "..\bitmaps\alphabmp\task.bmp" TOOLTIP "Run" ACTION Execute()

   DEFINE BUTTON OF oBar PROMPT "Exit"  FILE "..\bitmaps\32x32\quit.bmp" TOOLTIP "Exit" ;
      ACTION If( MsgYesNo( "Do you want to end ?" ), oWnd:End(),)
      
   @ 5.8, 0 XBROWSE oBrw OF oWnd SIZE 450, 500 ;
      FIELDS Scripts->Name, Scripts->Descript ;
      HEADERS "Name", "Description" CELL LINES
   
   oBrw:CreateFromCode()   
      
   DEFINE FONT oFont NAME "Courier New" SIZE 0, -14   
      
   @ 6.3, 57 GET oMemo VAR Scripts->Code MEMO OF oWnd SIZE 500, 500 FONT oFont
   
   @ 0, 450 SPLITTER oSplit ;
      VERTICAL _3DLOOK ;
      PREVIOUS CONTROLS oBrw ;
      HINDS CONTROLS oMemo ; 
      SIZE 4, 200 PIXEL ;
      OF oWnd
      
   oSplit:AdjClient()         
   
   DEFINE MSGBAR oMsgBar OF oWnd 2007 PROMPT "Scripting support for your applications"
   
   ACTIVATE WINDOW oWnd MAXIMIZED ;
      ON RESIZE oSplit:AdjClient()   
   
return nil

function BuildMenu()

   local oMenu
   
   MENU oMenu
      MENUITEM "Scripts management"
      MENU
         MENUITEM "About..." ACTION MsgAbout( "Scripting support for Harbour and FiveWin", "(c) FiveTech Software 2011" )
         SEPARATOR
         MENUITEM "Exit" ACTION If( MsgYesNo( "Do you want to end ?" ), oWnd:End(),)
      ENDMENU   
   ENDMENU
   
return oMenu      

function Execute()

   local oHrb

   MemoWrit( "_temp.prg", Scripts->Code )
   FReOpen_Stderr( "comp.log", "w" )
   oHrb = HB_CompileBuf( HB_ARGV( 0 ), "_temp.prg", "-n", "-Ic:\fwh\include" )
   // MsgInfo( MemoRead( "comp.log" ) )
   hb_HrbRun( oHrb )

return nil

#pragma BEGINDUMP

#include <stdio.h>
#include <hbapi.h>

HB_FUNC( FREOPEN_STDERR )
{
   hb_retnl( ( LONG ) freopen( hb_parc( 1 ), hb_parc( 2 ), stderr ) );
}    

#pragma ENDDUMP
 
Image

Re: Scripts for Harbour and FWH !!!

Posted: Tue May 17, 2011 10:07 am
by Antonio Linares
Enhanced version:

scripts.prg

Code: Select all

// Scripting for Harbour and FiveWin only (not supported on xHarbour)

#include "FiveWin.ch"
#include "Splitter.ch"
#include "xbrowse.ch"

static oWnd, oResult

function Main()

   local oBrw, oFont, oMemo, oSplit, oSplit2
   local cCode := '#include "FiveWin.ch"' + CRLF + CRLF + "function Test()" + CRLF + CRLF + '   MsgInfo( "Hello world!" )' + CRLF + CRLF + "return nil"
   local cResult
   
   if ! File( "scripts.dbf" )
      DbCreate( "scripts.dbf", { { "NAME", "C", 20, 0 }, { "DESCRIPT", "C", 100, 0 }, { "CODE", "M", 80, 0 } } )
   endif      
   
   USE scripts
   
   if RecCount() == 0
      APPEND BLANK
      Scripts->Name := "Test"
      Scripts->Descript := "This is a test script"
      Scripts->Code := cCode
   endif   
   
   DEFINE WINDOW oWnd TITLE "Scripts" MENU BuildMenu()
   
   DEFINE BUTTONBAR oBar OF oWnd SIZE 80, 80 2007
   
   DEFINE BUTTON OF oBar PROMPT "New" FILE "..\bitmaps\alphabmp\plus.bmp" TOOLTIP "New" ACTION ( DbAppend(), Scripts->Name := "new script", oBrw:Refresh(), oMemo:SetText( cCode ) )

   DEFINE BUTTON OF oBar PROMPT "Run" FILE "..\bitmaps\alphabmp\task.bmp" TOOLTIP "Run" ACTION Execute()

   DEFINE BUTTON OF oBar PROMPT "Exit"  FILE "..\bitmaps\32x32\quit.bmp" TOOLTIP "Exit" ;
      ACTION If( MsgYesNo( "Do you want to end ?" ), oWnd:End(),)
      
   @ 5.8, 0 XBROWSE oBrw OF oWnd SIZE 450, 500 ;
      FIELDS Scripts->Name, Scripts->Descript ;
      HEADERS "Name", "Description" CELL LINES ;
      ON CHANGE ( oMemo:SetText( Scripts->Code ), oMemo:Refresh() )
   
   oBrw:lFastEdit = .T.
   oBrw:CreateFromCode()   
      
   DEFINE FONT oFont NAME "Courier New" SIZE 0, -14   
      
   @ 6.3, 57 GET oMemo VAR Scripts->Code MEMO OF oWnd SIZE 500, 569 FONT oFont
   
   @ 50.5, 57 GET oResult VAR cResult MEMO OF oWnd SIZE 500, 300 FONT oFont
   
   @ 0, 450 SPLITTER oSplit ;
      VERTICAL _3DLOOK ;
      PREVIOUS CONTROLS oBrw ;
      HINDS CONTROLS oMemo, oResult ; 
      SIZE 4, 200 PIXEL ;
      OF oWnd
      
   @ 650, 460 SPLITTER oSplit2 ;
            HORIZONTAL _3DLOOK ;
            PREVIOUS CONTROLS oMemo ;
            HINDS CONTROLS oResult ;
            SIZE 500, 4  PIXEL ;
            OF oWnd 
   
   DEFINE MSGBAR oMsgBar OF oWnd 2007 PROMPT "Scripting support for your applications"
   
   ACTIVATE WINDOW oWnd MAXIMIZED ;
      ON RESIZE ( oSplit:AdjLeft(), oSplit2:AdjRight() )   
   
return nil

function BuildMenu()

   local oMenu
   
   MENU oMenu
      MENUITEM "Scripts management"
      MENU
         MENUITEM "About..." ACTION MsgAbout( "Scripting support for Harbour and FiveWin", "(c) FiveTech Software 2011" )
         SEPARATOR
         MENUITEM "Exit" ACTION If( MsgYesNo( "Do you want to end ?" ), oWnd:End(),)
      ENDMENU   
   ENDMENU
   
return oMenu      

function Execute()

   local oHrb, cResult 

   MemoWrit( "_temp.prg", Scripts->Code )
   // FReOpen_Stderr( "comp.log", "w" )
   oHrb = HB_CompileBuf( HB_ARGV( 0 ), "_temp.prg", "-n", "-Ic:\fwh\include", "-Ic:\harbour\include" )
   oResult:SetText( If( Empty( cResult := MemoRead( "comp.log" ) ), "ok", cResult ) )
   hb_HrbRun( oHrb )

return nil

#pragma BEGINDUMP

#include <stdio.h>
#include <hbapi.h>

HB_FUNC( FREOPEN_STDERR )
{
   hb_retnl( ( LONG ) freopen( hb_parc( 1 ), hb_parc( 2 ), stderr ) );
}    

#pragma ENDDUMP
 
Image

Uploaded with ImageShack.us

Re: Scripts for Harbour and FWH !!!

Posted: Tue May 17, 2011 10:49 pm
by Antonio Linares
An enhanced version:

Code: Select all

// Scripting for Harbour and FiveWin only (not supported on xHarbour)

#include "FiveWin.ch"
#include "Splitter.ch"
#include "xbrowse.ch"

static oWnd, oResult

function Main()

   local oBrw, oFont, oMemo, oSplit, oSplit2
   local cCode := '#include "FiveWin.ch"' + CRLF + CRLF + "function Test()" + CRLF + CRLF + '   MsgInfo( "Hello world!" )' + CRLF + CRLF + "return nil"
   local cResult
   
   if ! File( "scripts.dbf" )
      DbCreate( "scripts.dbf", { { "NAME", "C", 20, 0 }, { "DESCRIPT", "C", 100, 0 }, { "CODE", "M", 80, 0 } } )
   endif      
   
   USE scripts
   
   if RecCount() == 0
      APPEND BLANK
      Scripts->Name := "Test"
      Scripts->Descript := "This is a test script"
      Scripts->Code := cCode
   endif   
   
   DEFINE WINDOW oWnd TITLE "Scripts" MENU BuildMenu()
   
   DEFINE BUTTONBAR oBar OF oWnd SIZE 80, 80 2007
   
   DEFINE BUTTON OF oBar PROMPT "New" FILE "..\bitmaps\alphabmp\plus.bmp" TOOLTIP "New" ACTION ( DbAppend(), Scripts->Name := "new script", oBrw:Refresh(), oMemo:SetText( cCode ) )

   DEFINE BUTTON OF oBar PROMPT "Run" FILE "..\bitmaps\alphabmp\task.bmp" TOOLTIP "Run" ACTION Execute()

   DEFINE BUTTON OF oBar PROMPT "Exit"  FILE "..\bitmaps\32x32\quit.bmp" TOOLTIP "Exit" ;
      ACTION If( MsgYesNo( "Do you want to end ?" ), oWnd:End(),)
      
   @ 5.8, 0 XBROWSE oBrw OF oWnd SIZE 450, 500 ;
      FIELDS Scripts->Name, Scripts->Descript ;
      HEADERS "Name", "Description" CELL LINES ;
      ON CHANGE ( oMemo:SetText( Scripts->Code ), oMemo:Refresh() )
   
   oBrw:lFastEdit = .T.
   oBrw:CreateFromCode()   
      
   DEFINE FONT oFont NAME "Courier New" SIZE 0, -14   
      
   @ 6.3, 57 GET oMemo VAR Scripts->Code MEMO OF oWnd SIZE 500, 569 FONT oFont
   
   @ 50.5, 57 GET oResult VAR cResult MEMO OF oWnd SIZE 500, 300 FONT oFont
   
   @ 0, 450 SPLITTER oSplit ;
      VERTICAL _3DLOOK ;
      PREVIOUS CONTROLS oBrw ;
      HINDS CONTROLS oMemo, oResult ; 
      SIZE 4, 200 PIXEL ;
      OF oWnd
      
   @ 650, 460 SPLITTER oSplit2 ;
            HORIZONTAL _3DLOOK ;
            PREVIOUS CONTROLS oMemo ;
            HINDS CONTROLS oResult ;
            SIZE 500, 4  PIXEL ;
            OF oWnd 
   
   DEFINE MSGBAR oMsgBar OF oWnd 2007 PROMPT "Scripting support for your applications"
   
   ACTIVATE WINDOW oWnd MAXIMIZED ;
      ON RESIZE ( oSplit:AdjLeft(), oSplit2:AdjRight() )   
   
return nil

function BuildMenu()

   local oMenu
   
   MENU oMenu
      MENUITEM "Scripts management"
      MENU
         MENUITEM "About..." ACTION MsgAbout( "Scripting support for Harbour and FiveWin", "(c) FiveTech Software 2011" )
         SEPARATOR
         MENUITEM "Exit" ACTION If( MsgYesNo( "Do you want to end ?" ), oWnd:End(),)
      ENDMENU   
   ENDMENU
   
return oMenu      

function Execute()

   local oHrb, cResult 

   FReOpen_Stderr( "comp.log", "w" )
   oHrb = HB_CompileFromBuf( Scripts->Code, "-n", "-Ic:\fwh\include", "-Ic:\harbour\include" )
   oResult:SetText( If( Empty( cResult := MemoRead( "comp.log" ) ), "ok", cResult ) )
   
   if ! Empty( oHrb )
      hb_HrbRun( oHrb )
   endif   

return nil

#pragma BEGINDUMP

#include <stdio.h>
#include <hbapi.h>

HB_FUNC( FREOPEN_STDERR )
{
   hb_retnl( ( LONG ) freopen( hb_parc( 1 ), hb_parc( 2 ), stderr ) );
}    

#pragma ENDDUMP
 

Re: Scripts for Harbour and FWH !!!

Posted: Wed May 18, 2011 12:17 pm
by vilian
Antonio,

This works with resources and user functions ?

Re: Scripts for Harbour and FWH !!!

Posted: Thu May 19, 2011 3:32 am
by Antonio Linares
Yes :-)

Re: Scripts for Harbour and FWH !!!

Posted: Thu May 19, 2011 6:37 am
by Roberto Parisi
It will be great for xHarbour too. But xHarbour don't have CompileFromBuf function :(

Regards,
Roberto Parisi

Re: Scripts for Harbour and FWH !!!

Posted: Thu May 19, 2011 2:32 pm
by Otto
Hello Antonio,

Where is the rc/res file suspected to be?
Thanks in advance
Otto

Re: Scripts for Harbour and FWH !!!

Posted: Thu May 19, 2011 5:48 pm
by Antonio Linares
Otto,

You can directly use resources from a DLL using "SET RESOURCES TO ...", and also use the resources that are included in your original EXE (RC, RES files) :-)

Re: Scripts for Harbour and FWH !!!

Posted: Fri May 20, 2011 10:13 am
by Rimantas
Antonio Linares wrote:This is a powerful example that shows you how to add scripting support into your own applications:
Excuse Antonio , but as I can see , you copied my idea ... :-)

Re: Scripts for Harbour and FWH !!!

Posted: Fri May 20, 2011 1:18 pm
by Antonio Linares
Rimantas,

It is just a Harbour built-in feature, and it is just to explain everybody how to use it :-)

And as far as I remember I helped you to get it working ;-)

Re: Scripts for Harbour and FWH !!!

Posted: Fri May 20, 2011 6:46 pm
by Rimantas
Antonio Linares wrote: It is just a Harbour built-in feature, and it is just to explain everybody how to use it :-)
And as far as I remember I helped you to get it working ;-)
:) ... you are rigth , you helped me . Another question - can scripts be debugged ? I have something like tree of scripts , I can run , edit , save , compile them , but at develope stage it can be comfortable to use debugger with breakpoints in scripts .
How to do something like that ?