Vi riporto questa discussione apparsa nella M.L. ufficiale:
" Hi all
I currently helping to port a VB6 program to Gambas3, but to my
surprise, it does weird things like these from the post title: it is
calling methods and functions from the html code inside a webbrowser
control.
It has a wb control that acts like a sort of wizard/help thing, and the
html links in that view are calling methods inside the application. That links are as
'<a href="act:some_class.method">some action</a>
'
So the question is, could it be done with gambas somehow?
Regards
--
Jesus Guardon "
" Jesus (meant as an interjection)! I know I shouldn't question other people's
design choices but this is just mad. Actually it might have seemed like an
elegant way to communicate with an HTML page to the original author of that
program - I understand that - but this is just asking for trouble, IMHO.
However, I guess Object.Call() is your best bet here.
Regards,
Tobi "
" Hi Tobi, thanks for reply
I am at your side about some (mad) practices, but...
I think Object.Call is suitable for calling or executing code inside or
outside of the current class, but not the other way round, IIUC. How
could I execute Object.Call from inside a webview? None of the events
webview exposes are suitable for doing this IMO, since the webview.url
is a property, not a method.
I will experiment, thanks for the tip!
Regards
--
Jesus Guardon "
" Ahh, I got you wrong apparently... My answer can be applied if you have any
occasion (event) to parse an (X)HTML element for this special attribute
href="act:some_class.method". Then you can extract the class and method name and use Object.Call().
You can (ab)use the WebView's Click event like this: Modify the <a> element to look like this:
<a href="#act:some_class.method">some action</a>
Then, when clicked, the WebView will load the valid URL (the anchor sign had
to be prepended to make it valid - to trigger the Click event, in turn) and
provide you a WebFrame object in whose Url property you have the wanted
act:some_class.method string somewhere. Now parse and execute.
I think it's too late for me to find clear words, so I attach a minimal
example. (vedi allegato)
(Note that the code is executed twice in this project which is
certainly not desirable. You'll sure find a way to prevent this yourself.)
Regards,
Tobi "
" Many thanks, Tobi
I think this is a good starting point. Just hope I will not get crazy
after that. Now I'm going to sleep and tomorrow I will give it a fresh (re)view.
Regards
--
Jesus Guardon "
...potremmo modificare quel codice, eliminando la Classe secondaria e ponendo così la sub-routine all'interno della Classe principale.
Private wv As WebView
Public Sub Form_Open()
Crea_html()
With Me
.W = 300
.H = 300
End With
With wv = New WebView(Me) As "Wview"
.X = 0
.Y = 0
.W = Me.W
.H = Me.H
End With
wv.Url = "/tmp/html.html"
End
Public Sub Wview_Click(hFrame As WebFrame)
Dim aSpecial, aModuleMethod As String[]
If hFrame.Url Like "*#act:*" Then
Object.Call(FMain, "Da_Richiamare", ["Testo qualsiasi", 123456])
Endif
End
Public Sub Da_Richiamare(testo As String, numero As Integer)
Print testo, numero
End
Private Procedure Crea_html()
File.Save("/tmp/html.html", "<HTML>\n <BODY>\n\n" &
" <a href=\"#act:FMain.Da_Richiamare\">Click me!</a>\n\n" &
" </body>\n</html>")
End
Un'altra novità è che il Metodo "Object.Call( )", che chiama la sub-routine, le passa anche due argomenti.
Volendo poi inserire due o più collegamenti sul WebView:
Private wv As WebView
Public Sub Form_Open()
Crea_html()
With Me
.W = 300
.H = 300
End With
With wv = New WebView(Me) As "Wview"
.X = 0
.Y = 0
.W = Me.W
.H = Me.H
End With
wv.Url = "/tmp/html.html"
End
Public Sub Wview_Click(hFrame As WebFrame)
Dim aSpecial, aModuleMethod As String[]
Dim ss As String[]
ss = ["Chimata sub-routine 'Secondo()'"]
If hFrame.Url Like "*#act:*" Then
aSpecial = Split(hFrame.Url, "#")
aModuleMethod = Scan(aSpecial[aSpecial.Max], "act:*.*")
Select Case aModuleMethod[1]
Case "Primo"
Object.Call(Classes[aModuleMethod[0]].Instance, aModuleMethod[1])
Case "Secondo"
Object.Call(Classes[aModuleMethod[0]].Instance, aModuleMethod[1], ss)
End Select
Endif
End
Public Sub Primo()
Print "Chimata sub-routine 'Primo()'"
End
Public Sub Secondo(s As String)
Print s
End
Private Procedure Crea_html()
File.Save("/tmp/html.html", "<HTML>\n <BODY>\n\n" &
" <a href=\"#act:FMain.Primo\">Click me 1 !</a>\n" &
"<P><a href=\"#act:FMain.Secondo\">Click me 2 !</a></p>\n\n" &
" </body>\n</html>")
End