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



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
areas.
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.
Bkna.


/* ************** 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);

  gtk_widget_show_all(window);
  gtk_main();
  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:

http://pastebin.com/m19291a5d


-- 
Siddhesh Poyarekar
http://siddhesh.in







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