I am writing a calculator program and I have a basic problem. I have written callback function for 2 button and sent one entry and one value to callback using struct but now I have problem with functionvoid hi(GtkWidget *widget,gpointer data).i want if any button is pressed its corresponding character be written in the entry but now I don’t know what code I can write that send b1->num=X for each button you can compile and run the program to see where is my problem.
I appreciate from all friends who will help me.
Code:
#include <gtk/gtk.h>
#include <iostream>
#include <string>
#include <cctype>
struct hamid{
GtkWidget *entry;
const gchar *num;
}*b1;
void hi(GtkWidget *widget,gpointer data){
gtk_entry_set_text(GTK_ENTRY(b1->entry),b1->num);
}
int main(int argc,char *argv[]){
gtk_init(&argc,&argv);
GtkWidget *window;
GtkWidget *table;
GtkWidget *button1;
GtkWidget *button2;
GtkWidget *button3;
GtkWidget *button4;
GtkWidget *button5;
GtkWidget *button6;
GtkWidget *button7;
GtkWidget *button8;
GtkWidget *button9;
GtkWidget *button10;
GtkWidget *button11;
GtkWidget *button12;
GtkWidget *button13;
GtkWidget *button14;
GtkWidget *button15;
GtkWidget *button16;
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
table=gtk_table_new(5,4,TRUE);
b1=(hamid*)g_malloc(sizeof(hamid));
b1->entry=gtk_entry_new();
b1->num="1";
button1=gtk_button_new_with_label("1");
button2=gtk_button_new_with_label("2");
button3=gtk_button_new_with_label("3");
button4=gtk_button_new_with_label("4");
button5=gtk_button_new_with_label("5");
button6=gtk_button_new_with_label("6");
button7=gtk_button_new_with_label("7");
button8=gtk_button_new_with_label("8");
button9=gtk_button_new_with_label("9");
button10=gtk_button_new_with_label("0");
button11=gtk_button_new_with_label("=");
button12=gtk_button_new_with_label("+");
button13=gtk_button_new_with_label("-");
button14=gtk_button_new_with_label("/");
button15=gtk_button_new_with_label("*");
button16=gtk_button_new_with_label(".");
gtk_window_set_resizable(GTK_WINDOW(window),FALSE);
gtk_container_add(GTK_CONTAINER(window),table);
gtk_table_attach_defaults(GTK_TABLE(table),b1->entry,1,3,0,1);
gtk_table_attach_defaults(GTK_TABLE(table),button1,0,1,1,2);
gtk_table_attach_defaults(GTK_TABLE(table),button2,1,2,1,2);
gtk_table_attach_defaults(GTK_TABLE(table),button3,2,3,1,2);
gtk_table_attach_defaults(GTK_TABLE(table),button4,3,4,1,2);
gtk_table_attach_defaults(GTK_TABLE(table),button5,0,1,2,3);
gtk_table_attach_defaults(GTK_TABLE(table),button6,1,2,2,3);
gtk_table_attach_defaults(GTK_TABLE(table),button7,2,3,2,3);
gtk_table_attach_defaults(GTK_TABLE(table),button8,3,4,2,3);
gtk_table_attach_defaults(GTK_TABLE(table),button9,0,1,3,4);
gtk_table_attach_defaults(GTK_TABLE(table),button10,1,2,3,4);
gtk_table_attach_defaults(GTK_TABLE(table),button11,2,3,3,4);
gtk_table_attach_defaults(GTK_TABLE(table),button12,3,4,3,4);
gtk_table_attach_defaults(GTK_TABLE(table),button13,0,1,4,5);
gtk_table_attach_defaults(GTK_TABLE(table),button14,1,2,4,5);
gtk_table_attach_defaults(GTK_TABLE(table),button15,2,3,4,5);
gtk_table_attach_defaults(GTK_TABLE(table),button16,3,4,4,5);
g_signal_connect(G_OBJECT(button1),"clicked",G_CALLBACK(hi),(gpointer)b1);
b1->num="2";
g_signal_connect(G_OBJECT(button2),"clicked",G_CALLBACK(hi),(gpointer)b1);
gtk_widget_show(window);
gtk_widget_show(table);
gtk_widget_show(button1);
gtk_widget_show(button2);
gtk_widget_show(button3);
gtk_widget_show(button4);
gtk_widget_show(button5);
gtk_widget_show(button6);
gtk_widget_show(button7);
gtk_widget_show(button8);
gtk_widget_show(button9);
gtk_widget_show(button10);
gtk_widget_show(button11);
gtk_widget_show(button12);
gtk_widget_show(button13);
gtk_widget_show(button14);
gtk_widget_show(button15);
gtk_widget_show(button16);
gtk_widget_show(b1->entry);
gtk_main();
return 0;
}