Harbour C Extensions Xhgtk¶
Prikazaću najinteresantnije dijelove, da bih se upoznali sa načinom kako je napravljen binding prema gtk/gdk/cairo bibilotekama:
general.c¶
...
HB_FUNC( GTK_MAIN )
{
gtk_main();
}
HB_FUNC( GTK_INIT )
{
if (! initialized)
{
initialized = TRUE;
gtk_init(NULL,NULL);
}
}
HB_FUNC( GTK_MAIN_QUIT )
{
GList *list;
gchar *role = NULL;
list = gtk_window_list_toplevels();
list = g_list_first(list);
while (list)
{
if (GTK_IS_WINDOW(list->data))
{
role = g_strdup(gtk_window_get_role( GTK_WINDOW(list->data) ));
if (role && strcmp(role, "window") == 0)
gtk_widget_destroy( GTK_WIDGET(list->data) );
g_free(role);
}
list = g_list_next(list);
}
g_list_free(list);
gtk_main_quit();
}
...
general.c xhgtk_locale_to_utf8 , xhgtk_locale_from_utf8¶
gtk, glib-doc: file:///usr/share/gtk-doc/html/glib/glib-Character-Set-Conversion.html#g-locale-to-utf8
g_locale_to_utf8 ():- Converts a string which is in the encoding used for strings by the C runtime (usually the same as that used by the operating system) in the current locale into a UTF-8 string.
- Converts a string which is in the encoding used by GLib for filenames into a UTF-8 string. Note that on Windows GLib uses UTF-8 for filenames; on other platforms, this function indirectly depends on the current locale.
Locale (glib)¶
A number of interfaces in GLib depend on the current locale in which an application is running. Therefore, most GLib-using applications should call setlocale (LC_ALL, "") to set up the current locale.
On Windows, in a C program there are several locale concepts that not necessarily are synchronized. On one hand, there is the system default ANSI code-page, which determines what encoding is used for file names handled by the C library's functions and the Win32 API. (We are talking about the "narrow" functions here that take character pointers, not the "wide" ones.)
On the other hand, there is the C library's current locale. The character set (code-page) used by that is not necessarily the same as the system default ANSI code-page. Strings in this character set are returned by functions like strftime.
gchar * xhgtk_locale_to_utf8( const gchar *szLocale )
{
gchar *szText;
GError *error = NULL;
if (! g_utf8_validate(szLocale, -1, NULL))
if (strlen(sLocale) > 0)
szText = g_convert(szLocale, -1, "UTF-8", sLocale, NULL, NULL, &error );
else
szText = g_locale_to_utf8(szLocale, -1, NULL, NULL, NULL); <<<<<<<<<<<<<<<<<< g_locale_to_utf8 glib c funkcija
else
szText = g_strdup( szLocale );
if (error != NULL)
{
g_print("UTF-8 convertion failed !");
g_error_free(error);
}
return szText;
}
gchar * xhgtk_locale_from_utf8( const gchar *szLocale )
{
gchar *szText;
GError *error = NULL;
if (strlen(sLocale) > 0)
szText = g_convert(szLocale, -1, sLocale, "UTF-8", NULL, NULL, &error );
else
szText = g_locale_from_utf8( szLocale, -1, NULL, NULL, NULL); <<<<<<<<<<<<<<<<<< g_locale_from_utf8 glib c funkcija
if (error != NULL)
{
g_print("UTF-8 convertion failed !");
g_error_free(error);
}
return szText;
}
primjetimo da se radi o dvije c utility funkcije koje interno koristi xhgtk, a nikada direktno harbour applikacija.
gmain.c¶
#include "xhgtk.h"
HB_FUNC( G_MAIN_LOOP_NEW )
{
hb_retptr( (GMainLoop *) g_main_loop_new( (GMainContext *) hb_parptr(1), hb_parl(2)) );
}
HB_FUNC( G_MAIN_LOOP_RUN )
{
g_main_loop_run( (GMainLoop *) hb_parptr(1) );
}
HB_FUNC( G_MAIN_LOOP_QUIT )
{
g_main_loop_quit( (GMainLoop *) hb_parptr(1) );
}
HB_FUNC( G_MAIN_LOOP_UNREF )
{
g_main_loop_unref( (GMainLoop *) hb_parptr(1) );
}
HB_FUNC( G_MAIN_LOOP_IS_RUNNING )
{
hb_retl( g_main_loop_is_running( (GMainLoop *) hb_parptr(1) ) );
}
TWindow.prg: METHOD Activate() CLASS TWindow¶
METHOD Activate() CLASS TWindow
....
if ::Main
hMainWindow := ::handle
gtk_signal_connect(::handle, "destroy", {|| lMain := .F., gtk_main_quit()})
gtk_main()
elseif ::Loop
gtk_signal_connect(::handle, "destroy", {|| g_main_loop_quit(::hMainLoop)})
::hMainLoop := g_main_loop_new(NIL, FALSE)
g_main_loop_run(::hMainLoop)
g_main_loop_unref(::hMainLoop)
::hMainLoop = NIL
endif
RETURN nil
primjer: xhgtk_font_dialog¶
#include "xhgtk.h"
HB_FUNC( XHGTK_FONT_DIALOG )
{
GtkWidget *dialog;
char *fontname;
gchar *szText = xhgtk_locale_to_utf8( hb_parcx(1) );
dialog = gtk_font_selection_dialog_new(szText);
gtk_window_set_role(GTK_WINDOW(dialog), "window" );
gtk_dialog_run(GTK_DIALOG (dialog));
fontname = gtk_font_selection_dialog_get_font_name (GTK_FONT_SELECTION_DIALOG(dialog));
hb_retc(fontname); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< hb_retc ( char * => harbour string )
g_free(fontname);
g_free(szText);
gtk_widget_destroy(dialog);
}
gtk/gtklabel.c¶
/* GtkWidget* gtk_label_new (const char *str); */
HB_FUNC( GTK_LABEL_NEW )
{
gchar *szText = xhgtk_locale_to_utf8(hb_parcx(1)); <<<<<<<<<< uzmi prvi parametar iz harbour-a: hb_parcx(1)
hb_retptr( ( GtkWidget * ) gtk_label_new(szText) ); <<<<<<<<<< vrati pointer na novokreirani objekat: hb_retptr( c_object );
g_free(szText);
}
/* void gtk_label_set_text (GtkLabel *label, const char *str); */
HB_FUNC( GTK_LABEL_SET_TEXT )
{
gchar *szText = xhgtk_locale_to_utf8 (hb_parcx(2)); <<<<<<< drugi parametar je utf string (gchar *)
gtk_label_set_text( ( GtkLabel * ) hb_parptr(1), szText ); <<<<<<< prvi parametar je pointer na gtk_label objekat
g_free(szText);
}