Adding self-defined widgets to HUG
May 13, 2012 19:20:37 GMT 1
Post by Pjot on May 13, 2012 19:20:37 GMT 1
All,
Sometimes I receive requests to add such-and-such widget to HUG. But you can do this yourself, with a little knowledge of GTK. Inspired by the tutorials of bigbass, I thought it would be a good idea to explain how HUG can be expanded with additional widgets.
HUG has two functions, REGISTER and METHOD, which provide a convenient extension interface.
For this small tutorial we'll look at the GtkPlug/GtkSocket widgets. These are very simple widgets and therefore very suitable for this explanation.
The idea is to create a new INCLUDE file which should be added to your programs. Of course the new GTK functions must be imported first. Then, for the 'socket' widget, we will create a SOCK function to create the socket, and we'll create GET function to get the windowID to which it is attached. For the 'plug' widget we will create a PLUG function to create the plug, and a SET function to assign some widget to the plug.
So this is how our new INCLUDE file looks like. Let's call it 'sock_plug.bac'. Obviously, some knowledge about how the widgets work on GTK level is assumed. The code contains a lot of comments which explain each step.
Now we have our new INCLUDE file ready with the additional widgets. How should they be used?
First the program defining the socket. Note that it first includes HUG, and it after that includes our new widgets.
Finally, the program which defines the plug.
Now, start the socket program first and after that the plug program. The result will be that the MARK of the plug program is shown in the window of the socket program.
Regards
Peter
Sometimes I receive requests to add such-and-such widget to HUG. But you can do this yourself, with a little knowledge of GTK. Inspired by the tutorials of bigbass, I thought it would be a good idea to explain how HUG can be expanded with additional widgets.
HUG has two functions, REGISTER and METHOD, which provide a convenient extension interface.
For this small tutorial we'll look at the GtkPlug/GtkSocket widgets. These are very simple widgets and therefore very suitable for this explanation.
The idea is to create a new INCLUDE file which should be added to your programs. Of course the new GTK functions must be imported first. Then, for the 'socket' widget, we will create a SOCK function to create the socket, and we'll create GET function to get the windowID to which it is attached. For the 'plug' widget we will create a PLUG function to create the plug, and a SET function to assign some widget to the plug.
So this is how our new INCLUDE file looks like. Let's call it 'sock_plug.bac'. Obviously, some knowledge about how the widgets work on GTK level is assumed. The code contains a lot of comments which explain each step.
' Import GtkSock/GtkPlug functions
gtk$ = HUGLIB$("gtk")
IMPORT "gtk_socket_new(void)" FROM gtk$ TYPE long
IMPORT "gtk_socket_get_id(long)" FROM gtk$ TYPE long
IMPORT "gtk_plug_new(long)" FROM gtk$ TYPE long
' The self-defined GET for the SOCK
FUNCTION GET_winid(NUMBER widget)
LOCAL winid
' Get the windowID from the socket
winid = gtk_socket_get_id(widget)
RETURN winid
END FUNCTION
' Function to create the SOCKET
FUNCTION SOCK(int hug_xsize, int hug_ysize)
LOCAL widget
' Create the socket
widget = gtk_socket_new()
' Register and determine size and signal
REGISTER(widget, hug_xsize, hug_ysize, "plug-added", 0, 0, 0, 0)
' Determine the GET method
METHOD(widget, 3, GET_winid)
' Return the created socket
RETURN widget
END FUNCTION
' The self-defined SET for the PLUG
SUB SET_widget_to_plug(NUMBER plug_widget, NUMBER gtk_widget)
gtk_container_add(plug_widget, gtk_widget)
ENDSUB
' Function to create the plug
FUNCTION PLUG(NUMBER window)
LOCAL widget
' Create the plug
widget = gtk_plug_new(window)
' Register and determine signal (no need for size)
REGISTER(widget, 0, 0, "embedded", 0, 0, 0, 0)
' Determine the SET method
METHOD(widget, 4, SET_widget_to_plug)
' Return the created plug
RETURN widget
END FUNCTION
Now we have our new INCLUDE file ready with the additional widgets. How should they be used?
First the program defining the socket. Note that it first includes HUG, and it after that includes our new widgets.
' Get HUG functions
INCLUDE "hug.bac"
' Get our self-defined widgets
INCLUDE "sock_plug.bac"
' Callback for the socket
SUB msg
PRINT "The plug was inserted!"
END SUB
' Create the main window
win = WINDOW("Socket window", 400, 400)
' Create the socket
sock = SOCK(150, 30)
' Add socket to window and setup callback
ATTACH(win, sock, 10, 10)
CALLBACK(sock, msg)
' Get the windowID from the socket
winid = GET(sock)
' Save windowID to a file so the plug can read it
OPEN "/tmp/winid.txt" FOR WRITING AS comm
WRITELN winid TO comm
CLOSE FILE comm
' Endless GTK loop
DISPLAY
Finally, the program which defines the plug.
' Get HUG functions
INCLUDE "hug.bac"
' Get our self-defined widgets
INCLUDE "sock_plug.bac"
' Read winid from a file
OPEN "/tmp/winid.txt" FOR READING AS comm
READLN winid$ FROM comm
CLOSE FILE comm
' Create a widget, has same size as socket
txt = MARK("Hello from the plug!", 150, 30)
' Create the plug
plug = PLUG(VAL(winid$))
' Assign the widget to the plug
SET(plug, txt)
' Make sure the plug is visible
SHOW(plug)
' Endless GTK loop
DISPLAY
Now, start the socket program first and after that the plug program. The result will be that the MARK of the plug program is shown in the window of the socket program.
Regards
Peter