[C2hs] Re: [Gtk2hs-users] question on structs with compound members

Axel Simon A.Simon at kent.ac.uk
Wed Oct 22 21:26:16 EDT 2003


On Wed, Oct 22, 2003 at 02:51:15PM +0100, Duncan Coutts wrote:
> All,
> 
> So I'm trying to wrap this structure:
> 
> struct GtkSourceTagStyle {
> 
> 	/* readonly */
> 	gboolean is_default;
> 
> 	guint mask;
> 	
> 	GdkColor foreground;
> 	GdkColor background;
> 	
> 	gboolean italic;
> 	gboolean bold;
> 	gboolean underline;
> 	gboolean strikethrough;
> 
> 	/* Reserved for future expansion */
> 	guint8 reserved[16];	
> };
> 
> My problem is with the GdkColor members foreground & background. They
> are themselves structures so c2hs cannot make {#get #} mappings for
> them. Of course Color has already been wrapped (in
> gtk2hs/gtk/general/Structs.hsc) and is a member of the Storable class so
> all we need is to get a pointer to the member to be able to apply
> peek/poke to it. This is not hard if we manually edit the .hs file
> produced from the .chs binding file:

I kinda object to use c2hs for marshalling structures. When I wrote the
marshalling code for graphics contexts for gtk+hs, I used c2hs only to
find out that it calculates the wrong offsets. Moreover, it is doomed to 
produce the wrong offsets in some circumstances with bitfields. Windows 
uses a different layout than Unix, even if you use gcc on both platforms. 
(gcc behaves differently so that you can use the precompiled Microsoft 
libraries).

So I recommend to create a new .hsc file that does the marshalling of that 
structure. The foreground and background color is then simply another call 
to peek and poke, just like in the marshalling code of GCValues:

instance Storable GCValues where
  sizeOf _ = #{const sizeof(GdkGCValues)}
  alignment _ = alignment (undefined::Color)
  peek ptr = do
    foreground_ <- peek (#{ptr GdkGCValues, foreground} ptr)
    background_ <- peek (#{ptr GdkGCValues, background} ptr)
  ....

(this is from gtk/general/Structs.hsc)

Hope this helps,
Axel.




More information about the C2hs mailing list