GTK window, updated by GLib threads

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

GTK window, updated by GLib threads

Post by TJF »

Image

This little example is about a GTK window updated by two GLib threads. Each thread executes a time-based counter in an individual speed and shows the result in the main window. In parallel the buttons can be clicked and the main program evaluates the user action.

Calling GDK and GTK+ functions in a thread may cause race conditions. That's why the functions gdk_thread_enter() and gdk_thread_leave() are used.

The GUI is designed with the Glade3 GUI designer and saved as a GUI-XML-file. This file is used by GladeToBac to auto-create the FB source code, which is extended by individual features. GladeToBac generates several source files to ensure that further extensions or changes can be done without manual rework.

To compile this example the following file structure is neccessary:

Code: Select all

project -+-tobac -+- wwt_tobac.bas
         |        +- on_ButL_clicked.bas
         |        +- on_ButR_clicked.bas
         |
         +- wwt.bas
         +- wwt.ui
Find the source code on the original project page:
AGS
Posts: 1284
Joined: Sep 25, 2007 0:26
Location: the Netherlands

Re: GTK window, updated by GLib threads

Post by AGS »

I got it to work but not without some trouble. Apparently g_thread_init and g_thread_create are deprecated. g_thread_init and g_thread_create were not in the gtk binaries (at least not in version 2.24). And the *__ thingies did not work either.

I cut out the call to g_thread_init (according to the gtk+ manual I could) and changed g_thread_init to g_thread_init_new.

I had to remove the *__ (no doubt __ is used by gettext) for the code would not compile with those *__ in it.

Just for fun I added one line to each of the button definitions (to add shortcuts):

Code: Select all

<property name="use_underline">True</property>
and put a _ somewhere in the button labels (_button1 and b_utton2).

I had to use gtk+ version 2.24 or the program would not run.

External dependencies/platform differences make it hard to get gtk+ examples to compile/run. I can always get your gtk+ examples to compile but almost never without having to change some part of the example. Things that 'just work' on Linux sometimes don't 'just work' on Windows. Don't know why not (your code takes into account platform differences).

I guess it's just the way things are (as long as I can get programs to compile I don't mind a little bit of editing here-and-there).
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: GTK window, updated by GLib threads

Post by TJF »

Thanks for feedback! You tested on windows - which version?
AGS wrote:External dependencies/platform differences make it hard to get gtk+ examples to compile/run. I can always get your gtk+ examples to compile but almost never without having to change some part of the example. Things that 'just work' on Linux sometimes don't 'just work' on Windows. Don't know why not (your code takes into account platform differences).

I guess it's just the way things are (as long as I can get programs to compile I don't mind a little bit of editing here-and-there).
Get it running anyway is important. Then we can learn.

I also had trouble with this example: I tested on LINUX and published the code. Then I tested on win XP SP2 and had lots of trouble. While libgthread was loading on LINUX I had to add #INCLIB "gthread" for win32 in to the glib.bi file. I also had to fix a bug in the original gthread.h headers: g_thread_create() is declared as a function there. But it should be a macro wrapper for g_thread_create_full().
AGS wrote:Apparently g_thread_init and g_thread_create are deprecated. g_thread_init and g_thread_create were not in the gtk binaries (at least not in version 2.24).
Since GLib-2.32 g_thread_create() is replaced by g_thread_new(). And g_thread_init() is only needed if you have more than one thread, so in this example it should be called. But the deprecated function should still work. And GTK+-2.24 should go with GLib-2.30, so both functions should be available anyway. Did you use the latest header update (2012-05-24)?
AGS wrote:I had to remove the *__ (no doubt __ is used by gettext) for the code would not compile with those *__ in it.
Yes, it's declared in libintl.bi and it's for I18N. It's a shortcut:

Code: Select all

#DEFINE __(_T_) gettext(_T_)
There's a libintl.bi version from sir_mud that doesn't include this abbreviation (and has another downside - cant remember which ATM, maybe interaction with locale.bi ?!?).

Here's the code I use (.../freebasic/inc/libintl.bi):

Code: Select all

' This is file libintl.bi
' (FreeBasic binding for libintl version 0.18)
'
' (C) 2011-2012 Thomas[ dot ]Freiherr[ at ]gmx[ dot ]net
' translated with help of h_2_bi.bas
' (http://www.freebasic-portal.de/downloads/ressourcencompiler/h2bi-bas-134.html)
'
' Licence:
' This library binding is free software; you can redistribute it
' and/or modify it under the terms of the GNU Lesser General Public
' License as published by the Free Software Foundation; either
' version 2 of the License, or (at your option) ANY later version.
'
' This binding is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
' Lesser General Public License for more details, refer to:
' http://www.gnu.org/licenses/lgpl.html
'
'
' Original license text:
'
'/* Message catalogs for internationalization.
   'Copyright (C) 1995-2002, 2004, 2005 Free Software Foundation, Inc.
   'This file is part of the GNU C Library.
   'This file is derived from the file libgettext.h in the GNU gettext package.

   'The GNU C Library is free software; you can redistribute it and/or
   'modify it under the terms of the GNU Lesser General Public
   'License as published by the Free Software Foundation; either
   'version 2.1 of the License, or (at your option) any later version.

   'The GNU C Library is distributed in the hope that it will be useful,
   'but WITHOUT ANY WARRANTY; without even the implied warranty of
   'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   'Lesser General Public License for more details.

   'You should have received a copy of the GNU Lesser General Public
   'License along with the GNU C Library; if not, write to the Free
   'Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   '02111-1307 USA.  */

#IFDEF __FB_WIN32__
 #PRAGMA push(msbitfields)
 #INCLIB "iconv"
 #INCLIB "intl"
#ENDIF

#INCLUDE ONCE "crt/locale.bi"

EXTERN "C" ' (h_2_bi -P_oCD option)

' 001 start from: libintl.h2bi ==> libintl.h

#IFNDEF _LIBINTL_H
#DEFINE _LIBINTL_H 1

' file not found: features.h

#DEFINE __USE_GNU_GETTEXT 1
#DEFINE __GNU_GETTEXT_SUPPORTED_REVISION(major) IIF((major)= 0 , 1 , -1)

DECLARE FUNCTION gettext(BYVAL AS CONST ZSTRING PTR) AS ZSTRING PTR
DECLARE FUNCTION dgettext(BYVAL AS CONST ZSTRING PTR, BYVAL AS CONST ZSTRING PTR) AS ZSTRING PTR
DECLARE FUNCTION __dgettext(BYVAL AS CONST ZSTRING PTR, BYVAL AS CONST ZSTRING PTR) AS ZSTRING PTR
DECLARE FUNCTION dcgettext(BYVAL AS CONST ZSTRING PTR, BYVAL AS CONST ZSTRING PTR, BYVAL AS INTEGER) AS ZSTRING PTR
DECLARE FUNCTION __dcgettext(BYVAL AS CONST ZSTRING PTR, BYVAL AS CONST ZSTRING PTR, BYVAL AS INTEGER) AS ZSTRING PTR
DECLARE FUNCTION ngettext(BYVAL AS CONST ZSTRING PTR, BYVAL AS CONST ZSTRING PTR, BYVAL AS ULONG) AS ZSTRING PTR
DECLARE FUNCTION dngettext(BYVAL AS CONST ZSTRING PTR, BYVAL AS CONST ZSTRING PTR, BYVAL AS CONST ZSTRING PTR, BYVAL AS ULONG) AS ZSTRING PTR
DECLARE FUNCTION dcngettext(BYVAL AS CONST ZSTRING PTR, BYVAL AS CONST ZSTRING PTR, BYVAL AS CONST ZSTRING PTR, BYVAL AS ULONG, BYVAL AS INTEGER) AS ZSTRING PTR
DECLARE FUNCTION textdomain(BYVAL AS CONST ZSTRING PTR) AS ZSTRING PTR
DECLARE FUNCTION bindtextdomain(BYVAL AS CONST ZSTRING PTR, BYVAL AS CONST ZSTRING PTR) AS ZSTRING PTR
DECLARE FUNCTION bind_textdomain_codeset(BYVAL AS CONST ZSTRING PTR, BYVAL AS CONST ZSTRING PTR) AS ZSTRING PTR

#IF DEFINED (__OPTIMIZE__) AND NOT DEFINED (__cplusplus)
#DEFINE __need_NULL
#INCLUDE ONCE "crt/stddef.bi" '__HEADERS__: stddef.h

#DEFINE gettext(msgid) dgettext (NULL, msgid)
#DEFINE dgettext(domainname, msgid) dcgettext (domainname, msgid, LC_MESSAGES)
#DEFINE ngettext(msgid1, msgid2, n) dngettext (NULL, msgid1, msgid2, n)
#DEFINE dngettext(domainname, msgid1, msgid2, n) dcngettext (domainname, msgid1, msgid2, n, LC_MESSAGES)
#ENDIF ' DEFINED __OPTIM...
#ENDIF ' _LIBINTL_H

END EXTERN ' (h_2_bi -P_oCD option)

#IFDEF __FB_WIN32__
#PRAGMA pop(msbitfields)
#ENDIF

#DEFINE __(_T_) gettext(_T_)

' Translated at 11-01-28 11:16:38, by h_2_bi (version 0.2.0.1,
' released under GPLv3 by Thomas[ dot ]Freiherr[ at ]gmx[ dot ]net)

'   Protocol: libintl.bi
' Parameters:
'                                  Process time [s]: 0.006257920642383397
'                                  Bytes translated: 2184
'                                      Maximum deep: 1
'                                SUB/FUNCTION names: 11
'                                mangled TYPE names: 0
'                                        files done: 0
'                                      files missed: 1
' features.h
'                                                  __FOLDERS__ : 0
'                                                   __MACROS__ : 5
' 1: #define BEGIN_DECLS
' 1: #define END_DECLS
' 11: #define THROW
' 33: #define const const
' 8: #define attribute_format_arg (x)
'                                                  __HEADERS__ : 0
'                                                    __TYPES__ : 0
'                                                __POST_REPS__ : 0
When you get my original code running you can add in to your project folder a file .../project/locale/en/LC_MESSAGES/wwt.mo with the translations (created by PoEdit or a similar tool) and you'll have an english version of the example. Or replace the 'en' subfolder by 'nl' for a dutch version (and addapt the .mo file).
AGS
Posts: 1284
Joined: Sep 25, 2007 0:26
Location: the Netherlands

Post by AGS »

I tested on windows 7. The binary version of GTK+ I downloaded from gnome ftp contained no reference to gthread_init. According to GTK+ documentation calling gthread_init is deprecated. "The GLib threading system is automatically initialized at the start of your program" (at least that's what the Glib developers say).

Some of the glib functions mentioned in the online available glib api (latest version) that have to do with threads are absent from the binary I am using (the one from gnome ftp).

I found an 'interesting' message from the gnumeric developers.
April 2012: Gnumeric 1.11.3 is out. Get it from here!
We do not currently have a Win32; we are waiting for GTK3/Win32 to be ready
'we are waiting for GTK3/Win32' :) Who isn't.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re:

Post by TJF »

AGS wrote:I tested on windows 7. The binary version of GTK+ I downloaded from gnome ftp contained no reference to gthread_init. According to GTK+ documentation calling gthread_init is deprecated. "The GLib threading system is automatically initialized at the start of your program" (at least that's what the Glib developers say).
Yes, it's deprecated since version 2.32. (AFAIK, no FB header for that version yet. The latest GLib header I made 6 month ago is for version 2.31.)

You can check your installed version by printing some EXTERN variables

Code: Select all

#INCLUDE ONCE "glib.bi"

?glib_major_version_; "."; glib_minor_version_; "."; glib_micro_version_
AGS wrote:Some of the glib functions mentioned in the online available glib api (latest version) that have to do with threads are absent from the binary I am using (the one from gnome ftp).
There was a bug in the GLib header. On LINUX libgthread is included by default while on windows it needs to be included explicitly. I fixed it some days ago. Did you use the latest headers from
Post Reply