GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Width Of Button

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
krisri
Familiar Face


Joined: 09 Nov 2009
Posts: 5

PostPosted: Tue Nov 24, 2009 2:51 pm    Post subject: Width Of Button Reply with quote

Hi
I have 3 Buttons of one window,how can i know the width and height of each button.

rgds
kris
Back to top
LGV
Familiar Face


Joined: 31 Dec 2009
Posts: 5
Location: Argentina

PostPosted: Fri Jan 01, 2010 6:54 pm    Post subject: Reply with quote

I'm not sure if this is the correct way of doing it, but take look at the "allocation" member of the widget, it has the following variables:
Code: (C)
1
2
3
4
5

GtkWidget* button;
button->allocation.height;
button->allocation.width;
Back to top
tadeboro
Never Seen the Sunlight


Joined: 23 Jul 2008
Posts: 2114
Location: Slovenia

PostPosted: Fri Jan 01, 2010 7:28 pm    Post subject: Reply with quote

Hello.

Method that LGV proposed is the right one. There is only one thing I would like to add. Since GTK+-3.0, all widgets will be opaque structures, which means that direct access to structure fields will not be possible anymore. I would recommend adding some #if preprocessor statements that will check for GTK+ version and if they detect version >= 2.18, you should use gtk_widget_get_allocation() instead.

Tadej
Back to top
dreblen
Never Seen the Sunlight


Joined: 14 Jun 2007
Posts: 936
Location: Falun, WI USA

PostPosted: Sun Jan 03, 2010 12:59 am    Post subject: Reply with quote

Just to add to the #if recommendation, I would suggest that you do something like the following once,
rather than doing and #if check every time you want an allocation (note that this is essentially the content of the gtk_widget_get_allocation function):
Code: (C)
1
2
3
4
5
6
#if !GTK_CHECK_VERSION(2, 18, 0)
    #define gtk_widget_get_allocation(wi, al) \
        g_return_if_fail(GTK_IS_WIDGET(wi)); \
        g_return_if_fail(al != NULL); \
        *al = GTK_WIDGET(wi)->allocation
#endif /* !GTK_CHECK_VERSION(2, 18, 0) */

See here for more information on GTK_CHECK_VERSION.
Back to top
tadeboro
Never Seen the Sunlight


Joined: 23 Jul 2008
Posts: 2114
Location: Slovenia

PostPosted: Sun Jan 03, 2010 9:22 am    Post subject: Reply with quote

Hello.

dreblen wrote:
Code: (C)
1
2
3
4
5
6
#if !GTK_CHECK_VERSION(2, 18, 0)
    #define gtk_widget_get_allocation(wi, al) \
        g_return_if_fail(GTK_IS_WIDGET(wi)); \
        g_return_if_fail(al != NULL); \
        *al = GTK_WIDGET(wi)->allocation
#endif /* !GTK_CHECK_VERSION(2, 18, 0) */

This macro will cause code to malfunction if any of the assertions fail. Take for example this code, that assumes that last parameter passed in is valid GtkWidget:
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
static gboolean
cb_button_press( GtkWidget      *widget,
                 GdkEventButton *event,
                 GtkWidget      *data )
{
    GtkAllocation allocation;

    gtk_widget_get_allocation( data, &allocation );

    /* Some important functionality here */

   
return( FALSE );
}

If last parameter is not a widget by any chance, this code will still perform properly with GTK+ >= 2.18.0, but if you macro steps in, the whole cb_button_press() callback will be terminated prematurely and will even return with no value.

One way of writing this macro to emulate function would be like this:
Code: (C)
1
2
3
4
5
6
#if ! GTK_CHECK_VERSION( 2, 18, 0 )
    #define gtk_widget_get_allocation( wi, al ) \
        if( ( al ) ) \
            if( GTK_IS_WIDGET( ( wi ) ) ) \
                *( al ) = GTK_WIDGET( ( wi ) )->allocation
#endif


The way I would handle this situation would be to use autotools to detect GTK+ version and conditionally compile in a file with substitute functions for missing API. But since this is quite demanding stuff, I opted for single #if preprocessor in this case.

Tadej
Back to top
dreblen
Never Seen the Sunlight


Joined: 14 Jun 2007
Posts: 936
Location: Falun, WI USA

PostPosted: Sun Jan 03, 2010 11:34 pm    Post subject: Reply with quote

Thank you for pointing out my error.
Now that I've gone back and looked at this, I realize that gcc would have even warned me about this if I'd have had -Wall on like I usually do.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP