RE: Is it possible to share a callback function in order to detect which a button is clicked?

I cannot see the dm userdata parameter being initialised.

Insert dm->clicked = num; before g_signal_connect in the double-nested  loop.
Then you can test on dm->clicked in the callback to check which
button is clicked.

--- On Fri, 5/2/10, Bokyun Na <bkna kpu ac kr> wrote:

From: Bokyun Na <bkna kpu ac kr>
Subject: RE: Is it possible to share a callback function in order to detect which a button is clicked?
To: gtk-list gnome org
Date: Friday, 5 February, 2010, 5:47 PM

I appreciate for your attention on my problem.
I think we have a little bit different point of view on this problem.

I want to make a callback function on several buttons or even on drawing
For example, when I clicked a button, A, this callback function knows that
button A is clicked and
When button B is clicked the callback function knows that B is clicked.
That is,

void on_window_partition (GtkWidget *widget, dm_t *dm)
  if (button A is clicked) then printf("button A is clicked");
  else if (button B is clicked) then printf ("button B is clicked");

Of course, you can say every callback function for every button. But I
should take only one callback function for several buttons to detect which a
button is clicked.

Still I am trying other ways but no good luck yet.
Please help me.

Thank you.

/* ************** recog_darea.c ****************************************/
#include <gtk/gtk.h>
#include <stdio.h>

typedef struct dm_t_tag {
  int nwin;
  int clicked;
} dm_t;

/* Function Prototypes */

GtkWidget *makeFixedContainer(GtkWidget *window, dm_t *dm);

int main(int argc,char *argv[])
  GtkWidget *window;
  GtkWidget *topbox;

  dm_t dm;

  gtk_init (&argc, &argv); 
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  gtk_object_set_data (GTK_OBJECT (window), "window", window);
  gtk_widget_set_usize (window, 260, 150);
  gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE);
  gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size (GTK_WINDOW (window), 260, 150);
  gtk_signal_connect(GTK_OBJECT(window), "destroy",
GTK_SIGNAL_FUNC(gtk_main_quit), NULL);

  topbox = gtk_hbox_new(FALSE, 0);
  gtk_container_add(GTK_CONTAINER(window), topbox);
  gtk_container_set_border_width(GTK_CONTAINER(topbox), 5);

  makeFixedContainer (topbox, &dm);

  return 0;

void on_window_partition (GtkWidget *widget, dm_t *dm)
  fprintf (stderr, "darea %d is clicked.\n", dm->clicked);

GtkWidget *makeFixedContainer(GtkWidget *window, dm_t *dm)
  int i, j, num;
  GtkWidget *table;
  GtkWidget *afixed;

  GtkWidget *button[2];
  GtkWidget *darea[2];
  GtkWidget *fixed[2]; 

  afixed = gtk_fixed_new ();
  gtk_box_pack_start (GTK_BOX(window), afixed, FALSE, TRUE, 0);
  gtk_widget_show (afixed);

  for(j = 0; j < 1; j++) {
    for(i = 0; i < 2; i++) {
      num = i + 2 * j;
      button[num] = gtk_button_new();
      fixed[num] = afixed;
      darea[num] = gtk_drawing_area_new ();
      gtk_drawing_area_size (GTK_DRAWING_AREA(darea[num]), 100, 80);
      gtk_container_add (GTK_CONTAINER(button[num]), darea[num]);
      gtk_signal_connect (GTK_OBJECT(button[num]), "clicked",
GTK_SIGNAL_FUNC(on_window_partition), dm);

      gtk_fixed_put (GTK_FIXED(fixed[num]), button[num], 120*i, 85*j);
      gtk_widget_realize (darea[num]);

  return table;

-----Original Message-----
From: Siddhesh Poyarekar [mailto:siddhesh poyarekar gmail com]
Sent: Thursday, February 04, 2010 6:01 PM
To: Bokyun Na
Subject: Re: Is it possible to share a callback function in order to detect
which button is clicked?

On Thu, Feb 4, 2010 at 2:21 PM, Bokyun Na <bkna kpu ac kr> wrote:
> Can you add some more code into my example code?
> I might get what you wrote.
> But I did not know and still do not know how I can handle the widget,
> GtkWidget *widget.

You're already reusing the callback, so I assume you want to know what
to do with the GtkWidget. It is a button, so you can simply cast it as
a button using the GTK_BUTTON macro. See line 46 in this code which I
modified from your code:

Siddhesh Poyarekar

gtk-list mailing list
gtk-list gnome org

New Email addresses available on Yahoo!
Get the Email name you've always wanted on the new @ymail and @rocketmail.
Hurry before someone else does!

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]