 |
GTK+ Forums Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
|
|
|
| Author |
Message |
|
|
SlayR101
Joined: 28 Mar 2007 Posts: 2 Location: canada
|
Posted: Wed Mar 28, 2007 9:14 pm Post subject: Creating a Signal in C |
|
|
Hey I am farely new at GTK, and have been trying to create a signal using gtk_signal_new(), I was just wondering if anybody had an example on the proper syntax for this function.
I have created a test program that is supposed to increment all of the values of the labels that i have in the window by pressing a button that runs the gtk_signal_emit_by_name(). I have created this button as a test becuase this function is actually going to be called from somewhere else in the actual program. I want this signal to run a callback routine that increments all the label(for testing purposes).
Here is what i have so far for the test program:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <gtk/gtk.h>
GtkWidget
*input_trigger;
gtk_signal_new("input-trigger",
GTK_RUN_NO_HOOKS|GTK_RUN_LAST,
GtkWidget,
0,
g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
NULL,
0);
int main(int argc,char *argv[])
{
gtk_init(&argc,&argv);
int
num[6],
numt[6]={0,0,0,0,0,0},
i;
GtkWidget
*window,
*label,
*table,
*button,
*button2;
GtkTooltips
*tip;
char labelstring[6][20];
gboolean delete_event(GtkWidget *widget,GdkEvent *event,gpointer data)
{
//gtk_loop=1;
gtk_main_quit();
return(FALSE);
}
void destroy(GtkWidget *widget,gpointer data)
{
gtk_widget_destroy(data);
}
void count_coin(GtkWidget *input_trigger,GdkEvent *event,gpointer data)
{
int i;
char labelstring[6][20];
printf("run 1\n");
for(i=0;i<6;i++)
{
num[i]=1;
numt[i]+=num[i];
}
//table=gtk_table_new(10,10,FALSE);
for(i=0;i<5;i++)
{
sprintf(labelstring[i],"times button #%d was pressed: %d",i,numt[i]);
gtk_label_set_text(GTK_LABEL(label),labelstring[i]);
}
printf("run 2\n");
gtk_signal_emit_stop_by_name(GTK_OBJECT (input_trigger),"input-trigger");
}
void button_test(GtkWidget *button2,GdkEvent *event,gpointer data)
{
gtk_signal_emit_by_name (GTK_OBJECT (input_trigger),"input-trigger");
}
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window),(gchar *)"gtk_sim");
gtk_container_set_border_width(GTK_CONTAINER(window),80);
g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(delete_event),NULL);
table=gtk_table_new(10,10,FALSE);
gtk_container_add(GTK_CONTAINER(window),table);
gtk_table_set_row_spacings(GTK_TABLE(table),2);
gtk_table_set_col_spacings(GTK_TABLE(table),2);
label=gtk_label_new("number of times a button is pressed");
gtk_table_attach(GTK_TABLE(table),label,0,2,0,1,GTK_SHRINK,GTK_SHRINK,0,0);
gtk_widget_show(label);
for(i=0;i<6;i++)
{
sprintf(labelstring[i],"times button #%d was pressed: %d",i,numt[i]);
label=gtk_label_new(labelstring[i]);
gtk_table_attach(GTK_TABLE(table),label,0,2,i+1,i+2,GTK_SHRINK,GTK_SHRINK,0,0);
gtk_widget_show(label);
}
gtk_signal_connect(GTK_OBJECT(input_trigger),"key-press-event",G_CALLBACK(count_coin),NULL);
button=gtk_button_new_with_label("Quit");
g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(delete_event),NULL);
button2=gtk_button_new_with_label("increment all");
g_signal_connect(G_OBJECT(button2),"clicked",G_CALLBACK(button_test),NULL);
tip=gtk_tooltips_new();
gtk_tooltips_set_tip(tip,button,"This will Quit the program",NULL);
gtk_table_attach(GTK_TABLE(table),button,0,2,8,10,GTK_SHRINK,GTK_SHRINK,0,0);
gtk_table_attach(GTK_TABLE(table),button2,0,2,8,11,GTK_SHRINK,GTK_SHRINK,0,0);
gtk_widget_show(button);
gtk_widget_show(button2);
gtk_widget_show (table);
gtk_widget_show (window);
//g_signal_emit_by_name(input_trigger,"button-press-event");
gtk_main();
return 0;
} |
|
| Back to top |
|
 |
openldev Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 386 Location: Fairfax, Virginia
|
Posted: Wed Mar 28, 2007 9:25 pm Post subject: |
|
|
| Code: (Plaintext) | 1
| signal = g_signal_new ("signal-name", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (ObjectClass, prototype), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0); |
Let us say you have an object called Object. klass is the ObjectClass object that is passed to the class initialization function object_class_init(). prototype is a function prototype in ObjectClass. The following is an example prototype:
| Code: (Plaintext) | 1
| void (*prototype) (Object *obj); |
g_cclosure_marchal_VOID__VOID shows that the prototype returns void and accepts no additional parameters besides the object and user data pointer. Then, it is followed by the types of the additional parameters and the number of parameters. |
|
| Back to top |
|
 |
openldev Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 386 Location: Fairfax, Virginia
|
Posted: Wed Mar 28, 2007 9:27 pm Post subject: |
|
|
| I just looked at your code. You are trying to create a signal for the application, which is not what you should be doing. Signals in GTK+ are associated with GObjects. You have to derive your own widget or GObject to create signals. |
|
| Back to top |
|
 |
SlayR101
Joined: 28 Mar 2007 Posts: 2 Location: canada
|
Posted: Wed Mar 28, 2007 9:31 pm Post subject: |
|
|
| Would you know a simple way of doing this? |
|
| Back to top |
|
 |
|
Powered by phpBB © 2001, 2005 phpBB Group CodeBB 1.0 Beta 2
|