my_win.c
#include<stdio.h>
#include<gtk/gtk.h>
#include<string.h>
#include<stdlib.h>
GtkWidget *window;
GtkWidget *table1;
GtkWidget *table2;
GtkWidget *entry;
GtkWidget *button1,*button2,*button3,*button4,*button5,*button6,*button7,*button8;
GtkWidget *button9,*button10,*button11,*button12,*button13,*button14,*button15,*button16;
GtkWidget *vbox1,*vbox2,*vbox3,*vbox4;
float a=0;
float b=0;
char sy[3];
int method=0;
// int hasdot;
// int method;
void my_interface()
{
window=gtk_window_new(GTK_WINDOW_TOPLEVEL); //initial
gtk_window_set_title(GTK_WINDOW(window),"my computer");
gtk_widget_set_uposition(window,300,150);
table1=gtk_table_new(2,1,TRUE);
entry=gtk_entry_new();
table2=gtk_table_new(1,4,TRUE);
button1=gtk_button_new_with_label("1");
button2=gtk_button_new_with_label("4");
button3=gtk_button_new_with_label("7");
button4=gtk_button_new_with_label(".");
button5=gtk_button_new_with_label("2");
button6=gtk_button_new_with_label("5");
button7=gtk_button_new_with_label("8");
button8=gtk_button_new_with_label("0");
button9=gtk_button_new_with_label("3");
button10=gtk_button_new_with_label("6");
button11=gtk_button_new_with_label("9");
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("CE");
vbox1=gtk_vbox_new(TRUE,0);
vbox2=gtk_vbox_new(TRUE,0);
vbox3=gtk_vbox_new(TRUE,0);
vbox4=gtk_vbox_new(TRUE,0);
gtk_container_add(GTK_CONTAINER(window),table1); //load
gtk_table_attach(GTK_TABLE(table1),entry,0,1,0,1,(GtkAttachOptions)(GTK_FILL),(GtkAttachOptions)(0),0,0);
gtk_table_attach(GTK_TABLE(table1),table2,0,1,1,2,(GtkAttachOptions)(GTK_FILL),(GtkAttachOptions)(0),0,0);
gtk_table_attach(GTK_TABLE(table2),vbox1,0,1,0,1,(GtkAttachOptions)(GTK_FILL),(GtkAttachOptions)(0),0,0);
gtk_table_attach(GTK_TABLE(table2),vbox2,1,2,0,1,(GtkAttachOptions)(GTK_FILL),(GtkAttachOptions)(0),0,0);
gtk_table_attach(GTK_TABLE(table2),vbox3,2,3,0,1,(GtkAttachOptions)(GTK_FILL),(GtkAttachOptions)(0),0,0);
gtk_table_attach(GTK_TABLE(table2),vbox4,3,4,0,1,(GtkAttachOptions)(GTK_FILL),(GtkAttachOptions)(0),0,0);
gtk_box_pack_start(GTK_BOX(vbox1),button1,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox1),button2,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox1),button3,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox1),button4,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox2),button5,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox2),button6,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox2),button7,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox2),button8,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox3),button9,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox3),button10,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox3),button11,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox3),button12,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox4),button13,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox4),button14,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox4),button15,TRUE,FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox4),button16,TRUE,FALSE,0);
}
void my_input(GtkWidget *widget,gpointer data)
{
gtk_entry_append_text( GTK_ENTRY(entry),gtk_button_get_label(GTK_BUTTON(widget)));
}
void my_plus(GtkWidget *widget,gpointer data)
{
//strcpy(sy,gtk_button_get_label(GTK_BUTTON(data)));
//gtk_entry_append_text( GTK_ENTRY(entry),sy);
char num[50];
strcpy(num,gtk_entry_get_text(GTK_ENTRY(entry)));
if(a==0)
{
gtk_entry_set_text(GTK_ENTRY(entry),"");
a=atof(num);
}
else
{
b=atof(num);
switch (method)
{
case 1: a=a+b;break;
case 2: a=a-b;break;
case 3: a=a*b;break;
case 4: a=a/b;break;
}
b=0;
gcvt(a,10,num);
gtk_entry_set_text(GTK_ENTRY(entry),num);
a=0;
}
method=1;
}
void my_sub(GtkWidget *widget,gpointer data)
{
//strcpy(sy,gtk_button_get_label(GTK_BUTTON(data)));
//gtk_entry_append_text( GTK_ENTRY(entry),sy);
method=2;
char num[50];
strcpy(num,gtk_entry_get_text(GTK_ENTRY(entry)));
if(a==0)
{
gtk_entry_set_text(GTK_ENTRY(entry),"");
a=atof(num);
}
else
{
b=atof(num);
}
}
void my_mul(GtkWidget *widget,gpointer data)
{
//strcpy(sy,gtk_button_get_label(GTK_BUTTON(data)));
//gtk_entry_append_text( GTK_ENTRY(entry),sy);
method=3;
char num[50];
strcpy(num,gtk_entry_get_text(GTK_ENTRY(entry)));
if(a==0)
{
gtk_entry_set_text(GTK_ENTRY(entry),"");
a=atof(num);
}
else
{
b=atof(num);
}
}
void my_div(GtkWidget *widget,gpointer data)
{
char num[50];
strcpy(num,gtk_entry_get_text(GTK_ENTRY(entry)));
if(a==0)
{
gtk_entry_set_text(GTK_ENTRY(entry),"");
a=atof(num);
}
else
{
b=atof(num);
}
}
void my_clear(GtkWidget *widget,gpointer data)
{
gtk_entry_set_text(GTK_ENTRY(entry),"");
a=0;
b=0;
}
void my_quit(GtkWidget *widget,gpointer data)
{
gtk_main_quit();
}
void my_connect()
{
g_signal_connect(G_OBJECT(button1),"clicked",G_CALLBACK(my_input),NULL);
g_signal_connect(G_OBJECT(button2),"clicked",G_CALLBACK(my_input),NULL);
g_signal_connect(G_OBJECT(button3),"clicked",G_CALLBACK(my_input),NULL);
g_signal_connect(G_OBJECT(button4),"clicked",G_CALLBACK(my_input),NULL);
g_signal_connect(G_OBJECT(button5),"clicked",G_CALLBACK(my_input),NULL);
g_signal_connect(G_OBJECT(button6),"clicked",G_CALLBACK(my_input),NULL);
g_signal_connect(G_OBJECT(button7),"clicked",G_CALLBACK(my_input),NULL);
g_signal_connect(G_OBJECT(button8),"clicked",G_CALLBACK(my_input),NULL);
g_signal_connect(G_OBJECT(button9),"clicked",G_CALLBACK(my_input),NULL);
g_signal_connect(G_OBJECT(button10),"clicked",G_CALLBACK(my_input),NULL);
g_signal_connect(G_OBJECT(button11),"clicked",G_CALLBACK(my_input),NULL);
g_signal_connect(G_OBJECT(button12),"clicked",G_CALLBACK(my_plus),NULL);
g_signal_connect(G_OBJECT(button13),"clicked",G_CALLBACK(my_sub),NULL);
g_signal_connect(G_OBJECT(button14),"clicked",G_CALLBACK(my_mul),NULL);
g_signal_connect(G_OBJECT(button15),"clicked",G_CALLBACK(my_div),NULL);
g_signal_connect(G_OBJECT(button16),"clicked",G_CALLBACK(my_clear),NULL);
//g_signal_connect(G_OBJECT(table2),"clicked",G_CALLBACK(my_quit),NULL);
}
int main(int argc,char* argv[])
{
gtk_init(&argc,&argv);
my_interface();
my_connect();
gtk_widget_show_all(window);
gtk_main();
return 0;
}
gcc my_win.c -o `pkg-config --cflags --libs gtk+-2.0`