[Glade-users] Making labels sensitive to click and setting signal handler



This is a multi-part message in MIME format.
--------------020901010203040509000700
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit


Hi Janmejay,

you can use the following way to set a callback for click
<pre>

   GtkWidget *eventbox;
   GtkWidget *label;
   g_signal_connect ((gpointer) label, "button_press_event",
                     G_CALLBACK (on_label_button_press_event),
                     NULL);
   //be sure to use this if you use only a label,  or you can just add a
   //label to eventbox, eventbox also has this button_press_event
   gtk_label_set_selectable (GTK_LABEL (label), TRUE);

//the callback function
gboolean
on_label_button_press_event           (GtkWidget       *widget,
                                        GdkEventButton  *event,
                                        gpointer         user_data)
{
   //TODO: add you own code here.
   /* you can think that click_event is a button_press_event follow by
      a button_release_event, here you can change a flag that indicates
      the button has been pressed, then process the button_release_event
      to complete a click event callback.
    */

   return FALSE;
}

</pre>

for a full event types of GDK, you can refer to the
appendix B in gtk tutorial 
(http://www.gtk.org/tutorial/app-gdkeventtypes.html)

I've also attached an example code,

Regards,
Sam Jiang

--------------020901010203040509000700
Content-Type: text/x-c;
 name="labeltest.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="labeltest.c"

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
                                                                                
#include <gtk/gtk.h>

/* this program shows how to process button_press_event of eventbox and label 
 * NOTE: THIS PROGRAM IS FOR TEST ONLY, THERE IS ABSOLUTELY NO 
 * WARRANTY FOR THIS PROGRAM.
 *
 * author: jiangjinke_at_163_dot_com_spam
 *
 * filename: labeltest.c
 * compile with: 
 * gcc -Wall -O `pkg-config --cflags gtk+-2.0 glib-2.0 \
 * --libs gtk+-2.0 glib-2.0` labeltest.c -o labeltest
 */
gboolean button_pressed=FALSE;
gboolean
on_Labeltest_delete_event               (GtkWidget       *widget,
                                         GdkEvent        *event,
                                         gpointer         user_data)
{
  gtk_main_quit();
  return FALSE;
}
                                                                                
gboolean
on_Labeltest_destroy_event             (GtkWidget       *widget,
                                        GdkEvent        *event,
                                        gpointer         user_data)
{
  return FALSE;
}
                                                                                
gboolean
on_label_button_press_event           (GtkWidget       *widget,
                                       GdkEventButton  *event,
                                       gpointer         user_data)
{
  g_print("label\n");
  button_pressed=TRUE;
  return FALSE;
}

gboolean
on_label_button_release_event           (GtkWidget       *widget,
                                         GdkEventButton  *event,
                                         gpointer         user_data)
{
  g_print("released\n");
  if(button_pressed==TRUE)
  {
    g_print("clicked\n");
  }
  return FALSE;
}

gboolean
on_eventbox_event                     (GtkWidget       *widget,
                                       GdkEventButton  *event,
                                       gpointer         user_data)
{
  g_print("eventbox\n");
  return FALSE;
}

GtkWidget*
create_Labeltest (void)
{
  GtkWidget *Labeltest;
  GtkWidget *eventbox;
  GtkWidget *label_inside_eventbox;
  GtkWidget *label_outside_eventbox;
  GtkWidget *vbox;

  Labeltest = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (Labeltest), "Label Test");
  gtk_window_set_position (GTK_WINDOW (Labeltest), GTK_WIN_POS_CENTER);
  gtk_window_set_resizable (GTK_WINDOW (Labeltest), FALSE);
  gtk_window_set_destroy_with_parent (GTK_WINDOW (Labeltest), TRUE);

  eventbox = gtk_event_box_new ();
  gtk_widget_show (eventbox);

  vbox = gtk_vbox_new (FALSE, 0);
  gtk_widget_show (vbox);
  gtk_box_pack_start(GTK_BOX(vbox), eventbox, TRUE, TRUE, 0);

  gtk_container_add (GTK_CONTAINER (Labeltest), vbox);

  label_inside_eventbox = gtk_label_new ("label in eventbox!!!");
  gtk_widget_show (label_inside_eventbox);
  gtk_container_add (GTK_CONTAINER (eventbox), label_inside_eventbox);

  label_outside_eventbox = gtk_label_new("label outside box!!!");
  gtk_widget_show (label_outside_eventbox);

  gtk_box_pack_start(GTK_BOX(vbox), label_outside_eventbox, TRUE, TRUE, 0);
  gtk_label_set_selectable (GTK_LABEL (label_outside_eventbox), TRUE);

  gtk_container_add (GTK_CONTAINER (Labeltest), label_outside_eventbox);
    
  g_signal_connect ((gpointer) Labeltest, "delete_event",
                    G_CALLBACK (on_Labeltest_delete_event),
                    NULL);
  g_signal_connect ((gpointer) Labeltest, "destroy_event",
                    G_CALLBACK (on_Labeltest_destroy_event),
                    NULL);
  g_signal_connect ((gpointer) label_outside_eventbox, "button_press_event",
                    G_CALLBACK (on_label_button_press_event),
                    NULL);
  g_signal_connect ((gpointer) label_outside_eventbox, "button_release_event",
                    G_CALLBACK (on_label_button_release_event),
                    NULL);

  g_signal_connect ((gpointer) eventbox, "button_press_event",
                    G_CALLBACK (on_eventbox_event),
                    NULL);

  return Labeltest;
}

int
main (int argc, char *argv[])
{
  GtkWidget *Labeltest;
                                 
  gtk_init (&argc, &argv);
  Labeltest = create_Labeltest ();
  gtk_widget_set_size_request (Labeltest, 200, 100);
  gtk_widget_show (Labeltest);
                                                                                
  gtk_main ();
  return 0;
}


--------------020901010203040509000700--





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