Re: HB_WebView (a cross-platform Webview library)
Posted: Sat Mar 22, 2025 5:40 pm
Hi Serge,serge_girard wrote: ↑Sat Mar 22, 2025 10:41 am Claudio,
Here is a small example (not tested or compiled !) but it gives an idea!
Many thanks, Serge
Code: Select all
#include "hmg.ch" FUNCTION MAIN() /***************/ PUBLIC dbo PUBLIC dbname PUBLIC host PUBLIC user PUBLIC password PUBLIC cVersion PUBLIC aRESULT := {} IF SQL_Connect() cQuery1a := " SELECT SOME_FIELD " cQuery1a += " FROM SOME_TABLE " cQuery1a += " WHERE SOME_REC_NO = 1 " cSQL1 := cQuery1a ? cSQL1 cQuery1a := dbo:Query( cQuery1a ) IF cQuery1a:NetErr() MSGINFO( 'SQL DEMO ' + UPPER(cQuery1a:Error())) ENDIF FOR i := 1 to cQuery1a:Lastrec() aCurRowa := cQuery1a:GetRow(i) cNAAM := Strvalue(aCurRow:fieldGet(1)) AADD(aRESULT, {cNAAM } ) NEXT SQL_Disconnect() ? ? ELSE ? 'NO CONNECTION' ENDIF DEFINE WINDOW Form_1 ; AT 0,0 ; WIDTH 1200 ; HEIGHT 500 ; TITLE ''; MAIN ; FONT 'Arial' SIZE 15 ; @ 10,10 GRID Grid_ZF ; WIDTH 1150 ; HEIGHT 450 ; HEADERS {'SOME_FIELD'}; WIDTHS {250 }; JUSTIFY { GRID_JTFY_LEFT } ; FONT "Arial" SIZE 09 ; iTEMS aRESULT END WINDOW CENTER WINDOW Form_1 ACTIVATE WINDOW Form_1 FUNCTION SQL_Connect() /***********************/ dbname := 'dbname' host := 'host.db.host.xx' user := 'dbname_user' password := 'secret_password' dbo := tmysqlserver():new(AllTrim(host), AllTrim(user), AllTrim(password)) IF dbo:NetErr() ?'R1', dbo:ERROR() RETURN nil ENDIF IF!EMPTY(dbname) dbo:selectdb(dbname) IF dbo:NetErr() ?'R2', dbo:ERROR() RETURN nil ENDIF ENDIF cVersion := dbo:sql_Version() ? 'cVersion', cVersion RETURN dbo FUNCTION SQL_Disconnect() /************************/ dbo:Destroy() RETURN
I have slightly modified your code, adding the SQL_GetData() function, with two versions, one for a real SQL connection and another for a simulated connection for testing (see comments in the code).
Code: Select all
/*
* HB_WebView Demo
*
* Copyright 2025 by Dr. Claudio Soto (from Uruguay).
* mail: <srvet@adinet.com.uy>
* blog: http://srvet.blogspot.com
*/
#include "../hb_webview-lib/hb_webview.ch"
// #include "hb_webview-lib/hb_webview.prg"
REQUEST hb_alert
MEMVAR dbo, dbname, host, user, password, cVersion
FUNCTION Main
LOCAL w, html, allowDevTools
LOCAL context
PUBLIC dbo, dbname, host, user, password, cVersion
// aRESULT = SQL_GetData() // returns an array with subarray like: { { data1 }, { data2 }, ... , { dataN } }
html = ''
html += '<div>'
html += ' <p>Press CTRL+SHIFT+I to open the DevTools window</p>'
html += ' <button onclick="disp()"> SQL_GetData() </button>'
html += ' <pre id="idResult"></pre>'
html += '</div>'
html += '<script>'
html += ' async function disp(){'
html += ' let arrayData = await SQL_GetData();'
html += ' console.log( arrayData );'
html += ' let txt = "";'
html += ' for(let i=0; i < arrayData.length; i++)'
html += ' txt += "Row "+ i + ": " + arrayData[ i ].toString() + "\n";'
html += ' document.getElementById("idResult").textContent = "SQL Response: \n" + txt;'
html += ' }'
html += '</script>'
allowDevTools = .T.
w = hb_webview_create( allowDevTools, NIL )
hb_webview_set_size(w, 480, 320, WEBVIEW_HINT_NONE)
hb_webview_set_title(w, "Harbour WebView - SQL Demo")
/*
Remember that JavaScript is case sensitive:
If you bind the HB_ALERT() function as "hb_Alert" in the Harbour environment,
you must call it as hb_Alert() in JavaScript environment.
On the other hand, Harbour binding functions are passed as promises to JavaScript.
Therefore, they are executed in the JavaScript environment as asynchronous functions.
For more details on promises in JavaScript, see:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
For promises to execute synchronously, you must chain the calls in chained .then() statements
or call them with the await statement inside an asynchronous function.
For more details see:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await
*/
context = hb_webview_bind2(w, "SQL_GetData", HB_CALL_FUNC_DIRECT)
hb_webview_set_html(w, html)
hb_webview_run(w)
hb_webview_destroy_context( context )
hb_webview_terminate( w )
RETURN NIL
//============================================================================================
// Warning, this is a simulate response, please comment out this line in a real SQL connection.
#define SIMULATE_SQL
//============================================================================================
#ifdef SIMULATE_SQL
FUNCTION SQL_GetData()
LOCAL aRESULT := {}
aRESULT := Simulate_SQL_GetData_Response()
RETURN aRESULT // returns an array with subarray like: { { data1 }, { data2 }, ... , { dataN } }
FUNCTION Simulate_SQL_GetData_Response()
LOCAL aRESULT := {}
AADD( aRESULT, { "data1 A", "data1 B" } )
AADD( aRESULT, { "data2" } )
AADD( aRESULT, { "data3" } )
RETURN aRESULT
#else
FUNCTION SQL_Connect()
/***********************/
dbname := 'dbname'
host := 'host.db.host.xx'
user := 'dbname_user'
password := 'secret_password'
dbo := tmysqlserver():new(AllTrim(host), AllTrim(user), AllTrim(password))
IF dbo:NetErr()
?'R1', dbo:ERROR()
RETURN nil
ENDIF
IF!EMPTY(dbname)
dbo:selectdb(dbname)
IF dbo:NetErr()
?'R2', dbo:ERROR()
RETURN nil
ENDIF
ENDIF
cVersion := dbo:sql_Version()
? 'cVersion', cVersion
RETURN dbo
FUNCTION SQL_Disconnect()
/************************/
dbo:Destroy()
RETURN NIL
FUNCTION SQL_GetData()
LOCAL cQuery1a, i, aCurRow, cNAAM, cSQL1
LOCAL aRESULT := {}
IF SQL_Connect()
cQuery1a := " SELECT SOME_FIELD "
cQuery1a += " FROM SOME_TABLE "
cQuery1a += " WHERE SOME_REC_NO = 1 "
cSQL1 := cQuery1a
? cSQL1
cQuery1a := dbo:Query( cQuery1a )
IF cQuery1a:NetErr()
MSGINFO( 'SQL DEMO ' + UPPER(cQuery1a:Error()))
ENDIF
FOR i := 1 to cQuery1a:Lastrec()
aCurRow := cQuery1a:GetRow(i)
cNAAM := Strvalue(aCurRow:fieldGet(1))
AADD( aRESULT, { cNAAM } )
NEXT
SQL_Disconnect()
?
?
ELSE
? 'NO CONNECTION'
ENDIF
RETURN aRESULT // returns an array with subarray like: { { data1 }, { data2 }, ... , { dataN } }
#endif
Code: Select all
call ..\build_hb_cpp.bat demo_SQL.prg -w2 -es2 -lshlwapi demo.hbc