Anyone able to convert this to FreeBASIC?

General FreeBASIC programming questions.
Post Reply
Cretin Ho
Posts: 182
Joined: Feb 04, 2021 13:01

Anyone able to convert this to FreeBASIC?

Post by Cretin Ho »

The concept behind this is interesting: https://github.com/webview/webview

Anyone interested to convert webview.h to webview.bi? Please give if a try. It has an example for plain C but the header itself I think is C++.
Cretin Ho
Posts: 182
Joined: Feb 04, 2021 13:01

Re: Anyone able to convert this to FreeBASIC?

Post by Cretin Ho »

Working header for FreeBASIC:

webview_c.bi

Code: Select all

#pragma once

extern "C"

type webview_t as any ptr
declare function webview_create(byval debug as long, byval window as any ptr) as webview_t
declare sub webview_destroy(byval w as webview_t)
declare sub webview_run(byval w as webview_t)
declare sub webview_terminate(byval w as webview_t)
declare sub webview_dispatch(byval w as webview_t, byval fn as sub(byval w as webview_t, byval arg as any ptr), byval arg as any ptr)
declare function webview_get_window(byval w as webview_t) as any ptr
declare sub webview_set_title(byval w as webview_t, byval title as const zstring ptr)

const WEBVIEW_HINT_NONE = 0
const WEBVIEW_HINT_MIN = 1
const WEBVIEW_HINT_MAX = 2
const WEBVIEW_HINT_FIXED = 3

declare sub webview_set_size(byval w as webview_t, byval width as long, byval height as long, byval hints as long)
declare sub webview_navigate(byval w as webview_t, byval url as const zstring ptr)
declare sub webview_init(byval w as webview_t, byval js as const zstring ptr)
declare sub webview_eval(byval w as webview_t, byval js as const zstring ptr)
declare sub webview_bind(byval w as webview_t, byval name as const zstring ptr, byval fn as sub(byval seq as const zstring ptr, byval req as const zstring ptr, byval arg as any ptr), byval arg as any ptr)
declare sub webview_return(byval w as webview_t, byval seq as const zstring ptr, byval status as long, byval result as const zstring ptr)

end extern
A test program:

test.bas

Code: Select all

#include once "webview_c.bi"

#ifndef NULL
const NULL as any ptr = 0
#endif

dim w as webview_t
w = webview_create(0, NULL)
webview_set_title(w, "Webview Example")
webview_set_size(w, 480, 320, WEBVIEW_HINT_NONE)
webview_navigate(w, "https://en.m.wikipedia.org/wiki/Main_Page")
webview_run(w)
webview_destroy(w)
Compile fine, but failed to link since there is no shared library on Linux:

Code: Select all

$ fbc test.bas
test.o: In function `main':
test.c:(.text+0x3f): undefined reference to `webview_create'
test.c:(.text+0x5e): undefined reference to `webview_set_title'
test.c:(.text+0x79): undefined reference to `webview_set_size'
test.c:(.text+0x8c): undefined reference to `webview_navigate'
test.c:(.text+0x98): undefined reference to `webview_run'
test.c:(.text+0xa4): undefined reference to `webview_destroy'
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Anyone able to convert this to FreeBASIC?

Post by caseih »

There's no library here. webview_create() is defined in webview.h It's a C wrapper over a C++ call. In essence you have to compile that part with C++ to make an object file that you can use from FB. Should be relatively easy to do that:

Code: Select all

g++ -c webview.cc `pkg-config --cflags webkit2gtk-4.0`
Then use the resulting webview.o file when you're compiling your FB code. Although you'll need to manually tell FB to link in the shared libraries from webkit2gtk and its dependencies. Probably like this:

Code: Select all

/opt/FreeBASIC/bin/fbc test.bas webview.o -l webkit2gtk-4.0 -l gtk-3 -l gdk-3 -l pangocairo-1.0 -l pango-1.0 -l harfbuzz -l atk-1.0 -l cairo-gobject -l cairo -l gdk_pixbuf-2.0 -l soup-2.4 -l gio-2.0 -l javascriptcoregtk-4.0 -l gobject-2.0 -l glib-2.0 -l stdc++ 
It would be nice if FBC would support GCC-style library linker flags then you could use pkg-config directly in the linking step.
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Anyone able to convert this to FreeBASIC?

Post by coderJeff »

Moved to General Forum
Post Reply