Porting gtk using PROTO explained
Jun 18, 2014 15:36:49 GMT 1
Post by bigbass on Jun 18, 2014 15:36:49 GMT 1
PROTO gtk explained line by line
In order to explain the porting process completely
1.) We need to reference to original source code
this is linux that's why it is called open source code
just providing sources sometimes isn't enough
2.) Walking through every step using a simple demo
helps to get an overview of what's happening
the most important part of any code
is to be able to read it and understand it
and reuse it and finally improve it and
document along the way
This is a simple hello word gtk in C
We will use this as the example to port to BaCon
I know there is a lot of yuk syntax there we will hack out don't worry
C programmers complicate life with pointers and casting
and this is because of the headers
The functions are usually clean though when used in bacon
We will do this in two stages
the first stage will look closer to embedded
gtk but since we won't use headers we have
less syntax to get in our face
we improve on this again and make it look like HUG/BaCon
in the last example
Here we use an ALIAS to explain how HUG syntax came from
having the ALIAS removes the mystery
'--- set up for compiling embedded library GTK
PRAGMA LDFLAGS `pkg-config --cflags --libs gtk+-2.0`
'--- this replaces the need for IMPORT. the TYPE is not needed
'--- place all the functions that you will use here
PROTO gtk_init ALIAS INIT
PROTO gtk_window_new ALIAS WINDOW
PROTO gtk_window_set_title ALIAS TITLE
PROTO g_signal_connect_data ALIAS CALLBACK
PROTO gtk_widget_show_all ALIAS SHOW_ALL
PROTO gtk_main ALIAS DISPLAY
PROTO gtk_main_quit ALIAS QUIT
' get the needed missing values from the header
GTK_WINDOW_TOPLEVEL = 0
'===============
SUB exit_prog
'===============
QUIT
END SUB
INIT(0, 0)
window = WINDOW(GTK_WINDOW_TOPLEVEL)
TITLE(window, "Hello World!")
'--- Connect the main window to the destroy ---> replace with SUB exit_prog
CALLBACK(window, "delete-event", exit_prog, 0, 0, 0)
SHOW_ALL(window)
DISPLAY
=======================================================================
Now explained line by line may take some time and patients to follow
1.)
we don't need it because
the only thing declared in the headers
for this example is
' get the needed missing values
GTK_WINDOW_TOPLEVEL = 0
we just declare it in BaCon to avoid the headers and casting
=======================================================================
2.) remove the yuk this is a SUB since there is no return
here we use the simplified HUG syntax from the ALIAS
'===============
SUB exit_prog
'===============
QUIT
END SUB
=======================================================================
3.)
In BaCon we don't need the main
and since we didn't use the headers
we dont need to declare pointers and types
so in bacon we delete all that part!
however we need to INIT all code starts with this
because we have an alias gtk_init ALIAS INIT we can shorten the code with just
to initialize &argc, &argv to zero much easier to read!
=======================================================================
4.)
in BaCon with the ALIAS it can be simplified
=======================================================================
5.)
In BaCon the ALIAS as just TITLE is simple notice no casting also !
=======================================================================
6.)
In BaCon
=======================================================================
7.)
In BaCon we ALIAS the function
=======================================================================
8.)
In BaCon ALIAS this as DISPLAY
In order to explain the porting process completely
1.) We need to reference to original source code
this is linux that's why it is called open source code
just providing sources sometimes isn't enough
2.) Walking through every step using a simple demo
helps to get an overview of what's happening
the most important part of any code
is to be able to read it and understand it
and reuse it and finally improve it and
document along the way
This is a simple hello word gtk in C
We will use this as the example to port to BaCon
I know there is a lot of yuk syntax there we will hack out don't worry
C programmers complicate life with pointers and casting
and this is because of the headers
The functions are usually clean though when used in bacon
#include <gtk/gtk.h>
void destroy(GtkWidget *widget, gpointer data)
{
gtk_main_quit();
}
int main(int argc, char *argv[])
{
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Hello World!");
gtk_widget_show (window);
/* Connect the main window to the destroy */
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);
gtk_widget_show(window);
gtk_main();
return 0;
}
We will do this in two stages
the first stage will look closer to embedded
gtk but since we won't use headers we have
less syntax to get in our face
we improve on this again and make it look like HUG/BaCon
in the last example
'--- set up for compiling embedded library GTK
PRAGMA LDFLAGS `pkg-config --cflags --libs gtk+-2.0`
'--- this replaces the need for IMPORT. the TYPE is not needed
'--- place all the functions that you will use here
PROTO gtk_init
PROTO gtk_window_new
PROTO gtk_window_set_title
PROTO g_signal_connect_data,
PROTO g gtk_widget_show_all
PROTO gtk_main
PROTO gtk_main_quit
' get the needed missing values
GTK_WINDOW_TOPLEVEL = 0
'===============
SUB exit_prog
'===============
gtk_main_quit
END SUB
gtk_init(0, 0)
window = gtk_window_new(GTK_WINDOW_TOPLEVEL)
gtk_window_set_title(window, "Hello World!")
'--- Connect the main window to the destroy ---> replace with SUB exit_prog
g_signal_connect_data(window, "delete-event", exit_prog, 0, 0, 0)
gtk_widget_show_all(window)
gtk_main
Here we use an ALIAS to explain how HUG syntax came from
having the ALIAS removes the mystery
'--- set up for compiling embedded library GTK
PRAGMA LDFLAGS `pkg-config --cflags --libs gtk+-2.0`
'--- this replaces the need for IMPORT. the TYPE is not needed
'--- place all the functions that you will use here
PROTO gtk_init ALIAS INIT
PROTO gtk_window_new ALIAS WINDOW
PROTO gtk_window_set_title ALIAS TITLE
PROTO g_signal_connect_data ALIAS CALLBACK
PROTO gtk_widget_show_all ALIAS SHOW_ALL
PROTO gtk_main ALIAS DISPLAY
PROTO gtk_main_quit ALIAS QUIT
' get the needed missing values from the header
GTK_WINDOW_TOPLEVEL = 0
'===============
SUB exit_prog
'===============
QUIT
END SUB
INIT(0, 0)
window = WINDOW(GTK_WINDOW_TOPLEVEL)
TITLE(window, "Hello World!")
'--- Connect the main window to the destroy ---> replace with SUB exit_prog
CALLBACK(window, "delete-event", exit_prog, 0, 0, 0)
SHOW_ALL(window)
DISPLAY
=======================================================================
Now explained line by line may take some time and patients to follow
1.)
#include <gtk/gtk.h>
we don't need it because
the only thing declared in the headers
for this example is
' get the needed missing values
GTK_WINDOW_TOPLEVEL = 0
we just declare it in BaCon to avoid the headers and casting
=======================================================================
2.) remove the yuk this is a SUB since there is no return
void destroy(GtkWidget *widget, gpointer data)
{
gtk_main_quit();
}
here we use the simplified HUG syntax from the ALIAS
'===============
SUB exit_prog
'===============
QUIT
END SUB
=======================================================================
3.)
int main(int argc, char *argv[])
{
GtkWidget *window;
gtk_init(&argc, &argv);
In BaCon we don't need the main
and since we didn't use the headers
we dont need to declare pointers and types
so in bacon we delete all that part!
however we need to INIT all code starts with this
because we have an alias gtk_init ALIAS INIT we can shorten the code with just
INIT(0,0)
to initialize &argc, &argv to zero much easier to read!
=======================================================================
4.)
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
in BaCon with the ALIAS it can be simplified
window = WINDOW(GTK_WINDOW_TOPLEVEL)
=======================================================================
5.)
gtk_window_set_title(GTK_WINDOW(window), "Hello World!");
In BaCon the ALIAS as just TITLE is simple notice no casting also !
TITLE(window, "Hello World!")
=======================================================================
6.)
/* Connect the main window to the destroy */
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);
In BaCon
'--- Connect the main window to the destroy ---> replace with SUB exit_prog
CALLBACK(window, "delete-event", exit_prog, 0, 0, 0)
CALLBACK(window, "delete-event", exit_prog, 0, 0, 0)
=======================================================================
7.)
gtk_widget_show_all(window)
In BaCon we ALIAS the function
SHOW_ALL(window)
=======================================================================
8.)
gtk_main
In BaCon ALIAS this as DISPLAY
DISPLAY