FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Scripts for Harbour and FWH !!!
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Scripts for Harbour and FWH !!!
Posted: Tue May 17, 2011 08:25 AM
This is a powerful example that shows you how to add scripting support into your own applications:

scripts.prg
Code (fw): Select all Collapse
// 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


regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Scripts for Harbour and FWH !!!
Posted: Tue May 17, 2011 10:07 AM
Enhanced version:

scripts.prg
Code (fw): Select all Collapse
// 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




Uploaded with ImageShack.us
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Scripts for Harbour and FWH !!!
Posted: Tue May 17, 2011 10:49 PM
An enhanced version:
Code (fw): Select all Collapse
// 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
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1067
Joined: Wed Nov 09, 2005 02:17 AM
Re: Scripts for Harbour and FWH !!!
Posted: Wed May 18, 2011 12:17 PM

Antonio,

This works with resources and user functions ?

Sds,
Vilian F. Arraes
vilian@vfatec.com.br
Belém-Pa-Brazil
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Scripts for Harbour and FWH !!!
Posted: Thu May 19, 2011 03:32 AM

Yes :-)

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 116
Joined: Thu Oct 13, 2005 05:14 PM
Re: Scripts for Harbour and FWH !!!
Posted: Thu May 19, 2011 06:37 AM

It will be great for xHarbour too. But xHarbour don't have CompileFromBuf function :(

Regards,
Roberto Parisi

Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
Re: Scripts for Harbour and FWH !!!
Posted: Thu May 19, 2011 02:32 PM

Hello Antonio,

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

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Scripts for Harbour and FWH !!!
Posted: Thu May 19, 2011 05:48 PM

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) :-)

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 437
Joined: Fri Oct 07, 2005 12:56 PM
Re: Scripts for Harbour and FWH !!!
Posted: Fri May 20, 2011 10:13 AM
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 ... :-)
Rimantas U.
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Scripts for Harbour and FWH !!!
Posted: Fri May 20, 2011 01:18 PM

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 ;-)

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 437
Joined: Fri Oct 07, 2005 12:56 PM
Re: Scripts for Harbour and FWH !!!
Posted: Fri May 20, 2011 06:46 PM
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 ?
Rimantas U.

Continue the discussion