GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Apr 23, 2014 9:16 am

All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: toggle button help
PostPosted: Tue Feb 24, 2009 4:22 am 
Offline
GTK+ Geek

Joined: Wed Dec 31, 2008 12:44 am
Posts: 73
Location: Fort Worth TX
Im trying to make a toggle button that changes the label of other buttons from lower to upper and i cant figure out how? When you click on it once it will make them upper and when you click the toggle button again it will change them to lower. Anybody got any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2009 2:40 pm 
Offline
GTK+ Guru

Joined: Tue Feb 05, 2008 8:39 am
Posts: 130
I didn't realy understand what your problem is. You can create toggle button with gtk_toggle_button_new_with_label, then set your handler of signal "toggled" and in this handler change label text with gtk_label_set_text. Isn't it?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2009 3:52 am 
Offline
GTK+ Geek

Joined: Wed Dec 31, 2008 12:44 am
Posts: 73
Location: Fort Worth TX
no its gtk_button_set_label but i cant figure out how to make it change the buttons only the toggle button heres what i tried:

Code:

void activate_caps(GtkWidget *lettera)
{
gtk_button_set_label(GTK_BUTTON(lettera),"A");
}

g_signal_connect(G_OBJECT(togglebutton), "toggled",
        G_CALLBACK(activate_caps), G_OBJECT(window));


But when i click on the toggle button it just changes the toggle buttons text how do i fix this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2009 9:12 am 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

I'm still not sure if I understand you correctly, but if you want to switch toggle button's label case whenever the "toggled" signal is emitted, this is how I would write my callback function:

Code:
void cb_toggled( GtkToggleButton *button, gpointer data)
{
    gchar *string;

    string = g_utf8_strup( gtk_button_get_label( GTK_BUTTON( button ), -1 );
    gtk_button_set_label( GTK_BUTTON( button ), string );
    g_free( string );
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 26, 2009 8:20 pm 
Offline
GTK+ Geek

Joined: Wed Dec 31, 2008 12:44 am
Posts: 73
Location: Fort Worth TX
Well i want to make it to when you click the toggle button it will change the text of other buttons like (a,b,c,d,e,f) and change them to (A,B,C,D,E,F) but i cant make a callback that does it. and i couldnt get that example to compile.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2009 12:27 am 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hello.

Sorry for the missing ) in previous code snippet. I wrote the code directly in forum's input field and made a mistake.

Anyhow, here is a complete application that does what you need (I compiled this one;)

Code:
#include <gtk/gtk.h>

#define NO_BUTTONS 5

static void
cb_toggled( GtkToggleButton  *button,
         GtkWidget       **data )
{
   static gboolean  flag = FALSE;
   gchar           *string;
   gint             i;

   for( i = 0; i < NO_BUTTONS; i++ )
   {
      const gchar *label = gtk_button_get_label( GTK_BUTTON( data[i] ) );

      string = ( flag ? g_utf8_strdown( label, -1 ) : g_utf8_strup( label, -1 ) );
      gtk_button_set_label( GTK_BUTTON( data[i] ), string );
      g_free( string );
   }

   flag = ( flag ? FALSE : TRUE );
}

int
main( int    argc,
     char **argv )
{
   GtkWidget *window;
   GtkWidget *vbox;
   GtkWidget *toggle;
   GtkWidget *buttons[NO_BUTTONS];
   gint       i;

   gtk_init( &argc, &argv );

   window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
   gtk_container_set_border_width( GTK_CONTAINER( window ), 10 );
   g_signal_connect( G_OBJECT( window ), "destroy",
                 G_CALLBACK( gtk_main_quit ), NULL );

   vbox = gtk_vbox_new( TRUE, 5 );
   gtk_container_add( GTK_CONTAINER( window ), vbox );

   for( i = 0; i < NO_BUTTONS; i++ )
   {
      gchar label[] = { 97 + i, '\0' };

      buttons[i] = gtk_button_new_with_label( label );
      gtk_box_pack_start( GTK_BOX( vbox ), buttons[i], TRUE, TRUE, 0 );
   }

   toggle = gtk_toggle_button_new_with_label( "Toggle" );
   g_signal_connect( G_OBJECT( toggle ), "toggled",
                 G_CALLBACK( cb_toggled ), buttons );
   gtk_box_pack_start( GTK_BOX( vbox ), toggle, TRUE, TRUE, 0 );

   gtk_widget_show_all( window );

   gtk_main();

   return( 0 );
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2009 12:53 am 
Offline
GTK+ Geek

Joined: Wed Dec 31, 2008 12:44 am
Posts: 73
Location: Fort Worth TX
Thats a great example. Sorry to be a hassle but do you know a way to change the button labels individually without the loop?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2009 8:00 am 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

This is one way of doing it (I'm only changing button labeled 'c').

Code:
#include <gtk/gtk.h>

#define NO_BUTTONS 5

typedef struct _Data Data;
struct _Data
{
   GtkWidget *a;
   GtkWidget *b;
   GtkWidget *c;
   GtkWidget *d;
   GtkWidget *e;
};

static void
cb_toggled( GtkToggleButton  *button,
            Data             *data )
{
    gpointer  flag;
    gchar    *string;
    gint      i;
    const gchar *label;
   
   label = gtk_button_get_label( GTK_BUTTON( data->c ) );
   flag = g_object_get_data( G_OBJECT( data->c ), "flag" );

    string = ( flag ? g_utf8_strdown( label, -1 ) : g_utf8_strup( label, -1 ) );
    gtk_button_set_label( GTK_BUTTON( data->c ), string );
    g_free( string );

    g_object_set_data( G_OBJECT( data->c ), "flag",
                  ( flag ? NULL : GINT_TO_POINTER( 1 ) ) );
}

int
main( int    argc,
      char **argv )
{
    GtkWidget *window;
    GtkWidget *vbox;
    GtkWidget *toggle;
   Data       data;
    gint       i;

    gtk_init( &argc, &argv );

    window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
    gtk_container_set_border_width( GTK_CONTAINER( window ), 10 );
    g_signal_connect( G_OBJECT( window ), "destroy",
                      G_CALLBACK( gtk_main_quit ), NULL );

    vbox = gtk_vbox_new( TRUE, 5 );
    gtk_container_add( GTK_CONTAINER( window ), vbox );

    for( i = 0; i < NO_BUTTONS; i++ )
    {
        gchar      label[] = { 97 + i, '\0' };
      GtkWidget *button;

        button = gtk_button_new_with_label( label );
      g_object_set_data( G_OBJECT( button ), "flag", NULL );
        gtk_box_pack_start( GTK_BOX( vbox ), button, TRUE, TRUE, 0 );

      switch( i )
      {
         case 0:
            data.a = button;
            break;
         case 1:
            data.b = button;
            break;
         case 2:
            data.c = button;
            break;
         case 3:
            data.d = button;
            break;
         case 4:
            data.e = button;
            break;
      }
    }

    toggle = gtk_toggle_button_new_with_label( "Toggle" );
    g_signal_connect( G_OBJECT( toggle ), "toggled",
                      G_CALLBACK( cb_toggled ), &data );
    gtk_box_pack_start( GTK_BOX( vbox ), toggle, TRUE, TRUE, 0 );

    gtk_widget_show_all( window );

    gtk_main();

    return( 0 );
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 6:42 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
Just a comment on this snippet:
Code:
flag = ( flag ? FALSE : TRUE );

For those who don't like the wonderful ternary operator, it could also be accomplished shortly by doing this:
Code:
flag = !flag;


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group