Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-13-2004, 12:32 PM   #1
LQ Newbie
Registered: Dec 2003
Posts: 3

Rep: Reputation: 0
GTK+ and c question

i am currently teaching myself C and gtk at the same time by writing a simple/silly program for converting human years into cat or dog years. my question is how do i get the address/pointer/hopeyouknowehatimean of a gtkwidget radio button in a function so i can check to see if it is active? here is the code:

#include <stdio.h>
#include <stdlib.h> 
#include <gtk/gtk.h>

#define DOGYEARS 7
#define CATYEARS 4

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

static void to_dog( GtkWidget *widget,
		    GtkWidget *text )
  const gchar *entry_text;
  gchar *dog_years;
  float years;

  entry_text = gtk_entry_get_text (GTK_ENTRY (text));
  printf ("Text: %s\n", entry_text);
  years = (atof (entry_text)) * DOGYEARS;
  sprintf (dog_years, "%.0f", years);
  gtk_entry_set_text (text, dog_years);
  printf ("Years: %.0f\n", years);

int main( int argc,
	  char *argv[] )
  GtkWidget *window;
  GtkWidget *vbox;
  GtkWidget *vboxr;
  GtkWidget *button;
  GtkWidget *text;
  GtkWidget *radiocat;
  GtkWidget *radiodog;
  GSList *group;

  gtk_init (&argc, &argv);

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

  vbox = gtk_vbox_new (TRUE, 0);
  gtk_widget_show (vbox);
  vboxr = gtk_vbox_new (TRUE,0);
  gtk_widget_show (vboxr);

  text = gtk_entry_new();
  gtk_widget_show (text);

  button = gtk_button_new_with_label ("To dog years");
  g_signal_connect (G_OBJECT (button), "clicked",
		    G_CALLBACK (to_dog), (gpointer) text);
  gtk_widget_show (button);

  radiocat = gtk_radio_button_new_with_label (NULL, "Cat years");
  gtk_box_pack_start (GTK_BOX (vboxr), radiocat, FALSE, FALSE, 0);
  gtk_widget_show (radiocat);

  group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (radiocat));
  radiodog = gtk_radio_button_new_with_label (group, "Dog years");
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (radiodog), TRUE);
  gtk_box_pack_start (GTK_BOX (vboxr), radiodog, FALSE, FALSE, 0);
  gtk_widget_show (radiodog);
  gtk_widget_show (vboxr);

  gtk_box_pack_start (GTK_BOX (vbox), text, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), vboxr, FALSE, FALSE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);

  gtk_container_add (GTK_CONTAINER (window), vbox);

  gtk_widget_show (window);


  return 0;
i need to check the state of the radiocat widget from the function to_dog using the function "gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (radiocat))" but as i said, im not sure how to access this variable from the function to_dog


Old 04-13-2004, 12:51 PM   #2
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
IIRC, the last argument to g_signal_connect() is an arbitrary pointer which gets passed to the callback function. You can have it be a pointer to a structure with whatever stuff the function needs to run, including the radio button widget.

The other (lazy, bad) way is to just make the radio button widget a static global variable.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about gtk hardstyleFX Linux - Newbie 1 09-30-2005 04:51 PM
Question about C - GTK alitrix Programming 4 11-12-2004 04:47 AM
Newbie Question - What is GTK Exactly? Zeraxeal Slackware 4 03-24-2004 07:27 PM
GTK-Gnutella question Kensai Linux - Software 2 02-08-2004 06:13 PM
GTK question starbreaker Linux - Software 1 02-16-2002 12:55 PM

All times are GMT -5. The time now is 12:42 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration