GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Oct 24, 2014 11:27 am

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Callback will not set widget sentative
PostPosted: Tue Oct 02, 2012 6:33 am 
Offline
Familiar Face

Joined: Sat Mar 03, 2012 2:45 am
Posts: 18
Hello Everyone,

I have a callback on a button that changes a notebook page and sets the menu bar and buttonbox sensitive to true. The Notebook changes pages but i can't get the set sensitive on the menubar and buttonbox. Any Ideas? I am using GTK3, Ubuntu 12.04.1, I get no error on compile and on the terminal.

main.c
Code:
#include <config.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include "main.h"

GtkWidget *window;
GtkBuilder *builder;
GtkWidget *btn_login;
GtkWidget *menubar;
GtkWidget *buttonbox;
GtkWidget *notebook;

void btn_login_clicked_cb (GtkWidget *btn_login, gpointer data)
{
   menubar = _menubar ();
   buttonbox = _buttonbox ();
   notebook = _notebook ();
   
   gtk_widget_set_sensitive (GTK_WIDGET (menubar), TRUE);
   gtk_widget_set_sensitive (GTK_WIDGET (buttonbox), TRUE);
   gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), 1);
}

int main (int argc, char *argv[])
{

   gtk_init (&argc, &argv);

   window = _window ();
   gtk_widget_show_all (window);

   gtk_main ();

   return 0;
}


main.h
Code:
#include <config.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>


GtkWidget *window;
GtkBuilder *builder;
GtkWidget *btn_login;
GtkWidget *menubar;
GtkWidget *buttonbox;
GtkWidget *notebook;
GError* error = NULL;


void destroy (GtkWidget *widget, gpointer data)
{
   gtk_main_quit ();
}

void get_builder (void)
{
   builder = gtk_builder_new ();
   gtk_builder_add_from_file (builder, "src/osbuilder.ui", &error);
}

static GtkWidget* _window (void)
{
   get_builder();
   window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
   gtk_builder_connect_signals (builder, NULL);
   
   return window;
}

static GtkWidget* _btn_login (void)
{
   get_builder();
   btn_login = GTK_WIDGET (gtk_builder_get_object (builder, "btn_login"));
   gtk_builder_connect_signals (builder, NULL);

   return btn_login;
}

static GtkWidget* _menubar (void)
{
   get_builder();
   menubar = GTK_WIDGET (gtk_builder_get_object (builder, "menubar"));
   gtk_builder_connect_signals (builder, NULL);

   return menubar;
}

static GtkWidget* _buttonbox (void)
{
   get_builder();
   buttonbox = GTK_WIDGET (gtk_builder_get_object (builder, "buttonbox"));
   gtk_builder_connect_signals (builder, NULL);

   return buttonbox;
}

static GtkWidget* _notebook (void)
{
   get_builder();
   notebook = GTK_WIDGET (gtk_builder_get_object (builder, "notebook"));
   gtk_builder_connect_signals (builder, NULL);

   gtk_widget_show_all (GTK_WIDGET(notebook));

   return notebook;
}


Thank You Everyone.


Top
 Profile  
 
 Post subject: Re: Callback will not set widget sentative
PostPosted: Tue Oct 02, 2012 7:53 am 
Offline
Never Seen the Sunlight

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

I'm not sure you're aware of it, but during your application life cycle, you create not one but several distinct copies of your interface (each call to _-prefixed method creates one). Did you read any tutorial on how to use Glade from GTK+? Also, placing function definition in header file is something you should really avoid doing.

Anyhow, I remodeled your code into form that is usually used when developing applications using glade. Standard pattern of usage is: create builder, construct interface, store needed widget pointers and connect signals, destroy builder.

Code:
#include <gtk/gtk.h>

typedef struct _Data
{
  GtkWidget *window,
            *btn_login,
            *menubar,
            *buttonbox,
            *notebook;
}
Data;


static gboolean
create_gui (Data *data)
{
  GtkBuilder builder;
  GError *error = NULL;
  gboolean succ = TRUE;

  builder = gtk_builder_new ();
  if (!gtk_builder_add_from_file (builder, "osbuilder.ui", &error))
    {
      /* Report error here */
      g_error_free (error);
      succ = FALSE;
    }
  gtk_builder_connect_signals (builder, data);

#define GW(name) \
  data->name = GTK_WIDGET (gtk_builder_get_object (builder, #name));\
  if (!data->name)\
    succ = FALSE
  GW (window);
  GW (btn_login);
  GW (menubar);
  GW (buttonbox);
  GW (notebook);
#undef GW

  g_object_unref (builder);

  return succ;
}

G_MODULE_EXPORT void
btn_login_clicked_cb (GtkButton *button,
                      Data      *data)
{
  gtk_widget_set_sensitive (data->menubar, TRUE);
  gtk_widget_set_sensitive (data->buttonbox, TRUE);
  gtk_notebook_set_current_page (GTK_NOTEBOOK (data->notebook), 1);
}

int
main (int    argc,
      char **argv)
{
  Data *data;

  gtk_init (&argc, &argv);

  data = g_slice_new (Data);

  if (!create_gui (data))
    return 0;

  gtk_widget_show (data->window);

  gtk_main ();

  return 0;
}


Cheers,
Tadej


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 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